+1
-1
knotserver/config/config.go
+1
-1
knotserver/config/config.go
+5
-12
knotserver/git/git.go
+5
-12
knotserver/git/git.go
···
225
225
return branches, nil
226
226
}
227
227
228
-
func (g *GitRepo) FindMainBranch(branches []string) (string, error) {
229
-
branches = append(branches, []string{
230
-
"main",
231
-
"master",
232
-
"trunk",
233
-
}...)
234
-
for _, b := range branches {
235
-
_, err := g.r.ResolveRevision(plumbing.Revision(b))
236
-
if err == nil {
237
-
return b, nil
238
-
}
228
+
func (g *GitRepo) FindMainBranch() (string, error) {
229
+
ref, err := g.r.Head()
230
+
if err != nil {
231
+
return "", fmt.Errorf("unable to find main branch: %w", err)
239
232
}
240
-
return "", fmt.Errorf("unable to find main branch")
233
+
return string(ref.Name()), err
241
234
}
242
235
243
236
// WriteTar writes itself from a tree into a binary tar file format.
+11
-2
knotserver/git/repo.go
+11
-2
knotserver/git/repo.go
···
8
8
9
9
gogit "github.com/go-git/go-git/v5"
10
10
"github.com/go-git/go-git/v5/config"
11
+
"github.com/go-git/go-git/v5/plumbing"
11
12
)
12
13
13
-
func InitBare(path string) error {
14
+
func InitBare(path, defaultBranch string) error {
14
15
parent := filepath.Dir(path)
15
16
16
17
if err := os.MkdirAll(parent, 0755); errors.Is(err, os.ErrExist) {
···
22
23
return err
23
24
}
24
25
26
+
// set up default branch
25
27
err = repository.CreateBranch(&config.Branch{
26
-
Name: "main",
28
+
Name: defaultBranch,
27
29
})
28
30
if err != nil {
29
31
return fmt.Errorf("creating branch: %w", err)
32
+
}
33
+
34
+
defaultReference := plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", defaultBranch))
35
+
36
+
ref := plumbing.NewSymbolicReference(plumbing.HEAD, defaultReference)
37
+
if err = repository.Storer.SetReference(ref); err != nil {
38
+
return fmt.Errorf("creating symbolic reference: %w", err)
30
39
}
31
40
32
41
return nil
+12
-4
knotserver/routes.go
+12
-4
knotserver/routes.go
···
9
9
"errors"
10
10
"fmt"
11
11
"html/template"
12
+
"log"
12
13
"net/http"
13
14
"path/filepath"
14
15
"strconv"
···
37
38
38
39
gr, err := git.Open(path, ref)
39
40
if err != nil {
41
+
log.Println(err)
40
42
if errors.Is(err, plumbing.ErrReferenceNotFound) {
41
43
resp := types.RepoIndexResponse{
42
44
IsEmpty: true,
···
135
137
}
136
138
137
139
if ref == "" {
138
-
mainBranch, err := gr.FindMainBranch(h.c.Repo.MainBranch)
140
+
mainBranch, err := gr.FindMainBranch()
139
141
if err != nil {
140
142
writeError(w, err.Error(), http.StatusInternalServerError)
141
143
l.Error("finding main branch", "error", err.Error())
···
493
495
l := h.l.With("handler", "NewRepo")
494
496
495
497
data := struct {
496
-
Did string `json:"did"`
497
-
Name string `json:"name"`
498
+
Did string `json:"did"`
499
+
Name string `json:"name"`
500
+
DefaultBranch string `json:"default_branch,omitempty"`
498
501
}{}
499
502
500
503
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
···
502
505
return
503
506
}
504
507
508
+
if data.DefaultBranch == "" {
509
+
data.DefaultBranch = "main"
510
+
}
511
+
505
512
did := data.Did
506
513
name := data.Name
514
+
defaultBranch := data.DefaultBranch
507
515
508
516
relativeRepoPath := filepath.Join(did, name)
509
517
repoPath, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, relativeRepoPath)
510
-
err := git.InitBare(repoPath)
518
+
err := git.InitBare(repoPath, defaultBranch)
511
519
if err != nil {
512
520
l.Error("initializing bare repo", "error", err.Error())
513
521
if errors.Is(err, gogit.ErrRepositoryAlreadyExists) {