mergetool--lib: refactor {diff,merge}_cmd logic

Instead of needing a wrapper to call the diff/merge command, simply provide the diff_cmd and merge_cmd functions for user-specified tools in the same way as we do for built-in tools. Signed-off-by: John Keeping <john@keeping.me.uk> Acked-by: David Aguilar <davvid@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

John Keeping committed Jun 16, 2013 at 18:51 UTC d2512fc94fe75d8836a6fc965951d8180dbddbe6
1 file changed +35 -47
git-mergetool--lib.sh
+35 -47
@@ -114,6 +114,33 @@ valid_tool () {
114 test -n "$cmd"
115 }
116
117 +setup_user_tool () {
118 + merge_tool_cmd=$(get_merge_tool_cmd "$tool")
119 + test -n "$merge_tool_cmd" || return 1
120 +
121 + diff_cmd () {
122 + ( eval $merge_tool_cmd )
123 + status=$?
124 + return $status
125 + }
126 +
127 + merge_cmd () {
128 + trust_exit_code=$(git config --bool \
129 + "mergetool.$1.trustExitCode" || echo false)
130 + if test "$trust_exit_code" = "false"
131 + then
132 + touch "$BACKUP"
133 + ( eval $merge_tool_cmd )
134 + status=$?
135 + check_unchanged
136 + else
137 + ( eval $merge_tool_cmd )
138 + status=$?
139 + fi
140 + return $status
141 + }
142 +}
143 +
144 setup_tool () {
145 tool="$1"
146
@@ -142,15 +169,15 @@ setup_tool () {
169
170 if ! test -f "$MERGE_TOOLS_DIR/$tool"
171 then
145 - # Use a special return code for this case since we want to
146 - # source "defaults" even when an explicit tool path is
147 - # configured since the user can use that to override the
148 - # default path in the scriptlet.
149 - return 2
172 + setup_user_tool
173 + return $?
174 fi
175
176 # Load the redefined functions
177 . "$MERGE_TOOLS_DIR/$tool"
178 + # Now let the user override the default command for the tool. If
179 + # they have not done so then this will return 1 which we ignore.
180 + setup_user_tool
181
182 if merge_mode && ! can_merge
183 then
@@ -187,20 +214,7 @@ run_merge_tool () {
214 status=0
215
216 # Bring tool-specific functions into scope
190 - setup_tool "$1"
191 - exitcode=$?
192 - case $exitcode in
193 - 0)
194 - :
195 - ;;
196 - 2)
197 - # The configured tool is not a built-in tool.
198 - test -n "$merge_tool_path" || return 1
199 - ;;
200 - *)
201 - return $exitcode
202 - ;;
203 - esac
217 + setup_tool "$1" || return 1
218
219 if merge_mode
220 then
@@ -213,38 +227,12 @@ run_merge_tool () {
227
228 # Run a either a configured or built-in diff tool
229 run_diff_cmd () {
216 - merge_tool_cmd=$(get_merge_tool_cmd "$1")
217 - if test -n "$merge_tool_cmd"
218 - then
219 - ( eval $merge_tool_cmd )
220 - status=$?
221 - return $status
222 - else
223 - diff_cmd "$1"
224 - fi
230 + diff_cmd "$1"
231 }
232
233 # Run a either a configured or built-in merge tool
234 run_merge_cmd () {
229 - merge_tool_cmd=$(get_merge_tool_cmd "$1")
230 - if test -n "$merge_tool_cmd"
231 - then
232 - trust_exit_code=$(git config --bool \
233 - "mergetool.$1.trustExitCode" || echo false)
234 - if test "$trust_exit_code" = "false"
235 - then
236 - touch "$BACKUP"
237 - ( eval $merge_tool_cmd )
238 - status=$?
239 - check_unchanged
240 - else
241 - ( eval $merge_tool_cmd )
242 - status=$?
243 - fi
244 - return $status
245 - else
246 - merge_cmd "$1"
247 - fi
235 + merge_cmd "$1"
236 }
237
238 list_merge_tool_candidates () {