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
70 echo "Squash commit -- not updating HEAD"
71 squash_message
>"$GIT_DIR/SQUASH_MSG"
76 echo "No merge message -- not updating HEAD"
79 git-update-ref
-m "$rlogm" HEAD
"$1" "$head" ||
exit 1
90 git-diff-tree
--stat --summary -M "$head" "$1"
98 while case "$#" in 0) break ;; esac
101 -n|
--n|
--no|
--no-|
--no-s|
--no-su|
--no-sum|
--no-summ|\
102 --no-summa|
--no-summar|
--no-summary)
104 --sq|
--squ|
--squa|
--squas|
--squash)
105 squash
=t no_commit
=t
;;
106 --no-c|
--no-co|
--no-com|
--no-comm|
--no-commi|
--no-commit)
108 -s=*|
--s=*|
--st=*|
--str=*|
--stra=*|
--strat=*|
--strate=*|\
109 --strateg=*|
--strategy=*|\
110 -s|
--s|
--st|
--str|
--stra|
--strat|
--strate|
--strateg|
--strategy)
113 strategy
=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
120 case " $all_strategies " in
122 use_strategies
="$use_strategies$strategy " ;;
124 die
"available strategies are: $all_strategies" ;;
128 rloga
=`expr "z$1" : 'z-[^=]*=\(.*\)'`
139 head=$
(git-rev-parse
--verify "$1"^
0) || usage
142 # All the rest are remote heads
143 test "$#" = 0 && usage
;# we need at least one remote head.
144 test "$rloga" = '' && rloga
="merge: $@"
149 remotehead
=$
(git-rev-parse
--verify "$remote"^
0) ||
150 die
"$remote - not something we can merge"
151 remoteheads
="${remoteheads}$remotehead "
153 set x
$remoteheads ; shift
155 case "$use_strategies" in
159 use_strategies
="$default_twohead_strategies" ;;
161 use_strategies
="$default_octopus_strategies" ;;
166 for s
in $use_strategies
169 *" $no_trivial_merge_strategies "*)
178 common
=$
(git-merge-base
--all $head "$@")
181 common
=$
(git-show-branch
--merge-base $head "$@")
184 echo "$head" >"$GIT_DIR/ORIG_HEAD"
186 case "$index_merge,$#,$common,$no_commit" in
188 # We've been told not to try anything clever. Skip to real merge.
191 # No common ancestors found. We need a real merge.
194 # If head can reach all the merge then we are up to date.
195 # but first the most common case of merging one remote.
196 finish_up_to_date
"Already up-to-date."
200 # Again the most common case of merging one remote.
201 echo "Updating from $head to $1"
202 git-update-index
--refresh 2>/dev
/null
203 new_head
=$
(git-rev-parse
--verify "$1^0") &&
204 git-read-tree
-u -v -m $head "$new_head" &&
205 finish
"$new_head" "Fast forward"
210 # We are not doing octopus and not fast forward. Need a
214 # We are not doing octopus, not fast forward, and have only
215 # one common. See if it is really trivial.
216 git var GIT_COMMITTER_IDENT
>/dev
/null ||
exit
218 echo "Trying really trivial in-index merge..."
219 git-update-index
--refresh 2>/dev
/null
220 if git-read-tree
--trivial -m -u -v $common $head "$1" &&
221 result_tree
=$
(git-write-tree
)
226 git-commit-tree
$result_tree -p HEAD
-p "$1"
228 finish
"$result_commit" "In-index merge"
235 # An octopus. If we can reach all the remote we are up to date.
239 common_one
=$
(git-merge-base
--all $head $remote)
240 if test "$common_one" != "$remote"
246 if test "$up_to_date" = t
248 finish_up_to_date
"Already up-to-date. Yeeah!"
254 # We are going to make a new commit.
255 git var GIT_COMMITTER_IDENT
>/dev
/null ||
exit
257 # At this point, we need a real merge. No matter what strategy
258 # we use, it would operate on the index, possibly affecting the
259 # working tree, and when resolved cleanly, have the desired tree
260 # in the index -- this means that the index must be in sync with
261 # the $head commit. The strategies are responsible to ensure this.
263 case "$use_strategies" in
265 # Stash away the local changes so that we can try more than one.
270 rm -f "$GIT_DIR/MERGE_SAVE"
275 result_tree
= best_cnt
=-1 best_strategy
= wt_strategy
=
277 for strategy
in $use_strategies
279 test "$wt_strategy" = '' ||
{
280 echo "Rewinding the tree to pristine..."
283 case "$single_strategy" in
285 echo "Trying merge strategy $strategy..."
289 # Remember which strategy left the state in the working tree
290 wt_strategy
=$strategy
292 git-merge-
$strategy $common -- "$head_arg" "$@"
294 if test "$no_commit" = t
&& test "$exit" = 0
297 exit=1 ;# pretend it left conflicts.
300 test "$exit" = 0 ||
{
302 # The backend exits with 1 when conflicts are left to be resolved,
303 # with 2 when it does not handle the given merge at all.
305 if test "$exit" -eq 1
308 git-diff-files --name-only
309 git-ls-files --unmerged
311 if test $best_cnt -le 0 -o $cnt -le $best_cnt
313 best_strategy
=$strategy
320 # Automerge succeeded.
321 result_tree
=$
(git-write-tree
) && break
324 # If we have a resulting tree, that means the strategy module
325 # auto resolved the merge cleanly.
326 if test '' != "$result_tree"
328 parents
=$
(git-show-branch
--independent "$head" "$@" |
sed -e 's/^/-p /')
329 result_commit
=$
(echo "$merge_msg" | git-commit-tree
$result_tree $parents) ||
exit
330 finish
"$result_commit" "Merge made by $wt_strategy."
335 # Pick the result from the best strategy and have the user fix it up.
336 case "$best_strategy" in
339 echo >&2 "No merge strategy handled the merge."
343 # We already have its result in the working tree.
346 echo "Rewinding the tree to pristine..."
348 echo "Using the $best_strategy to prepare resolving by hand."
349 git-merge-
$best_strategy $common -- "$head_arg" "$@"
353 if test "$squash" = t
360 done >"$GIT_DIR/MERGE_HEAD"
361 echo "$merge_msg" >"$GIT_DIR/MERGE_MSG"
364 if test "$merge_was_ok" = t
367 "Automatic merge went well; stopped before committing as requested"
374 git ls-files
--unmerged |
375 sed -e 's/^[^ ]* / /' |
377 } >>"$GIT_DIR/MERGE_MSG"
378 if test -d "$GIT_DIR/rr-cache"
382 die
"Automatic merge failed; fix conflicts and then commit the result."