sparse-checkout: hold pattern list in index

As we modify the sparse-checkout definition, we perform index operations on a pattern_list that only exists in-memory. This allows easy backing out in case the index update fails. However, if the index write itself cares about the sparse-checkout pattern set, we need access to that in-memory copy. Place a pointer to a 'struct pattern_list' in the index so we can access this on-demand. This will be used in the next change which uses the sparse-checkout definition to filter out directories that are outside the sparse cone. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Mar 30, 2021 at 13:10 UTC 836e25c51b20c1f1d122dc17a1c57dad15ff5854
2 files changed +12 -7
builtin/sparse-checkout.c
+10 -7
@@ -110,6 +110,8 @@ static int update_working_directory(struct pattern_list *pl)
110 if (is_index_unborn(r->index))
111 return UPDATE_SPARSITY_SUCCESS;
112
113 + r->index->sparse_checkout_patterns = pl;
114 +
115 memset(&o, 0, sizeof(o));
116 o.verbose_update = isatty(2);
117 o.update = 1;
@@ -138,6 +140,7 @@ static int update_working_directory(struct pattern_list *pl)
140 else
141 rollback_lock_file(&lock_file);
142
143 + r->index->sparse_checkout_patterns = NULL;
144 return result;
145 }
146
@@ -517,19 +520,18 @@ static int modify_pattern_list(int argc, const char **argv, enum modify_type m)
520 {
521 int result;
522 int changed_config = 0;
520 - struct pattern_list pl;
521 - memset(&pl, 0, sizeof(pl));
523 + struct pattern_list *pl = xcalloc(1, sizeof(*pl));
524
525 switch (m) {
526 case ADD:
527 if (core_sparse_checkout_cone)
526 - add_patterns_cone_mode(argc, argv, &pl);
528 + add_patterns_cone_mode(argc, argv, pl);
529 else
528 - add_patterns_literal(argc, argv, &pl);
530 + add_patterns_literal(argc, argv, pl);
531 break;
532
533 case REPLACE:
532 - add_patterns_from_input(&pl, argc, argv);
534 + add_patterns_from_input(pl, argc, argv);
535 break;
536 }
537
@@ -539,12 +541,13 @@ static int modify_pattern_list(int argc, const char **argv, enum modify_type m)
541 changed_config = 1;
542 }
543
542 - result = write_patterns_and_update(&pl);
544 + result = write_patterns_and_update(pl);
545
546 if (result && changed_config)
547 set_config(MODE_NO_PATTERNS);
548
547 - clear_pattern_list(&pl);
549 + clear_pattern_list(pl);
550 + free(pl);
551 return result;
552 }
553
cache.h
+2
@@ -307,6 +307,7 @@ static inline unsigned int canon_mode(unsigned int mode)
307 struct split_index;
308 struct untracked_cache;
309 struct progress;
310 +struct pattern_list;
311
312 struct index_state {
313 struct cache_entry **cache;
@@ -338,6 +339,7 @@ struct index_state {
339 struct mem_pool *ce_mem_pool;
340 struct progress *progress;
341 struct repository *repo;
342 + struct pattern_list *sparse_checkout_patterns;
343 };
344
345 /* Name hashing */