blob: 8643f74cb09f278c37851c418e839c7d160f36ca [file] [log] [blame]
Junio C Hamano91063bb2005-09-08 13:47:12 -07001#!/bin/sh
2#
3# Copyright (c) 2005 Junio C Hamano
4#
5# Resolve two or more trees.
6#
7
Junio C Hamano17cf9392005-11-09 22:37:14 -08008LF='
9'
10
Junio C Hamanod165fa12005-11-27 23:33:54 -080011die () {
12 echo >&2 "$*"
13 exit 1
14}
15
Junio C Hamano91063bb2005-09-08 13:47:12 -070016# The first parameters up to -- are merge bases; the rest are heads.
17bases= head= remotes= sep_seen=
18for arg
19do
20 case ",$sep_seen,$head,$arg," in
21 *,--,)
22 sep_seen=yes
23 ;;
24 ,yes,,*)
25 head=$arg
26 ;;
27 ,yes,*)
28 remotes="$remotes$arg "
29 ;;
30 *)
31 bases="$bases$arg "
32 ;;
33 esac
34done
35
36# Reject if this is not an Octopus -- resolve should be used instead.
37case "$remotes" in
38?*' '?*)
39 ;;
40*)
41 exit 2 ;;
42esac
43
44# MRC is the current "merge reference commit"
45# MRT is the current "merge result tree"
46
Stephen Boydf08aa012009-12-11 16:38:59 -080047MRC=$(git rev-parse --verify -q $head)
Junio C Hamano5be60072007-07-02 22:52:14 -070048MRT=$(git write-tree)
Junio C Hamano91063bb2005-09-08 13:47:12 -070049NON_FF_MERGE=0
Junio C Hamano98efc8f2006-01-13 16:45:42 -080050OCTOPUS_FAILURE=0
Junio C Hamano91063bb2005-09-08 13:47:12 -070051for SHA1 in $remotes
52do
Junio C Hamano98efc8f2006-01-13 16:45:42 -080053 case "$OCTOPUS_FAILURE" in
54 1)
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
62
Stephen Boyd81334502009-12-11 16:38:57 -080063 eval pretty_name=\${GITHEAD_$SHA1:-$SHA1}
Johannes Schindelin4e57baf2010-06-12 00:45:35 +020064 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}
68 fi
Junio C Hamanoc5dc9a22008-07-27 13:47:22 -070069 common=$(git merge-base --all $SHA1 $MRC) ||
Stephen Boyd81334502009-12-11 16:38:57 -080070 die "Unable to find common commit with $pretty_name"
Junio C Hamano91063bb2005-09-08 13:47:12 -070071
Junio C Hamanoc884dd92006-01-13 01:37:09 -080072 case "$LF$common$LF" in
73 *"$LF$SHA1$LF"*)
Stephen Boyd81334502009-12-11 16:38:57 -080074 echo "Already up-to-date with $pretty_name"
Junio C Hamano91063bb2005-09-08 13:47:12 -070075 continue
Junio C Hamano17cf9392005-11-09 22:37:14 -080076 ;;
77 esac
Junio C Hamano91063bb2005-09-08 13:47:12 -070078
Junio C Hamano91063bb2005-09-08 13:47:12 -070079 if test "$common,$NON_FF_MERGE" = "$MRC,0"
80 then
81 # The first head being merged was a fast-forward.
82 # Advance MRC to the head being merged, and use that
83 # tree as the intermediate result of the merge.
84 # We still need to count this as part of the parent set.
85
Stephen Boyd81334502009-12-11 16:38:57 -080086 echo "Fast-forwarding to: $pretty_name"
Junio C Hamano5be60072007-07-02 22:52:14 -070087 git read-tree -u -m $head $SHA1 || exit
88 MRC=$SHA1 MRT=$(git write-tree)
Junio C Hamano91063bb2005-09-08 13:47:12 -070089 continue
90 fi
91
92 NON_FF_MERGE=1
93
Stephen Boyd81334502009-12-11 16:38:57 -080094 echo "Trying simple merge with $pretty_name"
Junio C Hamano5be60072007-07-02 22:52:14 -070095 git read-tree -u -m --aggressive $common $MRT $SHA1 || exit 2
96 next=$(git write-tree 2>/dev/null)
Junio C Hamano91063bb2005-09-08 13:47:12 -070097 if test $? -ne 0
98 then
99 echo "Simple merge did not work, trying automatic merge."
Junio C Hamano98efc8f2006-01-13 16:45:42 -0800100 git-merge-index -o git-merge-one-file -a ||
101 OCTOPUS_FAILURE=1
Junio C Hamano5be60072007-07-02 22:52:14 -0700102 next=$(git write-tree 2>/dev/null)
Junio C Hamano91063bb2005-09-08 13:47:12 -0700103 fi
Junio C Hamano17cf9392005-11-09 22:37:14 -0800104
Junio C Hamanoc5dc9a22008-07-27 13:47:22 -0700105 MRC="$MRC $SHA1"
Junio C Hamano91063bb2005-09-08 13:47:12 -0700106 MRT=$next
107done
108
Junio C Hamano98efc8f2006-01-13 16:45:42 -0800109exit "$OCTOPUS_FAILURE"