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