Add valgrind support in test scripts
[git.git] / git-mergetool.sh
blob09f3a1068f69718fa544e3e614b1040ed33ab292
1 #!/bin/sh
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 Hamano.
11 USAGE='[--tool=tool] [-y|--no-prompt|--prompt] [file to merge] ...'
12 SUBDIRECTORY_OK=Yes
13 OPTIONS_SPEC=
14 . git-sh-setup
15 require_work_tree
16 prefix=$(git rev-parse --show-prefix)
18 # Returns true if the mode reflects a symlink
19 is_symlink () {
20 test "$1" = 120000
23 local_present () {
24 test -n "$local_mode"
27 remote_present () {
28 test -n "$remote_mode"
31 base_present () {
32 test -n "$base_mode"
35 cleanup_temp_files () {
36 if test "$1" = --save-backup ; then
37 mv -- "$BACKUP" "$MERGED.orig"
38 rm -f -- "$LOCAL" "$REMOTE" "$BASE"
39 else
40 rm -f -- "$LOCAL" "$REMOTE" "$BASE" "$BACKUP"
44 describe_file () {
45 mode="$1"
46 branch="$2"
47 file="$3"
49 printf " {%s}: " "$branch"
50 if test -z "$mode"; then
51 echo "deleted"
52 elif is_symlink "$mode" ; then
53 echo "a symbolic link -> '$(cat "$file")'"
54 else
55 if base_present; then
56 echo "modified"
57 else
58 echo "created"
64 resolve_symlink_merge () {
65 while true; do
66 printf "Use (l)ocal or (r)emote, or (a)bort? "
67 read ans
68 case "$ans" in
69 [lL]*)
70 git checkout-index -f --stage=2 -- "$MERGED"
71 git add -- "$MERGED"
72 cleanup_temp_files --save-backup
73 return 0
75 [rR]*)
76 git checkout-index -f --stage=3 -- "$MERGED"
77 git add -- "$MERGED"
78 cleanup_temp_files --save-backup
79 return 0
81 [aA]*)
82 return 1
84 esac
85 done
88 resolve_deleted_merge () {
89 while true; do
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? "
95 read ans
96 case "$ans" in
97 [mMcC]*)
98 git add -- "$MERGED"
99 cleanup_temp_files --save-backup
100 return 0
102 [dD]*)
103 git rm -- "$MERGED" > /dev/null
104 cleanup_temp_files
105 return 0
107 [aA]*)
108 return 1
110 esac
111 done
114 check_unchanged () {
115 if test "$MERGED" -nt "$BACKUP" ; then
116 status=0;
117 else
118 while true; do
119 echo "$MERGED seems unchanged."
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
130 merge_file () {
131 MERGED="$1"
133 f=`git ls-files -u -- "$MERGED"`
134 if test -z "$f" ; then
135 if test ! -f "$MERGED" ; then
136 echo "$MERGED: file not found"
137 else
138 echo "$MERGED: file does not need merging"
140 return 1
143 ext="$$$(expr "$MERGED" : '.*\(\.[^/]*\)$')"
144 BACKUP="./$MERGED.BACKUP.$ext"
145 LOCAL="./$MERGED.LOCAL.$ext"
146 REMOTE="./$MERGED.REMOTE.$ext"
147 BASE="./$MERGED.BASE.$ext"
149 mv -- "$MERGED" "$BACKUP"
150 cp -- "$BACKUP" "$MERGED"
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;}'`
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
160 if test -z "$local_mode" -o -z "$remote_mode"; then
161 echo "Deleted merge conflict for '$MERGED':"
162 describe_file "$local_mode" "local" "$LOCAL"
163 describe_file "$remote_mode" "remote" "$REMOTE"
164 resolve_deleted_merge
165 return
168 if is_symlink "$local_mode" || is_symlink "$remote_mode"; then
169 echo "Symbolic link merge conflict for '$MERGED':"
170 describe_file "$local_mode" "local" "$LOCAL"
171 describe_file "$remote_mode" "remote" "$REMOTE"
172 resolve_symlink_merge
173 return
176 echo "Normal merge conflict for '$MERGED':"
177 describe_file "$local_mode" "local" "$LOCAL"
178 describe_file "$remote_mode" "remote" "$REMOTE"
179 if "$prompt" = true; then
180 printf "Hit return to start merge resolution tool (%s): " "$merge_tool"
181 read ans
184 case "$merge_tool" in
185 kdiff3)
186 if base_present ; then
187 ("$merge_tool_path" --auto --L1 "$MERGED (Base)" --L2 "$MERGED (Local)" --L3 "$MERGED (Remote)" \
188 -o "$MERGED" "$BASE" "$LOCAL" "$REMOTE" > /dev/null 2>&1)
189 else
190 ("$merge_tool_path" --auto --L1 "$MERGED (Local)" --L2 "$MERGED (Remote)" \
191 -o "$MERGED" "$LOCAL" "$REMOTE" > /dev/null 2>&1)
193 status=$?
195 tkdiff)
196 if base_present ; then
197 "$merge_tool_path" -a "$BASE" -o "$MERGED" "$LOCAL" "$REMOTE"
198 else
199 "$merge_tool_path" -o "$MERGED" "$LOCAL" "$REMOTE"
201 status=$?
203 meld)
204 touch "$BACKUP"
205 "$merge_tool_path" "$LOCAL" "$MERGED" "$REMOTE"
206 check_unchanged
208 vimdiff)
209 touch "$BACKUP"
210 "$merge_tool_path" -c "wincmd l" "$LOCAL" "$MERGED" "$REMOTE"
211 check_unchanged
213 gvimdiff)
214 touch "$BACKUP"
215 "$merge_tool_path" -c "wincmd l" -f "$LOCAL" "$MERGED" "$REMOTE"
216 check_unchanged
218 xxdiff)
219 touch "$BACKUP"
220 if base_present ; then
221 "$merge_tool_path" -X --show-merged-pane \
222 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
223 -R 'Accel.Search: "Ctrl+F"' \
224 -R 'Accel.SearchForward: "Ctrl-G"' \
225 --merged-file "$MERGED" "$LOCAL" "$BASE" "$REMOTE"
226 else
227 "$merge_tool_path" -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 "$MERGED" "$LOCAL" "$REMOTE"
233 check_unchanged
235 opendiff)
236 touch "$BACKUP"
237 if base_present; then
238 "$merge_tool_path" "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$MERGED" | cat
239 else
240 "$merge_tool_path" "$LOCAL" "$REMOTE" -merge "$MERGED" | cat
242 check_unchanged
244 ecmerge)
245 touch "$BACKUP"
246 if base_present; then
247 "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" --default --mode=merge3 --to="$MERGED"
248 else
249 "$merge_tool_path" "$LOCAL" "$REMOTE" --default --mode=merge2 --to="$MERGED"
251 check_unchanged
253 emerge)
254 if base_present ; then
255 "$merge_tool_path" -f emerge-files-with-ancestor-command "$LOCAL" "$REMOTE" "$BASE" "$(basename "$MERGED")"
256 else
257 "$merge_tool_path" -f emerge-files-command "$LOCAL" "$REMOTE" "$(basename "$MERGED")"
259 status=$?
262 if test -n "$merge_tool_cmd"; then
263 if test "$merge_tool_trust_exit_code" = "false"; then
264 touch "$BACKUP"
265 ( eval $merge_tool_cmd )
266 check_unchanged
267 else
268 ( eval $merge_tool_cmd )
269 status=$?
273 esac
274 if test "$status" -ne 0; then
275 echo "merge of $MERGED failed" 1>&2
276 mv -- "$BACKUP" "$MERGED"
278 if test "$merge_keep_temporaries" = "false"; then
279 cleanup_temp_files
282 return 1
285 if test "$merge_keep_backup" = "true"; then
286 mv -- "$BACKUP" "$MERGED.orig"
287 else
288 rm -- "$BACKUP"
291 git add -- "$MERGED"
292 cleanup_temp_files
293 return 0
296 prompt=$(git config --bool mergetool.prompt || echo true)
298 while test $# != 0
300 case "$1" in
301 -t|--tool*)
302 case "$#,$1" in
303 *,*=*)
304 merge_tool=`expr "z$1" : 'z-[^=]*=\(.*\)'`
306 1,*)
307 usage ;;
309 merge_tool="$2"
310 shift ;;
311 esac
313 -y|--no-prompt)
314 prompt=false
316 --prompt)
317 prompt=true
320 shift
321 break
324 usage
327 break
329 esac
330 shift
331 done
333 valid_custom_tool()
335 merge_tool_cmd="$(git config mergetool.$1.cmd)"
336 test -n "$merge_tool_cmd"
339 valid_tool() {
340 case "$1" in
341 kdiff3 | tkdiff | xxdiff | meld | opendiff | emerge | vimdiff | gvimdiff | ecmerge)
342 ;; # happy
344 if ! valid_custom_tool "$1"; then
345 return 1
348 esac
351 init_merge_tool_path() {
352 merge_tool_path=`git config mergetool.$1.path`
353 if test -z "$merge_tool_path" ; then
354 case "$1" in
355 emerge)
356 merge_tool_path=emacs
359 merge_tool_path=$1
361 esac
365 prompt_after_failed_merge() {
366 while true; do
367 printf "Continue merging other unresolved paths (y/n) ? "
368 read ans
369 case "$ans" in
371 [yY]*)
372 return 0
375 [nN]*)
376 return 1
378 esac
379 done
382 if test -z "$merge_tool"; then
383 merge_tool=`git config merge.tool`
384 if test -n "$merge_tool" && ! valid_tool "$merge_tool"; then
385 echo >&2 "git config option merge.tool set to unknown tool: $merge_tool"
386 echo >&2 "Resetting to default..."
387 unset merge_tool
391 if test -z "$merge_tool" ; then
392 if test -n "$DISPLAY"; then
393 if test -n "$GNOME_DESKTOP_SESSION_ID" ; then
394 merge_tool_candidates="meld kdiff3 tkdiff xxdiff gvimdiff"
395 else
396 merge_tool_candidates="kdiff3 tkdiff xxdiff meld gvimdiff"
399 if echo "${VISUAL:-$EDITOR}" | grep 'emacs' > /dev/null 2>&1; then
400 merge_tool_candidates="$merge_tool_candidates emerge opendiff vimdiff"
401 elif echo "${VISUAL:-$EDITOR}" | grep 'vim' > /dev/null 2>&1; then
402 merge_tool_candidates="$merge_tool_candidates vimdiff opendiff emerge"
403 else
404 merge_tool_candidates="$merge_tool_candidates opendiff emerge vimdiff"
406 echo "merge tool candidates: $merge_tool_candidates"
407 for i in $merge_tool_candidates; do
408 init_merge_tool_path $i
409 if type "$merge_tool_path" > /dev/null 2>&1; then
410 merge_tool=$i
411 break
413 done
414 if test -z "$merge_tool" ; then
415 echo "No known merge resolution program available."
416 exit 1
418 else
419 if ! valid_tool "$merge_tool"; then
420 echo >&2 "Unknown merge_tool $merge_tool"
421 exit 1
424 init_merge_tool_path "$merge_tool"
426 merge_keep_backup="$(git config --bool merge.keepBackup || echo true)"
427 merge_keep_temporaries="$(git config --bool mergetool.keepTemporaries || echo false)"
429 if test -z "$merge_tool_cmd" && ! type "$merge_tool_path" > /dev/null 2>&1; then
430 echo "The merge tool $merge_tool is not available as '$merge_tool_path'"
431 exit 1
434 if ! test -z "$merge_tool_cmd"; then
435 merge_tool_trust_exit_code="$(git config --bool mergetool.$merge_tool.trustExitCode || echo false)"
439 last_status=0
440 rollup_status=0
442 if test $# -eq 0 ; then
443 files=`git ls-files -u | sed -e 's/^[^ ]* //' | sort -u`
444 if test -z "$files" ; then
445 echo "No files need merging"
446 exit 0
448 echo Merging the files: "$files"
449 git ls-files -u |
450 sed -e 's/^[^ ]* //' |
451 sort -u |
452 while IFS= read i
454 if test $last_status -ne 0; then
455 prompt_after_failed_merge < /dev/tty || exit 1
457 printf "\n"
458 merge_file "$i" < /dev/tty > /dev/tty
459 last_status=$?
460 if test $last_status -ne 0; then
461 rollup_status=1
463 done
464 else
465 while test $# -gt 0; do
466 if test $last_status -ne 0; then
467 prompt_after_failed_merge || exit 1
469 printf "\n"
470 merge_file "$1"
471 last_status=$?
472 if test $last_status -ne 0; then
473 rollup_status=1
475 shift
476 done
479 exit $rollup_status