Changed an internal variable of mergetool to support custom commands
[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
8f321a39 13OPTIONS_SPEC=
c4b4a5af
TT
14. git-sh-setup
15require_work_tree
769f3986 16prefix=$(git rev-parse --show-prefix)
c4b4a5af
TT
17
18# Returns true if the mode reflects a symlink
262c981e 19is_symlink () {
c4b4a5af
TT
20 test "$1" = 120000
21}
22
262c981e 23local_present () {
c4b4a5af
TT
24 test -n "$local_mode"
25}
26
262c981e 27remote_present () {
c4b4a5af
TT
28 test -n "$remote_mode"
29}
30
262c981e 31base_present () {
c4b4a5af
TT
32 test -n "$base_mode"
33}
34
35cleanup_temp_files () {
36 if test "$1" = --save-backup ; then
b3ea27e4 37 mv -- "$BACKUP" "$MERGED.orig"
c4b4a5af
TT
38 rm -f -- "$LOCAL" "$REMOTE" "$BASE"
39 else
40 rm -f -- "$LOCAL" "$REMOTE" "$BASE" "$BACKUP"
41 fi
42}
43
262c981e 44describe_file () {
c4b4a5af
TT
45 mode="$1"
46 branch="$2"
47 file="$3"
48
27090aa1 49 printf " {%s}: " "$branch"
c4b4a5af 50 if test -z "$mode"; then
27090aa1 51 echo "deleted"
c4b4a5af 52 elif is_symlink "$mode" ; then
27090aa1 53 echo "a symbolic link -> '$(cat "$file")'"
c4b4a5af
TT
54 else
55 if base_present; then
27090aa1 56 echo "modified"
c4b4a5af 57 else
27090aa1 58 echo "created"
c4b4a5af
TT
59 fi
60 fi
c4b4a5af
TT
61}
62
63
64resolve_symlink_merge () {
d1dc6959 65 while true; do
27090aa1 66 printf "Use (l)ocal or (r)emote, or (a)bort? "
c4b4a5af
TT
67 read ans
68 case "$ans" in
69 [lL]*)
b3ea27e4
CB
70 git checkout-index -f --stage=2 -- "$MERGED"
71 git add -- "$MERGED"
c4b4a5af
TT
72 cleanup_temp_files --save-backup
73 return
74 ;;
5a174f1a 75 [rR]*)
b3ea27e4
CB
76 git checkout-index -f --stage=3 -- "$MERGED"
77 git add -- "$MERGED"
c4b4a5af
TT
78 cleanup_temp_files --save-backup
79 return
80 ;;
5a174f1a 81 [aA]*)
c4b4a5af
TT
82 exit 1
83 ;;
84 esac
85 done
86}
87
88resolve_deleted_merge () {
d1dc6959 89 while true; do
27090aa1
TT
90 if base_present; then
91 printf "Use (m)odified or (d)eleted file, or (a)bort? "
92 else
93 printf "Use (c)reated or (d)eleted file, or (a)bort? "
94 fi
c4b4a5af
TT
95 read ans
96 case "$ans" in
27090aa1 97 [mMcC]*)
b3ea27e4 98 git add -- "$MERGED"
c4b4a5af
TT
99 cleanup_temp_files --save-backup
100 return
101 ;;
5a174f1a 102 [dD]*)
b3ea27e4 103 git rm -- "$MERGED" > /dev/null
c4b4a5af
TT
104 cleanup_temp_files
105 return
106 ;;
5a174f1a 107 [aA]*)
c4b4a5af
TT
108 exit 1
109 ;;
110 esac
111 done
112}
113
ddc0c497 114check_unchanged () {
b3ea27e4 115 if test "$MERGED" -nt "$BACKUP" ; then
ddc0c497
TT
116 status=0;
117 else
118 while true; do
b3ea27e4 119 echo "$MERGED seems unchanged."
ddc0c497
TT
120 printf "Was the merge successful? [y/n] "
121 read answer < /dev/tty
122 case "$answer" in
123 y*|Y*) status=0; break ;;
124 n*|N*) status=1; break ;;
125 esac
126 done
127 fi
128}
129
c4b4a5af 130merge_file () {
b3ea27e4 131 MERGED="$1"
c4b4a5af 132
b3ea27e4 133 f=`git ls-files -u -- "$MERGED"`
c4b4a5af 134 if test -z "$f" ; then
b3ea27e4
CB
135 if test ! -f "$MERGED" ; then
136 echo "$MERGED: file not found"
ce5b6d75 137 else
b3ea27e4 138 echo "$MERGED: file does not need merging"
ce5b6d75 139 fi
c4b4a5af
TT
140 exit 1
141 fi
142
b3ea27e4
CB
143 ext="$$$(expr "$MERGED" : '.*\(\.[^/]*\)$')"
144 BACKUP="$MERGED.BACKUP.$ext"
145 LOCAL="$MERGED.LOCAL.$ext"
146 REMOTE="$MERGED.REMOTE.$ext"
147 BASE="$MERGED.BASE.$ext"
c4b4a5af 148
b3ea27e4
CB
149 mv -- "$MERGED" "$BACKUP"
150 cp -- "$BACKUP" "$MERGED"
c4b4a5af 151
b3ea27e4
CB
152 base_mode=`git ls-files -u -- "$MERGED" | awk '{if ($3==1) print $1;}'`
153 local_mode=`git ls-files -u -- "$MERGED" | awk '{if ($3==2) print $1;}'`
154 remote_mode=`git ls-files -u -- "$MERGED" | awk '{if ($3==3) print $1;}'`
c4b4a5af 155
b3ea27e4
CB
156 base_present && git cat-file blob ":1:$prefix$MERGED" >"$BASE" 2>/dev/null
157 local_present && git cat-file blob ":2:$prefix$MERGED" >"$LOCAL" 2>/dev/null
158 remote_present && git cat-file blob ":3:$prefix$MERGED" >"$REMOTE" 2>/dev/null
c4b4a5af
TT
159
160 if test -z "$local_mode" -o -z "$remote_mode"; then
b3ea27e4 161 echo "Deleted merge conflict for '$MERGED':"
c4b4a5af
TT
162 describe_file "$local_mode" "local" "$LOCAL"
163 describe_file "$remote_mode" "remote" "$REMOTE"
164 resolve_deleted_merge
165 return
166 fi
167
168 if is_symlink "$local_mode" || is_symlink "$remote_mode"; then
b3ea27e4 169 echo "Symbolic link merge conflict for '$MERGED':"
c4b4a5af
TT
170 describe_file "$local_mode" "local" "$LOCAL"
171 describe_file "$remote_mode" "remote" "$REMOTE"
172 resolve_symlink_merge
173 return
174 fi
175
b3ea27e4 176 echo "Normal merge conflict for '$MERGED':"
c4b4a5af
TT
177 describe_file "$local_mode" "local" "$LOCAL"
178 describe_file "$remote_mode" "remote" "$REMOTE"
20fa04ea 179 printf "Hit return to start merge resolution tool (%s): " "$merge_tool"
c4b4a5af
TT
180 read ans
181
182 case "$merge_tool" in
183 kdiff3)
184 if base_present ; then
b3ea27e4
CB
185 ("$merge_tool_path" --auto --L1 "$MERGED (Base)" --L2 "$MERGED (Local)" --L3 "$MERGED (Remote)" \
186 -o "$MERGED" -- "$BASE" "$LOCAL" "$REMOTE" > /dev/null 2>&1)
c4b4a5af 187 else
b3ea27e4
CB
188 ("$merge_tool_path" --auto --L1 "$MERGED (Local)" --L2 "$MERGED (Remote)" \
189 -o "$MERGED" -- "$LOCAL" "$REMOTE" > /dev/null 2>&1)
c4b4a5af
TT
190 fi
191 status=$?
c4b4a5af
TT
192 ;;
193 tkdiff)
194 if base_present ; then
b3ea27e4 195 "$merge_tool_path" -a "$BASE" -o "$MERGED" -- "$LOCAL" "$REMOTE"
c4b4a5af 196 else
b3ea27e4 197 "$merge_tool_path" -o "$MERGED" -- "$LOCAL" "$REMOTE"
c4b4a5af
TT
198 fi
199 status=$?
c4b4a5af 200 ;;
9cec6539 201 meld|vimdiff)
c4b4a5af 202 touch "$BACKUP"
b3ea27e4 203 "$merge_tool_path" -- "$LOCAL" "$MERGED" "$REMOTE"
ddc0c497 204 check_unchanged
c4b4a5af 205 ;;
730b5b45 206 gvimdiff)
44c36d1c 207 touch "$BACKUP"
b3ea27e4 208 "$merge_tool_path" -f -- "$LOCAL" "$MERGED" "$REMOTE"
44c36d1c
CB
209 check_unchanged
210 ;;
c4b4a5af
TT
211 xxdiff)
212 touch "$BACKUP"
213 if base_present ; then
e3fa2c76 214 "$merge_tool_path" -X --show-merged-pane \
c4b4a5af
TT
215 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
216 -R 'Accel.Search: "Ctrl+F"' \
217 -R 'Accel.SearchForward: "Ctrl-G"' \
b3ea27e4 218 --merged-file "$MERGED" -- "$LOCAL" "$BASE" "$REMOTE"
c4b4a5af 219 else
e3fa2c76 220 "$merge_tool_path" -X --show-merged-pane \
c4b4a5af
TT
221 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
222 -R 'Accel.Search: "Ctrl+F"' \
223 -R 'Accel.SearchForward: "Ctrl-G"' \
b3ea27e4 224 --merged-file "$MERGED" -- "$LOCAL" "$REMOTE"
c4b4a5af 225 fi
ddc0c497 226 check_unchanged
c4b4a5af 227 ;;
365cf979
TT
228 opendiff)
229 touch "$BACKUP"
230 if base_present; then
b3ea27e4 231 "$merge_tool_path" "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$MERGED" | cat
365cf979 232 else
b3ea27e4 233 "$merge_tool_path" "$LOCAL" "$REMOTE" -merge "$MERGED" | cat
365cf979
TT
234 fi
235 check_unchanged
365cf979 236 ;;
ca8e6b7a
SP
237 ecmerge)
238 touch "$BACKUP"
239 if base_present; then
b3ea27e4 240 "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" --mode=merge3 --to="$MERGED"
ca8e6b7a 241 else
b3ea27e4 242 "$merge_tool_path" "$LOCAL" "$REMOTE" --mode=merge2 --to="$MERGED"
ca8e6b7a
SP
243 fi
244 check_unchanged
ca8e6b7a 245 ;;
c4b4a5af
TT
246 emerge)
247 if base_present ; then
b3ea27e4 248 "$merge_tool_path" -f emerge-files-with-ancestor-command "$LOCAL" "$REMOTE" "$BASE" "$(basename "$MERGED")"
c4b4a5af 249 else
b3ea27e4 250 "$merge_tool_path" -f emerge-files-command "$LOCAL" "$REMOTE" "$(basename "$MERGED")"
c4b4a5af
TT
251 fi
252 status=$?
c4b4a5af
TT
253 ;;
254 esac
255 if test "$status" -ne 0; then
b3ea27e4
CB
256 echo "merge of $MERGED failed" 1>&2
257 mv -- "$BACKUP" "$MERGED"
c4b4a5af
TT
258 exit 1
259 fi
44c36d1c
CB
260
261 if test "$merge_keep_backup" = "true"; then
b3ea27e4 262 mv -- "$BACKUP" "$MERGED.orig"
44c36d1c
CB
263 else
264 rm -- "$BACKUP"
265 fi
266
b3ea27e4 267 git add -- "$MERGED"
c4b4a5af
TT
268 cleanup_temp_files
269}
270
822f7c73 271while test $# != 0
c4b4a5af
TT
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
e3fa2c76
SP
299valid_tool() {
300 case "$1" in
ca8e6b7a 301 kdiff3 | tkdiff | xxdiff | meld | opendiff | emerge | vimdiff | gvimdiff | ecmerge)
e3fa2c76
SP
302 ;; # happy
303 *)
304 return 1
305 ;;
306 esac
307}
308
309init_merge_tool_path() {
310 merge_tool_path=`git config mergetool.$1.path`
311 if test -z "$merge_tool_path" ; then
312 case "$1" in
313 emerge)
314 merge_tool_path=emacs
315 ;;
316 *)
317 merge_tool_path=$1
318 ;;
319 esac
320 fi
321}
322
323
c4b4a5af 324if test -z "$merge_tool"; then
5be60078 325 merge_tool=`git config merge.tool`
d279fc1a 326 if test -n "$merge_tool" && ! valid_tool "$merge_tool"; then
d6678c28
TT
327 echo >&2 "git config option merge.tool set to unknown tool: $merge_tool"
328 echo >&2 "Resetting to default..."
329 unset merge_tool
e3fa2c76 330 fi
c4b4a5af
TT
331fi
332
333if test -z "$merge_tool" ; then
301ac38b
TT
334 if test -n "$DISPLAY"; then
335 merge_tool_candidates="kdiff3 tkdiff xxdiff meld gvimdiff"
336 if test -n "$GNOME_DESKTOP_SESSION_ID" ; then
337 merge_tool_candidates="meld $merge_tool_candidates"
338 fi
339 if test "$KDE_FULL_SESSION" = "true"; then
340 merge_tool_candidates="kdiff3 $merge_tool_candidates"
341 fi
342 fi
343 if echo "${VISUAL:-$EDITOR}" | grep 'emacs' > /dev/null 2>&1; then
344 merge_tool_candidates="$merge_tool_candidates emerge"
345 fi
346 if echo "${VISUAL:-$EDITOR}" | grep 'vim' > /dev/null 2>&1; then
347 merge_tool_candidates="$merge_tool_candidates vimdiff"
348 fi
349 merge_tool_candidates="$merge_tool_candidates opendiff emerge vimdiff"
350 echo "merge tool candidates: $merge_tool_candidates"
351 for i in $merge_tool_candidates; do
e3fa2c76
SP
352 init_merge_tool_path $i
353 if type "$merge_tool_path" > /dev/null 2>&1; then
301ac38b
TT
354 merge_tool=$i
355 break
356 fi
357 done
358 if test -z "$merge_tool" ; then
e3fa2c76 359 echo "No known merge resolution program available."
c4b4a5af
TT
360 exit 1
361 fi
e3fa2c76
SP
362else
363 if ! valid_tool "$merge_tool"; then
364 echo >&2 "Unknown merge_tool $merge_tool"
365 exit 1
366 fi
367
368 init_merge_tool_path "$merge_tool"
369
44c36d1c
CB
370 merge_keep_backup="$(git config --bool merge.keepBackup || echo true)"
371
e3fa2c76
SP
372 if ! type "$merge_tool_path" > /dev/null 2>&1; then
373 echo "The merge tool $merge_tool is not available as '$merge_tool_path'"
374 exit 1
375 fi
c4b4a5af
TT
376fi
377
c4b4a5af
TT
378
379if test $# -eq 0 ; then
380 files=`git ls-files -u | sed -e 's/^[^ ]* //' | sort -u`
381 if test -z "$files" ; then
382 echo "No files need merging"
383 exit 0
384 fi
2e8fd781
RD
385 echo Merging the files: "$files"
386 git ls-files -u |
387 sed -e 's/^[^ ]* //' |
388 sort -u |
389 while IFS= read i
c4b4a5af 390 do
20fa04ea 391 printf "\n"
c4b4a5af
TT
392 merge_file "$i" < /dev/tty > /dev/tty
393 done
394else
395 while test $# -gt 0; do
20fa04ea 396 printf "\n"
c4b4a5af
TT
397 merge_file "$1"
398 shift
399 done
400fi
401exit 0