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