Skip to contentSkip to conversation

contrib: add 'git difftool' for launching common merge tools

PATCH Superseded David Aguilar 2 participants 7 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 contrib: add 'git difftool' for launching common merge tools

'git difftool' is a git command that allows you to compare and edit files between revisions using common merge tools. 'git difftool' does what 'git mergetool' does but its use is for non-merge situations such as when preparing commits or comparing changes against the index. It uses the same configuration variables as 'git mergetool' and provides the same command-line interface as 'git diff'.

---

 contrib/difftool/git-difftool        |   74 +++++++++++
 contrib/difftool/git-difftool-helper |  240 ++++++++++++++++++++++++++++++++++
 contrib/difftool/git-difftool.txt    |  104 +++++++++++++++
 3 files changed, 418 insertions(+), 0 deletions(-)
 create mode 100755 contrib/difftool/git-difftool
 create mode 100755 contrib/difftool/git-difftool-helper
 create mode 100644 contrib/difftool/git-difftool.txt

This patch was the result of the following thread on the git-list:
http://thread.gmane.org/gmane.comp.version-control.git/103918

contrib/difftool/git-difftool new file +74 -0
new file mode 100755
index 0000000..1fc087c
@@ -0,0 +1,74 @@
+#!/usr/bin/env perl
+# Copyright (c) 2009 David Aguilar
+#
+# This is a wrapper around the GIT_EXTERNAL_DIFF-compatible
+# git-difftool-helper script.  This script exports
+# GIT_EXTERNAL_DIFF and GIT_PAGER for use by git, and
+# GIT_NO_PROMPT and GIT_MERGE_TOOL for use by git-difftool-helper.
+# Any arguments that are unknown to this script are forwarded to 'git diff'.
+
+use strict;
+use warnings;
+use Cwd qw(abs_path);
+use File::Basename qw(dirname);
+
+my $DIR = abs_path(dirname($0));
+
+
+sub usage
+{
+	print << 'USAGE';
+
+usage: git difftool [--no-prompt] [--tool=tool] ["git diff" options]
+USAGE
+	exit 1;
+}
+
+sub setup_environment
+{
+	$ENV{PATH} = "$DIR:$ENV{PATH}";
+	$ENV{GIT_PAGER} = '';
+	$ENV{GIT_EXTERNAL_DIFF} = 'git-difftool-helper';
+}
+
+sub exe
+{
+	my $exe = shift;
+	return defined $ENV{COMSPEC} ? "$exe.exe" : $exe;
+}
+
+sub generate_command
+{
+	my @command = (exe('git'), 'diff');
+	my $skip_next = 0;
+	my $idx = -1;
+	for my $arg (@ARGV) {
+		$idx++;
+		if ($skip_next) {
+			$skip_next = 0;
+			next;
+		}
+		if ($arg eq '-t' or $arg eq '--tool') {
+			usage() if $#ARGV <= $idx;
+			$ENV{GIT_MERGE_TOOL} = $ARGV[$idx + 1];
+			$skip_next = 1;
+			next;
+		}
+		if ($arg =~ /^--tool=/) {
+			$ENV{GIT_MERGE_TOOL} = substr($arg, 7);
+			next;
+		}
+		if ($arg eq '--no-prompt') {
+			$ENV{GIT_DIFFTOOL_NO_PROMPT} = 'true';
+			next;
+		}
+		if ($arg eq '-h' or $arg eq '--help') {
+			usage();
+		}
+		push @command, $arg;
+	}
+	return @command
+}
+
+setup_environment();
+exec(generate_command());
contrib/difftool/git-difftool-helper new file +240 -0

240 lines not on this page · raw message

contrib/difftool/git-difftool.txt new file +104 -0

104 lines not on this page · raw message

Signature · 1 line
1.6.1.149.g7bbd8
#1 lore raw

David Aguilar, 16.01.2009:

contrib/difftool/git-difftool line 7

Quoted 13 lines
contrib/difftool/git-difftool new file +7 -0
new file mode 100755
index 0000000..1fc087c
@@ -0,0 +1,74 @@
+#!/usr/bin/env perl
+# Copyright (c) 2009 David Aguilar
+#
+# This is a wrapper around the GIT_EXTERNAL_DIFF-compatible
+# git-difftool-helper script.  This script exports
+# GIT_EXTERNAL_DIFF and GIT_PAGER for use by git, and
+# GIT_NO_PROMPT and GIT_MERGE_TOOL for use by git-difftool-helper.

