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