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_strategies
='recursive'
18 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
19 "$GIT_DIR/MERGE_SAVE" ||
exit 1
23 # Stash away any local modifications.
24 git-diff-index
-z --name-only $head |
25 cpio -0 -o >"$GIT_DIR/MERGE_SAVE"
29 if test -f "$GIT_DIR/MERGE_SAVE"
31 git
reset --hard $head
32 cpio -iuv <"$GIT_DIR/MERGE_SAVE"
33 git-update-index
--refresh >/dev
/null
38 test '' = "$2" ||
echo "$2"
41 echo "No merge message -- not updating HEAD"
44 git-update-ref HEAD
"$1" "$head" ||
exit 1
50 git-diff-tree
-p -M "$head" "$1" |
51 git-apply
--stat --summary
56 while case "$#" in 0) break ;; esac
59 -n|
--n|
--no|
--no-|
--no-s|
--no-su|
--no-sum|
--no-summ|\
60 --no-summa|
--no-summar|
--no-summary)
62 --no-c|
--no-co|
--no-com|
--no-comm|
--no-commi|
--no-commit)
64 -s=*|
--s=*|
--st=*|
--str=*|
--stra=*|
--strat=*|
--strate=*|\
65 --strateg=*|
--strategy=*|\
66 -s|
--s|
--st|
--str|
--stra|
--strat|
--strate|
--strateg|
--strategy)
69 strategy
=`expr "$1" : '-[^=]*=\(.*\)'` ;;
76 case " $all_strategies " in
78 use_strategies
="$use_strategies$strategy " ;;
80 die
"available strategies are: $all_strategies" ;;
89 test "$#" -le 2 && usage
;# we need at least two heads.
94 head=$
(git-rev-parse
--verify "$1"^
0) || usage
97 # All the rest are remote heads
101 remotehead
=$
(git-rev-parse
--verify "$remote"^
0) ||
102 die
"$remote - not something we can merge"
103 remoteheads
="${remoteheads}$remotehead "
105 set x
$remoteheads ; shift
109 common
=$
(git-merge-base
--all $head "$@")
112 common
=$
(git-show-branch
--merge-base $head "$@")
115 echo "$head" >"$GIT_DIR/ORIG_HEAD"
117 case "$#,$common,$no_commit" in
119 # No common ancestors found. We need a real merge.
122 # If head can reach all the merge then we are up to date.
123 # but first the most common case of merging one remote
124 echo "Already up-to-date."
129 # Again the most common case of merging one remote.
130 echo "Updating from $head to $1."
131 git-update-index
--refresh 2>/dev
/null
132 new_head
=$
(git-rev-parse
--verify "$1^0") &&
133 git-read-tree
-u -v -m $head "$new_head" &&
134 finish
"$new_head" "Fast forward"
139 # We are not doing octopus and not fast forward. Need a
143 # We are not doing octopus, not fast forward, and have only
144 # one common. See if it is really trivial.
145 git var GIT_COMMITTER_IDENT
>/dev
/null ||
exit
147 echo "Trying really trivial in-index merge..."
148 git-update-index
--refresh 2>/dev
/null
149 if git-read-tree
--trivial -m -u -v $common $head "$1" &&
150 result_tree
=$
(git-write-tree
)
155 git-commit-tree
$result_tree -p HEAD
-p "$1"
157 finish
"$result_commit" "In-index merge"
164 # An octopus. If we can reach all the remote we are up to date.
168 common_one
=$
(git-merge-base
--all $head $remote)
169 if test "$common_one" != "$remote"
175 if test "$up_to_date" = t
177 echo "Already up-to-date. Yeeah!"
184 # We are going to make a new commit.
185 git var GIT_COMMITTER_IDENT
>/dev
/null ||
exit
187 case "$use_strategies" in
191 use_strategies
="$default_strategies" ;;
193 use_strategies
=octopus
;;
198 # At this point, we need a real merge. No matter what strategy
199 # we use, it would operate on the index, possibly affecting the
200 # working tree, and when resolved cleanly, have the desired tree
201 # in the index -- this means that the index must be in sync with
202 # the $head commit. The strategies are responsible to ensure this.
204 case "$use_strategies" in
206 # Stash away the local changes so that we can try more than one.
211 rm -f "$GIT_DIR/MERGE_SAVE"
216 result_tree
= best_cnt
=-1 best_strategy
= wt_strategy
=
218 for strategy
in $use_strategies
220 test "$wt_strategy" = '' ||
{
221 echo "Rewinding the tree to pristine..."
224 case "$single_strategy" in
226 echo "Trying merge strategy $strategy..."
230 # Remember which strategy left the state in the working tree
231 wt_strategy
=$strategy
233 git-merge-
$strategy $common -- "$head_arg" "$@"
235 if test "$no_commit" = t
&& test "$exit" = 0
238 exit=1 ;# pretend it left conflicts.
241 test "$exit" = 0 ||
{
243 # The backend exits with 1 when conflicts are left to be resolved,
244 # with 2 when it does not handle the given merge at all.
246 if test "$exit" -eq 1
249 git-diff-files --name-only
250 git-ls-files --unmerged
252 if test $best_cnt -le 0 -o $cnt -le $best_cnt
254 best_strategy
=$strategy
261 # Automerge succeeded.
262 result_tree
=$
(git-write-tree
) && break
265 # If we have a resulting tree, that means the strategy module
266 # auto resolved the merge cleanly.
267 if test '' != "$result_tree"
272 parents
="$parents -p $remote"
274 result_commit
=$
(echo "$merge_msg" | git-commit-tree
$result_tree $parents) ||
exit
275 finish
"$result_commit" "Merge $result_commit, made by $wt_strategy."
280 # Pick the result from the best strategy and have the user fix it up.
281 case "$best_strategy" in
284 echo >&2 "No merge strategy handled the merge."
288 # We already have its result in the working tree.
291 echo "Rewinding the tree to pristine..."
293 echo "Using the $best_strategy to prepare resolving by hand."
294 git-merge-
$best_strategy $common -- "$head_arg" "$@"
300 done >"$GIT_DIR/MERGE_HEAD"
301 echo "$merge_msg" >"$GIT_DIR/MERGE_MSG"
303 if test "$merge_was_ok" = t
306 "Automatic merge went well; stopped before committing as requested"
313 git ls-files
--unmerged |
314 sed -e 's/^[^ ]* / /' |
316 } >>"$GIT_DIR/MERGE_MSG"
317 if test -d "$GIT_DIR/rr-cache"
321 die
"Automatic merge failed; fix up by hand"