Skip to content

Commit 55f56fe

Browse files
committed
Merge branch 'jk/mergetool'
Cleans up mergetool/difftool combo. * jk/mergetool: mergetools: simplify how we handle "vim" and "defaults" mergetool--lib: don't call "exit" in setup_tool mergetool--lib: improve show_tool_help() output mergetools/vim: remove redundant diff command git-difftool: use git-mergetool--lib for "--tool-help" git-mergetool: don't hardcode 'mergetool' in show_tool_help git-mergetool: remove redundant assignment git-mergetool: move show_tool_help to mergetool--lib
2 parents b9a5f68 + 073678b commit 55f56fe

File tree

8 files changed

+100
-133
lines changed

8 files changed

+100
-133
lines changed

git-difftool.perl

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -59,57 +59,16 @@ sub find_worktree
5959
return $worktree;
6060
}
6161

62-
sub filter_tool_scripts
63-
{
64-
my ($tools) = @_;
65-
if (-d $_) {
66-
if ($_ ne ".") {
67-
# Ignore files in subdirectories
68-
$File::Find::prune = 1;
69-
}
70-
} else {
71-
if ((-f $_) && ($_ ne "defaults")) {
72-
push(@$tools, $_);
73-
}
74-
}
75-
}
76-
7762
sub print_tool_help
7863
{
79-
my ($cmd, @found, @notfound, @tools);
80-
my $gitpath = Git::exec_path();
81-
82-
find(sub { filter_tool_scripts(\@tools) }, "$gitpath/mergetools");
83-
84-
foreach my $tool (@tools) {
85-
$cmd = "TOOL_MODE=diff";
86-
$cmd .= ' && . "$(git --exec-path)/git-mergetool--lib"';
87-
$cmd .= " && get_merge_tool_path $tool >/dev/null 2>&1";
88-
$cmd .= " && can_diff >/dev/null 2>&1";
89-
if (system('sh', '-c', $cmd) == 0) {
90-
push(@found, $tool);
91-
} else {
92-
push(@notfound, $tool);
93-
}
94-
}
95-
96-
print << 'EOF';
97-
'git difftool --tool=<tool>' may be set to one of the following:
98-
EOF
99-
print "\t$_\n" for (sort(@found));
64+
my $cmd = 'TOOL_MODE=diff';
65+
$cmd .= ' && . "$(git --exec-path)/git-mergetool--lib"';
66+
$cmd .= ' && show_tool_help';
10067

101-
print << 'EOF';
102-
103-
The following tools are valid, but not currently available:
104-
EOF
105-
print "\t$_\n" for (sort(@notfound));
106-
107-
print << 'EOF';
108-
109-
NOTE: Some of the tools listed above only work in a windowed
110-
environment. If run in a terminal-only session, they will fail.
111-
EOF
112-
exit(0);
68+
# See the comment at the bottom of file_diff() for the reason behind
69+
# using system() followed by exit() instead of exec().
70+
my $rc = system('sh', '-c', $cmd);
71+
exit($rc | ($rc >> 8));
11372
}
11473

11574
sub exit_cleanup

git-mergetool--lib.sh

Lines changed: 88 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/bin/sh
22
# git-mergetool--lib is a library for common merge tool functions
3+
MERGE_TOOLS_DIR=$(git --exec-path)/mergetools
4+
35
diff_mode() {
46
test "$TOOL_MODE" = diff
57
}
@@ -44,34 +46,51 @@ valid_tool () {
4446
}
4547

4648
setup_tool () {
47-
case "$1" in
48-
vim*|gvim*)
49-
tool=vim
50-
;;
51-
*)
52-
tool="$1"
53-
;;
54-
esac
55-
mergetools="$(git --exec-path)/mergetools"
49+
tool="$1"
50+
51+
# Fallback definitions, to be overriden by tools.
52+
can_merge () {
53+
return 0
54+
}
55+
56+
can_diff () {
57+
return 0
58+
}
59+
60+
diff_cmd () {
61+
status=1
62+
return $status
63+
}
5664

57-
# Load the default definitions
58-
. "$mergetools/defaults"
59-
if ! test -f "$mergetools/$tool"
65+
merge_cmd () {
66+
status=1
67+
return $status
68+
}
69+
70+
translate_merge_tool_path () {
71+
echo "$1"
72+
}
73+
74+
if ! test -f "$MERGE_TOOLS_DIR/$tool"
6075
then
61-
return 1
76+
# Use a special return code for this case since we want to
77+
# source "defaults" even when an explicit tool path is
78+
# configured since the user can use that to override the
79+
# default path in the scriptlet.
80+
return 2
6281
fi
6382

6483
# Load the redefined functions
65-
. "$mergetools/$tool"
84+
. "$MERGE_TOOLS_DIR/$tool"
6685

