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