Skip to content

Commit 6035a81

Browse files
committed
Updating PackBuilder design, moving it to Advanced namespace, allowing explicit write and reset calls
1 parent 7924b6a commit 6035a81

File tree

3 files changed

+146
-219
lines changed

3 files changed

+146
-219
lines changed

LibGit2Sharp.Tests/PackBuilderFixture.cs

Lines changed: 87 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,27 @@
44
using LibGit2Sharp.Tests.TestHelpers;
55
using Xunit;
66
using System.Collections.Generic;
7+
using LibGit2Sharp.Advanced;
78

89
namespace LibGit2Sharp.Tests
910
{
1011
public class PackBuilderFixture : BaseFixture
1112
{
1213
[Fact]
13-
public void TestDefaultPackDelegate()
14+
public void TestDefaultPackAllWithDirPath()
1415
{
15-
TestBody((repo, options) =>
16+
TestBody((repo, packDirPath) =>
1617
{
17-
PackBuilderResults results = repo.ObjectDatabase.Pack(options);
18+
PackBuilderResults results = repo.ObjectDatabase.PackAll(packDirPath);
1819
});
1920
}
2021

2122
[Fact]
22-
public void TestCommitsPerBranchPackDelegate()
23+
public void TestCommitsPerBranch()
2324
{
24-
TestBody((repo, options) =>
25+
TestBody((repo, packDirPath) =>
2526
{
26-
PackBuilderResults results = repo.ObjectDatabase.Pack(options, builder =>
27+
using (PackBuilder builder = new PackBuilder(repo))
2728
{
2829
foreach (Branch branch in repo.Branches)
2930
{
@@ -37,16 +38,18 @@ public void TestCommitsPerBranchPackDelegate()
3738
{
3839
builder.Add(tag.Target);
3940
}
40-
});
41+
42+
builder.WritePackTo(packDirPath);
43+
}
4144
});
4245
}
4346

4447
[Fact]
45-
public void TestCommitsPerBranchIdsPackDelegate()
48+
public void TestCommitsPerBranchIds()
4649
{
47-
TestBody((repo, options) =>
50+
TestBody((repo, packDirPath) =>
4851
{
49-
PackBuilderResults results = repo.ObjectDatabase.Pack(options, builder =>
52+
using (PackBuilder builder = new PackBuilder(repo))
5053
{
5154
foreach (Branch branch in repo.Branches)
5255
{
@@ -60,38 +63,46 @@ public void TestCommitsPerBranchIdsPackDelegate()
6063
{
6164
builder.Add(tag.Target.Id);
6265
}
63-
});
66+
67+
builder.WritePackTo(packDirPath);
68+
}
6469
});
6570
}
6671

6772
[Fact]
6873
public void TestCreatingMultiplePackFilesByType()
6974
{
70-
TestBody((repo, options) =>
75+
TestBody((repo, packDirPath) =>
7176
{
7277
long totalNumberOfWrittenObjects = 0;
73-
PackBuilderResults results;
7478

75-
for (int i = 0; i < 3; i++)
79+
using (PackBuilder builder = new PackBuilder(repo))
7680
{
77-
results = repo.ObjectDatabase.Pack(options, b =>
81+
for (int i = 0; i < 3; i++)
7882
{
7983
foreach (GitObject obj in repo.ObjectDatabase)
8084
{
8185
if (i == 0 && obj is Commit)
82-
b.Add(obj.Id);
86+
builder.Add(obj.Id);
87+
8388
if (i == 1 && obj is Tree)
84-
b.Add(obj.Id);
89+
builder.Add(obj.Id);
90+
8591
if (i == 2 && obj is Blob)
86-
b.Add(obj.Id);
92+
builder.Add(obj.Id);
8793
}
88-
});
8994

90-
// assert the pack file is written
91-
Assert.True(File.Exists(Path.Combine(options.PackDirectoryPath, "pack-" + results.PackHash + ".pack")));
92-
Assert.True(File.Exists(Path.Combine(options.PackDirectoryPath, "pack-" + results.PackHash + ".idx")));
95+
PackBuilderResults results = builder.WritePackTo(packDirPath);
96+
97+
// for reuse to build the next pack file.
98+
builder.Reset();
99+
100+
// assert the pack file is written
101+
Assert.True(File.Exists(Path.Combine(packDirPath, "pack-" + results.PackHash + ".pack")));
102+
Assert.True(File.Exists(Path.Combine(packDirPath, "pack-" + results.PackHash + ".idx")));
93103

94-
totalNumberOfWrittenObjects += results.WrittenObjectsCount;
104+
totalNumberOfWrittenObjects += results.WrittenObjectsCount;
105+
}
95106
}
96107

97108
// assert total number of written objects count is the same as in objects database
@@ -102,34 +113,43 @@ public void TestCreatingMultiplePackFilesByType()
102113
[Fact]
103114
public void TestCreatingMultiplePackFilesByCount()
104115
{
105-
TestBody((repo, options) =>
116+
TestBody((repo, packDirPath) =>
106117
{
107118
long totalNumberOfWrittenObjects = 0;
108119
PackBuilderResults results;
109120

110-
List<GitObject> objectsList = repo.ObjectDatabase.ToList();
111-
int totalObjectCount = objectsList.Count;
112-
113-
int currentObject = 0;
114-
115-
while (currentObject < totalObjectCount)
121+
using (PackBuilder packBuilder = new PackBuilder(repo))
116122
{
117-
results = repo.ObjectDatabase.Pack(options, b =>
123+
int currentObject = 0;
124+
125+
foreach (GitObject gitObject in repo.ObjectDatabase)
118126
{
119-
while (currentObject < totalObjectCount)
127+
packBuilder.Add(gitObject.Id);
128+
129+
if (currentObject++ % 100 == 0)
120130
{
121-
b.Add(objectsList[currentObject]);
131+
results = packBuilder.WritePackTo(packDirPath);
132+
packBuilder.Reset();
133+
134+
// assert the pack file is written
135+
Assert.True(File.Exists(Path.Combine(packDirPath, "pack-" + results.PackHash + ".pack")));
136+
Assert.True(File.Exists(Path.Combine(packDirPath, "pack-" + results.PackHash + ".idx")));
122137

123-
if (currentObject++ % 100 == 0)
124-
break;
138+
totalNumberOfWrittenObjects += results.WrittenObjectsCount;
125139
}
126-
});
140+
}
141+
142+
if (currentObject % 100 != 1)
143+
{
144+
results = packBuilder.WritePackTo(packDirPath);
145+
packBuilder.Reset();
127146

128-
// assert the pack file is written
129-
Assert.True(File.Exists(Path.Combine(options.PackDirectoryPath, "pack-" + results.PackHash + ".pack")));
130-
Assert.True(File.Exists(Path.Combine(options.PackDirectoryPath, "pack-" + results.PackHash + ".idx")));
147+
// assert the pack file is written
148+
Assert.True(File.Exists(Path.Combine(packDirPath, "pack-" + results.PackHash + ".pack")));
149+
Assert.True(File.Exists(Path.Combine(packDirPath, "pack-" + results.PackHash + ".idx")));
131150

132-
totalNumberOfWrittenObjects += results.WrittenObjectsCount;
151+
totalNumberOfWrittenObjects += results.WrittenObjectsCount;
152+
}
133153
}
134154

135155
// assert total number of written objects count is the same as in objects database
@@ -140,13 +160,13 @@ public void TestCreatingMultiplePackFilesByCount()
140160
[Fact]
141161
public void CanWritePackAndIndexFiles()
142162
{
143-
using (Repository repo = new Repository(SandboxStandardTestRepo()))
163+
using (Repository repo = new Repository(SandboxPackBuilderTestRepo()))
144164
{
145165
string path = Path.GetTempPath();
146-
PackBuilderResults results = repo.ObjectDatabase.Pack(new PackBuilderOptions(path));
147166

148-
Assert.Equal(repo.ObjectDatabase.Count(), results.WrittenObjectsCount);
167+
PackBuilderResults results = repo.ObjectDatabase.PackAll(path);
149168

169+
Assert.Equal(repo.ObjectDatabase.Count(), results.WrittenObjectsCount);
150170
Assert.True(File.Exists(Path.Combine(path, "pack-" + results.PackHash + ".pack")));
151171
Assert.True(File.Exists(Path.Combine(path, "pack-" + results.PackHash + ".idx")));
152172
}
@@ -158,13 +178,14 @@ public void TestEmptyPackFile()
158178
using (Repository repo = new Repository(SandboxPackBuilderTestRepo()))
159179
{
160180
string path = Path.GetTempPath();
161-
PackBuilderResults results = repo.ObjectDatabase.Pack(new PackBuilderOptions(path), b =>
162-
{
163181

164-
});
182+
using (PackBuilder builder = new PackBuilder(repo))
183+
{
184+
PackBuilderResults results = builder.WritePackTo(path);
165185

166-
Assert.True(File.Exists(Path.Combine(path, "pack-" + results.PackHash + ".pack")));
167-
Assert.True(File.Exists(Path.Combine(path, "pack-" + results.PackHash + ".idx")));
186+
Assert.True(File.Exists(Path.Combine(path, "pack-" + results.PackHash + ".pack")));
187+
Assert.True(File.Exists(Path.Combine(path, "pack-" + results.PackHash + ".idx")));
188+
}
168189
}
169190
}
170191

@@ -174,103 +195,55 @@ public void TestPackFileForEmptyRepository()
174195
using (Repository repo = new Repository(InitNewRepository()))
175196
{
176197
string path = Path.GetTempPath();
177-
PackBuilderResults results = repo.ObjectDatabase.Pack(new PackBuilderOptions(path));
178198

199+
PackBuilderResults results = repo.ObjectDatabase.PackAll(path);
200+
201+
Assert.Equal(repo.ObjectDatabase.Count(), results.WrittenObjectsCount);
179202
Assert.True(File.Exists(Path.Combine(path, "pack-" + results.PackHash + ".pack")));
180203
Assert.True(File.Exists(Path.Combine(path, "pack-" + results.PackHash + ".idx")));
181204
}
182205
}
183206

184207
[Fact]
185-
public void ExceptionIfPathDoesNotExist()
186-
{
187-
Assert.Throws<DirectoryNotFoundException>(() => new PackBuilderOptions("aaa"));
188-
}
189-
190-
[Fact]
191-
public void ExceptionIfPathEqualsNull()
192-
{
193-
Assert.Throws<ArgumentNullException>(() => new PackBuilderOptions(null));
194-
}
195-
196-
[Fact]
197-
public void ExceptionIfOptionsEqualsNull()
208+
public void ExceptionIfPathDoesNotExistAtPackAll()
198209
{
199-
string orgRepoPath = SandboxPackBuilderTestRepo();
200-
201-
using (Repository orgRepo = new Repository(orgRepoPath))
210+
using (Repository repo = new Repository(SandboxPackBuilderTestRepo()))
202211
{
203-
Assert.Throws<ArgumentNullException>(() =>
204-
{
205-
orgRepo.ObjectDatabase.Pack(null);
206-
});
212+
Assert.Throws<DirectoryNotFoundException>(() => repo.ObjectDatabase.PackAll("aaaaa"));
207213
}
208214
}
209215

210216
[Fact]
211-
public void ExceptionIfBuildDelegateEqualsNull()
217+
public void ExceptionIfPathDoesNotExistAtWriteToPack()
212218
{
213-
string orgRepoPath = SandboxPackBuilderTestRepo();
214-
PackBuilderOptions packBuilderOptions = new PackBuilderOptions(orgRepoPath);
215-
216-
using (Repository orgRepo = new Repository(orgRepoPath))
219+
using (Repository repo = new Repository(SandboxPackBuilderTestRepo()))
220+
using (PackBuilder builder = new PackBuilder(repo))
217221
{
218-
Assert.Throws<ArgumentNullException>(() =>
219-
{
220-
orgRepo.ObjectDatabase.Pack(packBuilderOptions, null);
221-
});
222+
Assert.Throws<DirectoryNotFoundException>(() => builder.WritePackTo("aaaaa"));
222223
}
223224
}
224225

225-
[Fact]
226-
public void ExceptionIfNegativeNumberOfThreads()
227-
{
228-
string orgRepoPath = SandboxPackBuilderTestRepo();
229-
PackBuilderOptions packBuilderOptions = new PackBuilderOptions(orgRepoPath);
230-
231-
Assert.Throws<ArgumentException>(() =>
232-
{
233-
packBuilderOptions.MaximumNumberOfThreads = -1;
234-
});
235-
}
236-
237226
[Fact]
238227
public void ExceptionIfAddNullObjectID()
239228
{
240-
string orgRepoPath = SandboxPackBuilderTestRepo();
241-
PackBuilderOptions packBuilderOptions = new PackBuilderOptions(orgRepoPath);
242-
243-
using (Repository orgRepo = new Repository(orgRepoPath))
229+
using (Repository repo = new Repository(SandboxPackBuilderTestRepo()))
230+
using (PackBuilder builder = new PackBuilder(repo))
244231
{
245-
Assert.Throws<ArgumentNullException>(() =>
246-
{
247-
orgRepo.ObjectDatabase.Pack(packBuilderOptions, builder =>
248-
{
249-
builder.Add(null);
250-
});
251-
});
232+
Assert.Throws<ArgumentNullException>(() => builder.Add(null));
252233
}
253234
}
254235

255236
[Fact]
256237
public void ExceptionIfAddRecursivelyNullObjectID()
257238
{
258-
string orgRepoPath = SandboxPackBuilderTestRepo();
259-
PackBuilderOptions packBuilderOptions = new PackBuilderOptions(orgRepoPath);
260-
261-
using (Repository orgRepo = new Repository(orgRepoPath))
239+
using (Repository repo = new Repository(SandboxPackBuilderTestRepo()))
240+
using (PackBuilder builder = new PackBuilder(repo))
262241
{
263-
Assert.Throws<ArgumentNullException>(() =>
264-
{
265-
orgRepo.ObjectDatabase.Pack(packBuilderOptions, builder =>
266-
{
267-
builder.AddRecursively(null);
268-
});
269-
});
242+
Assert.Throws<ArgumentNullException>(() => builder.AddRecursively(null));
270243
}
271244
}
272245

273-
internal void TestBody(Action<IRepository, PackBuilderOptions> fullPackingAction)
246+
internal void TestBody(Action<Repository, string> fullPackingAction)
274247
{
275248
// read a repo, pack with the provided action, write the pack file in a mirror repo, read new repo, compare
276249

@@ -281,11 +254,9 @@ internal void TestBody(Action<IRepository, PackBuilderOptions> fullPackingAction
281254
DirectoryHelper.DeleteDirectory(mrrRepoPackDirPath);
282255
Directory.CreateDirectory(mrrRepoPackDirPath + "/pack");
283256

284-
PackBuilderOptions packBuilderOptions = new PackBuilderOptions(mrrRepoPackDirPath + "/pack");
285-
286257
using (Repository orgRepo = new Repository(orgRepoPath))
287258
{
288-
fullPackingAction(orgRepo, packBuilderOptions);
259+
fullPackingAction(orgRepo, mrrRepoPackDirPath + "/pack");
289260

290261
// loading the mirror repo from the written pack file and make sure it's identical to the original.
291262
using (Repository mrrRepo = new Repository(mrrRepoPath))

0 commit comments

Comments
 (0)