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