3 # Copyright (c) 2005 Junio C Hamano
6 USAGE
='[-n] [--no-commit] [--squash] [-s <strategy>]... <merge-message> <head> <remote>+'
12 all_strategies
='recursive octopus resolve stupid ours'
13 default_twohead_strategies
='recursive'
14 default_octopus_strategies
='octopus'
15 no_trivial_merge_strategies
='ours'
19 if test "@@NO_PYTHON@@"; then
20 all_strategies
='resolve octopus stupid ours'
21 default_twohead_strategies
='resolve'
25 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
26 "$GIT_DIR/MERGE_SAVE" ||
exit 1
30 # Stash away any local modifications.
31 git-diff-index
-z --name-only $head |
32 cpio -0 -o >"$GIT_DIR/MERGE_SAVE"
36 if test -f "$GIT_DIR/MERGE_SAVE"
38 git
reset --hard $head
39 cpio -iuv <"$GIT_DIR/MERGE_SAVE"
40 git-update-index
--refresh >/dev
/null
44 finish_up_to_date
() {
47 echo "$1 (nothing to squash)" ;;
55 echo Squashed commit of the following
:
57 git-log
--no-merges ^
"$head" $remote
61 test '' = "$2" ||
echo "$2"
64 echo "Squash commit -- not updating HEAD"
65 squash_message
>"$GIT_DIR/SQUASH_MSG"
70 echo "No merge message -- not updating HEAD"
73 git-update-ref HEAD
"$1" "$head" ||
exit 1
84 git-diff-tree
--stat --summary -M "$head" "$1"
91 while case "$#" in 0) break ;; esac
94 -n|
--n|
--no|
--no-|
--no-s|
--no-su|
--no-sum|
--no-summ|\
95 --no-summa|
--no-summar|
--no-summary)
97 --sq|
--squ|
--squa|
--squas|
--squash)
98 squash
=t no_commit
=t
;;
99 --no-c|
--no-co|
--no-com|
--no-comm|
--no-commi|
--no-commit)
101 -s=*|
--s=*|
--st=*|
--str=*|
--stra=*|
--strat=*|
--strate=*|\
102 --strateg=*|
--strategy=*|\
103 -s|
--s|
--st|
--str|
--stra|
--strat|
--strate|
--strateg|
--strategy)
106 strategy
=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
113 case " $all_strategies " in
115 use_strategies
="$use_strategies$strategy " ;;
117 die
"available strategies are: $all_strategies" ;;
129 head=$
(git-rev-parse
--verify "$1"^
0) || usage
132 # All the rest are remote heads
133 test "$#" = 0 && usage
;# we need at least one remote head.
138 remotehead
=$
(git-rev-parse
--verify "$remote"^
0) ||
139 die
"$remote - not something we can merge"
140 remoteheads
="${remoteheads}$remotehead "
142 set x
$remoteheads ; shift
144 case "$use_strategies" in
148 use_strategies
="$default_twohead_strategies" ;;
150 use_strategies
="$default_octopus_strategies" ;;
155 for s
in $use_strategies
158 *" $no_trivial_merge_strategies "*)
167 common
=$
(git-merge-base
--all $head "$@")
170 common
=$
(git-show-branch
--merge-base $head "$@")
173 echo "$head" >"$GIT_DIR/ORIG_HEAD"
175 case "$index_merge,$#,$common,$no_commit" in
177 # We've been told not to try anything clever. Skip to real merge.
180 # No common ancestors found. We need a real merge.
183 # If head can reach all the merge then we are up to date.
184 # but first the most common case of merging one remote.
185 finish_up_to_date
"Already up-to-date."
189 # Again the most common case of merging one remote.
190 echo "Updating from $head to $1"
191 git-update-index
--refresh 2>/dev
/null
192 new_head
=$
(git-rev-parse
--verify "$1^0") &&
193 git-read-tree
-u -v -m $head "$new_head" &&
194 finish
"$new_head" "Fast forward"
199 # We are not doing octopus and not fast forward. Need a
203 # We are not doing octopus, not fast forward, and have only
204 # one common. See if it is really trivial.
205 git var GIT_COMMITTER_IDENT
>/dev
/null ||
exit
207 echo "Trying really trivial in-index merge..."
208 git-update-index
--refresh 2>/dev
/null
209 if git-read-tree
--trivial -m -u -v $common $head "$1" &&
210 result_tree
=$
(git-write-tree
)
215 git-commit-tree
$result_tree -p HEAD
-p "$1"
217 finish
"$result_commit" "In-index merge"
224 # An octopus. If we can reach all the remote we are up to date.
228 common_one
=$
(git-merge-base
--all $head $remote)
229 if test "$common_one" != "$remote"
235 if test "$up_to_date" = t
237 finish_up_to_date
"Already up-to-date. Yeeah!"
243 # We are going to make a new commit.
244 git var GIT_COMMITTER_IDENT
>/dev
/null ||
exit
246 # At this point, we need a real merge. No matter what strategy
247 # we use, it would operate on the index, possibly affecting the
248 # working tree, and when resolved cleanly, have the desired tree
249 # in the index -- this means that the index must be in sync with
250 # the $head commit. The strategies are responsible to ensure this.
252 case "$use_strategies" in
254 # Stash away the local changes so that we can try more than one.
259 rm -f "$GIT_DIR/MERGE_SAVE"
264 result_tree
= best_cnt
=-1 best_strategy
= wt_strategy
=
266 for strategy
in $use_strategies
268 test "$wt_strategy" = '' ||
{
269 echo "Rewinding the tree to pristine..."
272 case "$single_strategy" in
274 echo "Trying merge strategy $strategy..."
278 # Remember which strategy left the state in the working tree
279 wt_strategy
=$strategy
281 git-merge-
$strategy $common -- "$head_arg" "$@"
283 if test "$no_commit" = t
&& test "$exit" = 0
286 exit=1 ;# pretend it left conflicts.
289 test "$exit" = 0 ||
{
291 # The backend exits with 1 when conflicts are left to be resolved,
292 # with 2 when it does not handle the given merge at all.
294 if test "$exit" -eq 1
297 git-diff-files --name-only
298 git-ls-files --unmerged
300 if test $best_cnt -le 0 -o $cnt -le $best_cnt
302 best_strategy
=$strategy
309 # Automerge succeeded.
310 result_tree
=$
(git-write-tree
) && break
313 # If we have a resulting tree, that means the strategy module
314 # auto resolved the merge cleanly.
315 if test '' != "$result_tree"
317 parents
=$
(git-show-branch
--independent "$head" "$@" |
sed -e 's/^/-p /')
318 result_commit
=$
(echo "$merge_msg" | git-commit-tree
$result_tree $parents) ||
exit
319 finish
"$result_commit" "Merge $result_commit, made by $wt_strategy."
324 # Pick the result from the best strategy and have the user fix it up.
325 case "$best_strategy" in
328 echo >&2 "No merge strategy handled the merge."
332 # We already have its result in the working tree.
335 echo "Rewinding the tree to pristine..."
337 echo "Using the $best_strategy to prepare resolving by hand."
338 git-merge-
$best_strategy $common -- "$head_arg" "$@"
342 if test "$squash" = t
349 done >"$GIT_DIR/MERGE_HEAD"
350 echo "$merge_msg" >"$GIT_DIR/MERGE_MSG"
353 if test "$merge_was_ok" = t
356 "Automatic merge went well; stopped before committing as requested"
363 git ls-files
--unmerged |
364 sed -e 's/^[^ ]* / /' |
366 } >>"$GIT_DIR/MERGE_MSG"
367 if test -d "$GIT_DIR/rr-cache"
371 die
"Automatic merge failed; fix conflicts and then commit the result."