]> git.ipfire.org Git - thirdparty/git.git/commitdiff
sparse-checkout: pass use_stdin as a parameter instead of as a global
authorElijah Newren <newren@gmail.com>
Tue, 14 Dec 2021 04:09:03 +0000 (04:09 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 15 Dec 2021 19:48:21 +0000 (11:48 -0800)
add_patterns_from_input() has relied on a global variable,
set_opts.use_stdin, which has been used by both the `set` and `add`
subcommands of sparse-checkout.  Once we introduce an
add_opts.use_stdin, the hardcoding of set_opts.use_stdin will be
incorrect.  Pass the value as function parameter instead to allow us to
make subsequent changes.

Reviewed-by: Derrick Stolee <dstolee@microsoft.com>
Reviewed-by: Victoria Dye <vdye@github.com>
Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/sparse-checkout.c

index d0f5c4702be69d0c9fade08ffbf5183c5f3dbe7e..8612328e5dd50ffb73ee4f4d368544c76e98534d 100644 (file)
@@ -525,7 +525,8 @@ static struct sparse_checkout_set_opts {
 } set_opts;
 
 static void add_patterns_from_input(struct pattern_list *pl,
-                                   int argc, const char **argv)
+                                   int argc, const char **argv,
+                                   int use_stdin)
 {
        int i;
        if (core_sparse_checkout_cone) {
@@ -535,7 +536,7 @@ static void add_patterns_from_input(struct pattern_list *pl,
                hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0);
                pl->use_cone_patterns = 1;
 
-               if (set_opts.use_stdin) {
+               if (use_stdin) {
                        struct strbuf unquoted = STRBUF_INIT;
                        while (!strbuf_getline(&line, stdin)) {
                                if (line.buf[0] == '"') {
@@ -559,7 +560,7 @@ static void add_patterns_from_input(struct pattern_list *pl,
                        }
                }
        } else {
-               if (set_opts.use_stdin) {
+               if (use_stdin) {
                        struct strbuf line = STRBUF_INIT;
 
                        while (!strbuf_getline(&line, stdin)) {
@@ -580,7 +581,8 @@ enum modify_type {
 };
 
 static void add_patterns_cone_mode(int argc, const char **argv,
-                                  struct pattern_list *pl)
+                                  struct pattern_list *pl,
+                                  int use_stdin)
 {
        struct strbuf buffer = STRBUF_INIT;
        struct pattern_entry *pe;
@@ -588,7 +590,7 @@ static void add_patterns_cone_mode(int argc, const char **argv,
        struct pattern_list existing;
        char *sparse_filename = get_sparse_checkout_filename();
 
-       add_patterns_from_input(pl, argc, argv);
+       add_patterns_from_input(pl, argc, argv, use_stdin);
 
        memset(&existing, 0, sizeof(existing));
        existing.use_cone_patterns = core_sparse_checkout_cone;
@@ -614,17 +616,19 @@ static void add_patterns_cone_mode(int argc, const char **argv,
 }
 
 static void add_patterns_literal(int argc, const char **argv,
-                                struct pattern_list *pl)
+                                struct pattern_list *pl,
+                                int use_stdin)
 {
        char *sparse_filename = get_sparse_checkout_filename();
        if (add_patterns_from_file_to_list(sparse_filename, "", 0,
                                           pl, NULL, 0))
                die(_("unable to load existing sparse-checkout patterns"));
        free(sparse_filename);
-       add_patterns_from_input(pl, argc, argv);
+       add_patterns_from_input(pl, argc, argv, use_stdin);
 }
 
-static int modify_pattern_list(int argc, const char **argv, enum modify_type m)
+static int modify_pattern_list(int argc, const char **argv, int use_stdin,
+                              enum modify_type m)
 {
        int result;
        int changed_config = 0;
@@ -633,13 +637,13 @@ static int modify_pattern_list(int argc, const char **argv, enum modify_type m)
        switch (m) {
        case ADD:
                if (core_sparse_checkout_cone)
-                       add_patterns_cone_mode(argc, argv, pl);
+                       add_patterns_cone_mode(argc, argv, pl, use_stdin);
                else
-                       add_patterns_literal(argc, argv, pl);
+                       add_patterns_literal(argc, argv, pl, use_stdin);
                break;
 
        case REPLACE:
-               add_patterns_from_input(pl, argc, argv);
+               add_patterns_from_input(pl, argc, argv, use_stdin);
                break;
        }
 
@@ -675,7 +679,7 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
                             builtin_sparse_checkout_set_usage,
                             PARSE_OPT_KEEP_UNKNOWN);
 
-       return modify_pattern_list(argc, argv, m);
+       return modify_pattern_list(argc, argv, set_opts.use_stdin, m);
 }
 
 static char const * const builtin_sparse_checkout_reapply_usage[] = {