git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [GSoC][PATCH] tag: Make "git tag --contains <id>" less chatty if <id> is invalid
@ 2018-02-19 21:21 Paul-Sebastian Ungureanu
  2018-02-20 22:35 ` Stefan Beller
  0 siblings, 1 reply; 3+ messages in thread
From: Paul-Sebastian Ungureanu @ 2018-02-19 21:21 UTC (permalink / raw)
  To: git; +Cc: Paul-Sebastian Ungureanu

git tag --contains <id> prints the whole help text if <id> 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 <ungureanupaulsebastian@gmail.com>
---
 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", &colopts, N_("show tag list in columns")),
+
+		OPT_CONTAINS_STRS(&filter.with_commit_strs, N_("print only tags that contain the commit")),
+		OPT_NO_CONTAINS_STRS(&filter.no_commit_strs, N_("print only tags that don't contain the commit")),
+		OPT_WITH_STRS(&filter.with_commit_strs, N_("print only tags that contain the commit")),
+		OPT_WITHOUT_STRS(&filter.no_commit_strs, N_("print only tags that don't contain the commit")),
+
 		OPT_CONTAINS(&filter.with_commit, N_("print only tags that contain the commit")),
 		OPT_NO_CONTAINS(&filter.no_commit, N_("print only tags that don't contain the commit")),
 		OPT_WITH(&filter.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, &oid))
+		die(_("malformed object name %s"), item->string);
+	commit = lookup_commit_reference(&oid);
+	if (!commit)
+		die(_("no such commit %s"), item->string);
+	commit_list_insert(commit, commit_list);
+
+	return 0;
+}
+
 /*
  * API for filtering a set of refs. Based on the type of refs the user
  * has requested, we iterate through those refs and apply filters
@@ -2012,6 +2027,10 @@ int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int
 	int ret = 0;
 	unsigned int broken = 0;
 
+	/* Convert string representation and add to commit list. */
+	for_each_string_list(&filter->with_commit_strs, add_str_to_commit_list, &filter->with_commit);
+	for_each_string_list(&filter->no_commit_strs, add_str_to_commit_list, &filter->no_commit);
+
 	ref_cbdata.array = array;
 	ref_cbdata.filter = filter;
 
diff --git a/ref-filter.h b/ref-filter.h
index 0d98342b3..62f37760f 100644
--- a/ref-filter.h
+++ b/ref-filter.h
@@ -55,6 +55,9 @@ struct ref_filter {
 	struct commit_list *with_commit;
 	struct commit_list *no_commit;
 
+	struct string_list with_commit_strs;
+	struct string_list no_commit_strs;
+
 	enum {
 		REF_FILTER_MERGED_NONE = 0,
 		REF_FILTER_MERGED_INCLUDE,
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
@@ -0,0 +1,37 @@
+#!/bin/sh
+
+test_description='Test if git tag --contains prints only the error
+when used correctly but with an inexistent tag as argument'
+
+. ./test-lib.sh
+
+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"
+'
+
+test_expect_success 'tag --contains <existent_tag>' '
+	! (git tag --contains "v1.0" 2>&1 | grep -o "usage")
+'
+
+test_expect_success 'tag --contains <existent_tag>' '
+	! (git tag --contains "v1.1" 2>&1 | grep -o "usage")
+'
+
+test_expect_success 'tag --contains <inexistent_tag>' '
+	! (git tag --contains "notag" 2>&1 | grep -o "usage")
+'
+
+test_expect_success 'tag --contains <inexistent_tag>' '
+	! (git tag --contains "v1.2" 2>&1 | grep -o "usage")
+'
+
+test_expect_success 'tag usage error' '
+	git tag --sort 2>&1 | grep -o "usage"
+'
+
+test_done
-- 
2.16.1.194.gb2e45c695.dirty


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [GSoC][PATCH] tag: Make "git tag --contains <id>" less chatty if <id> is invalid
  2018-02-19 21:21 [GSoC][PATCH] tag: Make "git tag --contains <id>" less chatty if <id> is invalid Paul-Sebastian Ungureanu
@ 2018-02-20 22:35 ` Stefan Beller
  2018-02-20 23:29   ` Junio C Hamano
  0 siblings, 1 reply; 3+ messages in thread
From: Stefan Beller @ 2018-02-20 22:35 UTC (permalink / raw)
  To: Paul-Sebastian Ungureanu; +Cc: git

Welcome to the Git mailing list!

On Mon, Feb 19, 2018 at 1:21 PM, Paul-Sebastian Ungureanu
<ungureanupaulsebastian@gmail.com> 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@gmail.com>
> ---

> 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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [GSoC][PATCH] tag: Make "git tag --contains <id>" less chatty if <id> is invalid
  2018-02-20 22:35 ` Stefan Beller
@ 2018-02-20 23:29   ` Junio C Hamano
  0 siblings, 0 replies; 3+ messages in thread
From: Junio C Hamano @ 2018-02-20 23:29 UTC (permalink / raw)
  To: Stefan Beller; +Cc: Paul-Sebastian Ungureanu, git

Stefan Beller <sbeller@google.com> writes:

>> 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")

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).

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-02-20 23:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-02-19 21:21 [GSoC][PATCH] tag: Make "git tag --contains <id>" less chatty if <id> is invalid Paul-Sebastian Ungureanu
2018-02-20 22:35 ` Stefan Beller
2018-02-20 23:29   ` Junio C Hamano

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).