# [PATCH] receive-pack: simplify run_update_post_hook()

6 messages from 2017-03-17 to 2017-03-20. Participants: René Scharfe, Jeff King, Jonathan Nieder, Junio C Hamano.
Thread: https://gitlist.dev/t/45414

## René Scharfe, 2017-03-17 22:02

Subject: [PATCH] receive-pack: simplify run_update_post_hook()
Message-ID: <ac1c0cfc-b1de-37be-23b3-98ef1081ec4a@web.de>
URL: https://gitlist.dev/e/ac1c0cfc-b1de-37be-23b3-98ef1081ec4a%40web.de

```
Instead of counting the arguments to see if there are any and then
building the full command use a single loop and add the hook command
just before the first argument.  This reduces duplication and overall
code size.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 builtin/receive-pack.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index 83492af05f..fb2a090a0c 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -1128,25 +1128,22 @@ static const char *update(struct command *cmd, struct shallow_info *si)
 static void run_update_post_hook(struct command *commands)
 {
 	struct command *cmd;
-	int argc;
 	struct child_process proc = CHILD_PROCESS_INIT;
 	const char *hook;
 
 	hook = find_hook("post-update");
-	for (argc = 0, cmd = commands; cmd; cmd = cmd->next) {
-		if (cmd->error_string || cmd->did_not_exist)
-			continue;
-		argc++;
-	}
-	if (!argc || !hook)
+	if (!hook)
 		return;
 
-	argv_array_push(&proc.args, hook);
 	for (cmd = commands; cmd; cmd = cmd->next) {
 		if (cmd->error_string || cmd->did_not_exist)
 			continue;
+		if (!proc.args.argc)
+			argv_array_push(&proc.args, hook);
 		argv_array_push(&proc.args, cmd->ref_name);
 	}
+	if (!proc.args.argc)
+		return;
 
 	proc.no_stdin = 1;
 	proc.stdout_to_stderr = 1;
-- 
2.12.0


```

## Jeff King, 2017-03-17 22:23

Subject: Re: [PATCH] receive-pack: simplify run_update_post_hook()
Message-ID: <20170317222320.nsg3yk3nopjadr5t@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20170317222320.nsg3yk3nopjadr5t%40sigill.intra.peff.net
In-Reply-To: <ac1c0cfc-b1de-37be-23b3-98ef1081ec4a@web.de>

```
On Fri, Mar 17, 2017 at 11:02:13PM +0100, René Scharfe wrote:

> Instead of counting the arguments to see if there are any and then
> building the full command use a single loop and add the hook command
> just before the first argument.  This reduces duplication and overall
> code size.

Yeah, I agree one loop is nicer.

> -	argv_array_push(&proc.args, hook);
>  	for (cmd = commands; cmd; cmd = cmd->next) {
>  		if (cmd->error_string || cmd->did_not_exist)
>  			continue;
> +		if (!proc.args.argc)
> +			argv_array_push(&proc.args, hook);
>  		argv_array_push(&proc.args, cmd->ref_name);
>  	}
> +	if (!proc.args.argc)
> +		return;

It looks at first like the result leaks, because you have to realize
that the push will modify proc.args.argc. I wonder if:

  argv_array_push(&proc.args, hook);
  for (cmd = commands; cmd; cmd = cmd->next) {
	if (!cmd->error_string && !cmd->did_not_exist)
		argv_array_push(&proc.args, cmd->ref_name);
  }

  if (proc.args.argc == 1) {
	argv_array_clear(&proc.args);
	return;
  }

would be more obvious (at the cost of a pointless malloc in the corner
case. I can live with it either way.

-Peff

```

## René Scharfe, 2017-03-17 23:21

Subject: Re: [PATCH] receive-pack: simplify run_update_post_hook()
Message-ID: <f61a1089-833b-2883-deab-f97cb77cef96@web.de>
URL: https://gitlist.dev/e/f61a1089-833b-2883-deab-f97cb77cef96%40web.de
In-Reply-To: <20170317222320.nsg3yk3nopjadr5t@sigill.intra.peff.net>

```
Am 17.03.2017 um 23:23 schrieb Jeff King:
> On Fri, Mar 17, 2017 at 11:02:13PM +0100, René Scharfe wrote:
>
>> Instead of counting the arguments to see if there are any and then
>> building the full command use a single loop and add the hook command
>> just before the first argument.  This reduces duplication and overall
>> code size.
>
> Yeah, I agree one loop is nicer.
>
>> -	argv_array_push(&proc.args, hook);
>>  	for (cmd = commands; cmd; cmd = cmd->next) {
>>  		if (cmd->error_string || cmd->did_not_exist)
>>  			continue;
>> +		if (!proc.args.argc)
>> +			argv_array_push(&proc.args, hook);
>>  		argv_array_push(&proc.args, cmd->ref_name);
>>  	}
>> +	if (!proc.args.argc)
>> +		return;
>
> It looks at first like the result leaks, because you have to realize
> that the push will modify proc.args.argc. I wonder if:
>
>   argv_array_push(&proc.args, hook);
>   for (cmd = commands; cmd; cmd = cmd->next) {
> 	if (!cmd->error_string && !cmd->did_not_exist)
> 		argv_array_push(&proc.args, cmd->ref_name);
>   }
>
>   if (proc.args.argc == 1) {
> 	argv_array_clear(&proc.args);
> 	return;
>   }
>
> would be more obvious (at the cost of a pointless malloc in the corner
> case. I can live with it either way.

Sure, that's even simpler.  I don't know how often the no-args branch 
would be taken and if the extra allocation would even matter -- that's 
why I tried to avoid it -- but probably the answers are not often and 
not much.  The only test case that hits it is for the deletion of a 
non-existent ref.

René

```

