describe: --match=<pattern> must limit the refs even when used with --all

The logic to limit the refs used for describing with a matching pattern with --match=<pattern> parameter was implemented incorrectly when --all is in effect. It just demoted a ref that did not match the pattern to lower priority---if there aren't other refs with higher priority that describe the given commit, such an unmatching ref was still used. When --match is used, reject refs that do not match the given criteria, so that with or without --all, the output will only use refs that match the pattern. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Feb 28, 2013 at 13:53 UTC 46e1d6eb4dcdd2952149fae214aeaa32f29c8df5
1 file changed +20 -21
builtin/describe.c
+20 -21
@@ -137,40 +137,39 @@ static void add_to_known_names(const char *path,
137
138 static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
139 {
140 - int might_be_tag = !prefixcmp(path, "refs/tags/");
140 + int is_tag = !prefixcmp(path, "refs/tags/");
141 unsigned char peeled[20];
142 - int is_tag, prio;
142 + int is_annotated, prio;
143
144 - if (!all && !might_be_tag)
144 + /* Reject anything outside refs/tags/ unless --all */
145 + if (!all && !is_tag)
146 return 0;
147
148 + /* Accept only tags that match the pattern, if given */
149 + if (pattern && (!is_tag || fnmatch(pattern, path + 10, 0)))
150 + return 0;
151 +
152 + /* Is it annotated? */
153 if (!peel_ref(path, peeled)) {
148 - is_tag = !!hashcmp(sha1, peeled);
154 + is_annotated = !!hashcmp(sha1, peeled);
155 } else {
156 hashcpy(peeled, sha1);
151 - is_tag = 0;
157 + is_annotated = 0;
158 }
159
154 - /* If --all, then any refs are used.
155 - * If --tags, then any tags are used.
156 - * Otherwise only annotated tags are used.
160 + /*
161 + * By default, we only use annotated tags, but with --tags
162 + * we fall back to lightweight ones (even without --tags,
163 + * we still remember lightweight ones, only to give hints
164 + * in an error message). --all allows any refs to be used.
165 */
158 - if (might_be_tag) {
159 - if (is_tag)
160 - prio = 2;
161 - else
162 - prio = 1;
163 -
164 - if (pattern && fnmatch(pattern, path + 10, 0))
165 - prio = 0;
166 - }
166 + if (is_annotated)
167 + prio = 2;
168 + else if (is_tag)
169 + prio = 1;
170 else
171 prio = 0;
172
170 - if (!all) {
171 - if (!prio)
172 - return 0;
173 - }
173 add_to_known_names(all ? path + 5 : path + 10, peeled, prio, sha1);
174 return 0;
175 }