A vibe coded tangled fork which supports pijul.
1package xrpc
2
3import (
4 "net/http"
5 "strconv"
6
7 "github.com/go-git/go-git/v5/plumbing"
8 "github.com/go-git/go-git/v5/plumbing/object"
9 "tangled.org/core/knotserver/vcs"
10 "tangled.org/core/types"
11 xrpcerr "tangled.org/core/xrpc/errors"
12)
13
14func (x *Xrpc) RepoBranches(w http.ResponseWriter, r *http.Request) {
15 repo := r.URL.Query().Get("repo")
16 repoPath, err := x.parseRepoParam(repo)
17 if err != nil {
18 writeError(w, err.(xrpcerr.XrpcError), http.StatusBadRequest)
19 return
20 }
21
22 // default
23 limit := 50
24 offset := 0
25
26 if l, err := strconv.Atoi(r.URL.Query().Get("limit")); err == nil && l > 0 && l <= 100 {
27 limit = l
28 }
29
30 if o, err := strconv.Atoi(r.URL.Query().Get("cursor")); err == nil && o > 0 {
31 offset = o
32 }
33
34 rv, err := vcs.PlainOpen(repoPath)
35 if err != nil {
36 writeError(w, xrpcerr.RepoNotFoundError, http.StatusNoContent)
37 return
38 }
39
40 branchInfos, err := rv.Branches(&vcs.PaginationOpts{
41 Limit: limit,
42 Offset: offset,
43 })
44 if err != nil {
45 writeError(w, xrpcerr.GenericError(err), http.StatusInternalServerError)
46 return
47 }
48
49 branches := make([]types.Branch, 0, len(branchInfos))
50 for _, bi := range branchInfos {
51 b := types.Branch{
52 Reference: types.Reference{
53 Name: bi.Name,
54 Hash: bi.Hash,
55 },
56 IsDefault: bi.IsDefault,
57 }
58 if bi.LatestEntry != nil {
59 b.Commit = &object.Commit{
60 Hash: plumbing.NewHash(bi.LatestEntry.Hash),
61 Author: object.Signature{
62 Name: bi.LatestEntry.Author.Name,
63 Email: bi.LatestEntry.Author.Email,
64 When: bi.LatestEntry.Author.When,
65 },
66 Committer: object.Signature{
67 Name: bi.LatestEntry.Committer.Name,
68 Email: bi.LatestEntry.Committer.Email,
69 When: bi.LatestEntry.Committer.When,
70 },
71 Message: bi.LatestEntry.Message,
72 }
73 }
74 branches = append(branches, b)
75 }
76
77 writeJson(w, types.RepoBranchesResponse{Branches: branches})
78}