refs: extract out `loose_fill_ref_dir_regular_file()`

Extract out the code for adding a single file to the loose ref dir as `loose_fill_ref_dir_regular_file()` from `loose_fill_ref_dir()` in `refs/files-backend.c`. This allows us to use this function independently in the following commits where we add code to also add pseudorefs to the ref dir. Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed Feb 23, 2024 at 11:01 UTC f768296cf1ac83d7b3216615da907dddb35fc6cd
1 file changed +33 -29
refs/files-backend.c
+33 -29
@@ -229,6 +229,38 @@ static void add_per_worktree_entries_to_dir(struct ref_dir *dir, const char *dir
229 }
230 }
231
232 +static void loose_fill_ref_dir_regular_file(struct files_ref_store *refs,
233 + const char *refname,
234 + struct ref_dir *dir)
235 +{
236 + struct object_id oid;
237 + int flag;
238 +
239 + if (!refs_resolve_ref_unsafe(&refs->base, refname, RESOLVE_REF_READING,
240 + &oid, &flag)) {
241 + oidclr(&oid);
242 + flag |= REF_ISBROKEN;
243 + } else if (is_null_oid(&oid)) {
244 + /*
245 + * It is so astronomically unlikely
246 + * that null_oid is the OID of an
247 + * actual object that we consider its
248 + * appearance in a loose reference
249 + * file to be repo corruption
250 + * (probably due to a software bug).
251 + */
252 + flag |= REF_ISBROKEN;
253 + }
254 +
255 + if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
256 + if (!refname_is_safe(refname))
257 + die("loose refname is dangerous: %s", refname);
258 + oidclr(&oid);
259 + flag |= REF_BAD_NAME | REF_ISBROKEN;
260 + }
261 + add_entry_to_dir(dir, create_ref_entry(refname, &oid, flag));
262 +}
263 +
264 /*
265 * Read the loose references from the namespace dirname into dir
266 * (without recursing). dirname must end with '/'. dir must be the
@@ -257,8 +289,6 @@ static void loose_fill_ref_dir(struct ref_store *ref_store,
289 strbuf_add(&refname, dirname, dirnamelen);
290
291 while ((de = readdir(d)) != NULL) {
260 - struct object_id oid;
261 - int flag;
292 unsigned char dtype;
293
294 if (de->d_name[0] == '.')
@@ -274,33 +304,7 @@ static void loose_fill_ref_dir(struct ref_store *ref_store,
304 create_dir_entry(dir->cache, refname.buf,
305 refname.len));
306 } else if (dtype == DT_REG) {
277 - if (!refs_resolve_ref_unsafe(&refs->base,
278 - refname.buf,
279 - RESOLVE_REF_READING,
280 - &oid, &flag)) {
281 - oidclr(&oid);
282 - flag |= REF_ISBROKEN;
283 - } else if (is_null_oid(&oid)) {
284 - /*
285 - * It is so astronomically unlikely
286 - * that null_oid is the OID of an
287 - * actual object that we consider its
288 - * appearance in a loose reference
289 - * file to be repo corruption
290 - * (probably due to a software bug).
291 - */
292 - flag |= REF_ISBROKEN;
293 - }
294 -
295 - if (check_refname_format(refname.buf,
296 - REFNAME_ALLOW_ONELEVEL)) {
297 - if (!refname_is_safe(refname.buf))
298 - die("loose refname is dangerous: %s", refname.buf);
299 - oidclr(&oid);
300 - flag |= REF_BAD_NAME | REF_ISBROKEN;
301 - }
302 - add_entry_to_dir(dir,
303 - create_ref_entry(refname.buf, &oid, flag));
307 + loose_fill_ref_dir_regular_file(refs, refname.buf, dir);
308 }
309 strbuf_setlen(&refname, dirnamelen);
310 }