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