GIT_DIFFTOOL_NO_PROMPT

contrib/difftool/git-difftool line 21

Quoted 4 lines
contrib/difftool/git-difftool new file +4 -0
+sub usage
+{
+	print << 'USAGE';
+

Why the leading empty line?

contrib/difftool/git-difftool line 22

Quoted 1 line
contrib/difftool/git-difftool new file +1 -0
+usage: git difftool [--no-prompt] [--tool=tool] ["git diff" options]

--tool=<tool>

Swap the order of --no-prompt and --tool for consistency with git-difftool.txt and git-mergetool.

contrib/difftool/git-difftool-helper line 87

Quoted 13 lines
contrib/difftool/git-difftool-helper new file +7 -0
new file mode 100755
index 0000000..0b266e3
 
+	meld|vimdiff)
+		"$merge_tool_path" "$LOCAL" "$REMOTE"
+		;;
+
+	gvimdiff)
+		"$merge_tool_path" -f "$LOCAL" "$REMOTE"
+		;;

Maybe use '-c "wincmd l"' for Vim as in my patch for git-mergetool to automatically place the cursor in the editable file? Useful for editing, if git-difftool is used to diff a file from the working tree.

See http://thread.gmane.org/gmane.comp.version-control.git/106109

contrib/difftool/git-difftool.txt

Quoted 5 lines
contrib/difftool/git-difftool.txt new file +0 -0
new file mode 100644
index 0000000..3940c70

You have deleted all the '-' chars from git-command, but when using it as the name I think it's the preferred method, only when used as command then without slash.

Quoted 3 lines

+DESCRIPTION
+-----------
+'git difftool' is a git command that allows you to compare and edit files

If for example 'git difftool' is used as the name of the tool and not the command, I think the dash should be kept.

Quoted 2 lines

+between revisions using common merge tools. At its most basic level, +'git difftool' does what 'git mergetool' does but its use is for non-merge

Keep the dash.

contrib/difftool/git-difftool.txt line 20

Quoted 4 lines
contrib/difftool/git-difftool.txt new file +4 -0
+situations such as when preparing commits or comparing changes against
+the index.
+
+'git difftool' is a frontend to 'git diff' and accepts the same

Keep the dash.

contrib/difftool/git-difftool.txt line 23

Quoted 3 lines
contrib/difftool/git-difftool.txt new file +3 -0
+arguments and options.
+
+See linkgit:git-diff[7] for the full list of supported options.

[1]

contrib/difftool/git-difftool.txt line 33

Quoted 10 lines
contrib/difftool/git-difftool.txt new file +10 -0
+
+OPTIONS
+-------
+-t <tool>::
+--tool=<tool>::
+	Use the merge resolution program specified by <tool>.
+	Valid merge tools are:
+	kdiff3, tkdiff, meld, xxdiff, emerge, vimdiff, gvimdiff, ecmerge, and opendiff
++
+If a merge resolution program is not specified, 'git difftool'

Keep the dash.

contrib/difftool/git-difftool.txt line 35

Quoted 3 lines
contrib/difftool/git-difftool.txt new file +3 -0
+situations such as when preparing commits or comparing changes against
+will use the configuration variable `merge.tool`.  If the
+configuration variable `merge.tool` is not set, 'git difftool'

Keep the dash.

contrib/difftool/git-difftool.txt line 41

Quoted 7 lines
contrib/difftool/git-difftool.txt new file +7 -0
+situations such as when preparing commits or comparing changes against
+will pick a suitable default.
++
+You can explicitly provide a full path to the tool by setting the
+configuration variable `mergetool.<tool>.path`. For example, you
+can configure the absolute path to kdiff3 by setting
+`mergetool.kdiff3.path`. Otherwise, 'git difftool' assumes the

Keep the dash.

contrib/difftool/git-difftool.txt line 45

