@@ -17,7 +17,7 @@ public class GitRepository : IDisposable
17
17
{
18
18
private const string HeadFileName = "HEAD" ;
19
19
private const string GitDirectoryName = ".git" ;
20
- private readonly Lazy < GitPack [ ] > packs ;
20
+ private readonly Lazy < ReadOnlyMemory < GitPack > > packs ;
21
21
22
22
/// <summary>
23
23
/// UTF-16 encoded string.
@@ -137,7 +137,7 @@ public GitRepository(string workingDirectory, string gitDirectory, string common
137
137
this . objectPathBuffer [ this . ObjectDirectory . Length + 3 ] = '/' ;
138
138
this . objectPathBuffer [ pathLengthInChars - 1 ] = '\0 ' ; // Make sure to initialize with zeros
139
139
140
- this . packs = new Lazy < GitPack [ ] > ( this . LoadPacks ) ;
140
+ this . packs = new Lazy < ReadOnlyMemory < GitPack > > ( this . LoadPacks ) ;
141
141
}
142
142
143
143
// TODO: read from Git settings
@@ -375,7 +375,7 @@ public GitCommit GetCommit(GitObjectId sha, bool readAuthor = false)
375
375
376
376
var hex = ConvertHexStringToByteArray ( objectish ) ;
377
377
378
- foreach ( var pack in this . packs . Value )
378
+ foreach ( var pack in this . packs . Value . Span )
379
379
{
380
380
var objectId = pack . Lookup ( hex , endsWithHalfByte ) ;
381
381
@@ -514,7 +514,7 @@ public bool TryGetObjectBySha(GitObjectId sha, string objectType, out Stream? va
514
514
}
515
515
#endif
516
516
517
- foreach ( var pack in this . packs . Value )
517
+ foreach ( var pack in this . packs . Value . Span )
518
518
{
519
519
if ( pack . TryGetObject ( sha , objectType , out value ) )
520
520
{
@@ -563,7 +563,7 @@ public string GetCacheStatistics()
563
563
builder . AppendLine ( ) ;
564
564
#endif
565
565
566
- foreach ( var pack in this . packs . Value )
566
+ foreach ( var pack in this . packs . Value . Span )
567
567
{
568
568
pack . GetCacheStatistics ( builder ) ;
569
569
}
@@ -582,7 +582,7 @@ public void Dispose()
582
582
{
583
583
if ( this . packs . IsValueCreated )
584
584
{
585
- foreach ( var pack in this . packs . Value )
585
+ foreach ( var pack in this . packs . Value . Span )
586
586
{
587
587
pack . Dispose ( ) ;
588
588
}
@@ -638,7 +638,7 @@ private GitObjectId ResolveReference(object reference)
638
638
}
639
639
}
640
640
641
- private GitPack [ ] LoadPacks ( )
641
+ private ReadOnlyMemory < GitPack > LoadPacks ( )
642
642
{
643
643
var packDirectory = Path . Combine ( this . ObjectDirectory , "pack/" ) ;
644
644
@@ -648,15 +648,23 @@ private GitPack[] LoadPacks()
648
648
}
649
649
650
650
var indexFiles = Directory . GetFiles ( packDirectory , "*.idx" ) ;
651
- GitPack [ ] packs = new GitPack [ indexFiles . Length ] ;
651
+ var packs = new GitPack [ indexFiles . Length ] ;
652
+ int addCount = 0 ;
652
653
653
654
for ( int i = 0 ; i < indexFiles . Length ; i ++ )
654
655
{
655
656
var name = Path . GetFileNameWithoutExtension ( indexFiles [ i ] ) ;
656
- packs [ i ] = new GitPack ( this , name ) ;
657
+ var indexPath = Path . Combine ( this . ObjectDirectory , "pack" , $ "{ name } .idx") ;
658
+ var packPath = Path . Combine ( this . ObjectDirectory , "pack" , $ "{ name } .pack") ;
659
+
660
+ // Only proceed if both the packfile and index file exist.
661
+ if ( File . Exists ( packPath ) )
662
+ {
663
+ packs [ addCount ++ ] = new GitPack ( this . GetObjectBySha , indexPath , packPath ) ;
664
+ }
657
665
}
658
666
659
- return packs ;
667
+ return packs . AsMemory ( 0 , addCount ) ;
660
668
}
661
669
662
670
private static string TrimEndingDirectorySeparator ( string path )
0 commit comments