{"thread":{"id":"48092","subject":"[GSoC][PATCH v5] Make options that expect object ids less chatty if id is invalid","startedAt":"2018-03-19T21:29:34Z","lastAt":"2018-03-19T21:29:34Z","messageCount":1,"participants":["Paul-Sebastian Ungureanu"],"isPatch":true,"patchVersion":5,"patchTotal":null},"messages":[{"id":"342255","messageId":"20180319155929.7000-1-ungureanupaulsebastian@gmail.com","threadId":"48092","inReplyTo":null,"subject":"[GSoC][PATCH v5] Make options that expect object ids less chatty if id is invalid","fromName":"Paul-Sebastian Ungureanu","fromEmail":"ungureanupaulsebastian@gmail.com","sentAt":"2018-03-19T15:59:29Z","receivedAt":"2018-03-19T21:29:34Z","isPatch":true,"sender":{"key":"ungureanupaulsebastian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/24317622?v=4"},"body":"Usually, the usage should be shown only if the user does not know what\noptions are available. If the user specifies an invalid value, the user\nis already aware of the available options. In this case, there is no\npoint in displaying the usage anymore.\n\nThis patch applies to \"git tag --contains\", \"git branch --contains\",\n\"git branch --points-at\", \"git for-each-ref --contains\" and many more.\n\nSigned-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>\n---\n builtin/blame.c               |  1 +\n builtin/shortlog.c            |  1 +\n builtin/update-index.c        |  1 +\n parse-options.c               | 20 ++++----\n parse-options.h               |  1 +\n t/t0040-parse-options.sh      |  2 +-\n t/t0041-usage.sh              | 89 +++++++++++++++++++++++++++++++++++\n t/t3404-rebase-interactive.sh |  6 +--\n 8 files changed, 107 insertions(+), 14 deletions(-)\n create mode 100755 t/t0041-usage.sh\n\ndiff --git a/builtin/blame.c b/builtin/blame.c\nindex 9dcb367b9..e8c6a4d6a 100644\n--- a/builtin/blame.c\n+++ b/builtin/blame.c\n@@ -729,6 +729,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)\n \tfor (;;) {\n \t\tswitch (parse_options_step(&ctx, options, blame_opt_usage)) {\n \t\tcase PARSE_OPT_HELP:\n+\t\tcase PARSE_OPT_ERROR:\n \t\t\texit(129);\n \t\tcase PARSE_OPT_DONE:\n \t\t\tif (ctx.argv[0])\ndiff --git a/builtin/shortlog.c b/builtin/shortlog.c\nindex e29875b84..be4df6a03 100644\n--- a/builtin/shortlog.c\n+++ b/builtin/shortlog.c\n@@ -283,6 +283,7 @@ int cmd_shortlog(int argc, const char **argv, const char *prefix)\n \tfor (;;) {\n \t\tswitch (parse_options_step(&ctx, options, shortlog_usage)) {\n \t\tcase PARSE_OPT_HELP:\n+\t\tcase PARSE_OPT_ERROR:\n \t\t\texit(129);\n \t\tcase PARSE_OPT_DONE:\n \t\t\tgoto parse_done;\ndiff --git a/builtin/update-index.c b/builtin/update-index.c\nindex 58d1c2d28..34adf55a7 100644\n--- a/builtin/update-index.c\n+++ b/builtin/update-index.c\n@@ -1059,6 +1059,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)\n \t\t\tbreak;\n \t\tswitch (parseopt_state) {\n \t\tcase PARSE_OPT_HELP:\n+\t\tcase PARSE_OPT_ERROR:\n \t\t\texit(129);\n \t\tcase PARSE_OPT_NON_OPTION:\n \t\tcase PARSE_OPT_DONE:\ndiff --git a/parse-options.c b/parse-options.c\nindex d02eb8b01..47c09a82b 100644\n--- a/parse-options.c\n+++ b/parse-options.c\n@@ -317,14 +317,16 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,\n \t\treturn get_value(p, options, all_opts, flags ^ opt_flags);\n \t}\n \n-\tif (ambiguous_option)\n-\t\treturn error(\"Ambiguous option: %s \"\n+\tif (ambiguous_option) {\n+\t\terror(\"Ambiguous option: %s \"\n \t\t\t\"(could be --%s%s or --%s%s)\",\n \t\t\targ,\n \t\t\t(ambiguous_flags & OPT_UNSET) ?  \"no-\" : \"\",\n \t\t\tambiguous_option->long_name,\n \t\t\t(abbrev_flags & OPT_UNSET) ?  \"no-\" : \"\",\n \t\t\tabbrev_option->long_name);\n+\t\treturn -3;\n+\t}\n \tif (abbrev_option)\n \t\treturn get_value(p, abbrev_option, all_opts, abbrev_flags);\n \treturn -2;\n@@ -434,7 +436,6 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,\n \t\t       const char * const usagestr[])\n {\n \tint internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);\n-\tint err = 0;\n \n \t/* we must reset ->opt, unknown short option leave it dangling */\n \tctx->opt = NULL;\n@@ -459,7 +460,7 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,\n \t\t\tctx->opt = arg + 1;\n \t\t\tswitch (parse_short_opt(ctx, options)) {\n \t\t\tcase -1:\n-\t\t\t\tgoto show_usage_error;\n+\t\t\t\treturn PARSE_OPT_ERROR;\n \t\t\tcase -2:\n \t\t\t\tif (ctx->opt)\n \t\t\t\t\tcheck_typos(arg + 1, options);\n@@ -472,7 +473,7 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,\n \t\t\twhile (ctx->opt) {\n \t\t\t\tswitch (parse_short_opt(ctx, options)) {\n \t\t\t\tcase -1:\n-\t\t\t\t\tgoto show_usage_error;\n+\t\t\t\t\treturn PARSE_OPT_ERROR;\n \t\t\t\tcase -2:\n \t\t\t\t\tif (internal_help && *ctx->opt == 'h')\n \t\t\t\t\t\tgoto show_usage;\n@@ -504,9 +505,11 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,\n \t\t\tgoto show_usage;\n \t\tswitch (parse_long_opt(ctx, arg + 2, options)) {\n \t\tcase -1:\n-\t\t\tgoto show_usage_error;\n+\t\t\treturn PARSE_OPT_ERROR;\n \t\tcase -2:\n \t\t\tgoto unknown;\n+\t\tcase -3:\n+\t\t\tgoto show_usage;\n \t\t}\n \t\tcontinue;\n unknown:\n@@ -517,10 +520,8 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,\n \t}\n \treturn PARSE_OPT_DONE;\n \n- show_usage_error:\n-\terr = 1;\n  show_usage:\n-\treturn usage_with_options_internal(ctx, usagestr, options, 0, err);\n+\treturn usage_with_options_internal(ctx, usagestr, options, 0, 0);\n }\n \n int parse_options_end(struct parse_opt_ctx_t *ctx)\n@@ -539,6 +540,7 @@ int parse_options(int argc, const char **argv, const char *prefix,\n \tparse_options_start(&ctx, argc, argv, prefix, options, flags);\n \tswitch (parse_options_step(&ctx, options, usagestr)) {\n \tcase PARSE_OPT_HELP:\n+\tcase PARSE_OPT_ERROR:\n \t\texit(129);\n \tcase PARSE_OPT_NON_OPTION:\n \tcase PARSE_OPT_DONE:\ndiff --git a/parse-options.h b/parse-options.h\nindex af711227a..c77bb3b4f 100644\n--- a/parse-options.h\n+++ b/parse-options.h\n@@ -188,6 +188,7 @@ enum {\n \tPARSE_OPT_HELP = -1,\n \tPARSE_OPT_DONE,\n \tPARSE_OPT_NON_OPTION,\n+\tPARSE_OPT_ERROR,\n \tPARSE_OPT_UNKNOWN\n };\n \ndiff --git a/t/t0040-parse-options.sh b/t/t0040-parse-options.sh\nindex 0c2fc81d7..04d474c84 100755\n--- a/t/t0040-parse-options.sh\n+++ b/t/t0040-parse-options.sh\n@@ -291,7 +291,7 @@ test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '\n test_expect_success 'OPT_CALLBACK() and callback errors work' '\n \ttest_must_fail test-parse-options --no-length >output 2>output.err &&\n \ttest_i18ncmp expect output &&\n-\ttest_i18ncmp expect.err output.err\n+\ttest_must_be_empty output.err\n '\n \n cat >expect <<\\EOF\ndiff --git a/t/t0041-usage.sh b/t/t0041-usage.sh\nnew file mode 100755\nindex 000000000..2fc08ae70\n--- /dev/null\n+++ b/t/t0041-usage.sh\n@@ -0,0 +1,89 @@\n+#!/bin/sh\n+\n+test_description='Test commands behavior when given invalid argument value'\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'setup ' '\n+\tgit init . &&\n+\ttest_commit \"v1.0\" &&\n+\tgit tag \"v1.1\"\n+'\n+\n+test_expect_success 'tag --contains <existent_tag>' '\n+\tgit tag --contains \"v1.0\" >actual &&\n+\tgrep \"v1.0\" actual &&\n+\tgrep \"v1.1\" actual\n+'\n+\n+test_expect_success 'tag --contains <inexistent_tag>' '\n+\ttest_must_fail git tag --contains \"notag\" 2>actual &&\n+\ttest_i18ngrep \"error\" actual\n+'\n+\n+test_expect_success 'tag --no-contains <existent_tag>' '\n+\tgit tag --no-contains \"v1.1\" >actual &&\n+\ttest_line_count = 0 actual\n+'\n+\n+test_expect_success 'tag --no-contains <inexistent_tag>' '\n+\ttest_must_fail git tag --no-contains \"notag\" 2>actual &&\n+\ttest_i18ngrep \"error\" actual\n+'\n+\n+test_expect_success 'tag usage error' '\n+\ttest_must_fail git tag --noopt 2>actual &&\n+\ttest_i18ngrep \"usage\" actual\n+'\n+\n+test_expect_success 'branch --contains <existent_commit>' '\n+\tgit branch --contains \"master\" >actual &&\n+\ttest_i18ngrep \"master\" actual\n+'\n+\n+test_expect_success 'branch --contains <inexistent_commit>' '\n+\ttest_must_fail git branch --no-contains \"nocommit\" 2>actual &&\n+\ttest_i18ngrep \"error\" actual\n+'\n+\n+test_expect_success 'branch --no-contains <existent_commit>' '\n+\tgit branch --no-contains \"master\" >actual &&\n+\ttest_line_count = 0 actual\n+'\n+\n+test_expect_success 'branch --no-contains <inexistent_commit>' '\n+\ttest_must_fail git branch --no-contains \"nocommit\" 2>actual &&\n+\ttest_i18ngrep \"error\" actual\n+'\n+\n+test_expect_success 'branch usage error' '\n+\ttest_must_fail git branch --noopt 2>actual &&\n+\ttest_i18ngrep \"usage\" actual\n+'\n+\n+test_expect_success 'for-each-ref --contains <existent_object>' '\n+\tgit for-each-ref --contains \"master\" >actual &&\n+\ttest_line_count = 3 actual\n+'\n+\n+test_expect_success 'for-each-ref --contains <inexistent_object>' '\n+\ttest_must_fail git for-each-ref --no-contains \"noobject\" 2>actual &&\n+\ttest_i18ngrep \"error\" actual\n+'\n+\n+test_expect_success 'for-each-ref --no-contains <existent_object>' '\n+\tgit for-each-ref --no-contains \"master\" >actual &&\n+\ttest_line_count = 0 actual\n+'\n+\n+test_expect_success 'for-each-ref --no-contains <inexistent_object>' '\n+\ttest_must_fail git for-each-ref --no-contains \"noobject\" 2>actual &&\n+\ttest_i18ngrep \"error\" actual\n+'\n+\n+test_expect_success 'for-each-ref usage error' '\n+\ttest_must_fail git for-each-ref --noopt 2>actual &&\n+\ttest_i18ngrep \"usage\" actual\n+'\n+\n+test_done\ndiff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh\nindex ef2887bd8..cac8b2bd8 100755\n--- a/t/t3404-rebase-interactive.sh\n+++ b/t/t3404-rebase-interactive.sh\n@@ -919,10 +919,8 @@ test_expect_success 'rebase --exec works without -i ' '\n test_expect_success 'rebase -i --exec without <CMD>' '\n \tgit reset --hard execute &&\n \tset_fake_editor &&\n-\ttest_must_fail git rebase -i --exec 2>tmp &&\n-\tsed -e \"1d\" tmp >actual &&\n-\ttest_must_fail git rebase -h >expected &&\n-\ttest_cmp expected actual &&\n+\ttest_must_fail git rebase -i --exec 2>actual &&\n+\ttest_i18ngrep \"requires a value\" actual &&\n \tgit checkout master\n '\n \n-- \n2.16.2.346.g16307f54f.dirty\n\n"}]}