]> git.ipfire.org Git - thirdparty/git.git/blame - contrib/examples/git-merge.sh
Git 1.7.8-rc2
[thirdparty/git.git] / contrib / examples / git-merge.sh
CommitLineData
91063bbc
JH
1#!/bin/sh
2#
3# Copyright (c) 2005 Junio C Hamano
4#
5
b0bf1d8b
PH
6OPTIONS_KEEPDASHDASH=
7OPTIONS_SPEC="\
1b1dd23f
SB
8git merge [options] <remote>...
9git merge [options] <msg> HEAD <remote>
b0bf1d8b 10--
d8abe148 11stat show a diffstat at the end of the merge
c3170a83 12n don't show a diffstat at the end of the merge
d8abe148 13summary (synonym to --stat)
efb779f8 14log add list of one-line log to merge commit message
b0bf1d8b 15squash create a single commit instead of doing a merge
037e98f2 16commit perform a commit if the merge succeeds (default)
a75d7b54 17ff allow fast-forward (default)
f07df524 18ff-only abort if fast-forward is not possible
fdc4408a 19rerere-autoupdate update index with any reused conflict resolution
b0bf1d8b 20s,strategy= merge strategy to use
5d75a52b 21X= option for selected merge strategy
b0bf1d8b
PH
22m,message= message to be used for the merge commit (if any)
23"
17bcdad3 24
533b7039 25SUBDIRECTORY_OK=Yes
ae2b0f15 26. git-sh-setup
7eff28a9 27require_work_tree
533b7039 28cd_to_toplevel
91063bbc 29
d1014a17 30test -z "$(git ls-files -u)" ||
64048d67 31 die "Merge is not possible because you have unmerged files."
d1014a17 32
6239af1c
JN
33! test -e "$GIT_DIR/MERGE_HEAD" ||
34 die 'You have not concluded your merge (MERGE_HEAD exists).'
35
91063bbc
JH
36LF='
37'
38
68faf689 39all_strategies='recur recursive octopus resolve stupid ours subtree'
8cc5b290 40all_strategies="$all_strategies recursive-ours recursive-theirs"
f5d3a6f5 41not_strategies='base file index tree'
a06f678e 42default_twohead_strategies='recursive'
6ea23343 43default_octopus_strategies='octopus'
a0050852 44no_fast_forward_strategies='subtree ours'
8cc5b290 45no_trivial_strategies='recursive recur subtree ours recursive-ours recursive-theirs'
91063bbc 46use_strategies=
5d75a52b 47xopt=
6ea23343 48
a0050852 49allow_fast_forward=t
f07df524 50fast_forward_only=
a0050852 51allow_trivial_merge=t
fdc4408a 52squash= no_commit= log_arg= rr_arg=
91063bbc 53
a9358240 54dropsave() {
deca7e8c 55 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
2a70fa4f 56 "$GIT_DIR/MERGE_STASH" "$GIT_DIR/MERGE_MODE" || exit 1
a9358240
JH
57}
58
59savestate() {
60fa0560 60 # Stash away any local modifications.
a64d7784 61 git stash create >"$GIT_DIR/MERGE_STASH"
a9358240
JH
62}
63
64restorestate() {
a64d7784 65 if test -f "$GIT_DIR/MERGE_STASH"
deca7e8c 66 then
228e2eb6 67 git reset --hard $head >/dev/null
a64d7784 68 git stash apply $(cat "$GIT_DIR/MERGE_STASH")
5be60078 69 git update-index --refresh >/dev/null
deca7e8c 70 fi
91063bbc
JH
71}
72
7d0c6887
JH
73finish_up_to_date () {
74 case "$squash" in
75 t)
76 echo "$1 (nothing to squash)" ;;
77 '')
78 echo "$1" ;;
79 esac
80 dropsave
81}
82
83squash_message () {
84 echo Squashed commit of the following:
85 echo
9225d7be 86 git log --no-merges --pretty=medium ^"$head" $remoteheads
7d0c6887
JH
87}
88
4f692b19 89finish () {
e1447e38
SP
90 if test '' = "$2"
91 then
f9474132 92 rlogm="$GIT_REFLOG_ACTION"
e1447e38
SP
93 else
94 echo "$2"
f9474132 95 rlogm="$GIT_REFLOG_ACTION: $2"
e1447e38 96 fi
7d0c6887
JH
97 case "$squash" in
98 t)
99 echo "Squash commit -- not updating HEAD"
100 squash_message >"$GIT_DIR/SQUASH_MSG"
4f692b19 101 ;;
7d0c6887
JH
102 '')
103 case "$merge_msg" in
104 '')
105 echo "No merge message -- not updating HEAD"
106 ;;
107 *)
5be60078 108 git update-ref -m "$rlogm" HEAD "$1" "$head" || exit 1
d4bb43ee 109 git gc --auto
7d0c6887
JH
110 ;;
111 esac
4f692b19
JH
112 ;;
113 esac
7d0c6887 114 case "$1" in
91063bbc 115 '')
7d0c6887
JH
116 ;;
117 ?*)
51e7ecf4
AR
118 if test "$show_diffstat" = t
119 then
fefe49d1 120 # We want color (if set), but no pager
5be60078 121 GIT_PAGER='' git diff --stat --summary -M "$head" "$1"
51e7ecf4 122 fi
91063bbc
JH
123 ;;
124 esac
46232915
JE
125
126 # Run a post-merge hook
127 if test -x "$GIT_DIR"/hooks/post-merge
128 then
129 case "$squash" in
130 t)
131 "$GIT_DIR"/hooks/post-merge 1
132 ;;
133 '')
134 "$GIT_DIR"/hooks/post-merge 0
135 ;;
136 esac
137 fi
91063bbc
JH
138}
139
b1bfcae4
JH
140merge_name () {
141 remote="$1"
5be60078 142 rh=$(git rev-parse --verify "$remote^0" 2>/dev/null) || return
13dd255e 143 if truname=$(expr "$remote" : '\(.*\)~[0-9]*$') &&
5be60078 144 git show-ref -q --verify "refs/heads/$truname" 2>/dev/null
b1bfcae4
JH
145 then
146 echo "$rh branch '$truname' (early part) of ."
13dd255e
JN
147 return
148 fi
149 if found_ref=$(git rev-parse --symbolic-full-name --verify \
150 "$remote" 2>/dev/null)
151 then
c2c9a87d
JN
152 expanded=$(git check-ref-format --branch "$remote") ||
153 exit
13dd255e
JN
154 if test "${found_ref#refs/heads/}" != "$found_ref"
155 then
c2c9a87d 156 echo "$rh branch '$expanded' of ."
13dd255e
JN
157 return
158 elif test "${found_ref#refs/remotes/}" != "$found_ref"
159 then
c2c9a87d 160 echo "$rh remote branch '$expanded' of ."
13dd255e
JN
161 return
162 fi
163 fi
164 if test "$remote" = "FETCH_HEAD" -a -r "$GIT_DIR/FETCH_HEAD"
85295a52
MT
165 then
166 sed -e 's/ not-for-merge / /' -e 1q \
167 "$GIT_DIR/FETCH_HEAD"
13dd255e 168 return
b1bfcae4 169 fi
13dd255e 170 echo "$rh commit '$remote'"
b1bfcae4
JH
171}
172
aec7b362 173parse_config () {
b0bf1d8b
PH
174 while test $# != 0; do
175 case "$1" in
d8abe148 176 -n|--no-stat|--no-summary)
b0bf1d8b 177 show_diffstat=false ;;
d8abe148 178 --stat|--summary)
b0bf1d8b 179 show_diffstat=t ;;
efb779f8
SG
180 --log|--no-log)
181 log_arg=$1 ;;
b0bf1d8b 182 --squash)
e6d1f76c
GP
183 test "$allow_fast_forward" = t ||
184 die "You cannot combine --squash with --no-ff."
185 squash=t no_commit=t ;;
b0bf1d8b 186 --no-squash)
e6d1f76c 187 squash= no_commit= ;;
b0bf1d8b 188 --commit)
e6d1f76c 189 no_commit= ;;
b0bf1d8b 190 --no-commit)
e6d1f76c 191 no_commit=t ;;
b0bf1d8b 192 --ff)
e6d1f76c 193 allow_fast_forward=t ;;
b0bf1d8b 194 --no-ff)
e6d1f76c
GP
195 test "$squash" != t ||
196 die "You cannot combine --squash with --no-ff."
f07df524
JN
197 test "$fast_forward_only" != t ||
198 die "You cannot combine --ff-only with --no-ff."
e6d1f76c 199 allow_fast_forward=f ;;
f07df524
JN
200 --ff-only)
201 test "$allow_fast_forward" != f ||
202 die "You cannot combine --ff-only with --no-ff."
203 fast_forward_only=t ;;
fdc4408a
JN
204 --rerere-autoupdate|--no-rerere-autoupdate)
205 rr_arg=$1 ;;
b0bf1d8b
PH
206 -s|--strategy)
207 shift
208 case " $all_strategies " in
209 *" $1 "*)
e69dccf8
JN
210 use_strategies="$use_strategies$1 "
211 ;;
b0bf1d8b 212 *)
f5d3a6f5
JN
213 case " $not_strategies " in
214 *" $1 "*)
215 false
216 esac &&
e69dccf8
JN
217 type "git-merge-$1" >/dev/null 2>&1 ||
218 die "available strategies are: $all_strategies"
219 use_strategies="$use_strategies$1 "
220 ;;
b0bf1d8b
PH
221 esac
222 ;;
5d75a52b
JN
223 -X)
224 shift
225 xopt="${xopt:+$xopt }$(git rev-parse --sq-quote "--$1")"
226 ;;
b0bf1d8b
PH
227 -m|--message)
228 shift
229 merge_msg="$1"
230 have_message=t
231 ;;
232 --)
aec7b362 233 shift
b0bf1d8b
PH
234 break ;;
235 *) usage ;;
236 esac
237 shift
aec7b362 238 done
b0bf1d8b 239 args_left=$#
aec7b362
LH
240}
241
d38eb710
LH
242test $# != 0 || usage
243
244have_message=
aec7b362
LH
245
246if branch=$(git-symbolic-ref -q HEAD)
247then
248 mergeopts=$(git config "branch.${branch#refs/heads/}.mergeoptions")
249 if test -n "$mergeopts"
250 then
b0bf1d8b 251 parse_config $mergeopts --
aec7b362
LH
252 fi
253fi
254
b0bf1d8b
PH
255parse_config "$@"
256while test $args_left -lt $#; do shift; done
91063bbc 257
51e7ecf4 258if test -z "$show_diffstat"; then
5be60078 259 test "$(git config --bool merge.diffstat)" = false && show_diffstat=false
3e6c0a3f 260 test "$(git config --bool merge.stat)" = false && show_diffstat=false
51e7ecf4
AR
261 test -z "$show_diffstat" && show_diffstat=t
262fi
263
17bcdad3
JH
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.
269
270if test -z "$have_message" &&
5be60078
JH
271 second_token=$(git rev-parse --verify "$2^0" 2>/dev/null) &&
272 head_commit=$(git rev-parse --verify "HEAD" 2>/dev/null) &&
17bcdad3
JH
273 test "$second_token" = "$head_commit"
274then
275 merge_msg="$1"
276 shift
277 head_arg="$1"
278 shift
5be60078 279elif ! git rev-parse --verify HEAD >/dev/null 2>&1
8092c7f6
JH
280then
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
288 fi
289
22e0560a
JN
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"
8092c7f6
JH
294 rh=$(git rev-parse --verify "$1^0") ||
295 die "$1 - not something we can merge"
296
5be60078
JH
297 git update-ref -m "initial pull" HEAD "$rh" "" &&
298 git read-tree --reset -u HEAD
8092c7f6
JH
299 exit
300
17bcdad3
JH
301else
302 # We are invoked directly as the first-class UI.
303 head_arg=HEAD
304
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.
eeb70b67
JN
310 merge_msg="$(
311 for remote
17bcdad3 312 do
b1bfcae4 313 merge_name "$remote"
eeb70b67
JN
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
320 fi
321 )"
17bcdad3 322fi
5be60078 323head=$(git rev-parse --verify "$head_arg"^0) || usage
91063bbc
JH
324
325# All the rest are remote heads
6ea23343 326test "$#" = 0 && usage ;# we need at least one remote head.
7f9acb2a 327set_reflog_action "merge $*"
6ea23343 328
9954f5b8 329remoteheads=
91063bbc
JH
330for remote
331do
5be60078 332 remotehead=$(git rev-parse --verify "$remote"^0 2>/dev/null) ||
91063bbc 333 die "$remote - not something we can merge"
9954f5b8 334 remoteheads="${remoteheads}$remotehead "
e0ec1819
SP
335 eval GITHEAD_$remotehead='"$remote"'
336 export GITHEAD_$remotehead
91063bbc 337done
9954f5b8 338set x $remoteheads ; shift
91063bbc 339
6ea23343
JH
340case "$use_strategies" in
341'')
342 case "$#" in
343 1)
5be60078 344 var="`git config --get pull.twohead`"
de811948
SP
345 if test -n "$var"
346 then
347 use_strategies="$var"
348 else
349 use_strategies="$default_twohead_strategies"
350 fi ;;
6ea23343 351 *)
5be60078 352 var="`git config --get pull.octopus`"
de811948
SP
353 if test -n "$var"
354 then
355 use_strategies="$var"
356 else
357 use_strategies="$default_octopus_strategies"
358 fi ;;
6ea23343
JH
359 esac
360 ;;
361esac
362
363for s in $use_strategies
364do
a0050852 365 for ss in $no_fast_forward_strategies
de6f0def
JH
366 do
367 case " $s " in
a0050852
JH
368 *" $ss "*)
369 allow_fast_forward=f
370 break
371 ;;
372 esac
373 done
374 for ss in $no_trivial_strategies
375 do
376 case " $s " in
377 *" $ss "*)
378 allow_trivial_merge=f
de6f0def
JH
379 break
380 ;;
381 esac
382 done
6ea23343
JH
383done
384
13956670
JH
385case "$#" in
3861)
5be60078 387 common=$(git merge-base --all $head "$@")
13956670
JH
388 ;;
389*)
d9a680ac 390 common=$(git merge-base --all --octopus $head "$@")
13956670
JH
391 ;;
392esac
91063bbc
JH
393echo "$head" >"$GIT_DIR/ORIG_HEAD"
394
a0050852 395case "$allow_fast_forward,$#,$common,$no_commit" in
6ea23343 396?,*,'',*)
88f8f0a5 397 # No common ancestors found. We need a real merge.
91063bbc 398 ;;
6ea23343 399?,1,"$1",*)
91063bbc 400 # If head can reach all the merge then we are up to date.
6ea23343 401 # but first the most common case of merging one remote.
7d0c6887 402 finish_up_to_date "Already up-to-date."
91063bbc
JH
403 exit 0
404 ;;
a0050852 405t,1,"$head",*)
91063bbc 406 # Again the most common case of merging one remote.
5be60078
JH
407 echo "Updating $(git rev-parse --short $head)..$(git rev-parse --short $1)"
408 git update-index --refresh 2>/dev/null
a75d7b54 409 msg="Fast-forward"
be242d57
BF
410 if test -n "$have_message"
411 then
412 msg="$msg (no commit created; -m option ignored)"
413 fi
5be60078
JH
414 new_head=$(git rev-parse --verify "$1^0") &&
415 git read-tree -v -m -u --exclude-per-directory=.gitignore $head "$new_head" &&
be242d57 416 finish "$new_head" "$msg" || exit
a9358240 417 dropsave
91063bbc
JH
418 exit 0
419 ;;
6ea23343 420?,1,?*"$LF"?*,*)
a75d7b54 421 # We are not doing octopus and not fast-forward. Need a
91063bbc
JH
422 # real merge.
423 ;;
6ea23343 424?,1,*,)
a75d7b54 425 # We are not doing octopus, not fast-forward, and have only
c82d7117 426 # one common.
5be60078 427 git update-index --refresh 2>/dev/null
f07df524
JN
428 case "$allow_trivial_merge,$fast_forward_only" in
429 t,)
c82d7117
SP
430 # See if it is really trivial.
431 git var GIT_COMMITTER_IDENT >/dev/null || exit
432 echo "Trying really trivial in-index merge..."
5be60078
JH
433 if git read-tree --trivial -m -u -v $common $head "$1" &&
434 result_tree=$(git write-tree)
c82d7117
SP
435 then
436 echo "Wonderful."
437 result_commit=$(
a23bfaed 438 printf '%s\n' "$merge_msg" |
5be60078 439 git commit-tree $result_tree -p HEAD -p "$1"
c82d7117
SP
440 ) || exit
441 finish "$result_commit" "In-index merge"
442 dropsave
443 exit 0
444 fi
445 echo "Nope."
446 esac
f9d72413 447 ;;
91063bbc
JH
448*)
449 # An octopus. If we can reach all the remote we are up to date.
450 up_to_date=t
451 for remote
452 do
5be60078 453 common_one=$(git merge-base --all $head $remote)
91063bbc
JH
454 if test "$common_one" != "$remote"
455 then
456 up_to_date=f
457 break
458 fi
459 done
460 if test "$up_to_date" = t
461 then
7d0c6887 462 finish_up_to_date "Already up-to-date. Yeeah!"
91063bbc
JH
463 exit 0
464 fi
465 ;;
466esac
467
f07df524
JN
468if test "$fast_forward_only" = t
469then
470 die "Not possible to fast-forward, aborting."
471fi
472
e3b59a44
JH
473# We are going to make a new commit.
474git var GIT_COMMITTER_IDENT >/dev/null || exit
475
a9358240
JH
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
60fa0560 480# the $head commit. The strategies are responsible to ensure this.
91063bbc 481
a9358240
JH
482case "$use_strategies" in
483?*' '?*)
484 # Stash away the local changes so that we can try more than one.
485 savestate
486 single_strategy=no
487 ;;
488*)
a64d7784 489 rm -f "$GIT_DIR/MERGE_STASH"
a9358240
JH
490 single_strategy=yes
491 ;;
492esac
91063bbc
JH
493
494result_tree= best_cnt=-1 best_strategy= wt_strategy=
695bf722 495merge_was_ok=
91063bbc
JH
496for strategy in $use_strategies
497do
498 test "$wt_strategy" = '' || {
499 echo "Rewinding the tree to pristine..."
a9358240 500 restorestate
91063bbc 501 }
a9358240
JH
502 case "$single_strategy" in
503 no)
504 echo "Trying merge strategy $strategy..."
505 ;;
506 esac
507
508 # Remember which strategy left the state in the working tree
91063bbc 509 wt_strategy=$strategy
a9358240 510
5d75a52b 511 eval 'git-merge-$strategy '"$xopt"' $common -- "$head_arg" "$@"'
123ee3ca
JH
512 exit=$?
513 if test "$no_commit" = t && test "$exit" = 0
514 then
695bf722 515 merge_was_ok=t
123ee3ca
JH
516 exit=1 ;# pretend it left conflicts.
517 fi
518
519 test "$exit" = 0 || {
91063bbc
JH
520
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.
523
91063bbc
JH
524 if test "$exit" -eq 1
525 then
526 cnt=`{
5be60078
JH
527 git diff-files --name-only
528 git ls-files --unmerged
91063bbc
JH
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
534 fi
535 fi
536 continue
537 }
538
539 # Automerge succeeded.
5be60078 540 result_tree=$(git write-tree) && break
91063bbc
JH
541done
542
543# If we have a resulting tree, that means the strategy module
544# auto resolved the merge cleanly.
545if test '' != "$result_tree"
546then
d66424c4
LH
547 if test "$allow_fast_forward" = "t"
548 then
d9a680ac 549 parents=$(git merge-base --independent "$head" "$@")
d66424c4 550 else
d9a680ac 551 parents=$(git rev-parse "$head" "$@")
d66424c4
LH
552 fi
553 parents=$(echo "$parents" | sed -e 's/^/-p /')
5be60078 554 result_commit=$(printf '%s\n' "$merge_msg" | git commit-tree $result_tree $parents) || exit
e1447e38 555 finish "$result_commit" "Merge made by $wt_strategy."
a9358240 556 dropsave
91063bbc
JH
557 exit 0
558fi
559
560# Pick the result from the best strategy and have the user fix it up.
561case "$best_strategy" in
562'')
a9358240 563 restorestate
0c4e95d0
JH
564 case "$use_strategies" in
565 ?*' '?*)
566 echo >&2 "No merge strategy handled the merge."
567 ;;
568 *)
569 echo >&2 "Merge with strategy $use_strategies failed."
570 ;;
571 esac
4275df51 572 exit 2
91063bbc
JH
573 ;;
574"$wt_strategy")
575 # We already have its result in the working tree.
576 ;;
577*)
578 echo "Rewinding the tree to pristine..."
a9358240 579 restorestate
91063bbc 580 echo "Using the $best_strategy to prepare resolving by hand."
a9358240 581 git-merge-$best_strategy $common -- "$head_arg" "$@"
91063bbc
JH
582 ;;
583esac
7d0c6887
JH
584
585if test "$squash" = t
586then
587 finish
588else
589 for remote
590 do
591 echo $remote
592 done >"$GIT_DIR/MERGE_HEAD"
2a70fa4f
JN
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
599 :
600 fi >"$GIT_DIR/MERGE_MODE" ||
601 die "Could not write to $GIT_DIR/MERGE_MODE"
7d0c6887 602fi
deca7e8c 603
695bf722
JH
604if test "$merge_was_ok" = t
605then
606 echo >&2 \
607 "Automatic merge went well; stopped before committing as requested"
608 exit 0
609else
6b94f1e4
JH
610 {
611 echo '
612Conflicts:
613'
614 git ls-files --unmerged |
615 sed -e 's/^[^ ]* / /' |
616 uniq
617 } >>"$GIT_DIR/MERGE_MSG"
fdc4408a 618 git rerere $rr_arg
50ac7408 619 die "Automatic merge failed; fix conflicts and then commit the result."
695bf722 620fi