Skip to content

Rename PackBuilder -> PackDefinition, improve PackOptions, introduce WriteTo #1213

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
wants to merge 8 commits into from
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
2 changes: 1 addition & 1 deletion LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<Compile Include="FileHistoryFixture.cs" />
<Compile Include="FilterFixture.cs" />
<Compile Include="GlobalSettingsFixture.cs" />
<Compile Include="PackBuilderFixture.cs" />
<Compile Include="PackDefinitionFixture.cs" />
<Compile Include="PatchStatsFixture.cs" />
<Compile Include="RebaseFixture.cs" />
<Compile Include="RefSpecFixture.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System;
using System.IO;
using System.Linq;
using System.Text;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;

namespace LibGit2Sharp.Tests
{
public class PackBuilderFixture : BaseFixture
public class PackDefinitionFixture : BaseFixture
{
[Fact]
public void TestDefaultPackDelegate()
Expand All @@ -17,39 +18,42 @@ public void TestDefaultPackDelegate()
[Fact]
public void TestCommitsPerBranchPackDelegate()
{
TestIfSameRepoAfterPacking(AddingObjectIdsTestDelegate);
TestIfSameRepoAfterPacking(AddingObjectIdsPackBuilder);
}

[Fact]
public void TestCommitsPerBranchIdsPackDelegate()
{
TestIfSameRepoAfterPacking(AddingObjectsTestDelegate);
TestIfSameRepoAfterPacking(AddingObjectsPackBuilder);
}

internal void TestIfSameRepoAfterPacking(Action<IRepository, PackBuilder> packDelegate)
internal void TestIfSameRepoAfterPacking(Action<IRepository, PackDefinition> packBuilder)
{
// read a repo
// pack with the provided action
// write the pack file in a mirror repo
// read new repo
// compare

string orgRepoPath = SandboxPackBuilderTestRepo();
string mrrRepoPath = SandboxPackBuilderTestRepo();
string mrrRepoPackDirPath = Path.Combine(mrrRepoPath + "/.git/objects");
string orgRepoPath = SandboxPackDefinitionTestRepo();
string mrrRepoPath = SandboxPackDefinitionTestRepo();
string mrrRepoPackDirPath = Path.Combine(mrrRepoPath + ".git", "objects");

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

PackBuilderOptions packBuilderOptions = new PackBuilderOptions(mrrRepoPackDirPath + "/pack");
string packDir = Path.Combine(mrrRepoPackDirPath, "pack");
Directory.CreateDirectory(packDir);

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);
var packOptions = new PackOptions();

PackResults results;
if (packBuilder != null)
{
packOptions.PackBuilder = b => packBuilder(orgRepo, b);
}

results = orgRepo.ObjectDatabase.Pack(packDir, packOptions);

// written objects count is the same as in objects database
Assert.Equal(orgRepo.ObjectDatabase.Count(), results.WrittenObjectsCount);
Expand All @@ -68,7 +72,7 @@ internal void TestIfSameRepoAfterPacking(Action<IRepository, PackBuilder> packDe
}
}

internal void AddingObjectIdsTestDelegate(IRepository repo, PackBuilder builder)
internal void AddingObjectIdsPackBuilder(IRepository repo, PackDefinition builder)
{
foreach (Branch branch in repo.Branches)
{
Expand All @@ -84,7 +88,7 @@ internal void AddingObjectIdsTestDelegate(IRepository repo, PackBuilder builder)
}
}

internal void AddingObjectsTestDelegate(IRepository repo, PackBuilder builder)
internal void AddingObjectsPackBuilder(IRepository repo, PackDefinition builder)
{
foreach (Branch branch in repo.Branches)
{
Expand All @@ -103,99 +107,105 @@ internal void AddingObjectsTestDelegate(IRepository repo, PackBuilder builder)
[Fact]
public void ExceptionIfPathDoesNotExist()
{
PackBuilderOptions pbo;

Assert.Throws<DirectoryNotFoundException>(() =>
using (Repository repo = new Repository(SandboxPackDefinitionTestRepo()))
{
pbo = new PackBuilderOptions("aaa");
});
Assert.Throws<DirectoryNotFoundException>(() =>
{
repo.ObjectDatabase.Pack("aaa", new PackOptions());
});
}
}

[Fact]
public void ExceptionIfPathEqualsNull()
{
PackBuilderOptions pbo;

Assert.Throws<ArgumentNullException>(() =>
{
pbo = new PackBuilderOptions(null);
});
}

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

using (Repository orgRepo = new Repository(orgRepoPath))
using (Repository repo = new Repository(SandboxPackDefinitionTestRepo()))
{
Assert.Throws<ArgumentNullException>(() =>
{
orgRepo.ObjectDatabase.Pack(null);
repo.ObjectDatabase.Pack(default(string), new PackOptions());
});
}
}

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

using (Repository orgRepo = new Repository(orgRepoPath))
using (Repository repo = new Repository(SandboxPackDefinitionTestRepo()))
{
Assert.Throws<ArgumentNullException>(() =>
{
orgRepo.ObjectDatabase.Pack(packBuilderOptions, null);
repo.ObjectDatabase.Pack(default(Stream), new PackOptions());
});
}
}

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

Assert.Throws<ArgumentException>(() =>
{
packBuilderOptions.MaximumNumberOfThreads = -1;
(new PackOptions()).MaximumNumberOfThreads = -1;
});
}

