Skip to content

Commit 120b671

Browse files
pks-tgitster
authored andcommitted
refs/files: extract function to iterate through root refs
Extract a new function that can be used to iterate through all root refs known to the "files" backend. This will be used in the next commit, where we start to teach ref backends to remove themselves. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 66275a6 commit 120b671

File tree

1 file changed

+42
-9
lines changed

1 file changed

+42
-9
lines changed

refs/files-backend.c

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -323,25 +323,23 @@ static void loose_fill_ref_dir(struct ref_store *ref_store,
323323
add_per_worktree_entries_to_dir(dir, dirname);
324324
}
325325

326-
/*
327-
* Add root refs to the ref dir by parsing the directory for any files which
328-
* follow the root ref syntax.
329-
*/
330-
static void add_root_refs(struct files_ref_store *refs,
331-
struct ref_dir *dir)
326+
static int for_each_root_ref(struct files_ref_store *refs,
327+
int (*cb)(const char *refname, void *cb_data),
328+
void *cb_data)
332329
{
333330
struct strbuf path = STRBUF_INIT, refname = STRBUF_INIT;
334331
const char *dirname = refs->loose->root->name;
335332
struct dirent *de;
336333
size_t dirnamelen;
334+
int ret;
337335
DIR *d;
338336

339337
files_ref_path(refs, &path, dirname);
340338

341339
d = opendir(path.buf);
342340
if (!d) {
343341
strbuf_release(&path);
344-
return;
342+
return -1;
345343
}
346344

347345
strbuf_addstr(&refname, dirname);
@@ -357,14 +355,49 @@ static void add_root_refs(struct files_ref_store *refs,
357355
strbuf_addstr(&refname, de->d_name);
358356

359357
dtype = get_dtype(de, &path, 1);
360-
if (dtype == DT_REG && is_root_ref(de->d_name))
361-
loose_fill_ref_dir_regular_file(refs, refname.buf, dir);
358+
if (dtype == DT_REG && is_root_ref(de->d_name)) {
359+
ret = cb(refname.buf, cb_data);
360+
if (ret)
361+
goto done;
362+
}
362363

363364
strbuf_setlen(&refname, dirnamelen);
364365
}
366+
367+
ret = 0;
368+
369+
done:
365370
strbuf_release(&refname);
366371
strbuf_release(&path);
367372
closedir(d);
373+
return ret;
374+
}
375+
376+
struct fill_root_ref_data {
377+
struct files_ref_store *refs;
378+
struct ref_dir *dir;
379+
};
380+
381+
static int fill_root_ref(const char *refname, void *cb_data)
382+
{
383+
struct fill_root_ref_data *data = cb_data;
384+
loose_fill_ref_dir_regular_file(data->refs, refname, data->dir);
385+
return 0;
386+
}
387+
388+
/*
389+
* Add root refs to the ref dir by parsing the directory for any files which
390+
* follow the root ref syntax.
391+
*/
392+
static void add_root_refs(struct files_ref_store *refs,
393+
struct ref_dir *dir)
394+
{
395+
struct fill_root_ref_data data = {
396+
.refs = refs,
397+
.dir = dir,
398+
};
399+
400+
for_each_root_ref(refs, fill_root_ref, &data);
368401
}
369402

370403
static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs,

0 commit comments

Comments
 (0)