3 # Copyright (c) 2005 Junio C Hamano
6 USAGE
='[-n] [--no-commit] [--squash] [-s <strategy>] [--reflog-action=<action>] [-m=<merge-message>] <commit>+'
13 all_strategies
='recur recursive octopus resolve stupid ours'
14 default_twohead_strategies
='recursive'
15 default_octopus_strategies
='octopus'
16 no_trivial_merge_strategies
='ours'
22 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
23 "$GIT_DIR/MERGE_SAVE" ||
exit 1
27 # Stash away any local modifications.
28 git-diff-index
-z --name-only $head |
29 cpio -0 -o >"$GIT_DIR/MERGE_SAVE"
33 if test -f "$GIT_DIR/MERGE_SAVE"
35 git
reset --hard $head
36 cpio -iuv <"$GIT_DIR/MERGE_SAVE"
37 git-update-index
--refresh >/dev
/null
41 finish_up_to_date
() {
44 echo "$1 (nothing to squash)" ;;
52 echo Squashed commit of the following
:
54 git-log
--no-merges ^
"$head" $remote
67 echo "Squash commit -- not updating HEAD"
68 squash_message
>"$GIT_DIR/SQUASH_MSG"
73 echo "No merge message -- not updating HEAD"
76 git-update-ref
-m "$rlogm" HEAD
"$1" "$head" ||
exit 1
87 git-diff-tree
--stat --summary -M "$head" "$1"
94 case "$#" in 0) usage
;; esac
97 while case "$#" in 0) break ;; esac
100 -n|
--n|
--no|
--no-|
--no-s|
--no-su|
--no-sum|
--no-summ|\
101 --no-summa|
--no-summar|
--no-summary)
103 --sq|
--squ|
--squa|
--squas|
--squash)
104 squash
=t no_commit
=t
;;
105 --no-c|
--no-co|
--no-com|
--no-comm|
--no-commi|
--no-commit)
107 -s=*|
--s=*|
--st=*|
--str=*|
--stra=*|
--strat=*|
--strate=*|\
108 --strateg=*|
--strategy=*|\
109 -s|
--s|
--st|
--str|
--stra|
--strat|
--strate|
--strateg|
--strategy)
112 strategy
=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
119 case " $all_strategies " in
121 use_strategies
="$use_strategies$strategy " ;;
123 die
"available strategies are: $all_strategies" ;;
127 rloga
=`expr "z$1" : 'z-[^=]*=\(.*\)'`
129 -m=*|
--m=*|
--me=*|
--mes=*|
--mess=*|
--messa=*|
--messag=*|
--message=*)
130 merge_msg
=`expr "z$1" : 'z-[^=]*=\(.*\)'`
133 -m|
--m|
--me|
--mes|
--mess|
--messa|
--messag|
--message)
147 # This could be traditional "merge <msg> HEAD <commit>..." and the
148 # way we can tell it is to see if the second token is HEAD, but some
149 # people might have misused the interface and used a committish that
150 # is the same as HEAD there instead. Traditional format never would
151 # have "-m" so it is an additional safety measure to check for it.
153 if test -z "$have_message" &&
154 second_token
=$
(git-rev-parse
--verify "$2^0" 2>/dev
/null
) &&
155 head_commit
=$
(git-rev-parse
--verify "HEAD" 2>/dev
/null
) &&
156 test "$second_token" = "$head_commit"
162 elif ! git-rev-parse
--verify HEAD
>/dev
/null
2>&1
164 # If the merged head is a valid one there is no reason to
165 # forbid "git merge" into a branch yet to be born. We do
166 # the same for "git pull".
169 echo >&2 "Can merge only exactly one commit into empty head"
173 rh
=$
(git rev-parse
--verify "$1^0") ||
174 die
"$1 - not something we can merge"
176 git-update-ref
-m "initial pull" HEAD
"$rh" "" &&
177 git-read-tree
--reset -u HEAD
181 # We are invoked directly as the first-class UI.
184 # All the rest are the commits being merged; prepare
185 # the standard merge summary message to be appended to
186 # the given message. If remote is invalid we will die
187 # later in the common codepath so we discard the error
189 merge_name
=$
(for remote
191 rh
=$
(git-rev-parse
--verify "$remote"^
0 2>/dev
/null
) &&
192 bh
=$
(git show-ref
-s --verify "refs/heads/$remote") &&
193 if test "$rh" = "$bh"
195 echo "$rh branch '$remote' of ."
197 echo "$rh commit '$remote'"
199 done | git-fmt-merge-msg
201 merge_msg
="${merge_msg:+$merge_msg$LF$LF}$merge_name"
203 head=$
(git-rev-parse
--verify "$head_arg"^
0) || usage
205 # All the rest are remote heads
206 test "$#" = 0 && usage
;# we need at least one remote head.
207 test "$rloga" = '' && rloga
="merge: $@"
212 remotehead
=$
(git-rev-parse
--verify "$remote"^
0 2>/dev
/null
) ||
213 die
"$remote - not something we can merge"
214 remoteheads
="${remoteheads}$remotehead "
216 set x
$remoteheads ; shift
218 case "$use_strategies" in
222 use_strategies
="$default_twohead_strategies" ;;
224 use_strategies
="$default_octopus_strategies" ;;
229 for s
in $use_strategies
232 *" $no_trivial_merge_strategies "*)
241 common
=$
(git-merge-base
--all $head "$@")
244 common
=$
(git-show-branch
--merge-base $head "$@")
247 echo "$head" >"$GIT_DIR/ORIG_HEAD"
249 case "$index_merge,$#,$common,$no_commit" in
251 # We've been told not to try anything clever. Skip to real merge.
254 # No common ancestors found. We need a real merge.
257 # If head can reach all the merge then we are up to date.
258 # but first the most common case of merging one remote.
259 finish_up_to_date
"Already up-to-date."
263 # Again the most common case of merging one remote.
264 echo "Updating $(git-rev-parse --short $head)..$(git-rev-parse --short $1)"
265 git-update-index
--refresh 2>/dev
/null
266 new_head
=$
(git-rev-parse
--verify "$1^0") &&
267 git-read-tree
-u -v -m $head "$new_head" &&
268 finish
"$new_head" "Fast forward"
273 # We are not doing octopus and not fast forward. Need a
277 # We are not doing octopus, not fast forward, and have only
278 # one common. See if it is really trivial.
279 git var GIT_COMMITTER_IDENT
>/dev
/null ||
exit
281 echo "Trying really trivial in-index merge..."
282 git-update-index
--refresh 2>/dev
/null
283 if git-read-tree
--trivial -m -u -v $common $head "$1" &&
284 result_tree
=$
(git-write-tree
)
289 git-commit-tree
$result_tree -p HEAD
-p "$1"
291 finish
"$result_commit" "In-index merge"
298 # An octopus. If we can reach all the remote we are up to date.
302 common_one
=$
(git-merge-base
--all $head $remote)
303 if test "$common_one" != "$remote"
309 if test "$up_to_date" = t
311 finish_up_to_date
"Already up-to-date. Yeeah!"
317 # We are going to make a new commit.
318 git var GIT_COMMITTER_IDENT
>/dev
/null ||
exit
320 # At this point, we need a real merge. No matter what strategy
321 # we use, it would operate on the index, possibly affecting the
322 # working tree, and when resolved cleanly, have the desired tree
323 # in the index -- this means that the index must be in sync with
324 # the $head commit. The strategies are responsible to ensure this.
326 case "$use_strategies" in
328 # Stash away the local changes so that we can try more than one.
333 rm -f "$GIT_DIR/MERGE_SAVE"
338 result_tree
= best_cnt
=-1 best_strategy
= wt_strategy
=
340 for strategy
in $use_strategies
342 test "$wt_strategy" = '' ||
{
343 echo "Rewinding the tree to pristine..."
346 case "$single_strategy" in
348 echo "Trying merge strategy $strategy..."
352 # Remember which strategy left the state in the working tree
353 wt_strategy
=$strategy
355 git-merge-
$strategy $common -- "$head_arg" "$@"
357 if test "$no_commit" = t
&& test "$exit" = 0
360 exit=1 ;# pretend it left conflicts.
363 test "$exit" = 0 ||
{
365 # The backend exits with 1 when conflicts are left to be resolved,
366 # with 2 when it does not handle the given merge at all.
368 if test "$exit" -eq 1
371 git-diff-files --name-only
372 git-ls-files --unmerged
374 if test $best_cnt -le 0 -o $cnt -le $best_cnt
376 best_strategy
=$strategy
383 # Automerge succeeded.
384 result_tree
=$
(git-write-tree
) && break
387 # If we have a resulting tree, that means the strategy module
388 # auto resolved the merge cleanly.
389 if test '' != "$result_tree"
391 parents
=$
(git-show-branch
--independent "$head" "$@" |
sed -e 's/^/-p /')
392 result_commit
=$
(echo "$merge_msg" | git-commit-tree
$result_tree $parents) ||
exit
393 finish
"$result_commit" "Merge made by $wt_strategy."
398 # Pick the result from the best strategy and have the user fix it up.
399 case "$best_strategy" in
402 echo >&2 "No merge strategy handled the merge."
406 # We already have its result in the working tree.
409 echo "Rewinding the tree to pristine..."
411 echo "Using the $best_strategy to prepare resolving by hand."
412 git-merge-
$best_strategy $common -- "$head_arg" "$@"
416 if test "$squash" = t
423 done >"$GIT_DIR/MERGE_HEAD"
424 echo "$merge_msg" >"$GIT_DIR/MERGE_MSG"
427 if test "$merge_was_ok" = t
430 "Automatic merge went well; stopped before committing as requested"
437 git ls-files
--unmerged |
438 sed -e 's/^[^ ]* / /' |
440 } >>"$GIT_DIR/MERGE_MSG"
441 if test -d "$GIT_DIR/rr-cache"
445 die
"Automatic merge failed; fix conflicts and then commit the result."