# [PATCH] add: Use struct argv_array in run_add_interactive()

4 messages from 2014-03-15 to 2014-03-17. Participants: Fabian Ruch, Thomas Rast, Eric Sunshine.
Thread: https://gitlist.dev/t/36172

## Fabian Ruch, 2014-03-15 11:14

Subject: [PATCH] add: Use struct argv_array in run_add_interactive()
Message-ID: <53243620.8080401@gmail.com>
URL: https://gitlist.dev/e/53243620.8080401%40gmail.com

```
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>
---
 builtin/add.c | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)

diff --git a/builtin/add.c b/builtin/add.c
index 4b045ba..459208a 100644
--- a/builtin/add.c
+++ b/builtin/add.c
@@ -15,6 +15,7 @@
 #include "diffcore.h"
 #include "revision.h"
 #include "bulk-checkin.h"
+#include "argv-array.h"
 
 static const char * const builtin_add_usage[] = {
 	N_("git add [options] [--] <pathspec>..."),
@@ -141,23 +142,21 @@ static void refresh(int verbose, const struct pathspec *pathspec)
 int run_add_interactive(const char *revision, const char *patch_mode,
 			const struct pathspec *pathspec)
 {
-	int status, ac, i;
-	const char **args;
+	int status, i;
+	struct argv_array argv = ARGV_ARRAY_INIT;
 
-	args = xcalloc(sizeof(const char *), (pathspec->nr + 6));
-	ac = 0;
-	args[ac++] = "add--interactive";
+	argv_array_push(&argv, "add--interactive");
 	if (patch_mode)
-		args[ac++] = patch_mode;
+		argv_array_push(&argv, patch_mode);
 	if (revision)
-		args[ac++] = revision;
-	args[ac++] = "--";
+		argv_array_push(&argv, revision);
+	argv_array_push(&argv, "--");
 	for (i = 0; i < pathspec->nr; i++)
 		/* pass original pathspec, to be re-parsed */
-		args[ac++] = pathspec->items[i].original;
+		argv_array_push(&argv, pathspec->items[i].original);
 
-	status = run_command_v_opt(args, RUN_GIT_CMD);
-	free(args);
+	status = run_command_v_opt(argv.argv, RUN_GIT_CMD);
+	argv_array_clear(&argv);
 	return status;
 }
 
-- 
1.9.0

```

## Fabian Ruch, 2014-03-15 12:41

Subject: Re: [PATCH] add: Use struct argv_array in run_add_interactive()
Message-ID: <53244A95.8050308@gmail.com>
URL: https://gitlist.dev/e/53244A95.8050308%40gmail.com
In-Reply-To: <53243620.8080401@gmail.com>

```
On 03/15/2014 12:14 PM, Fabian Ruch wrote:
> 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>
> ---
>  builtin/add.c | 21 ++++++++++-----------
>  1 file changed, 10 insertions(+), 11 deletions(-)

I should mention that I am applying to this year's edition of Google's
Summer of Code.

```

## Thomas Rast, 2014-03-16 11:42

Subject: Re: [PATCH] add: Use struct argv_array in run_add_interactive()
Message-ID: <87a9cqxtcy.fsf@thomasrast.ch>
URL: https://gitlist.dev/e/87a9cqxtcy.fsf%40thomasrast.ch
In-Reply-To: <53243620.8080401@gmail.com>

