]> git.ipfire.org Git - thirdparty/git.git/blame - git-merge.sh
merge, pull: introduce '--(no-)stat' option
[thirdparty/git.git] / 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="\
8git-merge [options] <remote>...
9git-merge [options] <msg> HEAD <remote>
10--
d8abe148
SG
11stat show a diffstat at the end of the merge
12n,no-stat don't show a diffstat at the end of the merge
13summary (synonym to --stat)
14no-summary (synonym to --no-stat)
b0bf1d8b
PH
15squash create a single commit instead of doing a merge
16commit perform a commit if the merge sucesses (default)
17ff allow fast forward (default)
18s,strategy= merge strategy to use
19m,message= message to be used for the merge commit (if any)
20"
17bcdad3 21
533b7039 22SUBDIRECTORY_OK=Yes
ae2b0f15 23. git-sh-setup
7eff28a9 24require_work_tree
533b7039 25cd_to_toplevel
91063bbc 26
d1014a17 27test -z "$(git ls-files -u)" ||
533b7039 28 die "You are in the middle of a conflicted merge."
d1014a17 29
91063bbc
JH
30LF='
31'
32
68faf689 33all_strategies='recur recursive octopus resolve stupid ours subtree'
a06f678e 34default_twohead_strategies='recursive'
6ea23343 35default_octopus_strategies='octopus'
a0050852
JH
36no_fast_forward_strategies='subtree ours'
37no_trivial_strategies='recursive recur subtree ours'
91063bbc 38use_strategies=
6ea23343 39
a0050852
JH
40allow_fast_forward=t
41allow_trivial_merge=t
e6d1f76c 42squash= no_commit=
91063bbc 43
a9358240 44dropsave() {
deca7e8c 45 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
a64d7784 46 "$GIT_DIR/MERGE_STASH" || exit 1
a9358240
JH
47}
48
49savestate() {
60fa0560 50 # Stash away any local modifications.
a64d7784 51 git stash create >"$GIT_DIR/MERGE_STASH"
a9358240
JH
52}
53
54restorestate() {
a64d7784 55 if test -f "$GIT_DIR/MERGE_STASH"
deca7e8c 56 then
228e2eb6 57 git reset --hard $head >/dev/null
a64d7784 58 git stash apply $(cat "$GIT_DIR/MERGE_STASH")
5be60078 59 git update-index --refresh >/dev/null
deca7e8c 60 fi
91063bbc
JH
61}
62
7d0c6887
JH
63finish_up_to_date () {
64 case "$squash" in
65 t)
66 echo "$1 (nothing to squash)" ;;
67 '')
68 echo "$1" ;;
69 esac
70 dropsave
71}
72
73squash_message () {
74 echo Squashed commit of the following:
75 echo
9225d7be 76 git log --no-merges --pretty=medium ^"$head" $remoteheads
7d0c6887
JH
77}
78
4f692b19 79finish () {
e1447e38
SP
80 if test '' = "$2"
81 then
f9474132 82 rlogm="$GIT_REFLOG_ACTION"
e1447e38
SP
83 else
84 echo "$2"
f9474132 85 rlogm="$GIT_REFLOG_ACTION: $2"
e1447e38 86 fi
7d0c6887
JH
87 case "$squash" in
88 t)
89 echo "Squash commit -- not updating HEAD"
90 squash_message >"$GIT_DIR/SQUASH_MSG"
4f692b19 91 ;;
7d0c6887
JH
92 '')
93 case "$merge_msg" in
94 '')
95 echo "No merge message -- not updating HEAD"
96 ;;
97 *)
5be60078 98 git update-ref -m "$rlogm" HEAD "$1" "$head" || exit 1
d4bb43ee 99 git gc --auto
7d0c6887
JH
100 ;;
101 esac
4f692b19
JH
102 ;;
103 esac
7d0c6887 104 case "$1" in
91063bbc 105 '')
7d0c6887
JH
106 ;;
107 ?*)
51e7ecf4
AR
108 if test "$show_diffstat" = t
109 then
fefe49d1 110 # We want color (if set), but no pager
5be60078 111 GIT_PAGER='' git diff --stat --summary -M "$head" "$1"
51e7ecf4 112 fi
91063bbc
JH
113 ;;
114 esac
46232915
JE
115
116 # Run a post-merge hook
117 if test -x "$GIT_DIR"/hooks/post-merge
118 then
119 case "$squash" in
120 t)
121 "$GIT_DIR"/hooks/post-merge 1
122 ;;
123 '')
124 "$GIT_DIR"/hooks/post-merge 0
125 ;;
126 esac
127 fi
91063bbc
JH
128}
129
b1bfcae4
JH
130merge_name () {
131 remote="$1"
5be60078
JH
132 rh=$(git rev-parse --verify "$remote^0" 2>/dev/null) || return
133 bh=$(git show-ref -s --verify "refs/heads/$remote" 2>/dev/null)
b1bfcae4
JH
134 if test "$rh" = "$bh"
135 then
136 echo "$rh branch '$remote' of ."
137 elif truname=$(expr "$remote" : '\(.*\)~[1-9][0-9]*$') &&
5be60078 138 git show-ref -q --verify "refs/heads/$truname" 2>/dev/null
b1bfcae4
JH
139 then
140 echo "$rh branch '$truname' (early part) of ."
85295a52
MT
141 elif test "$remote" = "FETCH_HEAD" -a -r "$GIT_DIR/FETCH_HEAD"
142 then
143 sed -e 's/ not-for-merge / /' -e 1q \
144 "$GIT_DIR/FETCH_HEAD"
b1bfcae4
JH
145 else
146 echo "$rh commit '$remote'"
147 fi
148}
149
aec7b362 150parse_config () {
b0bf1d8b
PH
151 while test $# != 0; do
152 case "$1" in
d8abe148 153 -n|--no-stat|--no-summary)
b0bf1d8b 154 show_diffstat=false ;;
d8abe148 155 --stat|--summary)
b0bf1d8b
PH
156 show_diffstat=t ;;
157 --squash)
e6d1f76c
GP
158 test "$allow_fast_forward" = t ||
159 die "You cannot combine --squash with --no-ff."
160 squash=t no_commit=t ;;
b0bf1d8b 161 --no-squash)
e6d1f76c 162 squash= no_commit= ;;
b0bf1d8b 163 --commit)
e6d1f76c 164 no_commit= ;;
b0bf1d8b 165 --no-commit)
e6d1f76c 166 no_commit=t ;;
b0bf1d8b 167 --ff)
e6d1f76c 168 allow_fast_forward=t ;;
b0bf1d8b 169 --no-ff)
e6d1f76c
GP
170 test "$squash" != t ||
171 die "You cannot combine --squash with --no-ff."
172 allow_fast_forward=f ;;
b0bf1d8b
PH
173 -s|--strategy)
174 shift
175 case " $all_strategies " in
176 *" $1 "*)
177 use_strategies="$use_strategies$1 " ;;
178 *)
179 die "available strategies are: $all_strategies" ;;
180 esac
181 ;;
182 -m|--message)
183 shift
184 merge_msg="$1"
185 have_message=t
186 ;;
187 --)
aec7b362 188 shift
b0bf1d8b
PH
189 break ;;
190 *) usage ;;
191 esac
192 shift
aec7b362 193 done
b0bf1d8b 194 args_left=$#
aec7b362
LH
195}
196
d38eb710
LH
197test $# != 0 || usage
198
199have_message=
aec7b362
LH
200
201if branch=$(git-symbolic-ref -q HEAD)
202then
203 mergeopts=$(git config "branch.${branch#refs/heads/}.mergeoptions")
204 if test -n "$mergeopts"
205 then
b0bf1d8b 206 parse_config $mergeopts --
aec7b362
LH
207 fi
208fi
209
b0bf1d8b
PH
210parse_config "$@"
211while test $args_left -lt $#; do shift; done
91063bbc 212
51e7ecf4 213if test -z "$show_diffstat"; then
5be60078 214 test "$(git config --bool merge.diffstat)" = false && show_diffstat=false
51e7ecf4
AR
215 test -z "$show_diffstat" && show_diffstat=t
216fi
217
17bcdad3
JH
218# This could be traditional "merge <msg> HEAD <commit>..." and the
219# way we can tell it is to see if the second token is HEAD, but some
220# people might have misused the interface and used a committish that
221# is the same as HEAD there instead. Traditional format never would
222# have "-m" so it is an additional safety measure to check for it.
223
224if test -z "$have_message" &&
5be60078
JH
225 second_token=$(git rev-parse --verify "$2^0" 2>/dev/null) &&
226 head_commit=$(git rev-parse --verify "HEAD" 2>/dev/null) &&
17bcdad3
JH
227 test "$second_token" = "$head_commit"
228then
229 merge_msg="$1"
230 shift
231 head_arg="$1"
232 shift
5be60078 233elif ! git rev-parse --verify HEAD >/dev/null 2>&1
8092c7f6
JH
234then
235 # If the merged head is a valid one there is no reason to
236 # forbid "git merge" into a branch yet to be born. We do
237 # the same for "git pull".
238 if test 1 -ne $#
239 then
240 echo >&2 "Can merge only exactly one commit into empty head"
241 exit 1
242 fi
243
244 rh=$(git rev-parse --verify "$1^0") ||
245 die "$1 - not something we can merge"
246
5be60078
JH
247 git update-ref -m "initial pull" HEAD "$rh" "" &&
248 git read-tree --reset -u HEAD
8092c7f6
JH
249 exit
250
17bcdad3
JH
251else
252 # We are invoked directly as the first-class UI.
253 head_arg=HEAD
254
255 # All the rest are the commits being merged; prepare
256 # the standard merge summary message to be appended to
257 # the given message. If remote is invalid we will die
258 # later in the common codepath so we discard the error
259 # in this loop.
260 merge_name=$(for remote
261 do
b1bfcae4 262 merge_name "$remote"
5be60078 263 done | git fmt-merge-msg
17bcdad3
JH
264 )
265 merge_msg="${merge_msg:+$merge_msg$LF$LF}$merge_name"
266fi
5be60078 267head=$(git rev-parse --verify "$head_arg"^0) || usage
91063bbc
JH
268
269# All the rest are remote heads
6ea23343 270test "$#" = 0 && usage ;# we need at least one remote head.
7f9acb2a 271set_reflog_action "merge $*"
6ea23343 272
9954f5b8 273remoteheads=
91063bbc
JH
274for remote
275do
5be60078 276 remotehead=$(git rev-parse --verify "$remote"^0 2>/dev/null) ||
91063bbc 277 die "$remote - not something we can merge"
9954f5b8 278 remoteheads="${remoteheads}$remotehead "
e0ec1819
SP
279 eval GITHEAD_$remotehead='"$remote"'
280 export GITHEAD_$remotehead
91063bbc 281done
9954f5b8 282set x $remoteheads ; shift
91063bbc 283
6ea23343
JH
284case "$use_strategies" in
285'')
286 case "$#" in
287 1)
5be60078 288 var="`git config --get pull.twohead`"
de811948
SP
289 if test -n "$var"
290 then
291 use_strategies="$var"
292 else
293 use_strategies="$default_twohead_strategies"
294 fi ;;
6ea23343 295 *)
5be60078 296 var="`git config --get pull.octopus`"
de811948
SP
297 if test -n "$var"
298 then
299 use_strategies="$var"
300 else
301 use_strategies="$default_octopus_strategies"
302 fi ;;
6ea23343
JH
303 esac
304 ;;
305esac
306
307for s in $use_strategies
308do
a0050852 309 for ss in $no_fast_forward_strategies
de6f0def
JH
310 do
311 case " $s " in
a0050852
JH
312 *" $ss "*)
313 allow_fast_forward=f
314 break
315 ;;
316 esac
317 done
318 for ss in $no_trivial_strategies
319 do
320 case " $s " in
321 *" $ss "*)
322 allow_trivial_merge=f
de6f0def
JH
323 break
324 ;;
325 esac
326 done
6ea23343
JH
327done
328
13956670
JH
329case "$#" in
3301)
5be60078 331 common=$(git merge-base --all $head "$@")
13956670
JH
332 ;;
333*)
5be60078 334 common=$(git show-branch --merge-base $head "$@")
13956670
JH
335 ;;
336esac
91063bbc
JH
337echo "$head" >"$GIT_DIR/ORIG_HEAD"
338
a0050852 339case "$allow_fast_forward,$#,$common,$no_commit" in
6ea23343 340?,*,'',*)
88f8f0a5 341 # No common ancestors found. We need a real merge.
91063bbc 342 ;;
6ea23343 343?,1,"$1",*)
91063bbc 344 # If head can reach all the merge then we are up to date.
6ea23343 345 # but first the most common case of merging one remote.
7d0c6887 346 finish_up_to_date "Already up-to-date."
91063bbc
JH
347 exit 0
348 ;;
a0050852 349t,1,"$head",*)
91063bbc 350 # Again the most common case of merging one remote.
5be60078
JH
351 echo "Updating $(git rev-parse --short $head)..$(git rev-parse --short $1)"
352 git update-index --refresh 2>/dev/null
be242d57
BF
353 msg="Fast forward"
354 if test -n "$have_message"
355 then
356 msg="$msg (no commit created; -m option ignored)"
357 fi
5be60078
JH
358 new_head=$(git rev-parse --verify "$1^0") &&
359 git read-tree -v -m -u --exclude-per-directory=.gitignore $head "$new_head" &&
be242d57 360 finish "$new_head" "$msg" || exit
a9358240 361 dropsave
91063bbc
JH
362 exit 0
363 ;;
6ea23343 364?,1,?*"$LF"?*,*)
91063bbc
JH
365 # We are not doing octopus and not fast forward. Need a
366 # real merge.
367 ;;
6ea23343 368?,1,*,)
f9d72413 369 # We are not doing octopus, not fast forward, and have only
c82d7117 370 # one common.
5be60078 371 git update-index --refresh 2>/dev/null
a0050852
JH
372 case "$allow_trivial_merge" in
373 t)
c82d7117
SP
374 # See if it is really trivial.
375 git var GIT_COMMITTER_IDENT >/dev/null || exit
376 echo "Trying really trivial in-index merge..."
5be60078
JH
377 if git read-tree --trivial -m -u -v $common $head "$1" &&
378 result_tree=$(git write-tree)
c82d7117
SP
379 then
380 echo "Wonderful."
381 result_commit=$(
a23bfaed 382 printf '%s\n' "$merge_msg" |
5be60078 383 git commit-tree $result_tree -p HEAD -p "$1"
c82d7117
SP
384 ) || exit
385 finish "$result_commit" "In-index merge"
386 dropsave
387 exit 0
388 fi
389 echo "Nope."
390 esac
f9d72413 391 ;;
91063bbc
JH
392*)
393 # An octopus. If we can reach all the remote we are up to date.
394 up_to_date=t
395 for remote
396 do
5be60078 397 common_one=$(git merge-base --all $head $remote)
91063bbc
JH
398 if test "$common_one" != "$remote"
399 then
400 up_to_date=f
401 break
402 fi
403 done
404 if test "$up_to_date" = t
405 then
7d0c6887 406 finish_up_to_date "Already up-to-date. Yeeah!"
91063bbc
JH
407 exit 0
408 fi
409 ;;
410esac
411
e3b59a44
JH
412# We are going to make a new commit.
413git var GIT_COMMITTER_IDENT >/dev/null || exit
414
a9358240
JH
415# At this point, we need a real merge. No matter what strategy
416# we use, it would operate on the index, possibly affecting the
417# working tree, and when resolved cleanly, have the desired tree
418# in the index -- this means that the index must be in sync with
60fa0560 419# the $head commit. The strategies are responsible to ensure this.
91063bbc 420
a9358240
JH
421case "$use_strategies" in
422?*' '?*)
423 # Stash away the local changes so that we can try more than one.
424 savestate
425 single_strategy=no
426 ;;
427*)
a64d7784 428 rm -f "$GIT_DIR/MERGE_STASH"
a9358240
JH
429 single_strategy=yes
430 ;;
431esac
91063bbc
JH
432
433result_tree= best_cnt=-1 best_strategy= wt_strategy=
695bf722 434merge_was_ok=
91063bbc
JH
435for strategy in $use_strategies
436do
437 test "$wt_strategy" = '' || {
438 echo "Rewinding the tree to pristine..."
a9358240 439 restorestate
91063bbc 440 }
a9358240
JH
441 case "$single_strategy" in
442 no)
443 echo "Trying merge strategy $strategy..."
444 ;;
445 esac
446
447 # Remember which strategy left the state in the working tree
91063bbc 448 wt_strategy=$strategy
a9358240 449
123ee3ca
JH
450 git-merge-$strategy $common -- "$head_arg" "$@"
451 exit=$?
452 if test "$no_commit" = t && test "$exit" = 0
453 then
695bf722 454 merge_was_ok=t
123ee3ca
JH
455 exit=1 ;# pretend it left conflicts.
456 fi
457
458 test "$exit" = 0 || {
91063bbc
JH
459
460 # The backend exits with 1 when conflicts are left to be resolved,
461 # with 2 when it does not handle the given merge at all.
462
91063bbc
JH
463 if test "$exit" -eq 1
464 then
465 cnt=`{
5be60078
JH
466 git diff-files --name-only
467 git ls-files --unmerged
91063bbc
JH
468 } | wc -l`
469 if test $best_cnt -le 0 -o $cnt -le $best_cnt
470 then
471 best_strategy=$strategy
472 best_cnt=$cnt
473 fi
474 fi
475 continue
476 }
477
478 # Automerge succeeded.
5be60078 479 result_tree=$(git write-tree) && break
91063bbc
JH
480done
481
482# If we have a resulting tree, that means the strategy module
483# auto resolved the merge cleanly.
484if test '' != "$result_tree"
485then
d66424c4
LH
486 if test "$allow_fast_forward" = "t"
487 then
488 parents=$(git show-branch --independent "$head" "$@")
489 else
490 parents=$(git rev-parse "$head" "$@")
491 fi
492 parents=$(echo "$parents" | sed -e 's/^/-p /')
5be60078 493 result_commit=$(printf '%s\n' "$merge_msg" | git commit-tree $result_tree $parents) || exit
e1447e38 494 finish "$result_commit" "Merge made by $wt_strategy."
a9358240 495 dropsave
91063bbc
JH
496 exit 0
497fi
498
499# Pick the result from the best strategy and have the user fix it up.
500case "$best_strategy" in
501'')
a9358240 502 restorestate
0c4e95d0
JH
503 case "$use_strategies" in
504 ?*' '?*)
505 echo >&2 "No merge strategy handled the merge."
506 ;;
507 *)
508 echo >&2 "Merge with strategy $use_strategies failed."
509 ;;
510 esac
4275df51 511 exit 2
91063bbc
JH
512 ;;
513"$wt_strategy")
514 # We already have its result in the working tree.
515 ;;
516*)
517 echo "Rewinding the tree to pristine..."
a9358240 518 restorestate
91063bbc 519 echo "Using the $best_strategy to prepare resolving by hand."
a9358240 520 git-merge-$best_strategy $common -- "$head_arg" "$@"
91063bbc
JH
521 ;;
522esac
7d0c6887
JH
523
524if test "$squash" = t
525then
526 finish
527else
528 for remote
529 do
530 echo $remote
531 done >"$GIT_DIR/MERGE_HEAD"
a23bfaed 532 printf '%s\n' "$merge_msg" >"$GIT_DIR/MERGE_MSG"
7d0c6887 533fi
deca7e8c 534
695bf722
JH
535if test "$merge_was_ok" = t
536then
537 echo >&2 \
538 "Automatic merge went well; stopped before committing as requested"
539 exit 0
540else
6b94f1e4
JH
541 {
542 echo '
543Conflicts:
544'
545 git ls-files --unmerged |
546 sed -e 's/^[^ ]* / /' |
547 uniq
548 } >>"$GIT_DIR/MERGE_MSG"
b4372ef1 549 git rerere
50ac7408 550 die "Automatic merge failed; fix conflicts and then commit the result."
695bf722 551fi