Merge commit 'junio/next' into next
[git/platforms/storm.git] / git-merge.sh
blob2d177c1943382add953312c0d6c2dd2b9abbc703
1 #!/bin/sh
3 # Copyright (c) 2005 Junio C Hamano
6 OPTIONS_KEEPDASHDASH=
7 OPTIONS_SPEC="\
8 git-merge [options] <remote>...
9 git-merge [options] <msg> HEAD <remote>
11 stat show a diffstat at the end of the merge
12 n don't show a diffstat at the end of the merge
13 summary (synonym to --stat)
14 log add list of one-line log to merge commit message
15 squash create a single commit instead of doing a merge
16 commit perform a commit if the merge sucesses (default)
17 ff allow fast forward (default)
18 s,strategy= merge strategy to use
19 m,message= message to be used for the merge commit (if any)
22 SUBDIRECTORY_OK=Yes
23 . git-sh-setup
24 require_work_tree
25 cd_to_toplevel
27 test -z "$(git ls-files -u)" ||
28 die "You are in the middle of a conflicted merge."
30 LF='
33 all_strategies='recur recursive octopus resolve stupid ours subtree'
34 default_twohead_strategies='recursive'
35 default_octopus_strategies='octopus'
36 no_fast_forward_strategies='subtree ours'
37 no_trivial_strategies='recursive recur subtree ours'
38 use_strategies=
40 allow_fast_forward=t
41 allow_trivial_merge=t
42 squash= no_commit= log_arg=
44 dropsave() {
45 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
46 "$GIT_DIR/MERGE_STASH" || exit 1
49 savestate() {
50 # Stash away any local modifications.
51 git stash create >"$GIT_DIR/MERGE_STASH"
54 restorestate() {
55 if test -f "$GIT_DIR/MERGE_STASH"
56 then
57 git reset --hard $head >/dev/null
58 git stash apply $(cat "$GIT_DIR/MERGE_STASH")
59 git update-index --refresh >/dev/null
63 finish_up_to_date () {
64 case "$squash" in
66 echo "$1 (nothing to squash)" ;;
67 '')
68 echo "$1" ;;
69 esac
70 dropsave
73 squash_message () {
74 echo Squashed commit of the following:
75 echo
76 git log --no-merges --pretty=medium ^"$head" $remoteheads
79 finish () {
80 if test '' = "$2"
81 then
82 rlogm="$GIT_REFLOG_ACTION"
83 else
84 echo "$2"
85 rlogm="$GIT_REFLOG_ACTION: $2"
87 case "$squash" in
89 echo "Squash commit -- not updating HEAD"
90 squash_message >"$GIT_DIR/SQUASH_MSG"
92 '')
93 case "$merge_msg" in
94 '')
95 echo "No merge message -- not updating HEAD"
98 git update-ref -m "$rlogm" HEAD "$1" "$head" || exit 1
99 git gc --auto
101 esac
103 esac
104 case "$1" in
108 if test "$show_diffstat" = t
109 then
110 # We want color (if set), but no pager
111 GIT_PAGER='' git diff --stat --summary -M "$head" "$1"
114 esac
116 # Run a post-merge hook
117 if test -x "$GIT_DIR"/hooks/post-merge
118 then
119 case "$squash" in
121 "$GIT_DIR"/hooks/post-merge 1
124 "$GIT_DIR"/hooks/post-merge 0
126 esac
130 merge_name () {
131 remote="$1"
132 rh=$(git rev-parse --verify "$remote^0" 2>/dev/null) || return
133 bh=$(git show-ref -s --verify "refs/heads/$remote" 2>/dev/null)
134 if test "$rh" = "$bh"
135 then
136 echo "$rh branch '$remote' of ."
137 elif truname=$(expr "$remote" : '\(.*\)~[1-9][0-9]*$') &&
138 git show-ref -q --verify "refs/heads/$truname" 2>/dev/null
139 then
140 echo "$rh branch '$truname' (early part) of ."
141 elif test "$remote" = "FETCH_HEAD" -a -r "$GIT_DIR/FETCH_HEAD"
142 then
143 sed -e 's/ not-for-merge / /' -e 1q \
144 "$GIT_DIR/FETCH_HEAD"
145 else
146 echo "$rh commit '$remote'"
150 parse_config () {
151 while test $# != 0; do
152 case "$1" in
153 -n|--no-stat|--no-summary)
154 show_diffstat=false ;;
155 --stat|--summary)
156 show_diffstat=t ;;
157 --log|--no-log)
158 log_arg=$1 ;;
159 --squash)
160 test "$allow_fast_forward" = t ||
161 die "You cannot combine --squash with --no-ff."
162 squash=t no_commit=t ;;
163 --no-squash)
164 squash= no_commit= ;;
165 --commit)
166 no_commit= ;;
167 --no-commit)
168 no_commit=t ;;
169 --ff)
170 allow_fast_forward=t ;;
171 --no-ff)
172 test "$squash" != t ||
173 die "You cannot combine --squash with --no-ff."
174 allow_fast_forward=f ;;
175 -s|--strategy)
176 shift
177 case " $all_strategies " in
178 *" $1 "*)
179 use_strategies="$use_strategies$1 " ;;
181 die "available strategies are: $all_strategies" ;;
182 esac
184 -m|--message)
185 shift
186 merge_msg="$1"
187 have_message=t
190 shift
191 break ;;
192 *) usage ;;
193 esac
194 shift
195 done
196 args_left=$#
199 test $# != 0 || usage
201 have_message=
203 if branch=$(git-symbolic-ref -q HEAD)
204 then
205 mergeopts=$(git config "branch.${branch#refs/heads/}.mergeoptions")
206 if test -n "$mergeopts"
207 then
208 parse_config $mergeopts --
212 parse_config "$@"
213 while test $args_left -lt $#; do shift; done
215 if test -z "$show_diffstat"; then
216 test "$(git config --bool merge.diffstat)" = false && show_diffstat=false
217 test "$(git config --bool merge.stat)" = false && show_diffstat=false
218 test -z "$show_diffstat" && show_diffstat=t
221 # This could be traditional "merge <msg> HEAD <commit>..." and the
222 # way we can tell it is to see if the second token is HEAD, but some
223 # people might have misused the interface and used a committish that
224 # is the same as HEAD there instead. Traditional format never would
225 # have "-m" so it is an additional safety measure to check for it.
227 if test -z "$have_message" &&
228 second_token=$(git rev-parse --verify "$2^0" 2>/dev/null) &&
229 head_commit=$(git rev-parse --verify "HEAD" 2>/dev/null) &&
230 test "$second_token" = "$head_commit"
231 then
232 merge_msg="$1"
233 shift
234 head_arg="$1"
235 shift
236 elif ! git rev-parse --verify HEAD >/dev/null 2>&1
237 then
238 # If the merged head is a valid one there is no reason to
239 # forbid "git merge" into a branch yet to be born. We do
240 # the same for "git pull".
241 if test 1 -ne $#
242 then
243 echo >&2 "Can merge only exactly one commit into empty head"
244 exit 1
247 rh=$(git rev-parse --verify "$1^0") ||
248 die "$1 - not something we can merge"
250 git update-ref -m "initial pull" HEAD "$rh" "" &&
251 git read-tree --reset -u HEAD
252 exit
254 else
255 # We are invoked directly as the first-class UI.
256 head_arg=HEAD
258 if test -z "$merge_msg"
259 then
260 # All the rest are the commits being merged; prepare
261 # the standard merge summary message to be appended to
262 # the given message. If remote is invalid we will die
263 # later in the common codepath so we discard the error
264 # in this loop.
265 merge_msg=$(for remote
267 merge_name "$remote"
268 done | git fmt-merge-msg $log_arg
272 head=$(git rev-parse --verify "$head_arg"^0) || usage
274 # All the rest are remote heads
275 test "$#" = 0 && usage ;# we need at least one remote head.
276 set_reflog_action "merge $*"
278 remoteheads=
279 for remote
281 remotehead=$(git rev-parse --verify "$remote"^0 2>/dev/null) ||
282 die "$remote - not something we can merge"
283 remoteheads="${remoteheads}$remotehead "
284 eval GITHEAD_$remotehead='"$remote"'
285 export GITHEAD_$remotehead
286 done
287 set x $remoteheads ; shift
289 case "$use_strategies" in
291 case "$#" in
293 var="`git config --get pull.twohead`"
294 if test -n "$var"
295 then
296 use_strategies="$var"
297 else
298 use_strategies="$default_twohead_strategies"
299 fi ;;
301 var="`git config --get pull.octopus`"
302 if test -n "$var"
303 then
304 use_strategies="$var"
305 else
306 use_strategies="$default_octopus_strategies"
307 fi ;;
308 esac
310 esac
312 for s in $use_strategies
314 for ss in $no_fast_forward_strategies
316 case " $s " in
317 *" $ss "*)
318 allow_fast_forward=f
319 break
321 esac
322 done
323 for ss in $no_trivial_strategies
325 case " $s " in
326 *" $ss "*)
327 allow_trivial_merge=f
328 break
330 esac
331 done
332 done
334 case "$#" in
336 common=$(git merge-base --all $head "$@")
339 common=$(git show-branch --merge-base $head "$@")
341 esac
342 echo "$head" >"$GIT_DIR/ORIG_HEAD"
344 case "$allow_fast_forward,$#,$common,$no_commit" in
345 ?,*,'',*)
346 # No common ancestors found. We need a real merge.
348 ?,1,"$1",*)
349 # If head can reach all the merge then we are up to date.
350 # but first the most common case of merging one remote.
351 finish_up_to_date "Already up-to-date."
352 exit 0
354 t,1,"$head",*)
355 # Again the most common case of merging one remote.
356 echo "Updating $(git rev-parse --short $head)..$(git rev-parse --short $1)"
357 git update-index --refresh 2>/dev/null
358 msg="Fast forward"
359 if test -n "$have_message"
360 then
361 msg="$msg (no commit created; -m option ignored)"
363 new_head=$(git rev-parse --verify "$1^0") &&
364 git read-tree -v -m -u --exclude-per-directory=.gitignore $head "$new_head" &&
365 finish "$new_head" "$msg" || exit
366 dropsave
367 exit 0
369 ?,1,?*"$LF"?*,*)
370 # We are not doing octopus and not fast forward. Need a
371 # real merge.
373 ?,1,*,)
374 # We are not doing octopus, not fast forward, and have only
375 # one common.
376 git update-index --refresh 2>/dev/null
377 case "$allow_trivial_merge" in
379 # See if it is really trivial.
380 git var GIT_COMMITTER_IDENT >/dev/null || exit
381 echo "Trying really trivial in-index merge..."
382 if git read-tree --trivial -m -u -v $common $head "$1" &&
383 result_tree=$(git write-tree)
384 then
385 echo "Wonderful."
386 result_commit=$(
387 printf '%s\n' "$merge_msg" |
388 git commit-tree $result_tree -p HEAD -p "$1"
389 ) || exit
390 finish "$result_commit" "In-index merge"
391 dropsave
392 exit 0
394 echo "Nope."
395 esac
398 # An octopus. If we can reach all the remote we are up to date.
399 up_to_date=t
400 for remote
402 common_one=$(git merge-base --all $head $remote)
403 if test "$common_one" != "$remote"
404 then
405 up_to_date=f
406 break
408 done
409 if test "$up_to_date" = t
410 then
411 finish_up_to_date "Already up-to-date. Yeeah!"
412 exit 0
415 esac
417 # We are going to make a new commit.
418 git var GIT_COMMITTER_IDENT >/dev/null || exit
420 # At this point, we need a real merge. No matter what strategy
421 # we use, it would operate on the index, possibly affecting the
422 # working tree, and when resolved cleanly, have the desired tree
423 # in the index -- this means that the index must be in sync with
424 # the $head commit. The strategies are responsible to ensure this.
426 case "$use_strategies" in
427 ?*' '?*)
428 # Stash away the local changes so that we can try more than one.
429 savestate
430 single_strategy=no
433 rm -f "$GIT_DIR/MERGE_STASH"
434 single_strategy=yes
436 esac
438 result_tree= best_cnt=-1 best_strategy= wt_strategy=
439 merge_was_ok=
440 for strategy in $use_strategies
442 test "$wt_strategy" = '' || {
443 echo "Rewinding the tree to pristine..."
444 restorestate
446 case "$single_strategy" in
448 echo "Trying merge strategy $strategy..."
450 esac
452 # Remember which strategy left the state in the working tree
453 wt_strategy=$strategy
455 git-merge-$strategy $common -- "$head_arg" "$@"
456 exit=$?
457 if test "$no_commit" = t && test "$exit" = 0
458 then
459 merge_was_ok=t
460 exit=1 ;# pretend it left conflicts.
463 test "$exit" = 0 || {
465 # The backend exits with 1 when conflicts are left to be resolved,
466 # with 2 when it does not handle the given merge at all.
468 if test "$exit" -eq 1
469 then
470 cnt=`{
471 git diff-files --name-only
472 git ls-files --unmerged
473 } | wc -l`
474 if test $best_cnt -le 0 -o $cnt -le $best_cnt
475 then
476 best_strategy=$strategy
477 best_cnt=$cnt
480 continue
483 # Automerge succeeded.
484 result_tree=$(git write-tree) && break
485 done
487 # If we have a resulting tree, that means the strategy module
488 # auto resolved the merge cleanly.
489 if test '' != "$result_tree"
490 then
491 if test "$allow_fast_forward" = "t"
492 then
493 parents=$(git show-branch --independent "$head" "$@")
494 else
495 parents=$(git rev-parse "$head" "$@")
497 parents=$(echo "$parents" | sed -e 's/^/-p /')
498 result_commit=$(printf '%s\n' "$merge_msg" | git commit-tree $result_tree $parents) || exit
499 finish "$result_commit" "Merge made by $wt_strategy."
500 dropsave
501 exit 0
504 # Pick the result from the best strategy and have the user fix it up.
505 case "$best_strategy" in
507 restorestate
508 case "$use_strategies" in
509 ?*' '?*)
510 echo >&2 "No merge strategy handled the merge."
513 echo >&2 "Merge with strategy $use_strategies failed."
515 esac
516 exit 2
518 "$wt_strategy")
519 # We already have its result in the working tree.
522 echo "Rewinding the tree to pristine..."
523 restorestate
524 echo "Using the $best_strategy to prepare resolving by hand."
525 git-merge-$best_strategy $common -- "$head_arg" "$@"
527 esac
529 if test "$squash" = t
530 then
531 finish
532 else
533 for remote
535 echo $remote
536 done >"$GIT_DIR/MERGE_HEAD"
537 printf '%s\n' "$merge_msg" >"$GIT_DIR/MERGE_MSG"
540 if test "$merge_was_ok" = t
541 then
542 echo >&2 \
543 "Automatic merge went well; stopped before committing as requested"
544 exit 0
545 else
547 echo '
548 Conflicts:
550 git ls-files --unmerged |
551 sed -e 's/^[^ ]* / /' |
552 uniq
553 } >>"$GIT_DIR/MERGE_MSG"
554 git rerere
555 die "Automatic merge failed; fix conflicts and then commit the result."