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 | |
Junio C Hamano | 17cf939 | 2005-11-09 22:37:14 -0800 | [diff] [blame] | 8 | LF=' |
| 9 | ' |
| 10 | |
Junio C Hamano | d165fa1 | 2005-11-27 23:33:54 -0800 | [diff] [blame] | 11 | die () { |
| 12 | echo >&2 "$*" |
| 13 | exit 1 |
| 14 | } |
| 15 | |
Junio C Hamano | 91063bb | 2005-09-08 13:47:12 -0700 | [diff] [blame] | 16 | # The first parameters up to -- are merge bases; the rest are heads. |
| 17 | bases= head= remotes= sep_seen= |
| 18 | for arg |
| 19 | do |
| 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 |
| 34 | done |
| 35 | |
| 36 | # Reject if this is not an Octopus -- resolve should be used instead. |
| 37 | case "$remotes" in |
| 38 | ?*' '?*) |
| 39 | ;; |
| 40 | *) |
| 41 | exit 2 ;; |
| 42 | esac |
| 43 | |
| 44 | # MRC is the current "merge reference commit" |
| 45 | # MRT is the current "merge result tree" |
| 46 | |
| 47 | MRC=$head MSG= PARENT="-p $head" |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 48 | MRT=$(git write-tree) |
Junio C Hamano | 91063bb | 2005-09-08 13:47:12 -0700 | [diff] [blame] | 49 | CNT=1 ;# counting our head |
| 50 | NON_FF_MERGE=0 |
Junio C Hamano | 98efc8f | 2006-01-13 16:45:42 -0800 | [diff] [blame] | 51 | OCTOPUS_FAILURE=0 |
Junio C Hamano | 91063bb | 2005-09-08 13:47:12 -0700 | [diff] [blame] | 52 | for SHA1 in $remotes |
| 53 | do |
Junio C Hamano | 98efc8f | 2006-01-13 16:45:42 -0800 | [diff] [blame] | 54 | case "$OCTOPUS_FAILURE" in |
| 55 | 1) |
| 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 |
| 63 | |
Junio C Hamano | c5dc9a2 | 2008-07-27 13:47:22 -0700 | [diff] [blame] | 64 | common=$(git merge-base --all $SHA1 $MRC) || |
Junio C Hamano | 91063bb | 2005-09-08 13:47:12 -0700 | [diff] [blame] | 65 | die "Unable to find common commit with $SHA1" |
| 66 | |
Junio C Hamano | c884dd9 | 2006-01-13 01:37:09 -0800 | [diff] [blame] | 67 | case "$LF$common$LF" in |
| 68 | *"$LF$SHA1$LF"*) |
Junio C Hamano | 91063bb | 2005-09-08 13:47:12 -0700 | [diff] [blame] | 69 | echo "Already up-to-date with $SHA1" |
| 70 | continue |
Junio C Hamano | 17cf939 | 2005-11-09 22:37:14 -0800 | [diff] [blame] | 71 | ;; |
| 72 | esac |
Junio C Hamano | 91063bb | 2005-09-08 13:47:12 -0700 | [diff] [blame] | 73 | |
| 74 | CNT=`expr $CNT + 1` |
| 75 | PARENT="$PARENT -p $SHA1" |
| 76 | |
| 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. |
| 83 | |
| 84 | echo "Fast forwarding to: $SHA1" |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 85 | git read-tree -u -m $head $SHA1 || exit |
| 86 | MRC=$SHA1 MRT=$(git write-tree) |
Junio C Hamano | 91063bb | 2005-09-08 13:47:12 -0700 | [diff] [blame] | 87 | continue |
| 88 | fi |
| 89 | |
| 90 | NON_FF_MERGE=1 |
| 91 | |
| 92 | echo "Trying simple merge with $SHA1" |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 93 | git read-tree -u -m --aggressive $common $MRT $SHA1 || exit 2 |
| 94 | next=$(git write-tree 2>/dev/null) |
Junio C Hamano | 91063bb | 2005-09-08 13:47:12 -0700 | [diff] [blame] | 95 | if test $? -ne 0 |
| 96 | then |
| 97 | echo "Simple merge did not work, trying automatic merge." |
Junio C Hamano | 98efc8f | 2006-01-13 16:45:42 -0800 | [diff] [blame] | 98 | git-merge-index -o git-merge-one-file -a || |
| 99 | OCTOPUS_FAILURE=1 |
Junio C Hamano | 5be6007 | 2007-07-02 22:52:14 -0700 | [diff] [blame] | 100 | next=$(git write-tree 2>/dev/null) |
Junio C Hamano | 91063bb | 2005-09-08 13:47:12 -0700 | [diff] [blame] | 101 | fi |
Junio C Hamano | 17cf939 | 2005-11-09 22:37:14 -0800 | [diff] [blame] | 102 | |
Junio C Hamano | c5dc9a2 | 2008-07-27 13:47:22 -0700 | [diff] [blame] | 103 | MRC="$MRC $SHA1" |
Junio C Hamano | 91063bb | 2005-09-08 13:47:12 -0700 | [diff] [blame] | 104 | MRT=$next |
| 105 | done |
| 106 | |
Junio C Hamano | 98efc8f | 2006-01-13 16:45:42 -0800 | [diff] [blame] | 107 | exit "$OCTOPUS_FAILURE" |