forked from tangled.org/core
Monorepo for Tangled

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

authored by oppi.li and committed by Tangled f11d9cdf c458235d

Changed files
+18 -17
knotserver
types
-3
knotserver/git/git.go
··· 261 261 branches := []types.Branch{} 262 262 263 263 defaultBranch, err := g.FindMainBranch() 264 - if err != nil { 265 - return nil, fmt.Errorf("getting default branch", "error", err.Error()) 266 - } 267 264 268 265 _ = bi.ForEach(func(ref *plumbing.Reference) error { 269 266 b := types.Branch{}
+14 -10
knotserver/routes.go
··· 61 61 62 62 gr, err := git.Open(path, ref) 63 63 if err != nil { 64 + plain, err2 := git.PlainOpen(path) 65 + if err2 != nil { 66 + l.Error("opening repo", "error", err2.Error()) 67 + notFound(w) 68 + return 69 + } 70 + branches, _ := plain.Branches() 71 + 64 72 log.Println(err) 73 + 65 74 if errors.Is(err, plumbing.ErrReferenceNotFound) { 66 75 resp := types.RepoIndexResponse{ 67 - IsEmpty: true, 76 + IsEmpty: true, 77 + Branches: branches, 68 78 } 69 79 writeJSON(w, resp) 70 80 return ··· 461 471 462 472 func (h *Handle) Branches(w http.ResponseWriter, r *http.Request) { 463 473 path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r)) 464 - l := h.l.With("handler", "Branches") 465 474 466 - gr, err := git.Open(path, "") 475 + gr, err := git.PlainOpen(path) 467 476 if err != nil { 468 477 notFound(w) 469 478 return 470 479 } 471 480 472 - branches, err := gr.Branches() 473 - if err != nil { 474 - l.Error("getting branches", "error", err.Error()) 475 - writeError(w, err.Error(), http.StatusInternalServerError) 476 - return 477 - } 481 + branches, _ := gr.Branches() 478 482 479 483 resp := types.RepoBranchesResponse{ 480 484 Branches: branches, ··· 1143 1147 return 1144 1148 } 1145 1149 1146 - gr, err := git.Open(path, "") 1150 + gr, err := git.PlainOpen(path) 1147 1151 if err != nil { 1148 1152 notFound(w) 1149 1153 return
+4 -4
types/repo.go
··· 49 49 } 50 50 51 51 type TagReference struct { 52 - Reference `json:"ref,omitempty"` 53 - Tag *object.Tag `json:"tag,omitempty"` 54 - Message string `json:"message,omitempty"` 52 + Reference 53 + Tag *object.Tag `json:"tag,omitempty"` 54 + Message string `json:"message,omitempty"` 55 55 } 56 56 57 57 type Reference struct { ··· 74 74 } 75 75 76 76 type RepoBranchResponse struct { 77 - Branch Branch `json:"branch,omitempty"` 77 + Branch Branch 78 78 } 79 79 80 80 type RepoDefaultBranchResponse struct {