3 # Copyright (c) 2005 Junio C Hamano
6 USAGE='[-n] [--no-commit] [--squash] [-s <strategy>] [-m=<merge-message>] <commit>+'
9 set_reflog_action "merge $*"
14 all_strategies='recur recursive octopus resolve stupid ours'
15 default_twohead_strategies='recursive'
16 default_octopus_strategies='octopus'
17 no_trivial_merge_strategies='ours'
23 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
24 "$GIT_DIR/MERGE_SAVE" || exit 1
28 # Stash away any local modifications.
29 git-diff-index -z --name-only $head |
30 cpio -0 -o >"$GIT_DIR/MERGE_SAVE"
34 if test -f "$GIT_DIR/MERGE_SAVE"
36 git reset --hard $head >/dev/null
37 cpio -iuv <"$GIT_DIR/MERGE_SAVE"
38 git-update-index --refresh >/dev/null
42 finish_up_to_date () {
45 echo "$1 (nothing to squash)" ;;
53 echo Squashed commit of the following:
55 git-log --no-merges ^"$head" $remote
61 rlogm="$GIT_REFLOG_ACTION"
64 rlogm="$GIT_REFLOG_ACTION: $2"
68 echo "Squash commit -- not updating HEAD"
69 squash_message >"$GIT_DIR/SQUASH_MSG"
74 echo "No merge message -- not updating HEAD"
77 git-update-ref -m "$rlogm" HEAD "$1" "$head" || exit 1
88 git-diff-tree --stat --summary -M "$head" "$1"
97 rh=$(git-rev-parse --verify "$remote^0" 2>/dev/null) || return
98 bh=$(git-show-ref -s --verify "refs/heads/$remote" 2>/dev/null)
101 echo "$rh branch '$remote' of ."
102 elif truname=$(expr "$remote" : '\(.*\)~[1-9][0-9]*$') &&
103 git-show-ref -q --verify "refs/heads/$truname" 2>/dev/null
105 echo "$rh branch '$truname' (early part) of ."
107 echo "$rh commit '$remote'"
111 case "$#" in 0) usage ;; esac
114 while case "$#" in 0) break ;; esac
117 -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\
118 --no-summa|--no-summar|--no-summary)
120 --sq|--squ|--squa|--squas|--squash)
121 squash=t no_commit=t ;;
122 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
124 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
125 --strateg=*|--strategy=*|\
126 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
129 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
136 case " $all_strategies " in
138 use_strategies="$use_strategies$strategy " ;;
140 die "available strategies are: $all_strategies" ;;
143 -m=*|--m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
144 merge_msg=`expr "z$1" : 'z-[^=]*=\(.*\)'`
147 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
161 # This could be traditional "merge <msg> HEAD <commit>..." and the
162 # way we can tell it is to see if the second token is HEAD, but some
163 # people might have misused the interface and used a committish that
164 # is the same as HEAD there instead. Traditional format never would
165 # have "-m" so it is an additional safety measure to check for it.
167 if test -z "$have_message" &&
168 second_token=$(git-rev-parse --verify "$2^0" 2>/dev/null) &&
169 head_commit=$(git-rev-parse --verify "HEAD" 2>/dev/null) &&
170 test "$second_token" = "$head_commit"
176 elif ! git-rev-parse --verify HEAD >/dev/null 2>&1
178 # If the merged head is a valid one there is no reason to
179 # forbid "git merge" into a branch yet to be born. We do
180 # the same for "git pull".
183 echo >&2 "Can merge only exactly one commit into empty head"
187 rh=$(git rev-parse --verify "$1^0") ||
188 die "$1 - not something we can merge"
190 git-update-ref -m "initial pull" HEAD "$rh" "" &&
191 git-read-tree --reset -u HEAD
195 # We are invoked directly as the first-class UI.
198 # All the rest are the commits being merged; prepare
199 # the standard merge summary message to be appended to
200 # the given message. If remote is invalid we will die
201 # later in the common codepath so we discard the error
203 merge_name=$(for remote
206 done | git-fmt-merge-msg
208 merge_msg="${merge_msg:+$merge_msg$LF$LF}$merge_name"
210 head=$(git-rev-parse --verify "$head_arg"^0) || usage
212 # All the rest are remote heads
213 test "$#" = 0 && usage ;# we need at least one remote head.
218 remotehead=$(git-rev-parse --verify "$remote"^0 2>/dev/null) ||
219 die "$remote - not something we can merge"
220 remoteheads="${remoteheads}$remotehead "
221 eval GITHEAD_$remotehead='"$remote"'
222 export GITHEAD_$remotehead
224 set x $remoteheads ; shift
226 case "$use_strategies" in
230 var="`git-repo-config --get pull.twohead`"
233 use_strategies="$var"
235 use_strategies="$default_twohead_strategies"
238 var="`git-repo-config --get pull.octopus`"
241 use_strategies="$var"
243 use_strategies="$default_octopus_strategies"
249 for s in $use_strategies
252 *" $no_trivial_merge_strategies "*)
261 common=$(git-merge-base --all $head "$@")
264 common=$(git-show-branch --merge-base $head "$@")
267 echo "$head" >"$GIT_DIR/ORIG_HEAD"
269 case "$index_merge,$#,$common,$no_commit" in
271 # We've been told not to try anything clever. Skip to real merge.
274 # No common ancestors found. We need a real merge.
277 # If head can reach all the merge then we are up to date.
278 # but first the most common case of merging one remote.
279 finish_up_to_date "Already up-to-date."
283 # Again the most common case of merging one remote.
284 echo "Updating $(git-rev-parse --short $head)..$(git-rev-parse --short $1)"
285 git-update-index --refresh 2>/dev/null
286 new_head=$(git-rev-parse --verify "$1^0") &&
287 git-read-tree -v -m -u --exclude-per-directory=.gitignore $head "$new_head" &&
288 finish "$new_head" "Fast forward"
293 # We are not doing octopus and not fast forward. Need a
297 # We are not doing octopus, not fast forward, and have only
298 # one common. See if it is really trivial.
299 git var GIT_COMMITTER_IDENT >/dev/null || exit
301 echo "Trying really trivial in-index merge..."
302 git-update-index --refresh 2>/dev/null
303 if git-read-tree --trivial -m -u -v $common $head "$1" &&
304 result_tree=$(git-write-tree)
309 git-commit-tree $result_tree -p HEAD -p "$1"
311 finish "$result_commit" "In-index merge"
318 # An octopus. If we can reach all the remote we are up to date.
322 common_one=$(git-merge-base --all $head $remote)
323 if test "$common_one" != "$remote"
329 if test "$up_to_date" = t
331 finish_up_to_date "Already up-to-date. Yeeah!"
337 # We are going to make a new commit.
338 git var GIT_COMMITTER_IDENT >/dev/null || exit
340 # At this point, we need a real merge. No matter what strategy
341 # we use, it would operate on the index, possibly affecting the
342 # working tree, and when resolved cleanly, have the desired tree
343 # in the index -- this means that the index must be in sync with
344 # the $head commit. The strategies are responsible to ensure this.
346 case "$use_strategies" in
348 # Stash away the local changes so that we can try more than one.
353 rm -f "$GIT_DIR/MERGE_SAVE"
358 result_tree= best_cnt=-1 best_strategy= wt_strategy=
360 for strategy in $use_strategies
362 test "$wt_strategy" = '' || {
363 echo "Rewinding the tree to pristine..."
366 case "$single_strategy" in
368 echo "Trying merge strategy $strategy..."
372 # Remember which strategy left the state in the working tree
373 wt_strategy=$strategy
375 git-merge-$strategy $common -- "$head_arg" "$@"
377 if test "$no_commit" = t && test "$exit" = 0
380 exit=1 ;# pretend it left conflicts.
383 test "$exit" = 0 || {
385 # The backend exits with 1 when conflicts are left to be resolved,
386 # with 2 when it does not handle the given merge at all.
388 if test "$exit" -eq 1
391 git-diff-files --name-only
392 git-ls-files --unmerged
394 if test $best_cnt -le 0 -o $cnt -le $best_cnt
396 best_strategy=$strategy
403 # Automerge succeeded.
404 result_tree=$(git-write-tree) && break
407 # If we have a resulting tree, that means the strategy module
408 # auto resolved the merge cleanly.
409 if test '' != "$result_tree"
411 parents=$(git-show-branch --independent "$head" "$@" | sed -e 's/^/-p /')
412 result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents) || exit
413 finish "$result_commit" "Merge made by $wt_strategy."
418 # Pick the result from the best strategy and have the user fix it up.
419 case "$best_strategy" in
422 case "$use_strategies" in
424 echo >&2 "No merge strategy handled the merge."
427 echo >&2 "Merge with strategy $use_strategies failed."
433 # We already have its result in the working tree.
436 echo "Rewinding the tree to pristine..."
438 echo "Using the $best_strategy to prepare resolving by hand."
439 git-merge-$best_strategy $common -- "$head_arg" "$@"
443 if test "$squash" = t
450 done >"$GIT_DIR/MERGE_HEAD"
451 echo "$merge_msg" >"$GIT_DIR/MERGE_MSG"
454 if test "$merge_was_ok" = t
457 "Automatic merge went well; stopped before committing as requested"
464 git ls-files --unmerged |
465 sed -e 's/^[^ ]* / /' |
467 } >>"$GIT_DIR/MERGE_MSG"
468 if test -d "$GIT_DIR/rr-cache"
472 die "Automatic merge failed; fix conflicts and then commit the result."