Skip to content

Commit cbc46c0

Browse files
committed
Merge branch 'ps/reftable-exclude' into jc/cmake-unit-test-updates
* ps/reftable-exclude: refs/reftable: wire up support for exclude patterns reftable/reader: make table iterator reseekable t/unit-tests: introduce reftable library Makefile: stop listing test library objects twice builtin/receive-pack: fix exclude patterns when announcing refs refs: properly apply exclude patterns to namespaced refs
2 parents 6531f31 + 1869525 commit cbc46c0

16 files changed

+594
-201
lines changed

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,6 +1356,7 @@ UNIT_TEST_PROGRAMS += t-reftable-basics
13561356
UNIT_TEST_PROGRAMS += t-reftable-block
13571357
UNIT_TEST_PROGRAMS += t-reftable-merged
13581358
UNIT_TEST_PROGRAMS += t-reftable-pq
1359+
UNIT_TEST_PROGRAMS += t-reftable-reader
13591360
UNIT_TEST_PROGRAMS += t-reftable-readwrite
13601361
UNIT_TEST_PROGRAMS += t-reftable-record
13611362
UNIT_TEST_PROGRAMS += t-reftable-stack
@@ -1365,9 +1366,9 @@ UNIT_TEST_PROGRAMS += t-strcmp-offset
13651366
UNIT_TEST_PROGRAMS += t-trailer
13661367
UNIT_TEST_PROGRAMS += t-urlmatch-normalization
13671368
UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS))
1368-
UNIT_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(UNIT_TEST_PROGRAMS))
13691369
UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/test-lib.o
13701370
UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/lib-oid.o
1371+
UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/lib-reftable.o
13711372

13721373
# xdiff and reftable libs may in turn depend on what is in libgit.a
13731374
GITLIBS = common-main.o $(LIB_FILE) $(XDIFF_LIB) $(REFTABLE_LIB) $(LIB_FILE)
@@ -2725,6 +2726,7 @@ OBJECTS += $(FUZZ_OBJS)
27252726
OBJECTS += $(REFTABLE_OBJS) $(REFTABLE_TEST_OBJS)
27262727
OBJECTS += $(UNIT_TEST_OBJS)
27272728
OBJECTS += $(CLAR_TEST_OBJS)
2729+
OBJECTS += $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(UNIT_TEST_PROGRAMS))
27282730

27292731
ifndef NO_CURL
27302732
OBJECTS += http.o http-walker.o remote-curl.o
@@ -3864,9 +3866,7 @@ $(FUZZ_PROGRAMS): %: %.o oss-fuzz/dummy-cmd-main.o $(GITLIBS) GIT-LDFLAGS
38643866
-Wl,--allow-multiple-definition \
38653867
$(filter %.o,$^) $(filter %.a,$^) $(LIBS) $(LIB_FUZZING_ENGINE)
38663868

3867-
$(UNIT_TEST_PROGS): $(UNIT_TEST_BIN)/%$X: $(UNIT_TEST_DIR)/%.o \
3868-
$(UNIT_TEST_DIR)/test-lib.o \
3869-
$(UNIT_TEST_DIR)/lib-oid.o \
3869+
$(UNIT_TEST_PROGS): $(UNIT_TEST_BIN)/%$X: $(UNIT_TEST_DIR)/%.o $(UNIT_TEST_OBJS) \
38703870
$(GITLIBS) GIT-LDFLAGS
38713871
$(call mkdir_p_parent_template)
38723872
$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) \

builtin/receive-pack.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,12 +339,26 @@ static void show_one_alternate_ref(const struct object_id *oid,
339339
static void write_head_info(void)
340340
{
341341
static struct oidset seen = OIDSET_INIT;
342+
struct strvec excludes_vector = STRVEC_INIT;
343+
const char **exclude_patterns;
344+
345+
/*
346+
* We need access to the reference names both with and without their
347+
* namespace and thus cannot use `refs_for_each_namespaced_ref()`. We
348+
* thus have to adapt exclude patterns to carry the namespace prefix
349+
* ourselves.
350+
*/
351+
exclude_patterns = get_namespaced_exclude_patterns(
352+
hidden_refs_to_excludes(&hidden_refs),
353+
get_git_namespace(), &excludes_vector);
342354

343355
refs_for_each_fullref_in(get_main_ref_store(the_repository), "",
344-
hidden_refs_to_excludes(&hidden_refs),
345-
show_ref_cb, &seen);
356+
exclude_patterns, show_ref_cb, &seen);
346357
for_each_alternate_ref(show_one_alternate_ref, &seen);
358+
347359
oidset_clear(&seen);
360+
strvec_clear(&excludes_vector);
361+
348362
if (!sent_capabilities)
349363
show_ref("capabilities^{}", null_oid());
350364

refs.c

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,6 +1517,19 @@ const char **hidden_refs_to_excludes(const struct strvec *hide_refs)
15171517
return hide_refs->v;
15181518
}
15191519

