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