Subject: Re: [GSoC][PATCH 3/5 v3] submodule: port set_name_rev() from shell to C

From: Junio C Hamano <[email-masked]>
Date: Fri, 30 Jun 2017 15:49:23 -0700

Prathamesh Chavan <[email-masked]> writes:

> Function set_name_rev() is ported from git-submodule to the
> submodule--helper builtin. The function get_name_rev() generates the
> value of the revision name as required, and the function
> print_name_rev() handles the formating and printing of the obtained
> revision name.
>
> Mentored-by: Christian Couder <[email-masked]>
> Mentored-by: Stefan Beller <[email-masked]>
> Signed-off-by: Prathamesh Chavan <[email-masked]>
> ---
>  builtin/submodule--helper.c | 69 +++++++++++++++++++++++++++++++++++++++++++++
>  git-submodule.sh            | 16 ++---------
>  2 files changed, 71 insertions(+), 14 deletions(-)
>
> diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
> index c4286aac5..4103e40e4 100644
> --- a/builtin/submodule--helper.c
> +++ b/builtin/submodule--helper.c
> @@ -244,6 +244,74 @@ static char *get_submodule_displaypath(const char *path, const char *prefix)
>  	}
>  }
>  
> +enum describe_step {
> +	step_bare,
> +	step_tags,
> +	step_contains,
> +	step_all_always,
> +	step_end
> +};

Hmph.

The only difference between the steps being what subcommand is run,
a better implementation might be to do

	static const char *describe_bare[] = {
		NULL
	};

	...

	static const char **describe_argv[] = {
		describe_bare, describe_tags, describe_contains, 
                describe_all_always, NULL
	};

	const char ***d;

	for (d = describe_argv; *d; d++) {
		argv_array_pushl(&cp.args, "describe");
		argv_array_pushv(&cp.args, *d);
		... do the thing ...
	}

but unfortunately C is a bit klunky to do so; we cannot easily make
the contents of describe_argv[] as anonymous arrays.  An otherwise
useless enum stil bothers me, but I do not think of anything better
offhand.

> +
> +static char *get_name_rev(const char *sub_path, const char* object_id)
> +{
> +	struct strbuf sb = STRBUF_INIT;
> +	enum describe_step cur_step;
> +
> +	for (cur_step = step_bare; cur_step < step_end; cur_step++) {
> +		struct child_process cp = CHILD_PROCESS_INIT;
> +		prepare_submodule_repo_env(&cp.env_array);
> +		cp.dir = sub_path;
> +		cp.git_cmd = 1;
> +		cp.no_stderr = 1;
> +
> +		switch (cur_step) {
> +			case step_bare:
> +				argv_array_pushl(&cp.args, "describe",
> +						 object_id, NULL);
> +				break;
> +			case step_tags:	
> +				argv_array_pushl(&cp.args, "describe",
> +						 "--tags", object_id, NULL);
> +				break;
> +			case step_contains:
> +				argv_array_pushl(&cp.args, "describe",
> +						 "--contains", object_id,
> +						 NULL);
> +				break;
> +			case step_all_always:
> +				argv_array_pushl(&cp.args, "describe",
> +						 "--all", "--always",
> +						 object_id, NULL);
> +				break;
> +			default:
> +				BUG("unknown describe step '%d'", cur_step);
> +		}

Dedent the body of switch() by one level, i.e.

	switch (var) {
	case val1:
		do_this();
		break;
	case val2:
		do_that();
		...
	}

Otherwise looking good.

> @@ -1042,14 +1030,14 @@ cmd_status()
>  		fi
>  		if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path"
>  		then
> -			set_name_rev "$sm_path" "$sha1"
> +			revname=$(git submodule--helper print-name-rev "$sm_path" "$sha1")
>  			say " $sha1 $displaypath$revname"
>  		else
>  			if test -z "$cached"
>  			then
>  				sha1=$(sanitize_submodule_env; cd "$sm_path" && git rev-parse --verify HEAD)
>  			fi
> -			set_name_rev "$sm_path" "$sha1"
> +			revname=$(git submodule--helper print-name-rev "$sm_path" "$sha1")
>  			say "+$sha1 $displaypath$revname"
>  		fi