3 # Copyright (c) 2005 Junio C Hamano
6 USAGE='[-n] [--no-commit] [--squash] [-s <strategy>] [-m=<merge-message>] <commit>+'
9 set_reflog_action "merge $*"
12 test -z "$(git ls-files -u)" ||
13 die "You are in a middle of conflicted merge."
18 all_strategies='recur recursive octopus resolve stupid ours'
19 default_twohead_strategies='recursive'
20 default_octopus_strategies='octopus'
21 no_trivial_merge_strategies='ours'
27 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
28 "$GIT_DIR/MERGE_SAVE" || exit 1
32 # Stash away any local modifications.
33 git-diff-index -z --name-only $head |
34 cpio -0 -o >"$GIT_DIR/MERGE_SAVE"
38 if test -f "$GIT_DIR/MERGE_SAVE"
40 git reset --hard $head >/dev/null
41 cpio -iuv <"$GIT_DIR/MERGE_SAVE"
42 git-update-index --refresh >/dev/null
46 finish_up_to_date () {
49 echo "$1 (nothing to squash)" ;;
57 echo Squashed commit of the following:
59 git-log --no-merges ^"$head" $remote
65 rlogm="$GIT_REFLOG_ACTION"
68 rlogm="$GIT_REFLOG_ACTION: $2"
72 echo "Squash commit -- not updating HEAD"
73 squash_message >"$GIT_DIR/SQUASH_MSG"
78 echo "No merge message -- not updating HEAD"
81 git-update-ref -m "$rlogm" HEAD "$1" "$head" || exit 1
92 git-diff-tree --stat --summary -M "$head" "$1"
101 rh=$(git-rev-parse --verify "$remote^0" 2>/dev/null) || return
102 bh=$(git-show-ref -s --verify "refs/heads/$remote" 2>/dev/null)
103 if test "$rh" = "$bh"
105 echo "$rh branch '$remote' of ."
106 elif truname=$(expr "$remote" : '\(.*\)~[1-9][0-9]*$') &&
107 git-show-ref -q --verify "refs/heads/$truname" 2>/dev/null
109 echo "$rh branch '$truname' (early part) of ."
111 echo "$rh commit '$remote'"
115 case "$#" in 0) usage ;; esac
118 while case "$#" in 0) break ;; esac
121 -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\
122 --no-summa|--no-summar|--no-summary)
124 --sq|--squ|--squa|--squas|--squash)
125 squash=t no_commit=t ;;
126 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
128 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
129 --strateg=*|--strategy=*|\
130 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
133 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
140 case " $all_strategies " in
142 use_strategies="$use_strategies$strategy " ;;
144 die "available strategies are: $all_strategies" ;;
147 -m=*|--m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
148 merge_msg=`expr "z$1" : 'z-[^=]*=\(.*\)'`
151 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
165 # This could be traditional "merge <msg> HEAD <commit>..." and the
166 # way we can tell it is to see if the second token is HEAD, but some
167 # people might have misused the interface and used a committish that
168 # is the same as HEAD there instead. Traditional format never would
169 # have "-m" so it is an additional safety measure to check for it.
171 if test -z "$have_message" &&
172 second_token=$(git-rev-parse --verify "$2^0" 2>/dev/null) &&
173 head_commit=$(git-rev-parse --verify "HEAD" 2>/dev/null) &&
174 test "$second_token" = "$head_commit"
180 elif ! git-rev-parse --verify HEAD >/dev/null 2>&1
182 # If the merged head is a valid one there is no reason to
183 # forbid "git merge" into a branch yet to be born. We do
184 # the same for "git pull".
187 echo >&2 "Can merge only exactly one commit into empty head"
191 rh=$(git rev-parse --verify "$1^0") ||
192 die "$1 - not something we can merge"
194 git-update-ref -m "initial pull" HEAD "$rh" "" &&
195 git-read-tree --reset -u HEAD
199 # We are invoked directly as the first-class UI.
202 # All the rest are the commits being merged; prepare
203 # the standard merge summary message to be appended to
204 # the given message. If remote is invalid we will die
205 # later in the common codepath so we discard the error
207 merge_name=$(for remote
210 done | git-fmt-merge-msg
212 merge_msg="${merge_msg:+$merge_msg$LF$LF}$merge_name"
214 head=$(git-rev-parse --verify "$head_arg"^0) || usage
216 # All the rest are remote heads
217 test "$#" = 0 && usage ;# we need at least one remote head.
222 remotehead=$(git-rev-parse --verify "$remote"^0 2>/dev/null) ||
223 die "$remote - not something we can merge"
224 remoteheads="${remoteheads}$remotehead "
225 eval GITHEAD_$remotehead='"$remote"'
226 export GITHEAD_$remotehead
228 set x $remoteheads ; shift
230 case "$use_strategies" in
234 var="`git-repo-config --get pull.twohead`"
237 use_strategies="$var"
239 use_strategies="$default_twohead_strategies"
242 var="`git-repo-config --get pull.octopus`"
245 use_strategies="$var"
247 use_strategies="$default_octopus_strategies"
253 for s in $use_strategies
256 *" $no_trivial_merge_strategies "*)
265 common=$(git-merge-base --all $head "$@")
268 common=$(git-show-branch --merge-base $head "$@")
271 echo "$head" >"$GIT_DIR/ORIG_HEAD"
273 case "$index_merge,$#,$common,$no_commit" in
275 # We've been told not to try anything clever. Skip to real merge.
278 # No common ancestors found. We need a real merge.
281 # If head can reach all the merge then we are up to date.
282 # but first the most common case of merging one remote.
283 finish_up_to_date "Already up-to-date."
287 # Again the most common case of merging one remote.
288 echo "Updating $(git-rev-parse --short $head)..$(git-rev-parse --short $1)"
289 git-update-index --refresh 2>/dev/null
290 new_head=$(git-rev-parse --verify "$1^0") &&
291 git-read-tree -v -m -u --exclude-per-directory=.gitignore $head "$new_head" &&
292 finish "$new_head" "Fast forward"
297 # We are not doing octopus and not fast forward. Need a
301 # We are not doing octopus, not fast forward, and have only
302 # one common. See if it is really trivial.
303 git var GIT_COMMITTER_IDENT >/dev/null || exit
305 echo "Trying really trivial in-index merge..."
306 git-update-index --refresh 2>/dev/null
307 if git-read-tree --trivial -m -u -v $common $head "$1" &&
308 result_tree=$(git-write-tree)
313 git-commit-tree $result_tree -p HEAD -p "$1"
315 finish "$result_commit" "In-index merge"
322 # An octopus. If we can reach all the remote we are up to date.
326 common_one=$(git-merge-base --all $head $remote)
327 if test "$common_one" != "$remote"
333 if test "$up_to_date" = t
335 finish_up_to_date "Already up-to-date. Yeeah!"
341 # We are going to make a new commit.
342 git var GIT_COMMITTER_IDENT >/dev/null || exit
344 # At this point, we need a real merge. No matter what strategy
345 # we use, it would operate on the index, possibly affecting the
346 # working tree, and when resolved cleanly, have the desired tree
347 # in the index -- this means that the index must be in sync with
348 # the $head commit. The strategies are responsible to ensure this.
350 case "$use_strategies" in
352 # Stash away the local changes so that we can try more than one.
357 rm -f "$GIT_DIR/MERGE_SAVE"
362 result_tree= best_cnt=-1 best_strategy= wt_strategy=
364 for strategy in $use_strategies
366 test "$wt_strategy" = '' || {
367 echo "Rewinding the tree to pristine..."
370 case "$single_strategy" in
372 echo "Trying merge strategy $strategy..."
376 # Remember which strategy left the state in the working tree
377 wt_strategy=$strategy
379 git-merge-$strategy $common -- "$head_arg" "$@"
381 if test "$no_commit" = t && test "$exit" = 0
384 exit=1 ;# pretend it left conflicts.
387 test "$exit" = 0 || {
389 # The backend exits with 1 when conflicts are left to be resolved,
390 # with 2 when it does not handle the given merge at all.
392 if test "$exit" -eq 1
395 git-diff-files --name-only
396 git-ls-files --unmerged
398 if test $best_cnt -le 0 -o $cnt -le $best_cnt
400 best_strategy=$strategy
407 # Automerge succeeded.
408 result_tree=$(git-write-tree) && break
411 # If we have a resulting tree, that means the strategy module
412 # auto resolved the merge cleanly.
413 if test '' != "$result_tree"
415 parents=$(git-show-branch --independent "$head" "$@" | sed -e 's/^/-p /')
416 result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents) || exit
417 finish "$result_commit" "Merge made by $wt_strategy."
422 # Pick the result from the best strategy and have the user fix it up.
423 case "$best_strategy" in
426 case "$use_strategies" in
428 echo >&2 "No merge strategy handled the merge."
431 echo >&2 "Merge with strategy $use_strategies failed."
437 # We already have its result in the working tree.
440 echo "Rewinding the tree to pristine..."
442 echo "Using the $best_strategy to prepare resolving by hand."
443 git-merge-$best_strategy $common -- "$head_arg" "$@"
447 if test "$squash" = t
454 done >"$GIT_DIR/MERGE_HEAD"
455 echo "$merge_msg" >"$GIT_DIR/MERGE_MSG"
458 if test "$merge_was_ok" = t
461 "Automatic merge went well; stopped before committing as requested"
468 git ls-files --unmerged |
469 sed -e 's/^[^ ]* / /' |
471 } >>"$GIT_DIR/MERGE_MSG"
472 if test -d "$GIT_DIR/rr-cache"
476 die "Automatic merge failed; fix conflicts and then commit the result."