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
17 function is_symlink
() {
21 function local_present
() {
25 function remote_present
() {
26 test -n "$remote_mode"
29 function base_present
() {
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"
42 function describe_file
() {
48 if test -z "$mode"; then
49 echo -n "'$path' was deleted"
50 elif is_symlink
"$mode" ; then
51 echo -n "'$path' is a symlink containing '"
56 echo -n "'$path' was created"
58 echo -n "'$path' was modified"
61 echo " in the $branch branch"
65 resolve_symlink_merge
() {
67 echo -n "Use (r)emote or (l)ocal, or (a)bort? "
71 git-checkout-index
-f --stage=2 -- "$path"
73 cleanup_temp_files
--save-backup
77 git-checkout-index
-f --stage=3 -- "$path"
79 cleanup_temp_files
--save-backup
89 resolve_deleted_merge
() {
91 echo -n "Use (m)odified or (d)eleted file, or (a)bort? "
96 cleanup_temp_files
--save-backup
114 if test ! -f "$path" ; then
115 echo "$path: file not found"
119 f
=`git-ls-files -u -- "$path"`
120 if test -z "$f" ; then
121 echo "$path: file does not need merging"
125 BACKUP
="$path.BACKUP.$$"
126 LOCAL
="$path.LOCAL.$$"
127 REMOTE
="$path.REMOTE.$$"
130 mv -- "$path" "$BACKUP"
131 cp -- "$BACKUP" "$path"
133 base_mode
=`git ls-files -u -- "$path" | awk '{if ($3==1) print $1;}'`
134 local_mode
=`git ls-files -u -- "$path" | awk '{if ($3==2) print $1;}'`
135 remote_mode
=`git ls-files -u -- "$path" | awk '{if ($3==3) print $1;}'`
137 base_present
&& git cat-file blob
":1:$path" > "$BASE" 2>/dev
/null
138 local_present
&& git cat-file blob
":2:$path" > "$LOCAL" 2>/dev
/null
139 remote_present
&& git cat-file blob
":3:$path" > "$REMOTE" 2>/dev
/null
141 if test -z "$local_mode" -o -z "$remote_mode"; then
142 echo "Deleted merge conflict for $path:"
143 describe_file
"$local_mode" "local" "$LOCAL"
144 describe_file
"$remote_mode" "remote" "$REMOTE"
145 resolve_deleted_merge
149 if is_symlink
"$local_mode" || is_symlink
"$remote_mode"; then
150 echo "Symlink merge conflict for $path:"
151 describe_file
"$local_mode" "local" "$LOCAL"
152 describe_file
"$remote_mode" "remote" "$REMOTE"
153 resolve_symlink_merge
157 echo "Normal merge conflict for $path:"
158 describe_file
"$local_mode" "local" "$LOCAL"
159 describe_file
"$remote_mode" "remote" "$REMOTE"
160 echo -n "Hit return to start merge resolution tool ($merge_tool): "
163 case "$merge_tool" in
165 if base_present
; then
166 (kdiff3
--auto --L1 "$path (Base)" -L2 "$path (Local)" --L3 "$path (Remote)" \
167 -o "$path" -- "$BASE" "$LOCAL" "$REMOTE" > /dev
/null
2>&1)
169 (kdiff3
--auto -L1 "$path (Local)" --L2 "$path (Remote)" \
170 -o "$path" -- "$LOCAL" "$REMOTE" > /dev
/null
2>&1)
173 if test "$status" -eq 0; then
178 if base_present
; then
179 tkdiff
-a "$BASE" -o "$path" -- "$LOCAL" "$REMOTE"
181 tkdiff
-o "$path" -- "$LOCAL" "$REMOTE"
184 if test "$status" -eq 0; then
185 mv -- "$BACKUP" "$path.orig"
190 meld
-- "$LOCAL" "$path" "$REMOTE"
191 if test "$path" -nt "$BACKUP" ; then
195 echo "$path seems unchanged."
196 echo -n "Was the merge successful? [y/n] "
197 read answer
< /dev
/tty
199 y
*|Y
*) status
=0; break ;;
200 n
*|N
*) status
=1; break ;;
204 if test "$status" -eq 0; then
205 mv -- "$BACKUP" "$path.orig"
210 if base_present
; then
211 xxdiff
-X --show-merged-pane \
212 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
213 -R 'Accel.Search: "Ctrl+F"' \
214 -R 'Accel.SearchForward: "Ctrl-G"' \
215 --merged-file "$path" -- "$LOCAL" "$BASE" "$REMOTE"
217 xxdiff
-X --show-merged-pane \
218 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
219 -R 'Accel.Search: "Ctrl+F"' \
220 -R 'Accel.SearchForward: "Ctrl-G"' \
221 --merged-file "$path" -- "$LOCAL" "$REMOTE"
223 if test "$path" -nt "$BACKUP" ; then
227 echo "$path seems unchanged."
228 echo -n "Was the merge successful? [y/n] "
229 read answer
< /dev
/tty
231 y
*|Y
*) status
=0; break ;;
232 n
*|N
*) status
=1; break ;;
236 if test "$status" -eq 0; then
237 mv -- "$BACKUP" "$path.orig"
241 if base_present
; then
242 emacs
-f emerge-files-with-ancestor-command
"$LOCAL" "$REMOTE" "$BASE" "$path"
244 emacs
-f emerge-files-command
"$LOCAL" "$REMOTE" "$path"
247 if test "$status" -eq 0; then
248 mv -- "$BACKUP" "$path.orig"
252 if test "$status" -ne 0; then
253 echo "merge of $path failed" 1>&2
254 mv -- "$BACKUP" "$path"
261 while case $# in 0) break ;; esac
267 merge_tool
=`expr "z$1" : 'z-[^=]*=\(.*\)'`
289 if test -z "$merge_tool"; then
290 merge_tool
=`git-config merge.tool`
291 if test $merge_tool = kdiff3
-o $merge_tool = tkdiff
-o \
292 $merge_tool = xxdiff
-o $merge_tool = meld
; then
297 if test -z "$merge_tool" ; then
298 if type kdiff3
>/dev
/null
2>&1 && test -n "$DISPLAY"; then
300 elif type tkdiff
>/dev
/null
2>&1 && test -n "$DISPLAY"; then
302 elif type xxdiff
>/dev
/null
2>&1 && test -n "$DISPLAY"; then
304 elif type meld
>/dev
/null
2>&1 && test -n "$DISPLAY"; then
306 elif type emacs
>/dev
/null
2>&1; then
309 echo "No available merge resolution programs available."
314 case "$merge_tool" in
315 kdiff3|tkdiff|meld|xxdiff
)
316 if ! type "$merge_tool" > /dev
/null
2>&1; then
317 echo "The merge tool $merge_tool is not available"
322 if ! type "emacs" > /dev
/null
2>&1; then
323 echo "Emacs is not available"
328 echo "Unknown merge tool: $merge_tool"
333 if test $# -eq 0 ; then
334 files
=`git ls-files -u | sed -e 's/^[^ ]* //' | sort -u`
335 if test -z "$files" ; then
336 echo "No files need merging"
339 echo Merging the files
: $files
340 git ls-files
-u |
sed -e 's/^[^ ]* //' |
sort -u |
while read i
343 merge_file
"$i" < /dev
/tty
> /dev
/tty
346 while test $# -gt 0; do