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 cleanup_temp_files --save-backup
133 git rm -- "$MERGED" > /dev/null
144 resolve_submodule_merge () {
147 printf "Use (l)ocal or (r)emote, or (a)bort? "
153 if test -n "$(git ls-tree HEAD -- "$MERGED")"
155 # Local isn't present, but it's a subdirectory
156 git ls-tree --full-name -r HEAD -- "$MERGED" |
157 git update-index --index-info || exit $?
159 test -e "$MERGED" && mv -- "$MERGED" "$BACKUP"
160 git update-index --force-remove "$MERGED"
161 cleanup_temp_files --save-backup
163 elif is_submodule "$local_mode"
165 stage_submodule "$MERGED" "$local_sha1"
167 git checkout-index -f --stage=2 -- "$MERGED"
175 if test -n "$(git ls-tree MERGE_HEAD -- "$MERGED")"
177 # Remote isn't present, but it's a subdirectory
178 git ls-tree --full-name -r MERGE_HEAD -- "$MERGED" |
179 git update-index --index-info || exit $?
181 test -e "$MERGED" && mv -- "$MERGED" "$BACKUP"
182 git update-index --force-remove "$MERGED"
184 elif is_submodule "$remote_mode"
186 ! is_submodule "$local_mode" &&
188 mv -- "$MERGED" "$BACKUP"
189 stage_submodule "$MERGED" "$remote_sha1"
191 test -e "$MERGED" && mv -- "$MERGED" "$BACKUP"
192 git checkout-index -f --stage=3 -- "$MERGED"
195 cleanup_temp_files --save-backup
209 die "fatal: unable to create directory for module at $path"
210 # Find $path relative to work tree
211 work_tree_root=$(cd_to_toplevel && pwd)
212 work_rel_path=$(cd "$path" &&
213 GIT_WORK_TREE="${work_tree_root}" git rev-parse --show-prefix
215 test -n "$work_rel_path" ||
216 die "fatal: unable to get path of module $path relative to work tree"
217 git update-index --add --replace --cacheinfo 160000 "$submodule_sha1" "${work_rel_path%/}" || die
220 checkout_staged_file () {
222 "$(git checkout-index --temp --stage="$1" "$2" 2>/dev/null)" \
225 if test $? -eq 0 && test -n "$tmpfile"
227 mv -- "$(git rev-parse --show-cdup)$tmpfile" "$3"
236 f=$(git ls-files -u -- "$MERGED")
239 if test ! -f "$MERGED"
241 echo "$MERGED: file not found"
243 echo "$MERGED: file does not need merging"
248 if BASE=$(expr "$MERGED" : '\(.*\)\.[^/]*$')
250 ext=$(expr "$MERGED" : '.*\(\.[^/]*\)$')
256 mergetool_tmpdir_init
258 if test "$MERGETOOL_TMPDIR" != "."
260 # If we're using a temporary directory then write to the
261 # top-level of that directory.
265 BACKUP="$MERGETOOL_TMPDIR/${BASE}_BACKUP_$$$ext"
266 LOCAL="$MERGETOOL_TMPDIR/${BASE}_LOCAL_$$$ext"
267 REMOTE="$MERGETOOL_TMPDIR/${BASE}_REMOTE_$$$ext"
268 BASE="$MERGETOOL_TMPDIR/${BASE}_BASE_$$$ext"
270 base_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==1) print $1;}')
271 local_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==2) print $1;}')
272 remote_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==3) print $1;}')
274 if is_submodule "$local_mode" || is_submodule "$remote_mode"
276 echo "Submodule merge conflict for '$MERGED':"
277 local_sha1=$(git ls-files -u -- "$MERGED" | awk '{if ($3==2) print $2;}')
278 remote_sha1=$(git ls-files -u -- "$MERGED" | awk '{if ($3==3) print $2;}')
279 describe_file "$local_mode" "local" "$local_sha1"
280 describe_file "$remote_mode" "remote" "$remote_sha1"
281 resolve_submodule_merge
285 mv -- "$MERGED" "$BACKUP"
286 cp -- "$BACKUP" "$MERGED"
288 checkout_staged_file 1 "$MERGED" "$BASE"
289 checkout_staged_file 2 "$MERGED" "$LOCAL"
290 checkout_staged_file 3 "$MERGED" "$REMOTE"
292 if test -z "$local_mode" || test -z "$remote_mode"
294 echo "Deleted merge conflict for '$MERGED':"
295 describe_file "$local_mode" "local" "$LOCAL"
296 describe_file "$remote_mode" "remote" "$REMOTE"
297 resolve_deleted_merge
301 if is_symlink "$local_mode" || is_symlink "$remote_mode"
303 echo "Symbolic link merge conflict for '$MERGED':"
304 describe_file "$local_mode" "local" "$LOCAL"
305 describe_file "$remote_mode" "remote" "$REMOTE"
306 resolve_symlink_merge
310 echo "Normal merge conflict for '$MERGED':"
311 describe_file "$local_mode" "local" "$LOCAL"
312 describe_file "$remote_mode" "remote" "$REMOTE"
313 if test "$guessed_merge_tool" = true || test "$prompt" = true
315 printf "Hit return to start merge resolution tool (%s): " "$merge_tool"
326 if ! run_merge_tool "$merge_tool" "$present"
328 echo "merge of $MERGED failed" 1>&2
329 mv -- "$BACKUP" "$MERGED"
331 if test "$merge_keep_temporaries" = "false"
339 if test "$merge_keep_backup" = "true"
341 mv -- "$BACKUP" "$MERGED.orig"
351 prompt=$(git config --bool mergetool.prompt)
352 guessed_merge_tool=false
358 TOOL_MODE=${1#--tool-help=}
367 merge_tool=$(expr "z$1" : 'z-[^=]*=\(.*\)')
396 prompt_after_failed_merge () {
399 printf "Continue merging other unresolved paths (y/n) ? "
415 if test -z "$merge_tool"
417 # Check if a merge tool has been configured
418 merge_tool=$(get_configured_merge_tool)
419 # Try to guess an appropriate merge tool if no tool has been set.
420 if test -z "$merge_tool"
422 merge_tool=$(guess_merge_tool) || exit
423 guessed_merge_tool=true
426 merge_keep_backup="$(git config --bool mergetool.keepBackup || echo true)"
427 merge_keep_temporaries="$(git config --bool mergetool.keepTemporaries || echo false)"
435 if test -e "$GIT_DIR/MERGE_RR"
437 files=$(git rerere remaining)
439 files=$(git ls-files -u | sed -e 's/^[^ ]* //' | sort -u)
442 files=$(git ls-files -u -- "$@" | sed -e 's/^[^ ]* //' | sort -u)
447 echo "No files need merging"
452 printf "%s\n" "$files"
461 prompt_after_failed_merge || exit 1