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

Re: [PATCH v2] submodule: port subcommand 'set-branch' from shell to C

From
Eric Sunshine <sunshine@sunshineco.com>
Date
May 19, 2020, 18:57 UTC
Message-ID
<CAPig+cSKFBFdNXA52f5f0q3SetA2btmkXeqyHNw-qwJ5ECq5mQ@mail.gmail.com>
In-Reply-To
<20200519182654.33318-1-shouryashukla.oo@gmail.com>

On Tue, May 19, 2020 at 2:27 PM Shourya Shukla <shouryashukla.oo@gmail.com> wrote:

> Convert submodule subcommand 'set-branch' to a builtin. Port 'set-branch'
> to 'submodule--helper.c' and call the latter via 'git-submodule.sh'.
You can reduce the redundancy by writing this as:
    Convert git-submodule subcommand 'set-branch' to a builtin and
    call it via 'git-submodule.sh'.
Show 8 quoted lines
> Signed-off-by: Shourya Shukla <shouryashukla.oo@gmail.com>
> ---
> diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
> @@ -2284,6 +2284,46 @@ static int module_set_url(int argc, const char **argv, const char *prefix)
> +static int module_set_branch(int argc, const char **argv, const char *prefix)
> +{
> +       int quiet = 0, opt_default = 0;
> +       char *opt_branch = NULL;
This can be 'const const *', can't it?
> +       struct option options[] = {
> +               OPT__QUIET(&quiet,
> +                       N_("suppress output for setting default tracking branch of a submodule")),

This is unusually verbose for a _short_ description of the option. Other commands use simpler descriptions. Perhaps take a hint from the git-submodule man page:

    N("only print error messages")),

However, the bigger question is: Why is the --quiet option even here? None of the code in this function ever consults the 'quiet' variable, so its presence seems pointless.

Looking at the git-submodule documentation, I see that it is already documented as accepted --quiet, so it may make some sense for you to accept the option here. However, it might be a good idea either to have an in-code comment or a blurb in the commit message explaining that this C rewrite accepts the option for backward-compatibility (and for future extension), not because it is actually used presently.

> +               OPT_BOOL(0, "default", &opt_default,
> +                       N_("set the default tracking branch to master")),

We can make this and the next short description more precise and concise like this:

    N_("reset the default tracking branch to master")),
> +               OPT_STRING(0, "branch", &opt_branch, N_("branch"),
> +                       N_("set the default tracking branch to the one specified")),
Then:
    N_("set the default tracking branch")),
Show 12 quoted lines
> +               OPT_END()
> +       };
> +       const char *const usage[] = {
> +               N_("git submodule--helper set-branch [--quiet] (-d|--default) <path>"),
> +               N_("git submodule--helper set-branch [--quiet] (-b|--branch) <branch> <path>"),
> +               NULL
> +       };
> +
> +       argc = parse_options(argc, argv, prefix, options, usage, 0);
> +
> +       if (!opt_branch && !opt_default)
> +               die(_("at least one of --branch and --default required"));

This wording makes no sense considering that --branch and --default are mutually exclusive. By writing "at least one of", you're saying that you can use _more than one_, which is clearly incorrect. Reword it like this:

    die(_("--branch or --default required"));
> +       if (opt_branch && opt_default)
> +               die(_("--branch and --default do not make sense together"));
A more precise way to say this is:
    die(_("--branch and --default are mutually exclusive"));
Show 5 quoted lines
> +       if (argc != 1 || !(path = argv[0]))
> +               usage_with_options(usage, options);
> +
> +       config_name = xstrfmt("submodule.%s.branch", path);
> +       config_set_in_gitmodules_file_gently(config_name, opt_branch);

Tracing through the config code, I see that config_set_in_gitmodules_file_gently() removes the key if 'opt_branch' is NULL, which mirrors the behavior of the shell code this is replacing. Good.

Previous: Shourya ShuklaNext: Shourya Shukla
Message 2 of 6 in “submodule: port subcommand 'set-branch' from shell to C”
  1. submodule: port subcommand 'set-branch' from shell to CShourya Shukla, May 19, 2020
  2. Eric SunshineMay 19, 2020
  3. Shourya ShuklaMay 20, 2020
  4. Kaartic SivaraamMay 20, 2020
  5. Junio C HamanoMay 20, 2020
  6. Eric SunshineMay 20, 2020

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.