Skip to content

Writing multiple pack files for the same repository #1216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
298 changes: 194 additions & 104 deletions LibGit2Sharp.Tests/PackBuilderFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,190 +3,280 @@
using System.Linq;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;
using System.Collections.Generic;
using LibGit2Sharp.Advanced;

namespace LibGit2Sharp.Tests
{
public class PackBuilderFixture : BaseFixture
{
[Fact]
public void TestDefaultPackDelegate()
public void TestDefaultPackAllWithDirPath()
{
TestIfSameRepoAfterPacking(null);
TestBody((repo, packDirPath) =>
{
PackBuilderResults results = repo.ObjectDatabase.PackAll(packDirPath);
});
}

[Fact]
public void TestCommitsPerBranchPackDelegate()
public void TestCommitsPerBranch()
{
TestIfSameRepoAfterPacking(AddingObjectIdsTestDelegate);
TestBody((repo, packDirPath) =>
{
using (PackBuilder builder = new PackBuilder(repo))
{
foreach (Branch branch in repo.Branches)
{
foreach (Commit commit in branch.Commits)
{
builder.AddRecursively(commit);
}
}

foreach (Tag tag in repo.Tags)
{
builder.Add(tag.Target);
}

builder.WritePackTo(packDirPath);
}
});
}

[Fact]
public void TestCommitsPerBranchIdsPackDelegate()
public void TestCommitsPerBranchIds()
{
TestIfSameRepoAfterPacking(AddingObjectsTestDelegate);
TestBody((repo, packDirPath) =>
{
using (PackBuilder builder = new PackBuilder(repo))
{
foreach (Branch branch in repo.Branches)
{
foreach (Commit commit in branch.Commits)
{
builder.AddRecursively(commit.Id);
}
}

foreach (Tag tag in repo.Tags)
{
builder.Add(tag.Target.Id);
}

builder.WritePackTo(packDirPath);
}
});
}

internal void TestIfSameRepoAfterPacking(Action<IRepository, PackBuilder> packDelegate)
[Fact]
public void TestCreatingMultiplePackFilesByType()
{
// read a repo
// pack with the provided action
// write the pack file in a mirror repo
// read new repo
// compare
TestBody((repo, packDirPath) =>
{
long totalNumberOfWrittenObjects = 0;

string orgRepoPath = SandboxPackBuilderTestRepo();
string mrrRepoPath = SandboxPackBuilderTestRepo();
string mrrRepoPackDirPath = Path.Combine(mrrRepoPath + "/.git/objects");
using (PackBuilder builder = new PackBuilder(repo))
{
for (int i = 0; i < 3; i++)
{
foreach (GitObject obj in repo.ObjectDatabase)
{
if (i == 0 && obj is Commit)
builder.Add(obj.Id);

DirectoryHelper.DeleteDirectory(mrrRepoPackDirPath);
Directory.CreateDirectory(mrrRepoPackDirPath + "/pack");
if (i == 1 && obj is Tree)
builder.Add(obj.Id);

PackBuilderOptions packBuilderOptions = new PackBuilderOptions(mrrRepoPackDirPath + "/pack");
if (i == 2 && obj is Blob)
builder.Add(obj.Id);
}

using (Repository orgRepo = new Repository(orgRepoPath))
{
PackBuilderResults results;
if (packDelegate != null)
results = orgRepo.ObjectDatabase.Pack(packBuilderOptions, b => packDelegate(orgRepo, b));
else
results = orgRepo.ObjectDatabase.Pack(packBuilderOptions);
PackBuilderResults results = builder.WritePackTo(packDirPath);

// written objects count is the same as in objects database
Assert.Equal(orgRepo.ObjectDatabase.Count(), results.WrittenObjectsCount);
// for reuse to build the next pack file.
builder.Reset();

// loading a repo from the written pack file.
using (Repository mrrRepo = new Repository(mrrRepoPath))
{
// make sure the objects of the original repo are the same as the ones in the mirror repo
// doing that by making sure the count is the same, and the set difference is empty
Assert.True(mrrRepo.ObjectDatabase.Count() == orgRepo.ObjectDatabase.Count() && !mrrRepo.ObjectDatabase.Except(orgRepo.ObjectDatabase).Any());
// assert the pack file is written
Assert.True(File.Exists(Path.Combine(packDirPath, "pack-" + results.PackHash + ".pack")));
Assert.True(File.Exists(Path.Combine(packDirPath, "pack-" + results.PackHash + ".idx")));

Assert.Equal(orgRepo.Commits.Count(), mrrRepo.Commits.Count());
Assert.Equal(orgRepo.Branches.Count(), mrrRepo.Branches.Count());
Assert.Equal(orgRepo.Refs.Count(), mrrRepo.Refs.Count());
totalNumberOfWrittenObjects += results.WrittenObjectsCount;
}
}
}

// assert total number of written objects count is the same as in objects database
Assert.Equal(repo.ObjectDatabase.Count(), totalNumberOfWrittenObjects);
});
}

internal void AddingObjectIdsTestDelegate(IRepository repo, PackBuilder builder)
[Fact]
public void TestCreatingMultiplePackFilesByCount()
{
foreach (Branch branch in repo.Branches)
TestBody((repo, packDirPath) =>
{
foreach (Commit commit in branch.Commits)
long totalNumberOfWrittenObjects = 0;
PackBuilderResults results;

using (PackBuilder packBuilder = new PackBuilder(repo))
{
builder.AddRecursively(commit.Id);
int currentObject = 0;

foreach (GitObject gitObject in repo.ObjectDatabase)
{
packBuilder.Add(gitObject.Id);

if (currentObject++ % 100 == 0)
{
results = packBuilder.WritePackTo(packDirPath);
packBuilder.Reset();

// assert the pack file is written
Assert.True(File.Exists(Path.Combine(packDirPath, "pack-" + results.PackHash + ".pack")));
Assert.True(File.Exists(Path.Combine(packDirPath, "pack-" + results.PackHash + ".idx")));

totalNumberOfWrittenObjects += results.WrittenObjectsCount;
}
}

if (currentObject % 100 != 1)
{
results = packBuilder.WritePackTo(packDirPath);
packBuilder.Reset();

// assert the pack file is written
Assert.True(File.Exists(Path.Combine(packDirPath, "pack-" + results.PackHash + ".pack")));
Assert.True(File.Exists(Path.Combine(packDirPath, "pack-" + results.PackHash + ".idx")));

totalNumberOfWrittenObjects += results.WrittenObjectsCount;
}
}
}

foreach (Tag tag in repo.Tags)
// assert total number of written objects count is the same as in objects database
Assert.Equal(repo.ObjectDatabase.Count(), totalNumberOfWrittenObjects);
});
}

[Fact]
public void CanWritePackAndIndexFiles()
{
using (Repository repo = new Repository(SandboxPackBuilderTestRepo()))
{
builder.Add(tag.Target.Id);
string path = Path.GetTempPath();

PackBuilderResults results = repo.ObjectDatabase.PackAll(path);

Assert.Equal(repo.ObjectDatabase.Count(), results.WrittenObjectsCount);
Assert.True(File.Exists(Path.Combine(path, "pack-" + results.PackHash + ".pack")));
Assert.True(File.Exists(Path.Combine(path, "pack-" + results.PackHash + ".idx")));
}
}

internal void AddingObjectsTestDelegate(IRepository repo, PackBuilder builder)
[Fact]
public void TestEmptyPackFile()
{
foreach (Branch branch in repo.Branches)
using (Repository repo = new Repository(SandboxPackBuilderTestRepo()))
{
foreach (Commit commit in branch.Commits)
string path = Path.GetTempPath();

using (PackBuilder builder = new PackBuilder(repo))
{
builder.AddRecursively(commit);
}
}
PackBuilderResults results = builder.WritePackTo(path);

foreach (Tag tag in repo.Tags)
{
builder.Add(tag.Target);
Assert.True(File.Exists(Path.Combine(path, "pack-" + results.PackHash + ".pack")));
Assert.True(File.Exists(Path.Combine(path, "pack-" + results.PackHash + ".idx")));
}
}
}

[Fact]
public void ExceptionIfPathDoesNotExist()
public void TestPackFileForEmptyRepository()
{
Assert.Throws<DirectoryNotFoundException>(() => new PackBuilderOptions("aaa"));
using (Repository repo = new Repository(InitNewRepository()))
{
string path = Path.GetTempPath();

PackBuilderResults results = repo.ObjectDatabase.PackAll(path);

Assert.Equal(repo.ObjectDatabase.Count(), results.WrittenObjectsCount);
Assert.True(File.Exists(Path.Combine(path, "pack-" + results.PackHash + ".pack")));
Assert.True(File.Exists(Path.Combine(path, "pack-" + results.PackHash + ".idx")));
}
}

[Fact]
public void ExceptionIfPathEqualsNull()
public void ExceptionIfPathDoesNotExistAtPackAll()
{
Assert.Throws<ArgumentNullException>(() => new PackBuilderOptions(null));
using (Repository repo = new Repository(SandboxPackBuilderTestRepo()))
{
Assert.Throws<DirectoryNotFoundException>(() => repo.ObjectDatabase.PackAll("aaaaa"));
}
}

[Fact]
public void ExceptionIfOptionsEqualsNull()
public void ExceptionIfPathDoesNotExistAtWriteToPack()
{
string orgRepoPath = SandboxPackBuilderTestRepo();

using (Repository orgRepo = new Repository(orgRepoPath))
using (Repository repo = new Repository(SandboxPackBuilderTestRepo()))
using (PackBuilder builder = new PackBuilder(repo))
{
Assert.Throws<ArgumentNullException>(() =>
{
orgRepo.ObjectDatabase.Pack(null);
});
Assert.Throws<DirectoryNotFoundException>(() => builder.WritePackTo("aaaaa"));
}
}

[Fact]
public void ExceptionIfBuildDelegateEqualsNull()
public void ExceptionIfAddNullObjectID()
{
string orgRepoPath = SandboxPackBuilderTestRepo();
PackBuilderOptions packBuilderOptions = new PackBuilderOptions(orgRepoPath);

using (Repository orgRepo = new Repository(orgRepoPath))
using (Repository repo = new Repository(SandboxPackBuilderTestRepo()))
using (PackBuilder builder = new PackBuilder(repo))
{
Assert.Throws<ArgumentNullException>(() =>
{
orgRepo.ObjectDatabase.Pack(packBuilderOptions, null);
});
Assert.Throws<ArgumentNullException>(() => builder.Add(null));
}
}

[Fact]
public void ExceptionIfNegativeNumberOfThreads()
public void ExceptionIfAddRecursivelyNullObjectID()
{
string orgRepoPath = SandboxPackBuilderTestRepo();
PackBuilderOptions packBuilderOptions = new PackBuilderOptions(orgRepoPath);

Assert.Throws<ArgumentException>(() =>
using (Repository repo = new Repository(SandboxPackBuilderTestRepo()))
using (PackBuilder builder = new PackBuilder(repo))
{
packBuilderOptions.MaximumNumberOfThreads = -1;
});
Assert.Throws<ArgumentNullException>(() => builder.AddRecursively(null));
}
}

