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