1#!/bin/sh 2# 3# Copyright (c) 2005 Junio C Hamano 4# 5 6. git-sh-setup|| die "Not a git archive" 7 8LF=' 9' 10 11usage () { 12 die "git-merge [-n] [-s <strategy>]... <merge-message> <head> <remote>+" 13} 14 15# all_strategies='resolve recursive stupid octopus' 16 17all_strategies='recursive octopus resolve stupid' 18default_strategies='resolve octopus' 19use_strategies= 20 21dropsave() { 22rm-f --"$GIT_DIR/MERGE_HEAD" \ 23"$GIT_DIR/MERGE_SAVE"||exit1 24} 25 26savestate() { 27 git diff-r -z --name-only$head|cpio-0 -o>"$GIR_DIR/MERGE_SAVE" 28} 29 30restorestate() { 31 git reset--hard$head 32cpio-iuv<"$GIT_DIR/MERGE_SAVE" 33 git-update-index --refresh>/dev/null 34} 35 36summary() { 37case"$no_summary"in 38'') 39 git-diff-tree -p -M$head"$1"| 40 git-apply --stat --summary 41;; 42esac 43} 44 45while case"$#"in0)break;;esac 46do 47case"$1"in 48-n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\ 49--no-summa|--no-summar|--no-summary) 50 no_summary=t ;; 51-s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\ 52--strateg=*|--strategy=*|\ 53-s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy) 54case"$#,$1"in 55*,*=*) 56 strategy=`expr "$1" : '-[^=]*=\(.*\)'`;; 571,*) 58 usage ;; 59*) 60 strategy="$2" 61shift;; 62esac 63case"$all_strategies"in 64*"$strategy"*) 65 use_strategies="$use_strategies$strategy";; 66*) 67 die "available strategies are:$all_strategies";; 68esac 69;; 70-*) usage ;; 71*)break;; 72esac 73shift 74done 75 76case"$use_strategies"in 77'') 78 use_strategies=$default_strategies 79;; 80esac 81test"$#"-le2&& usage ;# we need at least two heads. 82 83merge_msg="$1" 84shift 85head_arg="$1" 86head=$(git-rev-parse --verify "$1"^0)|| usage 87shift 88 89# All the rest are remote heads 90for remote 91do 92 git-rev-parse --verify"$remote"^0>/dev/null || 93 die "$remote- not something we can merge" 94done 95 96common=$(git-show-branch --merge-base $head "$@") 97echo"$head">"$GIT_DIR/ORIG_HEAD" 98 99case"$#,$common"in 100*,'') 101 die "Unable to find common commit between$head_argand $*" 102;; 1031,"$1") 104# If head can reach all the merge then we are up to date. 105# but first the most common case of merging one remote 106echo"Already up-to-date. Yeeah!" 107 dropsave 108exit0 109;; 1101,"$head") 111# Again the most common case of merging one remote. 112echo"Updating from$headto$1." 113 git-update-index --refresh2>/dev/null 114 git-read-tree -u -m$head"$1"||exit1 115 git-rev-parse --verify"$1^0">"$GIT_DIR/HEAD" 116 summary "$1" 117 dropsave 118exit0 119;; 1201,*) 121# We are not doing octopus and not fast forward. Need a 122# real merge. 123;; 124*) 125# An octopus. If we can reach all the remote we are up to date. 126 up_to_date=t 127for remote 128do 129 common_one=$(git-merge-base $head $remote) 130iftest"$common_one"!="$remote" 131then 132 up_to_date=f 133break 134fi 135done 136iftest"$up_to_date"= t 137then 138echo"Already up-to-date. Yeeah!" 139 dropsave 140exit0 141fi 142;; 143esac 144 145# At this point, we need a real merge. No matter what strategy 146# we use, it would operate on the index, possibly affecting the 147# working tree, and when resolved cleanly, have the desired tree 148# in the index -- this means that the index must be in sync with 149# the $head commit. 150files=$(git-diff-index --cached --name-only $head)||exit 151if["$files"];then 152echo>&2"Dirty index: cannot merge (dirty:$files)" 153exit1 154fi 155 156case"$use_strategies"in 157?*' '?*) 158# Stash away the local changes so that we can try more than one. 159 savestate 160 single_strategy=no 161;; 162*) 163 single_strategy=yes 164;; 165esac 166 167result_tree= best_cnt=-1 best_strategy= wt_strategy= 168for strategy in$use_strategies 169do 170test"$wt_strategy"=''|| { 171echo"Rewinding the tree to pristine..." 172 restorestate 173} 174case"$single_strategy"in 175 no) 176echo"Trying merge strategy$strategy..." 177;; 178esac 179 180# Remember which strategy left the state in the working tree 181 wt_strategy=$strategy 182 183 git-merge-$strategy $common--"$head_arg""$@"|| { 184 185# The backend exits with 1 when conflicts are left to be resolved, 186# with 2 when it does not handle the given merge at all. 187 188exit=$? 189iftest"$exit"-eq1 190then 191 cnt=`{ 192 git-diff-files --name-only 193 git-ls-files --unmerged 194 } | wc -l` 195iftest$best_cnt-le0-o$cnt-le$best_cnt 196then 197 best_strategy=$strategy 198 best_cnt=$cnt 199fi 200fi 201continue 202} 203 204# Automerge succeeded. 205 result_tree=$(git-write-tree)&&break 206done 207 208# If we have a resulting tree, that means the strategy module 209# auto resolved the merge cleanly. 210iftest''!="$result_tree" 211then 212 parents="-p$head" 213for remote 214do 215 parents="$parents-p$remote" 216done 217 result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents) 218echo"Committed merge$result_commit, made by$wt_strategy." 219echo$result_commit>"$GIT_DIR/HEAD" 220 summary $result_commit 221 dropsave 222exit0 223fi 224 225# Pick the result from the best strategy and have the user fix it up. 226case"$best_strategy"in 227'') 228 restorestate 229 die "No merge strategy handled the merge." 230;; 231"$wt_strategy") 232# We already have its result in the working tree. 233;; 234*) 235echo"Rewinding the tree to pristine..." 236 restorestate 237echo"Using the$best_strategyto prepare resolving by hand." 238 git-merge-$best_strategy $common--"$head_arg""$@" 239;; 240esac 241for remote 242do 243echo$remote 244done>"$GIT_DIR/MERGE_HEAD" 245die "Automatic merge failed; fix up by hand"