git, vimdiff and dirdiff

2019-01-22 13:40发布

问题:

I'm trying to use vimdiff+dirdiff.vim to diff inside Vim multiple files versionned with Git.

For Mercurial, it is possible with mercurial extdiff extension.

The only way I found on the web to integrate Vim with Git diff is to use vimdiff on a singe file, as describe in this post.

Does any one know how to use vimdiff+dirdiff+git?

回答1:

Before git version 1.7.11

git-diffall is what I need, thanks a lot. With help of this page about git difftool and this one about running vim+dirdiff from command line, I wrote my $HOME/.gitconfig as:

[diff]
  tool = default-difftool

[difftool "default-difftool"]
  cmd = vim -f '+next' '+execute \"DirDiff\" argv(0) argv(1)' $LOCAL $REMOTE

[difftool]
  prompt = false

After putting git-diffall in my PATH, I can diff for example working directory with branch dev with:

git diffall dev

The --copy-back is also what I need if I want to modify the working directory persitantly from Vim:

git diffall --copy-back dev

Since git version 1.7.11

Since version 1.7.11, "git difftool" learned the "--dir-diff" option that simplify things and git-diffall is no longer needed.

.gitconfig contains:

[diff]
  tool = default-difftool
[difftool "default-difftool"]
  cmd = vim -f '+next' '+execute \"DirDiff\" argv(0) argv(1)' $LOCAL $REMOTE '+syntax off'

And diffing for example working directory with branch dev is done with:

git difftool -d dev


回答2:

Tim Pope's fugitive is the quintessential git plug-in for vim. It might not have dirdiff's functionality, but it does integrate git status output beautifully, with key mappings to easily navigate between modified files. Any files listed in your git status output can then easy be diffed with the D mapping, allowing you to customise the exact changes going into your changes.



回答3:

Git does not support directory diffs directly, but it can be done with 3rd party extension.

Check out git diffall (disclosure: I wrote this script). This script works with the tool set by git config diff.tool <TOOL> to perform a true directory diff.

Also, see these related questions:

  • git difftool, open all diff files immediately, not in serial
  • Getting Beyond Compare to Diff 2 Branches Simultaneously


回答4:

It would be interesting to monitor the commits for the Vim module fugitive, because, starting with (git 1.7.11, June 2012), git now can diff directories (ie display all the files to be compared, before opening the difftool)

See "git difftool to give directory compare?"

So adding that feature to fugitive.vim would be an evolution from a file to file diff approach.

However, Zyx points out in the comment the limits of that approach:

All you need to get this working is:

  1. git diff --name-status and
  2. git cat-file,

nothing more (except for some vimscript code).
And, unlike git difftool, this works for any VCS with vcs diff and vcs cat-file support

Zyx mentions as an example of that file-byfile approach his project aurum:

My aurum has dirdiff-like functionality (AuVimDiff full opens a bunch of tabs with vimdiff view of files that have differences) and it never used any sort of difftool support.

(See script aurum / autoload / aurum / vimdiff.vim)

Zyx adds:

it cannot be a good guide for a person that wants to add this functionality to fugitive:
Actual git diff --name-status call is hidden inside the git driver in a function called by s:git.status accessed as repo.functions.status (because mercurial uses hg status -r rev1:rev2, not hg diff --name-status and I find this rather logical).
Thus you have to traverse three levels of abstraction here (repository interface, shell commands wrapper and only then actual git call).
It is faster to write everything from scratch



标签: git vim vimdiff