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