forked from tangled.org/core
Monorepo for Tangled — https://tangled.org
at master 1.2 kB view raw
1package xrpc 2 3import ( 4 "net/http" 5 "strconv" 6 7 "tangled.org/core/knotserver/git" 8 "tangled.org/core/types" 9 xrpcerr "tangled.org/core/xrpc/errors" 10) 11 12func (x *Xrpc) RepoBranches(w http.ResponseWriter, r *http.Request) { 13 repo := r.URL.Query().Get("repo") 14 repoPath, err := x.parseRepoParam(repo) 15 if err != nil { 16 writeError(w, err.(xrpcerr.XrpcError), http.StatusBadRequest) 17 return 18 } 19 20 cursor := r.URL.Query().Get("cursor") 21 22 // limit := 50 // default 23 // if limitStr := r.URL.Query().Get("limit"); limitStr != "" { 24 // if l, err := strconv.Atoi(limitStr); err == nil && l > 0 && l <= 100 { 25 // limit = l 26 // } 27 // } 28 29 limit := 500 30 31 gr, err := git.PlainOpen(repoPath) 32 if err != nil { 33 writeError(w, xrpcerr.RepoNotFoundError, http.StatusNoContent) 34 return 35 } 36 37 branches, _ := gr.Branches() 38 39 offset := 0 40 if cursor != "" { 41 if o, err := strconv.Atoi(cursor); err == nil && o >= 0 && o < len(branches) { 42 offset = o 43 } 44 } 45 46 end := min(offset+limit, len(branches)) 47 48 paginatedBranches := branches[offset:end] 49 50 // Create response using existing types.RepoBranchesResponse 51 response := types.RepoBranchesResponse{ 52 Branches: paginatedBranches, 53 } 54 55 writeJson(w, response) 56}