@@ -17,7 +17,7 @@ public class GitRepository : IDisposable
1717 {
1818 private const string HeadFileName = "HEAD" ;
1919 private const string GitDirectoryName = ".git" ;
20- private readonly Lazy < GitPack [ ] > packs ;
20+ private readonly Lazy < ReadOnlyMemory < GitPack > > packs ;
2121
2222 /// <summary>
2323 /// UTF-16 encoded string.
@@ -137,7 +137,7 @@ public GitRepository(string workingDirectory, string gitDirectory, string common
137137 this . objectPathBuffer [ this . ObjectDirectory . Length + 3 ] = '/' ;
138138 this . objectPathBuffer [ pathLengthInChars - 1 ] = '\0 ' ; // Make sure to initialize with zeros
139139
140- this . packs = new Lazy < GitPack [ ] > ( this . LoadPacks ) ;
140+ this . packs = new Lazy < ReadOnlyMemory < GitPack > > ( this . LoadPacks ) ;
141141 }
142142
143143 // TODO: read from Git settings
@@ -375,7 +375,7 @@ public GitCommit GetCommit(GitObjectId sha, bool readAuthor = false)
375375
376376 var hex = ConvertHexStringToByteArray ( objectish ) ;
377377
378- foreach ( var pack in this . packs . Value )
378+ foreach ( var pack in this . packs . Value . Span )
379379 {
380380 var objectId = pack . Lookup ( hex , endsWithHalfByte ) ;
381381
@@ -514,7 +514,7 @@ public bool TryGetObjectBySha(GitObjectId sha, string objectType, out Stream? va
514514 }
515515#endif
516516
517- foreach ( var pack in this . packs . Value )
517+ foreach ( var pack in this . packs . Value . Span )
518518 {
519519 if ( pack . TryGetObject ( sha , objectType , out value ) )
520520 {
@@ -563,7 +563,7 @@ public string GetCacheStatistics()
563563 builder . AppendLine ( ) ;
564564#endif
565565
566- foreach ( var pack in this . packs . Value )
566+ foreach ( var pack in this . packs . Value . Span )
567567 {
568568 pack . GetCacheStatistics ( builder ) ;
569569 }
@@ -582,7 +582,7 @@ public void Dispose()
582582 {
583583 if ( this . packs . IsValueCreated )
584584 {
585- foreach ( var pack in this . packs . Value )
585+ foreach ( var pack in this . packs . Value . Span )
586586 {
587587 pack . Dispose ( ) ;
588588 }
@@ -638,7 +638,7 @@ private GitObjectId ResolveReference(object reference)
638638 }
639639 }
640640
641- private GitPack [ ] LoadPacks ( )
641+ private ReadOnlyMemory < GitPack > LoadPacks ( )
642642 {
643643 var packDirectory = Path . Combine ( this . ObjectDirectory , "pack/" ) ;
644644
@@ -648,15 +648,23 @@ private GitPack[] LoadPacks()
648648 }
649649
650650 var indexFiles = Directory . GetFiles ( packDirectory , "*.idx" ) ;
651- GitPack [ ] packs = new GitPack [ indexFiles . Length ] ;
651+ var packs = new GitPack [ indexFiles . Length ] ;
652+ int addCount = 0 ;
652653
653654 for ( int i = 0 ; i < indexFiles . Length ; i ++ )
654655 {
655656 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+ }
657665 }
658666
659- return packs ;
667+ return packs . AsMemory ( 0 , addCount ) ;
660668 }
661669
662670 private static string TrimEndingDirectorySeparator ( string path )
0 commit comments