Migration This is my new public git source viewer. Source coming soon. Please contact with concerns/bugs. Contact me

internal/modules/example_authors.go

4bda28df3ab3dbfcc789dcbbb184cabc8cac9398 · 1.1 KB · 52 lines raw

1 package modules
2
3 import (
4 "sort"
5 "strconv"
6
7 "gsg/internal/core"
8 )
9
10 // This file is the ENTIRE cost of adding a feature. No core change, no
11 // route change, no registration list to update -- write the module, then
12 // call `mod "authors"` from any template that wants it.
13
14 type Author struct {
15 Name string
16 Commits int
17 }
18
19 func init() { core.Register("authors", authorsMod) }
20
21 // authors: commit counts per author over the last N commits (default 500).
22 func authorsMod(c *core.Ctx, args ...string) (any, error) {
23 n := 500
24 if len(args) > 0 {
25 if v, err := strconv.Atoi(args[0]); err == nil {
26 n = v
27 }
28 }
29 rev := c.Commit
30 if rev == "" {
31 rev = c.Ref
32 }
33 log, err := c.Repo.Log(rev, n, 0, false)
34 if err != nil {
35 return nil, err
36 }
37 counts := map[string]int{}
38 for _, cm := range log {
39 counts[cm.Author]++
40 }
41 out := make([]Author, 0, len(counts))
42 for name, n := range counts {
43 out = append(out, Author{Name: name, Commits: n})
44 }
45 sort.Slice(out, func(i, j int) bool {
46 if out[i].Commits != out[j].Commits {
47 return out[i].Commits > out[j].Commits
48 }
49 return out[i].Name < out[j].Name
50 })
51 return out, nil
52 }