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