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