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