@@ -933,6 +933,10 @@ static void clear_loose_ref_cache(struct ref_cache *refs)
933
933
}
934
934
}
935
935
936
+ /*
937
+ * Create a new submodule ref cache and add it to the internal
938
+ * set of caches.
939
+ */
936
940
static struct ref_cache * create_ref_cache (const char * submodule )
937
941
{
938
942
int len ;
@@ -942,16 +946,12 @@ static struct ref_cache *create_ref_cache(const char *submodule)
942
946
len = strlen (submodule ) + 1 ;
943
947
refs = xcalloc (1 , sizeof (struct ref_cache ) + len );
944
948
memcpy (refs -> name , submodule , len );
949
+ refs -> next = submodule_ref_caches ;
950
+ submodule_ref_caches = refs ;
945
951
return refs ;
946
952
}
947
953
948
- /*
949
- * Return a pointer to a ref_cache for the specified submodule. For
950
- * the main repository, use submodule==NULL. The returned structure
951
- * will be allocated and initialized but not necessarily populated; it
952
- * should not be freed.
953
- */
954
- static struct ref_cache * get_ref_cache (const char * submodule )
954
+ static struct ref_cache * lookup_ref_cache (const char * submodule )
955
955
{
956
956
struct ref_cache * refs ;
957
957
@@ -961,10 +961,20 @@ static struct ref_cache *get_ref_cache(const char *submodule)
961
961
for (refs = submodule_ref_caches ; refs ; refs = refs -> next )
962
962
if (!strcmp (submodule , refs -> name ))
963
963
return refs ;
964
+ return NULL ;
965
+ }
964
966
965
- refs = create_ref_cache (submodule );
966
- refs -> next = submodule_ref_caches ;
967
- submodule_ref_caches = refs ;
967
+ /*
968
+ * Return a pointer to a ref_cache for the specified submodule. For
969
+ * the main repository, use submodule==NULL. The returned structure
970
+ * will be allocated and initialized but not necessarily populated; it
971
+ * should not be freed.
972
+ */
973
+ static struct ref_cache * get_ref_cache (const char * submodule )
974
+ {
975
+ struct ref_cache * refs = lookup_ref_cache (submodule );
976
+ if (!refs )
977
+ refs = create_ref_cache (submodule );
968
978
return refs ;
969
979
}
970
980
@@ -1336,16 +1346,24 @@ static int resolve_gitlink_ref_recursive(struct ref_cache *refs,
1336
1346
int resolve_gitlink_ref (const char * path , const char * refname , unsigned char * sha1 )
1337
1347
{
1338
1348
int len = strlen (path ), retval ;
1339
- char * submodule ;
1349
+ struct strbuf submodule = STRBUF_INIT ;
1340
1350
struct ref_cache * refs ;
1341
1351
1342
1352
while (len && path [len - 1 ] == '/' )
1343
1353
len -- ;
1344
1354
if (!len )
1345
1355
return -1 ;
1346
- submodule = xstrndup (path , len );
1347
- refs = get_ref_cache (submodule );
1348
- free (submodule );
1356
+
1357
+ strbuf_add (& submodule , path , len );
1358
+ refs = lookup_ref_cache (submodule .buf );
1359
+ if (!refs ) {
1360
+ if (!is_nonbare_repository_dir (& submodule )) {
1361
+ strbuf_release (& submodule );
1362
+ return -1 ;
1363
+ }
1364
+ refs = create_ref_cache (submodule .buf );
1365
+ }
1366
+ strbuf_release (& submodule );
1349
1367
1350
1368
retval = resolve_gitlink_ref_recursive (refs , refname , sha1 , 0 );
1351
1369
return retval ;
0 commit comments