octopus: allow criss-cross and clarify the message when it rejects
[git.git] / git-merge-octopus.sh
blobd1f9f3d2d046395cea94a85a5861fc353afb8ec4
1 #!/bin/sh
3 # Copyright (c) 2005 Junio C Hamano
5 # Resolve two or more trees.
8 LF='
11 die () {
12 echo >&2 "$*"
13 exit 1
16 # The first parameters up to -- are merge bases; the rest are heads.
17 bases= head= remotes= sep_seen=
18 for arg
20 case ",$sep_seen,$head,$arg," in
21 *,--,)
22 sep_seen=yes
24 ,yes,,*)
25 head=$arg
27 ,yes,*)
28 remotes="$remotes$arg "
31 bases="$bases$arg "
33 esac
34 done
36 # Reject if this is not an Octopus -- resolve should be used instead.
37 case "$remotes" in
38 ?*' '?*)
41 exit 2 ;;
42 esac
44 # MRC is the current "merge reference commit"
45 # MRT is the current "merge result tree"
47 MRC=$head MSG= PARENT="-p $head"
48 MRT=$(git-write-tree)
49 CNT=1 ;# counting our head
50 NON_FF_MERGE=0
51 for SHA1 in $remotes
53 common=$(git-merge-base --all $MRC $SHA1) ||
54 die "Unable to find common commit with $SHA1"
56 case "$LF$common$LF" in
57 *"$LF$SHA1$LF"*)
58 echo "Already up-to-date with $SHA1"
59 continue
61 esac
63 CNT=`expr $CNT + 1`
64 PARENT="$PARENT -p $SHA1"
66 if test "$common,$NON_FF_MERGE" = "$MRC,0"
67 then
68 # The first head being merged was a fast-forward.
69 # Advance MRC to the head being merged, and use that
70 # tree as the intermediate result of the merge.
71 # We still need to count this as part of the parent set.
73 echo "Fast forwarding to: $SHA1"
74 git-read-tree -u -m $head $SHA1 || exit
75 MRC=$SHA1 MRT=$(git-write-tree)
76 continue
79 NON_FF_MERGE=1
81 echo "Trying simple merge with $SHA1"
82 git-read-tree -u -m $common $MRT $SHA1 || exit 2
83 next=$(git-write-tree 2>/dev/null)
84 if test $? -ne 0
85 then
86 echo "Simple merge did not work, trying automatic merge."
87 git-merge-index -o git-merge-one-file -a || {
88 echo "Not trivially merged."
89 echo "Should not be doing an Octopus."
90 exit 2
92 next=$(git-write-tree 2>/dev/null)
95 # We have merged the other branch successfully. Ideally
96 # we could implement OR'ed heads in merge-base, and keep
97 # a list of commits we have merged so far in MRC to feed
98 # them to merge-base, but we approximate it by keep using
99 # the current MRC. We used to update it to $common, which
100 # was incorrectly doing AND'ed merge-base here, which was
101 # unneeded.
103 MRT=$next
104 done
106 exit 0