msysGit-based Git for Windows 1.x was superseded by Git for Windows 2.x
[git/mingw/4msysgit.git] / git-merge-octopus.sh
blob2226eeba99a241ec87ffb076b3c54118b3e6766e
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=$(git rev-parse --verify -q $head)
48 MRT=$(git write-tree)
49 NON_FF_MERGE=0
50 OCTOPUS_FAILURE=0
51 for SHA1 in $remotes
53 case "$OCTOPUS_FAILURE" in
55 # We allow only last one to have a hand-resolvable
56 # conflicts. Last round failed and we still had
57 # a head to merge.
58 echo "Automated merge did not work."
59 echo "Should not be doing an Octopus."
60 exit 2
61 esac
63 eval pretty_name=\${GITHEAD_$SHA1:-$SHA1}
64 if test "$SHA1" = "$pretty_name"
65 then
66 SHA1_UP="$(echo "$SHA1" | tr a-z A-Z)"
67 eval pretty_name=\${GITHEAD_$SHA1_UP:-$pretty_name}
69 common=$(git merge-base --all $SHA1 $MRC) ||
70 die "Unable to find common commit with $pretty_name"
72 case "$LF$common$LF" in
73 *"$LF$SHA1$LF"*)
74 cat << EOF
75 Already up-to-date with $pretty_name
76 EOF
77 continue
79 esac
81 if test "$common,$NON_FF_MERGE" = "$MRC,0"
82 then
83 # The first head being merged was a fast-forward.
84 # Advance MRC to the head being merged, and use that
85 # tree as the intermediate result of the merge.
86 # We still need to count this as part of the parent set.
88 cat << EOF
89 Fast-forwarding to: $pretty_name
90 EOF
91 git read-tree -u -m $head $SHA1 || exit
92 MRC=$SHA1 MRT=$(git write-tree)
93 continue
96 NON_FF_MERGE=1
98 cat << EOF
99 Trying simple merge with $pretty_name
101 git read-tree -u -m --aggressive $common $MRT $SHA1 || exit 2
102 next=$(git write-tree 2>/dev/null)
103 if test $? -ne 0
104 then
105 echo "Simple merge did not work, trying automatic merge."
106 git-merge-index -o git-merge-one-file -a ||
107 OCTOPUS_FAILURE=1
108 next=$(git write-tree 2>/dev/null)
111 MRC="$MRC $SHA1"
112 MRT=$next
113 done
115 exit "$OCTOPUS_FAILURE"