3 # Copyright (c) 2005 Junio C Hamano
6 . git-sh-setup || die "Not a git archive"
12 die "git-merge [-n] [-s <strategy>]... <merge-message> <head> <remote>+"
15 # all_strategies='resolve recursive stupid octopus'
17 all_strategies='recursive octopus resolve stupid'
18 default_strategies='resolve octopus'
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 -r -z --name-only $head |
29 cpio -0 -o >"$GIR_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
44 git-diff-tree -p -M $head "$1" |
45 git-apply --stat --summary
50 while case "$#" in 0) break ;; esac
53 -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\
54 --no-summa|--no-summar|--no-summary)
56 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
57 --strateg=*|--strategy=*|\
58 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
61 strategy=`expr "$1" : '-[^=]*=\(.*\)'` ;;
68 case " $all_strategies " in
70 use_strategies="$use_strategies$strategy " ;;
72 die "available strategies are: $all_strategies" ;;
81 case "$use_strategies" in
83 use_strategies=$default_strategies
86 test "$#" -le 2 && usage ;# we need at least two heads.
91 head=$(git-rev-parse --verify "$1"^0) || usage
94 # All the rest are remote heads
97 git-rev-parse --verify "$remote"^0 >/dev/null ||
98 die "$remote - not something we can merge"
101 common=$(git-show-branch --merge-base $head "$@")
102 echo "$head" >"$GIT_DIR/ORIG_HEAD"
106 die "Unable to find common commit between $head_arg and $*"
109 # If head can reach all the merge then we are up to date.
110 # but first the most common case of merging one remote
111 echo "Already up-to-date. Yeeah!"
116 # Again the most common case of merging one remote.
117 echo "Updating from $head to $1."
118 git-update-index --refresh 2>/dev/null
119 git-read-tree -u -m $head "$1" || exit 1
120 git-rev-parse --verify "$1^0" > "$GIT_DIR/HEAD"
126 # We are not doing octopus and not fast forward. Need a
130 # An octopus. If we can reach all the remote we are up to date.
134 common_one=$(git-merge-base $head $remote)
135 if test "$common_one" != "$remote"
141 if test "$up_to_date" = t
143 echo "Already up-to-date. Yeeah!"
150 # At this point, we need a real merge. No matter what strategy
151 # we use, it would operate on the index, possibly affecting the
152 # working tree, and when resolved cleanly, have the desired tree
153 # in the index -- this means that the index must be in sync with
154 # the $head commit. The strategies are responsible to ensure this.
156 case "$use_strategies" in
158 # Stash away the local changes so that we can try more than one.
163 rm -f "$GIT_DIR/MERGE_SAVE"
168 result_tree= best_cnt=-1 best_strategy= wt_strategy=
169 for strategy in $use_strategies
171 test "$wt_strategy" = '' || {
172 echo "Rewinding the tree to pristine..."
175 case "$single_strategy" in
177 echo "Trying merge strategy $strategy..."
181 # Remember which strategy left the state in the working tree
182 wt_strategy=$strategy
184 git-merge-$strategy $common -- "$head_arg" "$@" || {
186 # The backend exits with 1 when conflicts are left to be resolved,
187 # with 2 when it does not handle the given merge at all.
190 if test "$exit" -eq 1
193 git-diff-files --name-only
194 git-ls-files --unmerged
196 if test $best_cnt -le 0 -o $cnt -le $best_cnt
198 best_strategy=$strategy
205 # Automerge succeeded.
206 result_tree=$(git-write-tree) && break
209 # If we have a resulting tree, that means the strategy module
210 # auto resolved the merge cleanly.
211 if test '' != "$result_tree"
216 parents="$parents -p $remote"
218 result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents)
219 echo "Committed merge $result_commit, made by $wt_strategy."
220 echo $result_commit >"$GIT_DIR/HEAD"
221 summary $result_commit
226 # Pick the result from the best strategy and have the user fix it up.
227 case "$best_strategy" in
230 die "No merge strategy handled the merge."
233 # We already have its result in the working tree.
236 echo "Rewinding the tree to pristine..."
238 echo "Using the $best_strategy to prepare resolving by hand."
239 git-merge-$best_strategy $common -- "$head_arg" "$@"
245 done >"$GIT_DIR/MERGE_HEAD"
246 echo $merge_msg >"$GIT_DIR/MERGE_MSG"
248 die "Automatic merge failed; fix up by hand"