merge script: notice @{-1} shorthand
[alt-git.git] / contrib / examples / git-merge.sh
blobbed55a48dfbbea5055a2bb5cbe3436feb5c4458f
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" "$GIT_DIR/MERGE_MODE" || 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 expanded=$(git check-ref-format --branch "$remote") ||
152 exit
153 if test "${found_ref#refs/heads/}" != "$found_ref"
154 then
155 echo "$rh branch '$expanded' of ."
156 return
157 elif test "${found_ref#refs/remotes/}" != "$found_ref"
158 then
159 echo "$rh remote branch '$expanded' of ."
160 return
163 if test "$remote" = "FETCH_HEAD" -a -r "$GIT_DIR/FETCH_HEAD"
164 then
165 sed -e 's/ not-for-merge / /' -e 1q \
166 "$GIT_DIR/FETCH_HEAD"
167 return
169 echo "$rh commit '$remote'"
172 parse_config () {
173 while test $# != 0; do
174 case "$1" in
175 -n|--no-stat|--no-summary)
176 show_diffstat=false ;;
177 --stat|--summary)
178 show_diffstat=t ;;
179 --log|--no-log)
180 log_arg=$1 ;;
181 --squash)
182 test "$allow_fast_forward" = t ||
183 die "You cannot combine --squash with --no-ff."
184 squash=t no_commit=t ;;
185 --no-squash)
186 squash= no_commit= ;;
187 --commit)
188 no_commit= ;;
189 --no-commit)
190 no_commit=t ;;
191 --ff)
192 allow_fast_forward=t ;;
193 --no-ff)
194 test "$squash" != t ||
195 die "You cannot combine --squash with --no-ff."
196 test "$fast_forward_only" != t ||
197 die "You cannot combine --ff-only with --no-ff."
198 allow_fast_forward=f ;;
199 --ff-only)
200 test "$allow_fast_forward" != f ||
201 die "You cannot combine --ff-only with --no-ff."
202 fast_forward_only=t ;;
203 -s|--strategy)
204 shift
205 case " $all_strategies " in
206 *" $1 "*)
207 use_strategies="$use_strategies$1 "
210 case " $not_strategies " in
211 *" $1 "*)
212 false
213 esac &&
214 type "git-merge-$1" >/dev/null 2>&1 ||
215 die "available strategies are: $all_strategies"
216 use_strategies="$use_strategies$1 "
218 esac
221 shift
222 xopt="${xopt:+$xopt }$(git rev-parse --sq-quote "--$1")"
224 -m|--message)
225 shift
226 merge_msg="$1"
227 have_message=t
230 shift
231 break ;;
232 *) usage ;;
233 esac
234 shift
235 done
236 args_left=$#
239 test $# != 0 || usage
241 have_message=
243 if branch=$(git-symbolic-ref -q HEAD)
244 then
245 mergeopts=$(git config "branch.${branch#refs/heads/}.mergeoptions")
246 if test -n "$mergeopts"
247 then
248 parse_config $mergeopts --
252 parse_config "$@"
253 while test $args_left -lt $#; do shift; done
255 if test -z "$show_diffstat"; then
256 test "$(git config --bool merge.diffstat)" = false && show_diffstat=false
257 test "$(git config --bool merge.stat)" = false && show_diffstat=false
258 test -z "$show_diffstat" && show_diffstat=t
261 # This could be traditional "merge <msg> HEAD <commit>..." and the
262 # way we can tell it is to see if the second token is HEAD, but some
263 # people might have misused the interface and used a committish that
264 # is the same as HEAD there instead. Traditional format never would
265 # have "-m" so it is an additional safety measure to check for it.
267 if test -z "$have_message" &&
268 second_token=$(git rev-parse --verify "$2^0" 2>/dev/null) &&
269 head_commit=$(git rev-parse --verify "HEAD" 2>/dev/null) &&
270 test "$second_token" = "$head_commit"
271 then
272 merge_msg="$1"
273 shift
274 head_arg="$1"
275 shift
276 elif ! git rev-parse --verify HEAD >/dev/null 2>&1
277 then
278 # If the merged head is a valid one there is no reason to
279 # forbid "git merge" into a branch yet to be born. We do
280 # the same for "git pull".
281 if test 1 -ne $#
282 then
283 echo >&2 "Can merge only exactly one commit into empty head"
284 exit 1
287 test "$squash" != t ||
288 die "Squash commit into empty head not supported yet"
289 test "$allow_fast_forward" = t ||
290 die "Non-fast-forward into an empty head does not make sense"
291 rh=$(git rev-parse --verify "$1^0") ||
292 die "$1 - not something we can merge"
294 git update-ref -m "initial pull" HEAD "$rh" "" &&
295 git read-tree --reset -u HEAD
296 exit
298 else
299 # We are invoked directly as the first-class UI.
300 head_arg=HEAD
302 # All the rest are the commits being merged; prepare
303 # the standard merge summary message to be appended to
304 # the given message. If remote is invalid we will die
305 # later in the common codepath so we discard the error
306 # in this loop.
307 merge_msg="$(
308 for remote
310 merge_name "$remote"
311 done |
312 if test "$have_message" = t
313 then
314 git fmt-merge-msg -m "$merge_msg" $log_arg
315 else
316 git fmt-merge-msg $log_arg
320 head=$(git rev-parse --verify "$head_arg"^0) || usage
322 # All the rest are remote heads
323 test "$#" = 0 && usage ;# we need at least one remote head.
324 set_reflog_action "merge $*"
326 remoteheads=
327 for remote
329 remotehead=$(git rev-parse --verify "$remote"^0 2>/dev/null) ||
330 die "$remote - not something we can merge"
331 remoteheads="${remoteheads}$remotehead "
332 eval GITHEAD_$remotehead='"$remote"'
333 export GITHEAD_$remotehead
334 done
335 set x $remoteheads ; shift
337 case "$use_strategies" in
339 case "$#" in
341 var="`git config --get pull.twohead`"
342 if test -n "$var"
343 then
344 use_strategies="$var"
345 else
346 use_strategies="$default_twohead_strategies"
347 fi ;;
349 var="`git config --get pull.octopus`"
350 if test -n "$var"
351 then
352 use_strategies="$var"
353 else
354 use_strategies="$default_octopus_strategies"
355 fi ;;
356 esac
358 esac
360 for s in $use_strategies
362 for ss in $no_fast_forward_strategies
364 case " $s " in
365 *" $ss "*)
366 allow_fast_forward=f
367 break
369 esac
370 done
371 for ss in $no_trivial_strategies
373 case " $s " in
374 *" $ss "*)
375 allow_trivial_merge=f
376 break
378 esac
379 done
380 done
382 case "$#" in
384 common=$(git merge-base --all $head "$@")
387 common=$(git merge-base --all --octopus $head "$@")
389 esac
390 echo "$head" >"$GIT_DIR/ORIG_HEAD"
392 case "$allow_fast_forward,$#,$common,$no_commit" in
393 ?,*,'',*)
394 # No common ancestors found. We need a real merge.
396 ?,1,"$1",*)
397 # If head can reach all the merge then we are up to date.
398 # but first the most common case of merging one remote.
399 finish_up_to_date "Already up-to-date."
400 exit 0
402 t,1,"$head",*)
403 # Again the most common case of merging one remote.
404 echo "Updating $(git rev-parse --short $head)..$(git rev-parse --short $1)"
405 git update-index --refresh 2>/dev/null
406 msg="Fast-forward"
407 if test -n "$have_message"
408 then
409 msg="$msg (no commit created; -m option ignored)"
411 new_head=$(git rev-parse --verify "$1^0") &&
412 git read-tree -v -m -u --exclude-per-directory=.gitignore $head "$new_head" &&
413 finish "$new_head" "$msg" || exit
414 dropsave
415 exit 0
417 ?,1,?*"$LF"?*,*)
418 # We are not doing octopus and not fast-forward. Need a
419 # real merge.
421 ?,1,*,)
422 # We are not doing octopus, not fast-forward, and have only
423 # one common.
424 git update-index --refresh 2>/dev/null
425 case "$allow_trivial_merge,$fast_forward_only" in
427 # See if it is really trivial.
428 git var GIT_COMMITTER_IDENT >/dev/null || exit
429 echo "Trying really trivial in-index merge..."
430 if git read-tree --trivial -m -u -v $common $head "$1" &&
431 result_tree=$(git write-tree)
432 then
433 echo "Wonderful."
434 result_commit=$(
435 printf '%s\n' "$merge_msg" |
436 git commit-tree $result_tree -p HEAD -p "$1"
437 ) || exit
438 finish "$result_commit" "In-index merge"
439 dropsave
440 exit 0
442 echo "Nope."
443 esac
446 # An octopus. If we can reach all the remote we are up to date.
447 up_to_date=t
448 for remote
450 common_one=$(git merge-base --all $head $remote)
451 if test "$common_one" != "$remote"
452 then
453 up_to_date=f
454 break
456 done
457 if test "$up_to_date" = t
458 then
459 finish_up_to_date "Already up-to-date. Yeeah!"
460 exit 0
463 esac
465 if test "$fast_forward_only" = t
466 then
467 die "Not possible to fast-forward, aborting."
470 # We are going to make a new commit.
471 git var GIT_COMMITTER_IDENT >/dev/null || exit
473 # At this point, we need a real merge. No matter what strategy
474 # we use, it would operate on the index, possibly affecting the
475 # working tree, and when resolved cleanly, have the desired tree
476 # in the index -- this means that the index must be in sync with
477 # the $head commit. The strategies are responsible to ensure this.
479 case "$use_strategies" in
480 ?*' '?*)
481 # Stash away the local changes so that we can try more than one.
482 savestate
483 single_strategy=no
486 rm -f "$GIT_DIR/MERGE_STASH"
487 single_strategy=yes
489 esac
491 result_tree= best_cnt=-1 best_strategy= wt_strategy=
492 merge_was_ok=
493 for strategy in $use_strategies
495 test "$wt_strategy" = '' || {
496 echo "Rewinding the tree to pristine..."
497 restorestate
499 case "$single_strategy" in
501 echo "Trying merge strategy $strategy..."
503 esac
505 # Remember which strategy left the state in the working tree
506 wt_strategy=$strategy
508 eval 'git-merge-$strategy '"$xopt"' $common -- "$head_arg" "$@"'
509 exit=$?
510 if test "$no_commit" = t && test "$exit" = 0
511 then
512 merge_was_ok=t
513 exit=1 ;# pretend it left conflicts.
516 test "$exit" = 0 || {
518 # The backend exits with 1 when conflicts are left to be resolved,
519 # with 2 when it does not handle the given merge at all.
521 if test "$exit" -eq 1
522 then
523 cnt=`{
524 git diff-files --name-only
525 git ls-files --unmerged
526 } | wc -l`
527 if test $best_cnt -le 0 -o $cnt -le $best_cnt
528 then
529 best_strategy=$strategy
530 best_cnt=$cnt
533 continue
536 # Automerge succeeded.
537 result_tree=$(git write-tree) && break
538 done
540 # If we have a resulting tree, that means the strategy module
541 # auto resolved the merge cleanly.
542 if test '' != "$result_tree"
543 then
544 if test "$allow_fast_forward" = "t"
545 then
546 parents=$(git merge-base --independent "$head" "$@")
547 else
548 parents=$(git rev-parse "$head" "$@")
550 parents=$(echo "$parents" | sed -e 's/^/-p /')
551 result_commit=$(printf '%s\n' "$merge_msg" | git commit-tree $result_tree $parents) || exit
552 finish "$result_commit" "Merge made by $wt_strategy."
553 dropsave
554 exit 0
557 # Pick the result from the best strategy and have the user fix it up.
558 case "$best_strategy" in
560 restorestate
561 case "$use_strategies" in
562 ?*' '?*)
563 echo >&2 "No merge strategy handled the merge."
566 echo >&2 "Merge with strategy $use_strategies failed."
568 esac
569 exit 2
571 "$wt_strategy")
572 # We already have its result in the working tree.
575 echo "Rewinding the tree to pristine..."
576 restorestate
577 echo "Using the $best_strategy to prepare resolving by hand."
578 git-merge-$best_strategy $common -- "$head_arg" "$@"
580 esac
582 if test "$squash" = t
583 then
584 finish
585 else
586 for remote
588 echo $remote
589 done >"$GIT_DIR/MERGE_HEAD"
590 printf '%s\n' "$merge_msg" >"$GIT_DIR/MERGE_MSG" ||
591 die "Could not write to $GIT_DIR/MERGE_MSG"
592 if test "$allow_fast_forward" != t
593 then
594 printf "%s" no-ff
595 else
597 fi >"$GIT_DIR/MERGE_MODE" ||
598 die "Could not write to $GIT_DIR/MERGE_MODE"
601 if test "$merge_was_ok" = t
602 then
603 echo >&2 \
604 "Automatic merge went well; stopped before committing as requested"
605 exit 0
606 else
608 echo '
609 Conflicts:
611 git ls-files --unmerged |
612 sed -e 's/^[^ ]* / /' |
613 uniq
614 } >>"$GIT_DIR/MERGE_MSG"
615 git rerere
616 die "Automatic merge failed; fix conflicts and then commit the result."