[Fact]
public void ExceptionIfAddNullObjectID()
internal void TestBody(Action<Repository, string> fullPackingAction)
{
// read a repo, pack with the provided action, write the pack file in a mirror repo, read new repo, compare

string orgRepoPath = SandboxPackBuilderTestRepo();
PackBuilderOptions packBuilderOptions = new PackBuilderOptions(orgRepoPath);
string mrrRepoPath = SandboxPackBuilderTestRepo();
string mrrRepoPackDirPath = Path.Combine(mrrRepoPath + "/.git/objects");

DirectoryHelper.DeleteDirectory(mrrRepoPackDirPath);
Directory.CreateDirectory(mrrRepoPackDirPath + "/pack");

using (Repository orgRepo = new Repository(orgRepoPath))
{
Assert.Throws<ArgumentNullException>(() =>
fullPackingAction(orgRepo, mrrRepoPackDirPath + "/pack");

// loading the mirror repo from the written pack file and make sure it's identical to the original.
using (Repository mrrRepo = new Repository(mrrRepoPath))
{
orgRepo.ObjectDatabase.Pack(packBuilderOptions, builder =>
{
builder.Add(null);
});
});
AssertIfNotIdenticalRepositories(orgRepo, mrrRepo);
}
}
}

[Fact]
public void ExceptionIfAddRecursivelyNullObjectID()
internal void AssertIfNotIdenticalRepositories(IRepository repo1, IRepository repo2)
{
string orgRepoPath = SandboxPackBuilderTestRepo();
PackBuilderOptions packBuilderOptions = new PackBuilderOptions(orgRepoPath);
// make sure the objects of the original repo are the same as the ones in the mirror repo
// doing that by making sure the count is the same, and the set difference is empty
Assert.True(repo1.ObjectDatabase.Count() == repo2.ObjectDatabase.Count()
&& !repo2.ObjectDatabase.Except(repo1.ObjectDatabase).Any());

using (Repository orgRepo = new Repository(orgRepoPath))
{
Assert.Throws<ArgumentNullException>(() =>
{
orgRepo.ObjectDatabase.Pack(packBuilderOptions, builder =>
{
builder.AddRecursively(null);
});
});
}
Assert.Equal(repo1.Commits.Count(), repo2.Commits.Count());
Assert.Equal(repo1.Branches.Count(), repo2.Branches.Count());
Assert.Equal(repo1.Refs.Count(), repo2.Refs.Count());
Assert.Equal(repo1.Tags.Count(), repo2.Tags.Count());
}
}
}
3 changes: 3 additions & 0 deletions LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,9 @@ internal static extern int git_packbuilder_write(
[DllImport(libgit2)]
internal static extern UInt32 git_packbuilder_written(PackBuilderSafeHandle packbuilder);

[DllImport(libgit2)]
internal static extern OidSafeHandle git_packbuilder_hash(PackBuilderSafeHandle packbuilder);

[DllImport(libgit2)]
internal static extern int git_reference_create(
out ReferenceSafeHandle reference,
Expand Down
Loading