## Jonathan Nieder, 2017-03-17 22:50

Subject: Re: [PATCH] receive-pack: simplify run_update_post_hook()
Message-ID: <20170317225040.GQ26789@aiede.mtv.corp.google.com>
URL: https://gitlist.dev/e/20170317225040.GQ26789%40aiede.mtv.corp.google.com
In-Reply-To: <ac1c0cfc-b1de-37be-23b3-98ef1081ec4a@web.de>

```
René Scharfe wrote:

> Instead of counting the arguments to see if there are any and then
> building the full command use a single loop and add the hook command
> just before the first argument.  This reduces duplication and overall
> code size.
>
> Signed-off-by: Rene Scharfe <l.s.r@web.de>
> ---
>  builtin/receive-pack.c | 13 +++++--------
>  1 file changed, 5 insertions(+), 8 deletions(-)

Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>

```

## Junio C Hamano, 2017-03-18 17:12

Subject: Re: [PATCH] receive-pack: simplify run_update_post_hook()
Message-ID: <xmqqshma4ihk.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqqshma4ihk.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <20170317222320.nsg3yk3nopjadr5t@sigill.intra.peff.net>

```
Jeff King <peff@peff.net> writes:

> On Fri, Mar 17, 2017 at 11:02:13PM +0100, René Scharfe wrote:
>
>> Instead of counting the arguments to see if there are any and then
>> building the full command use a single loop and add the hook command
>> just before the first argument.  This reduces duplication and overall
>> code size.
>
> Yeah, I agree one loop is nicer.
>
>> -	argv_array_push(&proc.args, hook);
>>  	for (cmd = commands; cmd; cmd = cmd->next) {
>>  		if (cmd->error_string || cmd->did_not_exist)
>>  			continue;
>> +		if (!proc.args.argc)
>> +			argv_array_push(&proc.args, hook);
>>  		argv_array_push(&proc.args, cmd->ref_name);
>>  	}
>> +	if (!proc.args.argc)
>> +		return;
>
> It looks at first like the result leaks, because you have to realize
> that the push will modify proc.args.argc. 

Hmph, I needed to read the original twice to imagine how a paranoid
person can fear leaks.  The return condition says "if args array is
empty, just return" and the thing being empty is an enough indication
to think nothing is leaking, at least for me.

Having said that, I'd admit that the "always push hook and then
clean up before returning if it turns out there is nothing to call
the hook for" is what I would have wrote if I were doing this, but
I'm inclined to think that is not because I would have thought of
both versions and picked the better one, but because I wouldn't have
noticed the "optimization opportunity" René spotted here (not that I
think an extra alloc would matter).

I'll queue the patch as-is, at least for now.

Thanks.

> I wonder if:
>
>   argv_array_push(&proc.args, hook);
>   for (cmd = commands; cmd; cmd = cmd->next) {
> 	if (!cmd->error_string && !cmd->did_not_exist)
> 		argv_array_push(&proc.args, cmd->ref_name);
>   }
>
>   if (proc.args.argc == 1) {
> 	argv_array_clear(&proc.args);
> 	return;
>   }
>
> would be more obvious (at the cost of a pointless malloc in the corner
> case. I can live with it either way.
>
> -Peff

```

## Jeff King, 2017-03-20 03:35

Subject: Re: [PATCH] receive-pack: simplify run_update_post_hook()
Message-ID: <20170320033530.sl4rbd2lq3qo2kgl@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20170320033530.sl4rbd2lq3qo2kgl%40sigill.intra.peff.net
In-Reply-To: <xmqqshma4ihk.fsf@gitster.mtv.corp.google.com>

```
On Sat, Mar 18, 2017 at 10:12:07AM -0700, Junio C Hamano wrote:

> >> -	argv_array_push(&proc.args, hook);
> >>  	for (cmd = commands; cmd; cmd = cmd->next) {
> >>  		if (cmd->error_string || cmd->did_not_exist)
> >>  			continue;
> >> +		if (!proc.args.argc)
> >> +			argv_array_push(&proc.args, hook);
> >>  		argv_array_push(&proc.args, cmd->ref_name);
> >>  	}
> >> +	if (!proc.args.argc)
> >> +		return;
> >
> > It looks at first like the result leaks, because you have to realize
> > that the push will modify proc.args.argc. 
> 
> Hmph, I needed to read the original twice to imagine how a paranoid
> person can fear leaks.  The return condition says "if args array is
> empty, just return" and the thing being empty is an enough indication
> to think nothing is leaking, at least for me.

Yeah, I think I just read it funny the first time. I'm OK with it as-is.

-Peff

```
