Skip to content

Commit

Permalink
Merge pull request #597 from qmfrederik/fixes/missing-idx
Browse files Browse the repository at this point in the history
Don't load git packs if the .pack file is missing
  • Loading branch information
AArnott committed Apr 29, 2021
2 parents 96b6548 + 0303d96 commit e3477ca
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 27 deletions.
17 changes: 0 additions & 17 deletions src/NerdBank.GitVersioning/ManagedGit/GitPack.cs
Expand Up @@ -51,23 +51,6 @@ public class GitPack : IDisposable
// are closed by the caller).
private readonly Queue<GitPackPooledStream> pooledStreams = new Queue<GitPackPooledStream>();

/// <summary>
/// Initializes a new instance of the <see cref="GitPack"/> class.
/// </summary>
/// <param name="repository">
/// The repository to which this pack file belongs.
/// </param>
/// <param name="name">
/// The name of the pack file.
/// </param>
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"))
{
}

/// <summary>
/// Initializes a new instance of the <see cref="GitPack"/> class.
/// </summary>
Expand Down
28 changes: 18 additions & 10 deletions src/NerdBank.GitVersioning/ManagedGit/GitRepository.cs
Expand Up @@ -17,7 +17,7 @@ public class GitRepository : IDisposable
{
private const string HeadFileName = "HEAD";
private const string GitDirectoryName = ".git";
private readonly Lazy<GitPack[]> packs;
private readonly Lazy<ReadOnlyMemory<GitPack>> packs;

/// <summary>
/// UTF-16 encoded string.
Expand Down Expand Up @@ -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<GitPack[]>(this.LoadPacks);
this.packs = new Lazy<ReadOnlyMemory<GitPack>>(this.LoadPacks);
}

// TODO: read from Git settings
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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))
{
Expand Down Expand Up @@ -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);
}
Expand All @@ -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();
}
Expand Down Expand Up @@ -638,7 +638,7 @@ private GitObjectId ResolveReference(object reference)
}
}

private GitPack[] LoadPacks()
private ReadOnlyMemory<GitPack> LoadPacks()
{
var packDirectory = Path.Combine(this.ObjectDirectory, "pack/");

Expand All @@ -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)
Expand Down

0 comments on commit e3477ca

Please sign in to comment.