3 # Copyright (c) 2005 Junio C Hamano
6 USAGE
='[-n] [--summary] [--[no-]commit] [--[no-]squash] [--[no-]ff] [-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 subtree'
20 default_twohead_strategies
='recursive'
21 default_octopus_strategies
='octopus'
22 no_fast_forward_strategies
='subtree ours'
23 no_trivial_strategies
='recursive recur subtree ours'
29 # Fix some commands on Windows
32 # there's no cpio; emulate with tar
36 tar --create --file=- --null --files-from=-
41 *) die
"internal error: unexpected cpio $*";;
48 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
49 "$GIT_DIR/MERGE_STASH" ||
exit 1
53 # Stash away any local modifications.
54 git stash create
>"$GIT_DIR/MERGE_STASH"
58 if test -f "$GIT_DIR/MERGE_STASH"
60 git
reset --hard $head >/dev
/null
61 git stash apply $
(cat "$GIT_DIR/MERGE_STASH")
62 git update-index
--refresh >/dev
/null
66 finish_up_to_date
() {
69 echo "$1 (nothing to squash)" ;;
77 echo Squashed commit of the following
:
79 git log
--no-merges ^
"$head" $remoteheads
85 rlogm
="$GIT_REFLOG_ACTION"
88 rlogm
="$GIT_REFLOG_ACTION: $2"
92 echo "Squash commit -- not updating HEAD"
93 squash_message
>"$GIT_DIR/SQUASH_MSG"
98 echo "No merge message -- not updating HEAD"
101 git update-ref
-m "$rlogm" HEAD
"$1" "$head" ||
exit 1
111 if test "$show_diffstat" = t
113 # We want color (if set), but no pager
114 GIT_PAGER
='' git
diff --stat --summary -M "$head" "$1"
119 # Run a post-merge hook
120 if test -x "$GIT_DIR"/hooks
/post-merge
124 "$GIT_DIR"/hooks
/post-merge
1
127 "$GIT_DIR"/hooks
/post-merge
0
135 rh
=$
(git rev-parse
--verify "$remote^0" 2>/dev
/null
) ||
return
136 bh
=$
(git show-ref
-s --verify "refs/heads/$remote" 2>/dev
/null
)
137 if test "$rh" = "$bh"
139 echo "$rh branch '$remote' of ."
140 elif truname
=$
(expr "$remote" : '\(.*\)~[1-9][0-9]*$') &&
141 git show-ref
-q --verify "refs/heads/$truname" 2>/dev
/null
143 echo "$rh branch '$truname' (early part) of ."
144 elif test "$remote" = "FETCH_HEAD" -a -r "$GIT_DIR/FETCH_HEAD"
146 sed -e 's/ not-for-merge / /' -e 1q \
147 "$GIT_DIR/FETCH_HEAD"
149 echo "$rh commit '$remote'"
155 -n|
--n|
--no|
--no-|
--no-s|
--no-su|
--no-sum|
--no-summ|\
156 --no-summa|
--no-summar|
--no-summary)
157 show_diffstat
=false
;;
160 --sq|
--squ|
--squa|
--squas|
--squash)
161 allow_fast_forward
=t squash
=t no_commit
=t
;;
162 --no-sq|
--no-squ|
--no-squa|
--no-squas|
--no-squash)
163 allow_fast_forward
=t squash
= no_commit
= ;;
164 --c|
--co|
--com|
--comm|
--commi|
--commit)
165 allow_fast_forward
=t squash
= no_commit
= ;;
166 --no-c|
--no-co|
--no-com|
--no-comm|
--no-commi|
--no-commit)
167 allow_fast_forward
=t squash
= no_commit
=t
;;
169 allow_fast_forward
=t squash
= no_commit
= ;;
171 allow_fast_forward
=false squash
= no_commit
= ;;
172 -s=*|
--s=*|
--st=*|
--str=*|
--stra=*|
--strat=*|
--strate=*|\
173 --strateg=*|
--strategy=*|\
174 -s|
--s|
--st|
--str|
--stra|
--strat|
--strate|
--strateg|
--strategy)
177 strategy
=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
184 case " $all_strategies " in
186 use_strategies
="$use_strategies$strategy " ;;
188 die
"available strategies are: $all_strategies" ;;
191 -m=*|
--m=*|
--me=*|
--mes=*|
--mess=*|
--messa=*|
--messag=*|
--message=*)
192 merge_msg
=`expr "z$1" : 'z-[^=]*=\(.*\)'`
195 -m|
--m|
--me|
--mes|
--mess|
--messa|
--messag|
--message)
213 parse_option
"$@" || usage
214 while test $args_left -lt $#
221 test $# != 0 || usage
225 if branch
=$
(git-symbolic-ref
-q HEAD
)
227 mergeopts
=$
(git config
"branch.${branch#refs/heads/}.mergeoptions")
228 if test -n "$mergeopts"
230 parse_config
$mergeopts
234 while parse_option
"$@"
236 while test $args_left -lt $#
242 if test -z "$show_diffstat"; then
243 test "$(git config --bool merge.diffstat)" = false
&& show_diffstat
=false
244 test -z "$show_diffstat" && show_diffstat
=t
247 # This could be traditional "merge <msg> HEAD <commit>..." and the
248 # way we can tell it is to see if the second token is HEAD, but some
249 # people might have misused the interface and used a committish that
250 # is the same as HEAD there instead. Traditional format never would
251 # have "-m" so it is an additional safety measure to check for it.
253 if test -z "$have_message" &&
254 second_token
=$
(git rev-parse
--verify "$2^0" 2>/dev
/null
) &&
255 head_commit
=$
(git rev-parse
--verify "HEAD" 2>/dev
/null
) &&
256 test "$second_token" = "$head_commit"
262 elif ! git rev-parse
--verify HEAD
>/dev
/null
2>&1
264 # If the merged head is a valid one there is no reason to
265 # forbid "git merge" into a branch yet to be born. We do
266 # the same for "git pull".
269 echo >&2 "Can merge only exactly one commit into empty head"
273 rh
=$
(git rev-parse
--verify "$1^0") ||
274 die
"$1 - not something we can merge"
276 git update-ref
-m "initial pull" HEAD
"$rh" "" &&
277 git read-tree
--reset -u HEAD
281 # We are invoked directly as the first-class UI.
284 # All the rest are the commits being merged; prepare
285 # the standard merge summary message to be appended to
286 # the given message. If remote is invalid we will die
287 # later in the common codepath so we discard the error
289 merge_name
=$
(for remote
292 done | git fmt-merge-msg
294 merge_msg
="${merge_msg:+$merge_msg$LF$LF}$merge_name"
296 head=$
(git rev-parse
--verify "$head_arg"^
0) || usage
298 # All the rest are remote heads
299 test "$#" = 0 && usage
;# we need at least one remote head.
300 set_reflog_action
"merge $*"
305 remotehead
=$
(git rev-parse
--verify "$remote"^
0 2>/dev
/null
) ||
306 die
"$remote - not something we can merge"
307 remoteheads
="${remoteheads}$remotehead "
308 eval GITHEAD_
$remotehead='"$remote"'
309 export GITHEAD_
$remotehead
311 set x
$remoteheads ; shift
313 case "$use_strategies" in
317 var
="`git config --get pull.twohead`"
320 use_strategies
="$var"
322 use_strategies
="$default_twohead_strategies"
325 var
="`git config --get pull.octopus`"
328 use_strategies
="$var"
330 use_strategies
="$default_octopus_strategies"
336 for s
in $use_strategies
338 for ss
in $no_fast_forward_strategies
347 for ss
in $no_trivial_strategies
351 allow_trivial_merge
=f
360 common
=$
(git merge-base
--all $head "$@")
363 common
=$
(git show-branch
--merge-base $head "$@")
366 echo "$head" >"$GIT_DIR/ORIG_HEAD"
368 case "$allow_fast_forward,$#,$common,$no_commit" in
370 # No common ancestors found. We need a real merge.
373 # If head can reach all the merge then we are up to date.
374 # but first the most common case of merging one remote.
375 finish_up_to_date
"Already up-to-date."
379 # Again the most common case of merging one remote.
380 echo "Updating $(git rev-parse --short $head)..$(git rev-parse --short $1)"
381 git update-index
--refresh 2>/dev
/null
383 if test -n "$have_message"
385 msg
="$msg (no commit created; -m option ignored)"
387 new_head
=$
(git rev-parse
--verify "$1^0") &&
388 git read-tree
-v -m -u --exclude-per-directory=.gitignore
$head "$new_head" &&
389 finish
"$new_head" "$msg" ||
exit
394 # We are not doing octopus and not fast forward. Need a
398 # We are not doing octopus, not fast forward, and have only
400 git update-index
--refresh 2>/dev
/null
401 case "$allow_trivial_merge" in
403 # See if it is really trivial.
404 git var GIT_COMMITTER_IDENT
>/dev
/null ||
exit
405 echo "Trying really trivial in-index merge..."
406 if git read-tree
--trivial -m -u -v $common $head "$1" &&
407 result_tree
=$
(git write-tree
)
411 printf '%s\n' "$merge_msg" |
412 git commit-tree
$result_tree -p HEAD
-p "$1"
414 finish
"$result_commit" "In-index merge"
422 # An octopus. If we can reach all the remote we are up to date.
426 common_one
=$
(git merge-base
--all $head $remote)
427 if test "$common_one" != "$remote"
433 if test "$up_to_date" = t
435 finish_up_to_date
"Already up-to-date. Yeeah!"
441 # We are going to make a new commit.
442 git var GIT_COMMITTER_IDENT
>/dev
/null ||
exit
444 # At this point, we need a real merge. No matter what strategy
445 # we use, it would operate on the index, possibly affecting the
446 # working tree, and when resolved cleanly, have the desired tree
447 # in the index -- this means that the index must be in sync with
448 # the $head commit. The strategies are responsible to ensure this.
450 case "$use_strategies" in
452 # Stash away the local changes so that we can try more than one.
457 rm -f "$GIT_DIR/MERGE_STASH"
462 result_tree
= best_cnt
=-1 best_strategy
= wt_strategy
=
464 for strategy
in $use_strategies
466 test "$wt_strategy" = '' ||
{
467 echo "Rewinding the tree to pristine..."
470 case "$single_strategy" in
472 echo "Trying merge strategy $strategy..."
476 # Remember which strategy left the state in the working tree
477 wt_strategy
=$strategy
479 git-merge-
$strategy $common -- "$head_arg" "$@"
481 if test "$no_commit" = t
&& test "$exit" = 0
484 exit=1 ;# pretend it left conflicts.
487 test "$exit" = 0 ||
{
489 # The backend exits with 1 when conflicts are left to be resolved,
490 # with 2 when it does not handle the given merge at all.
492 if test "$exit" -eq 1
495 git diff-files --name-only
496 git ls-files --unmerged
498 if test $best_cnt -le 0 -o $cnt -le $best_cnt
500 best_strategy
=$strategy
507 # Automerge succeeded.
508 result_tree
=$
(git write-tree
) && break
511 # If we have a resulting tree, that means the strategy module
512 # auto resolved the merge cleanly.
513 if test '' != "$result_tree"
515 if test "$allow_fast_forward" = "t"
517 parents
=$
(git show-branch
--independent "$head" "$@")
519 parents
=$
(git rev-parse
"$head" "$@")
521 parents
=$
(echo "$parents" |
sed -e 's/^/-p /')
522 result_commit
=$
(printf '%s\n' "$merge_msg" | git commit-tree
$result_tree $parents) ||
exit
523 finish
"$result_commit" "Merge made by $wt_strategy."
528 # Pick the result from the best strategy and have the user fix it up.
529 case "$best_strategy" in
532 case "$use_strategies" in
534 echo >&2 "No merge strategy handled the merge."
537 echo >&2 "Merge with strategy $use_strategies failed."
543 # We already have its result in the working tree.
546 echo "Rewinding the tree to pristine..."
548 echo "Using the $best_strategy to prepare resolving by hand."
549 git-merge-
$best_strategy $common -- "$head_arg" "$@"
553 if test "$squash" = t
560 done >"$GIT_DIR/MERGE_HEAD"
561 printf '%s\n' "$merge_msg" >"$GIT_DIR/MERGE_MSG"
564 if test "$merge_was_ok" = t
567 "Automatic merge went well; stopped before committing as requested"
574 git ls-files
--unmerged |
575 sed -e 's/^[^ ]* / /' |
577 } >>"$GIT_DIR/MERGE_MSG"
579 die
"Automatic merge failed; fix conflicts and then commit the result."