git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [GSoC][PATCH v2 1/2] submodule: port set_name_rev from shell to C

From
Stefan Beller <sbeller@google.com>
Date
Jun 5, 2017, 22:50 UTC
Message-ID
<CAGZ79kZv_Jw1n6z=5sDOtiJecyJmQ-NEzYN6kPL_uCCHTKE0bg@mail.gmail.com>
In-Reply-To
<20170605202529.22959-1-pc44800@gmail.com>
On Mon, Jun 5, 2017 at 1:25 PM, Prathamesh Chavan <pc44800@gmail.com> wrote:
Show 32 quoted lines
> 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 <christian.couder@gmail.com>
> Mentored-by: Stefan Beller <sbeller@google.com>
> Signed-off-by: Prathamesh Chavan <pc44800@gmail.com>
> ---
>  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.)

Show 7 quoted lines
> +       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".

Show 7 quoted lines
> +{
> +       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

Previous: Christian CouderNext: Brandon Williams
Message 8 of 9 in “submodule: port set_name_rev from shell to C”
  1. Prathamesh ChavanMay 21, 2017
  2. [GSoC][PATCH v1 2/2] submodule: port submodule subcommand statusPrathamesh Chavan, May 21, 2017
  3. Stefan BellerMay 22, 2017
  4. [GSoC][PATCH v2 1/2] submodule: port set_name_rev from shell to CPrathamesh Chavan, Jun 5, 2017
  5. [GSoC][PATCH v2 2/2] submodule: port submodule subcommand statusPrathamesh Chavan, Jun 5, 2017
  6. Stefan BellerJun 5, 2017
  7. Christian CouderJun 6, 2017
  8. Stefan BellerJun 5, 2017
  9. Brandon WilliamsJun 5, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.