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 Hammano.
11 USAGE
='[--tool=tool] [file to merge] ...'
16 # Returns true if the mode reflects a symlink
26 test -n "$remote_mode"
33 cleanup_temp_files
() {
34 if test "$1" = --save-backup ; then
35 mv -- "$BACKUP" "$path.orig"
36 rm -f -- "$LOCAL" "$REMOTE" "$BASE"
38 rm -f -- "$LOCAL" "$REMOTE" "$BASE" "$BACKUP"
48 if test -z "$mode"; then
49 printf "'%s' was deleted" "$path"
50 elif is_symlink
"$mode" ; then
51 printf "'%s' is a symlink containing '%s'" "$path" "$file"
54 printf "'%s' was created" "$path"
56 printf "'%s' was modified" "$path"
59 echo " in the $branch branch"
63 resolve_symlink_merge
() {
65 printf "Use (r)emote or (l)ocal, 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
() {
89 printf "Use (m)odified or (d)eleted file, or (a)bort? "
94 cleanup_temp_files
--save-backup
110 if test "$path" -nt "$BACKUP" ; then
114 echo "$path seems unchanged."
115 printf "Was the merge successful? [y/n] "
116 read answer
< /dev
/tty
118 y
*|Y
*) status
=0; break ;;
119 n
*|N
*) status
=1; break ;;
126 if test "$status" -eq 0; then
127 mv -- "$BACKUP" "$path.orig"
132 if test "$status" -eq 0; then
140 f
=`git-ls-files -u -- "$path"`
141 if test -z "$f" ; then
142 if test ! -f "$path" ; then
143 echo "$path: file not found"
145 echo "$path: file does not need merging"
150 BACKUP
="$path.BACKUP.$$"
151 LOCAL
="$path.LOCAL.$$"
152 REMOTE
="$path.REMOTE.$$"
155 mv -- "$path" "$BACKUP"
156 cp -- "$BACKUP" "$path"
158 base_mode
=`git ls-files -u -- "$path" | awk '{if ($3==1) print $1;}'`
159 local_mode
=`git ls-files -u -- "$path" | awk '{if ($3==2) print $1;}'`
160 remote_mode
=`git ls-files -u -- "$path" | awk '{if ($3==3) print $1;}'`
162 base_present
&& git cat-file blob
":1:$path" > "$BASE" 2>/dev
/null
163 local_present
&& git cat-file blob
":2:$path" > "$LOCAL" 2>/dev
/null
164 remote_present
&& git cat-file blob
":3:$path" > "$REMOTE" 2>/dev
/null
166 if test -z "$local_mode" -o -z "$remote_mode"; then
167 echo "Deleted merge conflict for $path:"
168 describe_file
"$local_mode" "local" "$LOCAL"
169 describe_file
"$remote_mode" "remote" "$REMOTE"
170 resolve_deleted_merge
174 if is_symlink
"$local_mode" || is_symlink
"$remote_mode"; then
175 echo "Symlink merge conflict for $path:"
176 describe_file
"$local_mode" "local" "$LOCAL"
177 describe_file
"$remote_mode" "remote" "$REMOTE"
178 resolve_symlink_merge
182 echo "Normal merge conflict for $path:"
183 describe_file
"$local_mode" "local" "$LOCAL"
184 describe_file
"$remote_mode" "remote" "$REMOTE"
185 printf "Hit return to start merge resolution tool (%s): " "$merge_tool"
188 case "$merge_tool" in
190 if base_present
; then
191 (kdiff3
--auto --L1 "$path (Base)" -L2 "$path (Local)" --L3 "$path (Remote)" \
192 -o "$path" -- "$BASE" "$LOCAL" "$REMOTE" > /dev
/null
2>&1)
194 (kdiff3
--auto -L1 "$path (Local)" --L2 "$path (Remote)" \
195 -o "$path" -- "$LOCAL" "$REMOTE" > /dev
/null
2>&1)
201 if base_present
; then
202 tkdiff
-a "$BASE" -o "$path" -- "$LOCAL" "$REMOTE"
204 tkdiff
-o "$path" -- "$LOCAL" "$REMOTE"
211 $merge_tool -- "$LOCAL" "$path" "$REMOTE"
217 if base_present
; then
218 xxdiff
-X --show-merged-pane \
219 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
220 -R 'Accel.Search: "Ctrl+F"' \
221 -R 'Accel.SearchForward: "Ctrl-G"' \
222 --merged-file "$path" -- "$LOCAL" "$BASE" "$REMOTE"
224 xxdiff
-X --show-merged-pane \
225 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
226 -R 'Accel.Search: "Ctrl+F"' \
227 -R 'Accel.SearchForward: "Ctrl-G"' \
228 --merged-file "$path" -- "$LOCAL" "$REMOTE"
234 if base_present
; then
235 emacs
-f emerge-files-with-ancestor-command
"$LOCAL" "$REMOTE" "$BASE" "$path"
237 emacs
-f emerge-files-command
"$LOCAL" "$REMOTE" "$path"
243 if test "$status" -ne 0; then
244 echo "merge of $path failed" 1>&2
245 mv -- "$BACKUP" "$path"
252 while case $# in 0) break ;; esac
258 merge_tool
=`expr "z$1" : 'z-[^=]*=\(.*\)'`
280 if test -z "$merge_tool"; then
281 merge_tool
=`git-config merge.tool`
282 case "$merge_tool" in
283 kdiff3 | tkdiff | xxdiff | meld | emerge | vimdiff
)
286 echo >&2 "git config option merge.tool set to unknown tool: $merge_tool"
287 echo >&2 "Resetting to default..."
293 if test -z "$merge_tool" ; then
294 if type kdiff3
>/dev
/null
2>&1 && test -n "$DISPLAY"; then
296 elif type tkdiff
>/dev
/null
2>&1 && test -n "$DISPLAY"; then
298 elif type xxdiff
>/dev
/null
2>&1 && test -n "$DISPLAY"; then
300 elif type meld
>/dev
/null
2>&1 && test -n "$DISPLAY"; then
302 elif type emacs
>/dev
/null
2>&1; then
304 elif type vimdiff
>/dev
/null
2>&1; then
307 echo "No available merge resolution programs available."
312 case "$merge_tool" in
313 kdiff3|tkdiff|meld|xxdiff|vimdiff
)
314 if ! type "$merge_tool" > /dev
/null
2>&1; then
315 echo "The merge tool $merge_tool is not available"
320 if ! type "emacs" > /dev
/null
2>&1; then
321 echo "Emacs is not available"
326 echo "Unknown merge tool: $merge_tool"
331 if test $# -eq 0 ; then
332 files
=`git ls-files -u | sed -e 's/^[^ ]* //' | sort -u`
333 if test -z "$files" ; then
334 echo "No files need merging"
337 echo Merging the files
: $files
338 git ls-files
-u |
sed -e 's/^[^ ]* //' |
sort -u |
while read i
341 merge_file
"$i" < /dev
/tty
> /dev
/tty
344 while test $# -gt 0; do