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