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