rebase -i: work on a detached HEAD
[git/spearce.git] / git-merge-octopus.sh
blob645e1147dc886f2b1ca6d2020b44db746b082bf0
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 OCTOPUS_FAILURE=0
52 for SHA1 in $remotes
54 case "$OCTOPUS_FAILURE" in
56 # We allow only last one to have a hand-resolvable
57 # conflicts. Last round failed and we still had
58 # a head to merge.
59 echo "Automated merge did not work."
60 echo "Should not be doing an Octopus."
61 exit 2
62 esac
64 common=$(git merge-base --all $MRC $SHA1) ||
65 die "Unable to find common commit with $SHA1"
67 case "$LF$common$LF" in
68 *"$LF$SHA1$LF"*)
69 echo "Already up-to-date with $SHA1"
70 continue
72 esac
74 CNT=`expr $CNT + 1`
75 PARENT="$PARENT -p $SHA1"
77 if test "$common,$NON_FF_MERGE" = "$MRC,0"
78 then
79 # The first head being merged was a fast-forward.
80 # Advance MRC to the head being merged, and use that
81 # tree as the intermediate result of the merge.
82 # We still need to count this as part of the parent set.
84 echo "Fast forwarding to: $SHA1"
85 git read-tree -u -m $head $SHA1 || exit
86 MRC=$SHA1 MRT=$(git write-tree)
87 continue
90 NON_FF_MERGE=1
92 echo "Trying simple merge with $SHA1"
93 git read-tree -u -m --aggressive $common $MRT $SHA1 || exit 2
94 next=$(git write-tree 2>/dev/null)
95 if test $? -ne 0
96 then
97 echo "Simple merge did not work, trying automatic merge."
98 git-merge-index -o git-merge-one-file -a ||
99 OCTOPUS_FAILURE=1
100 next=$(git write-tree 2>/dev/null)
103 # We have merged the other branch successfully. Ideally
104 # we could implement OR'ed heads in merge-base, and keep
105 # a list of commits we have merged so far in MRC to feed
106 # them to merge-base, but we approximate it by keep using
107 # the current MRC. We used to update it to $common, which
108 # was incorrectly doing AND'ed merge-base here, which was
109 # unneeded.
111 MRT=$next
112 done
114 exit "$OCTOPUS_FAILURE"