Skip to contentSkip to conversation

extend describe and name-rev pattern matching

PATCH Superseded Jacob Keller 3 participants 8 messages Dec 17, 2016 – Dec 18, 2016 Documentation

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover extend describe and name-rev pattern matching

From: Jacob Keller <[email protected]>

This series adds support for extending describe and name-rev pattern matching to allow (a) taking multiple patterns and (b) negative patterns which discard instead of keep. These changes increase the flexibility of the describe mechanism so that searching for specific tag formats can be done more easily.

Changes since v1
* add new patches for negative pattern matching
* tweak the documentation slightly

-- interdiff since v1 --

Documentation/git-describe.txt +0 -0
index c85f2811ce28..a89bbde207b2 100644
@@ -83,11 +83,18 @@ OPTIONS
 --match <pattern>::

Only consider tags matching the given glob(7) pattern, excluding the "refs/tags/" prefix. This can be used to avoid
- leaking private tags from the repository, or to shrink the scope of
- searched tags to avoid -rc tags or similar. If given multiple a list
- of patterns will be accumulated, and tags matching any of the patterns
- will be considered. Use --no-match to clear and reset the list of
- patterns.
+ leaking private tags from the repository. If given multiple times, a
+ list of patterns will be accumulated, and tags matching any of the
+ patterns will be considered. Use --no-match to clear and reset the
+ list of patterns.
+
+--discard <pattern>::
+ Do not consider tags matching the given glob(7) pattern, excluding
+ the "refs/tags/" prefix. This can be used to narrow the tag space and
+ find only tags matching some meaningful criteria. If given multiple
+ times, a list of patterns will be accumulated and tags matching any
+ of the patterns will be discarded. Use --no-discard to clear and
+ reset the list of patterns.

--always::
Show uniquely abbreviated commit object as fallback.

Documentation/git-name-rev.txt +0 -0
index 7433627db12d..9b46e5ea9aae 100644
@@ -30,6 +30,13 @@ OPTIONS

given multiple times, use refs whose names match any of the given shell patterns. Use --no-refs to clear any previous ref patterns given.

+--discard=<pattern>::
+ Do not use any ref whose name matches a given shell pattern. The
+ pattern can be one of branch name, tag name or fully qualified ref
+ name. If given multiple times, discard refs that match any of the given
+ shell patterns. Use --no-discards to clear the list of discard
+ patterns.
+
--all::
List all commits reachable from all refs

builtin/describe.c +1 -0
index e3ceab65e273..c09288ee6321 100644
@@ -29,6 +29,7 @@ static int max_candidates = 10;
 static struct hashmap names;
 static int have_util;
 static struct string_list patterns = STRING_LIST_INIT_NODUP;
+static struct string_list discard_patterns = STRING_LIST_INIT_NODUP;
 static int always;
 static const char *dirty;
 
