]> git.ipfire.org Git - thirdparty/git.git/blame - git-mergetool--lib.sh
Sync with 2.4.3
[thirdparty/git.git] / git-mergetool--lib.sh
CommitLineData
11d62145 1# git-mergetool--lib is a shell library for common merge tool functions
f35ec546
DA
2
3: ${MERGE_TOOLS_DIR=$(git --exec-path)/mergetools}
073678b8 4
719518f5
DA
5IFS='
6'
7
17a1f1c5
DA
8mode_ok () {
9 if diff_mode
10 then
11 can_diff
12 elif merge_mode
13 then
14 can_merge
15 else
16 false
17 fi
18}
19
20is_available () {
21 merge_tool_path=$(translate_merge_tool_path "$1") &&
22 type "$merge_tool_path" >/dev/null 2>&1
23}
24
665682c9
JK
25list_config_tools () {
26 section=$1
27 line_prefix=${2:-}
28
29 git config --get-regexp $section'\..*\.cmd' |
30 while read -r key value
31 do
32 toolname=${key#$section.}
33 toolname=${toolname%.cmd}
34
35 printf "%s%s\n" "$line_prefix" "$toolname"
36 done
37}
38
17a1f1c5
DA
39show_tool_names () {
40 condition=${1:-true} per_line_prefix=${2:-} preamble=${3:-}
41 not_found_msg=${4:-}
665682c9 42 extra_content=${5:-}
17a1f1c5
DA
43
44 shown_any=
45 ( cd "$MERGE_TOOLS_DIR" && ls ) | {
46 while read toolname
47 do
48 if setup_tool "$toolname" 2>/dev/null &&
49 (eval "$condition" "$toolname")
50 then
51 if test -n "$preamble"
52 then
53 printf "%s\n" "$preamble"
54 preamble=
55 fi
56 shown_any=yes
57 printf "%s%s\n" "$per_line_prefix" "$toolname"
58 fi
59 done
60
665682c9
JK
61 if test -n "$extra_content"
62 then
63 if test -n "$preamble"
64 then
65 # Note: no '\n' here since we don't want a
66 # blank line if there is no initial content.
67 printf "%s" "$preamble"
68 preamble=
69 fi
70 shown_any=yes
71 printf "\n%s\n" "$extra_content"
72 fi
73
17a1f1c5
DA
74 if test -n "$preamble" && test -n "$not_found_msg"
75 then
76 printf "%s\n" "$not_found_msg"
77 fi
78
79 test -n "$shown_any"
80 }
81}
82
21d0ba7e
DA
83diff_mode() {
84 test "$TOOL_MODE" = diff
85}
86
87merge_mode() {
88 test "$TOOL_MODE" = merge
89}
90
91translate_merge_tool_path () {
bc7a96a8 92 echo "$1"
21d0ba7e
DA
93}
94
95check_unchanged () {
240dc3e8
DA
96 if test "$MERGED" -nt "$BACKUP"
97 then
1b6a5343 98 return 0
21d0ba7e 99 else
240dc3e8
DA
100 while true
101 do
21d0ba7e
DA
102 echo "$MERGED seems unchanged."
103 printf "Was the merge successful? [y/n] "
e622f41d 104 read answer || return 1
21d0ba7e 105 case "$answer" in
1b6a5343
DA
106 y*|Y*) return 0 ;;
107 n*|N*) return 1 ;;
21d0ba7e
DA
108 esac
109 done
110 fi
111}
112
113valid_tool () {
80ff2b68
DA
114 setup_tool "$1" && return 0
115 cmd=$(get_merge_tool_cmd "$1")
116 test -n "$cmd"
bc7a96a8
DA
117}
118
d2512fc9
JK
119setup_user_tool () {
120 merge_tool_cmd=$(get_merge_tool_cmd "$tool")
121 test -n "$merge_tool_cmd" || return 1
122
123 diff_cmd () {
124 ( eval $merge_tool_cmd )
d2512fc9
JK
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 )
d2512fc9
JK
134 check_unchanged
135 else
136 ( eval $merge_tool_cmd )
d2512fc9 137 fi
d2512fc9
JK
138 }
139}
140
bc7a96a8 141setup_tool () {
073678b8
DA
142 tool="$1"
143
98e023de 144 # Fallback definitions, to be overridden by tools.
073678b8
DA
145 can_merge () {
146 return 0
147 }
148
149 can_diff () {
150 return 0
151 }
152
153 diff_cmd () {
1b6a5343 154 return 1
073678b8
DA
155 }
156
157 merge_cmd () {
1b6a5343 158 return 1
073678b8 159 }
bc7a96a8 160
073678b8
DA
161 translate_merge_tool_path () {
162 echo "$1"
163 }
164
165 if ! test -f "$MERGE_TOOLS_DIR/$tool"
bc7a96a8 166 then
d2512fc9
JK
167 setup_user_tool
168 return $?
bc7a96a8
DA
169 fi
170
171 # Load the redefined functions
073678b8 172 . "$MERGE_TOOLS_DIR/$tool"
d2512fc9
JK
173 # Now let the user override the default command for the tool. If
174 # they have not done so then this will return 1 which we ignore.
175 setup_user_tool
bc7a96a8
DA
176
177 if merge_mode && ! can_merge
178 then
179 echo "error: '$tool' can not be used to resolve merges" >&2
62957bea 180 return 1
bc7a96a8
DA
181 elif diff_mode && ! can_diff
182 then
183 echo "error: '$tool' can only be used to resolve merges" >&2
62957bea 184 return 1
bc7a96a8
DA
185 fi
186 return 0
21d0ba7e
DA
187}
188
189get_merge_tool_cmd () {
bc7a96a8 190 merge_tool="$1"
240dc3e8
DA
191 if diff_mode
192 then
80ff2b68
DA
193 git config "difftool.$merge_tool.cmd" ||
194 git config "mergetool.$merge_tool.cmd"
47d65924 195 else
80ff2b68 196 git config "mergetool.$merge_tool.cmd"
47d65924 197 fi
21d0ba7e
DA
198}
199
bc7a96a8 200# Entry point for running tools
21d0ba7e 201run_merge_tool () {
f9ad901f
DA
202 # If GIT_PREFIX is empty then we cannot use it in tools
203 # that expect to be able to chdir() to its value.
204 GIT_PREFIX=${GIT_PREFIX:-.}
205 export GIT_PREFIX
206
80ff2b68 207 merge_tool_path=$(get_merge_tool_path "$1") || exit
21d0ba7e 208 base_present="$2"
21d0ba7e 209
bc7a96a8 210 # Bring tool-specific functions into scope
d2512fc9 211 setup_tool "$1" || return 1
bc7a96a8
DA
212
213 if merge_mode
214 then
a427ef7a 215 run_merge_cmd "$1"
bc7a96a8 216 else
a427ef7a 217 run_diff_cmd "$1"
bc7a96a8 218 fi
21d0ba7e
DA
219}
220
a427ef7a
DA
221# Run a either a configured or built-in diff tool
222run_diff_cmd () {
d2512fc9 223 diff_cmd "$1"
a427ef7a
DA
224}
225
226# Run a either a configured or built-in merge tool
227run_merge_cmd () {
d2512fc9 228 merge_cmd "$1"
a427ef7a
DA
229}
230
109859e2 231list_merge_tool_candidates () {
240dc3e8
DA
232 if merge_mode
233 then
21d0ba7e
DA
234 tools="tortoisemerge"
235 else
236 tools="kompare"
237 fi
240dc3e8
DA
238 if test -n "$DISPLAY"
239 then
240 if test -n "$GNOME_DESKTOP_SESSION_ID"
241 then
21d0ba7e
DA
242 tools="meld opendiff kdiff3 tkdiff xxdiff $tools"
243 else
244 tools="opendiff kdiff3 tkdiff xxdiff meld $tools"
245 fi
c5f424fd 246 tools="$tools gvimdiff diffuse diffmerge ecmerge"
f13f9b0e 247 tools="$tools p4merge araxis bc codecompare"
21d0ba7e 248 fi
7b104229
RS
249 case "${VISUAL:-$EDITOR}" in
250 *vim*)
21d0ba7e 251 tools="$tools vimdiff emerge"
7b104229
RS
252 ;;
253 *)
21d0ba7e 254 tools="$tools emerge vimdiff"
7b104229
RS
255 ;;
256 esac
109859e2
JH
257}
258
4a8273a3 259show_tool_help () {
2b7ca916 260 tool_opt="'git ${TOOL_MODE}tool --tool=<tool>'"
88d3406a 261
17a1f1c5
DA
262 tab=' '
263 LF='
264'
265 any_shown=no
62b6f7e0
JK
266
267 cmd_name=${TOOL_MODE}tool
665682c9
JK
268 config_tools=$({
269 diff_mode && list_config_tools difftool "$tab$tab"
270 list_config_tools mergetool "$tab$tab"
271 } | sort)
272 extra_content=
273 if test -n "$config_tools"
274 then
275 extra_content="${tab}user-defined:${LF}$config_tools"
276 fi
277
17a1f1c5
DA
278 show_tool_names 'mode_ok && is_available' "$tab$tab" \
279 "$tool_opt may be set to one of the following:" \
665682c9
JK
280 "No suitable tool for 'git $cmd_name --tool=<tool>' found." \
281 "$extra_content" &&
17a1f1c5
DA
282 any_shown=yes
283
284 show_tool_names 'mode_ok && ! is_available' "$tab$tab" \
285 "${LF}The following tools are valid, but not currently available:" &&
286 any_shown=yes
287
288 if test "$any_shown" = yes
4a8273a3
JK
289 then
290 echo
291 echo "Some of the tools listed above only work in a windowed"
292 echo "environment. If run in a terminal-only session, they will fail."
293 fi
294 exit 0
295}
296
109859e2
JH
297guess_merge_tool () {
298 list_merge_tool_candidates
5338a6a9
DA
299 cat >&2 <<-EOF
300
301 This message is displayed because '$TOOL_MODE.tool' is not configured.
302 See 'git ${TOOL_MODE}tool --tool-help' or 'git help config' for more details.
303 'git ${TOOL_MODE}tool' will now attempt to use one of the following tools:
304 $tools
305 EOF
21d0ba7e
DA
306
307 # Loop over each candidate and stop when a valid merge tool is found.
17a1f1c5 308 for tool in $tools
21d0ba7e 309 do
17a1f1c5 310 is_available "$tool" && echo "$tool" && return 0
21d0ba7e
DA
311 done
312
17a1f1c5 313 echo >&2 "No known ${TOOL_MODE} tool is available."
47d65924 314 return 1
21d0ba7e
DA
315}
316
317get_configured_merge_tool () {
318 # Diff mode first tries diff.tool and falls back to merge.tool.
319 # Merge mode only checks merge.tool
240dc3e8
DA
320 if diff_mode
321 then
47d65924
DA
322 merge_tool=$(git config diff.tool || git config merge.tool)
323 else
324 merge_tool=$(git config merge.tool)
21d0ba7e 325 fi
240dc3e8
DA
326 if test -n "$merge_tool" && ! valid_tool "$merge_tool"
327 then
21d0ba7e
DA
328 echo >&2 "git config option $TOOL_MODE.tool set to unknown tool: $merge_tool"
329 echo >&2 "Resetting to default..."
330 return 1
331 fi
47d65924 332 echo "$merge_tool"
21d0ba7e
DA
333}
334
335get_merge_tool_path () {
336 # A merge tool has been set, so verify that it's valid.
bc7a96a8 337 merge_tool="$1"
240dc3e8
DA
338 if ! valid_tool "$merge_tool"
339 then
21d0ba7e
DA
340 echo >&2 "Unknown merge tool $merge_tool"
341 exit 1
342 fi
240dc3e8
DA
343 if diff_mode
344 then
47d65924 345 merge_tool_path=$(git config difftool."$merge_tool".path ||
285c6cbf 346 git config mergetool."$merge_tool".path)
47d65924
DA
347 else
348 merge_tool_path=$(git config mergetool."$merge_tool".path)
21d0ba7e 349 fi
240dc3e8
DA
350 if test -z "$merge_tool_path"
351 then
80ff2b68 352 merge_tool_path=$(translate_merge_tool_path "$merge_tool")
21d0ba7e 353 fi
47d65924 354 if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
240dc3e8
DA
355 ! type "$merge_tool_path" >/dev/null 2>&1
356 then
47d65924 357 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
285c6cbf 358 "'$merge_tool_path'"
21d0ba7e
DA
359 exit 1
360 fi
361 echo "$merge_tool_path"
362}
363
364get_merge_tool () {
21d0ba7e 365 # Check if a merge tool has been configured
80ff2b68 366 merge_tool=$(get_configured_merge_tool)
21d0ba7e 367 # Try to guess an appropriate merge tool if no tool has been set.
240dc3e8
DA
368 if test -z "$merge_tool"
369 then
80ff2b68 370 merge_tool=$(guess_merge_tool) || exit
21d0ba7e
DA
371 fi
372 echo "$merge_tool"
373}