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