6786
if merge_mode && ! can_merge
6887
then
6988
echo "error: '$tool' can not be used to resolve merges" >&2
70-
exit 1
89+
return 1
7190
elif diff_mode && ! can_diff
7291
then
7392
echo "error: '$tool' can only be used to resolve merges" >&2
74-
exit 1
93+
return 1
7594
fi
7695
return 0
7796
}
@@ -101,6 +120,19 @@ run_merge_tool () {
101120

102121
# Bring tool-specific functions into scope
103122
setup_tool "$1"
123+
exitcode=$?
124+
case $exitcode in
125+
0)
126+
:
127+
;;
128+
2)
129+
# The configured tool is not a built-in tool.
130+
test -n "$merge_tool_path" || return 1
131+
;;
132+
*)
133+
return $exitcode
134+
;;
135+
esac
104136

105137
if merge_mode
106138
then
@@ -174,6 +206,46 @@ list_merge_tool_candidates () {
174206
esac
175207
}
176208

209+
show_tool_help () {
210+
unavailable= available= LF='
211+
'
212+
for i in "$MERGE_TOOLS_DIR"/*
213+
do
214+
tool=$(basename "$i")
215+
setup_tool "$tool" 2>/dev/null || continue
216+
217+
merge_tool_path=$(translate_merge_tool_path "$tool")
218+
if type "$merge_tool_path" >/dev/null 2>&1
219+
then
220+
available="$available$tool$LF"
221+
else
222+
unavailable="$unavailable$tool$LF"
223+
fi
224+
done
225+
226+
cmd_name=${TOOL_MODE}tool
227+
if test -n "$available"
228+
then
229+
echo "'git $cmd_name --tool=<tool>' may be set to one of the following:"
230+
echo "$available" | sort | sed -e 's/^/ /'
231+
else
232+
echo "No suitable tool for 'git $cmd_name --tool=<tool>' found."
233+
fi
234+
if test -n "$unavailable"
235+
then
236+
echo
237+
echo 'The following tools are valid, but not currently available:'
238+
echo "$unavailable" | sort | sed -e 's/^/ /'
239+
fi
240+
if test -n "$unavailable$available"
241+
then
242+
echo
243+
echo "Some of the tools listed above only work in a windowed"
244+
echo "environment. If run in a terminal-only session, they will fail."
245+
fi
246+
exit 0
247+
}
248+
177249
guess_merge_tool () {
178250
list_merge_tool_candidates
179251
echo >&2 "merge tool candidates: $tools"

git-mergetool.sh

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -315,43 +315,6 @@ merge_file () {
315315
return 0
316316
}
317317

318-
show_tool_help () {
319-
TOOL_MODE=merge
320-
list_merge_tool_candidates
321-
unavailable= available= LF='
322-
'
323-
for i in $tools
324-
do
325-
merge_tool_path=$(translate_merge_tool_path "$i")
326-
if type "$merge_tool_path" >/dev/null 2>&1
327-
then
328-
available="$available$i$LF"
329-
else
330-
unavailable="$unavailable$i$LF"
331-
fi
332-
done
333-
if test -n "$available"
334-
then
335-
echo "'git mergetool --tool=<tool>' may be set to one of the following:"
336-
echo "$available" | sort | sed -e 's/^/ /'
337-
else
338-
echo "No suitable tool for 'git mergetool --tool=<tool>' found."
339-
fi
340-
if test -n "$unavailable"
341-
then
342-
echo
343-
echo 'The following tools are valid, but not currently available:'
344-
echo "$unavailable" | sort | sed -e 's/^/ /'
345-
fi
346-
if test -n "$unavailable$available"
347-
then
348-
echo
349-
echo "Some of the tools listed above only work in a windowed"
350-
echo "environment. If run in a terminal-only session, they will fail."
351-
fi
352-
exit 0
353-
}
354-
355318
prompt=$(git config --bool mergetool.prompt || echo true)
356319

357320
while test $# != 0

mergetools/defaults

Lines changed: 0 additions & 22 deletions
This file was deleted.

mergetools/gvimdiff

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
. "$MERGE_TOOLS_DIR/vimdiff"

mergetools/gvimdiff2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
. "$MERGE_TOOLS_DIR/vimdiff"

mergetools/vim renamed to mergetools/vimdiff

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
diff_cmd () {
2-
case "$1" in
3-
gvimdiff|vimdiff)
4-
"$merge_tool_path" -R -f -d \
5-
-c 'wincmd l' -c 'cd $GIT_PREFIX' "$LOCAL" "$REMOTE"
6-
;;
7-
gvimdiff2|vimdiff2)
8-
"$merge_tool_path" -R -f -d \
9-
-c 'wincmd l' -c 'cd $GIT_PREFIX' "$LOCAL" "$REMOTE"
10-
;;
11-
esac
2+
"$merge_tool_path" -R -f -d \
3+
-c 'wincmd l' -c 'cd $GIT_PREFIX' "$LOCAL" "$REMOTE"
124
}
135

146
merge_cmd () {

mergetools/vimdiff2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
. "$MERGE_TOOLS_DIR/vimdiff"

0 commit comments

Comments
 (0)