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] [-s <strategy>]... <merge-message> <head> <remote>+" 13} 14 15# all_strategies='resolve fredrik stupid octopus' 16 17all_strategies='fredrik octopus resolve stupid' 18default_strategies='resolve octopus' 19use_strategies= 20 21dropheads() { 22rm-f --"$GIT_DIR/MERGE_HEAD"||exit1 23} 24 25summary() { 26case"$no_summary"in 27'') 28 git-diff-tree -p -M$head"$1"| 29 git-apply --stat --summary 30;; 31esac 32} 33 34while case"$#"in0)break;;esac 35do 36case"$1"in 37-n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\ 38--no-summa|--no-summar|--no-summary) 39 no_summary=t ;; 40-s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\ 41--strateg=*|--strategy=*|\ 42-s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy) 43case"$#,$1"in 44*,*=*) 45 strategy=`expr "$1" : '-[^=]*=\(.*\)'`;; 461,*) 47 usage ;; 48*) 49 strategy="$2" 50shift;; 51esac 52case"$all_strategies"in 53*"$strategy"*) 54 use_strategies="$use_strategies$strategy";; 55*) 56 die "available strategies are:$all_strategies";; 57esac 58;; 59-*) usage ;; 60*)break;; 61esac 62shift 63done 64 65case"$use_strategies"in 66'') 67 use_strategies=$default_strategies 68;; 69esac 70test"$#"-le2&& usage ;# we need at least two heads. 71 72merge_msg="$1" 73shift 74head=$(git-rev-parse --verify "$1"^0)|| usage 75shift 76 77# All the rest are remote heads 78for remote 79do 80 git-rev-parse --verify"$remote"^0>/dev/null || 81 die "$remote- not something we can merge" 82done 83 84common=$(git-show-branch --merge-base $head "$@") 85echo"$head">"$GIT_DIR/ORIG_HEAD" 86 87case"$#,$common"in 88*,'') 89 die "Unable to find common commit between$headand $*" 90;; 911,"$1") 92# If head can reach all the merge then we are up to date. 93# but first the most common case of merging one remote 94echo"Already up-to-date. Yeeah!" 95 dropheads 96exit0 97;; 981,"$head") 99# Again the most common case of merging one remote. 100echo"Updating from$headto$1." 101 git-update-index --refresh2>/dev/null 102 git-read-tree -u -m$head"$1"||exit1 103 git-rev-parse --verify"$1^0">"$GIT_DIR/HEAD" 104 summary "$1" 105 dropheads 106exit0 107;; 1081,*) 109# We are not doing octopus and not fast forward. Need a 110# real merge. 111;; 112*) 113# An octopus. If we can reach all the remote we are up to date. 114 up_to_date=t 115for remote 116do 117 common_one=$(git-merge-base $head $remote) 118iftest"$common_one"!="$remote" 119then 120 up_to_date=f 121break 122fi 123done 124iftest"$up_to_date"= t 125then 126echo"Already up-to-date. Yeeah!" 127 dropheads 128exit0 129fi 130;; 131esac 132 133# At this point we need a real merge. Require that the tree matches 134# exactly our head. 135 136git-update-index --refresh&& 137test''="`git-diff-index --cached --name-only$head`"|| { 138 die "Need real merge but the working tree has local changes." 139} 140 141result_tree= best_cnt=-1 best_strategy= wt_strategy= 142for strategy in$use_strategies 143do 144test"$wt_strategy"=''|| { 145echo"Rewinding the tree to pristine..." 146 git reset--hard$head 147} 148echo"Trying merge strategy$strategy..." 149 wt_strategy=$strategy 150 git-merge-$strategy $common--$head"$@"|| { 151 152# The backend exits with 1 when conflicts are left to be resolved, 153# with 2 when it does not handle the given merge at all. 154 155exit=$? 156iftest"$exit"-eq1 157then 158 cnt=`{ 159 git-diff-files --name-only 160 git-ls-files --unmerged 161 } | wc -l` 162iftest$best_cnt-le0-o$cnt-le$best_cnt 163then 164 best_strategy=$strategy 165 best_cnt=$cnt 166fi 167fi 168continue 169} 170 171# Automerge succeeded. 172 result_tree=$(git-write-tree)&&break 173done 174 175# If we have a resulting tree, that means the strategy module 176# auto resolved the merge cleanly. 177iftest''!="$result_tree" 178then 179 parents="-p$head" 180for remote 181do 182 parents="$parents-p$remote" 183done 184 result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents) 185echo"Committed merge$result_commit, made by$wt_strategy." 186echo$result_commit>"$GIT_DIR/HEAD" 187 summary $result_commit 188 dropheads 189exit0 190fi 191 192# Pick the result from the best strategy and have the user fix it up. 193case"$best_strategy"in 194'') 195 git reset--hard$head 196 die "No merge strategy handled the merge." 197;; 198"$wt_strategy") 199# We already have its result in the working tree. 200;; 201*) 202echo"Rewinding the tree to pristine..." 203 git reset--hard$head 204echo"Using the$best_strategyto prepare resolving by hand." 205 git-merge-$best_strategy $common--$head"$@" 206;; 207esac 208for remote 209do 210echo$remote 211done>"$GIT_DIR/MERGE_HEAD" 212die "Automatic merge failed; fix up by hand"