]>
Commit | Line | Data |
---|---|---|
1 | #include "cache.h" | |
2 | #include "strvec.h" | |
3 | #include "repository.h" | |
4 | #include "config.h" | |
5 | #include "dir.h" | |
6 | #include "environment.h" | |
7 | #include "gettext.h" | |
8 | #include "hex.h" | |
9 | #include "tree.h" | |
10 | #include "tree-walk.h" | |
11 | #include "cache-tree.h" | |
12 | #include "unpack-trees.h" | |
13 | #include "progress.h" | |
14 | #include "refs.h" | |
15 | #include "attr.h" | |
16 | #include "split-index.h" | |
17 | #include "sparse-index.h" | |
18 | #include "submodule.h" | |
19 | #include "submodule-config.h" | |
20 | #include "fsmonitor.h" | |
21 | #include "object-store.h" | |
22 | #include "promisor-remote.h" | |
23 | #include "entry.h" | |
24 | #include "parallel-checkout.h" | |
25 | ||
26 | /* | |
27 | * Error messages expected by scripts out of plumbing commands such as | |
28 | * read-tree. Non-scripted Porcelain is not required to use these messages | |
29 | * and in fact are encouraged to reword them to better suit their particular | |
30 | * situation better. See how "git checkout" and "git merge" replaces | |
31 | * them using setup_unpack_trees_porcelain(), for example. | |
32 | */ | |
33 | static const char *unpack_plumbing_errors[NB_UNPACK_TREES_WARNING_TYPES] = { | |
34 | /* ERROR_WOULD_OVERWRITE */ | |
35 | "Entry '%s' would be overwritten by merge. Cannot merge.", | |
36 | ||
37 | /* ERROR_NOT_UPTODATE_FILE */ | |
38 | "Entry '%s' not uptodate. Cannot merge.", | |
39 | ||
40 | /* ERROR_NOT_UPTODATE_DIR */ | |
41 | "Updating '%s' would lose untracked files in it", | |
42 | ||
43 | /* ERROR_CWD_IN_THE_WAY */ | |
44 | "Refusing to remove '%s' since it is the current working directory.", | |
45 | ||
46 | /* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */ | |
47 | "Untracked working tree file '%s' would be overwritten by merge.", | |
48 | ||
49 | /* ERROR_WOULD_LOSE_UNTRACKED_REMOVED */ | |
50 | "Untracked working tree file '%s' would be removed by merge.", | |
51 | ||
52 | /* ERROR_BIND_OVERLAP */ | |
53 | "Entry '%s' overlaps with '%s'. Cannot bind.", | |
54 | ||
55 | /* ERROR_WOULD_LOSE_SUBMODULE */ | |
56 | "Submodule '%s' cannot checkout new HEAD.", | |
57 | ||
58 | /* NB_UNPACK_TREES_ERROR_TYPES; just a meta value */ | |
59 | "", | |
60 | ||
61 | /* WARNING_SPARSE_NOT_UPTODATE_FILE */ | |
62 | "Path '%s' not uptodate; will not remove from working tree.", | |
63 | ||
64 | /* WARNING_SPARSE_UNMERGED_FILE */ | |
65 | "Path '%s' unmerged; will not remove from working tree.", | |
66 | ||
67 | /* WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN */ | |
68 | "Path '%s' already present; will not overwrite with sparse update.", | |
69 | }; | |
70 | ||
71 | #define ERRORMSG(o,type) \ | |
72 | ( ((o) && (o)->internal.msgs[(type)]) \ | |
73 | ? ((o)->internal.msgs[(type)]) \ | |
74 | : (unpack_plumbing_errors[(type)]) ) | |
75 | ||
76 | static const char *super_prefixed(const char *path, const char *super_prefix) | |
77 | { | |
78 | /* | |
79 | * It is necessary and sufficient to have two static buffers | |
80 | * here, as the return value of this function is fed to | |
81 | * error() using the unpack_*_errors[] templates we see above. | |
82 | */ | |
83 | static struct strbuf buf[2] = {STRBUF_INIT, STRBUF_INIT}; | |
84 | static int super_prefix_len = -1; | |
85 | static unsigned idx = ARRAY_SIZE(buf) - 1; | |
86 | ||
87 | if (super_prefix_len < 0) { | |
88 | if (!super_prefix) { | |
89 | super_prefix_len = 0; | |
90 | } else { | |
91 | int i; | |
92 | for (i = 0; i < ARRAY_SIZE(buf); i++) | |
93 | strbuf_addstr(&buf[i], super_prefix); | |
94 | super_prefix_len = buf[0].len; | |
95 | } | |
96 | } | |
97 | ||
98 | if (!super_prefix_len) | |
99 | return path; | |
100 | ||
101 | if (++idx >= ARRAY_SIZE(buf)) | |
102 | idx = 0; | |
103 | ||
104 | strbuf_setlen(&buf[idx], super_prefix_len); | |
105 | strbuf_addstr(&buf[idx], path); | |
106 | ||
107 | return buf[idx].buf; | |
108 | } | |
109 | ||
110 | void setup_unpack_trees_porcelain(struct unpack_trees_options *opts, | |
111 | const char *cmd) | |
112 | { | |
113 | int i; | |
114 | const char **msgs = opts->internal.msgs; | |
115 | const char *msg; | |
116 | ||
117 | strvec_init(&opts->internal.msgs_to_free); | |
118 | ||
119 | if (!strcmp(cmd, "checkout")) | |
120 | msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE) | |
121 | ? _("Your local changes to the following files would be overwritten by checkout:\n%%s" | |
122 | "Please commit your changes or stash them before you switch branches.") | |
123 | : _("Your local changes to the following files would be overwritten by checkout:\n%%s"); | |
124 | else if (!strcmp(cmd, "merge")) | |
125 | msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE) | |
126 | ? _("Your local changes to the following files would be overwritten by merge:\n%%s" | |
127 | "Please commit your changes or stash them before you merge.") | |
128 | : _("Your local changes to the following files would be overwritten by merge:\n%%s"); | |
129 | else | |
130 | msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE) | |
131 | ? _("Your local changes to the following files would be overwritten by %s:\n%%s" | |
132 | "Please commit your changes or stash them before you %s.") | |
133 | : _("Your local changes to the following files would be overwritten by %s:\n%%s"); | |
134 | msgs[ERROR_WOULD_OVERWRITE] = msgs[ERROR_NOT_UPTODATE_FILE] = | |
135 | strvec_pushf(&opts->internal.msgs_to_free, msg, cmd, cmd); | |
136 | ||
137 | msgs[ERROR_NOT_UPTODATE_DIR] = | |
138 | _("Updating the following directories would lose untracked files in them:\n%s"); | |
139 | ||
140 | msgs[ERROR_CWD_IN_THE_WAY] = | |
141 | _("Refusing to remove the current working directory:\n%s"); | |
142 | ||
143 | if (!strcmp(cmd, "checkout")) | |
144 | msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE) | |
145 | ? _("The following untracked working tree files would be removed by checkout:\n%%s" | |
146 | "Please move or remove them before you switch branches.") | |
147 | : _("The following untracked working tree files would be removed by checkout:\n%%s"); | |
148 | else if (!strcmp(cmd, "merge")) | |
149 | msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE) | |
150 | ? _("The following untracked working tree files would be removed by merge:\n%%s" | |
151 | "Please move or remove them before you merge.") | |
152 | : _("The following untracked working tree files would be removed by merge:\n%%s"); | |
153 | else | |
154 | msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE) | |
155 | ? _("The following untracked working tree files would be removed by %s:\n%%s" | |
156 | "Please move or remove them before you %s.") | |
157 | : _("The following untracked working tree files would be removed by %s:\n%%s"); | |
158 | msgs[ERROR_WOULD_LOSE_UNTRACKED_REMOVED] = | |
159 | strvec_pushf(&opts->internal.msgs_to_free, msg, cmd, cmd); | |
160 | ||
161 | if (!strcmp(cmd, "checkout")) | |
162 | msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE) | |
163 | ? _("The following untracked working tree files would be overwritten by checkout:\n%%s" | |
164 | "Please move or remove them before you switch branches.") | |
165 | : _("The following untracked working tree files would be overwritten by checkout:\n%%s"); | |
166 | else if (!strcmp(cmd, "merge")) | |
167 | msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE) | |
168 | ? _("The following untracked working tree files would be overwritten by merge:\n%%s" | |
169 | "Please move or remove them before you merge.") | |
170 | : _("The following untracked working tree files would be overwritten by merge:\n%%s"); | |
171 | else | |
172 | msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE) | |
173 | ? _("The following untracked working tree files would be overwritten by %s:\n%%s" | |
174 | "Please move or remove them before you %s.") | |
175 | : _("The following untracked working tree files would be overwritten by %s:\n%%s"); | |
176 | msgs[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN] = | |
177 | strvec_pushf(&opts->internal.msgs_to_free, msg, cmd, cmd); | |
178 | ||
179 | /* | |
180 | * Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we | |
181 | * cannot easily display it as a list. | |
182 | */ | |
183 | msgs[ERROR_BIND_OVERLAP] = _("Entry '%s' overlaps with '%s'. Cannot bind."); | |
184 | ||
185 | msgs[ERROR_WOULD_LOSE_SUBMODULE] = | |
186 | _("Cannot update submodule:\n%s"); | |
187 | ||
188 | msgs[WARNING_SPARSE_NOT_UPTODATE_FILE] = | |
189 | _("The following paths are not up to date and were left despite sparse patterns:\n%s"); | |
190 | msgs[WARNING_SPARSE_UNMERGED_FILE] = | |
191 | _("The following paths are unmerged and were left despite sparse patterns:\n%s"); | |
192 | msgs[WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN] = | |
193 | _("The following paths were already present and thus not updated despite sparse patterns:\n%s"); | |
194 | ||
195 | opts->internal.show_all_errors = 1; | |
196 | /* rejected paths may not have a static buffer */ | |
197 | for (i = 0; i < ARRAY_SIZE(opts->internal.unpack_rejects); i++) | |
198 | opts->internal.unpack_rejects[i].strdup_strings = 1; | |
199 | } | |
200 | ||
201 | void clear_unpack_trees_porcelain(struct unpack_trees_options *opts) | |
202 | { | |
203 | strvec_clear(&opts->internal.msgs_to_free); | |
204 | memset(opts->internal.msgs, 0, sizeof(opts->internal.msgs)); | |
205 | } | |
206 | ||
207 | static int do_add_entry(struct unpack_trees_options *o, struct cache_entry *ce, | |
208 | unsigned int set, unsigned int clear) | |
209 | { | |
210 | clear |= CE_HASHED; | |
211 | ||
212 | if (set & CE_REMOVE) | |
213 | set |= CE_WT_REMOVE; | |
214 | ||
215 | ce->ce_flags = (ce->ce_flags & ~clear) | set; | |
216 | return add_index_entry(&o->internal.result, ce, | |
217 | ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); | |
218 | } | |
219 | ||
220 | static void add_entry(struct unpack_trees_options *o, | |
221 | const struct cache_entry *ce, | |
222 | unsigned int set, unsigned int clear) | |
223 | { | |
224 | do_add_entry(o, dup_cache_entry(ce, &o->internal.result), set, clear); | |
225 | } | |
226 | ||
227 | /* | |
228 | * add error messages on path <path> | |
229 | * corresponding to the type <e> with the message <msg> | |
230 | * indicating if it should be display in porcelain or not | |
231 | */ | |
232 | static int add_rejected_path(struct unpack_trees_options *o, | |
233 | enum unpack_trees_error_types e, | |
234 | const char *path) | |
235 | { | |
236 | if (o->quiet) | |
237 | return -1; | |
238 | ||
239 | if (!o->internal.show_all_errors) | |
240 | return error(ERRORMSG(o, e), super_prefixed(path, | |
241 | o->super_prefix)); | |
242 | ||
243 | /* | |
244 | * Otherwise, insert in a list for future display by | |
245 | * display_(error|warning)_msgs() | |
246 | */ | |
247 | string_list_append(&o->internal.unpack_rejects[e], path); | |
248 | return -1; | |
249 | } | |
250 | ||
251 | /* | |
252 | * display all the error messages stored in a nice way | |
253 | */ | |
254 | static void display_error_msgs(struct unpack_trees_options *o) | |
255 | { | |
256 | int e; | |
257 | unsigned error_displayed = 0; | |
258 | for (e = 0; e < NB_UNPACK_TREES_ERROR_TYPES; e++) { | |
259 | struct string_list *rejects = &o->internal.unpack_rejects[e]; | |
260 | ||
261 | if (rejects->nr > 0) { | |
262 | int i; | |
263 | struct strbuf path = STRBUF_INIT; | |
264 | ||
265 | error_displayed = 1; | |
266 | for (i = 0; i < rejects->nr; i++) | |
267 | strbuf_addf(&path, "\t%s\n", rejects->items[i].string); | |
268 | error(ERRORMSG(o, e), super_prefixed(path.buf, | |
269 | o->super_prefix)); | |
270 | strbuf_release(&path); | |
271 | } | |
272 | string_list_clear(rejects, 0); | |
273 | } | |
274 | if (error_displayed) | |
275 | fprintf(stderr, _("Aborting\n")); | |
276 | } | |
277 | ||
278 | /* | |
279 | * display all the warning messages stored in a nice way | |
280 | */ | |
281 | static void display_warning_msgs(struct unpack_trees_options *o) | |
282 | { | |
283 | int e; | |
284 | unsigned warning_displayed = 0; | |
285 | for (e = NB_UNPACK_TREES_ERROR_TYPES + 1; | |
286 | e < NB_UNPACK_TREES_WARNING_TYPES; e++) { | |
287 | struct string_list *rejects = &o->internal.unpack_rejects[e]; | |
288 | ||
289 | if (rejects->nr > 0) { | |
290 | int i; | |
291 | struct strbuf path = STRBUF_INIT; | |
292 | ||
293 | warning_displayed = 1; | |
294 | for (i = 0; i < rejects->nr; i++) | |
295 | strbuf_addf(&path, "\t%s\n", rejects->items[i].string); | |
296 | warning(ERRORMSG(o, e), super_prefixed(path.buf, | |
297 | o->super_prefix)); | |
298 | strbuf_release(&path); | |
299 | } | |
300 | string_list_clear(rejects, 0); | |
301 | } | |
302 | if (warning_displayed) | |
303 | fprintf(stderr, _("After fixing the above paths, you may want to run `git sparse-checkout reapply`.\n")); | |
304 | } | |
305 | static int check_submodule_move_head(const struct cache_entry *ce, | |
306 | const char *old_id, | |
307 | const char *new_id, | |
308 | struct unpack_trees_options *o) | |
309 | { | |
310 | unsigned flags = SUBMODULE_MOVE_HEAD_DRY_RUN; | |
311 | const struct submodule *sub = submodule_from_ce(ce); | |
312 | ||
313 | if (!sub) | |
314 | return 0; | |
315 | ||
316 | if (o->reset) | |
317 | flags |= SUBMODULE_MOVE_HEAD_FORCE; | |
318 | ||
319 | if (submodule_move_head(ce->name, o->super_prefix, old_id, new_id, | |
320 | flags)) | |
321 | return add_rejected_path(o, ERROR_WOULD_LOSE_SUBMODULE, ce->name); | |
322 | return 0; | |
323 | } | |
324 | ||
325 | /* | |
326 | * Perform the loading of the repository's gitmodules file. This function is | |
327 | * used by 'check_update()' to perform loading of the gitmodules file in two | |
328 | * different situations: | |
329 | * (1) before removing entries from the working tree if the gitmodules file has | |
330 | * been marked for removal. This situation is specified by 'state' == NULL. | |
331 | * (2) before checking out entries to the working tree if the gitmodules file | |
332 | * has been marked for update. This situation is specified by 'state' != NULL. | |
333 | */ | |
334 | static void load_gitmodules_file(struct index_state *index, | |
335 | struct checkout *state) | |
336 | { | |
337 | int pos = index_name_pos(index, GITMODULES_FILE, strlen(GITMODULES_FILE)); | |
338 | ||
339 | if (pos >= 0) { | |
340 | struct cache_entry *ce = index->cache[pos]; | |
341 | if (!state && ce->ce_flags & CE_WT_REMOVE) { | |
342 | repo_read_gitmodules(the_repository, 0); | |
343 | } else if (state && (ce->ce_flags & CE_UPDATE)) { | |
344 | submodule_free(the_repository); | |
345 | checkout_entry(ce, state, NULL, NULL); | |
346 | repo_read_gitmodules(the_repository, 0); | |
347 | } | |
348 | } | |
349 | } | |
350 | ||
351 | static struct progress *get_progress(struct unpack_trees_options *o, | |
352 | struct index_state *index) | |
353 | { | |
354 | unsigned cnt = 0, total = 0; | |
355 | ||
356 | if (!o->update || !o->verbose_update) | |
357 | return NULL; | |
358 | ||
359 | for (; cnt < index->cache_nr; cnt++) { | |
360 | const struct cache_entry *ce = index->cache[cnt]; | |
361 | if (ce->ce_flags & (CE_UPDATE | CE_WT_REMOVE)) | |
362 | total++; | |
363 | } | |
364 | ||
365 | return start_delayed_progress(_("Updating files"), total); | |
366 | } | |
367 | ||
368 | static void setup_collided_checkout_detection(struct checkout *state, | |
369 | struct index_state *index) | |
370 | { | |
371 | int i; | |
372 | ||
373 | state->clone = 1; | |
374 | for (i = 0; i < index->cache_nr; i++) | |
375 | index->cache[i]->ce_flags &= ~CE_MATCHED; | |
376 | } | |
377 | ||
378 | static void report_collided_checkout(struct index_state *index) | |
379 | { | |
380 | struct string_list list = STRING_LIST_INIT_NODUP; | |
381 | int i; | |
382 | ||
383 | for (i = 0; i < index->cache_nr; i++) { | |
384 | struct cache_entry *ce = index->cache[i]; | |
385 | ||
386 | if (!(ce->ce_flags & CE_MATCHED)) | |
387 | continue; | |
388 | ||
389 | string_list_append(&list, ce->name); | |
390 | ce->ce_flags &= ~CE_MATCHED; | |
391 | } | |
392 | ||
393 | list.cmp = fspathcmp; | |
394 | string_list_sort(&list); | |
395 | ||
396 | if (list.nr) { | |
397 | warning(_("the following paths have collided (e.g. case-sensitive paths\n" | |
398 | "on a case-insensitive filesystem) and only one from the same\n" | |
399 | "colliding group is in the working tree:\n")); | |
400 | ||
401 | for (i = 0; i < list.nr; i++) | |
402 | fprintf(stderr, " '%s'\n", list.items[i].string); | |
403 | } | |
404 | ||
405 | string_list_clear(&list, 0); | |
406 | } | |
407 | ||
408 | static int must_checkout(const struct cache_entry *ce) | |
409 | { | |
410 | return ce->ce_flags & CE_UPDATE; | |
411 | } | |
412 | ||
413 | static int check_updates(struct unpack_trees_options *o, | |
414 | struct index_state *index) | |
415 | { | |
416 | unsigned cnt = 0; | |
417 | int errs = 0; | |
418 | struct progress *progress; | |
419 | struct checkout state = CHECKOUT_INIT; | |
420 | int i, pc_workers, pc_threshold; | |
421 | ||
422 | trace_performance_enter(); | |
423 | state.super_prefix = o->super_prefix; | |
424 | state.force = 1; | |
425 | state.quiet = 1; | |
426 | state.refresh_cache = 1; | |
427 | state.istate = index; | |
428 | clone_checkout_metadata(&state.meta, &o->meta, NULL); | |
429 | ||
430 | if (!o->update || o->dry_run) { | |
431 | remove_marked_cache_entries(index, 0); | |
432 | trace_performance_leave("check_updates"); | |
433 | return 0; | |
434 | } | |
435 | ||
436 | if (o->clone) | |
437 | setup_collided_checkout_detection(&state, index); | |
438 | ||
439 | progress = get_progress(o, index); | |
440 | ||
441 | /* Start with clean cache to avoid using any possibly outdated info. */ | |
442 | invalidate_lstat_cache(); | |
443 | ||
444 | git_attr_set_direction(GIT_ATTR_CHECKOUT); | |
445 | ||
446 | if (should_update_submodules()) | |
447 | load_gitmodules_file(index, NULL); | |
448 | ||
449 | for (i = 0; i < index->cache_nr; i++) { | |
450 | const struct cache_entry *ce = index->cache[i]; | |
451 | ||
452 | if (ce->ce_flags & CE_WT_REMOVE) { | |
453 | display_progress(progress, ++cnt); | |
454 | unlink_entry(ce, o->super_prefix); | |
455 | } | |
456 | } | |
457 | ||
458 | remove_marked_cache_entries(index, 0); | |
459 | remove_scheduled_dirs(); | |
460 | ||
461 | if (should_update_submodules()) | |
462 | load_gitmodules_file(index, &state); | |
463 | ||
464 | if (has_promisor_remote()) | |
465 | /* | |
466 | * Prefetch the objects that are to be checked out in the loop | |
467 | * below. | |
468 | */ | |
469 | prefetch_cache_entries(index, must_checkout); | |
470 | ||
471 | get_parallel_checkout_configs(&pc_workers, &pc_threshold); | |
472 | ||
473 | enable_delayed_checkout(&state); | |
474 | if (pc_workers > 1) | |
475 | init_parallel_checkout(); | |
476 | for (i = 0; i < index->cache_nr; i++) { | |
477 | struct cache_entry *ce = index->cache[i]; | |
478 | ||
479 | if (must_checkout(ce)) { | |
480 | size_t last_pc_queue_size = pc_queue_size(); | |
481 | ||
482 | if (ce->ce_flags & CE_WT_REMOVE) | |
483 | BUG("both update and delete flags are set on %s", | |
484 | ce->name); | |
485 | ce->ce_flags &= ~CE_UPDATE; | |
486 | errs |= checkout_entry(ce, &state, NULL, NULL); | |
487 | ||
488 | if (last_pc_queue_size == pc_queue_size()) | |
489 | display_progress(progress, ++cnt); | |
490 | } | |
491 | } | |
492 | if (pc_workers > 1) | |
493 | errs |= run_parallel_checkout(&state, pc_workers, pc_threshold, | |
494 | progress, &cnt); | |
495 | stop_progress(&progress); | |
496 | errs |= finish_delayed_checkout(&state, o->verbose_update); | |
497 | git_attr_set_direction(GIT_ATTR_CHECKIN); | |
498 | ||
499 | if (o->clone) | |
500 | report_collided_checkout(index); | |
501 | ||
502 | trace_performance_leave("check_updates"); | |
503 | return errs != 0; | |
504 | } | |
505 | ||
506 | static int verify_uptodate_sparse(const struct cache_entry *ce, | |
507 | struct unpack_trees_options *o); | |
508 | static int verify_absent_sparse(const struct cache_entry *ce, | |
509 | enum unpack_trees_error_types, | |
510 | struct unpack_trees_options *o); | |
511 | ||
512 | static int apply_sparse_checkout(struct index_state *istate, | |
513 | struct cache_entry *ce, | |
514 | struct unpack_trees_options *o) | |
515 | { | |
516 | int was_skip_worktree = ce_skip_worktree(ce); | |
517 | ||
518 | if (ce->ce_flags & CE_NEW_SKIP_WORKTREE) | |
519 | ce->ce_flags |= CE_SKIP_WORKTREE; | |
520 | else | |
521 | ce->ce_flags &= ~CE_SKIP_WORKTREE; | |
522 | if (was_skip_worktree != ce_skip_worktree(ce)) { | |
523 | ce->ce_flags |= CE_UPDATE_IN_BASE; | |
524 | mark_fsmonitor_invalid(istate, ce); | |
525 | istate->cache_changed |= CE_ENTRY_CHANGED; | |
526 | } | |
527 | ||
528 | /* | |
529 | * if (!was_skip_worktree && !ce_skip_worktree()) { | |
530 | * This is perfectly normal. Move on; | |
531 | * } | |
532 | */ | |
533 | ||
534 | /* | |
535 | * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout | |
536 | * area as a result of ce_skip_worktree() shortcuts in | |
537 | * verify_absent() and verify_uptodate(). | |
538 | * Make sure they don't modify worktree if they are already | |
539 | * outside checkout area | |
540 | */ | |
541 | if (was_skip_worktree && ce_skip_worktree(ce)) { | |
542 | ce->ce_flags &= ~CE_UPDATE; | |
543 | ||
544 | /* | |
545 | * By default, when CE_REMOVE is on, CE_WT_REMOVE is also | |
546 | * on to get that file removed from both index and worktree. | |
547 | * If that file is already outside worktree area, don't | |
548 | * bother remove it. | |
549 | */ | |
550 | if (ce->ce_flags & CE_REMOVE) | |
551 | ce->ce_flags &= ~CE_WT_REMOVE; | |
552 | } | |
553 | ||
554 | if (!was_skip_worktree && ce_skip_worktree(ce)) { | |
555 | /* | |
556 | * If CE_UPDATE is set, verify_uptodate() must be called already | |
557 | * also stat info may have lost after merged_entry() so calling | |
558 | * verify_uptodate() again may fail | |
559 | */ | |
560 | if (!(ce->ce_flags & CE_UPDATE) && | |
561 | verify_uptodate_sparse(ce, o)) { | |
562 | ce->ce_flags &= ~CE_SKIP_WORKTREE; | |
563 | return -1; | |
564 | } | |
565 | ce->ce_flags |= CE_WT_REMOVE; | |
566 | ce->ce_flags &= ~CE_UPDATE; | |
567 | } | |
568 | if (was_skip_worktree && !ce_skip_worktree(ce)) { | |
569 | if (verify_absent_sparse(ce, WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN, o)) | |
570 | return -1; | |
571 | ce->ce_flags |= CE_UPDATE; | |
572 | } | |
573 | return 0; | |
574 | } | |
575 | ||
576 | static int warn_conflicted_path(struct index_state *istate, | |
577 | int i, | |
578 | struct unpack_trees_options *o) | |
579 | { | |
580 | char *conflicting_path = istate->cache[i]->name; | |
581 | int count = 0; | |
582 | ||
583 | add_rejected_path(o, WARNING_SPARSE_UNMERGED_FILE, conflicting_path); | |
584 | ||
585 | /* Find out how many higher stage entries are at same path */ | |
586 | while ((++count) + i < istate->cache_nr && | |
587 | !strcmp(conflicting_path, istate->cache[count + i]->name)) | |
588 | ; /* do nothing */ | |
589 | ||
590 | return count; | |
591 | } | |
592 | ||
593 | static inline int call_unpack_fn(const struct cache_entry * const *src, | |
594 | struct unpack_trees_options *o) | |
595 | { | |
596 | int ret = o->fn(src, o); | |
597 | if (ret > 0) | |
598 | ret = 0; | |
599 | return ret; | |
600 | } | |
601 | ||
602 | static void mark_ce_used(struct cache_entry *ce, struct unpack_trees_options *o) | |
603 | { | |
604 | ce->ce_flags |= CE_UNPACKED; | |
605 | ||
606 | if (o->internal.cache_bottom < o->src_index->cache_nr && | |
607 | o->src_index->cache[o->internal.cache_bottom] == ce) { | |
608 | int bottom = o->internal.cache_bottom; | |
609 | ||
610 | while (bottom < o->src_index->cache_nr && | |
611 | o->src_index->cache[bottom]->ce_flags & CE_UNPACKED) | |
612 | bottom++; | |
613 | o->internal.cache_bottom = bottom; | |
614 | } | |
615 | } | |
616 | ||
617 | static void mark_all_ce_unused(struct index_state *index) | |
618 | { | |
619 | int i; | |
620 | for (i = 0; i < index->cache_nr; i++) | |
621 | index->cache[i]->ce_flags &= ~(CE_UNPACKED | CE_ADDED | CE_NEW_SKIP_WORKTREE); | |
622 | } | |
623 | ||
624 | static int locate_in_src_index(const struct cache_entry *ce, | |
625 | struct unpack_trees_options *o) | |
626 | { | |
627 | struct index_state *index = o->src_index; | |
628 | int len = ce_namelen(ce); | |
629 | int pos = index_name_pos(index, ce->name, len); | |
630 | if (pos < 0) | |
631 | pos = -1 - pos; | |
632 | return pos; | |
633 | } | |
634 | ||
635 | /* | |
636 | * We call unpack_index_entry() with an unmerged cache entry | |
637 | * only in diff-index, and it wants a single callback. Skip | |
638 | * the other unmerged entry with the same name. | |
639 | */ | |
640 | static void mark_ce_used_same_name(struct cache_entry *ce, | |
641 | struct unpack_trees_options *o) | |
642 | { | |
643 | struct index_state *index = o->src_index; | |
644 | int len = ce_namelen(ce); | |
645 | int pos; | |
646 | ||
647 | for (pos = locate_in_src_index(ce, o); pos < index->cache_nr; pos++) { | |
648 | struct cache_entry *next = index->cache[pos]; | |
649 | if (len != ce_namelen(next) || | |
650 | memcmp(ce->name, next->name, len)) | |
651 | break; | |
652 | mark_ce_used(next, o); | |
653 | } | |
654 | } | |
655 | ||
656 | static struct cache_entry *next_cache_entry(struct unpack_trees_options *o) | |
657 | { | |
658 | const struct index_state *index = o->src_index; | |
659 | int pos = o->internal.cache_bottom; | |
660 | ||
661 | while (pos < index->cache_nr) { | |
662 | struct cache_entry *ce = index->cache[pos]; | |
663 | if (!(ce->ce_flags & CE_UNPACKED)) | |
664 | return ce; | |
665 | pos++; | |
666 | } | |
667 | return NULL; | |
668 | } | |
669 | ||
670 | static void add_same_unmerged(const struct cache_entry *ce, | |
671 | struct unpack_trees_options *o) | |
672 | { | |
673 | struct index_state *index = o->src_index; | |
674 | int len = ce_namelen(ce); | |
675 | int pos = index_name_pos(index, ce->name, len); | |
676 | ||
677 | if (0 <= pos) | |
678 | die("programming error in a caller of mark_ce_used_same_name"); | |
679 | for (pos = -pos - 1; pos < index->cache_nr; pos++) { | |
680 | struct cache_entry *next = index->cache[pos]; | |
681 | if (len != ce_namelen(next) || | |
682 | memcmp(ce->name, next->name, len)) | |
683 | break; | |
684 | add_entry(o, next, 0, 0); | |
685 | mark_ce_used(next, o); | |
686 | } | |
687 | } | |
688 | ||
689 | static int unpack_index_entry(struct cache_entry *ce, | |
690 | struct unpack_trees_options *o) | |
691 | { | |
692 | const struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, }; | |
693 | int ret; | |
694 | ||
695 | src[0] = ce; | |
696 | ||
697 | mark_ce_used(ce, o); | |
698 | if (ce_stage(ce)) { | |
699 | if (o->skip_unmerged) { | |
700 | add_entry(o, ce, 0, 0); | |
701 | return 0; | |
702 | } | |
703 | } | |
704 | ret = call_unpack_fn(src, o); | |
705 | if (ce_stage(ce)) | |
706 | mark_ce_used_same_name(ce, o); | |
707 | return ret; | |
708 | } | |
709 | ||
710 | static int find_cache_pos(struct traverse_info *, const char *p, size_t len); | |
711 | ||
712 | static void restore_cache_bottom(struct traverse_info *info, int bottom) | |
713 | { | |
714 | struct unpack_trees_options *o = info->data; | |
715 | ||
716 | if (o->diff_index_cached) | |
717 | return; | |
718 | o->internal.cache_bottom = bottom; | |
719 | } | |
720 | ||
721 | static int switch_cache_bottom(struct traverse_info *info) | |
722 | { | |
723 | struct unpack_trees_options *o = info->data; | |
724 | int ret, pos; | |
725 | ||
726 | if (o->diff_index_cached) | |
727 | return 0; | |
728 | ret = o->internal.cache_bottom; | |
729 | pos = find_cache_pos(info->prev, info->name, info->namelen); | |
730 | ||
731 | if (pos < -1) | |
732 | o->internal.cache_bottom = -2 - pos; | |
733 | else if (pos < 0) | |
734 | o->internal.cache_bottom = o->src_index->cache_nr; | |
735 | return ret; | |
736 | } | |
737 | ||
738 | static inline int are_same_oid(struct name_entry *name_j, struct name_entry *name_k) | |
739 | { | |
740 | return !is_null_oid(&name_j->oid) && !is_null_oid(&name_k->oid) && oideq(&name_j->oid, &name_k->oid); | |
741 | } | |
742 | ||
743 | static int all_trees_same_as_cache_tree(int n, unsigned long dirmask, | |
744 | struct name_entry *names, | |
745 | struct traverse_info *info) | |
746 | { | |
747 | struct unpack_trees_options *o = info->data; | |
748 | int i; | |
749 | ||
750 | if (!o->merge || dirmask != ((1 << n) - 1)) | |
751 | return 0; | |
752 | ||
753 | for (i = 1; i < n; i++) | |
754 | if (!are_same_oid(names, names + i)) | |
755 | return 0; | |
756 | ||
757 | return cache_tree_matches_traversal(o->src_index->cache_tree, names, info); | |
758 | } | |
759 | ||
760 | static int index_pos_by_traverse_info(struct name_entry *names, | |
761 | struct traverse_info *info) | |
762 | { | |
763 | struct unpack_trees_options *o = info->data; | |
764 | struct strbuf name = STRBUF_INIT; | |
765 | int pos; | |
766 | ||
767 | strbuf_make_traverse_path(&name, info, names->path, names->pathlen); | |
768 | strbuf_addch(&name, '/'); | |
769 | pos = index_name_pos(o->src_index, name.buf, name.len); | |
770 | if (pos >= 0) { | |
771 | if (!o->src_index->sparse_index || | |
772 | !(o->src_index->cache[pos]->ce_flags & CE_SKIP_WORKTREE)) | |
773 | BUG("This is a directory and should not exist in index"); | |
774 | } else { | |
775 | pos = -pos - 1; | |
776 | } | |
777 | if (pos >= o->src_index->cache_nr || | |
778 | !starts_with(o->src_index->cache[pos]->name, name.buf) || | |
779 | (pos > 0 && starts_with(o->src_index->cache[pos-1]->name, name.buf))) | |
780 | BUG("pos %d doesn't point to the first entry of %s in index", | |
781 | pos, name.buf); | |
782 | strbuf_release(&name); | |
783 | return pos; | |
784 | } | |
785 | ||
786 | /* | |
787 | * Fast path if we detect that all trees are the same as cache-tree at this | |
788 | * path. We'll walk these trees in an iterative loop using cache-tree/index | |
789 | * instead of ODB since we already know what these trees contain. | |
790 | */ | |
791 | static int traverse_by_cache_tree(int pos, int nr_entries, int nr_names, | |
792 | struct traverse_info *info) | |
793 | { | |
794 | struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, }; | |
795 | struct unpack_trees_options *o = info->data; | |
796 | struct cache_entry *tree_ce = NULL; | |
797 | int ce_len = 0; | |
798 | int i, d; | |
799 | ||
800 | if (!o->merge) | |
801 | BUG("We need cache-tree to do this optimization"); | |
802 | ||
803 | /* | |
804 | * Do what unpack_callback() and unpack_single_entry() normally | |
805 | * do. But we walk all paths in an iterative loop instead. | |
806 | * | |
807 | * D/F conflicts and higher stage entries are not a concern | |
808 | * because cache-tree would be invalidated and we would never | |
809 | * get here in the first place. | |
810 | */ | |
811 | for (i = 0; i < nr_entries; i++) { | |
812 | int new_ce_len, len, rc; | |
813 | ||
814 | src[0] = o->src_index->cache[pos + i]; | |
815 | ||
816 | len = ce_namelen(src[0]); | |
817 | new_ce_len = cache_entry_size(len); | |
818 | ||
819 | if (new_ce_len > ce_len) { | |
820 | new_ce_len <<= 1; | |
821 | tree_ce = xrealloc(tree_ce, new_ce_len); | |
822 | memset(tree_ce, 0, new_ce_len); | |
823 | ce_len = new_ce_len; | |
824 | ||
825 | tree_ce->ce_flags = create_ce_flags(0); | |
826 | ||
827 | for (d = 1; d <= nr_names; d++) | |
828 | src[d] = tree_ce; | |
829 | } | |
830 | ||
831 | tree_ce->ce_mode = src[0]->ce_mode; | |
832 | tree_ce->ce_namelen = len; | |
833 | oidcpy(&tree_ce->oid, &src[0]->oid); | |
834 | memcpy(tree_ce->name, src[0]->name, len + 1); | |
835 | ||
836 | rc = call_unpack_fn((const struct cache_entry * const *)src, o); | |
837 | if (rc < 0) { | |
838 | free(tree_ce); | |
839 | return rc; | |
840 | } | |
841 | ||
842 | mark_ce_used(src[0], o); | |
843 | } | |
844 | free(tree_ce); | |
845 | if (o->internal.debug_unpack) | |
846 | printf("Unpacked %d entries from %s to %s using cache-tree\n", | |
847 | nr_entries, | |
848 | o->src_index->cache[pos]->name, | |
849 | o->src_index->cache[pos + nr_entries - 1]->name); | |
850 | return 0; | |
851 | } | |
852 | ||
853 | static int traverse_trees_recursive(int n, unsigned long dirmask, | |
854 | unsigned long df_conflicts, | |
855 | struct name_entry *names, | |
856 | struct traverse_info *info) | |
857 | { | |
858 | struct unpack_trees_options *o = info->data; | |
859 | int i, ret, bottom; | |
860 | int nr_buf = 0; | |
861 | struct tree_desc t[MAX_UNPACK_TREES]; | |
862 | void *buf[MAX_UNPACK_TREES]; | |
863 | struct traverse_info newinfo; | |
864 | struct name_entry *p; | |
865 | int nr_entries; | |
866 | ||
867 | nr_entries = all_trees_same_as_cache_tree(n, dirmask, names, info); | |
868 | if (nr_entries > 0) { | |
869 | int pos = index_pos_by_traverse_info(names, info); | |
870 | ||
871 | if (!o->merge || df_conflicts) | |
872 | BUG("Wrong condition to get here buddy"); | |
873 | ||
874 | /* | |
875 | * All entries up to 'pos' must have been processed | |
876 | * (i.e. marked CE_UNPACKED) at this point. But to be safe, | |
877 | * save and restore cache_bottom anyway to not miss | |
878 | * unprocessed entries before 'pos'. | |
879 | */ | |
880 | bottom = o->internal.cache_bottom; | |
881 | ret = traverse_by_cache_tree(pos, nr_entries, n, info); | |
882 | o->internal.cache_bottom = bottom; | |
883 | return ret; | |
884 | } | |
885 | ||
886 | p = names; | |
887 | while (!p->mode) | |
888 | p++; | |
889 | ||
890 | newinfo = *info; | |
891 | newinfo.prev = info; | |
892 | newinfo.pathspec = info->pathspec; | |
893 | newinfo.name = p->path; | |
894 | newinfo.namelen = p->pathlen; | |
895 | newinfo.mode = p->mode; | |
896 | newinfo.pathlen = st_add3(newinfo.pathlen, tree_entry_len(p), 1); | |
897 | newinfo.df_conflicts |= df_conflicts; | |
898 | ||
899 | /* | |
900 | * Fetch the tree from the ODB for each peer directory in the | |
901 | * n commits. | |
902 | * | |
903 | * For 2- and 3-way traversals, we try to avoid hitting the | |
904 | * ODB twice for the same OID. This should yield a nice speed | |
905 | * up in checkouts and merges when the commits are similar. | |
906 | * | |
907 | * We don't bother doing the full O(n^2) search for larger n, | |
908 | * because wider traversals don't happen that often and we | |
909 | * avoid the search setup. | |
910 | * | |
911 | * When 2 peer OIDs are the same, we just copy the tree | |
912 | * descriptor data. This implicitly borrows the buffer | |
913 | * data from the earlier cell. | |
914 | */ | |
915 | for (i = 0; i < n; i++, dirmask >>= 1) { | |
916 | if (i > 0 && are_same_oid(&names[i], &names[i - 1])) | |
917 | t[i] = t[i - 1]; | |
918 | else if (i > 1 && are_same_oid(&names[i], &names[i - 2])) | |
919 | t[i] = t[i - 2]; | |
920 | else { | |
921 | const struct object_id *oid = NULL; | |
922 | if (dirmask & 1) | |
923 | oid = &names[i].oid; | |
924 | buf[nr_buf++] = fill_tree_descriptor(the_repository, t + i, oid); | |
925 | } | |
926 | } | |
927 | ||
928 | bottom = switch_cache_bottom(&newinfo); | |
929 | ret = traverse_trees(o->src_index, n, t, &newinfo); | |
930 | restore_cache_bottom(&newinfo, bottom); | |
931 | ||
932 | for (i = 0; i < nr_buf; i++) | |
933 | free(buf[i]); | |
934 | ||
935 | return ret; | |
936 | } | |
937 | ||
938 | /* | |
939 | * Compare the traverse-path to the cache entry without actually | |
940 | * having to generate the textual representation of the traverse | |
941 | * path. | |
942 | * | |
943 | * NOTE! This *only* compares up to the size of the traverse path | |
944 | * itself - the caller needs to do the final check for the cache | |
945 | * entry having more data at the end! | |
946 | */ | |
947 | static int do_compare_entry_piecewise(const struct cache_entry *ce, | |
948 | const struct traverse_info *info, | |
949 | const char *name, size_t namelen, | |
950 | unsigned mode) | |
951 | { | |
952 | int pathlen, ce_len; | |
953 | const char *ce_name; | |
954 | ||
955 | if (info->prev) { | |
956 | int cmp = do_compare_entry_piecewise(ce, info->prev, | |
957 | info->name, info->namelen, | |
958 | info->mode); | |
959 | if (cmp) | |
960 | return cmp; | |
961 | } | |
962 | pathlen = info->pathlen; | |
963 | ce_len = ce_namelen(ce); | |
964 | ||
965 | /* If ce_len < pathlen then we must have previously hit "name == directory" entry */ | |
966 | if (ce_len < pathlen) | |
967 | return -1; | |
968 | ||
969 | ce_len -= pathlen; | |
970 | ce_name = ce->name + pathlen; | |
971 | ||
972 | return df_name_compare(ce_name, ce_len, S_IFREG, name, namelen, mode); | |
973 | } | |
974 | ||
975 | static int do_compare_entry(const struct cache_entry *ce, | |
976 | const struct traverse_info *info, | |
977 | const char *name, size_t namelen, | |
978 | unsigned mode) | |
979 | { | |
980 | int pathlen, ce_len; | |
981 | const char *ce_name; | |
982 | int cmp; | |
983 | unsigned ce_mode; | |
984 | ||
985 | /* | |
986 | * If we have not precomputed the traverse path, it is quicker | |
987 | * to avoid doing so. But if we have precomputed it, | |
988 | * it is quicker to use the precomputed version. | |
989 | */ | |
990 | if (!info->traverse_path) | |
991 | return do_compare_entry_piecewise(ce, info, name, namelen, mode); | |
992 | ||
993 | cmp = strncmp(ce->name, info->traverse_path, info->pathlen); | |
994 | if (cmp) | |
995 | return cmp; | |
996 | ||
997 | pathlen = info->pathlen; | |
998 | ce_len = ce_namelen(ce); | |
999 | ||
1000 | if (ce_len < pathlen) | |
1001 | return -1; | |
1002 | ||
1003 | ce_len -= pathlen; | |
1004 | ce_name = ce->name + pathlen; | |
1005 | ||
1006 | ce_mode = S_ISSPARSEDIR(ce->ce_mode) ? S_IFDIR : S_IFREG; | |
1007 | return df_name_compare(ce_name, ce_len, ce_mode, name, namelen, mode); | |
1008 | } | |
1009 | ||
1010 | static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n) | |
1011 | { | |
1012 | int cmp = do_compare_entry(ce, info, n->path, n->pathlen, n->mode); | |
1013 | if (cmp) | |
1014 | return cmp; | |
1015 | ||
1016 | /* | |
1017 | * At this point, we know that we have a prefix match. If ce | |
1018 | * is a sparse directory, then allow an exact match. This only | |
1019 | * works when the input name is a directory, since ce->name | |
1020 | * ends in a directory separator. | |
1021 | */ | |
1022 | if (S_ISSPARSEDIR(ce->ce_mode) && | |
1023 | ce->ce_namelen == traverse_path_len(info, tree_entry_len(n)) + 1) | |
1024 | return 0; | |
1025 | ||
1026 | /* | |
1027 | * Even if the beginning compared identically, the ce should | |
1028 | * compare as bigger than a directory leading up to it! | |
1029 | */ | |
1030 | return ce_namelen(ce) > traverse_path_len(info, tree_entry_len(n)); | |
1031 | } | |
1032 | ||
1033 | static int ce_in_traverse_path(const struct cache_entry *ce, | |
1034 | const struct traverse_info *info) | |
1035 | { | |
1036 | if (!info->prev) | |
1037 | return 1; | |
1038 | if (do_compare_entry(ce, info->prev, | |
1039 | info->name, info->namelen, info->mode)) | |
1040 | return 0; | |
1041 | /* | |
1042 | * If ce (blob) is the same name as the path (which is a tree | |
1043 | * we will be descending into), it won't be inside it. | |
1044 | */ | |
1045 | return (info->pathlen < ce_namelen(ce)); | |
1046 | } | |
1047 | ||
1048 | static struct cache_entry *create_ce_entry(const struct traverse_info *info, | |
1049 | const struct name_entry *n, | |
1050 | int stage, | |
1051 | struct index_state *istate, | |
1052 | int is_transient, | |
1053 | int is_sparse_directory) | |
1054 | { | |
1055 | size_t len = traverse_path_len(info, tree_entry_len(n)); | |
1056 | size_t alloc_len = is_sparse_directory ? len + 1 : len; | |
1057 | struct cache_entry *ce = | |
1058 | is_transient ? | |
1059 | make_empty_transient_cache_entry(alloc_len, NULL) : | |
1060 | make_empty_cache_entry(istate, alloc_len); | |
1061 | ||
1062 | ce->ce_mode = create_ce_mode(n->mode); | |
1063 | ce->ce_flags = create_ce_flags(stage); | |
1064 | ce->ce_namelen = len; | |
1065 | oidcpy(&ce->oid, &n->oid); | |
1066 | /* len+1 because the cache_entry allocates space for NUL */ | |
1067 | make_traverse_path(ce->name, len + 1, info, n->path, n->pathlen); | |
1068 | ||
1069 | if (is_sparse_directory) { | |
1070 | ce->name[len] = '/'; | |
1071 | ce->name[len + 1] = '\0'; | |
1072 | ce->ce_namelen++; | |
1073 | ce->ce_flags |= CE_SKIP_WORKTREE; | |
1074 | } | |
1075 | ||
1076 | return ce; | |
1077 | } | |
1078 | ||
1079 | /* | |
1080 | * Determine whether the path specified by 'p' should be unpacked as a new | |
1081 | * sparse directory in a sparse index. A new sparse directory 'A/': | |
1082 | * - must be outside the sparse cone. | |
1083 | * - must not already be in the index (i.e., no index entry with name 'A/' | |
1084 | * exists). | |
1085 | * - must not have any child entries in the index (i.e., no index entry | |
1086 | * 'A/<something>' exists). | |
1087 | * If 'p' meets the above requirements, return 1; otherwise, return 0. | |
1088 | */ | |
1089 | static int entry_is_new_sparse_dir(const struct traverse_info *info, | |
1090 | const struct name_entry *p) | |
1091 | { | |
1092 | int res, pos; | |
1093 | struct strbuf dirpath = STRBUF_INIT; | |
1094 | struct unpack_trees_options *o = info->data; | |
1095 | ||
1096 | if (!S_ISDIR(p->mode)) | |
1097 | return 0; | |
1098 | ||
1099 | /* | |
1100 | * If the path is inside the sparse cone, it can't be a sparse directory. | |
1101 | */ | |
1102 | strbuf_add(&dirpath, info->traverse_path, info->pathlen); | |
1103 | strbuf_add(&dirpath, p->path, p->pathlen); | |
1104 | strbuf_addch(&dirpath, '/'); | |
1105 | if (path_in_cone_mode_sparse_checkout(dirpath.buf, o->src_index)) { | |
1106 | res = 0; | |
1107 | goto cleanup; | |
1108 | } | |
1109 | ||
1110 | pos = index_name_pos_sparse(o->src_index, dirpath.buf, dirpath.len); | |
1111 | if (pos >= 0) { | |
1112 | /* Path is already in the index, not a new sparse dir */ | |
1113 | res = 0; | |
1114 | goto cleanup; | |
1115 | } | |
1116 | ||
1117 | /* Where would this sparse dir be inserted into the index? */ | |
1118 | pos = -pos - 1; | |
1119 | if (pos >= o->src_index->cache_nr) { | |
1120 | /* | |
1121 | * Sparse dir would be inserted at the end of the index, so we | |
1122 | * know it has no child entries. | |
1123 | */ | |
1124 | res = 1; | |
1125 | goto cleanup; | |
1126 | } | |
1127 | ||
1128 | /* | |
1129 | * If the dir has child entries in the index, the first would be at the | |
1130 | * position the sparse directory would be inserted. If the entry at this | |
1131 | * position is inside the dir, not a new sparse dir. | |
1132 | */ | |
1133 | res = strncmp(o->src_index->cache[pos]->name, dirpath.buf, dirpath.len); | |
1134 | ||
1135 | cleanup: | |
1136 | strbuf_release(&dirpath); | |
1137 | return res; | |
1138 | } | |
1139 | ||
1140 | /* | |
1141 | * Note that traverse_by_cache_tree() duplicates some logic in this function | |
1142 | * without actually calling it. If you change the logic here you may need to | |
1143 | * check and change there as well. | |
1144 | */ | |
1145 | static int unpack_single_entry(int n, unsigned long mask, | |
1146 | unsigned long dirmask, | |
1147 | struct cache_entry **src, | |
1148 | const struct name_entry *names, | |
1149 | const struct traverse_info *info, | |
1150 | int *is_new_sparse_dir) | |
1151 | { | |
1152 | int i; | |
1153 | struct unpack_trees_options *o = info->data; | |
1154 | unsigned long conflicts = info->df_conflicts | dirmask; | |
1155 | const struct name_entry *p = names; | |
1156 | ||
1157 | *is_new_sparse_dir = 0; | |
1158 | if (mask == dirmask && !src[0]) { | |
1159 | /* | |
1160 | * If we're not in a sparse index, we can't unpack a directory | |
1161 | * without recursing into it, so we return. | |
1162 | */ | |
1163 | if (!o->src_index->sparse_index) | |
1164 | return 0; | |
1165 | ||
1166 | /* Find first entry with a real name (we could use "mask" too) */ | |
1167 | while (!p->mode) | |
1168 | p++; | |
1169 | ||
1170 | /* | |
1171 | * If the directory is completely missing from the index but | |
1172 | * would otherwise be a sparse directory, we should unpack it. | |
1173 | * If not, we'll return and continue recursively traversing the | |
1174 | * tree. | |
1175 | */ | |
1176 | *is_new_sparse_dir = entry_is_new_sparse_dir(info, p); | |
1177 | if (!*is_new_sparse_dir) | |
1178 | return 0; | |
1179 | } | |
1180 | ||
1181 | /* | |
1182 | * When we are unpacking a sparse directory, then this isn't necessarily | |
1183 | * a directory-file conflict. | |
1184 | */ | |
1185 | if (mask == dirmask && | |
1186 | (*is_new_sparse_dir || (src[0] && S_ISSPARSEDIR(src[0]->ce_mode)))) | |
1187 | conflicts = 0; | |
1188 | ||
1189 | /* | |
1190 | * Ok, we've filled in up to any potential index entry in src[0], | |
1191 | * now do the rest. | |
1192 | */ | |
1193 | for (i = 0; i < n; i++) { | |
1194 | int stage; | |
1195 | unsigned int bit = 1ul << i; | |
1196 | if (conflicts & bit) { | |
1197 | src[i + o->merge] = o->df_conflict_entry; | |
1198 | continue; | |
1199 | } | |
1200 | if (!(mask & bit)) | |
1201 | continue; | |
1202 | if (!o->merge) | |
1203 | stage = 0; | |
1204 | else if (i + 1 < o->head_idx) | |
1205 | stage = 1; | |
1206 | else if (i + 1 > o->head_idx) | |
1207 | stage = 3; | |
1208 | else | |
1209 | stage = 2; | |
1210 | ||
1211 | /* | |
1212 | * If the merge bit is set, then the cache entries are | |
1213 | * discarded in the following block. In this case, | |
1214 | * construct "transient" cache_entries, as they are | |
1215 | * not stored in the index. otherwise construct the | |
1216 | * cache entry from the index aware logic. | |
1217 | */ | |
1218 | src[i + o->merge] = create_ce_entry(info, names + i, stage, | |
1219 | &o->internal.result, | |
1220 | o->merge, bit & dirmask); | |
1221 | } | |
1222 | ||
1223 | if (o->merge) { | |
1224 | int rc = call_unpack_fn((const struct cache_entry * const *)src, | |
1225 | o); | |
1226 | for (i = 0; i < n; i++) { | |
1227 | struct cache_entry *ce = src[i + o->merge]; | |
1228 | if (ce != o->df_conflict_entry) | |
1229 | discard_cache_entry(ce); | |
1230 | } | |
1231 | return rc; | |
1232 | } | |
1233 | ||
1234 | for (i = 0; i < n; i++) | |
1235 | if (src[i] && src[i] != o->df_conflict_entry) | |
1236 | if (do_add_entry(o, src[i], 0, 0)) | |
1237 | return -1; | |
1238 | ||
1239 | return 0; | |
1240 | } | |
1241 | ||
1242 | static int unpack_failed(struct unpack_trees_options *o, const char *message) | |
1243 | { | |
1244 | discard_index(&o->internal.result); | |
1245 | if (!o->quiet && !o->exiting_early) { | |
1246 | if (message) | |
1247 | return error("%s", message); | |
1248 | return -1; | |
1249 | } | |
1250 | return -1; | |
1251 | } | |
1252 | ||
1253 | /* | |
1254 | * The tree traversal is looking at name p. If we have a matching entry, | |
1255 | * return it. If name p is a directory in the index, do not return | |
1256 | * anything, as we will want to match it when the traversal descends into | |
1257 | * the directory. | |
1258 | */ | |
1259 | static int find_cache_pos(struct traverse_info *info, | |
1260 | const char *p, size_t p_len) | |
1261 | { | |
1262 | int pos; | |
1263 | struct unpack_trees_options *o = info->data; | |
1264 | struct index_state *index = o->src_index; | |
1265 | int pfxlen = info->pathlen; | |
1266 | ||
1267 | for (pos = o->internal.cache_bottom; pos < index->cache_nr; pos++) { | |
1268 | const struct cache_entry *ce = index->cache[pos]; | |
1269 | const char *ce_name, *ce_slash; | |
1270 | int cmp, ce_len; | |
1271 | ||
1272 | if (ce->ce_flags & CE_UNPACKED) { | |
1273 | /* | |
1274 | * cache_bottom entry is already unpacked, so | |
1275 | * we can never match it; don't check it | |
1276 | * again. | |
1277 | */ | |
1278 | if (pos == o->internal.cache_bottom) | |
1279 | ++o->internal.cache_bottom; | |
1280 | continue; | |
1281 | } | |
1282 | if (!ce_in_traverse_path(ce, info)) { | |
1283 | /* | |
1284 | * Check if we can skip future cache checks | |
1285 | * (because we're already past all possible | |
1286 | * entries in the traverse path). | |
1287 | */ | |
1288 | if (info->traverse_path) { | |
1289 | if (strncmp(ce->name, info->traverse_path, | |
1290 | info->pathlen) > 0) | |
1291 | break; | |
1292 | } | |
1293 | continue; | |
1294 | } | |
1295 | ce_name = ce->name + pfxlen; | |
1296 | ce_slash = strchr(ce_name, '/'); | |
1297 | if (ce_slash) | |
1298 | ce_len = ce_slash - ce_name; | |
1299 | else | |
1300 | ce_len = ce_namelen(ce) - pfxlen; | |
1301 | cmp = name_compare(p, p_len, ce_name, ce_len); | |
1302 | /* | |
1303 | * Exact match; if we have a directory we need to | |
1304 | * delay returning it. | |
1305 | */ | |
1306 | if (!cmp) | |
1307 | return ce_slash ? -2 - pos : pos; | |
1308 | if (0 < cmp) | |
1309 | continue; /* keep looking */ | |
1310 | /* | |
1311 | * ce_name sorts after p->path; could it be that we | |
1312 | * have files under p->path directory in the index? | |
1313 | * E.g. ce_name == "t-i", and p->path == "t"; we may | |
1314 | * have "t/a" in the index. | |
1315 | */ | |
1316 | if (p_len < ce_len && !memcmp(ce_name, p, p_len) && | |
1317 | ce_name[p_len] < '/') | |
1318 | continue; /* keep looking */ | |
1319 | break; | |
1320 | } | |
1321 | return -1; | |
1322 | } | |
1323 | ||
1324 | /* | |
1325 | * Given a sparse directory entry 'ce', compare ce->name to | |
1326 | * info->traverse_path + p->path + '/' if info->traverse_path | |
1327 | * is non-empty. | |
1328 | * | |
1329 | * Compare ce->name to p->path + '/' otherwise. Note that | |
1330 | * ce->name must end in a trailing '/' because it is a sparse | |
1331 | * directory entry. | |
1332 | */ | |
1333 | static int sparse_dir_matches_path(const struct cache_entry *ce, | |
1334 | struct traverse_info *info, | |
1335 | const struct name_entry *p) | |
1336 | { | |
1337 | assert(S_ISSPARSEDIR(ce->ce_mode)); | |
1338 | assert(ce->name[ce->ce_namelen - 1] == '/'); | |
1339 | ||
1340 | if (info->pathlen) | |
1341 | return ce->ce_namelen == info->pathlen + p->pathlen + 1 && | |
1342 | ce->name[info->pathlen - 1] == '/' && | |
1343 | !strncmp(ce->name, info->traverse_path, info->pathlen) && | |
1344 | !strncmp(ce->name + info->pathlen, p->path, p->pathlen); | |
1345 | return ce->ce_namelen == p->pathlen + 1 && | |
1346 | !strncmp(ce->name, p->path, p->pathlen); | |
1347 | } | |
1348 | ||
1349 | static struct cache_entry *find_cache_entry(struct traverse_info *info, | |
1350 | const struct name_entry *p) | |
1351 | { | |
1352 | const char *path; | |
1353 | int pos = find_cache_pos(info, p->path, p->pathlen); | |
1354 | struct unpack_trees_options *o = info->data; | |
1355 | ||
1356 | if (0 <= pos) | |
1357 | return o->src_index->cache[pos]; | |
1358 | ||
1359 | /* | |
1360 | * Check for a sparse-directory entry named "path/". | |
1361 | * Due to the input p->path not having a trailing | |
1362 | * slash, the negative 'pos' value overshoots the | |
1363 | * expected position, hence "-2" instead of "-1". | |
1364 | */ | |
1365 | pos = -pos - 2; | |
1366 | ||
1367 | if (pos < 0 || pos >= o->src_index->cache_nr) | |
1368 | return NULL; | |
1369 | ||
1370 | /* | |
1371 | * Due to lexicographic sorting and sparse directory | |
1372 | * entries ending with a trailing slash, our path as a | |
1373 | * sparse directory (e.g "subdir/") and our path as a | |
1374 | * file (e.g. "subdir") might be separated by other | |
1375 | * paths (e.g. "subdir-"). | |
1376 | */ | |
1377 | while (pos >= 0) { | |
1378 | struct cache_entry *ce = o->src_index->cache[pos]; | |
1379 | ||
1380 | if (!skip_prefix(ce->name, info->traverse_path, &path) || | |
1381 | strncmp(path, p->path, p->pathlen) || | |
1382 | path[p->pathlen] != '/') | |
1383 | return NULL; | |
1384 | ||
1385 | if (S_ISSPARSEDIR(ce->ce_mode) && | |
1386 | sparse_dir_matches_path(ce, info, p)) | |
1387 | return ce; | |
1388 | ||
1389 | pos--; | |
1390 | } | |
1391 | ||
1392 | return NULL; | |
1393 | } | |
1394 | ||
1395 | static void debug_path(struct traverse_info *info) | |
1396 | { | |
1397 | if (info->prev) { | |
1398 | debug_path(info->prev); | |
1399 | if (*info->prev->name) | |
1400 | putchar('/'); | |
1401 | } | |
1402 | printf("%s", info->name); | |
1403 | } | |
1404 | ||
1405 | static void debug_name_entry(int i, struct name_entry *n) | |
1406 | { | |
1407 | printf("ent#%d %06o %s\n", i, | |
1408 | n->path ? n->mode : 0, | |
1409 | n->path ? n->path : "(missing)"); | |
1410 | } | |
1411 | ||
1412 | static void debug_unpack_callback(int n, | |
1413 | unsigned long mask, | |
1414 | unsigned long dirmask, | |
1415 | struct name_entry *names, | |
1416 | struct traverse_info *info) | |
1417 | { | |
1418 | int i; | |
1419 | printf("* unpack mask %lu, dirmask %lu, cnt %d ", | |
1420 | mask, dirmask, n); | |
1421 | debug_path(info); | |
1422 | putchar('\n'); | |
1423 | for (i = 0; i < n; i++) | |
1424 | debug_name_entry(i, names + i); | |
1425 | } | |
1426 | ||
1427 | /* | |
1428 | * Returns true if and only if the given cache_entry is a | |
1429 | * sparse-directory entry that matches the given name_entry | |
1430 | * from the tree walk at the given traverse_info. | |
1431 | */ | |
1432 | static int is_sparse_directory_entry(struct cache_entry *ce, | |
1433 | const struct name_entry *name, | |
1434 | struct traverse_info *info) | |
1435 | { | |
1436 | if (!ce || !name || !S_ISSPARSEDIR(ce->ce_mode)) | |
1437 | return 0; | |
1438 | ||
1439 | return sparse_dir_matches_path(ce, info, name); | |
1440 | } | |
1441 | ||
1442 | static int unpack_sparse_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info) | |
1443 | { | |
1444 | struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, }; | |
1445 | struct unpack_trees_options *o = info->data; | |
1446 | int ret, is_new_sparse_dir; | |
1447 | ||
1448 | assert(o->merge); | |
1449 | ||
1450 | /* | |
1451 | * Unlike in 'unpack_callback', where src[0] is derived from the index when | |
1452 | * merging, src[0] is a transient cache entry derived from the first tree | |
1453 | * provided. Create the temporary entry as if it came from a non-sparse index. | |
1454 | */ | |
1455 | if (!is_null_oid(&names[0].oid)) { | |
1456 | src[0] = create_ce_entry(info, &names[0], 0, | |
1457 | &o->internal.result, 1, | |
1458 | dirmask & (1ul << 0)); | |
1459 | src[0]->ce_flags |= (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE); | |
1460 | } | |
1461 | ||
1462 | /* | |
1463 | * 'unpack_single_entry' assumes that src[0] is derived directly from | |
1464 | * the index, rather than from an entry in 'names'. This is *not* true when | |
1465 | * merging a sparse directory, in which case names[0] is the "index" source | |
1466 | * entry. To match the expectations of 'unpack_single_entry', shift past the | |
1467 | * "index" tree (i.e., names[0]) and adjust 'names', 'n', 'mask', and | |
1468 | * 'dirmask' accordingly. | |
1469 | */ | |
1470 | ret = unpack_single_entry(n - 1, mask >> 1, dirmask >> 1, src, names + 1, info, &is_new_sparse_dir); | |
1471 | ||
1472 | if (src[0]) | |
1473 | discard_cache_entry(src[0]); | |
1474 | ||
1475 | return ret >= 0 ? mask : -1; | |
1476 | } | |
1477 | ||
1478 | /* | |
1479 | * Note that traverse_by_cache_tree() duplicates some logic in this function | |
1480 | * without actually calling it. If you change the logic here you may need to | |
1481 | * check and change there as well. | |
1482 | */ | |
1483 | static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info) | |
1484 | { | |
1485 | struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, }; | |
1486 | struct unpack_trees_options *o = info->data; | |
1487 | const struct name_entry *p = names; | |
1488 | int is_new_sparse_dir; | |
1489 | ||
1490 | /* Find first entry with a real name (we could use "mask" too) */ | |
1491 | while (!p->mode) | |
1492 | p++; | |
1493 | ||
1494 | if (o->internal.debug_unpack) | |
1495 | debug_unpack_callback(n, mask, dirmask, names, info); | |
1496 | ||
1497 | /* Are we supposed to look at the index too? */ | |
1498 | if (o->merge) { | |
1499 | while (1) { | |
1500 | int cmp; | |
1501 | struct cache_entry *ce; | |
1502 | ||
1503 | if (o->diff_index_cached) | |
1504 | ce = next_cache_entry(o); | |
1505 | else | |
1506 | ce = find_cache_entry(info, p); | |
1507 | ||
1508 | if (!ce) | |
1509 | break; | |
1510 | cmp = compare_entry(ce, info, p); | |
1511 | if (cmp < 0) { | |
1512 | if (unpack_index_entry(ce, o) < 0) | |
1513 | return unpack_failed(o, NULL); | |
1514 | continue; | |
1515 | } | |
1516 | if (!cmp) { | |
1517 | if (ce_stage(ce)) { | |
1518 | /* | |
1519 | * If we skip unmerged index | |
1520 | * entries, we'll skip this | |
1521 | * entry *and* the tree | |
1522 | * entries associated with it! | |
1523 | */ | |
1524 | if (o->skip_unmerged) { | |
1525 | add_same_unmerged(ce, o); | |
1526 | return mask; | |
1527 | } | |
1528 | } | |
1529 | src[0] = ce; | |
1530 | } | |
1531 | break; | |
1532 | } | |
1533 | } | |
1534 | ||
1535 | if (unpack_single_entry(n, mask, dirmask, src, names, info, &is_new_sparse_dir)) | |
1536 | return -1; | |
1537 | ||
1538 | if (o->merge && src[0]) { | |
1539 | if (ce_stage(src[0])) | |
1540 | mark_ce_used_same_name(src[0], o); | |
1541 | else | |
1542 | mark_ce_used(src[0], o); | |
1543 | } | |
1544 | ||
1545 | /* Now handle any directories.. */ | |
1546 | if (dirmask) { | |
1547 | /* special case: "diff-index --cached" looking at a tree */ | |
1548 | if (o->diff_index_cached && | |
1549 | n == 1 && dirmask == 1 && S_ISDIR(names->mode)) { | |
1550 | int matches; | |
1551 | matches = cache_tree_matches_traversal(o->src_index->cache_tree, | |
1552 | names, info); | |
1553 | /* | |
1554 | * Everything under the name matches; skip the | |
1555 | * entire hierarchy. diff_index_cached codepath | |
1556 | * special cases D/F conflicts in such a way that | |
1557 | * it does not do any look-ahead, so this is safe. | |
1558 | */ | |
1559 | if (matches) { | |
1560 | /* | |
1561 | * Only increment the cache_bottom if the | |
1562 | * directory isn't a sparse directory index | |
1563 | * entry (if it is, it was already incremented) | |
1564 | * in 'mark_ce_used()' | |
1565 | */ | |
1566 | if (!src[0] || !S_ISSPARSEDIR(src[0]->ce_mode)) | |
1567 | o->internal.cache_bottom += matches; | |
1568 | return mask; | |
1569 | } | |
1570 | } | |
1571 | ||
1572 | if (!is_sparse_directory_entry(src[0], p, info) && | |
1573 | !is_new_sparse_dir && | |
1574 | traverse_trees_recursive(n, dirmask, mask & ~dirmask, | |
1575 | names, info) < 0) { | |
1576 | return -1; | |
1577 | } | |
1578 | ||
1579 | return mask; | |
1580 | } | |
1581 | ||
1582 | return mask; | |
1583 | } | |
1584 | ||
1585 | static int clear_ce_flags_1(struct index_state *istate, | |
1586 | struct cache_entry **cache, int nr, | |
1587 | struct strbuf *prefix, | |
1588 | int select_mask, int clear_mask, | |
1589 | struct pattern_list *pl, | |
1590 | enum pattern_match_result default_match, | |
1591 | int progress_nr); | |
1592 | ||
1593 | /* Whole directory matching */ | |
1594 | static int clear_ce_flags_dir(struct index_state *istate, | |
1595 | struct cache_entry **cache, int nr, | |
1596 | struct strbuf *prefix, | |
1597 | char *basename, | |
1598 | int select_mask, int clear_mask, | |
1599 | struct pattern_list *pl, | |
1600 | enum pattern_match_result default_match, | |
1601 | int progress_nr) | |
1602 | { | |
1603 | struct cache_entry **cache_end; | |
1604 | int dtype = DT_DIR; | |
1605 | int rc; | |
1606 | enum pattern_match_result ret, orig_ret; | |
1607 | orig_ret = path_matches_pattern_list(prefix->buf, prefix->len, | |
1608 | basename, &dtype, pl, istate); | |
1609 | ||
1610 | strbuf_addch(prefix, '/'); | |
1611 | ||
1612 | /* If undecided, use matching result of parent dir in defval */ | |
1613 | if (orig_ret == UNDECIDED) | |
1614 | ret = default_match; | |
1615 | else | |
1616 | ret = orig_ret; | |
1617 | ||
1618 | for (cache_end = cache; cache_end != cache + nr; cache_end++) { | |
1619 | struct cache_entry *ce = *cache_end; | |
1620 | if (strncmp(ce->name, prefix->buf, prefix->len)) | |
1621 | break; | |
1622 | } | |
1623 | ||
1624 | if (pl->use_cone_patterns && orig_ret == MATCHED_RECURSIVE) { | |
1625 | struct cache_entry **ce = cache; | |
1626 | rc = cache_end - cache; | |
1627 | ||
1628 | while (ce < cache_end) { | |
1629 | (*ce)->ce_flags &= ~clear_mask; | |
1630 | ce++; | |
1631 | } | |
1632 | } else if (pl->use_cone_patterns && orig_ret == NOT_MATCHED) { | |
1633 | rc = cache_end - cache; | |
1634 | } else { | |
1635 | rc = clear_ce_flags_1(istate, cache, cache_end - cache, | |
1636 | prefix, | |
1637 | select_mask, clear_mask, | |
1638 | pl, ret, | |
1639 | progress_nr); | |
1640 | } | |
1641 | ||
1642 | strbuf_setlen(prefix, prefix->len - 1); | |
1643 | return rc; | |
1644 | } | |
1645 | ||
1646 | /* | |
1647 | * Traverse the index, find every entry that matches according to | |
1648 | * o->pl. Do "ce_flags &= ~clear_mask" on those entries. Return the | |
1649 | * number of traversed entries. | |
1650 | * | |
1651 | * If select_mask is non-zero, only entries whose ce_flags has on of | |
1652 | * those bits enabled are traversed. | |
1653 | * | |
1654 | * cache : pointer to an index entry | |
1655 | * prefix_len : an offset to its path | |
1656 | * | |
1657 | * The current path ("prefix") including the trailing '/' is | |
1658 | * cache[0]->name[0..(prefix_len-1)] | |
1659 | * Top level path has prefix_len zero. | |
1660 | */ | |
1661 | static int clear_ce_flags_1(struct index_state *istate, | |
1662 | struct cache_entry **cache, int nr, | |
1663 | struct strbuf *prefix, | |
1664 | int select_mask, int clear_mask, | |
1665 | struct pattern_list *pl, | |
1666 | enum pattern_match_result default_match, | |
1667 | int progress_nr) | |
1668 | { | |
1669 | struct cache_entry **cache_end = nr ? cache + nr : cache; | |
1670 | ||
1671 | /* | |
1672 | * Process all entries that have the given prefix and meet | |
1673 | * select_mask condition | |
1674 | */ | |
1675 | while(cache != cache_end) { | |
1676 | struct cache_entry *ce = *cache; | |
1677 | const char *name, *slash; | |
1678 | int len, dtype; | |
1679 | enum pattern_match_result ret; | |
1680 | ||
1681 | display_progress(istate->progress, progress_nr); | |
1682 | ||
1683 | if (select_mask && !(ce->ce_flags & select_mask)) { | |
1684 | cache++; | |
1685 | progress_nr++; | |
1686 | continue; | |
1687 | } | |
1688 | ||
1689 | if (prefix->len && strncmp(ce->name, prefix->buf, prefix->len)) | |
1690 | break; | |
1691 | ||
1692 | name = ce->name + prefix->len; | |
1693 | slash = strchr(name, '/'); | |
1694 | ||
1695 | /* If it's a directory, try whole directory match first */ | |
1696 | if (slash) { | |
1697 | int processed; | |
1698 | ||
1699 | len = slash - name; | |
1700 | strbuf_add(prefix, name, len); | |
1701 | ||
1702 | processed = clear_ce_flags_dir(istate, cache, cache_end - cache, | |
1703 | prefix, | |
1704 | prefix->buf + prefix->len - len, | |
1705 | select_mask, clear_mask, | |
1706 | pl, default_match, | |
1707 | progress_nr); | |
1708 | ||
1709 | /* clear_c_f_dir eats a whole dir already? */ | |
1710 | if (processed) { | |
1711 | cache += processed; | |
1712 | progress_nr += processed; | |
1713 | strbuf_setlen(prefix, prefix->len - len); | |
1714 | continue; | |
1715 | } | |
1716 | ||
1717 | strbuf_addch(prefix, '/'); | |
1718 | processed = clear_ce_flags_1(istate, cache, cache_end - cache, | |
1719 | prefix, | |
1720 | select_mask, clear_mask, pl, | |
1721 | default_match, progress_nr); | |
1722 | ||
1723 | cache += processed; | |
1724 | progress_nr += processed; | |
1725 | ||
1726 | strbuf_setlen(prefix, prefix->len - len - 1); | |
1727 | continue; | |
1728 | } | |
1729 | ||
1730 | /* Non-directory */ | |
1731 | dtype = ce_to_dtype(ce); | |
1732 | ret = path_matches_pattern_list(ce->name, | |
1733 | ce_namelen(ce), | |
1734 | name, &dtype, pl, istate); | |
1735 | if (ret == UNDECIDED) | |
1736 | ret = default_match; | |
1737 | if (ret == MATCHED || ret == MATCHED_RECURSIVE) | |
1738 | ce->ce_flags &= ~clear_mask; | |
1739 | cache++; | |
1740 | progress_nr++; | |
1741 | } | |
1742 | ||
1743 | display_progress(istate->progress, progress_nr); | |
1744 | return nr - (cache_end - cache); | |
1745 | } | |
1746 | ||
1747 | static int clear_ce_flags(struct index_state *istate, | |
1748 | int select_mask, int clear_mask, | |
1749 | struct pattern_list *pl, | |
1750 | int show_progress) | |
1751 | { | |
1752 | static struct strbuf prefix = STRBUF_INIT; | |
1753 | char label[100]; | |
1754 | int rval; | |
1755 | ||
1756 | strbuf_reset(&prefix); | |
1757 | if (show_progress) | |
1758 | istate->progress = start_delayed_progress( | |
1759 | _("Updating index flags"), | |
1760 | istate->cache_nr); | |
1761 | ||
1762 | xsnprintf(label, sizeof(label), "clear_ce_flags(0x%08lx,0x%08lx)", | |
1763 | (unsigned long)select_mask, (unsigned long)clear_mask); | |
1764 | trace2_region_enter("unpack_trees", label, the_repository); | |
1765 | rval = clear_ce_flags_1(istate, | |
1766 | istate->cache, | |
1767 | istate->cache_nr, | |
1768 | &prefix, | |
1769 | select_mask, clear_mask, | |
1770 | pl, 0, 0); | |
1771 | trace2_region_leave("unpack_trees", label, the_repository); | |
1772 | ||
1773 | stop_progress(&istate->progress); | |
1774 | return rval; | |
1775 | } | |
1776 | ||
1777 | /* | |
1778 | * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout | |
1779 | */ | |
1780 | static void mark_new_skip_worktree(struct pattern_list *pl, | |
1781 | struct index_state *istate, | |
1782 | int select_flag, int skip_wt_flag, | |
1783 | int show_progress) | |
1784 | { | |
1785 | int i; | |
1786 | ||
1787 | /* | |
1788 | * 1. Pretend the narrowest worktree: only unmerged entries | |
1789 | * are checked out | |
1790 | */ | |
1791 | for (i = 0; i < istate->cache_nr; i++) { | |
1792 | struct cache_entry *ce = istate->cache[i]; | |
1793 | ||
1794 | if (select_flag && !(ce->ce_flags & select_flag)) | |
1795 | continue; | |
1796 | ||
1797 | if (!ce_stage(ce) && !(ce->ce_flags & CE_CONFLICTED)) | |
1798 | ce->ce_flags |= skip_wt_flag; | |
1799 | else | |
1800 | ce->ce_flags &= ~skip_wt_flag; | |
1801 | } | |
1802 | ||
1803 | /* | |
1804 | * 2. Widen worktree according to sparse-checkout file. | |
1805 | * Matched entries will have skip_wt_flag cleared (i.e. "in") | |
1806 | */ | |
1807 | clear_ce_flags(istate, select_flag, skip_wt_flag, pl, show_progress); | |
1808 | } | |
1809 | ||
1810 | static void populate_from_existing_patterns(struct unpack_trees_options *o, | |
1811 | struct pattern_list *pl) | |
1812 | { | |
1813 | if (get_sparse_checkout_patterns(pl) < 0) | |
1814 | o->skip_sparse_checkout = 1; | |
1815 | else | |
1816 | o->internal.pl = pl; | |
1817 | } | |
1818 | ||
1819 | static void update_sparsity_for_prefix(const char *prefix, | |
1820 | struct index_state *istate) | |
1821 | { | |
1822 | int prefix_len = strlen(prefix); | |
1823 | struct strbuf ce_prefix = STRBUF_INIT; | |
1824 | ||
1825 | if (!istate->sparse_index) | |
1826 | return; | |
1827 | ||
1828 | while (prefix_len > 0 && prefix[prefix_len - 1] == '/') | |
1829 | prefix_len--; | |
1830 | ||
1831 | if (prefix_len <= 0) | |
1832 | BUG("Invalid prefix passed to update_sparsity_for_prefix"); | |
1833 | ||
1834 | strbuf_grow(&ce_prefix, prefix_len + 1); | |
1835 | strbuf_add(&ce_prefix, prefix, prefix_len); | |
1836 | strbuf_addch(&ce_prefix, '/'); | |
1837 | ||
1838 | /* | |
1839 | * If the prefix points to a sparse directory or a path inside a sparse | |
1840 | * directory, the index should be expanded. This is accomplished in one | |
1841 | * of two ways: | |
1842 | * - if the prefix is inside a sparse directory, it will be expanded by | |
1843 | * the 'ensure_full_index(...)' call in 'index_name_pos(...)'. | |
1844 | * - if the prefix matches an existing sparse directory entry, | |
1845 | * 'index_name_pos(...)' will return its index position, triggering | |
1846 | * the 'ensure_full_index(...)' below. | |
1847 | */ | |
1848 | if (!path_in_cone_mode_sparse_checkout(ce_prefix.buf, istate) && | |
1849 | index_name_pos(istate, ce_prefix.buf, ce_prefix.len) >= 0) | |
1850 | ensure_full_index(istate); | |
1851 | ||
1852 | strbuf_release(&ce_prefix); | |
1853 | } | |
1854 | ||
1855 | static int verify_absent(const struct cache_entry *, | |
1856 | enum unpack_trees_error_types, | |
1857 | struct unpack_trees_options *); | |
1858 | /* | |
1859 | * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the | |
1860 | * resulting index, -2 on failure to reflect the changes to the work tree. | |
1861 | * | |
1862 | * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally | |
1863 | */ | |
1864 | int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o) | |
1865 | { | |
1866 | struct repository *repo = the_repository; | |
1867 | int i, ret; | |
1868 | static struct cache_entry *dfc; | |
1869 | struct pattern_list pl; | |
1870 | int free_pattern_list = 0; | |
1871 | struct dir_struct dir = DIR_INIT; | |
1872 | ||
1873 | if (o->reset == UNPACK_RESET_INVALID) | |
1874 | BUG("o->reset had a value of 1; should be UNPACK_TREES_*_UNTRACKED"); | |
1875 | ||
1876 | if (len > MAX_UNPACK_TREES) | |
1877 | die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES); | |
1878 | if (o->internal.dir) | |
1879 | BUG("o->internal.dir is for internal use only"); | |
1880 | if (o->internal.pl) | |
1881 | BUG("o->internal.pl is for internal use only"); | |
1882 | if (o->df_conflict_entry) | |
1883 | BUG("o->df_conflict_entry is an output only field"); | |
1884 | ||
1885 | trace_performance_enter(); | |
1886 | trace2_region_enter("unpack_trees", "unpack_trees", the_repository); | |
1887 | ||
1888 | prepare_repo_settings(repo); | |
1889 | if (repo->settings.command_requires_full_index) { | |
1890 | ensure_full_index(o->src_index); | |
1891 | if (o->dst_index) | |
1892 | ensure_full_index(o->dst_index); | |
1893 | } | |
1894 | ||
1895 | if (o->reset == UNPACK_RESET_OVERWRITE_UNTRACKED && | |
1896 | o->preserve_ignored) | |
1897 | BUG("UNPACK_RESET_OVERWRITE_UNTRACKED incompatible with preserved ignored files"); | |
1898 | ||
1899 | if (!o->preserve_ignored) { | |
1900 | o->internal.dir = &dir; | |
1901 | o->internal.dir->flags |= DIR_SHOW_IGNORED; | |
1902 | setup_standard_excludes(o->internal.dir); | |
1903 | } | |
1904 | ||
1905 | if (o->prefix) | |
1906 | update_sparsity_for_prefix(o->prefix, o->src_index); | |
1907 | ||
1908 | if (!core_apply_sparse_checkout || !o->update) | |
1909 | o->skip_sparse_checkout = 1; | |
1910 | if (!o->skip_sparse_checkout) { | |
1911 | memset(&pl, 0, sizeof(pl)); | |
1912 | free_pattern_list = 1; | |
1913 | populate_from_existing_patterns(o, &pl); | |
1914 | } | |
1915 | ||
1916 | index_state_init(&o->internal.result, o->src_index->repo); | |
1917 | o->internal.result.initialized = 1; | |
1918 | o->internal.result.timestamp.sec = o->src_index->timestamp.sec; | |
1919 | o->internal.result.timestamp.nsec = o->src_index->timestamp.nsec; | |
1920 | o->internal.result.version = o->src_index->version; | |
1921 | if (!o->src_index->split_index) { | |
1922 | o->internal.result.split_index = NULL; | |
1923 | } else if (o->src_index == o->dst_index) { | |
1924 | /* | |
1925 | * o->dst_index (and thus o->src_index) will be discarded | |
1926 | * and overwritten with o->internal.result at the end of | |
1927 | * this function, so just use src_index's split_index to | |
1928 | * avoid having to create a new one. | |
1929 | */ | |
1930 | o->internal.result.split_index = o->src_index->split_index; | |
1931 | o->internal.result.split_index->refcount++; | |
1932 | } else { | |
1933 | o->internal.result.split_index = | |
1934 | init_split_index(&o->internal.result); | |
1935 | } | |
1936 | oidcpy(&o->internal.result.oid, &o->src_index->oid); | |
1937 | o->internal.merge_size = len; | |
1938 | mark_all_ce_unused(o->src_index); | |
1939 | ||
1940 | o->internal.result.fsmonitor_last_update = | |
1941 | xstrdup_or_null(o->src_index->fsmonitor_last_update); | |
1942 | o->internal.result.fsmonitor_has_run_once = o->src_index->fsmonitor_has_run_once; | |
1943 | ||
1944 | if (!o->src_index->initialized && | |
1945 | !repo->settings.command_requires_full_index && | |
1946 | is_sparse_index_allowed(&o->internal.result, 0)) | |
1947 | o->internal.result.sparse_index = 1; | |
1948 | ||
1949 | /* | |
1950 | * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries | |
1951 | */ | |
1952 | if (!o->skip_sparse_checkout) | |
1953 | mark_new_skip_worktree(o->internal.pl, o->src_index, 0, | |
1954 | CE_NEW_SKIP_WORKTREE, o->verbose_update); | |
1955 | ||
1956 | if (!dfc) | |
1957 | dfc = xcalloc(1, cache_entry_size(0)); | |
1958 | o->df_conflict_entry = dfc; | |
1959 | ||
1960 | if (len) { | |
1961 | const char *prefix = o->prefix ? o->prefix : ""; | |
1962 | struct traverse_info info; | |
1963 | ||
1964 | setup_traverse_info(&info, prefix); | |
1965 | info.fn = unpack_callback; | |
1966 | info.data = o; | |
1967 | info.show_all_errors = o->internal.show_all_errors; | |
1968 | info.pathspec = o->pathspec; | |
1969 | ||
1970 | if (o->prefix) { | |
1971 | /* | |
1972 | * Unpack existing index entries that sort before the | |
1973 | * prefix the tree is spliced into. Note that o->merge | |
1974 | * is always true in this case. | |
1975 | */ | |
1976 | while (1) { | |
1977 | struct cache_entry *ce = next_cache_entry(o); | |
1978 | if (!ce) | |
1979 | break; | |
1980 | if (ce_in_traverse_path(ce, &info)) | |
1981 | break; | |
1982 | if (unpack_index_entry(ce, o) < 0) | |
1983 | goto return_failed; | |
1984 | } | |
1985 | } | |
1986 | ||
1987 | trace_performance_enter(); | |
1988 | trace2_region_enter("unpack_trees", "traverse_trees", the_repository); | |
1989 | ret = traverse_trees(o->src_index, len, t, &info); | |
1990 | trace2_region_leave("unpack_trees", "traverse_trees", the_repository); | |
1991 | trace_performance_leave("traverse_trees"); | |
1992 | if (ret < 0) | |
1993 | goto return_failed; | |
1994 | } | |
1995 | ||
1996 | /* Any left-over entries in the index? */ | |
1997 | if (o->merge) { | |
1998 | while (1) { | |
1999 | struct cache_entry *ce = next_cache_entry(o); | |
2000 | if (!ce) | |
2001 | break; | |
2002 | if (unpack_index_entry(ce, o) < 0) | |
2003 | goto return_failed; | |
2004 | } | |
2005 | } | |
2006 | mark_all_ce_unused(o->src_index); | |
2007 | ||
2008 | if (o->trivial_merges_only && o->internal.nontrivial_merge) { | |
2009 | ret = unpack_failed(o, "Merge requires file-level merging"); | |
2010 | goto done; | |
2011 | } | |
2012 | ||
2013 | if (!o->skip_sparse_checkout) { | |
2014 | /* | |
2015 | * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #1 | |
2016 | * If they will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE | |
2017 | * so apply_sparse_checkout() won't attempt to remove it from worktree | |
2018 | */ | |
2019 | mark_new_skip_worktree(o->internal.pl, &o->internal.result, | |
2020 | CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE, | |
2021 | o->verbose_update); | |
2022 | ||
2023 | ret = 0; | |
2024 | for (i = 0; i < o->internal.result.cache_nr; i++) { | |
2025 | struct cache_entry *ce = o->internal.result.cache[i]; | |
2026 | ||
2027 | /* | |
2028 | * Entries marked with CE_ADDED in merged_entry() do not have | |
2029 | * verify_absent() check (the check is effectively disabled | |
2030 | * because CE_NEW_SKIP_WORKTREE is set unconditionally). | |
2031 | * | |
2032 | * Do the real check now because we have had | |
2033 | * correct CE_NEW_SKIP_WORKTREE | |
2034 | */ | |
2035 | if (ce->ce_flags & CE_ADDED && | |
2036 | verify_absent(ce, WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN, o)) | |
2037 | ret = 1; | |
2038 | ||
2039 | if (apply_sparse_checkout(&o->internal.result, ce, o)) | |
2040 | ret = 1; | |
2041 | } | |
2042 | if (ret == 1) { | |
2043 | /* | |
2044 | * Inability to sparsify or de-sparsify individual | |
2045 | * paths is not an error, but just a warning. | |
2046 | */ | |
2047 | if (o->internal.show_all_errors) | |
2048 | display_warning_msgs(o); | |
2049 | ret = 0; | |
2050 | } | |
2051 | } | |
2052 | ||
2053 | ret = check_updates(o, &o->internal.result) ? (-2) : 0; | |
2054 | if (o->dst_index) { | |
2055 | move_index_extensions(&o->internal.result, o->src_index); | |
2056 | if (!ret) { | |
2057 | if (git_env_bool("GIT_TEST_CHECK_CACHE_TREE", 0)) | |
2058 | cache_tree_verify(the_repository, | |
2059 | &o->internal.result); | |
2060 | if (!o->skip_cache_tree_update && | |
2061 | !cache_tree_fully_valid(o->internal.result.cache_tree)) | |
2062 | cache_tree_update(&o->internal.result, | |
2063 | WRITE_TREE_SILENT | | |
2064 | WRITE_TREE_REPAIR); | |
2065 | } | |
2066 | ||
2067 | o->internal.result.updated_workdir = 1; | |
2068 | discard_index(o->dst_index); | |
2069 | *o->dst_index = o->internal.result; | |
2070 | } else { | |
2071 | discard_index(&o->internal.result); | |
2072 | } | |
2073 | o->src_index = NULL; | |
2074 | ||
2075 | done: | |
2076 | if (free_pattern_list) | |
2077 | clear_pattern_list(&pl); | |
2078 | if (o->internal.dir) { | |
2079 | dir_clear(o->internal.dir); | |
2080 | o->internal.dir = NULL; | |
2081 | } | |
2082 | trace2_region_leave("unpack_trees", "unpack_trees", the_repository); | |
2083 | trace_performance_leave("unpack_trees"); | |
2084 | return ret; | |
2085 | ||
2086 | return_failed: | |
2087 | if (o->internal.show_all_errors) | |
2088 | display_error_msgs(o); | |
2089 | mark_all_ce_unused(o->src_index); | |
2090 | ret = unpack_failed(o, NULL); | |
2091 | if (o->exiting_early) | |
2092 | ret = 0; | |
2093 | goto done; | |
2094 | } | |
2095 | ||
2096 | /* | |
2097 | * Update SKIP_WORKTREE bits according to sparsity patterns, and update | |
2098 | * working directory to match. | |
2099 | * | |
2100 | * CE_NEW_SKIP_WORKTREE is used internally. | |
2101 | */ | |
2102 | enum update_sparsity_result update_sparsity(struct unpack_trees_options *o, | |
2103 | struct pattern_list *pl) | |
2104 | { | |
2105 | enum update_sparsity_result ret = UPDATE_SPARSITY_SUCCESS; | |
2106 | int i; | |
2107 | unsigned old_show_all_errors; | |
2108 | int free_pattern_list = 0; | |
2109 | ||
2110 | old_show_all_errors = o->internal.show_all_errors; | |
2111 | o->internal.show_all_errors = 1; | |
2112 | index_state_init(&o->internal.result, o->src_index->repo); | |
2113 | ||
2114 | /* Sanity checks */ | |
2115 | if (!o->update || o->index_only || o->skip_sparse_checkout) | |
2116 | BUG("update_sparsity() is for reflecting sparsity patterns in working directory"); | |
2117 | if (o->src_index != o->dst_index || o->fn) | |
2118 | BUG("update_sparsity() called wrong"); | |
2119 | ||
2120 | trace_performance_enter(); | |
2121 | ||
2122 | /* If we weren't given patterns, use the recorded ones */ | |
2123 | if (!pl) { | |
2124 | free_pattern_list = 1; | |
2125 | pl = xcalloc(1, sizeof(*pl)); | |
2126 | populate_from_existing_patterns(o, pl); | |
2127 | } | |
2128 | o->internal.pl = pl; | |
2129 | ||
2130 | /* Expand sparse directories as needed */ | |
2131 | expand_index(o->src_index, o->internal.pl); | |
2132 | ||
2133 | /* Set NEW_SKIP_WORKTREE on existing entries. */ | |
2134 | mark_all_ce_unused(o->src_index); | |
2135 | mark_new_skip_worktree(o->internal.pl, o->src_index, 0, | |
2136 | CE_NEW_SKIP_WORKTREE, o->verbose_update); | |
2137 | ||
2138 | /* Then loop over entries and update/remove as needed */ | |
2139 | ret = UPDATE_SPARSITY_SUCCESS; | |
2140 | for (i = 0; i < o->src_index->cache_nr; i++) { | |
2141 | struct cache_entry *ce = o->src_index->cache[i]; | |
2142 | ||
2143 | ||
2144 | if (ce_stage(ce)) { | |
2145 | /* -1 because for loop will increment by 1 */ | |
2146 | i += warn_conflicted_path(o->src_index, i, o) - 1; | |
2147 | ret = UPDATE_SPARSITY_WARNINGS; | |
2148 | continue; | |
2149 | } | |
2150 | ||
2151 | if (apply_sparse_checkout(o->src_index, ce, o)) | |
2152 | ret = UPDATE_SPARSITY_WARNINGS; | |
2153 | } | |
2154 | ||
2155 | if (check_updates(o, o->src_index)) | |
2156 | ret = UPDATE_SPARSITY_WORKTREE_UPDATE_FAILURES; | |
2157 | ||
2158 | display_warning_msgs(o); | |
2159 | o->internal.show_all_errors = old_show_all_errors; | |
2160 | if (free_pattern_list) { | |
2161 | clear_pattern_list(pl); | |
2162 | free(pl); | |
2163 | o->internal.pl = NULL; | |
2164 | } | |
2165 | trace_performance_leave("update_sparsity"); | |
2166 | return ret; | |
2167 | } | |
2168 | ||
2169 | /* Here come the merge functions */ | |
2170 | ||
2171 | static int reject_merge(const struct cache_entry *ce, | |
2172 | struct unpack_trees_options *o) | |
2173 | { | |
2174 | return add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name); | |
2175 | } | |
2176 | ||
2177 | static int same(const struct cache_entry *a, const struct cache_entry *b) | |
2178 | { | |
2179 | if (!!a != !!b) | |
2180 | return 0; | |
2181 | if (!a && !b) | |
2182 | return 1; | |
2183 | if ((a->ce_flags | b->ce_flags) & CE_CONFLICTED) | |
2184 | return 0; | |
2185 | return a->ce_mode == b->ce_mode && | |
2186 | oideq(&a->oid, &b->oid); | |
2187 | } | |
2188 | ||
2189 | ||
2190 | /* | |
2191 | * When a CE gets turned into an unmerged entry, we | |
2192 | * want it to be up-to-date | |
2193 | */ | |
2194 | static int verify_uptodate_1(const struct cache_entry *ce, | |
2195 | struct unpack_trees_options *o, | |
2196 | enum unpack_trees_error_types error_type) | |
2197 | { | |
2198 | struct stat st; | |
2199 | ||
2200 | if (o->index_only) | |
2201 | return 0; | |
2202 | ||
2203 | /* | |
2204 | * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again | |
2205 | * if this entry is truly up-to-date because this file may be | |
2206 | * overwritten. | |
2207 | */ | |
2208 | if ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce)) | |
2209 | ; /* keep checking */ | |
2210 | else if (o->reset || ce_uptodate(ce)) | |
2211 | return 0; | |
2212 | ||
2213 | if (!lstat(ce->name, &st)) { | |
2214 | int flags = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE; | |
2215 | unsigned changed = ie_match_stat(o->src_index, ce, &st, flags); | |
2216 | ||
2217 | if (submodule_from_ce(ce)) { | |
2218 | int r = check_submodule_move_head(ce, | |
2219 | "HEAD", oid_to_hex(&ce->oid), o); | |
2220 | if (r) | |
2221 | return add_rejected_path(o, error_type, ce->name); | |
2222 | return 0; | |
2223 | } | |
2224 | ||
2225 | if (!changed) | |
2226 | return 0; | |
2227 | /* | |
2228 | * Historic default policy was to allow submodule to be out | |
2229 | * of sync wrt the superproject index. If the submodule was | |
2230 | * not considered interesting above, we don't care here. | |
2231 | */ | |
2232 | if (S_ISGITLINK(ce->ce_mode)) | |
2233 | return 0; | |
2234 | ||
2235 | errno = 0; | |
2236 | } | |
2237 | if (errno == ENOENT) | |
2238 | return 0; | |
2239 | return add_rejected_path(o, error_type, ce->name); | |
2240 | } | |
2241 | ||
2242 | int verify_uptodate(const struct cache_entry *ce, | |
2243 | struct unpack_trees_options *o) | |
2244 | { | |
2245 | if (!o->skip_sparse_checkout && | |
2246 | (ce->ce_flags & CE_SKIP_WORKTREE) && | |
2247 | (ce->ce_flags & CE_NEW_SKIP_WORKTREE)) | |
2248 | return 0; | |
2249 | return verify_uptodate_1(ce, o, ERROR_NOT_UPTODATE_FILE); | |
2250 | } | |
2251 | ||
2252 | static int verify_uptodate_sparse(const struct cache_entry *ce, | |
2253 | struct unpack_trees_options *o) | |
2254 | { | |
2255 | return verify_uptodate_1(ce, o, WARNING_SPARSE_NOT_UPTODATE_FILE); | |
2256 | } | |
2257 | ||
2258 | /* | |
2259 | * TODO: We should actually invalidate o->internal.result, not src_index [1]. | |
2260 | * But since cache tree and untracked cache both are not copied to | |
2261 | * o->internal.result until unpacking is complete, we invalidate them on | |
2262 | * src_index instead with the assumption that they will be copied to | |
2263 | * dst_index at the end. | |
2264 | * | |
2265 | * [1] src_index->cache_tree is also used in unpack_callback() so if | |
2266 | * we invalidate o->internal.result, we need to update it to use | |
2267 | * o->internal.result.cache_tree as well. | |
2268 | */ | |
2269 | static void invalidate_ce_path(const struct cache_entry *ce, | |
2270 | struct unpack_trees_options *o) | |
2271 | { | |
2272 | if (!ce) | |
2273 | return; | |
2274 | cache_tree_invalidate_path(o->src_index, ce->name); | |
2275 | untracked_cache_invalidate_path(o->src_index, ce->name, 1); | |
2276 | } | |
2277 | ||
2278 | /* | |
2279 | * Check that checking out ce->sha1 in subdir ce->name is not | |
2280 | * going to overwrite any working files. | |
2281 | */ | |
2282 | static int verify_clean_submodule(const char *old_sha1, | |
2283 | const struct cache_entry *ce, | |
2284 | struct unpack_trees_options *o) | |
2285 | { | |
2286 | if (!submodule_from_ce(ce)) | |
2287 | return 0; | |
2288 | ||
2289 | return check_submodule_move_head(ce, old_sha1, | |
2290 | oid_to_hex(&ce->oid), o); | |
2291 | } | |
2292 | ||
2293 | static int verify_clean_subdirectory(const struct cache_entry *ce, | |
2294 | struct unpack_trees_options *o) | |
2295 | { | |
2296 | /* | |
2297 | * we are about to extract "ce->name"; we would not want to lose | |
2298 | * anything in the existing directory there. | |
2299 | */ | |
2300 | int namelen; | |
2301 | int i; | |
2302 | struct dir_struct d; | |
2303 | char *pathbuf; | |
2304 | int cnt = 0; | |
2305 | ||
2306 | if (S_ISGITLINK(ce->ce_mode)) { | |
2307 | struct object_id oid; | |
2308 | int sub_head = resolve_gitlink_ref(ce->name, "HEAD", &oid); | |
2309 | /* | |
2310 | * If we are not going to update the submodule, then | |
2311 | * we don't care. | |
2312 | */ | |
2313 | if (!sub_head && oideq(&oid, &ce->oid)) | |
2314 | return 0; | |
2315 | return verify_clean_submodule(sub_head ? NULL : oid_to_hex(&oid), | |
2316 | ce, o); | |
2317 | } | |
2318 | ||
2319 | /* | |
2320 | * First let's make sure we do not have a local modification | |
2321 | * in that directory. | |
2322 | */ | |
2323 | namelen = ce_namelen(ce); | |
2324 | for (i = locate_in_src_index(ce, o); | |
2325 | i < o->src_index->cache_nr; | |
2326 | i++) { | |
2327 | struct cache_entry *ce2 = o->src_index->cache[i]; | |
2328 | int len = ce_namelen(ce2); | |
2329 | if (len < namelen || | |
2330 | strncmp(ce->name, ce2->name, namelen) || | |
2331 | ce2->name[namelen] != '/') | |
2332 | break; | |
2333 | /* | |
2334 | * ce2->name is an entry in the subdirectory to be | |
2335 | * removed. | |
2336 | */ | |
2337 | if (!ce_stage(ce2)) { | |
2338 | if (verify_uptodate(ce2, o)) | |
2339 | return -1; | |
2340 | add_entry(o, ce2, CE_REMOVE, 0); | |
2341 | invalidate_ce_path(ce, o); | |
2342 | mark_ce_used(ce2, o); | |
2343 | } | |
2344 | cnt++; | |
2345 | } | |
2346 | ||
2347 | /* Do not lose a locally present file that is not ignored. */ | |
2348 | pathbuf = xstrfmt("%.*s/", namelen, ce->name); | |
2349 | ||
2350 | memset(&d, 0, sizeof(d)); | |
2351 | if (o->internal.dir) | |
2352 | setup_standard_excludes(&d); | |
2353 | i = read_directory(&d, o->src_index, pathbuf, namelen+1, NULL); | |
2354 | dir_clear(&d); | |
2355 | free(pathbuf); | |
2356 | if (i) | |
2357 | return add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name); | |
2358 | ||
2359 | /* Do not lose startup_info->original_cwd */ | |
2360 | if (startup_info->original_cwd && | |
2361 | !strcmp(startup_info->original_cwd, ce->name)) | |
2362 | return add_rejected_path(o, ERROR_CWD_IN_THE_WAY, ce->name); | |
2363 | ||
2364 | return cnt; | |
2365 | } | |
2366 | ||
2367 | /* | |
2368 | * This gets called when there was no index entry for the tree entry 'dst', | |
2369 | * but we found a file in the working tree that 'lstat()' said was fine, | |
2370 | * and we're on a case-insensitive filesystem. | |
2371 | * | |
2372 | * See if we can find a case-insensitive match in the index that also | |
2373 | * matches the stat information, and assume it's that other file! | |
2374 | */ | |
2375 | static int icase_exists(struct unpack_trees_options *o, const char *name, int len, struct stat *st) | |
2376 | { | |
2377 | const struct cache_entry *src; | |
2378 | ||
2379 | src = index_file_exists(o->src_index, name, len, 1); | |
2380 | return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE); | |
2381 | } | |
2382 | ||
2383 | enum absent_checking_type { | |
2384 | COMPLETELY_ABSENT, | |
2385 | ABSENT_ANY_DIRECTORY | |
2386 | }; | |
2387 | ||
2388 | static int check_ok_to_remove(const char *name, int len, int dtype, | |
2389 | const struct cache_entry *ce, struct stat *st, | |
2390 | enum unpack_trees_error_types error_type, | |
2391 | enum absent_checking_type absent_type, | |
2392 | struct unpack_trees_options *o) | |
2393 | { | |
2394 | const struct cache_entry *result; | |
2395 | ||
2396 | /* | |
2397 | * It may be that the 'lstat()' succeeded even though | |
2398 | * target 'ce' was absent, because there is an old | |
2399 | * entry that is different only in case.. | |
2400 | * | |
2401 | * Ignore that lstat() if it matches. | |
2402 | */ | |
2403 | if (ignore_case && icase_exists(o, name, len, st)) | |
2404 | return 0; | |
2405 | ||
2406 | if (o->internal.dir && | |
2407 | is_excluded(o->internal.dir, o->src_index, name, &dtype)) | |
2408 | /* | |
2409 | * ce->name is explicitly excluded, so it is Ok to | |
2410 | * overwrite it. | |
2411 | */ | |
2412 | return 0; | |
2413 | if (S_ISDIR(st->st_mode)) { | |
2414 | /* | |
2415 | * We are checking out path "foo" and | |
2416 | * found "foo/." in the working tree. | |
2417 | * This is tricky -- if we have modified | |
2418 | * files that are in "foo/" we would lose | |
2419 | * them. | |
2420 | */ | |
2421 | if (verify_clean_subdirectory(ce, o) < 0) | |
2422 | return -1; | |
2423 | return 0; | |
2424 | } | |
2425 | ||
2426 | /* If we only care about directories, then we can remove */ | |
2427 | if (absent_type == ABSENT_ANY_DIRECTORY) | |
2428 | return 0; | |
2429 | ||
2430 | /* | |
2431 | * The previous round may already have decided to | |
2432 | * delete this path, which is in a subdirectory that | |
2433 | * is being replaced with a blob. | |
2434 | */ | |
2435 | result = index_file_exists(&o->internal.result, name, len, 0); | |
2436 | if (result) { | |
2437 | if (result->ce_flags & CE_REMOVE) | |
2438 | return 0; | |
2439 | } | |
2440 | ||
2441 | return add_rejected_path(o, error_type, name); | |
2442 | } | |
2443 | ||
2444 | /* | |
2445 | * We do not want to remove or overwrite a working tree file that | |
2446 | * is not tracked, unless it is ignored. | |
2447 | */ | |
2448 | static int verify_absent_1(const struct cache_entry *ce, | |
2449 | enum unpack_trees_error_types error_type, | |
2450 | enum absent_checking_type absent_type, | |
2451 | struct unpack_trees_options *o) | |
2452 | { | |
2453 | int len; | |
2454 | struct stat st; | |
2455 | ||
2456 | if (o->index_only || !o->update) | |
2457 | return 0; | |
2458 | ||
2459 | if (o->reset == UNPACK_RESET_OVERWRITE_UNTRACKED) { | |
2460 | /* Avoid nuking startup_info->original_cwd... */ | |
2461 | if (startup_info->original_cwd && | |
2462 | !strcmp(startup_info->original_cwd, ce->name)) | |
2463 | return add_rejected_path(o, ERROR_CWD_IN_THE_WAY, | |
2464 | ce->name); | |
2465 | /* ...but nuke anything else. */ | |
2466 | return 0; | |
2467 | } | |
2468 | ||
2469 | len = check_leading_path(ce->name, ce_namelen(ce), 0); | |
2470 | if (!len) | |
2471 | return 0; | |
2472 | else if (len > 0) { | |
2473 | char *path; | |
2474 | int ret; | |
2475 | ||
2476 | path = xmemdupz(ce->name, len); | |
2477 | if (lstat(path, &st)) | |
2478 | ret = error_errno("cannot stat '%s'", path); | |
2479 | else { | |
2480 | if (submodule_from_ce(ce)) | |
2481 | ret = check_submodule_move_head(ce, | |
2482 | oid_to_hex(&ce->oid), | |
2483 | NULL, o); | |
2484 | else | |
2485 | ret = check_ok_to_remove(path, len, DT_UNKNOWN, NULL, | |
2486 | &st, error_type, | |
2487 | absent_type, o); | |
2488 | } | |
2489 | free(path); | |
2490 | return ret; | |
2491 | } else if (lstat(ce->name, &st)) { | |
2492 | if (errno != ENOENT) | |
2493 | return error_errno("cannot stat '%s'", ce->name); | |
2494 | return 0; | |
2495 | } else { | |
2496 | if (submodule_from_ce(ce)) | |
2497 | return check_submodule_move_head(ce, oid_to_hex(&ce->oid), | |
2498 | NULL, o); | |
2499 | ||
2500 | return check_ok_to_remove(ce->name, ce_namelen(ce), | |
2501 | ce_to_dtype(ce), ce, &st, | |
2502 | error_type, absent_type, o); | |
2503 | } | |
2504 | } | |
2505 | ||
2506 | static int verify_absent(const struct cache_entry *ce, | |
2507 | enum unpack_trees_error_types error_type, | |
2508 | struct unpack_trees_options *o) | |
2509 | { | |
2510 | if (!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE)) | |
2511 | return 0; | |
2512 | return verify_absent_1(ce, error_type, COMPLETELY_ABSENT, o); | |
2513 | } | |
2514 | ||
2515 | static int verify_absent_if_directory(const struct cache_entry *ce, | |
2516 | enum unpack_trees_error_types error_type, | |
2517 | struct unpack_trees_options *o) | |
2518 | { | |
2519 | if (!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE)) | |
2520 | return 0; | |
2521 | return verify_absent_1(ce, error_type, ABSENT_ANY_DIRECTORY, o); | |
2522 | } | |
2523 | ||
2524 | static int verify_absent_sparse(const struct cache_entry *ce, | |
2525 | enum unpack_trees_error_types error_type, | |
2526 | struct unpack_trees_options *o) | |
2527 | { | |
2528 | return verify_absent_1(ce, error_type, COMPLETELY_ABSENT, o); | |
2529 | } | |
2530 | ||
2531 | static int merged_entry(const struct cache_entry *ce, | |
2532 | const struct cache_entry *old, | |
2533 | struct unpack_trees_options *o) | |
2534 | { | |
2535 | int update = CE_UPDATE; | |
2536 | struct cache_entry *merge = dup_cache_entry(ce, &o->internal.result); | |
2537 | ||
2538 | if (!old) { | |
2539 | /* | |
2540 | * New index entries. In sparse checkout, the following | |
2541 | * verify_absent() will be delayed until after | |
2542 | * traverse_trees() finishes in unpack_trees(), then: | |
2543 | * | |
2544 | * - CE_NEW_SKIP_WORKTREE will be computed correctly | |
2545 | * - verify_absent() be called again, this time with | |
2546 | * correct CE_NEW_SKIP_WORKTREE | |
2547 | * | |
2548 | * verify_absent() call here does nothing in sparse | |
2549 | * checkout (i.e. o->skip_sparse_checkout == 0) | |
2550 | */ | |
2551 | update |= CE_ADDED; | |
2552 | merge->ce_flags |= CE_NEW_SKIP_WORKTREE; | |
2553 | ||
2554 | if (verify_absent(merge, | |
2555 | ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) { | |
2556 | discard_cache_entry(merge); | |
2557 | return -1; | |
2558 | } | |
2559 | invalidate_ce_path(merge, o); | |
2560 | ||
2561 | if (submodule_from_ce(ce) && file_exists(ce->name)) { | |
2562 | int ret = check_submodule_move_head(ce, NULL, | |
2563 | oid_to_hex(&ce->oid), | |
2564 | o); | |
2565 | if (ret) | |
2566 | return ret; | |
2567 | } | |
2568 | ||
2569 | } else if (!(old->ce_flags & CE_CONFLICTED)) { | |
2570 | /* | |
2571 | * See if we can re-use the old CE directly? | |
2572 | * That way we get the uptodate stat info. | |
2573 | * | |
2574 | * This also removes the UPDATE flag on a match; otherwise | |
2575 | * we will end up overwriting local changes in the work tree. | |
2576 | */ | |
2577 | if (same(old, merge)) { | |
2578 | copy_cache_entry(merge, old); | |
2579 | update = 0; | |
2580 | } else { | |
2581 | if (verify_uptodate(old, o)) { | |
2582 | discard_cache_entry(merge); | |
2583 | return -1; | |
2584 | } | |
2585 | /* Migrate old flags over */ | |
2586 | update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE); | |
2587 | invalidate_ce_path(old, o); | |
2588 | } | |
2589 | ||
2590 | if (submodule_from_ce(ce) && file_exists(ce->name)) { | |
2591 | int ret = check_submodule_move_head(ce, oid_to_hex(&old->oid), | |
2592 | oid_to_hex(&ce->oid), | |
2593 | o); | |
2594 | if (ret) | |
2595 | return ret; | |
2596 | } | |
2597 | } else { | |
2598 | /* | |
2599 | * Previously unmerged entry left as an existence | |
2600 | * marker by read_index_unmerged(); | |
2601 | */ | |
2602 | if (verify_absent_if_directory(merge, | |
2603 | ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) { | |
2604 | discard_cache_entry(merge); | |
2605 | return -1; | |
2606 | } | |
2607 | ||
2608 | invalidate_ce_path(old, o); | |
2609 | } | |
2610 | ||
2611 | if (do_add_entry(o, merge, update, CE_STAGEMASK) < 0) | |
2612 | return -1; | |
2613 | return 1; | |
2614 | } | |
2615 | ||
2616 | static int merged_sparse_dir(const struct cache_entry * const *src, int n, | |
2617 | struct unpack_trees_options *o) | |
2618 | { | |
2619 | struct tree_desc t[MAX_UNPACK_TREES + 1]; | |
2620 | void * tree_bufs[MAX_UNPACK_TREES + 1]; | |
2621 | struct traverse_info info; | |
2622 | int i, ret; | |
2623 | ||
2624 | /* | |
2625 | * Create the tree traversal information for traversing into *only* the | |
2626 | * sparse directory. | |
2627 | */ | |
2628 | setup_traverse_info(&info, src[0]->name); | |
2629 | info.fn = unpack_sparse_callback; | |
2630 | info.data = o; | |
2631 | info.show_all_errors = o->internal.show_all_errors; | |
2632 | info.pathspec = o->pathspec; | |
2633 | ||
2634 | /* Get the tree descriptors of the sparse directory in each of the merging trees */ | |
2635 | for (i = 0; i < n; i++) | |
2636 | tree_bufs[i] = fill_tree_descriptor(o->src_index->repo, &t[i], | |
2637 | src[i] && !is_null_oid(&src[i]->oid) ? &src[i]->oid : NULL); | |
2638 | ||
2639 | ret = traverse_trees(o->src_index, n, t, &info); | |
2640 | ||
2641 | for (i = 0; i < n; i++) | |
2642 | free(tree_bufs[i]); | |
2643 | ||
2644 | return ret; | |
2645 | } | |
2646 | ||
2647 | static int deleted_entry(const struct cache_entry *ce, | |
2648 | const struct cache_entry *old, | |
2649 | struct unpack_trees_options *o) | |
2650 | { | |
2651 | /* Did it exist in the index? */ | |
2652 | if (!old) { | |
2653 | if (verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o)) | |
2654 | return -1; | |
2655 | return 0; | |
2656 | } else if (verify_absent_if_directory(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o)) { | |
2657 | return -1; | |
2658 | } | |
2659 | ||
2660 | if (!(old->ce_flags & CE_CONFLICTED) && verify_uptodate(old, o)) | |
2661 | return -1; | |
2662 | add_entry(o, ce, CE_REMOVE, 0); | |
2663 | invalidate_ce_path(ce, o); | |
2664 | return 1; | |
2665 | } | |
2666 | ||
2667 | static int keep_entry(const struct cache_entry *ce, | |
2668 | struct unpack_trees_options *o) | |
2669 | { | |
2670 | add_entry(o, ce, 0, 0); | |
2671 | if (ce_stage(ce)) | |
2672 | invalidate_ce_path(ce, o); | |
2673 | return 1; | |
2674 | } | |
2675 | ||
2676 | #if DBRT_DEBUG | |
2677 | static void show_stage_entry(FILE *o, | |
2678 | const char *label, const struct cache_entry *ce) | |
2679 | { | |
2680 | if (!ce) | |
2681 | fprintf(o, "%s (missing)\n", label); | |
2682 | else | |
2683 | fprintf(o, "%s%06o %s %d\t%s\n", | |
2684 | label, | |
2685 | ce->ce_mode, | |
2686 | oid_to_hex(&ce->oid), | |
2687 | ce_stage(ce), | |
2688 | ce->name); | |
2689 | } | |
2690 | #endif | |
2691 | ||
2692 | int threeway_merge(const struct cache_entry * const *stages, | |
2693 | struct unpack_trees_options *o) | |
2694 | { | |
2695 | const struct cache_entry *index; | |
2696 | const struct cache_entry *head; | |
2697 | const struct cache_entry *remote = stages[o->head_idx + 1]; | |
2698 | int count; | |
2699 | int head_match = 0; | |
2700 | int remote_match = 0; | |
2701 | ||
2702 | int df_conflict_head = 0; | |
2703 | int df_conflict_remote = 0; | |
2704 | ||
2705 | int any_anc_missing = 0; | |
2706 | int no_anc_exists = 1; | |
2707 | int i; | |
2708 | ||
2709 | for (i = 1; i < o->head_idx; i++) { | |
2710 | if (!stages[i] || stages[i] == o->df_conflict_entry) | |
2711 | any_anc_missing = 1; | |
2712 | else | |
2713 | no_anc_exists = 0; | |
2714 | } | |
2715 | ||
2716 | index = stages[0]; | |
2717 | head = stages[o->head_idx]; | |
2718 | ||
2719 | if (head == o->df_conflict_entry) { | |
2720 | df_conflict_head = 1; | |
2721 | head = NULL; | |
2722 | } | |
2723 | ||
2724 | if (remote == o->df_conflict_entry) { | |
2725 | df_conflict_remote = 1; | |
2726 | remote = NULL; | |
2727 | } | |
2728 | ||
2729 | /* | |
2730 | * First, if there's a #16 situation, note that to prevent #13 | |
2731 | * and #14. | |
2732 | */ | |
2733 | if (!same(remote, head)) { | |
2734 | for (i = 1; i < o->head_idx; i++) { | |
2735 | if (same(stages[i], head)) { | |
2736 | head_match = i; | |
2737 | } | |
2738 | if (same(stages[i], remote)) { | |
2739 | remote_match = i; | |
2740 | } | |
2741 | } | |
2742 | } | |
2743 | ||
2744 | /* | |
2745 | * We start with cases where the index is allowed to match | |
2746 | * something other than the head: #14(ALT) and #2ALT, where it | |
2747 | * is permitted to match the result instead. | |
2748 | */ | |
2749 | /* #14, #14ALT, #2ALT */ | |
2750 | if (remote && !df_conflict_head && head_match && !remote_match) { | |
2751 | if (index && !same(index, remote) && !same(index, head)) { | |
2752 | if (S_ISSPARSEDIR(index->ce_mode)) | |
2753 | return merged_sparse_dir(stages, 4, o); | |
2754 | else | |
2755 | return reject_merge(index, o); | |
2756 | } | |
2757 | return merged_entry(remote, index, o); | |
2758 | } | |
2759 | /* | |
2760 | * If we have an entry in the index cache, then we want to | |
2761 | * make sure that it matches head. | |
2762 | */ | |
2763 | if (index && !same(index, head)) { | |
2764 | if (S_ISSPARSEDIR(index->ce_mode)) | |
2765 | return merged_sparse_dir(stages, 4, o); | |
2766 | else | |
2767 | return reject_merge(index, o); | |
2768 | } | |
2769 | ||
2770 | if (head) { | |
2771 | /* #5ALT, #15 */ | |
2772 | if (same(head, remote)) | |
2773 | return merged_entry(head, index, o); | |
2774 | /* #13, #3ALT */ | |
2775 | if (!df_conflict_remote && remote_match && !head_match) | |
2776 | return merged_entry(head, index, o); | |
2777 | } | |
2778 | ||
2779 | /* #1 */ | |
2780 | if (!head && !remote && any_anc_missing) | |
2781 | return 0; | |
2782 | ||
2783 | /* | |
2784 | * Under the "aggressive" rule, we resolve mostly trivial | |
2785 | * cases that we historically had git-merge-one-file resolve. | |
2786 | */ | |
2787 | if (o->aggressive) { | |
2788 | int head_deleted = !head; | |
2789 | int remote_deleted = !remote; | |
2790 | const struct cache_entry *ce = NULL; | |
2791 | ||
2792 | if (index) | |
2793 | ce = index; | |
2794 | else if (head) | |
2795 | ce = head; | |
2796 | else if (remote) | |
2797 | ce = remote; | |
2798 | else { | |
2799 | for (i = 1; i < o->head_idx; i++) { | |
2800 | if (stages[i] && stages[i] != o->df_conflict_entry) { | |
2801 | ce = stages[i]; | |
2802 | break; | |
2803 | } | |
2804 | } | |
2805 | } | |
2806 | ||
2807 | /* | |
2808 | * Deleted in both. | |
2809 | * Deleted in one and unchanged in the other. | |
2810 | */ | |
2811 | if ((head_deleted && remote_deleted) || | |
2812 | (head_deleted && remote && remote_match) || | |
2813 | (remote_deleted && head && head_match)) { | |
2814 | if (index) | |
2815 | return deleted_entry(index, index, o); | |
2816 | if (ce && !head_deleted) { | |
2817 | if (verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o)) | |
2818 | return -1; | |
2819 | } | |
2820 | return 0; | |
2821 | } | |
2822 | /* | |
2823 | * Added in both, identically. | |
2824 | */ | |
2825 | if (no_anc_exists && head && remote && same(head, remote)) | |
2826 | return merged_entry(head, index, o); | |
2827 | ||
2828 | } | |
2829 | ||
2830 | /* Handle "no merge" cases (see t/t1000-read-tree-m-3way.sh) */ | |
2831 | if (index) { | |
2832 | /* | |
2833 | * If we've reached the "no merge" cases and we're merging | |
2834 | * a sparse directory, we may have an "edit/edit" conflict that | |
2835 | * can be resolved by individually merging directory contents. | |
2836 | */ | |
2837 | if (S_ISSPARSEDIR(index->ce_mode)) | |
2838 | return merged_sparse_dir(stages, 4, o); | |
2839 | ||
2840 | /* | |
2841 | * If we're not merging a sparse directory, ensure the index is | |
2842 | * up-to-date to avoid files getting overwritten with conflict | |
2843 | * resolution files | |
2844 | */ | |
2845 | if (verify_uptodate(index, o)) | |
2846 | return -1; | |
2847 | } | |
2848 | ||
2849 | o->internal.nontrivial_merge = 1; | |
2850 | ||
2851 | /* #2, #3, #4, #6, #7, #9, #10, #11. */ | |
2852 | count = 0; | |
2853 | if (!head_match || !remote_match) { | |
2854 | for (i = 1; i < o->head_idx; i++) { | |
2855 | if (stages[i] && stages[i] != o->df_conflict_entry) { | |
2856 | keep_entry(stages[i], o); | |
2857 | count++; | |
2858 | break; | |
2859 | } | |
2860 | } | |
2861 | } | |
2862 | #if DBRT_DEBUG | |
2863 | else { | |
2864 | fprintf(stderr, "read-tree: warning #16 detected\n"); | |
2865 | show_stage_entry(stderr, "head ", stages[head_match]); | |
2866 | show_stage_entry(stderr, "remote ", stages[remote_match]); | |
2867 | } | |
2868 | #endif | |
2869 | if (head) { count += keep_entry(head, o); } | |
2870 | if (remote) { count += keep_entry(remote, o); } | |
2871 | return count; | |
2872 | } | |
2873 | ||
2874 | /* | |
2875 | * Two-way merge. | |
2876 | * | |
2877 | * The rule is to "carry forward" what is in the index without losing | |
2878 | * information across a "fast-forward", favoring a successful merge | |
2879 | * over a merge failure when it makes sense. For details of the | |
2880 | * "carry forward" rule, please see <Documentation/git-read-tree.txt>. | |
2881 | * | |
2882 | */ | |
2883 | int twoway_merge(const struct cache_entry * const *src, | |
2884 | struct unpack_trees_options *o) | |
2885 | { | |
2886 | const struct cache_entry *current = src[0]; | |
2887 | const struct cache_entry *oldtree = src[1]; | |
2888 | const struct cache_entry *newtree = src[2]; | |
2889 | ||
2890 | if (o->internal.merge_size != 2) | |
2891 | return error("Cannot do a twoway merge of %d trees", | |
2892 | o->internal.merge_size); | |
2893 | ||
2894 | if (oldtree == o->df_conflict_entry) | |
2895 | oldtree = NULL; | |
2896 | if (newtree == o->df_conflict_entry) | |
2897 | newtree = NULL; | |
2898 | ||
2899 | if (current) { | |
2900 | if (current->ce_flags & CE_CONFLICTED) { | |
2901 | if (same(oldtree, newtree) || o->reset) { | |
2902 | if (!newtree) | |
2903 | return deleted_entry(current, current, o); | |
2904 | else | |
2905 | return merged_entry(newtree, current, o); | |
2906 | } | |
2907 | return reject_merge(current, o); | |
2908 | } else if ((!oldtree && !newtree) || /* 4 and 5 */ | |
2909 | (!oldtree && newtree && | |
2910 | same(current, newtree)) || /* 6 and 7 */ | |
2911 | (oldtree && newtree && | |
2912 | same(oldtree, newtree)) || /* 14 and 15 */ | |
2913 | (oldtree && newtree && | |
2914 | !same(oldtree, newtree) && /* 18 and 19 */ | |
2915 | same(current, newtree))) { | |
2916 | return keep_entry(current, o); | |
2917 | } else if (oldtree && !newtree && same(current, oldtree)) { | |
2918 | /* 10 or 11 */ | |
2919 | return deleted_entry(oldtree, current, o); | |
2920 | } else if (oldtree && newtree && | |
2921 | same(current, oldtree) && !same(current, newtree)) { | |
2922 | /* 20 or 21 */ | |
2923 | return merged_entry(newtree, current, o); | |
2924 | } else if (current && !oldtree && newtree && | |
2925 | S_ISSPARSEDIR(current->ce_mode) != S_ISSPARSEDIR(newtree->ce_mode) && | |
2926 | ce_stage(current) == 0) { | |
2927 | /* | |
2928 | * This case is a directory/file conflict across the sparse-index | |
2929 | * boundary. When we are changing from one path to another via | |
2930 | * 'git checkout', then we want to replace one entry with another | |
2931 | * via merged_entry(). If there are staged changes, then we should | |
2932 | * reject the merge instead. | |
2933 | */ | |
2934 | return merged_entry(newtree, current, o); | |
2935 | } else if (S_ISSPARSEDIR(current->ce_mode)) { | |
2936 | /* | |
2937 | * The sparse directories differ, but we don't know whether that's | |
2938 | * because of two different files in the directory being modified | |
2939 | * (can be trivially merged) or if there is a real file conflict. | |
2940 | * Merge the sparse directory by OID to compare file-by-file. | |
2941 | */ | |
2942 | return merged_sparse_dir(src, 3, o); | |
2943 | } else | |
2944 | return reject_merge(current, o); | |
2945 | } | |
2946 | else if (newtree) { | |
2947 | if (oldtree && !o->initial_checkout) { | |
2948 | /* | |
2949 | * deletion of the path was staged; | |
2950 | */ | |
2951 | if (same(oldtree, newtree)) | |
2952 | return 1; | |
2953 | return reject_merge(oldtree, o); | |
2954 | } | |
2955 | return merged_entry(newtree, current, o); | |
2956 | } | |
2957 | return deleted_entry(oldtree, current, o); | |
2958 | } | |
2959 | ||
2960 | /* | |
2961 | * Bind merge. | |
2962 | * | |
2963 | * Keep the index entries at stage0, collapse stage1 but make sure | |
2964 | * stage0 does not have anything there. | |
2965 | */ | |
2966 | int bind_merge(const struct cache_entry * const *src, | |
2967 | struct unpack_trees_options *o) | |
2968 | { | |
2969 | const struct cache_entry *old = src[0]; | |
2970 | const struct cache_entry *a = src[1]; | |
2971 | ||
2972 | if (o->internal.merge_size != 1) | |
2973 | return error("Cannot do a bind merge of %d trees", | |
2974 | o->internal.merge_size); | |
2975 | if (a && old) | |
2976 | return o->quiet ? -1 : | |
2977 | error(ERRORMSG(o, ERROR_BIND_OVERLAP), | |
2978 | super_prefixed(a->name, o->super_prefix), | |
2979 | super_prefixed(old->name, o->super_prefix)); | |
2980 | if (!a) | |
2981 | return keep_entry(old, o); | |
2982 | else | |
2983 | return merged_entry(a, NULL, o); | |
2984 | } | |
2985 | ||
2986 | /* | |
2987 | * One-way merge. | |
2988 | * | |
2989 | * The rule is: | |
2990 | * - take the stat information from stage0, take the data from stage1 | |
2991 | */ | |
2992 | int oneway_merge(const struct cache_entry * const *src, | |
2993 | struct unpack_trees_options *o) | |
2994 | { | |
2995 | const struct cache_entry *old = src[0]; | |
2996 | const struct cache_entry *a = src[1]; | |
2997 | ||
2998 | if (o->internal.merge_size != 1) | |
2999 | return error("Cannot do a oneway merge of %d trees", | |
3000 | o->internal.merge_size); | |
3001 | ||
3002 | if (!a || a == o->df_conflict_entry) | |
3003 | return deleted_entry(old, old, o); | |
3004 | ||
3005 | if (old && same(old, a)) { | |
3006 | int update = 0; | |
3007 | if (o->reset && o->update && !ce_uptodate(old) && !ce_skip_worktree(old) && | |
3008 | !(old->ce_flags & CE_FSMONITOR_VALID)) { | |
3009 | struct stat st; | |
3010 | if (lstat(old->name, &st) || | |
3011 | ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE)) | |
3012 | update |= CE_UPDATE; | |
3013 | } | |
3014 | if (o->update && S_ISGITLINK(old->ce_mode) && | |
3015 | should_update_submodules() && !verify_uptodate(old, o)) | |
3016 | update |= CE_UPDATE; | |
3017 | add_entry(o, old, update, CE_STAGEMASK); | |
3018 | return 0; | |
3019 | } | |
3020 | return merged_entry(a, old, o); | |
3021 | } | |
3022 | ||
3023 | /* | |
3024 | * Merge worktree and untracked entries in a stash entry. | |
3025 | * | |
3026 | * Ignore all index entries. Collapse remaining trees but make sure that they | |
3027 | * don't have any conflicting files. | |
3028 | */ | |
3029 | int stash_worktree_untracked_merge(const struct cache_entry * const *src, | |
3030 | struct unpack_trees_options *o) | |
3031 | { | |
3032 | const struct cache_entry *worktree = src[1]; | |
3033 | const struct cache_entry *untracked = src[2]; | |
3034 | ||
3035 | if (o->internal.merge_size != 2) | |
3036 | BUG("invalid merge_size: %d", o->internal.merge_size); | |
3037 | ||
3038 | if (worktree && untracked) | |
3039 | return error(_("worktree and untracked commit have duplicate entries: %s"), | |
3040 | super_prefixed(worktree->name, o->super_prefix)); | |
3041 | ||
3042 | return merged_entry(worktree ? worktree : untracked, NULL, o); | |
3043 | } |