1520+
const char **get_namespaced_exclude_patterns(const char **exclude_patterns,
1521+
const char *namespace,
1522+
struct strvec *out)
1523+
{
1524+
if (!namespace || !*namespace || !exclude_patterns || !*exclude_patterns)
1525+
return exclude_patterns;
1526+
1527+
for (size_t i = 0; exclude_patterns[i]; i++)
1528+
strvec_pushf(out, "%s%s", namespace, exclude_patterns[i]);
1529+
1530+
return out->v;
1531+
}
1532+
15201533
const char *find_descendant_ref(const char *dirname,
15211534
const struct string_list *extras,
15221535
const struct string_list *skip)
@@ -1634,11 +1647,19 @@ int refs_for_each_namespaced_ref(struct ref_store *refs,
16341647
const char **exclude_patterns,
16351648
each_ref_fn fn, void *cb_data)
16361649
{
1637-
struct strbuf buf = STRBUF_INIT;
1650+
struct strvec namespaced_exclude_patterns = STRVEC_INIT;
1651+
struct strbuf prefix = STRBUF_INIT;
16381652
int ret;
1639-
strbuf_addf(&buf, "%srefs/", get_git_namespace());
1640-
ret = do_for_each_ref(refs, buf.buf, exclude_patterns, fn, 0, 0, cb_data);
1641-
strbuf_release(&buf);
1653+
1654+
exclude_patterns = get_namespaced_exclude_patterns(exclude_patterns,
1655+
get_git_namespace(),
1656+
&namespaced_exclude_patterns);
1657+
1658+
strbuf_addf(&prefix, "%srefs/", get_git_namespace());
1659+
ret = do_for_each_ref(refs, prefix.buf, exclude_patterns, fn, 0, 0, cb_data);
1660+
1661+
strvec_clear(&namespaced_exclude_patterns);
1662+
strbuf_release(&prefix);
16421663
return ret;
16431664
}
16441665

