gitweb: Create Gitweb::Util module
[git/jnareb-git/bp-gitweb.git] / contrib / examples / git-merge.sh
blob7b922c39480c8b78d0687cbabac59672f9ae6ede
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 rerere-autoupdate update index with any reused conflict resolution
20 s,strategy= merge strategy to use
21 X= option for selected merge strategy
22 m,message= message to be used for the merge commit (if any)
25 SUBDIRECTORY_OK=Yes
26 . git-sh-setup
27 require_work_tree
28 cd_to_toplevel
30 test -z "$(git ls-files -u)" ||
31 die "Merge is not possible because you have unmerged files."
33 ! test -e "$GIT_DIR/MERGE_HEAD" ||
34 die 'You have not concluded your merge (MERGE_HEAD exists).'
36 LF='
39 all_strategies='recur recursive octopus resolve stupid ours subtree'
40 all_strategies="$all_strategies recursive-ours recursive-theirs"
41 not_strategies='base file index tree'
42 default_twohead_strategies='recursive'
43 default_octopus_strategies='octopus'
44 no_fast_forward_strategies='subtree ours'
45 no_trivial_strategies='recursive recur subtree ours recursive-ours recursive-theirs'
46 use_strategies=
47 xopt=
49 allow_fast_forward=t
50 fast_forward_only=
51 allow_trivial_merge=t
52 squash= no_commit= log_arg= rr_arg=
54 dropsave() {
55 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
56 "$GIT_DIR/MERGE_STASH" "$GIT_DIR/MERGE_MODE" || exit 1
59 savestate() {
60 # Stash away any local modifications.
61 git stash create >"$GIT_DIR/MERGE_STASH"
64 restorestate() {
65 if test -f "$GIT_DIR/MERGE_STASH"
66 then
67 git reset --hard $head >/dev/null
68 git stash apply $(cat "$GIT_DIR/MERGE_STASH")
69 git update-index --refresh >/dev/null
73 finish_up_to_date () {
74 case "$squash" in
76 echo "$1 (nothing to squash)" ;;
77 '')
78 echo "$1" ;;
79 esac
80 dropsave
83 squash_message () {
84 echo Squashed commit of the following:
85 echo
86 git log --no-merges --pretty=medium ^"$head" $remoteheads
89 finish () {
90 if test '' = "$2"
91 then
92 rlogm="$GIT_REFLOG_ACTION"
93 else
94 echo "$2"
95 rlogm="$GIT_REFLOG_ACTION: $2"
97 case "$squash" in
99 echo "Squash commit -- not updating HEAD"
100 squash_message >"$GIT_DIR/SQUASH_MSG"
103 case "$merge_msg" in
105 echo "No merge message -- not updating HEAD"
108 git update-ref -m "$rlogm" HEAD "$1" "$head" || exit 1
109 git gc --auto
111 esac
113 esac
114 case "$1" in
118 if test "$show_diffstat" = t
119 then
120 # We want color (if set), but no pager
121 GIT_PAGER='' git diff --stat --summary -M "$head" "$1"
124 esac
126 # Run a post-merge hook
127 if test -x "$GIT_DIR"/hooks/post-merge
128 then
129 case "$squash" in
131 "$GIT_DIR"/hooks/post-merge 1
134 "$GIT_DIR"/hooks/post-merge 0
136 esac
140 merge_name () {
141 remote="$1"
142 rh=$(git rev-parse --verify "$remote^0" 2>/dev/null) || return
143 if truname=$(expr "$remote" : '\(.*\)~[0-9]*$') &&
144 git show-ref -q --verify "refs/heads/$truname" 2>/dev/null
145 then
146 echo "$rh branch '$truname' (early part) of ."
147 return
149 if found_ref=$(git rev-parse --symbolic-full-name --verify \
150 "$remote" 2>/dev/null)
151 then
152 expanded=$(git check-ref-format --branch "$remote") ||
153 exit
154 if test "${found_ref#refs/heads/}" != "$found_ref"
155 then
156 echo "$rh branch '$expanded' of ."
157 return
158 elif test "${found_ref#refs/remotes/}" != "$found_ref"
159 then
160 echo "$rh remote branch '$expanded' of ."
161 return
164 if test "$remote" = "FETCH_HEAD" -a -r "$GIT_DIR/FETCH_HEAD"
165 then
166 sed -e 's/ not-for-merge / /' -e 1q \
167 "$GIT_DIR/FETCH_HEAD"
168 return
170 echo "$rh commit '$remote'"
173 parse_config () {
174 while test $# != 0; do
175 case "$1" in
176 -n|--no-stat|--no-summary)
177 show_diffstat=false ;;
178 --stat|--summary)
179 show_diffstat=t ;;
180 --log|--no-log)
181 log_arg=$1 ;;
182 --squash)
183 test "$allow_fast_forward" = t ||
184 die "You cannot combine --squash with --no-ff."
185 squash=t no_commit=t ;;
186 --no-squash)
187 squash= no_commit= ;;
188 --commit)
189 no_commit= ;;
190 --no-commit)
191 no_commit=t ;;
192 --ff)
193 allow_fast_forward=t ;;
194 --no-ff)
195 test "$squash" != t ||
196 die "You cannot combine --squash with --no-ff."
197 test "$fast_forward_only" != t ||
198 die "You cannot combine --ff-only with --no-ff."
199 allow_fast_forward=f ;;
200 --ff-only)
201 test "$allow_fast_forward" != f ||
202 die "You cannot combine --ff-only with --no-ff."
203 fast_forward_only=t ;;
204 --rerere-autoupdate|--no-rerere-autoupdate)
205 rr_arg=$1 ;;
206 -s|--strategy)
207 shift
208 case " $all_strategies " in
209 *" $1 "*)
210 use_strategies="$use_strategies$1 "
213 case " $not_strategies " in
214 *" $1 "*)
215 false
216 esac &&
217 type "git-merge-$1" >/dev/null 2>&1 ||
218 die "available strategies are: $all_strategies"
219 use_strategies="$use_strategies$1 "
221 esac
224 shift
225 xopt="${xopt:+$xopt }$(git rev-parse --sq-quote "--$1")"
227 -m|--message)
228 shift
229 merge_msg="$1"
230 have_message=t
233 shift
234 break ;;
235 *) usage ;;
236 esac
237 shift
238 done
239 args_left=$#
242 test $# != 0 || usage
244 have_message=
246 if branch=$(git-symbolic-ref -q HEAD)
247 then
248 mergeopts=$(git config "branch.${branch#refs/heads/}.mergeoptions")
249 if test -n "$mergeopts"
250 then
251 parse_config $mergeopts --
255 parse_config "$@"
256 while test $args_left -lt $#; do shift; done
258 if test -z "$show_diffstat"; then
259 test "$(git config --bool merge.diffstat)" = false && show_diffstat=false
260 test "$(git config --bool merge.stat)" = false && show_diffstat=false
261 test -z "$show_diffstat" && show_diffstat=t
264 # This could be traditional "merge <msg> HEAD <commit>..." and the
265 # way we can tell it is to see if the second token is HEAD, but some
266 # people might have misused the interface and used a committish that
267 # is the same as HEAD there instead. Traditional format never would
268 # have "-m" so it is an additional safety measure to check for it.
270 if test -z "$have_message" &&
271 second_token=$(git rev-parse --verify "$2^0" 2>/dev/null) &&
272 head_commit=$(git rev-parse --verify "HEAD" 2>/dev/null) &&
273 test "$second_token" = "$head_commit"
274 then
275 merge_msg="$1"
276 shift
277 head_arg="$1"
278 shift
279 elif ! git rev-parse --verify HEAD >/dev/null 2>&1
280 then
281 # If the merged head is a valid one there is no reason to
282 # forbid "git merge" into a branch yet to be born. We do
283 # the same for "git pull".
284 if test 1 -ne $#
285 then
286 echo >&2 "Can merge only exactly one commit into empty head"
287 exit 1
290 test "$squash" != t ||
291 die "Squash commit into empty head not supported yet"
292 test "$allow_fast_forward" = t ||
293 die "Non-fast-forward into an empty head does not make sense"
294 rh=$(git rev-parse --verify "$1^0") ||
295 die "$1 - not something we can merge"
297 git update-ref -m "initial pull" HEAD "$rh" "" &&
298 git read-tree --reset -u HEAD
299 exit
301 else
302 # We are invoked directly as the first-class UI.
303 head_arg=HEAD
305 # All the rest are the commits being merged; prepare
306 # the standard merge summary message to be appended to
307 # the given message. If remote is invalid we will die
308 # later in the common codepath so we discard the error
309 # in this loop.
310 merge_msg="$(
311 for remote
313 merge_name "$remote"
314 done |
315 if test "$have_message" = t
316 then
317 git fmt-merge-msg -m "$merge_msg" $log_arg
318 else
319 git fmt-merge-msg $log_arg
323 head=$(git rev-parse --verify "$head_arg"^0) || usage
325 # All the rest are remote heads
326 test "$#" = 0 && usage ;# we need at least one remote head.
327 set_reflog_action "merge $*"
329 remoteheads=
330 for remote
332 remotehead=$(git rev-parse --verify "$remote"^0 2>/dev/null) ||
333 die "$remote - not something we can merge"
334 remoteheads="${remoteheads}$remotehead "
335 eval GITHEAD_$remotehead='"$remote"'
336 export GITHEAD_$remotehead
337 done
338 set x $remoteheads ; shift
340 case "$use_strategies" in
342 case "$#" in
344 var="`git config --get pull.twohead`"
345 if test -n "$var"
346 then
347 use_strategies="$var"
348 else
349 use_strategies="$default_twohead_strategies"
350 fi ;;
352 var="`git config --get pull.octopus`"
353 if test -n "$var"
354 then
355 use_strategies="$var"
356 else
357 use_strategies="$default_octopus_strategies"
358 fi ;;
359 esac
361 esac
363 for s in $use_strategies
365 for ss in $no_fast_forward_strategies
367 case " $s " in
368 *" $ss "*)
369 allow_fast_forward=f
370 break
372 esac
373 done
374 for ss in $no_trivial_strategies
376 case " $s " in
377 *" $ss "*)
378 allow_trivial_merge=f
379 break
381 esac
382 done
383 done
385 case "$#" in
387 common=$(git merge-base --all $head "$@")
390 common=$(git merge-base --all --octopus $head "$@")
392 esac
393 echo "$head" >"$GIT_DIR/ORIG_HEAD"
395 case "$allow_fast_forward,$#,$common,$no_commit" in
396 ?,*,'',*)
397 # No common ancestors found. We need a real merge.
399 ?,1,"$1",*)
400 # If head can reach all the merge then we are up to date.
401 # but first the most common case of merging one remote.
402 finish_up_to_date "Already up-to-date."
403 exit 0
405 t,1,"$head",*)
406 # Again the most common case of merging one remote.
407 echo "Updating $(git rev-parse --short $head)..$(git rev-parse --short $1)"
408 git update-index --refresh 2>/dev/null
409 msg="Fast-forward"
410 if test -n "$have_message"
411 then
412 msg="$msg (no commit created; -m option ignored)"
414 new_head=$(git rev-parse --verify "$1^0") &&
415 git read-tree -v -m -u --exclude-per-directory=.gitignore $head "$new_head" &&
416 finish "$new_head" "$msg" || exit
417 dropsave
418 exit 0
420 ?,1,?*"$LF"?*,*)
421 # We are not doing octopus and not fast-forward. Need a
422 # real merge.
424 ?,1,*,)
425 # We are not doing octopus, not fast-forward, and have only
426 # one common.
427 git update-index --refresh 2>/dev/null
428 case "$allow_trivial_merge,$fast_forward_only" in
430 # See if it is really trivial.
431 git var GIT_COMMITTER_IDENT >/dev/null || exit
432 echo "Trying really trivial in-index merge..."
433 if git read-tree --trivial -m -u -v $common $head "$1" &&
434 result_tree=$(git write-tree)
435 then
436 echo "Wonderful."
437 result_commit=$(
438 printf '%s\n' "$merge_msg" |
439 git commit-tree $result_tree -p HEAD -p "$1"
440 ) || exit
441 finish "$result_commit" "In-index merge"
442 dropsave
443 exit 0
445 echo "Nope."
446 esac
449 # An octopus. If we can reach all the remote we are up to date.
450 up_to_date=t
451 for remote
453 common_one=$(git merge-base --all $head $remote)
454 if test "$common_one" != "$remote"
455 then
456 up_to_date=f
457 break
459 done
460 if test "$up_to_date" = t
461 then
462 finish_up_to_date "Already up-to-date. Yeeah!"
463 exit 0
466 esac
468 if test "$fast_forward_only" = t
469 then
470 die "Not possible to fast-forward, aborting."
473 # We are going to make a new commit.
474 git var GIT_COMMITTER_IDENT >/dev/null || exit
476 # At this point, we need a real merge. No matter what strategy
477 # we use, it would operate on the index, possibly affecting the
478 # working tree, and when resolved cleanly, have the desired tree
479 # in the index -- this means that the index must be in sync with
480 # the $head commit. The strategies are responsible to ensure this.
482 case "$use_strategies" in
483 ?*' '?*)
484 # Stash away the local changes so that we can try more than one.
485 savestate
486 single_strategy=no
489 rm -f "$GIT_DIR/MERGE_STASH"
490 single_strategy=yes
492 esac
494 result_tree= best_cnt=-1 best_strategy= wt_strategy=
495 merge_was_ok=
496 for strategy in $use_strategies
498 test "$wt_strategy" = '' || {
499 echo "Rewinding the tree to pristine..."
500 restorestate
502 case "$single_strategy" in
504 echo "Trying merge strategy $strategy..."
506 esac
508 # Remember which strategy left the state in the working tree
509 wt_strategy=$strategy
511 eval 'git-merge-$strategy '"$xopt"' $common -- "$head_arg" "$@"'
512 exit=$?
513 if test "$no_commit" = t && test "$exit" = 0
514 then
515 merge_was_ok=t
516 exit=1 ;# pretend it left conflicts.
519 test "$exit" = 0 || {
521 # The backend exits with 1 when conflicts are left to be resolved,
522 # with 2 when it does not handle the given merge at all.
524 if test "$exit" -eq 1
525 then
526 cnt=`{
527 git diff-files --name-only
528 git ls-files --unmerged
529 } | wc -l`
530 if test $best_cnt -le 0 -o $cnt -le $best_cnt
531 then
532 best_strategy=$strategy
533 best_cnt=$cnt
536 continue
539 # Automerge succeeded.
540 result_tree=$(git write-tree) && break
541 done
543 # If we have a resulting tree, that means the strategy module
544 # auto resolved the merge cleanly.
545 if test '' != "$result_tree"
546 then
547 if test "$allow_fast_forward" = "t"
548 then
549 parents=$(git merge-base --independent "$head" "$@")
550 else
551 parents=$(git rev-parse "$head" "$@")
553 parents=$(echo "$parents" | sed -e 's/^/-p /')
554 result_commit=$(printf '%s\n' "$merge_msg" | git commit-tree $result_tree $parents) || exit
555 finish "$result_commit" "Merge made by $wt_strategy."
556 dropsave
557 exit 0
560 # Pick the result from the best strategy and have the user fix it up.
561 case "$best_strategy" in
563 restorestate
564 case "$use_strategies" in
565 ?*' '?*)
566 echo >&2 "No merge strategy handled the merge."
569 echo >&2 "Merge with strategy $use_strategies failed."
571 esac
572 exit 2
574 "$wt_strategy")
575 # We already have its result in the working tree.
578 echo "Rewinding the tree to pristine..."
579 restorestate
580 echo "Using the $best_strategy to prepare resolving by hand."
581 git-merge-$best_strategy $common -- "$head_arg" "$@"
583 esac
585 if test "$squash" = t
586 then
587 finish
588 else
589 for remote
591 echo $remote
592 done >"$GIT_DIR/MERGE_HEAD"
593 printf '%s\n' "$merge_msg" >"$GIT_DIR/MERGE_MSG" ||
594 die "Could not write to $GIT_DIR/MERGE_MSG"
595 if test "$allow_fast_forward" != t
596 then
597 printf "%s" no-ff
598 else
600 fi >"$GIT_DIR/MERGE_MODE" ||
601 die "Could not write to $GIT_DIR/MERGE_MODE"
604 if test "$merge_was_ok" = t
605 then
606 echo >&2 \
607 "Automatic merge went well; stopped before committing as requested"
608 exit 0
609 else
611 echo '
612 Conflicts:
614 git ls-files --unmerged |
615 sed -e 's/^[^ ]* / /' |
616 uniq
617 } >>"$GIT_DIR/MERGE_MSG"
618 git rerere $rr_arg
619 die "Automatic merge failed; fix conflicts and then commit the result."