Subject: Re: [PATCH v3 16/28] receive-pack: reorder some code in unpack()

From: Junio C Hamano <[email-masked]>
Date: Mon, 02 Dec 2013 14:25:08 -0800

Nguyễn Thái Ngọc Duy  <[email-masked]> writes:

> This is the preparation for adding --shallow-file to both
> unpack-objects and index-pack. To sum up:
>
>  - status/code, ip/child, unpacker/keeper and i (now ac) are moved out
>    to function top level
>
>  - successful flow now ends at the end of the function
>
> Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>

Sounds sensible; for a new piece of code, we probably should see if
argv_array API is more appropriate, and I think it is for this case.

Thanks.

> ---
>  builtin/receive-pack.c | 74 +++++++++++++++++++++++---------------------------
>  1 file changed, 34 insertions(+), 40 deletions(-)
>
> diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
> index cc8c34f..22e162d 100644
> --- a/builtin/receive-pack.c
> +++ b/builtin/receive-pack.c
> @@ -822,8 +822,11 @@ static const char *pack_lockfile;
>  static const char *unpack(int err_fd)
>  {
>  	struct pack_header hdr;
> +	const char *argv[7];
>  	const char *hdr_err;
> +	int status, ac = 0;
>  	char hdr_arg[38];
> +	struct child_process child;
>  	int fsck_objects = (receive_fsck_objects >= 0
>  			    ? receive_fsck_objects
>  			    : transfer_fsck_objects >= 0
> @@ -840,63 +843,54 @@ static const char *unpack(int err_fd)
>  			"--pack_header=%"PRIu32",%"PRIu32,
>  			ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
>  
> +	memset(&child, 0, sizeof(child));
>  	if (ntohl(hdr.hdr_entries) < unpack_limit) {
> -		int code, i = 0;
> -		struct child_process child;
> -		const char *unpacker[5];
> -		unpacker[i++] = "unpack-objects";
> +		argv[ac++] = "unpack-objects";
>  		if (quiet)
> -			unpacker[i++] = "-q";
> +			argv[ac++] = "-q";
>  		if (fsck_objects)
> -			unpacker[i++] = "--strict";
> -		unpacker[i++] = hdr_arg;
> -		unpacker[i++] = NULL;
> -		memset(&child, 0, sizeof(child));
> -		child.argv = unpacker;
> +			argv[ac++] = "--strict";
> +		argv[ac++] = hdr_arg;
> +		argv[ac++] = NULL;
> +		child.argv = argv;
>  		child.no_stdout = 1;
>  		child.err = err_fd;
>  		child.git_cmd = 1;
> -		code = run_command(&child);
> -		if (!code)
> -			return NULL;
> -		return "unpack-objects abnormal exit";
> +		status = run_command(&child);
> +		if (status)
> +			return "unpack-objects abnormal exit";
>  	} else {
> -		const char *keeper[7];
> -		int s, status, i = 0;
> +		int s;
>  		char keep_arg[256];
> -		struct child_process ip;
>  
>  		s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid());
>  		if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
>  			strcpy(keep_arg + s, "localhost");
>  
> -		keeper[i++] = "index-pack";
> -		keeper[i++] = "--stdin";
> +		argv[ac++] = "index-pack";
> +		argv[ac++] = "--stdin";
>  		if (fsck_objects)
> -			keeper[i++] = "--strict";
> +			argv[ac++] = "--strict";
>  		if (fix_thin)
> -			keeper[i++] = "--fix-thin";
> -		keeper[i++] = hdr_arg;
> -		keeper[i++] = keep_arg;
> -		keeper[i++] = NULL;
> -		memset(&ip, 0, sizeof(ip));
> -		ip.argv = keeper;
> -		ip.out = -1;
> -		ip.err = err_fd;
> -		ip.git_cmd = 1;
> -		status = start_command(&ip);
> -		if (status) {
> +			argv[ac++] = "--fix-thin";
> +		argv[ac++] = hdr_arg;
> +		argv[ac++] = keep_arg;
> +		argv[ac++] = NULL;
> +		child.argv = argv;
> +		child.out = -1;
> +		child.err = err_fd;
> +		child.git_cmd = 1;
> +		status = start_command(&child);
> +		if (status)
>  			return "index-pack fork failed";
> -		}
> -		pack_lockfile = index_pack_lockfile(ip.out);
> -		close(ip.out);
> -		status = finish_command(&ip);
> -		if (!status) {
> -			reprepare_packed_git();
> -			return NULL;
> -		}
> -		return "index-pack abnormal exit";
> +		pack_lockfile = index_pack_lockfile(child.out);
> +		close(child.out);
> +		status = finish_command(&child);
> +		if (status)
> +			return "index-pack abnormal exit";
> +		reprepare_packed_git();
>  	}
> +	return NULL;
>  }
>  
>  static const char *unpack_with_sideband(void)