3 # Copyright (c) 2005 Junio C Hamano
12 die "git-merge [-n] [--no-commit] [-s <strategy>]... <merge-message> <head> <remote>+"
15 all_strategies='recursive octopus resolve stupid ours'
16 default_strategies='recursive'
20 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
21 "$GIT_DIR/MERGE_SAVE" || exit 1
25 # Stash away any local modifications.
26 git-diff-index -z --name-only $head |
27 cpio -0 -o >"$GIT_DIR/MERGE_SAVE"
31 if test -f "$GIT_DIR/MERGE_SAVE"
33 git reset --hard $head
34 cpio -iuv <"$GIT_DIR/MERGE_SAVE"
35 git-update-index --refresh >/dev/null
40 test '' = "$2" || echo "$2"
43 echo "No merge message -- not updating HEAD"
46 git-update-ref HEAD "$1" "$head" || exit 1
52 git-diff-tree -p -M "$head" "$1" |
53 git-apply --stat --summary
58 while case "$#" in 0) break ;; esac
61 -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\
62 --no-summa|--no-summar|--no-summary)
64 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
66 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
67 --strateg=*|--strategy=*|\
68 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
71 strategy=`expr "$1" : '-[^=]*=\(.*\)'` ;;
78 case " $all_strategies " in
80 use_strategies="$use_strategies$strategy " ;;
82 die "available strategies are: $all_strategies" ;;
91 test "$#" -le 2 && usage ;# we need at least two heads.
96 head=$(git-rev-parse --verify "$1"^0) || usage
99 # All the rest are remote heads
103 remotehead=$(git-rev-parse --verify "$remote"^0) ||
104 die "$remote - not something we can merge"
105 remoteheads="${remoteheads}$remotehead "
107 set x $remoteheads ; shift
111 common=$(git-merge-base --all $head "$@")
114 common=$(git-show-branch --merge-base $head "$@")
117 echo "$head" >"$GIT_DIR/ORIG_HEAD"
119 case "$#,$common,$no_commit" in
121 # No common ancestors found. We need a real merge.
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
126 echo "Already up-to-date."
131 # Again the most common case of merging one remote.
132 echo "Updating from $head to $1."
133 git-update-index --refresh 2>/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"
141 # We are not doing octopus and not fast forward. Need a
145 # We are not doing octopus, not fast forward, and have only
146 # one common. See if it is really trivial.
147 echo "Trying really trivial in-index merge..."
148 git-update-index --refresh 2>/dev/null
149 if git-read-tree --trivial -m -u $common $head "$1" &&
150 result_tree=$(git-write-tree)
155 git-commit-tree $result_tree -p HEAD -p "$1"
157 finish "$result_commit" "In-index merge"
164 # An octopus. If we can reach all the remote we are up to date.
168 common_one=$(git-merge-base --all $head $remote)
169 if test "$common_one" != "$remote"
175 if test "$up_to_date" = t
177 echo "Already up-to-date. Yeeah!"
184 case "$use_strategies" in
188 use_strategies="$default_strategies" ;;
190 use_strategies=octopus ;;
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.
201 case "$use_strategies" in
203 # Stash away the local changes so that we can try more than one.
208 rm -f "$GIT_DIR/MERGE_SAVE"
213 result_tree= best_cnt=-1 best_strategy= wt_strategy=
214 for strategy in $use_strategies
216 test "$wt_strategy" = '' || {
217 echo "Rewinding the tree to pristine..."
220 case "$single_strategy" in
222 echo "Trying merge strategy $strategy..."
226 # Remember which strategy left the state in the working tree
227 wt_strategy=$strategy
229 git-merge-$strategy $common -- "$head_arg" "$@"
231 if test "$no_commit" = t && test "$exit" = 0
233 exit=1 ;# pretend it left conflicts.
236 test "$exit" = 0 || {
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.
241 if test "$exit" -eq 1
244 git-diff-files --name-only
245 git-ls-files --unmerged
247 if test $best_cnt -le 0 -o $cnt -le $best_cnt
249 best_strategy=$strategy
256 # Automerge succeeded.
257 result_tree=$(git-write-tree) && break
260 # If we have a resulting tree, that means the strategy module
261 # auto resolved the merge cleanly.
262 if test '' != "$result_tree"
267 parents="$parents -p $remote"
269 result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents) || exit
270 finish "$result_commit" "Merge $result_commit, made by $wt_strategy."
275 # Pick the result from the best strategy and have the user fix it up.
276 case "$best_strategy" in
279 echo >&2 "No merge strategy handled the merge."
283 # We already have its result in the working tree.
286 echo "Rewinding the tree to pristine..."
288 echo "Using the $best_strategy to prepare resolving by hand."
289 git-merge-$best_strategy $common -- "$head_arg" "$@"
295 done >"$GIT_DIR/MERGE_HEAD"
296 echo $merge_msg >"$GIT_DIR/MERGE_MSG"
298 die "Automatic merge failed/prevented; fix up by hand"