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