git-mergetool.sh 10.7 KB
Newer Older
1 2 3 4 5 6 7
#!/bin/sh
#
# This program resolves merge conflicts in git
#
# Copyright (c) 2006 Theodore Y. Ts'o
#
# This file is licensed under the GPL v2, or a later version
J
Josh Triplett 已提交
8
# at the discretion of Junio C Hamano.
9 10
#

11
USAGE='[--tool=tool] [-y|--no-prompt|--prompt] [file to merge] ...'
12
SUBDIRECTORY_OK=Yes
13
OPTIONS_SPEC=
14 15
. git-sh-setup
require_work_tree
16
prefix=$(git rev-parse --show-prefix)
17 18

# Returns true if the mode reflects a symlink
19
is_symlink () {
20 21 22
    test "$1" = 120000
}

23
local_present () {
24 25 26
    test -n "$local_mode"
}

27
remote_present () {
28 29 30
    test -n "$remote_mode"
}

31
base_present () {
32 33 34 35 36
    test -n "$base_mode"
}

cleanup_temp_files () {
    if test "$1" = --save-backup ; then
37
	mv -- "$BACKUP" "$MERGED.orig"
38 39 40 41 42 43
	rm -f -- "$LOCAL" "$REMOTE" "$BASE"
    else
	rm -f -- "$LOCAL" "$REMOTE" "$BASE" "$BACKUP"
    fi
}

44
describe_file () {
45 46 47 48
    mode="$1"
    branch="$2"
    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 55
    else
	if base_present; then
56
	    echo "modified"
57
	else
58
	    echo "created"
59 60 61 62 63 64
	fi
    fi
}


resolve_symlink_merge () {
65
    while true; do
66
	printf "Use (l)ocal or (r)emote, or (a)bort? "
67 68 69
	read ans
	case "$ans" in
	    [lL]*)
70 71
		git checkout-index -f --stage=2 -- "$MERGED"
		git add -- "$MERGED"
72
		cleanup_temp_files --save-backup
73
		return 0
74
		;;
75
	    [rR]*)
76 77
		git checkout-index -f --stage=3 -- "$MERGED"
		git add -- "$MERGED"
78
		cleanup_temp_files --save-backup
79
		return 0
80
		;;
81
	    [aA]*)
82
		return 1
83 84 85 86 87 88
		;;
	    esac
	done
}

resolve_deleted_merge () {
89
    while true; do
90 91 92 93 94
	if base_present; then
	    printf "Use (m)odified or (d)eleted file, or (a)bort? "
	else
	    printf "Use (c)reated or (d)eleted file, or (a)bort? "
	fi
95 96
	read ans
	case "$ans" in
97
	    [mMcC]*)
98
		git add -- "$MERGED"
99
		cleanup_temp_files --save-backup
100
		return 0
101
		;;
102
	    [dD]*)
103
		git rm -- "$MERGED" > /dev/null
104
		cleanup_temp_files
105
		return 0
106
		;;
107
	    [aA]*)
108
		return 1
109 110 111 112 113
		;;
	    esac
	done
}

114
check_unchanged () {
115
    if test "$MERGED" -nt "$BACKUP" ; then
116 117 118
	status=0;
    else
	while true; do
119
	    echo "$MERGED seems unchanged."
120 121 122 123 124 125 126 127 128 129
	    printf "Was the merge successful? [y/n] "
	    read answer < /dev/tty
	    case "$answer" in
		y*|Y*) status=0; break ;;
		n*|N*) status=1; break ;;
	    esac
	done
    fi
}

