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