From 639b3186a3c897547a85a8522730028eef8ce0e1 Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 3 Mar 2025 12:57:25 +0000 Subject: [PATCH] set default branch from KNOT_REPO_MAIN_BRANCH --- knotserver/config/config.go | 2 +- knotserver/git/git.go | 17 +++++------------ knotserver/git/repo.go | 13 +++++++++++-- knotserver/routes.go | 16 ++++++++++++---- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/knotserver/config/config.go b/knotserver/config/config.go index 158587b..8998dda 100644 --- a/knotserver/config/config.go +++ b/knotserver/config/config.go @@ -9,7 +9,7 @@ import ( type Repo struct { ScanPath string `env:"SCAN_PATH, default=/home/git"` Readme []string `env:"README"` - MainBranch []string `env:"MAIN_BRANCH"` + MainBranch string `env:"MAIN_BRANCH, default=main"` } type Server struct { diff --git a/knotserver/git/git.go b/knotserver/git/git.go index e1579d8..bfa9586 100644 --- a/knotserver/git/git.go +++ b/knotserver/git/git.go @@ -225,19 +225,12 @@ func (g *GitRepo) Branches() ([]*plumbing.Reference, error) { 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 - } +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 "", fmt.Errorf("unable to find main branch") + return string(ref.Name()), err } // WriteTar writes itself from a tree into a binary tar file format. diff --git a/knotserver/git/repo.go b/knotserver/git/repo.go index 8fbb7c4..aa5ef17 100644 --- a/knotserver/git/repo.go +++ b/knotserver/git/repo.go @@ -8,9 +8,10 @@ import ( 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 string) error { +func InitBare(path, defaultBranch string) error { parent := filepath.Dir(path) if err := os.MkdirAll(parent, 0755); errors.Is(err, os.ErrExist) { @@ -22,12 +23,20 @@ func InitBare(path string) error { return err } + // set up default branch err = repository.CreateBranch(&config.Branch{ - Name: "main", + 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 } diff --git a/knotserver/routes.go b/knotserver/routes.go index fed96bc..cf2f8ef 100644 --- a/knotserver/routes.go +++ b/knotserver/routes.go @@ -9,6 +9,7 @@ import ( "errors" "fmt" "html/template" + "log" "net/http" "path/filepath" "strconv" @@ -37,6 +38,7 @@ func (h *Handle) RepoIndex(w http.ResponseWriter, r *http.Request) { gr, err := git.Open(path, ref) if err != nil { + log.Println(err) if errors.Is(err, plumbing.ErrReferenceNotFound) { resp := types.RepoIndexResponse{ IsEmpty: true, @@ -135,7 +137,7 @@ func (h *Handle) RepoIndex(w http.ResponseWriter, r *http.Request) { } if ref == "" { - mainBranch, err := gr.FindMainBranch(h.c.Repo.MainBranch) + mainBranch, err := gr.FindMainBranch() if err != nil { writeError(w, err.Error(), http.StatusInternalServerError) l.Error("finding main branch", "error", err.Error()) @@ -493,8 +495,9 @@ func (h *Handle) NewRepo(w http.ResponseWriter, r *http.Request) { l := h.l.With("handler", "NewRepo") data := struct { - Did string `json:"did"` - Name string `json:"name"` + Did string `json:"did"` + Name string `json:"name"` + DefaultBranch string `json:"default_branch,omitempty"` }{} if err := json.NewDecoder(r.Body).Decode(&data); err != nil { @@ -502,12 +505,17 @@ func (h *Handle) NewRepo(w http.ResponseWriter, r *http.Request) { 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) + err := git.InitBare(repoPath, defaultBranch) if err != nil { l.Error("initializing bare repo", "error", err.Error()) if errors.Is(err, gogit.ErrRepositoryAlreadyExists) { -- 2.51.2