From: Jens Lehmann <Jens.Lehmann@web.de>
To: Jeff King <peff@peff.net>
Cc: Dan Johnson <computerdruid@gmail.com>,
git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>,
Oswald Buddenhagen <ossi@kde.org>
Subject: [PATCH] submodule: use argv_array instead of hand-building arrays
Date: Sat, 01 Sep 2012 17:27:06 +0200 [thread overview]
Message-ID: <5042294A.7020507@web.de> (raw)
In-Reply-To: <50421CF8.60703@web.de>
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 <Jens.Lehmann@web.de>
---
Am 01.09.2012 16:34, schrieb Jens Lehmann:
> Am 01.09.2012 13:27, schrieb Jeff King:
>> It may be that fetch_populated_submodules would also benefit from
>> conversion (here I just pass in the argc and argv separately), but I
>> didn't look.
>
> Yes, it does some similar brittle stuff and should be changed to use
> the argv-array too. I'll look into that.
Maybe something like this on top of your two patches?
I thought about adding an argv_array_cat() function to replace the
for() loop copying the option values into the argv-array built inside
fetch_populated_submodules(), but I suspect saving one line from the
code is not worth it. Yet I didn't check if others would benefit from
such a function too.
builtin/fetch.c | 2 +-
submodule.c | 31 ++++++++++++++++---------------
submodule.h | 3 ++-
3 files changed, 19 insertions(+), 17 deletions(-)
diff --git a/builtin/fetch.c b/builtin/fetch.c
index b6a8be0..aaba61e 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -1012,7 +1012,7 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
struct argv_array options = ARGV_ARRAY_INIT;
add_options_to_argv(&options);
- result = fetch_populated_submodules(options.argc, options.argv,
+ result = fetch_populated_submodules(&options,
submodule_prefix,
recurse_submodules,
verbosity < 0);
diff --git a/submodule.c b/submodule.c
index 19dc6a6..51d48c2 100644
--- a/submodule.c
+++ b/submodule.c
@@ -588,13 +588,13 @@ static void calculate_changed_submodule_paths(void)
initialized_fetch_ref_tips = 0;
}
-int fetch_populated_submodules(int num_options, const char **options,
+int fetch_populated_submodules(const struct argv_array *options,
const char *prefix, int command_line_option,
int quiet)
{
- int i, result = 0, argc = 0, default_argc;
+ int i, result = 0;
struct child_process cp;
- const char **argv;
+ struct argv_array argv = ARGV_ARRAY_INIT;
struct string_list_item *name_for_path;
const char *work_tree = get_git_work_tree();
if (!work_tree)
@@ -604,17 +604,13 @@ int fetch_populated_submodules(int num_options, const char **options,
if (read_cache() < 0)
die("index file corrupt");
- /* 6: "fetch" (options) --recurse-submodules-default default "--submodule-prefix" prefix NULL */
- argv = xcalloc(num_options + 6, sizeof(const char *));
- argv[argc++] = "fetch";
- for (i = 0; i < num_options; i++)
- argv[argc++] = options[i];
- argv[argc++] = "--recurse-submodules-default";
- default_argc = argc++;
- argv[argc++] = "--submodule-prefix";
+ argv_array_push(&argv, "fetch");
+ for (i = 0; i < options->argc; i++)
+ argv_array_push(&argv, options->argv[i]);
+ argv_array_push(&argv, "--recurse-submodules-default");
+ /* default value, "--submodule-prefix" and its value are added later */
memset(&cp, 0, sizeof(cp));
- cp.argv = argv;
cp.env = local_repo_env;
cp.git_cmd = 1;
cp.no_stdin = 1;
@@ -674,16 +670,21 @@ int fetch_populated_submodules(int num_options, const char **options,
if (!quiet)
printf("Fetching submodule %s%s\n", prefix, ce->name);
cp.dir = submodule_path.buf;
- argv[default_argc] = default_argv;
- argv[argc] = submodule_prefix.buf;
+ argv_array_push(&argv, default_argv);
+ argv_array_push(&argv, "--submodule-prefix");
+ argv_array_push(&argv, submodule_prefix.buf);
+ cp.argv = argv.argv;
if (run_command(&cp))
result = 1;
+ argv_array_pop(&argv);
+ argv_array_pop(&argv);
+ argv_array_pop(&argv);
}
strbuf_release(&submodule_path);
strbuf_release(&submodule_git_dir);
strbuf_release(&submodule_prefix);
}
- free(argv);
+ argv_array_clear(&argv);
out:
string_list_clear(&changed_submodule_paths, 1);
return result;
diff --git a/submodule.h b/submodule.h
index e105b0e..594b50d 100644
--- a/submodule.h
+++ b/submodule.h
@@ -2,6 +2,7 @@
#define SUBMODULE_H
struct diff_options;
+struct argv_array;
enum {
RECURSE_SUBMODULES_ON_DEMAND = -1,
@@ -23,7 +24,7 @@ void show_submodule_summary(FILE *f, const char *path,
const char *del, const char *add, const char *reset);
void set_config_fetch_recurse_submodules(int value);
void check_for_new_submodule_commits(unsigned char new_sha1[20]);
-int fetch_populated_submodules(int num_options, const char **options,
+int fetch_populated_submodules(const struct argv_array *options,
const char *prefix, int command_line_option,
int quiet);
unsigned is_submodule_modified(const char *path, int ignore_untracked);
--
1.7.12.149.g47e61ec
next prev parent reply other threads:[~2012-09-01 15:27 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-08-05 4:56 Bringing a bit more sanity to $GIT_DIR/objects/info/alternates? Junio C Hamano
2012-08-05 9:38 ` Michael Haggerty
2012-08-05 19:01 ` Junio C Hamano
2012-08-07 6:16 ` Jeff King
2012-08-06 21:55 ` Junio C Hamano
2012-08-08 1:42 ` Sascha Cunz
2012-08-11 9:35 ` Hallvard Breien Furuseth
2012-08-27 22:39 ` Oswald Buddenhagen
2012-08-28 19:19 ` GC of alternate object store (was: Bringing a bit more sanity to $GIT_DIR/objects/info/alternates?) Hallvard Breien Furuseth
2012-08-29 7:42 ` Oswald Buddenhagen
2012-08-29 15:52 ` GC of alternate object store Junio C Hamano
2012-08-30 9:53 ` Oswald Buddenhagen
2012-08-30 16:03 ` Junio C Hamano
2012-08-31 16:26 ` Oswald Buddenhagen
2012-08-31 19:18 ` Dan Johnson
2012-08-31 19:45 ` Junio C Hamano
2012-09-01 4:25 ` [PATCH] fetch --all: pass --tags/--no-tags through to each remote Dan Johnson
2012-09-01 11:22 ` Jeff King
2012-09-01 11:25 ` [PATCH 1/2] argv-array: add pop function Jeff King
2012-09-01 11:27 ` [PATCH 2/2] fetch: use argv_array instead of hand-building arrays Jeff King
2012-09-01 14:34 ` Jens Lehmann
2012-09-01 15:27 ` Jens Lehmann [this message]
2012-09-01 11:32 ` [PATCH] fetch --all: pass --tags/--no-tags through to each remote Jeff King
2012-09-01 11:34 ` [PATCH 3/2] argv-array: fix bogus cast when freeing array Jeff King
2012-09-05 21:22 ` [PATCHv2] fetch --all: pass --tags/--no-tags through to each remote Dan Johnson
2012-09-07 17:07 ` Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5042294A.7020507@web.de \
--to=jens.lehmann@web.de \
--cc=computerdruid@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=ossi@kde.org \
--cc=peff@peff.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).