Skip to content

Commit 5b3433b

Browse files
authored
Merge pull request #27 from alerque/only-older
Add option to ignore files that are older than history
2 parents 134302e + c8b227c commit 5b3433b

File tree

4 files changed

+48
-13
lines changed

4 files changed

+48
-13
lines changed

README.md

+13-10
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,24 @@ For more usage see the `--help` output:
2626

2727
```console
2828
$ git-warp-time --help
29-
CLI utility that resets the timestamps of files in a Git repository working directory to the exact
30-
timestamp of the last commit which modified each file
29+
CLI utility that resets the timestamps of files in a Git repository working
30+
directory to the exact timestamp of the last commit which modified each file
3131

3232
Usage: git-warp-time [OPTIONS] [PATHS]...
3333

3434
Arguments:
35-
[PATHS]... Optional list of paths to operate on instead of default which is all files tracked by
36-
Git
35+
[PATHS]... Optional list of paths to operate on instead of default which is
36+
all files tracked by Git
3737

3838
Options:
39-
-d, --dirty Include files tracked by Git but modifications in the working tee
40-
-i, --ignored Include files tracked by Git but also ignored
41-
-q, --quiet Don't print any output about files touched or skipped
42-
-h, --help Print help
43-
-V, --version Print version
39+
-d, --dirty Include files tracked by Git but modifications in the
40+
working tee
41+
-i, --ignored Include files tracked by Git but also ignored
42+
-o, --ignore-older Only touch files that are newer than their history, ignore
43+
ones that are older
44+
-q, --quiet Don't print any output about files touched or skipped
45+
-h, --help Print help
46+
-V, --version Print version
4447
```
4548

4649
## Library Usage
@@ -49,7 +52,7 @@ In your `Cargo.toml` file.
4952

5053
```toml
5154
[dependencies]
52-
git-warp-time = "0.7"
55+
git-warp-time = "0.8"
5356
```
5457

5558
Then use the crate functions and types in your project something like this:

src/bin/git-warp-time.rs

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ fn main() -> Result<()> {
4444
let mut opts = git_warp_time::Options::new()
4545
.dirty(matches.get_flag("dirty"))
4646
.ignored(matches.get_flag("ignored"))
47+
.ignore_older(matches.get_flag("ignore_older"))
4748
.verbose(!matches.get_flag("quiet"));
4849
if matches.contains_id("paths") {
4950
let mut paths: FileSet = FileSet::new();

src/cli.rs

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ pub struct Cli {
1313
#[clap(short, long)]
1414
pub ignored: bool,
1515

16+
/// Only touch files that are newer than their history, ignore ones that are older
17+
#[clap(short = 'o', long)]
18+
pub ignore_older: bool,
19+
1620
/// Don't print any output about files touched or skipped
1721
#[clap(short, long)]
1822
pub quiet: bool,

src/lib.rs

+30-3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ pub struct Options {
5656
paths: Option<FileSet>,
5757
dirty: bool,
5858
ignored: bool,
59+
ignore_older: bool,
5960
verbose: bool,
6061
}
6162

@@ -74,6 +75,7 @@ impl Options {
7475
paths: None,
7576
dirty: false,
7677
ignored: false,
78+
ignore_older: false,
7779
verbose: false,
7880
}
7981
}
@@ -84,6 +86,7 @@ impl Options {
8486
paths: self.paths.clone(),
8587
dirty: flag,
8688
ignored: self.ignored,
89+
ignore_older: self.ignore_older,
8790
verbose: self.verbose,
8891
}
8992
}
@@ -94,6 +97,18 @@ impl Options {
9497
paths: self.paths.clone(),
9598
dirty: self.dirty,
9699
ignored: flag,
100+
ignore_older: self.ignore_older,
101+
verbose: self.verbose,
102+
}
103+
}
104+
105+
/// Whether or not to touch files older than history, default is true
106+
pub fn ignore_older(&self, flag: bool) -> Options {
107+
Options {
108+
paths: self.paths.clone(),
109+
dirty: self.dirty,
110+
ignored: self.ignored,
111+
ignore_older: flag,
97112
verbose: self.verbose,
98113
}
99114
}
@@ -104,6 +119,7 @@ impl Options {
104119
paths: self.paths.clone(),
105120
dirty: self.dirty,
106121
ignored: self.ignored,
122+
ignore_older: self.ignore_older,
107123
verbose: flag,
108124
}
109125
}
@@ -114,6 +130,7 @@ impl Options {
114130
paths: input,
115131
dirty: self.dirty,
116132
ignored: self.ignored,
133+
ignore_older: self.ignore_older,
117134
verbose: self.verbose,
118135
}
119136
}
@@ -236,11 +253,16 @@ fn diff_affects_oid(diff: &Diff, oid: &Oid, touchable_path: &mut Utf8PathBuf) ->
236253
})
237254
}
238255

239-
fn touch_if_older(path: Utf8PathBuf, time: i64, verbose: bool) -> Result<bool> {
256+
fn touch_if_time_mismatch(
257+
path: Utf8PathBuf,
258+
time: i64,
259+
verbose: bool,
260+
ignore_older: bool,
261+
) -> Result<bool> {
240262
let commit_time = FileTime::from_unix_time(time, 0);
241263
let metadata = fs::metadata(&path).context(IoSnafu)?;
242264
let file_mtime = FileTime::from_last_modification_time(&metadata);
243-
if file_mtime != commit_time {
265+
if file_mtime > commit_time || (!ignore_older && file_mtime < commit_time) {
244266
filetime::set_file_mtime(&path, commit_time).context(IoSnafu)?;
245267
if verbose {
246268
println!("Rewound the clock: {path}");
@@ -296,7 +318,12 @@ fn process_touchables(repo: &Repository, touchables: FileSet, opts: &Options) ->
296318
let affected = diff_affects_oid(&diff, oid, touchable_path);
297319
if affected {
298320
let time = commit.time().seconds();
299-
if let Ok(true) = touch_if_older(touchable_path.to_path_buf(), time, opts.verbose) {
321+
if let Ok(true) = touch_if_time_mismatch(
322+
touchable_path.to_path_buf(),
323+
time,
324+
opts.verbose,
325+
opts.ignore_older,
326+
) {
300327
touched
301328
.write()
302329
.unwrap()

0 commit comments

Comments
 (0)