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