BlueGit

timfa bluegit

Files

bluegit / internal / server / commit_list.go

commit_list.go

feat: geef de commitlijst de GitLab-opbouw
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Passed b026b59a History
commit_list.go 3.9 KiB Download Raw
1package server
2
3import (
4 "context"
5 "crypto/sha256"
6 "fmt"
7 "html/template"
8 "net/url"
9 "strconv"
10 "strings"
11 "time"
12
13 "gitlab.com/Timfa/bluegit/internal/gitrepo"
14 "gitlab.com/Timfa/bluegit/internal/i18n"
15)
16
17const (
18 commitPageSize = 20
19 commitScanCap = 4000
20)
21
22func finishCommit(row CommitRow, email, gitAuthor string) CommitRow {
23 if strings.TrimSpace(row.Who) == "" {
24 row.Who = strings.TrimSpace(gitAuthor)
25 }
26 if row.Who == "" {
27 row.Who = row.Author
28 }
29 if row.WhoURL == "" {
30 seed := strings.TrimSpace(email)
31 if seed == "" {
32 seed = row.Who
33 }
34 row.Ident = identiconSVG(seed)
35 }
36 return row
37}
38
39func identiconSVG(seed string) template.HTML {
40 sum := sha256.Sum256([]byte(strings.ToLower(strings.TrimSpace(seed))))
41 palette := []string{"#1f6feb", "#146c43", "#9f2d2d", "#8a5a00", "#6f42c1", "#0f766e", "#b45309"}
42 color := palette[int(sum[0])%len(palette)]
43 var b strings.Builder
44 b.WriteString(`<svg class="avatar ident" viewBox="0 0 5 5" aria-hidden="true">`)
45 b.WriteString(`<rect width="5" height="5" fill="#eef2f7"/>`)
46 painted := false
47 i := 1
48 for y := 0; y < 5; y++ {
49 for x := 0; x < 3; x++ {
50 if sum[i]%2 == 0 {
51 fmt.Fprintf(&b, `<rect x="%d" y="%d" width="1" height="1" fill="%s"/>`, x, y, color)
52 if x < 2 {
53 fmt.Fprintf(&b, `<rect x="%d" y="%d" width="1" height="1" fill="%s"/>`, 4-x, y, color)
54 }
55 painted = true
56 }
57 i++
58 }
59 }
60 if !painted {
61 fmt.Fprintf(&b, `<rect x="2" y="2" width="1" height="1" fill="%s"/>`, color)
62 }
63 b.WriteString(`</svg>`)
64 return template.HTML(b.String())
65}
66
67func (s *Server) commitWindow(ctx context.Context, dir, rev, query string, skip, limit int) ([]gitrepo.Commit, bool) {
68 if limit < 1 {
69 limit = commitPageSize
70 }
71 if skip < 0 {
72 skip = 0
73 }
74 needle := strings.ToLower(strings.TrimSpace(query))
75 if needle == "" {
76 list, err := gitrepo.LogPage(ctx, s.Git, dir, rev, skip, limit+1)
77 if err != nil || len(list) == 0 {
78 return nil, false
79 }
80 more := len(list) > limit
81 if more {
82 list = list[:limit]
83 }
84 return list, more
85 }
86 var match []gitrepo.Commit
87 scanned := 0
88 for len(match) < skip+limit+1 && scanned < commitScanCap {
89 chunk, err := gitrepo.LogPage(ctx, s.Git, dir, rev, scanned, 80)
90 if err != nil || len(chunk) == 0 {
91 break
92 }
93 for _, c := range chunk {
94 if commitMatches(c, needle) {
95 match = append(match, c)
96 }
97 }
98 scanned += len(chunk)
99 if len(chunk) < 80 {
100 break
101 }
102 }
103 if skip >= len(match) {
104 return nil, false
105 }
106 end := skip + limit
107 more := len(match) > end
108 if end > len(match) {
109 end = len(match)
110 }
111 return match[skip:end], more
112}
113
114func commitMatches(c gitrepo.Commit, needle string) bool {
115 return strings.Contains(strings.ToLower(c.Subject), needle) ||
116 strings.Contains(strings.ToLower(c.Body), needle) ||
117 strings.Contains(strings.ToLower(c.Author), needle) ||
118 strings.Contains(strings.ToLower(c.Email), needle)
119}
120
121func groupCommitDays(rows []CommitRow, lang i18n.Lang) []CommitDay {
122 var days []CommitDay
123 for _, row := range rows {
124 label := commitDayLabel(lang, row.Date)
125 if n := len(days); n == 0 || days[n-1].Label != label {
126 days = append(days, CommitDay{Label: label})
127 }
128 days[len(days)-1].Items = append(days[len(days)-1].Items, row)
129 }
130 return days
131}
132
133func commitDayLabel(lang i18n.Lang, date string) string {
134 at, err := time.Parse("2006-01-02", strings.TrimSpace(date))
135 if err != nil {
136 return date
137 }
138 return i18n.FormatDate(lang, at)
139}
140
141func commitPageNum(raw string) int {
142 n, err := strconv.Atoi(strings.TrimSpace(raw))
143 if err != nil || n < 1 {
144 return 1
145 }
146 if n > 1000 {
147 return 1000
148 }
149 return n
150}
151
152func clipQuery(raw string) string {
153 q := strings.TrimSpace(raw)
154 if len(q) > 200 {
155 q = q[:200]
156 }
157 return q
158}
159
160func commitListURL(repo, rev, query string, page int) string {
161 v := url.Values{}
162 if rev != "" {
163 v.Set("rev", rev)
164 }
165 if query != "" {
166 v.Set("q", query)
167 }
168 if page > 1 {
169 v.Set("page", strconv.Itoa(page))
170 }
171 u := "/" + repo + "/-/commits"
172 if enc := v.Encode(); enc != "" {
173 u += "?" + enc
174 }
175 return u
176}