]> git.ipfire.org Git - thirdparty/git.git/blame - git-mergetool--lib.sh
mergetool--lib: improve show_tool_help() output
[thirdparty/git.git] / git-mergetool--lib.sh
CommitLineData
02e51243 1#!/bin/sh
21d0ba7e
DA
2# git-mergetool--lib is a library for common merge tool functions
3diff_mode() {
4 test "$TOOL_MODE" = diff
5}
6
7merge_mode() {
8 test "$TOOL_MODE" = merge
9}
10
11translate_merge_tool_path () {
bc7a96a8 12 echo "$1"
21d0ba7e
DA
13}
14
15check_unchanged () {
240dc3e8
DA
16 if test "$MERGED" -nt "$BACKUP"
17 then
21d0ba7e
DA
18 status=0
19 else
240dc3e8
DA
20 while true
21 do
21d0ba7e
DA
22 echo "$MERGED seems unchanged."
23 printf "Was the merge successful? [y/n] "
e622f41d 24 read answer || return 1
21d0ba7e
DA
25 case "$answer" in
26 y*|Y*) status=0; break ;;
27 n*|N*) status=1; break ;;
28 esac
29 done
30 fi
31}
32
bc7a96a8
DA
33valid_tool_config () {
34 if test -n "$(get_merge_tool_cmd "$1")"
35 then
36 return 0
37 else
38 return 1
39 fi
40}
41
21d0ba7e 42valid_tool () {
bc7a96a8
DA
43 setup_tool "$1" || valid_tool_config "$1"
44}
45
46setup_tool () {
21d0ba7e 47 case "$1" in
bc7a96a8
DA
48 vim*|gvim*)
49 tool=vim
21d0ba7e
DA
50 ;;
51 *)
bc7a96a8 52 tool="$1"
21d0ba7e
DA
53 ;;
54 esac
bc7a96a8
DA
55 mergetools="$(git --exec-path)/mergetools"
56
57 # Load the default definitions
58 . "$mergetools/defaults"
59 if ! test -f "$mergetools/$tool"
60 then
61 return 1
62 fi
63
64 # Load the redefined functions
65 . "$mergetools/$tool"
66
67 if merge_mode && ! can_merge
68 then
69 echo "error: '$tool' can not be used to resolve merges" >&2
70 exit 1
71 elif diff_mode && ! can_diff
72 then
73 echo "error: '$tool' can only be used to resolve merges" >&2
74 exit 1
75 fi
76 return 0
21d0ba7e
DA
77}
78
79get_merge_tool_cmd () {
47d65924 80 # Prints the custom command for a merge tool
bc7a96a8 81 merge_tool="$1"
240dc3e8
DA
82 if diff_mode
83 then
47d65924 84 echo "$(git config difftool.$merge_tool.cmd ||
285c6cbf 85 git config mergetool.$merge_tool.cmd)"
47d65924
DA
86 else
87 echo "$(git config mergetool.$merge_tool.cmd)"
88 fi
21d0ba7e
DA
89}
90
bc7a96a8 91# Entry point for running tools
21d0ba7e 92run_merge_tool () {
f9ad901f
DA
93 # If GIT_PREFIX is empty then we cannot use it in tools
94 # that expect to be able to chdir() to its value.
95 GIT_PREFIX=${GIT_PREFIX:-.}
96 export GIT_PREFIX
97
47d65924 98 merge_tool_path="$(get_merge_tool_path "$1")" || exit
21d0ba7e
DA
99 base_present="$2"
100 status=0
101
bc7a96a8
DA
102 # Bring tool-specific functions into scope
103 setup_tool "$1"
104
105 if merge_mode
106 then
a427ef7a 107 run_merge_cmd "$1"
bc7a96a8 108 else
a427ef7a 109 run_diff_cmd "$1"
bc7a96a8 110 fi
21d0ba7e
DA
111 return $status
112}
113
a427ef7a
DA
114# Run a either a configured or built-in diff tool
115run_diff_cmd () {
116 merge_tool_cmd="$(get_merge_tool_cmd "$1")"
117 if test -n "$merge_tool_cmd"
118 then
119 ( eval $merge_tool_cmd )
120 status=$?
121 return $status
122 else
123 diff_cmd "$1"
124 fi
125}
126
127# Run a either a configured or built-in merge tool
128run_merge_cmd () {
129 merge_tool_cmd="$(get_merge_tool_cmd "$1")"
130 if test -n "$merge_tool_cmd"
131 then
132 trust_exit_code="$(git config --bool \
133 mergetool."$1".trustExitCode || echo false)"
134 if test "$trust_exit_code" = "false"
135 then
136 touch "$BACKUP"
137 ( eval $merge_tool_cmd )
138 status=$?
139 check_unchanged
140 else
141 ( eval $merge_tool_cmd )
142 status=$?
143 fi
144 return $status
145 else
146 merge_cmd "$1"
147 fi
148}
149
109859e2 150list_merge_tool_candidates () {
240dc3e8
DA
151 if merge_mode
152 then
21d0ba7e
DA
153 tools="tortoisemerge"
154 else
155 tools="kompare"
156 fi
240dc3e8
DA
157 if test -n "$DISPLAY"
158 then
159 if test -n "$GNOME_DESKTOP_SESSION_ID"
160 then
21d0ba7e
DA
161 tools="meld opendiff kdiff3 tkdiff xxdiff $tools"
162 else
163 tools="opendiff kdiff3 tkdiff xxdiff meld $tools"
164 fi
755e8b3f 165 tools="$tools gvimdiff diffuse ecmerge p4merge araxis bc3 codecompare"
21d0ba7e 166 fi
7b104229
RS
167 case "${VISUAL:-$EDITOR}" in
168 *vim*)
21d0ba7e 169 tools="$tools vimdiff emerge"
7b104229
RS
170 ;;
171 *)
21d0ba7e 172 tools="$tools emerge vimdiff"
7b104229
RS
173 ;;
174 esac
109859e2
JH
175}
176
4a8273a3 177show_tool_help () {
4a8273a3
JK
178 unavailable= available= LF='
179'
88d3406a
DA
180
181 scriptlets="$(git --exec-path)"/mergetools
182 for i in "$scriptlets"/*
4a8273a3 183 do
88d3406a
DA
184 . "$scriptlets"/defaults
185 . "$i"
186
187 tool="$(basename "$i")"
188 if test "$tool" = "defaults"
189 then
190 continue
191 elif merge_mode && ! can_merge
192 then
193 continue
194 elif diff_mode && ! can_diff
195 then
196 continue
197 fi
198
199 merge_tool_path=$(translate_merge_tool_path "$tool")
4a8273a3
JK
200 if type "$merge_tool_path" >/dev/null 2>&1
201 then
88d3406a 202 available="$available$tool$LF"
4a8273a3 203 else
88d3406a 204 unavailable="$unavailable$tool$LF"
4a8273a3
JK
205 fi
206 done
62b6f7e0
JK
207
208 cmd_name=${TOOL_MODE}tool
4a8273a3
JK
209 if test -n "$available"
210 then
62b6f7e0 211 echo "'git $cmd_name --tool=<tool>' may be set to one of the following:"
4a8273a3
JK
212 echo "$available" | sort | sed -e 's/^/ /'
213 else
62b6f7e0 214 echo "No suitable tool for 'git $cmd_name --tool=<tool>' found."
4a8273a3
JK
215 fi
216 if test -n "$unavailable"
217 then
218 echo
219 echo 'The following tools are valid, but not currently available:'
220 echo "$unavailable" | sort | sed -e 's/^/ /'
221 fi
222 if test -n "$unavailable$available"
223 then
224 echo
225 echo "Some of the tools listed above only work in a windowed"
226 echo "environment. If run in a terminal-only session, they will fail."
227 fi
228 exit 0
229}
230
109859e2
JH
231guess_merge_tool () {
232 list_merge_tool_candidates
21d0ba7e
DA
233 echo >&2 "merge tool candidates: $tools"
234
235 # Loop over each candidate and stop when a valid merge tool is found.
236 for i in $tools
237 do
238 merge_tool_path="$(translate_merge_tool_path "$i")"
240dc3e8
DA
239 if type "$merge_tool_path" >/dev/null 2>&1
240 then
47d65924
DA
241 echo "$i"
242 return 0
21d0ba7e
DA
243 fi
244 done
245
47d65924
DA
246 echo >&2 "No known merge resolution program available."
247 return 1
21d0ba7e
DA
248}
249
250get_configured_merge_tool () {
251 # Diff mode first tries diff.tool and falls back to merge.tool.
252 # Merge mode only checks merge.tool
240dc3e8
DA
253 if diff_mode
254 then
47d65924
DA
255 merge_tool=$(git config diff.tool || git config merge.tool)
256 else
257 merge_tool=$(git config merge.tool)
21d0ba7e 258 fi
240dc3e8
DA
259 if test -n "$merge_tool" && ! valid_tool "$merge_tool"
260 then
21d0ba7e
DA
261 echo >&2 "git config option $TOOL_MODE.tool set to unknown tool: $merge_tool"
262 echo >&2 "Resetting to default..."
263 return 1
264 fi
47d65924 265 echo "$merge_tool"
21d0ba7e
DA
266}
267
268get_merge_tool_path () {
269 # A merge tool has been set, so verify that it's valid.
bc7a96a8 270 merge_tool="$1"
240dc3e8
DA
271 if ! valid_tool "$merge_tool"
272 then
21d0ba7e
DA
273 echo >&2 "Unknown merge tool $merge_tool"
274 exit 1
275 fi
240dc3e8
DA
276 if diff_mode
277 then
47d65924 278 merge_tool_path=$(git config difftool."$merge_tool".path ||
285c6cbf 279 git config mergetool."$merge_tool".path)
47d65924
DA
280 else
281 merge_tool_path=$(git config mergetool."$merge_tool".path)
21d0ba7e 282 fi
240dc3e8
DA
283 if test -z "$merge_tool_path"
284 then
47d65924 285 merge_tool_path="$(translate_merge_tool_path "$merge_tool")"
21d0ba7e 286 fi
47d65924 287 if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
240dc3e8
DA
288 ! type "$merge_tool_path" >/dev/null 2>&1
289 then
47d65924 290 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
285c6cbf 291 "'$merge_tool_path'"
21d0ba7e
DA
292 exit 1
293 fi
294 echo "$merge_tool_path"
295}
296
297get_merge_tool () {
21d0ba7e 298 # Check if a merge tool has been configured
bc7a96a8 299 merge_tool="$(get_configured_merge_tool)"
21d0ba7e 300 # Try to guess an appropriate merge tool if no tool has been set.
240dc3e8
DA
301 if test -z "$merge_tool"
302 then
47d65924 303 merge_tool="$(guess_merge_tool)" || exit
21d0ba7e
DA
304 fi
305 echo "$merge_tool"
306}