this repo has no description
1package xrpc
2
3import (
4 "encoding/json"
5 "net/http"
6 "strconv"
7
8 "tangled.sh/tangled.sh/core/knotserver/git"
9 "tangled.sh/tangled.sh/core/types"
10 xrpcerr "tangled.sh/tangled.sh/core/xrpc/errors"
11)
12
13func (x *Xrpc) RepoBranches(w http.ResponseWriter, r *http.Request) {
14 repo := r.URL.Query().Get("repo")
15 repoPath, err := x.parseRepoParam(repo)
16 if err != nil {
17 writeError(w, err.(xrpcerr.XrpcError), http.StatusBadRequest)
18 return
19 }
20
21 cursor := r.URL.Query().Get("cursor")
22
23 // limit := 50 // default
24 // if limitStr := r.URL.Query().Get("limit"); limitStr != "" {
25 // if l, err := strconv.Atoi(limitStr); err == nil && l > 0 && l <= 100 {
26 // limit = l
27 // }
28 // }
29
30 limit := 500
31
32 gr, err := git.PlainOpen(repoPath)
33 if err != nil {
34 writeError(w, xrpcerr.RepoNotFoundError, http.StatusNoContent)
35 return
36 }
37
38 branches, _ := gr.Branches()
39
40 offset := 0
41 if cursor != "" {
42 if o, err := strconv.Atoi(cursor); err == nil && o >= 0 && o < len(branches) {
43 offset = o
44 }
45 }
46
47 end := min(offset+limit, len(branches))
48
49 paginatedBranches := branches[offset:end]
50
51 // Create response using existing types.RepoBranchesResponse
52 response := types.RepoBranchesResponse{
53 Branches: paginatedBranches,
54 }
55
56 // Write JSON response directly
57 w.Header().Set("Content-Type", "application/json")
58 if err := json.NewEncoder(w).Encode(response); err != nil {
59 x.Logger.Error("failed to encode response", "error", err)
60 writeError(w, xrpcerr.NewXrpcError(
61 xrpcerr.WithTag("InternalServerError"),
62 xrpcerr.WithMessage("failed to encode response"),
63 ), http.StatusInternalServerError)
64 return
65 }
66}