add: use struct argv_array in run_add_interactive()
run_add_interactive() in builtin/add.c manually computes array bounds and allocates a static args array to build the add--interactive command line, which is error-prone. Use the argv-array helper functions instead. Signed-off-by: Fabian Ruch <bafain@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Fabian Ruch committed
Mar 15, 2014 at 12:14 UTC
c45a18e88f47fc9b23f8e8e7b14a030434917a66
1 file changed
+10
-11
builtin/add.c
+10
-11
@@ -15,6 +15,7 @@
15
#include "diffcore.h"
16
#include "revision.h"
17
#include "bulk-checkin.h"
18
+#include "argv-array.h"
19
20
static const char * const builtin_add_usage[] = {
21
N_("git add [options] [--] <pathspec>..."),
@@ -246,23 +247,21 @@ static void refresh(int verbose, const struct pathspec *pathspec)
247
int run_add_interactive(const char *revision, const char *patch_mode,
248
const struct pathspec *pathspec)
249
{
249
- int status, ac, i;
250
- const char **args;
250
+ int status, i;
251
+ struct argv_array argv = ARGV_ARRAY_INIT;
252
252
- args = xcalloc(sizeof(const char *), (pathspec->nr + 6));
253
- ac = 0;
254
- args[ac++] = "add--interactive";
253
+ argv_array_push(&argv, "add--interactive");
254
if (patch_mode)
256
- args[ac++] = patch_mode;
255
+ argv_array_push(&argv, patch_mode);
256
if (revision)
258
- args[ac++] = revision;
259
- args[ac++] = "--";
257
+ argv_array_push(&argv, revision);
258
+ argv_array_push(&argv, "--");
259
for (i = 0; i < pathspec->nr; i++)
260
/* pass original pathspec, to be re-parsed */
262
- args[ac++] = pathspec->items[i].original;
261
+ argv_array_push(&argv, pathspec->items[i].original);
262
264
- status = run_command_v_opt(args, RUN_GIT_CMD);
265
- free(args);
263
+ status = run_command_v_opt(argv.argv, RUN_GIT_CMD);
264
+ argv_array_clear(&argv);
265
return status;
266
}
267