Junio C Hamano | 91063bb | 2005-09-08 13:47:12 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (c) 2005 Junio C Hamano |
| 4 | # |
| 5 | # Resolve two or more trees. |
| 6 | # |
| 7 | |
Vasco Almeida | ff3b018 | 2016-06-17 20:20:57 +0000 | [diff] [blame] | 8 | . git-sh-setup |
Vasco Almeida | 6a4eb91 | 2016-06-17 20:20:56 +0000 | [diff] [blame] | 9 | |
Junio C Hamano | 17cf939 | 2005-11-09 22:37:14 -0800 | [diff] [blame] | 10 | LF=' |
| 11 | ' |
| 12 | |
Junio C Hamano | 91063bb | 2005-09-08 13:47:12 -0700 | [diff] [blame] | 13 | # The first parameters up to -- are merge bases; the rest are heads. |
| 14 | bases= head= remotes= sep_seen= |
| 15 | for arg |
| 16 | do |
| 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 |
| 31 | done |
| 32 | |
Alex Henrie | 1c67d53 | 2016-09-07 22:34:40 -0600 | [diff] [blame] | 33 | # Reject if this is not an octopus -- resolve should be used instead. |
Junio C Hamano | 91063bb | 2005-09-08 13:47:12 -0700 | [diff] [blame] | 34 | case "$remotes" in |
| 35 | ?*' '?*) |
| 36 | ;; |
| 37 | *) |
| 38 | exit 2 ;; |
| 39 | esac |
| 40 | |
| 41 | # MRC is the current "merge reference commit" |
| 42 | # MRT is the current "merge result tree" |
| 43 | |
Elijah Newren | 3ec62ad | 2016-04-09 23:13:38 -0700 | [diff] [blame] | 44 | if ! git diff-index --quiet --cached HEAD -- |
| 45 | then |
Vasco Almeida | 6a4eb91 | 2016-06-17 20:20:56 +0000 | [diff] [blame] | 46 | gettextln "Error: Your local changes to the following files would be overwritten by merge" |
Elijah Newren | 3ec62ad | 2016-04-09 23:13:38 -0700 | [diff] [blame] | 47 | git diff-index --cached --name-only HEAD -- | sed -e 's/^/ /' |
| 48 | exit 2 |
| 49 | fi |
Stephen Boyd | f08aa01 | 2009-12-11 16:38:59 -0800 | [diff] [blame] | 50 | MRC=$(git rev-parse --verify -q $head) |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 51 | MRT=$(git write-tree) |
Junio C Hamano | 91063bb | 2005-09-08 13:47:12 -0700 | [diff] [blame] | 52 | NON_FF_MERGE=0 |
Junio C Hamano | 98efc8f | 2006-01-13 16:45:42 -0800 | [diff] [blame] | 53 | OCTOPUS_FAILURE=0 |
Junio C Hamano | 91063bb | 2005-09-08 13:47:12 -0700 | [diff] [blame] | 54 | for SHA1 in $remotes |
| 55 | do |
Junio C Hamano | 98efc8f | 2006-01-13 16:45:42 -0800 | [diff] [blame] | 56 | 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 Almeida | 6a4eb91 | 2016-06-17 20:20:56 +0000 | [diff] [blame] | 61 | gettextln "Automated merge did not work." |
Alex Henrie | 1c67d53 | 2016-09-07 22:34:40 -0600 | [diff] [blame] | 62 | gettextln "Should not be doing an octopus." |
Junio C Hamano | 98efc8f | 2006-01-13 16:45:42 -0800 | [diff] [blame] | 63 | exit 2 |
| 64 | esac |
| 65 | |
Stephen Boyd | 8133450 | 2009-12-11 16:38:57 -0800 | [diff] [blame] | 66 | eval pretty_name=\${GITHEAD_$SHA1:-$SHA1} |
Johannes Schindelin | 4e57baf | 2010-06-12 00:45:35 +0200 | [diff] [blame] | 67 | 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 Hamano | c5dc9a2 | 2008-07-27 13:47:22 -0700 | [diff] [blame] | 72 | common=$(git merge-base --all $SHA1 $MRC) || |
Vasco Almeida | 6a4eb91 | 2016-06-17 20:20:56 +0000 | [diff] [blame] | 73 | die "$(eval_gettext "Unable to find common commit with \$pretty_name")" |
Junio C Hamano | 91063bb | 2005-09-08 13:47:12 -0700 | [diff] [blame] | 74 | |
Junio C Hamano | c884dd9 | 2006-01-13 01:37:09 -0800 | [diff] [blame] | 75 | case "$LF$common$LF" in |
| 76 | *"$LF$SHA1$LF"*) |
Martin Ă…gren | 7560f54 | 2017-08-23 19:49:35 +0200 | [diff] [blame] | 77 | eval_gettextln "Already up to date with \$pretty_name" |
Junio C Hamano | 91063bb | 2005-09-08 13:47:12 -0700 | [diff] [blame] | 78 | continue |
Junio C Hamano | 17cf939 | 2005-11-09 22:37:14 -0800 | [diff] [blame] | 79 | ;; |
| 80 | esac |
Junio C Hamano | 91063bb | 2005-09-08 13:47:12 -0700 | [diff] [blame] | 81 | |
Junio C Hamano | 91063bb | 2005-09-08 13:47:12 -0700 | [diff] [blame] | 82 | 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 Almeida | 6a4eb91 | 2016-06-17 20:20:56 +0000 | [diff] [blame] | 89 | eval_gettextln "Fast-forwarding to: \$pretty_name" |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 90 | git read-tree -u -m $head $SHA1 || exit |
| 91 | MRC=$SHA1 MRT=$(git write-tree) |
Junio C Hamano | 91063bb | 2005-09-08 13:47:12 -0700 | [diff] [blame] | 92 | continue |
| 93 | fi |
| 94 | |
| 95 | NON_FF_MERGE=1 |
| 96 | |
Vasco Almeida | 6a4eb91 | 2016-06-17 20:20:56 +0000 | [diff] [blame] | 97 | eval_gettextln "Trying simple merge with \$pretty_name" |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 98 | git read-tree -u -m --aggressive $common $MRT $SHA1 || exit 2 |
| 99 | next=$(git write-tree 2>/dev/null) |
Junio C Hamano | 91063bb | 2005-09-08 13:47:12 -0700 | [diff] [blame] | 100 | if test $? -ne 0 |
| 101 | then |
Vasco Almeida | 6a4eb91 | 2016-06-17 20:20:56 +0000 | [diff] [blame] | 102 | gettextln "Simple merge did not work, trying automatic merge." |
Michael Forney | 974ce80 | 2017-08-04 23:49:05 -0700 | [diff] [blame] | 103 | git merge-index -o git-merge-one-file -a || |
Junio C Hamano | 98efc8f | 2006-01-13 16:45:42 -0800 | [diff] [blame] | 104 | OCTOPUS_FAILURE=1 |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 105 | next=$(git write-tree 2>/dev/null) |
Junio C Hamano | 91063bb | 2005-09-08 13:47:12 -0700 | [diff] [blame] | 106 | fi |
Junio C Hamano | 17cf939 | 2005-11-09 22:37:14 -0800 | [diff] [blame] | 107 | |
Junio C Hamano | c5dc9a2 | 2008-07-27 13:47:22 -0700 | [diff] [blame] | 108 | MRC="$MRC $SHA1" |
Junio C Hamano | 91063bb | 2005-09-08 13:47:12 -0700 | [diff] [blame] | 109 | MRT=$next |
| 110 | done |
| 111 | |
Junio C Hamano | 98efc8f | 2006-01-13 16:45:42 -0800 | [diff] [blame] | 112 | exit "$OCTOPUS_FAILURE" |