3 # Copyright (c) 2005 Junio C Hamano
6 USAGE='[-n] [--no-commit] [--squash] [-s <strategy>] [-m=<merge-message>] <commit>+'
13 test -z "$(git ls-files -u)" ||
14 die "You are in the middle of a conflicted merge."
19 all_strategies='recur recursive octopus resolve stupid ours'
20 default_twohead_strategies='recursive'
21 default_octopus_strategies='octopus'
22 no_trivial_merge_strategies='ours'
28 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
29 "$GIT_DIR/MERGE_SAVE" || exit 1
33 # Stash away any local modifications.
34 git-diff-index -z --name-only $head |
35 cpio -0 -o >"$GIT_DIR/MERGE_SAVE"
39 if test -f "$GIT_DIR/MERGE_SAVE"
41 git reset --hard $head >/dev/null
42 cpio -iuv <"$GIT_DIR/MERGE_SAVE"
43 git-update-index --refresh >/dev/null
47 finish_up_to_date () {
50 echo "$1 (nothing to squash)" ;;
58 echo Squashed commit of the following:
60 git-log --no-merges ^"$head" $remote
66 rlogm="$GIT_REFLOG_ACTION"
69 rlogm="$GIT_REFLOG_ACTION: $2"
73 echo "Squash commit -- not updating HEAD"
74 squash_message >"$GIT_DIR/SQUASH_MSG"
79 echo "No merge message -- not updating HEAD"
82 git-update-ref -m "$rlogm" HEAD "$1" "$head" || exit 1
93 git-diff-tree --stat --summary -M "$head" "$1"
102 rh=$(git-rev-parse --verify "$remote^0" 2>/dev/null) || return
103 bh=$(git-show-ref -s --verify "refs/heads/$remote" 2>/dev/null)
104 if test "$rh" = "$bh"
106 echo "$rh branch '$remote' of ."
107 elif truname=$(expr "$remote" : '\(.*\)~[1-9][0-9]*$') &&
108 git-show-ref -q --verify "refs/heads/$truname" 2>/dev/null
110 echo "$rh branch '$truname' (early part) of ."
112 echo "$rh commit '$remote'"
116 case "$#" in 0) usage ;; esac
119 while case "$#" in 0) break ;; esac
122 -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\
123 --no-summa|--no-summar|--no-summary)
125 --sq|--squ|--squa|--squas|--squash)
126 squash=t no_commit=t ;;
127 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
129 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
130 --strateg=*|--strategy=*|\
131 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
134 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
141 case " $all_strategies " in
143 use_strategies="$use_strategies$strategy " ;;
145 die "available strategies are: $all_strategies" ;;
148 -m=*|--m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
149 merge_msg=`expr "z$1" : 'z-[^=]*=\(.*\)'`
152 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
166 # This could be traditional "merge <msg> HEAD <commit>..." and the
167 # way we can tell it is to see if the second token is HEAD, but some
168 # people might have misused the interface and used a committish that
169 # is the same as HEAD there instead. Traditional format never would
170 # have "-m" so it is an additional safety measure to check for it.
172 if test -z "$have_message" &&
173 second_token=$(git-rev-parse --verify "$2^0" 2>/dev/null) &&
174 head_commit=$(git-rev-parse --verify "HEAD" 2>/dev/null) &&
175 test "$second_token" = "$head_commit"
181 elif ! git-rev-parse --verify HEAD >/dev/null 2>&1
183 # If the merged head is a valid one there is no reason to
184 # forbid "git merge" into a branch yet to be born. We do
185 # the same for "git pull".
188 echo >&2 "Can merge only exactly one commit into empty head"
192 rh=$(git rev-parse --verify "$1^0") ||
193 die "$1 - not something we can merge"
195 git-update-ref -m "initial pull" HEAD "$rh" "" &&
196 git-read-tree --reset -u HEAD
200 # We are invoked directly as the first-class UI.
203 # All the rest are the commits being merged; prepare
204 # the standard merge summary message to be appended to
205 # the given message. If remote is invalid we will die
206 # later in the common codepath so we discard the error
208 merge_name=$(for remote
211 done | git-fmt-merge-msg
213 merge_msg="${merge_msg:+$merge_msg$LF$LF}$merge_name"
215 head=$(git-rev-parse --verify "$head_arg"^0) || usage
217 # All the rest are remote heads
218 test "$#" = 0 && usage ;# we need at least one remote head.
219 set_reflog_action "merge $*"
224 remotehead=$(git-rev-parse --verify "$remote"^0 2>/dev/null) ||
225 die "$remote - not something we can merge"
226 remoteheads="${remoteheads}$remotehead "
227 eval GITHEAD_$remotehead='"$remote"'
228 export GITHEAD_$remotehead
230 set x $remoteheads ; shift
232 case "$use_strategies" in
236 var="`git-config --get pull.twohead`"
239 use_strategies="$var"
241 use_strategies="$default_twohead_strategies"
244 var="`git-config --get pull.octopus`"
247 use_strategies="$var"
249 use_strategies="$default_octopus_strategies"
255 for s in $use_strategies
257 for nt in $no_trivial_merge_strategies
270 common=$(git-merge-base --all $head "$@")
273 common=$(git-show-branch --merge-base $head "$@")
276 echo "$head" >"$GIT_DIR/ORIG_HEAD"
278 case "$index_merge,$#,$common,$no_commit" in
280 # We've been told not to try anything clever. Skip to real merge.
283 # No common ancestors found. We need a real merge.
286 # If head can reach all the merge then we are up to date.
287 # but first the most common case of merging one remote.
288 finish_up_to_date "Already up-to-date."
292 # Again the most common case of merging one remote.
293 echo "Updating $(git-rev-parse --short $head)..$(git-rev-parse --short $1)"
294 git-update-index --refresh 2>/dev/null
295 new_head=$(git-rev-parse --verify "$1^0") &&
296 git-read-tree -v -m -u --exclude-per-directory=.gitignore $head "$new_head" &&
297 finish "$new_head" "Fast forward" || exit
302 # We are not doing octopus and not fast forward. Need a
306 # We are not doing octopus, not fast forward, and have only
308 git-update-index --refresh 2>/dev/null
309 case " $use_strategies " in
310 *' recursive '*|*' recur '*)
314 # See if it is really trivial.
315 git var GIT_COMMITTER_IDENT >/dev/null || exit
316 echo "Trying really trivial in-index merge..."
317 if git-read-tree --trivial -m -u -v $common $head "$1" &&
318 result_tree=$(git-write-tree)
323 git-commit-tree $result_tree -p HEAD -p "$1"
325 finish "$result_commit" "In-index merge"
333 # An octopus. If we can reach all the remote we are up to date.
337 common_one=$(git-merge-base --all $head $remote)
338 if test "$common_one" != "$remote"
344 if test "$up_to_date" = t
346 finish_up_to_date "Already up-to-date. Yeeah!"
352 # We are going to make a new commit.
353 git var GIT_COMMITTER_IDENT >/dev/null || exit
355 # At this point, we need a real merge. No matter what strategy
356 # we use, it would operate on the index, possibly affecting the
357 # working tree, and when resolved cleanly, have the desired tree
358 # in the index -- this means that the index must be in sync with
359 # the $head commit. The strategies are responsible to ensure this.
361 case "$use_strategies" in
363 # Stash away the local changes so that we can try more than one.
368 rm -f "$GIT_DIR/MERGE_SAVE"
373 result_tree= best_cnt=-1 best_strategy= wt_strategy=
375 for strategy in $use_strategies
377 test "$wt_strategy" = '' || {
378 echo "Rewinding the tree to pristine..."
381 case "$single_strategy" in
383 echo "Trying merge strategy $strategy..."
387 # Remember which strategy left the state in the working tree
388 wt_strategy=$strategy
390 git-merge-$strategy $common -- "$head_arg" "$@"
392 if test "$no_commit" = t && test "$exit" = 0
395 exit=1 ;# pretend it left conflicts.
398 test "$exit" = 0 || {
400 # The backend exits with 1 when conflicts are left to be resolved,
401 # with 2 when it does not handle the given merge at all.
403 if test "$exit" -eq 1
406 git-diff-files --name-only
407 git-ls-files --unmerged
409 if test $best_cnt -le 0 -o $cnt -le $best_cnt
411 best_strategy=$strategy
418 # Automerge succeeded.
419 result_tree=$(git-write-tree) && break
422 # If we have a resulting tree, that means the strategy module
423 # auto resolved the merge cleanly.
424 if test '' != "$result_tree"
426 parents=$(git-show-branch --independent "$head" "$@" | sed -e 's/^/-p /')
427 result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents) || exit
428 finish "$result_commit" "Merge made by $wt_strategy."
433 # Pick the result from the best strategy and have the user fix it up.
434 case "$best_strategy" in
437 case "$use_strategies" in
439 echo >&2 "No merge strategy handled the merge."
442 echo >&2 "Merge with strategy $use_strategies failed."
448 # We already have its result in the working tree.
451 echo "Rewinding the tree to pristine..."
453 echo "Using the $best_strategy to prepare resolving by hand."
454 git-merge-$best_strategy $common -- "$head_arg" "$@"
458 if test "$squash" = t
465 done >"$GIT_DIR/MERGE_HEAD"
466 echo "$merge_msg" >"$GIT_DIR/MERGE_MSG"
469 if test "$merge_was_ok" = t
472 "Automatic merge went well; stopped before committing as requested"
479 git ls-files --unmerged |
480 sed -e 's/^[^ ]* / /' |
482 } >>"$GIT_DIR/MERGE_MSG"
483 if test -d "$GIT_DIR/rr-cache"
487 die "Automatic merge failed; fix conflicts and then commit the result."