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