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