@@ -157,7 +157,7 @@ static struct cached_refs {
157
157
char did_packed ;
158
158
struct ref_list * loose ;
159
159
struct ref_list * packed ;
160
- } cached_refs ;
160
+ } cached_refs , submodule_refs ;
161
161
static struct ref_list * current_ref ;
162
162
163
163
static struct ref_list * extra_refs ;
@@ -229,23 +229,45 @@ void clear_extra_refs(void)
229
229
extra_refs = NULL ;
230
230
}
231
231
232
- static struct ref_list * get_packed_refs (void )
232
+ static struct ref_list * get_packed_refs (const char * submodule )
233
233
{
234
- if (!cached_refs .did_packed ) {
235
- FILE * f = fopen (git_path ("packed-refs" ), "r" );
236
- cached_refs .packed = NULL ;
234
+ const char * packed_refs_file ;
235
+ struct cached_refs * refs ;
236
+
237
+ if (submodule ) {
238
+ packed_refs_file = git_path_submodule (submodule , "packed-refs" );
239
+ refs = & submodule_refs ;
240
+ free_ref_list (refs -> packed );
241
+ } else {
242
+ packed_refs_file = git_path ("packed-refs" );
243
+ refs = & cached_refs ;
244
+ }
245
+
246
+ if (!refs -> did_packed || submodule ) {
247
+ FILE * f = fopen (packed_refs_file , "r" );
248
+ refs -> packed = NULL ;
237
249
if (f ) {
238
- read_packed_refs (f , & cached_refs );
250
+ read_packed_refs (f , refs );
239
251
fclose (f );
240
252
}
241
- cached_refs . did_packed = 1 ;
253
+ refs -> did_packed = 1 ;
242
254
}
243
- return cached_refs . packed ;
255
+ return refs -> packed ;
244
256
}
245
257
246
- static struct ref_list * get_ref_dir (const char * base , struct ref_list * list )
258
+ static struct ref_list * get_ref_dir (const char * submodule , const char * base ,
259
+ struct ref_list * list )
247
260
{
248
- DIR * dir = opendir (git_path ("%s" , base ));
261
+ DIR * dir ;
262
+ const char * path ;
263
+
264
+ if (submodule )
265
+ path = git_path_submodule (submodule , "%s" , base );
266
+ else
267
+ path = git_path ("%s" , base );
268
+
269
+
270
+ dir = opendir (path );
249
271
250
272
if (dir ) {
251
273
struct dirent * de ;
@@ -261,6 +283,7 @@ static struct ref_list *get_ref_dir(const char *base, struct ref_list *list)
261
283
struct stat st ;
262
284
int flag ;
263
285
int namelen ;
286
+ const char * refdir ;
264
287
265
288
if (de -> d_name [0 ] == '.' )
266
289
continue ;
@@ -270,16 +293,27 @@ static struct ref_list *get_ref_dir(const char *base, struct ref_list *list)
270
293
if (has_extension (de -> d_name , ".lock" ))
271
294
continue ;
272
295
memcpy (ref + baselen , de -> d_name , namelen + 1 );
273
- if (stat (git_path ("%s" , ref ), & st ) < 0 )
296
+ refdir = submodule
297
+ ? git_path_submodule (submodule , "%s" , ref )
298
+ : git_path ("%s" , ref );
299
+ if (stat (refdir , & st ) < 0 )
274
300
continue ;
275
301
if (S_ISDIR (st .st_mode )) {
276
- list = get_ref_dir (ref , list );
302
+ list = get_ref_dir (submodule , ref , list );
277
303
continue ;
278
304
}
279
- if (! resolve_ref ( ref , sha1 , 1 , & flag ) ) {
305
+ if (submodule ) {
280
306
hashclr (sha1 );
281
- flag |= REF_BROKEN ;
282
- }
307
+ flag = 0 ;
308
+ if (resolve_gitlink_ref (submodule , ref , sha1 ) < 0 ) {
309
+ hashclr (sha1 );
310
+ flag |= REF_BROKEN ;
311
+ }
312
+ } else
313
+ if (!resolve_ref (ref , sha1 , 1 , & flag )) {
314
+ hashclr (sha1 );
315
+ flag |= REF_BROKEN ;
316
+ }
283
317
list = add_ref (ref , sha1 , flag , list , NULL );
284
318
}
285
319
free (ref );
@@ -322,10 +356,16 @@ void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname)
322
356
for_each_rawref (warn_if_dangling_symref , & data );
323
357
}
324
358
325
- static struct ref_list * get_loose_refs (void )
359
+ static struct ref_list * get_loose_refs (const char * submodule )
326
360
{
361
+ if (submodule ) {
362
+ free_ref_list (submodule_refs .loose );
363
+ submodule_refs .loose = get_ref_dir (submodule , "refs" , NULL );
364
+ return submodule_refs .loose ;
365
+ }
366
+
327
367
if (!cached_refs .did_loose ) {
328
- cached_refs .loose = get_ref_dir ("refs" , NULL );
368
+ cached_refs .loose = get_ref_dir (NULL , "refs" , NULL );
329
369
cached_refs .did_loose = 1 ;
330
370
}
331
371
return cached_refs .loose ;
@@ -459,7 +499,7 @@ const char *resolve_ref(const char *ref, unsigned char *sha1, int reading, int *
459
499
git_snpath (path , sizeof (path ), "%s" , ref );
460
500
/* Special case: non-existing file. */
461
501
if (lstat (path , & st ) < 0 ) {
462
- struct ref_list * list = get_packed_refs ();
502
+ struct ref_list * list = get_packed_refs (NULL );
463
503
while (list ) {
464
504
if (!strcmp (ref , list -> name )) {
465
505
hashcpy (sha1 , list -> sha1 );
@@ -588,7 +628,7 @@ int peel_ref(const char *ref, unsigned char *sha1)
588
628
return -1 ;
589
629
590
630
if ((flag & REF_ISPACKED )) {
591
- struct ref_list * list = get_packed_refs ();
631
+ struct ref_list * list = get_packed_refs (NULL );
592
632
593
633
while (list ) {
594
634
if (!strcmp (list -> name , ref )) {
@@ -615,12 +655,12 @@ int peel_ref(const char *ref, unsigned char *sha1)
615
655
return -1 ;
616
656
}
617
657
618
- static int do_for_each_ref (const char * base , each_ref_fn fn , int trim ,
619
- int flags , void * cb_data )
658
+ static int do_for_each_ref (const char * submodule , const char * base , each_ref_fn fn ,
659
+ int trim , int flags , void * cb_data )
620
660
{
621
661
int retval = 0 ;
622
- struct ref_list * packed = get_packed_refs ();
623
- struct ref_list * loose = get_loose_refs ();
662
+ struct ref_list * packed = get_packed_refs (submodule );
663
+ struct ref_list * loose = get_loose_refs (submodule );
624
664
625
665
struct ref_list * extra ;
626
666
@@ -657,24 +697,38 @@ static int do_for_each_ref(const char *base, each_ref_fn fn, int trim,
657
697
return retval ;
658
698
}
659
699
660
- int head_ref (each_ref_fn fn , void * cb_data )
700
+
701
+ static int do_head_ref (const char * submodule , each_ref_fn fn , void * cb_data )
661
702
{
662
703
unsigned char sha1 [20 ];
663
704
int flag ;
664
705
706
+ if (submodule ) {
707
+ if (resolve_gitlink_ref (submodule , "HEAD" , sha1 ) == 0 )
708
+ return fn ("HEAD" , sha1 , 0 , cb_data );
709
+
710
+ return 0 ;
711
+ }
712
+
665
713
if (resolve_ref ("HEAD" , sha1 , 1 , & flag ))
666
714
return fn ("HEAD" , sha1 , flag , cb_data );
715
+
667
716
return 0 ;
668
717
}
669
718
719
+ int head_ref (each_ref_fn fn , void * cb_data )
720
+ {
721
+ return do_head_ref (NULL , fn , cb_data );
722
+ }
723
+
670
724
int for_each_ref (each_ref_fn fn , void * cb_data )
671
725
{
672
- return do_for_each_ref ("refs/" , fn , 0 , 0 , cb_data );
726
+ return do_for_each_ref (NULL , "refs/" , fn , 0 , 0 , cb_data );
673
727
}
674
728
675
729
int for_each_ref_in (const char * prefix , each_ref_fn fn , void * cb_data )
676
730
{
677
- return do_for_each_ref (prefix , fn , strlen (prefix ), 0 , cb_data );
731
+ return do_for_each_ref (NULL , prefix , fn , strlen (prefix ), 0 , cb_data );
678
732
}
679
733
680
734
int for_each_tag_ref (each_ref_fn fn , void * cb_data )
@@ -694,7 +748,7 @@ int for_each_remote_ref(each_ref_fn fn, void *cb_data)
694
748
695
749
int for_each_replace_ref (each_ref_fn fn , void * cb_data )
696
750
{
697
- return do_for_each_ref ("refs/replace/" , fn , 13 , 0 , cb_data );
751
+ return do_for_each_ref (NULL , "refs/replace/" , fn , 13 , 0 , cb_data );
698
752
}
699
753
700
754
int for_each_glob_ref_in (each_ref_fn fn , const char * pattern ,
@@ -734,7 +788,7 @@ int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data)
734
788
735
789
int for_each_rawref (each_ref_fn fn , void * cb_data )
736
790
{
737
- return do_for_each_ref ("refs/" , fn , 0 ,
791
+ return do_for_each_ref (NULL , "refs/" , fn , 0 ,
738
792
DO_FOR_EACH_INCLUDE_BROKEN , cb_data );
739
793
}
740
794
@@ -958,7 +1012,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char
958
1012
* name is a proper prefix of our refname.
959
1013
*/
960
1014
if (missing &&
961
- !is_refname_available (ref , NULL , get_packed_refs (), 0 )) {
1015
+ !is_refname_available (ref , NULL , get_packed_refs (NULL ), 0 )) {
962
1016
last_errno = ENOTDIR ;
963
1017
goto error_return ;
964
1018
}
@@ -1021,7 +1075,7 @@ static int repack_without_ref(const char *refname)
1021
1075
int fd ;
1022
1076
int found = 0 ;
1023
1077
1024
- packed_ref_list = get_packed_refs ();
1078
+ packed_ref_list = get_packed_refs (NULL );
1025
1079
for (list = packed_ref_list ; list ; list = list -> next ) {
1026
1080
if (!strcmp (refname , list -> name )) {
1027
1081
found = 1 ;
@@ -1110,10 +1164,10 @@ int rename_ref(const char *oldref, const char *newref, const char *logmsg)
1110
1164
if (!symref )
1111
1165
return error ("refname %s not found" , oldref );
1112
1166
1113
- if (!is_refname_available (newref , oldref , get_packed_refs (), 0 ))
1167
+ if (!is_refname_available (newref , oldref , get_packed_refs (NULL ), 0 ))
1114
1168
return 1 ;
1115
1169
1116
- if (!is_refname_available (newref , oldref , get_loose_refs (), 0 ))
1170
+ if (!is_refname_available (newref , oldref , get_loose_refs (NULL ), 0 ))
1117
1171
return 1 ;
1118
1172
1119
1173
lock = lock_ref_sha1_basic (renamed_ref , NULL , 0 , NULL );
0 commit comments