3 # Copyright (c) 2005 Junio C Hamano
6 USAGE
='[-n] [--summary] [--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 subtree'
20 default_twohead_strategies
='recursive'
21 default_octopus_strategies
='octopus'
22 no_trivial_merge_strategies
='ours subtree'
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
91 if test "$show_diffstat" = t
93 # We want color (if set), but no pager
94 GIT_PAGER
='' git
diff --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 ."
111 elif test "$remote" = "FETCH_HEAD" -a -r "$GIT_DIR/FETCH_HEAD"
113 sed -e 's/ not-for-merge / /' -e 1q \
114 "$GIT_DIR/FETCH_HEAD"
116 echo "$rh commit '$remote'"
120 case "$#" in 0) usage
;; esac
123 while case "$#" in 0) break ;; esac
126 -n|
--n|
--no|
--no-|
--no-s|
--no-su|
--no-sum|
--no-summ|\
127 --no-summa|
--no-summar|
--no-summary)
128 show_diffstat
=false
;;
131 --sq|
--squ|
--squa|
--squas|
--squash)
132 squash
=t no_commit
=t
;;
133 --no-c|
--no-co|
--no-com|
--no-comm|
--no-commi|
--no-commit)
135 -s=*|
--s=*|
--st=*|
--str=*|
--stra=*|
--strat=*|
--strate=*|\
136 --strateg=*|
--strategy=*|\
137 -s|
--s|
--st|
--str|
--stra|
--strat|
--strate|
--strateg|
--strategy)
140 strategy
=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
147 case " $all_strategies " in
149 use_strategies
="$use_strategies$strategy " ;;
151 die
"available strategies are: $all_strategies" ;;
154 -m=*|
--m=*|
--me=*|
--mes=*|
--mess=*|
--messa=*|
--messag=*|
--message=*)
155 merge_msg
=`expr "z$1" : 'z-[^=]*=\(.*\)'`
158 -m|
--m|
--me|
--mes|
--mess|
--messa|
--messag|
--message)
172 if test -z "$show_diffstat"; then
173 test "$(git config --bool merge.diffstat)" = false
&& show_diffstat
=false
174 test -z "$show_diffstat" && show_diffstat
=t
177 # This could be traditional "merge <msg> HEAD <commit>..." and the
178 # way we can tell it is to see if the second token is HEAD, but some
179 # people might have misused the interface and used a committish that
180 # is the same as HEAD there instead. Traditional format never would
181 # have "-m" so it is an additional safety measure to check for it.
183 if test -z "$have_message" &&
184 second_token
=$
(git rev-parse
--verify "$2^0" 2>/dev
/null
) &&
185 head_commit
=$
(git rev-parse
--verify "HEAD" 2>/dev
/null
) &&
186 test "$second_token" = "$head_commit"
192 elif ! git rev-parse
--verify HEAD
>/dev
/null
2>&1
194 # If the merged head is a valid one there is no reason to
195 # forbid "git merge" into a branch yet to be born. We do
196 # the same for "git pull".
199 echo >&2 "Can merge only exactly one commit into empty head"
203 rh
=$
(git rev-parse
--verify "$1^0") ||
204 die
"$1 - not something we can merge"
206 git update-ref
-m "initial pull" HEAD
"$rh" "" &&
207 git read-tree
--reset -u HEAD
211 # We are invoked directly as the first-class UI.
214 # All the rest are the commits being merged; prepare
215 # the standard merge summary message to be appended to
216 # the given message. If remote is invalid we will die
217 # later in the common codepath so we discard the error
219 merge_name
=$
(for remote
222 done | git fmt-merge-msg
224 merge_msg
="${merge_msg:+$merge_msg$LF$LF}$merge_name"
226 head=$
(git rev-parse
--verify "$head_arg"^
0) || usage
228 # All the rest are remote heads
229 test "$#" = 0 && usage
;# we need at least one remote head.
230 set_reflog_action
"merge $*"
235 remotehead
=$
(git rev-parse
--verify "$remote"^
0 2>/dev
/null
) ||
236 die
"$remote - not something we can merge"
237 remoteheads
="${remoteheads}$remotehead "
238 eval GITHEAD_
$remotehead='"$remote"'
239 export GITHEAD_
$remotehead
241 set x
$remoteheads ; shift
243 case "$use_strategies" in
247 var
="`git config --get pull.twohead`"
250 use_strategies
="$var"
252 use_strategies
="$default_twohead_strategies"
255 var
="`git config --get pull.octopus`"
258 use_strategies
="$var"
260 use_strategies
="$default_octopus_strategies"
266 for s
in $use_strategies
268 for nt
in $no_trivial_merge_strategies
281 common
=$
(git merge-base
--all $head "$@")
284 common
=$
(git show-branch
--merge-base $head "$@")
287 echo "$head" >"$GIT_DIR/ORIG_HEAD"
289 case "$index_merge,$#,$common,$no_commit" in
291 # We've been told not to try anything clever. Skip to real merge.
294 # No common ancestors found. We need a real merge.
297 # If head can reach all the merge then we are up to date.
298 # but first the most common case of merging one remote.
299 finish_up_to_date
"Already up-to-date."
303 # Again the most common case of merging one remote.
304 echo "Updating $(git rev-parse --short $head)..$(git rev-parse --short $1)"
305 git update-index
--refresh 2>/dev
/null
307 if test -n "$have_message"
309 msg
="$msg (no commit created; -m option ignored)"
311 new_head
=$
(git rev-parse
--verify "$1^0") &&
312 git read-tree
-v -m -u --exclude-per-directory=.gitignore
$head "$new_head" &&
313 finish
"$new_head" "$msg" ||
exit
318 # We are not doing octopus and not fast forward. Need a
322 # We are not doing octopus, not fast forward, and have only
324 git update-index
--refresh 2>/dev
/null
325 case " $use_strategies " in
326 *' recursive '*|
*' recur '*)
330 # See if it is really trivial.
331 git var GIT_COMMITTER_IDENT
>/dev
/null ||
exit
332 echo "Trying really trivial in-index merge..."
333 if git read-tree
--trivial -m -u -v $common $head "$1" &&
334 result_tree
=$
(git write-tree
)
338 printf '%s\n' "$merge_msg" |
339 git commit-tree
$result_tree -p HEAD
-p "$1"
341 finish
"$result_commit" "In-index merge"
349 # An octopus. If we can reach all the remote we are up to date.
353 common_one
=$
(git merge-base
--all $head $remote)
354 if test "$common_one" != "$remote"
360 if test "$up_to_date" = t
362 finish_up_to_date
"Already up-to-date. Yeeah!"
368 # We are going to make a new commit.
369 git var GIT_COMMITTER_IDENT
>/dev
/null ||
exit
371 # At this point, we need a real merge. No matter what strategy
372 # we use, it would operate on the index, possibly affecting the
373 # working tree, and when resolved cleanly, have the desired tree
374 # in the index -- this means that the index must be in sync with
375 # the $head commit. The strategies are responsible to ensure this.
377 case "$use_strategies" in
379 # Stash away the local changes so that we can try more than one.
384 rm -f "$GIT_DIR/MERGE_SAVE"
389 result_tree
= best_cnt
=-1 best_strategy
= wt_strategy
=
391 for strategy
in $use_strategies
393 test "$wt_strategy" = '' ||
{
394 echo "Rewinding the tree to pristine..."
397 case "$single_strategy" in
399 echo "Trying merge strategy $strategy..."
403 # Remember which strategy left the state in the working tree
404 wt_strategy
=$strategy
406 git-merge-
$strategy $common -- "$head_arg" "$@"
408 if test "$no_commit" = t
&& test "$exit" = 0
411 exit=1 ;# pretend it left conflicts.
414 test "$exit" = 0 ||
{
416 # The backend exits with 1 when conflicts are left to be resolved,
417 # with 2 when it does not handle the given merge at all.
419 if test "$exit" -eq 1
422 git diff-files --name-only
423 git ls-files --unmerged
425 if test $best_cnt -le 0 -o $cnt -le $best_cnt
427 best_strategy
=$strategy
434 # Automerge succeeded.
435 result_tree
=$
(git write-tree
) && break
438 # If we have a resulting tree, that means the strategy module
439 # auto resolved the merge cleanly.
440 if test '' != "$result_tree"
442 parents
=$
(git show-branch
--independent "$head" "$@" |
sed -e 's/^/-p /')
443 result_commit
=$
(printf '%s\n' "$merge_msg" | git commit-tree
$result_tree $parents) ||
exit
444 finish
"$result_commit" "Merge made by $wt_strategy."
449 # Pick the result from the best strategy and have the user fix it up.
450 case "$best_strategy" in
453 case "$use_strategies" in
455 echo >&2 "No merge strategy handled the merge."
458 echo >&2 "Merge with strategy $use_strategies failed."
464 # We already have its result in the working tree.
467 echo "Rewinding the tree to pristine..."
469 echo "Using the $best_strategy to prepare resolving by hand."
470 git-merge-
$best_strategy $common -- "$head_arg" "$@"
474 if test "$squash" = t
481 done >"$GIT_DIR/MERGE_HEAD"
482 printf '%s\n' "$merge_msg" >"$GIT_DIR/MERGE_MSG"
485 if test "$merge_was_ok" = t
488 "Automatic merge went well; stopped before committing as requested"
495 git ls-files
--unmerged |
496 sed -e 's/^[^ ]* / /' |
498 } >>"$GIT_DIR/MERGE_MSG"
499 if test -d "$GIT_DIR/rr-cache"
503 die
"Automatic merge failed; fix conflicts and then commit the result."