package modules import ( "strconv" "gsg/internal/core" "gsg/internal/gitx" ) func init() { core.Register("log", logMod) } // LogPage is one page of commit history, with enough to render prev/next // links without a separate count query (see the n+1 fetch trick below). type LogPage struct { Commits []gitx.Commit Ofs, PageSize int HasPrev, HasNext bool PrevOfs, NextOfs int } // log: a page of recent commits. {{ mod "log" "20" }} -- default // core.LogPageSize, "0" = all. Paginated via the ?ofs= query param on the // log route (see buildCtx), which core.LogPageSize must match. func logMod(c *core.Ctx, args ...string) (any, error) { n := core.LogPageSize if len(args) > 0 { if v, err := strconv.Atoi(args[0]); err == nil { n = v } } ofs, _ := strconv.Atoi(c.Var("ofs")) if ofs < 0 { ofs = 0 } rev := c.Commit if rev == "" { rev = c.Ref } fetch := n if n > 0 { fetch = n + 1 // one extra row to detect a next page } commits, err := c.Repo.Log(rev, fetch, ofs, true) if err != nil { return nil, err } page := LogPage{Ofs: ofs, PageSize: n, HasPrev: ofs > 0, PrevOfs: ofs - n} if page.PrevOfs < 0 { page.PrevOfs = 0 } if n > 0 && len(commits) > n { commits = commits[:n] page.HasNext, page.NextOfs = true, ofs+n } page.Commits = commits return page, nil }