3 # Copyright (c) 2005 Junio C Hamano
6 USAGE
='[-n] [--no-commit] [--squash] [-s <strategy>]... <merge-message> <head> <remote>+'
12 all_strategies
='recursive recur octopus resolve stupid ours'
13 case "${GIT_USE_RECUR_FOR_RECURSIVE}" in
15 default_twohead_strategies
=recursive
;;
17 default_twohead_strategies
=recur
;;
19 default_octopus_strategies
='octopus'
20 no_trivial_merge_strategies
='ours'
24 if test "@@NO_PYTHON@@"; then
25 all_strategies
='recur resolve octopus stupid ours'
26 default_twohead_strategies
='resolve'
30 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
31 "$GIT_DIR/MERGE_SAVE" ||
exit 1
35 # Stash away any local modifications.
36 git-diff-index
-z --name-only $head |
37 cpio -0 -o >"$GIT_DIR/MERGE_SAVE"
41 if test -f "$GIT_DIR/MERGE_SAVE"
43 git
reset --hard $head
44 cpio -iuv <"$GIT_DIR/MERGE_SAVE"
45 git-update-index
--refresh >/dev
/null
49 finish_up_to_date
() {
52 echo "$1 (nothing to squash)" ;;
60 echo Squashed commit of the following
:
62 git-log
--no-merges ^
"$head" $remote
66 test '' = "$2" ||
echo "$2"
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 HEAD
"$1" "$head" ||
exit 1
89 git-diff-tree
--stat --summary -M "$head" "$1"
96 while case "$#" in 0) break ;; esac
99 -n|
--n|
--no|
--no-|
--no-s|
--no-su|
--no-sum|
--no-summ|\
100 --no-summa|
--no-summar|
--no-summary)
102 --sq|
--squ|
--squa|
--squas|
--squash)
103 squash
=t no_commit
=t
;;
104 --no-c|
--no-co|
--no-com|
--no-comm|
--no-commi|
--no-commit)
106 -s=*|
--s=*|
--st=*|
--str=*|
--stra=*|
--strat=*|
--strate=*|\
107 --strateg=*|
--strategy=*|\
108 -s|
--s|
--st|
--str|
--stra|
--strat|
--strate|
--strateg|
--strategy)
111 strategy
=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
118 case "$strategy,${GIT_USE_RECUR_FOR_RECURSIVE}" in
122 case " $all_strategies " in
124 use_strategies
="$use_strategies$strategy " ;;
126 die
"available strategies are: $all_strategies" ;;
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.
147 remotehead
=$
(git-rev-parse
--verify "$remote"^
0) ||
148 die
"$remote - not something we can merge"
149 remoteheads
="${remoteheads}$remotehead "
151 set x
$remoteheads ; shift
153 case "$use_strategies" in
157 use_strategies
="$default_twohead_strategies" ;;
159 use_strategies
="$default_octopus_strategies" ;;
164 for s
in $use_strategies
167 *" $no_trivial_merge_strategies "*)
176 common
=$
(git-merge-base
--all $head "$@")
179 common
=$
(git-show-branch
--merge-base $head "$@")
182 echo "$head" >"$GIT_DIR/ORIG_HEAD"
184 case "$index_merge,$#,$common,$no_commit" in
186 # We've been told not to try anything clever. Skip to real merge.
189 # No common ancestors found. We need a real merge.
192 # If head can reach all the merge then we are up to date.
193 # but first the most common case of merging one remote.
194 finish_up_to_date
"Already up-to-date."
198 # Again the most common case of merging one remote.
199 echo "Updating from $head to $1"
200 git-update-index
--refresh 2>/dev
/null
201 new_head
=$
(git-rev-parse
--verify "$1^0") &&
202 git-read-tree
-u -v -m $head "$new_head" &&
203 finish
"$new_head" "Fast forward"
208 # We are not doing octopus and not fast forward. Need a
212 # We are not doing octopus, not fast forward, and have only
213 # one common. See if it is really trivial.
214 git var GIT_COMMITTER_IDENT
>/dev
/null ||
exit
216 echo "Trying really trivial in-index merge..."
217 git-update-index
--refresh 2>/dev
/null
218 if git-read-tree
--trivial -m -u -v $common $head "$1" &&
219 result_tree
=$
(git-write-tree
)
224 git-commit-tree
$result_tree -p HEAD
-p "$1"
226 finish
"$result_commit" "In-index merge"
233 # An octopus. If we can reach all the remote we are up to date.
237 common_one
=$
(git-merge-base
--all $head $remote)
238 if test "$common_one" != "$remote"
244 if test "$up_to_date" = t
246 finish_up_to_date
"Already up-to-date. Yeeah!"
252 # We are going to make a new commit.
253 git var GIT_COMMITTER_IDENT
>/dev
/null ||
exit
255 # At this point, we need a real merge. No matter what strategy
256 # we use, it would operate on the index, possibly affecting the
257 # working tree, and when resolved cleanly, have the desired tree
258 # in the index -- this means that the index must be in sync with
259 # the $head commit. The strategies are responsible to ensure this.
261 case "$use_strategies" in
263 # Stash away the local changes so that we can try more than one.
268 rm -f "$GIT_DIR/MERGE_SAVE"
273 result_tree
= best_cnt
=-1 best_strategy
= wt_strategy
=
275 for strategy
in $use_strategies
277 test "$wt_strategy" = '' ||
{
278 echo "Rewinding the tree to pristine..."
281 case "$single_strategy" in
283 echo "Trying merge strategy $strategy..."
287 # Remember which strategy left the state in the working tree
288 wt_strategy
=$strategy
290 git-merge-
$strategy $common -- "$head_arg" "$@"
292 if test "$no_commit" = t
&& test "$exit" = 0
295 exit=1 ;# pretend it left conflicts.
298 test "$exit" = 0 ||
{
300 # The backend exits with 1 when conflicts are left to be resolved,
301 # with 2 when it does not handle the given merge at all.
303 if test "$exit" -eq 1
306 git-diff-files --name-only
307 git-ls-files --unmerged
309 if test $best_cnt -le 0 -o $cnt -le $best_cnt
311 best_strategy
=$strategy
318 # Automerge succeeded.
319 result_tree
=$
(git-write-tree
) && break
322 # If we have a resulting tree, that means the strategy module
323 # auto resolved the merge cleanly.
324 if test '' != "$result_tree"
326 parents
=$
(git-show-branch
--independent "$head" "$@" |
sed -e 's/^/-p /')
327 result_commit
=$
(echo "$merge_msg" | git-commit-tree
$result_tree $parents) ||
exit
328 finish
"$result_commit" "Merge $result_commit, made by $wt_strategy."
333 # Pick the result from the best strategy and have the user fix it up.
334 case "$best_strategy" in
337 echo >&2 "No merge strategy handled the merge."
341 # We already have its result in the working tree.
344 echo "Rewinding the tree to pristine..."
346 echo "Using the $best_strategy to prepare resolving by hand."
347 git-merge-
$best_strategy $common -- "$head_arg" "$@"
351 if test "$squash" = t
358 done >"$GIT_DIR/MERGE_HEAD"
359 echo "$merge_msg" >"$GIT_DIR/MERGE_MSG"
362 if test "$merge_was_ok" = t
365 "Automatic merge went well; stopped before committing as requested"
372 git ls-files
--unmerged |
373 sed -e 's/^[^ ]* / /' |
375 } >>"$GIT_DIR/MERGE_MSG"
376 if test -d "$GIT_DIR/rr-cache"
380 die
"Automatic merge failed; fix conflicts and then commit the result."