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

Re: [GSoC][PATCH v4] Make options that expect object ids less chatty if id is invalid

From
MÅMartin Ågren <martin.agren@gmail.com>
Date
Mar 8, 2018, 19:27 UTC
Message-ID
<CAN0heSrnERYggyJ8bL1nAV=X2JQYS8aOkc6nWZhnZu3oqhzAfA@mail.gmail.com>
In-Reply-To
<20180306193116.23876-1-ungureanupaulsebastian@gmail.com>
Hi Paul-Sebastian

On 6 March 2018 at 20:31, Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> wrote:

Show 7 quoted lines
> Usually, the usage should be shown only if the user does not know what
> options are available. If the user specifies an invalid value, the user
> is already aware of the available options. In this case, there is no
> point in displaying the usage anymore.
>
> This patch applies to "git tag --contains", "git branch --contains",
> "git branch --points-at", "git for-each-ref --contains" and many more.
Thanks for scratching this itch.
Show 39 quoted lines
>  8 files changed, 110 insertions(+), 14 deletions(-)
>  create mode 100755 t/tcontains.sh
>
> diff --git a/builtin/blame.c b/builtin/blame.c
> index 9dcb367b9..a5fbec8fb 100644
> --- a/builtin/blame.c
> +++ b/builtin/blame.c
> @@ -728,6 +728,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
>                             PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
>         for (;;) {
>                 switch (parse_options_step(&ctx, options, blame_opt_usage)) {
> +               case PARSE_OPT_ERROR:
>                 case PARSE_OPT_HELP:
>                         exit(129);
>                 case PARSE_OPT_DONE:
> diff --git a/builtin/shortlog.c b/builtin/shortlog.c
> index e29875b84..0789e2eea 100644
> --- a/builtin/shortlog.c
> +++ b/builtin/shortlog.c
> @@ -282,6 +282,7 @@ int cmd_shortlog(int argc, const char **argv, const char *prefix)
>
>         for (;;) {
>                 switch (parse_options_step(&ctx, options, shortlog_usage)) {
> +               case PARSE_OPT_ERROR:
>                 case PARSE_OPT_HELP:
>                         exit(129);
>                 case PARSE_OPT_DONE:
> diff --git a/builtin/update-index.c b/builtin/update-index.c
> index 58d1c2d28..34adf55a7 100644
> --- a/builtin/update-index.c
> +++ b/builtin/update-index.c
> @@ -1059,6 +1059,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
>                         break;
>                 switch (parseopt_state) {
>                 case PARSE_OPT_HELP:
> +               case PARSE_OPT_ERROR:
>                         exit(129);
>                 case PARSE_OPT_NON_OPTION:
>                 case PARSE_OPT_DONE:
> diff --git a/parse-options.c b/parse-options.c
> index d02eb8b01..47c09a82b 100644
> --- a/parse-options.c
> +++ b/parse-options.c
[...]
Show 9 quoted lines
>  int parse_options_end(struct parse_opt_ctx_t *ctx)
> @@ -539,6 +540,7 @@ int parse_options(int argc, const char **argv, const char *prefix,
>         parse_options_start(&ctx, argc, argv, prefix, options, flags);
>         switch (parse_options_step(&ctx, options, usagestr)) {
>         case PARSE_OPT_HELP:
> +       case PARSE_OPT_ERROR:
>                 exit(129);
>         case PARSE_OPT_NON_OPTION:
>         case PARSE_OPT_DONE:

These are very slightly inconsistent with the ordering of PARSE_OPT_ERROR and PARSE_OPT_HELP. I don't think it matters much. ;-)

Show 5 quoted lines
> diff --git a/t/tcontains.sh b/t/tcontains.sh
> new file mode 100755
> index 000000000..4856111ff
> --- /dev/null
> +++ b/t/tcontains.sh

This filename is not on the usual form, t1234-foo. As a result, it it is not picked up by `make test`, so isn't actually exercised. I believe you selected this name based on the feedback in [1], but I do not think that this ("tcontains.sh") was what Junio had in mind. Running the script manually succeeds though. :-)

> @@ -0,0 +1,92 @@
> +#!/bin/sh
> +
> +test_description='Test "contains" argument behavior'
Ok, that matches the file name.
Show 8 quoted lines
> +test_expect_success 'setup ' '
> +       git init . &&
> +       echo "this is a test" >file &&
> +       git add -A &&
> +       git commit -am "tag test" &&
> +       git tag "v1.0" &&
> +       git tag "v1.1"
> +'

You might find `test_commit` helpful. All in all, this could be a two-liner (this example is white-space mangled though):

test_expect_success 'setup ' '
        test_commit "v1.0" &&
        git tag "v1.1"
'
> +test_expect_success 'tag usage error' '
> +       test_must_fail git tag --noopt 2>actual &&
> +       test_i18ngrep "usage" actual
> +'

Hmm, this one and a couple like it do not match the filename or test description. I wonder if these are needed at all, or if some checks like these are already done elsewhere (maybe not for "git tag", but for some other user of the option-parsing machinery).

I hope you find this useful.
Martin
[1] https://public-inbox.org/git/xmqqinar1bq2.fsf@gitster-ct.c.googlers.com/
Previous: Junio C HamanoNext: ungureanupaulsebastian@gmail.com
Message 4 of 5 in “Make options that expect object ids less chatty if id is invalid”
  1. Paul-Sebastian UngureanuMar 6, 2018
  2. Junio C HamanoMar 6, 2018
  3. Junio C HamanoMar 6, 2018
  4. Martin ÅgrenMar 8, 2018
  5. ungureanupaulsebastian@gmail.comMar 19, 2018

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.