Skip to content

Commit 8a0e1a1

Browse files
committed
core: create concept of routes file
The routes file stores the list of known routes. These correspond to directories in the repo root and web root. While here, update our use of core.Repository to be pointer-based.
1 parent 501f64f commit 8a0e1a1

File tree

5 files changed

+96
-23
lines changed

5 files changed

+96
-23
lines changed

cmd/git-bundle-server/init.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ func (Init) run(args []string) error {
2323
url := args[0]
2424
route := args[1]
2525

26-
repo := core.GetRepository(route)
26+
repo, err := core.CreateRepository(route)
27+
if err != nil {
28+
return err
29+
}
2730

2831
fmt.Printf("Cloning repository from %s\n", url)
2932
gitErr := git.GitCommand("clone", "--mirror", url, repo.RepoDir)
@@ -35,7 +38,7 @@ func (Init) run(args []string) error {
3538
bundle := bundles.CreateInitialBundle(repo)
3639
fmt.Printf("Constructing base bundle file at %s\n", bundle.Filename)
3740

38-
written, gitErr := git.CreateBundle(repo, bundle.Filename)
41+
written, gitErr := git.CreateBundle(repo.RepoDir, bundle.Filename)
3942
if gitErr != nil {
4043
return fmt.Errorf("failed to create bundle: %w", gitErr)
4144
}

cmd/git-bundle-server/update.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ func (Update) run(args []string) error {
2020
}
2121

2222
route := args[0]
23-
repo := core.GetRepository(route)
23+
repo, err := core.CreateRepository(route)
24+
if err != nil {
25+
return err
26+
}
2427

2528
list, err := bundles.GetBundleList(repo)
2629
if err != nil {

internal/bundles/bundles.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func addBundleToList(bundle Bundle, list *BundleList) {
4040
list.Bundles[bundle.CreationToken] = bundle
4141
}
4242

43-
func CreateInitialBundle(repo core.Repository) Bundle {
43+
func CreateInitialBundle(repo *core.Repository) Bundle {
4444
timestamp := time.Now().UTC().Unix()
4545
bundleName := "bundle-" + fmt.Sprint(timestamp) + ".bundle"
4646
bundleFile := repo.WebDir + "/" + bundleName
@@ -53,7 +53,7 @@ func CreateInitialBundle(repo core.Repository) Bundle {
5353
return bundle
5454
}
5555

56-
func CreateDistinctBundle(repo core.Repository, list *BundleList) Bundle {
56+
func CreateDistinctBundle(repo *core.Repository, list *BundleList) Bundle {
5757
timestamp := time.Now().UTC().Unix()
5858

5959
keys := GetSortedCreationTokens(list)
@@ -83,7 +83,7 @@ func CreateSingletonList(bundle Bundle) *BundleList {
8383
}
8484

8585
// Given a BundleList
86-
func WriteBundleList(list *BundleList, repo core.Repository) error {
86+
func WriteBundleList(list *BundleList, repo *core.Repository) error {
8787
listFile := repo.WebDir + "/bundle-list"
8888
jsonFile := repo.RepoDir + "/bundle-list.json"
8989

@@ -144,7 +144,7 @@ func WriteBundleList(list *BundleList, repo core.Repository) error {
144144
return os.Rename(listFile+".lock", listFile)
145145
}
146146

147-
func GetBundleList(repo core.Repository) (*BundleList, error) {
147+
func GetBundleList(repo *core.Repository) (*BundleList, error) {
148148
jsonFile := repo.RepoDir + "/bundle-list.json"
149149

150150
reader, err := os.Open(jsonFile)
@@ -248,15 +248,15 @@ func GetAllPrereqsForIncrementalBundle(list *BundleList) ([]string, error) {
248248
return prereqs, nil
249249
}
250250

251-
func CreateIncrementalBundle(repo core.Repository, list *BundleList) (*Bundle, error) {
251+
func CreateIncrementalBundle(repo *core.Repository, list *BundleList) (*Bundle, error) {
252252
bundle := CreateDistinctBundle(repo, list)
253253

254254
lines, err := GetAllPrereqsForIncrementalBundle(list)
255255
if err != nil {
256256
return nil, err
257257
}
258258

259-
written, err := git.CreateIncrementalBundle(repo, bundle.Filename, lines)
259+
written, err := git.CreateIncrementalBundle(repo.RepoDir, bundle.Filename, lines)
260260
if err != nil {
261261
return nil, fmt.Errorf("failed to create incremental bundle: %w", err)
262262
}
@@ -268,7 +268,7 @@ func CreateIncrementalBundle(repo core.Repository, list *BundleList) (*Bundle, e
268268
return &bundle, nil
269269
}
270270

271-
func CollapseList(repo core.Repository, list *BundleList) error {
271+
func CollapseList(repo *core.Repository, list *BundleList) error {
272272
maxBundles := 5
273273

274274
if len(list.Bundles) <= maxBundles {
@@ -318,7 +318,7 @@ func CollapseList(repo core.Repository, list *BundleList) error {
318318
URI: fmt.Sprintf("./base-%d.bundle", maxTimestamp),
319319
}
320320

321-
err := git.CreateBundleFromRefs(repo, bundle.Filename, refs)
321+
err := git.CreateBundleFromRefs(repo.RepoDir, bundle.Filename, refs)
322322
if err != nil {
323323
return err
324324
}

internal/core/repo.go

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package core
22

33
import (
4+
"bufio"
5+
"fmt"
6+
"io"
47
"log"
58
"os"
69
)
@@ -11,18 +14,83 @@ type Repository struct {
1114
WebDir string
1215
}
1316

14-
func GetRepository(route string) Repository {
15-
repo := reporoot() + route
17+
func CreateRepository(route string) (*Repository, error) {
18+
repos, err := GetRepositories()
19+
if err != nil {
20+
return nil, fmt.Errorf("failed to parse routes file")
21+
}
22+
23+
repo, contains := repos[route]
24+
if contains {
25+
return &repo, nil
26+
}
27+
28+
repodir := reporoot() + route
1629
web := webroot() + route
1730

1831
mkdirErr := os.MkdirAll(web, os.ModePerm)
1932
if mkdirErr != nil {
2033
log.Fatal("failed to create web directory: ", mkdirErr)
2134
}
2235

23-
return Repository{
36+
repo = Repository{
2437
Route: route,
25-
RepoDir: repo,
38+
RepoDir: repodir,
2639
WebDir: web,
2740
}
41+
42+
repos[route] = repo
43+
44+
err = WriteRouteFile(repos)
45+
if err != nil {
46+
return nil, fmt.Errorf("warning: failed to write route file")
47+
}
48+
49+
return &repo, nil
50+
}
51+
52+
func WriteRouteFile(repos map[string]Repository) error {
53+
dir := bundleroot()
54+
routefile := dir + "/routes"
55+
56+
contents := ""
57+
58+
for routes := range repos {
59+
contents = contents + routes + "\n"
60+
}
61+
62+
return os.WriteFile(routefile, []byte(contents), 0600)
63+
}
64+
65+
func GetRepositories() (map[string]Repository, error) {
66+
repos := make(map[string]Repository)
67+
68+
dir := bundleroot()
69+
routefile := dir + "/routes"
70+
71+
file, err := os.OpenFile(routefile, os.O_RDONLY|os.O_CREATE, 0600)
72+
if err != nil {
73+
// Assume that the file doesn't exist?
74+
return repos, nil
75+
}
76+
77+
reader := bufio.NewReader(file)
78+
for {
79+
line, err := reader.ReadString('\n')
80+
if line == "" || line[0] == '\n' ||
81+
(err != nil && err != io.EOF) {
82+
break
83+
}
84+
85+
route := line[0 : len(line)-1]
86+
87+
repo := Repository{
88+
Route: route,
89+
RepoDir: reporoot() + route,
90+
WebDir: webroot() + route,
91+
}
92+
repos[route] = repo
93+
}
94+
95+
return repos, nil
2896
}

internal/git/git.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package git
33
import (
44
"bytes"
55
"fmt"
6-
"git-bundle-server/internal/core"
76
"os"
87
"os/exec"
98
"strings"
@@ -66,9 +65,9 @@ func GitCommandWithStdin(stdinLines []string, args ...string) error {
6665
return err
6766
}
6867

69-
func CreateBundle(repo core.Repository, filename string) (bool, error) {
68+
func CreateBundle(repoDir string, filename string) (bool, error) {
7069
err := GitCommand(
71-
"-C", repo.RepoDir, "bundle", "create",
70+
"-C", repoDir, "bundle", "create",
7271
filename, "--all")
7372
if err != nil {
7473
if strings.Contains(err.Error(), "Refusing to create empty bundle") {
@@ -80,11 +79,11 @@ func CreateBundle(repo core.Repository, filename string) (bool, error) {
8079
return true, nil
8180
}
8281

83-
func CreateBundleFromRefs(repo core.Repository, filename string, refs map[string]string) error {
82+
func CreateBundleFromRefs(repoDir string, filename string, refs map[string]string) error {
8483
refNames := []string{}
8584

8685
for ref, oid := range refs {
87-
err := GitCommand("-C", repo.RepoDir, "branch", "-f", ref, oid)
86+
err := GitCommand("-C", repoDir, "branch", "-f", ref, oid)
8887
if err != nil {
8988
return fmt.Errorf("failed to create ref %s: %w", ref, err)
9089
}
@@ -94,7 +93,7 @@ func CreateBundleFromRefs(repo core.Repository, filename string, refs map[string
9493

9594
err := GitCommandWithStdin(
9695
refNames,
97-
"-C", repo.RepoDir, "bundle", "create",
96+
"-C", repoDir, "bundle", "create",
9897
filename, "--stdin")
9998
if err != nil {
10099
return err
@@ -103,9 +102,9 @@ func CreateBundleFromRefs(repo core.Repository, filename string, refs map[string
103102
return nil
104103
}
105104

106-
func CreateIncrementalBundle(repo core.Repository, filename string, prereqs []string) (bool, error) {
105+
func CreateIncrementalBundle(repoDir string, filename string, prereqs []string) (bool, error) {
107106
err := GitCommandWithStdin(
108-
prereqs, "-C", repo.RepoDir, "bundle", "create",
107+
prereqs, "-C", repoDir, "bundle", "create",
109108
filename, "--stdin", "--all")
110109
if err != nil {
111110
if strings.Contains(err.Error(), "Refusing to create empty bundle") {

0 commit comments

Comments
 (0)