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