3 # Copyright (c) 2005 Junio C Hamano
6 . git-sh-setup || die
"Not a git archive"
12 die
"git-merge [-n] [--no-commit] [-s <strategy>]... <merge-message> <head> <remote>+"
15 # all_strategies='resolve recursive stupid octopus'
17 all_strategies
='recursive octopus resolve stupid ours'
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
-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 case "$use_strategies" in
95 use_strategies
=$default_strategies
98 test "$#" -le 2 && usage
;# we need at least two heads.
103 head=$
(git-rev-parse
--verify "$1"^
0) || usage
106 # All the rest are remote heads
109 git-rev-parse
--verify "$remote"^
0 >/dev
/null ||
110 die
"$remote - not something we can merge"
115 common
=$
(git-merge-base
--all $head "$@")
118 common
=$
(git-show-branch
--merge-base $head "$@")
121 echo "$head" >"$GIT_DIR/ORIG_HEAD"
123 case "$#,$common,$no_commit" in
125 # No common ancestors found. We need a real merge.
128 # If head can reach all the merge then we are up to date.
129 # but first the most common case of merging one remote
130 echo "Already up-to-date."
135 # Again the most common case of merging one remote.
136 echo "Updating from $head to $1."
137 git-update-index
--refresh 2>/dev
/null
138 new_head
=$
(git-rev-parse
--verify "$1^0") &&
139 git-read-tree
-u -m $head "$new_head" &&
140 finish
"$new_head" "Fast forward"
145 # We are not doing octopus and not fast forward. Need a
149 # We are not doing octopus, not fast forward, and have only
150 # one common. See if it is really trivial.
151 echo "Trying really trivial in-index merge..."
152 git-update-index
--refresh 2>/dev
/null
153 if git-read-tree
--trivial -m -u $common $head "$1" &&
154 result_tree
=$
(git-write-tree
)
159 git-commit-tree
$result_tree -p HEAD
-p "$1"
161 finish
"$result_commit" "In-index merge"
168 # An octopus. If we can reach all the remote we are up to date.
172 common_one
=$
(git-merge-base
--all $head $remote)
173 if test "$common_one" != "$remote"
179 if test "$up_to_date" = t
181 echo "Already up-to-date. Yeeah!"
188 # At this point, we need a real merge. No matter what strategy
189 # we use, it would operate on the index, possibly affecting the
190 # working tree, and when resolved cleanly, have the desired tree
191 # in the index -- this means that the index must be in sync with
192 # the $head commit. The strategies are responsible to ensure this.
194 case "$use_strategies" in
196 # Stash away the local changes so that we can try more than one.
201 rm -f "$GIT_DIR/MERGE_SAVE"
206 result_tree
= best_cnt
=-1 best_strategy
= wt_strategy
=
207 for strategy
in $use_strategies
209 test "$wt_strategy" = '' ||
{
210 echo "Rewinding the tree to pristine..."
213 case "$single_strategy" in
215 echo "Trying merge strategy $strategy..."
219 # Remember which strategy left the state in the working tree
220 wt_strategy
=$strategy
222 git-merge-
$strategy $common -- "$head_arg" "$@"
224 if test "$no_commit" = t
&& test "$exit" = 0
226 exit=1 ;# pretend it left conflicts.
229 test "$exit" = 0 ||
{
231 # The backend exits with 1 when conflicts are left to be resolved,
232 # with 2 when it does not handle the given merge at all.
234 if test "$exit" -eq 1
237 git-diff-files --name-only
238 git-ls-files --unmerged
240 if test $best_cnt -le 0 -o $cnt -le $best_cnt
242 best_strategy
=$strategy
249 # Automerge succeeded.
250 result_tree
=$
(git-write-tree
) && break
253 # If we have a resulting tree, that means the strategy module
254 # auto resolved the merge cleanly.
255 if test '' != "$result_tree"
260 parents
="$parents -p $remote"
262 result_commit
=$
(echo "$merge_msg" | git-commit-tree
$result_tree $parents) ||
exit
263 finish
"$result_commit" "Merge $result_commit, made by $wt_strategy."
268 # Pick the result from the best strategy and have the user fix it up.
269 case "$best_strategy" in
272 die
"No merge strategy handled the merge."
275 # We already have its result in the working tree.
278 echo "Rewinding the tree to pristine..."
280 echo "Using the $best_strategy to prepare resolving by hand."
281 git-merge-
$best_strategy $common -- "$head_arg" "$@"
287 done >"$GIT_DIR/MERGE_HEAD"
288 echo $merge_msg >"$GIT_DIR/MERGE_MSG"
290 die
"Automatic merge failed/prevented; fix up by hand"