[Fact]
public void ExceptionIfAddNullObjectID()
{
string orgRepoPath = SandboxPackBuilderTestRepo();
PackBuilderOptions packBuilderOptions = new PackBuilderOptions(orgRepoPath);
string orgRepoPath = SandboxPackDefinitionTestRepo();
var packOptions = new PackOptions();

using (Repository orgRepo = new Repository(orgRepoPath))
{
Assert.Throws<ArgumentNullException>(() =>
packOptions.PackBuilder = builder => builder.Add(null);

using (var ms = new MemoryStream())
{
orgRepo.ObjectDatabase.Pack(packBuilderOptions, builder =>
Assert.Throws<ArgumentNullException>(() =>
{
builder.Add(null);
orgRepo.ObjectDatabase.Pack(ms, packOptions);
});
});
}
}
}

[Fact]
public void ExceptionIfAddRecursivelyNullObjectID()
{
string orgRepoPath = SandboxPackBuilderTestRepo();
PackBuilderOptions packBuilderOptions = new PackBuilderOptions(orgRepoPath);
string orgRepoPath = SandboxPackDefinitionTestRepo();
var packOptions = new PackOptions();

using (Repository orgRepo = new Repository(orgRepoPath))
{
Assert.Throws<ArgumentNullException>(() =>
packOptions.PackBuilder = builder => builder.AddRecursively(null);

using (var ms = new MemoryStream())
{
orgRepo.ObjectDatabase.Pack(packBuilderOptions, builder =>
Assert.Throws<ArgumentNullException>(() =>
{
builder.AddRecursively(null);
orgRepo.ObjectDatabase.Pack(ms, packOptions);
});
});
}
}
}

[Fact]
public void WriteToStreamWritesAllObjects()
{
var testRepoPath = SandboxPackDefinitionTestRepo();
using (var testRepo = new Repository(testRepoPath))
{
using (var packOutput = new MemoryStream())
{
PackResults results = testRepo.ObjectDatabase.Pack(packOutput, new PackOptions());

const string packHeader = "PACK";
Assert.Equal(
packHeader,
Encoding.UTF8.GetString(packOutput.GetBuffer(), 0, packHeader.Length));

Assert.Equal(testRepo.ObjectDatabase.Count(), results.WrittenObjectsCount);
}
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions LibGit2Sharp.Tests/TestHelpers/BaseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static BaseFixture()
private static string SubmoduleTargetTestRepoWorkingDirPath { get; set; }
private static string AssumeUnchangedRepoWorkingDirPath { get; set; }
public static string SubmoduleSmallTestRepoWorkingDirPath { get; set; }
public static string PackBuilderTestRepoPath { get; private set; }
public static string PackDefinitionTestRepoPath { get; private set; }

public static DirectoryInfo ResourcesDirectory { get; private set; }

Expand Down Expand Up @@ -75,7 +75,7 @@ private static void SetUpTestEnvironment()
SubmoduleTargetTestRepoWorkingDirPath = Path.Combine(sourceRelativePath, "submodule_target_wd");
AssumeUnchangedRepoWorkingDirPath = Path.Combine(sourceRelativePath, "assume_unchanged_wd");
SubmoduleSmallTestRepoWorkingDirPath = Path.Combine(sourceRelativePath, "submodule_small_wd");
PackBuilderTestRepoPath = Path.Combine(sourceRelativePath, "packbuilder_testrepo_wd");
PackDefinitionTestRepoPath = Path.Combine(sourceRelativePath, "packbuilder_testrepo_wd");

CleanupTestReposOlderThan(TimeSpan.FromMinutes(15));
}
Expand Down Expand Up @@ -176,9 +176,9 @@ public string SandboxSubmoduleSmallTestRepo()
return path;
}

protected string SandboxPackBuilderTestRepo()
protected string SandboxPackDefinitionTestRepo()
{
return Sandbox(PackBuilderTestRepoPath);
return Sandbox(PackDefinitionTestRepoPath);
}

protected string Sandbox(string sourceDirectoryPath, params string[] additionalSourcePaths)
Expand Down
7 changes: 7 additions & 0 deletions LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,13 @@ internal static extern int git_patch_line_stats(
/* Push network progress notification function */
internal delegate int git_push_transfer_progress(uint current, uint total, UIntPtr bytes, IntPtr payload);
internal delegate int git_packbuilder_progress(int stage, uint current, uint total, IntPtr payload);
internal delegate int git_packbuilder_foreach_callback(IntPtr buf, UIntPtr size, IntPtr payload);

[DllImport(libgit2)]
internal static extern int git_packbuilder_foreach(
PackBuilderSafeHandle packbuilder,
git_packbuilder_foreach_callback foreachCallback,
IntPtr payload);

[DllImport(libgit2)]
internal static extern void git_packbuilder_free(IntPtr packbuilder);
Expand Down
7 changes: 7 additions & 0 deletions LibGit2Sharp/Core/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1572,6 +1572,13 @@ public static void git_packbuilder_insert(PackBuilderSafeHandle packbuilder, Obj
Ensure.ZeroResult(res);
}

public static int git_packbuilder_foreach(
PackBuilderSafeHandle packbuilder,
NativeMethods.git_packbuilder_foreach_callback callback)
{
return NativeMethods.git_packbuilder_foreach(packbuilder, callback, IntPtr.Zero);
}

internal static void git_packbuilder_insert_commit(PackBuilderSafeHandle packbuilder, ObjectId targetId)
{
GitOid oid = targetId.Oid;
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/LibGit2Sharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
<Compile Include="MergeTreeResult.cs" />
<Compile Include="NotFoundException.cs" />
<Compile Include="GitObjectMetadata.cs" />
<Compile Include="PackBuilder.cs" />
<Compile Include="PackDefinition.cs" />
<Compile Include="PatchEntryChanges.cs" />
<Compile Include="PatchStats.cs" />
<Compile Include="PeelException.cs" />
Expand Down
Loading