mergetool: add copyright
[git/git.git] / git-mergetool.sh
CommitLineData
c4b4a5af
TT
1#!/bin/sh
2#
3# This program resolves merge conflicts in git
4#
5# Copyright (c) 2006 Theodore Y. Ts'o
8827b3a8 6# Copyright (c) 2009-2016 David Aguilar
c4b4a5af
TT
7#
8# This file is licensed under the GPL v2, or a later version
2571ac67 9# at the discretion of Junio C Hamano.
c4b4a5af
TT
10#
11
109859e2 12USAGE='[--tool=tool] [--tool-help] [-y|--no-prompt|--prompt] [file to merge] ...'
c4b4a5af 13SUBDIRECTORY_OK=Yes
7bfb7c35 14NONGIT_OK=Yes
8f321a39 15OPTIONS_SPEC=
21d0ba7e 16TOOL_MODE=merge
c4b4a5af 17. git-sh-setup
21d0ba7e 18. git-mergetool--lib
c4b4a5af
TT
19
20# Returns true if the mode reflects a symlink
262c981e 21is_symlink () {
f8750a0e 22 test "$1" = 120000
c4b4a5af
TT
23}
24
ff7f089e 25is_submodule () {
f8750a0e 26 test "$1" = 160000
ff7f089e
JM
27}
28
262c981e 29local_present () {
f8750a0e 30 test -n "$local_mode"
c4b4a5af
TT
31}
32
262c981e 33remote_present () {
f8750a0e 34 test -n "$remote_mode"
c4b4a5af
TT
35}
36
262c981e 37base_present () {
f8750a0e 38 test -n "$base_mode"
c4b4a5af
TT
39}
40
8f0cb41d
DA
41mergetool_tmpdir_init () {
42 if test "$(git config --bool mergetool.writeToTemp)" != true
43 then
44 MERGETOOL_TMPDIR=.
45 return 0
46 fi
47 if MERGETOOL_TMPDIR=$(mktemp -d -t "git-mergetool-XXXXXX" 2>/dev/null)
48 then
49 return 0
50 fi
51 die "error: mktemp is needed when 'mergetool.writeToTemp' is true"
52}
53
c4b4a5af 54cleanup_temp_files () {
f8750a0e
JH
55 if test "$1" = --save-backup
56 then
57 rm -rf -- "$MERGED.orig"
58 test -e "$BACKUP" && mv -- "$BACKUP" "$MERGED.orig"
59 rm -f -- "$LOCAL" "$REMOTE" "$BASE"
60 else
61 rm -f -- "$LOCAL" "$REMOTE" "$BASE" "$BACKUP"
62 fi
8f0cb41d
DA
63 if test "$MERGETOOL_TMPDIR" != "."
64 then
65 rmdir "$MERGETOOL_TMPDIR"
66 fi
c4b4a5af
TT
67}
68
262c981e 69describe_file () {
f8750a0e
JH
70 mode="$1"
71 branch="$2"
72 file="$3"
73
74 printf " {%s}: " "$branch"
75 if test -z "$mode"
76 then
77 echo "deleted"
78 elif is_symlink "$mode"
79 then
80 echo "a symbolic link -> '$(cat "$file")'"
81 elif is_submodule "$mode"
82 then
83 echo "submodule commit $file"
84 elif base_present
85 then
86 echo "modified file"
c4b4a5af 87 else
f8750a0e 88 echo "created file"
c4b4a5af 89 fi
c4b4a5af
TT
90}
91
c4b4a5af 92resolve_symlink_merge () {
f8750a0e
JH
93 while true
94 do
95 printf "Use (l)ocal or (r)emote, or (a)bort? "
96 read ans || return 1
97 case "$ans" in
98 [lL]*)
99 git checkout-index -f --stage=2 -- "$MERGED"
100 git add -- "$MERGED"
101 cleanup_temp_files --save-backup
102 return 0
103 ;;
104 [rR]*)
105 git checkout-index -f --stage=3 -- "$MERGED"
106 git add -- "$MERGED"
107 cleanup_temp_files --save-backup
108 return 0
109 ;;
110 [aA]*)
111 return 1
112 ;;
113 esac
c4b4a5af
TT
114 done
115}
116
117resolve_deleted_merge () {
f8750a0e
JH
118 while true
119 do
120 if base_present
121 then
122 printf "Use (m)odified or (d)eleted file, or (a)bort? "
123 else
124 printf "Use (c)reated or (d)eleted file, or (a)bort? "
125 fi
126 read ans || return 1
127 case "$ans" in
128 [mMcC]*)
129 git add -- "$MERGED"
a2986045
DA
130 if test "$merge_keep_backup" = "true"
131 then
132 cleanup_temp_files --save-backup
133 else
134 cleanup_temp_files
135 fi
f8750a0e
JH
136 return 0
137 ;;
138 [dD]*)
139 git rm -- "$MERGED" > /dev/null
140 cleanup_temp_files
141 return 0
142 ;;
143 [aA]*)
a2986045
DA
144 if test "$merge_keep_temporaries" = "false"
145 then
146 cleanup_temp_files
147 fi
f8750a0e
JH
148 return 1
149 ;;
150 esac
c4b4a5af
TT
151 done
152}
153
ff7f089e 154resolve_submodule_merge () {
f8750a0e
JH
155 while true
156 do
157 printf "Use (l)ocal or (r)emote, or (a)bort? "
158 read ans || return 1
159 case "$ans" in
160 [lL]*)
161 if ! local_present
162 then
163 if test -n "$(git ls-tree HEAD -- "$MERGED")"
164 then
165 # Local isn't present, but it's a subdirectory
166 git ls-tree --full-name -r HEAD -- "$MERGED" |
167 git update-index --index-info || exit $?
168 else
169 test -e "$MERGED" && mv -- "$MERGED" "$BACKUP"
170 git update-index --force-remove "$MERGED"
171 cleanup_temp_files --save-backup
172 fi
173 elif is_submodule "$local_mode"
174 then
175 stage_submodule "$MERGED" "$local_sha1"
176 else
177 git checkout-index -f --stage=2 -- "$MERGED"
178 git add -- "$MERGED"
179 fi
180 return 0
181 ;;
182 [rR]*)
183 if ! remote_present
184 then
185 if test -n "$(git ls-tree MERGE_HEAD -- "$MERGED")"
186 then
187 # Remote isn't present, but it's a subdirectory
188 git ls-tree --full-name -r MERGE_HEAD -- "$MERGED" |
189 git update-index --index-info || exit $?
190 else
191 test -e "$MERGED" && mv -- "$MERGED" "$BACKUP"
192 git update-index --force-remove "$MERGED"
193 fi
194 elif is_submodule "$remote_mode"
195 then
196 ! is_submodule "$local_mode" &&
197 test -e "$MERGED" &&
198 mv -- "$MERGED" "$BACKUP"
199 stage_submodule "$MERGED" "$remote_sha1"
200 else
201 test -e "$MERGED" && mv -- "$MERGED" "$BACKUP"
202 git checkout-index -f --stage=3 -- "$MERGED"
203 git add -- "$MERGED"
204 fi
ff7f089e 205 cleanup_temp_files --save-backup
f8750a0e
JH
206 return 0
207 ;;
208 [aA]*)
209 return 1
210 ;;
211 esac
ff7f089e
JM
212 done
213}
214
215stage_submodule () {
f8750a0e
JH
216 path="$1"
217 submodule_sha1="$2"
218 mkdir -p "$path" ||
219 die "fatal: unable to create directory for module at $path"
220 # Find $path relative to work tree
221 work_tree_root=$(cd_to_toplevel && pwd)
222 work_rel_path=$(cd "$path" &&
223 GIT_WORK_TREE="${work_tree_root}" git rev-parse --show-prefix
224 )
225 test -n "$work_rel_path" ||
226 die "fatal: unable to get path of module $path relative to work tree"
227 git update-index --add --replace --cacheinfo 160000 "$submodule_sha1" "${work_rel_path%/}" || die
ff7f089e
JM
228}
229
0ec7b6c2 230checkout_staged_file () {
f8750a0e
JH
231 tmpfile=$(expr \
232 "$(git checkout-index --temp --stage="$1" "$2" 2>/dev/null)" \
233 : '\([^ ]*\) ')
234
1cb49373 235 if test $? -eq 0 && test -n "$tmpfile"
f8750a0e
JH
236 then
237 mv -- "$(git rev-parse --show-cdup)$tmpfile" "$3"
238 else
239 >"$3"
240 fi
0ec7b6c2
CB
241}
242
c4b4a5af 243merge_file () {
f8750a0e 244 MERGED="$1"
c4b4a5af 245
f8750a0e
JH
246 f=$(git ls-files -u -- "$MERGED")
247 if test -z "$f"
248 then
249 if test ! -f "$MERGED"
250 then
251 echo "$MERGED: file not found"
252 else
253 echo "$MERGED: file does not need merging"
254 fi
255 return 1
ce5b6d75 256 fi
c4b4a5af 257
eab335c4
DA
258 if BASE=$(expr "$MERGED" : '\(.*\)\.[^/]*$')
259 then
260 ext=$(expr "$MERGED" : '.*\(\.[^/]*\)$')
261 else
262 BASE=$MERGED
263 ext=
264 fi
8f0cb41d
DA
265
266 mergetool_tmpdir_init
267
268 if test "$MERGETOOL_TMPDIR" != "."
269 then
270 # If we're using a temporary directory then write to the
271 # top-level of that directory.
272 BASE=${BASE##*/}
273 fi
274
275 BACKUP="$MERGETOOL_TMPDIR/${BASE}_BACKUP_$$$ext"
276 LOCAL="$MERGETOOL_TMPDIR/${BASE}_LOCAL_$$$ext"
277 REMOTE="$MERGETOOL_TMPDIR/${BASE}_REMOTE_$$$ext"
278 BASE="$MERGETOOL_TMPDIR/${BASE}_BASE_$$$ext"
f8750a0e
JH
279
280 base_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==1) print $1;}')
281 local_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==2) print $1;}')
282 remote_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==3) print $1;}')
283
284 if is_submodule "$local_mode" || is_submodule "$remote_mode"
285 then
286 echo "Submodule merge conflict for '$MERGED':"
287 local_sha1=$(git ls-files -u -- "$MERGED" | awk '{if ($3==2) print $2;}')
288 remote_sha1=$(git ls-files -u -- "$MERGED" | awk '{if ($3==3) print $2;}')
289 describe_file "$local_mode" "local" "$local_sha1"
290 describe_file "$remote_mode" "remote" "$remote_sha1"
291 resolve_submodule_merge
292 return
293 fi
294
faaab8d5
DA
295 if test -f "$MERGED"
296 then
297 mv -- "$MERGED" "$BACKUP"
298 cp -- "$BACKUP" "$MERGED"
299 fi
300 # Create a parent directory to handle delete/delete conflicts
301 # where the base's directory no longer exists.
302 mkdir -p "$(dirname "$MERGED")"
f8750a0e
JH
303
304 checkout_staged_file 1 "$MERGED" "$BASE"
305 checkout_staged_file 2 "$MERGED" "$LOCAL"
306 checkout_staged_file 3 "$MERGED" "$REMOTE"
307
1cb49373 308 if test -z "$local_mode" || test -z "$remote_mode"
f8750a0e
JH
309 then
310 echo "Deleted merge conflict for '$MERGED':"
311 describe_file "$local_mode" "local" "$LOCAL"
312 describe_file "$remote_mode" "remote" "$REMOTE"
313 resolve_deleted_merge
faaab8d5
DA
314 status=$?
315 rmdir -p "$(dirname "$MERGED")" 2>/dev/null
316 return $status
f8750a0e
JH
317 fi
318
319 if is_symlink "$local_mode" || is_symlink "$remote_mode"
320 then
321 echo "Symbolic link merge conflict for '$MERGED':"
322 describe_file "$local_mode" "local" "$LOCAL"
323 describe_file "$remote_mode" "remote" "$REMOTE"
324 resolve_symlink_merge
325 return
326 fi
327
328 echo "Normal merge conflict for '$MERGED':"
c4b4a5af
TT
329 describe_file "$local_mode" "local" "$LOCAL"
330 describe_file "$remote_mode" "remote" "$REMOTE"
4ecc63d7 331 if test "$guessed_merge_tool" = true || test "$prompt" = true
f8750a0e
JH
332 then
333 printf "Hit return to start merge resolution tool (%s): " "$merge_tool"
334 read ans || return 1
162eba8b
CB
335 fi
336
f8750a0e
JH
337 if base_present
338 then
339 present=true
340 else
341 present=false
342 fi
343
344 if ! run_merge_tool "$merge_tool" "$present"
345 then
346 echo "merge of $MERGED failed" 1>&2
347 mv -- "$BACKUP" "$MERGED"
348
349 if test "$merge_keep_temporaries" = "false"
350 then
351 cleanup_temp_files
352 fi
44c36d1c 353
f8750a0e
JH
354 return 1
355 fi
44c36d1c 356
f8750a0e
JH
357 if test "$merge_keep_backup" = "true"
358 then
359 mv -- "$BACKUP" "$MERGED.orig"
360 else
361 rm -- "$BACKUP"
362 fi
363
364 git add -- "$MERGED"
365 cleanup_temp_files
366 return 0
c4b4a5af
TT
367}
368
4ecc63d7
FC
369prompt=$(git config --bool mergetool.prompt)
370guessed_merge_tool=false
682b451f 371
822f7c73 372while test $# != 0
c4b4a5af 373do
f8750a0e 374 case "$1" in
4fb4b02d
CB
375 --tool-help=*)
376 TOOL_MODE=${1#--tool-help=}
377 show_tool_help
378 ;;
109859e2
JH
379 --tool-help)
380 show_tool_help
381 ;;
c4b4a5af 382 -t|--tool*)
f8750a0e 383 case "$#,$1" in
c4b4a5af 384 *,*=*)
f8750a0e
JH
385 merge_tool=$(expr "z$1" : 'z-[^=]*=\(.*\)')
386 ;;
c4b4a5af 387 1,*)
f8750a0e 388 usage ;;
c4b4a5af 389 *)
f8750a0e
JH
390 merge_tool="$2"
391 shift ;;
392 esac
393 ;;
682b451f 394 -y|--no-prompt)
f8750a0e
JH
395 prompt=false
396 ;;
682b451f 397 --prompt)
f8750a0e
JH
398 prompt=true
399 ;;
c4b4a5af 400 --)
f8750a0e
JH
401 shift
402 break
403 ;;
c4b4a5af 404 -*)
f8750a0e 405 usage
b0169d84 406 ;;
f8750a0e
JH
407 *)
408 break
b0169d84
CB
409 ;;
410 esac
f8750a0e
JH
411 shift
412done
413
414prompt_after_failed_merge () {
415 while true
416 do
cce076e3 417 printf "Continue merging other unresolved paths [y/n]? "
f8750a0e
JH
418 read ans || return 1
419 case "$ans" in
420 [yY]*)
421 return 0
422 ;;
423 [nN]*)
424 return 1
425 ;;
426 esac
427 done
b0169d84 428}
e3fa2c76 429
7bfb7c35
CB
430git_dir_init
431require_work_tree
432
f8750a0e
JH
433if test -z "$merge_tool"
434then
4ecc63d7
FC
435 # Check if a merge tool has been configured
436 merge_tool=$(get_configured_merge_tool)
437 # Try to guess an appropriate merge tool if no tool has been set.
438 if test -z "$merge_tool"
439 then
440 merge_tool=$(guess_merge_tool) || exit
441 guessed_merge_tool=true
442 fi
47d65924 443fi
70af4e9b 444merge_keep_backup="$(git config --bool mergetool.keepBackup || echo true)"
21d0ba7e 445merge_keep_temporaries="$(git config --bool mergetool.keepTemporaries || echo false)"
c4b4a5af 446
3e8e691a 447files=
c4b4a5af 448
f8750a0e
JH
449if test $# -eq 0
450then
451 cd_to_toplevel
bb0a484e 452
f8750a0e
JH
453 if test -e "$GIT_DIR/MERGE_RR"
454 then
455 files=$(git rerere remaining)
456 else
457 files=$(git ls-files -u | sed -e 's/^[^ ]* //' | sort -u)
458 fi
3e8e691a 459else
f8750a0e 460 files=$(git ls-files -u -- "$@" | sed -e 's/^[^ ]* //' | sort -u)
3e8e691a 461fi
af314714 462
f8750a0e
JH
463if test -z "$files"
464then
465 echo "No files need merging"
466 exit 0
3e8e691a 467fi
af314714 468
3e8e691a 469printf "Merging:\n"
59cf706b 470printf "%s\n" "$files"
bb0a484e 471
98a26022 472rc=0
6d9990a9 473for i in $files
3e8e691a 474do
f8750a0e 475 printf "\n"
98a26022 476 if ! merge_file "$i"
f8750a0e 477 then
98a26022
DA
478 rc=1
479 prompt_after_failed_merge || exit 1
f8750a0e 480 fi
3e8e691a 481done
b0169d84 482
98a26022 483exit $rc