package xrpc import ( "net/http" "strconv" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" "tangled.org/core/knotserver/vcs" "tangled.org/core/types" xrpcerr "tangled.org/core/xrpc/errors" ) func (x *Xrpc) RepoBranches(w http.ResponseWriter, r *http.Request) { repo := r.URL.Query().Get("repo") repoPath, err := x.parseRepoParam(repo) if err != nil { writeError(w, err.(xrpcerr.XrpcError), http.StatusBadRequest) return } // default limit := 50 offset := 0 if l, err := strconv.Atoi(r.URL.Query().Get("limit")); err == nil && l > 0 && l <= 100 { limit = l } if o, err := strconv.Atoi(r.URL.Query().Get("cursor")); err == nil && o > 0 { offset = o } rv, err := vcs.PlainOpen(repoPath) if err != nil { writeError(w, xrpcerr.RepoNotFoundError, http.StatusNoContent) return } branchInfos, err := rv.Branches(&vcs.PaginationOpts{ Limit: limit, Offset: offset, }) if err != nil { writeError(w, xrpcerr.GenericError(err), http.StatusInternalServerError) return } branches := make([]types.Branch, 0, len(branchInfos)) for _, bi := range branchInfos { b := types.Branch{ Reference: types.Reference{ Name: bi.Name, Hash: bi.Hash, }, IsDefault: bi.IsDefault, } if bi.LatestEntry != nil { b.Commit = &object.Commit{ Hash: plumbing.NewHash(bi.LatestEntry.Hash), Author: object.Signature{ Name: bi.LatestEntry.Author.Name, Email: bi.LatestEntry.Author.Email, When: bi.LatestEntry.Author.When, }, Committer: object.Signature{ Name: bi.LatestEntry.Committer.Name, Email: bi.LatestEntry.Committer.Email, When: bi.LatestEntry.Committer.When, }, Message: bi.LatestEntry.Message, } } branches = append(branches, b) } writeJson(w, types.RepoBranchesResponse{Branches: branches}) }