Skip to content

Commit d0f00c1

Browse files
KarthikNayakgitster
authored andcommitted
refs: introduce refs_for_each_include_root_refs()
Introduce a new ref iteration flag `DO_FOR_EACH_INCLUDE_ROOT_REFS`, which will be used to iterate over regular refs plus pseudorefs and HEAD. Refs which fall outside the `refs/` and aren't either pseudorefs or HEAD are more of a grey area. This is because we don't block the users from creating such refs but they are not officially supported. Introduce `refs_for_each_include_root_refs()` which calls `do_for_each_ref()` with this newly introduced flag. In `refs/files-backend.c`, introduce a new function `add_pseudoref_and_head_entries()` to add pseudorefs and HEAD to the `ref_dir`. We then finally call `add_pseudoref_and_head_entries()` whenever the `DO_FOR_EACH_INCLUDE_ROOT_REFS` flag is set. Any new ref backend will also have to implement similar changes on its end. Signed-off-by: Karthik Nayak <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f768296 commit d0f00c1

File tree

4 files changed

+79
-5
lines changed

4 files changed

+79
-5
lines changed

refs.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,6 +1765,13 @@ int for_each_rawref(each_ref_fn fn, void *cb_data)
17651765
return refs_for_each_rawref(get_main_ref_store(the_repository), fn, cb_data);
17661766
}
17671767

1768+
int refs_for_each_include_root_refs(struct ref_store *refs, each_ref_fn fn,
1769+
void *cb_data)
1770+
{
1771+
return do_for_each_ref(refs, "", NULL, fn, 0,
1772+
DO_FOR_EACH_INCLUDE_ROOT_REFS, cb_data);
1773+
}
1774+
17681775
static int qsort_strcmp(const void *va, const void *vb)
17691776
{
17701777
const char *a = *(const char **)va;

refs.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,12 @@ int for_each_namespaced_ref(const char **exclude_patterns,
398398
int refs_for_each_rawref(struct ref_store *refs, each_ref_fn fn, void *cb_data);
399399
int for_each_rawref(each_ref_fn fn, void *cb_data);
400400

401+
/*
402+
* Iterates over all refs including root refs, i.e. pseudorefs and HEAD.
403+
*/
404+
int refs_for_each_include_root_refs(struct ref_store *refs, each_ref_fn fn,
405+
void *cb_data);
406+
401407
/*
402408
* Normalizes partial refs to their fully qualified form.
403409
* Will prepend <prefix> to the <pattern> if it doesn't start with 'refs/'.

refs/files-backend.c

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,59 @@ static void loose_fill_ref_dir(struct ref_store *ref_store,
315315
add_per_worktree_entries_to_dir(dir, dirname);
316316
}
317317

318-
static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs)
318+
/*
319+
* Add pseudorefs to the ref dir by parsing the directory for any files
320+
* which follow the pseudoref syntax.
321+
*/
322+
static void add_pseudoref_and_head_entries(struct ref_store *ref_store,
323+
struct ref_dir *dir,
324+
const char *dirname)
325+
{
326+
struct files_ref_store *refs =
327+
files_downcast(ref_store, REF_STORE_READ, "fill_ref_dir");
328+
struct strbuf path = STRBUF_INIT, refname = STRBUF_INIT;
329+
struct dirent *de;
330+
size_t dirnamelen;
331+
DIR *d;
332+
333+
files_ref_path(refs, &path, dirname);
334+
335+
d = opendir(path.buf);
336+
if (!d) {
337+
strbuf_release(&path);
338+
return;
339+
}
340+
341+
strbuf_addstr(&refname, dirname);
342+
dirnamelen = refname.len;
343+
344+
while ((de = readdir(d)) != NULL) {
345+
unsigned char dtype;
346+
347+
if (de->d_name[0] == '.')
348+
continue;
349+
if (ends_with(de->d_name, ".lock"))
350+
continue;
351+
strbuf_addstr(&refname, de->d_name);
352+
353+
dtype = get_dtype(de, &path, 1);
354+
if (dtype == DT_REG && (is_pseudoref(ref_store, de->d_name) ||
355+
is_headref(ref_store, de->d_name)))
356+
loose_fill_ref_dir_regular_file(refs, refname.buf, dir);
357+
358+
strbuf_setlen(&refname, dirnamelen);
359+
}
360+
strbuf_release(&refname);
361+
strbuf_release(&path);
362+
closedir(d);
363+
}
364+
365+
static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs,
366+
unsigned int flags)
319367
{
320368
if (!refs->loose) {
369+
struct ref_dir *dir;
370+
321371
/*
322372
* Mark the top-level directory complete because we
323373
* are about to read the only subdirectory that can
@@ -328,12 +378,17 @@ static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs)
328378
/* We're going to fill the top level ourselves: */
329379
refs->loose->root->flag &= ~REF_INCOMPLETE;
330380

381+
dir = get_ref_dir(refs->loose->root);
382+
383+
if (flags & DO_FOR_EACH_INCLUDE_ROOT_REFS)
384+
add_pseudoref_and_head_entries(dir->cache->ref_store, dir,
385+
refs->loose->root->name);
386+
331387
/*
332388
* Add an incomplete entry for "refs/" (to be filled
333389
* lazily):
334390
*/
335-
add_entry_to_dir(get_ref_dir(refs->loose->root),
336-
create_dir_entry(refs->loose, "refs/", 5));
391+
add_entry_to_dir(dir, create_dir_entry(refs->loose, "refs/", 5));
337392
}
338393
return refs->loose;
339394
}
@@ -861,7 +916,7 @@ static struct ref_iterator *files_ref_iterator_begin(
861916
* disk, and re-reads it if not.
862917
*/
863918

864-
loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs),
919+
loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs, flags),
865920
prefix, ref_store->repo, 1);
866921

867922
/*
@@ -1222,7 +1277,7 @@ static int files_pack_refs(struct ref_store *ref_store,
12221277

12231278
packed_refs_lock(refs->packed_ref_store, LOCK_DIE_ON_ERROR, &err);
12241279

1225-
iter = cache_ref_iterator_begin(get_loose_ref_cache(refs), NULL,
1280+
iter = cache_ref_iterator_begin(get_loose_ref_cache(refs, 0), NULL,
12261281
the_repository, 0);
12271282
while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
12281283
/*

refs/refs-internal.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,12 @@ enum do_for_each_ref_flags {
260260
* INCLUDE_BROKEN, since they are otherwise not included at all.
261261
*/
262262
DO_FOR_EACH_OMIT_DANGLING_SYMREFS = (1 << 2),
263+
264+
/*
265+
* Include root refs i.e. HEAD and pseudorefs along with the regular
266+
* refs.
267+
*/
268+
DO_FOR_EACH_INCLUDE_ROOT_REFS = (1 << 3),
263269
};
264270

265271
/*

0 commit comments

Comments
 (0)