Welcome to the Git mailing list! On Mon, Feb 19, 2018 at 1:21 PM, Paul-Sebastian Ungureanu <ungureanupaulsebastian@xxxxxxxxx> wrote: > git tag --contains <id> prints the whole help text if <id> is > invalid. It should only show the error message instead. Makes sense. > > This bug was a side effect of looking up the commit in option > parser callback. When a error occurs in the option parser, the > full usage is shown. To fix this bug, the part related to > looking up the commit was moved outside of the option parser > to the ref-filter module. > > Basically, the option parser only parses strings that represent > commits and the ref-filter performs the commit look-up. If an > error occurs during the option parsing, then it must be an invalid > argument and the user should be informed of usage, but if a error > occurs during ref-filtering, then it is a problem with the > argument. > > Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@xxxxxxxxx> > --- > diff --git a/parse-options.h b/parse-options.h > index af711227a..3aa8ddd46 100644 > --- a/parse-options.h > +++ b/parse-options.h parse-options.h is a very generic place in Gits source code, so this would also fix 'git branch --contains=<id>' at the same time? Would it make sense to say so in the commit message or have a test for that? > @@ -258,9 +258,20 @@ extern int parse_opt_passthru_argv(const struct option *, const char *, int); > PARSE_OPT_LASTARG_DEFAULT | flag, \ > parse_opt_commits, (intptr_t) "HEAD" \ > } > +#define _OPT_CONTAINS_OR_WITH_STRS(name, variable, help, flag) \ > + { OPTION_CALLBACK, 0, name, (variable), N_("commit"), (help), \ > + PARSE_OPT_LASTARG_DEFAULT | flag, \ > + parse_opt_string_list, (intptr_t) "HEAD" \ > + } This is the same as _OPT_CONTAINS_OR_WITH except parse_opt_commits is substituted by parse_opt_string_list. Do we need both? (As far as I understand this addresses a whole class of errors that could be eliminated, we do not want to fix git-tag only, we'd also want to fix git-branch as well as git-for-each-ref ?) So instead convert all callers to the new behavior, or would that break existing conventions? (Then there is no need to have 2 competing defines that are nearly identical) Instead of double-defining, would it be possible to turn it into a flag? OPT_DONT_HELP_ON_BAD_OBJECTID or something? > + > #define OPT_CONTAINS(v, h) _OPT_CONTAINS_OR_WITH("contains", v, h, PARSE_OPT_NONEG) > #define OPT_NO_CONTAINS(v, h) _OPT_CONTAINS_OR_WITH("no-contains", v, h, PARSE_OPT_NONEG) > #define OPT_WITH(v, h) _OPT_CONTAINS_OR_WITH("with", v, h, PARSE_OPT_HIDDEN | PARSE_OPT_NONEG) > #define OPT_WITHOUT(v, h) _OPT_CONTAINS_OR_WITH("without", v, h, PARSE_OPT_HIDDEN | PARSE_OPT_NONEG) > > +#define OPT_CONTAINS_STRS(v, h) _OPT_CONTAINS_OR_WITH_STRS("contains", v, h, PARSE_OPT_NONEG) > +#define OPT_NO_CONTAINS_STRS(v, h) _OPT_CONTAINS_OR_WITH_STRS("no-contains", v, h, PARSE_OPT_NONEG) > +#define OPT_WITH_STRS(v, h) _OPT_CONTAINS_OR_WITH_STRS("with", v, h, PARSE_OPT_HIDDEN | PARSE_OPT_NONEG) > +#define OPT_WITHOUT_STRS(v, h) _OPT_CONTAINS_OR_WITH_STRS("without", v, h, PARSE_OPT_HIDDEN | PARSE_OPT_NONEG) > + > diff --git a/t/t7013-tag-contains.sh b/t/t7013-tag-contains.sh > new file mode 100755 > index 000000000..65119dada > --- /dev/null > +++ b/t/t7013-tag-contains.sh Thanks for adding the tests into a new file instead of putting it somewhere where it is already convenient. (We have too many of those "just add it there as it is easiest to fit in") > + > +test_expect_success 'tag --contains <existent_tag>' ' > + ! (git tag --contains "v1.0" 2>&1 | grep -o "usage") In the test suite we assume everything but Git flawless, but Git *may* be faulty. What if Git crashes (segfault) ? Then this test is still producing a valid "ok" as grep doesn't find "usage". This pattern of piping output of Git into other commands is around the test suite unfortunately, but we'd want to not add this pattern in new code. So maybe: git tag --contains v1.0 2 >error && grep "usage" err Another thing on this: we'd want to check the return code of git tag in this case. In case of an error in parse-opt we error out with 129 just as git would without this patch: $ git tag --contains=a6d7eb2c7a6a402a938824bcf1c5f331dd1a06bc error: no such commit a6d7eb2c7a6a402a938824bcf1c5f331dd1a06bc usage: git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] <tagname> [<head>] or: git tag -d <tagname>... or: git tag -l [-n[<num>]] [--contains <commit>] [--no-contains <commit>] [--points-at <object>] [....] $ echo $? 129 But after applying this patch, we also do not want to have 129 as the return code as that indicates that parsing the options was unsuccessful. Instead we want to see that the --contains operation was unsucessful, maybe? So I'd expect the return code to be 0 (if we don't care) or 1 (if we do care), in the case of 1, we shall write: test_must_fail git tag --contains ... && grep .... (A long way of hinting at the test_must_fail test function, that lives in t/test-lib-functions.sh) Thanks, Stefan