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-merge 1 119;; 120'') 121"$GIT_DIR"/hooks/post-merge 0 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 136then 137echo"$rhbranch '$truname' (early part) of ." 138eliftest"$remote"="FETCH_HEAD"-a -r"$GIT_DIR/FETCH_HEAD" 139then 140sed-e's/ not-for-merge / /'-e1q \ 141"$GIT_DIR/FETCH_HEAD" 142else 143echo"$rhcommit '$remote'" 144fi 145} 146 147parse_config () { 148whiletest$#!=0;do 149case"$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) 167shift 168case"$all_strategies"in 169*"$1"*) 170 use_strategies="$use_strategies$1";; 171*) 172 die "available strategies are:$all_strategies";; 173esac 174;; 175-m|--message) 176shift 177 merge_msg="$1" 178 have_message=t 179;; 180--) 181shift 182break;; 183*) usage ;; 184esac 185shift 186done 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") 197iftest -n"$mergeopts" 198then 199 parse_config $mergeopts-- 200fi 201fi 202 203parse_config "$@" 204whiletest$args_left-lt$#;doshift;done 205 206iftest -z"$show_diffstat";then 207test"$(git config --bool merge.diffstat)"= false && show_diffstat=false 208test -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 217iftest -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) && 220test"$second_token"="$head_commit" 221then 222 merge_msg="$1" 223shift 224 head_arg="$1" 225shift 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". 231iftest1-ne$# 232then 233echo>&2"Can merge only exactly one commit into empty head" 234exit1 235fi 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 242exit 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 254do 255 merge_name "$remote" 256done| 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" 272eval GITHEAD_$remotehead='"$remote"' 273export GITHEAD_$remotehead 274done 275set x $remoteheads;shift 276 277case"$use_strategies"in 278'') 279case"$#"in 2801) 281 var="`git config --get pull.twohead`" 282iftest -n"$var" 283then 284 use_strategies="$var" 285else 286 use_strategies="$default_twohead_strategies" 287fi;; 288*) 289 var="`git config --get pull.octopus`" 290iftest -n"$var" 291then 292 use_strategies="$var" 293else 294 use_strategies="$default_octopus_strategies" 295fi;; 296esac 297;; 298esac 299 300for s in$use_strategies 301do 302for ss in$no_fast_forward_strategies 303do 304case"$s"in 305*"$ss"*) 306 allow_fast_forward=f 307break 308;; 309esac 310done 311for ss in$no_trivial_strategies 312do 313case"$s"in 314*"$ss"*) 315 allow_trivial_merge=f 316break 317;; 318esac 319done 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." 340exit0 341;; 342t,1,"$head",*) 343# Again the most common case of merging one remote. 344echo"Updating $(git rev-parse --short$head)..$(git rev-parse --short$1)" 345 git update-index --refresh2>/dev/null 346 msg="Fast forward" 347iftest -n"$have_message" 348then 349 msg="$msg(no commit created; -m option ignored)" 350fi 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 355exit0 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 --refresh2>/dev/null 365case"$allow_trivial_merge"in 366 t) 367# See if it is really trivial. 368 git var GIT_COMMITTER_IDENT >/dev/null ||exit 369echo"Trying really trivial in-index merge..." 370if git read-tree --trivial -m -u -v$common $head"$1"&& 371 result_tree=$(git write-tree) 372then 373echo"Wonderful." 374 result_commit=$( 375printf'%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 380exit0 381fi 382echo"Nope." 383esac 384;; 385*) 386# An octopus. If we can reach all the remote we are up to date. 387 up_to_date=t 388for remote 389do 390 common_one=$(git merge-base --all$head $remote) 391iftest"$common_one"!="$remote" 392then 393 up_to_date=f 394break 395fi 396done 397iftest"$up_to_date"= t 398then 399 finish_up_to_date "Already up-to-date. Yeeah!" 400exit0 401fi 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 $head commit. 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*) 421rm-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 430test"$wt_strategy"=''|| { 431echo"Rewinding the tree to pristine..." 432 restorestate 433} 434case"$single_strategy"in 435 no) 436echo"Trying merge strategy$strategy..." 437;; 438esac 439 440# Remember which strategy left the state in the working tree 441 wt_strategy=$strategy 442 443 git-merge-$strategy $common--"$head_arg""$@" 444exit=$? 445iftest"$no_commit"= t &&test"$exit"=0 446then 447 merge_was_ok=t 448exit=1;# pretend it left conflicts. 449fi 450 451test"$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 456iftest"$exit"-eq1 457then 458 cnt=`{ 459 git diff-files --name-only 460 git ls-files --unmerged 461 } | wc -l` 462iftest$best_cnt-le0-o$cnt-le$best_cnt 463then 464 best_strategy=$strategy 465 best_cnt=$cnt 466fi 467fi 468continue 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. 477iftest''!="$result_tree" 478then 479iftest"$allow_fast_forward"="t" 480then 481 parents=$(git show-branch --independent"$head""$@") 482else 483 parents=$(git rev-parse "$head""$@") 484fi 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 489exit0 490fi 491 492# Pick the result from the best strategy and have the user fix it up. 493case"$best_strategy"in 494'') 495 restorestate 496case"$use_strategies"in 497 ?*' '?*) 498echo>&2"No merge strategy handled the merge." 499;; 500*) 501echo>&2"Merge with strategy$use_strategiesfailed." 502;; 503esac 504exit2 505;; 506"$wt_strategy") 507# We already have its result in the working tree. 508;; 509*) 510echo"Rewinding the tree to pristine..." 511 restorestate 512echo"Using the$best_strategyto prepare resolving by hand." 513 git-merge-$best_strategy $common--"$head_arg""$@" 514;; 515esac 516 517iftest"$squash"= t 518then 519 finish 520else 521for remote 522do 523echo$remote 524done>"$GIT_DIR/MERGE_HEAD" 525printf'%s\n'"$merge_msg">"$GIT_DIR/MERGE_MSG" 526fi 527 528iftest"$merge_was_ok"= t 529then 530echo>&2 \ 531"Automatic merge went well; stopped before committing as requested" 532exit0 533else 534{ 535echo' 536Conflicts: 537' 538 git ls-files --unmerged| 539sed-e's/^[^ ]* / /'| 540uniq 541} >>"$GIT_DIR/MERGE_MSG" 542 git rerere 543 die "Automatic merge failed; fix conflicts and then commit the result." 544fi