3 # Copyright (c) 2005 Junio C Hamano
6 USAGE
='[-n] [--no-commit] [--squash] [-s <strategy>]... <merge-message> <head> <remote>+'
12 all_strategies
='recur recursive recursive-old 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
='recur recursive resolve octopus stupid ours'
24 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
25 "$GIT_DIR/MERGE_SAVE" ||
exit 1
29 # Stash away any local modifications.
30 git-diff-index
-z --name-only $head |
31 cpio -0 -o >"$GIT_DIR/MERGE_SAVE"
35 if test -f "$GIT_DIR/MERGE_SAVE"
37 git
reset --hard $head
38 cpio -iuv <"$GIT_DIR/MERGE_SAVE"
39 git-update-index
--refresh >/dev
/null
43 finish_up_to_date
() {
46 echo "$1 (nothing to squash)" ;;
54 echo Squashed commit of the following
:
56 git-log
--no-merges ^
"$head" $remote
69 echo "Squash commit -- not updating HEAD"
70 squash_message
>"$GIT_DIR/SQUASH_MSG"
75 echo "No merge message -- not updating HEAD"
78 git-update-ref
-m "$rlogm" HEAD
"$1" "$head" ||
exit 1
89 git-diff-tree
--stat --summary -M "$head" "$1"
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-[^=]*=\(.*\)'`
138 head=$
(git-rev-parse
--verify "$1"^
0) || usage
141 # All the rest are remote heads
142 test "$#" = 0 && usage
;# we need at least one remote head.
143 test "$rloga" = '' && rloga
="merge: $@"
148 remotehead
=$
(git-rev-parse
--verify "$remote"^
0) ||
149 die
"$remote - not something we can merge"
150 remoteheads
="${remoteheads}$remotehead "
152 set x
$remoteheads ; shift
154 case "$use_strategies" in
158 use_strategies
="$default_twohead_strategies" ;;
160 use_strategies
="$default_octopus_strategies" ;;
165 for s
in $use_strategies
168 *" $no_trivial_merge_strategies "*)
177 common
=$
(git-merge-base
--all $head "$@")
180 common
=$
(git-show-branch
--merge-base $head "$@")
183 echo "$head" >"$GIT_DIR/ORIG_HEAD"
185 case "$index_merge,$#,$common,$no_commit" in
187 # We've been told not to try anything clever. Skip to real merge.
190 # No common ancestors found. We need a real merge.
193 # If head can reach all the merge then we are up to date.
194 # but first the most common case of merging one remote.
195 finish_up_to_date
"Already up-to-date."
199 # Again the most common case of merging one remote.
200 echo "Updating $(git-rev-parse --short $head)..$(git-rev-parse --short $1)"
201 git-update-index
--refresh 2>/dev
/null
202 new_head
=$
(git-rev-parse
--verify "$1^0") &&
203 git-read-tree
-u -v -m $head "$new_head" &&
204 finish
"$new_head" "Fast forward"
209 # We are not doing octopus and not fast forward. Need a
213 # We are not doing octopus, not fast forward, and have only
214 # one common. See if it is really trivial.
215 git var GIT_COMMITTER_IDENT
>/dev
/null ||
exit
217 echo "Trying really trivial in-index merge..."
218 git-update-index
--refresh 2>/dev
/null
219 if git-read-tree
--trivial -m -u -v $common $head "$1" &&
220 result_tree
=$
(git-write-tree
)
225 git-commit-tree
$result_tree -p HEAD
-p "$1"
227 finish
"$result_commit" "In-index merge"
234 # An octopus. If we can reach all the remote we are up to date.
238 common_one
=$
(git-merge-base
--all $head $remote)
239 if test "$common_one" != "$remote"
245 if test "$up_to_date" = t
247 finish_up_to_date
"Already up-to-date. Yeeah!"
253 # We are going to make a new commit.
254 git var GIT_COMMITTER_IDENT
>/dev
/null ||
exit
256 # At this point, we need a real merge. No matter what strategy
257 # we use, it would operate on the index, possibly affecting the
258 # working tree, and when resolved cleanly, have the desired tree
259 # in the index -- this means that the index must be in sync with
260 # the $head commit. The strategies are responsible to ensure this.
262 case "$use_strategies" in
264 # Stash away the local changes so that we can try more than one.
269 rm -f "$GIT_DIR/MERGE_SAVE"
274 result_tree
= best_cnt
=-1 best_strategy
= wt_strategy
=
276 for strategy
in $use_strategies
278 test "$wt_strategy" = '' ||
{
279 echo "Rewinding the tree to pristine..."
282 case "$single_strategy" in
284 echo "Trying merge strategy $strategy..."
288 # Remember which strategy left the state in the working tree
289 wt_strategy
=$strategy
291 git-merge-
$strategy $common -- "$head_arg" "$@"
293 if test "$no_commit" = t
&& test "$exit" = 0
296 exit=1 ;# pretend it left conflicts.
299 test "$exit" = 0 ||
{
301 # The backend exits with 1 when conflicts are left to be resolved,
302 # with 2 when it does not handle the given merge at all.
304 if test "$exit" -eq 1
307 git-diff-files --name-only
308 git-ls-files --unmerged
310 if test $best_cnt -le 0 -o $cnt -le $best_cnt
312 best_strategy
=$strategy
319 # Automerge succeeded.
320 result_tree
=$
(git-write-tree
) && break
323 # If we have a resulting tree, that means the strategy module
324 # auto resolved the merge cleanly.
325 if test '' != "$result_tree"
327 parents
=$
(git-show-branch
--independent "$head" "$@" |
sed -e 's/^/-p /')
328 result_commit
=$
(echo "$merge_msg" | git-commit-tree
$result_tree $parents) ||
exit
329 finish
"$result_commit" "Merge made by $wt_strategy."
334 # Pick the result from the best strategy and have the user fix it up.
335 case "$best_strategy" in
338 echo >&2 "No merge strategy handled the merge."
342 # We already have its result in the working tree.
345 echo "Rewinding the tree to pristine..."
347 echo "Using the $best_strategy to prepare resolving by hand."
348 git-merge-
$best_strategy $common -- "$head_arg" "$@"
352 if test "$squash" = t
359 done >"$GIT_DIR/MERGE_HEAD"
360 echo "$merge_msg" >"$GIT_DIR/MERGE_MSG"
363 if test "$merge_was_ok" = t
366 "Automatic merge went well; stopped before committing as requested"
373 git ls-files
--unmerged |
374 sed -e 's/^[^ ]* / /' |
376 } >>"$GIT_DIR/MERGE_MSG"
377 if test -d "$GIT_DIR/rr-cache"
381 die
"Automatic merge failed; fix conflicts and then commit the result."