Skip to content

Commit 50d89ad

Browse files
jlehmanngitster
authored andcommitted
submodule: use argv_array instead of hand-building arrays
fetch_populated_submodules() allocates the full argv array it uses to recurse into the submodules from the number of given options plus the six argv values it is going to add. It then initializes it with those values which won't change during the iteration and copies the given options into it. Inside the loop the two argv values different for each submodule get replaced with those currently valid. However, this technique is brittle and error-prone (as the comment to explain the magic number 6 indicates), so let's replace it with an argv_array. Instead of replacing the argv values, push them to the argv_array just before the run_command() call (including the option separating them) and pop them from the argv_array right after that. Signed-off-by: Jens Lehmann <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 85556d4 commit 50d89ad

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

builtin/fetch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
10121012
struct argv_array options = ARGV_ARRAY_INIT;
10131013

10141014
add_options_to_argv(&options);
1015-
result = fetch_populated_submodules(options.argc, options.argv,
1015+
result = fetch_populated_submodules(&options,
10161016
submodule_prefix,
10171017
recurse_submodules,
10181018
verbosity < 0);

submodule.c

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -586,13 +586,13 @@ static void calculate_changed_submodule_paths(void)
586586
initialized_fetch_ref_tips = 0;
587587
}
588588

589-
int fetch_populated_submodules(int num_options, const char **options,
589+
int fetch_populated_submodules(const struct argv_array *options,
590590
const char *prefix, int command_line_option,
591591
int quiet)
592592
{
593-
int i, result = 0, argc = 0, default_argc;
593+
int i, result = 0;
594594
struct child_process cp;
595-
const char **argv;
595+
struct argv_array argv = ARGV_ARRAY_INIT;
596596
struct string_list_item *name_for_path;
597597
const char *work_tree = get_git_work_tree();
598598
if (!work_tree)
@@ -602,17 +602,13 @@ int fetch_populated_submodules(int num_options, const char **options,
602602
if (read_cache() < 0)
603603
die("index file corrupt");
604604

605-
/* 6: "fetch" (options) --recurse-submodules-default default "--submodule-prefix" prefix NULL */
606-
argv = xcalloc(num_options + 6, sizeof(const char *));
607-
argv[argc++] = "fetch";
608-
for (i = 0; i < num_options; i++)
609-
argv[argc++] = options[i];
610-
argv[argc++] = "--recurse-submodules-default";
611-
default_argc = argc++;
612-
argv[argc++] = "--submodule-prefix";
605+
argv_array_push(&argv, "fetch");
606+
for (i = 0; i < options->argc; i++)
607+
argv_array_push(&argv, options->argv[i]);
608+
argv_array_push(&argv, "--recurse-submodules-default");
609+
/* default value, "--submodule-prefix" and its value are added later */
613610

614611
memset(&cp, 0, sizeof(cp));
615-
cp.argv = argv;
616612
cp.env = local_repo_env;
617613
cp.git_cmd = 1;
618614
cp.no_stdin = 1;
@@ -672,16 +668,21 @@ int fetch_populated_submodules(int num_options, const char **options,
672668
if (!quiet)
673669
printf("Fetching submodule %s%s\n", prefix, ce->name);
674670
cp.dir = submodule_path.buf;
675-
argv[default_argc] = default_argv;
676-
argv[argc] = submodule_prefix.buf;
671+
argv_array_push(&argv, default_argv);
672+
argv_array_push(&argv, "--submodule-prefix");
673+
argv_array_push(&argv, submodule_prefix.buf);
674+
cp.argv = argv.argv;
677675
if (run_command(&cp))
678676
result = 1;
677+
argv_array_pop(&argv);
678+
argv_array_pop(&argv);
679+
argv_array_pop(&argv);
679680
}
680681
strbuf_release(&submodule_path);
681682
strbuf_release(&submodule_git_dir);
682683
strbuf_release(&submodule_prefix);
683684
}
684-
free(argv);
685+
argv_array_clear(&argv);
685686
out:
686687
string_list_clear(&changed_submodule_paths, 1);
687688
return result;

submodule.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define SUBMODULE_H
33

44
struct diff_options;
5+
struct argv_array;
56

67
enum {
78
RECURSE_SUBMODULES_ON_DEMAND = -1,
@@ -23,7 +24,7 @@ void show_submodule_summary(FILE *f, const char *path,
2324
const char *del, const char *add, const char *reset);
2425
void set_config_fetch_recurse_submodules(int value);
2526
void check_for_new_submodule_commits(unsigned char new_sha1[20]);
26-
int fetch_populated_submodules(int num_options, const char **options,
27+
int fetch_populated_submodules(const struct argv_array *options,
2728
const char *prefix, int command_line_option,
2829
int quiet);
2930
unsigned is_submodule_modified(const char *path, int ignore_untracked);

0 commit comments

Comments
 (0)