@@ -101,13 +101,45 @@ int check_refname_format(const char *refname, int flags)
101
101
102
102
struct ref_entry ;
103
103
104
+ /*
105
+ * Information used (along with the information in ref_entry) to
106
+ * describe a single cached reference. This data structure only
107
+ * occurs embedded in a union in struct ref_entry, and only when
108
+ * (ref_entry->flag & REF_DIR) is zero.
109
+ */
104
110
struct ref_value {
105
111
unsigned char sha1 [20 ];
106
112
unsigned char peeled [20 ];
107
113
};
108
114
109
115
struct ref_cache ;
110
116
117
+ /*
118
+ * Information used (along with the information in ref_entry) to
119
+ * describe a level in the hierarchy of references. This data
120
+ * structure only occurs embedded in a union in struct ref_entry, and
121
+ * only when (ref_entry.flag & REF_DIR) is set. In that case,
122
+ * (ref_entry.flag & REF_INCOMPLETE) determines whether the references
123
+ * in the directory have already been read:
124
+ *
125
+ * (ref_entry.flag & REF_INCOMPLETE) unset -- a directory of loose
126
+ * or packed references, already read.
127
+ *
128
+ * (ref_entry.flag & REF_INCOMPLETE) set -- a directory of loose
129
+ * references that hasn't been read yet (nor has any of its
130
+ * subdirectories).
131
+ *
132
+ * Entries within a directory are stored within a growable array of
133
+ * pointers to ref_entries (entries, nr, alloc). Entries 0 <= i <
134
+ * sorted are sorted by their component name in strcmp() order and the
135
+ * remaining entries are unsorted.
136
+ *
137
+ * Loose references are read lazily, one directory at a time. When a
138
+ * directory of loose references is read, then all of the references
139
+ * in that directory are stored, and REF_INCOMPLETE stubs are created
140
+ * for any subdirectories, but the subdirectories themselves are not
141
+ * read. The reading is triggered by get_ref_dir().
142
+ */
111
143
struct ref_dir {
112
144
int nr , alloc ;
113
145
@@ -127,19 +159,33 @@ struct ref_dir {
127
159
128
160
/* ISSYMREF=0x01, ISPACKED=0x02, and ISBROKEN=0x04 are public interfaces */
129
161
#define REF_KNOWS_PEELED 0x08
162
+
163
+ /* ref_entry represents a directory of references */
130
164
#define REF_DIR 0x10
131
165
166
+ /*
167
+ * Entry has not yet been read from disk (used only for REF_DIR
168
+ * entries representing loose references)
169
+ */
170
+ #define REF_INCOMPLETE 0x20
171
+
132
172
/*
133
173
* A ref_entry represents either a reference or a "subdirectory" of
134
- * references. Each directory in the reference namespace is
135
- * represented by a ref_entry with (flags & REF_DIR) set and
136
- * containing a subdir member that holds the entries in that
137
- * directory. References are represented by a ref_entry with (flags &
138
- * REF_DIR) unset and a value member that describes the reference's
139
- * value. The flag member is at the ref_entry level, but it is also
140
- * needed to interpret the contents of the value field (in other
141
- * words, a ref_value object is not very much use without the
142
- * enclosing ref_entry).
174
+ * references.
175
+ *
176
+ * Each directory in the reference namespace is represented by a
177
+ * ref_entry with (flags & REF_DIR) set and containing a subdir member
178
+ * that holds the entries in that directory that have been read so
179
+ * far. If (flags & REF_INCOMPLETE) is set, then the directory and
180
+ * its subdirectories haven't been read yet. REF_INCOMPLETE is only
181
+ * used for loose reference directories.
182
+ *
183
+ * References are represented by a ref_entry with (flags & REF_DIR)
184
+ * unset and a value member that describes the reference's value. The
185
+ * flag member is at the ref_entry level, but it is also needed to
186
+ * interpret the contents of the value field (in other words, a
187
+ * ref_value object is not very much use without the enclosing
188
+ * ref_entry).
143
189
*
144
190
* Reference names cannot end with slash and directories' names are
145
191
* always stored with a trailing slash (except for the top-level
@@ -176,10 +222,18 @@ struct ref_entry {
176
222
char name [FLEX_ARRAY ];
177
223
};
178
224
225
+ static void read_loose_refs (const char * dirname , struct ref_dir * dir );
226
+
179
227
static struct ref_dir * get_ref_dir (struct ref_entry * entry )
180
228
{
229
+ struct ref_dir * dir ;
181
230
assert (entry -> flag & REF_DIR );
182
- return & entry -> u .subdir ;
231
+ dir = & entry -> u .subdir ;
232
+ if (entry -> flag & REF_INCOMPLETE ) {
233
+ read_loose_refs (entry -> name , dir );
234
+ entry -> flag &= ~REF_INCOMPLETE ;
235
+ }
236
+ return dir ;
183
237
}
184
238
185
239
static struct ref_entry * create_ref_entry (const char * refname ,
@@ -240,14 +294,14 @@ static void clear_ref_dir(struct ref_dir *dir)
240
294
* "refs/heads/") or "" for the top-level directory.
241
295
*/
242
296
static struct ref_entry * create_dir_entry (struct ref_cache * ref_cache ,
243
- const char * dirname )
297
+ const char * dirname , int incomplete )
244
298
{
245
299
struct ref_entry * direntry ;
246
300
int len = strlen (dirname );
247
301
direntry = xcalloc (1 , sizeof (struct ref_entry ) + len + 1 );
248
302
memcpy (direntry -> name , dirname , len + 1 );
249
303
direntry -> u .subdir .ref_cache = ref_cache ;
250
- direntry -> flag = REF_DIR ;
304
+ direntry -> flag = REF_DIR | ( incomplete ? REF_INCOMPLETE : 0 ) ;
251
305
return direntry ;
252
306
}
253
307
@@ -263,7 +317,7 @@ static void sort_ref_dir(struct ref_dir *dir);
263
317
/*
264
318
* Return the entry with the given refname from the ref_dir
265
319
* (non-recursively), sorting dir if necessary. Return NULL if no
266
- * such entry is found.
320
+ * such entry is found. dir must already be complete.
267
321
*/
268
322
static struct ref_entry * search_ref_dir (struct ref_dir * dir , const char * refname )
269
323
{
@@ -294,7 +348,7 @@ static struct ref_entry *search_ref_dir(struct ref_dir *dir, const char *refname
294
348
* recursing). Sort dir if necessary. subdirname must be a directory
295
349
* name (i.e., end in '/'). If mkdir is set, then create the
296
350
* directory if it is missing; otherwise, return NULL if the desired
297
- * directory cannot be found.
351
+ * directory cannot be found. dir must already be complete.
298
352
*/
299
353
static struct ref_dir * search_for_subdir (struct ref_dir * dir ,
300
354
const char * subdirname , int mkdir )
@@ -303,7 +357,13 @@ static struct ref_dir *search_for_subdir(struct ref_dir *dir,
303
357
if (!entry ) {
304
358
if (!mkdir )
305
359
return NULL ;
306
- entry = create_dir_entry (dir -> ref_cache , subdirname );
360
+ /*
361
+ * Since dir is complete, the absence of a subdir
362
+ * means that the subdir really doesn't exist;
363
+ * therefore, create an empty record for it but mark
364
+ * the record complete.
365
+ */
366
+ entry = create_dir_entry (dir -> ref_cache , subdirname , 0 );
307
367
add_entry_to_dir (dir , entry );
308
368
}
309
369
return get_ref_dir (entry );
@@ -313,10 +373,10 @@ static struct ref_dir *search_for_subdir(struct ref_dir *dir,
313
373
* If refname is a reference name, find the ref_dir within the dir
314
374
* tree that should hold refname. If refname is a directory name
315
375
* (i.e., ends in '/'), then return that ref_dir itself. dir must
316
- * represent the top-level directory. Sort ref_dirs and recurse into
317
- * subdirectories as necessary. If mkdir is set, then create any
318
- * missing directories; otherwise, return NULL if the desired
319
- * directory cannot be found.
376
+ * represent the top-level directory and must already be complete.
377
+ * Sort ref_dirs and recurse into subdirectories as necessary. If
378
+ * mkdir is set, then create any missing directories; otherwise,
379
+ * return NULL if the desired directory cannot be found.
320
380
*/
321
381
static struct ref_dir * find_containing_dir (struct ref_dir * dir ,
322
382
const char * refname , int mkdir )
@@ -760,7 +820,7 @@ static struct ref_dir *get_packed_refs(struct ref_cache *refs)
760
820
const char * packed_refs_file ;
761
821
FILE * f ;
762
822
763
- refs -> packed = create_dir_entry (refs , "" );
823
+ refs -> packed = create_dir_entry (refs , "" , 0 );
764
824
if (* refs -> name )
765
825
packed_refs_file = git_path_submodule (refs -> name , "packed-refs" );
766
826
else
@@ -781,9 +841,9 @@ void add_packed_ref(const char *refname, const unsigned char *sha1)
781
841
}
782
842
783
843
/*
784
- * Read the loose references for refs from the namespace dirname.
785
- * dirname must end with '/'. dir must be the directory entry
786
- * corresponding to dirname.
844
+ * Read the loose references from the namespace dirname into dir
845
+ * (without recursing). dirname must end with '/'. dir must be the
846
+ * directory entry corresponding to dirname.
787
847
*/
788
848
static void read_loose_refs (const char * dirname , struct ref_dir * dir )
789
849
{
@@ -824,8 +884,8 @@ static void read_loose_refs(const char *dirname, struct ref_dir *dir)
824
884
; /* silently ignore */
825
885
} else if (S_ISDIR (st .st_mode )) {
826
886
strbuf_addch (& refname , '/' );
827
- read_loose_refs ( refname . buf ,
828
- search_for_subdir ( dir , refname .buf , 1 ));
887
+ add_entry_to_dir ( dir ,
888
+ create_dir_entry ( refs , refname .buf , 1 ));
829
889
} else {
830
890
if (* refs -> name ) {
831
891
hashclr (sha1 );
@@ -850,10 +910,17 @@ static void read_loose_refs(const char *dirname, struct ref_dir *dir)
850
910
static struct ref_dir * get_loose_refs (struct ref_cache * refs )
851
911
{
852
912
if (!refs -> loose ) {
853
- refs -> loose = create_dir_entry (refs , "" );
854
- read_loose_refs ("refs/" ,
855
- search_for_subdir (get_ref_dir (refs -> loose ),
856
- "refs/" , 1 ));
913
+ /*
914
+ * Mark the top-level directory complete because we
915
+ * are about to read the only subdirectory that can
916
+ * hold references:
917
+ */
918
+ refs -> loose = create_dir_entry (refs , "" , 0 );
919
+ /*
920
+ * Create an incomplete entry for "refs/":
921
+ */
922
+ add_entry_to_dir (get_ref_dir (refs -> loose ),
923
+ create_dir_entry (refs , "refs/" , 1 ));
857
924
}
858
925
return get_ref_dir (refs -> loose );
859
926
}
0 commit comments