On Mon, Jun 5, 2017 at 1:25 PM, Prathamesh Chavan <[email protected]> wrote:
> Since later on we want to port submodule subcommand status, and since
> set_name_rev is part of cmd_status, hence this function is ported. It
> has been ported to function print_name_rev in C, which calls get_name_rev
> to get the revname, and after formatting it, print_name_rev prints it.
> And hence in this way, the command `git submodule--helper print-name-rev
> "sm_path" "sha1"` sets value of revname in git-submodule.sh
>
> The function get_name_rev returns the stdout of the git describe
> commands. Since there are four different git-describe commands used for
> generating the name rev, four child_process are introduced, each successive
> child process running only when previous has no stdout. The order of these
> four git-describe commands is maintained the same as it was in the function
> set_name_rev() in shell script.
>
> Mentored-by: Christian Couder <[email protected]>
> Mentored-by: Stefan Beller <[email protected]>
> Signed-off-by: Prathamesh Chavan <[email protected]>
> ---
> builtin/submodule--helper.c | 67
> +++++++++++++++++++++++++++++++++++++++++++++
> git-submodule.sh | 16 ++---------
> 2 files changed, 69 insertions(+), 14 deletions(-)
>
> diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
> index 566a5b6a6..3022118d1 100644
> --- a/builtin/submodule--helper.c
> +++ b/builtin/submodule--helper.c
> @@ -219,6 +219,72 @@ static int resolve_relative_url_test(int argc, const
> char **argv, const char *pr
> return 0;
> }
>
> +enum describe_step {
> + step_bare = 0,
Do we rely on step_bare to be equal to 0?
(This is the hint I am reading from '=0' here.
If we do not, please omit.)
> + step_tags,
> + step_contains,
> + step_all_always,
> + step_end
> +};
> +
> +static char *get_name_rev(int argc, const char **argv, const char *prefix)
So we split up the functionality into two functions.
get_name_rev, which does the heavy lifting work, and
print_name_rev, that is a wrapper around having to deal with
going from shell to C and back.
One of C strength' compared to shell is type safety,
so maybe we can tighten the contract that get_name_rev
offers to its callers and make it
get_name_rev(const char *sub_path, const char *object_id / sha1)
and then have print_name_rev call it via
get_name_rev (argv[1], argv[2])
(which coincidentally is right after checking for
argc != 3, which reinforces that the contract of the
wrapper is "just making sure we have valid input" and
this function "just does heavy lifting, assuming input
is valid".
> +{
> + struct child_process cp;
> + struct strbuf sb = STRBUF_INIT;
> + enum describe_step cur_step;
> +
> + for (cur_step = step_bare; cur_step < step_end; cur_step++) {
> + child_process_init(&cp);
(minor nit, personal opinion, feel free to ignore:)
Alternatively, you could declare cp inside the loop assigned to
CHILD_PROCESS_INIT.
Same for strbuf sb as well, such that you only declare the iterator
variable outside the loop.
> + prepare_submodule_repo_env(&cp.env_array);
> + cp.dir = argv[1];
> + cp.no_stderr = 1;
cp.git_cmd = 1; as well?
Thanks,
Stefan