6e611e94a467ba3b3e1f56624807e231e865bce7
1 #!/bin/sh
2 #
3 # This program resolves merge conflicts in git
4 #
5 # Copyright (c) 2006 Theodore Y. Ts'o
6 #
7 # This file is licensed under the GPL v2, or a later version
8 # at the discretion of Junio C Hamano.
9 #
11 USAGE='[--tool=tool] [-y|--no-prompt|--prompt] [file to merge] ...'
12 SUBDIRECTORY_OK=Yes
13 OPTIONS_SPEC=
14 . git-sh-setup
15 require_work_tree
17 # Returns true if the mode reflects a symlink
18 is_symlink () {
19 test "$1" = 120000
20 }
22 local_present () {
23 test -n "$local_mode"
24 }
26 remote_present () {
27 test -n "$remote_mode"
28 }
30 base_present () {
31 test -n "$base_mode"
32 }
34 cleanup_temp_files () {
35 if test "$1" = --save-backup ; then
36 mv -- "$BACKUP" "$MERGED.orig"
37 rm -f -- "$LOCAL" "$REMOTE" "$BASE"
38 else
39 rm -f -- "$LOCAL" "$REMOTE" "$BASE" "$BACKUP"
40 fi
41 }
43 describe_file () {
44 mode="$1"
45 branch="$2"
46 file="$3"
48 printf " {%s}: " "$branch"
49 if test -z "$mode"; then
50 echo "deleted"
51 elif is_symlink "$mode" ; then
52 echo "a symbolic link -> '$(cat "$file")'"
53 else
54 if base_present; then
55 echo "modified"
56 else
57 echo "created"
58 fi
59 fi
60 }
63 resolve_symlink_merge () {
64 while true; do
65 printf "Use (l)ocal or (r)emote, or (a)bort? "
66 read ans
67 case "$ans" in
68 [lL]*)
69 git checkout-index -f --stage=2 -- "$MERGED"
70 git add -- "$MERGED"
71 cleanup_temp_files --save-backup
72 return 0
73 ;;
74 [rR]*)
75 git checkout-index -f --stage=3 -- "$MERGED"
76 git add -- "$MERGED"
77 cleanup_temp_files --save-backup
78 return 0
79 ;;
80 [aA]*)
81 return 1
82 ;;
83 esac
84 done
85 }
87 resolve_deleted_merge () {
88 while true; do
89 if base_present; then
90 printf "Use (m)odified or (d)eleted file, or (a)bort? "
91 else
92 printf "Use (c)reated or (d)eleted file, or (a)bort? "
93 fi
94 read ans
95 case "$ans" in
96 [mMcC]*)
97 git add -- "$MERGED"
98 cleanup_temp_files --save-backup
99 return 0
100 ;;
101 [dD]*)
102 git rm -- "$MERGED" > /dev/null
103 cleanup_temp_files
104 return 0
105 ;;
106 [aA]*)
107 return 1
108 ;;
109 esac
110 done
111 }
113 check_unchanged () {
114 if test "$MERGED" -nt "$BACKUP" ; then
115 status=0;
116 else
117 while true; do
118 echo "$MERGED seems unchanged."
119 printf "Was the merge successful? [y/n] "
120 read answer < /dev/tty
121 case "$answer" in
122 y*|Y*) status=0; break ;;
123 n*|N*) status=1; break ;;
124 esac
125 done
126 fi
127 }
129 checkout_staged_file () {
130 tmpfile=$(expr "$(git checkout-index --temp --stage="$1" "$2")" : '\([^ ]*\) ')
132 if test $? -eq 0 -a -n "$tmpfile" ; then
133 mv -- "$(git rev-parse --show-cdup)$tmpfile" "$3"
134 fi
135 }
137 merge_file () {
138 MERGED="$1"
140 f=`git ls-files -u -- "$MERGED"`
141 if test -z "$f" ; then
142 if test ! -f "$MERGED" ; then
143 echo "$MERGED: file not found"
144 else
145 echo "$MERGED: file does not need merging"
146 fi
147 return 1
148 fi
150 ext="$$$(expr "$MERGED" : '.*\(\.[^/]*\)$')"
151 BACKUP="./$MERGED.BACKUP.$ext"
152 LOCAL="./$MERGED.LOCAL.$ext"
153 REMOTE="./$MERGED.REMOTE.$ext"
154 BASE="./$MERGED.BASE.$ext"
156 mv -- "$MERGED" "$BACKUP"
157 cp -- "$BACKUP" "$MERGED"
159 base_mode=`git ls-files -u -- "$MERGED" | awk '{if ($3==1) print $1;}'`
160 local_mode=`git ls-files -u -- "$MERGED" | awk '{if ($3==2) print $1;}'`
161 remote_mode=`git ls-files -u -- "$MERGED" | awk '{if ($3==3) print $1;}'`
163 base_present && checkout_staged_file 1 "$MERGED" "$BASE"
164 local_present && checkout_staged_file 2 "$MERGED" "$LOCAL"
165 remote_present && checkout_staged_file 3 "$MERGED" "$REMOTE"
167 if test -z "$local_mode" -o -z "$remote_mode"; then
168 echo "Deleted merge conflict for '$MERGED':"
169 describe_file "$local_mode" "local" "$LOCAL"
170 describe_file "$remote_mode" "remote" "$REMOTE"
171 resolve_deleted_merge
172 return
173 fi
175 if is_symlink "$local_mode" || is_symlink "$remote_mode"; then
176 echo "Symbolic link merge conflict for '$MERGED':"
177 describe_file "$local_mode" "local" "$LOCAL"
178 describe_file "$remote_mode" "remote" "$REMOTE"
179 resolve_symlink_merge
180 return
181 fi
183 echo "Normal merge conflict for '$MERGED':"
184 describe_file "$local_mode" "local" "$LOCAL"
185 describe_file "$remote_mode" "remote" "$REMOTE"
186 if "$prompt" = true; then
187 printf "Hit return to start merge resolution tool (%s): " "$merge_tool"
188 read ans
189 fi
191 case "$merge_tool" in
192 kdiff3)
193 if base_present ; then
194 ("$merge_tool_path" --auto --L1 "$MERGED (Base)" --L2 "$MERGED (Local)" --L3 "$MERGED (Remote)" \
195 -o "$MERGED" "$BASE" "$LOCAL" "$REMOTE" > /dev/null 2>&1)
196 else
197 ("$merge_tool_path" --auto --L1 "$MERGED (Local)" --L2 "$MERGED (Remote)" \
198 -o "$MERGED" "$LOCAL" "$REMOTE" > /dev/null 2>&1)
199 fi
200 status=$?
201 ;;
202 tkdiff)
203 if base_present ; then
204 "$merge_tool_path" -a "$BASE" -o "$MERGED" "$LOCAL" "$REMOTE"
205 else
206 "$merge_tool_path" -o "$MERGED" "$LOCAL" "$REMOTE"
207 fi
208 status=$?
209 ;;
210 meld)
211 touch "$BACKUP"
212 "$merge_tool_path" "$LOCAL" "$MERGED" "$REMOTE"
213 check_unchanged
214 ;;
215 vimdiff)
216 touch "$BACKUP"
217 "$merge_tool_path" -d -c "wincmd l" "$LOCAL" "$MERGED" "$REMOTE"
218 check_unchanged
219 ;;
220 gvimdiff)
221 touch "$BACKUP"
222 "$merge_tool_path" -d -c "wincmd l" -f "$LOCAL" "$MERGED" "$REMOTE"
223 check_unchanged
224 ;;
225 xxdiff)
226 touch "$BACKUP"
227 if base_present ; then
228 "$merge_tool_path" -X --show-merged-pane \
229 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
230 -R 'Accel.Search: "Ctrl+F"' \
231 -R 'Accel.SearchForward: "Ctrl-G"' \
232 --merged-file "$MERGED" "$LOCAL" "$BASE" "$REMOTE"
233 else
234 "$merge_tool_path" -X --show-merged-pane \
235 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
236 -R 'Accel.Search: "Ctrl+F"' \
237 -R 'Accel.SearchForward: "Ctrl-G"' \
238 --merged-file "$MERGED" "$LOCAL" "$REMOTE"
239 fi
240 check_unchanged
241 ;;
242 opendiff)
243 touch "$BACKUP"
244 if base_present; then
245 "$merge_tool_path" "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$MERGED" | cat
246 else
247 "$merge_tool_path" "$LOCAL" "$REMOTE" -merge "$MERGED" | cat
248 fi
249 check_unchanged
250 ;;
251 ecmerge)
252 touch "$BACKUP"
253 if base_present; then
254 "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" --default --mode=merge3 --to="$MERGED"
255 else
256 "$merge_tool_path" "$LOCAL" "$REMOTE" --default --mode=merge2 --to="$MERGED"
257 fi
258 check_unchanged
259 ;;
260 emerge)
261 if base_present ; then
262 "$merge_tool_path" -f emerge-files-with-ancestor-command "$LOCAL" "$REMOTE" "$BASE" "$(basename "$MERGED")"
263 else
264 "$merge_tool_path" -f emerge-files-command "$LOCAL" "$REMOTE" "$(basename "$MERGED")"
265 fi
266 status=$?
267 ;;
268 *)
269 if test -n "$merge_tool_cmd"; then
270 if test "$merge_tool_trust_exit_code" = "false"; then
271 touch "$BACKUP"
272 ( eval $merge_tool_cmd )
273 check_unchanged
274 else
275 ( eval $merge_tool_cmd )
276 status=$?
277 fi
278 fi
279 ;;
280 esac
281 if test "$status" -ne 0; then
282 echo "merge of $MERGED failed" 1>&2
283 mv -- "$BACKUP" "$MERGED"
285 if test "$merge_keep_temporaries" = "false"; then
286 cleanup_temp_files
287 fi
289 return 1
290 fi
292 if test "$merge_keep_backup" = "true"; then
293 mv -- "$BACKUP" "$MERGED.orig"
294 else
295 rm -- "$BACKUP"
296 fi
298 git add -- "$MERGED"
299 cleanup_temp_files
300 return 0
301 }
303 prompt=$(git config --bool mergetool.prompt || echo true)
305 while test $# != 0
306 do
307 case "$1" in
308 -t|--tool*)
309 case "$#,$1" in
310 *,*=*)
311 merge_tool=`expr "z$1" : 'z-[^=]*=\(.*\)'`
312 ;;
313 1,*)
314 usage ;;
315 *)
316 merge_tool="$2"
317 shift ;;
318 esac
319 ;;
320 -y|--no-prompt)
321 prompt=false
322 ;;
323 --prompt)
324 prompt=true
325 ;;
326 --)
327 shift
328 break
329 ;;
330 -*)
331 usage
332 ;;
333 *)
334 break
335 ;;
336 esac
337 shift
338 done
340 valid_custom_tool()
341 {
342 merge_tool_cmd="$(git config mergetool.$1.cmd)"
343 test -n "$merge_tool_cmd"
344 }
346 valid_tool() {
347 case "$1" in
348 kdiff3 | tkdiff | xxdiff | meld | opendiff | emerge | vimdiff | gvimdiff | ecmerge)
349 ;; # happy
350 *)
351 if ! valid_custom_tool "$1"; then
352 return 1
353 fi
354 ;;
355 esac
356 }
358 init_merge_tool_path() {
359 merge_tool_path=`git config mergetool.$1.path`
360 if test -z "$merge_tool_path" ; then
361 case "$1" in
362 vimdiff)
363 merge_tool_path=vim
364 ;;
365 gvimdiff)
366 merge_tool_path=gvim
367 ;;
368 emerge)
369 merge_tool_path=emacs
370 ;;
371 *)
372 merge_tool_path=$1
373 ;;
374 esac
375 fi
376 }
378 prompt_after_failed_merge() {
379 while true; do
380 printf "Continue merging other unresolved paths (y/n) ? "
381 read ans
382 case "$ans" in
384 [yY]*)
385 return 0
386 ;;
388 [nN]*)
389 return 1
390 ;;
391 esac
392 done
393 }
395 if test -z "$merge_tool"; then
396 merge_tool=`git config merge.tool`
397 if test -n "$merge_tool" && ! valid_tool "$merge_tool"; then
398 echo >&2 "git config option merge.tool set to unknown tool: $merge_tool"
399 echo >&2 "Resetting to default..."
400 unset merge_tool
401 fi
402 fi
404 if test -z "$merge_tool" ; then
405 if test -n "$DISPLAY"; then
406 if test -n "$GNOME_DESKTOP_SESSION_ID" ; then
407 merge_tool_candidates="meld kdiff3 tkdiff xxdiff gvimdiff"
408 else
409 merge_tool_candidates="kdiff3 tkdiff xxdiff meld gvimdiff"
410 fi
411 fi
412 if echo "${VISUAL:-$EDITOR}" | grep 'emacs' > /dev/null 2>&1; then
413 merge_tool_candidates="$merge_tool_candidates emerge opendiff vimdiff"
414 elif echo "${VISUAL:-$EDITOR}" | grep 'vim' > /dev/null 2>&1; then
415 merge_tool_candidates="$merge_tool_candidates vimdiff opendiff emerge"
416 else
417 merge_tool_candidates="$merge_tool_candidates opendiff emerge vimdiff"
418 fi
419 echo "merge tool candidates: $merge_tool_candidates"
420 for i in $merge_tool_candidates; do
421 init_merge_tool_path $i
422 if type "$merge_tool_path" > /dev/null 2>&1; then
423 merge_tool=$i
424 break
425 fi
426 done
427 if test -z "$merge_tool" ; then
428 echo "No known merge resolution program available."
429 exit 1
430 fi
431 else
432 if ! valid_tool "$merge_tool"; then
433 echo >&2 "Unknown merge_tool $merge_tool"
434 exit 1
435 fi
437 init_merge_tool_path "$merge_tool"
439 merge_keep_backup="$(git config --bool merge.keepBackup || echo true)"
440 merge_keep_temporaries="$(git config --bool mergetool.keepTemporaries || echo false)"
442 if test -z "$merge_tool_cmd" && ! type "$merge_tool_path" > /dev/null 2>&1; then
443 echo "The merge tool $merge_tool is not available as '$merge_tool_path'"
444 exit 1
445 fi
447 if ! test -z "$merge_tool_cmd"; then
448 merge_tool_trust_exit_code="$(git config --bool mergetool.$merge_tool.trustExitCode || echo false)"
449 fi
450 fi
452 last_status=0
453 rollup_status=0
455 if test $# -eq 0 ; then
456 files=`git ls-files -u | sed -e 's/^[^ ]* //' | sort -u`
457 if test -z "$files" ; then
458 echo "No files need merging"
459 exit 0
460 fi
461 echo Merging the files: "$files"
462 git ls-files -u |
463 sed -e 's/^[^ ]* //' |
464 sort -u |
465 while IFS= read i
466 do
467 if test $last_status -ne 0; then
468 prompt_after_failed_merge < /dev/tty || exit 1
469 fi
470 printf "\n"
471 merge_file "$i" < /dev/tty > /dev/tty
472 last_status=$?
473 if test $last_status -ne 0; then
474 rollup_status=1
475 fi
476 done
477 else
478 while test $# -gt 0; do
479 if test $last_status -ne 0; then
480 prompt_after_failed_merge || exit 1
481 fi
482 printf "\n"
483 merge_file "$1"
484 last_status=$?
485 if test $last_status -ne 0; then
486 rollup_status=1
487 fi
488 shift
489 done
490 fi
492 exit $rollup_status