diff --git a/src/NerdBank.GitVersioning/ManagedGit/GitPack.cs b/src/NerdBank.GitVersioning/ManagedGit/GitPack.cs index 4a5d25ba..27c45c69 100644 --- a/src/NerdBank.GitVersioning/ManagedGit/GitPack.cs +++ b/src/NerdBank.GitVersioning/ManagedGit/GitPack.cs @@ -51,23 +51,6 @@ public class GitPack : IDisposable // are closed by the caller). private readonly Queue pooledStreams = new Queue(); - /// - /// Initializes a new instance of the class. - /// - /// - /// The repository to which this pack file belongs. - /// - /// - /// The name of the pack file. - /// - internal GitPack(GitRepository repository, string name) - : this( - repository.GetObjectBySha, - indexPath: Path.Combine(repository.ObjectDirectory, "pack", $"{name}.idx"), - packPath: Path.Combine(repository.ObjectDirectory, "pack", $"{name}.pack")) - { - } - /// /// Initializes a new instance of the class. /// diff --git a/src/NerdBank.GitVersioning/ManagedGit/GitRepository.cs b/src/NerdBank.GitVersioning/ManagedGit/GitRepository.cs index 227e2b89..741ec25c 100644 --- a/src/NerdBank.GitVersioning/ManagedGit/GitRepository.cs +++ b/src/NerdBank.GitVersioning/ManagedGit/GitRepository.cs @@ -17,7 +17,7 @@ public class GitRepository : IDisposable { private const string HeadFileName = "HEAD"; private const string GitDirectoryName = ".git"; - private readonly Lazy packs; + private readonly Lazy> packs; /// /// UTF-16 encoded string. @@ -137,7 +137,7 @@ public GitRepository(string workingDirectory, string gitDirectory, string common this.objectPathBuffer[this.ObjectDirectory.Length + 3] = '/'; this.objectPathBuffer[pathLengthInChars - 1] = '\0'; // Make sure to initialize with zeros - this.packs = new Lazy(this.LoadPacks); + this.packs = new Lazy>(this.LoadPacks); } // TODO: read from Git settings @@ -375,7 +375,7 @@ public GitCommit GetCommit(GitObjectId sha, bool readAuthor = false) var hex = ConvertHexStringToByteArray(objectish); - foreach (var pack in this.packs.Value) + foreach (var pack in this.packs.Value.Span) { var objectId = pack.Lookup(hex, endsWithHalfByte); @@ -514,7 +514,7 @@ public bool TryGetObjectBySha(GitObjectId sha, string objectType, out Stream? va } #endif - foreach (var pack in this.packs.Value) + foreach (var pack in this.packs.Value.Span) { if (pack.TryGetObject(sha, objectType, out value)) { @@ -563,7 +563,7 @@ public string GetCacheStatistics() builder.AppendLine(); #endif - foreach (var pack in this.packs.Value) + foreach (var pack in this.packs.Value.Span) { pack.GetCacheStatistics(builder); } @@ -582,7 +582,7 @@ public void Dispose() { if (this.packs.IsValueCreated) { - foreach (var pack in this.packs.Value) + foreach (var pack in this.packs.Value.Span) { pack.Dispose(); } @@ -638,7 +638,7 @@ private GitObjectId ResolveReference(object reference) } } - private GitPack[] LoadPacks() + private ReadOnlyMemory LoadPacks() { var packDirectory = Path.Combine(this.ObjectDirectory, "pack/"); @@ -648,15 +648,23 @@ private GitPack[] LoadPacks() } var indexFiles = Directory.GetFiles(packDirectory, "*.idx"); - GitPack[] packs = new GitPack[indexFiles.Length]; + var packs = new GitPack[indexFiles.Length]; + int addCount = 0; for (int i = 0; i < indexFiles.Length; i++) { var name = Path.GetFileNameWithoutExtension(indexFiles[i]); - packs[i] = new GitPack(this, name); + var indexPath = Path.Combine(this.ObjectDirectory, "pack", $"{name}.idx"); + var packPath = Path.Combine(this.ObjectDirectory, "pack", $"{name}.pack"); + + // Only proceed if both the packfile and index file exist. + if (File.Exists(packPath)) + { + packs[addCount++] = new GitPack(this.GetObjectBySha, indexPath, packPath); + } } - return packs; + return packs.AsMemory(0, addCount); } private static string TrimEndingDirectorySeparator(string path)