refs: fix some exclude patterns being ignored

`--exclude` from rev-list and rev-parse fails to exclude references if the next `--branches`, `--tags` or `--remotes` use the optional inclusive glob because those options are implemented as particular cases of `--glob=`, which itself requires that exclude patterns begin with 'refs/'. But it makes sense for `--branches=glob` and friends to be aware that exclusions patterns for them shouldn't be 'refs/<type>/' prefixed, the same way exclude patterns for `--branches` and friends (without the optional glob) already are. Let's record in 'refs.c:struct ref_filter' which context the exclude pattern is tied to, so refs.c:filter_refs() can decide if it should ignore the prefix when trying to match. Signed-off-by: Rafael Ascensão <rafa.almas@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Rafael Ascensão committed Nov 12, 2018 at 13:25 UTC 9ab9b5df0ee2a49e0c1354a667e1e00db903f516
2 files changed +16 -12
refs.c
+4
@@ -217,6 +217,7 @@ char *resolve_refdup(const char *refname, int resolve_flags,
217 /* The argument to filter_refs */
218 struct ref_filter {
219 const char *pattern;
220 + const char *prefix;
221 each_ref_fn *fn;
222 void *cb_data;
223 };
@@ -296,6 +297,8 @@ static int filter_refs(const char *refname, const struct object_id *oid,
297
298 if (wildmatch(filter->pattern, refname, 0))
299 return 0;
300 + if (filter->prefix)
301 + skip_prefix(refname, filter->prefix, &refname);
302 return filter->fn(refname, oid, flags, filter->cb_data);
303 }
304
@@ -458,6 +461,7 @@ int for_each_glob_ref_in(each_ref_fn fn, const char *pattern,
461 }
462
463 filter.pattern = real_pattern.buf;
464 + filter.prefix = prefix;
465 filter.fn = fn;
466 filter.cb_data = cb_data;
467 ret = for_each_ref(filter_refs, &filter);
t/t6018-rev-list-glob.sh
+12 -12
@@ -147,51 +147,51 @@ test_expect_success 'rev-parse accumulates multiple --exclude' '
147 compare rev-parse "--exclude=refs/remotes/* --exclude=refs/tags/* --all" --branches
148 '
149
150 -test_expect_failure 'rev-parse --exclude=glob with --branches=glob' '
150 +test_expect_success 'rev-parse --exclude=glob with --branches=glob' '
151 compare rev-parse "--exclude=subspace-* --branches=sub*" "subspace/one subspace/two"
152 '
153
154 -test_expect_failure 'rev-parse --exclude=glob with --tags=glob' '
154 +test_expect_success 'rev-parse --exclude=glob with --tags=glob' '
155 compare rev-parse "--exclude=qux/? --tags=qux/*" "qux/one qux/two"
156 '
157
158 -test_expect_failure 'rev-parse --exclude=glob with --remotes=glob' '
158 +test_expect_success 'rev-parse --exclude=glob with --remotes=glob' '
159 compare rev-parse "--exclude=upstream/? --remotes=upstream/*" "upstream/one upstream/two"
160 '
161
162 -test_expect_failure 'rev-parse --exclude=ref with --branches=glob' '
162 +test_expect_success 'rev-parse --exclude=ref with --branches=glob' '
163 compare rev-parse "--exclude=subspace-x --branches=sub*" "subspace/one subspace/two"
164 '
165
166 -test_expect_failure 'rev-parse --exclude=ref with --tags=glob' '
166 +test_expect_success 'rev-parse --exclude=ref with --tags=glob' '
167 compare rev-parse "--exclude=qux/x --tags=qux/*" "qux/one qux/two"
168 '
169
170 -test_expect_failure 'rev-parse --exclude=ref with --remotes=glob' '
170 +test_expect_success 'rev-parse --exclude=ref with --remotes=glob' '
171 compare rev-parse "--exclude=upstream/x --remotes=upstream/*" "upstream/one upstream/two"
172 '
173
174 -test_expect_failure 'rev-list --exclude=glob with --branches=glob' '
174 +test_expect_success 'rev-list --exclude=glob with --branches=glob' '
175 compare rev-list "--exclude=subspace-* --branches=sub*" "subspace/one subspace/two"
176 '
177
178 -test_expect_failure 'rev-list --exclude=glob with --tags=glob' '
178 +test_expect_success 'rev-list --exclude=glob with --tags=glob' '
179 compare rev-list "--exclude=qux/? --tags=qux/*" "qux/one qux/two"
180 '
181
182 -test_expect_failure 'rev-list --exclude=glob with --remotes=glob' '
182 +test_expect_success 'rev-list --exclude=glob with --remotes=glob' '
183 compare rev-list "--exclude=upstream/? --remotes=upstream/*" "upstream/one upstream/two"
184 '
185
186 -test_expect_failure 'rev-list --exclude=ref with --branches=glob' '
186 +test_expect_success 'rev-list --exclude=ref with --branches=glob' '
187 compare rev-list "--exclude=subspace-x --branches=sub*" "subspace/one subspace/two"
188 '
189
190 -test_expect_failure 'rev-list --exclude=ref with --tags=glob' '
190 +test_expect_success 'rev-list --exclude=ref with --tags=glob' '
191 compare rev-list "--exclude=qux/x --tags=qux/*" "qux/one qux/two"
192 '
193
194 -test_expect_failure 'rev-list --exclude=ref with --remotes=glob' '
194 +test_expect_success 'rev-list --exclude=ref with --remotes=glob' '
195 compare rev-list "--exclude=upstream/x --remotes=upstream/*" "upstream/one upstream/two"
196 '
197