Quoted 5 lines
contrib/difftool/git-difftool.txt new file +5 -0
+situations such as when preparing commits or comparing changes against
+tool is available in PATH.
++
+Instead of running one of the known merge tool programs,
+'git difftool' can be customized to run an alternative program

Keep the dash.

contrib/difftool/git-difftool.txt line 49

Quoted 5 lines
contrib/difftool/git-difftool.txt new file +5 -0
+situations such as when preparing commits or comparing changes against
+by specifying the command line to invoke in a configuration
+variable `mergetool.<tool>.cmd`.
++
+When 'git difftool' is invoked with this tool (either through the

Keep the dash.

contrib/difftool/git-difftool.txt line 59

Quoted 2 lines

+--no-prompt::

contrib/difftool/git-difftool.txt new file +1 -0
+	Do not prompt before launching a merge tool.

the diff tool

contrib/difftool/git-difftool.txt line 84

Quoted 3 lines
contrib/difftool/git-difftool.txt new file +3 -0
+SEE ALSO
+--------
+linkgit:git-diff[7]::

[1]

contrib/difftool/git-difftool.txt line 90

Quoted 6 lines
contrib/difftool/git-difftool.txt new file +6 -0
+	 Show changes between commits, commit and working tree, etc
+
+linkgit:git-mergetool[1]::
+	Run merge conflict resolution tools to resolve merge conflicts
+
+linkgit:git-config[7]::

[1]

Works fine for me, thanks.

Markus

#2 lore raw

PATCH v2 1/1 contrib: add 'git-difftool' for launching common diff tools

'git-difftool' is a git command that allows you to compare and edit files between revisions using common merge tools. 'git-difftool' does what 'git-mergetool' does but its use is for non-merge situations such as when preparing commits or comparing changes against the index. It uses the same configuration variables as 'git-mergetool' and provides the same command-line interface as 'git-diff'.

---

This newer version addresses the feedback from Markus Heidelberg. difftool is now more consistent with mergetool and it includes Markus's vimdiff enhancements for positioning the cursor at startup.

 contrib/difftool/git-difftool        |   73 ++++++++++
 contrib/difftool/git-difftool-helper |  244 ++++++++++++++++++++++++++++++++++
 contrib/difftool/git-difftool.txt    |  104 ++++++++++++++
 3 files changed, 421 insertions(+), 0 deletions(-)
 create mode 100755 contrib/difftool/git-difftool
 create mode 100755 contrib/difftool/git-difftool-helper
 create mode 100644 contrib/difftool/git-difftool.txt
contrib/difftool/git-difftool new file +73 -0
new file mode 100755
index 0000000..0cda3d2
@@ -0,0 +1,73 @@
+#!/usr/bin/env perl
+# Copyright (c) 2009 David Aguilar
+#
+# This is a wrapper around the GIT_EXTERNAL_DIFF-compatible
+# git-difftool-helper script.  This script exports
+# GIT_EXTERNAL_DIFF and GIT_PAGER for use by git, and
+# GIT_DIFFTOOL_NO_PROMPT and GIT_MERGE_TOOL for use by git-difftool-helper.
+# Any arguments that are unknown to this script are forwarded to 'git diff'.
+
+use strict;
+use warnings;
+use Cwd qw(abs_path);
+use File::Basename qw(dirname);
+
+my $DIR = abs_path(dirname($0));
+
+
+sub usage
+{
+	print << 'USAGE';
+usage: git difftool [--tool=<tool>] [--no-prompt] ["git diff" options]
+USAGE
+	exit 1;
+}
+
+sub setup_environment
+{
+	$ENV{PATH} = "$DIR:$ENV{PATH}";
+	$ENV{GIT_PAGER} = '';
+	$ENV{GIT_EXTERNAL_DIFF} = 'git-difftool-helper';
+}
+
+sub exe
+{
+	my $exe = shift;
+	return defined $ENV{COMSPEC} ? "$exe.exe" : $exe;
+}
+
+sub generate_command
+{
+	my @command = (exe('git'), 'diff');
+	my $skip_next = 0;
+	my $idx = -1;
+	for my $arg (@ARGV) {
+		$idx++;
+		if ($skip_next) {
+			$skip_next = 0;
+			next;
+		}
+		if ($arg eq '-t' or $arg eq '--tool') {
+			usage() if $#ARGV <= $idx;
+			$ENV{GIT_MERGE_TOOL} = $ARGV[$idx + 1];
+			$skip_next = 1;
+			next;
+		}
+		if ($arg =~ /^--tool=/) {
+			$ENV{GIT_MERGE_TOOL} = substr($arg, 7);
+			next;
+		}
+		if ($arg eq '--no-prompt') {
+			$ENV{GIT_DIFFTOOL_NO_PROMPT} = 'true';
+			next;
+		}
+		if ($arg eq '-h' or $arg eq '--help') {
+			usage();
+		}
+		push @command, $arg;
+	}
+	return @command
+}
+
+setup_environment();
+exec(generate_command());
contrib/difftool/git-difftool-helper new file +244 -0

244 lines not on this page · raw message

contrib/difftool/git-difftool.txt new file +104 -0

104 lines not on this page · raw message

Signature · 1 line
1.6.1.149.g7bbd8
#3 lore raw

Re: [PATCH v2] contrib: add 'git-difftool' for launching common diff tools

David Aguilar, 19.01.2009:

Quoted 7 lines

'git-difftool' is a git command that allows you to compare and edit files between revisions using common merge tools. 'git-difftool' does what 'git-mergetool' does but its use is for non-merge situations such as when preparing commits or comparing changes against the index. It uses the same configuration variables as 'git-mergetool' and provides the same command-line interface as 'git-diff'.

Quoted 6 lines

---

This newer version addresses the feedback from Markus Heidelberg. difftool is now more consistent with mergetool and it includes Markus's vimdiff enhancements for positioning the cursor at startup.

Your patch is already in master, you should send a patch against it.

Markus

#4 lore raw
In reply to Markus Heidelberg
Quoted 7 lines · Markus Heidelberg

David Aguilar, 16.01.2009:

contrib/difftool/git-difftool line 7

Quoted 3 lines
contrib/difftool/git-difftool new file +3 -0
+# git-difftool-helper script.  This script exports
+# GIT_EXTERNAL_DIFF and GIT_PAGER for use by git, and
+# GIT_NO_PROMPT and GIT_MERGE_TOOL for use by git-difftool-helper.

GIT_DIFFTOOL_NO_PROMPT

Thanks for catching that.

Quoted 6 lines

contrib/difftool/git-difftool line 21

Quoted 4 lines
contrib/difftool/git-difftool new file +4 -0
+sub usage
+{
+     print << 'USAGE';
+

Why the leading empty line?

Fixed.

Quoted 6 lines
Quoted 1 line

+usage: git difftool [--no-prompt] [--tool=tool] ["git diff" options]

--tool=<tool>

Swap the order of --no-prompt and --tool for consistency with git-difftool.txt and git-mergetool.

Done.

Quoted 8 lines

contrib/difftool/git-difftool-helper line 87

Quoted 7 lines
contrib/difftool/git-difftool-helper new file +7 -0
+     meld|vimdiff)
+             "$merge_tool_path" "$LOCAL" "$REMOTE"
+             ;;
+
+     gvimdiff)
+             "$merge_tool_path" -f "$LOCAL" "$REMOTE"
+             ;;
Quoted 5 lines

