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