130
merge_file () {
131
    MERGED="$1"
132

133
    f=`git ls-files -u -- "$MERGED"`
134
    if test -z "$f" ; then
135 136
	if test ! -f "$MERGED" ; then
	    echo "$MERGED: file not found"
137
	else
138
	    echo "$MERGED: file does not need merging"
139
	fi
140
	return 1
141 142
    fi

143
    ext="$$$(expr "$MERGED" : '.*\(\.[^/]*\)$')"
144 145 146 147
    BACKUP="./$MERGED.BACKUP.$ext"
    LOCAL="./$MERGED.LOCAL.$ext"
    REMOTE="./$MERGED.REMOTE.$ext"
    BASE="./$MERGED.BASE.$ext"
148

149 150
    mv -- "$MERGED" "$BACKUP"
    cp -- "$BACKUP" "$MERGED"
151

152 153 154
    base_mode=`git ls-files -u -- "$MERGED" | awk '{if ($3==1) print $1;}'`
    local_mode=`git ls-files -u -- "$MERGED" | awk '{if ($3==2) print $1;}'`
    remote_mode=`git ls-files -u -- "$MERGED" | awk '{if ($3==3) print $1;}'`
155

156 157 158
    base_present   && git cat-file blob ":1:$prefix$MERGED" >"$BASE" 2>/dev/null
    local_present  && git cat-file blob ":2:$prefix$MERGED" >"$LOCAL" 2>/dev/null
    remote_present && git cat-file blob ":3:$prefix$MERGED" >"$REMOTE" 2>/dev/null
159 160

    if test -z "$local_mode" -o -z "$remote_mode"; then
161
	echo "Deleted merge conflict for '$MERGED':"
162 163 164 165 166 167 168
	describe_file "$local_mode" "local" "$LOCAL"
	describe_file "$remote_mode" "remote" "$REMOTE"
	resolve_deleted_merge
	return
    fi

    if is_symlink "$local_mode" || is_symlink "$remote_mode"; then
169
	echo "Symbolic link merge conflict for '$MERGED':"
170 171 172 173 174 175
	describe_file "$local_mode" "local" "$LOCAL"
	describe_file "$remote_mode" "remote" "$REMOTE"
	resolve_symlink_merge
	return
    fi

176
    echo "Normal merge conflict for '$MERGED':"
177 178
    describe_file "$local_mode" "local" "$LOCAL"
    describe_file "$remote_mode" "remote" "$REMOTE"
179 180 181 182
    if "$prompt" = true; then
	printf "Hit return to start merge resolution tool (%s): " "$merge_tool"
	read ans
    fi
183 184 185 186

    case "$merge_tool" in
	kdiff3)
	    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)
192 193 194 195 196
	    fi
	    status=$?
	    ;;
	tkdiff)
	    if base_present ; then
197
		"$merge_tool_path" -a "$BASE" -o "$MERGED" "$LOCAL" "$REMOTE"
198
	    else
199
		"$merge_tool_path" -o "$MERGED" "$LOCAL" "$REMOTE"
200 201 202
	    fi
	    status=$?
	    ;;
203
	meld|vimdiff)
204
	    touch "$BACKUP"
205
	    "$merge_tool_path" "$LOCAL" "$MERGED" "$REMOTE"
206
	    check_unchanged
207
	    ;;
208
	gvimdiff)
209
	    touch "$BACKUP"
210
	    "$merge_tool_path" -f "$LOCAL" "$MERGED" "$REMOTE"
211 212
	    check_unchanged
	    ;;
213 214 215
	xxdiff)
	    touch "$BACKUP"
	    if base_present ; then
216
		"$merge_tool_path" -X --show-merged-pane \
217 218 219
		    -R 'Accel.SaveAsMerged: "Ctrl-S"' \
		    -R 'Accel.Search: "Ctrl+F"' \
		    -R 'Accel.SearchForward: "Ctrl-G"' \
220
		    --merged-file "$MERGED" "$LOCAL" "$BASE" "$REMOTE"
221
	    else
222
		"$merge_tool_path" -X --show-merged-pane \
223 224 225
		    -R 'Accel.SaveAsMerged: "Ctrl-S"' \
		    -R 'Accel.Search: "Ctrl+F"' \
		    -R 'Accel.SearchForward: "Ctrl-G"' \
226
		    --merged-file "$MERGED" "$LOCAL" "$REMOTE"
227
	    fi
228
	    check_unchanged
