3 # Copyright (c) 2005 Junio C Hamano
7 USAGE
='[-n] [--no-commit] [-s <strategy>]... <merge-message> <head> <remote>+'
13 all_strategies
='recursive octopus resolve stupid ours'
14 default_twohead_strategies
='recursive'
15 default_octopus_strategies
='octopus'
16 no_trivial_merge_strategies
='ours'
20 if test "@@NO_PYTHON@@"; then
21 all_strategies
='resolve octopus stupid ours'
22 default_twohead_strategies
='resolve'
26 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
27 "$GIT_DIR/MERGE_SAVE" ||
exit 1
31 # Stash away any local modifications.
32 git-diff-index
-z --name-only $head |
33 cpio -0 -o >"$GIT_DIR/MERGE_SAVE"
37 if test -f "$GIT_DIR/MERGE_SAVE"
39 git
reset --hard $head
40 cpio -iuv <"$GIT_DIR/MERGE_SAVE"
41 git-update-index
--refresh >/dev
/null
46 test '' = "$2" ||
echo "$2"
49 echo "No merge message -- not updating HEAD"
52 git-update-ref HEAD
"$1" "$head" ||
exit 1
58 git-diff-tree
-p --stat --summary -M "$head" "$1"
63 while case "$#" in 0) break ;; esac
66 -n|
--n|
--no|
--no-|
--no-s|
--no-su|
--no-sum|
--no-summ|\
67 --no-summa|
--no-summar|
--no-summary)
69 --no-c|
--no-co|
--no-com|
--no-comm|
--no-commi|
--no-commit)
71 -s=*|
--s=*|
--st=*|
--str=*|
--stra=*|
--strat=*|
--strate=*|\
72 --strateg=*|
--strategy=*|\
73 -s|
--s|
--st|
--str|
--stra|
--strat|
--strate|
--strateg|
--strategy)
76 strategy
=`expr "$1" : '-[^=]*=\(.*\)'` ;;
83 case " $all_strategies " in
85 use_strategies
="$use_strategies$strategy " ;;
87 die
"available strategies are: $all_strategies" ;;
99 head=$
(git-rev-parse
--verify "$1"^
0) || usage
102 # All the rest are remote heads
103 test "$#" = 0 && usage
;# we need at least one remote head.
108 remotehead
=$
(git-rev-parse
--verify "$remote"^
0) ||
109 die
"$remote - not something we can merge"
110 remoteheads
="${remoteheads}$remotehead "
112 set x
$remoteheads ; shift
114 case "$use_strategies" in
118 use_strategies
="$default_twohead_strategies" ;;
120 use_strategies
="$default_octopus_strategies" ;;
125 for s
in $use_strategies
128 *" $no_trivial_merge_strategies "*)
137 common
=$
(git-merge-base
--all $head "$@")
140 common
=$
(git-show-branch
--merge-base $head "$@")
143 echo "$head" >"$GIT_DIR/ORIG_HEAD"
145 case "$index_merge,$#,$common,$no_commit" in
147 # We've been told not to try anything clever. Skip to real merge.
150 # No common ancestors found. We need a real merge.
153 # If head can reach all the merge then we are up to date.
154 # but first the most common case of merging one remote.
155 echo "Already up-to-date."
160 # Again the most common case of merging one remote.
161 echo "Updating from $head to $1"
162 git-update-index
--refresh 2>/dev
/null
163 new_head
=$
(git-rev-parse
--verify "$1^0") &&
164 git-read-tree
-u -v -m $head "$new_head" &&
165 finish
"$new_head" "Fast forward"
170 # We are not doing octopus and not fast forward. Need a
174 # We are not doing octopus, not fast forward, and have only
175 # one common. See if it is really trivial.
176 git var GIT_COMMITTER_IDENT
>/dev
/null ||
exit
178 echo "Trying really trivial in-index merge..."
179 git-update-index
--refresh 2>/dev
/null
180 if git-read-tree
--trivial -m -u -v $common $head "$1" &&
181 result_tree
=$
(git-write-tree
)
186 git-commit-tree
$result_tree -p HEAD
-p "$1"
188 finish
"$result_commit" "In-index merge"
195 # An octopus. If we can reach all the remote we are up to date.
199 common_one
=$
(git-merge-base
--all $head $remote)
200 if test "$common_one" != "$remote"
206 if test "$up_to_date" = t
208 echo "Already up-to-date. Yeeah!"
215 # We are going to make a new commit.
216 git var GIT_COMMITTER_IDENT
>/dev
/null ||
exit
218 # At this point, we need a real merge. No matter what strategy
219 # we use, it would operate on the index, possibly affecting the
220 # working tree, and when resolved cleanly, have the desired tree
221 # in the index -- this means that the index must be in sync with
222 # the $head commit. The strategies are responsible to ensure this.
224 case "$use_strategies" in
226 # Stash away the local changes so that we can try more than one.
231 rm -f "$GIT_DIR/MERGE_SAVE"
236 result_tree
= best_cnt
=-1 best_strategy
= wt_strategy
=
238 for strategy
in $use_strategies
240 test "$wt_strategy" = '' ||
{
241 echo "Rewinding the tree to pristine..."
244 case "$single_strategy" in
246 echo "Trying merge strategy $strategy..."
250 # Remember which strategy left the state in the working tree
251 wt_strategy
=$strategy
253 git-merge-
$strategy $common -- "$head_arg" "$@"
255 if test "$no_commit" = t
&& test "$exit" = 0
258 exit=1 ;# pretend it left conflicts.
261 test "$exit" = 0 ||
{
263 # The backend exits with 1 when conflicts are left to be resolved,
264 # with 2 when it does not handle the given merge at all.
266 if test "$exit" -eq 1
269 git-diff-files --name-only
270 git-ls-files --unmerged
272 if test $best_cnt -le 0 -o $cnt -le $best_cnt
274 best_strategy
=$strategy
281 # Automerge succeeded.
282 result_tree
=$
(git-write-tree
) && break
285 # If we have a resulting tree, that means the strategy module
286 # auto resolved the merge cleanly.
287 if test '' != "$result_tree"
289 parents
=$
(git-show-branch
--independent "$head" "$@" |
sed -e 's/^/-p /')
290 result_commit
=$
(echo "$merge_msg" | git-commit-tree
$result_tree $parents) ||
exit
291 finish
"$result_commit" "Merge $result_commit, made by $wt_strategy."
296 # Pick the result from the best strategy and have the user fix it up.
297 case "$best_strategy" in
300 echo >&2 "No merge strategy handled the merge."
304 # We already have its result in the working tree.
307 echo "Rewinding the tree to pristine..."
309 echo "Using the $best_strategy to prepare resolving by hand."
310 git-merge-
$best_strategy $common -- "$head_arg" "$@"
316 done >"$GIT_DIR/MERGE_HEAD"
317 echo "$merge_msg" >"$GIT_DIR/MERGE_MSG"
319 if test "$merge_was_ok" = t
322 "Automatic merge went well; stopped before committing as requested"
329 git ls-files
--unmerged |
330 sed -e 's/^[^ ]* / /' |
332 } >>"$GIT_DIR/MERGE_MSG"
333 if test -d "$GIT_DIR/rr-cache"
337 die
"Automatic merge failed; fix conflicts and then commit the result."