4
4
using LibGit2Sharp . Tests . TestHelpers ;
5
5
using Xunit ;
6
6
using System . Collections . Generic ;
7
+ using LibGit2Sharp . Advanced ;
7
8
8
9
namespace LibGit2Sharp . Tests
9
10
{
10
11
public class PackBuilderFixture : BaseFixture
11
12
{
12
13
[ Fact ]
13
- public void TestDefaultPackDelegate ( )
14
+ public void TestDefaultPackAllWithDirPath ( )
14
15
{
15
- TestBody ( ( repo , options ) =>
16
+ TestBody ( ( repo , packDirPath ) =>
16
17
{
17
- PackBuilderResults results = repo . ObjectDatabase . Pack ( options ) ;
18
+ PackBuilderResults results = repo . ObjectDatabase . PackAll ( packDirPath ) ;
18
19
} ) ;
19
20
}
20
21
21
22
[ Fact ]
22
- public void TestCommitsPerBranchPackDelegate ( )
23
+ public void TestCommitsPerBranch ( )
23
24
{
24
- TestBody ( ( repo , options ) =>
25
+ TestBody ( ( repo , packDirPath ) =>
25
26
{
26
- PackBuilderResults results = repo . ObjectDatabase . Pack ( options , builder =>
27
+ using ( PackBuilder builder = new PackBuilder ( repo ) )
27
28
{
28
29
foreach ( Branch branch in repo . Branches )
29
30
{
@@ -37,16 +38,18 @@ public void TestCommitsPerBranchPackDelegate()
37
38
{
38
39
builder . Add ( tag . Target ) ;
39
40
}
40
- } ) ;
41
+
42
+ builder . WritePackTo ( packDirPath ) ;
43
+ }
41
44
} ) ;
42
45
}
43
46
44
47
[ Fact ]
45
- public void TestCommitsPerBranchIdsPackDelegate ( )
48
+ public void TestCommitsPerBranchIds ( )
46
49
{
47
- TestBody ( ( repo , options ) =>
50
+ TestBody ( ( repo , packDirPath ) =>
48
51
{
49
- PackBuilderResults results = repo . ObjectDatabase . Pack ( options , builder =>
52
+ using ( PackBuilder builder = new PackBuilder ( repo ) )
50
53
{
51
54
foreach ( Branch branch in repo . Branches )
52
55
{
@@ -60,38 +63,46 @@ public void TestCommitsPerBranchIdsPackDelegate()
60
63
{
61
64
builder . Add ( tag . Target . Id ) ;
62
65
}
63
- } ) ;
66
+
67
+ builder . WritePackTo ( packDirPath ) ;
68
+ }
64
69
} ) ;
65
70
}
66
71
67
72
[ Fact ]
68
73
public void TestCreatingMultiplePackFilesByType ( )
69
74
{
70
- TestBody ( ( repo , options ) =>
75
+ TestBody ( ( repo , packDirPath ) =>
71
76
{
72
77
long totalNumberOfWrittenObjects = 0 ;
73
- PackBuilderResults results ;
74
78
75
- for ( int i = 0 ; i < 3 ; i ++ )
79
+ using ( PackBuilder builder = new PackBuilder ( repo ) )
76
80
{
77
- results = repo . ObjectDatabase . Pack ( options , b =>
81
+ for ( int i = 0 ; i < 3 ; i ++ )
78
82
{
79
83
foreach ( GitObject obj in repo . ObjectDatabase )
80
84
{
81
85
if ( i == 0 && obj is Commit )
82
- b . Add ( obj . Id ) ;
86
+ builder . Add ( obj . Id ) ;
87
+
83
88
if ( i == 1 && obj is Tree )
84
- b . Add ( obj . Id ) ;
89
+ builder . Add ( obj . Id ) ;
90
+
85
91
if ( i == 2 && obj is Blob )
86
- b . Add ( obj . Id ) ;
92
+ builder . Add ( obj . Id ) ;
87
93
}
88
- } ) ;
89
94
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 ( ) ;
93
99
94
- totalNumberOfWrittenObjects += results . WrittenObjectsCount ;
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" ) ) ) ;
103
+
104
+ totalNumberOfWrittenObjects += results . WrittenObjectsCount ;
105
+ }
95
106
}
96
107
97
108
// assert total number of written objects count is the same as in objects database
@@ -102,34 +113,43 @@ public void TestCreatingMultiplePackFilesByType()
102
113
[ Fact ]
103
114
public void TestCreatingMultiplePackFilesByCount ( )
104
115
{
105
- TestBody ( ( repo , options ) =>
116
+ TestBody ( ( repo , packDirPath ) =>
106
117
{
107
118
long totalNumberOfWrittenObjects = 0 ;
108
119
PackBuilderResults results ;
109
120
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 ) )
116
122
{
117
- results = repo . ObjectDatabase . Pack ( options , b =>
123
+ int currentObject = 0 ;
124
+
125
+ foreach ( GitObject gitObject in repo . ObjectDatabase )
118
126
{
119
- while ( currentObject < totalObjectCount )
127
+ packBuilder . Add ( gitObject . Id ) ;
128
+
129
+ if ( currentObject ++ % 100 == 0 )
120
130
{
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" ) ) ) ;
122
137
123
- if ( currentObject ++ % 100 == 0 )
124
- break ;
138
+ totalNumberOfWrittenObjects += results . WrittenObjectsCount ;
125
139
}
126
- } ) ;
140
+ }
141
+
142
+ if ( currentObject % 100 != 1 )
143
+ {
144
+ results = packBuilder . WritePackTo ( packDirPath ) ;
145
+ packBuilder . Reset ( ) ;
127
146
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" ) ) ) ;
131
150
132
- totalNumberOfWrittenObjects += results . WrittenObjectsCount ;
151
+ totalNumberOfWrittenObjects += results . WrittenObjectsCount ;
152
+ }
133
153
}
134
154
135
155
// assert total number of written objects count is the same as in objects database
@@ -140,13 +160,13 @@ public void TestCreatingMultiplePackFilesByCount()
140
160
[ Fact ]
141
161
public void CanWritePackAndIndexFiles ( )
142
162
{
143
- using ( Repository repo = new Repository ( SandboxStandardTestRepo ( ) ) )
163
+ using ( Repository repo = new Repository ( SandboxPackBuilderTestRepo ( ) ) )
144
164
{
145
165
string path = Path . GetTempPath ( ) ;
146
- PackBuilderResults results = repo . ObjectDatabase . Pack ( new PackBuilderOptions ( path ) ) ;
147
166
148
- Assert . Equal ( repo . ObjectDatabase . Count ( ) , results . WrittenObjectsCount ) ;
167
+ PackBuilderResults results = repo . ObjectDatabase . PackAll ( path ) ;
149
168
169
+ Assert . Equal ( repo . ObjectDatabase . Count ( ) , results . WrittenObjectsCount ) ;
150
170
Assert . True ( File . Exists ( Path . Combine ( path , "pack-" + results . PackHash + ".pack" ) ) ) ;
151
171
Assert . True ( File . Exists ( Path . Combine ( path , "pack-" + results . PackHash + ".idx" ) ) ) ;
152
172
}
@@ -158,13 +178,14 @@ public void TestEmptyPackFile()
158
178
using ( Repository repo = new Repository ( SandboxPackBuilderTestRepo ( ) ) )
159
179
{
160
180
string path = Path . GetTempPath ( ) ;
161
- PackBuilderResults results = repo . ObjectDatabase . Pack ( new PackBuilderOptions ( path ) , b =>
162
- {
163
181
164
- } ) ;
182
+ using ( PackBuilder builder = new PackBuilder ( repo ) )
183
+ {
184
+ PackBuilderResults results = builder . WritePackTo ( path ) ;
165
185
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
+ }
168
189
}
169
190
}
170
191
@@ -174,113 +195,67 @@ public void TestPackFileForEmptyRepository()
174
195
using ( Repository repo = new Repository ( InitNewRepository ( ) ) )
175
196
{
176
197
string path = Path . GetTempPath ( ) ;
177
- PackBuilderResults results = repo . ObjectDatabase . Pack ( new PackBuilderOptions ( path ) ) ;
178
198
199
+ PackBuilderResults results = repo . ObjectDatabase . PackAll ( path ) ;
200
+
201
+ Assert . Equal ( repo . ObjectDatabase . Count ( ) , results . WrittenObjectsCount ) ;
179
202
Assert . True ( File . Exists ( Path . Combine ( path , "pack-" + results . PackHash + ".pack" ) ) ) ;
180
203
Assert . True ( File . Exists ( Path . Combine ( path , "pack-" + results . PackHash + ".idx" ) ) ) ;
181
204
}
182
205
}
183
206
184
207
[ Fact ]
185
- public void ExceptionIfPathDoesNotExist ( )
208
+ public void ExceptionIfPathDoesNotExistAtPackAll ( )
186
209
{
187
- PackBuilderOptions pbo ;
188
-
189
- Assert . Throws < DirectoryNotFoundException > ( ( ) =>
190
- {
191
- pbo = new PackBuilderOptions ( "aaa" ) ;
192
- } ) ;
193
- }
194
-
195
- [ Fact ]
196
- public void ExceptionIfPathEqualsNull ( )
197
- {
198
- PackBuilderOptions pbo ;
199
-
200
- Assert . Throws < ArgumentNullException > ( ( ) =>
201
- {
202
- pbo = new PackBuilderOptions ( null ) ;
203
- } ) ;
204
- }
205
-
206
- [ Fact ]
207
- public void ExceptionIfOptionsEqualsNull ( )
208
- {
209
- string orgRepoPath = SandboxPackBuilderTestRepo ( ) ;
210
-
211
- using ( Repository orgRepo = new Repository ( orgRepoPath ) )
210
+ using ( Repository repo = new Repository ( SandboxPackBuilderTestRepo ( ) ) )
212
211
{
213
- Assert . Throws < ArgumentNullException > ( ( ) =>
212
+ Assert . Throws < DirectoryNotFoundException > ( ( ) =>
214
213
{
215
- orgRepo . ObjectDatabase . Pack ( null ) ;
214
+ PackBuilderResults results = repo . ObjectDatabase . PackAll ( "aaaaa" ) ;
216
215
} ) ;
217
216
}
218
217
}
219
218
220
219
[ Fact ]
221
- public void ExceptionIfBuildDelegateEqualsNull ( )
220
+ public void ExceptionIfPathDoesNotExistAtWriteToPack ( )
222
221
{
223
- string orgRepoPath = SandboxPackBuilderTestRepo ( ) ;
224
- PackBuilderOptions packBuilderOptions = new PackBuilderOptions ( orgRepoPath ) ;
225
-
226
- using ( Repository orgRepo = new Repository ( orgRepoPath ) )
222
+ using ( Repository repo = new Repository ( SandboxPackBuilderTestRepo ( ) ) )
223
+ using ( PackBuilder builder = new PackBuilder ( repo ) )
227
224
{
228
- Assert . Throws < ArgumentNullException > ( ( ) =>
225
+ Assert . Throws < DirectoryNotFoundException > ( ( ) =>
229
226
{
230
- orgRepo . ObjectDatabase . Pack ( packBuilderOptions , null ) ;
227
+ PackBuilderResults results = builder . WritePackTo ( "aaaaa" ) ;
231
228
} ) ;
232
229
}
233
230
}
234
231
235
- [ Fact ]
236
- public void ExceptionIfNegativeNumberOfThreads ( )
237
- {
238
- string orgRepoPath = SandboxPackBuilderTestRepo ( ) ;
239
- PackBuilderOptions packBuilderOptions = new PackBuilderOptions ( orgRepoPath ) ;
240
-
241
- Assert . Throws < ArgumentException > ( ( ) =>
242
- {
243
- packBuilderOptions . MaximumNumberOfThreads = - 1 ;
244
- } ) ;
245
- }
246
-
247
232
[ Fact ]
248
233
public void ExceptionIfAddNullObjectID ( )
249
234
{
250
- string orgRepoPath = SandboxPackBuilderTestRepo ( ) ;
251
- PackBuilderOptions packBuilderOptions = new PackBuilderOptions ( orgRepoPath ) ;
252
-
253
- using ( Repository orgRepo = new Repository ( orgRepoPath ) )
235
+ using ( Repository repo = new Repository ( SandboxPackBuilderTestRepo ( ) ) )
236
+ using ( PackBuilder builder = new PackBuilder ( repo ) )
254
237
{
255
238
Assert . Throws < ArgumentNullException > ( ( ) =>
256
239
{
257
- orgRepo . ObjectDatabase . Pack ( packBuilderOptions , builder =>
258
- {
259
- builder . Add ( null ) ;
260
- } ) ;
240
+ builder . Add ( null ) ;
261
241
} ) ;
262
242
}
263
243
}
264
244
265
245
[ Fact ]
266
246
public void ExceptionIfAddRecursivelyNullObjectID ( )
267
247
{
268
- string orgRepoPath = SandboxPackBuilderTestRepo ( ) ;
269
- PackBuilderOptions packBuilderOptions = new PackBuilderOptions ( orgRepoPath ) ;
270
-
271
- using ( Repository orgRepo = new Repository ( orgRepoPath ) )
248
+ using ( Repository repo = new Repository ( SandboxPackBuilderTestRepo ( ) ) )
249
+ using ( PackBuilder builder = new PackBuilder ( repo ) )
272
250
{
273
251
Assert . Throws < ArgumentNullException > ( ( ) =>
274
252
{
275
- orgRepo . ObjectDatabase . Pack ( packBuilderOptions , builder =>
276
- {
277
- builder . AddRecursively ( null ) ;
278
- } ) ;
253
+ builder . AddRecursively ( null ) ;
279
254
} ) ;
280
255
}
281
256
}
282
257
283
- internal void TestBody ( Action < IRepository , PackBuilderOptions > fullPackingAction )
258
+ internal void TestBody ( Action < Repository , string > fullPackingAction )
284
259
{
285
260
// read a repo, pack with the provided action, write the pack file in a mirror repo, read new repo, compare
286
261
@@ -291,11 +266,9 @@ internal void TestBody(Action<IRepository, PackBuilderOptions> fullPackingAction
291
266
DirectoryHelper . DeleteDirectory ( mrrRepoPackDirPath ) ;
292
267
Directory . CreateDirectory ( mrrRepoPackDirPath + "/pack" ) ;
293
268
294
- PackBuilderOptions packBuilderOptions = new PackBuilderOptions ( mrrRepoPackDirPath + "/pack" ) ;
295
-
296
269
using ( Repository orgRepo = new Repository ( orgRepoPath ) )
297
270
{
298
- fullPackingAction ( orgRepo , packBuilderOptions ) ;
271
+ fullPackingAction ( orgRepo , mrrRepoPackDirPath + "/pack" ) ;
299
272
300
273
// loading the mirror repo from the written pack file and make sure it's identical to the original.
301
274
using ( Repository mrrRepo = new Repository ( mrrRepoPath ) )
0 commit comments