Makefile: customization for supporting HP-UX
[git/fastimport.git] / git-merge.sh
blob1c123a37e6941b6727c6101bb29cbc485a380c20
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 summary show a diffstat at the end of the merge
12 n,no-summary don't show a diffstat at the end of the merge
13 squash create a single commit instead of doing a merge
14 commit perform a commit if the merge sucesses (default)
15 ff allow fast forward (default)
16 s,strategy= merge strategy to use
17 m,message= message to be used for the merge commit (if any)
20 SUBDIRECTORY_OK=Yes
21 . git-sh-setup
22 require_work_tree
23 cd_to_toplevel
25 test -z "$(git ls-files -u)" ||
26 die "You are in the middle of a conflicted merge."
28 LF='
31 all_strategies='recur recursive octopus resolve stupid ours subtree'
32 default_twohead_strategies='recursive'
33 default_octopus_strategies='octopus'
34 no_fast_forward_strategies='subtree ours'
35 no_trivial_strategies='recursive recur subtree ours'
36 use_strategies=
38 allow_fast_forward=t
39 allow_trivial_merge=t
41 dropsave() {
42 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
43 "$GIT_DIR/MERGE_STASH" || exit 1
46 savestate() {
47 # Stash away any local modifications.
48 git stash create >"$GIT_DIR/MERGE_STASH"
51 restorestate() {
52 if test -f "$GIT_DIR/MERGE_STASH"
53 then
54 git reset --hard $head >/dev/null
55 git stash apply $(cat "$GIT_DIR/MERGE_STASH")
56 git update-index --refresh >/dev/null
60 finish_up_to_date () {
61 case "$squash" in
63 echo "$1 (nothing to squash)" ;;
64 '')
65 echo "$1" ;;
66 esac
67 dropsave
70 squash_message () {
71 echo Squashed commit of the following:
72 echo
73 git log --no-merges ^"$head" $remoteheads
76 finish () {
77 if test '' = "$2"
78 then
79 rlogm="$GIT_REFLOG_ACTION"
80 else
81 echo "$2"
82 rlogm="$GIT_REFLOG_ACTION: $2"
84 case "$squash" in
86 echo "Squash commit -- not updating HEAD"
87 squash_message >"$GIT_DIR/SQUASH_MSG"
89 '')
90 case "$merge_msg" in
91 '')
92 echo "No merge message -- not updating HEAD"
95 git update-ref -m "$rlogm" HEAD "$1" "$head" || exit 1
96 git gc --auto
98 esac
100 esac
101 case "$1" in
105 if test "$show_diffstat" = t
106 then
107 # We want color (if set), but no pager
108 GIT_PAGER='' git diff --stat --summary -M "$head" "$1"
111 esac
113 # Run a post-merge hook
114 if test -x "$GIT_DIR"/hooks/post-merge
115 then
116 case "$squash" in
118 "$GIT_DIR"/hooks/post-merge 1
121 "$GIT_DIR"/hooks/post-merge 0
123 esac
127 merge_name () {
128 remote="$1"
129 rh=$(git rev-parse --verify "$remote^0" 2>/dev/null) || return
130 bh=$(git show-ref -s --verify "refs/heads/$remote" 2>/dev/null)
131 if test "$rh" = "$bh"
132 then
133 echo "$rh branch '$remote' of ."
134 elif truname=$(expr "$remote" : '\(.*\)~[1-9][0-9]*$') &&
135 git show-ref -q --verify "refs/heads/$truname" 2>/dev/null
136 then
137 echo "$rh branch '$truname' (early part) of ."
138 elif test "$remote" = "FETCH_HEAD" -a -r "$GIT_DIR/FETCH_HEAD"
139 then
140 sed -e 's/ not-for-merge / /' -e 1q \
141 "$GIT_DIR/FETCH_HEAD"
142 else
143 echo "$rh commit '$remote'"
147 parse_config () {
148 while test $# != 0; do
149 case "$1" in
150 -n|--no-summary)
151 show_diffstat=false ;;
152 --summary)
153 show_diffstat=t ;;
154 --squash)
155 allow_fast_forward=t squash=t no_commit=t ;;
156 --no-squash)
157 allow_fast_forward=t squash= no_commit= ;;
158 --commit)
159 allow_fast_forward=t squash= no_commit= ;;
160 --no-commit)
161 allow_fast_forward=t squash= no_commit=t ;;
162 --ff)
163 allow_fast_forward=t squash= no_commit= ;;
164 --no-ff)
165 allow_fast_forward=false squash= no_commit= ;;
166 -s|--strategy)
167 shift
168 case " $all_strategies " in
169 *" $1 "*)
170 use_strategies="$use_strategies$1 " ;;
172 die "available strategies are: $all_strategies" ;;
173 esac
175 -m|--message)
176 shift
177 merge_msg="$1"
178 have_message=t
181 shift
182 break ;;
183 *) usage ;;
184 esac
185 shift
186 done
187 args_left=$#
190 test $# != 0 || usage
192 have_message=
194 if branch=$(git-symbolic-ref -q HEAD)
195 then
196 mergeopts=$(git config "branch.${branch#refs/heads/}.mergeoptions")
197 if test -n "$mergeopts"
198 then
199 parse_config $mergeopts --
203 parse_config "$@"
204 while test $args_left -lt $#; do shift; done
206 if test -z "$show_diffstat"; then
207 test "$(git config --bool merge.diffstat)" = false && show_diffstat=false
208 test -z "$show_diffstat" && show_diffstat=t
211 # This could be traditional "merge <msg> HEAD <commit>..." and the
212 # way we can tell it is to see if the second token is HEAD, but some
213 # people might have misused the interface and used a committish that
214 # is the same as HEAD there instead. Traditional format never would
215 # have "-m" so it is an additional safety measure to check for it.
217 if test -z "$have_message" &&
218 second_token=$(git rev-parse --verify "$2^0" 2>/dev/null) &&
219 head_commit=$(git rev-parse --verify "HEAD" 2>/dev/null) &&
220 test "$second_token" = "$head_commit"
221 then
222 merge_msg="$1"
223 shift
224 head_arg="$1"
225 shift
226 elif ! git rev-parse --verify HEAD >/dev/null 2>&1
227 then
228 # If the merged head is a valid one there is no reason to
229 # forbid "git merge" into a branch yet to be born. We do
230 # the same for "git pull".
231 if test 1 -ne $#
232 then
233 echo >&2 "Can merge only exactly one commit into empty head"
234 exit 1
237 rh=$(git rev-parse --verify "$1^0") ||
238 die "$1 - not something we can merge"
240 git update-ref -m "initial pull" HEAD "$rh" "" &&
241 git read-tree --reset -u HEAD
242 exit
244 else
245 # We are invoked directly as the first-class UI.
246 head_arg=HEAD
248 # All the rest are the commits being merged; prepare
249 # the standard merge summary message to be appended to
250 # the given message. If remote is invalid we will die
251 # later in the common codepath so we discard the error
252 # in this loop.
253 merge_name=$(for remote
255 merge_name "$remote"
256 done | git fmt-merge-msg
258 merge_msg="${merge_msg:+$merge_msg$LF$LF}$merge_name"
260 head=$(git rev-parse --verify "$head_arg"^0) || usage
262 # All the rest are remote heads
263 test "$#" = 0 && usage ;# we need at least one remote head.
264 set_reflog_action "merge $*"
266 remoteheads=
267 for remote
269 remotehead=$(git rev-parse --verify "$remote"^0 2>/dev/null) ||
270 die "$remote - not something we can merge"
271 remoteheads="${remoteheads}$remotehead "
272 eval GITHEAD_$remotehead='"$remote"'
273 export GITHEAD_$remotehead
274 done
275 set x $remoteheads ; shift
277 case "$use_strategies" in
279 case "$#" in
281 var="`git config --get pull.twohead`"
282 if test -n "$var"
283 then
284 use_strategies="$var"
285 else
286 use_strategies="$default_twohead_strategies"
287 fi ;;
289 var="`git config --get pull.octopus`"
290 if test -n "$var"
291 then
292 use_strategies="$var"
293 else
294 use_strategies="$default_octopus_strategies"
295 fi ;;
296 esac
298 esac
300 for s in $use_strategies
302 for ss in $no_fast_forward_strategies
304 case " $s " in
305 *" $ss "*)
306 allow_fast_forward=f
307 break
309 esac
310 done
311 for ss in $no_trivial_strategies
313 case " $s " in
314 *" $ss "*)
315 allow_trivial_merge=f
316 break
318 esac
319 done
320 done
322 case "$#" in
324 common=$(git merge-base --all $head "$@")
327 common=$(git show-branch --merge-base $head "$@")
329 esac
330 echo "$head" >"$GIT_DIR/ORIG_HEAD"
332 case "$allow_fast_forward,$#,$common,$no_commit" in
333 ?,*,'',*)
334 # No common ancestors found. We need a real merge.
336 ?,1,"$1",*)
337 # If head can reach all the merge then we are up to date.
338 # but first the most common case of merging one remote.
339 finish_up_to_date "Already up-to-date."
340 exit 0
342 t,1,"$head",*)
343 # Again the most common case of merging one remote.
344 echo "Updating $(git rev-parse --short $head)..$(git rev-parse --short $1)"
345 git update-index --refresh 2>/dev/null
346 msg="Fast forward"
347 if test -n "$have_message"
348 then
349 msg="$msg (no commit created; -m option ignored)"
351 new_head=$(git rev-parse --verify "$1^0") &&
352 git read-tree -v -m -u --exclude-per-directory=.gitignore $head "$new_head" &&
353 finish "$new_head" "$msg" || exit
354 dropsave
355 exit 0
357 ?,1,?*"$LF"?*,*)
358 # We are not doing octopus and not fast forward. Need a
359 # real merge.
361 ?,1,*,)
362 # We are not doing octopus, not fast forward, and have only
363 # one common.
364 git update-index --refresh 2>/dev/null
365 case "$allow_trivial_merge" in
367 # See if it is really trivial.
368 git var GIT_COMMITTER_IDENT >/dev/null || exit
369 echo "Trying really trivial in-index merge..."
370 if git read-tree --trivial -m -u -v $common $head "$1" &&
371 result_tree=$(git write-tree)
372 then
373 echo "Wonderful."
374 result_commit=$(
375 printf '%s\n' "$merge_msg" |
376 git commit-tree $result_tree -p HEAD -p "$1"
377 ) || exit
378 finish "$result_commit" "In-index merge"
379 dropsave
380 exit 0
382 echo "Nope."
383 esac
386 # An octopus. If we can reach all the remote we are up to date.
387 up_to_date=t
388 for remote
390 common_one=$(git merge-base --all $head $remote)
391 if test "$common_one" != "$remote"
392 then
393 up_to_date=f
394 break
396 done
397 if test "$up_to_date" = t
398 then
399 finish_up_to_date "Already up-to-date. Yeeah!"
400 exit 0
403 esac
405 # We are going to make a new commit.
406 git var GIT_COMMITTER_IDENT >/dev/null || exit
408 # At this point, we need a real merge. No matter what strategy
409 # we use, it would operate on the index, possibly affecting the
410 # working tree, and when resolved cleanly, have the desired tree
411 # in the index -- this means that the index must be in sync with
412 # the $head commit. The strategies are responsible to ensure this.
414 case "$use_strategies" in
415 ?*' '?*)
416 # Stash away the local changes so that we can try more than one.
417 savestate
418 single_strategy=no
421 rm -f "$GIT_DIR/MERGE_STASH"
422 single_strategy=yes
424 esac
426 result_tree= best_cnt=-1 best_strategy= wt_strategy=
427 merge_was_ok=
428 for strategy in $use_strategies
430 test "$wt_strategy" = '' || {
431 echo "Rewinding the tree to pristine..."
432 restorestate
434 case "$single_strategy" in
436 echo "Trying merge strategy $strategy..."
438 esac
440 # Remember which strategy left the state in the working tree
441 wt_strategy=$strategy
443 git-merge-$strategy $common -- "$head_arg" "$@"
444 exit=$?
445 if test "$no_commit" = t && test "$exit" = 0
446 then
447 merge_was_ok=t
448 exit=1 ;# pretend it left conflicts.
451 test "$exit" = 0 || {
453 # The backend exits with 1 when conflicts are left to be resolved,
454 # with 2 when it does not handle the given merge at all.
456 if test "$exit" -eq 1
457 then
458 cnt=`{
459 git diff-files --name-only
460 git ls-files --unmerged
461 } | wc -l`
462 if test $best_cnt -le 0 -o $cnt -le $best_cnt
463 then
464 best_strategy=$strategy
465 best_cnt=$cnt
468 continue
471 # Automerge succeeded.
472 result_tree=$(git write-tree) && break
473 done
475 # If we have a resulting tree, that means the strategy module
476 # auto resolved the merge cleanly.
477 if test '' != "$result_tree"
478 then
479 if test "$allow_fast_forward" = "t"
480 then
481 parents=$(git show-branch --independent "$head" "$@")
482 else
483 parents=$(git rev-parse "$head" "$@")
485 parents=$(echo "$parents" | sed -e 's/^/-p /')
486 result_commit=$(printf '%s\n' "$merge_msg" | git commit-tree $result_tree $parents) || exit
487 finish "$result_commit" "Merge made by $wt_strategy."
488 dropsave
489 exit 0
492 # Pick the result from the best strategy and have the user fix it up.
493 case "$best_strategy" in
495 restorestate
496 case "$use_strategies" in
497 ?*' '?*)
498 echo >&2 "No merge strategy handled the merge."
501 echo >&2 "Merge with strategy $use_strategies failed."
503 esac
504 exit 2
506 "$wt_strategy")
507 # We already have its result in the working tree.
510 echo "Rewinding the tree to pristine..."
511 restorestate
512 echo "Using the $best_strategy to prepare resolving by hand."
513 git-merge-$best_strategy $common -- "$head_arg" "$@"
515 esac
517 if test "$squash" = t
518 then
519 finish
520 else
521 for remote
523 echo $remote
524 done >"$GIT_DIR/MERGE_HEAD"
525 printf '%s\n' "$merge_msg" >"$GIT_DIR/MERGE_MSG"
528 if test "$merge_was_ok" = t
529 then
530 echo >&2 \
531 "Automatic merge went well; stopped before committing as requested"
532 exit 0
533 else
535 echo '
536 Conflicts:
538 git ls-files --unmerged |
539 sed -e 's/^[^ ]* / /' |
540 uniq
541 } >>"$GIT_DIR/MERGE_MSG"
542 git rerere
543 die "Automatic merge failed; fix conflicts and then commit the result."