3 # This program resolves merge conflicts in git
5 # Copyright (c) 2006 Theodore Y. Ts'o
7 # This file is licensed under the GPL v2, or a later version
8 # at the discretion of Junio C Hamano.
11 USAGE
='[--tool=tool] [--tool-help] [-y|--no-prompt|--prompt] [file to merge] ...'
19 # Returns true if the mode reflects a symlink
33 test -n "$remote_mode"
40 mergetool_tmpdir_init
() {
41 if test "$(git config --bool mergetool.writeToTemp)" != true
46 if MERGETOOL_TMPDIR
=$
(mktemp
-d -t "git-mergetool-XXXXXX" 2>/dev
/null
)
50 die
"error: mktemp is needed when 'mergetool.writeToTemp' is true"
53 cleanup_temp_files
() {
54 if test "$1" = --save-backup
56 rm -rf -- "$MERGED.orig"
57 test -e "$BACKUP" && mv -- "$BACKUP" "$MERGED.orig"
58 rm -f -- "$LOCAL" "$REMOTE" "$BASE"
60 rm -f -- "$LOCAL" "$REMOTE" "$BASE" "$BACKUP"
62 if test "$MERGETOOL_TMPDIR" != "."
64 rmdir "$MERGETOOL_TMPDIR"
73 printf " {%s}: " "$branch"
77 elif is_symlink
"$mode"
79 echo "a symbolic link -> '$(cat "$file")'"
80 elif is_submodule
"$mode"
82 echo "submodule commit $file"
91 resolve_symlink_merge
() {
94 printf "Use (l)ocal or (r)emote, or (a)bort? "
98 git checkout-index
-f --stage=2 -- "$MERGED"
100 cleanup_temp_files
--save-backup
104 git checkout-index
-f --stage=3 -- "$MERGED"
106 cleanup_temp_files
--save-backup
116 resolve_deleted_merge
() {
121 printf "Use (m)odified or (d)eleted file, or (a)bort? "
123 printf "Use (c)reated or (d)eleted file, or (a)bort? "
129 if test "$merge_keep_backup" = "true"
131 cleanup_temp_files
--save-backup
138 git
rm -- "$MERGED" > /dev
/null
143 if test "$merge_keep_temporaries" = "false"
153 resolve_submodule_merge
() {
156 printf "Use (l)ocal or (r)emote, or (a)bort? "
162 if test -n "$(git ls-tree HEAD -- "$MERGED")"
164 # Local isn't present, but it's a subdirectory
165 git ls-tree
--full-name -r HEAD
-- "$MERGED" |
166 git update-index
--index-info ||
exit $?
168 test -e "$MERGED" && mv -- "$MERGED" "$BACKUP"
169 git update-index
--force-remove "$MERGED"
170 cleanup_temp_files
--save-backup
172 elif is_submodule
"$local_mode"
174 stage_submodule
"$MERGED" "$local_sha1"
176 git checkout-index
-f --stage=2 -- "$MERGED"
184 if test -n "$(git ls-tree MERGE_HEAD -- "$MERGED")"
186 # Remote isn't present, but it's a subdirectory
187 git ls-tree
--full-name -r MERGE_HEAD
-- "$MERGED" |
188 git update-index
--index-info ||
exit $?
190 test -e "$MERGED" && mv -- "$MERGED" "$BACKUP"
191 git update-index
--force-remove "$MERGED"
193 elif is_submodule
"$remote_mode"
195 ! is_submodule
"$local_mode" &&
197 mv -- "$MERGED" "$BACKUP"
198 stage_submodule
"$MERGED" "$remote_sha1"
200 test -e "$MERGED" && mv -- "$MERGED" "$BACKUP"
201 git checkout-index
-f --stage=3 -- "$MERGED"
204 cleanup_temp_files
--save-backup
218 die
"fatal: unable to create directory for module at $path"
219 # Find $path relative to work tree
220 work_tree_root
=$
(cd_to_toplevel
&& pwd)
221 work_rel_path
=$
(cd "$path" &&
222 GIT_WORK_TREE
="${work_tree_root}" git rev-parse
--show-prefix
224 test -n "$work_rel_path" ||
225 die
"fatal: unable to get path of module $path relative to work tree"
226 git update-index
--add --replace --cacheinfo 160000 "$submodule_sha1" "${work_rel_path%/}" || die
229 checkout_staged_file
() {
231 "$(git checkout-index --temp --stage="$1" "$2" 2>/dev/null)" \
234 if test $?
-eq 0 && test -n "$tmpfile"
236 mv -- "$(git rev-parse --show-cdup)$tmpfile" "$3"
245 f
=$
(git ls-files
-u -- "$MERGED")
248 if test ! -f "$MERGED"
250 echo "$MERGED: file not found"
252 echo "$MERGED: file does not need merging"
257 if BASE
=$
(expr "$MERGED" : '\(.*\)\.[^/]*$')
259 ext
=$
(expr "$MERGED" : '.*\(\.[^/]*\)$')
265 mergetool_tmpdir_init
267 if test "$MERGETOOL_TMPDIR" != "."
269 # If we're using a temporary directory then write to the
270 # top-level of that directory.
274 BACKUP
="$MERGETOOL_TMPDIR/${BASE}_BACKUP_$$$ext"
275 LOCAL
="$MERGETOOL_TMPDIR/${BASE}_LOCAL_$$$ext"
276 REMOTE
="$MERGETOOL_TMPDIR/${BASE}_REMOTE_$$$ext"
277 BASE
="$MERGETOOL_TMPDIR/${BASE}_BASE_$$$ext"
279 base_mode
=$
(git ls-files
-u -- "$MERGED" |
awk '{if ($3==1) print $1;}')
280 local_mode
=$
(git ls-files
-u -- "$MERGED" |
awk '{if ($3==2) print $1;}')
281 remote_mode
=$
(git ls-files
-u -- "$MERGED" |
awk '{if ($3==3) print $1;}')
283 if is_submodule
"$local_mode" || is_submodule
"$remote_mode"
285 echo "Submodule merge conflict for '$MERGED':"
286 local_sha1
=$
(git ls-files
-u -- "$MERGED" |
awk '{if ($3==2) print $2;}')
287 remote_sha1
=$
(git ls-files
-u -- "$MERGED" |
awk '{if ($3==3) print $2;}')
288 describe_file
"$local_mode" "local" "$local_sha1"
289 describe_file
"$remote_mode" "remote" "$remote_sha1"
290 resolve_submodule_merge
296 mv -- "$MERGED" "$BACKUP"
297 cp -- "$BACKUP" "$MERGED"
299 # Create a parent directory to handle delete/delete conflicts
300 # where the base's directory no longer exists.
301 mkdir
-p "$(dirname "$MERGED")"
303 checkout_staged_file
1 "$MERGED" "$BASE"
304 checkout_staged_file
2 "$MERGED" "$LOCAL"
305 checkout_staged_file
3 "$MERGED" "$REMOTE"
307 if test -z "$local_mode" ||
test -z "$remote_mode"
309 echo "Deleted merge conflict for '$MERGED':"
310 describe_file
"$local_mode" "local" "$LOCAL"
311 describe_file
"$remote_mode" "remote" "$REMOTE"
312 resolve_deleted_merge
314 rmdir -p "$(dirname "$MERGED")" 2>/dev
/null
318 if is_symlink
"$local_mode" || is_symlink
"$remote_mode"
320 echo "Symbolic link merge conflict for '$MERGED':"
321 describe_file
"$local_mode" "local" "$LOCAL"
322 describe_file
"$remote_mode" "remote" "$REMOTE"
323 resolve_symlink_merge
327 echo "Normal merge conflict for '$MERGED':"
328 describe_file
"$local_mode" "local" "$LOCAL"
329 describe_file
"$remote_mode" "remote" "$REMOTE"
330 if test "$guessed_merge_tool" = true ||
test "$prompt" = true
332 printf "Hit return to start merge resolution tool (%s): " "$merge_tool"
343 if ! run_merge_tool
"$merge_tool" "$present"
345 echo "merge of $MERGED failed" 1>&2
346 mv -- "$BACKUP" "$MERGED"
348 if test "$merge_keep_temporaries" = "false"
356 if test "$merge_keep_backup" = "true"
358 mv -- "$BACKUP" "$MERGED.orig"
368 prompt
=$
(git config
--bool mergetool.prompt
)
369 guessed_merge_tool
=false
375 TOOL_MODE
=${1#--tool-help=}
384 merge_tool
=$
(expr "z$1" : 'z-[^=]*=\(.*\)')
413 prompt_after_failed_merge
() {
416 printf "Continue merging other unresolved paths [y/n]? "
432 if test -z "$merge_tool"
434 # Check if a merge tool has been configured
435 merge_tool
=$
(get_configured_merge_tool
)
436 # Try to guess an appropriate merge tool if no tool has been set.
437 if test -z "$merge_tool"
439 merge_tool
=$
(guess_merge_tool
) ||
exit
440 guessed_merge_tool
=true
443 merge_keep_backup
="$(git config --bool mergetool.keepBackup || echo true)"
444 merge_keep_temporaries
="$(git config --bool mergetool.keepTemporaries || echo false)"
452 if test -e "$GIT_DIR/MERGE_RR"
454 files
=$
(git rerere remaining
)
456 files
=$
(git ls-files
-u |
sed -e 's/^[^ ]* //' |
sort -u)
459 files
=$
(git ls-files
-u -- "$@" |
sed -e 's/^[^ ]* //' |
sort -u)
464 echo "No files need merging"
469 printf "%s\n" "$files"
478 prompt_after_failed_merge ||
exit 1