mergetool: Fix abort command when resolving symlinks and deleted files
[git/git.git] / git-mergetool.sh
CommitLineData
c4b4a5af
TT
1#!/bin/sh
2#
3# This program resolves merge conflicts in git
4#
5# Copyright (c) 2006 Theodore Y. Ts'o
6#
7# This file is licensed under the GPL v2, or a later version
8# at the discretion of Junio C Hammano.
9#
10
11USAGE='[--tool=tool] [file to merge] ...'
12SUBDIRECTORY_OK=Yes
13. git-sh-setup
14require_work_tree
15
16# Returns true if the mode reflects a symlink
262c981e 17is_symlink () {
c4b4a5af
TT
18 test "$1" = 120000
19}
20
262c981e 21local_present () {
c4b4a5af
TT
22 test -n "$local_mode"
23}
24
262c981e 25remote_present () {
c4b4a5af
TT
26 test -n "$remote_mode"
27}
28
262c981e 29base_present () {
c4b4a5af
TT
30 test -n "$base_mode"
31}
32
33cleanup_temp_files () {
34 if test "$1" = --save-backup ; then
35 mv -- "$BACKUP" "$path.orig"
36 rm -f -- "$LOCAL" "$REMOTE" "$BASE"
37 else
38 rm -f -- "$LOCAL" "$REMOTE" "$BASE" "$BACKUP"
39 fi
40}
41
262c981e 42describe_file () {
c4b4a5af
TT
43 mode="$1"
44 branch="$2"
45 file="$3"
46
20fa04ea 47 printf " "
c4b4a5af 48 if test -z "$mode"; then
20fa04ea 49 printf "'%s' was deleted" "$path"
c4b4a5af 50 elif is_symlink "$mode" ; then
20fa04ea 51 printf "'%s' is a symlink containing '%s'" "$path" "$file"
c4b4a5af
TT
52 else
53 if base_present; then
20fa04ea 54 printf "'%s' was created" "$path"
c4b4a5af 55 else
20fa04ea 56 printf "'%s' was modified" "$path"
c4b4a5af
TT
57 fi
58 fi
59 echo " in the $branch branch"
60}
61
62
63resolve_symlink_merge () {
d1dc6959 64 while true; do
20fa04ea 65 printf "Use (r)emote or (l)ocal, or (a)bort? "
c4b4a5af
TT
66 read ans
67 case "$ans" in
68 [lL]*)
69 git-checkout-index -f --stage=2 -- "$path"
70 git-add -- "$path"
71 cleanup_temp_files --save-backup
72 return
73 ;;
5a174f1a 74 [rR]*)
c4b4a5af
TT
75 git-checkout-index -f --stage=3 -- "$path"
76 git-add -- "$path"
77 cleanup_temp_files --save-backup
78 return
79 ;;
5a174f1a 80 [aA]*)
c4b4a5af
TT
81 exit 1
82 ;;
83 esac
84 done
85}
86
87resolve_deleted_merge () {
d1dc6959 88 while true; do
20fa04ea 89 printf "Use (m)odified or (d)eleted file, or (a)bort? "
c4b4a5af
TT
90 read ans
91 case "$ans" in
92 [mM]*)
93 git-add -- "$path"
94 cleanup_temp_files --save-backup
95 return
96 ;;
5a174f1a 97 [dD]*)
c4b4a5af
TT
98 git-rm -- "$path"
99 cleanup_temp_files
100 return
101 ;;
5a174f1a 102 [aA]*)
c4b4a5af
TT
103 exit 1
104 ;;
105 esac
106 done
107}
108
ddc0c497
TT
109check_unchanged () {
110 if test "$path" -nt "$BACKUP" ; then
111 status=0;
112 else
113 while true; do
114 echo "$path seems unchanged."
115 printf "Was the merge successful? [y/n] "
116 read answer < /dev/tty
117 case "$answer" in
118 y*|Y*) status=0; break ;;
119 n*|N*) status=1; break ;;
120 esac
121 done
122 fi
123}
124
125save_backup () {
126 if test "$status" -eq 0; then
127 mv -- "$BACKUP" "$path.orig"
128 fi
129}
130
131remove_backup () {
132 if test "$status" -eq 0; then
133 rm "$BACKUP"
134 fi
135}
136
c4b4a5af
TT
137merge_file () {
138 path="$1"
139
c4b4a5af
TT
140 f=`git-ls-files -u -- "$path"`
141 if test -z "$f" ; then
ddc0c497 142 if test ! -f "$path" ; then
ce5b6d75
TT
143 echo "$path: file not found"
144 else
145 echo "$path: file does not need merging"
146 fi
c4b4a5af
TT
147 exit 1
148 fi
149
150 BACKUP="$path.BACKUP.$$"
151 LOCAL="$path.LOCAL.$$"
152 REMOTE="$path.REMOTE.$$"
153 BASE="$path.BASE.$$"
154
155 mv -- "$path" "$BACKUP"
156 cp -- "$BACKUP" "$path"
157
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;}'`
161
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
165
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
171 return
172 fi
173
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
179 return
180 fi
181
182 echo "Normal merge conflict for $path:"
183 describe_file "$local_mode" "local" "$LOCAL"
184 describe_file "$remote_mode" "remote" "$REMOTE"
20fa04ea 185 printf "Hit return to start merge resolution tool (%s): " "$merge_tool"
c4b4a5af
TT
186 read ans
187
188 case "$merge_tool" in
189 kdiff3)
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)
193 else
194 (kdiff3 --auto -L1 "$path (Local)" --L2 "$path (Remote)" \
195 -o "$path" -- "$LOCAL" "$REMOTE" > /dev/null 2>&1)
196 fi
197 status=$?
ddc0c497 198 remove_backup
c4b4a5af
TT
199 ;;
200 tkdiff)
201 if base_present ; then
202 tkdiff -a "$BASE" -o "$path" -- "$LOCAL" "$REMOTE"
203 else
204 tkdiff -o "$path" -- "$LOCAL" "$REMOTE"
205 fi
206 status=$?
ddc0c497 207 save_backup
c4b4a5af 208 ;;
9cec6539 209 meld|vimdiff)
c4b4a5af 210 touch "$BACKUP"
9cec6539 211 $merge_tool -- "$LOCAL" "$path" "$REMOTE"
ddc0c497
TT
212 check_unchanged
213 save_backup
c4b4a5af
TT
214 ;;
215 xxdiff)
216 touch "$BACKUP"
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"
223 else
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"
229 fi
ddc0c497
TT
230 check_unchanged
231 save_backup
c4b4a5af
TT
232 ;;
233 emerge)
234 if base_present ; then
235 emacs -f emerge-files-with-ancestor-command "$LOCAL" "$REMOTE" "$BASE" "$path"
236 else
237 emacs -f emerge-files-command "$LOCAL" "$REMOTE" "$path"
238 fi
239 status=$?
ddc0c497 240 save_backup
c4b4a5af
TT
241 ;;
242 esac
243 if test "$status" -ne 0; then
244 echo "merge of $path failed" 1>&2
245 mv -- "$BACKUP" "$path"
246 exit 1
247 fi
248 git add -- "$path"
249 cleanup_temp_files
250}
251
252while case $# in 0) break ;; esac
253do
254 case "$1" in
255 -t|--tool*)
256 case "$#,$1" in
257 *,*=*)
258 merge_tool=`expr "z$1" : 'z-[^=]*=\(.*\)'`
259 ;;
260 1,*)
261 usage ;;
262 *)
263 merge_tool="$2"
264 shift ;;
265 esac
266 ;;
267 --)
268 break
269 ;;
270 -*)
271 usage
272 ;;
273 *)
274 break
275 ;;
276 esac
277 shift
278done
279
280if test -z "$merge_tool"; then
281 merge_tool=`git-config merge.tool`
d6678c28 282 case "$merge_tool" in
b7b36f92 283 kdiff3 | tkdiff | xxdiff | meld | emerge | vimdiff | "")
d6678c28
TT
284 ;; # happy
285 *)
286 echo >&2 "git config option merge.tool set to unknown tool: $merge_tool"
287 echo >&2 "Resetting to default..."
288 unset merge_tool
289 ;;
290 esac
c4b4a5af
TT
291fi
292
293if test -z "$merge_tool" ; then
294 if type kdiff3 >/dev/null 2>&1 && test -n "$DISPLAY"; then
295 merge_tool="kdiff3";
296 elif type tkdiff >/dev/null 2>&1 && test -n "$DISPLAY"; then
297 merge_tool=tkdiff
298 elif type xxdiff >/dev/null 2>&1 && test -n "$DISPLAY"; then
299 merge_tool=xxdiff
300 elif type meld >/dev/null 2>&1 && test -n "$DISPLAY"; then
301 merge_tool=meld
302 elif type emacs >/dev/null 2>&1; then
303 merge_tool=emerge
9cec6539
JB
304 elif type vimdiff >/dev/null 2>&1; then
305 merge_tool=vimdiff
c4b4a5af
TT
306 else
307 echo "No available merge resolution programs available."
308 exit 1
309 fi
310fi
311
312case "$merge_tool" in
9cec6539 313 kdiff3|tkdiff|meld|xxdiff|vimdiff)
c4b4a5af
TT
314 if ! type "$merge_tool" > /dev/null 2>&1; then
315 echo "The merge tool $merge_tool is not available"
316 exit 1
317 fi
318 ;;
319 emerge)
320 if ! type "emacs" > /dev/null 2>&1; then
321 echo "Emacs is not available"
322 exit 1
323 fi
324 ;;
325 *)
326 echo "Unknown merge tool: $merge_tool"
327 exit 1
328 ;;
329esac
330
331if 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"
335 exit 0
336 fi
337 echo Merging the files: $files
338 git ls-files -u | sed -e 's/^[^ ]* //' | sort -u | while read i
339 do
20fa04ea 340 printf "\n"
c4b4a5af
TT
341 merge_file "$i" < /dev/tty > /dev/tty
342 done
343else
344 while test $# -gt 0; do
20fa04ea 345 printf "\n"
c4b4a5af
TT
346 merge_file "$1"
347 shift
348 done
349fi
350exit 0