Skip to content

Commit cbf521e

Browse files
committed
Ensuring arguments are not null.
1 parent 60f0cb5 commit cbf521e

File tree

3 files changed

+50
-76
lines changed

3 files changed

+50
-76
lines changed

LibGit2Sharp.Tests/PackBuilderFixture.cs

Lines changed: 18 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using LibGit2Sharp.Tests.TestHelpers;
2-
using System.Collections.Generic;
2+
using System;
33
using System.IO;
44
using System.Linq;
55
using Xunit;
@@ -9,18 +9,25 @@ namespace LibGit2Sharp.Tests
99
public class PackBuilderFixture : BaseFixture
1010
{
1111
[Fact]
12-
public void ObjectsWrittenSameAsOriginal()
12+
public void TestDefaultBuildDelegate()
13+
{
14+
TestIfSameRepoAfterPacking(null);
15+
}
16+
17+
[Fact]
18+
public void TestCommitsPerBranchBuildDelegate()
19+
{
20+
TestIfSameRepoAfterPacking(TestBuildDelegate);
21+
}
22+
23+
internal void TestIfSameRepoAfterPacking(Action<PackBuilder> buildingDelegate)
1324
{
1425
// read a repo
15-
// pack with default action
26+
// pack with the provided action
1627
// write the pack file in a mirror repo
1728
// read new repo
1829
// compare
1930

20-
//string orgRepoPath = @"D:\TestRepos\packbuilder_testrepo_wd\packbuilder_testrepo_git_org\dot_git";
21-
//string mrrRepoPath = @"D:\TestRepos\packbuilder_testrepo_wd\packbuilder_testrepo_git_mrr_1\dot_git";
22-
//string packFilePath = @"D:\TestRepos\packbuilder_testrepo_wd\packbuilder_testrepo_git_mrr_1\dot_git\objects\pack";
23-
2431
string orgRepoPath = SandboxPackBuilderTestRepo();
2532
SelfCleaningDirectory scd = BuildSelfCleaningDirectory(orgRepoPath + "_mrr");
2633
string mrrRepoPath = scd.DirectoryPath;
@@ -37,7 +44,7 @@ public void ObjectsWrittenSameAsOriginal()
3744

3845
Repository orgRepo = new Repository(orgRepoPath);
3946

40-
PackBuilderResults results = orgRepo.ObjectDatabase.Pack(packBuilderOptions, null);
47+
PackBuilderResults results = orgRepo.ObjectDatabase.Pack(packBuilderOptions, buildingDelegate);
4148

4249
// written objects count is the same as in objects database
4350
Assert.Equal(orgRepo.ObjectDatabase.Count(), results.WrittenObjectsCount);
@@ -57,53 +64,13 @@ public void ObjectsWrittenSameAsOriginal()
5764
mrrRepo.Dispose();
5865
}
5966

60-
[Fact]
61-
public void ObjectsWrittenSameAsOriginalUsingDelegate()
62-
{
63-
string orgRepoPath = SandboxPackBuilderTestRepo();
64-
SelfCleaningDirectory scd = BuildSelfCleaningDirectory(orgRepoPath + "_mrr");
65-
string mrrRepoPath = scd.DirectoryPath;
66-
string mrrRepoPathPackDirPath = mrrRepoPath + "/.git/objects";
67-
68-
DirectoryHelper.CopyFilesRecursively(new DirectoryInfo(orgRepoPath), new DirectoryInfo(mrrRepoPath));
69-
DirectoryHelper.DeleteDirectory(mrrRepoPathPackDirPath);
70-
Directory.CreateDirectory(mrrRepoPathPackDirPath);
71-
Directory.CreateDirectory(mrrRepoPathPackDirPath + "/pack");
72-
73-
PackBuilderOptions packBuilderOptions = new PackBuilderOptions();
74-
packBuilderOptions.MaximumNumberOfThreads = 0;
75-
packBuilderOptions.PackFilePath = mrrRepoPathPackDirPath + "/pack";
76-
77-
Repository orgRepo = new Repository(orgRepoPath);
78-
79-
// pack using the custom build method
80-
PackBuilderResults results = orgRepo.ObjectDatabase.Pack(packBuilderOptions, TestBuildDelegate);
81-
82-
// written objects count is the same as in objects database
83-
Assert.Equal(results.WrittenObjectsCount, orgRepo.ObjectDatabase.Count());
84-
85-
// loading a repo from the written pack file.
86-
Repository mrrRepo = new Repository(mrrRepoPath);
87-
88-
// make sure the objects of the original repo are the same as the ones in the mirror repo
89-
// doing that by making sure the count is the same, and the set differnce is empty
90-
Assert.True(mrrRepo.ObjectDatabase.Count() == orgRepo.ObjectDatabase.Count() && !mrrRepo.ObjectDatabase.Except(orgRepo.ObjectDatabase).Any());
91-
92-
Assert.Equal(orgRepo.Commits.Count(), mrrRepo.Commits.Count());
93-
Assert.Equal(orgRepo.Branches.Count(), mrrRepo.Branches.Count());
94-
Assert.Equal(orgRepo.Refs.Count(), mrrRepo.Refs.Count());
95-
96-
orgRepo.Dispose();
97-
mrrRepo.Dispose();
98-
}
99-
100-
internal void TestBuildDelegate (PackBuilder builder)
67+
internal void TestBuildDelegate(PackBuilder builder)
10168
{
102-
foreach(Branch branch in builder.Repository.Branches)
69+
foreach (Branch branch in builder.Repository.Branches)
10370
{
10471
foreach (Commit commit in branch.Commits)
10572
{
106-
builder.AddRecursively(commit);
73+
builder.AddRecursively(commit.Id);
10774
}
10875
}
10976
}

LibGit2Sharp/ObjectDatabase.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -644,8 +644,17 @@ public virtual MergeTreeResult MergeCommits(Commit ours, Commit theirs, MergeTre
644644
}
645645
}
646646

