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