merge script: --ff-only to disallow true merge
[git/dscho.git] / contrib / examples / git-merge.sh
blobdfd6e17946946825ad7a486e90d35a9f310020e6
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 succeeds (default)
17 ff allow fast-forward (default)
18 ff-only abort if fast-forward is not possible
19 s,strategy= merge strategy to use
20 X= option for selected merge strategy
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 "Merge is not possible because you have unmerged files."
32 ! test -e "$GIT_DIR/MERGE_HEAD" ||
33 die 'You have not concluded your merge (MERGE_HEAD exists).'
35 LF='
38 all_strategies='recur recursive octopus resolve stupid ours subtree'
39 all_strategies="$all_strategies recursive-ours recursive-theirs"
40 not_strategies='base file index tree'
41 default_twohead_strategies='recursive'
42 default_octopus_strategies='octopus'
43 no_fast_forward_strategies='subtree ours'
44 no_trivial_strategies='recursive recur subtree ours recursive-ours recursive-theirs'
45 use_strategies=
46 xopt=
48 allow_fast_forward=t
49 fast_forward_only=
50 allow_trivial_merge=t
51 squash= no_commit= log_arg=
53 dropsave() {
54 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
55 "$GIT_DIR/MERGE_STASH" || exit 1
58 savestate() {
59 # Stash away any local modifications.
60 git stash create >"$GIT_DIR/MERGE_STASH"
63 restorestate() {
64 if test -f "$GIT_DIR/MERGE_STASH"
65 then
66 git reset --hard $head >/dev/null
67 git stash apply $(cat "$GIT_DIR/MERGE_STASH")
68 git update-index --refresh >/dev/null
72 finish_up_to_date () {
73 case "$squash" in
75 echo "$1 (nothing to squash)" ;;
76 '')
77 echo "$1" ;;
78 esac
79 dropsave
82 squash_message () {
83 echo Squashed commit of the following:
84 echo
85 git log --no-merges --pretty=medium ^"$head" $remoteheads
88 finish () {
89 if test '' = "$2"
90 then
91 rlogm="$GIT_REFLOG_ACTION"
92 else
93 echo "$2"
94 rlogm="$GIT_REFLOG_ACTION: $2"
96 case "$squash" in
98 echo "Squash commit -- not updating HEAD"
99 squash_message >"$GIT_DIR/SQUASH_MSG"
102 case "$merge_msg" in
104 echo "No merge message -- not updating HEAD"
107 git update-ref -m "$rlogm" HEAD "$1" "$head" || exit 1
108 git gc --auto
110 esac
112 esac
113 case "$1" in
117 if test "$show_diffstat" = t
118 then
119 # We want color (if set), but no pager
120 GIT_PAGER='' git diff --stat --summary -M "$head" "$1"
123 esac
125 # Run a post-merge hook
126 if test -x "$GIT_DIR"/hooks/post-merge
127 then
128 case "$squash" in
130 "$GIT_DIR"/hooks/post-merge 1
133 "$GIT_DIR"/hooks/post-merge 0
135 esac
139 merge_name () {
140 remote="$1"
141 rh=$(git rev-parse --verify "$remote^0" 2>/dev/null) || return
142 if truname=$(expr "$remote" : '\(.*\)~[0-9]*$') &&
143 git show-ref -q --verify "refs/heads/$truname" 2>/dev/null
144 then
145 echo "$rh branch '$truname' (early part) of ."
146 return
148 if found_ref=$(git rev-parse --symbolic-full-name --verify \
149 "$remote" 2>/dev/null)
150 then
151 if test "${found_ref#refs/heads/}" != "$found_ref"
152 then
153 echo "$rh branch '$remote' of ."
154 return
155 elif test "${found_ref#refs/remotes/}" != "$found_ref"
156 then
157 echo "$rh remote branch '$remote' of ."
158 return
161 if test "$remote" = "FETCH_HEAD" -a -r "$GIT_DIR/FETCH_HEAD"
162 then
163 sed -e 's/ not-for-merge / /' -e 1q \
164 "$GIT_DIR/FETCH_HEAD"
165 return
167 echo "$rh commit '$remote'"
170 parse_config () {
171 while test $# != 0; do
172 case "$1" in
173 -n|--no-stat|--no-summary)
174 show_diffstat=false ;;
175 --stat|--summary)
176 show_diffstat=t ;;
177 --log|--no-log)
178 log_arg=$1 ;;
179 --squash)
180 test "$allow_fast_forward" = t ||
181 die "You cannot combine --squash with --no-ff."
182 squash=t no_commit=t ;;
183 --no-squash)
184 squash= no_commit= ;;
185 --commit)
186 no_commit= ;;
187 --no-commit)
188 no_commit=t ;;
189 --ff)
190 allow_fast_forward=t ;;
191 --no-ff)
192 test "$squash" != t ||
193 die "You cannot combine --squash with --no-ff."
194 test "$fast_forward_only" != t ||
195 die "You cannot combine --ff-only with --no-ff."
196 allow_fast_forward=f ;;
197 --ff-only)
198 test "$allow_fast_forward" != f ||
199 die "You cannot combine --ff-only with --no-ff."
200 fast_forward_only=t ;;
201 -s|--strategy)
202 shift
203 case " $all_strategies " in
204 *" $1 "*)
205 use_strategies="$use_strategies$1 "
208 case " $not_strategies " in
209 *" $1 "*)
210 false
211 esac &&
212 type "git-merge-$1" >/dev/null 2>&1 ||
213 die "available strategies are: $all_strategies"
214 use_strategies="$use_strategies$1 "
216 esac
219 shift
220 xopt="${xopt:+$xopt }$(git rev-parse --sq-quote "--$1")"
222 -m|--message)
223 shift
224 merge_msg="$1"
225 have_message=t
228 shift
229 break ;;
230 *) usage ;;
231 esac
232 shift
233 done
234 args_left=$#
237 test $# != 0 || usage
239 have_message=
241 if branch=$(git-symbolic-ref -q HEAD)
242 then
243 mergeopts=$(git config "branch.${branch#refs/heads/}.mergeoptions")
244 if test -n "$mergeopts"
245 then
246 parse_config $mergeopts --
250 parse_config "$@"
251 while test $args_left -lt $#; do shift; done
253 if test -z "$show_diffstat"; then
254 test "$(git config --bool merge.diffstat)" = false && show_diffstat=false
255 test "$(git config --bool merge.stat)" = false && show_diffstat=false
256 test -z "$show_diffstat" && show_diffstat=t
259 # This could be traditional "merge <msg> HEAD <commit>..." and the
260 # way we can tell it is to see if the second token is HEAD, but some
261 # people might have misused the interface and used a committish that
262 # is the same as HEAD there instead. Traditional format never would
263 # have "-m" so it is an additional safety measure to check for it.
265 if test -z "$have_message" &&
266 second_token=$(git rev-parse --verify "$2^0" 2>/dev/null) &&
267 head_commit=$(git rev-parse --verify "HEAD" 2>/dev/null) &&
268 test "$second_token" = "$head_commit"
269 then
270 merge_msg="$1"
271 shift
272 head_arg="$1"
273 shift
274 elif ! git rev-parse --verify HEAD >/dev/null 2>&1
275 then
276 # If the merged head is a valid one there is no reason to
277 # forbid "git merge" into a branch yet to be born. We do
278 # the same for "git pull".
279 if test 1 -ne $#
280 then
281 echo >&2 "Can merge only exactly one commit into empty head"
282 exit 1
285 test "$squash" != t ||
286 die "Squash commit into empty head not supported yet"
287 test "$allow_fast_forward" = t ||
288 die "Non-fast-forward into an empty head does not make sense"
289 rh=$(git rev-parse --verify "$1^0") ||
290 die "$1 - not something we can merge"
292 git update-ref -m "initial pull" HEAD "$rh" "" &&
293 git read-tree --reset -u HEAD
294 exit
296 else
297 # We are invoked directly as the first-class UI.
298 head_arg=HEAD
300 # All the rest are the commits being merged; prepare
301 # the standard merge summary message to be appended to
302 # the given message. If remote is invalid we will die
303 # later in the common codepath so we discard the error
304 # in this loop.
305 merge_msg="$(
306 for remote
308 merge_name "$remote"
309 done |
310 if test "$have_message" = t
311 then
312 git fmt-merge-msg -m "$merge_msg" $log_arg
313 else
314 git fmt-merge-msg $log_arg
318 head=$(git rev-parse --verify "$head_arg"^0) || usage
320 # All the rest are remote heads
321 test "$#" = 0 && usage ;# we need at least one remote head.
322 set_reflog_action "merge $*"
324 remoteheads=
325 for remote
327 remotehead=$(git rev-parse --verify "$remote"^0 2>/dev/null) ||
328 die "$remote - not something we can merge"
329 remoteheads="${remoteheads}$remotehead "
330 eval GITHEAD_$remotehead='"$remote"'
331 export GITHEAD_$remotehead
332 done
333 set x $remoteheads ; shift
335 case "$use_strategies" in
337 case "$#" in
339 var="`git config --get pull.twohead`"
340 if test -n "$var"
341 then
342 use_strategies="$var"
343 else
344 use_strategies="$default_twohead_strategies"
345 fi ;;
347 var="`git config --get pull.octopus`"
348 if test -n "$var"
349 then
350 use_strategies="$var"
351 else
352 use_strategies="$default_octopus_strategies"
353 fi ;;
354 esac
356 esac
358 for s in $use_strategies
360 for ss in $no_fast_forward_strategies
362 case " $s " in
363 *" $ss "*)
364 allow_fast_forward=f
365 break
367 esac
368 done
369 for ss in $no_trivial_strategies
371 case " $s " in
372 *" $ss "*)
373 allow_trivial_merge=f
374 break
376 esac
377 done
378 done
380 case "$#" in
382 common=$(git merge-base --all $head "$@")
385 common=$(git merge-base --all --octopus $head "$@")
387 esac
388 echo "$head" >"$GIT_DIR/ORIG_HEAD"
390 case "$allow_fast_forward,$#,$common,$no_commit" in
391 ?,*,'',*)
392 # No common ancestors found. We need a real merge.
394 ?,1,"$1",*)
395 # If head can reach all the merge then we are up to date.
396 # but first the most common case of merging one remote.
397 finish_up_to_date "Already up-to-date."
398 exit 0
400 t,1,"$head",*)
401 # Again the most common case of merging one remote.
402 echo "Updating $(git rev-parse --short $head)..$(git rev-parse --short $1)"
403 git update-index --refresh 2>/dev/null
404 msg="Fast-forward"
405 if test -n "$have_message"
406 then
407 msg="$msg (no commit created; -m option ignored)"
409 new_head=$(git rev-parse --verify "$1^0") &&
410 git read-tree -v -m -u --exclude-per-directory=.gitignore $head "$new_head" &&
411 finish "$new_head" "$msg" || exit
412 dropsave
413 exit 0
415 ?,1,?*"$LF"?*,*)
416 # We are not doing octopus and not fast-forward. Need a
417 # real merge.
419 ?,1,*,)
420 # We are not doing octopus, not fast-forward, and have only
421 # one common.
422 git update-index --refresh 2>/dev/null
423 case "$allow_trivial_merge,$fast_forward_only" in
425 # See if it is really trivial.
426 git var GIT_COMMITTER_IDENT >/dev/null || exit
427 echo "Trying really trivial in-index merge..."
428 if git read-tree --trivial -m -u -v $common $head "$1" &&
429 result_tree=$(git write-tree)
430 then
431 echo "Wonderful."
432 result_commit=$(
433 printf '%s\n' "$merge_msg" |
434 git commit-tree $result_tree -p HEAD -p "$1"
435 ) || exit
436 finish "$result_commit" "In-index merge"
437 dropsave
438 exit 0
440 echo "Nope."
441 esac
444 # An octopus. If we can reach all the remote we are up to date.
445 up_to_date=t
446 for remote
448 common_one=$(git merge-base --all $head $remote)
449 if test "$common_one" != "$remote"
450 then
451 up_to_date=f
452 break
454 done
455 if test "$up_to_date" = t
456 then
457 finish_up_to_date "Already up-to-date. Yeeah!"
458 exit 0
461 esac
463 if test "$fast_forward_only" = t
464 then
465 die "Not possible to fast-forward, aborting."
468 # We are going to make a new commit.
469 git var GIT_COMMITTER_IDENT >/dev/null || exit
471 # At this point, we need a real merge. No matter what strategy
472 # we use, it would operate on the index, possibly affecting the
473 # working tree, and when resolved cleanly, have the desired tree
474 # in the index -- this means that the index must be in sync with
475 # the $head commit. The strategies are responsible to ensure this.
477 case "$use_strategies" in
478 ?*' '?*)
479 # Stash away the local changes so that we can try more than one.
480 savestate
481 single_strategy=no
484 rm -f "$GIT_DIR/MERGE_STASH"
485 single_strategy=yes
487 esac
489 result_tree= best_cnt=-1 best_strategy= wt_strategy=
490 merge_was_ok=
491 for strategy in $use_strategies
493 test "$wt_strategy" = '' || {
494 echo "Rewinding the tree to pristine..."
495 restorestate
497 case "$single_strategy" in
499 echo "Trying merge strategy $strategy..."
501 esac
503 # Remember which strategy left the state in the working tree
504 wt_strategy=$strategy
506 eval 'git-merge-$strategy '"$xopt"' $common -- "$head_arg" "$@"'
507 exit=$?
508 if test "$no_commit" = t && test "$exit" = 0
509 then
510 merge_was_ok=t
511 exit=1 ;# pretend it left conflicts.
514 test "$exit" = 0 || {
516 # The backend exits with 1 when conflicts are left to be resolved,
517 # with 2 when it does not handle the given merge at all.
519 if test "$exit" -eq 1
520 then
521 cnt=`{
522 git diff-files --name-only
523 git ls-files --unmerged
524 } | wc -l`
525 if test $best_cnt -le 0 -o $cnt -le $best_cnt
526 then
527 best_strategy=$strategy
528 best_cnt=$cnt
531 continue
534 # Automerge succeeded.
535 result_tree=$(git write-tree) && break
536 done
538 # If we have a resulting tree, that means the strategy module
539 # auto resolved the merge cleanly.
540 if test '' != "$result_tree"
541 then
542 if test "$allow_fast_forward" = "t"
543 then
544 parents=$(git merge-base --independent "$head" "$@")
545 else
546 parents=$(git rev-parse "$head" "$@")
548 parents=$(echo "$parents" | sed -e 's/^/-p /')
549 result_commit=$(printf '%s\n' "$merge_msg" | git commit-tree $result_tree $parents) || exit
550 finish "$result_commit" "Merge made by $wt_strategy."
551 dropsave
552 exit 0
555 # Pick the result from the best strategy and have the user fix it up.
556 case "$best_strategy" in
558 restorestate
559 case "$use_strategies" in
560 ?*' '?*)
561 echo >&2 "No merge strategy handled the merge."
564 echo >&2 "Merge with strategy $use_strategies failed."
566 esac
567 exit 2
569 "$wt_strategy")
570 # We already have its result in the working tree.
573 echo "Rewinding the tree to pristine..."
574 restorestate
575 echo "Using the $best_strategy to prepare resolving by hand."
576 git-merge-$best_strategy $common -- "$head_arg" "$@"
578 esac
580 if test "$squash" = t
581 then
582 finish
583 else
584 for remote
586 echo $remote
587 done >"$GIT_DIR/MERGE_HEAD"
588 printf '%s\n' "$merge_msg" >"$GIT_DIR/MERGE_MSG"
591 if test "$merge_was_ok" = t
592 then
593 echo >&2 \
594 "Automatic merge went well; stopped before committing as requested"
595 exit 0
596 else
598 echo '
599 Conflicts:
601 git ls-files --unmerged |
602 sed -e 's/^[^ ]* / /' |
603 uniq
604 } >>"$GIT_DIR/MERGE_MSG"
605 git rerere
606 die "Automatic merge failed; fix conflicts and then commit the result."