Re: [GSoC][PATCH] tag: Make "git tag --contains " less chatty if is invalid
Stefan Bellerwrites: >> diff --git a/t/t7013-tag-contains.sh b/t/t7013-tag-contains.sh >> new file mode 100755 >> index 0..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") Careful, as that cuts both ways. We want to strongly encourage people to see if there is already a place that is a good enough fit for new tests before adding small test scripts randomly to consume the test serial numbers and test process start-up cost. Only when there is nowhere appropriate, we do want to add. And if this covers both tag and branch, then a new script may be appropriate but it shouldn't limit its future enhancement (to test 'git branch') by having 'tag' to pretend that this file must be limited to 'git tag'. > 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) If you are looking at error stream, it is very likely that you would want to study test_i18ngrep and use it, as errors are fair game for i18n (and possibly coloring, which is a near-by topic).
Re: [GSoC][PATCH] tag: Make "git tag --contains " less chatty if is invalid
Welcome to the Git mailing list! On Mon, Feb 19, 2018 at 1:21 PM, Paul-Sebastian Ungureanuwrote: > git tag --contains prints the whole help text if 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 > --- > 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=' 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 0..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 ' ' > + ! (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 ] [-f] [-m | -F ] [] or: git tag -d ... or: git tag -l [-n[]] [--contains ] [--no-contains ] [--points-at ] [] $ 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
[GSoC][PATCH] tag: Make "git tag --contains " less chatty if is invalid
git tag --contains prints the whole help text if is invalid. It should only show the error message instead. 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--- builtin/tag.c | 11 +-- parse-options.h | 11 +++ ref-filter.c| 19 +++ ref-filter.h| 3 +++ t/t7013-tag-contains.sh | 37 + 5 files changed, 79 insertions(+), 2 deletions(-) create mode 100755 t/t7013-tag-contains.sh diff --git a/builtin/tag.c b/builtin/tag.c index a7e6a5b0f..3fce14713 100644 --- a/builtin/tag.c +++ b/builtin/tag.c @@ -396,6 +396,12 @@ int cmd_tag(int argc, const char **argv, const char *prefix) OPT_GROUP(N_("Tag listing options")), OPT_COLUMN(0, "column", , N_("show tag list in columns")), + + OPT_CONTAINS_STRS(_commit_strs, N_("print only tags that contain the commit")), + OPT_NO_CONTAINS_STRS(_commit_strs, N_("print only tags that don't contain the commit")), + OPT_WITH_STRS(_commit_strs, N_("print only tags that contain the commit")), + OPT_WITHOUT_STRS(_commit_strs, N_("print only tags that don't contain the commit")), + OPT_CONTAINS(_commit, N_("print only tags that contain the commit")), OPT_NO_CONTAINS(_commit, N_("print only tags that don't contain the commit")), OPT_WITH(_commit, N_("print only tags that contain the commit")), @@ -437,6 +443,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix) cmdmode = 'l'; else if (filter.with_commit || filter.no_commit || filter.points_at.nr || filter.merge_commit || +filter.with_commit_strs.nr || filter.no_commit_strs.nr || filter.lines != -1) cmdmode = 'l'; } @@ -473,9 +480,9 @@ int cmd_tag(int argc, const char **argv, const char *prefix) } if (filter.lines != -1) die(_("-n option is only allowed in list mode")); - if (filter.with_commit) + if (filter.with_commit || filter.with_commit_strs.nr) die(_("--contains option is only allowed in list mode")); - if (filter.no_commit) + if (filter.no_commit || filter.no_commit_strs.nr) die(_("--no-contains option is only allowed in list mode")); if (filter.points_at.nr) die(_("--points-at option is only allowed in list mode")); diff --git a/parse-options.h b/parse-options.h index af711227a..3aa8ddd46 100644 --- a/parse-options.h +++ b/parse-options.h @@ -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" \ + } + #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) + #endif diff --git a/ref-filter.c b/ref-filter.c index f9e25aea7..e7bdaa27f 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -2000,6 +2000,21 @@ static void do_merge_filter(struct ref_filter_cbdata *ref_cbdata) free(to_clear); } +int add_str_to_commit_list(struct string_list_item *item, void *commit_list) +{ + struct object_id oid; + struct commit *commit; + + if (get_oid(item->string, )) +