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

2018-03-19 Thread ungureanupaulsebastian
Hello,

Thank you for your advice! Soon enough, I wil submit a new patch which
fixes the issues you mentioned.

Best regards,
Paul Ungureanu


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

2018-03-08 Thread Martin Ă…gren
Hi Paul-Sebastian

On 6 March 2018 at 20:31, Paul-Sebastian Ungureanu
 wrote:
> 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.

>  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
[...]
>  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. ;-)

> diff --git a/t/tcontains.sh b/t/tcontains.sh
> new file mode 100755
> index 0..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.

> +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@gitster-ct.c.googlers.com/


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

2018-03-06 Thread Junio C Hamano
Junio C Hamano  writes:

> I kind of find it surprising that the one single case I happened to
> have noticed is the only one that needed special treatment.  Did you
> go though all the codepath and made sure that the ones that still
> return -1 (not -2 and not -3) to parse_options_step() are all OK (in
> other words, I was just lucky) or does this version change only the
> "ambiguous" case, simply because that was the only one I noticed in
> my review (in other words, this may still not be sufficient)?
>
> Just double checking.
>
> The changes to existing tests have become a lot less noisy, compared
> to the previous one, which is probably a good thing.

I guess I should stop reading my inbox in reverse order.  In your
reply to my v3 review you said you studied all the codepaths from
parse_short_opt() and parse_long_opt() and addition of -3 was needed
only for the "ambiguous" case, so the answer to my question above is
"I was just lucky and happened to have hit the only problematic
case" ;-)

Will revert what is in 'next' and queue this one.

Thanks.


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

2018-03-06 Thread Junio C Hamano
Paul-Sebastian Ungureanu  writes:

> 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.
>
> Signed-off-by: Paul-Sebastian Ungureanu 
> ---

I notice that this version changes the way the case where an
unbiguous long option is given, compared to the previous one is
handled.  And I recall that that single case is what I happened to
have noticed during my review of the previous one, in which I was
not trying to be exhausitive.  

I kind of find it surprising that the one single case I happened to
have noticed is the only one that needed special treatment.  Did you
go though all the codepath and made sure that the ones that still
return -1 (not -2 and not -3) to parse_options_step() are all OK (in
other words, I was just lucky) or does this version change only the
"ambiguous" case, simply because that was the only one I noticed in
my review (in other words, this may still not be sufficient)?

Just double checking.

The changes to existing tests have become a lot less noisy, compared
to the previous one, which is probably a good thing.

Thanks.


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

2018-03-06 Thread Paul-Sebastian Ungureanu
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.

Signed-off-by: Paul-Sebastian Ungureanu 
---
 builtin/blame.c   |  1 +
 builtin/shortlog.c|  1 +
 builtin/update-index.c|  1 +
 parse-options.c   | 20 
 parse-options.h   |  1 +
 t/t0040-parse-options.sh  |  2 +-
 t/t3404-rebase-interactive.sh |  6 +--
 t/tcontains.sh| 92 +++
 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
@@ -317,14 +317,16 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, 
const char *arg,
return get_value(p, options, all_opts, flags ^ opt_flags);
}
 
-   if (ambiguous_option)
-   return error("Ambiguous option: %s "
+   if (ambiguous_option) {
+   error("Ambiguous option: %s "
"(could be --%s%s or --%s%s)",
arg,
(ambiguous_flags & OPT_UNSET) ?  "no-" : "",
ambiguous_option->long_name,
(abbrev_flags & OPT_UNSET) ?  "no-" : "",
abbrev_option->long_name);
+   return -3;
+   }
if (abbrev_option)
return get_value(p, abbrev_option, all_opts, abbrev_flags);
return -2;
@@ -434,7 +436,6 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
   const char * const usagestr[])
 {
int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
-   int err = 0;
 
/* we must reset ->opt, unknown short option leave it dangling */
ctx->opt = NULL;
@@ -459,7 +460,7 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
ctx->opt = arg + 1;
switch (parse_short_opt(ctx, options)) {
case -1:
-   goto show_usage_error;
+   return PARSE_OPT_ERROR;
case -2:
if (ctx->opt)
check_typos(arg + 1, options);
@@ -472,7 +473,7 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
while (ctx->opt) {
switch (parse_short_opt(ctx, options)) {
case -1:
-   goto show_usage_error;
+   return PARSE_OPT_ERROR;
case -2:
if (internal_help && *ctx->opt == 'h')
goto show_usage;
@@ -504,9 +505,11 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
goto show_usage;
switch (parse_long_opt(ctx, arg + 2, options)) {
case -1:
-   goto show_usage_error;
+   return PARSE_OPT_ERROR;
case -2: