send-pack: cluster ref status reporting
[git/mingw.git] / git-merge.sh
blobc2092a204035ad0315a3d37ed2f70097e68ed052
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 dropsave() {
30 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
31 "$GIT_DIR/MERGE_SAVE" || exit 1
34 savestate() {
35 # Stash away any local modifications.
36 git diff-index -z --name-only $head |
37 cpio -0 -o >"$GIT_DIR/MERGE_SAVE"
40 restorestate() {
41 if test -f "$GIT_DIR/MERGE_SAVE"
42 then
43 git reset --hard $head >/dev/null
44 cpio -iuv <"$GIT_DIR/MERGE_SAVE"
45 git update-index --refresh >/dev/null
49 finish_up_to_date () {
50 case "$squash" in
52 echo "$1 (nothing to squash)" ;;
53 '')
54 echo "$1" ;;
55 esac
56 dropsave
59 squash_message () {
60 echo Squashed commit of the following:
61 echo
62 git log --no-merges ^"$head" $remoteheads
65 finish () {
66 if test '' = "$2"
67 then
68 rlogm="$GIT_REFLOG_ACTION"
69 else
70 echo "$2"
71 rlogm="$GIT_REFLOG_ACTION: $2"
73 case "$squash" in
75 echo "Squash commit -- not updating HEAD"
76 squash_message >"$GIT_DIR/SQUASH_MSG"
78 '')
79 case "$merge_msg" in
80 '')
81 echo "No merge message -- not updating HEAD"
84 git update-ref -m "$rlogm" HEAD "$1" "$head" || exit 1
85 git gc --auto
87 esac
89 esac
90 case "$1" in
91 '')
93 ?*)
94 if test "$show_diffstat" = t
95 then
96 # We want color (if set), but no pager
97 GIT_PAGER='' git diff --stat --summary -M "$head" "$1"
100 esac
102 # Run a post-merge hook
103 if test -x "$GIT_DIR"/hooks/post-merge
104 then
105 case "$squash" in
107 "$GIT_DIR"/hooks/post-merge 1
110 "$GIT_DIR"/hooks/post-merge 0
112 esac
116 merge_name () {
117 remote="$1"
118 rh=$(git rev-parse --verify "$remote^0" 2>/dev/null) || return
119 bh=$(git show-ref -s --verify "refs/heads/$remote" 2>/dev/null)
120 if test "$rh" = "$bh"
121 then
122 echo "$rh branch '$remote' of ."
123 elif truname=$(expr "$remote" : '\(.*\)~[1-9][0-9]*$') &&
124 git show-ref -q --verify "refs/heads/$truname" 2>/dev/null
125 then
126 echo "$rh branch '$truname' (early part) of ."
127 elif test "$remote" = "FETCH_HEAD" -a -r "$GIT_DIR/FETCH_HEAD"
128 then
129 sed -e 's/ not-for-merge / /' -e 1q \
130 "$GIT_DIR/FETCH_HEAD"
131 else
132 echo "$rh commit '$remote'"
136 parse_option () {
137 case "$1" in
138 -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\
139 --no-summa|--no-summar|--no-summary)
140 show_diffstat=false ;;
141 --summary)
142 show_diffstat=t ;;
143 --sq|--squ|--squa|--squas|--squash)
144 allow_fast_forward=t squash=t no_commit=t ;;
145 --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
146 allow_fast_forward=t squash= no_commit= ;;
147 --c|--co|--com|--comm|--commi|--commit)
148 allow_fast_forward=t squash= no_commit= ;;
149 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
150 allow_fast_forward=t squash= no_commit=t ;;
151 --ff)
152 allow_fast_forward=t squash= no_commit= ;;
153 --no-ff)
154 allow_fast_forward=false squash= no_commit= ;;
155 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
156 --strateg=*|--strategy=*|\
157 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
158 case "$#,$1" in
159 *,*=*)
160 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
161 1,*)
162 usage ;;
164 strategy="$2"
165 shift ;;
166 esac
167 case " $all_strategies " in
168 *" $strategy "*)
169 use_strategies="$use_strategies$strategy " ;;
171 die "available strategies are: $all_strategies" ;;
172 esac
174 -m=*|--m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
175 merge_msg=`expr "z$1" : 'z-[^=]*=\(.*\)'`
176 have_message=t
178 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
179 shift
180 case "$#" in
181 1) usage ;;
182 esac
183 merge_msg="$1"
184 have_message=t
186 -*) usage ;;
187 *) return 1 ;;
188 esac
189 shift
190 args_left=$#
193 parse_config () {
194 while test $# -gt 0
196 parse_option "$@" || usage
197 while test $args_left -lt $#
199 shift
200 done
201 done
204 test $# != 0 || usage
206 have_message=
208 if branch=$(git-symbolic-ref -q HEAD)
209 then
210 mergeopts=$(git config "branch.${branch#refs/heads/}.mergeoptions")
211 if test -n "$mergeopts"
212 then
213 parse_config $mergeopts
217 while parse_option "$@"
219 while test $args_left -lt $#
221 shift
222 done
223 done
225 if test -z "$show_diffstat"; then
226 test "$(git config --bool merge.diffstat)" = false && show_diffstat=false
227 test -z "$show_diffstat" && show_diffstat=t
230 # This could be traditional "merge <msg> HEAD <commit>..." and the
231 # way we can tell it is to see if the second token is HEAD, but some
232 # people might have misused the interface and used a committish that
233 # is the same as HEAD there instead. Traditional format never would
234 # have "-m" so it is an additional safety measure to check for it.
236 if test -z "$have_message" &&
237 second_token=$(git rev-parse --verify "$2^0" 2>/dev/null) &&
238 head_commit=$(git rev-parse --verify "HEAD" 2>/dev/null) &&
239 test "$second_token" = "$head_commit"
240 then
241 merge_msg="$1"
242 shift
243 head_arg="$1"
244 shift
245 elif ! git rev-parse --verify HEAD >/dev/null 2>&1
246 then
247 # If the merged head is a valid one there is no reason to
248 # forbid "git merge" into a branch yet to be born. We do
249 # the same for "git pull".
250 if test 1 -ne $#
251 then
252 echo >&2 "Can merge only exactly one commit into empty head"
253 exit 1
256 rh=$(git rev-parse --verify "$1^0") ||
257 die "$1 - not something we can merge"
259 git update-ref -m "initial pull" HEAD "$rh" "" &&
260 git read-tree --reset -u HEAD
261 exit
263 else
264 # We are invoked directly as the first-class UI.
265 head_arg=HEAD
267 # All the rest are the commits being merged; prepare
268 # the standard merge summary message to be appended to
269 # the given message. If remote is invalid we will die
270 # later in the common codepath so we discard the error
271 # in this loop.
272 merge_name=$(for remote
274 merge_name "$remote"
275 done | git fmt-merge-msg
277 merge_msg="${merge_msg:+$merge_msg$LF$LF}$merge_name"
279 head=$(git rev-parse --verify "$head_arg"^0) || usage
281 # All the rest are remote heads
282 test "$#" = 0 && usage ;# we need at least one remote head.
283 set_reflog_action "merge $*"
285 remoteheads=
286 for remote
288 remotehead=$(git rev-parse --verify "$remote"^0 2>/dev/null) ||
289 die "$remote - not something we can merge"
290 remoteheads="${remoteheads}$remotehead "
291 eval GITHEAD_$remotehead='"$remote"'
292 export GITHEAD_$remotehead
293 done
294 set x $remoteheads ; shift
296 case "$use_strategies" in
298 case "$#" in
300 var="`git config --get pull.twohead`"
301 if test -n "$var"
302 then
303 use_strategies="$var"
304 else
305 use_strategies="$default_twohead_strategies"
306 fi ;;
308 var="`git config --get pull.octopus`"
309 if test -n "$var"
310 then
311 use_strategies="$var"
312 else
313 use_strategies="$default_octopus_strategies"
314 fi ;;
315 esac
317 esac
319 for s in $use_strategies
321 for ss in $no_fast_forward_strategies
323 case " $s " in
324 *" $ss "*)
325 allow_fast_forward=f
326 break
328 esac
329 done
330 for ss in $no_trivial_strategies
332 case " $s " in
333 *" $ss "*)
334 allow_trivial_merge=f
335 break
337 esac
338 done
339 done
341 case "$#" in
343 common=$(git merge-base --all $head "$@")
346 common=$(git show-branch --merge-base $head "$@")
348 esac
349 echo "$head" >"$GIT_DIR/ORIG_HEAD"
351 case "$allow_fast_forward,$#,$common,$no_commit" in
352 ?,*,'',*)
353 # No common ancestors found. We need a real merge.
355 ?,1,"$1",*)
356 # If head can reach all the merge then we are up to date.
357 # but first the most common case of merging one remote.
358 finish_up_to_date "Already up-to-date."
359 exit 0
361 t,1,"$head",*)
362 # Again the most common case of merging one remote.
363 echo "Updating $(git rev-parse --short $head)..$(git rev-parse --short $1)"
364 git update-index --refresh 2>/dev/null
365 msg="Fast forward"
366 if test -n "$have_message"
367 then
368 msg="$msg (no commit created; -m option ignored)"
370 new_head=$(git rev-parse --verify "$1^0") &&
371 git read-tree -v -m -u --exclude-per-directory=.gitignore $head "$new_head" &&
372 finish "$new_head" "$msg" || exit
373 dropsave
374 exit 0
376 ?,1,?*"$LF"?*,*)
377 # We are not doing octopus and not fast forward. Need a
378 # real merge.
380 ?,1,*,)
381 # We are not doing octopus, not fast forward, and have only
382 # one common.
383 git update-index --refresh 2>/dev/null
384 case "$allow_trivial_merge" in
386 # See if it is really trivial.
387 git var GIT_COMMITTER_IDENT >/dev/null || exit
388 echo "Trying really trivial in-index merge..."
389 if git read-tree --trivial -m -u -v $common $head "$1" &&
390 result_tree=$(git write-tree)
391 then
392 echo "Wonderful."
393 result_commit=$(
394 printf '%s\n' "$merge_msg" |
395 git commit-tree $result_tree -p HEAD -p "$1"
396 ) || exit
397 finish "$result_commit" "In-index merge"
398 dropsave
399 exit 0
401 echo "Nope."
402 esac
405 # An octopus. If we can reach all the remote we are up to date.
406 up_to_date=t
407 for remote
409 common_one=$(git merge-base --all $head $remote)
410 if test "$common_one" != "$remote"
411 then
412 up_to_date=f
413 break
415 done
416 if test "$up_to_date" = t
417 then
418 finish_up_to_date "Already up-to-date. Yeeah!"
419 exit 0
422 esac
424 # We are going to make a new commit.
425 git var GIT_COMMITTER_IDENT >/dev/null || exit
427 # At this point, we need a real merge. No matter what strategy
428 # we use, it would operate on the index, possibly affecting the
429 # working tree, and when resolved cleanly, have the desired tree
430 # in the index -- this means that the index must be in sync with
431 # the $head commit. The strategies are responsible to ensure this.
433 case "$use_strategies" in
434 ?*' '?*)
435 # Stash away the local changes so that we can try more than one.
436 savestate
437 single_strategy=no
440 rm -f "$GIT_DIR/MERGE_SAVE"
441 single_strategy=yes
443 esac
445 result_tree= best_cnt=-1 best_strategy= wt_strategy=
446 merge_was_ok=
447 for strategy in $use_strategies
449 test "$wt_strategy" = '' || {
450 echo "Rewinding the tree to pristine..."
451 restorestate
453 case "$single_strategy" in
455 echo "Trying merge strategy $strategy..."
457 esac
459 # Remember which strategy left the state in the working tree
460 wt_strategy=$strategy
462 git-merge-$strategy $common -- "$head_arg" "$@"
463 exit=$?
464 if test "$no_commit" = t && test "$exit" = 0
465 then
466 merge_was_ok=t
467 exit=1 ;# pretend it left conflicts.
470 test "$exit" = 0 || {
472 # The backend exits with 1 when conflicts are left to be resolved,
473 # with 2 when it does not handle the given merge at all.
475 if test "$exit" -eq 1
476 then
477 cnt=`{
478 git diff-files --name-only
479 git ls-files --unmerged
480 } | wc -l`
481 if test $best_cnt -le 0 -o $cnt -le $best_cnt
482 then
483 best_strategy=$strategy
484 best_cnt=$cnt
487 continue
490 # Automerge succeeded.
491 result_tree=$(git write-tree) && break
492 done
494 # If we have a resulting tree, that means the strategy module
495 # auto resolved the merge cleanly.
496 if test '' != "$result_tree"
497 then
498 if test "$allow_fast_forward" = "t"
499 then
500 parents=$(git show-branch --independent "$head" "$@")
501 else
502 parents=$(git rev-parse "$head" "$@")
504 parents=$(echo "$parents" | sed -e 's/^/-p /')
505 result_commit=$(printf '%s\n' "$merge_msg" | git commit-tree $result_tree $parents) || exit
506 finish "$result_commit" "Merge made by $wt_strategy."
507 dropsave
508 exit 0
511 # Pick the result from the best strategy and have the user fix it up.
512 case "$best_strategy" in
514 restorestate
515 case "$use_strategies" in
516 ?*' '?*)
517 echo >&2 "No merge strategy handled the merge."
520 echo >&2 "Merge with strategy $use_strategies failed."
522 esac
523 exit 2
525 "$wt_strategy")
526 # We already have its result in the working tree.
529 echo "Rewinding the tree to pristine..."
530 restorestate
531 echo "Using the $best_strategy to prepare resolving by hand."
532 git-merge-$best_strategy $common -- "$head_arg" "$@"
534 esac
536 if test "$squash" = t
537 then
538 finish
539 else
540 for remote
542 echo $remote
543 done >"$GIT_DIR/MERGE_HEAD"
544 printf '%s\n' "$merge_msg" >"$GIT_DIR/MERGE_MSG"
547 if test "$merge_was_ok" = t
548 then
549 echo >&2 \
550 "Automatic merge went well; stopped before committing as requested"
551 exit 0
552 else
554 echo '
555 Conflicts:
557 git ls-files --unmerged |
558 sed -e 's/^[^ ]* / /' |
559 uniq
560 } >>"$GIT_DIR/MERGE_MSG"
561 git rerere
562 die "Automatic merge failed; fix conflicts and then commit the result."