@@ -130,6 +131,22 @@ static int get_name(const char *path, const struct object_id *oid, int flag, voi
		return 0;
 
	/*

+ * If we're given discard patterns, first discard any tag which match
+ * any of the discard pattern.
+ */
+ if (discard_patterns.nr) {
+ struct string_list_item *item;
+
+ if (!is_tag)
+ return 0;
+
+ for_each_string_list_item(item, &discard_patterns) {
+ if (!wildmatch(item->string, path + 10, 0, NULL))
+ return 0;
+ }
+ }
+
+ /*
* If we're given patterns, accept only tags which match at least one
* pattern.
*/
@@ -140,9 +157,8 @@ static int get_name(const char *path, const struct object_id *oid, int flag, voi

			return 0;
 
		for_each_string_list_item(item, &patterns) {
-			if (!wildmatch(item->string, path + 10, 0, NULL)) {
+			if (!wildmatch(item->string, path + 10, 0, NULL))
				break;

- }

			/* If we get here, no pattern matched. */
			return 0;
@@ -422,6 +438,8 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
			    N_("consider <n> most recent tags (default: 10)")),
		OPT_STRING_LIST(0, "match", &patterns, N_("pattern"),
			   N_("only consider tags matching <pattern>")),

+ OPT_STRING_LIST(0, "discard", &discard_patterns, N_("pattern"),
+ N_("do not consider tags matching <pattern>")),
OPT_BOOL(0, "always", &always,
N_("show abbreviated commit object as fallback")),
{OPTION_STRING, 0, "dirty", &dirty, N_("mark"),
@@ -459,6 +477,8 @@ int cmd_describe(int argc, const char **argv, const char *prefix)

			argv_array_push(&args, "--tags");
			for_each_string_list_item(item, &patterns)
				argv_array_pushf(&args, "--refs=refs/tags/%s", item->string);
+			for_each_string_list_item(item, &discard_patterns)
+				argv_array_pushf(&args, "--discard=refs/tags/%s", item->string);
		}
		if (argc)
			argv_array_pushv(&args, argv);
builtin/name-rev.c +0 -0
index 000a2a700ed3..86479c17a7c9 100644
@@ -109,6 +109,7 @@ struct name_ref_data {
	int tags_only;
	int name_only;
	struct string_list ref_filters;

+ struct string_list discard_filters;
};

static struct tip_table {
@@ -150,6 +151,15 @@ static int name_ref(const char *path, const struct object_id *oid, int flags, vo

	if (data->tags_only && !starts_with(path, "refs/tags/"))
		return 0;

+ if (data->discard_filters.nr) {
+ struct string_list_item *item;
+
+ for_each_string_list_item(item, &data->discard_filters) {
+ if (subpath_matches(path, item->string) >= 0)
+ return 0;
+ }
+ }
+

	if (data->ref_filters.nr) {
		struct string_list_item *item;
		int matched = 0;
@@ -323,12 +333,14 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
 {
	struct object_array revs = OBJECT_ARRAY_INIT;
	int all = 0, transform_stdin = 0, allow_undefined = 1, always = 0, peel_tag = 0;
-	struct name_ref_data data = { 0, 0, STRING_LIST_INIT_NODUP };
+	struct name_ref_data data = { 0, 0, STRING_LIST_INIT_NODUP, STRING_LIST_INIT_NODUP };
	struct option opts[] = {
		OPT_BOOL(0, "name-only", &data.name_only, N_("print only names (no SHA-1)")),
		OPT_BOOL(0, "tags", &data.tags_only, N_("only use tags to name the commits")),
		OPT_STRING_LIST(0, "refs", &data.ref_filters, N_("pattern"),
				   N_("only use refs matching <pattern>")),
+		OPT_STRING_LIST(0, "discard", &data.discard_filters, N_("pattern"),
+				   N_("ignore refs matching <pattern>")),
		OPT_GROUP(""),
		OPT_BOOL(0, "all", &all, N_("list all commits reachable from all refs")),
		OPT_BOOL(0, "stdin", &transform_stdin, N_("read from stdin")),
t/t6007-rev-list-cherry-pick-file.sh +0 -0
index d072ec43b016..8a4c35f6ffee 100755
@@ -118,6 +118,13 @@ test_expect_success 'name-rev --refs excludes non-matched patterns' '

test_cmp actual.named expect
'

+test_expect_success 'name-rev --discard excludes matched patterns' '
+ git rev-list --left-right --cherry-pick F...E -- bar > actual &&
+ git name-rev --stdin --name-only --refs="*tags/*" --discard="*E" \
+ < actual > actual.named &&
+ test_cmp actual.named expect
+'
+
cat >expect <<EOF
$(git rev-list --left-right --cherry-pick F...E -- bar) EOF

t/t6120-describe.sh +0 -0
index 9e5db9b87a1f..4e4a9f2e5305 100755
@@ -218,6 +218,14 @@ test_expect_success 'describe --contains and --match' '

test_cmp expect actual
'

+test_expect_success 'describe --discard' '
+ echo "c~1" >expect &&
+ tagged_commit=$(git rev-parse "refs/tags/A^0") &&
+ test_must_fail git describe --contains --match="B" $tagged_commit &&
+ git describe --contains --match="?" --discard="A" $tagged_commit >actual &&
+ test_cmp expect actual
+'
+
test_expect_success 'describe --contains and --no-match' '

	echo "A^0" >expect &&
	tagged_commit=$(git rev-parse "refs/tags/A^0") &&

Jacob Keller (5):
doc: add documentation for OPT_STRING_LIST
name-rev: extend --refs to accept multiple patterns
name-rev: add support to discard refs by pattern match
describe: teach --match to accept multiple patterns
describe: teach describe negative pattern matches

 Documentation/git-describe.txt                | 13 ++++++-
 Documentation/git-name-rev.txt                | 11 +++++-
 Documentation/technical/api-parse-options.txt |  5 +++
 builtin/describe.c                            | 51 ++++++++++++++++++++++----
 builtin/name-rev.c                            | 53 +++++++++++++++++++++------
 t/t6007-rev-list-cherry-pick-file.sh          | 30 +++++++++++++++
 t/t6120-describe.sh                           | 19 ++++++++++
 7 files changed, 161 insertions(+), 21 deletions(-)
Signature · 1 line
2.11.0.rc2.152.g4d04e67
#1 lore raw

PATCH v2 1/5 doc: add documentation for OPT_STRING_LIST

From: Jacob Keller <[email protected]>

Commit c8ba16391655 ("parse-options: add OPT_STRING_LIST helper", 2011-06-09) added the OPT_STRING_LIST as a way to accumulate a repeated list of strings. However, this was not documented in the api-parse-options documentation. Add documentation now so that future developers may learn of its existence.

---
 Documentation/technical/api-parse-options.txt | 5 +++++
 1 file changed, 5 insertions(+)
Documentation/technical/api-parse-options.txt +5 -0
index 27bd701c0d68..92791740aa64 100644
@@ -168,6 +168,11 @@ There are some macros to easily define options:
 	Introduce an option with string argument.
 	The string argument is put into `str_var`.
 
+`OPT_STRING_LIST(short long, &list, arg_str, description)`::
+	Introduce an option with a string argument. Repeated invocations
+	accumulate into a list of strings. Reset and clear the list with
+	`--no-option`.
+
 `OPT_INTEGER(short, long, &int_var, description)`::
 	Introduce an option with integer argument.
 	The integer is put into `int_var`.
Signature · 1 line
2.11.0.rc2.152.g4d04e67
#2 lore raw

Re: [PATCH v2 1/5] doc: add documentation for OPT_STRING_LIST

From: "Jacob Keller" <[email protected]>

Quoted 18 lines

From: Jacob Keller <[email protected]>

Commit c8ba16391655 ("parse-options: add OPT_STRING_LIST helper", 2011-06-09) added the OPT_STRING_LIST as a way to accumulate a repeated list of strings. However, this was not documented in the api-parse-options documentation. Add documentation now so that future developers may learn of its existence.

---
Documentation/technical/api-parse-options.txt | 5 +++++

1 file changed, 5 insertions(+)
Documentation/technical/api-parse-options.txt +0 -0
index 27bd701c0d68..92791740aa64 100644

Documentation/technical/api-parse-options.txt There are some macros to easily define options: · line 171

Quoted 4 lines
Documentation/technical/api-parse-options.txt +1 -0
@@ -168,6 +168,11 @@ There are some macros to easily define options:
 Introduce an option with string argument.
 The string argument is put into `str_var`.
 
+`OPT_STRING_LIST(short long, &list, arg_str, description)`::

should there be an extra comma between 'short long' in a similar manner to the OPT_INTEGER argument list below?

Quoted 11 lines
Documentation/technical/api-parse-options.txt +4 -0
+ Introduce an option with a string argument. Repeated invocations
+ accumulate into a list of strings. Reset and clear the list with
+ `--no-option`.
+

OPT_INTEGER(short, long, &int_var, description)::
Introduce an option with integer argument. The integer is put into int_var.

Signature · 1 line
2.11.0.rc2.152.g4d04e67
#3 lore raw

Re: [PATCH v2 1/5] doc: add documentation for OPT_STRING_LIST

Quoted 24 lines · Philip Oakley

From: "Jacob Keller" <[email protected]>

Quoted 4 lines

From: Jacob Keller <[email protected]>

Commit c8ba16391655 ("parse-options: add OPT_STRING_LIST helper", 2011-06-09) added the OPT_STRING_LIST as a way to accumulate a

repeated

Quoted 18 lines

list of strings. However, this was not documented in the api-parse-options documentation. Add documentation now so that future developers may learn of its existence.

---
Documentation/technical/api-parse-options.txt | 5 +++++

1 file changed, 5 insertions(+)
Documentation/technical/api-parse-options.txt +0 -0
index 27bd701c0d68..92791740aa64 100644
@@ -168,6 +168,11 @@ There are some macros to easily define options:
 Introduce an option with string argument.
 The string argument is put into `str_var`.
 
Quoted 7 lines

Documentation/technical/api-parse-options.txt There are some macros to easily define options: · line 171

Quoted 1 line
Documentation/technical/api-parse-options.txt +1 -0
+`OPT_STRING_LIST(short long, &list, arg_str, description)`::

should there be an extra comma between 'short long' in a similar manner to
the OPT_INTEGER argument list below?

You are indeed correct sir. I will fix this up.

Thanks,
Jake

#4 lore raw

PATCH v2 2/5 name-rev: extend --refs to accept multiple patterns

In reply to Jacob Keller

From: Jacob Keller <[email protected]>

Teach git name-rev to take a string list of patterns from --refs instead of only a single pattern. The list of patterns will be matched inclusively, such that a ref only needs to match one pattern to be included. If a ref will only be excluded if it does not match any of the patterns.

Add tests and documentation for this change.

---
 Documentation/git-name-rev.txt       |  4 +++-
 builtin/name-rev.c                   | 41 +++++++++++++++++++++++++-----------
 t/t6007-rev-list-cherry-pick-file.sh | 30 ++++++++++++++++++++++++++
 3 files changed, 62 insertions(+), 13 deletions(-)
Documentation/git-name-rev.txt +3 -1
index ca28fb8e2a07..7433627db12d 100644
@@ -26,7 +26,9 @@ OPTIONS
 
 --refs=<pattern>::
 	Only use refs whose names match a given shell pattern.  The pattern
-	can be one of branch name, tag name or fully qualified ref name.
+	can be one of branch name, tag name or fully qualified ref name. If
+	given multiple times, use refs whose names match any of the given shell
+	patterns. Use `--no-refs` to clear any previous ref patterns given.
 
 --all::
 	List all commits reachable from all refs
builtin/name-rev.c +29 -12

63 lines not on this page · raw message

t/t6007-rev-list-cherry-pick-file.sh +30 -0
index 1408b608eb03..d072ec43b016 100755
@@ -99,6 +99,36 @@ test_expect_success '--cherry-pick bar does not come up empty (II)' '
 	test_cmp actual.named expect
 '
 
+test_expect_success 'name-rev multiple --refs combine inclusive' '
+	git rev-list --left-right --cherry-pick F...E -- bar > actual &&
+	git name-rev --stdin --name-only --refs="*tags/F" --refs="*tags/E" \
+		< actual > actual.named &&
+	test_cmp actual.named expect
+'
+
+cat >expect <<EOF
+<tags/F
+$(git rev-list --left-right --right-only --cherry-pick F...E -- bar)
+EOF
+
+test_expect_success 'name-rev --refs excludes non-matched patterns' '
+	git rev-list --left-right --cherry-pick F...E -- bar > actual &&
+	git name-rev --stdin --name-only --refs="*tags/F" \
+		< actual > actual.named &&
+	test_cmp actual.named expect
+'
+
+cat >expect <<EOF
+$(git rev-list --left-right --cherry-pick F...E -- bar)
+EOF
+
+test_expect_success 'name-rev --no-refs clears the refs list' '
+	git rev-list --left-right --cherry-pick F...E -- bar > actual &&
+	git name-rev --stdin --name-only --refs="*tags/F" --refs="*tags/E" --no-refs --refs="*tags/G" \
+		< actual > actual.named &&
+	test_cmp actual.named expect
+'
+
 cat >expect <<EOF
 +tags/F
 =tags/D
Signature · 1 line
2.11.0.rc2.152.g4d04e67
#5 lore raw

PATCH v2 3/5 name-rev: add support to discard refs by pattern match

In reply to Jacob Keller

From: Jacob Keller <[email protected]>

Extend name-rev further to support matching refs by adding --discard patterns. These patterns will limit the scope of refs by discarding any ref that matches at least one discard pattern. Checking the discard refs shall happen first, before checking the include --refs patterns. This will allow more flexibility to matching certain kinds of references.

Add tests and update Documentation for this change.

---
 Documentation/git-name-rev.txt |  7 +++++++
 builtin/name-rev.c             | 14 +++++++++++++-
 2 files changed, 20 insertions(+), 1 deletion(-)
Documentation/git-name-rev.txt +7 -0
index 7433627db12d..9b46e5ea9aae 100644
@@ -30,6 +30,13 @@ OPTIONS
 	given multiple times, use refs whose names match any of the given shell
 	patterns. Use `--no-refs` to clear any previous ref patterns given.
 
+--discard=<pattern>::
+	Do not use any ref whose name matches a given shell pattern. The
+	pattern can be one of branch name, tag name or fully qualified ref
+	name. If given multiple times, discard refs that match any of the given
+	shell patterns. Use `--no-discards` to clear the list of discard
+	patterns.
+
 --all::
 	List all commits reachable from all refs
 
builtin/name-rev.c +13 -1
index 000a2a700ed3..86479c17a7c9 100644
@@ -109,6 +109,7 @@ struct name_ref_data {
 	int tags_only;
 	int name_only;
 	struct string_list ref_filters;
+	struct string_list discard_filters;
 };
 
 static struct tip_table {
@@ -150,6 +151,15 @@ static int name_ref(const char *path, const struct object_id *oid, int flags, vo
 	if (data->tags_only && !starts_with(path, "refs/tags/"))
 		return 0;
 
+	if (data->discard_filters.nr) {
+		struct string_list_item *item;
+
+		for_each_string_list_item(item, &data->discard_filters) {
+			if (subpath_matches(path, item->string) >= 0)
+				return 0;
+		}
+	}
+
 	if (data->ref_filters.nr) {
 		struct string_list_item *item;
 		int matched = 0;
@@ -323,12 +333,14 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
 {
 	struct object_array revs = OBJECT_ARRAY_INIT;
 	int all = 0, transform_stdin = 0, allow_undefined = 1, always = 0, peel_tag = 0;
-	struct name_ref_data data = { 0, 0, STRING_LIST_INIT_NODUP };
+	struct name_ref_data data = { 0, 0, STRING_LIST_INIT_NODUP, STRING_LIST_INIT_NODUP };
 	struct option opts[] = {
 		OPT_BOOL(0, "name-only", &data.name_only, N_("print only names (no SHA-1)")),
 		OPT_BOOL(0, "tags", &data.tags_only, N_("only use tags to name the commits")),
 		OPT_STRING_LIST(0, "refs", &data.ref_filters, N_("pattern"),
 				   N_("only use refs matching <pattern>")),
+		OPT_STRING_LIST(0, "discard", &data.discard_filters, N_("pattern"),
+				   N_("ignore refs matching <pattern>")),
 		OPT_GROUP(""),
 		OPT_BOOL(0, "all", &all, N_("list all commits reachable from all refs")),
 		OPT_BOOL(0, "stdin", &transform_stdin, N_("read from stdin")),
Signature · 1 line
2.11.0.rc2.152.g4d04e67
#6 lore raw

PATCH v2 4/5 describe: teach --match to accept multiple patterns

In reply to Jacob Keller

From: Jacob Keller <[email protected]>

Teach --match to be accepted multiple times, accumulating a list of patterns to match into a string list. Each pattern is inclusive, such that a tag need only match one of the provided patterns to be considered for matching.

This extension is useful as it enables more flexibility in what tags match, and may avoid the need to run the describe command multiple times to get the same result.

Add tests and update the documentation for this change.

---
 Documentation/git-describe.txt |  5 ++++-
 builtin/describe.c             | 30 +++++++++++++++++++++++-------
 t/t6120-describe.sh            | 19 +++++++++++++++++++
 3 files changed, 46 insertions(+), 8 deletions(-)
Documentation/git-describe.txt +4 -1
index e4ac448ff565..7ad41e2f6ade 100644
@@ -83,7 +83,10 @@ OPTIONS
 --match <pattern>::
 	Only consider tags matching the given `glob(7)` pattern,
 	excluding the "refs/tags/" prefix.  This can be used to avoid
-	leaking private tags from the repository.
+	leaking private tags from the repository. If given multiple times, a
+	list of patterns will be accumulated, and tags matching any of the
+	patterns will be considered. Use `--no-match` to clear and reset the
+	list of patterns.
 
 --always::
 	Show uniquely abbreviated commit object as fallback.
builtin/describe.c +23 -7
index 01490a157efc..5cc9e9abe798 100644
@@ -28,7 +28,7 @@ static int abbrev = -1; /* unspecified */
 static int max_candidates = 10;
 static struct hashmap names;
 static int have_util;
-static const char *pattern;
+static struct string_list patterns = STRING_LIST_INIT_NODUP;
 static int always;
 static const char *dirty;
 
@@ -129,9 +129,24 @@ static int get_name(const char *path, const struct object_id *oid, int flag, voi
 	if (!all && !is_tag)
 		return 0;
 
-	/* Accept only tags that match the pattern, if given */
-	if (pattern && (!is_tag || wildmatch(pattern, path + 10, 0, NULL)))
-		return 0;
+	/*
+	 * If we're given patterns, accept only tags which match at least one
+	 * pattern.
+	 */
+	if (patterns.nr) {
+		struct string_list_item *item;
+
+		if (!is_tag)
+			return 0;
+
+		for_each_string_list_item(item, &patterns) {
+			if (!wildmatch(item->string, path + 10, 0, NULL))
+				break;
+
+			/* If we get here, no pattern matched. */
+			return 0;
+		}
+	}
 
 	/* Is it annotated? */
 	if (!peel_ref(path, peeled.hash)) {
@@ -404,7 +419,7 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
 			    N_("only output exact matches"), 0),
 		OPT_INTEGER(0, "candidates", &max_candidates,
 			    N_("consider <n> most recent tags (default: 10)")),
-		OPT_STRING(0, "match",       &pattern, N_("pattern"),
+		OPT_STRING_LIST(0, "match", &patterns, N_("pattern"),
 			   N_("only consider tags matching <pattern>")),
 		OPT_BOOL(0, "always",        &always,
 			N_("show abbreviated commit object as fallback")),
@@ -430,6 +445,7 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
 		die(_("--long is incompatible with --abbrev=0"));
 
 	if (contains) {
+		struct string_list_item *item;
 		struct argv_array args;
 
 		argv_array_init(&args);
@@ -440,8 +456,8 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
 			argv_array_push(&args, "--always");
 		if (!all) {
 			argv_array_push(&args, "--tags");
-			if (pattern)
-				argv_array_pushf(&args, "--refs=refs/tags/%s", pattern);
+			for_each_string_list_item(item, &patterns)
+				argv_array_pushf(&args, "--refs=refs/tags/%s", item->string);
 		}
 		if (argc)
 			argv_array_pushv(&args, argv);
t/t6120-describe.sh +19 -0
index 85f269411cb3..9e5db9b87a1f 100755
@@ -182,6 +182,10 @@ check_describe "test2-lightweight-*" --tags --match="test2-*"
 
 check_describe "test2-lightweight-*" --long --tags --match="test2-*" HEAD^
 
+check_describe "test1-lightweight-*" --long --tags --match="test1-*" --match="test2-*" HEAD^
+
+check_describe "test2-lightweight-*" --long --tags --match="test1-*" --no-match --match="test2-*" HEAD^
+
 test_expect_success 'name-rev with exact tags' '
 	echo A >expect &&
 	tag_object=$(git rev-parse refs/tags/A) &&
@@ -206,4 +210,19 @@ test_expect_success 'describe --contains with the exact tags' '
 	test_cmp expect actual
 '
 
+test_expect_success 'describe --contains and --match' '
+	echo "A^0" >expect &&
+	tagged_commit=$(git rev-parse "refs/tags/A^0") &&
+	test_must_fail git describe --contains --match="B" $tagged_commit &&
+	git describe --contains --match="B" --match="A" $tagged_commit >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'describe --contains and --no-match' '
+	echo "A^0" >expect &&
+	tagged_commit=$(git rev-parse "refs/tags/A^0") &&
+	git describe --contains --match="B" --no-match $tagged_commit >actual &&
+	test_cmp expect actual
+'
+
 test_done
Signature · 1 line
2.11.0.rc2.152.g4d04e67
#7 lore raw

PATCH v2 5/5 describe: teach describe negative pattern matches

In reply to Jacob Keller

From: Jacob Keller <[email protected]>

Teach git-describe the --discard option which will allow specifying a glob pattern of tags to ignore. This can be combined with the --match patterns to enable more flexibility in determining which tags to consider.

For example, suppose you wish to find the first official release tag that contains a certain commit. If we assume that official release tags are of the form "v*" and pre-release candidates include "*rc*" in their name, we can now find the first tag that introduces commit abcdef via:

  git describe --contains --match="v*" --discard="*rc*"

Add documentation and tests for this change.

---
 Documentation/git-describe.txt |  8 ++++++++
 builtin/describe.c             | 21 +++++++++++++++++++++
 2 files changed, 29 insertions(+)
Documentation/git-describe.txt +8 -0
index 7ad41e2f6ade..a89bbde207b2 100644
@@ -88,6 +88,14 @@ OPTIONS
 	patterns will be considered. Use `--no-match` to clear and reset the
 	list of patterns.
 
+--discard <pattern>::
+	Do not consider tags matching the given `glob(7)` pattern, excluding
+	the "refs/tags/" prefix. This can be used to narrow the tag space and
+	find only tags matching some meaningful criteria. If given multiple
+	times, a list of patterns will be accumulated and tags matching any
+	of the patterns will be discarded. Use `--no-discard` to clear and
+	reset the list of patterns.
+
 --always::
 	Show uniquely abbreviated commit object as fallback.
 
builtin/describe.c +21 -0
index 5cc9e9abe798..c09288ee6321 100644
@@ -29,6 +29,7 @@ static int max_candidates = 10;
 static struct hashmap names;
 static int have_util;
 static struct string_list patterns = STRING_LIST_INIT_NODUP;
+static struct string_list discard_patterns = STRING_LIST_INIT_NODUP;
 static int always;
 static const char *dirty;
 
@@ -130,6 +131,22 @@ static int get_name(const char *path, const struct object_id *oid, int flag, voi
 		return 0;
 
 	/*
+	 * If we're given discard patterns, first discard any tag which match
+	 * any of the discard pattern.
+	 */
+	if (discard_patterns.nr) {
+		struct string_list_item *item;
+
+		if (!is_tag)
+			return 0;
+
+		for_each_string_list_item(item, &discard_patterns) {
+			if (!wildmatch(item->string, path + 10, 0, NULL))
+				return 0;
+		}
+	}
+
+	/*
 	 * If we're given patterns, accept only tags which match at least one
 	 * pattern.
 	 */
@@ -421,6 +438,8 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
 			    N_("consider <n> most recent tags (default: 10)")),
 		OPT_STRING_LIST(0, "match", &patterns, N_("pattern"),
 			   N_("only consider tags matching <pattern>")),
+		OPT_STRING_LIST(0, "discard", &discard_patterns, N_("pattern"),
+			   N_("do not consider tags matching <pattern>")),
 		OPT_BOOL(0, "always",        &always,
 			N_("show abbreviated commit object as fallback")),
 		{OPTION_STRING, 0, "dirty",  &dirty, N_("mark"),
@@ -458,6 +477,8 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
 			argv_array_push(&args, "--tags");
 			for_each_string_list_item(item, &patterns)
 				argv_array_pushf(&args, "--refs=refs/tags/%s", item->string);
+			for_each_string_list_item(item, &discard_patterns)
+				argv_array_pushf(&args, "--discard=refs/tags/%s", item->string);
 		}
 		if (argc)
 			argv_array_pushv(&args, argv);
Signature · 1 line
2.11.0.rc2.152.g4d04e67