1#!/bin/sh 2# 3# Copyright (c) 2005 Junio C Hamano 4# 5 6OPTIONS_KEEPDASHDASH= 7OPTIONS_SPEC="\ 8git-merge [options] <remote>... 9git-merge [options] <msg> HEAD <remote> 10-- 11stat show a diffstat at the end of the merge 12n,no-stat don't show a diffstat at the end of the merge 13summary (synonym to --stat) 14no-summary (synonym to --no-stat) 15log add list of one-line log to merge commit message 16no-log don't add list of one-line log to merge commit message 17squash create a single commit instead of doing a merge 18commit perform a commit if the merge sucesses (default) 19ff allow fast forward (default) 20s,strategy= merge strategy to use 21m,message= message to be used for the merge commit (if any) 22" 23 24SUBDIRECTORY_OK=Yes 25. git-sh-setup 26require_work_tree 27cd_to_toplevel 28 29test -z"$(git ls-files -u)"|| 30 die "You are in the middle of a conflicted merge." 31 32LF=' 33' 34 35all_strategies='recur recursive octopus resolve stupid ours subtree' 36default_twohead_strategies='recursive' 37default_octopus_strategies='octopus' 38no_fast_forward_strategies='subtree ours' 39no_trivial_strategies='recursive recur subtree ours' 40use_strategies= 41 42allow_fast_forward=t 43allow_trivial_merge=t 44squash= no_commit= log_arg= 45 46dropsave() { 47rm-f --"$GIT_DIR/MERGE_HEAD""$GIT_DIR/MERGE_MSG" \ 48"$GIT_DIR/MERGE_STASH"||exit1 49} 50 51savestate() { 52# Stash away any local modifications. 53 git stash create >"$GIT_DIR/MERGE_STASH" 54} 55 56restorestate() { 57iftest -f"$GIT_DIR/MERGE_STASH" 58then 59 git reset--hard$head>/dev/null 60 git stash apply $(cat"$GIT_DIR/MERGE_STASH") 61 git update-index --refresh>/dev/null 62fi 63} 64 65finish_up_to_date () { 66case"$squash"in 67 t) 68echo"$1(nothing to squash)";; 69'') 70echo"$1";; 71esac 72 dropsave 73} 74 75squash_message () { 76echo Squashed commit of the following: 77echo 78 git log --no-merges --pretty=medium ^"$head"$remoteheads 79} 80 81finish () { 82iftest''="$2" 83then 84 rlogm="$GIT_REFLOG_ACTION" 85else 86echo"$2" 87 rlogm="$GIT_REFLOG_ACTION:$2" 88fi 89case"$squash"in 90 t) 91echo"Squash commit -- not updating HEAD" 92 squash_message >"$GIT_DIR/SQUASH_MSG" 93;; 94'') 95case"$merge_msg"in 96'') 97echo"No merge message -- not updating HEAD" 98;; 99*) 100 git update-ref -m"$rlogm" HEAD "$1""$head"||exit1 101 git gc --auto 102;; 103esac 104;; 105esac 106case"$1"in 107'') 108;; 109 ?*) 110iftest"$show_diffstat"= t 111then 112# We want color (if set), but no pager 113 GIT_PAGER='' git diff--stat --summary -M"$head""$1" 114fi 115;; 116esac 117 118# Run a post-merge hook 119iftest -x"$GIT_DIR"/hooks/post-merge 120then 121case"$squash"in 122 t) 123"$GIT_DIR"/hooks/post-merge 1 124;; 125'') 126"$GIT_DIR"/hooks/post-merge 0 127;; 128esac 129fi 130} 131 132merge_name () { 133 remote="$1" 134 rh=$(git rev-parse --verify"$remote^0"2>/dev/null) ||return 135 bh=$(git show-ref -s --verify"refs/heads/$remote"2>/dev/null) 136iftest"$rh"="$bh" 137then 138echo"$rhbranch '$remote' of ." 139elif truname=$(expr"$remote":'\(.*\)~[1-9][0-9]*$') && 140 git show-ref -q --verify"refs/heads/$truname"2>/dev/null 141then 142echo"$rhbranch '$truname' (early part) of ." 143eliftest"$remote"="FETCH_HEAD"-a -r"$GIT_DIR/FETCH_HEAD" 144then 145sed-e's/ not-for-merge / /'-e1q \ 146"$GIT_DIR/FETCH_HEAD" 147else 148echo"$rhcommit '$remote'" 149fi 150} 151 152parse_config () { 153whiletest$#!=0;do 154case"$1"in 155-n|--no-stat|--no-summary) 156 show_diffstat=false ;; 157--stat|--summary) 158 show_diffstat=t ;; 159--log|--no-log) 160 log_arg=$1;; 161--squash) 162test"$allow_fast_forward"= t || 163 die "You cannot combine --squash with --no-ff." 164 squash=t no_commit=t ;; 165--no-squash) 166 squash= no_commit= ;; 167--commit) 168 no_commit= ;; 169--no-commit) 170 no_commit=t ;; 171--ff) 172 allow_fast_forward=t ;; 173--no-ff) 174test"$squash"!= t || 175 die "You cannot combine --squash with --no-ff." 176 allow_fast_forward=f ;; 177-s|--strategy) 178shift 179case"$all_strategies"in 180*"$1"*) 181 use_strategies="$use_strategies$1";; 182*) 183 die "available strategies are:$all_strategies";; 184esac 185;; 186-m|--message) 187shift 188 merge_msg="$1" 189 have_message=t 190;; 191--) 192shift 193break;; 194*) usage ;; 195esac 196shift 197done 198 args_left=$# 199} 200 201test$#!=0|| usage 202 203have_message= 204 205if branch=$(git-symbolic-ref -q HEAD) 206then 207 mergeopts=$(git config "branch.${branch#refs/heads/}.mergeoptions") 208iftest -n"$mergeopts" 209then 210 parse_config $mergeopts-- 211fi 212fi 213 214parse_config "$@" 215whiletest$args_left-lt$#;doshift;done 216 217iftest -z"$show_diffstat";then 218test"$(git config --bool merge.diffstat)"= false && show_diffstat=false 219test"$(git config --bool merge.stat)"= false && show_diffstat=false 220test -z"$show_diffstat"&& show_diffstat=t 221fi 222 223# This could be traditional "merge <msg> HEAD <commit>..." and the 224# way we can tell it is to see if the second token is HEAD, but some 225# people might have misused the interface and used a committish that 226# is the same as HEAD there instead. Traditional format never would 227# have "-m" so it is an additional safety measure to check for it. 228 229iftest -z"$have_message"&& 230 second_token=$(git rev-parse --verify"$2^0"2>/dev/null) && 231 head_commit=$(git rev-parse --verify"HEAD"2>/dev/null) && 232test"$second_token"="$head_commit" 233then 234 merge_msg="$1" 235shift 236 head_arg="$1" 237shift 238elif! git rev-parse --verify HEAD >/dev/null 2>&1 239then 240# If the merged head is a valid one there is no reason to 241# forbid "git merge" into a branch yet to be born. We do 242# the same for "git pull". 243iftest1-ne$# 244then 245echo>&2"Can merge only exactly one commit into empty head" 246exit1 247fi 248 249 rh=$(git rev-parse --verify"$1^0") || 250 die "$1- not something we can merge" 251 252 git update-ref -m"initial pull" HEAD "$rh"""&& 253 git read-tree --reset -u HEAD 254exit 255 256else 257# We are invoked directly as the first-class UI. 258 head_arg=HEAD 259 260# All the rest are the commits being merged; prepare 261# the standard merge summary message to be appended to 262# the given message. If remote is invalid we will die 263# later in the common codepath so we discard the error 264# in this loop. 265 merge_name=$(for remote 266do 267 merge_name "$remote" 268done| git fmt-merge-msg $log_arg 269) 270 merge_msg="${merge_msg:+$merge_msg$LF$LF}$merge_name" 271fi 272head=$(git rev-parse --verify"$head_arg"^0) || usage 273 274# All the rest are remote heads 275test"$#"=0&& usage ;# we need at least one remote head. 276set_reflog_action "merge $*" 277 278remoteheads= 279for remote 280do 281 remotehead=$(git rev-parse --verify"$remote"^0 2>/dev/null) || 282 die "$remote- not something we can merge" 283 remoteheads="${remoteheads}$remotehead" 284eval GITHEAD_$remotehead='"$remote"' 285export GITHEAD_$remotehead 286done 287set x $remoteheads;shift 288 289case"$use_strategies"in 290'') 291case"$#"in 2921) 293 var="`git config --get pull.twohead`" 294iftest -n"$var" 295then 296 use_strategies="$var" 297else 298 use_strategies="$default_twohead_strategies" 299fi;; 300*) 301 var="`git config --get pull.octopus`" 302iftest -n"$var" 303then 304 use_strategies="$var" 305else 306 use_strategies="$default_octopus_strategies" 307fi;; 308esac 309;; 310esac 311 312for s in$use_strategies 313do 314for ss in$no_fast_forward_strategies 315do 316case"$s"in 317*"$ss"*) 318 allow_fast_forward=f 319break 320;; 321esac 322done 323for ss in$no_trivial_strategies 324do 325case"$s"in 326*"$ss"*) 327 allow_trivial_merge=f 328break 329;; 330esac 331done 332done 333 334case"$#"in 3351) 336 common=$(git merge-base --all$head"$@") 337;; 338*) 339 common=$(git show-branch --merge-base$head"$@") 340;; 341esac 342echo"$head">"$GIT_DIR/ORIG_HEAD" 343 344case"$allow_fast_forward,$#,$common,$no_commit"in 345?,*,'',*) 346# No common ancestors found. We need a real merge. 347;; 348?,1,"$1",*) 349# If head can reach all the merge then we are up to date. 350# but first the most common case of merging one remote. 351 finish_up_to_date "Already up-to-date." 352exit0 353;; 354t,1,"$head",*) 355# Again the most common case of merging one remote. 356echo"Updating $(git rev-parse --short$head)..$(git rev-parse --short$1)" 357 git update-index --refresh2>/dev/null 358 msg="Fast forward" 359iftest -n"$have_message" 360then 361 msg="$msg(no commit created; -m option ignored)" 362fi 363 new_head=$(git rev-parse --verify"$1^0") && 364 git read-tree -v -m -u --exclude-per-directory=.gitignore $head"$new_head"&& 365 finish "$new_head""$msg"||exit 366 dropsave 367exit0 368;; 369?,1,?*"$LF"?*,*) 370# We are not doing octopus and not fast forward. Need a 371# real merge. 372;; 373?,1,*,) 374# We are not doing octopus, not fast forward, and have only 375# one common. 376 git update-index --refresh2>/dev/null 377case"$allow_trivial_merge"in 378 t) 379# See if it is really trivial. 380 git var GIT_COMMITTER_IDENT >/dev/null ||exit 381echo"Trying really trivial in-index merge..." 382if git read-tree --trivial -m -u -v$common $head"$1"&& 383 result_tree=$(git write-tree) 384then 385echo"Wonderful." 386 result_commit=$( 387printf'%s\n'"$merge_msg"| 388 git commit-tree $result_tree-p HEAD -p"$1" 389) ||exit 390 finish "$result_commit""In-index merge" 391 dropsave 392exit0 393fi 394echo"Nope." 395esac 396;; 397*) 398# An octopus. If we can reach all the remote we are up to date. 399 up_to_date=t 400for remote 401do 402 common_one=$(git merge-base --all$head $remote) 403iftest"$common_one"!="$remote" 404then 405 up_to_date=f 406break 407fi 408done 409iftest"$up_to_date"= t 410then 411 finish_up_to_date "Already up-to-date. Yeeah!" 412exit0 413fi 414;; 415esac 416 417# We are going to make a new commit. 418git var GIT_COMMITTER_IDENT >/dev/null ||exit 419 420# At this point, we need a real merge. No matter what strategy 421# we use, it would operate on the index, possibly affecting the 422# working tree, and when resolved cleanly, have the desired tree 423# in the index -- this means that the index must be in sync with 424# the $head commit. The strategies are responsible to ensure this. 425 426case"$use_strategies"in 427?*' '?*) 428# Stash away the local changes so that we can try more than one. 429 savestate 430 single_strategy=no 431;; 432*) 433rm-f"$GIT_DIR/MERGE_STASH" 434 single_strategy=yes 435;; 436esac 437 438result_tree= best_cnt=-1 best_strategy= wt_strategy= 439merge_was_ok= 440for strategy in$use_strategies 441do 442test"$wt_strategy"=''|| { 443echo"Rewinding the tree to pristine..." 444 restorestate 445} 446case"$single_strategy"in 447 no) 448echo"Trying merge strategy$strategy..." 449;; 450esac 451 452# Remember which strategy left the state in the working tree 453 wt_strategy=$strategy 454 455 git-merge-$strategy $common--"$head_arg""$@" 456exit=$? 457iftest"$no_commit"= t &&test"$exit"=0 458then 459 merge_was_ok=t 460exit=1;# pretend it left conflicts. 461fi 462 463test"$exit"=0|| { 464 465# The backend exits with 1 when conflicts are left to be resolved, 466# with 2 when it does not handle the given merge at all. 467 468iftest"$exit"-eq1 469then 470 cnt=`{ 471 git diff-files --name-only 472 git ls-files --unmerged 473 } | wc -l` 474iftest$best_cnt-le0-o$cnt-le$best_cnt 475then 476 best_strategy=$strategy 477 best_cnt=$cnt 478fi 479fi 480continue 481} 482 483# Automerge succeeded. 484 result_tree=$(git write-tree) &&break 485done 486 487# If we have a resulting tree, that means the strategy module 488# auto resolved the merge cleanly. 489iftest''!="$result_tree" 490then 491iftest"$allow_fast_forward"="t" 492then 493 parents=$(git show-branch --independent"$head""$@") 494else 495 parents=$(git rev-parse "$head""$@") 496fi 497 parents=$(echo"$parents"|sed-e's/^/-p /') 498 result_commit=$(printf'%s\n'"$merge_msg"| git commit-tree $result_tree $parents) ||exit 499 finish "$result_commit""Merge made by$wt_strategy." 500 dropsave 501exit0 502fi 503 504# Pick the result from the best strategy and have the user fix it up. 505case"$best_strategy"in 506'') 507 restorestate 508case"$use_strategies"in 509 ?*' '?*) 510echo>&2"No merge strategy handled the merge." 511;; 512*) 513echo>&2"Merge with strategy$use_strategiesfailed." 514;; 515esac 516exit2 517;; 518"$wt_strategy") 519# We already have its result in the working tree. 520;; 521*) 522echo"Rewinding the tree to pristine..." 523 restorestate 524echo"Using the$best_strategyto prepare resolving by hand." 525 git-merge-$best_strategy $common--"$head_arg""$@" 526;; 527esac 528 529iftest"$squash"= t 530then 531 finish 532else 533for remote 534do 535echo$remote 536done>"$GIT_DIR/MERGE_HEAD" 537printf'%s\n'"$merge_msg">"$GIT_DIR/MERGE_MSG" 538fi 539 540iftest"$merge_was_ok"= t 541then 542echo>&2 \ 543"Automatic merge went well; stopped before committing as requested" 544exit0 545else 546{ 547echo' 548Conflicts: 549' 550 git ls-files --unmerged| 551sed-e's/^[^ ]* / /'| 552uniq 553} >>"$GIT_DIR/MERGE_MSG" 554 git rerere 555 die "Automatic merge failed; fix conflicts and then commit the result." 556fi