mergetool: use path to mergetool in config var mergetool.<tool>.path
This commit adds a mechanism to provide absolute paths to the external programs called by 'git mergetool'. A path can be specified in the configuation variable mergetool.<tool>.path. The configuration variable is similar to how we name branches and remotes. It is extensible if we need to specify more details about a tool. The mechanism is especially useful on Windows, where external programs are unlikely to be in PATH. [sp: Fixed a few minor issues prior to applying] Signed-off-by: Steffen Prohaska <prohaska@zib.de> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Steffen Prohaska committed
Oct 17, 2007 at 19:16 UTC
e3fa2c761fdc490494e8e0855bcee4d7e58ada6a
2 files changed
+60
-43
Documentation/git-mergetool.txt
+6
@@ -31,6 +31,12 @@ If a merge resolution program is not specified, 'git mergetool'
31
will use the configuration variable merge.tool. If the
32
configuration variable merge.tool is not set, 'git mergetool'
33
will pick a suitable default.
34
++
35
+You can explicitly provide a full path to the tool by setting the
36
+configuration variable mergetool.<tool>.path. For example, you
37
+can configure the absolute path to kdiff3 by setting
38
+mergetool.kdiff3.path. Otherwise, 'git mergetool' assumes the tool
39
+is available in PATH.
40
41
Author
42
------
git-mergetool.sh
+54
-43
@@ -192,10 +192,10 @@ merge_file () {
192
case "$merge_tool" in
193
kdiff3)
194
if base_present ; then
195
- (kdiff3 --auto --L1 "$path (Base)" --L2 "$path (Local)" --L3 "$path (Remote)" \
195
+ ("$merge_tool_path" --auto --L1 "$path (Base)" --L2 "$path (Local)" --L3 "$path (Remote)" \
196
-o "$path" -- "$BASE" "$LOCAL" "$REMOTE" > /dev/null 2>&1)
197
else
198
- (kdiff3 --auto --L1 "$path (Local)" --L2 "$path (Remote)" \
198
+ ("$merge_tool_path" --auto --L1 "$path (Local)" --L2 "$path (Remote)" \
199
-o "$path" -- "$LOCAL" "$REMOTE" > /dev/null 2>&1)
200
fi
201
status=$?
@@ -203,35 +203,35 @@ merge_file () {
203
;;
204
tkdiff)
205
if base_present ; then
206
- tkdiff -a "$BASE" -o "$path" -- "$LOCAL" "$REMOTE"
206
+ "$merge_tool_path" -a "$BASE" -o "$path" -- "$LOCAL" "$REMOTE"
207
else
208
- tkdiff -o "$path" -- "$LOCAL" "$REMOTE"
208
+ "$merge_tool_path" -o "$path" -- "$LOCAL" "$REMOTE"
209
fi
210
status=$?
211
save_backup
212
;;
213
meld|vimdiff)
214
touch "$BACKUP"
215
- $merge_tool -- "$LOCAL" "$path" "$REMOTE"
215
+ "$merge_tool_path" -- "$LOCAL" "$path" "$REMOTE"
216
check_unchanged
217
save_backup
218
;;
219
gvimdiff)
220
touch "$BACKUP"
221
- gvimdiff -f -- "$LOCAL" "$path" "$REMOTE"
221
+ "$merge_tool_path" -f -- "$LOCAL" "$path" "$REMOTE"
222
check_unchanged
223
save_backup
224
;;
225
xxdiff)
226
touch "$BACKUP"
227
if base_present ; then
228
- xxdiff -X --show-merged-pane \
228
+ "$merge_tool_path" -X --show-merged-pane \
229
-R 'Accel.SaveAsMerged: "Ctrl-S"' \
230
-R 'Accel.Search: "Ctrl+F"' \
231
-R 'Accel.SearchForward: "Ctrl-G"' \
232
--merged-file "$path" -- "$LOCAL" "$BASE" "$REMOTE"
233
else
234
- xxdiff -X --show-merged-pane \
234
+ "$merge_tool_path" -X --show-merged-pane \
235
-R 'Accel.SaveAsMerged: "Ctrl-S"' \
236
-R 'Accel.Search: "Ctrl+F"' \
237
-R 'Accel.SearchForward: "Ctrl-G"' \
@@ -243,18 +243,18 @@ merge_file () {
243
opendiff)
244
touch "$BACKUP"
245
if base_present; then
246
- opendiff "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$path" | cat
246
+ "$merge_tool_path" "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$path" | cat
247
else
248
- opendiff "$LOCAL" "$REMOTE" -merge "$path" | cat
248
+ "$merge_tool_path" "$LOCAL" "$REMOTE" -merge "$path" | cat
249
fi
250
check_unchanged
251
save_backup
252
;;
253
emerge)
254
if base_present ; then
255
- emacs -f emerge-files-with-ancestor-command "$LOCAL" "$REMOTE" "$BASE" "$(basename "$path")"
255
+ "$merge_tool_path" -f emerge-files-with-ancestor-command "$LOCAL" "$REMOTE" "$BASE" "$(basename "$path")"
256
else
257
- emacs -f emerge-files-command "$LOCAL" "$REMOTE" "$(basename "$path")"
257
+ "$merge_tool_path" -f emerge-files-command "$LOCAL" "$REMOTE" "$(basename "$path")"
258
fi
259
status=$?
260
save_backup
@@ -297,17 +297,38 @@ do
297
shift
298
done
299
300
+valid_tool() {
301
+ case "$1" in
302
+ kdiff3 | tkdiff | xxdiff | meld | opendiff | emerge | vimdiff | gvimdiff)
303
+ ;; # happy
304
+ *)
305
+ return 1
306
+ ;;
307
+ esac
308
+}
309
+
310
+init_merge_tool_path() {
311
+ merge_tool_path=`git config mergetool.$1.path`
312
+ if test -z "$merge_tool_path" ; then
313
+ case "$1" in
314
+ emerge)
315
+ merge_tool_path=emacs
316
+ ;;
317
+ *)
318
+ merge_tool_path=$1
319
+ ;;
320
+ esac
321
+ fi
322
+}
323
+
324
+
325
if test -z "$merge_tool"; then
326
merge_tool=`git config merge.tool`
302
- case "$merge_tool" in
303
- kdiff3 | tkdiff | xxdiff | meld | opendiff | emerge | vimdiff | gvimdiff | "")
304
- ;; # happy
305
- *)
327
+ if ! valid_tool "$merge_tool"; then
328
echo >&2 "git config option merge.tool set to unknown tool: $merge_tool"
329
echo >&2 "Resetting to default..."
330
unset merge_tool
309
- ;;
310
- esac
331
+ fi
332
fi
333
334
if test -z "$merge_tool" ; then
@@ -329,40 +350,30 @@ if test -z "$merge_tool" ; then
350
merge_tool_candidates="$merge_tool_candidates opendiff emerge vimdiff"
351
echo "merge tool candidates: $merge_tool_candidates"
352
for i in $merge_tool_candidates; do
332
- if test $i = emerge ; then
333
- cmd=emacs
334
- else
335
- cmd=$i
336
- fi
337
- if type $cmd > /dev/null 2>&1; then
353
+ init_merge_tool_path $i
354
+ if type "$merge_tool_path" > /dev/null 2>&1; then
355
merge_tool=$i
356
break
357
fi
358
done
359
if test -z "$merge_tool" ; then
343
- echo "No available merge resolution programs available."
360
+ echo "No known merge resolution program available."
361
exit 1
362
fi
363
+else
364
+ if ! valid_tool "$merge_tool"; then
365
+ echo >&2 "Unknown merge_tool $merge_tool"
366
+ exit 1
367
+ fi
368
+
369
+ init_merge_tool_path "$merge_tool"
370
+
371
+ if ! type "$merge_tool_path" > /dev/null 2>&1; then
372
+ echo "The merge tool $merge_tool is not available as '$merge_tool_path'"
373
+ exit 1
374
+ fi
375
fi
376
348
-case "$merge_tool" in
349
- kdiff3|tkdiff|meld|xxdiff|vimdiff|gvimdiff|opendiff)
350
- if ! type "$merge_tool" > /dev/null 2>&1; then
351
- echo "The merge tool $merge_tool is not available"
352
- exit 1
353
- fi
354
- ;;
355
- emerge)
356
- if ! type "emacs" > /dev/null 2>&1; then
357
- echo "Emacs is not available"
358
- exit 1
359
- fi
360
- ;;
361
- *)
362
- echo "Unknown merge tool: $merge_tool"
363
- exit 1
364
- ;;
365
-esac
377
378
if test $# -eq 0 ; then
379
files=`git ls-files -u | sed -e 's/^[^ ]* //' | sort -u`