229
	    ;;
230 231 232
	opendiff)
	    touch "$BACKUP"
	    if base_present; then
233
		"$merge_tool_path" "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$MERGED" | cat
234
	    else
235
		"$merge_tool_path" "$LOCAL" "$REMOTE" -merge "$MERGED" | cat
236 237 238
	    fi
	    check_unchanged
	    ;;
239 240 241
	ecmerge)
	    touch "$BACKUP"
	    if base_present; then
242
		"$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" --default --mode=merge3 --to="$MERGED"
243
	    else
244
		"$merge_tool_path" "$LOCAL" "$REMOTE" --default --mode=merge2 --to="$MERGED"
245 246 247
	    fi
	    check_unchanged
	    ;;
248 249
	emerge)
	    if base_present ; then
250
		"$merge_tool_path" -f emerge-files-with-ancestor-command "$LOCAL" "$REMOTE" "$BASE" "$(basename "$MERGED")"
251
	    else
252
		"$merge_tool_path" -f emerge-files-command "$LOCAL" "$REMOTE" "$(basename "$MERGED")"
253 254 255
	    fi
	    status=$?
	    ;;
256 257 258 259 260 261 262 263 264 265 266 267
	*)
	    if test -n "$merge_tool_cmd"; then
		if test "$merge_tool_trust_exit_code" = "false"; then
		    touch "$BACKUP"
		    ( eval $merge_tool_cmd )
		    check_unchanged
		else
		    ( eval $merge_tool_cmd )
		    status=$?
		fi
	    fi
	    ;;
268 269
    esac
    if test "$status" -ne 0; then
270 271
	echo "merge of $MERGED failed" 1>&2
	mv -- "$BACKUP" "$MERGED"
272 273 274 275 276

	if test "$merge_keep_temporaries" = "false"; then
	    cleanup_temp_files
	fi

277
	return 1
278
    fi
279 280

    if test "$merge_keep_backup" = "true"; then
281
	mv -- "$BACKUP" "$MERGED.orig"
282 283 284 285
    else
	rm -- "$BACKUP"
    fi

286
    git add -- "$MERGED"
287
    cleanup_temp_files
288
    return 0
289 290
}

291 292
prompt=$(git config --bool mergetool.prompt || echo true)

293
while test $# != 0
294 295 296 297 298 299 300 301 302 303 304 305 306 307
do
    case "$1" in
	-t|--tool*)
	    case "$#,$1" in
		*,*=*)
		    merge_tool=`expr "z$1" : 'z-[^=]*=\(.*\)'`
		    ;;
		1,*)
		    usage ;;
		*)
		    merge_tool="$2"
		    shift ;;
	    esac
	    ;;
308 309 310 311 312 313
	-y|--no-prompt)
	    prompt=false
	    ;;
	--prompt)
	    prompt=true
	    ;;
314
	--)
315
	    shift
316 317 318 319 320 321 322 323 324 325 326 327
	    break
	    ;;
	-*)
	    usage
	    ;;
	*)
	    break
	    ;;
    esac
    shift
done

328 329 330 331 332 333
valid_custom_tool()
{
    merge_tool_cmd="$(git config mergetool.$1.cmd)"
    test -n "$merge_tool_cmd"
}

334 335
valid_tool() {
	case "$1" in
336
		kdiff3 | tkdiff | xxdiff | meld | opendiff | emerge | vimdiff | gvimdiff | ecmerge)
337 338
			;; # happy
		*)
339 340 341
			if ! valid_custom_tool "$1"; then
				return 1
			fi
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
			;;
	esac
}

init_merge_tool_path() {
	merge_tool_path=`git config mergetool.$1.path`
	if test -z "$merge_tool_path" ; then
		case "$1" in
			emerge)
				merge_tool_path=emacs
				;;
			*)
				merge_tool_path=$1
				;;
		esac
	fi
}

