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] [file to merge] ...'
15 prefix=$(git rev-parse --show-prefix)
17 # Returns true if the mode reflects a symlink
27 test -n "$remote_mode"
34 cleanup_temp_files () {
35 if test "$1" = --save-backup ; then
36 mv -- "$BACKUP" "$path.orig"
37 rm -f -- "$LOCAL" "$REMOTE" "$BASE"
39 rm -f -- "$LOCAL" "$REMOTE" "$BASE" "$BACKUP"
48 printf " {%s}: " "$branch"
49 if test -z "$mode"; then
51 elif is_symlink "$mode" ; then
52 echo "a symbolic link -> '$(cat "$file")'"
63 resolve_symlink_merge () {
65 printf "Use (l)ocal or (r)emote, or (a)bort? "
69 git checkout-index -f --stage=2 -- "$path"
71 cleanup_temp_files --save-backup
75 git checkout-index -f --stage=3 -- "$path"
77 cleanup_temp_files --save-backup
87 resolve_deleted_merge () {
90 printf "Use (m)odified or (d)eleted file, or (a)bort? "
92 printf "Use (c)reated or (d)eleted file, or (a)bort? "
98 cleanup_temp_files --save-backup
102 git rm -- "$path" > /dev/null
114 if test "$path" -nt "$BACKUP" ; then
118 echo "$path seems unchanged."
119 printf "Was the merge successful? [y/n] "
120 read answer < /dev/tty
122 y*|Y*) status=0; break ;;
123 n*|N*) status=1; break ;;
130 if test "$status" -eq 0; then
131 mv -- "$BACKUP" "$path.orig"
136 if test "$status" -eq 0; then
144 f=`git ls-files -u -- "$path"`
145 if test -z "$f" ; then
146 if test ! -f "$path" ; then
147 echo "$path: file not found"
149 echo "$path: file does not need merging"
154 BACKUP="$path.BACKUP.$$"
155 LOCAL="$path.LOCAL.$$"
156 REMOTE="$path.REMOTE.$$"
159 mv -- "$path" "$BACKUP"
160 cp -- "$BACKUP" "$path"
162 base_mode=`git ls-files -u -- "$path" | awk '{if ($3==1) print $1;}'`
163 local_mode=`git ls-files -u -- "$path" | awk '{if ($3==2) print $1;}'`
164 remote_mode=`git ls-files -u -- "$path" | awk '{if ($3==3) print $1;}'`
166 base_present && git cat-file blob ":1:$prefix$path" >"$BASE" 2>/dev/null
167 local_present && git cat-file blob ":2:$prefix$path" >"$LOCAL" 2>/dev/null
168 remote_present && git cat-file blob ":3:$prefix$path" >"$REMOTE" 2>/dev/null
170 if test -z "$local_mode" -o -z "$remote_mode"; then
171 echo "Deleted merge conflict for '$path':"
172 describe_file "$local_mode" "local" "$LOCAL"
173 describe_file "$remote_mode" "remote" "$REMOTE"
174 resolve_deleted_merge
178 if is_symlink "$local_mode" || is_symlink "$remote_mode"; then
179 echo "Symbolic link merge conflict for '$path':"
180 describe_file "$local_mode" "local" "$LOCAL"
181 describe_file "$remote_mode" "remote" "$REMOTE"
182 resolve_symlink_merge
186 echo "Normal merge conflict for '$path':"
187 describe_file "$local_mode" "local" "$LOCAL"
188 describe_file "$remote_mode" "remote" "$REMOTE"
189 printf "Hit return to start merge resolution tool (%s): " "$merge_tool"
192 case "$merge_tool" in
194 if base_present ; then
195 (kdiff3 --auto --L1 "$path (Base)" --L2 "$path (Local)" --L3 "$path (Remote)" \
196 -o "$path" -- "$BASE" "$LOCAL" "$REMOTE" > /dev/null 2>&1)
198 (kdiff3 --auto --L1 "$path (Local)" --L2 "$path (Remote)" \
199 -o "$path" -- "$LOCAL" "$REMOTE" > /dev/null 2>&1)
205 if base_present ; then
206 tkdiff -a "$BASE" -o "$path" -- "$LOCAL" "$REMOTE"
208 tkdiff -o "$path" -- "$LOCAL" "$REMOTE"
215 $merge_tool -- "$LOCAL" "$path" "$REMOTE"
221 gvimdiff -f -- "$LOCAL" "$path" "$REMOTE"
227 if base_present ; then
228 xxdiff -X --show-merged-pane \
229 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
230 -R 'Accel.Search: "Ctrl+F"' \
231 -R 'Accel.SearchForward: "Ctrl-G"' \
232 --merged-file "$path" -- "$LOCAL" "$BASE" "$REMOTE"
234 xxdiff -X --show-merged-pane \
235 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
236 -R 'Accel.Search: "Ctrl+F"' \
237 -R 'Accel.SearchForward: "Ctrl-G"' \
238 --merged-file "$path" -- "$LOCAL" "$REMOTE"
245 if base_present; then
246 opendiff "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$path" | cat
248 opendiff "$LOCAL" "$REMOTE" -merge "$path" | cat
254 if base_present ; then
255 emacs -f emerge-files-with-ancestor-command "$LOCAL" "$REMOTE" "$BASE" "$(basename "$path")"
257 emacs -f emerge-files-command "$LOCAL" "$REMOTE" "$(basename "$path")"
263 if test "$status" -ne 0; then
264 echo "merge of $path failed" 1>&2
265 mv -- "$BACKUP" "$path"
278 merge_tool=`expr "z$1" : 'z-[^=]*=\(.*\)'`
300 if test -z "$merge_tool"; then
301 merge_tool=`git config merge.tool`
302 case "$merge_tool" in
303 kdiff3 | tkdiff | xxdiff | meld | opendiff | emerge | vimdiff | gvimdiff | "")
306 echo >&2 "git config option merge.tool set to unknown tool: $merge_tool"
307 echo >&2 "Resetting to default..."
313 if test -z "$merge_tool" ; then
314 if test -n "$DISPLAY"; then
315 merge_tool_candidates="kdiff3 tkdiff xxdiff meld gvimdiff"
316 if test -n "$GNOME_DESKTOP_SESSION_ID" ; then
317 merge_tool_candidates="meld $merge_tool_candidates"
319 if test "$KDE_FULL_SESSION" = "true"; then
320 merge_tool_candidates="kdiff3 $merge_tool_candidates"
323 if echo "${VISUAL:-$EDITOR}" | grep 'emacs' > /dev/null 2>&1; then
324 merge_tool_candidates="$merge_tool_candidates emerge"
326 if echo "${VISUAL:-$EDITOR}" | grep 'vim' > /dev/null 2>&1; then
327 merge_tool_candidates="$merge_tool_candidates vimdiff"
329 merge_tool_candidates="$merge_tool_candidates opendiff emerge vimdiff"
330 echo "merge tool candidates: $merge_tool_candidates"
331 for i in $merge_tool_candidates; do
332 if test $i = emerge ; then
337 if type $cmd > /dev/null 2>&1; then
342 if test -z "$merge_tool" ; then
343 echo "No available merge resolution programs available."
348 case "$merge_tool" in
349 kdiff3|tkdiff|meld|xxdiff|vimdiff|gvimdiff|opendiff)
350 if ! type "$merge_tool" > /dev/null 2>&1; then
351 echo "The merge tool $merge_tool is not available"
356 if ! type "emacs" > /dev/null 2>&1; then
357 echo "Emacs is not available"
362 echo "Unknown merge tool: $merge_tool"
367 if test $# -eq 0 ; then
368 files=`git ls-files -u | sed -e 's/^[^ ]* //' | sort -u`
369 if test -z "$files" ; then
370 echo "No files need merging"
373 echo Merging the files: $files
374 git ls-files -u | sed -e 's/^[^ ]* //' | sort -u | while read i
377 merge_file "$i" < /dev/tty > /dev/tty
380 while test $# -gt 0; do