[PATCH] git-mergetool: Allow gvimdiff to be used as a mergetool
[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
2571ac67 8# at the discretion of Junio C Hamano.
c4b4a5af
TT
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
27090aa1 47 printf " {%s}: " "$branch"
c4b4a5af 48 if test -z "$mode"; then
27090aa1 49 echo "deleted"
c4b4a5af 50 elif is_symlink "$mode" ; then
27090aa1 51 echo "a symbolic link -> '$(cat "$file")'"
c4b4a5af
TT
52 else
53 if base_present; then
27090aa1 54 echo "modified"
c4b4a5af 55 else
27090aa1 56 echo "created"
c4b4a5af
TT
57 fi
58 fi
c4b4a5af
TT
59}
60
61
62resolve_symlink_merge () {
d1dc6959 63 while true; do
27090aa1 64 printf "Use (l)ocal or (r)emote, or (a)bort? "
c4b4a5af
TT
65 read ans
66 case "$ans" in
67 [lL]*)
68 git-checkout-index -f --stage=2 -- "$path"
69 git-add -- "$path"
70 cleanup_temp_files --save-backup
71 return
72 ;;
5a174f1a 73 [rR]*)
c4b4a5af
TT
74 git-checkout-index -f --stage=3 -- "$path"
75 git-add -- "$path"
76 cleanup_temp_files --save-backup
77 return
78 ;;
5a174f1a 79 [aA]*)
c4b4a5af
TT
80 exit 1
81 ;;
82 esac
83 done
84}
85
86resolve_deleted_merge () {
d1dc6959 87 while true; do
27090aa1
TT
88 if base_present; then
89 printf "Use (m)odified or (d)eleted file, or (a)bort? "
90 else
91 printf "Use (c)reated or (d)eleted file, or (a)bort? "
92 fi
c4b4a5af
TT
93 read ans
94 case "$ans" in
27090aa1 95 [mMcC]*)
c4b4a5af
TT
96 git-add -- "$path"
97 cleanup_temp_files --save-backup
98 return
99 ;;
5a174f1a 100 [dD]*)
1346c999 101 git-rm -- "$path" > /dev/null
c4b4a5af
TT
102 cleanup_temp_files
103 return
104 ;;
5a174f1a 105 [aA]*)
c4b4a5af
TT
106 exit 1
107 ;;
108 esac
109 done
110}
111
ddc0c497
TT
112check_unchanged () {
113 if test "$path" -nt "$BACKUP" ; then
114 status=0;
115 else
116 while true; do
117 echo "$path seems unchanged."
118 printf "Was the merge successful? [y/n] "
119 read answer < /dev/tty
120 case "$answer" in
121 y*|Y*) status=0; break ;;
122 n*|N*) status=1; break ;;
123 esac
124 done
125 fi
126}
127
128save_backup () {
129 if test "$status" -eq 0; then
130 mv -- "$BACKUP" "$path.orig"
131 fi
132}
133
134remove_backup () {
135 if test "$status" -eq 0; then
136 rm "$BACKUP"
137 fi
138}
139
c4b4a5af
TT
140merge_file () {
141 path="$1"
142
c4b4a5af
TT
143 f=`git-ls-files -u -- "$path"`
144 if test -z "$f" ; then
ddc0c497 145 if test ! -f "$path" ; then
ce5b6d75
TT
146 echo "$path: file not found"
147 else
148 echo "$path: file does not need merging"
149 fi
c4b4a5af
TT
150 exit 1
151 fi
152
153 BACKUP="$path.BACKUP.$$"
154 LOCAL="$path.LOCAL.$$"
155 REMOTE="$path.REMOTE.$$"
156 BASE="$path.BASE.$$"
157
158 mv -- "$path" "$BACKUP"
159 cp -- "$BACKUP" "$path"
160
161 base_mode=`git ls-files -u -- "$path" | awk '{if ($3==1) print $1;}'`
162 local_mode=`git ls-files -u -- "$path" | awk '{if ($3==2) print $1;}'`
163 remote_mode=`git ls-files -u -- "$path" | awk '{if ($3==3) print $1;}'`
164
165 base_present && git cat-file blob ":1:$path" > "$BASE" 2>/dev/null
166 local_present && git cat-file blob ":2:$path" > "$LOCAL" 2>/dev/null
167 remote_present && git cat-file blob ":3:$path" > "$REMOTE" 2>/dev/null
168
169 if test -z "$local_mode" -o -z "$remote_mode"; then
27090aa1 170 echo "Deleted merge conflict for '$path':"
c4b4a5af
TT
171 describe_file "$local_mode" "local" "$LOCAL"
172 describe_file "$remote_mode" "remote" "$REMOTE"
173 resolve_deleted_merge
174 return
175 fi
176
177 if is_symlink "$local_mode" || is_symlink "$remote_mode"; then
27090aa1 178 echo "Symbolic link merge conflict for '$path':"
c4b4a5af
TT
179 describe_file "$local_mode" "local" "$LOCAL"
180 describe_file "$remote_mode" "remote" "$REMOTE"
181 resolve_symlink_merge
182 return
183 fi
184
27090aa1 185 echo "Normal merge conflict for '$path':"
c4b4a5af
TT
186 describe_file "$local_mode" "local" "$LOCAL"
187 describe_file "$remote_mode" "remote" "$REMOTE"
20fa04ea 188 printf "Hit return to start merge resolution tool (%s): " "$merge_tool"
c4b4a5af
TT
189 read ans
190
191 case "$merge_tool" in
192 kdiff3)
193 if base_present ; then
194 (kdiff3 --auto --L1 "$path (Base)" -L2 "$path (Local)" --L3 "$path (Remote)" \
195 -o "$path" -- "$BASE" "$LOCAL" "$REMOTE" > /dev/null 2>&1)
196 else
197 (kdiff3 --auto -L1 "$path (Local)" --L2 "$path (Remote)" \
198 -o "$path" -- "$LOCAL" "$REMOTE" > /dev/null 2>&1)
199 fi
200 status=$?
ddc0c497 201 remove_backup
c4b4a5af
TT
202 ;;
203 tkdiff)
204 if base_present ; then
205 tkdiff -a "$BASE" -o "$path" -- "$LOCAL" "$REMOTE"
206 else
207 tkdiff -o "$path" -- "$LOCAL" "$REMOTE"
208 fi
209 status=$?
ddc0c497 210 save_backup
c4b4a5af 211 ;;
9cec6539 212 meld|vimdiff)
c4b4a5af 213 touch "$BACKUP"
9cec6539 214 $merge_tool -- "$LOCAL" "$path" "$REMOTE"
ddc0c497
TT
215 check_unchanged
216 save_backup
c4b4a5af 217 ;;
730b5b45
DM
218 gvimdiff)
219 touch "$BACKUP"
220 gvimdiff -f -- "$LOCAL" "$path" "$REMOTE"
221 check_unchanged
222 save_backup
223 ;;
c4b4a5af
TT
224 xxdiff)
225 touch "$BACKUP"
226 if base_present ; then
227 xxdiff -X --show-merged-pane \
228 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
229 -R 'Accel.Search: "Ctrl+F"' \
230 -R 'Accel.SearchForward: "Ctrl-G"' \
231 --merged-file "$path" -- "$LOCAL" "$BASE" "$REMOTE"
232 else
233 xxdiff -X --show-merged-pane \
234 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
235 -R 'Accel.Search: "Ctrl+F"' \
236 -R 'Accel.SearchForward: "Ctrl-G"' \
237 --merged-file "$path" -- "$LOCAL" "$REMOTE"
238 fi
ddc0c497
TT
239 check_unchanged
240 save_backup
c4b4a5af 241 ;;
365cf979
TT
242 opendiff)
243 touch "$BACKUP"
244 if base_present; then
245 opendiff "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$path" | cat
246 else
247 opendiff "$LOCAL" "$REMOTE" -merge "$path" | cat
248 fi
249 check_unchanged
250 save_backup
251 ;;
c4b4a5af
TT
252 emerge)
253 if base_present ; then
254 emacs -f emerge-files-with-ancestor-command "$LOCAL" "$REMOTE" "$BASE" "$path"
255 else
256 emacs -f emerge-files-command "$LOCAL" "$REMOTE" "$path"
257 fi
258 status=$?
ddc0c497 259 save_backup
c4b4a5af
TT
260 ;;
261 esac
262 if test "$status" -ne 0; then
263 echo "merge of $path failed" 1>&2
264 mv -- "$BACKUP" "$path"
265 exit 1
266 fi
267 git add -- "$path"
268 cleanup_temp_files
269}
270
271while case $# in 0) break ;; esac
272do
273 case "$1" in
274 -t|--tool*)
275 case "$#,$1" in
276 *,*=*)
277 merge_tool=`expr "z$1" : 'z-[^=]*=\(.*\)'`
278 ;;
279 1,*)
280 usage ;;
281 *)
282 merge_tool="$2"
283 shift ;;
284 esac
285 ;;
286 --)
287 break
288 ;;
289 -*)
290 usage
291 ;;
292 *)
293 break
294 ;;
295 esac
296 shift
297done
298
299if test -z "$merge_tool"; then
300 merge_tool=`git-config merge.tool`
d6678c28 301 case "$merge_tool" in
730b5b45 302 kdiff3 | tkdiff | xxdiff | meld | opendiff | emerge | vimdiff | gvimdiff | "")
d6678c28
TT
303 ;; # happy
304 *)
305 echo >&2 "git config option merge.tool set to unknown tool: $merge_tool"
306 echo >&2 "Resetting to default..."
307 unset merge_tool
308 ;;
309 esac
c4b4a5af
TT
310fi
311
312if test -z "$merge_tool" ; then
313 if type kdiff3 >/dev/null 2>&1 && test -n "$DISPLAY"; then
314 merge_tool="kdiff3";
315 elif type tkdiff >/dev/null 2>&1 && test -n "$DISPLAY"; then
316 merge_tool=tkdiff
317 elif type xxdiff >/dev/null 2>&1 && test -n "$DISPLAY"; then
318 merge_tool=xxdiff
319 elif type meld >/dev/null 2>&1 && test -n "$DISPLAY"; then
320 merge_tool=meld
730b5b45
DM
321 elif type gvimdiff >/dev/null 2>&1 && test -n "$DISPLAY"; then
322 merge_tool=gvimdiff
365cf979
TT
323 elif type opendiff >/dev/null 2>&1; then
324 merge_tool=opendiff
c4b4a5af
TT
325 elif type emacs >/dev/null 2>&1; then
326 merge_tool=emerge
9cec6539
JB
327 elif type vimdiff >/dev/null 2>&1; then
328 merge_tool=vimdiff
c4b4a5af
TT
329 else
330 echo "No available merge resolution programs available."
331 exit 1
332 fi
333fi
334
335case "$merge_tool" in
730b5b45 336 kdiff3|tkdiff|meld|xxdiff|vimdiff|gvimdiff|opendiff)
c4b4a5af
TT
337 if ! type "$merge_tool" > /dev/null 2>&1; then
338 echo "The merge tool $merge_tool is not available"
339 exit 1
340 fi
341 ;;
342 emerge)
343 if ! type "emacs" > /dev/null 2>&1; then
344 echo "Emacs is not available"
345 exit 1
346 fi
347 ;;
348 *)
349 echo "Unknown merge tool: $merge_tool"
350 exit 1
351 ;;
352esac
353
354if test $# -eq 0 ; then
355 files=`git ls-files -u | sed -e 's/^[^ ]* //' | sort -u`
356 if test -z "$files" ; then
357 echo "No files need merging"
358 exit 0
359 fi
360 echo Merging the files: $files
361 git ls-files -u | sed -e 's/^[^ ]* //' | sort -u | while read i
362 do
20fa04ea 363 printf "\n"
c4b4a5af
TT
364 merge_file "$i" < /dev/tty > /dev/tty
365 done
366else
367 while test $# -gt 0; do
20fa04ea 368 printf "\n"
c4b4a5af
TT
369 merge_file "$1"
370 shift
371 done
372fi
373exit 0