Migration
This is my new public git source viewer. Source coming soon. Please contact with concerns/bugs.
Contact me
internal/modules/log.go
83aa601b98007ead72afeadb131a93d5694e04ca
· 1.3 KB · 59 lines
raw
| 1 | package modules |
| 2 | |
| 3 | import ( |
| 4 | "strconv" |
| 5 | |
| 6 | "gsg/internal/core" |
| 7 | "gsg/internal/gitx" |
| 8 | ) |
| 9 | |
| 10 | func init() { core.Register("log", logMod) } |
| 11 | |
| 12 | // LogPage is one page of commit history, with enough to render prev/next |
| 13 | // links without a separate count query (see the n+1 fetch trick below). |
| 14 | type LogPage struct { |
| 15 | Commits []gitx.Commit |
| 16 | Ofs, PageSize int |
| 17 | HasPrev, HasNext bool |
| 18 | PrevOfs, NextOfs int |
| 19 | } |
| 20 | |
| 21 | // log: a page of recent commits. {{ mod "log" "20" }} -- default |
| 22 | // core.LogPageSize, "0" = all. Paginated via the ?ofs= query param on the |
| 23 | // log route (see buildCtx), which core.LogPageSize must match. |
| 24 | func logMod(c *core.Ctx, args ...string) (any, error) { |
| 25 | n := core.LogPageSize |
| 26 | if len(args) > 0 { |
| 27 | if v, err := strconv.Atoi(args[0]); err == nil { |
| 28 | n = v |
| 29 | } |
| 30 | } |
| 31 | ofs, _ := strconv.Atoi(c.Var("ofs")) |
| 32 | if ofs < 0 { |
| 33 | ofs = 0 |
| 34 | } |
| 35 | rev := c.Commit |
| 36 | if rev == "" { |
| 37 | rev = c.Ref |
| 38 | } |
| 39 | |
| 40 | fetch := n |
| 41 | if n > 0 { |
| 42 | fetch = n + 1 // one extra row to detect a next page |
| 43 | } |
| 44 | commits, err := c.Repo.Log(rev, fetch, ofs, true) |
| 45 | if err != nil { |
| 46 | return nil, err |
| 47 | } |
| 48 | |
| 49 | page := LogPage{Ofs: ofs, PageSize: n, HasPrev: ofs > 0, PrevOfs: ofs - n} |
| 50 | if page.PrevOfs < 0 { |
| 51 | page.PrevOfs = 0 |
| 52 | } |
| 53 | if n > 0 && len(commits) > n { |
| 54 | commits = commits[:n] |
| 55 | page.HasNext, page.NextOfs = true, ofs+n |
| 56 | } |
| 57 | page.Commits = commits |
| 58 | return page, nil |
| 59 | } |