Merge branch 'as/graph' into next
[git/platforms/storm.git] / git-merge.sh
blobcf41af22c3378b66224fdac9e2a618af221cb065
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,no-stat don't show a diffstat at the end of the merge
13 summary (synonym to --stat)
14 no-summary (synonym to --no-stat)
15 log add list of one-line log to merge commit message
16 no-log don't add list of one-line log to merge commit message
17 squash create a single commit instead of doing a merge
18 commit perform a commit if the merge sucesses (default)
19 ff allow fast forward (default)
20 s,strategy= merge strategy to use
21 m,message= message to be used for the merge commit (if any)
24 SUBDIRECTORY_OK=Yes
25 . git-sh-setup
26 require_work_tree
27 cd_to_toplevel
29 test -z "$(git ls-files -u)" ||
30 die "You are in the middle of a conflicted merge."
32 LF='
35 all_strategies='recur recursive octopus resolve stupid ours subtree'
36 default_twohead_strategies='recursive'
37 default_octopus_strategies='octopus'
38 no_fast_forward_strategies='subtree ours'
39 no_trivial_strategies='recursive recur subtree ours'
40 use_strategies=
42 allow_fast_forward=t
43 allow_trivial_merge=t
44 squash= no_commit= log_arg=
46 dropsave() {
47 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
48 "$GIT_DIR/MERGE_STASH" || exit 1
51 savestate() {
52 # Stash away any local modifications.
53 git stash create >"$GIT_DIR/MERGE_STASH"
56 restorestate() {
57 if test -f "$GIT_DIR/MERGE_STASH"
58 then
59 git reset --hard $head >/dev/null
60 git stash apply $(cat "$GIT_DIR/MERGE_STASH")
61 git update-index --refresh >/dev/null
65 finish_up_to_date () {
66 case "$squash" in
68 echo "$1 (nothing to squash)" ;;
69 '')
70 echo "$1" ;;
71 esac
72 dropsave
75 squash_message () {
76 echo Squashed commit of the following:
77 echo
78 git log --no-merges --pretty=medium ^"$head" $remoteheads
81 finish () {
82 if test '' = "$2"
83 then
84 rlogm="$GIT_REFLOG_ACTION"
85 else
86 echo "$2"
87 rlogm="$GIT_REFLOG_ACTION: $2"
89 case "$squash" in
91 echo "Squash commit -- not updating HEAD"
92 squash_message >"$GIT_DIR/SQUASH_MSG"
94 '')
95 case "$merge_msg" in
96 '')
97 echo "No merge message -- not updating HEAD"
100 git update-ref -m "$rlogm" HEAD "$1" "$head" || exit 1
101 git gc --auto
103 esac
105 esac
106 case "$1" in
110 if test "$show_diffstat" = t
111 then
112 # We want color (if set), but no pager
113 GIT_PAGER='' git diff --stat --summary -M "$head" "$1"
116 esac
118 # Run a post-merge hook
119 if test -x "$GIT_DIR"/hooks/post-merge
120 then
121 case "$squash" in
123 "$GIT_DIR"/hooks/post-merge 1
126 "$GIT_DIR"/hooks/post-merge 0
128 esac
132 merge_name () {
133 remote="$1"
134 rh=$(git rev-parse --verify "$remote^0" 2>/dev/null) || return
135 bh=$(git show-ref -s --verify "refs/heads/$remote" 2>/dev/null)
136 if test "$rh" = "$bh"
137 then
138 echo "$rh branch '$remote' of ."
139 elif truname=$(expr "$remote" : '\(.*\)~[1-9][0-9]*$') &&
140 git show-ref -q --verify "refs/heads/$truname" 2>/dev/null
141 then
142 echo "$rh branch '$truname' (early part) of ."
143 elif test "$remote" = "FETCH_HEAD" -a -r "$GIT_DIR/FETCH_HEAD"
144 then
145 sed -e 's/ not-for-merge / /' -e 1q \
146 "$GIT_DIR/FETCH_HEAD"
147 else
148 echo "$rh commit '$remote'"
152 parse_config () {
153 while test $# != 0; do
154 case "$1" in
155 -n|--no-stat|--no-summary)
156 show_diffstat=false ;;
157 --stat|--summary)
158 show_diffstat=t ;;
159 --log|--no-log)
160 log_arg=$1 ;;
161 --squash)
162 test "$allow_fast_forward" = t ||
163 die "You cannot combine --squash with --no-ff."
164 squash=t no_commit=t ;;
165 --no-squash)
166 squash= no_commit= ;;
167 --commit)
168 no_commit= ;;
169 --no-commit)
170 no_commit=t ;;
171 --ff)
172 allow_fast_forward=t ;;
173 --no-ff)
174 test "$squash" != t ||
175 die "You cannot combine --squash with --no-ff."
176 allow_fast_forward=f ;;
177 -s|--strategy)
178 shift
179 case " $all_strategies " in
180 *" $1 "*)
181 use_strategies="$use_strategies$1 " ;;
183 die "available strategies are: $all_strategies" ;;
184 esac
186 -m|--message)
187 shift
188 merge_msg="$1"
189 have_message=t
192 shift
193 break ;;
194 *) usage ;;
195 esac
196 shift
197 done
198 args_left=$#
201 test $# != 0 || usage
203 have_message=
205 if branch=$(git-symbolic-ref -q HEAD)
206 then
207 mergeopts=$(git config "branch.${branch#refs/heads/}.mergeoptions")
208 if test -n "$mergeopts"
209 then
210 parse_config $mergeopts --
214 parse_config "$@"
215 while test $args_left -lt $#; do shift; done
217 if test -z "$show_diffstat"; then
218 test "$(git config --bool merge.diffstat)" = false && show_diffstat=false
219 test "$(git config --bool merge.stat)" = false && show_diffstat=false
220 test -z "$show_diffstat" && show_diffstat=t
223 # This could be traditional "merge <msg> HEAD <commit>..." and the
224 # way we can tell it is to see if the second token is HEAD, but some
225 # people might have misused the interface and used a committish that
226 # is the same as HEAD there instead. Traditional format never would
227 # have "-m" so it is an additional safety measure to check for it.
229 if test -z "$have_message" &&
230 second_token=$(git rev-parse --verify "$2^0" 2>/dev/null) &&
231 head_commit=$(git rev-parse --verify "HEAD" 2>/dev/null) &&
232 test "$second_token" = "$head_commit"
233 then
234 merge_msg="$1"
235 shift
236 head_arg="$1"
237 shift
238 elif ! git rev-parse --verify HEAD >/dev/null 2>&1
239 then
240 # If the merged head is a valid one there is no reason to
241 # forbid "git merge" into a branch yet to be born. We do
242 # the same for "git pull".
243 if test 1 -ne $#
244 then
245 echo >&2 "Can merge only exactly one commit into empty head"
246 exit 1
249 rh=$(git rev-parse --verify "$1^0") ||
250 die "$1 - not something we can merge"
252 git update-ref -m "initial pull" HEAD "$rh" "" &&
253 git read-tree --reset -u HEAD
254 exit
256 else
257 # We are invoked directly as the first-class UI.
258 head_arg=HEAD
260 if test -z "$merge_msg"
261 then
262 # All the rest are the commits being merged; prepare
263 # the standard merge summary message to be appended to
264 # the given message. If remote is invalid we will die
265 # later in the common codepath so we discard the error
266 # in this loop.
267 merge_msg=$(for remote
269 merge_name "$remote"
270 done | git fmt-merge-msg $log_arg
274 head=$(git rev-parse --verify "$head_arg"^0) || usage
276 # All the rest are remote heads
277 test "$#" = 0 && usage ;# we need at least one remote head.
278 set_reflog_action "merge $*"
280 remoteheads=
281 for remote
283 remotehead=$(git rev-parse --verify "$remote"^0 2>/dev/null) ||
284 die "$remote - not something we can merge"
285 remoteheads="${remoteheads}$remotehead "
286 eval GITHEAD_$remotehead='"$remote"'
287 export GITHEAD_$remotehead
288 done
289 set x $remoteheads ; shift
291 case "$use_strategies" in
293 case "$#" in
295 var="`git config --get pull.twohead`"
296 if test -n "$var"
297 then
298 use_strategies="$var"
299 else
300 use_strategies="$default_twohead_strategies"
301 fi ;;
303 var="`git config --get pull.octopus`"
304 if test -n "$var"
305 then
306 use_strategies="$var"
307 else
308 use_strategies="$default_octopus_strategies"
309 fi ;;
310 esac
312 esac
314 for s in $use_strategies
316 for ss in $no_fast_forward_strategies
318 case " $s " in
319 *" $ss "*)
320 allow_fast_forward=f
321 break
323 esac
324 done
325 for ss in $no_trivial_strategies
327 case " $s " in
328 *" $ss "*)
329 allow_trivial_merge=f
330 break
332 esac
333 done
334 done
336 case "$#" in
338 common=$(git merge-base --all $head "$@")
341 common=$(git show-branch --merge-base $head "$@")
343 esac
344 echo "$head" >"$GIT_DIR/ORIG_HEAD"
346 case "$allow_fast_forward,$#,$common,$no_commit" in
347 ?,*,'',*)
348 # No common ancestors found. We need a real merge.
350 ?,1,"$1",*)
351 # If head can reach all the merge then we are up to date.
352 # but first the most common case of merging one remote.
353 finish_up_to_date "Already up-to-date."
354 exit 0
356 t,1,"$head",*)
357 # Again the most common case of merging one remote.
358 echo "Updating $(git rev-parse --short $head)..$(git rev-parse --short $1)"
359 git update-index --refresh 2>/dev/null
360 msg="Fast forward"
361 if test -n "$have_message"
362 then
363 msg="$msg (no commit created; -m option ignored)"
365 new_head=$(git rev-parse --verify "$1^0") &&
366 git read-tree -v -m -u --exclude-per-directory=.gitignore $head "$new_head" &&
367 finish "$new_head" "$msg" || exit
368 dropsave
369 exit 0
371 ?,1,?*"$LF"?*,*)
372 # We are not doing octopus and not fast forward. Need a
373 # real merge.
375 ?,1,*,)
376 # We are not doing octopus, not fast forward, and have only
377 # one common.
378 git update-index --refresh 2>/dev/null
379 case "$allow_trivial_merge" in
381 # See if it is really trivial.
382 git var GIT_COMMITTER_IDENT >/dev/null || exit
383 echo "Trying really trivial in-index merge..."
384 if git read-tree --trivial -m -u -v $common $head "$1" &&
385 result_tree=$(git write-tree)
386 then
387 echo "Wonderful."
388 result_commit=$(
389 printf '%s\n' "$merge_msg" |
390 git commit-tree $result_tree -p HEAD -p "$1"
391 ) || exit
392 finish "$result_commit" "In-index merge"
393 dropsave
394 exit 0
396 echo "Nope."
397 esac
400 # An octopus. If we can reach all the remote we are up to date.
401 up_to_date=t
402 for remote
404 common_one=$(git merge-base --all $head $remote)
405 if test "$common_one" != "$remote"
406 then
407 up_to_date=f
408 break
410 done
411 if test "$up_to_date" = t
412 then
413 finish_up_to_date "Already up-to-date. Yeeah!"
414 exit 0
417 esac
419 # We are going to make a new commit.
420 git var GIT_COMMITTER_IDENT >/dev/null || exit
422 # At this point, we need a real merge. No matter what strategy
423 # we use, it would operate on the index, possibly affecting the
424 # working tree, and when resolved cleanly, have the desired tree
425 # in the index -- this means that the index must be in sync with
426 # the $head commit. The strategies are responsible to ensure this.
428 case "$use_strategies" in
429 ?*' '?*)
430 # Stash away the local changes so that we can try more than one.
431 savestate
432 single_strategy=no
435 rm -f "$GIT_DIR/MERGE_STASH"
436 single_strategy=yes
438 esac
440 result_tree= best_cnt=-1 best_strategy= wt_strategy=
441 merge_was_ok=
442 for strategy in $use_strategies
444 test "$wt_strategy" = '' || {
445 echo "Rewinding the tree to pristine..."
446 restorestate
448 case "$single_strategy" in
450 echo "Trying merge strategy $strategy..."
452 esac
454 # Remember which strategy left the state in the working tree
455 wt_strategy=$strategy
457 git-merge-$strategy $common -- "$head_arg" "$@"
458 exit=$?
459 if test "$no_commit" = t && test "$exit" = 0
460 then
461 merge_was_ok=t
462 exit=1 ;# pretend it left conflicts.
465 test "$exit" = 0 || {
467 # The backend exits with 1 when conflicts are left to be resolved,
468 # with 2 when it does not handle the given merge at all.
470 if test "$exit" -eq 1
471 then
472 cnt=`{
473 git diff-files --name-only
474 git ls-files --unmerged
475 } | wc -l`
476 if test $best_cnt -le 0 -o $cnt -le $best_cnt
477 then
478 best_strategy=$strategy
479 best_cnt=$cnt
482 continue
485 # Automerge succeeded.
486 result_tree=$(git write-tree) && break
487 done
489 # If we have a resulting tree, that means the strategy module
490 # auto resolved the merge cleanly.
491 if test '' != "$result_tree"
492 then
493 if test "$allow_fast_forward" = "t"
494 then
495 parents=$(git show-branch --independent "$head" "$@")
496 else
497 parents=$(git rev-parse "$head" "$@")
499 parents=$(echo "$parents" | sed -e 's/^/-p /')
500 result_commit=$(printf '%s\n' "$merge_msg" | git commit-tree $result_tree $parents) || exit
501 finish "$result_commit" "Merge made by $wt_strategy."
502 dropsave
503 exit 0
506 # Pick the result from the best strategy and have the user fix it up.
507 case "$best_strategy" in
509 restorestate
510 case "$use_strategies" in
511 ?*' '?*)
512 echo >&2 "No merge strategy handled the merge."
515 echo >&2 "Merge with strategy $use_strategies failed."
517 esac
518 exit 2
520 "$wt_strategy")
521 # We already have its result in the working tree.
524 echo "Rewinding the tree to pristine..."
525 restorestate
526 echo "Using the $best_strategy to prepare resolving by hand."
527 git-merge-$best_strategy $common -- "$head_arg" "$@"
529 esac
531 if test "$squash" = t
532 then
533 finish
534 else
535 for remote
537 echo $remote
538 done >"$GIT_DIR/MERGE_HEAD"
539 printf '%s\n' "$merge_msg" >"$GIT_DIR/MERGE_MSG"
542 if test "$merge_was_ok" = t
543 then
544 echo >&2 \
545 "Automatic merge went well; stopped before committing as requested"
546 exit 0
547 else
549 echo '
550 Conflicts:
552 git ls-files --unmerged |
553 sed -e 's/^[^ ]* / /' |
554 uniq
555 } >>"$GIT_DIR/MERGE_MSG"
556 git rerere
557 die "Automatic merge failed; fix conflicts and then commit the result."