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