forked from tangled.org/core
this repo has no description

set default branch from KNOT_REPO_MAIN_BRANCH

Changed files
+29 -19
knotserver
+1 -1
knotserver/config/config.go
···
type Repo struct {
ScanPath string `env:"SCAN_PATH, default=/home/git"`
Readme []string `env:"README"`
-
MainBranch []string `env:"MAIN_BRANCH"`
}
type Server struct {
···
type Repo struct {
ScanPath string `env:"SCAN_PATH, default=/home/git"`
Readme []string `env:"README"`
+
MainBranch string `env:"MAIN_BRANCH, default=main"`
}
type Server struct {
+5 -12
knotserver/git/git.go
···
return branches, nil
}
-
func (g *GitRepo) FindMainBranch(branches []string) (string, error) {
-
branches = append(branches, []string{
-
"main",
-
"master",
-
"trunk",
-
}...)
-
for _, b := range branches {
-
_, err := g.r.ResolveRevision(plumbing.Revision(b))
-
if err == nil {
-
return b, nil
-
}
}
-
return "", fmt.Errorf("unable to find main branch")
}
// WriteTar writes itself from a tree into a binary tar file format.
···
return branches, nil
}
+
func (g *GitRepo) FindMainBranch() (string, error) {
+
ref, err := g.r.Head()
+
if err != nil {
+
return "", fmt.Errorf("unable to find main branch: %w", err)
}
+
return string(ref.Name()), err
}
// WriteTar writes itself from a tree into a binary tar file format.
+11 -2
knotserver/git/repo.go
···
gogit "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
)
-
func InitBare(path string) error {
parent := filepath.Dir(path)
if err := os.MkdirAll(parent, 0755); errors.Is(err, os.ErrExist) {
···
return err
}
err = repository.CreateBranch(&config.Branch{
-
Name: "main",
})
if err != nil {
return fmt.Errorf("creating branch: %w", err)
}
return nil
···
gogit "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
+
"github.com/go-git/go-git/v5/plumbing"
)
+
func InitBare(path, defaultBranch string) error {
parent := filepath.Dir(path)
if err := os.MkdirAll(parent, 0755); errors.Is(err, os.ErrExist) {
···
return err
}
+
// set up default branch
err = repository.CreateBranch(&config.Branch{
+
Name: defaultBranch,
})
if err != nil {
return fmt.Errorf("creating branch: %w", err)
+
}
+
+
defaultReference := plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", defaultBranch))
+
+
ref := plumbing.NewSymbolicReference(plumbing.HEAD, defaultReference)
+
if err = repository.Storer.SetReference(ref); err != nil {
+
return fmt.Errorf("creating symbolic reference: %w", err)
}
return nil
+12 -4
knotserver/routes.go
···
"errors"
"fmt"
"html/template"
"net/http"
"path/filepath"
"strconv"
···
gr, err := git.Open(path, ref)
if err != nil {
if errors.Is(err, plumbing.ErrReferenceNotFound) {
resp := types.RepoIndexResponse{
IsEmpty: true,
···
}
if ref == "" {
-
mainBranch, err := gr.FindMainBranch(h.c.Repo.MainBranch)
if err != nil {
writeError(w, err.Error(), http.StatusInternalServerError)
l.Error("finding main branch", "error", err.Error())
···
l := h.l.With("handler", "NewRepo")
data := struct {
-
Did string `json:"did"`
-
Name string `json:"name"`
}{}
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
···
return
}
did := data.Did
name := data.Name
relativeRepoPath := filepath.Join(did, name)
repoPath, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, relativeRepoPath)
-
err := git.InitBare(repoPath)
if err != nil {
l.Error("initializing bare repo", "error", err.Error())
if errors.Is(err, gogit.ErrRepositoryAlreadyExists) {
···
"errors"
"fmt"
"html/template"
+
"log"
"net/http"
"path/filepath"
"strconv"
···
gr, err := git.Open(path, ref)
if err != nil {
+
log.Println(err)
if errors.Is(err, plumbing.ErrReferenceNotFound) {
resp := types.RepoIndexResponse{
IsEmpty: true,
···
}
if ref == "" {
+
mainBranch, err := gr.FindMainBranch()
if err != nil {
writeError(w, err.Error(), http.StatusInternalServerError)
l.Error("finding main branch", "error", err.Error())
···
l := h.l.With("handler", "NewRepo")
data := struct {
+
Did string `json:"did"`
+
Name string `json:"name"`
+
DefaultBranch string `json:"default_branch,omitempty"`
}{}
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
···
return
}
+
if data.DefaultBranch == "" {
+
data.DefaultBranch = "main"
+
}
+
did := data.Did
name := data.Name
+
defaultBranch := data.DefaultBranch
relativeRepoPath := filepath.Join(did, name)
repoPath, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, relativeRepoPath)
+
err := git.InitBare(repoPath, defaultBranch)
if err != nil {
l.Error("initializing bare repo", "error", err.Error())
if errors.Is(err, gogit.ErrRepositoryAlreadyExists) {