engine.pl: Fix a recent breakage of the buildsystem generator
[git/dscho.git] / contrib / examples / git-merge.sh
blob8f617fcb7089caedce163871c3eb3a97eadb204b
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 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 all_strategies="$all_strategies recursive-ours recursive-theirs"
35 default_twohead_strategies='recursive'
36 default_octopus_strategies='octopus'
37 no_fast_forward_strategies='subtree ours'
38 no_trivial_strategies='recursive recur subtree ours recursive-ours recursive-theirs'
39 use_strategies=
41 allow_fast_forward=t
42 allow_trivial_merge=t
43 squash= no_commit= log_arg=
45 dropsave() {
46 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
47 "$GIT_DIR/MERGE_STASH" || exit 1
50 savestate() {
51 # Stash away any local modifications.
52 git stash create >"$GIT_DIR/MERGE_STASH"
55 restorestate() {
56 if test -f "$GIT_DIR/MERGE_STASH"
57 then
58 git reset --hard $head >/dev/null
59 git stash apply $(cat "$GIT_DIR/MERGE_STASH")
60 git update-index --refresh >/dev/null
64 finish_up_to_date () {
65 case "$squash" in
67 echo "$1 (nothing to squash)" ;;
68 '')
69 echo "$1" ;;
70 esac
71 dropsave
74 squash_message () {
75 echo Squashed commit of the following:
76 echo
77 git log --no-merges --pretty=medium ^"$head" $remoteheads
80 finish () {
81 if test '' = "$2"
82 then
83 rlogm="$GIT_REFLOG_ACTION"
84 else
85 echo "$2"
86 rlogm="$GIT_REFLOG_ACTION: $2"
88 case "$squash" in
90 echo "Squash commit -- not updating HEAD"
91 squash_message >"$GIT_DIR/SQUASH_MSG"
93 '')
94 case "$merge_msg" in
95 '')
96 echo "No merge message -- not updating HEAD"
99 git update-ref -m "$rlogm" HEAD "$1" "$head" || exit 1
100 git gc --auto
102 esac
104 esac
105 case "$1" in
109 if test "$show_diffstat" = t
110 then
111 # We want color (if set), but no pager
112 GIT_PAGER='' git diff --stat --summary -M "$head" "$1"
115 esac
117 # Run a post-merge hook
118 if test -x "$GIT_DIR"/hooks/post-merge
119 then
120 case "$squash" in
122 "$GIT_DIR"/hooks/post-merge 1
125 "$GIT_DIR"/hooks/post-merge 0
127 esac
131 merge_name () {
132 remote="$1"
133 rh=$(git rev-parse --verify "$remote^0" 2>/dev/null) || return
134 bh=$(git show-ref -s --verify "refs/heads/$remote" 2>/dev/null)
135 if test "$rh" = "$bh"
136 then
137 echo "$rh branch '$remote' of ."
138 elif truname=$(expr "$remote" : '\(.*\)~[1-9][0-9]*$') &&
139 git show-ref -q --verify "refs/heads/$truname" 2>/dev/null
140 then
141 echo "$rh branch '$truname' (early part) of ."
142 elif test "$remote" = "FETCH_HEAD" -a -r "$GIT_DIR/FETCH_HEAD"
143 then
144 sed -e 's/ not-for-merge / /' -e 1q \
145 "$GIT_DIR/FETCH_HEAD"
146 else
147 echo "$rh commit '$remote'"
151 parse_config () {
152 while test $# != 0; do
153 case "$1" in
154 -n|--no-stat|--no-summary)
155 show_diffstat=false ;;
156 --stat|--summary)
157 show_diffstat=t ;;
158 --log|--no-log)
159 log_arg=$1 ;;
160 --squash)
161 test "$allow_fast_forward" = t ||
162 die "You cannot combine --squash with --no-ff."
163 squash=t no_commit=t ;;
164 --no-squash)
165 squash= no_commit= ;;
166 --commit)
167 no_commit= ;;
168 --no-commit)
169 no_commit=t ;;
170 --ff)
171 allow_fast_forward=t ;;
172 --no-ff)
173 test "$squash" != t ||
174 die "You cannot combine --squash with --no-ff."
175 allow_fast_forward=f ;;
176 -s|--strategy)
177 shift
178 case " $all_strategies " in
179 *" $1 "*)
180 use_strategies="$use_strategies$1 " ;;
182 die "available strategies are: $all_strategies" ;;
183 esac
185 -m|--message)
186 shift
187 merge_msg="$1"
188 have_message=t
191 shift
192 break ;;
193 *) usage ;;
194 esac
195 shift
196 done
197 args_left=$#
200 test $# != 0 || usage
202 have_message=
204 if branch=$(git-symbolic-ref -q HEAD)
205 then
206 mergeopts=$(git config "branch.${branch#refs/heads/}.mergeoptions")
207 if test -n "$mergeopts"
208 then
209 parse_config $mergeopts --
213 parse_config "$@"
214 while test $args_left -lt $#; do shift; done
216 if test -z "$show_diffstat"; then
217 test "$(git config --bool merge.diffstat)" = false && show_diffstat=false
218 test "$(git config --bool merge.stat)" = false && show_diffstat=false
219 test -z "$show_diffstat" && show_diffstat=t
222 # This could be traditional "merge <msg> HEAD <commit>..." and the
223 # way we can tell it is to see if the second token is HEAD, but some
224 # people might have misused the interface and used a committish that
225 # is the same as HEAD there instead. Traditional format never would
226 # have "-m" so it is an additional safety measure to check for it.
228 if test -z "$have_message" &&
229 second_token=$(git rev-parse --verify "$2^0" 2>/dev/null) &&
230 head_commit=$(git rev-parse --verify "HEAD" 2>/dev/null) &&
231 test "$second_token" = "$head_commit"
232 then
233 merge_msg="$1"
234 shift
235 head_arg="$1"
236 shift
237 elif ! git rev-parse --verify HEAD >/dev/null 2>&1
238 then
239 # If the merged head is a valid one there is no reason to
240 # forbid "git merge" into a branch yet to be born. We do
241 # the same for "git pull".
242 if test 1 -ne $#
243 then
244 echo >&2 "Can merge only exactly one commit into empty head"
245 exit 1
248 rh=$(git rev-parse --verify "$1^0") ||
249 die "$1 - not something we can merge"
251 git update-ref -m "initial pull" HEAD "$rh" "" &&
252 git read-tree --reset -u HEAD
253 exit
255 else
256 # We are invoked directly as the first-class UI.
257 head_arg=HEAD
259 # All the rest are the commits being merged; prepare
260 # the standard merge summary message to be appended to
261 # the given message. If remote is invalid we will die
262 # later in the common codepath so we discard the error
263 # in this loop.
264 merge_name=$(for remote
266 merge_name "$remote"
267 done | git fmt-merge-msg $log_arg
269 merge_msg="${merge_msg:+$merge_msg$LF$LF}$merge_name"
271 head=$(git rev-parse --verify "$head_arg"^0) || usage
273 # All the rest are remote heads
274 test "$#" = 0 && usage ;# we need at least one remote head.
275 set_reflog_action "merge $*"
277 remoteheads=
278 for remote
280 remotehead=$(git rev-parse --verify "$remote"^0 2>/dev/null) ||
281 die "$remote - not something we can merge"
282 remoteheads="${remoteheads}$remotehead "
283 eval GITHEAD_$remotehead='"$remote"'
284 export GITHEAD_$remotehead
285 done
286 set x $remoteheads ; shift
288 case "$use_strategies" in
290 case "$#" in
292 var="`git config --get pull.twohead`"
293 if test -n "$var"
294 then
295 use_strategies="$var"
296 else
297 use_strategies="$default_twohead_strategies"
298 fi ;;
300 var="`git config --get pull.octopus`"
301 if test -n "$var"
302 then
303 use_strategies="$var"
304 else
305 use_strategies="$default_octopus_strategies"
306 fi ;;
307 esac
309 esac
311 for s in $use_strategies
313 for ss in $no_fast_forward_strategies
315 case " $s " in
316 *" $ss "*)
317 allow_fast_forward=f
318 break
320 esac
321 done
322 for ss in $no_trivial_strategies
324 case " $s " in
325 *" $ss "*)
326 allow_trivial_merge=f
327 break
329 esac
330 done
331 done
333 case "$#" in
335 common=$(git merge-base --all $head "$@")
338 common=$(git show-branch --merge-base $head "$@")
340 esac
341 echo "$head" >"$GIT_DIR/ORIG_HEAD"
343 case "$allow_fast_forward,$#,$common,$no_commit" in
344 ?,*,'',*)
345 # No common ancestors found. We need a real merge.
347 ?,1,"$1",*)
348 # If head can reach all the merge then we are up to date.
349 # but first the most common case of merging one remote.
350 finish_up_to_date "Already up-to-date."
351 exit 0
353 t,1,"$head",*)
354 # Again the most common case of merging one remote.
355 echo "Updating $(git rev-parse --short $head)..$(git rev-parse --short $1)"
356 git update-index --refresh 2>/dev/null
357 msg="Fast-forward"
358 if test -n "$have_message"
359 then
360 msg="$msg (no commit created; -m option ignored)"
362 new_head=$(git rev-parse --verify "$1^0") &&
363 git read-tree -v -m -u --exclude-per-directory=.gitignore $head "$new_head" &&
364 finish "$new_head" "$msg" || exit
365 dropsave
366 exit 0
368 ?,1,?*"$LF"?*,*)
369 # We are not doing octopus and not fast-forward. Need a
370 # real merge.
372 ?,1,*,)
373 # We are not doing octopus, not fast-forward, and have only
374 # one common.
375 git update-index --refresh 2>/dev/null
376 case "$allow_trivial_merge" in
378 # See if it is really trivial.
379 git var GIT_COMMITTER_IDENT >/dev/null || exit
380 echo "Trying really trivial in-index merge..."
381 if git read-tree --trivial -m -u -v $common $head "$1" &&
382 result_tree=$(git write-tree)
383 then
384 echo "Wonderful."
385 result_commit=$(
386 printf '%s\n' "$merge_msg" |
387 git commit-tree $result_tree -p HEAD -p "$1"
388 ) || exit
389 finish "$result_commit" "In-index merge"
390 dropsave
391 exit 0
393 echo "Nope."
394 esac
397 # An octopus. If we can reach all the remote we are up to date.
398 up_to_date=t
399 for remote
401 common_one=$(git merge-base --all $head $remote)
402 if test "$common_one" != "$remote"
403 then
404 up_to_date=f
405 break
407 done
408 if test "$up_to_date" = t
409 then
410 finish_up_to_date "Already up-to-date. Yeeah!"
411 exit 0
414 esac
416 # We are going to make a new commit.
417 git var GIT_COMMITTER_IDENT >/dev/null || exit
419 # At this point, we need a real merge. No matter what strategy
420 # we use, it would operate on the index, possibly affecting the
421 # working tree, and when resolved cleanly, have the desired tree
422 # in the index -- this means that the index must be in sync with
423 # the $head commit. The strategies are responsible to ensure this.
425 case "$use_strategies" in
426 ?*' '?*)
427 # Stash away the local changes so that we can try more than one.
428 savestate
429 single_strategy=no
432 rm -f "$GIT_DIR/MERGE_STASH"
433 single_strategy=yes
435 esac
437 result_tree= best_cnt=-1 best_strategy= wt_strategy=
438 merge_was_ok=
439 for strategy in $use_strategies
441 test "$wt_strategy" = '' || {
442 echo "Rewinding the tree to pristine..."
443 restorestate
445 case "$single_strategy" in
447 echo "Trying merge strategy $strategy..."
449 esac
451 # Remember which strategy left the state in the working tree
452 wt_strategy=$strategy
454 git-merge-$strategy $common -- "$head_arg" "$@"
455 exit=$?
456 if test "$no_commit" = t && test "$exit" = 0
457 then
458 merge_was_ok=t
459 exit=1 ;# pretend it left conflicts.
462 test "$exit" = 0 || {
464 # The backend exits with 1 when conflicts are left to be resolved,
465 # with 2 when it does not handle the given merge at all.
467 if test "$exit" -eq 1
468 then
469 cnt=`{
470 git diff-files --name-only
471 git ls-files --unmerged
472 } | wc -l`
473 if test $best_cnt -le 0 -o $cnt -le $best_cnt
474 then
475 best_strategy=$strategy
476 best_cnt=$cnt
479 continue
482 # Automerge succeeded.
483 result_tree=$(git write-tree) && break
484 done
486 # If we have a resulting tree, that means the strategy module
487 # auto resolved the merge cleanly.
488 if test '' != "$result_tree"
489 then
490 if test "$allow_fast_forward" = "t"
491 then
492 parents=$(git show-branch --independent "$head" "$@")
493 else
494 parents=$(git rev-parse "$head" "$@")
496 parents=$(echo "$parents" | sed -e 's/^/-p /')
497 result_commit=$(printf '%s\n' "$merge_msg" | git commit-tree $result_tree $parents) || exit
498 finish "$result_commit" "Merge made by $wt_strategy."
499 dropsave
500 exit 0
503 # Pick the result from the best strategy and have the user fix it up.
504 case "$best_strategy" in
506 restorestate
507 case "$use_strategies" in
508 ?*' '?*)
509 echo >&2 "No merge strategy handled the merge."
512 echo >&2 "Merge with strategy $use_strategies failed."
514 esac
515 exit 2
517 "$wt_strategy")
518 # We already have its result in the working tree.
521 echo "Rewinding the tree to pristine..."
522 restorestate
523 echo "Using the $best_strategy to prepare resolving by hand."
524 git-merge-$best_strategy $common -- "$head_arg" "$@"
526 esac
528 if test "$squash" = t
529 then
530 finish
531 else
532 for remote
534 echo $remote
535 done >"$GIT_DIR/MERGE_HEAD"
536 printf '%s\n' "$merge_msg" >"$GIT_DIR/MERGE_MSG"
539 if test "$merge_was_ok" = t
540 then
541 echo >&2 \
542 "Automatic merge went well; stopped before committing as requested"
543 exit 0
544 else
546 echo '
547 Conflicts:
549 git ls-files --unmerged |
550 sed -e 's/^[^ ]* / /' |
551 uniq
552 } >>"$GIT_DIR/MERGE_MSG"
553 git rerere
554 die "Automatic merge failed; fix conflicts and then commit the result."