How do you cancel an external git diff?
Asked Answered
M

3

20

I've got vim setup as my external diff tool:

[diff]
        external = git_diff_wrapper

#!/bin/sh

vimdiff "$2" "$5"

Say I have 300 files that have been modified; via bash, I type "git diff". It launches 300 vimdiffs sequentially, how do I abort it?

Malchus answered 7/5, 2010 at 6:25 Comment(1)
Note that the same solutions can be used with git mergetool too.Historiography
M
3

If stopping the process is not enough, killing the shell itself (in which you launched the git diff) might be more effective.

https://static.mcmap.net/file/mcmap/ZG-Ab5ovK1-wZVMrKnXhanMAW7XlbmyQKnyx/wp/wp-uploads/2009/06/close-this-window.png


See also Git Diff with Vimdiff

VimDiff

Not being ready to go full speed into using vimdiff (I’m just new to it), I put the following in ‘gitvimdiff’.
The result is that I can use vimdiff to look at git-diff by running ‘gitvimdiff‘, but a normal invocation of ‘git diff’ behaves as I’m used to.

#!/bin/sh

if [ -n "${GIT_EXTERNAL_DIFF}" ]; then
[ "${GIT_EXTERNAL_DIFF}" = "${0}" ] ||
{ echo “GIT_EXTERNAL_DIFF set to unexpected value” 1>&2; exit 1; }
exec vimdiff “$2″ “$5″
else
GIT_EXTERNAL_DIFF=”${0}” exec git –no-pager diff “$@”
fi

But if you still want the modified git diff, a git status might help before launching it ;)

And you can setup a function to get the old git diff behavior if needed:

I still have access to the default git diff behavior with the --no-ext-diff flag. Here’s a function I put in my bash configuration files:

function git_diff() {
  git diff --no-ext-diff -w "$@" | vim -R -
}
  • --no-ext-diff: to prevent using vimdiff
  • -w: to ignore whitespace
  • -R: to start vim in read-only mode
  • -: to make vim act as a pager
Mudpack answered 7/5, 2010 at 6:29 Comment(0)
C
27

Use :cquit to exit vim with an error code. Git will detect the error and stop opening new vimdiffs. You'll probably want to create a mapping for it in your .vimrc:

if &diff
  map Q :cquit<CR>
endif

Then just hit Q to early abort from a git diff run.

In order for this to work you must edit your gitconfig:

git config --global difftool.trustExitCode true
git config --global mergetool.trustExitCode true
Conway answered 7/5, 2010 at 6:25 Comment(6)
This should be the accepted answer as it directly addresses the question instead of providing a workaround. Wonderful idea for the if &diff mapping too. Thanks all around!Drumm
This doesn't work for me. I'm not referring to the mapping but the :cquit command itself. Vim exits correctly but Git still carries on with the diffing process.Serriform
@kai git-scm.com/docs/git-difftool says you need to add --trust-exit-code to your git difftool command. My alias is now d = difftool --trust-exit-code after adding --trust-exit-code I can use :cq in vim to abort the diff.Hamby
@Hamby I'm having the same problem as kai. trustExitCode and --trust-exit-code both don't work for me. Any suggestions?Logomachy
@kai I think I see now. difftool.trustExitCode seems to be present only in newer versions of git and is not available for e.g. git 1.9.1.Logomachy
Thank you! :D I just needed out after a mistake I knew I needed to start all over because of.Robustious
M
3

If stopping the process is not enough, killing the shell itself (in which you launched the git diff) might be more effective.

https://static.mcmap.net/file/mcmap/ZG-Ab5ovK1-wZVMrKnXhanMAW7XlbmyQKnyx/wp/wp-uploads/2009/06/close-this-window.png


See also Git Diff with Vimdiff

VimDiff

Not being ready to go full speed into using vimdiff (I’m just new to it), I put the following in ‘gitvimdiff’.
The result is that I can use vimdiff to look at git-diff by running ‘gitvimdiff‘, but a normal invocation of ‘git diff’ behaves as I’m used to.

#!/bin/sh

if [ -n "${GIT_EXTERNAL_DIFF}" ]; then
[ "${GIT_EXTERNAL_DIFF}" = "${0}" ] ||
{ echo “GIT_EXTERNAL_DIFF set to unexpected value” 1>&2; exit 1; }
exec vimdiff “$2″ “$5″
else
GIT_EXTERNAL_DIFF=”${0}” exec git –no-pager diff “$@”
fi

But if you still want the modified git diff, a git status might help before launching it ;)

And you can setup a function to get the old git diff behavior if needed:

I still have access to the default git diff behavior with the --no-ext-diff flag. Here’s a function I put in my bash configuration files:

function git_diff() {
  git diff --no-ext-diff -w "$@" | vim -R -
}
  • --no-ext-diff: to prevent using vimdiff
  • -w: to ignore whitespace
  • -R: to start vim in read-only mode
  • -: to make vim act as a pager
Mudpack answered 7/5, 2010 at 6:29 Comment(0)
P
2

Just kill the parent process. Open up a terminal, use pstree -p to find the process ID (PID) of the git process, then kill -9 it. On my system, it looks something like this:

$ pstree -p
...
        ├─gnome-terminal(20473)─┬─bash(10302)───git(10331)───pager(10332)
...
$ kill -9 10331

Not exactly elegant, but it works. On your system, pager will probably be something different, but it will have git as a parent process.

Pleochroism answered 7/5, 2010 at 6:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.