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
33 test -n "$remote_mode"
40 cleanup_temp_files () {
41 if test "$1" = --save-backup ; then
42 rm -rf -- "$MERGED.orig"
43 test -e "$BACKUP" && mv -- "$BACKUP" "$MERGED.orig"
44 rm -f -- "$LOCAL" "$REMOTE" "$BASE"
46 rm -f -- "$LOCAL" "$REMOTE" "$BASE" "$BACKUP"
55 printf " {%s}: " "$branch"
56 if test -z "$mode"; then
58 elif is_symlink "$mode" ; then
59 echo "a symbolic link -> '$(cat "$file")'"
60 elif is_submodule "$mode" ; then
61 echo "submodule commit $file"
72 resolve_symlink_merge () {
74 printf "Use (l)ocal or (r)emote, or (a)bort? "
78 git checkout-index -f --stage=2 -- "$MERGED"
80 cleanup_temp_files --save-backup
84 git checkout-index -f --stage=3 -- "$MERGED"
86 cleanup_temp_files --save-backup
96 resolve_deleted_merge () {
99 printf "Use (m)odified or (d)eleted file, or (a)bort? "
101 printf "Use (c)reated or (d)eleted file, or (a)bort? "
107 cleanup_temp_files --save-backup
111 git rm -- "$MERGED" > /dev/null
122 resolve_submodule_merge () {
124 printf "Use (l)ocal or (r)emote, or (a)bort? "
128 if ! local_present; then
129 if test -n "$(git ls-tree HEAD -- "$MERGED")"; then
130 # Local isn't present, but it's a subdirectory
131 git ls-tree --full-name -r HEAD -- "$MERGED" | git update-index --index-info || exit $?
133 test -e "$MERGED" && mv -- "$MERGED" "$BACKUP"
134 git update-index --force-remove "$MERGED"
135 cleanup_temp_files --save-backup
137 elif is_submodule "$local_mode"; then
138 stage_submodule "$MERGED" "$local_sha1"
140 git checkout-index -f --stage=2 -- "$MERGED"
146 if ! remote_present; then
147 if test -n "$(git ls-tree MERGE_HEAD -- "$MERGED")"; then
148 # Remote isn't present, but it's a subdirectory
149 git ls-tree --full-name -r MERGE_HEAD -- "$MERGED" | git update-index --index-info || exit $?
151 test -e "$MERGED" && mv -- "$MERGED" "$BACKUP"
152 git update-index --force-remove "$MERGED"
154 elif is_submodule "$remote_mode"; then
155 ! is_submodule "$local_mode" && test -e "$MERGED" && mv -- "$MERGED" "$BACKUP"
156 stage_submodule "$MERGED" "$remote_sha1"
158 test -e "$MERGED" && mv -- "$MERGED" "$BACKUP"
159 git checkout-index -f --stage=3 -- "$MERGED"
162 cleanup_temp_files --save-backup
175 mkdir -p "$path" || die "fatal: unable to create directory for module at $path"
176 # Find $path relative to work tree
177 work_tree_root=$(cd_to_toplevel && pwd)
178 work_rel_path=$(cd "$path" && GIT_WORK_TREE="${work_tree_root}" git rev-parse --show-prefix)
179 test -n "$work_rel_path" || die "fatal: unable to get path of module $path relative to work tree"
180 git update-index --add --replace --cacheinfo 160000 "$submodule_sha1" "${work_rel_path%/}" || die
183 checkout_staged_file () {
184 tmpfile=$(expr "$(git checkout-index --temp --stage="$1" "$2")" : '\([^ ]*\) ')
186 if test $? -eq 0 -a -n "$tmpfile" ; then
187 mv -- "$(git rev-parse --show-cdup)$tmpfile" "$3"
194 f=$(git ls-files -u -- "$MERGED")
195 if test -z "$f" ; then
196 if test ! -f "$MERGED" ; then
197 echo "$MERGED: file not found"
199 echo "$MERGED: file does not need merging"
204 ext="$$$(expr "$MERGED" : '.*\(\.[^/]*\)$')"
205 BACKUP="./$MERGED.BACKUP.$ext"
206 LOCAL="./$MERGED.LOCAL.$ext"
207 REMOTE="./$MERGED.REMOTE.$ext"
208 BASE="./$MERGED.BASE.$ext"
210 base_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==1) print $1;}')
211 local_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==2) print $1;}')
212 remote_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==3) print $1;}')
214 if is_submodule "$local_mode" || is_submodule "$remote_mode"; then
215 echo "Submodule merge conflict for '$MERGED':"
216 local_sha1=$(git ls-files -u -- "$MERGED" | awk '{if ($3==2) print $2;}')
217 remote_sha1=$(git ls-files -u -- "$MERGED" | awk '{if ($3==3) print $2;}')
218 describe_file "$local_mode" "local" "$local_sha1"
219 describe_file "$remote_mode" "remote" "$remote_sha1"
220 resolve_submodule_merge
224 mv -- "$MERGED" "$BACKUP"
225 cp -- "$BACKUP" "$MERGED"
227 base_present && checkout_staged_file 1 "$MERGED" "$BASE"
228 local_present && checkout_staged_file 2 "$MERGED" "$LOCAL"
229 remote_present && checkout_staged_file 3 "$MERGED" "$REMOTE"
231 if test -z "$local_mode" -o -z "$remote_mode"; then
232 echo "Deleted merge conflict for '$MERGED':"
233 describe_file "$local_mode" "local" "$LOCAL"
234 describe_file "$remote_mode" "remote" "$REMOTE"
235 resolve_deleted_merge
239 if is_symlink "$local_mode" || is_symlink "$remote_mode"; then
240 echo "Symbolic link merge conflict for '$MERGED':"
241 describe_file "$local_mode" "local" "$LOCAL"
242 describe_file "$remote_mode" "remote" "$REMOTE"
243 resolve_symlink_merge
247 echo "Normal merge conflict for '$MERGED':"
248 describe_file "$local_mode" "local" "$LOCAL"
249 describe_file "$remote_mode" "remote" "$REMOTE"
250 if "$prompt" = true; then
251 printf "Hit return to start merge resolution tool (%s): " "$merge_tool"
255 if base_present; then
261 if ! run_merge_tool "$merge_tool" "$present"; then
262 echo "merge of $MERGED failed" 1>&2
263 mv -- "$BACKUP" "$MERGED"
265 if test "$merge_keep_temporaries" = "false"; then
272 if test "$merge_keep_backup" = "true"; then
273 mv -- "$BACKUP" "$MERGED.orig"
283 prompt=$(git config --bool mergetool.prompt || echo true)
291 merge_tool=$(expr "z$1" : 'z-[^=]*=\(.*\)')
320 prompt_after_failed_merge() {
322 printf "Continue merging other unresolved paths (y/n) ? "
337 if test -z "$merge_tool"; then
338 merge_tool=$(get_merge_tool "$merge_tool") || exit
340 merge_keep_backup="$(git config --bool mergetool.keepBackup || echo true)"
341 merge_keep_temporaries="$(git config --bool mergetool.keepTemporaries || echo false)"
347 if test $# -eq 0 ; then
350 if test -e "$GIT_DIR/MERGE_RR"
352 files=$(git rerere remaining)
354 files=$(git ls-files -u | sed -e 's/^[^ ]* //' | sort -u)
357 files=$(git ls-files -u -- "$@" | sed -e 's/^[^ ]* //' | sort -u)
360 if test -z "$files" ; then
361 echo "No files need merging"
372 if test $last_status -ne 0; then
373 prompt_after_failed_merge || exit 1
378 if test $last_status -ne 0; then