]>
Commit | Line | Data |
---|---|---|
16da134b | 1 | #include "cache.h" |
f8a9d428 | 2 | #include "dir.h" |
16da134b JS |
3 | #include "tree.h" |
4 | #include "tree-walk.h" | |
076b0adc | 5 | #include "cache-tree.h" |
16da134b | 6 | #include "unpack-trees.h" |
96a02f8f | 7 | #include "progress.h" |
0cf73755 | 8 | #include "refs.h" |
16da134b | 9 | |
b48d5a05 LT |
10 | static inline void remove_entry(int remove) |
11 | { | |
12 | if (remove >= 0) | |
13 | remove_cache_entry_at(remove); | |
14 | } | |
15 | ||
16da134b JS |
16 | /* Unlink the last component and attempt to remove leading |
17 | * directories, in case this unlink is the removal of the | |
18 | * last entry in the directory -- empty directories are removed. | |
19 | */ | |
16a4c617 | 20 | static void unlink_entry(char *name, char *last_symlink) |
16da134b JS |
21 | { |
22 | char *cp, *prev; | |
23 | ||
16a4c617 JH |
24 | if (has_symlink_leading_path(name, last_symlink)) |
25 | return; | |
16da134b JS |
26 | if (unlink(name)) |
27 | return; | |
28 | prev = NULL; | |
29 | while (1) { | |
30 | int status; | |
31 | cp = strrchr(name, '/'); | |
32 | if (prev) | |
33 | *prev = '/'; | |
34 | if (!cp) | |
35 | break; | |
36 | ||
37 | *cp = 0; | |
38 | status = rmdir(name); | |
39 | if (status) { | |
40 | *cp = '/'; | |
41 | break; | |
42 | } | |
43 | prev = cp; | |
44 | } | |
45 | } | |
46 | ||
16da134b | 47 | static struct checkout state; |
33ecf7eb | 48 | static void check_updates(struct unpack_trees_options *o) |
16da134b | 49 | { |
96a02f8f | 50 | unsigned cnt = 0, total = 0; |
dc6a0757 | 51 | struct progress *progress = NULL; |
16a4c617 | 52 | char last_symlink[PATH_MAX]; |
33ecf7eb | 53 | int i; |
16da134b JS |
54 | |
55 | if (o->update && o->verbose_update) { | |
33ecf7eb DB |
56 | for (total = cnt = 0; cnt < active_nr; cnt++) { |
57 | struct cache_entry *ce = active_cache[cnt]; | |
7a51ed66 | 58 | if (ce->ce_flags & (CE_UPDATE | CE_REMOVE)) |
16da134b JS |
59 | total++; |
60 | } | |
61 | ||
dc6a0757 | 62 | progress = start_progress_delay("Checking out files", |
e8548645 | 63 | total, 50, 1); |
16da134b JS |
64 | cnt = 0; |
65 | } | |
66 | ||
16a4c617 | 67 | *last_symlink = '\0'; |
33ecf7eb DB |
68 | for (i = 0; i < active_nr; i++) { |
69 | struct cache_entry *ce = active_cache[i]; | |
16da134b | 70 | |
7a51ed66 | 71 | if (ce->ce_flags & (CE_UPDATE | CE_REMOVE)) |
4d4fcc54 | 72 | display_progress(progress, ++cnt); |
7a51ed66 | 73 | if (ce->ce_flags & CE_REMOVE) { |
16da134b | 74 | if (o->update) |
16a4c617 | 75 | unlink_entry(ce->name, last_symlink); |
33ecf7eb DB |
76 | remove_cache_entry_at(i); |
77 | i--; | |
16da134b JS |
78 | continue; |
79 | } | |
7a51ed66 LT |
80 | if (ce->ce_flags & CE_UPDATE) { |
81 | ce->ce_flags &= ~CE_UPDATE; | |
16a4c617 | 82 | if (o->update) { |
16da134b | 83 | checkout_entry(ce, &state, NULL); |
16a4c617 JH |
84 | *last_symlink = '\0'; |
85 | } | |
16da134b JS |
86 | } |
87 | } | |
4d4fcc54 | 88 | stop_progress(&progress); |
16da134b JS |
89 | } |
90 | ||
01904572 LT |
91 | static inline int call_unpack_fn(struct cache_entry **src, struct unpack_trees_options *o, int remove) |
92 | { | |
93 | int ret = o->fn(src, o, remove); | |
94 | if (ret > 0) { | |
95 | o->pos += ret; | |
96 | ret = 0; | |
97 | } | |
98 | return ret; | |
99 | } | |
100 | ||
101 | static int unpack_index_entry(struct cache_entry *ce, struct unpack_trees_options *o) | |
102 | { | |
103 | struct cache_entry *src[5] = { ce, }; | |
104 | if (ce_stage(ce)) { | |
105 | if (o->skip_unmerged) { | |
106 | o->pos++; | |
107 | } else { | |
108 | remove_entry(o->pos); | |
109 | } | |
110 | return 0; | |
111 | } | |
112 | return call_unpack_fn(src, o, o->pos); | |
113 | } | |
114 | ||
115 | int traverse_trees_recursive(int n, unsigned long dirmask, unsigned long df_conflicts, struct name_entry *names, struct traverse_info *info) | |
16da134b | 116 | { |
16da134b | 117 | int i; |
01904572 LT |
118 | struct tree_desc t[3]; |
119 | struct traverse_info newinfo; | |
120 | struct name_entry *p; | |
121 | ||
122 | p = names; | |
123 | while (!p->mode) | |
124 | p++; | |
125 | ||
126 | newinfo = *info; | |
127 | newinfo.prev = info; | |
128 | newinfo.name = *p; | |
129 | newinfo.pathlen += tree_entry_len(p->path, p->sha1) + 1; | |
130 | newinfo.conflicts |= df_conflicts; | |
131 | ||
132 | for (i = 0; i < n; i++, dirmask >>= 1) { | |
133 | const unsigned char *sha1 = NULL; | |
134 | if (dirmask & 1) | |
135 | sha1 = names[i].sha1; | |
136 | fill_tree_descriptor(t+i, sha1); | |
137 | } | |
138 | traverse_trees(n, t, &newinfo); | |
139 | return 0; | |
140 | } | |
141 | ||
142 | /* | |
143 | * Compare the traverse-path to the cache entry without actually | |
144 | * having to generate the textual representation of the traverse | |
145 | * path. | |
146 | * | |
147 | * NOTE! This *only* compares up to the size of the traverse path | |
148 | * itself - the caller needs to do the final check for the cache | |
149 | * entry having more data at the end! | |
150 | */ | |
151 | static int do_compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n) | |
152 | { | |
153 | int len, pathlen, ce_len; | |
154 | const char *ce_name; | |
155 | ||
156 | if (info->prev) { | |
157 | int cmp = do_compare_entry(ce, info->prev, &info->name); | |
158 | if (cmp) | |
159 | return cmp; | |
160 | } | |
161 | pathlen = info->pathlen; | |
162 | ce_len = ce_namelen(ce); | |
163 | ||
164 | /* If ce_len < pathlen then we must have previously hit "name == directory" entry */ | |
165 | if (ce_len < pathlen) | |
166 | return -1; | |
167 | ||
168 | ce_len -= pathlen; | |
169 | ce_name = ce->name + pathlen; | |
170 | ||
171 | len = tree_entry_len(n->path, n->sha1); | |
172 | return df_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode); | |
173 | } | |
174 | ||
175 | static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n) | |
176 | { | |
177 | int cmp = do_compare_entry(ce, info, n); | |
178 | if (cmp) | |
179 | return cmp; | |
180 | ||
181 | /* | |
182 | * Even if the beginning compared identically, the ce should | |
183 | * compare as bigger than a directory leading up to it! | |
184 | */ | |
185 | return ce_namelen(ce) > traverse_path_len(info, n); | |
186 | } | |
187 | ||
188 | static struct cache_entry *create_ce_entry(const struct traverse_info *info, const struct name_entry *n, int stage) | |
189 | { | |
190 | int len = traverse_path_len(info, n); | |
191 | struct cache_entry *ce = xcalloc(1, cache_entry_size(len)); | |
192 | ||
193 | ce->ce_mode = create_ce_mode(n->mode); | |
194 | ce->ce_flags = create_ce_flags(len, stage); | |
195 | hashcpy(ce->sha1, n->sha1); | |
196 | make_traverse_path(ce->name, info, n); | |
197 | ||
198 | return ce; | |
199 | } | |
200 | ||
201 | static int unpack_nondirectories(int n, unsigned long mask, unsigned long dirmask, struct cache_entry *src[5], | |
202 | const struct name_entry *names, const struct traverse_info *info, int remove) | |
203 | { | |
204 | int i; | |
205 | struct unpack_trees_options *o = info->data; | |
206 | unsigned long conflicts; | |
207 | ||
208 | /* Do we have *only* directories? Nothing to do */ | |
209 | if (mask == dirmask && !src[0]) | |
210 | return 0; | |
211 | ||
212 | conflicts = info->conflicts; | |
213 | if (o->merge) | |
214 | conflicts >>= 1; | |
215 | conflicts |= dirmask; | |
216 | ||
217 | /* | |
218 | * Ok, we've filled in up to any potential index entry in src[0], | |
219 | * now do the rest. | |
220 | */ | |
221 | for (i = 0; i < n; i++) { | |
222 | int stage; | |
223 | unsigned int bit = 1ul << i; | |
224 | if (conflicts & bit) { | |
225 | src[i + o->merge] = o->df_conflict_entry; | |
226 | continue; | |
227 | } | |
228 | if (!(mask & bit)) | |
229 | continue; | |
230 | if (!o->merge) | |
231 | stage = 0; | |
232 | else if (i + 1 < o->head_idx) | |
233 | stage = 1; | |
234 | else if (i + 1 > o->head_idx) | |
235 | stage = 3; | |
236 | else | |
237 | stage = 2; | |
238 | src[i + o->merge] = create_ce_entry(info, names + i, stage); | |
239 | } | |
240 | ||
241 | if (o->merge) | |
242 | return call_unpack_fn(src, o, remove); | |
243 | ||
244 | n += o->merge; | |
245 | remove_entry(remove); | |
246 | for (i = 0; i < n; i++) | |
247 | add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK); | |
248 | return 0; | |
249 | } | |
250 | ||
251 | static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info) | |
252 | { | |
253 | struct cache_entry *src[5] = { NULL, }; | |
254 | struct unpack_trees_options *o = info->data; | |
255 | int remove = -1; | |
256 | const struct name_entry *p = names; | |
257 | ||
258 | /* Find first entry with a real name (we could use "mask" too) */ | |
259 | while (!p->mode) | |
260 | p++; | |
261 | ||
262 | /* Are we supposed to look at the index too? */ | |
263 | if (o->merge) { | |
264 | while (o->pos < active_nr) { | |
265 | struct cache_entry *ce = active_cache[o->pos]; | |
266 | int cmp = compare_entry(ce, info, p); | |
267 | if (cmp < 0) { | |
268 | if (unpack_index_entry(ce, o) < 0) | |
269 | return -1; | |
270 | continue; | |
271 | } | |
272 | if (!cmp) { | |
273 | if (ce_stage(ce)) { | |
274 | /* | |
275 | * If we skip unmerged index entries, we'll skip this | |
276 | * entry *and* the tree entries associated with it! | |
277 | */ | |
278 | if (o->skip_unmerged) | |
279 | return mask; | |
280 | remove_entry(o->pos); | |
281 | continue; | |
282 | } | |
283 | src[0] = ce; | |
284 | remove = o->pos; | |
285 | } | |
286 | break; | |
287 | } | |
288 | } | |
289 | ||
290 | if (unpack_nondirectories(n, mask, dirmask, src, names, info, remove) < 0) | |
291 | return -1; | |
292 | ||
293 | /* Now handle any directories.. */ | |
294 | if (dirmask) { | |
295 | unsigned long conflicts = mask & ~dirmask; | |
296 | if (o->merge) { | |
297 | conflicts <<= 1; | |
298 | if (src[0]) | |
299 | conflicts |= 1; | |
300 | } | |
301 | traverse_trees_recursive(n, dirmask, conflicts, names, info); | |
302 | return mask; | |
303 | } | |
304 | ||
305 | return mask; | |
306 | } | |
307 | ||
308 | static int unpack_failed(struct unpack_trees_options *o, const char *message) | |
309 | { | |
310 | if (!o->gently) { | |
311 | if (message) | |
312 | return error(message); | |
313 | return -1; | |
314 | } | |
315 | discard_cache(); | |
316 | read_cache(); | |
317 | return -1; | |
318 | } | |
319 | ||
320 | int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o) | |
321 | { | |
0fb1eaa8 | 322 | static struct cache_entry *dfc; |
16da134b | 323 | |
01904572 LT |
324 | if (len > 4) |
325 | die("unpack_trees takes at most four trees"); | |
076b0adc | 326 | memset(&state, 0, sizeof(state)); |
16da134b JS |
327 | state.base_dir = ""; |
328 | state.force = 1; | |
329 | state.quiet = 1; | |
330 | state.refresh_cache = 1; | |
331 | ||
332 | o->merge_size = len; | |
0fb1eaa8 JH |
333 | |
334 | if (!dfc) | |
335 | dfc = xcalloc(1, sizeof(struct cache_entry) + 1); | |
336 | o->df_conflict_entry = dfc; | |
16da134b JS |
337 | |
338 | if (len) { | |
01904572 LT |
339 | const char *prefix = o->prefix ? o->prefix : ""; |
340 | struct traverse_info info; | |
341 | ||
342 | setup_traverse_info(&info, prefix); | |
343 | info.fn = unpack_callback; | |
344 | info.data = o; | |
345 | ||
346 | if (traverse_trees(len, t, &info) < 0) | |
347 | return unpack_failed(o, NULL); | |
16da134b JS |
348 | } |
349 | ||
01904572 LT |
350 | /* Any left-over entries in the index? */ |
351 | if (o->merge) { | |
352 | while (o->pos < active_nr) { | |
353 | struct cache_entry *ce = active_cache[o->pos]; | |
354 | if (unpack_index_entry(ce, o) < 0) | |
355 | return unpack_failed(o, NULL); | |
17e46426 | 356 | } |
17e46426 | 357 | } |
16da134b | 358 | |
01904572 LT |
359 | if (o->trivial_merges_only && o->nontrivial_merge) |
360 | return unpack_failed(o, "Merge requires file-level merging"); | |
361 | ||
33ecf7eb | 362 | check_updates(o); |
16da134b JS |
363 | return 0; |
364 | } | |
076b0adc JS |
365 | |
366 | /* Here come the merge functions */ | |
367 | ||
203a2fe1 | 368 | static int reject_merge(struct cache_entry *ce) |
076b0adc | 369 | { |
203a2fe1 DB |
370 | return error("Entry '%s' would be overwritten by merge. Cannot merge.", |
371 | ce->name); | |
076b0adc JS |
372 | } |
373 | ||
374 | static int same(struct cache_entry *a, struct cache_entry *b) | |
375 | { | |
376 | if (!!a != !!b) | |
377 | return 0; | |
378 | if (!a && !b) | |
379 | return 1; | |
380 | return a->ce_mode == b->ce_mode && | |
a89fccd2 | 381 | !hashcmp(a->sha1, b->sha1); |
076b0adc JS |
382 | } |
383 | ||
384 | ||
385 | /* | |
386 | * When a CE gets turned into an unmerged entry, we | |
387 | * want it to be up-to-date | |
388 | */ | |
203a2fe1 | 389 | static int verify_uptodate(struct cache_entry *ce, |
076b0adc JS |
390 | struct unpack_trees_options *o) |
391 | { | |
392 | struct stat st; | |
393 | ||
394 | if (o->index_only || o->reset) | |
203a2fe1 | 395 | return 0; |
076b0adc JS |
396 | |
397 | if (!lstat(ce->name, &st)) { | |
4bd5b7da | 398 | unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID); |
076b0adc | 399 | if (!changed) |
203a2fe1 | 400 | return 0; |
936492d3 JH |
401 | /* |
402 | * NEEDSWORK: the current default policy is to allow | |
403 | * submodule to be out of sync wrt the supermodule | |
404 | * index. This needs to be tightened later for | |
405 | * submodules that are marked to be automatically | |
406 | * checked out. | |
407 | */ | |
7a51ed66 | 408 | if (S_ISGITLINK(ce->ce_mode)) |
203a2fe1 | 409 | return 0; |
076b0adc JS |
410 | errno = 0; |
411 | } | |
076b0adc | 412 | if (errno == ENOENT) |
203a2fe1 | 413 | return 0; |
17e46426 DB |
414 | return o->gently ? -1 : |
415 | error("Entry '%s' not uptodate. Cannot merge.", ce->name); | |
076b0adc JS |
416 | } |
417 | ||
418 | static void invalidate_ce_path(struct cache_entry *ce) | |
419 | { | |
420 | if (ce) | |
421 | cache_tree_invalidate_path(active_cache_tree, ce->name); | |
422 | } | |
423 | ||
0cf73755 SV |
424 | /* |
425 | * Check that checking out ce->sha1 in subdir ce->name is not | |
426 | * going to overwrite any working files. | |
427 | * | |
428 | * Currently, git does not checkout subprojects during a superproject | |
429 | * checkout, so it is not going to overwrite anything. | |
430 | */ | |
431 | static int verify_clean_submodule(struct cache_entry *ce, const char *action, | |
432 | struct unpack_trees_options *o) | |
433 | { | |
434 | return 0; | |
435 | } | |
436 | ||
437 | static int verify_clean_subdirectory(struct cache_entry *ce, const char *action, | |
c8193534 JH |
438 | struct unpack_trees_options *o) |
439 | { | |
440 | /* | |
0cf73755 | 441 | * we are about to extract "ce->name"; we would not want to lose |
c8193534 JH |
442 | * anything in the existing directory there. |
443 | */ | |
444 | int namelen; | |
445 | int pos, i; | |
446 | struct dir_struct d; | |
447 | char *pathbuf; | |
448 | int cnt = 0; | |
0cf73755 SV |
449 | unsigned char sha1[20]; |
450 | ||
7a51ed66 | 451 | if (S_ISGITLINK(ce->ce_mode) && |
0cf73755 SV |
452 | resolve_gitlink_ref(ce->name, "HEAD", sha1) == 0) { |
453 | /* If we are not going to update the submodule, then | |
454 | * we don't care. | |
455 | */ | |
456 | if (!hashcmp(sha1, ce->sha1)) | |
457 | return 0; | |
458 | return verify_clean_submodule(ce, action, o); | |
459 | } | |
c8193534 JH |
460 | |
461 | /* | |
462 | * First let's make sure we do not have a local modification | |
463 | * in that directory. | |
464 | */ | |
0cf73755 SV |
465 | namelen = strlen(ce->name); |
466 | pos = cache_name_pos(ce->name, namelen); | |
c8193534 JH |
467 | if (0 <= pos) |
468 | return cnt; /* we have it as nondirectory */ | |
469 | pos = -pos - 1; | |
470 | for (i = pos; i < active_nr; i++) { | |
471 | struct cache_entry *ce = active_cache[i]; | |
472 | int len = ce_namelen(ce); | |
473 | if (len < namelen || | |
0cf73755 | 474 | strncmp(ce->name, ce->name, namelen) || |
c8193534 JH |
475 | ce->name[namelen] != '/') |
476 | break; | |
477 | /* | |
478 | * ce->name is an entry in the subdirectory. | |
479 | */ | |
480 | if (!ce_stage(ce)) { | |
203a2fe1 DB |
481 | if (verify_uptodate(ce, o)) |
482 | return -1; | |
7a51ed66 | 483 | ce->ce_flags |= CE_REMOVE; |
c8193534 JH |
484 | } |
485 | cnt++; | |
486 | } | |
487 | ||
488 | /* | |
489 | * Then we need to make sure that we do not lose a locally | |
490 | * present file that is not ignored. | |
491 | */ | |
492 | pathbuf = xmalloc(namelen + 2); | |
0cf73755 | 493 | memcpy(pathbuf, ce->name, namelen); |
c8193534 JH |
494 | strcpy(pathbuf+namelen, "/"); |
495 | ||
496 | memset(&d, 0, sizeof(d)); | |
497 | if (o->dir) | |
498 | d.exclude_per_dir = o->dir->exclude_per_dir; | |
0cf73755 | 499 | i = read_directory(&d, ce->name, pathbuf, namelen+1, NULL); |
c8193534 | 500 | if (i) |
17e46426 DB |
501 | return o->gently ? -1 : |
502 | error("Updating '%s' would lose untracked files in it", | |
503 | ce->name); | |
c8193534 JH |
504 | free(pathbuf); |
505 | return cnt; | |
506 | } | |
507 | ||
076b0adc JS |
508 | /* |
509 | * We do not want to remove or overwrite a working tree file that | |
f8a9d428 | 510 | * is not tracked, unless it is ignored. |
076b0adc | 511 | */ |
203a2fe1 DB |
512 | static int verify_absent(struct cache_entry *ce, const char *action, |
513 | struct unpack_trees_options *o) | |
076b0adc JS |
514 | { |
515 | struct stat st; | |
516 | ||
517 | if (o->index_only || o->reset || !o->update) | |
203a2fe1 | 518 | return 0; |
c8193534 | 519 | |
0cf73755 | 520 | if (has_symlink_leading_path(ce->name, NULL)) |
203a2fe1 | 521 | return 0; |
ec0603e1 | 522 | |
0cf73755 | 523 | if (!lstat(ce->name, &st)) { |
c8193534 | 524 | int cnt; |
6831a88a | 525 | int dtype = ce_to_dtype(ce); |
c8193534 | 526 | |
6831a88a | 527 | if (o->dir && excluded(o->dir, ce->name, &dtype)) |
c8193534 | 528 | /* |
0cf73755 | 529 | * ce->name is explicitly excluded, so it is Ok to |
c8193534 JH |
530 | * overwrite it. |
531 | */ | |
203a2fe1 | 532 | return 0; |
c8193534 JH |
533 | if (S_ISDIR(st.st_mode)) { |
534 | /* | |
535 | * We are checking out path "foo" and | |
536 | * found "foo/." in the working tree. | |
537 | * This is tricky -- if we have modified | |
538 | * files that are in "foo/" we would lose | |
539 | * it. | |
540 | */ | |
0cf73755 | 541 | cnt = verify_clean_subdirectory(ce, action, o); |
c8193534 JH |
542 | |
543 | /* | |
544 | * If this removed entries from the index, | |
545 | * what that means is: | |
546 | * | |
547 | * (1) the caller unpack_trees_rec() saw path/foo | |
548 | * in the index, and it has not removed it because | |
549 | * it thinks it is handling 'path' as blob with | |
550 | * D/F conflict; | |
551 | * (2) we will return "ok, we placed a merged entry | |
552 | * in the index" which would cause o->pos to be | |
553 | * incremented by one; | |
554 | * (3) however, original o->pos now has 'path/foo' | |
555 | * marked with "to be removed". | |
556 | * | |
557 | * We need to increment it by the number of | |
558 | * deleted entries here. | |
559 | */ | |
560 | o->pos += cnt; | |
203a2fe1 | 561 | return 0; |
c8193534 JH |
562 | } |
563 | ||
564 | /* | |
565 | * The previous round may already have decided to | |
566 | * delete this path, which is in a subdirectory that | |
567 | * is being replaced with a blob. | |
568 | */ | |
0cf73755 | 569 | cnt = cache_name_pos(ce->name, strlen(ce->name)); |
c8193534 JH |
570 | if (0 <= cnt) { |
571 | struct cache_entry *ce = active_cache[cnt]; | |
7a51ed66 | 572 | if (ce->ce_flags & CE_REMOVE) |
203a2fe1 | 573 | return 0; |
c8193534 JH |
574 | } |
575 | ||
17e46426 DB |
576 | return o->gently ? -1 : |
577 | error("Untracked working tree file '%s' " | |
578 | "would be %s by merge.", ce->name, action); | |
c8193534 | 579 | } |
203a2fe1 | 580 | return 0; |
076b0adc JS |
581 | } |
582 | ||
583 | static int merged_entry(struct cache_entry *merge, struct cache_entry *old, | |
584 | struct unpack_trees_options *o) | |
585 | { | |
7a51ed66 | 586 | merge->ce_flags |= CE_UPDATE; |
076b0adc JS |
587 | if (old) { |
588 | /* | |
589 | * See if we can re-use the old CE directly? | |
590 | * That way we get the uptodate stat info. | |
591 | * | |
592 | * This also removes the UPDATE flag on | |
593 | * a match. | |
594 | */ | |
595 | if (same(old, merge)) { | |
eb7a2f1d | 596 | copy_cache_entry(merge, old); |
076b0adc | 597 | } else { |
203a2fe1 DB |
598 | if (verify_uptodate(old, o)) |
599 | return -1; | |
076b0adc JS |
600 | invalidate_ce_path(old); |
601 | } | |
602 | } | |
603 | else { | |
203a2fe1 DB |
604 | if (verify_absent(merge, "overwritten", o)) |
605 | return -1; | |
076b0adc JS |
606 | invalidate_ce_path(merge); |
607 | } | |
608 | ||
7a51ed66 | 609 | merge->ce_flags &= ~CE_STAGEMASK; |
076b0adc JS |
610 | add_cache_entry(merge, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE); |
611 | return 1; | |
612 | } | |
613 | ||
614 | static int deleted_entry(struct cache_entry *ce, struct cache_entry *old, | |
615 | struct unpack_trees_options *o) | |
616 | { | |
203a2fe1 DB |
617 | if (old) { |
618 | if (verify_uptodate(old, o)) | |
619 | return -1; | |
620 | } else | |
621 | if (verify_absent(ce, "removed", o)) | |
622 | return -1; | |
7a51ed66 | 623 | ce->ce_flags |= CE_REMOVE; |
076b0adc JS |
624 | add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE); |
625 | invalidate_ce_path(ce); | |
626 | return 1; | |
627 | } | |
628 | ||
7f7932ab | 629 | static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o) |
076b0adc JS |
630 | { |
631 | add_cache_entry(ce, ADD_CACHE_OK_TO_ADD); | |
632 | return 1; | |
633 | } | |
634 | ||
635 | #if DBRT_DEBUG | |
636 | static void show_stage_entry(FILE *o, | |
637 | const char *label, const struct cache_entry *ce) | |
638 | { | |
639 | if (!ce) | |
640 | fprintf(o, "%s (missing)\n", label); | |
641 | else | |
642 | fprintf(o, "%s%06o %s %d\t%s\n", | |
643 | label, | |
7a51ed66 | 644 | ce->ce_mode, |
076b0adc JS |
645 | sha1_to_hex(ce->sha1), |
646 | ce_stage(ce), | |
647 | ce->name); | |
648 | } | |
649 | #endif | |
650 | ||
651 | int threeway_merge(struct cache_entry **stages, | |
b48d5a05 LT |
652 | struct unpack_trees_options *o, |
653 | int remove) | |
076b0adc JS |
654 | { |
655 | struct cache_entry *index; | |
656 | struct cache_entry *head; | |
657 | struct cache_entry *remote = stages[o->head_idx + 1]; | |
658 | int count; | |
659 | int head_match = 0; | |
660 | int remote_match = 0; | |
076b0adc JS |
661 | |
662 | int df_conflict_head = 0; | |
663 | int df_conflict_remote = 0; | |
664 | ||
665 | int any_anc_missing = 0; | |
666 | int no_anc_exists = 1; | |
667 | int i; | |
668 | ||
669 | for (i = 1; i < o->head_idx; i++) { | |
4c4caafc | 670 | if (!stages[i] || stages[i] == o->df_conflict_entry) |
076b0adc | 671 | any_anc_missing = 1; |
4c4caafc | 672 | else |
076b0adc | 673 | no_anc_exists = 0; |
076b0adc JS |
674 | } |
675 | ||
676 | index = stages[0]; | |
677 | head = stages[o->head_idx]; | |
678 | ||
679 | if (head == o->df_conflict_entry) { | |
680 | df_conflict_head = 1; | |
681 | head = NULL; | |
682 | } | |
683 | ||
684 | if (remote == o->df_conflict_entry) { | |
685 | df_conflict_remote = 1; | |
686 | remote = NULL; | |
687 | } | |
688 | ||
076b0adc JS |
689 | /* First, if there's a #16 situation, note that to prevent #13 |
690 | * and #14. | |
691 | */ | |
692 | if (!same(remote, head)) { | |
693 | for (i = 1; i < o->head_idx; i++) { | |
694 | if (same(stages[i], head)) { | |
695 | head_match = i; | |
696 | } | |
697 | if (same(stages[i], remote)) { | |
698 | remote_match = i; | |
699 | } | |
700 | } | |
701 | } | |
702 | ||
703 | /* We start with cases where the index is allowed to match | |
704 | * something other than the head: #14(ALT) and #2ALT, where it | |
705 | * is permitted to match the result instead. | |
706 | */ | |
707 | /* #14, #14ALT, #2ALT */ | |
708 | if (remote && !df_conflict_head && head_match && !remote_match) { | |
709 | if (index && !same(index, remote) && !same(index, head)) | |
17e46426 | 710 | return o->gently ? -1 : reject_merge(index); |
076b0adc JS |
711 | return merged_entry(remote, index, o); |
712 | } | |
713 | /* | |
714 | * If we have an entry in the index cache, then we want to | |
715 | * make sure that it matches head. | |
716 | */ | |
4e7c4571 | 717 | if (index && !same(index, head)) |
17e46426 | 718 | return o->gently ? -1 : reject_merge(index); |
076b0adc JS |
719 | |
720 | if (head) { | |
721 | /* #5ALT, #15 */ | |
722 | if (same(head, remote)) | |
723 | return merged_entry(head, index, o); | |
724 | /* #13, #3ALT */ | |
725 | if (!df_conflict_remote && remote_match && !head_match) | |
726 | return merged_entry(head, index, o); | |
727 | } | |
728 | ||
729 | /* #1 */ | |
566b5c05 LT |
730 | if (!head && !remote && any_anc_missing) { |
731 | remove_entry(remove); | |
076b0adc | 732 | return 0; |
566b5c05 | 733 | } |
076b0adc JS |
734 | |
735 | /* Under the new "aggressive" rule, we resolve mostly trivial | |
736 | * cases that we historically had git-merge-one-file resolve. | |
737 | */ | |
738 | if (o->aggressive) { | |
739 | int head_deleted = !head && !df_conflict_head; | |
740 | int remote_deleted = !remote && !df_conflict_remote; | |
0cf73755 | 741 | struct cache_entry *ce = NULL; |
4c4caafc JH |
742 | |
743 | if (index) | |
0cf73755 | 744 | ce = index; |
4c4caafc | 745 | else if (head) |
0cf73755 | 746 | ce = head; |
4c4caafc | 747 | else if (remote) |
0cf73755 | 748 | ce = remote; |
4c4caafc JH |
749 | else { |
750 | for (i = 1; i < o->head_idx; i++) { | |
751 | if (stages[i] && stages[i] != o->df_conflict_entry) { | |
0cf73755 | 752 | ce = stages[i]; |
4c4caafc JH |
753 | break; |
754 | } | |
755 | } | |
756 | } | |
757 | ||
076b0adc JS |
758 | /* |
759 | * Deleted in both. | |
760 | * Deleted in one and unchanged in the other. | |
761 | */ | |
762 | if ((head_deleted && remote_deleted) || | |
763 | (head_deleted && remote && remote_match) || | |
764 | (remote_deleted && head && head_match)) { | |
566b5c05 | 765 | remove_entry(remove); |
076b0adc JS |
766 | if (index) |
767 | return deleted_entry(index, index, o); | |
203a2fe1 DB |
768 | else if (ce && !head_deleted) { |
769 | if (verify_absent(ce, "removed", o)) | |
770 | return -1; | |
771 | } | |
076b0adc JS |
772 | return 0; |
773 | } | |
774 | /* | |
775 | * Added in both, identically. | |
776 | */ | |
777 | if (no_anc_exists && head && remote && same(head, remote)) | |
778 | return merged_entry(head, index, o); | |
779 | ||
780 | } | |
781 | ||
782 | /* Below are "no merge" cases, which require that the index be | |
783 | * up-to-date to avoid the files getting overwritten with | |
784 | * conflict resolution files. | |
785 | */ | |
786 | if (index) { | |
203a2fe1 DB |
787 | if (verify_uptodate(index, o)) |
788 | return -1; | |
076b0adc | 789 | } |
076b0adc | 790 | |
566b5c05 | 791 | remove_entry(remove); |
076b0adc JS |
792 | o->nontrivial_merge = 1; |
793 | ||
ea4b52a8 | 794 | /* #2, #3, #4, #6, #7, #9, #10, #11. */ |
076b0adc JS |
795 | count = 0; |
796 | if (!head_match || !remote_match) { | |
797 | for (i = 1; i < o->head_idx; i++) { | |
4c4caafc | 798 | if (stages[i] && stages[i] != o->df_conflict_entry) { |
7f7932ab | 799 | keep_entry(stages[i], o); |
076b0adc JS |
800 | count++; |
801 | break; | |
802 | } | |
803 | } | |
804 | } | |
805 | #if DBRT_DEBUG | |
806 | else { | |
807 | fprintf(stderr, "read-tree: warning #16 detected\n"); | |
808 | show_stage_entry(stderr, "head ", stages[head_match]); | |
809 | show_stage_entry(stderr, "remote ", stages[remote_match]); | |
810 | } | |
811 | #endif | |
7f7932ab JH |
812 | if (head) { count += keep_entry(head, o); } |
813 | if (remote) { count += keep_entry(remote, o); } | |
076b0adc JS |
814 | return count; |
815 | } | |
816 | ||
817 | /* | |
818 | * Two-way merge. | |
819 | * | |
820 | * The rule is to "carry forward" what is in the index without losing | |
821 | * information across a "fast forward", favoring a successful merge | |
822 | * over a merge failure when it makes sense. For details of the | |
823 | * "carry forward" rule, please see <Documentation/git-read-tree.txt>. | |
824 | * | |
825 | */ | |
826 | int twoway_merge(struct cache_entry **src, | |
b48d5a05 LT |
827 | struct unpack_trees_options *o, |
828 | int remove) | |
076b0adc JS |
829 | { |
830 | struct cache_entry *current = src[0]; | |
b8ba1535 JH |
831 | struct cache_entry *oldtree = src[1]; |
832 | struct cache_entry *newtree = src[2]; | |
076b0adc JS |
833 | |
834 | if (o->merge_size != 2) | |
835 | return error("Cannot do a twoway merge of %d trees", | |
836 | o->merge_size); | |
837 | ||
b8ba1535 JH |
838 | if (oldtree == o->df_conflict_entry) |
839 | oldtree = NULL; | |
840 | if (newtree == o->df_conflict_entry) | |
841 | newtree = NULL; | |
842 | ||
076b0adc JS |
843 | if (current) { |
844 | if ((!oldtree && !newtree) || /* 4 and 5 */ | |
845 | (!oldtree && newtree && | |
846 | same(current, newtree)) || /* 6 and 7 */ | |
847 | (oldtree && newtree && | |
848 | same(oldtree, newtree)) || /* 14 and 15 */ | |
849 | (oldtree && newtree && | |
b8ba1535 | 850 | !same(oldtree, newtree) && /* 18 and 19 */ |
076b0adc | 851 | same(current, newtree))) { |
7f7932ab | 852 | return keep_entry(current, o); |
076b0adc JS |
853 | } |
854 | else if (oldtree && !newtree && same(current, oldtree)) { | |
855 | /* 10 or 11 */ | |
d699676d | 856 | remove_entry(remove); |
076b0adc JS |
857 | return deleted_entry(oldtree, current, o); |
858 | } | |
859 | else if (oldtree && newtree && | |
860 | same(current, oldtree) && !same(current, newtree)) { | |
861 | /* 20 or 21 */ | |
862 | return merged_entry(newtree, current, o); | |
863 | } | |
864 | else { | |
865 | /* all other failures */ | |
d699676d | 866 | remove_entry(remove); |
076b0adc | 867 | if (oldtree) |
17e46426 | 868 | return o->gently ? -1 : reject_merge(oldtree); |
076b0adc | 869 | if (current) |
17e46426 | 870 | return o->gently ? -1 : reject_merge(current); |
076b0adc | 871 | if (newtree) |
17e46426 | 872 | return o->gently ? -1 : reject_merge(newtree); |
076b0adc JS |
873 | return -1; |
874 | } | |
875 | } | |
876 | else if (newtree) | |
877 | return merged_entry(newtree, current, o); | |
d699676d LT |
878 | remove_entry(remove); |
879 | return deleted_entry(oldtree, current, o); | |
076b0adc JS |
880 | } |
881 | ||
882 | /* | |
883 | * Bind merge. | |
884 | * | |
885 | * Keep the index entries at stage0, collapse stage1 but make sure | |
886 | * stage0 does not have anything there. | |
887 | */ | |
888 | int bind_merge(struct cache_entry **src, | |
b48d5a05 LT |
889 | struct unpack_trees_options *o, |
890 | int remove) | |
076b0adc JS |
891 | { |
892 | struct cache_entry *old = src[0]; | |
893 | struct cache_entry *a = src[1]; | |
894 | ||
895 | if (o->merge_size != 1) | |
896 | return error("Cannot do a bind merge of %d trees\n", | |
897 | o->merge_size); | |
898 | if (a && old) | |
17e46426 DB |
899 | return o->gently ? -1 : |
900 | error("Entry '%s' overlaps. Cannot bind.", a->name); | |
076b0adc | 901 | if (!a) |
7f7932ab | 902 | return keep_entry(old, o); |
076b0adc JS |
903 | else |
904 | return merged_entry(a, NULL, o); | |
905 | } | |
906 | ||
907 | /* | |
908 | * One-way merge. | |
909 | * | |
910 | * The rule is: | |
911 | * - take the stat information from stage0, take the data from stage1 | |
912 | */ | |
913 | int oneway_merge(struct cache_entry **src, | |
b48d5a05 LT |
914 | struct unpack_trees_options *o, |
915 | int remove) | |
076b0adc JS |
916 | { |
917 | struct cache_entry *old = src[0]; | |
918 | struct cache_entry *a = src[1]; | |
919 | ||
920 | if (o->merge_size != 1) | |
921 | return error("Cannot do a oneway merge of %d trees", | |
922 | o->merge_size); | |
923 | ||
288f072e LT |
924 | if (!a) { |
925 | remove_entry(remove); | |
076b0adc | 926 | return deleted_entry(old, old, o); |
288f072e | 927 | } |
076b0adc JS |
928 | if (old && same(old, a)) { |
929 | if (o->reset) { | |
930 | struct stat st; | |
931 | if (lstat(old->name, &st) || | |
4bd5b7da | 932 | ce_match_stat(old, &st, CE_MATCH_IGNORE_VALID)) |
7a51ed66 | 933 | old->ce_flags |= CE_UPDATE; |
076b0adc | 934 | } |
7f7932ab | 935 | return keep_entry(old, o); |
076b0adc JS |
936 | } |
937 | return merged_entry(a, old, o); | |
938 | } |