xonecas.io
8356
Wx

www-xonecas-io / files

www-xonecas-io / internal / site / commits.go

3.0 KB
1package site
2
3import (
4	"regexp"
5	"slices"
6	"strings"
7
8	"xonecas.io/site/internal/project"
9	"xonecas.io/site/internal/repo"
10)
11
12// PerPage is how many commits one page of a project's log lists.
13const PerPage = 50
14
15// LogEntry is one commit of a project, from any of its sources.
16type LogEntry struct {
17	Hash, Short, Author, Date, Subject, Body, Dir string
18	Parents                                       []string
19}
20
21// LogPage is one page of a project's commit log.
22type LogPage struct {
23	Row                ProjectRow
24	Name               string
25	Commits            []LogEntry
26	Page, Pages, Total int
27}
28
29// CommitPage is one commit with its diff.
30type CommitPage struct {
31	Row   ProjectRow
32	Name  string
33	C     LogEntry
34	Diffs []Diff
35	Cut   bool // the diff is over MaxDiffLines and was cut short
36}
37
38// email is an address in angle brackets, as in Co-authored-by trailers. The site shows names only.
39var email = regexp.MustCompile(`\s*<[^<>\s]+@[^<>\s]+>`)
40
41// ReadLog is every commit on every ref of every source, newest first, each hash once.
42func ReadLog(p project.Project) ([]LogEntry, error) {
43	var log []LogEntry
44	seen := map[string]bool{}
45	for _, s := range p.Sources {
46		if s.Commits == 0 {
47			continue
48		}
49		out, err := repo.Git(s.Dir, "log", "--all", "--date-order", "--format=%x1e%H%x1f%h%x1f%an%x1f%as%x1f%P%x1f%B")
50		if err != nil {
51			return nil, err
52		}
53		for _, rec := range strings.Split(out, "\x1e")[1:] {
54			f := strings.SplitN(rec, "\x1f", 6)
55			if len(f) < 6 || seen[f[0]] {
56				continue
57			}
58			seen[f[0]] = true
59			subject, body, _ := strings.Cut(strings.TrimSpace(email.ReplaceAllString(f[5], "")), "\n")
60			log = append(log, LogEntry{Hash: f[0], Short: f[1], Author: f[2], Date: f[3], Parents: strings.Fields(f[4]),
61				Subject: subject, Body: strings.TrimSpace(body), Dir: s.Dir})
62		}
63	}
64	slices.SortStableFunc(log, func(a, b LogEntry) int { return strings.Compare(b.Date, a.Date) })
65	return log, nil
66}
67
68// NewLogPage is page n, from 1, of log, the commits of project name.
69func NewLogPage(name string, row ProjectRow, log []LogEntry, n int) (LogPage, error) {
70	pages := max(1, (len(log)+PerPage-1)/PerPage)
71	if n < 1 || n > pages {
72		return LogPage{}, ErrNotFound
73	}
74	return LogPage{Row: row, Name: name, Commits: log[(n-1)*PerPage : min(len(log), n*PerPage)], Page: n, Pages: pages, Total: len(log)}, nil
75}
76
77// NewCommitPage is the commit with hash in log, with its diff.
78func NewCommitPage(name string, row ProjectRow, log []LogEntry, hash string) (CommitPage, error) {
79	i := slices.IndexFunc(log, func(c LogEntry) bool { return c.Hash == hash })
80	if i < 0 {
81		return CommitPage{}, ErrNotFound
82	}
83	c := log[i]
84	patch, err := repo.Git(c.Dir, "-c", "core.quotePath=false", "show", "--format=", "--no-color", "-M", "--diff-merges=first-parent", c.Hash)
85	if err != nil {
86		return CommitPage{}, err
87	}
88	diffs := parseDiff(patch)
89	cut := slices.ContainsFunc(diffs, func(d Diff) bool { return strings.HasPrefix(d.Note, "Cut short") })
90	return CommitPage{Row: row, Name: name, C: c, Diffs: diffs, Cut: cut}, nil
91}