Skip to content

Commit 5d94036

Browse files
committed
bundles: refactor into BundleProvider interface
Refactor 'bundles.go', changing its standalone functions into methods of a 'BundleProvider' interface. Like the 'RepoProvider' refactor before it, this makes the functions mockable for future testing and allows for easy use of the 'TraceLogger' into the bundle handling functions. Signed-off-by: Victoria Dye <[email protected]>
1 parent cadf8b0 commit 5d94036

File tree

4 files changed

+52
-24
lines changed

4 files changed

+52
-24
lines changed

cmd/git-bundle-server/init.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func (i *initCmd) Run(ctx context.Context, args []string) error {
4242
parser.Parse(ctx, args)
4343

4444
repoProvider := utils.GetDependency[core.RepositoryProvider](ctx, i.container)
45+
bundleProvider := utils.GetDependency[bundles.BundleProvider](ctx, i.container)
4546

4647
repo, err := repoProvider.CreateRepository(ctx, *route)
4748
if err != nil {
@@ -65,7 +66,7 @@ func (i *initCmd) Run(ctx context.Context, args []string) error {
6566
return i.logger.Errorf(ctx, "failed to fetch latest refs: %w", gitErr)
6667
}
6768

68-
bundle := bundles.CreateInitialBundle(repo)
69+
bundle := bundleProvider.CreateInitialBundle(ctx, repo)
6970
fmt.Printf("Constructing base bundle file at %s\n", bundle.Filename)
7071

7172
written, gitErr := git.CreateBundle(repo.RepoDir, bundle.Filename)
@@ -76,8 +77,8 @@ func (i *initCmd) Run(ctx context.Context, args []string) error {
7677
return i.logger.Errorf(ctx, "refused to write empty bundle. Is the repo empty?")
7778
}
7879

79-
list := bundles.CreateSingletonList(bundle)
80-
listErr := bundles.WriteBundleList(list, repo)
80+
list := bundleProvider.CreateSingletonList(ctx, bundle)
81+
listErr := bundleProvider.WriteBundleList(ctx, list, repo)
8182
if listErr != nil {
8283
return i.logger.Errorf(ctx, "failed to write bundle list: %w", listErr)
8384
}

cmd/git-bundle-server/update.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,20 @@ func (u *updateCmd) Run(ctx context.Context, args []string) error {
4040
parser.Parse(ctx, args)
4141

4242
repoProvider := utils.GetDependency[core.RepositoryProvider](ctx, u.container)
43+
bundleProvider := utils.GetDependency[bundles.BundleProvider](ctx, u.container)
4344

4445
repo, err := repoProvider.CreateRepository(ctx, *route)
4546
if err != nil {
4647
return u.logger.Error(ctx, err)
4748
}
4849

49-
list, err := bundles.GetBundleList(repo)
50+
list, err := bundleProvider.GetBundleList(ctx, repo)
5051
if err != nil {
5152
return u.logger.Errorf(ctx, "failed to load bundle list: %w", err)
5253
}
5354

5455
fmt.Printf("Creating new incremental bundle\n")
55-
bundle, err := bundles.CreateIncrementalBundle(repo, list)
56+
bundle, err := bundleProvider.CreateIncrementalBundle(ctx, repo, list)
5657
if err != nil {
5758
return u.logger.Error(ctx, err)
5859
}
@@ -65,13 +66,13 @@ func (u *updateCmd) Run(ctx context.Context, args []string) error {
6566
list.Bundles[bundle.CreationToken] = *bundle
6667

6768
fmt.Printf("Collapsing bundle list\n")
68-
err = bundles.CollapseList(repo, list)
69+
err = bundleProvider.CollapseList(ctx, repo, list)
6970
if err != nil {
7071
return u.logger.Error(ctx, err)
7172
}
7273

7374
fmt.Printf("Writing updated bundle list\n")
74-
listErr := bundles.WriteBundleList(list, repo)
75+
listErr := bundleProvider.WriteBundleList(ctx, list, repo)
7576
if listErr != nil {
7677
return u.logger.Errorf(ctx, "failed to write bundle list: %w", listErr)
7778
}

cmd/utils/container-helpers.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package utils
33
import (
44
"context"
55

6+
"github.com/github/git-bundle-server/internal/bundles"
67
"github.com/github/git-bundle-server/internal/common"
78
"github.com/github/git-bundle-server/internal/core"
89
"github.com/github/git-bundle-server/internal/daemon"
@@ -27,6 +28,9 @@ func BuildGitBundleServerContainer(logger log.TraceLogger) *DependencyContainer
2728
GetDependency[common.FileSystem](ctx, container),
2829
)
2930
})
31+
registerDependency(container, func(ctx context.Context) bundles.BundleProvider {
32+
return bundles.NewBundleProvider(logger)
33+
})
3034
registerDependency(container, func(ctx context.Context) daemon.DaemonProvider {
3135
t, err := daemon.NewDaemonProvider(
3236
logger,

internal/bundles/bundles.go

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package bundles
22

33
import (
44
"bufio"
5+
"context"
56
"encoding/json"
67
"fmt"
78
"os"
@@ -12,6 +13,7 @@ import (
1213

1314
"github.com/github/git-bundle-server/internal/core"
1415
"github.com/github/git-bundle-server/internal/git"
16+
"github.com/github/git-bundle-server/internal/log"
1517
)
1618

1719
type BundleHeader struct {
@@ -41,7 +43,27 @@ func addBundleToList(bundle Bundle, list *BundleList) {
4143
list.Bundles[bundle.CreationToken] = bundle
4244
}
4345

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 {
4567
timestamp := time.Now().UTC().Unix()
4668
bundleName := "bundle-" + fmt.Sprint(timestamp) + ".bundle"
4769
bundleFile := repo.WebDir + "/" + bundleName
@@ -54,10 +76,10 @@ func CreateInitialBundle(repo *core.Repository) Bundle {
5476
return bundle
5577
}
5678

57-
func CreateDistinctBundle(repo *core.Repository, list *BundleList) Bundle {
79+
func (b *bundleProvider) createDistinctBundle(repo *core.Repository, list *BundleList) Bundle {
5880
timestamp := time.Now().UTC().Unix()
5981

60-
keys := GetSortedCreationTokens(list)
82+
keys := b.getSortedCreationTokens(list)
6183

6284
maxTimestamp := keys[len(keys)-1]
6385
if timestamp <= maxTimestamp {
@@ -75,7 +97,7 @@ func CreateDistinctBundle(repo *core.Repository, list *BundleList) Bundle {
7597
return bundle
7698
}
7799

78-
func CreateSingletonList(bundle Bundle) *BundleList {
100+
func (b *bundleProvider) CreateSingletonList(ctx context.Context, bundle Bundle) *BundleList {
79101
list := BundleList{1, "all", make(map[int64]Bundle)}
80102

81103
addBundleToList(bundle, &list)
@@ -84,7 +106,7 @@ func CreateSingletonList(bundle Bundle) *BundleList {
84106
}
85107

86108
// 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 {
88110
listFile := repo.WebDir + "/bundle-list"
89111
jsonFile := repo.RepoDir + "/bundle-list.json"
90112

@@ -100,7 +122,7 @@ func WriteBundleList(list *BundleList, repo *core.Repository) error {
100122
out, "[bundle]\n\tversion = %d\n\tmode = %s\n\n",
101123
list.Version, list.Mode)
102124

103-
keys := GetSortedCreationTokens(list)
125+
keys := b.getSortedCreationTokens(list)
104126

105127
for _, token := range keys {
106128
bundle := list.Bundles[token]
@@ -145,7 +167,7 @@ func WriteBundleList(list *BundleList, repo *core.Repository) error {
145167
return os.Rename(listFile+".lock", listFile)
146168
}
147169

148-
func GetBundleList(repo *core.Repository) (*BundleList, error) {
170+
func (b *bundleProvider) GetBundleList(ctx context.Context, repo *core.Repository) (*BundleList, error) {
149171
jsonFile := repo.RepoDir + "/bundle-list.json"
150172

151173
reader, err := os.Open(jsonFile)
@@ -162,7 +184,7 @@ func GetBundleList(repo *core.Repository) (*BundleList, error) {
162184
return &list, nil
163185
}
164186

165-
func GetBundleHeader(bundle Bundle) (*BundleHeader, error) {
187+
func (b *bundleProvider) getBundleHeader(bundle Bundle) (*BundleHeader, error) {
166188
file, err := os.Open(bundle.Filename)
167189
if err != nil {
168190
return nil, fmt.Errorf("failed to open bundle file: %w", err)
@@ -232,11 +254,11 @@ func GetBundleHeader(bundle Bundle) (*BundleHeader, error) {
232254
return &header, nil
233255
}
234256

235-
func GetAllPrereqsForIncrementalBundle(list *BundleList) ([]string, error) {
257+
func (b *bundleProvider) getAllPrereqsForIncrementalBundle(list *BundleList) ([]string, error) {
236258
prereqs := []string{}
237259

238260
for _, bundle := range list.Bundles {
239-
header, err := GetBundleHeader(bundle)
261+
header, err := b.getBundleHeader(bundle)
240262
if err != nil {
241263
return nil, fmt.Errorf("failed to parse bundle file %s: %w", bundle.Filename, err)
242264
}
@@ -249,10 +271,10 @@ func GetAllPrereqsForIncrementalBundle(list *BundleList) ([]string, error) {
249271
return prereqs, nil
250272
}
251273

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)
254276

255-
lines, err := GetAllPrereqsForIncrementalBundle(list)
277+
lines, err := b.getAllPrereqsForIncrementalBundle(list)
256278
if err != nil {
257279
return nil, err
258280
}
@@ -269,14 +291,14 @@ func CreateIncrementalBundle(repo *core.Repository, list *BundleList) (*Bundle,
269291
return &bundle, nil
270292
}
271293

272-
func CollapseList(repo *core.Repository, list *BundleList) error {
294+
func (b *bundleProvider) CollapseList(ctx context.Context, repo *core.Repository, list *BundleList) error {
273295
maxBundles := 5
274296

275297
if len(list.Bundles) <= maxBundles {
276298
return nil
277299
}
278300

279-
keys := GetSortedCreationTokens(list)
301+
keys := b.getSortedCreationTokens(list)
280302

281303
refs := make(map[string]string)
282304

@@ -289,7 +311,7 @@ func CollapseList(repo *core.Repository, list *BundleList) error {
289311
maxTimestamp = bundle.CreationToken
290312
}
291313

292-
header, err := GetBundleHeader(bundle)
314+
header, err := b.getBundleHeader(bundle)
293315
if err != nil {
294316
return fmt.Errorf("failed to parse bundle file %s: %w", bundle.Filename, err)
295317
}
@@ -328,7 +350,7 @@ func CollapseList(repo *core.Repository, list *BundleList) error {
328350
return nil
329351
}
330352

331-
func GetSortedCreationTokens(list *BundleList) []int64 {
353+
func (b *bundleProvider) getSortedCreationTokens(list *BundleList) []int64 {
332354
keys := make([]int64, 0, len(list.Bundles))
333355
for timestamp := range list.Bundles {
334356
keys = append(keys, timestamp)

0 commit comments

Comments
 (0)