@matheusbsbs / matheus-bagliano / commits / 47d65924a6
Fork 0

mergetool--lib: simplify API usage by removing more global variables

The mergetool--lib scriplet was tricky to use because it relied upon the existance of several global shell variables. This removes more global variables so that things are simpler for callers. A side effect is that some variables are recomputed each time run_merge_tool() is called, but the overhead for recomputing them is justified by the simpler implementation. Signed-off-by: David Aguilar <davvid@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

David Aguilar committed Apr 11, 2009 at 20:41 UTC 47d65924a69576bd9f3254f7055de6b37a359596
4 files changed +76 -70
Documentation/git-mergetool--lib.txt
+7 -9
@@ -7,7 +7,7 @@ git-mergetool--lib - Common git merge tool shell scriptlets
7
8 SYNOPSIS
9 --------
10 -'. "$(git --exec-path)/git-mergetool--lib"'
10 +'TOOL_MODE=(diff|merge) . "$(git --exec-path)/git-mergetool--lib"'
11
12 DESCRIPTION
13 -----------
@@ -20,14 +20,14 @@ The 'git-mergetool--lib' scriptlet is designed to be sourced (using
20 `.`) by other shell scripts to set up functions for working
21 with git merge tools.
22
23 -Before sourcing it, your script should set up a few variables;
24 -`TOOL_MODE` is used to define the operation mode for various
25 -functions. 'diff' and 'merge' are valid values.
23 +Before sourcing 'git-mergetool--lib', your script must set `TOOL_MODE`
24 +to define the operation mode for the functions listed below.
25 +'diff' and 'merge' are valid values.
26
27 FUNCTIONS
28 ---------
29 get_merge_tool::
30 - returns a merge tool
30 + returns a merge tool.
31
32 get_merge_tool_cmd::
33 returns the custom command for a merge tool.
@@ -38,10 +38,8 @@ get_merge_tool_path::
38 run_merge_tool::
39 launches a merge tool given the tool name and a true/false
40 flag to indicate whether a merge base is present.
41 - '$merge_tool', '$merge_tool_path', and for custom commands,
42 - '$merge_tool_cmd', must be defined prior to calling
43 - run_merge_tool. Additionally, '$MERGED', '$LOCAL', '$REMOTE',
44 - and '$BASE' must be defined for use by the merge tool.
41 + '$MERGED', '$LOCAL', '$REMOTE', and '$BASE' must be defined
42 + for use by the merge tool.
43
44 Author
45 ------
git-difftool--helper.sh
+3 -3
@@ -47,9 +47,9 @@ launch_merge_tool () {
47 test -n "$GIT_MERGE_TOOL" && merge_tool="$GIT_MERGE_TOOL"
48 test -n "$GIT_DIFF_TOOL" && merge_tool="$GIT_DIFF_TOOL"
49
50 -merge_tool=$(get_merge_tool "$merge_tool") || exit
51 -merge_tool_cmd="$(get_merge_tool_cmd "$merge_tool")"
52 -merge_tool_path="$(get_merge_tool_path "$merge_tool")" || exit
50 +if test -z "$merge_tool"; then
51 + merge_tool="$(get_merge_tool)" || exit
52 +fi
53
54 # Launch the merge tool on each path provided by 'git diff'
55 while test $# -gt 6
git-mergetool--lib.sh
+58 -51
@@ -8,25 +8,20 @@ merge_mode() {
8 }
9
10 translate_merge_tool_path () {
11 - if test -n "$2"; then
12 - echo "$2"
13 - else
14 - case "$1" in
15 - vimdiff)
16 - path=vim
17 - ;;
18 - gvimdiff)
19 - path=gvim
20 - ;;
21 - emerge)
22 - path=emacs
23 - ;;
24 - *)
25 - path="$1"
26 - ;;
27 - esac
28 - echo "$path"
29 - fi
11 + case "$1" in
12 + vimdiff)
13 + echo vim
14 + ;;
15 + gvimdiff)
16 + echo gvim
17 + ;;
18 + emerge)
19 + echo emacs
20 + ;;
21 + *)
22 + echo "$1"
23 + ;;
24 + esac
25 }
26
27 check_unchanged () {
@@ -69,15 +64,22 @@ valid_tool () {
64 }
65
66 get_merge_tool_cmd () {
72 - diff_mode &&
73 - custom_cmd="$(git config difftool.$1.cmd)"
74 - test -z "$custom_cmd" &&
75 - custom_cmd="$(git config mergetool.$1.cmd)"
76 - test -n "$custom_cmd" &&
77 - echo "$custom_cmd"
67 + # Prints the custom command for a merge tool
68 + if test -n "$1"; then
69 + merge_tool="$1"
70 + else
71 + merge_tool="$(get_merge_tool)"
72 + fi
73 + if diff_mode; then
74 + echo "$(git config difftool.$merge_tool.cmd ||
75 + git config mergetool.$merge_tool.cmd)"
76 + else
77 + echo "$(git config mergetool.$merge_tool.cmd)"
78 + fi
79 }
80
81 run_merge_tool () {
82 + merge_tool_path="$(get_merge_tool_path "$1")" || exit
83 base_present="$2"
84 status=0
85
@@ -103,9 +105,9 @@ run_merge_tool () {
105 status=$?
106 else
107 ("$merge_tool_path" --auto \
106 - --L1 "$MERGED (A)" \
107 - --L2 "$MERGED (B)" "$LOCAL" "$REMOTE" \
108 - > /dev/null 2>&1)
108 + --L1 "$MERGED (A)" \
109 + --L2 "$MERGED (B)" "$LOCAL" "$REMOTE" \
110 + > /dev/null 2>&1)
111 fi
112 ;;
113 kompare)
@@ -262,6 +264,7 @@ run_merge_tool () {
264 fi
265 ;;
266 *)
267 + merge_tool_cmd="$(get_merge_tool_cmd "$1")"
268 if test -z "$merge_tool_cmd"; then
269 if merge_mode; then
270 status=1
@@ -269,7 +272,9 @@ run_merge_tool () {
272 break
273 fi
274 if merge_mode; then
272 - if test "$merge_tool_trust_exit_code" = "false"; then
275 + trust_exit_code="$(git config --bool \
276 + mergetool."$1".trustExitCode || echo false)"
277 + if test "$trust_exit_code" = "false"; then
278 touch "$BACKUP"
279 ( eval $merge_tool_cmd )
280 check_unchanged
@@ -315,64 +320,66 @@ guess_merge_tool () {
320 do
321 merge_tool_path="$(translate_merge_tool_path "$i")"
322 if type "$merge_tool_path" > /dev/null 2>&1; then
318 - merge_tool="$i"
319 - break
323 + echo "$i"
324 + return 0
325 fi
326 done
327
323 - if test -z "$merge_tool" ; then
324 - echo >&2 "No known merge resolution program available."
325 - return 1
326 - fi
327 - echo "$merge_tool"
328 + echo >&2 "No known merge resolution program available."
329 + return 1
330 }
331
332 get_configured_merge_tool () {
333 # Diff mode first tries diff.tool and falls back to merge.tool.
334 # Merge mode only checks merge.tool
335 if diff_mode; then
334 - tool=$(git config diff.tool)
335 - fi
336 - if test -z "$tool"; then
337 - tool=$(git config merge.tool)
336 + merge_tool=$(git config diff.tool || git config merge.tool)
337 + else
338 + merge_tool=$(git config merge.tool)
339 fi
340 if test -n "$merge_tool" && ! valid_tool "$merge_tool"; then
341 echo >&2 "git config option $TOOL_MODE.tool set to unknown tool: $merge_tool"
342 echo >&2 "Resetting to default..."
343 return 1
344 fi
344 - echo "$tool"
345 + echo "$merge_tool"
346 }
347
348 get_merge_tool_path () {
349 # A merge tool has been set, so verify that it's valid.
350 + if test -n "$1"; then
351 + merge_tool="$1"
352 + else
353 + merge_tool="$(get_merge_tool)"
354 + fi
355 if ! valid_tool "$merge_tool"; then
356 echo >&2 "Unknown merge tool $merge_tool"
357 exit 1
358 fi
359 if diff_mode; then
354 - merge_tool_path=$(git config difftool."$merge_tool".path)
360 + merge_tool_path=$(git config difftool."$merge_tool".path ||
361 + git config mergetool."$merge_tool".path)
362 + else
363 + merge_tool_path=$(git config mergetool."$merge_tool".path)
364 fi
365 if test -z "$merge_tool_path"; then
357 - merge_tool_path=$(git config mergetool."$merge_tool".path)
366 + merge_tool_path="$(translate_merge_tool_path "$merge_tool")"
367 fi
359 - merge_tool_path="$(translate_merge_tool_path "$merge_tool" "$merge_tool_path")"
360 - if test -z "$merge_tool_cmd" && ! type "$merge_tool_path" > /dev/null 2>&1; then
361 - echo >&2 "The $TOOL_MODE tool $merge_tool is not available as '$merge_tool_path'"
368 + if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
369 + ! type "$merge_tool_path" > /dev/null 2>&1; then
370 + echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
371 + "'$merge_tool_path'"
372 exit 1
373 fi
374 echo "$merge_tool_path"
375 }
376
377 get_merge_tool () {
368 - merge_tool="$1"
378 # Check if a merge tool has been configured
370 - if test -z "$merge_tool"; then
371 - merge_tool=$(get_configured_merge_tool)
372 - fi
379 + merge_tool=$(get_configured_merge_tool)
380 # Try to guess an appropriate merge tool if no tool has been set.
381 if test -z "$merge_tool"; then
375 - merge_tool=$(guess_merge_tool) || exit
382 + merge_tool="$(guess_merge_tool)" || exit
383 fi
384 echo "$merge_tool"
385 }
git-mergetool.sh
+8 -7
@@ -174,9 +174,11 @@ merge_file () {
174 read ans
175 fi
176
177 - present=false
178 - base_present &&
179 - present=true
177 + if base_present; then
178 + present=true
179 + else
180 + present=false
181 + fi
182
183 if ! run_merge_tool "$merge_tool" "$present"; then
184 echo "merge of $MERGED failed" 1>&2
@@ -254,12 +256,11 @@ prompt_after_failed_merge() {
256 done
257 }
258
257 -merge_tool=$(get_merge_tool "$merge_tool") || exit
258 -merge_tool_cmd="$(get_merge_tool_cmd "$merge_tool")"
259 -merge_tool_path="$(get_merge_tool_path "$merge_tool")" || exit
259 +if test -z "$merge_tool"; then
260 + merge_tool=$(get_merge_tool "$merge_tool") || exit
261 +fi
262 merge_keep_backup="$(git config --bool mergetool.keepBackup || echo true)"
263 merge_keep_temporaries="$(git config --bool mergetool.keepTemporaries || echo false)"
262 -merge_tool_trust_exit_code="$(git config --bool mergetool."$merge_tool".trustExitCode || echo false)"
264
265 last_status=0
266 rollup_status=0