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