1#!/bin/sh 2# 3# Copyright (c) 2005 Junio C Hamano 4# 5 6USAGE='[-n] [--no-commit] [--squash] [-s <strategy>] [--reflog-action=<action>] [-m=<merge-message>] <commit>+' 7 8. git-sh-setup 9 10LF=' 11' 12 13all_strategies='recur recursive octopus resolve stupid ours' 14default_twohead_strategies='recursive' 15default_octopus_strategies='octopus' 16no_trivial_merge_strategies='ours' 17use_strategies= 18 19index_merge=t 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>/dev/null 36cpio-iuv<"$GIT_DIR/MERGE_SAVE" 37 git-update-index --refresh>/dev/null 38fi 39} 40 41finish_up_to_date () { 42case"$squash"in 43 t) 44echo"$1(nothing to squash)";; 45'') 46echo"$1";; 47esac 48 dropsave 49} 50 51squash_message () { 52echo Squashed commit of the following: 53echo 54 git-log --no-merges ^"$head"$remote 55} 56 57finish () { 58iftest''="$2" 59then 60 rlogm="$rloga" 61else 62echo"$2" 63 rlogm="$rloga:$2" 64fi 65case"$squash"in 66 t) 67echo"Squash commit -- not updating HEAD" 68 squash_message >"$GIT_DIR/SQUASH_MSG" 69;; 70'') 71case"$merge_msg"in 72'') 73echo"No merge message -- not updating HEAD" 74;; 75*) 76 git-update-ref -m"$rlogm" HEAD "$1""$head"||exit1 77;; 78esac 79;; 80esac 81case"$1"in 82'') 83;; 84 ?*) 85case"$no_summary"in 86'') 87 git-diff-tree --stat --summary -M"$head""$1" 88;; 89esac 90;; 91esac 92} 93 94merge_name () { 95 remote="$1" 96 rh=$(git-rev-parse --verify"$remote^0"2>/dev/null) ||return 97 bh=$(git-show-ref -s --verify"refs/heads/$remote"2>/dev/null) 98iftest"$rh"="$bh" 99then 100echo"$rhbranch '$remote' of ." 101elif truname=$(expr"$remote":'\(.*\)~[1-9][0-9]*$') && 102 git-show-ref -q --verify"refs/heads/$truname"2>/dev/null 103then 104echo"$rhbranch '$truname' (early part) of ." 105else 106echo"$rhcommit '$remote'" 107fi 108} 109 110case"$#"in0) usage ;;esac 111 112rloga= have_message= 113while case"$#"in0)break;;esac 114do 115case"$1"in 116-n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\ 117--no-summa|--no-summar|--no-summary) 118 no_summary=t ;; 119--sq|--squ|--squa|--squas|--squash) 120 squash=t no_commit=t ;; 121--no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit) 122 no_commit=t ;; 123-s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\ 124--strateg=*|--strategy=*|\ 125-s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy) 126case"$#,$1"in 127*,*=*) 128 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'`;; 1291,*) 130 usage ;; 131*) 132 strategy="$2" 133shift;; 134esac 135case"$all_strategies"in 136*"$strategy"*) 137 use_strategies="$use_strategies$strategy";; 138*) 139 die "available strategies are:$all_strategies";; 140esac 141;; 142--reflog-action=*) 143 rloga=`expr "z$1" : 'z-[^=]*=\(.*\)'` 144;; 145-m=*|--m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*) 146 merge_msg=`expr "z$1" : 'z-[^=]*=\(.*\)'` 147 have_message=t 148;; 149-m|--m|--me|--mes|--mess|--messa|--messag|--message) 150shift 151case"$#"in 1521) usage ;; 153esac 154 merge_msg="$1" 155 have_message=t 156;; 157-*) usage ;; 158*)break;; 159esac 160shift 161done 162 163# This could be traditional "merge <msg> HEAD <commit>..." and the 164# way we can tell it is to see if the second token is HEAD, but some 165# people might have misused the interface and used a committish that 166# is the same as HEAD there instead. Traditional format never would 167# have "-m" so it is an additional safety measure to check for it. 168 169iftest -z"$have_message"&& 170 second_token=$(git-rev-parse --verify"$2^0"2>/dev/null) && 171 head_commit=$(git-rev-parse --verify"HEAD"2>/dev/null) && 172test"$second_token"="$head_commit" 173then 174 merge_msg="$1" 175shift 176 head_arg="$1" 177shift 178elif! git-rev-parse --verify HEAD >/dev/null 2>&1 179then 180# If the merged head is a valid one there is no reason to 181# forbid "git merge" into a branch yet to be born. We do 182# the same for "git pull". 183iftest1-ne$# 184then 185echo>&2"Can merge only exactly one commit into empty head" 186exit1 187fi 188 189 rh=$(git rev-parse --verify"$1^0") || 190 die "$1- not something we can merge" 191 192 git-update-ref -m"initial pull" HEAD "$rh"""&& 193 git-read-tree --reset -u HEAD 194exit 195 196else 197# We are invoked directly as the first-class UI. 198 head_arg=HEAD 199 200# All the rest are the commits being merged; prepare 201# the standard merge summary message to be appended to 202# the given message. If remote is invalid we will die 203# later in the common codepath so we discard the error 204# in this loop. 205 merge_name=$(for remote 206do 207 merge_name "$remote" 208done| git-fmt-merge-msg 209) 210 merge_msg="${merge_msg:+$merge_msg$LF$LF}$merge_name" 211fi 212head=$(git-rev-parse --verify"$head_arg"^0) || usage 213 214# All the rest are remote heads 215test"$#"=0&& usage ;# we need at least one remote head. 216test"$rloga"=''&& rloga="merge: $@" 217 218remoteheads= 219for remote 220do 221 remotehead=$(git-rev-parse --verify"$remote"^0 2>/dev/null) || 222 die "$remote- not something we can merge" 223 remoteheads="${remoteheads}$remotehead" 224eval GITHEAD_$remotehead='"$remote"' 225export GITHEAD_$remotehead 226done 227set x $remoteheads;shift 228 229case"$use_strategies"in 230'') 231case"$#"in 2321) 233 use_strategies="$default_twohead_strategies";; 234*) 235 use_strategies="$default_octopus_strategies";; 236esac 237;; 238esac 239 240for s in$use_strategies 241do 242case"$s"in 243*"$no_trivial_merge_strategies"*) 244 index_merge=f 245break 246;; 247esac 248done 249 250case"$#"in 2511) 252 common=$(git-merge-base --all$head"$@") 253;; 254*) 255 common=$(git-show-branch --merge-base$head"$@") 256;; 257esac 258echo"$head">"$GIT_DIR/ORIG_HEAD" 259 260case"$index_merge,$#,$common,$no_commit"in 261f,*) 262# We've been told not to try anything clever. Skip to real merge. 263;; 264?,*,'',*) 265# No common ancestors found. We need a real merge. 266;; 267?,1,"$1",*) 268# If head can reach all the merge then we are up to date. 269# but first the most common case of merging one remote. 270 finish_up_to_date "Already up-to-date." 271exit0 272;; 273?,1,"$head",*) 274# Again the most common case of merging one remote. 275echo"Updating $(git-rev-parse --short$head)..$(git-rev-parse --short$1)" 276 git-update-index --refresh2>/dev/null 277 new_head=$(git-rev-parse --verify"$1^0") && 278 git-read-tree -v -m -u --exclude-per-directory=.gitignore $head"$new_head"&& 279 finish "$new_head""Fast forward" 280 dropsave 281exit0 282;; 283?,1,?*"$LF"?*,*) 284# We are not doing octopus and not fast forward. Need a 285# real merge. 286;; 287?,1,*,) 288# We are not doing octopus, not fast forward, and have only 289# one common. See if it is really trivial. 290 git var GIT_COMMITTER_IDENT >/dev/null ||exit 291 292echo"Trying really trivial in-index merge..." 293 git-update-index --refresh2>/dev/null 294if git-read-tree --trivial -m -u -v$common $head"$1"&& 295 result_tree=$(git-write-tree) 296then 297echo"Wonderful." 298 result_commit=$( 299echo"$merge_msg"| 300 git-commit-tree $result_tree-p HEAD -p"$1" 301) ||exit 302 finish "$result_commit""In-index merge" 303 dropsave 304exit0 305fi 306echo"Nope." 307;; 308*) 309# An octopus. If we can reach all the remote we are up to date. 310 up_to_date=t 311for remote 312do 313 common_one=$(git-merge-base --all$head $remote) 314iftest"$common_one"!="$remote" 315then 316 up_to_date=f 317break 318fi 319done 320iftest"$up_to_date"= t 321then 322 finish_up_to_date "Already up-to-date. Yeeah!" 323exit0 324fi 325;; 326esac 327 328# We are going to make a new commit. 329git var GIT_COMMITTER_IDENT >/dev/null ||exit 330 331# At this point, we need a real merge. No matter what strategy 332# we use, it would operate on the index, possibly affecting the 333# working tree, and when resolved cleanly, have the desired tree 334# in the index -- this means that the index must be in sync with 335# the $head commit. The strategies are responsible to ensure this. 336 337case"$use_strategies"in 338?*' '?*) 339# Stash away the local changes so that we can try more than one. 340 savestate 341 single_strategy=no 342;; 343*) 344rm-f"$GIT_DIR/MERGE_SAVE" 345 single_strategy=yes 346;; 347esac 348 349result_tree= best_cnt=-1 best_strategy= wt_strategy= 350merge_was_ok= 351for strategy in$use_strategies 352do 353test"$wt_strategy"=''|| { 354echo"Rewinding the tree to pristine..." 355 restorestate 356} 357case"$single_strategy"in 358 no) 359echo"Trying merge strategy$strategy..." 360;; 361esac 362 363# Remember which strategy left the state in the working tree 364 wt_strategy=$strategy 365 366 git-merge-$strategy $common--"$head_arg""$@" 367exit=$? 368iftest"$no_commit"= t &&test"$exit"=0 369then 370 merge_was_ok=t 371exit=1;# pretend it left conflicts. 372fi 373 374test"$exit"=0|| { 375 376# The backend exits with 1 when conflicts are left to be resolved, 377# with 2 when it does not handle the given merge at all. 378 379iftest"$exit"-eq1 380then 381 cnt=`{ 382 git-diff-files --name-only 383 git-ls-files --unmerged 384 } | wc -l` 385iftest$best_cnt-le0-o$cnt-le$best_cnt 386then 387 best_strategy=$strategy 388 best_cnt=$cnt 389fi 390fi 391continue 392} 393 394# Automerge succeeded. 395 result_tree=$(git-write-tree) &&break 396done 397 398# If we have a resulting tree, that means the strategy module 399# auto resolved the merge cleanly. 400iftest''!="$result_tree" 401then 402 parents=$(git-show-branch --independent"$head""$@"|sed-e's/^/-p /') 403 result_commit=$(echo"$merge_msg"| git-commit-tree $result_tree $parents) ||exit 404 finish "$result_commit""Merge made by$wt_strategy." 405 dropsave 406exit0 407fi 408 409# Pick the result from the best strategy and have the user fix it up. 410case"$best_strategy"in 411'') 412 restorestate 413case"$use_strategies"in 414 ?*' '?*) 415echo>&2"No merge strategy handled the merge." 416;; 417*) 418echo>&2"Merge with strategy$use_strategiesfailed." 419;; 420esac 421exit2 422;; 423"$wt_strategy") 424# We already have its result in the working tree. 425;; 426*) 427echo"Rewinding the tree to pristine..." 428 restorestate 429echo"Using the$best_strategyto prepare resolving by hand." 430 git-merge-$best_strategy $common--"$head_arg""$@" 431;; 432esac 433 434iftest"$squash"= t 435then 436 finish 437else 438for remote 439do 440echo$remote 441done>"$GIT_DIR/MERGE_HEAD" 442echo"$merge_msg">"$GIT_DIR/MERGE_MSG" 443fi 444 445iftest"$merge_was_ok"= t 446then 447echo>&2 \ 448"Automatic merge went well; stopped before committing as requested" 449exit0 450else 451{ 452echo' 453Conflicts: 454' 455 git ls-files --unmerged| 456sed-e's/^[^ ]* / /'| 457uniq 458} >>"$GIT_DIR/MERGE_MSG" 459iftest -d"$GIT_DIR/rr-cache" 460then 461 git-rerere 462fi 463 die "Automatic merge failed; fix conflicts and then commit the result." 464fi