Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to ignore files that are older than history #27

Merged
merged 5 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,24 @@ For more usage see the `--help` output:

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

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

Arguments:
[PATHS]... Optional list of paths to operate on instead of default which is all files tracked by
Git
[PATHS]... Optional list of paths to operate on instead of default which is
all files tracked by Git

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

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

```toml
[dependencies]
git-warp-time = "0.7"
git-warp-time = "0.8"
```

Then use the crate functions and types in your project something like this:
Expand Down
1 change: 1 addition & 0 deletions src/bin/git-warp-time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ fn main() -> Result<()> {
let mut opts = git_warp_time::Options::new()
.dirty(matches.get_flag("dirty"))
.ignored(matches.get_flag("ignored"))
.ignore_older(matches.get_flag("ignore_older"))
.verbose(!matches.get_flag("quiet"));
if matches.contains_id("paths") {
let mut paths: FileSet = FileSet::new();
Expand Down
4 changes: 4 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ pub struct Cli {
#[clap(short, long)]
pub ignored: bool,

/// Only touch files that are newer than their history, ignore ones that are older
#[clap(short = 'o', long)]
pub ignore_older: bool,

/// Don't print any output about files touched or skipped
#[clap(short, long)]
pub quiet: bool,
Expand Down
33 changes: 30 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub struct Options {
paths: Option<FileSet>,
dirty: bool,
ignored: bool,
ignore_older: bool,
verbose: bool,
}

Expand All @@ -74,6 +75,7 @@ impl Options {
paths: None,
dirty: false,
ignored: false,
ignore_older: false,
verbose: false,
}
}
Expand All @@ -84,6 +86,7 @@ impl Options {
paths: self.paths.clone(),
dirty: flag,
ignored: self.ignored,
ignore_older: self.ignore_older,
verbose: self.verbose,
}
}
Expand All @@ -94,6 +97,18 @@ impl Options {
paths: self.paths.clone(),
dirty: self.dirty,
ignored: flag,
ignore_older: self.ignore_older,
verbose: self.verbose,
}
}

/// Whether or not to touch files older than history, default is true
pub fn ignore_older(&self, flag: bool) -> Options {
Options {
paths: self.paths.clone(),
dirty: self.dirty,
ignored: self.ignored,
ignore_older: flag,
verbose: self.verbose,
}
}
Expand All @@ -104,6 +119,7 @@ impl Options {
paths: self.paths.clone(),
dirty: self.dirty,
ignored: self.ignored,
ignore_older: self.ignore_older,
verbose: flag,
}
}
Expand All @@ -114,6 +130,7 @@ impl Options {
paths: input,
dirty: self.dirty,
ignored: self.ignored,
ignore_older: self.ignore_older,
verbose: self.verbose,
}
}
Expand Down Expand Up @@ -236,11 +253,16 @@ fn diff_affects_oid(diff: &Diff, oid: &Oid, touchable_path: &mut Utf8PathBuf) ->
})
}

fn touch_if_older(path: Utf8PathBuf, time: i64, verbose: bool) -> Result<bool> {
fn touch_if_time_mismatch(
path: Utf8PathBuf,
time: i64,
verbose: bool,
ignore_older: bool,
) -> Result<bool> {
let commit_time = FileTime::from_unix_time(time, 0);
let metadata = fs::metadata(&path).context(IoSnafu)?;
let file_mtime = FileTime::from_last_modification_time(&metadata);
if file_mtime != commit_time {
if file_mtime > commit_time || (!ignore_older && file_mtime < commit_time) {
filetime::set_file_mtime(&path, commit_time).context(IoSnafu)?;
if verbose {
println!("Rewound the clock: {path}");
Expand Down Expand Up @@ -296,7 +318,12 @@ fn process_touchables(repo: &Repository, touchables: FileSet, opts: &Options) ->
let affected = diff_affects_oid(&diff, oid, touchable_path);
if affected {
let time = commit.time().seconds();
if let Ok(true) = touch_if_older(touchable_path.to_path_buf(), time, opts.verbose) {
if let Ok(true) = touch_if_time_mismatch(
touchable_path.to_path_buf(),
time,
opts.verbose,
opts.ignore_older,
) {
touched
.write()
.unwrap()
Expand Down
Loading