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