git-merge-octopus.sh 2.42 KB
Newer Older
1 2 3 4 5 6 7
#!/bin/sh
#
# Copyright (c) 2005 Junio C Hamano
#
# Resolve two or more trees.
#

8
. git-sh-setup
9

10 11 12
LF='
'

13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
# The first parameters up to -- are merge bases; the rest are heads.
bases= head= remotes= sep_seen=
for arg
do
	case ",$sep_seen,$head,$arg," in
	*,--,)
		sep_seen=yes
		;;
	,yes,,*)
		head=$arg
		;;
	,yes,*)
		remotes="$remotes$arg "
		;;
	*)
		bases="$bases$arg "
		;;
	esac
done

33
# Reject if this is not an octopus -- resolve should be used instead.
34 35 36 37 38 39 40 41 42 43
case "$remotes" in
?*' '?*)
	;;
*)
	exit 2 ;;
esac

# MRC is the current "merge reference commit"
# MRT is the current "merge result tree"

44 45
if ! git diff-index --quiet --cached HEAD --
then
46
    gettextln "Error: Your local changes to the following files would be overwritten by merge"
47 48 49
    git diff-index --cached --name-only HEAD -- | sed -e 's/^/    /'
    exit 2
fi
Stephen Boyd's avatar
Stephen Boyd committed
50
MRC=$(git rev-parse --verify -q $head)
51
MRT=$(git write-tree)
52
NON_FF_MERGE=0
53
OCTOPUS_FAILURE=0
54 55
for SHA1 in $remotes
do
56 57 58 59 60
	case "$OCTOPUS_FAILURE" in
	1)
		# We allow only last one to have a hand-resolvable
		# conflicts.  Last round failed and we still had
		# a head to merge.
61
		gettextln "Automated merge did not work."
62
		gettextln "Should not be doing an octopus."
63 64 65
		exit 2
	esac

66
	eval pretty_name=\${GITHEAD_$SHA1:-$SHA1}
67 68 69 70 71
	if test "$SHA1" = "$pretty_name"
	then
		SHA1_UP="$(echo "$SHA1" | tr a-z A-Z)"
		eval pretty_name=\${GITHEAD_$SHA1_UP:-$pretty_name}
	fi
72
	common=$(git merge-base --all $SHA1 $MRC) ||
73
		die "$(eval_gettext "Unable to find common commit with \$pretty_name")"
74

75 76
	case "$LF$common$LF" in
	*"$LF$SHA1$LF"*)
77
		eval_gettextln "Already up to date with \$pretty_name"
78
		continue
79 80
		;;
	esac
81 82 83 84 85 86 87 88

	if test "$common,$NON_FF_MERGE" = "$MRC,0"
	then
		# The first head being merged was a fast-forward.
		# Advance MRC to the head being merged, and use that
		# tree as the intermediate result of the merge.
		# We still need to count this as part of the parent set.

89
		eval_gettextln "Fast-forwarding to: \$pretty_name"
90 91
		git read-tree -u -m $head $SHA1 || exit
		MRC=$SHA1 MRT=$(git write-tree)
92 93 94 95 96
		continue
	fi

	NON_FF_MERGE=1

97
	eval_gettextln "Trying simple merge with \$pretty_name"
98 99
	git read-tree -u -m --aggressive  $common $MRT $SHA1 || exit 2
	next=$(git write-tree 2>/dev/null)
100 101
	if test $? -ne 0
	then
102
		gettextln "Simple merge did not work, trying automatic merge."
103
		git merge-index -o git-merge-one-file -a ||
104
		OCTOPUS_FAILURE=1
105
		next=$(git write-tree 2>/dev/null)
106
	fi
107

108
	MRC="$MRC $SHA1"
109 110 111
	MRT=$next
done

112
exit "$OCTOPUS_FAILURE"