@@ -1719,6 +1740,7 @@ int refs_for_each_fullref_in_prefixes(struct ref_store *ref_store,
17191740
const char **exclude_patterns,
17201741
each_ref_fn fn, void *cb_data)
17211742
{
1743+
struct strvec namespaced_exclude_patterns = STRVEC_INIT;
17221744
struct string_list prefixes = STRING_LIST_INIT_DUP;
17231745
struct string_list_item *prefix;
17241746
struct strbuf buf = STRBUF_INIT;
@@ -1730,6 +1752,10 @@ int refs_for_each_fullref_in_prefixes(struct ref_store *ref_store,
17301752
strbuf_addstr(&buf, namespace);
17311753
namespace_len = buf.len;
17321754

1755+
exclude_patterns = get_namespaced_exclude_patterns(exclude_patterns,
1756+
namespace,
1757+
&namespaced_exclude_patterns);
1758+
17331759
for_each_string_list_item(prefix, &prefixes) {
17341760
strbuf_addstr(&buf, prefix->string);
17351761
ret = refs_for_each_fullref_in(ref_store, buf.buf,
@@ -1739,6 +1765,7 @@ int refs_for_each_fullref_in_prefixes(struct ref_store *ref_store,
17391765
strbuf_setlen(&buf, namespace_len);
17401766
}
17411767

1768+
strvec_clear(&namespaced_exclude_patterns);
17421769
string_list_clear(&prefixes, 0);
17431770
strbuf_release(&buf);
17441771
return ret;

refs.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,15 @@ int ref_is_hidden(const char *, const char *, const struct strvec *);
859859
*/
860860
const char **hidden_refs_to_excludes(const struct strvec *hide_refs);
861861

862+
/*
863+
* Prefix all exclude patterns with the namespace, if any. This is required
864+
* because exclude patterns apply to the stripped reference name, not the full
865+
* reference name with the namespace.
866+
*/
867+
const char **get_namespaced_exclude_patterns(const char **exclude_patterns,
868+
const char *namespace,
869+
struct strvec *out);
870+
862871
/* Is this a per-worktree ref living in the refs/ namespace? */
863872
int is_per_worktree_ref(const char *refname);
864873

refs/reftable-backend.c

Lines changed: 130 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "../reftable/reftable-iterator.h"
2222
#include "../setup.h"
2323
#include "../strmap.h"
24+
#include "../trace2.h"
2425
#include "parse.h"
2526
#include "refs-internal.h"
2627

@@ -447,10 +448,81 @@ struct reftable_ref_iterator {
447448

448449
const char *prefix;
449450
size_t prefix_len;
451+
char **exclude_patterns;
452+
size_t exclude_patterns_index;
453+
size_t exclude_patterns_strlen;
450454
unsigned int flags;
451455
int err;
452456
};
453457

458+
/*
459+
* Handle exclude patterns. Returns either `1`, which tells the caller that the
460+
* current reference shall not be shown. Or `0`, which indicates that it should
461+
* be shown.
462+
*/
463+
static int should_exclude_current_ref(struct reftable_ref_iterator *iter)
464+
{
465+
while (iter->exclude_patterns[iter->exclude_patterns_index]) {
466+
const char *pattern = iter->exclude_patterns[iter->exclude_patterns_index];
467+
char *ref_after_pattern;
468+
int cmp;
469+
470+
/*
471+
* Lazily cache the pattern length so that we don't have to
472+
* recompute it every time this function is called.
473+
*/
474+
if (!iter->exclude_patterns_strlen)
475+
iter->exclude_patterns_strlen = strlen(pattern);
476+
477+
/*
478+
* When the reference name is lexicographically bigger than the
479+
* current exclude pattern we know that it won't ever match any
480+
* of the following references, either. We thus advance to the
481+
* next pattern and re-check whether it matches.
482+
*
483+
* Otherwise, if it's smaller, then we do not have a match and
484+
* thus want to show the current reference.
485+
*/
486+
cmp = strncmp(iter->ref.refname, pattern,
487+
iter->exclude_patterns_strlen);
488+
if (cmp > 0) {
489+
iter->exclude_patterns_index++;
490+
iter->exclude_patterns_strlen = 0;
491+
continue;
492+
}
493+
if (cmp < 0)
494+
return 0;
495+
496+
/*
497+
* The reference shares a prefix with the exclude pattern and
498+
* shall thus be omitted. We skip all references that match the
499+
* pattern by seeking to the first reference after the block of
500+
* matches.
501+
*
502+
* This is done by appending the highest possible character to
503+
* the pattern. Consequently, all references that have the
504+
* pattern as prefix and whose suffix starts with anything in
505+
* the range [0x00, 0xfe] are skipped. And given that 0xff is a
506+
* non-printable character that shouldn't ever be in a ref name,
507+
* we'd not yield any such record, either.
508+
*
509+
* Note that the seeked-to reference may also be excluded. This
510+
* is not handled here though, but the caller is expected to
511+
* loop and re-verify the next reference for us.
512+
*/
513+
ref_after_pattern = xstrfmt("%s%c", pattern, 0xff);
514+
iter->err = reftable_iterator_seek_ref(&iter->iter, ref_after_pattern);
515+
iter->exclude_patterns_index++;
516+
iter->exclude_patterns_strlen = 0;
517+
trace2_counter_add(TRACE2_COUNTER_ID_REFTABLE_RESEEKS, 1);
518+
519+
free(ref_after_pattern);
520+
return 1;
521+
}
522+
523+
return 0;
524+
}
525+
454526
static int reftable_ref_iterator_advance(struct ref_iterator *ref_iterator)
455527
{
456528
struct reftable_ref_iterator *iter =
@@ -481,6 +553,9 @@ static int reftable_ref_iterator_advance(struct ref_iterator *ref_iterator)
481553
break;
482554
}
483555

556+
if (iter->exclude_patterns && should_exclude_current_ref(iter))
557+
continue;
558+
484559
if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
485560
parse_worktree_ref(iter->ref.refname, NULL, NULL, NULL) !=
486561
REF_WORKTREE_CURRENT)
@@ -570,6 +645,11 @@ static int reftable_ref_iterator_abort(struct ref_iterator *ref_iterator)
570645
(struct reftable_ref_iterator *)ref_iterator;
571646
reftable_ref_record_release(&iter->ref);
572647
reftable_iterator_destroy(&iter->iter);
648+
if (iter->exclude_patterns) {
649+
for (size_t i = 0; iter->exclude_patterns[i]; i++)
650+
free(iter->exclude_patterns[i]);
651+
free(iter->exclude_patterns);
652+
}
573653
free(iter);
574654
return ITER_DONE;
575655
}
@@ -580,9 +660,53 @@ static struct ref_iterator_vtable reftable_ref_iterator_vtable = {
580660
.abort = reftable_ref_iterator_abort
581661
};
582662

663+
static int qsort_strcmp(const void *va, const void *vb)
664+
{
665+
const char *a = *(const char **)va;
666+
const char *b = *(const char **)vb;
667+
return strcmp(a, b);
668+
}
669+
670+
static char **filter_exclude_patterns(const char **exclude_patterns)
671+
{
672+
size_t filtered_size = 0, filtered_alloc = 0;
673+
char **filtered = NULL;
674+
675+
if (!exclude_patterns)
676+
return NULL;
677+
678+
for (size_t i = 0; ; i++) {
679+
const char *exclude_pattern = exclude_patterns[i];
680+
int has_glob = 0;
681+
682+
if (!exclude_pattern)
683+
break;
684+
685+
for (const char *p = exclude_pattern; *p; p++) {
686+
has_glob = is_glob_special(*p);
687+
if (has_glob)
688+
break;
689+
}
690+
if (has_glob)
691+
continue;
692+
693+
ALLOC_GROW(filtered, filtered_size + 1, filtered_alloc);
694+
filtered[filtered_size++] = xstrdup(exclude_pattern);
695+
}
696+
697+
if (filtered_size) {
698+
QSORT(filtered, filtered_size, qsort_strcmp);
699+
ALLOC_GROW(filtered, filtered_size + 1, filtered_alloc);
700+
filtered[filtered_size++] = NULL;
701+
}
702+
703+
return filtered;
704+
}
705+
583706
static struct reftable_ref_iterator *ref_iterator_for_stack(struct reftable_ref_store *refs,
584707
struct reftable_stack *stack,
585708
const char *prefix,
709+
const char **exclude_patterns,
586710
int flags)
587711
{
588712
struct reftable_ref_iterator *iter;
@@ -595,6 +719,7 @@ static struct reftable_ref_iterator *ref_iterator_for_stack(struct reftable_ref_
595719
iter->base.oid = &iter->oid;
596720
iter->flags = flags;
597721
iter->refs = refs;
722+
iter->exclude_patterns = filter_exclude_patterns(exclude_patterns);
598723

599724
ret = refs->err;
600725
if (ret)
@@ -616,7 +741,7 @@ static struct reftable_ref_iterator *ref_iterator_for_stack(struct reftable_ref_
616741

617742
static struct ref_iterator *reftable_be_iterator_begin(struct ref_store *ref_store,
618743
const char *prefix,
619-
const char **exclude_patterns UNUSED,
744+
const char **exclude_patterns,
620745
unsigned int flags)
621746
{
622747
struct reftable_ref_iterator *main_iter, *worktree_iter;
@@ -627,7 +752,8 @@ static struct ref_iterator *reftable_be_iterator_begin(struct ref_store *ref_sto
627752
required_flags |= REF_STORE_ODB;
628753
refs = reftable_be_downcast(ref_store, required_flags, "ref_iterator_begin");
629754

630-
main_iter = ref_iterator_for_stack(refs, refs->main_stack, prefix, flags);
755+
main_iter = ref_iterator_for_stack(refs, refs->main_stack, prefix,
756+
exclude_patterns, flags);
631757

632758
/*
633759
* The worktree stack is only set when we're in an actual worktree
@@ -641,7 +767,8 @@ static struct ref_iterator *reftable_be_iterator_begin(struct ref_store *ref_sto
641767
* Otherwise we merge both the common and the per-worktree refs into a
642768
* single iterator.
643769
*/
644-
worktree_iter = ref_iterator_for_stack(refs, refs->worktree_stack, prefix, flags);
770+
worktree_iter = ref_iterator_for_stack(refs, refs->worktree_stack, prefix,
771+
exclude_patterns, flags);
645772
return merge_ref_iterator_begin(&worktree_iter->base, &main_iter->base,
646773
ref_iterator_select, NULL);
647774
}

reftable/reader.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ static int table_iter_seek_to(struct table_iter *ti, uint64_t off, uint8_t typ)
328328
ti->typ = block_reader_type(&ti->br);
329329
ti->block_off = off;
330330
block_iter_seek_start(&ti->bi, &ti->br);
331+
ti->is_finished = 0;
331332
return 0;
332333
}
333334

0 commit comments

Comments
 (0)