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] [-y|--no-prompt|--prompt] [file to merge] ...'
19 # Returns true if the mode reflects a symlink
29 test -n "$remote_mode"
36 cleanup_temp_files () {
37 if test "$1" = --save-backup ; then
38 mv -- "$BACKUP" "$MERGED.orig"
39 rm -f -- "$LOCAL" "$REMOTE" "$BASE"
41 rm -f -- "$LOCAL" "$REMOTE" "$BASE" "$BACKUP"
50 printf " {%s}: " "$branch"
51 if test -z "$mode"; then
53 elif is_symlink "$mode" ; then
54 echo "a symbolic link -> '$(cat "$file")'"
65 resolve_symlink_merge () {
67 printf "Use (l)ocal or (r)emote, or (a)bort? "
71 git checkout-index -f --stage=2 -- "$MERGED"
73 cleanup_temp_files --save-backup
77 git checkout-index -f --stage=3 -- "$MERGED"
79 cleanup_temp_files --save-backup
89 resolve_deleted_merge () {
92 printf "Use (m)odified or (d)eleted file, or (a)bort? "
94 printf "Use (c)reated or (d)eleted file, or (a)bort? "
100 cleanup_temp_files --save-backup
104 git rm -- "$MERGED" > /dev/null
115 checkout_staged_file () {
116 tmpfile=$(expr "$(git checkout-index --temp --stage="$1" "$2")" : '\([^ ]*\) ')
118 if test $? -eq 0 -a -n "$tmpfile" ; then
119 mv -- "$(git rev-parse --show-cdup)$tmpfile" "$3"
126 f=$(git ls-files -u -- "$MERGED")
127 if test -z "$f" ; then
128 if test ! -f "$MERGED" ; then
129 echo "$MERGED: file not found"
131 echo "$MERGED: file does not need merging"
136 ext="$$$(expr "$MERGED" : '.*\(\.[^/]*\)$')"
137 BACKUP="./$MERGED.BACKUP.$ext"
138 LOCAL="./$MERGED.LOCAL.$ext"
139 REMOTE="./$MERGED.REMOTE.$ext"
140 BASE="./$MERGED.BASE.$ext"
142 mv -- "$MERGED" "$BACKUP"
143 cp -- "$BACKUP" "$MERGED"
145 base_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==1) print $1;}')
146 local_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==2) print $1;}')
147 remote_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==3) print $1;}')
149 base_present && checkout_staged_file 1 "$MERGED" "$BASE"
150 local_present && checkout_staged_file 2 "$MERGED" "$LOCAL"
151 remote_present && checkout_staged_file 3 "$MERGED" "$REMOTE"
153 if test -z "$local_mode" -o -z "$remote_mode"; then
154 echo "Deleted merge conflict for '$MERGED':"
155 describe_file "$local_mode" "local" "$LOCAL"
156 describe_file "$remote_mode" "remote" "$REMOTE"
157 resolve_deleted_merge
161 if is_symlink "$local_mode" || is_symlink "$remote_mode"; then
162 echo "Symbolic link merge conflict for '$MERGED':"
163 describe_file "$local_mode" "local" "$LOCAL"
164 describe_file "$remote_mode" "remote" "$REMOTE"
165 resolve_symlink_merge
169 echo "Normal merge conflict for '$MERGED':"
170 describe_file "$local_mode" "local" "$LOCAL"
171 describe_file "$remote_mode" "remote" "$REMOTE"
172 if "$prompt" = true; then
173 printf "Hit return to start merge resolution tool (%s): " "$merge_tool"
177 if base_present; then
183 if ! run_merge_tool "$merge_tool" "$present"; then
184 echo "merge of $MERGED failed" 1>&2
185 mv -- "$BACKUP" "$MERGED"
187 if test "$merge_keep_temporaries" = "false"; then
194 if test "$merge_keep_backup" = "true"; then
195 mv -- "$BACKUP" "$MERGED.orig"
205 prompt=$(git config --bool mergetool.prompt || echo true)
213 merge_tool=$(expr "z$1" : 'z-[^=]*=\(.*\)')
242 prompt_after_failed_merge() {
244 printf "Continue merging other unresolved paths (y/n) ? "
259 if test -z "$merge_tool"; then
260 merge_tool=$(get_merge_tool "$merge_tool") || exit
262 merge_keep_backup="$(git config --bool mergetool.keepBackup || echo true)"
263 merge_keep_temporaries="$(git config --bool mergetool.keepTemporaries || echo false)"
270 if test "$rerere" = true
274 git ls-files -u | sed -e 's/^[^ ]* //' | sort -u
279 if test $# -eq 0 ; then
282 if test -e "$GIT_DIR/MERGE_RR"
287 files=$(files_to_merge)
288 if test -z "$files" ; then
289 echo "No files need merging"
293 # Save original stdin
302 if test $last_status -ne 0; then
303 prompt_after_failed_merge <&3 || exit 1
308 if test $last_status -ne 0; then
313 while test $# -gt 0; do
314 if test $last_status -ne 0; then
315 prompt_after_failed_merge || exit 1
320 if test $last_status -ne 0; then