|
1 | 1 | #!/bin/sh |
2 | 2 | # git-mergetool--lib is a library for common merge tool functions |
| 3 | +MERGE_TOOLS_DIR=$(git --exec-path)/mergetools |
| 4 | + |
3 | 5 | diff_mode() { |
4 | 6 | test "$TOOL_MODE" = diff |
5 | 7 | } |
@@ -44,34 +46,51 @@ valid_tool () { |
44 | 46 | } |
45 | 47 |
|
46 | 48 | 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 | + } |
56 | 64 |
|
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" |
60 | 75 | 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 |
62 | 81 | fi |
63 | 82 |
|
64 | 83 | # Load the redefined functions |
65 | | - . "$mergetools/$tool" |
| 84 | + . "$MERGE_TOOLS_DIR/$tool" |
66 | 85 |
|
67 | 86 | if merge_mode && ! can_merge |
68 | 87 | then |
69 | 88 | echo "error: '$tool' can not be used to resolve merges" >&2 |
70 | | - exit 1 |
| 89 | + return 1 |
71 | 90 | elif diff_mode && ! can_diff |
72 | 91 | then |
73 | 92 | echo "error: '$tool' can only be used to resolve merges" >&2 |
74 | | - exit 1 |
| 93 | + return 1 |
75 | 94 | fi |
76 | 95 | return 0 |
77 | 96 | } |
@@ -101,6 +120,19 @@ run_merge_tool () { |
101 | 120 |
|
102 | 121 | # Bring tool-specific functions into scope |
103 | 122 | 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 |
104 | 136 |
|
105 | 137 | if merge_mode |
106 | 138 | then |
@@ -174,6 +206,46 @@ list_merge_tool_candidates () { |
174 | 206 | esac |
175 | 207 | } |
176 | 208 |
|
| 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 | + |
177 | 249 | guess_merge_tool () { |
178 | 250 | list_merge_tool_candidates |
179 | 251 | echo >&2 "merge tool candidates: $tools" |
|
0 commit comments