647+
/// <summary>
648+
/// Pack the objects in the <see cref="ObjectDatabase"/> and write a packfile and indexfile for it.
649+
/// </summary>
650+
/// <param name="options">Packing options</param>
651+
/// <param name="buildDelegate">Packing action. If null is passed, the pack method will invoke the default action of packing
652+
/// all objects in an arbitrary order.</param>
653+
/// <returns>Packing results</returns>
647654
public virtual PackBuilderResults Pack(PackBuilderOptions options, Action<PackBuilder> buildDelegate)
648655
{
656+
Ensure.ArgumentNotNull(options, "options");
657+
649658
PackBuilderResults results = new PackBuilderResults();
650659
PackBuilder builder = new PackBuilder(repo);
651660

@@ -657,8 +666,7 @@ public virtual PackBuilderResults Pack(PackBuilderOptions options, Action<PackBu
657666
// if no build delegate is provided, just insert all objects one by one.
658667
foreach (GitObject obj in this)
659668
{
660-
Console.WriteLine();
661-
builder.Add(obj);
669+
builder.Add(obj.Id);
662670
}
663671
}
664672
else

LibGit2Sharp/PackBuilder.cs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ namespace LibGit2Sharp
1010
public sealed class PackBuilder : IDisposable
1111
{
1212
private readonly PackBuilderSafeHandle packBuilderHandle;
13-
internal Repository repo;
13+
private readonly Repository repo;
1414

1515
/// <summary>
1616
/// Constructs a packbuilder from a repository.
1717
/// </summary>
1818
internal PackBuilder(Repository repository)
1919
{
20+
Ensure.ArgumentNotNull(repository, "repository");
21+
2022
repo = repository;
2123
packBuilderHandle = Proxy.git_packbuilder_new(repo.Handle);
2224
}
@@ -25,35 +27,23 @@ internal PackBuilder(Repository repository)
2527
/// Inserts a single object to the packbuilder.
2628
/// For an optimal pack it's mandatory to insert objects in recency order, commits followed by trees and blobs. (quoted from libgit2 API ref)
2729
/// </summary>
28-
/// <param name="gitObject">The object to be inserted.</param>
29-
public void Add(GitObject gitObject)
30+
/// <param name="gitObjectID">The object ID to be inserted.</param>
31+
public void Add(ObjectId gitObjectID)
3032
{
31-
Proxy.git_packbuilder_insert(packBuilderHandle, gitObject.Id, null);
33+
Ensure.ArgumentNotNull(gitObjectID, "gitObject");
34+
35+
Proxy.git_packbuilder_insert(packBuilderHandle, gitObjectID, null);
3236
}
3337

3438
/// <summary>
3539
/// Recursively insert an object and its referenced objects. Inserts the object as well as any object it references.
3640
/// </summary>
37-
/// <param name="gitObject">The object to be recursively inserted.</param>
38-
public void AddRecursively(GitObject gitObject)
41+
/// <param name="gitObjectID">The object ID to be recursively inserted.</param>
42+
public void AddRecursively(ObjectId gitObjectID)
3943
{
40-
Proxy.git_packbuilder_insert_recur(packBuilderHandle, gitObject.Id, null);
41-
}
44+
Ensure.ArgumentNotNull(gitObjectID, "gitObject");
4245

43-
public Repository Repository
44-
{
45-
get { return repo; }
46-
}
47-
48-
//================
49-
50-
/// <summary>
51-
/// Insert a <see cref="Tag"/> object to the packbuilder.
52-
/// </summary>
53-
/// <param name="tag">The tag object to be inserted.</param>
54-
internal void InsertTag(Tag tag)
55-
{
56-
Proxy.git_packbuilder_insert(packBuilderHandle, tag.Target.Id, null);
46+
Proxy.git_packbuilder_insert_recur(packBuilderHandle, gitObjectID, null);
5747
}
5848

5949
/// <summary>
@@ -93,13 +83,22 @@ internal long ObjectsCount
9383
}
9484

9585
/// <summary>
96-
/// Get the number of objects the packbuilder has already written out. This is only correct after the packfile has been written.
86+
/// Get the number of objects the packbuilder has already written out.
87+
/// This is only correct after the packfile has been written.
9788
/// </summary>
9889
internal long WrittenObjectsCount
9990
{
10091
get { return Proxy.git_packbuilder_written(packBuilderHandle); }
10192
}
10293

94+
/// <summary>
95+
/// A refernce to the repository that the packbuilder is working on.
96+
/// </summary>
97+
public Repository Repository
98+
{
99+
get { return repo; }
100+
}
101+
103102
internal PackBuilderSafeHandle Handle
104103
{
105104
get { return packBuilderHandle; }

0 commit comments

Comments
 (0)