sparse-checkout: load sparse-checkout patterns

A future feature will want to load the sparse-checkout patterns into a pattern_list, but the current mechanism to do so is a bit complicated. This is made difficult due to needing to find the sparse-checkout file in different ways throughout the codebase. The logic implemented in the new get_sparse_checkout_patterns() was duplicated in populate_from_existing_patterns() in unpack-trees.c. Use the new method instead, keeping the logic around handling the struct unpack_trees_options. The callers to get_sparse_checkout_filename() in builtin/sparse-checkout.c manipulate the sparse-checkout file directly, so it is not appropriate to replace logic in that file with get_sparse_checkout_patterns(). Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Jan 23, 2021 at 19:58 UTC dd23022acbf7433514e20a14a9f74e39fec9d340
4 files changed +20 -10
builtin/sparse-checkout.c
-5
@@ -22,11 +22,6 @@ static char const * const builtin_sparse_checkout_usage[] = {
22 NULL
23 };
24
25 -static char *get_sparse_checkout_filename(void)
26 -{
27 - return git_pathdup("info/sparse-checkout");
28 -}
29 -
25 static void write_patterns_to_file(FILE *fp, struct pattern_list *pl)
26 {
27 int i;
dir.c
+17
@@ -2998,6 +2998,23 @@ void setup_standard_excludes(struct dir_struct *dir)
2998 }
2999 }
3000
3001 +char *get_sparse_checkout_filename(void)
3002 +{
3003 + return git_pathdup("info/sparse-checkout");
3004 +}
3005 +
3006 +int get_sparse_checkout_patterns(struct pattern_list *pl)
3007 +{
3008 + int res;
3009 + char *sparse_filename = get_sparse_checkout_filename();
3010 +
3011 + pl->use_cone_patterns = core_sparse_checkout_cone;
3012 + res = add_patterns_from_file_to_list(sparse_filename, "", 0, pl, NULL);
3013 +
3014 + free(sparse_filename);
3015 + return res;
3016 +}
3017 +
3018 int remove_path(const char *name)
3019 {
3020 char *slash;
dir.h
+2
@@ -448,6 +448,8 @@ int is_empty_dir(const char *dir);
448
449 void setup_standard_excludes(struct dir_struct *dir);
450
451 +char *get_sparse_checkout_filename(void);
452 +int get_sparse_checkout_patterns(struct pattern_list *pl);
453
454 /* Constants for remove_dir_recursively: */
455
unpack-trees.c
+1 -5
@@ -1549,14 +1549,10 @@ static void mark_new_skip_worktree(struct pattern_list *pl,
1549 static void populate_from_existing_patterns(struct unpack_trees_options *o,
1550 struct pattern_list *pl)
1551 {
1552 - char *sparse = git_pathdup("info/sparse-checkout");
1553 -
1554 - pl->use_cone_patterns = core_sparse_checkout_cone;
1555 - if (add_patterns_from_file_to_list(sparse, "", 0, pl, NULL) < 0)
1552 + if (get_sparse_checkout_patterns(pl) < 0)
1553 o->skip_sparse_checkout = 1;
1554 else
1555 o->pl = pl;
1559 - free(sparse);
1556 }
1557
1558