This commit is contained in:
2026-06-22 08:55:57 +08:00
parent dbb87823e8
commit 6ade6e8fa9
325 changed files with 41131 additions and 855 deletions
+63
View File
@@ -11,6 +11,7 @@ import (
"errors"
"net/http"
"strconv"
"strings"
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
@@ -61,6 +62,7 @@ func (h *Handler) Mount(r chi.Router) {
r.Post("/main/commit", h.commitOnMain)
r.Post("/main/push", h.pushOnMain)
r.Get("/main/log", h.logOnMain)
r.Get("/diff", h.diffOnMain)
r.Get("/worktrees", h.listWorktrees)
r.Post("/worktrees", h.createWorktree)
@@ -74,6 +76,7 @@ func (h *Handler) Mount(r chi.Router) {
r.Post("/commit", h.commitOnWorktree)
r.Post("/push", h.pushOnWorktree)
r.Get("/log", h.logOnWorktree)
r.Get("/diff", h.diffOnWorktree)
})
}
@@ -595,3 +598,63 @@ func parseLogN(r *http.Request) int {
}
return n
}
func (h *Handler) diffOnMain(w http.ResponseWriter, r *http.Request) {
h.diffGeneric(w, r, true)
}
func (h *Handler) diffOnWorktree(w http.ResponseWriter, r *http.Request) {
h.diffGeneric(w, r, false)
}
func (h *Handler) diffGeneric(w http.ResponseWriter, r *http.Request, onMain bool) {
c, err := h.caller(r)
if err != nil {
writeErr(w, r, err)
return
}
param := "wsID"
if !onMain {
param = "wtID"
}
id, err := parseUUID(chi.URLParam(r, param))
if err != nil {
writeErr(w, r, err)
return
}
opts := parseDiffOptions(r)
var res git.DiffResult
if onMain {
res, err = h.gop.DiffOnMain(r.Context(), c, id, opts)
} else {
res, err = h.gop.DiffOnWorktree(r.Context(), c, id, opts)
}
if err != nil {
writeErr(w, r, err)
return
}
writeJSON(w, http.StatusOK, diffResp{Diff: res.Diff, Truncated: res.Truncated})
}
// parseDiffOptions reads the diff query params (ref, against, staged, paths, context).
func parseDiffOptions(r *http.Request) git.DiffOptions {
q := r.URL.Query()
opts := git.DiffOptions{
Ref: q.Get("ref"),
Against: q.Get("against"),
Staged: q.Get("staged") == "true" || q.Get("staged") == "1",
}
if raw := q.Get("paths"); raw != "" {
for _, p := range strings.Split(raw, ",") {
if p = strings.TrimSpace(p); p != "" {
opts.Paths = append(opts.Paths, p)
}
}
}
if cl := q.Get("context"); cl != "" {
if n, err := strconv.Atoi(cl); err == nil && n >= 0 {
opts.ContextLines = n
}
}
return opts
}