360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
prompt_after_failed_merge() {
    while true; do
	printf "Continue merging other unresolved paths (y/n) ? "
	read ans
	case "$ans" in

	    [yY]*)
		return 0
		;;

	    [nN]*)
		return 1
		;;
	esac
    done
}
376

377
if test -z "$merge_tool"; then
378
    merge_tool=`git config merge.tool`
379
    if test -n "$merge_tool" && ! valid_tool "$merge_tool"; then
380 381 382
	    echo >&2 "git config option merge.tool set to unknown tool: $merge_tool"
	    echo >&2 "Resetting to default..."
	    unset merge_tool
383
    fi
384 385 386
fi

if test -z "$merge_tool" ; then
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
    if test -n "$DISPLAY"; then
        merge_tool_candidates="kdiff3 tkdiff xxdiff meld gvimdiff"
        if test -n "$GNOME_DESKTOP_SESSION_ID" ; then
            merge_tool_candidates="meld $merge_tool_candidates"
        fi
        if test "$KDE_FULL_SESSION" = "true"; then
            merge_tool_candidates="kdiff3 $merge_tool_candidates"
        fi
    fi
    if echo "${VISUAL:-$EDITOR}" | grep 'emacs' > /dev/null 2>&1; then
        merge_tool_candidates="$merge_tool_candidates emerge"
    fi
    if echo "${VISUAL:-$EDITOR}" | grep 'vim' > /dev/null 2>&1; then
        merge_tool_candidates="$merge_tool_candidates vimdiff"
    fi
    merge_tool_candidates="$merge_tool_candidates opendiff emerge vimdiff"
    echo "merge tool candidates: $merge_tool_candidates"
    for i in $merge_tool_candidates; do
405 406
        init_merge_tool_path $i
        if type "$merge_tool_path" > /dev/null 2>&1; then
407 408 409 410 411
            merge_tool=$i
            break
        fi
    done
    if test -z "$merge_tool" ; then
412
	echo "No known merge resolution program available."
413 414
	exit 1
    fi
415 416 417 418 419 420 421 422
else
    if ! valid_tool "$merge_tool"; then
        echo >&2 "Unknown merge_tool $merge_tool"
        exit 1
    fi

    init_merge_tool_path "$merge_tool"

423
    merge_keep_backup="$(git config --bool merge.keepBackup || echo true)"
424
    merge_keep_temporaries="$(git config --bool mergetool.keepTemporaries || echo false)"
425

426
    if test -z "$merge_tool_cmd" && ! type "$merge_tool_path" > /dev/null 2>&1; then
427 428 429
        echo "The merge tool $merge_tool is not available as '$merge_tool_path'"
        exit 1
    fi
430 431 432 433

    if ! test -z "$merge_tool_cmd"; then
        merge_tool_trust_exit_code="$(git config --bool mergetool.$merge_tool.trustExitCode || echo false)"
    fi
434 435
fi

436 437
last_status=0
rollup_status=0
438 439

if test $# -eq 0 ; then
440 441 442 443 444 445 446 447 448 449 450
    files=`git ls-files -u | sed -e 's/^[^	]*	//' | sort -u`
    if test -z "$files" ; then
	echo "No files need merging"
	exit 0
    fi
    echo Merging the files: "$files"
    git ls-files -u |
    sed -e 's/^[^	]*	//' |
    sort -u |
    while IFS= read i
    do
451 452
	if test $last_status -ne 0; then
	    prompt_after_failed_merge < /dev/tty || exit 1
453
	fi
454 455
	printf "\n"
	merge_file "$i" < /dev/tty > /dev/tty
456 457 458 459
	last_status=$?
	if test $last_status -ne 0; then
	    rollup_status=1
	fi
460
    done
461
else
462
    while test $# -gt 0; do
463 464 465
	if test $last_status -ne 0; then
	    prompt_after_failed_merge || exit 1
	fi
466 467
	printf "\n"
	merge_file "$1"
468 469 470 471
	last_status=$?
	if test $last_status -ne 0; then
	    rollup_status=1
	fi
472 473
	shift
    done
474
fi
475 476

exit $rollup_status