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-merge1 132;; 133'') 134"$GIT_DIR"/hooks/post-merge0 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 145 then 146 echo "$rhbranch '$truname' (early part) of ." 147 return 148 fi 149 if found_ref=$(git rev-parse --symbolic-full-name --verify \ 150 "$remote" 2>/dev/null) 151 then 152 expanded=$(git check-ref-format --branch "$remote")|| 153 exit 154 if test "${found_ref#refs/heads/}" != "$found_ref" 155 then 156 echo "$rhbranch '$expanded' of ." 157 return 158 elif test "${found_ref#refs/remotes/}" != "$found_ref" 159 then 160 echo "$rhremote branch '$expanded' of ." 161 return 162 fi 163 fi 164 if test "$remote" = "FETCH_HEAD" && test -r "$GIT_DIR/FETCH_HEAD" 165 then 166 sed -e 's/ not-for-merge/ /' -e 1q \ 167 "$GIT_DIR/FETCH_HEAD" 168 return 169 fi 170 echo "$rhcommit '$remote'" 171} 172 173parse_config () { 174 while test$#!= 0; do 175 case "$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) 183 test "$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) 195 test "$squash" != t || 196 die "You cannot combine --squash with --no-ff." 197 test "$fast_forward_only" != t || 198 die "You cannot combine --ff-only with --no-ff." 199 allow_fast_forward=f ;; 200 --ff-only) 201 test "$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) 207 shift 208 case "$all_strategies" in 209 *"$1"*) 210 use_strategies="$use_strategies$1" 211 ;; 212 *) 213 case "$not_strategies" in 214 *"$1"*) 215 false 216 esac && 217 type "git-merge-$1" >/dev/null 2>&1 || 218 die "available strategies are:$all_strategies" 219 use_strategies="$use_strategies$1" 220 ;; 221 esac 222 ;; 223 -X) 224 shift 225 xopt="${xopt:+$xopt }$(git rev-parse --sq-quote "--$1")" 226 ;; 227 -m|--message) 228 shift 229 merge_msg="$1" 230 have_message=t 231 ;; 232 --) 233 shift 234 break ;; 235 *) usage ;; 236 esac 237 shift 238 done 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") 249 if test -n "$mergeopts" 250 then 251 parse_config$mergeopts-- 252 fi 253fi 254 255parse_config "$@" 256while test$args_left-lt$#; do shift; done 257 258if test -z "$show_diffstat"; then 259 test "$(git config --bool merge.diffstat)" = false && show_diffstat=false 260 test "$(git config --bool merge.stat)" = false && show_diffstat=false 261 test -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 270if test -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)&& 273 test "$second_token" = "$head_commit" 274then 275 merge_msg="$1" 276 shift 277 head_arg="$1" 278 shift 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". 284 if test 1 -ne$# 285 then 286 echo >&2 "Can merge only exactly one commit into empty head" 287 exit 1 288 fi 289 290 test "$squash" != t || 291 die "Squash commit into empty head not supported yet" 292 test "$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 299 exit 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" 335 eval GITHEAD_$remotehead='"$remote"' 336 export GITHEAD_$remotehead 337done 338set x$remoteheads; shift 339 340case "$use_strategies" in 341'') 342 case "$#" in 343 1) 344 var="$(git config --get pull.twohead)" 345 if test -n "$var" 346 then 347 use_strategies="$var" 348 else 349 use_strategies="$default_twohead_strategies" 350 fi ;; 351 *) 352 var="$(git config --get pull.octopus)" 353 if test -n "$var" 354 then 355 use_strategies="$var" 356 else 357 use_strategies="$default_octopus_strategies" 358 fi ;; 359 esac 360 ;; 361esac 362 363for s in$use_strategies 364do 365 for ss in$no_fast_forward_strategies 366 do 367 case "$s" in 368 *"$ss"*) 369 allow_fast_forward=f 370 break 371 ;; 372 esac 373 done 374 for ss in$no_trivial_strategies 375 do 376 case "$s" in 377 *"$ss"*) 378 allow_trivial_merge=f 379 break 380 ;; 381 esac 382 done 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." 403 exit 0 404 ;; 405t,1,"$head",*) 406 # Again the most common case of merging one remote. 407 echo "Updating$(git rev-parse --short $head)..$(git rev-parse --short $1)" 408 git update-index --refresh 2>/dev/null 409 msg="Fast-forward" 410 if test -n "$have_message" 411 then 412 msg="$msg(no commit created; -m option ignored)" 413 fi 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 418 exit 0 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 --refresh 2>/dev/null 428 case "$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 432 echo "Trying really trivial in-index merge..." 433 if git read-tree --trivial -m -u -v$common$head"$1" && 434 result_tree=$(git write-tree) 435 then 436 echo "Wonderful." 437 result_commit=$( 438 printf '%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 443 exit 0 444 fi 445 echo "Nope." 446 esac 447 ;; 448*) 449 # An octopus. If we can reach all the remote we are up to date. 450 up_to_date=t 451 for remote 452 do 453 common_one=$(git merge-base --all $head $remote) 454 if test "$common_one" != "$remote" 455 then 456 up_to_date=f 457 break 458 fi 459 done 460 if test "$up_to_date" = t 461 then 462 finish_up_to_date "Already up-to-date. Yeeah!" 463 exit 0 464 fi 465 ;; 466esac 467 468if test "$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$headcommit. 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*) 489 rm -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 498 test "$wt_strategy" = '' || { 499 echo "Rewinding the tree to pristine..." 500 restorestate 501 } 502 case "$single_strategy" in 503 no) 504 echo "Trying merge strategy$strategy..." 505 ;; 506 esac 507 508 # Remember which strategy left the state in the working tree 509 wt_strategy=$strategy 510 511 eval 'git-merge-$strategy'"$xopt"'$common--"$head_arg""$@"' 512 exit=$? 513 if test "$no_commit" = t && test "$exit" = 0 514 then 515 merge_was_ok=t 516 exit=1 ;# pretend it left conflicts. 517 fi 518 519 test "$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 524 if test "$exit" -eq 1 525 then 526 cnt=`{ 527 git diff-files --name-only 528 git ls-files --unmerged 529 } | wc -l` 530 if test$best_cnt-le 0 || test$cnt-le$best_cnt 531 then 532 best_strategy=$strategy 533 best_cnt=$cnt 534 fi 535 fi 536 continue 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. 545if test '' != "$result_tree" 546then 547 if test "$allow_fast_forward" = "t" 548 then 549 parents=$(git merge-base --independent "$head" "$@") 550 else 551 parents=$(git rev-parse "$head" "$@") 552 fi 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 557 exit 0 558fi 559 560# Pick the result from the best strategy and have the user fix it up. 561case "$best_strategy" in 562'') 563 restorestate 564 case "$use_strategies" in 565 ?*''?*) 566 echo >&2 "No merge strategy handled the merge." 567 ;; 568 *) 569 echo >&2 "Merge with strategy$use_strategiesfailed." 570 ;; 571 esac 572 exit 2 573 ;; 574"$wt_strategy") 575 # We already have its result in the working tree. 576 ;; 577*) 578 echo "Rewinding the tree to pristine..." 579 restorestate 580 echo "Using the$best_strategyto prepare resolving by hand." 581 git-merge-$best_strategy$common-- "$head_arg" "$@" 582 ;; 583esac 584 585if test "$squash" = t 586then 587 finish 588else 589 for remote 590 do 591 echo$remote 592 done >"$GIT_DIR/MERGE_HEAD" 593 printf '%s\n' "$merge_msg" >"$GIT_DIR/MERGE_MSG" || 594 die "Could not write to$GIT_DIR/MERGE_MSG" 595 if test "$allow_fast_forward" != t 596 then 597 printf "%s" no-ff 598 else 599 : 600 fi >"$GIT_DIR/MERGE_MODE" || 601 die "Could not write to$GIT_DIR/MERGE_MODE" 602fi 603 604if test "$merge_was_ok" = t 605then 606 echo >&2 \ 607 "Automatic merge went well; stopped before committing as requested" 608 exit 0 609else 610 { 611 echo ' 612Conflicts: 613' 614 git ls-files --unmerged | 615 sed -e 's/^[^ ]* / /' | 616 uniq 617 } >>"$GIT_DIR/MERGE_MSG" 618 git rerere$rr_arg 619 die "Automatic merge failed; fix conflicts and then commit the result." 620fi