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