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