Skip to contentSkip to conversation

difftool: print list of valid tools with '--tool-help'

PATCH Superseded Tim Henigan 2 participants 5 messages Jun 15, 2016 Documentation

v8 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

PATCH v8 9/9 difftool: print list of valid tools with '--tool-help'

Since bc7a96a (mergetool--lib: Refactor tools into separate files, 2011-08-18), it is possible to add a new diff tool by creating a simple script in the '$(git --exec-path)/mergetools' directory. Updating the difftool help text is still a manual process, and the documentation can easily go out of sync.

This commit teaches difftool the '--tool-help' option, which:
- Reads the list of valid tools from 'mergetools/*'
- Determines which of them are actually installed
- Determines which are capable of diffing (i.e. not just a merge tool)
- Prints the resulting list for the user

---

This replaces 9/9 in the previous version of the series.

Changes in v8:
- Replaced 'glob' with 'File::Find'. Glob will fail if file paths include spaces. Using File::Find overcomes that limitation.
- Added 'TOOL_MODE=DIFF' prior to calling 'git-mergetool--lib.sh'. This insures that the shell script executes as designed.
- difftool now calls 'can_diff' from 'git-mergetool--lib.sh' to insure that only tools that are capable of diffing are shown as valid options. For example, 'tortoisemerge' cannot be used as a diff viewer.
- The list of tools shown to the user is now sorted.

 Documentation/git-difftool.txt |   11 +++++-----
 git-difftool.perl              |   43 ++++++++++++++++++++++++++++++++++++++--
 t/t7800-difftool.sh            |    5 +++++
 3 files changed, 52 insertions(+), 7 deletions(-)
Documentation/git-difftool.txt +6 -5
index aba5e76..31fc2e3 100644
@@ -36,11 +36,9 @@ OPTIONS
 
 -t <tool>::
 --tool=<tool>::
-	Use the diff tool specified by <tool>.
-	Valid diff tools are:
-	araxis, bc3, deltawalker, diffuse, emerge, ecmerge, gvimdiff,
-	kdiff3,	kompare, meld, opendiff, p4merge, tkdiff, vimdiff and
-	xxdiff.
+	Use the diff tool specified by <tool>.  Valid values include
+	emerge, kompare, meld, and vimdiff. Run `git difftool --tool-help`
+	for the list of valid <tool> settings.
 +
 If a diff tool is not specified, 'git difftool'
 will use the configuration variable `diff.tool`.  If the
@@ -68,6 +66,9 @@ of the diff post-image.  `$MERGED` is the name of the file which is
 being compared. `$BASE` is provided for compatibility
 with custom merge tool commands and has the same value as `$MERGED`.
 
+--tool-help::
+	Print a list of diff tools that may be used with `--tool`.
+
 -x <command>::
 --extcmd=<command>::
 	Specify a custom command for viewing diffs.
git-difftool.perl +41 -2

78 lines not on this page · raw message

t/t7800-difftool.sh +5 -0
index 478c1be..bbe71e5 100755
@@ -319,6 +319,11 @@ test_expect_success PERL 'say no to the second file' '
 	echo "$diff" | stdin_doesnot_contain br2
 '
 
+test_expect_success PERL 'difftool --tool-help' '
+	tool_help=$(git difftool --tool-help) &&
+	echo "$tool_help" | stdin_contains tool
+'
+
 test_expect_success PERL 'setup change in subdirectory' '
 	git checkout master &&
 	mkdir sub &&
Signature · 1 line
1.7.10.rc2.21.g8cb1a
#1 lore raw
Quoted 3 lines · Tim Henigan

Changes in v8:
- Replaced 'glob' with 'File::Find'. Glob will fail if file paths include spaces. Using File::Find overcomes that limitation.

OK, but doesn't File::Find recurse into its subdirectories? If you create a 'foo' directory there and drop a 'bar' script in it, is the rest of the code prepared to give you "git difftool -t foo/bar"?

Quoted 5 lines

