最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Detect renames with `git difftool --dir-diff` - Stack Overflow

programmeradmin1浏览0评论

With git difftool --dir-diff, I can 'diff a whole tree by preparing a temporary copy'. However, I then lose information about renames. Here's a comprehensive example demonstrating the same.

git init
echo xyz >foo.txt
git add .
git commit -m 'Initial commit'
git mv foo.txt bar.txt
git commit -m 'Renamed'
git difftool --dir-diff --tool meld HEAD^

Meld is used just as an example.

Actual Output

Meld thinks a file was removed and a different file was added. A custom external diff tool which doesn't do similarity analysis would do the same.

Desired Output

Something resembling the output of git diff HEAD^. In theory, I could run a recursive similarity analysis on the left and right directories. However Git knows that foo.txt was renamed to bar.txt, and I'd ideally like this information to be sent to the custom external diff tool.

diff --git a/foo.txt b/bar.txt
similarity index 100%
rename from foo.txt
rename to bar.txt

Git - git-config Documentation | difftool.<tool>.cmd only mentions two arguments, LEFT and RIGHT, the two directories where the files will be made available.

Git - git-difftool Documentation doesn't provide any details on how to accomplish this, either.

How can I achieve correct rename detection without redoing the processing Git already does? Assuming I build my own custom external diff tool (using git config diff.tool custom and git config difftool.custom.cmd custom_command), how can I go about this?

With git difftool --dir-diff, I can 'diff a whole tree by preparing a temporary copy'. However, I then lose information about renames. Here's a comprehensive example demonstrating the same.

git init
echo xyz >foo.txt
git add .
git commit -m 'Initial commit'
git mv foo.txt bar.txt
git commit -m 'Renamed'
git difftool --dir-diff --tool meld HEAD^

Meld is used just as an example.

Actual Output

Meld thinks a file was removed and a different file was added. A custom external diff tool which doesn't do similarity analysis would do the same.

Desired Output

Something resembling the output of git diff HEAD^. In theory, I could run a recursive similarity analysis on the left and right directories. However Git knows that foo.txt was renamed to bar.txt, and I'd ideally like this information to be sent to the custom external diff tool.

diff --git a/foo.txt b/bar.txt
similarity index 100%
rename from foo.txt
rename to bar.txt

Git - git-config Documentation | difftool.<tool>.cmd only mentions two arguments, LEFT and RIGHT, the two directories where the files will be made available.

Git - git-difftool Documentation doesn't provide any details on how to accomplish this, either.

How can I achieve correct rename detection without redoing the processing Git already does? Assuming I build my own custom external diff tool (using git config diff.tool custom and git config difftool.custom.cmd custom_command), how can I go about this?

Share Improve this question edited Mar 29 at 19:11 tfpf asked Mar 28 at 17:01 tfpftfpf 6841 gold badge9 silver badges20 bronze badges 5
  • 1 Sounds like gitlab.gnome./GNOME/meld/-/issues/730, did you read through that already? – Mike 'Pomax' Kamermans Commented Mar 28 at 17:16
  • @Mike'Pomax'Kamermans Thanks for the link! Hadn't seen it before. However, it is Meld-specific. My question was how a generic external diff tool could obtain rename information without analysing the directories, since Git already does this (and I don't mind creating a custom tool if I have to). – tfpf Commented Mar 28 at 17:52
  • Your screenshot is specific to Meld: don't tell me about details, tell everyone by updating your post (I'm almost certainly not the person to write you an eventual answer) – Mike 'Pomax' Kamermans Commented Mar 29 at 0:09
  • Interesting question. I think meld is incapable of this. Check this: reddit/r/software/comments/1c9mcd2/… FreeFileSync is alleged to be capable of it, although it does not seem to use git diff – KH Kim Commented Mar 29 at 4:16
  • 1 git difftool basically delegates to the target diff tool the complete burden of computing the diff. Look into the options for meld or kdiff3 or ... to see if they have a similarity détection option. – LeGEC Commented Mar 29 at 5:22
Add a comment  | 

1 Answer 1

Reset to default 1

As you have already understood: git difftool -d creates 2 temporary directories with the content to compare, and then calls the external diff tool on these two directories. By doing so, it completely delegates the task of computing/displaying/rendering the diff to that external tool.

So in some way, your question is more a meld question (or whatever diff tool) than a pure git question, although a certain number of git options can give you an opportunity to tweak the process.


By looking at the man page for meld or kdiff3, I don't see direct options to detect renaming when comparing two directories.

You could write your own wrapper script and try to find renames before invoking the actual diff tool, and then define that script as a diff viewer:

git config --global difftool.meld_rename.cmd '/path/to/my/script $LOCAL $REMOTE'

# usage:
git difftool -d --tool=meld_rename HEAD~
发布评论

评论列表(0)

  1. 暂无评论