fetch-pack: Enable sideband communication.
[git/dscho.git] / git-merge.sh
blobb6f49c0f8026a4be2322f387d25a04dc3452a331
1 #!/bin/sh
3 # Copyright (c) 2005 Junio C Hamano
6 USAGE='[-n] [--summary] [--[no-]commit] [--[no-]squash] [--[no-]ff] [-s <strategy>] [-m=<merge-message>] <commit>+'
8 SUBDIRECTORY_OK=Yes
9 . git-sh-setup
10 require_work_tree
11 cd_to_toplevel
13 test -z "$(git ls-files -u)" ||
14 die "You are in the middle of a conflicted merge."
16 LF='
19 all_strategies='recur recursive octopus resolve stupid ours subtree'
20 default_twohead_strategies='recursive'
21 default_octopus_strategies='octopus'
22 no_fast_forward_strategies='subtree ours'
23 no_trivial_strategies='recursive recur subtree ours'
24 use_strategies=
26 allow_fast_forward=t
27 allow_trivial_merge=t
29 # Fix some commands on Windows
30 case $(uname -s) in
31 *MINGW*)
32 # there's no cpio; emulate with tar
33 cpio () {
34 case "$*" in
35 "-0 -o")
36 tar --create --file=- --null --files-from=-
38 "-iuv")
39 tar xvf -
41 *) die "internal error: unexpected cpio $*";;
42 esac
45 esac
47 dropsave() {
48 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
49 "$GIT_DIR/MERGE_STASH" || exit 1
52 savestate() {
53 # Stash away any local modifications.
54 git stash create >"$GIT_DIR/MERGE_STASH"
57 restorestate() {
58 if test -f "$GIT_DIR/MERGE_STASH"
59 then
60 git reset --hard $head >/dev/null
61 git stash apply $(cat "$GIT_DIR/MERGE_STASH")
62 git update-index --refresh >/dev/null
66 finish_up_to_date () {
67 case "$squash" in
69 echo "$1 (nothing to squash)" ;;
70 '')
71 echo "$1" ;;
72 esac
73 dropsave
76 squash_message () {
77 echo Squashed commit of the following:
78 echo
79 git log --no-merges ^"$head" $remoteheads
82 finish () {
83 if test '' = "$2"
84 then
85 rlogm="$GIT_REFLOG_ACTION"
86 else
87 echo "$2"
88 rlogm="$GIT_REFLOG_ACTION: $2"
90 case "$squash" in
92 echo "Squash commit -- not updating HEAD"
93 squash_message >"$GIT_DIR/SQUASH_MSG"
95 '')
96 case "$merge_msg" in
97 '')
98 echo "No merge message -- not updating HEAD"
101 git update-ref -m "$rlogm" HEAD "$1" "$head" || exit 1
102 git gc --auto
104 esac
106 esac
107 case "$1" in
111 if test "$show_diffstat" = t
112 then
113 # We want color (if set), but no pager
114 GIT_PAGER='' git diff --stat --summary -M "$head" "$1"
117 esac
119 # Run a post-merge hook
120 if test -x "$GIT_DIR"/hooks/post-merge
121 then
122 case "$squash" in
124 "$GIT_DIR"/hooks/post-merge 1
127 "$GIT_DIR"/hooks/post-merge 0
129 esac
133 merge_name () {
134 remote="$1"
135 rh=$(git rev-parse --verify "$remote^0" 2>/dev/null) || return
136 bh=$(git show-ref -s --verify "refs/heads/$remote" 2>/dev/null)
137 if test "$rh" = "$bh"
138 then
139 echo "$rh branch '$remote' of ."
140 elif truname=$(expr "$remote" : '\(.*\)~[1-9][0-9]*$') &&
141 git show-ref -q --verify "refs/heads/$truname" 2>/dev/null
142 then
143 echo "$rh branch '$truname' (early part) of ."
144 elif test "$remote" = "FETCH_HEAD" -a -r "$GIT_DIR/FETCH_HEAD"
145 then
146 sed -e 's/ not-for-merge / /' -e 1q \
147 "$GIT_DIR/FETCH_HEAD"
148 else
149 echo "$rh commit '$remote'"
153 parse_option () {
154 case "$1" in
155 -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\
156 --no-summa|--no-summar|--no-summary)
157 show_diffstat=false ;;
158 --summary)
159 show_diffstat=t ;;
160 --sq|--squ|--squa|--squas|--squash)
161 allow_fast_forward=t squash=t no_commit=t ;;
162 --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
163 allow_fast_forward=t squash= no_commit= ;;
164 --c|--co|--com|--comm|--commi|--commit)
165 allow_fast_forward=t squash= no_commit= ;;
166 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
167 allow_fast_forward=t squash= no_commit=t ;;
168 --ff)
169 allow_fast_forward=t squash= no_commit= ;;
170 --no-ff)
171 allow_fast_forward=false squash= no_commit= ;;
172 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
173 --strateg=*|--strategy=*|\
174 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
175 case "$#,$1" in
176 *,*=*)
177 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
178 1,*)
179 usage ;;
181 strategy="$2"
182 shift ;;
183 esac
184 case " $all_strategies " in
185 *" $strategy "*)
186 use_strategies="$use_strategies$strategy " ;;
188 die "available strategies are: $all_strategies" ;;
189 esac
191 -m=*|--m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
192 merge_msg=`expr "z$1" : 'z-[^=]*=\(.*\)'`
193 have_message=t
195 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
196 shift
197 case "$#" in
198 1) usage ;;
199 esac
200 merge_msg="$1"
201 have_message=t
203 -*) usage ;;
204 *) return 1 ;;
205 esac
206 shift
207 args_left=$#
210 parse_config () {
211 while test $# -gt 0
213 parse_option "$@" || usage
214 while test $args_left -lt $#
216 shift
217 done
218 done
221 test $# != 0 || usage
223 have_message=
225 if branch=$(git-symbolic-ref -q HEAD)
226 then
227 mergeopts=$(git config "branch.${branch#refs/heads/}.mergeoptions")
228 if test -n "$mergeopts"
229 then
230 parse_config $mergeopts
234 while parse_option "$@"
236 while test $args_left -lt $#
238 shift
239 done
240 done
242 if test -z "$show_diffstat"; then
243 test "$(git config --bool merge.diffstat)" = false && show_diffstat=false
244 test -z "$show_diffstat" && show_diffstat=t
247 # This could be traditional "merge <msg> HEAD <commit>..." and the
248 # way we can tell it is to see if the second token is HEAD, but some
249 # people might have misused the interface and used a committish that
250 # is the same as HEAD there instead. Traditional format never would
251 # have "-m" so it is an additional safety measure to check for it.
253 if test -z "$have_message" &&
254 second_token=$(git rev-parse --verify "$2^0" 2>/dev/null) &&
255 head_commit=$(git rev-parse --verify "HEAD" 2>/dev/null) &&
256 test "$second_token" = "$head_commit"
257 then
258 merge_msg="$1"
259 shift
260 head_arg="$1"
261 shift
262 elif ! git rev-parse --verify HEAD >/dev/null 2>&1
263 then
264 # If the merged head is a valid one there is no reason to
265 # forbid "git merge" into a branch yet to be born. We do
266 # the same for "git pull".
267 if test 1 -ne $#
268 then
269 echo >&2 "Can merge only exactly one commit into empty head"
270 exit 1
273 rh=$(git rev-parse --verify "$1^0") ||
274 die "$1 - not something we can merge"
276 git update-ref -m "initial pull" HEAD "$rh" "" &&
277 git read-tree --reset -u HEAD
278 exit
280 else
281 # We are invoked directly as the first-class UI.
282 head_arg=HEAD
284 # All the rest are the commits being merged; prepare
285 # the standard merge summary message to be appended to
286 # the given message. If remote is invalid we will die
287 # later in the common codepath so we discard the error
288 # in this loop.
289 merge_name=$(for remote
291 merge_name "$remote"
292 done | git fmt-merge-msg
294 merge_msg="${merge_msg:+$merge_msg$LF$LF}$merge_name"
296 head=$(git rev-parse --verify "$head_arg"^0) || usage
298 # All the rest are remote heads
299 test "$#" = 0 && usage ;# we need at least one remote head.
300 set_reflog_action "merge $*"
302 remoteheads=
303 for remote
305 remotehead=$(git rev-parse --verify "$remote"^0 2>/dev/null) ||
306 die "$remote - not something we can merge"
307 remoteheads="${remoteheads}$remotehead "
308 eval GITHEAD_$remotehead='"$remote"'
309 export GITHEAD_$remotehead
310 done
311 set x $remoteheads ; shift
313 case "$use_strategies" in
315 case "$#" in
317 var="`git config --get pull.twohead`"
318 if test -n "$var"
319 then
320 use_strategies="$var"
321 else
322 use_strategies="$default_twohead_strategies"
323 fi ;;
325 var="`git config --get pull.octopus`"
326 if test -n "$var"
327 then
328 use_strategies="$var"
329 else
330 use_strategies="$default_octopus_strategies"
331 fi ;;
332 esac
334 esac
336 for s in $use_strategies
338 for ss in $no_fast_forward_strategies
340 case " $s " in
341 *" $ss "*)
342 allow_fast_forward=f
343 break
345 esac
346 done
347 for ss in $no_trivial_strategies
349 case " $s " in
350 *" $ss "*)
351 allow_trivial_merge=f
352 break
354 esac
355 done
356 done
358 case "$#" in
360 common=$(git merge-base --all $head "$@")
363 common=$(git show-branch --merge-base $head "$@")
365 esac
366 echo "$head" >"$GIT_DIR/ORIG_HEAD"
368 case "$allow_fast_forward,$#,$common,$no_commit" in
369 ?,*,'',*)
370 # No common ancestors found. We need a real merge.
372 ?,1,"$1",*)
373 # If head can reach all the merge then we are up to date.
374 # but first the most common case of merging one remote.
375 finish_up_to_date "Already up-to-date."
376 exit 0
378 t,1,"$head",*)
379 # Again the most common case of merging one remote.
380 echo "Updating $(git rev-parse --short $head)..$(git rev-parse --short $1)"
381 git update-index --refresh 2>/dev/null
382 msg="Fast forward"
383 if test -n "$have_message"
384 then
385 msg="$msg (no commit created; -m option ignored)"
387 new_head=$(git rev-parse --verify "$1^0") &&
388 git read-tree -v -m -u --exclude-per-directory=.gitignore $head "$new_head" &&
389 finish "$new_head" "$msg" || exit
390 dropsave
391 exit 0
393 ?,1,?*"$LF"?*,*)
394 # We are not doing octopus and not fast forward. Need a
395 # real merge.
397 ?,1,*,)
398 # We are not doing octopus, not fast forward, and have only
399 # one common.
400 git update-index --refresh 2>/dev/null
401 case "$allow_trivial_merge" in
403 # See if it is really trivial.
404 git var GIT_COMMITTER_IDENT >/dev/null || exit
405 echo "Trying really trivial in-index merge..."
406 if git read-tree --trivial -m -u -v $common $head "$1" &&
407 result_tree=$(git write-tree)
408 then
409 echo "Wonderful."
410 result_commit=$(
411 printf '%s\n' "$merge_msg" |
412 git commit-tree $result_tree -p HEAD -p "$1"
413 ) || exit
414 finish "$result_commit" "In-index merge"
415 dropsave
416 exit 0
418 echo "Nope."
419 esac
422 # An octopus. If we can reach all the remote we are up to date.
423 up_to_date=t
424 for remote
426 common_one=$(git merge-base --all $head $remote)
427 if test "$common_one" != "$remote"
428 then
429 up_to_date=f
430 break
432 done
433 if test "$up_to_date" = t
434 then
435 finish_up_to_date "Already up-to-date. Yeeah!"
436 exit 0
439 esac
441 # We are going to make a new commit.
442 git var GIT_COMMITTER_IDENT >/dev/null || exit
444 # At this point, we need a real merge. No matter what strategy
445 # we use, it would operate on the index, possibly affecting the
446 # working tree, and when resolved cleanly, have the desired tree
447 # in the index -- this means that the index must be in sync with
448 # the $head commit. The strategies are responsible to ensure this.
450 case "$use_strategies" in
451 ?*' '?*)
452 # Stash away the local changes so that we can try more than one.
453 savestate
454 single_strategy=no
457 rm -f "$GIT_DIR/MERGE_STASH"
458 single_strategy=yes
460 esac
462 result_tree= best_cnt=-1 best_strategy= wt_strategy=
463 merge_was_ok=
464 for strategy in $use_strategies
466 test "$wt_strategy" = '' || {
467 echo "Rewinding the tree to pristine..."
468 restorestate
470 case "$single_strategy" in
472 echo "Trying merge strategy $strategy..."
474 esac
476 # Remember which strategy left the state in the working tree
477 wt_strategy=$strategy
479 git-merge-$strategy $common -- "$head_arg" "$@"
480 exit=$?
481 if test "$no_commit" = t && test "$exit" = 0
482 then
483 merge_was_ok=t
484 exit=1 ;# pretend it left conflicts.
487 test "$exit" = 0 || {
489 # The backend exits with 1 when conflicts are left to be resolved,
490 # with 2 when it does not handle the given merge at all.
492 if test "$exit" -eq 1
493 then
494 cnt=`{
495 git diff-files --name-only
496 git ls-files --unmerged
497 } | wc -l`
498 if test $best_cnt -le 0 -o $cnt -le $best_cnt
499 then
500 best_strategy=$strategy
501 best_cnt=$cnt
504 continue
507 # Automerge succeeded.
508 result_tree=$(git write-tree) && break
509 done
511 # If we have a resulting tree, that means the strategy module
512 # auto resolved the merge cleanly.
513 if test '' != "$result_tree"
514 then
515 if test "$allow_fast_forward" = "t"
516 then
517 parents=$(git show-branch --independent "$head" "$@")
518 else
519 parents=$(git rev-parse "$head" "$@")
521 parents=$(echo "$parents" | sed -e 's/^/-p /')
522 result_commit=$(printf '%s\n' "$merge_msg" | git commit-tree $result_tree $parents) || exit
523 finish "$result_commit" "Merge made by $wt_strategy."
524 dropsave
525 exit 0
528 # Pick the result from the best strategy and have the user fix it up.
529 case "$best_strategy" in
531 restorestate
532 case "$use_strategies" in
533 ?*' '?*)
534 echo >&2 "No merge strategy handled the merge."
537 echo >&2 "Merge with strategy $use_strategies failed."
539 esac
540 exit 2
542 "$wt_strategy")
543 # We already have its result in the working tree.
546 echo "Rewinding the tree to pristine..."
547 restorestate
548 echo "Using the $best_strategy to prepare resolving by hand."
549 git-merge-$best_strategy $common -- "$head_arg" "$@"
551 esac
553 if test "$squash" = t
554 then
555 finish
556 else
557 for remote
559 echo $remote
560 done >"$GIT_DIR/MERGE_HEAD"
561 printf '%s\n' "$merge_msg" >"$GIT_DIR/MERGE_MSG"
564 if test "$merge_was_ok" = t
565 then
566 echo >&2 \
567 "Automatic merge went well; stopped before committing as requested"
568 exit 0
569 else
571 echo '
572 Conflicts:
574 git ls-files --unmerged |
575 sed -e 's/^[^ ]* / /' |
576 uniq
577 } >>"$GIT_DIR/MERGE_MSG"
578 git rerere
579 die "Automatic merge failed; fix conflicts and then commit the result."