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