blob: 7d19d379512b52168d7c604002e872fc3eef9fd7 [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
Vasco Almeidaff3b0182016-06-17 20:20:57 +00008. git-sh-setup
Vasco Almeida6a4eb912016-06-17 20:20:56 +00009
Junio C Hamano17cf9392005-11-09 22:37:14 -080010LF='
11'
12
Junio C Hamano91063bb2005-09-08 13:47:12 -070013# The first parameters up to -- are merge bases; the rest are heads.
14bases= head= remotes= sep_seen=
15for arg
16do
17 case ",$sep_seen,$head,$arg," in
18 *,--,)
19 sep_seen=yes
20 ;;
21 ,yes,,*)
22 head=$arg
23 ;;
24 ,yes,*)
25 remotes="$remotes$arg "
26 ;;
27 *)
28 bases="$bases$arg "
29 ;;
30 esac
31done
32
Alex Henrie1c67d532016-09-07 22:34:40 -060033# Reject if this is not an octopus -- resolve should be used instead.
Junio C Hamano91063bb2005-09-08 13:47:12 -070034case "$remotes" in
35?*' '?*)
36 ;;
37*)
38 exit 2 ;;
39esac
40
41# MRC is the current "merge reference commit"
42# MRT is the current "merge result tree"
43
Elijah Newren3ec62ad2016-04-09 23:13:38 -070044if ! git diff-index --quiet --cached HEAD --
45then
Vasco Almeida6a4eb912016-06-17 20:20:56 +000046 gettextln "Error: Your local changes to the following files would be overwritten by merge"
Elijah Newren3ec62ad2016-04-09 23:13:38 -070047 git diff-index --cached --name-only HEAD -- | sed -e 's/^/ /'
48 exit 2
49fi
Stephen Boydf08aa012009-12-11 16:38:59 -080050MRC=$(git rev-parse --verify -q $head)
Junio C Hamano5be60072007-07-02 22:52:14 -070051MRT=$(git write-tree)
Junio C Hamano91063bb2005-09-08 13:47:12 -070052NON_FF_MERGE=0
Junio C Hamano98efc8f2006-01-13 16:45:42 -080053OCTOPUS_FAILURE=0
Junio C Hamano91063bb2005-09-08 13:47:12 -070054for SHA1 in $remotes
55do
Junio C Hamano98efc8f2006-01-13 16:45:42 -080056 case "$OCTOPUS_FAILURE" in
57 1)
58 # We allow only last one to have a hand-resolvable
59 # conflicts. Last round failed and we still had
60 # a head to merge.
Vasco Almeida6a4eb912016-06-17 20:20:56 +000061 gettextln "Automated merge did not work."
Alex Henrie1c67d532016-09-07 22:34:40 -060062 gettextln "Should not be doing an octopus."
Junio C Hamano98efc8f2006-01-13 16:45:42 -080063 exit 2
64 esac
65
Stephen Boyd81334502009-12-11 16:38:57 -080066 eval pretty_name=\${GITHEAD_$SHA1:-$SHA1}
Johannes Schindelin4e57baf2010-06-12 00:45:35 +020067 if test "$SHA1" = "$pretty_name"
68 then
69 SHA1_UP="$(echo "$SHA1" | tr a-z A-Z)"
70 eval pretty_name=\${GITHEAD_$SHA1_UP:-$pretty_name}
71 fi
Junio C Hamanoc5dc9a22008-07-27 13:47:22 -070072 common=$(git merge-base --all $SHA1 $MRC) ||
Vasco Almeida6a4eb912016-06-17 20:20:56 +000073 die "$(eval_gettext "Unable to find common commit with \$pretty_name")"
Junio C Hamano91063bb2005-09-08 13:47:12 -070074
Junio C Hamanoc884dd92006-01-13 01:37:09 -080075 case "$LF$common$LF" in
76 *"$LF$SHA1$LF"*)
Martin Ă…gren7560f542017-08-23 19:49:35 +020077 eval_gettextln "Already up to date with \$pretty_name"
Junio C Hamano91063bb2005-09-08 13:47:12 -070078 continue
Junio C Hamano17cf9392005-11-09 22:37:14 -080079 ;;
80 esac
Junio C Hamano91063bb2005-09-08 13:47:12 -070081
Junio C Hamano91063bb2005-09-08 13:47:12 -070082 if test "$common,$NON_FF_MERGE" = "$MRC,0"
83 then
84 # The first head being merged was a fast-forward.
85 # Advance MRC to the head being merged, and use that
86 # tree as the intermediate result of the merge.
87 # We still need to count this as part of the parent set.
88
Vasco Almeida6a4eb912016-06-17 20:20:56 +000089 eval_gettextln "Fast-forwarding to: \$pretty_name"
Junio C Hamano5be60072007-07-02 22:52:14 -070090 git read-tree -u -m $head $SHA1 || exit
91 MRC=$SHA1 MRT=$(git write-tree)
Junio C Hamano91063bb2005-09-08 13:47:12 -070092 continue
93 fi
94
95 NON_FF_MERGE=1
96
Vasco Almeida6a4eb912016-06-17 20:20:56 +000097 eval_gettextln "Trying simple merge with \$pretty_name"
Junio C Hamano5be60072007-07-02 22:52:14 -070098 git read-tree -u -m --aggressive $common $MRT $SHA1 || exit 2
99 next=$(git write-tree 2>/dev/null)
Junio C Hamano91063bb2005-09-08 13:47:12 -0700100 if test $? -ne 0
101 then
Vasco Almeida6a4eb912016-06-17 20:20:56 +0000102 gettextln "Simple merge did not work, trying automatic merge."
Michael Forney974ce802017-08-04 23:49:05 -0700103 git merge-index -o git-merge-one-file -a ||
Junio C Hamano98efc8f2006-01-13 16:45:42 -0800104 OCTOPUS_FAILURE=1
Junio C Hamano5be60072007-07-02 22:52:14 -0700105 next=$(git write-tree 2>/dev/null)
Junio C Hamano91063bb2005-09-08 13:47:12 -0700106 fi
Junio C Hamano17cf9392005-11-09 22:37:14 -0800107
Junio C Hamanoc5dc9a22008-07-27 13:47:22 -0700108 MRC="$MRC $SHA1"
Junio C Hamano91063bb2005-09-08 13:47:12 -0700109 MRT=$next
110done
111
Junio C Hamano98efc8f2006-01-13 16:45:42 -0800112exit "$OCTOPUS_FAILURE"