1#!/bin/sh 2# 3# Copyright (c) 2005 Junio C Hamano 4# 5 6 7USAGE='[-n] [--no-commit] [-s <strategy>]... <merge-message> <head> <remote>+' 8. git-sh-setup 9 10LF=' 11' 12 13all_strategies='recursive octopus resolve stupid ours' 14default_strategies='recursive' 15use_strategies= 16iftest"@@NO_PYTHON@@";then 17 all_strategies='resolve octopus stupid ours' 18 default_strategies='resolve' 19fi 20 21dropsave() { 22rm-f --"$GIT_DIR/MERGE_HEAD""$GIT_DIR/MERGE_MSG" \ 23"$GIT_DIR/MERGE_SAVE"||exit1 24} 25 26savestate() { 27# Stash away any local modifications. 28 git-diff-index -z --name-only$head| 29cpio-0 -o>"$GIT_DIR/MERGE_SAVE" 30} 31 32restorestate() { 33iftest -f"$GIT_DIR/MERGE_SAVE" 34then 35 git reset--hard$head 36cpio-iuv<"$GIT_DIR/MERGE_SAVE" 37 git-update-index --refresh>/dev/null 38fi 39} 40 41finish () { 42test''="$2"||echo"$2" 43case"$merge_msg"in 44'') 45echo"No merge message -- not updating HEAD" 46;; 47*) 48 git-update-ref HEAD "$1""$head"||exit1 49;; 50esac 51 52case"$no_summary"in 53'') 54 git-diff-tree -p -M"$head""$1"| 55 git-apply --stat --summary 56;; 57esac 58} 59 60while case"$#"in0)break;;esac 61do 62case"$1"in 63-n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\ 64--no-summa|--no-summar|--no-summary) 65 no_summary=t ;; 66--no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit) 67 no_commit=t ;; 68-s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\ 69--strateg=*|--strategy=*|\ 70-s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy) 71case"$#,$1"in 72*,*=*) 73 strategy=`expr "$1" : '-[^=]*=\(.*\)'`;; 741,*) 75 usage ;; 76*) 77 strategy="$2" 78shift;; 79esac 80case"$all_strategies"in 81*"$strategy"*) 82 use_strategies="$use_strategies$strategy";; 83*) 84 die "available strategies are:$all_strategies";; 85esac 86;; 87-*) usage ;; 88*)break;; 89esac 90shift 91done 92 93test"$#"-le2&& usage ;# we need at least two heads. 94 95merge_msg="$1" 96shift 97head_arg="$1" 98head=$(git-rev-parse --verify "$1"^0)|| usage 99shift 100 101# All the rest are remote heads 102remoteheads= 103for remote 104do 105 remotehead=$(git-rev-parse --verify "$remote"^0)|| 106 die "$remote- not something we can merge" 107 remoteheads="${remoteheads}$remotehead" 108done 109set x $remoteheads;shift 110 111case"$#"in 1121) 113 common=$(git-merge-base --all $head "$@") 114;; 115*) 116 common=$(git-show-branch --merge-base $head "$@") 117;; 118esac 119echo"$head">"$GIT_DIR/ORIG_HEAD" 120 121case"$#,$common,$no_commit"in 122*,'',*) 123# No common ancestors found. We need a real merge. 124;; 1251,"$1",*) 126# If head can reach all the merge then we are up to date. 127# but first the most common case of merging one remote 128echo"Already up-to-date." 129 dropsave 130exit0 131;; 1321,"$head",*) 133# Again the most common case of merging one remote. 134echo"Updating from$headto$1." 135 git-update-index --refresh2>/dev/null 136 new_head=$(git-rev-parse --verify "$1^0")&& 137 git-read-tree -u -m$head"$new_head"&& 138 finish "$new_head""Fast forward" 139 dropsave 140exit0 141;; 1421,?*"$LF"?*,*) 143# We are not doing octopus and not fast forward. Need a 144# real merge. 145;; 1461,*,) 147# We are not doing octopus, not fast forward, and have only 148# one common. See if it is really trivial. 149 git var GIT_COMMITTER_IDENT >/dev/null ||exit 150 151echo"Trying really trivial in-index merge..." 152 git-update-index --refresh2>/dev/null 153if git-read-tree --trivial -m -u$common $head"$1"&& 154 result_tree=$(git-write-tree) 155then 156echo"Wonderful." 157 result_commit=$( 158echo"$merge_msg"| 159 git-commit-tree$result_tree-p HEAD -p"$1" 160) ||exit 161 finish "$result_commit""In-index merge" 162 dropsave 163exit0 164fi 165echo"Nope." 166;; 167*) 168# An octopus. If we can reach all the remote we are up to date. 169 up_to_date=t 170for remote 171do 172 common_one=$(git-merge-base --all $head $remote) 173iftest"$common_one"!="$remote" 174then 175 up_to_date=f 176break 177fi 178done 179iftest"$up_to_date"= t 180then 181echo"Already up-to-date. Yeeah!" 182 dropsave 183exit0 184fi 185;; 186esac 187 188# We are going to make a new commit. 189git var GIT_COMMITTER_IDENT >/dev/null ||exit 190 191case"$use_strategies"in 192'') 193case"$#"in 1941) 195 use_strategies="$default_strategies";; 196*) 197 use_strategies=octopus ;; 198esac 199;; 200esac 201 202# At this point, we need a real merge. No matter what strategy 203# we use, it would operate on the index, possibly affecting the 204# working tree, and when resolved cleanly, have the desired tree 205# in the index -- this means that the index must be in sync with 206# the $head commit. The strategies are responsible to ensure this. 207 208case"$use_strategies"in 209?*' '?*) 210# Stash away the local changes so that we can try more than one. 211 savestate 212 single_strategy=no 213;; 214*) 215rm-f"$GIT_DIR/MERGE_SAVE" 216 single_strategy=yes 217;; 218esac 219 220result_tree= best_cnt=-1 best_strategy= wt_strategy= 221merge_was_ok= 222for strategy in$use_strategies 223do 224test"$wt_strategy"=''|| { 225echo"Rewinding the tree to pristine..." 226 restorestate 227} 228case"$single_strategy"in 229 no) 230echo"Trying merge strategy$strategy..." 231;; 232esac 233 234# Remember which strategy left the state in the working tree 235 wt_strategy=$strategy 236 237 git-merge-$strategy $common--"$head_arg""$@" 238exit=$? 239iftest"$no_commit"= t &&test"$exit"=0 240then 241 merge_was_ok=t 242exit=1;# pretend it left conflicts. 243fi 244 245test"$exit"=0|| { 246 247# The backend exits with 1 when conflicts are left to be resolved, 248# with 2 when it does not handle the given merge at all. 249 250iftest"$exit"-eq1 251then 252 cnt=`{ 253 git-diff-files --name-only 254 git-ls-files --unmerged 255 } | wc -l` 256iftest$best_cnt-le0-o$cnt-le$best_cnt 257then 258 best_strategy=$strategy 259 best_cnt=$cnt 260fi 261fi 262continue 263} 264 265# Automerge succeeded. 266 result_tree=$(git-write-tree)&&break 267done 268 269# If we have a resulting tree, that means the strategy module 270# auto resolved the merge cleanly. 271iftest''!="$result_tree" 272then 273 parents="-p$head" 274for remote 275do 276 parents="$parents-p$remote" 277done 278 result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents)||exit 279 finish "$result_commit""Merge$result_commit, made by$wt_strategy." 280 dropsave 281exit0 282fi 283 284# Pick the result from the best strategy and have the user fix it up. 285case"$best_strategy"in 286'') 287 restorestate 288echo>&2"No merge strategy handled the merge." 289exit2 290;; 291"$wt_strategy") 292# We already have its result in the working tree. 293;; 294*) 295echo"Rewinding the tree to pristine..." 296 restorestate 297echo"Using the$best_strategyto prepare resolving by hand." 298 git-merge-$best_strategy $common--"$head_arg""$@" 299;; 300esac 301for remote 302do 303echo$remote 304done>"$GIT_DIR/MERGE_HEAD" 305echo"$merge_msg">"$GIT_DIR/MERGE_MSG" 306 307iftest"$merge_was_ok"= t 308then 309echo>&2 \ 310"Automatic merge went well; stopped before committing as requested" 311exit0 312else 313{ 314echo' 315Conflicts: 316' 317 git ls-files --unmerged| 318sed-e's/^[^ ]* / /'| 319uniq 320} >>"$GIT_DIR/MERGE_MSG" 321iftest -d"$GIT_DIR/rr-cache" 322then 323 git-rerere 324fi 325 die "Automatic merge failed; fix up by hand" 326fi