describe: teach describe negative pattern matches

Teach git-describe the `--exclude` 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 release tag that introduces the commit abcdef: git describe --contains --match="v*" --exclude="*rc*" abcdef Add documentation, tests, and completion for this change. Signed-off-by: Jacob Keller <jacob.keller@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jacob Keller committed Jan 18, 2017 at 15:06 UTC 77d21f29eaddb1fbe82e013ea42d4e0180bbdda2
4 files changed +40
Documentation/git-describe.txt
+10
@@ -88,6 +88,16 @@ OPTIONS
88 patterns will be considered. Use `--no-match` to clear and reset the
89 list of patterns.
90
91 +--exclude <pattern>::
92 + Do not consider tags matching the given `glob(7)` pattern, excluding
93 + the "refs/tags/" prefix. This can be used to narrow the tag space and
94 + find only tags matching some meaningful criteria. If given multiple
95 + times, a list of patterns will be accumulated and tags matching any
96 + of the patterns will be excluded. When combined with --match a tag will
97 + be considered when it matches at least one --match pattern and does not
98 + match any of the --exclude patterns. Use `--no-exclude` to clear and
99 + reset the list of patterns.
100 +
101 --always::
102 Show uniquely abbreviated commit object as fallback.
103
builtin/describe.c
+21
@@ -29,6 +29,7 @@ static int max_candidates = 10;
29 static struct hashmap names;
30 static int have_util;
31 static struct string_list patterns = STRING_LIST_INIT_NODUP;
32 +static struct string_list exclude_patterns = STRING_LIST_INIT_NODUP;
33 static int always;
34 static const char *dirty;
35
@@ -129,6 +130,22 @@ static int get_name(const char *path, const struct object_id *oid, int flag, voi
130 if (!all && !is_tag)
131 return 0;
132
133 + /*
134 + * If we're given exclude patterns, first exclude any tag which match
135 + * any of the exclude pattern.
136 + */
137 + if (exclude_patterns.nr) {
138 + struct string_list_item *item;
139 +
140 + if (!is_tag)
141 + return 0;
142 +
143 + for_each_string_list_item(item, &exclude_patterns) {
144 + if (!wildmatch(item->string, path + 10, 0, NULL))
145 + return 0;
146 + }
147 + }
148 +
149 /*
150 * If we're given patterns, accept only tags which match at least one
151 * pattern.
@@ -421,6 +438,8 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
438 N_("consider <n> most recent tags (default: 10)")),
439 OPT_STRING_LIST(0, "match", &patterns, N_("pattern"),
440 N_("only consider tags matching <pattern>")),
441 + OPT_STRING_LIST(0, "exclude", &exclude_patterns, N_("pattern"),
442 + N_("do not consider tags matching <pattern>")),
443 OPT_BOOL(0, "always", &always,
444 N_("show abbreviated commit object as fallback")),
445 {OPTION_STRING, 0, "dirty", &dirty, N_("mark"),
@@ -458,6 +477,8 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
477 argv_array_push(&args, "--tags");
478 for_each_string_list_item(item, &patterns)
479 argv_array_pushf(&args, "--refs=refs/tags/%s", item->string);
480 + for_each_string_list_item(item, &exclude_patterns)
481 + argv_array_pushf(&args, "--exclude=refs/tags/%s", item->string);
482 }
483 if (argc)
484 argv_array_pushv(&args, argv);
contrib/completion/git-completion.bash
+1
@@ -1198,6 +1198,7 @@ _git_describe ()
1198 __gitcomp "
1199 --all --tags --contains --abbrev= --candidates=
1200 --exact-match --debug --long --match --always
1201 + --exclude
1202 "
1203 return
1204 esac
t/t6120-describe.sh
+8
@@ -218,6 +218,14 @@ test_expect_success 'describe --contains and --match' '
218 test_cmp expect actual
219 '
220
221 +test_expect_success 'describe --exclude' '
222 + echo "c~1" >expect &&
223 + tagged_commit=$(git rev-parse "refs/tags/A^0") &&
224 + test_must_fail git describe --contains --match="B" $tagged_commit &&
225 + git describe --contains --match="?" --exclude="A" $tagged_commit >actual &&
226 + test_cmp expect actual
227 +'
228 +
229 test_expect_success 'describe --contains and --no-match' '
230 echo "A^0" >expect &&
231 tagged_commit=$(git rev-parse "refs/tags/A^0") &&