@@ -811,9 +811,13 @@ static struct ref_cache {
811
811
struct ref_cache * next ;
812
812
struct ref_entry * loose ;
813
813
struct ref_entry * packed ;
814
- /* The submodule name, or "" for the main repo. */
815
- char name [FLEX_ARRAY ];
816
- } * ref_cache ;
814
+ /*
815
+ * The submodule name, or "" for the main repo. We allocate
816
+ * length 1 rather than FLEX_ARRAY so that the main ref_cache
817
+ * is initialized correctly.
818
+ */
819
+ char name [1 ];
820
+ } ref_cache , * submodule_ref_caches ;
817
821
818
822
static void clear_packed_ref_cache (struct ref_cache * refs )
819
823
{
@@ -851,18 +855,18 @@ static struct ref_cache *create_ref_cache(const char *submodule)
851
855
*/
852
856
static struct ref_cache * get_ref_cache (const char * submodule )
853
857
{
854
- struct ref_cache * refs = ref_cache ;
855
- if (!submodule )
856
- submodule = "" ;
857
- while (refs ) {
858
+ struct ref_cache * refs ;
859
+
860
+ if (!submodule || !* submodule )
861
+ return & ref_cache ;
862
+
863
+ for (refs = submodule_ref_caches ; refs ; refs = refs -> next )
858
864
if (!strcmp (submodule , refs -> name ))
859
865
return refs ;
860
- refs = refs -> next ;
861
- }
862
866
863
867
refs = create_ref_cache (submodule );
864
- refs -> next = ref_cache ;
865
- ref_cache = refs ;
868
+ refs -> next = submodule_ref_caches ;
869
+ submodule_ref_caches = refs ;
866
870
return refs ;
867
871
}
868
872
@@ -1011,8 +1015,8 @@ static struct ref_dir *get_packed_refs(struct ref_cache *refs)
1011
1015
1012
1016
void add_packed_ref (const char * refname , const unsigned char * sha1 )
1013
1017
{
1014
- add_ref (get_packed_refs (get_ref_cache ( NULL ) ),
1015
- create_ref_entry (refname , sha1 , REF_ISPACKED , 1 ));
1018
+ add_ref (get_packed_refs (& ref_cache ),
1019
+ create_ref_entry (refname , sha1 , REF_ISPACKED , 1 ));
1016
1020
}
1017
1021
1018
1022
/*
@@ -1187,7 +1191,7 @@ int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sh
1187
1191
*/
1188
1192
static struct ref_entry * get_packed_ref (const char * refname )
1189
1193
{
1190
- return find_ref (get_packed_refs (get_ref_cache ( NULL ) ), refname );
1194
+ return find_ref (get_packed_refs (& ref_cache ), refname );
1191
1195
}
1192
1196
1193
1197
const char * resolve_ref_unsafe (const char * refname , unsigned char * sha1 , int reading , int * flag )
@@ -1592,7 +1596,7 @@ int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
1592
1596
1593
1597
int for_each_ref (each_ref_fn fn , void * cb_data )
1594
1598
{
1595
- return do_for_each_ref (get_ref_cache ( NULL ) , "" , fn , 0 , 0 , cb_data );
1599
+ return do_for_each_ref (& ref_cache , "" , fn , 0 , 0 , cb_data );
1596
1600
}
1597
1601
1598
1602
int for_each_ref_submodule (const char * submodule , each_ref_fn fn , void * cb_data )
@@ -1602,7 +1606,7 @@ int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
1602
1606
1603
1607
int for_each_ref_in (const char * prefix , each_ref_fn fn , void * cb_data )
1604
1608
{
1605
- return do_for_each_ref (get_ref_cache ( NULL ) , prefix , fn , strlen (prefix ), 0 , cb_data );
1609
+ return do_for_each_ref (& ref_cache , prefix , fn , strlen (prefix ), 0 , cb_data );
1606
1610
}
1607
1611
1608
1612
int for_each_ref_in_submodule (const char * submodule , const char * prefix ,
@@ -1643,7 +1647,7 @@ int for_each_remote_ref_submodule(const char *submodule, each_ref_fn fn, void *c
1643
1647
1644
1648
int for_each_replace_ref (each_ref_fn fn , void * cb_data )
1645
1649
{
1646
- return do_for_each_ref (get_ref_cache ( NULL ) , "refs/replace/" , fn , 13 , 0 , cb_data );
1650
+ return do_for_each_ref (& ref_cache , "refs/replace/" , fn , 13 , 0 , cb_data );
1647
1651
}
1648
1652
1649
1653
int head_ref_namespaced (each_ref_fn fn , void * cb_data )
@@ -1666,7 +1670,7 @@ int for_each_namespaced_ref(each_ref_fn fn, void *cb_data)
1666
1670
struct strbuf buf = STRBUF_INIT ;
1667
1671
int ret ;
1668
1672
strbuf_addf (& buf , "%srefs/" , get_git_namespace ());
1669
- ret = do_for_each_ref (get_ref_cache ( NULL ) , buf .buf , fn , 0 , 0 , cb_data );
1673
+ ret = do_for_each_ref (& ref_cache , buf .buf , fn , 0 , 0 , cb_data );
1670
1674
strbuf_release (& buf );
1671
1675
return ret ;
1672
1676
}
@@ -1708,7 +1712,7 @@ int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data)
1708
1712
1709
1713
int for_each_rawref (each_ref_fn fn , void * cb_data )
1710
1714
{
1711
- return do_for_each_ref (get_ref_cache ( NULL ) , "" , fn , 0 ,
1715
+ return do_for_each_ref (& ref_cache , "" , fn , 0 ,
1712
1716
DO_FOR_EACH_INCLUDE_BROKEN , cb_data );
1713
1717
}
1714
1718
@@ -1914,7 +1918,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
1914
1918
* name is a proper prefix of our refname.
1915
1919
*/
1916
1920
if (missing &&
1917
- !is_refname_available (refname , NULL , get_packed_refs (get_ref_cache ( NULL ) ))) {
1921
+ !is_refname_available (refname , NULL , get_packed_refs (& ref_cache ))) {
1918
1922
last_errno = ENOTDIR ;
1919
1923
goto error_return ;
1920
1924
}
@@ -2103,7 +2107,7 @@ int pack_refs(unsigned int flags)
2103
2107
2104
2108
write_or_die (cbdata .fd , PACKED_REFS_HEADER , strlen (PACKED_REFS_HEADER ));
2105
2109
2106
- do_for_each_entry (get_ref_cache ( NULL ) , "" , pack_one_ref , & cbdata );
2110
+ do_for_each_entry (& ref_cache , "" , pack_one_ref , & cbdata );
2107
2111
if (commit_lock_file (& packlock ) < 0 )
2108
2112
die_errno ("unable to overwrite old ref-pack file" );
2109
2113
prune_refs (cbdata .ref_to_prune );
@@ -2161,7 +2165,6 @@ static int repack_ref_fn(struct ref_entry *entry, void *cb_data)
2161
2165
static int repack_without_ref (const char * refname )
2162
2166
{
2163
2167
int fd ;
2164
- struct ref_cache * refs = get_ref_cache (NULL );
2165
2168
struct ref_dir * packed ;
2166
2169
2167
2170
if (!get_packed_ref (refname ))
@@ -2172,8 +2175,8 @@ static int repack_without_ref(const char *refname)
2172
2175
unable_to_lock_error (git_path ("packed-refs" ), errno );
2173
2176
return error ("cannot delete '%s' from packed refs" , refname );
2174
2177
}
2175
- clear_packed_ref_cache (refs );
2176
- packed = get_packed_refs (refs );
2178
+ clear_packed_ref_cache (& ref_cache );
2179
+ packed = get_packed_refs (& ref_cache );
2177
2180
/* Remove refname from the cache. */
2178
2181
if (remove_entry (packed , refname ) == -1 ) {
2179
2182
/*
@@ -2213,7 +2216,7 @@ int delete_ref(const char *refname, const unsigned char *sha1, int delopt)
2213
2216
ret |= repack_without_ref (lock -> ref_name );
2214
2217
2215
2218
unlink_or_warn (git_path ("logs/%s" , lock -> ref_name ));
2216
- clear_loose_ref_cache (get_ref_cache ( NULL ) );
2219
+ clear_loose_ref_cache (& ref_cache );
2217
2220
unlock_ref (lock );
2218
2221
return ret ;
2219
2222
}
@@ -2235,7 +2238,6 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
2235
2238
struct stat loginfo ;
2236
2239
int log = !lstat (git_path ("logs/%s" , oldrefname ), & loginfo );
2237
2240
const char * symref = NULL ;
2238
- struct ref_cache * refs = get_ref_cache (NULL );
2239
2241
2240
2242
if (log && S_ISLNK (loginfo .st_mode ))
2241
2243
return error ("reflog for %s is a symlink" , oldrefname );
@@ -2247,10 +2249,10 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
2247
2249
if (!symref )
2248
2250
return error ("refname %s not found" , oldrefname );
2249
2251
2250
- if (!is_refname_available (newrefname , oldrefname , get_packed_refs (refs )))
2252
+ if (!is_refname_available (newrefname , oldrefname , get_packed_refs (& ref_cache )))
2251
2253
return 1 ;
2252
2254
2253
- if (!is_refname_available (newrefname , oldrefname , get_loose_refs (refs )))
2255
+ if (!is_refname_available (newrefname , oldrefname , get_loose_refs (& ref_cache )))
2254
2256
return 1 ;
2255
2257
2256
2258
if (log && rename (git_path ("logs/%s" , oldrefname ), git_path (TMP_RENAMED_LOG )))
@@ -2506,7 +2508,7 @@ int write_ref_sha1(struct ref_lock *lock,
2506
2508
unlock_ref (lock );
2507
2509
return -1 ;
2508
2510
}
2509
- clear_loose_ref_cache (get_ref_cache ( NULL ) );
2511
+ clear_loose_ref_cache (& ref_cache );
2510
2512
if (log_ref_write (lock -> ref_name , lock -> old_sha1 , sha1 , logmsg ) < 0 ||
2511
2513
(strcmp (lock -> ref_name , lock -> orig_ref_name ) &&
2512
2514
log_ref_write (lock -> orig_ref_name , lock -> old_sha1 , sha1 , logmsg ) < 0 )) {
0 commit comments