3
3
using System . Linq ;
4
4
using LibGit2Sharp . Tests . TestHelpers ;
5
5
using Xunit ;
6
+ using System . Collections . Generic ;
6
7
7
8
namespace LibGit2Sharp . Tests
8
9
{
@@ -11,85 +12,71 @@ public class PackBuilderFixture : BaseFixture
11
12
[ Fact ]
12
13
public void TestDefaultPackDelegate ( )
13
14
{
14
- TestIfSameRepoAfterPacking ( null ) ;
15
+ TestBody ( ( repo , options ) =>
16
+ {
17
+ PackBuilderResults results = repo . ObjectDatabase . Pack ( options ) ;
18
+ } ) ;
15
19
}
16
20
17
21
[ Fact ]
18
22
public void TestCommitsPerBranchPackDelegate ( )
19
23
{
20
- TestIfSameRepoAfterPacking ( AddingObjectIdsTestDelegate ) ;
24
+ TestBody ( ( repo , options ) =>
25
+ {
26
+ PackBuilderResults results = repo . ObjectDatabase . Pack ( options , builder =>
27
+ {
28
+ foreach ( Branch branch in repo . Branches )
29
+ {
30
+ foreach ( Commit commit in branch . Commits )
31
+ {
32
+ builder . AddRecursively ( commit ) ;
33
+ }
34
+ }
35
+
36
+ foreach ( Tag tag in repo . Tags )
37
+ {
38
+ builder . Add ( tag . Target ) ;
39
+ }
40
+ } ) ;
41
+ } ) ;
21
42
}
22
43
23
44
[ Fact ]
24
45
public void TestCommitsPerBranchIdsPackDelegate ( )
25
46
{
26
- TestIfSameRepoAfterPacking ( AddingObjectsTestDelegate ) ;
27
- }
28
-
29
- internal void TestIfSameRepoAfterPacking ( Action < IRepository , PackBuilder > packDelegate )
30
- {
31
- // read a repo
32
- // pack with the provided action
33
- // write the pack file in a mirror repo
34
- // read new repo
35
- // compare
36
-
37
- string orgRepoPath = SandboxPackBuilderTestRepo ( ) ;
38
- string mrrRepoPath = SandboxPackBuilderTestRepo ( ) ;
39
- string mrrRepoPackDirPath = Path . Combine ( mrrRepoPath + "/.git/objects" ) ;
40
-
41
- DirectoryHelper . DeleteDirectory ( mrrRepoPackDirPath ) ;
42
- Directory . CreateDirectory ( mrrRepoPackDirPath + "/pack" ) ;
43
-
44
- PackBuilderOptions packBuilderOptions = new PackBuilderOptions ( mrrRepoPackDirPath + "/pack" ) ;
45
-
46
- using ( Repository orgRepo = new Repository ( orgRepoPath ) )
47
+ TestBody ( ( repo , options ) =>
47
48
{
48
- PackBuilderResults results ;
49
- if ( packDelegate != null )
50
- results = orgRepo . ObjectDatabase . Pack ( packBuilderOptions , b => packDelegate ( orgRepo , b ) ) ;
51
- else
52
- results = orgRepo . ObjectDatabase . Pack ( packBuilderOptions ) ;
53
-
54
- // written objects count is the same as in objects database
55
- Assert . Equal ( orgRepo . ObjectDatabase . Count ( ) , results . WrittenObjectsCount ) ;
56
-
57
- // loading a repo from the written pack file.
58
- using ( Repository mrrRepo = new Repository ( mrrRepoPath ) )
49
+ PackBuilderResults results = repo . ObjectDatabase . Pack ( options , builder =>
59
50
{
60
- // make sure the objects of the original repo are the same as the ones in the mirror repo
61
- // doing that by making sure the count is the same, and the set difference is empty
62
- Assert . True ( mrrRepo . ObjectDatabase . Count ( ) == orgRepo . ObjectDatabase . Count ( ) && ! mrrRepo . ObjectDatabase . Except ( orgRepo . ObjectDatabase ) . Any ( ) ) ;
51
+ foreach ( Branch branch in repo . Branches )
52
+ {
53
+ foreach ( Commit commit in branch . Commits )
54
+ {
55
+ builder . AddRecursively ( commit . Id ) ;
56
+ }
57
+ }
63
58
64
- Assert . Equal ( orgRepo . Commits . Count ( ) , mrrRepo . Commits . Count ( ) ) ;
65
- Assert . Equal ( orgRepo . Branches . Count ( ) , mrrRepo . Branches . Count ( ) ) ;
66
- Assert . Equal ( orgRepo . Refs . Count ( ) , mrrRepo . Refs . Count ( ) ) ;
67
- }
68
- }
59
+ foreach ( Tag tag in repo . Tags )
60
+ {
61
+ builder . Add ( tag . Target . Id ) ;
62
+ }
63
+ } ) ;
64
+ } ) ;
69
65
}
70
66
71
67
[ Fact ]
72
- internal void TestCreatingMultiplePackFiles ( )
68
+ public void TestCreatingMultiplePackFilesByType ( )
73
69
{
74
- string orgRepoPath = SandboxPackBuilderTestRepo ( ) ;
75
- string mrrRepoPath = SandboxPackBuilderTestRepo ( ) ;
76
- string mrrRepoPackDirPath = Path . Combine ( mrrRepoPath + "/.git/objects" ) ;
77
-
78
- DirectoryHelper . DeleteDirectory ( mrrRepoPackDirPath ) ;
79
- Directory . CreateDirectory ( mrrRepoPackDirPath + "/pack" ) ;
80
-
81
- PackBuilderOptions packBuilderOptions = new PackBuilderOptions ( mrrRepoPackDirPath + "/pack" ) ;
82
-
83
- using ( Repository orgRepo = new Repository ( orgRepoPath ) )
70
+ TestBody ( ( repo , options ) =>
84
71
{
85
72
long totalNumberOfWrittenObjects = 0 ;
86
73
PackBuilderResults results ;
87
74
88
75
for ( int i = 0 ; i < 3 ; i ++ )
89
76
{
90
- results = results = orgRepo . ObjectDatabase . Pack ( packBuilderOptions , b =>
77
+ results = repo . ObjectDatabase . Pack ( options , b =>
91
78
{
92
- foreach ( GitObject obj in orgRepo . ObjectDatabase )
79
+ foreach ( GitObject obj in repo . ObjectDatabase )
93
80
{
94
81
if ( i == 0 && obj is Commit )
95
82
b . Add ( obj . Id ) ;
@@ -100,55 +87,97 @@ internal void TestCreatingMultiplePackFiles()
100
87
}
101
88
} ) ;
102
89
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" ) ) ) ;
93
+
103
94
totalNumberOfWrittenObjects += results . WrittenObjectsCount ;
104
95
}
105
96
106
- // written objects count is the same as in objects database
107
- Assert . Equal ( orgRepo . ObjectDatabase . Count ( ) , totalNumberOfWrittenObjects ) ;
97
+ // assert total number of written objects count is the same as in objects database
98
+ Assert . Equal ( repo . ObjectDatabase . Count ( ) , totalNumberOfWrittenObjects ) ;
99
+ } ) ;
100
+ }
108
101
109
- // loading a repo from the written pack file.
110
- using ( Repository mrrRepo = new Repository ( mrrRepoPath ) )
102
+ [ Fact ]
103
+ public void TestCreatingMultiplePackFilesByCount ( )
104
+ {
105
+ TestBody ( ( repo , options ) =>
106
+ {
107
+ long totalNumberOfWrittenObjects = 0 ;
108
+ PackBuilderResults results ;
109
+
110
+ List < GitObject > objectsList = repo . ObjectDatabase . ToList ( ) ;
111
+ int totalObjectCount = objectsList . Count ;
112
+
113
+ int currentObject = 0 ;
114
+
115
+ while ( currentObject < totalObjectCount )
111
116
{
112
- // make sure the objects of the original repo are the same as the ones in the mirror repo
113
- // doing that by making sure the count is the same, and the set difference is empty
114
- Assert . True ( mrrRepo . ObjectDatabase . Count ( ) == orgRepo . ObjectDatabase . Count ( ) && ! mrrRepo . ObjectDatabase . Except ( orgRepo . ObjectDatabase ) . Any ( ) ) ;
117
+ results = repo . ObjectDatabase . Pack ( options , b =>
118
+ {
119
+ while ( currentObject < totalObjectCount )
120
+ {
121
+ b . Add ( objectsList [ currentObject ] ) ;
115
122
116
- Assert . Equal ( orgRepo . Commits . Count ( ) , mrrRepo . Commits . Count ( ) ) ;
117
- Assert . Equal ( orgRepo . Branches . Count ( ) , mrrRepo . Branches . Count ( ) ) ;
118
- Assert . Equal ( orgRepo . Refs . Count ( ) , mrrRepo . Refs . Count ( ) ) ;
123
+ if ( currentObject ++ % 100 == 0 )
124
+ break ;
125
+ }
126
+ } ) ;
127
+
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" ) ) ) ;
131
+
132
+ totalNumberOfWrittenObjects += results . WrittenObjectsCount ;
119
133
}
120
- }
134
+
135
+ // assert total number of written objects count is the same as in objects database
136
+ Assert . Equal ( repo . ObjectDatabase . Count ( ) , totalNumberOfWrittenObjects ) ;
137
+ } ) ;
121
138
}
122
139
123
- internal void AddingObjectIdsTestDelegate ( IRepository repo , PackBuilder builder )
140
+ [ Fact ]
141
+ public void CanWritePackAndIndexFiles ( )
124
142
{
125
- foreach ( Branch branch in repo . Branches )
143
+ using ( Repository repo = new Repository ( SandboxStandardTestRepo ( ) ) )
126
144
{
127
- foreach ( Commit commit in branch . Commits )
128
- {
129
- builder . AddRecursively ( commit . Id ) ;
130
- }
131
- }
145
+ string path = Path . GetTempPath ( ) ;
146
+ PackBuilderResults results = repo . ObjectDatabase . Pack ( new PackBuilderOptions ( path ) ) ;
132
147
133
- foreach ( Tag tag in repo . Tags )
134
- {
135
- builder . Add ( tag . Target . Id ) ;
148
+ Assert . Equal ( repo . ObjectDatabase . Count ( ) , results . WrittenObjectsCount ) ;
149
+
150
+ Assert . True ( File . Exists ( Path . Combine ( path , "pack-" + results . PackHash + ".pack" ) ) ) ;
151
+ Assert . True ( File . Exists ( Path . Combine ( path , "pack-" + results . PackHash + ".idx" ) ) ) ;
136
152
}
137
153
}
138
154
139
- internal void AddingObjectsTestDelegate ( IRepository repo , PackBuilder builder )
155
+ [ Fact ]
156
+ public void TestEmptyPackFile ( )
140
157
{
141
- foreach ( Branch branch in repo . Branches )
158
+ using ( Repository repo = new Repository ( SandboxPackBuilderTestRepo ( ) ) )
142
159
{
143
- foreach ( Commit commit in branch . Commits )
160
+ string path = Path . GetTempPath ( ) ;
161
+ PackBuilderResults results = repo . ObjectDatabase . Pack ( new PackBuilderOptions ( path ) , b =>
144
162
{
145
- builder . AddRecursively ( commit ) ;
146
- }
163
+
164
+ } ) ;
165
+
166
+ Assert . True ( File . Exists ( Path . Combine ( path , "pack-" + results . PackHash + ".pack" ) ) ) ;
167
+ Assert . True ( File . Exists ( Path . Combine ( path , "pack-" + results . PackHash + ".idx" ) ) ) ;
147
168
}
169
+ }
148
170
149
- foreach ( Tag tag in repo . Tags )
171
+ [ Fact ]
172
+ public void TestPackFileForEmptyRepository ( )
173
+ {
174
+ using ( Repository repo = new Repository ( InitNewRepository ( ) ) )
150
175
{
151
- builder . Add ( tag . Target ) ;
176
+ string path = Path . GetTempPath ( ) ;
177
+ PackBuilderResults results = repo . ObjectDatabase . Pack ( new PackBuilderOptions ( path ) ) ;
178
+
179
+ Assert . True ( File . Exists ( Path . Combine ( path , "pack-" + results . PackHash + ".pack" ) ) ) ;
180
+ Assert . True ( File . Exists ( Path . Combine ( path , "pack-" + results . PackHash + ".idx" ) ) ) ;
152
181
}
153
182
}
154
183
@@ -250,5 +279,43 @@ public void ExceptionIfAddRecursivelyNullObjectID()
250
279
} ) ;
251
280
}
252
281
}
282
+
283
+ internal void TestBody ( Action < IRepository , PackBuilderOptions > fullPackingAction )
284
+ {
285
+ // read a repo, pack with the provided action, write the pack file in a mirror repo, read new repo, compare
286
+
287
+ string orgRepoPath = SandboxPackBuilderTestRepo ( ) ;
288
+ string mrrRepoPath = SandboxPackBuilderTestRepo ( ) ;
289
+ string mrrRepoPackDirPath = Path . Combine ( mrrRepoPath + "/.git/objects" ) ;
290
+
291
+ DirectoryHelper . DeleteDirectory ( mrrRepoPackDirPath ) ;
292
+ Directory . CreateDirectory ( mrrRepoPackDirPath + "/pack" ) ;
293
+
294
+ PackBuilderOptions packBuilderOptions = new PackBuilderOptions ( mrrRepoPackDirPath + "/pack" ) ;
295
+
296
+ using ( Repository orgRepo = new Repository ( orgRepoPath ) )
297
+ {
298
+ fullPackingAction ( orgRepo , packBuilderOptions ) ;
299
+
300
+ // loading the mirror repo from the written pack file and make sure it's identical to the original.
301
+ using ( Repository mrrRepo = new Repository ( mrrRepoPath ) )
302
+ {
303
+ AssertIfNotIdenticalRepositories ( orgRepo , mrrRepo ) ;
304
+ }
305
+ }
306
+ }
307
+
308
+ internal void AssertIfNotIdenticalRepositories ( IRepository repo1 , IRepository repo2 )
309
+ {
310
+ // make sure the objects of the original repo are the same as the ones in the mirror repo
311
+ // doing that by making sure the count is the same, and the set difference is empty
312
+ Assert . True ( repo1 . ObjectDatabase . Count ( ) == repo2 . ObjectDatabase . Count ( )
313
+ && ! repo2 . ObjectDatabase . Except ( repo1 . ObjectDatabase ) . Any ( ) ) ;
314
+
315
+ Assert . Equal ( repo1 . Commits . Count ( ) , repo2 . Commits . Count ( ) ) ;
316
+ Assert . Equal ( repo1 . Branches . Count ( ) , repo2 . Branches . Count ( ) ) ;
317
+ Assert . Equal ( repo1 . Refs . Count ( ) , repo2 . Refs . Count ( ) ) ;
318
+ Assert . Equal ( repo1 . Tags . Count ( ) , repo2 . Tags . Count ( ) ) ;
319
+ }
253
320
}
254
321
}
0 commit comments