@@ -2,6 +2,7 @@ package bundles
2
2
3
3
import (
4
4
"bufio"
5
+ "context"
5
6
"encoding/json"
6
7
"fmt"
7
8
"os"
@@ -12,6 +13,7 @@ import (
12
13
13
14
"github.com/github/git-bundle-server/internal/core"
14
15
"github.com/github/git-bundle-server/internal/git"
16
+ "github.com/github/git-bundle-server/internal/log"
15
17
)
16
18
17
19
type BundleHeader struct {
@@ -41,7 +43,27 @@ func addBundleToList(bundle Bundle, list *BundleList) {
41
43
list .Bundles [bundle .CreationToken ] = bundle
42
44
}
43
45
44
- func CreateInitialBundle (repo * core.Repository ) Bundle {
46
+ type BundleProvider interface {
47
+ CreateInitialBundle (ctx context.Context , repo * core.Repository ) Bundle
48
+ CreateIncrementalBundle (ctx context.Context , repo * core.Repository , list * BundleList ) (* Bundle , error )
49
+
50
+ CreateSingletonList (ctx context.Context , bundle Bundle ) * BundleList
51
+ WriteBundleList (ctx context.Context , list * BundleList , repo * core.Repository ) error
52
+ GetBundleList (ctx context.Context , repo * core.Repository ) (* BundleList , error )
53
+ CollapseList (ctx context.Context , repo * core.Repository , list * BundleList ) error
54
+ }
55
+
56
+ type bundleProvider struct {
57
+ logger log.TraceLogger
58
+ }
59
+
60
+ func NewBundleProvider (logger log.TraceLogger ) BundleProvider {
61
+ return & bundleProvider {
62
+ logger : logger ,
63
+ }
64
+ }
65
+
66
+ func (b * bundleProvider ) CreateInitialBundle (ctx context.Context , repo * core.Repository ) Bundle {
45
67
timestamp := time .Now ().UTC ().Unix ()
46
68
bundleName := "bundle-" + fmt .Sprint (timestamp ) + ".bundle"
47
69
bundleFile := repo .WebDir + "/" + bundleName
@@ -54,10 +76,10 @@ func CreateInitialBundle(repo *core.Repository) Bundle {
54
76
return bundle
55
77
}
56
78
57
- func CreateDistinctBundle (repo * core.Repository , list * BundleList ) Bundle {
79
+ func ( b * bundleProvider ) createDistinctBundle (repo * core.Repository , list * BundleList ) Bundle {
58
80
timestamp := time .Now ().UTC ().Unix ()
59
81
60
- keys := GetSortedCreationTokens (list )
82
+ keys := b . getSortedCreationTokens (list )
61
83
62
84
maxTimestamp := keys [len (keys )- 1 ]
63
85
if timestamp <= maxTimestamp {
@@ -75,7 +97,7 @@ func CreateDistinctBundle(repo *core.Repository, list *BundleList) Bundle {
75
97
return bundle
76
98
}
77
99
78
- func CreateSingletonList (bundle Bundle ) * BundleList {
100
+ func ( b * bundleProvider ) CreateSingletonList (ctx context. Context , bundle Bundle ) * BundleList {
79
101
list := BundleList {1 , "all" , make (map [int64 ]Bundle )}
80
102
81
103
addBundleToList (bundle , & list )
@@ -84,7 +106,7 @@ func CreateSingletonList(bundle Bundle) *BundleList {
84
106
}
85
107
86
108
// Given a BundleList
87
- func WriteBundleList (list * BundleList , repo * core.Repository ) error {
109
+ func ( b * bundleProvider ) WriteBundleList (ctx context. Context , list * BundleList , repo * core.Repository ) error {
88
110
listFile := repo .WebDir + "/bundle-list"
89
111
jsonFile := repo .RepoDir + "/bundle-list.json"
90
112
@@ -100,7 +122,7 @@ func WriteBundleList(list *BundleList, repo *core.Repository) error {
100
122
out , "[bundle]\n \t version = %d\n \t mode = %s\n \n " ,
101
123
list .Version , list .Mode )
102
124
103
- keys := GetSortedCreationTokens (list )
125
+ keys := b . getSortedCreationTokens (list )
104
126
105
127
for _ , token := range keys {
106
128
bundle := list .Bundles [token ]
@@ -145,7 +167,7 @@ func WriteBundleList(list *BundleList, repo *core.Repository) error {
145
167
return os .Rename (listFile + ".lock" , listFile )
146
168
}
147
169
148
- func GetBundleList (repo * core.Repository ) (* BundleList , error ) {
170
+ func ( b * bundleProvider ) GetBundleList (ctx context. Context , repo * core.Repository ) (* BundleList , error ) {
149
171
jsonFile := repo .RepoDir + "/bundle-list.json"
150
172
151
173
reader , err := os .Open (jsonFile )
@@ -162,7 +184,7 @@ func GetBundleList(repo *core.Repository) (*BundleList, error) {
162
184
return & list , nil
163
185
}
164
186
165
- func GetBundleHeader (bundle Bundle ) (* BundleHeader , error ) {
187
+ func ( b * bundleProvider ) getBundleHeader (bundle Bundle ) (* BundleHeader , error ) {
166
188
file , err := os .Open (bundle .Filename )
167
189
if err != nil {
168
190
return nil , fmt .Errorf ("failed to open bundle file: %w" , err )
@@ -232,11 +254,11 @@ func GetBundleHeader(bundle Bundle) (*BundleHeader, error) {
232
254
return & header , nil
233
255
}
234
256
235
- func GetAllPrereqsForIncrementalBundle (list * BundleList ) ([]string , error ) {
257
+ func ( b * bundleProvider ) getAllPrereqsForIncrementalBundle (list * BundleList ) ([]string , error ) {
236
258
prereqs := []string {}
237
259
238
260
for _ , bundle := range list .Bundles {
239
- header , err := GetBundleHeader (bundle )
261
+ header , err := b . getBundleHeader (bundle )
240
262
if err != nil {
241
263
return nil , fmt .Errorf ("failed to parse bundle file %s: %w" , bundle .Filename , err )
242
264
}
@@ -249,10 +271,10 @@ func GetAllPrereqsForIncrementalBundle(list *BundleList) ([]string, error) {
249
271
return prereqs , nil
250
272
}
251
273
252
- func CreateIncrementalBundle (repo * core.Repository , list * BundleList ) (* Bundle , error ) {
253
- bundle := CreateDistinctBundle (repo , list )
274
+ func ( b * bundleProvider ) CreateIncrementalBundle (ctx context. Context , repo * core.Repository , list * BundleList ) (* Bundle , error ) {
275
+ bundle := b . createDistinctBundle (repo , list )
254
276
255
- lines , err := GetAllPrereqsForIncrementalBundle (list )
277
+ lines , err := b . getAllPrereqsForIncrementalBundle (list )
256
278
if err != nil {
257
279
return nil , err
258
280
}
@@ -269,14 +291,14 @@ func CreateIncrementalBundle(repo *core.Repository, list *BundleList) (*Bundle,
269
291
return & bundle , nil
270
292
}
271
293
272
- func CollapseList (repo * core.Repository , list * BundleList ) error {
294
+ func ( b * bundleProvider ) CollapseList (ctx context. Context , repo * core.Repository , list * BundleList ) error {
273
295
maxBundles := 5
274
296
275
297
if len (list .Bundles ) <= maxBundles {
276
298
return nil
277
299
}
278
300
279
- keys := GetSortedCreationTokens (list )
301
+ keys := b . getSortedCreationTokens (list )
280
302
281
303
refs := make (map [string ]string )
282
304
@@ -289,7 +311,7 @@ func CollapseList(repo *core.Repository, list *BundleList) error {
289
311
maxTimestamp = bundle .CreationToken
290
312
}
291
313
292
- header , err := GetBundleHeader (bundle )
314
+ header , err := b . getBundleHeader (bundle )
293
315
if err != nil {
294
316
return fmt .Errorf ("failed to parse bundle file %s: %w" , bundle .Filename , err )
295
317
}
@@ -328,7 +350,7 @@ func CollapseList(repo *core.Repository, list *BundleList) error {
328
350
return nil
329
351
}
330
352
331
- func GetSortedCreationTokens (list * BundleList ) []int64 {
353
+ func ( b * bundleProvider ) getSortedCreationTokens (list * BundleList ) []int64 {
332
354
keys := make ([]int64 , 0 , len (list .Bundles ))
333
355
for timestamp := range list .Bundles {
334
356
keys = append (keys , timestamp )
0 commit comments