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 git diff -r -z --name-only $head | cpio -0 -o >"$GIR_DIR/MERGE_SAVE"
31 if test -f "$GIT_DIR/MERGE_SAVE"
33 git reset --hard $head
34 cpio -iuv <"$GIT_DIR/MERGE_SAVE"
35 git-update-index --refresh >/dev/null
42 git-diff-tree -p -M $head "$1" |
43 git-apply --stat --summary
48 while case "$#" in 0) break ;; esac
51 -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\
52 --no-summa|--no-summar|--no-summary)
54 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
55 --strateg=*|--strategy=*|\
56 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
59 strategy=`expr "$1" : '-[^=]*=\(.*\)'` ;;
66 case " $all_strategies " in
68 use_strategies="$use_strategies$strategy " ;;
70 die "available strategies are: $all_strategies" ;;
79 case "$use_strategies" in
81 use_strategies=$default_strategies
84 test "$#" -le 2 && usage ;# we need at least two heads.
89 head=$(git-rev-parse --verify "$1"^0) || usage
92 # All the rest are remote heads
95 git-rev-parse --verify "$remote"^0 >/dev/null ||
96 die "$remote - not something we can merge"
99 common=$(git-show-branch --merge-base $head "$@")
100 echo "$head" >"$GIT_DIR/ORIG_HEAD"
104 die "Unable to find common commit between $head_arg and $*"
107 # If head can reach all the merge then we are up to date.
108 # but first the most common case of merging one remote
109 echo "Already up-to-date. Yeeah!"
114 # Again the most common case of merging one remote.
115 echo "Updating from $head to $1."
116 git-update-index --refresh 2>/dev/null
117 git-read-tree -u -m $head "$1" || exit 1
118 git-rev-parse --verify "$1^0" > "$GIT_DIR/HEAD"
124 # We are not doing octopus and not fast forward. Need a
128 # An octopus. If we can reach all the remote we are up to date.
132 common_one=$(git-merge-base $head $remote)
133 if test "$common_one" != "$remote"
139 if test "$up_to_date" = t
141 echo "Already up-to-date. Yeeah!"
148 # At this point, we need a real merge. No matter what strategy
149 # we use, it would operate on the index, possibly affecting the
150 # working tree, and when resolved cleanly, have the desired tree
151 # in the index -- this means that the index must be in sync with
153 files=$(git-diff-index --cached --name-only $head) || exit
154 if [ "$files" ]; then
155 echo >&2 "Dirty index: cannot merge (dirty: $files)"
159 case "$use_strategies" in
161 # Stash away the local changes so that we can try more than one.
166 rm -f "$GIT_DIR/MERGE_SAVE"
171 result_tree= best_cnt=-1 best_strategy= wt_strategy=
172 for strategy in $use_strategies
174 test "$wt_strategy" = '' || {
175 echo "Rewinding the tree to pristine..."
178 case "$single_strategy" in
180 echo "Trying merge strategy $strategy..."
184 # Remember which strategy left the state in the working tree
185 wt_strategy=$strategy
187 git-merge-$strategy $common -- "$head_arg" "$@" || {
189 # The backend exits with 1 when conflicts are left to be resolved,
190 # with 2 when it does not handle the given merge at all.
193 if test "$exit" -eq 1
196 git-diff-files --name-only
197 git-ls-files --unmerged
199 if test $best_cnt -le 0 -o $cnt -le $best_cnt
201 best_strategy=$strategy
208 # Automerge succeeded.
209 result_tree=$(git-write-tree) && break
212 # If we have a resulting tree, that means the strategy module
213 # auto resolved the merge cleanly.
214 if test '' != "$result_tree"
219 parents="$parents -p $remote"
221 result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents)
222 echo "Committed merge $result_commit, made by $wt_strategy."
223 echo $result_commit >"$GIT_DIR/HEAD"
224 summary $result_commit
229 # Pick the result from the best strategy and have the user fix it up.
230 case "$best_strategy" in
233 die "No merge strategy handled the merge."
236 # We already have its result in the working tree.
239 echo "Rewinding the tree to pristine..."
241 echo "Using the $best_strategy to prepare resolving by hand."
242 git-merge-$best_strategy $common -- "$head_arg" "$@"
248 done >"$GIT_DIR/MERGE_HEAD"
249 echo $merge_msg >"$GIT_DIR/MERGE_MSG"
251 die "Automatic merge failed; fix up by hand"