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) RepoLog(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 ref := r.URL.Query().Get("ref") 22 23 path := r.URL.Query().Get("path") 24 cursor := r.URL.Query().Get("cursor") 25 26 limit := 50 // default 27 if limitStr := r.URL.Query().Get("limit"); limitStr != "" { 28 if l, err := strconv.Atoi(limitStr); err == nil && l > 0 && l <= 100 { 29 limit = l 30 } 31 } 32 33 gr, err := git.Open(repoPath, ref) 34 if err != nil { 35 writeError(w, xrpcerr.NewXrpcError( 36 xrpcerr.WithTag("RefNotFound"), 37 xrpcerr.WithMessage("repository or ref not found"), 38 ), http.StatusNotFound) 39 return 40 } 41 42 offset := 0 43 if cursor != "" { 44 if o, err := strconv.Atoi(cursor); err == nil && o >= 0 { 45 offset = o 46 } 47 } 48 49 commits, err := gr.Commits(offset, limit) 50 if err != nil { 51 x.Logger.Error("fetching commits", "error", err.Error()) 52 writeError(w, xrpcerr.NewXrpcError( 53 xrpcerr.WithTag("PathNotFound"), 54 xrpcerr.WithMessage("failed to read commit log"), 55 ), http.StatusNotFound) 56 return 57 } 58 59 total, err := gr.TotalCommits() 60 if err != nil { 61 x.Logger.Error("fetching total commits", "error", err.Error()) 62 writeError(w, xrpcerr.NewXrpcError( 63 xrpcerr.WithTag("InternalServerError"), 64 xrpcerr.WithMessage("failed to fetch total commits"), 65 ), http.StatusNotFound) 66 return 67 } 68 69 // Create response using existing types.RepoLogResponse 70 response := types.RepoLogResponse{ 71 Commits: commits, 72 Ref: ref, 73 Page: (offset / limit) + 1, 74 PerPage: limit, 75 Total: total, 76 } 77 78 if path != "" { 79 response.Description = path 80 } 81 82 response.Log = true 83 84 // Write JSON response directly 85 w.Header().Set("Content-Type", "application/json") 86 if err := json.NewEncoder(w).Encode(response); err != nil { 87 x.Logger.Error("failed to encode response", "error", err) 88 writeError(w, xrpcerr.NewXrpcError( 89 xrpcerr.WithTag("InternalServerError"), 90 xrpcerr.WithMessage("failed to encode response"), 91 ), http.StatusInternalServerError) 92 return 93 } 94}