From f11d9cdfe276c2819be5819877f8527438f865ed Mon Sep 17 00:00:00 2001 From: oppiliappan Date: Mon, 19 May 2025 23:09:00 +0100 Subject: [PATCH] knotserver: better support for empty repos - setting default-branch now works on repos that have empty default branches - RepoIndex now returns branch-list if available on empty repos --- knotserver/git/git.go | 3 --- knotserver/routes.go | 24 ++++++++++++++---------- types/repo.go | 8 ++++---- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/knotserver/git/git.go b/knotserver/git/git.go index 56b116ed..1fa77559 100644 --- a/knotserver/git/git.go +++ b/knotserver/git/git.go @@ -261,9 +261,6 @@ func (g *GitRepo) Branches() ([]types.Branch, error) { branches := []types.Branch{} defaultBranch, err := g.FindMainBranch() - if err != nil { - return nil, fmt.Errorf("getting default branch", "error", err.Error()) - } _ = bi.ForEach(func(ref *plumbing.Reference) error { b := types.Branch{} diff --git a/knotserver/routes.go b/knotserver/routes.go index e517ec32..8664a6bf 100644 --- a/knotserver/routes.go +++ b/knotserver/routes.go @@ -61,10 +61,20 @@ func (h *Handle) RepoIndex(w http.ResponseWriter, r *http.Request) { gr, err := git.Open(path, ref) if err != nil { + plain, err2 := git.PlainOpen(path) + if err2 != nil { + l.Error("opening repo", "error", err2.Error()) + notFound(w) + return + } + branches, _ := plain.Branches() + log.Println(err) + if errors.Is(err, plumbing.ErrReferenceNotFound) { resp := types.RepoIndexResponse{ - IsEmpty: true, + IsEmpty: true, + Branches: branches, } writeJSON(w, resp) return @@ -461,20 +471,14 @@ func (h *Handle) Tags(w http.ResponseWriter, r *http.Request) { func (h *Handle) Branches(w http.ResponseWriter, r *http.Request) { path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r)) - l := h.l.With("handler", "Branches") - gr, err := git.Open(path, "") + gr, err := git.PlainOpen(path) if err != nil { notFound(w) return } - branches, err := gr.Branches() - if err != nil { - l.Error("getting branches", "error", err.Error()) - writeError(w, err.Error(), http.StatusInternalServerError) - return - } + branches, _ := gr.Branches() resp := types.RepoBranchesResponse{ Branches: branches, @@ -1143,7 +1147,7 @@ func (h *Handle) SetDefaultBranch(w http.ResponseWriter, r *http.Request) { return } - gr, err := git.Open(path, "") + gr, err := git.PlainOpen(path) if err != nil { notFound(w) return diff --git a/types/repo.go b/types/repo.go index daa3d13a..ad0ebfd5 100644 --- a/types/repo.go +++ b/types/repo.go @@ -49,9 +49,9 @@ type RepoTreeResponse struct { } type TagReference struct { - Reference `json:"ref,omitempty"` - Tag *object.Tag `json:"tag,omitempty"` - Message string `json:"message,omitempty"` + Reference + Tag *object.Tag `json:"tag,omitempty"` + Message string `json:"message,omitempty"` } type Reference struct { @@ -74,7 +74,7 @@ type RepoBranchesResponse struct { } type RepoBranchResponse struct { - Branch Branch `json:"branch,omitempty"` + Branch Branch } type RepoDefaultBranchResponse struct { -- 2.51.2