Maybe use '-c "wincmd l"' for Vim as in my patch for git-mergetool to automatically place the cursor in the editable file? Useful for editing, if git-difftool is used to diff a file from the working tree.

See http://thread.gmane.org/gmane.comp.version-control.git/106109

Very cool. When you have unstaged changes git diff sends the local filename as the 2nd argument so I changed the vim command to "wincmd r" so that vim places the cursor on the right hand side.

Quoted 3 lines

You have deleted all the '-' chars from git-command, but when using it as the name I think it's the preferred method, only when used as command then without slash.

I was wondering about that. I think I tried to follow the lead from the git-diff.txt documentation, but "diff" is a builtin and thus doesn't have an actual git-diff, so I see why they should be different.

I've changed the docs as you suggested -- 'git-difftool' is used to name the script while 'git difftool' is used in the SYNOPSIS section.

Quoted 6 lines

contrib/difftool/git-difftool.txt line 84

Quoted 3 lines
contrib/difftool/git-difftool.txt new file +3 -0
+SEE ALSO
+--------
+linkgit:git-diff[7]::

[1]

Quoted 8 lines

contrib/difftool/git-difftool.txt line 90

Quoted 6 lines
contrib/difftool/git-difftool.txt new file +6 -0
+      Show changes between commits, commit and working tree, etc
+
+linkgit:git-mergetool[1]::
+     Run merge conflict resolution tools to resolve merge conflicts
+
+linkgit:git-config[7]::

