1#!/bin/sh 2# 3# Copyright (c) 2005 Junio C Hamano 4# 5# Resolve two or more trees. 6# 7 8. git-sh-i18n 9 10LF=' 11' 12 13die () { 14echo>&2"$*" 15exit1 16} 17 18# The first parameters up to -- are merge bases; the rest are heads. 19bases=head= remotes= sep_seen= 20for arg 21do 22case",$sep_seen,$head,$arg,"in 23*,--,) 24 sep_seen=yes 25;; 26,yes,,*) 27head=$arg 28;; 29,yes,*) 30 remotes="$remotes$arg" 31;; 32*) 33 bases="$bases$arg" 34;; 35esac 36done 37 38# Reject if this is not an Octopus -- resolve should be used instead. 39case"$remotes"in 40?*' '?*) 41;; 42*) 43exit2;; 44esac 45 46# MRC is the current "merge reference commit" 47# MRT is the current "merge result tree" 48 49if! git diff-index --quiet --cached HEAD -- 50then 51 gettextln "Error: Your local changes to the following files would be overwritten by merge" 52 git diff-index --cached --name-only HEAD --|sed-e's/^/ /' 53exit2 54fi 55MRC=$(git rev-parse --verify -q $head) 56MRT=$(git write-tree) 57NON_FF_MERGE=0 58OCTOPUS_FAILURE=0 59for SHA1 in$remotes 60do 61case"$OCTOPUS_FAILURE"in 621) 63# We allow only last one to have a hand-resolvable 64# conflicts. Last round failed and we still had 65# a head to merge. 66 gettextln "Automated merge did not work." 67 gettextln "Should not be doing an Octopus." 68exit2 69esac 70 71eval pretty_name=\${GITHEAD_$SHA1:-$SHA1} 72iftest"$SHA1"="$pretty_name" 73then 74 SHA1_UP="$(echo "$SHA1" | tr a-z A-Z)" 75eval pretty_name=\${GITHEAD_$SHA1_UP:-$pretty_name} 76fi 77 common=$(git merge-base --all $SHA1 $MRC)|| 78 die "$(eval_gettext "Unable to find common commit with \$pretty_name")" 79 80case"$LF$common$LF"in 81*"$LF$SHA1$LF"*) 82 eval_gettextln "Already up-to-date with \$pretty_name" 83continue 84;; 85esac 86 87iftest"$common,$NON_FF_MERGE"="$MRC,0" 88then 89# The first head being merged was a fast-forward. 90# Advance MRC to the head being merged, and use that 91# tree as the intermediate result of the merge. 92# We still need to count this as part of the parent set. 93 94 eval_gettextln "Fast-forwarding to: \$pretty_name" 95 git read-tree -u -m$head $SHA1||exit 96 MRC=$SHA1 MRT=$(git write-tree) 97continue 98fi 99 100 NON_FF_MERGE=1 101 102 eval_gettextln "Trying simple merge with \$pretty_name" 103 git read-tree -u -m --aggressive$common $MRT $SHA1||exit2 104 next=$(git write-tree 2>/dev/null) 105iftest $? -ne0 106then 107 gettextln "Simple merge did not work, trying automatic merge." 108 git-merge-index -o git-merge-one-file -a|| 109 OCTOPUS_FAILURE=1 110 next=$(git write-tree 2>/dev/null) 111fi 112 113 MRC="$MRC$SHA1" 114 MRT=$next 115done 116 117exit"$OCTOPUS_FAILURE"