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

Re: [PATCHv4 1/3] submodule: implement `list` as a builtin helper

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Sep 1, 2015, 23:17 UTC
Message-ID
<CAPig+cTBK0Zw6ECJY4cDeP-eYOoGrCGAPXAvj1Xk-HABeBv-_A@mail.gmail.com>
In-Reply-To
<1441131869-29474-2-git-send-email-sbeller@google.com>
On Tue, Sep 1, 2015 at 2:24 PM, Stefan Beller <sbeller@google.com> wrote:
Show 22 quoted lines
> Most of the submodule operations work on a set of submodules.
> Calculating and using this set is usually done via:
>
>        module_list "$@" | {
>            while read mode sha1 stage sm_path
>            do
>                 # the actual operation
>            done
>        }
>
> Currently the function `module_list` is implemented in the
> git-submodule.sh as a shell script wrapping a perl script.
> The rewrite is in C, such that it is faster and can later be
> easily adapted when other functions are rewritten in C.
>
> [...]
>
> Signed-off-by: Stefan Beller <sbeller@google.com>
> ---
> +struct cmd_struct {
> +       const char *cmd;
> +       int (*fct)(int, const char **, const char *);

It would be better to stick with a more idiomatic name such as 'f' or 'func' than invent an entirely new one ('fct').

Show 26 quoted lines
> +};
> +
> +static struct cmd_struct commands[] = {
> +       {"list", module_list},
> +};
> +
> +int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
> +{
> +       int i;
> +       if (argc < 2)
> +               goto out;
> +
> +       for (i = 0; i < ARRAY_SIZE(commands); i++)
> +               if (!strcmp(argv[1], commands[i].cmd))
> +                       return commands[i].fct(argc - 1, argv + 1, prefix);
> +
> +out:
> +       if (argc > 1)
> +               fprintf(stderr, _("fatal: '%s' is not a valid submodule--helper "
> +                                 "subcommand, which are:\n"), argv[1]);
> +       else
> +               fprintf(stderr, _("fatal: submodule--helper subcommand must be "
> +                                 "called with a subcommand, which are:\n"));
> +
> +       for (i = 0; i < ARRAY_SIZE(commands); i++)
> +               fprintf(stderr, "\t%s\n", commands[i].cmd);
A couple observations:
1. git-submodule--helper isn't the only Git command featuring
subcommands which could benefit from this dispatching and error
reporting code, so making it re-usable would be sensible rather than
hiding it away inside this one (very low-level, not user-facing)
command. If you go that route, it would deserve its own patch series,
and well thought out design and interface. However...
2. I realize that the suggestion of listing available subcommands was
put forth tentatively by Junio[1], but it seems overkill for a command
like this which is not user-facing, and is inconsistent with other
subcommand-bearing commands. At this stage, it should be sufficient
merely to emit a plain error message (without any usage information):
    unrecognized subcommand: %s
    missing subcommand

at which point the user can consult the man page or "git subcommand--helper -h" to find out what went wrong.

[1]: http://article.gmane.org/gmane.comp.version-control.git/276935
> +
> +       exit(129);
> +}
Previous: Junio C HamanoNext: Stefan Beller
Message 5 of 12 in “[PATCHv4 0/3] submodule--helper: Have some refactoring only patches first”
  1. Stefan BellerSep 1, 2015
  2. 1/3 submodule: implement `list` as a builtin helperStefan Beller, Sep 1, 2015
  3. Junio C HamanoSep 1, 2015
  4. Junio C HamanoSep 2, 2015
  5. Eric SunshineSep 1, 2015
  6. 2/3 submodule: implement `name` as a builtin helperStefan Beller, Sep 1, 2015
  7. Eric SunshineSep 1, 2015
  8. 3/3 submodule: implement `clone` as a builtin helperStefan Beller, Sep 1, 2015
  9. Eric SunshineSep 1, 2015
  10. Jeff KingSep 1, 2015
  11. Eric SunshineSep 1, 2015
  12. Junio C HamanoSep 1, 2015

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.