[1]

Oops, I don't know how that happened.

Quoted 5 lines

Works fine for me, thanks.

Markus

Thanks again for your help.

Signature · 1 line
    David
#5 lore raw

David Aguilar, 19.01.2009:

Quoted 15 lines
Quoted 13 lines · Markus Heidelberg

David Aguilar, 16.01.2009:

contrib/difftool/git-difftool-helper line 87

Quoted 7 lines
contrib/difftool/git-difftool-helper new file +7 -0
+     meld|vimdiff)
+             "$merge_tool_path" "$LOCAL" "$REMOTE"
+             ;;
+
+     gvimdiff)
+             "$merge_tool_path" -f "$LOCAL" "$REMOTE"
+             ;;

Maybe use '-c "wincmd l"' for Vim as in my patch for git-mergetool to automatically place the cursor in the editable file? Useful for editing, if git-difftool is used to diff a file from the working tree.

Quoted 6 lines
Quoted 1 line

See http://thread.gmane.org/gmane.comp.version-control.git/106109

Very cool. When you have unstaged changes git diff sends the local filename as the 2nd argument so I changed the vim command to "wincmd r" so that vim places the cursor on the right hand side.

Well, in "wincmd l" "l" doesn't mean "left", it doesn't mean "put the cursor into the left window", it just moves the cursor into the next right window. So "wincmd r" doesn't mean "put the cursor into the right window", but "rotate the window" (see :help ctrl-w_r).

So with "wincmd r" the local file would be moved to the left side and the index file to the right side, still containing the cursor. Not what we want.

With "wincmd l" the local file would stay on the right side, the index file on the left side, but the cursor would move from the left to the right side. Now we can edit the local file.

Quoted 8 lines
Quoted 3 lines

You have deleted all the '-' chars from git-command, but when using it as the name I think it's the preferred method, only when used as command then without slash.

I was wondering about that. I think I tried to follow the lead from the git-diff.txt documentation, but "diff" is a builtin and thus doesn't have an actual git-diff, so I see why they should be different.

Hmm, I don't think it makes a difference, whether it's a builtin or not. git-diff only exists behind the scenes, invoking it as "git-diff" doesn't work anymore with default settings. On the other hand, I can also invoke "git difftool" without the slash, git can find it. The same way I can for example call "git svn", which also isn't a builtin.

Markus

#6 lore raw

On Sun, Jan 18, 2009 at 9:03 PM, Markus Heidelberg

Quoted 4 lines

Well, in "wincmd l" "l" doesn't mean "left", it doesn't mean "put the cursor into the left window", it just moves the cursor into the next right window. So "wincmd r" doesn't mean "put the cursor into the right window", but "rotate the window" (see :help ctrl-w_r).

oops.. i should know better. sorry... let's ignore 2/2 for now then. I'll resend. duh.. I should have known better.. h left, j down, k up, l right.

Quoted 27 lines

So with "wincmd r" the local file would be moved to the left side and the index file to the right side, still containing the cursor. Not what we want.

With "wincmd l" the local file would stay on the right side, the index file on the left side, but the cursor would move from the left to the right side. Now we can edit the local file.

Quoted 8 lines
Quoted 3 lines

You have deleted all the '-' chars from git-command, but when using it as the name I think it's the preferred method, only when used as command then without slash.

I was wondering about that. I think I tried to follow the lead from the git-diff.txt documentation, but "diff" is a builtin and thus doesn't have an actual git-diff, so I see why they should be different.

Hmm, I don't think it makes a difference, whether it's a builtin or not. git-diff only exists behind the scenes, invoking it as "git-diff" doesn't work anymore with default settings. On the other hand, I can also invoke "git difftool" without the slash, git can find it. The same way I can for example call "git svn", which also isn't a builtin.

Markus

Signature · 1 line
    David