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