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