Skip to content

Commit

Permalink
Detect git difftool paths and prefer the second argument
Browse files Browse the repository at this point in the history
Fixes #620
  • Loading branch information
Wilfred committed Feb 20, 2024
1 parent 607b5d2 commit 1cc0c42
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Added support for Smali.

### Display

Fixed an issue with paths not showing the containing directory when
using difftastic with `git difftool`.

Fixed an issue with the experimental JSON display mode where it
ignored `--skip-unchanged`.

Expand Down
21 changes: 21 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,30 @@ fn common_path_suffix(lhs_path: &Path, rhs_path: &Path) -> Option<String> {
}
}

/// Does `path` look like "/tmp/git-blob-abcdef/modified_field.txt"?
fn is_git_tmpfile(path: &Path) -> bool {
let Ok(rel_path) = path.strip_prefix(std::env::temp_dir()) else {
return false;
};

let components: Vec<_> = rel_path.components().collect();
if components.len() != 2 {
return false;
}

components[0]
.as_os_str()
.to_string_lossy()
.starts_with("git-blob-")
}

fn build_display_path(lhs_path: &FileArgument, rhs_path: &FileArgument) -> String {
match (lhs_path, rhs_path) {
(FileArgument::NamedPath(lhs), FileArgument::NamedPath(rhs)) => {
if is_git_tmpfile(lhs) {
return rhs.display().to_string();
}

match common_path_suffix(lhs, rhs) {
Some(common_suffix) => common_suffix,
None => rhs.display().to_string(),
Expand Down

0 comments on commit 1cc0c42

Please sign in to comment.