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'
16 if test "@@NO_PYTHON@@"; then
17 all_strategies='resolve octopus stupid ours'
18 default_strategies='resolve'
22 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
23 "$GIT_DIR/MERGE_SAVE" || exit 1
27 # Stash away any local modifications.
28 git-diff-index -z --name-only $head |
29 cpio -0 -o >"$GIT_DIR/MERGE_SAVE"
33 if test -f "$GIT_DIR/MERGE_SAVE"
35 git reset --hard $head
36 cpio -iuv <"$GIT_DIR/MERGE_SAVE"
37 git-update-index --refresh >/dev/null
42 test '' = "$2" || echo "$2"
45 echo "No merge message -- not updating HEAD"
48 git-update-ref HEAD "$1" "$head" || exit 1
54 git-diff-tree -p -M "$head" "$1" |
55 git-apply --stat --summary
60 while case "$#" in 0) break ;; esac
63 -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\
64 --no-summa|--no-summar|--no-summary)
66 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
68 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
69 --strateg=*|--strategy=*|\
70 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
73 strategy=`expr "$1" : '-[^=]*=\(.*\)'` ;;
80 case " $all_strategies " in
82 use_strategies="$use_strategies$strategy " ;;
84 die "available strategies are: $all_strategies" ;;
93 test "$#" -le 2 && usage ;# we need at least two heads.
98 head=$(git-rev-parse --verify "$1"^0) || usage
101 # All the rest are remote heads
105 remotehead=$(git-rev-parse --verify "$remote"^0) ||
106 die "$remote - not something we can merge"
107 remoteheads="${remoteheads}$remotehead "
109 set x $remoteheads ; shift
113 common=$(git-merge-base --all $head "$@")
116 common=$(git-show-branch --merge-base $head "$@")
119 echo "$head" >"$GIT_DIR/ORIG_HEAD"
121 case "$#,$common,$no_commit" in
123 # No common ancestors found. We need a real merge.
126 # If head can reach all the merge then we are up to date.
127 # but first the most common case of merging one remote
128 echo "Already up-to-date."
133 # Again the most common case of merging one remote.
134 echo "Updating from $head to $1."
135 git-update-index --refresh 2>/dev/null
136 new_head=$(git-rev-parse --verify "$1^0") &&
137 git-read-tree -u -m $head "$new_head" &&
138 finish "$new_head" "Fast forward"
143 # We are not doing octopus and not fast forward. Need a
147 # We are not doing octopus, not fast forward, and have only
148 # one common. See if it is really trivial.
149 echo "Trying really trivial in-index merge..."
150 git-update-index --refresh 2>/dev/null
151 if git-read-tree --trivial -m -u $common $head "$1" &&
152 result_tree=$(git-write-tree)
157 git-commit-tree $result_tree -p HEAD -p "$1"
159 finish "$result_commit" "In-index merge"
166 # An octopus. If we can reach all the remote we are up to date.
170 common_one=$(git-merge-base --all $head $remote)
171 if test "$common_one" != "$remote"
177 if test "$up_to_date" = t
179 echo "Already up-to-date. Yeeah!"
186 case "$use_strategies" in
190 use_strategies="$default_strategies" ;;
192 use_strategies=octopus ;;
197 # At this point, we need a real merge. No matter what strategy
198 # we use, it would operate on the index, possibly affecting the
199 # working tree, and when resolved cleanly, have the desired tree
200 # in the index -- this means that the index must be in sync with
201 # the $head commit. The strategies are responsible to ensure this.
203 case "$use_strategies" in
205 # Stash away the local changes so that we can try more than one.
210 rm -f "$GIT_DIR/MERGE_SAVE"
215 result_tree= best_cnt=-1 best_strategy= wt_strategy=
217 for strategy in $use_strategies
219 test "$wt_strategy" = '' || {
220 echo "Rewinding the tree to pristine..."
223 case "$single_strategy" in
225 echo "Trying merge strategy $strategy..."
229 # Remember which strategy left the state in the working tree
230 wt_strategy=$strategy
232 git-merge-$strategy $common -- "$head_arg" "$@"
234 if test "$no_commit" = t && test "$exit" = 0
237 exit=1 ;# pretend it left conflicts.
240 test "$exit" = 0 || {
242 # The backend exits with 1 when conflicts are left to be resolved,
243 # with 2 when it does not handle the given merge at all.
245 if test "$exit" -eq 1
248 git-diff-files --name-only
249 git-ls-files --unmerged
251 if test $best_cnt -le 0 -o $cnt -le $best_cnt
253 best_strategy=$strategy
260 # Automerge succeeded.
261 result_tree=$(git-write-tree) && break
264 # If we have a resulting tree, that means the strategy module
265 # auto resolved the merge cleanly.
266 if test '' != "$result_tree"
271 parents="$parents -p $remote"
273 result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents) || exit
274 finish "$result_commit" "Merge $result_commit, made by $wt_strategy."
279 # Pick the result from the best strategy and have the user fix it up.
280 case "$best_strategy" in
283 echo >&2 "No merge strategy handled the merge."
287 # We already have its result in the working tree.
290 echo "Rewinding the tree to pristine..."
292 echo "Using the $best_strategy to prepare resolving by hand."
293 git-merge-$best_strategy $common -- "$head_arg" "$@"
299 done >"$GIT_DIR/MERGE_HEAD"
300 echo "$merge_msg" >"$GIT_DIR/MERGE_MSG"
302 if test "$merge_was_ok" = t
305 "Automatic merge went well; stopped before committing as requested"
312 git ls-files --unmerged |
313 sed -e 's/^[^ ]* / /' |
315 } >>"$GIT_DIR/MERGE_MSG"
316 if test -d "$GIT_DIR/rr-cache"
320 die "Automatic merge failed; fix up by hand"