- Added 'TOOL_MODE=DIFF' prior to calling 'git-mergetool--lib.sh'. This insures that the shell script executes as designed.
- difftool now calls 'can_diff' from 'git-mergetool--lib.sh' to insure that only tools that are capable of diffing are shown as valid options. For example, 'tortoisemerge' cannot be used as a diff viewer.

Good that you caught these brown-paper-bag bugs ;-) It feels a bit awkward that nobody pointed these out, even though the series has been queued in 'pu' since its early iterations.

Quoted 8 lines
git-difftool.perl +0 -0
index 0fa131c..15fd572 100755
@@ -38,6 +39,40 @@ USAGE
 	exit($exitcode);
 }
 

git-difftool.perl USAGE · line 51

Quoted 10 lines
git-difftool.perl +10 -0
+sub print_tool_help
+{
+	my ($cmd, @found, @notfound, @tools);
+	my $gitpath = Git::exec_path();
+
+	find(sub { push(@tools, $_) unless (-d $_) }, "$gitpath/mergetools");
+
+	for (@tools) {
+		my $tool = $_;
+		next if ($tool eq "defaults");

Now you use File::Find::find(), you probably should do this kind of trivial filtering inside the callback, no?

#2 lore raw
Quoted 5 lines · Junio C Hamano

Tim Henigan <[email protected]> writes:

OK, but doesn't File::Find recurse into its subdirectories?  If you create a 'foo' directory there and drop a 'bar' script in it, is the rest of the code prepared to give you "git difftool -t foo/bar"?

It does recurse, but in this context '$_' only contains the current file name within the directory...not the directory itself [1]. So if we call 'find' on a directory that contains:

foo
bar/
baz

then @tools = ('foo', 'baz')

[1]: http://perldoc.perl.org/File/Find.html#The-wanted-function

Quoted 6 lines
Quoted 3 lines

+     for (@tools) {
+             my $tool = $_;
+             next if ($tool eq "defaults");

Now you use File::Find::find(), you probably should do this kind of trivial filtering inside the callback, no?

I thought about that, but the filter is so simple that it seemed like overkill to add another function. If there is another revision of this patch, I will reconsider.

As always, thanks for the review :)

#3 lore raw
Quoted 11 lines · Tim Henigan
Quoted 5 lines · Junio C Hamano

Tim Henigan <[email protected]> writes:

OK, but doesn't File::Find recurse into its subdirectories?  If you create a 'foo' directory there and drop a 'bar' script in it, is the rest of the code prepared to give you "git difftool -t foo/bar"?

It does recurse, but in this context '$_' only contains the current file name within the directory...not the directory itself [1]. So if we call 'find' on a directory that contains:

Quoted 5 lines

foo
bar/
baz

then @tools = ('foo', 'baz')

That is even worse, no? Is the rest of the code prepared to give you "git difftool -t baz" in such a layout? What if you have another baz next to foo and bar?

What I was hinting at was that you may want to $File::Find::prune=1 when you find a subdirectory. While at it, you may also want to replace the "unless -d $_" with "if -f $_ && -x _" or something.

#4 lore raw
Quoted 8 lines · Junio C Hamano

That is even worse, no?  Is the rest of the code prepared to give you "git difftool -t baz" in such a layout?  What if you have another baz next to foo and bar?

What I was hinting at was that you may want to $File::Find::prune=1 when you find a subdirectory.  While at it, you may also want to replace the "unless -d $_" with "if -f $_ && -x _" or something.

I see now...sorry I missed your obvious point. I will look into this further.

The script makes some assumptions right now:
1) All tool config scripts must be located in "$(git --exec-path)/mergetools".
2) There is only one tool config in each script
- See my other other recent patch that splits vim/gvim [1]
3) The name of the tool script matches the name of the tool itself.

Do these assumptions seem reasonable? If so, perhaps I should add a README to the mergetools directory that outlines these assumptions?

[1]: http://thread.gmane.org/gmane.comp.version-control.git/194179