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