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