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