@@ -0,0 +1,117 @@
1+package site
2+
3+import (
4+ "errors"
5+ "fmt"
6+ "os"
7+ "path/filepath"
8+ "regexp"
9+ "runtime"
10+ "slices"
11+ "strings"
12+ "sync"
13+
14+ "xonecas.io/site/internal/project"
15+ "xonecas.io/site/internal/repo"
16+)
17+
18+// PerPage is how many commits one page of a project's log lists.
19+const PerPage = 50
20+
21+// LogEntry is one commit of a project, from any of its sources.
22+type LogEntry struct {
23+ Hash, Short, Author, Date, Subject, Body, Dir string
24+ Parents []string
25+}
26+
27+// LogPage is one page of a project's commit log.
28+type LogPage struct {
29+ Row ProjectRow
30+ Name string
31+ Commits []LogEntry
32+ Page, Pages, Total int
33+}
34+
35+// CommitPage is one commit with its diff.
36+type CommitPage struct {
37+ Row ProjectRow
38+ Name string
39+ C LogEntry
40+ Diffs []Diff
41+ Cut bool // the diff is over MaxDiffLines and was cut short
42+}
43+
44+// email is an address in angle brackets, as in Co-authored-by trailers. The site shows names only.
45+var email = regexp.MustCompile(`\s*<[^<>\s]+@[^<>\s]+>`)
46+
47+// readLog is every commit on every ref of every source, newest first, each hash once.
48+func readLog(p project.Project) ([]LogEntry, error) {
49+ var log []LogEntry
50+ seen := map[string]bool{}
51+ for _, s := range p.Sources {
52+ if s.Commits == 0 {
53+ continue
54+ }
55+ out, err := repo.Git(s.Dir, "log", "--all", "--date-order", "--format=%x1e%H%x1f%h%x1f%an%x1f%as%x1f%P%x1f%B")
56+ if err != nil {
57+ return nil, err
58+ }
59+ for _, rec := range strings.Split(out, "\x1e")[1:] {
60+ f := strings.SplitN(rec, "\x1f", 6)
61+ if len(f) < 6 || seen[f[0]] {
62+ continue
63+ }
64+ seen[f[0]] = true
65+ subject, body, _ := strings.Cut(strings.TrimSpace(email.ReplaceAllString(f[5], "")), "\n")
66+ log = append(log, LogEntry{Hash: f[0], Short: f[1], Author: f[2], Date: f[3], Parents: strings.Fields(f[4]),
67+ Subject: subject, Body: strings.TrimSpace(body), Dir: s.Dir})
68+ }
69+ }
70+ slices.SortStableFunc(log, func(a, b LogEntry) int { return strings.Compare(b.Date, a.Date) })
71+ return log, nil
72+}
73+
74+// writeCommits writes the log pages, and a page for each commit not already built by this
75+// same build of the site (stamp), so a rebuild only renders new commits.
76+func writeCommits(out string, p project.Project, row ProjectRow, fresh bool) error {
77+ log, err := readLog(p)
78+ if err != nil {
79+ return err
80+ }
81+ base := filepath.Join(out, "projects", p.Name, "commits")
82+ pages := max(1, (len(log)+PerPage-1)/PerPage)
83+ for i := range pages {
84+ dir := base
85+ if i > 0 {
86+ dir = filepath.Join(base, fmt.Sprint(i+1))
87+ }
88+ page := LogPage{Row: row, Name: p.Name, Commits: log[i*PerPage : min(len(log), (i+1)*PerPage)], Page: i + 1, Pages: pages, Total: len(log)}
89+ if err := render(filepath.Join(dir, "index.html"), "commits.html", page); err != nil {
90+ return err
91+ }
92+ }
93+ var wg sync.WaitGroup
94+ errs := make([]error, len(log))
95+ sem := make(chan struct{}, runtime.NumCPU())
96+ for i, c := range log {
97+ path := filepath.Join(out, "projects", p.Name, "commit", c.Hash, "index.html")
98+ if _, err := os.Stat(path); err == nil && fresh {
99+ continue
100+ }
101+ wg.Go(func() {
102+ sem <- struct{}{}
103+ defer func() { <-sem }()
104+ patch, err := repo.Git(c.Dir, "-c", "core.quotePath=false", "show", "--format=", "--no-color", "-M", "--diff-merges=first-parent", c.Hash)
105+ if err == nil {
106+ diffs := parseDiff(patch)
107+ cut := slices.ContainsFunc(diffs, func(d Diff) bool { return strings.HasPrefix(d.Note, "Cut short") })
108+ err = render(path, "commit.html", CommitPage{Row: row, Name: p.Name, C: c, Diffs: diffs, Cut: cut})
109+ }
110+ if err != nil {
111+ errs[i] = fmt.Errorf("%s: %w", c.Short, err)
112+ }
113+ })
114+ }
115+ wg.Wait()
116+ return errors.Join(errs...)
117+}