gitweb: Add parse_commits, used to bulk load commit objects.
[git/dscho.git] / git-merge.sh
blob7dd0a112368a8b3672a98fdfcdb5290146cefeba
1 #!/bin/sh
3 # Copyright (c) 2005 Junio C Hamano
6 USAGE='[-n] [--no-commit] [--squash] [-s <strategy>] [--reflog-action=<action>] [-m=<merge-message>] <commit>+'
8 . git-sh-setup
10 LF='
13 all_strategies='recur recursive octopus resolve stupid ours'
14 default_twohead_strategies='recursive'
15 default_octopus_strategies='octopus'
16 no_trivial_merge_strategies='ours'
17 use_strategies=
19 index_merge=t
21 dropsave() {
22 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
23 "$GIT_DIR/MERGE_SAVE" || exit 1
26 savestate() {
27 # Stash away any local modifications.
28 git-diff-index -z --name-only $head |
29 cpio -0 -o >"$GIT_DIR/MERGE_SAVE"
32 restorestate() {
33 if test -f "$GIT_DIR/MERGE_SAVE"
34 then
35 git reset --hard $head >/dev/null
36 cpio -iuv <"$GIT_DIR/MERGE_SAVE"
37 git-update-index --refresh >/dev/null
41 finish_up_to_date () {
42 case "$squash" in
44 echo "$1 (nothing to squash)" ;;
45 '')
46 echo "$1" ;;
47 esac
48 dropsave
51 squash_message () {
52 echo Squashed commit of the following:
53 echo
54 git-log --no-merges ^"$head" $remote
57 finish () {
58 if test '' = "$2"
59 then
60 rlogm="$rloga"
61 else
62 echo "$2"
63 rlogm="$rloga: $2"
65 case "$squash" in
67 echo "Squash commit -- not updating HEAD"
68 squash_message >"$GIT_DIR/SQUASH_MSG"
70 '')
71 case "$merge_msg" in
72 '')
73 echo "No merge message -- not updating HEAD"
76 git-update-ref -m "$rlogm" HEAD "$1" "$head" || exit 1
78 esac
80 esac
81 case "$1" in
82 '')
84 ?*)
85 case "$no_summary" in
86 '')
87 git-diff-tree --stat --summary -M "$head" "$1"
89 esac
91 esac
94 merge_name () {
95 remote="$1"
96 rh=$(git-rev-parse --verify "$remote^0" 2>/dev/null) || return
97 bh=$(git-show-ref -s --verify "refs/heads/$remote" 2>/dev/null)
98 if test "$rh" = "$bh"
99 then
100 echo "$rh branch '$remote' of ."
101 elif truname=$(expr "$remote" : '\(.*\)~[1-9][0-9]*$') &&
102 git-show-ref -q --verify "refs/heads/$truname" 2>/dev/null
103 then
104 echo "$rh branch '$truname' (early part) of ."
105 else
106 echo "$rh commit '$remote'"
110 case "$#" in 0) usage ;; esac
112 rloga= have_message=
113 while case "$#" in 0) break ;; esac
115 case "$1" in
116 -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\
117 --no-summa|--no-summar|--no-summary)
118 no_summary=t ;;
119 --sq|--squ|--squa|--squas|--squash)
120 squash=t no_commit=t ;;
121 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
122 no_commit=t ;;
123 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
124 --strateg=*|--strategy=*|\
125 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
126 case "$#,$1" in
127 *,*=*)
128 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
129 1,*)
130 usage ;;
132 strategy="$2"
133 shift ;;
134 esac
135 case " $all_strategies " in
136 *" $strategy "*)
137 use_strategies="$use_strategies$strategy " ;;
139 die "available strategies are: $all_strategies" ;;
140 esac
142 --reflog-action=*)
143 rloga=`expr "z$1" : 'z-[^=]*=\(.*\)'`
145 -m=*|--m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
146 merge_msg=`expr "z$1" : 'z-[^=]*=\(.*\)'`
147 have_message=t
149 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
150 shift
151 case "$#" in
152 1) usage ;;
153 esac
154 merge_msg="$1"
155 have_message=t
157 -*) usage ;;
158 *) break ;;
159 esac
160 shift
161 done
163 # This could be traditional "merge <msg> HEAD <commit>..." and the
164 # way we can tell it is to see if the second token is HEAD, but some
165 # people might have misused the interface and used a committish that
166 # is the same as HEAD there instead. Traditional format never would
167 # have "-m" so it is an additional safety measure to check for it.
169 if test -z "$have_message" &&
170 second_token=$(git-rev-parse --verify "$2^0" 2>/dev/null) &&
171 head_commit=$(git-rev-parse --verify "HEAD" 2>/dev/null) &&
172 test "$second_token" = "$head_commit"
173 then
174 merge_msg="$1"
175 shift
176 head_arg="$1"
177 shift
178 elif ! git-rev-parse --verify HEAD >/dev/null 2>&1
179 then
180 # If the merged head is a valid one there is no reason to
181 # forbid "git merge" into a branch yet to be born. We do
182 # the same for "git pull".
183 if test 1 -ne $#
184 then
185 echo >&2 "Can merge only exactly one commit into empty head"
186 exit 1
189 rh=$(git rev-parse --verify "$1^0") ||
190 die "$1 - not something we can merge"
192 git-update-ref -m "initial pull" HEAD "$rh" "" &&
193 git-read-tree --reset -u HEAD
194 exit
196 else
197 # We are invoked directly as the first-class UI.
198 head_arg=HEAD
200 # All the rest are the commits being merged; prepare
201 # the standard merge summary message to be appended to
202 # the given message. If remote is invalid we will die
203 # later in the common codepath so we discard the error
204 # in this loop.
205 merge_name=$(for remote
207 merge_name "$remote"
208 done | git-fmt-merge-msg
210 merge_msg="${merge_msg:+$merge_msg$LF$LF}$merge_name"
212 head=$(git-rev-parse --verify "$head_arg"^0) || usage
214 # All the rest are remote heads
215 test "$#" = 0 && usage ;# we need at least one remote head.
216 test "$rloga" = '' && rloga="merge: $@"
218 remoteheads=
219 for remote
221 remotehead=$(git-rev-parse --verify "$remote"^0 2>/dev/null) ||
222 die "$remote - not something we can merge"
223 remoteheads="${remoteheads}$remotehead "
224 eval GITHEAD_$remotehead='"$remote"'
225 export GITHEAD_$remotehead
226 done
227 set x $remoteheads ; shift
229 case "$use_strategies" in
231 case "$#" in
233 use_strategies="$default_twohead_strategies" ;;
235 use_strategies="$default_octopus_strategies" ;;
236 esac
238 esac
240 for s in $use_strategies
242 case " $s " in
243 *" $no_trivial_merge_strategies "*)
244 index_merge=f
245 break
247 esac
248 done
250 case "$#" in
252 common=$(git-merge-base --all $head "$@")
255 common=$(git-show-branch --merge-base $head "$@")
257 esac
258 echo "$head" >"$GIT_DIR/ORIG_HEAD"
260 case "$index_merge,$#,$common,$no_commit" in
261 f,*)
262 # We've been told not to try anything clever. Skip to real merge.
264 ?,*,'',*)
265 # No common ancestors found. We need a real merge.
267 ?,1,"$1",*)
268 # If head can reach all the merge then we are up to date.
269 # but first the most common case of merging one remote.
270 finish_up_to_date "Already up-to-date."
271 exit 0
273 ?,1,"$head",*)
274 # Again the most common case of merging one remote.
275 echo "Updating $(git-rev-parse --short $head)..$(git-rev-parse --short $1)"
276 git-update-index --refresh 2>/dev/null
277 new_head=$(git-rev-parse --verify "$1^0") &&
278 git-read-tree -v -m -u --exclude-per-directory=.gitignore $head "$new_head" &&
279 finish "$new_head" "Fast forward"
280 dropsave
281 exit 0
283 ?,1,?*"$LF"?*,*)
284 # We are not doing octopus and not fast forward. Need a
285 # real merge.
287 ?,1,*,)
288 # We are not doing octopus, not fast forward, and have only
289 # one common. See if it is really trivial.
290 git var GIT_COMMITTER_IDENT >/dev/null || exit
292 echo "Trying really trivial in-index merge..."
293 git-update-index --refresh 2>/dev/null
294 if git-read-tree --trivial -m -u -v $common $head "$1" &&
295 result_tree=$(git-write-tree)
296 then
297 echo "Wonderful."
298 result_commit=$(
299 echo "$merge_msg" |
300 git-commit-tree $result_tree -p HEAD -p "$1"
301 ) || exit
302 finish "$result_commit" "In-index merge"
303 dropsave
304 exit 0
306 echo "Nope."
309 # An octopus. If we can reach all the remote we are up to date.
310 up_to_date=t
311 for remote
313 common_one=$(git-merge-base --all $head $remote)
314 if test "$common_one" != "$remote"
315 then
316 up_to_date=f
317 break
319 done
320 if test "$up_to_date" = t
321 then
322 finish_up_to_date "Already up-to-date. Yeeah!"
323 exit 0
326 esac
328 # We are going to make a new commit.
329 git var GIT_COMMITTER_IDENT >/dev/null || exit
331 # At this point, we need a real merge. No matter what strategy
332 # we use, it would operate on the index, possibly affecting the
333 # working tree, and when resolved cleanly, have the desired tree
334 # in the index -- this means that the index must be in sync with
335 # the $head commit. The strategies are responsible to ensure this.
337 case "$use_strategies" in
338 ?*' '?*)
339 # Stash away the local changes so that we can try more than one.
340 savestate
341 single_strategy=no
344 rm -f "$GIT_DIR/MERGE_SAVE"
345 single_strategy=yes
347 esac
349 result_tree= best_cnt=-1 best_strategy= wt_strategy=
350 merge_was_ok=
351 for strategy in $use_strategies
353 test "$wt_strategy" = '' || {
354 echo "Rewinding the tree to pristine..."
355 restorestate
357 case "$single_strategy" in
359 echo "Trying merge strategy $strategy..."
361 esac
363 # Remember which strategy left the state in the working tree
364 wt_strategy=$strategy
366 git-merge-$strategy $common -- "$head_arg" "$@"
367 exit=$?
368 if test "$no_commit" = t && test "$exit" = 0
369 then
370 merge_was_ok=t
371 exit=1 ;# pretend it left conflicts.
374 test "$exit" = 0 || {
376 # The backend exits with 1 when conflicts are left to be resolved,
377 # with 2 when it does not handle the given merge at all.
379 if test "$exit" -eq 1
380 then
381 cnt=`{
382 git-diff-files --name-only
383 git-ls-files --unmerged
384 } | wc -l`
385 if test $best_cnt -le 0 -o $cnt -le $best_cnt
386 then
387 best_strategy=$strategy
388 best_cnt=$cnt
391 continue
394 # Automerge succeeded.
395 result_tree=$(git-write-tree) && break
396 done
398 # If we have a resulting tree, that means the strategy module
399 # auto resolved the merge cleanly.
400 if test '' != "$result_tree"
401 then
402 parents=$(git-show-branch --independent "$head" "$@" | sed -e 's/^/-p /')
403 result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents) || exit
404 finish "$result_commit" "Merge made by $wt_strategy."
405 dropsave
406 exit 0
409 # Pick the result from the best strategy and have the user fix it up.
410 case "$best_strategy" in
412 restorestate
413 case "$use_strategies" in
414 ?*' '?*)
415 echo >&2 "No merge strategy handled the merge."
418 echo >&2 "Merge with strategy $use_strategies failed."
420 esac
421 exit 2
423 "$wt_strategy")
424 # We already have its result in the working tree.
427 echo "Rewinding the tree to pristine..."
428 restorestate
429 echo "Using the $best_strategy to prepare resolving by hand."
430 git-merge-$best_strategy $common -- "$head_arg" "$@"
432 esac
434 if test "$squash" = t
435 then
436 finish
437 else
438 for remote
440 echo $remote
441 done >"$GIT_DIR/MERGE_HEAD"
442 echo "$merge_msg" >"$GIT_DIR/MERGE_MSG"
445 if test "$merge_was_ok" = t
446 then
447 echo >&2 \
448 "Automatic merge went well; stopped before committing as requested"
449 exit 0
450 else
452 echo '
453 Conflicts:
455 git ls-files --unmerged |
456 sed -e 's/^[^ ]* / /' |
457 uniq
458 } >>"$GIT_DIR/MERGE_MSG"
459 if test -d "$GIT_DIR/rr-cache"
460 then
461 git-rerere
463 die "Automatic merge failed; fix conflicts and then commit the result."