Skip to content

Commit e3477ca

Browse files
authored
Merge pull request #597 from qmfrederik/fixes/missing-idx
Don't load git packs if the .pack file is missing
2 parents 96b6548 + 0303d96 commit e3477ca

File tree

2 files changed

+18
-27
lines changed

2 files changed

+18
-27
lines changed

src/NerdBank.GitVersioning/ManagedGit/GitPack.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,6 @@ public class GitPack : IDisposable
5151
// are closed by the caller).
5252
private readonly Queue<GitPackPooledStream> pooledStreams = new Queue<GitPackPooledStream>();
5353

54-
/// <summary>
55-
/// Initializes a new instance of the <see cref="GitPack"/> class.
56-
/// </summary>
57-
/// <param name="repository">
58-
/// The repository to which this pack file belongs.
59-
/// </param>
60-
/// <param name="name">
61-
/// The name of the pack file.
62-
/// </param>
63-
internal GitPack(GitRepository repository, string name)
64-
: this(
65-
repository.GetObjectBySha,
66-
indexPath: Path.Combine(repository.ObjectDirectory, "pack", $"{name}.idx"),
67-
packPath: Path.Combine(repository.ObjectDirectory, "pack", $"{name}.pack"))
68-
{
69-
}
70-
7154
/// <summary>
7255
/// Initializes a new instance of the <see cref="GitPack"/> class.
7356
/// </summary>

src/NerdBank.GitVersioning/ManagedGit/GitRepository.cs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)