3 # Copyright (c) 2005 Junio C Hamano
6 USAGE
='[-n] [--no-commit] [--squash] [-s <strategy>] [-m=<merge-message>] <commit>+'
9 set_reflog_action
"merge $*"
11 test -z "$(git ls-files -u)" ||
12 die
"You are in a middle of conflicted merge."
17 all_strategies
='recur recursive octopus resolve stupid ours'
18 default_twohead_strategies
='recursive'
19 default_octopus_strategies
='octopus'
20 no_trivial_merge_strategies
='ours'
26 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
27 "$GIT_DIR/MERGE_SAVE" ||
exit 1
31 # Stash away any local modifications.
32 git-diff-index
-z --name-only $head |
33 cpio -0 -o >"$GIT_DIR/MERGE_SAVE"
37 if test -f "$GIT_DIR/MERGE_SAVE"
39 git
reset --hard $head >/dev
/null
40 cpio -iuv <"$GIT_DIR/MERGE_SAVE"
41 git-update-index
--refresh >/dev
/null
45 finish_up_to_date
() {
48 echo "$1 (nothing to squash)" ;;
56 echo Squashed commit of the following
:
58 git-log
--no-merges ^
"$head" $remote
64 rlogm
="$GIT_REFLOG_ACTION"
67 rlogm
="$GIT_REFLOG_ACTION: $2"
71 echo "Squash commit -- not updating HEAD"
72 squash_message
>"$GIT_DIR/SQUASH_MSG"
77 echo "No merge message -- not updating HEAD"
80 git-update-ref
-m "$rlogm" HEAD
"$1" "$head" ||
exit 1
91 git-diff-tree
--stat --summary -M "$head" "$1"
100 rh
=$
(git-rev-parse
--verify "$remote^0" 2>/dev
/null
) ||
return
101 bh
=$
(git-show-ref
-s --verify "refs/heads/$remote" 2>/dev
/null
)
102 if test "$rh" = "$bh"
104 echo "$rh branch '$remote' of ."
105 elif truname
=$
(expr "$remote" : '\(.*\)~[1-9][0-9]*$') &&
106 git-show-ref
-q --verify "refs/heads/$truname" 2>/dev
/null
108 echo "$rh branch '$truname' (early part) of ."
110 echo "$rh commit '$remote'"
114 case "$#" in 0) usage
;; esac
117 while case "$#" in 0) break ;; esac
120 -n|
--n|
--no|
--no-|
--no-s|
--no-su|
--no-sum|
--no-summ|\
121 --no-summa|
--no-summar|
--no-summary)
123 --sq|
--squ|
--squa|
--squas|
--squash)
124 squash
=t no_commit
=t
;;
125 --no-c|
--no-co|
--no-com|
--no-comm|
--no-commi|
--no-commit)
127 -s=*|
--s=*|
--st=*|
--str=*|
--stra=*|
--strat=*|
--strate=*|\
128 --strateg=*|
--strategy=*|\
129 -s|
--s|
--st|
--str|
--stra|
--strat|
--strate|
--strateg|
--strategy)
132 strategy
=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
139 case " $all_strategies " in
141 use_strategies
="$use_strategies$strategy " ;;
143 die
"available strategies are: $all_strategies" ;;
146 -m=*|
--m=*|
--me=*|
--mes=*|
--mess=*|
--messa=*|
--messag=*|
--message=*)
147 merge_msg
=`expr "z$1" : 'z-[^=]*=\(.*\)'`
150 -m|
--m|
--me|
--mes|
--mess|
--messa|
--messag|
--message)
164 # This could be traditional "merge <msg> HEAD <commit>..." and the
165 # way we can tell it is to see if the second token is HEAD, but some
166 # people might have misused the interface and used a committish that
167 # is the same as HEAD there instead. Traditional format never would
168 # have "-m" so it is an additional safety measure to check for it.
170 if test -z "$have_message" &&
171 second_token
=$
(git-rev-parse
--verify "$2^0" 2>/dev
/null
) &&
172 head_commit
=$
(git-rev-parse
--verify "HEAD" 2>/dev
/null
) &&
173 test "$second_token" = "$head_commit"
179 elif ! git-rev-parse
--verify HEAD
>/dev
/null
2>&1
181 # If the merged head is a valid one there is no reason to
182 # forbid "git merge" into a branch yet to be born. We do
183 # the same for "git pull".
186 echo >&2 "Can merge only exactly one commit into empty head"
190 rh
=$
(git rev-parse
--verify "$1^0") ||
191 die
"$1 - not something we can merge"
193 git-update-ref
-m "initial pull" HEAD
"$rh" "" &&
194 git-read-tree
--reset -u HEAD
198 # We are invoked directly as the first-class UI.
201 # All the rest are the commits being merged; prepare
202 # the standard merge summary message to be appended to
203 # the given message. If remote is invalid we will die
204 # later in the common codepath so we discard the error
206 merge_name
=$
(for remote
209 done | git-fmt-merge-msg
211 merge_msg
="${merge_msg:+$merge_msg$LF$LF}$merge_name"
213 head=$
(git-rev-parse
--verify "$head_arg"^
0) || usage
215 # All the rest are remote heads
216 test "$#" = 0 && usage
;# we need at least one remote head.
221 remotehead
=$
(git-rev-parse
--verify "$remote"^
0 2>/dev
/null
) ||
222 die
"$remote - not something we can merge"
223 remoteheads
="${remoteheads}$remotehead "
224 eval GITHEAD_
$remotehead='"$remote"'
225 export GITHEAD_
$remotehead
227 set x
$remoteheads ; shift
229 case "$use_strategies" in
233 var
="`git-repo-config --get pull.twohead`"
236 use_strategies
="$var"
238 use_strategies
="$default_twohead_strategies"
241 var
="`git-repo-config --get pull.octopus`"
244 use_strategies
="$var"
246 use_strategies
="$default_octopus_strategies"
252 for s
in $use_strategies
255 *" $no_trivial_merge_strategies "*)
264 common
=$
(git-merge-base
--all $head "$@")
267 common
=$
(git-show-branch
--merge-base $head "$@")
270 echo "$head" >"$GIT_DIR/ORIG_HEAD"
272 case "$index_merge,$#,$common,$no_commit" in
274 # We've been told not to try anything clever. Skip to real merge.
277 # No common ancestors found. We need a real merge.
280 # If head can reach all the merge then we are up to date.
281 # but first the most common case of merging one remote.
282 finish_up_to_date
"Already up-to-date."
286 # Again the most common case of merging one remote.
287 echo "Updating $(git-rev-parse --short $head)..$(git-rev-parse --short $1)"
288 git-update-index
--refresh 2>/dev
/null
289 new_head
=$
(git-rev-parse
--verify "$1^0") &&
290 git-read-tree
-v -m -u --exclude-per-directory=.gitignore
$head "$new_head" &&
291 finish
"$new_head" "Fast forward"
296 # We are not doing octopus and not fast forward. Need a
300 # We are not doing octopus, not fast forward, and have only
301 # one common. See if it is really trivial.
302 git var GIT_COMMITTER_IDENT
>/dev
/null ||
exit
304 echo "Trying really trivial in-index merge..."
305 git-update-index
--refresh 2>/dev
/null
306 if git-read-tree
--trivial -m -u -v $common $head "$1" &&
307 result_tree
=$
(git-write-tree
)
312 git-commit-tree
$result_tree -p HEAD
-p "$1"
314 finish
"$result_commit" "In-index merge"
321 # An octopus. If we can reach all the remote we are up to date.
325 common_one
=$
(git-merge-base
--all $head $remote)
326 if test "$common_one" != "$remote"
332 if test "$up_to_date" = t
334 finish_up_to_date
"Already up-to-date. Yeeah!"
340 # We are going to make a new commit.
341 git var GIT_COMMITTER_IDENT
>/dev
/null ||
exit
343 # At this point, we need a real merge. No matter what strategy
344 # we use, it would operate on the index, possibly affecting the
345 # working tree, and when resolved cleanly, have the desired tree
346 # in the index -- this means that the index must be in sync with
347 # the $head commit. The strategies are responsible to ensure this.
349 case "$use_strategies" in
351 # Stash away the local changes so that we can try more than one.
356 rm -f "$GIT_DIR/MERGE_SAVE"
361 result_tree
= best_cnt
=-1 best_strategy
= wt_strategy
=
363 for strategy
in $use_strategies
365 test "$wt_strategy" = '' ||
{
366 echo "Rewinding the tree to pristine..."
369 case "$single_strategy" in
371 echo "Trying merge strategy $strategy..."
375 # Remember which strategy left the state in the working tree
376 wt_strategy
=$strategy
378 git-merge-
$strategy $common -- "$head_arg" "$@"
380 if test "$no_commit" = t
&& test "$exit" = 0
383 exit=1 ;# pretend it left conflicts.
386 test "$exit" = 0 ||
{
388 # The backend exits with 1 when conflicts are left to be resolved,
389 # with 2 when it does not handle the given merge at all.
391 if test "$exit" -eq 1
394 git-diff-files --name-only
395 git-ls-files --unmerged
397 if test $best_cnt -le 0 -o $cnt -le $best_cnt
399 best_strategy
=$strategy
406 # Automerge succeeded.
407 result_tree
=$
(git-write-tree
) && break
410 # If we have a resulting tree, that means the strategy module
411 # auto resolved the merge cleanly.
412 if test '' != "$result_tree"
414 parents
=$
(git-show-branch
--independent "$head" "$@" |
sed -e 's/^/-p /')
415 result_commit
=$
(echo "$merge_msg" | git-commit-tree
$result_tree $parents) ||
exit
416 finish
"$result_commit" "Merge made by $wt_strategy."
421 # Pick the result from the best strategy and have the user fix it up.
422 case "$best_strategy" in
425 case "$use_strategies" in
427 echo >&2 "No merge strategy handled the merge."
430 echo >&2 "Merge with strategy $use_strategies failed."
436 # We already have its result in the working tree.
439 echo "Rewinding the tree to pristine..."
441 echo "Using the $best_strategy to prepare resolving by hand."
442 git-merge-
$best_strategy $common -- "$head_arg" "$@"
446 if test "$squash" = t
453 done >"$GIT_DIR/MERGE_HEAD"
454 echo "$merge_msg" >"$GIT_DIR/MERGE_MSG"
457 if test "$merge_was_ok" = t
460 "Automatic merge went well; stopped before committing as requested"
467 git ls-files
--unmerged |
468 sed -e 's/^[^ ]* / /' |
470 } >>"$GIT_DIR/MERGE_MSG"
471 if test -d "$GIT_DIR/rr-cache"
475 die
"Automatic merge failed; fix conflicts and then commit the result."