```
Fabian Ruch <bafain@gmail.com> writes:

> 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>

Thanks, this is a nicely done cleanup.

> ---
>  builtin/add.c | 21 ++++++++++-----------
>  1 file changed, 10 insertions(+), 11 deletions(-)
>
> diff --git a/builtin/add.c b/builtin/add.c
> index 4b045ba..459208a 100644
> --- a/builtin/add.c
> +++ b/builtin/add.c
> @@ -15,6 +15,7 @@
>  #include "diffcore.h"
>  #include "revision.h"
>  #include "bulk-checkin.h"
> +#include "argv-array.h"
>  
>  static const char * const builtin_add_usage[] = {
>  	N_("git add [options] [--] <pathspec>..."),
> @@ -141,23 +142,21 @@ static void refresh(int verbose, const struct pathspec *pathspec)
>  int run_add_interactive(const char *revision, const char *patch_mode,
>  			const struct pathspec *pathspec)
>  {
> +	int status, i;
> +	struct argv_array argv = ARGV_ARRAY_INIT;
>  
> -	args = xcalloc(sizeof(const char *), (pathspec->nr + 6));
> -	ac = 0;
> -	args[ac++] = "add--interactive";
> +	argv_array_push(&argv, "add--interactive");
>  	if (patch_mode)
> -		args[ac++] = patch_mode;
> +		argv_array_push(&argv, patch_mode);
>  	if (revision)
> -		args[ac++] = revision;
> -	args[ac++] = "--";
> +		argv_array_push(&argv, revision);
> +	argv_array_push(&argv, "--");
>  	for (i = 0; i < pathspec->nr; i++)
>  		/* pass original pathspec, to be re-parsed */
> -		args[ac++] = pathspec->items[i].original;
> +		argv_array_push(&argv, pathspec->items[i].original);
>  
> -	status = run_command_v_opt(args, RUN_GIT_CMD);
> -	free(args);
> +	status = run_command_v_opt(argv.argv, RUN_GIT_CMD);
> +	argv_array_clear(&argv);
>  	return status;
>  }

-- 
Thomas Rast
tr@thomasrast.ch

```

## Eric Sunshine, 2014-03-17 09:28

Subject: Re: [PATCH] add: Use struct argv_array in run_add_interactive()
Message-ID: <CAPig+cQVLd9kxa1gV1GpWN=NBt8DvVo9uKNDTdjwzjF7wCt67g@mail.gmail.com>
URL: https://gitlist.dev/e/CAPig%2BcQVLd9kxa1gV1GpWN%3DNBt8DvVo9uKNDTdjwzjF7wCt67g%40mail.gmail.com
In-Reply-To: <87a9cqxtcy.fsf@thomasrast.ch>

```
On Sun, Mar 16, 2014 at 7:42 AM, Thomas Rast <tr@thomasrast.ch> wrote:
> Fabian Ruch <bafain@gmail.com> writes:
>
>> 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>
>
> Thanks, this is a nicely done cleanup.

I second that. Nicely done. You didn't give reviewers any opportunity
to provide constructive feedback. :-)

>> ---
>>  builtin/add.c | 21 ++++++++++-----------
>>  1 file changed, 10 insertions(+), 11 deletions(-)
>>
>> diff --git a/builtin/add.c b/builtin/add.c
>> index 4b045ba..459208a 100644
>> --- a/builtin/add.c
>> +++ b/builtin/add.c
>> @@ -15,6 +15,7 @@
>>  #include "diffcore.h"
>>  #include "revision.h"
>>  #include "bulk-checkin.h"
>> +#include "argv-array.h"
>>
>>  static const char * const builtin_add_usage[] = {
>>       N_("git add [options] [--] <pathspec>..."),
>> @@ -141,23 +142,21 @@ static void refresh(int verbose, const struct pathspec *pathspec)
>>  int run_add_interactive(const char *revision, const char *patch_mode,
>>                       const struct pathspec *pathspec)
>>  {
>> +     int status, i;
>> +     struct argv_array argv = ARGV_ARRAY_INIT;
>>
>> -     args = xcalloc(sizeof(const char *), (pathspec->nr + 6));
>> -     ac = 0;
>> -     args[ac++] = "add--interactive";
>> +     argv_array_push(&argv, "add--interactive");
>>       if (patch_mode)
>> -             args[ac++] = patch_mode;
>> +             argv_array_push(&argv, patch_mode);
>>       if (revision)
>> -             args[ac++] = revision;
>> -     args[ac++] = "--";
>> +             argv_array_push(&argv, revision);
>> +     argv_array_push(&argv, "--");
>>       for (i = 0; i < pathspec->nr; i++)
>>               /* pass original pathspec, to be re-parsed */
>> -             args[ac++] = pathspec->items[i].original;
>> +             argv_array_push(&argv, pathspec->items[i].original);
>>
>> -     status = run_command_v_opt(args, RUN_GIT_CMD);
>> -     free(args);
>> +     status = run_command_v_opt(argv.argv, RUN_GIT_CMD);
>> +     argv_array_clear(&argv);
>>       return status;
>>  }

```
