sha1-file: support OBJECT_INFO_FOR_PREFETCH
[git/git.git] / unpack-trees.c
CommitLineData
16da134b 1#include "cache.h"
1c41d280 2#include "argv-array.h"
33028713 3#include "repository.h"
b2141fc1 4#include "config.h"
f8a9d428 5#include "dir.h"
16da134b
JS
6#include "tree.h"
7#include "tree-walk.h"
076b0adc 8#include "cache-tree.h"
16da134b 9#include "unpack-trees.h"
96a02f8f 10#include "progress.h"
0cf73755 11#include "refs.h"
06f33c17 12#include "attr.h"
5fc2fc8f 13#include "split-index.h"
e931371a 14#include "dir.h"
a7bc845a
SB
15#include "submodule.h"
16#include "submodule-config.h"
883e248b 17#include "fsmonitor.h"
cbd53a21 18#include "object-store.h"
c0c578b3 19#include "fetch-object.h"
16da134b 20
8ccba008
JH
21/*
22 * Error messages expected by scripts out of plumbing commands such as
23 * read-tree. Non-scripted Porcelain is not required to use these messages
24 * and in fact are encouraged to reword them to better suit their particular
23cbf11b 25 * situation better. See how "git checkout" and "git merge" replaces
dc1166e6 26 * them using setup_unpack_trees_porcelain(), for example.
8ccba008 27 */
c2e86add 28static const char *unpack_plumbing_errors[NB_UNPACK_TREES_ERROR_TYPES] = {
08353ebb 29 /* ERROR_WOULD_OVERWRITE */
8ccba008
JH
30 "Entry '%s' would be overwritten by merge. Cannot merge.",
31
08353ebb 32 /* ERROR_NOT_UPTODATE_FILE */
8ccba008
JH
33 "Entry '%s' not uptodate. Cannot merge.",
34
08353ebb 35 /* ERROR_NOT_UPTODATE_DIR */
8ccba008
JH
36 "Updating '%s' would lose untracked files in it",
37
08402b04
MM
38 /* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */
39 "Untracked working tree file '%s' would be overwritten by merge.",
8ccba008 40
08402b04
MM
41 /* ERROR_WOULD_LOSE_UNTRACKED_REMOVED */
42 "Untracked working tree file '%s' would be removed by merge.",
8ccba008 43
08353ebb 44 /* ERROR_BIND_OVERLAP */
8ccba008 45 "Entry '%s' overlaps with '%s'. Cannot bind.",
e800ec9d 46
08353ebb 47 /* ERROR_SPARSE_NOT_UPTODATE_FILE */
e800ec9d
NTND
48 "Entry '%s' not uptodate. Cannot update sparse checkout.",
49
08402b04
MM
50 /* ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN */
51 "Working tree file '%s' would be overwritten by sparse checkout update.",
52
53 /* ERROR_WOULD_LOSE_ORPHANED_REMOVED */
54 "Working tree file '%s' would be removed by sparse checkout update.",
a7bc845a
SB
55
56 /* ERROR_WOULD_LOSE_SUBMODULE */
57 "Submodule '%s' cannot checkout new HEAD.",
8ccba008
JH
58};
59
08353ebb
MM
60#define ERRORMSG(o,type) \
61 ( ((o) && (o)->msgs[(type)]) \
62 ? ((o)->msgs[(type)]) \
63 : (unpack_plumbing_errors[(type)]) )
8ccba008 64
3d415425
SB
65static const char *super_prefixed(const char *path)
66{
67 /*
68 * It is necessary and sufficient to have two static buffers
69 * here, as the return value of this function is fed to
70 * error() using the unpack_*_errors[] templates we see above.
71 */
72 static struct strbuf buf[2] = {STRBUF_INIT, STRBUF_INIT};
73 static int super_prefix_len = -1;
74 static unsigned idx = ARRAY_SIZE(buf) - 1;
75
76 if (super_prefix_len < 0) {
77 const char *super_prefix = get_super_prefix();
78 if (!super_prefix) {
79 super_prefix_len = 0;
80 } else {
81 int i;
82 for (i = 0; i < ARRAY_SIZE(buf); i++)
83 strbuf_addstr(&buf[i], super_prefix);
84 super_prefix_len = buf[0].len;
85 }
86 }
87
88 if (!super_prefix_len)
89 return path;
90
91 if (++idx >= ARRAY_SIZE(buf))
92 idx = 0;
93
94 strbuf_setlen(&buf[idx], super_prefix_len);
95 strbuf_addstr(&buf[idx], path);
96
97 return buf[idx].buf;
98}
99
e294030f
MM
100void setup_unpack_trees_porcelain(struct unpack_trees_options *opts,
101 const char *cmd)
dc1166e6 102{
7980872d 103 int i;
e294030f 104 const char **msgs = opts->msgs;
dc1166e6 105 const char *msg;
fa3f60b7 106
1c41d280
107 argv_array_init(&opts->msgs_to_free);
108
2e3926b9
VA
109 if (!strcmp(cmd, "checkout"))
110 msg = advice_commit_before_merge
111 ? _("Your local changes to the following files would be overwritten by checkout:\n%%s"
c2691e2a 112 "Please commit your changes or stash them before you switch branches.")
2e3926b9
VA
113 : _("Your local changes to the following files would be overwritten by checkout:\n%%s");
114 else if (!strcmp(cmd, "merge"))
115 msg = advice_commit_before_merge
116 ? _("Your local changes to the following files would be overwritten by merge:\n%%s"
c2691e2a 117 "Please commit your changes or stash them before you merge.")
2e3926b9 118 : _("Your local changes to the following files would be overwritten by merge:\n%%s");
dc1166e6 119 else
2e3926b9
VA
120 msg = advice_commit_before_merge
121 ? _("Your local changes to the following files would be overwritten by %s:\n%%s"
c2691e2a 122 "Please commit your changes or stash them before you %s.")
2e3926b9 123 : _("Your local changes to the following files would be overwritten by %s:\n%%s");
fa3f60b7 124 msgs[ERROR_WOULD_OVERWRITE] = msgs[ERROR_NOT_UPTODATE_FILE] =
1c41d280 125 argv_array_pushf(&opts->msgs_to_free, msg, cmd, cmd);
dc1166e6
MM
126
127 msgs[ERROR_NOT_UPTODATE_DIR] =
584f99c8 128 _("Updating the following directories would lose untracked files in them:\n%s");
dc1166e6 129
2e3926b9
VA
130 if (!strcmp(cmd, "checkout"))
131 msg = advice_commit_before_merge
132 ? _("The following untracked working tree files would be removed by checkout:\n%%s"
c2691e2a 133 "Please move or remove them before you switch branches.")
2e3926b9
VA
134 : _("The following untracked working tree files would be removed by checkout:\n%%s");
135 else if (!strcmp(cmd, "merge"))
136 msg = advice_commit_before_merge
137 ? _("The following untracked working tree files would be removed by merge:\n%%s"
c2691e2a 138 "Please move or remove them before you merge.")
2e3926b9 139 : _("The following untracked working tree files would be removed by merge:\n%%s");
dc1166e6 140 else
2e3926b9
VA
141 msg = advice_commit_before_merge
142 ? _("The following untracked working tree files would be removed by %s:\n%%s"
c2691e2a 143 "Please move or remove them before you %s.")
2e3926b9 144 : _("The following untracked working tree files would be removed by %s:\n%%s");
1c41d280
145 msgs[ERROR_WOULD_LOSE_UNTRACKED_REMOVED] =
146 argv_array_pushf(&opts->msgs_to_free, msg, cmd, cmd);
2e3926b9
VA
147
148 if (!strcmp(cmd, "checkout"))
149 msg = advice_commit_before_merge
150 ? _("The following untracked working tree files would be overwritten by checkout:\n%%s"
c2691e2a 151 "Please move or remove them before you switch branches.")
2e3926b9
VA
152 : _("The following untracked working tree files would be overwritten by checkout:\n%%s");
153 else if (!strcmp(cmd, "merge"))
154 msg = advice_commit_before_merge
155 ? _("The following untracked working tree files would be overwritten by merge:\n%%s"
c2691e2a 156 "Please move or remove them before you merge.")
2e3926b9
VA
157 : _("The following untracked working tree files would be overwritten by merge:\n%%s");
158 else
159 msg = advice_commit_before_merge
160 ? _("The following untracked working tree files would be overwritten by %s:\n%%s"
c2691e2a 161 "Please move or remove them before you %s.")
2e3926b9 162 : _("The following untracked working tree files would be overwritten by %s:\n%%s");
1c41d280
163 msgs[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN] =
164 argv_array_pushf(&opts->msgs_to_free, msg, cmd, cmd);
dc1166e6
MM
165
166 /*
167 * Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we
168 * cannot easily display it as a list.
169 */
ed47fdf7 170 msgs[ERROR_BIND_OVERLAP] = _("Entry '%s' overlaps with '%s'. Cannot bind.");
dc1166e6
MM
171
172 msgs[ERROR_SPARSE_NOT_UPTODATE_FILE] =
7560f547 173 _("Cannot update sparse checkout: the following entries are not up to date:\n%s");
dc1166e6 174 msgs[ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN] =
a1c80446 175 _("The following working tree files would be overwritten by sparse checkout update:\n%s");
dc1166e6 176 msgs[ERROR_WOULD_LOSE_ORPHANED_REMOVED] =
a1c80446 177 _("The following working tree files would be removed by sparse checkout update:\n%s");
a7bc845a 178 msgs[ERROR_WOULD_LOSE_SUBMODULE] =
6362ed0b 179 _("Cannot update submodule:\n%s");
5e65ee35
MM
180
181 opts->show_all_errors = 1;
7980872d
CB
182 /* rejected paths may not have a static buffer */
183 for (i = 0; i < ARRAY_SIZE(opts->unpack_rejects); i++)
184 opts->unpack_rejects[i].strdup_strings = 1;
dc1166e6
MM
185}
186
1c41d280
187void clear_unpack_trees_porcelain(struct unpack_trees_options *opts)
188{
189 argv_array_clear(&opts->msgs_to_free);
190 memset(opts->msgs, 0, sizeof(opts->msgs));
191}
192
46169180 193static int do_add_entry(struct unpack_trees_options *o, struct cache_entry *ce,
6ff264ee 194 unsigned int set, unsigned int clear)
b48d5a05 195{
419a597f 196 clear |= CE_HASHED;
34110cd4 197
700e66d6
NTND
198 if (set & CE_REMOVE)
199 set |= CE_WT_REMOVE;
200
6ff264ee 201 ce->ce_flags = (ce->ce_flags & ~clear) | set;
46169180
JK
202 return add_index_entry(&o->result, ce,
203 ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
6ff264ee
RS
204}
205
a33bd4d3
RS
206static void add_entry(struct unpack_trees_options *o,
207 const struct cache_entry *ce,
208 unsigned int set, unsigned int clear)
209{
8e72d675 210 do_add_entry(o, dup_cache_entry(ce, &o->result), set, clear);
b48d5a05
LT
211}
212
e6c111b4
MM
213/*
214 * add error messages on path <path>
215 * corresponding to the type <e> with the message <msg>
216 * indicating if it should be display in porcelain or not
217 */
218static int add_rejected_path(struct unpack_trees_options *o,
219 enum unpack_trees_error_types e,
220 const char *path)
221{
e6c111b4 222 if (!o->show_all_errors)
3d415425 223 return error(ERRORMSG(o, e), super_prefixed(path));
e6c111b4
MM
224
225 /*
226 * Otherwise, insert in a list for future display by
227 * display_error_msgs()
228 */
7980872d 229 string_list_append(&o->unpack_rejects[e], path);
e6c111b4
MM
230 return -1;
231}
232
e6c111b4
MM
233/*
234 * display all the error messages stored in a nice way
235 */
236static void display_error_msgs(struct unpack_trees_options *o)
237{
7980872d 238 int e, i;
e6c111b4
MM
239 int something_displayed = 0;
240 for (e = 0; e < NB_UNPACK_TREES_ERROR_TYPES; e++) {
7980872d
CB
241 struct string_list *rejects = &o->unpack_rejects[e];
242 if (rejects->nr > 0) {
e6c111b4
MM
243 struct strbuf path = STRBUF_INIT;
244 something_displayed = 1;
7980872d
CB
245 for (i = 0; i < rejects->nr; i++)
246 strbuf_addf(&path, "\t%s\n", rejects->items[i].string);
3d415425 247 error(ERRORMSG(o, e), super_prefixed(path.buf));
e6c111b4 248 strbuf_release(&path);
e6c111b4 249 }
7980872d 250 string_list_clear(rejects, 0);
e6c111b4
MM
251 }
252 if (something_displayed)
ed47fdf7 253 fprintf(stderr, _("Aborting\n"));
e6c111b4
MM
254}
255
a7bc845a
SB
256static int check_submodule_move_head(const struct cache_entry *ce,
257 const char *old_id,
258 const char *new_id,
259 struct unpack_trees_options *o)
260{
cd279e2e 261 unsigned flags = SUBMODULE_MOVE_HEAD_DRY_RUN;
a7bc845a 262 const struct submodule *sub = submodule_from_ce(ce);
7463e2ec 263
a7bc845a
SB
264 if (!sub)
265 return 0;
266
cd279e2e
SB
267 if (o->reset)
268 flags |= SUBMODULE_MOVE_HEAD_FORCE;
269
7463e2ec
BW
270 if (submodule_move_head(ce->name, old_id, new_id, flags))
271 return o->gently ? -1 :
272 add_rejected_path(o, ERROR_WOULD_LOSE_SUBMODULE, ce->name);
273 return 0;
a7bc845a
SB
274}
275
33028713
BW
276/*
277 * Preform the loading of the repository's gitmodules file. This function is
278 * used by 'check_update()' to perform loading of the gitmodules file in two
279 * differnt situations:
280 * (1) before removing entries from the working tree if the gitmodules file has
281 * been marked for removal. This situation is specified by 'state' == NULL.
282 * (2) before checking out entries to the working tree if the gitmodules file
283 * has been marked for update. This situation is specified by 'state' != NULL.
284 */
285static void load_gitmodules_file(struct index_state *index,
286 struct checkout *state)
a7bc845a 287{
33028713
BW
288 int pos = index_name_pos(index, GITMODULES_FILE, strlen(GITMODULES_FILE));
289
290 if (pos >= 0) {
291 struct cache_entry *ce = index->cache[pos];
292 if (!state && ce->ce_flags & CE_WT_REMOVE) {
293 repo_read_gitmodules(the_repository);
294 } else if (state && (ce->ce_flags & CE_UPDATE)) {
f793b895 295 submodule_free(the_repository);
0f086e6d 296 checkout_entry(ce, state, NULL, NULL);
33028713 297 repo_read_gitmodules(the_repository);
a7bc845a
SB
298 }
299 }
300}
301
384f1a16 302static struct progress *get_progress(struct unpack_trees_options *o)
16da134b 303{
96a02f8f 304 unsigned cnt = 0, total = 0;
384f1a16
SB
305 struct index_state *index = &o->result;
306
307 if (!o->update || !o->verbose_update)
308 return NULL;
309
310 for (; cnt < index->cache_nr; cnt++) {
311 const struct cache_entry *ce = index->cache[cnt];
312 if (ce->ce_flags & (CE_UPDATE | CE_WT_REMOVE))
313 total++;
314 }
315
8aade107 316 return start_delayed_progress(_("Checking out files"), total);
384f1a16
SB
317}
318
b878579a
NTND
319static void setup_collided_checkout_detection(struct checkout *state,
320 struct index_state *index)
321{
322 int i;
323
324 state->clone = 1;
325 for (i = 0; i < index->cache_nr; i++)
326 index->cache[i]->ce_flags &= ~CE_MATCHED;
327}
328
329static void report_collided_checkout(struct index_state *index)
330{
331 struct string_list list = STRING_LIST_INIT_NODUP;
332 int i;
333
334 for (i = 0; i < index->cache_nr; i++) {
335 struct cache_entry *ce = index->cache[i];
336
337 if (!(ce->ce_flags & CE_MATCHED))
338 continue;
339
340 string_list_append(&list, ce->name);
341 ce->ce_flags &= ~CE_MATCHED;
342 }
343
344 list.cmp = fspathcmp;
345 string_list_sort(&list);
346
347 if (list.nr) {
348 warning(_("the following paths have collided (e.g. case-sensitive paths\n"
349 "on a case-insensitive filesystem) and only one from the same\n"
350 "colliding group is in the working tree:\n"));
351
352 for (i = 0; i < list.nr; i++)
353 fprintf(stderr, " '%s'\n", list.items[i].string);
354 }
355
356 string_list_clear(&list, 0);
357}
358
384f1a16
SB
359static int check_updates(struct unpack_trees_options *o)
360{
361 unsigned cnt = 0;
30ac275b 362 int errs = 0;
07f967ad 363 struct progress *progress;
34110cd4 364 struct index_state *index = &o->result;
30ac275b 365 struct checkout state = CHECKOUT_INIT;
33ecf7eb 366 int i;
16da134b 367
0d1ed596 368 trace_performance_enter();
30ac275b
SB
369 state.force = 1;
370 state.quiet = 1;
371 state.refresh_cache = 1;
372 state.istate = index;
16da134b 373
b878579a
NTND
374 if (o->clone)
375 setup_collided_checkout_detection(&state, index);
376
384f1a16 377 progress = get_progress(o);
16da134b 378
66985e66 379 if (o->update)
c4500e25 380 git_attr_set_direction(GIT_ATTR_CHECKOUT);
33028713
BW
381
382 if (should_update_submodules() && o->update && !o->dry_run)
383 load_gitmodules_file(index, NULL);
384
34110cd4 385 for (i = 0; i < index->cache_nr; i++) {
9c5e6c80 386 const struct cache_entry *ce = index->cache[i];
16da134b 387
e663db2f
NTND
388 if (ce->ce_flags & CE_WT_REMOVE) {
389 display_progress(progress, ++cnt);
2c9078d0 390 if (o->update && !o->dry_run)
e663db2f 391 unlink_entry(ce);
e663db2f 392 }
1fa6ead4 393 }
6fdc2057 394 remove_marked_cache_entries(index, 0);
78478927 395 remove_scheduled_dirs();
1fa6ead4 396
a7bc845a 397 if (should_update_submodules() && o->update && !o->dry_run)
33028713 398 load_gitmodules_file(index, &state);
a7bc845a 399
2841e8f8 400 enable_delayed_checkout(&state);
c0c578b3
JT
401 if (repository_format_partial_clone && o->update && !o->dry_run) {
402 /*
403 * Prefetch the objects that are to be checked out in the loop
404 * below.
405 */
406 struct oid_array to_fetch = OID_ARRAY_INIT;
c0c578b3
JT
407 for (i = 0; i < index->cache_nr; i++) {
408 struct cache_entry *ce = index->cache[i];
0f4a4fb1
JT
409
410 if (!(ce->ce_flags & CE_UPDATE) ||
411 S_ISGITLINK(ce->ce_mode))
412 continue;
413 if (!oid_object_info_extended(the_repository, &ce->oid,
414 NULL,
415 OBJECT_INFO_FOR_PREFETCH))
416 continue;
417 oid_array_append(&to_fetch, &ce->oid);
c0c578b3
JT
418 }
419 if (to_fetch.nr)
420 fetch_objects(repository_format_partial_clone,
8708ca09 421 to_fetch.oid, to_fetch.nr);
9f242a13 422 oid_array_clear(&to_fetch);
c0c578b3 423 }
1fa6ead4
LT
424 for (i = 0; i < index->cache_nr; i++) {
425 struct cache_entry *ce = index->cache[i];
426
7a51ed66 427 if (ce->ce_flags & CE_UPDATE) {
7d782416 428 if (ce->ce_flags & CE_WT_REMOVE)
033abf97 429 BUG("both update and delete flags are set on %s",
7d782416 430 ce->name);
1fa6ead4 431 display_progress(progress, ++cnt);
7a51ed66 432 ce->ce_flags &= ~CE_UPDATE;
2c9078d0 433 if (o->update && !o->dry_run) {
0f086e6d 434 errs |= checkout_entry(ce, &state, NULL, NULL);
16a4c617 435 }
16da134b
JS
436 }
437 }
4d4fcc54 438 stop_progress(&progress);
0f086e6d 439 errs |= finish_delayed_checkout(&state, NULL);
66985e66 440 if (o->update)
c4500e25 441 git_attr_set_direction(GIT_ATTR_CHECKIN);
b878579a
NTND
442
443 if (o->clone)
444 report_collided_checkout(index);
445
0d1ed596 446 trace_performance_leave("check_updates");
c4758d3c 447 return errs != 0;
16da134b
JS
448}
449
eb9ae4b5
RS
450static int verify_uptodate_sparse(const struct cache_entry *ce,
451 struct unpack_trees_options *o);
452static int verify_absent_sparse(const struct cache_entry *ce,
453 enum unpack_trees_error_types,
454 struct unpack_trees_options *o);
e800ec9d 455
a5c446f1
NTND
456static int apply_sparse_checkout(struct index_state *istate,
457 struct cache_entry *ce,
458 struct unpack_trees_options *o)
e800ec9d
NTND
459{
460 int was_skip_worktree = ce_skip_worktree(ce);
461
2431afbf 462 if (ce->ce_flags & CE_NEW_SKIP_WORKTREE)
e800ec9d
NTND
463 ce->ce_flags |= CE_SKIP_WORKTREE;
464 else
465 ce->ce_flags &= ~CE_SKIP_WORKTREE;
078a58e8
NTND
466 if (was_skip_worktree != ce_skip_worktree(ce)) {
467 ce->ce_flags |= CE_UPDATE_IN_BASE;
883e248b 468 mark_fsmonitor_invalid(istate, ce);
a5c446f1 469 istate->cache_changed |= CE_ENTRY_CHANGED;
078a58e8 470 }
e800ec9d
NTND
471
472 /*
eec3fc03
NTND
473 * if (!was_skip_worktree && !ce_skip_worktree()) {
474 * This is perfectly normal. Move on;
475 * }
e800ec9d 476 */
eec3fc03
NTND
477
478 /*
479 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout
480 * area as a result of ce_skip_worktree() shortcuts in
700e66d6
NTND
481 * verify_absent() and verify_uptodate().
482 * Make sure they don't modify worktree if they are already
483 * outside checkout area
eec3fc03 484 */
700e66d6
NTND
485 if (was_skip_worktree && ce_skip_worktree(ce)) {
486 ce->ce_flags &= ~CE_UPDATE;
487
488 /*
489 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also
490 * on to get that file removed from both index and worktree.
491 * If that file is already outside worktree area, don't
492 * bother remove it.
493 */
494 if (ce->ce_flags & CE_REMOVE)
495 ce->ce_flags &= ~CE_WT_REMOVE;
496 }
e800ec9d
NTND
497
498 if (!was_skip_worktree && ce_skip_worktree(ce)) {
499 /*
500 * If CE_UPDATE is set, verify_uptodate() must be called already
501 * also stat info may have lost after merged_entry() so calling
502 * verify_uptodate() again may fail
503 */
504 if (!(ce->ce_flags & CE_UPDATE) && verify_uptodate_sparse(ce, o))
505 return -1;
506 ce->ce_flags |= CE_WT_REMOVE;
7d782416 507 ce->ce_flags &= ~CE_UPDATE;
e800ec9d
NTND
508 }
509 if (was_skip_worktree && !ce_skip_worktree(ce)) {
08402b04 510 if (verify_absent_sparse(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o))
e800ec9d
NTND
511 return -1;
512 ce->ce_flags |= CE_UPDATE;
513 }
514 return 0;
515}
516
5828e835
RS
517static inline int call_unpack_fn(const struct cache_entry * const *src,
518 struct unpack_trees_options *o)
01904572 519{
34110cd4
LT
520 int ret = o->fn(src, o);
521 if (ret > 0)
01904572 522 ret = 0;
01904572
LT
523 return ret;
524}
525
da165f47
JH
526static void mark_ce_used(struct cache_entry *ce, struct unpack_trees_options *o)
527{
528 ce->ce_flags |= CE_UNPACKED;
529
530 if (o->cache_bottom < o->src_index->cache_nr &&
531 o->src_index->cache[o->cache_bottom] == ce) {
532 int bottom = o->cache_bottom;
533 while (bottom < o->src_index->cache_nr &&
534 o->src_index->cache[bottom]->ce_flags & CE_UNPACKED)
535 bottom++;
536 o->cache_bottom = bottom;
537 }
538}
539
540static void mark_all_ce_unused(struct index_state *index)
541{
542 int i;
543 for (i = 0; i < index->cache_nr; i++)
2431afbf 544 index->cache[i]->ce_flags &= ~(CE_UNPACKED | CE_ADDED | CE_NEW_SKIP_WORKTREE);
da165f47
JH
545}
546
eb9ae4b5 547static int locate_in_src_index(const struct cache_entry *ce,
da165f47
JH
548 struct unpack_trees_options *o)
549{
550 struct index_state *index = o->src_index;
551 int len = ce_namelen(ce);
552 int pos = index_name_pos(index, ce->name, len);
553 if (pos < 0)
554 pos = -1 - pos;
555 return pos;
556}
557
558/*
559 * We call unpack_index_entry() with an unmerged cache entry
560 * only in diff-index, and it wants a single callback. Skip
561 * the other unmerged entry with the same name.
562 */
563static void mark_ce_used_same_name(struct cache_entry *ce,
564 struct unpack_trees_options *o)
565{
566 struct index_state *index = o->src_index;
567 int len = ce_namelen(ce);
568 int pos;
569
570 for (pos = locate_in_src_index(ce, o); pos < index->cache_nr; pos++) {
571 struct cache_entry *next = index->cache[pos];
572 if (len != ce_namelen(next) ||
573 memcmp(ce->name, next->name, len))
574 break;
575 mark_ce_used(next, o);
576 }
577}
578
579static struct cache_entry *next_cache_entry(struct unpack_trees_options *o)
580{
581 const struct index_state *index = o->src_index;
582 int pos = o->cache_bottom;
583
584 while (pos < index->cache_nr) {
585 struct cache_entry *ce = index->cache[pos];
586 if (!(ce->ce_flags & CE_UNPACKED))
587 return ce;
588 pos++;
589 }
590 return NULL;
591}
592
9c5e6c80 593static void add_same_unmerged(const struct cache_entry *ce,
da165f47
JH
594 struct unpack_trees_options *o)
595{
596 struct index_state *index = o->src_index;
597 int len = ce_namelen(ce);
598 int pos = index_name_pos(index, ce->name, len);
599
600 if (0 <= pos)
601 die("programming error in a caller of mark_ce_used_same_name");
602 for (pos = -pos - 1; pos < index->cache_nr; pos++) {
603 struct cache_entry *next = index->cache[pos];
604 if (len != ce_namelen(next) ||
605 memcmp(ce->name, next->name, len))
606 break;
607 add_entry(o, next, 0, 0);
608 mark_ce_used(next, o);
609 }
610}
611
612static int unpack_index_entry(struct cache_entry *ce,
613 struct unpack_trees_options *o)
01904572 614{
5828e835 615 const struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
da165f47 616 int ret;
34110cd4 617
66dbfd55
GV
618 src[0] = ce;
619
da165f47 620 mark_ce_used(ce, o);
01904572
LT
621 if (ce_stage(ce)) {
622 if (o->skip_unmerged) {
34110cd4
LT
623 add_entry(o, ce, 0, 0);
624 return 0;
01904572 625 }
01904572 626 }
da165f47
JH
627 ret = call_unpack_fn(src, o);
628 if (ce_stage(ce))
629 mark_ce_used_same_name(ce, o);
630 return ret;
01904572
LT
631}
632
730f7284
JH
633static int find_cache_pos(struct traverse_info *, const struct name_entry *);
634
635static void restore_cache_bottom(struct traverse_info *info, int bottom)
636{
637 struct unpack_trees_options *o = info->data;
638
639 if (o->diff_index_cached)
640 return;
641 o->cache_bottom = bottom;
642}
643
644static int switch_cache_bottom(struct traverse_info *info)
645{
646 struct unpack_trees_options *o = info->data;
647 int ret, pos;
648
649 if (o->diff_index_cached)
650 return 0;
651 ret = o->cache_bottom;
652 pos = find_cache_pos(info->prev, &info->name);
653
654 if (pos < -1)
655 o->cache_bottom = -2 - pos;
656 else if (pos < 0)
657 o->cache_bottom = o->src_index->cache_nr;
658 return ret;
01904572
LT
659}
660
d12a8cf0
JH
661static inline int are_same_oid(struct name_entry *name_j, struct name_entry *name_k)
662{
ea82b2a0 663 return !is_null_oid(&name_j->oid) && !is_null_oid(&name_k->oid) && oideq(&name_j->oid, &name_k->oid);
d12a8cf0
JH
664}
665
b4da3738
NTND
666static int all_trees_same_as_cache_tree(int n, unsigned long dirmask,
667 struct name_entry *names,
668 struct traverse_info *info)
669{
670 struct unpack_trees_options *o = info->data;
671 int i;
672
673 if (!o->merge || dirmask != ((1 << n) - 1))
674 return 0;
675
676 for (i = 1; i < n; i++)
677 if (!are_same_oid(names, names + i))
678 return 0;
679
680 return cache_tree_matches_traversal(o->src_index->cache_tree, names, info);
681}
682
683static int index_pos_by_traverse_info(struct name_entry *names,
684 struct traverse_info *info)
685{
686 struct unpack_trees_options *o = info->data;
687 int len = traverse_path_len(info, names);
688 char *name = xmalloc(len + 1 /* slash */ + 1 /* NUL */);
689 int pos;
690
691 make_traverse_path(name, info, names);
692 name[len++] = '/';
693 name[len] = '\0';
694 pos = index_name_pos(o->src_index, name, len);
695 if (pos >= 0)
696 BUG("This is a directory and should not exist in index");
697 pos = -pos - 1;
698 if (!starts_with(o->src_index->cache[pos]->name, name) ||
699 (pos > 0 && starts_with(o->src_index->cache[pos-1]->name, name)))
700 BUG("pos must point at the first entry in this directory");
701 free(name);
702 return pos;
703}
704
705/*
706 * Fast path if we detect that all trees are the same as cache-tree at this
5f4436a7
NTND
707 * path. We'll walk these trees in an iterative loop using cache-tree/index
708 * instead of ODB since we already know what these trees contain.
b4da3738
NTND
709 */
710static int traverse_by_cache_tree(int pos, int nr_entries, int nr_names,
711 struct name_entry *names,
712 struct traverse_info *info)
713{
714 struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
715 struct unpack_trees_options *o = info->data;
f1e11c65
NTND
716 struct cache_entry *tree_ce = NULL;
717 int ce_len = 0;
b4da3738
NTND
718 int i, d;
719
720 if (!o->merge)
721 BUG("We need cache-tree to do this optimization");
722
723 /*
724 * Do what unpack_callback() and unpack_nondirectories() normally
725 * do. But we walk all paths in an iterative loop instead.
726 *
727 * D/F conflicts and higher stage entries are not a concern
728 * because cache-tree would be invalidated and we would never
729 * get here in the first place.
730 */
731 for (i = 0; i < nr_entries; i++) {
f1e11c65 732 int new_ce_len, len, rc;
b4da3738
NTND
733
734 src[0] = o->src_index->cache[pos + i];
735
736 len = ce_namelen(src[0]);
f1e11c65
NTND
737 new_ce_len = cache_entry_size(len);
738
739 if (new_ce_len > ce_len) {
740 new_ce_len <<= 1;
741 tree_ce = xrealloc(tree_ce, new_ce_len);
742 memset(tree_ce, 0, new_ce_len);
743 ce_len = new_ce_len;
744
745 tree_ce->ce_flags = create_ce_flags(0);
746
747 for (d = 1; d <= nr_names; d++)
748 src[d] = tree_ce;
749 }
b4da3738
NTND
750
751 tree_ce->ce_mode = src[0]->ce_mode;
b4da3738
NTND
752 tree_ce->ce_namelen = len;
753 oidcpy(&tree_ce->oid, &src[0]->oid);
754 memcpy(tree_ce->name, src[0]->name, len + 1);
755
b4da3738 756 rc = call_unpack_fn((const struct cache_entry * const *)src, o);
f1e11c65
NTND
757 if (rc < 0) {
758 free(tree_ce);
b4da3738 759 return rc;
f1e11c65 760 }
b4da3738
NTND
761
762 mark_ce_used(src[0], o);
763 }
f1e11c65 764 free(tree_ce);
b4da3738
NTND
765 if (o->debug_unpack)
766 printf("Unpacked %d entries from %s to %s using cache-tree\n",
767 nr_entries,
768 o->src_index->cache[pos]->name,
769 o->src_index->cache[pos + nr_entries - 1]->name);
770 return 0;
771}
772
84563a62
JH
773static int traverse_trees_recursive(int n, unsigned long dirmask,
774 unsigned long df_conflicts,
775 struct name_entry *names,
776 struct traverse_info *info)
16da134b 777{
67022e02 778 struct unpack_trees_options *o = info->data;
730f7284 779 int i, ret, bottom;
d12a8cf0 780 int nr_buf = 0;
ca885a4f 781 struct tree_desc t[MAX_UNPACK_TREES];
1ce584b0 782 void *buf[MAX_UNPACK_TREES];
01904572
LT
783 struct traverse_info newinfo;
784 struct name_entry *p;
b4da3738
NTND
785 int nr_entries;
786
787 nr_entries = all_trees_same_as_cache_tree(n, dirmask, names, info);
788 if (nr_entries > 0) {
b4da3738
NTND
789 int pos = index_pos_by_traverse_info(names, info);
790
791 if (!o->merge || df_conflicts)
792 BUG("Wrong condition to get here buddy");
793
794 /*
795 * All entries up to 'pos' must have been processed
796 * (i.e. marked CE_UNPACKED) at this point. But to be safe,
797 * save and restore cache_bottom anyway to not miss
798 * unprocessed entries before 'pos'.
799 */
800 bottom = o->cache_bottom;
801 ret = traverse_by_cache_tree(pos, nr_entries, n, names, info);
802 o->cache_bottom = bottom;
803 return ret;
804 }
01904572
LT
805
806 p = names;
807 while (!p->mode)
808 p++;
809
810 newinfo = *info;
811 newinfo.prev = info;
40e37256 812 newinfo.pathspec = info->pathspec;
01904572 813 newinfo.name = *p;
0de16337 814 newinfo.pathlen += tree_entry_len(p) + 1;
603d2498 815 newinfo.df_conflicts |= df_conflicts;
01904572 816
d12a8cf0
JH
817 /*
818 * Fetch the tree from the ODB for each peer directory in the
819 * n commits.
820 *
821 * For 2- and 3-way traversals, we try to avoid hitting the
822 * ODB twice for the same OID. This should yield a nice speed
823 * up in checkouts and merges when the commits are similar.
824 *
825 * We don't bother doing the full O(n^2) search for larger n,
826 * because wider traversals don't happen that often and we
827 * avoid the search setup.
828 *
829 * When 2 peer OIDs are the same, we just copy the tree
830 * descriptor data. This implicitly borrows the buffer
831 * data from the earlier cell.
832 */
01904572 833 for (i = 0; i < n; i++, dirmask >>= 1) {
d12a8cf0
JH
834 if (i > 0 && are_same_oid(&names[i], &names[i - 1]))
835 t[i] = t[i - 1];
836 else if (i > 1 && are_same_oid(&names[i], &names[i - 2]))
837 t[i] = t[i - 2];
838 else {
5c377d3d 839 const struct object_id *oid = NULL;
d12a8cf0 840 if (dirmask & 1)
ea82b2a0 841 oid = &names[i].oid;
5c377d3d 842 buf[nr_buf++] = fill_tree_descriptor(t + i, oid);
d12a8cf0 843 }
01904572 844 }
730f7284
JH
845
846 bottom = switch_cache_bottom(&newinfo);
67022e02 847 ret = traverse_trees(o->src_index, n, t, &newinfo);
730f7284 848 restore_cache_bottom(&newinfo, bottom);
1ce584b0 849
d12a8cf0 850 for (i = 0; i < nr_buf; i++)
1ce584b0
JN
851 free(buf[i]);
852
730f7284 853 return ret;
01904572
LT
854}
855
856/*
857 * Compare the traverse-path to the cache entry without actually
858 * having to generate the textual representation of the traverse
859 * path.
860 *
861 * NOTE! This *only* compares up to the size of the traverse path
862 * itself - the caller needs to do the final check for the cache
863 * entry having more data at the end!
864 */
d9c2bd56 865static int do_compare_entry_piecewise(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
01904572
LT
866{
867 int len, pathlen, ce_len;
868 const char *ce_name;
869
870 if (info->prev) {
d9c2bd56
DT
871 int cmp = do_compare_entry_piecewise(ce, info->prev,
872 &info->name);
01904572
LT
873 if (cmp)
874 return cmp;
875 }
876 pathlen = info->pathlen;
877 ce_len = ce_namelen(ce);
878
879 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
880 if (ce_len < pathlen)
881 return -1;
882
883 ce_len -= pathlen;
884 ce_name = ce->name + pathlen;
885
0de16337 886 len = tree_entry_len(n);
01904572
LT
887 return df_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode);
888}
889
d9c2bd56
DT
890static int do_compare_entry(const struct cache_entry *ce,
891 const struct traverse_info *info,
892 const struct name_entry *n)
893{
894 int len, pathlen, ce_len;
895 const char *ce_name;
896 int cmp;
897
898 /*
899 * If we have not precomputed the traverse path, it is quicker
900 * to avoid doing so. But if we have precomputed it,
901 * it is quicker to use the precomputed version.
902 */
903 if (!info->traverse_path)
904 return do_compare_entry_piecewise(ce, info, n);
905
906 cmp = strncmp(ce->name, info->traverse_path, info->pathlen);
907 if (cmp)
908 return cmp;
909
910 pathlen = info->pathlen;
911 ce_len = ce_namelen(ce);
912
913 if (ce_len < pathlen)
914 return -1;
915
916 ce_len -= pathlen;
917 ce_name = ce->name + pathlen;
918
919 len = tree_entry_len(n);
920 return df_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode);
921}
922
01904572
LT
923static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
924{
925 int cmp = do_compare_entry(ce, info, n);
926 if (cmp)
927 return cmp;
928
929 /*
930 * Even if the beginning compared identically, the ce should
931 * compare as bigger than a directory leading up to it!
932 */
933 return ce_namelen(ce) > traverse_path_len(info, n);
934}
935
da165f47
JH
936static int ce_in_traverse_path(const struct cache_entry *ce,
937 const struct traverse_info *info)
938{
939 if (!info->prev)
940 return 1;
941 if (do_compare_entry(ce, info->prev, &info->name))
942 return 0;
943 /*
944 * If ce (blob) is the same name as the path (which is a tree
945 * we will be descending into), it won't be inside it.
946 */
947 return (info->pathlen < ce_namelen(ce));
948}
949
a849735b
JM
950static struct cache_entry *create_ce_entry(const struct traverse_info *info,
951 const struct name_entry *n,
952 int stage,
953 struct index_state *istate,
954 int is_transient)
01904572
LT
955{
956 int len = traverse_path_len(info, n);
a849735b
JM
957 struct cache_entry *ce =
958 is_transient ?
959 make_empty_transient_cache_entry(len) :
960 make_empty_cache_entry(istate, len);
01904572
LT
961
962 ce->ce_mode = create_ce_mode(n->mode);
b60e188c
TG
963 ce->ce_flags = create_ce_flags(stage);
964 ce->ce_namelen = len;
ea82b2a0 965 oidcpy(&ce->oid, &n->oid);
01904572
LT
966 make_traverse_path(ce->name, info, n);
967
968 return ce;
969}
970
b4da3738
NTND
971/*
972 * Note that traverse_by_cache_tree() duplicates some logic in this function
973 * without actually calling it. If you change the logic here you may need to
974 * check and change there as well.
975 */
c7cddc1a
RS
976static int unpack_nondirectories(int n, unsigned long mask,
977 unsigned long dirmask,
978 struct cache_entry **src,
979 const struct name_entry *names,
980 const struct traverse_info *info)
01904572
LT
981{
982 int i;
983 struct unpack_trees_options *o = info->data;
603d2498 984 unsigned long conflicts = info->df_conflicts | dirmask;
01904572
LT
985
986 /* Do we have *only* directories? Nothing to do */
987 if (mask == dirmask && !src[0])
988 return 0;
989
01904572
LT
990 /*
991 * Ok, we've filled in up to any potential index entry in src[0],
992 * now do the rest.
993 */
994 for (i = 0; i < n; i++) {
995 int stage;
996 unsigned int bit = 1ul << i;
997 if (conflicts & bit) {
998 src[i + o->merge] = o->df_conflict_entry;
999 continue;
1000 }
1001 if (!(mask & bit))
1002 continue;
1003 if (!o->merge)
1004 stage = 0;
1005 else if (i + 1 < o->head_idx)
1006 stage = 1;
1007 else if (i + 1 > o->head_idx)
1008 stage = 3;
1009 else
1010 stage = 2;
a849735b
JM
1011
1012 /*
1013 * If the merge bit is set, then the cache entries are
1014 * discarded in the following block. In this case,
1015 * construct "transient" cache_entries, as they are
1016 * not stored in the index. otherwise construct the
1017 * cache entry from the index aware logic.
1018 */
1019 src[i + o->merge] = create_ce_entry(info, names + i, stage, &o->result, o->merge);
01904572
LT
1020 }
1021
5d80ef5a
RS
1022 if (o->merge) {
1023 int rc = call_unpack_fn((const struct cache_entry * const *)src,
1024 o);
1025 for (i = 0; i < n; i++) {
1026 struct cache_entry *ce = src[i + o->merge];
1027 if (ce != o->df_conflict_entry)
a849735b 1028 discard_cache_entry(ce);
5d80ef5a
RS
1029 }
1030 return rc;
1031 }
01904572 1032
01904572 1033 for (i = 0; i < n; i++)
aab3b9a1 1034 if (src[i] && src[i] != o->df_conflict_entry)
46169180
JK
1035 if (do_add_entry(o, src[i], 0, 0))
1036 return -1;
1037
01904572
LT
1038 return 0;
1039}
1040
353c5eeb
JH
1041static int unpack_failed(struct unpack_trees_options *o, const char *message)
1042{
1043 discard_index(&o->result);
b4194828 1044 if (!o->gently && !o->exiting_early) {
353c5eeb
JH
1045 if (message)
1046 return error("%s", message);
1047 return -1;
1048 }
1049 return -1;
1050}
1051
730f7284
JH
1052/*
1053 * The tree traversal is looking at name p. If we have a matching entry,
1054 * return it. If name p is a directory in the index, do not return
1055 * anything, as we will want to match it when the traversal descends into
1056 * the directory.
1057 */
1058static int find_cache_pos(struct traverse_info *info,
1059 const struct name_entry *p)
1060{
1061 int pos;
1062 struct unpack_trees_options *o = info->data;
1063 struct index_state *index = o->src_index;
1064 int pfxlen = info->pathlen;
0de16337 1065 int p_len = tree_entry_len(p);
730f7284
JH
1066
1067 for (pos = o->cache_bottom; pos < index->cache_nr; pos++) {
9c5e6c80 1068 const struct cache_entry *ce = index->cache[pos];
730f7284
JH
1069 const char *ce_name, *ce_slash;
1070 int cmp, ce_len;
1071
e53e6b44
BD
1072 if (ce->ce_flags & CE_UNPACKED) {
1073 /*
1074 * cache_bottom entry is already unpacked, so
1075 * we can never match it; don't check it
1076 * again.
1077 */
1078 if (pos == o->cache_bottom)
1079 ++o->cache_bottom;
730f7284 1080 continue;
e53e6b44 1081 }
a6720955
DT
1082 if (!ce_in_traverse_path(ce, info)) {
1083 /*
1084 * Check if we can skip future cache checks
1085 * (because we're already past all possible
1086 * entries in the traverse path).
1087 */
1088 if (info->traverse_path) {
1089 if (strncmp(ce->name, info->traverse_path,
1090 info->pathlen) > 0)
1091 break;
1092 }
730f7284 1093 continue;
a6720955 1094 }
730f7284
JH
1095 ce_name = ce->name + pfxlen;
1096 ce_slash = strchr(ce_name, '/');
1097 if (ce_slash)
1098 ce_len = ce_slash - ce_name;
1099 else
1100 ce_len = ce_namelen(ce) - pfxlen;
1101 cmp = name_compare(p->path, p_len, ce_name, ce_len);
1102 /*
1103 * Exact match; if we have a directory we need to
1104 * delay returning it.
1105 */
1106 if (!cmp)
1107 return ce_slash ? -2 - pos : pos;
1108 if (0 < cmp)
1109 continue; /* keep looking */
1110 /*
1111 * ce_name sorts after p->path; could it be that we
1112 * have files under p->path directory in the index?
1113 * E.g. ce_name == "t-i", and p->path == "t"; we may
1114 * have "t/a" in the index.
1115 */
1116 if (p_len < ce_len && !memcmp(ce_name, p->path, p_len) &&
1117 ce_name[p_len] < '/')
1118 continue; /* keep looking */
1119 break;
1120 }
1121 return -1;
1122}
1123
1124static struct cache_entry *find_cache_entry(struct traverse_info *info,
1125 const struct name_entry *p)
1126{
1127 int pos = find_cache_pos(info, p);
1128 struct unpack_trees_options *o = info->data;
1129
1130 if (0 <= pos)
1131 return o->src_index->cache[pos];
1132 else
1133 return NULL;
1134}
1135
ba655da5
JH
1136static void debug_path(struct traverse_info *info)
1137{
1138 if (info->prev) {
1139 debug_path(info->prev);
1140 if (*info->prev->name.path)
1141 putchar('/');
1142 }
1143 printf("%s", info->name.path);
1144}
1145
1146static void debug_name_entry(int i, struct name_entry *n)
1147{
1148 printf("ent#%d %06o %s\n", i,
1149 n->path ? n->mode : 0,
1150 n->path ? n->path : "(missing)");
1151}
1152
1153static void debug_unpack_callback(int n,
1154 unsigned long mask,
1155 unsigned long dirmask,
1156 struct name_entry *names,
1157 struct traverse_info *info)
1158{
1159 int i;
1160 printf("* unpack mask %lu, dirmask %lu, cnt %d ",
1161 mask, dirmask, n);
1162 debug_path(info);
1163 putchar('\n');
1164 for (i = 0; i < n; i++)
1165 debug_name_entry(i, names + i);
1166}
1167
b4da3738
NTND
1168/*
1169 * Note that traverse_by_cache_tree() duplicates some logic in this function
1170 * without actually calling it. If you change the logic here you may need to
1171 * check and change there as well.
1172 */
01904572
LT
1173static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)
1174{
c7cddc1a 1175 struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
01904572 1176 struct unpack_trees_options *o = info->data;
01904572
LT
1177 const struct name_entry *p = names;
1178
1179 /* Find first entry with a real name (we could use "mask" too) */
1180 while (!p->mode)
1181 p++;
1182
ba655da5
JH
1183 if (o->debug_unpack)
1184 debug_unpack_callback(n, mask, dirmask, names, info);
1185
01904572
LT
1186 /* Are we supposed to look at the index too? */
1187 if (o->merge) {
da165f47 1188 while (1) {
da165f47 1189 int cmp;
730f7284
JH
1190 struct cache_entry *ce;
1191
1192 if (o->diff_index_cached)
1193 ce = next_cache_entry(o);
1194 else
1195 ce = find_cache_entry(info, p);
1196
da165f47
JH
1197 if (!ce)
1198 break;
1199 cmp = compare_entry(ce, info, p);
01904572
LT
1200 if (cmp < 0) {
1201 if (unpack_index_entry(ce, o) < 0)
353c5eeb 1202 return unpack_failed(o, NULL);
01904572
LT
1203 continue;
1204 }
1205 if (!cmp) {
1206 if (ce_stage(ce)) {
1207 /*
da165f47
JH
1208 * If we skip unmerged index
1209 * entries, we'll skip this
1210 * entry *and* the tree
1211 * entries associated with it!
01904572 1212 */
34110cd4 1213 if (o->skip_unmerged) {
da165f47 1214 add_same_unmerged(ce, o);
01904572 1215 return mask;
34110cd4 1216 }
01904572
LT
1217 }
1218 src[0] = ce;
01904572
LT
1219 }
1220 break;
1221 }
1222 }
1223
34110cd4 1224 if (unpack_nondirectories(n, mask, dirmask, src, names, info) < 0)
01904572
LT
1225 return -1;
1226
97e5954b 1227 if (o->merge && src[0]) {
da165f47
JH
1228 if (ce_stage(src[0]))
1229 mark_ce_used_same_name(src[0], o);
1230 else
1231 mark_ce_used(src[0], o);
1232 }
1233
01904572
LT
1234 /* Now handle any directories.. */
1235 if (dirmask) {
b65982b6
JH
1236 /* special case: "diff-index --cached" looking at a tree */
1237 if (o->diff_index_cached &&
1238 n == 1 && dirmask == 1 && S_ISDIR(names->mode)) {
1239 int matches;
1240 matches = cache_tree_matches_traversal(o->src_index->cache_tree,
1241 names, info);
1242 /*
da165f47
JH
1243 * Everything under the name matches; skip the
1244 * entire hierarchy. diff_index_cached codepath
1245 * special cases D/F conflicts in such a way that
1246 * it does not do any look-ahead, so this is safe.
b65982b6
JH
1247 */
1248 if (matches) {
da165f47 1249 o->cache_bottom += matches;
b65982b6
JH
1250 return mask;
1251 }
1252 }
1253
603d2498 1254 if (traverse_trees_recursive(n, dirmask, mask & ~dirmask,
542c264b
JH
1255 names, info) < 0)
1256 return -1;
01904572
LT
1257 return mask;
1258 }
1259
1260 return mask;
1261}
1262
27c82fb3
NTND
1263static int clear_ce_flags_1(struct index_state *istate,
1264 struct cache_entry **cache, int nr,
fc2b6214 1265 struct strbuf *prefix,
28911091
NTND
1266 int select_mask, int clear_mask,
1267 struct exclude_list *el, int defval);
1268
9037026d 1269/* Whole directory matching */
27c82fb3
NTND
1270static int clear_ce_flags_dir(struct index_state *istate,
1271 struct cache_entry **cache, int nr,
fc2b6214 1272 struct strbuf *prefix,
9037026d
NTND
1273 char *basename,
1274 int select_mask, int clear_mask,
28911091 1275 struct exclude_list *el, int defval)
9037026d 1276{
28911091 1277 struct cache_entry **cache_end;
9037026d 1278 int dtype = DT_DIR;
fc2b6214 1279 int ret = is_excluded_from_list(prefix->buf, prefix->len,
27c82fb3 1280 basename, &dtype, el, istate);
fc2b6214 1281 int rc;
9037026d 1282
fc2b6214 1283 strbuf_addch(prefix, '/');
9037026d 1284
28911091
NTND
1285 /* If undecided, use matching result of parent dir in defval */
1286 if (ret < 0)
1287 ret = defval;
9037026d 1288
28911091
NTND
1289 for (cache_end = cache; cache_end != cache + nr; cache_end++) {
1290 struct cache_entry *ce = *cache_end;
fc2b6214 1291 if (strncmp(ce->name, prefix->buf, prefix->len))
28911091 1292 break;
9037026d
NTND
1293 }
1294
28911091
NTND
1295 /*
1296 * TODO: check el, if there are no patterns that may conflict
1297 * with ret (iow, we know in advance the incl/excl
1298 * decision for the entire directory), clear flag here without
1299 * calling clear_ce_flags_1(). That function will call
07958050 1300 * the expensive is_excluded_from_list() on every entry.
28911091 1301 */
27c82fb3 1302 rc = clear_ce_flags_1(istate, cache, cache_end - cache,
fc2b6214
AP
1303 prefix,
1304 select_mask, clear_mask,
1305 el, ret);
1306 strbuf_setlen(prefix, prefix->len - 1);
1307 return rc;
9037026d
NTND
1308}
1309
1310/*
1311 * Traverse the index, find every entry that matches according to
1312 * o->el. Do "ce_flags &= ~clear_mask" on those entries. Return the
1313 * number of traversed entries.
1314 *
1315 * If select_mask is non-zero, only entries whose ce_flags has on of
1316 * those bits enabled are traversed.
1317 *
1318 * cache : pointer to an index entry
1319 * prefix_len : an offset to its path
1320 *
1321 * The current path ("prefix") including the trailing '/' is
1322 * cache[0]->name[0..(prefix_len-1)]
1323 * Top level path has prefix_len zero.
1324 */
27c82fb3
NTND
1325static int clear_ce_flags_1(struct index_state *istate,
1326 struct cache_entry **cache, int nr,
fc2b6214 1327 struct strbuf *prefix,
9037026d 1328 int select_mask, int clear_mask,
28911091 1329 struct exclude_list *el, int defval)
9037026d
NTND
1330{
1331 struct cache_entry **cache_end = cache + nr;
1332
1333 /*
1334 * Process all entries that have the given prefix and meet
1335 * select_mask condition
1336 */
1337 while(cache != cache_end) {
1338 struct cache_entry *ce = *cache;
1339 const char *name, *slash;
28911091 1340 int len, dtype, ret;
9037026d
NTND
1341
1342 if (select_mask && !(ce->ce_flags & select_mask)) {
1343 cache++;
1344 continue;
1345 }
1346
fc2b6214 1347 if (prefix->len && strncmp(ce->name, prefix->buf, prefix->len))
9037026d
NTND
1348 break;
1349
fc2b6214 1350 name = ce->name + prefix->len;
9037026d
NTND
1351 slash = strchr(name, '/');
1352
1353 /* If it's a directory, try whole directory match first */
1354 if (slash) {
1355 int processed;
1356
1357 len = slash - name;
fc2b6214 1358 strbuf_add(prefix, name, len);
9037026d 1359
27c82fb3 1360 processed = clear_ce_flags_dir(istate, cache, cache_end - cache,
fc2b6214
AP
1361 prefix,
1362 prefix->buf + prefix->len - len,
9037026d 1363 select_mask, clear_mask,
28911091 1364 el, defval);
9037026d
NTND
1365
1366 /* clear_c_f_dir eats a whole dir already? */
1367 if (processed) {
1368 cache += processed;
fc2b6214 1369 strbuf_setlen(prefix, prefix->len - len);
9037026d
NTND
1370 continue;
1371 }
1372
fc2b6214 1373 strbuf_addch(prefix, '/');
27c82fb3 1374 cache += clear_ce_flags_1(istate, cache, cache_end - cache,
fc2b6214 1375 prefix,
28911091 1376 select_mask, clear_mask, el, defval);
fc2b6214 1377 strbuf_setlen(prefix, prefix->len - len - 1);
9037026d
NTND
1378 continue;
1379 }
1380
1381 /* Non-directory */
1382 dtype = ce_to_dtype(ce);
07958050 1383 ret = is_excluded_from_list(ce->name, ce_namelen(ce),
27c82fb3 1384 name, &dtype, el, istate);
28911091
NTND
1385 if (ret < 0)
1386 ret = defval;
1387 if (ret > 0)
9037026d
NTND
1388 ce->ce_flags &= ~clear_mask;
1389 cache++;
1390 }
1391 return nr - (cache_end - cache);
1392}
1393
27c82fb3
NTND
1394static int clear_ce_flags(struct index_state *istate,
1395 int select_mask, int clear_mask,
1396 struct exclude_list *el)
9037026d 1397{
fc2b6214
AP
1398 static struct strbuf prefix = STRBUF_INIT;
1399
1400 strbuf_reset(&prefix);
1401
27c82fb3
NTND
1402 return clear_ce_flags_1(istate,
1403 istate->cache,
1404 istate->cache_nr,
fc2b6214 1405 &prefix,
9037026d 1406 select_mask, clear_mask,
28911091 1407 el, 0);
9037026d
NTND
1408}
1409
2431afbf
NTND
1410/*
1411 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout
1412 */
1413static void mark_new_skip_worktree(struct exclude_list *el,
86016ec3 1414 struct index_state *istate,
2431afbf
NTND
1415 int select_flag, int skip_wt_flag)
1416{
1417 int i;
1418
9037026d
NTND
1419 /*
1420 * 1. Pretend the narrowest worktree: only unmerged entries
1421 * are checked out
1422 */
86016ec3
NTND
1423 for (i = 0; i < istate->cache_nr; i++) {
1424 struct cache_entry *ce = istate->cache[i];
2431afbf
NTND
1425
1426 if (select_flag && !(ce->ce_flags & select_flag))
1427 continue;
1428
b33fdfc3 1429 if (!ce_stage(ce) && !(ce->ce_flags & CE_CONFLICTED))
2431afbf
NTND
1430 ce->ce_flags |= skip_wt_flag;
1431 else
1432 ce->ce_flags &= ~skip_wt_flag;
1433 }
9037026d
NTND
1434
1435 /*
1436 * 2. Widen worktree according to sparse-checkout file.
1437 * Matched entries will have skip_wt_flag cleared (i.e. "in")
1438 */
27c82fb3 1439 clear_ce_flags(istate, select_flag, skip_wt_flag, el);
2431afbf
NTND
1440}
1441
eb9ae4b5
RS
1442static int verify_absent(const struct cache_entry *,
1443 enum unpack_trees_error_types,
1444 struct unpack_trees_options *);
2e2b887d
JH
1445/*
1446 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the
1447 * resulting index, -2 on failure to reflect the changes to the work tree.
2431afbf
NTND
1448 *
1449 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally
2e2b887d 1450 */
01904572
LT
1451int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
1452{
e800ec9d 1453 int i, ret;
0fb1eaa8 1454 static struct cache_entry *dfc;
08aefc9e 1455 struct exclude_list el;
16da134b 1456
ca885a4f
JH
1457 if (len > MAX_UNPACK_TREES)
1458 die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES);
16da134b 1459
0d1ed596 1460 trace_performance_enter();
08aefc9e
NTND
1461 memset(&el, 0, sizeof(el));
1462 if (!core_apply_sparse_checkout || !o->update)
1463 o->skip_sparse_checkout = 1;
1464 if (!o->skip_sparse_checkout) {
fcd12db6 1465 char *sparse = git_pathdup("info/sparse-checkout");
473e3930 1466 if (add_excludes_from_file_to_list(sparse, "", 0, &el, NULL) < 0)
08aefc9e
NTND
1467 o->skip_sparse_checkout = 1;
1468 else
1469 o->el = &el;
fcd12db6 1470 free(sparse);
08aefc9e
NTND
1471 }
1472
34110cd4 1473 memset(&o->result, 0, sizeof(o->result));
913e0e99 1474 o->result.initialized = 1;
da165f47
JH
1475 o->result.timestamp.sec = o->src_index->timestamp.sec;
1476 o->result.timestamp.nsec = o->src_index->timestamp.nsec;
9170c7ab 1477 o->result.version = o->src_index->version;
7db11830
EN
1478 if (!o->src_index->split_index) {
1479 o->result.split_index = NULL;
1480 } else if (o->src_index == o->dst_index) {
1481 /*
1482 * o->dst_index (and thus o->src_index) will be discarded
1483 * and overwritten with o->result at the end of this function,
1484 * so just use src_index's split_index to avoid having to
1485 * create a new one.
1486 */
1487 o->result.split_index = o->src_index->split_index;
5fc2fc8f 1488 o->result.split_index->refcount++;
7db11830
EN
1489 } else {
1490 o->result.split_index = init_split_index(&o->result);
1491 }
75691ea3 1492 oidcpy(&o->result.oid, &o->src_index->oid);
16da134b 1493 o->merge_size = len;
da165f47 1494 mark_all_ce_unused(o->src_index);
0fb1eaa8 1495
2431afbf
NTND
1496 /*
1497 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries
1498 */
1499 if (!o->skip_sparse_checkout)
1500 mark_new_skip_worktree(o->el, o->src_index, 0, CE_NEW_SKIP_WORKTREE);
1501
0fb1eaa8 1502 if (!dfc)
13494ed1 1503 dfc = xcalloc(1, cache_entry_size(0));
0fb1eaa8 1504 o->df_conflict_entry = dfc;
16da134b
JS
1505
1506 if (len) {
01904572
LT
1507 const char *prefix = o->prefix ? o->prefix : "";
1508 struct traverse_info info;
1509
1510 setup_traverse_info(&info, prefix);
1511 info.fn = unpack_callback;
1512 info.data = o;
e6c111b4 1513 info.show_all_errors = o->show_all_errors;
40e37256 1514 info.pathspec = o->pathspec;
01904572 1515
da165f47
JH
1516 if (o->prefix) {
1517 /*
1518 * Unpack existing index entries that sort before the
1519 * prefix the tree is spliced into. Note that o->merge
1520 * is always true in this case.
1521 */
1522 while (1) {
1523 struct cache_entry *ce = next_cache_entry(o);
1524 if (!ce)
1525 break;
1526 if (ce_in_traverse_path(ce, &info))
1527 break;
1528 if (unpack_index_entry(ce, o) < 0)
1529 goto return_failed;
1530 }
08aefc9e 1531 }
da165f47 1532
0d1ed596 1533 trace_performance_enter();
67022e02 1534 ret = traverse_trees(o->src_index, len, t, &info);
0d1ed596
NTND
1535 trace_performance_leave("traverse_trees");
1536 if (ret < 0)
da165f47 1537 goto return_failed;
16da134b
JS
1538 }
1539
01904572
LT
1540 /* Any left-over entries in the index? */
1541 if (o->merge) {
da165f47
JH
1542 while (1) {
1543 struct cache_entry *ce = next_cache_entry(o);
1544 if (!ce)
1545 break;
01904572 1546 if (unpack_index_entry(ce, o) < 0)
da165f47 1547 goto return_failed;
17e46426 1548 }
17e46426 1549 }
da165f47 1550 mark_all_ce_unused(o->src_index);
16da134b 1551
08aefc9e
NTND
1552 if (o->trivial_merges_only && o->nontrivial_merge) {
1553 ret = unpack_failed(o, "Merge requires file-level merging");
1554 goto done;
1555 }
01904572 1556
e800ec9d 1557 if (!o->skip_sparse_checkout) {
9e1afb16 1558 int empty_worktree = 1;
2431afbf
NTND
1559
1560 /*
1561 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #1
1562 * If the will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE
1563 * so apply_sparse_checkout() won't attempt to remove it from worktree
1564 */
1565 mark_new_skip_worktree(o->el, &o->result, CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);
1566
17d26a4d 1567 ret = 0;
2431afbf 1568 for (i = 0; i < o->result.cache_nr; i++) {
e800ec9d
NTND
1569 struct cache_entry *ce = o->result.cache[i];
1570
2431afbf
NTND
1571 /*
1572 * Entries marked with CE_ADDED in merged_entry() do not have
1573 * verify_absent() check (the check is effectively disabled
1574 * because CE_NEW_SKIP_WORKTREE is set unconditionally).
1575 *
1576 * Do the real check now because we have had
1577 * correct CE_NEW_SKIP_WORKTREE
1578 */
1579 if (ce->ce_flags & CE_ADDED &&
17d26a4d
NTND
1580 verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {
1581 if (!o->show_all_errors)
1582 goto return_failed;
1583 ret = -1;
1584 }
2431afbf 1585
a5c446f1 1586 if (apply_sparse_checkout(&o->result, ce, o)) {
17d26a4d
NTND
1587 if (!o->show_all_errors)
1588 goto return_failed;
e800ec9d 1589 ret = -1;
e800ec9d 1590 }
eec3fc03 1591 if (!ce_skip_worktree(ce))
9e1afb16 1592 empty_worktree = 0;
f1f523ea 1593
e800ec9d 1594 }
17d26a4d
NTND
1595 if (ret < 0)
1596 goto return_failed;
a7bc906f
NTND
1597 /*
1598 * Sparse checkout is meant to narrow down checkout area
1599 * but it does not make sense to narrow down to empty working
1600 * tree. This is usually a mistake in sparse checkout rules.
1601 * Do not allow users to do that.
1602 */
9e1afb16
NTND
1603 if (o->result.cache_nr && empty_worktree) {
1604 ret = unpack_failed(o, "Sparse checkout leaves no entry on working directory");
1605 goto done;
1606 }
e800ec9d 1607 }
01904572 1608
30ac275b 1609 ret = check_updates(o) ? (-2) : 0;
e28f7641 1610 if (o->dst_index) {
836ef2b6 1611 move_index_extensions(&o->result, o->src_index);
52fca218 1612 if (!ret) {
4592e608 1613 if (git_env_bool("GIT_TEST_CHECK_CACHE_TREE", 0))
c207e9e1 1614 cache_tree_verify(the_repository, &o->result);
52fca218
BD
1615 if (!o->result.cache_tree)
1616 o->result.cache_tree = cache_tree();
1617 if (!cache_tree_fully_valid(o->result.cache_tree))
1618 cache_tree_update(&o->result,
1619 WRITE_TREE_SILENT |
1620 WRITE_TREE_REPAIR);
1621 }
e28f7641 1622 discard_index(o->dst_index);
34110cd4 1623 *o->dst_index = o->result;
a16cc8b2
JH
1624 } else {
1625 discard_index(&o->result);
e28f7641 1626 }
7db11830 1627 o->src_index = NULL;
08aefc9e
NTND
1628
1629done:
0d1ed596 1630 trace_performance_leave("unpack_trees");
f6198812 1631 clear_exclude_list(&el);
2e2b887d 1632 return ret;
da165f47
JH
1633
1634return_failed:
e6c111b4
MM
1635 if (o->show_all_errors)
1636 display_error_msgs(o);
da165f47 1637 mark_all_ce_unused(o->src_index);
026680f8 1638 ret = unpack_failed(o, NULL);
b4194828
JH
1639 if (o->exiting_early)
1640 ret = 0;
026680f8 1641 goto done;
16da134b 1642}
076b0adc
JS
1643
1644/* Here come the merge functions */
1645
eb9ae4b5
RS
1646static int reject_merge(const struct cache_entry *ce,
1647 struct unpack_trees_options *o)
076b0adc 1648{
6a143aa2
JN
1649 return o->gently ? -1 :
1650 add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name);
076b0adc
JS
1651}
1652
eb9ae4b5 1653static int same(const struct cache_entry *a, const struct cache_entry *b)
076b0adc
JS
1654{
1655 if (!!a != !!b)
1656 return 0;
1657 if (!a && !b)
1658 return 1;
e11d7b59
JH
1659 if ((a->ce_flags | b->ce_flags) & CE_CONFLICTED)
1660 return 0;
076b0adc 1661 return a->ce_mode == b->ce_mode &&
4a7e27e9 1662 oideq(&a->oid, &b->oid);
076b0adc
JS
1663}
1664
1665
1666/*
1667 * When a CE gets turned into an unmerged entry, we
1668 * want it to be up-to-date
1669 */
eb9ae4b5
RS
1670static int verify_uptodate_1(const struct cache_entry *ce,
1671 struct unpack_trees_options *o,
1672 enum unpack_trees_error_types error_type)
076b0adc
JS
1673{
1674 struct stat st;
1675
d5b66299
NTND
1676 if (o->index_only)
1677 return 0;
1678
1679 /*
1680 * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again
1681 * if this entry is truly up-to-date because this file may be
1682 * overwritten.
1683 */
1684 if ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce))
1685 ; /* keep checking */
1686 else if (o->reset || ce_uptodate(ce))
203a2fe1 1687 return 0;
076b0adc
JS
1688
1689 if (!lstat(ce->name, &st)) {
d5b66299
NTND
1690 int flags = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE;
1691 unsigned changed = ie_match_stat(o->src_index, ce, &st, flags);
a7bc845a
SB
1692
1693 if (submodule_from_ce(ce)) {
1694 int r = check_submodule_move_head(ce,
1695 "HEAD", oid_to_hex(&ce->oid), o);
1696 if (r)
1697 return o->gently ? -1 :
1698 add_rejected_path(o, error_type, ce->name);
1699 return 0;
1700 }
1701
076b0adc 1702 if (!changed)
203a2fe1 1703 return 0;
936492d3 1704 /*
a7bc845a
SB
1705 * Historic default policy was to allow submodule to be out
1706 * of sync wrt the superproject index. If the submodule was
1707 * not considered interesting above, we don't care here.
936492d3 1708 */
7a51ed66 1709 if (S_ISGITLINK(ce->ce_mode))
203a2fe1 1710 return 0;
a7bc845a 1711
076b0adc
JS
1712 errno = 0;
1713 }
076b0adc 1714 if (errno == ENOENT)
203a2fe1 1715 return 0;
17e46426 1716 return o->gently ? -1 :
e6c111b4 1717 add_rejected_path(o, error_type, ce->name);
35a5aa79
NTND
1718}
1719
64b1abe9
EN
1720int verify_uptodate(const struct cache_entry *ce,
1721 struct unpack_trees_options *o)
35a5aa79 1722{
2431afbf 1723 if (!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))
f1f523ea 1724 return 0;
08402b04 1725 return verify_uptodate_1(ce, o, ERROR_NOT_UPTODATE_FILE);
076b0adc
JS
1726}
1727
eb9ae4b5 1728static int verify_uptodate_sparse(const struct cache_entry *ce,
e800ec9d
NTND
1729 struct unpack_trees_options *o)
1730{
08402b04 1731 return verify_uptodate_1(ce, o, ERROR_SPARSE_NOT_UPTODATE_FILE);
076b0adc
JS
1732}
1733
383480ba
NTND
1734/*
1735 * TODO: We should actually invalidate o->result, not src_index [1].
1736 * But since cache tree and untracked cache both are not copied to
1737 * o->result until unpacking is complete, we invalidate them on
1738 * src_index instead with the assumption that they will be copied to
1739 * dst_index at the end.
1740 *
1741 * [1] src_index->cache_tree is also used in unpack_callback() so if
1742 * we invalidate o->result, we need to update it to use
1743 * o->result.cache_tree as well.
1744 */
eb9ae4b5
RS
1745static void invalidate_ce_path(const struct cache_entry *ce,
1746 struct unpack_trees_options *o)
076b0adc 1747{
e931371a
NTND
1748 if (!ce)
1749 return;
1750 cache_tree_invalidate_path(o->src_index, ce->name);
0cacebf0 1751 untracked_cache_invalidate_path(o->src_index, ce->name, 1);
076b0adc
JS
1752}
1753
0cf73755
SV
1754/*
1755 * Check that checking out ce->sha1 in subdir ce->name is not
1756 * going to overwrite any working files.
1757 *
1758 * Currently, git does not checkout subprojects during a superproject
1759 * checkout, so it is not going to overwrite anything.
1760 */
d6b12300
SB
1761static int verify_clean_submodule(const char *old_sha1,
1762 const struct cache_entry *ce,
eb9ae4b5
RS
1763 enum unpack_trees_error_types error_type,
1764 struct unpack_trees_options *o)
0cf73755 1765{
a7bc845a
SB
1766 if (!submodule_from_ce(ce))
1767 return 0;
1768
1769 return check_submodule_move_head(ce, old_sha1,
1770 oid_to_hex(&ce->oid), o);
0cf73755
SV
1771}
1772
eb9ae4b5
RS
1773static int verify_clean_subdirectory(const struct cache_entry *ce,
1774 enum unpack_trees_error_types error_type,
1775 struct unpack_trees_options *o)
c8193534
JH
1776{
1777 /*
0cf73755 1778 * we are about to extract "ce->name"; we would not want to lose
c8193534
JH
1779 * anything in the existing directory there.
1780 */
1781 int namelen;
7b9e3ce0 1782 int i;
c8193534
JH
1783 struct dir_struct d;
1784 char *pathbuf;
1785 int cnt = 0;
0cf73755 1786
d6b12300 1787 if (S_ISGITLINK(ce->ce_mode)) {
1053fe82 1788 struct object_id oid;
a98e6101 1789 int sub_head = resolve_gitlink_ref(ce->name, "HEAD", &oid);
d6b12300
SB
1790 /*
1791 * If we are not going to update the submodule, then
0cf73755
SV
1792 * we don't care.
1793 */
4a7e27e9 1794 if (!sub_head && oideq(&oid, &ce->oid))
0cf73755 1795 return 0;
1053fe82 1796 return verify_clean_submodule(sub_head ? NULL : oid_to_hex(&oid),
d6b12300 1797 ce, error_type, o);
0cf73755 1798 }
c8193534
JH
1799
1800 /*
1801 * First let's make sure we do not have a local modification
1802 * in that directory.
1803 */
68c4f6a5 1804 namelen = ce_namelen(ce);
da165f47
JH
1805 for (i = locate_in_src_index(ce, o);
1806 i < o->src_index->cache_nr;
1807 i++) {
837e5fe9
CB
1808 struct cache_entry *ce2 = o->src_index->cache[i];
1809 int len = ce_namelen(ce2);
c8193534 1810 if (len < namelen ||
837e5fe9
CB
1811 strncmp(ce->name, ce2->name, namelen) ||
1812 ce2->name[namelen] != '/')
c8193534
JH
1813 break;
1814 /*
da165f47
JH
1815 * ce2->name is an entry in the subdirectory to be
1816 * removed.
c8193534 1817 */
837e5fe9
CB
1818 if (!ce_stage(ce2)) {
1819 if (verify_uptodate(ce2, o))
203a2fe1 1820 return -1;
837e5fe9 1821 add_entry(o, ce2, CE_REMOVE, 0);
5697ca9a 1822 invalidate_ce_path(ce, o);
da165f47 1823 mark_ce_used(ce2, o);
c8193534
JH
1824 }
1825 cnt++;
1826 }
1827
1828 /*
1829 * Then we need to make sure that we do not lose a locally
1830 * present file that is not ignored.
1831 */
75faa45a 1832 pathbuf = xstrfmt("%.*s/", namelen, ce->name);
c8193534
JH
1833
1834 memset(&d, 0, sizeof(d));
1835 if (o->dir)
1836 d.exclude_per_dir = o->dir->exclude_per_dir;
c7f3259d 1837 i = read_directory(&d, o->src_index, pathbuf, namelen+1, NULL);
c8193534 1838 if (i)
17e46426 1839 return o->gently ? -1 :
e6c111b4 1840 add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);
c8193534
JH
1841 free(pathbuf);
1842 return cnt;
1843}
1844
32260ad5
LT
1845/*
1846 * This gets called when there was no index entry for the tree entry 'dst',
1847 * but we found a file in the working tree that 'lstat()' said was fine,
1848 * and we're on a case-insensitive filesystem.
1849 *
1850 * See if we can find a case-insensitive match in the index that also
1851 * matches the stat information, and assume it's that other file!
1852 */
a9307f5a 1853static int icase_exists(struct unpack_trees_options *o, const char *name, int len, struct stat *st)
32260ad5 1854{
9c5e6c80 1855 const struct cache_entry *src;
32260ad5 1856
ebbd7439 1857 src = index_file_exists(o->src_index, name, len, 1);
56cac48c 1858 return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
32260ad5
LT
1859}
1860
a9307f5a 1861static int check_ok_to_remove(const char *name, int len, int dtype,
eb9ae4b5 1862 const struct cache_entry *ce, struct stat *st,
a9307f5a
CB
1863 enum unpack_trees_error_types error_type,
1864 struct unpack_trees_options *o)
1865{
9c5e6c80 1866 const struct cache_entry *result;
a9307f5a
CB
1867
1868 /*
1869 * It may be that the 'lstat()' succeeded even though
1870 * target 'ce' was absent, because there is an old
1871 * entry that is different only in case..
1872 *
1873 * Ignore that lstat() if it matches.
1874 */
1875 if (ignore_case && icase_exists(o, name, len, st))
1876 return 0;
1877
589570db 1878 if (o->dir &&
c7f3259d 1879 is_excluded(o->dir, o->src_index, name, &dtype))
a9307f5a
CB
1880 /*
1881 * ce->name is explicitly excluded, so it is Ok to
1882 * overwrite it.
1883 */
1884 return 0;
1885 if (S_ISDIR(st->st_mode)) {
1886 /*
1887 * We are checking out path "foo" and
1888 * found "foo/." in the working tree.
1889 * This is tricky -- if we have modified
1890 * files that are in "foo/" we would lose
1891 * them.
1892 */
1893 if (verify_clean_subdirectory(ce, error_type, o) < 0)
1894 return -1;
1895 return 0;
1896 }
1897
1898 /*
1899 * The previous round may already have decided to
1900 * delete this path, which is in a subdirectory that
1901 * is being replaced with a blob.
1902 */
ebbd7439 1903 result = index_file_exists(&o->result, name, len, 0);
a9307f5a
CB
1904 if (result) {
1905 if (result->ce_flags & CE_REMOVE)
1906 return 0;
1907 }
1908
1909 return o->gently ? -1 :
1910 add_rejected_path(o, error_type, name);
1911}
1912
076b0adc
JS
1913/*
1914 * We do not want to remove or overwrite a working tree file that
f8a9d428 1915 * is not tracked, unless it is ignored.
076b0adc 1916 */
eb9ae4b5
RS
1917static int verify_absent_1(const struct cache_entry *ce,
1918 enum unpack_trees_error_types error_type,
1919 struct unpack_trees_options *o)
076b0adc 1920{
f66caaf9 1921 int len;
076b0adc
JS
1922 struct stat st;
1923
1924 if (o->index_only || o->reset || !o->update)
203a2fe1 1925 return 0;
c8193534 1926
f66caaf9
CB
1927 len = check_leading_path(ce->name, ce_namelen(ce));
1928 if (!len)
203a2fe1 1929 return 0;
f66caaf9 1930 else if (len > 0) {
f514ef97
JK
1931 char *path;
1932 int ret;
1933
1934 path = xmemdupz(ce->name, len);
a93e5301 1935 if (lstat(path, &st))
43c728e2 1936 ret = error_errno("cannot stat '%s'", path);
a7bc845a
SB
1937 else {
1938 if (submodule_from_ce(ce))
1939 ret = check_submodule_move_head(ce,
1940 oid_to_hex(&ce->oid),
1941 NULL, o);
1942 else
1943 ret = check_ok_to_remove(path, len, DT_UNKNOWN, NULL,
1944 &st, error_type, o);
1945 }
f514ef97
JK
1946 free(path);
1947 return ret;
92fda79e
JN
1948 } else if (lstat(ce->name, &st)) {
1949 if (errno != ENOENT)
43c728e2 1950 return error_errno("cannot stat '%s'", ce->name);
92fda79e
JN
1951 return 0;
1952 } else {
a7bc845a
SB
1953 if (submodule_from_ce(ce))
1954 return check_submodule_move_head(ce, oid_to_hex(&ce->oid),
1955 NULL, o);
1956
a9307f5a 1957 return check_ok_to_remove(ce->name, ce_namelen(ce),
92fda79e
JN
1958 ce_to_dtype(ce), ce, &st,
1959 error_type, o);
1960 }
076b0adc 1961}
a9307f5a 1962
eb9ae4b5 1963static int verify_absent(const struct cache_entry *ce,
08402b04 1964 enum unpack_trees_error_types error_type,
35a5aa79
NTND
1965 struct unpack_trees_options *o)
1966{
2431afbf 1967 if (!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))
f1f523ea 1968 return 0;
08402b04 1969 return verify_absent_1(ce, error_type, o);
35a5aa79 1970}
076b0adc 1971
eb9ae4b5
RS
1972static int verify_absent_sparse(const struct cache_entry *ce,
1973 enum unpack_trees_error_types error_type,
1974 struct unpack_trees_options *o)
e800ec9d 1975{
08402b04
MM
1976 enum unpack_trees_error_types orphaned_error = error_type;
1977 if (orphaned_error == ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN)
1978 orphaned_error = ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN;
1979
1980 return verify_absent_1(ce, orphaned_error, o);
e800ec9d 1981}
076b0adc 1982
f2fa3542 1983static int merged_entry(const struct cache_entry *ce,
eb9ae4b5 1984 const struct cache_entry *old,
f2fa3542 1985 struct unpack_trees_options *o)
076b0adc 1986{
7f8ab8dc 1987 int update = CE_UPDATE;
8e72d675 1988 struct cache_entry *merge = dup_cache_entry(ce, &o->result);
7f8ab8dc 1989
e11d7b59 1990 if (!old) {
2431afbf
NTND
1991 /*
1992 * New index entries. In sparse checkout, the following
1993 * verify_absent() will be delayed until after
1994 * traverse_trees() finishes in unpack_trees(), then:
1995 *
1996 * - CE_NEW_SKIP_WORKTREE will be computed correctly
1997 * - verify_absent() be called again, this time with
1998 * correct CE_NEW_SKIP_WORKTREE
1999 *
2000 * verify_absent() call here does nothing in sparse
2001 * checkout (i.e. o->skip_sparse_checkout == 0)
2002 */
2003 update |= CE_ADDED;
2004 merge->ce_flags |= CE_NEW_SKIP_WORKTREE;
2005
f2fa3542
RS
2006 if (verify_absent(merge,
2007 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {
a849735b 2008 discard_cache_entry(merge);
e11d7b59 2009 return -1;
f2fa3542 2010 }
e11d7b59 2011 invalidate_ce_path(merge, o);
a7bc845a
SB
2012
2013 if (submodule_from_ce(ce)) {
2014 int ret = check_submodule_move_head(ce, NULL,
2015 oid_to_hex(&ce->oid),
2016 o);
2017 if (ret)
2018 return ret;
2019 }
2020
e11d7b59 2021 } else if (!(old->ce_flags & CE_CONFLICTED)) {
076b0adc
JS
2022 /*
2023 * See if we can re-use the old CE directly?
2024 * That way we get the uptodate stat info.
2025 *
7f8ab8dc
LT
2026 * This also removes the UPDATE flag on a match; otherwise
2027 * we will end up overwriting local changes in the work tree.
076b0adc
JS
2028 */
2029 if (same(old, merge)) {
eb7a2f1d 2030 copy_cache_entry(merge, old);
7f8ab8dc 2031 update = 0;
076b0adc 2032 } else {
f2fa3542 2033 if (verify_uptodate(old, o)) {
a849735b 2034 discard_cache_entry(merge);
203a2fe1 2035 return -1;
f2fa3542 2036 }
2431afbf
NTND
2037 /* Migrate old flags over */
2038 update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);
bc052d7f 2039 invalidate_ce_path(old, o);
076b0adc 2040 }
a7bc845a
SB
2041
2042 if (submodule_from_ce(ce)) {
2043 int ret = check_submodule_move_head(ce, oid_to_hex(&old->oid),
2044 oid_to_hex(&ce->oid),
2045 o);
2046 if (ret)
2047 return ret;
2048 }
e11d7b59
JH
2049 } else {
2050 /*
2051 * Previously unmerged entry left as an existence
2052 * marker by read_index_unmerged();
2053 */
2054 invalidate_ce_path(old, o);
076b0adc
JS
2055 }
2056
f2fa3542 2057 do_add_entry(o, merge, update, CE_STAGEMASK);
076b0adc
JS
2058 return 1;
2059}
2060
eb9ae4b5
RS
2061static int deleted_entry(const struct cache_entry *ce,
2062 const struct cache_entry *old,
2063 struct unpack_trees_options *o)
076b0adc 2064{
34110cd4
LT
2065 /* Did it exist in the index? */
2066 if (!old) {
08402b04 2067 if (verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))
203a2fe1 2068 return -1;
34110cd4
LT
2069 return 0;
2070 }
e11d7b59 2071 if (!(old->ce_flags & CE_CONFLICTED) && verify_uptodate(old, o))
34110cd4
LT
2072 return -1;
2073 add_entry(o, ce, CE_REMOVE, 0);
bc052d7f 2074 invalidate_ce_path(ce, o);
076b0adc
JS
2075 return 1;
2076}
2077
eb9ae4b5
RS
2078static int keep_entry(const struct cache_entry *ce,
2079 struct unpack_trees_options *o)
076b0adc 2080{
34110cd4 2081 add_entry(o, ce, 0, 0);
5697ca9a
NTND
2082 if (ce_stage(ce))
2083 invalidate_ce_path(ce, o);
076b0adc
JS
2084 return 1;
2085}
2086
2087#if DBRT_DEBUG
2088static void show_stage_entry(FILE *o,
2089 const char *label, const struct cache_entry *ce)
2090{
2091 if (!ce)
2092 fprintf(o, "%s (missing)\n", label);
2093 else
2094 fprintf(o, "%s%06o %s %d\t%s\n",
2095 label,
7a51ed66 2096 ce->ce_mode,
99d1a986 2097 oid_to_hex(&ce->oid),
076b0adc
JS
2098 ce_stage(ce),
2099 ce->name);
2100}
2101#endif
2102
5828e835
RS
2103int threeway_merge(const struct cache_entry * const *stages,
2104 struct unpack_trees_options *o)
076b0adc 2105{
eb9ae4b5
RS
2106 const struct cache_entry *index;
2107 const struct cache_entry *head;
2108 const struct cache_entry *remote = stages[o->head_idx + 1];
076b0adc
JS
2109 int count;
2110 int head_match = 0;
2111 int remote_match = 0;
076b0adc
JS
2112
2113 int df_conflict_head = 0;
2114 int df_conflict_remote = 0;
2115
2116 int any_anc_missing = 0;
2117 int no_anc_exists = 1;
2118 int i;
2119
2120 for (i = 1; i < o->head_idx; i++) {
4c4caafc 2121 if (!stages[i] || stages[i] == o->df_conflict_entry)
076b0adc 2122 any_anc_missing = 1;
4c4caafc 2123 else
076b0adc 2124 no_anc_exists = 0;
076b0adc
JS
2125 }
2126
2127 index = stages[0];
2128 head = stages[o->head_idx];
2129
2130 if (head == o->df_conflict_entry) {
2131 df_conflict_head = 1;
2132 head = NULL;
2133 }
2134
2135 if (remote == o->df_conflict_entry) {
2136 df_conflict_remote = 1;
2137 remote = NULL;
2138 }
2139
cee2d6ae
JH
2140 /*
2141 * First, if there's a #16 situation, note that to prevent #13
076b0adc
JS
2142 * and #14.
2143 */
2144 if (!same(remote, head)) {
2145 for (i = 1; i < o->head_idx; i++) {
2146 if (same(stages[i], head)) {
2147 head_match = i;
2148 }
2149 if (same(stages[i], remote)) {
2150 remote_match = i;
2151 }
2152 }
2153 }
2154
cee2d6ae
JH
2155 /*
2156 * We start with cases where the index is allowed to match
076b0adc
JS
2157 * something other than the head: #14(ALT) and #2ALT, where it
2158 * is permitted to match the result instead.
2159 */
2160 /* #14, #14ALT, #2ALT */
2161 if (remote && !df_conflict_head && head_match && !remote_match) {
2162 if (index && !same(index, remote) && !same(index, head))
6a143aa2 2163 return reject_merge(index, o);
076b0adc
JS
2164 return merged_entry(remote, index, o);
2165 }
2166 /*
2167 * If we have an entry in the index cache, then we want to
2168 * make sure that it matches head.
2169 */
4e7c4571 2170 if (index && !same(index, head))
6a143aa2 2171 return reject_merge(index, o);
076b0adc
JS
2172
2173 if (head) {
2174 /* #5ALT, #15 */
2175 if (same(head, remote))
2176 return merged_entry(head, index, o);
2177 /* #13, #3ALT */
2178 if (!df_conflict_remote && remote_match && !head_match)
2179 return merged_entry(head, index, o);
2180 }
2181
2182 /* #1 */
34110cd4 2183 if (!head && !remote && any_anc_missing)
076b0adc
JS
2184 return 0;
2185
cee2d6ae
JH
2186 /*
2187 * Under the "aggressive" rule, we resolve mostly trivial
076b0adc
JS
2188 * cases that we historically had git-merge-one-file resolve.
2189 */
2190 if (o->aggressive) {
cee2d6ae
JH
2191 int head_deleted = !head;
2192 int remote_deleted = !remote;
eb9ae4b5 2193 const struct cache_entry *ce = NULL;
4c4caafc
JH
2194
2195 if (index)
0cf73755 2196 ce = index;
4c4caafc 2197 else if (head)
0cf73755 2198 ce = head;
4c4caafc 2199 else if (remote)
0cf73755 2200 ce = remote;
4c4caafc
JH
2201 else {
2202 for (i = 1; i < o->head_idx; i++) {
2203 if (stages[i] && stages[i] != o->df_conflict_entry) {
0cf73755 2204 ce = stages[i];
4c4caafc
JH
2205 break;
2206 }
2207 }
2208 }
2209
076b0adc
JS
2210 /*
2211 * Deleted in both.
2212 * Deleted in one and unchanged in the other.
2213 */
2214 if ((head_deleted && remote_deleted) ||
2215 (head_deleted && remote && remote_match) ||
2216 (remote_deleted && head && head_match)) {
2217 if (index)
2218 return deleted_entry(index, index, o);
34110cd4 2219 if (ce && !head_deleted) {
08402b04 2220 if (verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))
203a2fe1
DB
2221 return -1;
2222 }
076b0adc
JS
2223 return 0;
2224 }
2225 /*
2226 * Added in both, identically.
2227 */
2228 if (no_anc_exists && head && remote && same(head, remote))
2229 return merged_entry(head, index, o);
2230
2231 }
2232
2233 /* Below are "no merge" cases, which require that the index be
2234 * up-to-date to avoid the files getting overwritten with
2235 * conflict resolution files.
2236 */
2237 if (index) {
203a2fe1
DB
2238 if (verify_uptodate(index, o))
2239 return -1;
076b0adc 2240 }
076b0adc
JS
2241
2242 o->nontrivial_merge = 1;
2243
ea4b52a8 2244 /* #2, #3, #4, #6, #7, #9, #10, #11. */
076b0adc
JS
2245 count = 0;
2246 if (!head_match || !remote_match) {
2247 for (i = 1; i < o->head_idx; i++) {
4c4caafc 2248 if (stages[i] && stages[i] != o->df_conflict_entry) {
7f7932ab 2249 keep_entry(stages[i], o);
076b0adc
JS
2250 count++;
2251 break;
2252 }
2253 }
2254 }
2255#if DBRT_DEBUG
2256 else {
2257 fprintf(stderr, "read-tree: warning #16 detected\n");
2258 show_stage_entry(stderr, "head ", stages[head_match]);
2259 show_stage_entry(stderr, "remote ", stages[remote_match]);
2260 }
2261#endif
7f7932ab
JH
2262 if (head) { count += keep_entry(head, o); }
2263 if (remote) { count += keep_entry(remote, o); }
076b0adc
JS
2264 return count;
2265}
2266
2267/*
2268 * Two-way merge.
2269 *
2270 * The rule is to "carry forward" what is in the index without losing
a75d7b54 2271 * information across a "fast-forward", favoring a successful merge
076b0adc
JS
2272 * over a merge failure when it makes sense. For details of the
2273 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
2274 *
2275 */
5828e835
RS
2276int twoway_merge(const struct cache_entry * const *src,
2277 struct unpack_trees_options *o)
076b0adc 2278{
eb9ae4b5
RS
2279 const struct cache_entry *current = src[0];
2280 const struct cache_entry *oldtree = src[1];
2281 const struct cache_entry *newtree = src[2];
076b0adc
JS
2282
2283 if (o->merge_size != 2)
2284 return error("Cannot do a twoway merge of %d trees",
2285 o->merge_size);
2286
b8ba1535
JH
2287 if (oldtree == o->df_conflict_entry)
2288 oldtree = NULL;
2289 if (newtree == o->df_conflict_entry)
2290 newtree = NULL;
2291
076b0adc 2292 if (current) {
b018ff60
JK
2293 if (current->ce_flags & CE_CONFLICTED) {
2294 if (same(oldtree, newtree) || o->reset) {
2295 if (!newtree)
2296 return deleted_entry(current, current, o);
2297 else
2298 return merged_entry(newtree, current, o);
2299 }
6a143aa2 2300 return reject_merge(current, o);
6c1db1b3 2301 } else if ((!oldtree && !newtree) || /* 4 and 5 */
b018ff60
JK
2302 (!oldtree && newtree &&
2303 same(current, newtree)) || /* 6 and 7 */
2304 (oldtree && newtree &&
2305 same(oldtree, newtree)) || /* 14 and 15 */
2306 (oldtree && newtree &&
2307 !same(oldtree, newtree) && /* 18 and 19 */
2308 same(current, newtree))) {
7f7932ab 2309 return keep_entry(current, o);
6c1db1b3 2310 } else if (oldtree && !newtree && same(current, oldtree)) {
076b0adc
JS
2311 /* 10 or 11 */
2312 return deleted_entry(oldtree, current, o);
6c1db1b3 2313 } else if (oldtree && newtree &&
076b0adc
JS
2314 same(current, oldtree) && !same(current, newtree)) {
2315 /* 20 or 21 */
2316 return merged_entry(newtree, current, o);
6c1db1b3 2317 } else
6a143aa2 2318 return reject_merge(current, o);
076b0adc 2319 }
55218834
JH
2320 else if (newtree) {
2321 if (oldtree && !o->initial_checkout) {
2322 /*
2323 * deletion of the path was staged;
2324 */
2325 if (same(oldtree, newtree))
2326 return 1;
2327 return reject_merge(oldtree, o);
2328 }
076b0adc 2329 return merged_entry(newtree, current, o);
55218834 2330 }
d699676d 2331 return deleted_entry(oldtree, current, o);
076b0adc
JS
2332}
2333
2334/*
2335 * Bind merge.
2336 *
2337 * Keep the index entries at stage0, collapse stage1 but make sure
2338 * stage0 does not have anything there.
2339 */
5828e835
RS
2340int bind_merge(const struct cache_entry * const *src,
2341 struct unpack_trees_options *o)
076b0adc 2342{
eb9ae4b5
RS
2343 const struct cache_entry *old = src[0];
2344 const struct cache_entry *a = src[1];
076b0adc
JS
2345
2346 if (o->merge_size != 1)
82247e9b 2347 return error("Cannot do a bind merge of %d trees",
076b0adc
JS
2348 o->merge_size);
2349 if (a && old)
17e46426 2350 return o->gently ? -1 :
3d415425
SB
2351 error(ERRORMSG(o, ERROR_BIND_OVERLAP),
2352 super_prefixed(a->name),
2353 super_prefixed(old->name));
076b0adc 2354 if (!a)
7f7932ab 2355 return keep_entry(old, o);
076b0adc
JS
2356 else
2357 return merged_entry(a, NULL, o);
2358}
2359
2360/*
2361 * One-way merge.
2362 *
2363 * The rule is:
2364 * - take the stat information from stage0, take the data from stage1
2365 */
5828e835
RS
2366int oneway_merge(const struct cache_entry * const *src,
2367 struct unpack_trees_options *o)
076b0adc 2368{
eb9ae4b5
RS
2369 const struct cache_entry *old = src[0];
2370 const struct cache_entry *a = src[1];
076b0adc
JS
2371
2372 if (o->merge_size != 1)
2373 return error("Cannot do a oneway merge of %d trees",
2374 o->merge_size);
2375
78d3b06e 2376 if (!a || a == o->df_conflict_entry)
076b0adc 2377 return deleted_entry(old, old, o);
34110cd4 2378
076b0adc 2379 if (old && same(old, a)) {
34110cd4 2380 int update = 0;
686b2de0 2381 if (o->reset && o->update && !ce_uptodate(old) && !ce_skip_worktree(old)) {
076b0adc
JS
2382 struct stat st;
2383 if (lstat(old->name, &st) ||
56cac48c 2384 ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))
34110cd4 2385 update |= CE_UPDATE;
076b0adc 2386 }
ad17312e
SB
2387 if (o->update && S_ISGITLINK(old->ce_mode) &&
2388 should_update_submodules() && !verify_uptodate(old, o))
2389 update |= CE_UPDATE;
34110cd4
LT
2390 add_entry(o, old, update, 0);
2391 return 0;
076b0adc
JS
2392 }
2393 return merged_entry(a, old, o);
2394}