Skip to content

Commit 0a19c15

Browse files
authored
Merge pull request libgit2#4629 from neithernut/enhance-glob-perf
refdb_fs: enhance performance of globbing
2 parents 81c9894 + 20a2b02 commit 0a19c15

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

src/refdb_fs.c

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -505,26 +505,53 @@ static int iter_load_loose_paths(refdb_fs_backend *backend, refdb_fs_iter *iter)
505505
git_iterator *fsit = NULL;
506506
git_iterator_options fsit_opts = GIT_ITERATOR_OPTIONS_INIT;
507507
const git_index_entry *entry = NULL;
508+
const char *ref_prefix = GIT_REFS_DIR;
509+
size_t ref_prefix_len = strlen(ref_prefix);
508510

509511
if (!backend->commonpath) /* do nothing if no commonpath for loose refs */
510512
return 0;
511513

512514
fsit_opts.flags = backend->iterator_flags;
513515

514-
if ((error = git_buf_printf(&path, "%s/refs", backend->commonpath)) < 0 ||
516+
if (iter->glob) {
517+
const char *last_sep = NULL;
518+
const char *pos;
519+
for (pos = iter->glob; *pos; ++pos) {
520+
switch (*pos) {
521+
case '?':
522+
case '*':
523+
case '[':
524+
case '\\':
525+
break;
526+
case '/':
527+
last_sep = pos;
528+
/* FALLTHROUGH */
529+
default:
530+
continue;
531+
}
532+
break;
533+
}
534+
if (last_sep) {
535+
ref_prefix = iter->glob;
536+
ref_prefix_len = (last_sep - ref_prefix) + 1;
537+
}
538+
}
539+
540+
if ((error = git_buf_printf(&path, "%s/", backend->commonpath)) < 0 ||
541+
(error = git_buf_put(&path, ref_prefix, ref_prefix_len)) < 0 ||
515542
(error = git_iterator_for_filesystem(&fsit, path.ptr, &fsit_opts)) < 0) {
516543
git_buf_free(&path);
517544
return error;
518545
}
519546

520-
error = git_buf_sets(&path, GIT_REFS_DIR);
547+
error = git_buf_sets(&path, ref_prefix);
521548

522549
while (!error && !git_iterator_advance(&entry, fsit)) {
523550
const char *ref_name;
524551
struct packref *ref;
525552
char *ref_dup;
526553

527-
git_buf_truncate(&path, strlen(GIT_REFS_DIR));
554+
git_buf_truncate(&path, ref_prefix_len);
528555
git_buf_puts(&path, entry->path);
529556
ref_name = git_buf_cstr(&path);
530557

0 commit comments

Comments
 (0)