@@ -229,6 +229,38 @@ static void add_per_worktree_entries_to_dir(struct ref_dir *dir, const char *dir
229229 }
230230}
231231
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+
232264/*
233265 * Read the loose references from the namespace dirname into dir
234266 * (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,
257289 strbuf_add (& refname , dirname , dirnamelen );
258290
259291 while ((de = readdir (d )) != NULL ) {
260- struct object_id oid ;
261- int flag ;
262292 unsigned char dtype ;
263293
264294 if (de -> d_name [0 ] == '.' )
@@ -274,33 +304,7 @@ static void loose_fill_ref_dir(struct ref_store *ref_store,
274304 create_dir_entry (dir -> cache , refname .buf ,
275305 refname .len ));
276306 } 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 );
304308 }
305309 strbuf_setlen (& refname , dirnamelen );
306310 }
@@ -311,9 +315,59 @@ static void loose_fill_ref_dir(struct ref_store *ref_store,
311315 add_per_worktree_entries_to_dir (dir , dirname );
312316}
313317
314- 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 )
315367{
316368 if (!refs -> loose ) {
369+ struct ref_dir * dir ;
370+
317371 /*
318372 * Mark the top-level directory complete because we
319373 * are about to read the only subdirectory that can
@@ -324,12 +378,17 @@ static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs)
324378 /* We're going to fill the top level ourselves: */
325379 refs -> loose -> root -> flag &= ~REF_INCOMPLETE ;
326380
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+
327387 /*
328388 * Add an incomplete entry for "refs/" (to be filled
329389 * lazily):
330390 */
331- add_entry_to_dir (get_ref_dir (refs -> loose -> root ),
332- create_dir_entry (refs -> loose , "refs/" , 5 ));
391+ add_entry_to_dir (dir , create_dir_entry (refs -> loose , "refs/" , 5 ));
333392 }
334393 return refs -> loose ;
335394}
@@ -857,7 +916,7 @@ static struct ref_iterator *files_ref_iterator_begin(
857916 * disk, and re-reads it if not.
858917 */
859918
860- loose_iter = cache_ref_iterator_begin (get_loose_ref_cache (refs ),
919+ loose_iter = cache_ref_iterator_begin (get_loose_ref_cache (refs , flags ),
861920 prefix , ref_store -> repo , 1 );
862921
863922 /*
@@ -1217,7 +1276,7 @@ static int files_pack_refs(struct ref_store *ref_store,
12171276
12181277 packed_refs_lock (refs -> packed_ref_store , LOCK_DIE_ON_ERROR , & err );
12191278
1220- iter = cache_ref_iterator_begin (get_loose_ref_cache (refs ), NULL ,
1279+ iter = cache_ref_iterator_begin (get_loose_ref_cache (refs , 0 ), NULL ,
12211280 the_repository , 0 );
12221281 while ((ok = ref_iterator_advance (iter )) == ITER_OK ) {
12231282 /*
0 commit comments