-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
222 lines (199 loc) · 4.69 KB
/
helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package gitstats
import (
"bytes"
"database/sql/driver"
"fmt"
"html/template"
"path"
"strconv"
"strings"
"zgo.at/zstd/zint"
)
var quote = strings.NewReplacer(`\`, `\\`, `"`, `\"`)
func isSpace(ch byte) bool {
// see array_isspace:
// https://github.com/postgres/postgres/blob/master/src/backend/utils/adt/arrayfuncs.c
return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' || ch == '\v' || ch == '\f'
}
type Strings []string
func (l Strings) String() string { return strings.Join(l, ", ") }
func (l Strings) Join(sep string) string { return strings.Join(l, sep) }
func (l Strings) Value() (driver.Value, error) {
for i := range l {
if l[i] == "" || l[i] == "null" ||
isSpace(l[i][0]) || isSpace(l[i][len(l[i])-1]) ||
strings.ContainsAny(l[i], `{},"\`) {
l[i] = `"` + quote.Replace(l[i]) + `"`
}
}
return `{` + strings.Join(l, `,`) + `}`, nil
}
func (l *Strings) Scan(v any) error {
if v == nil {
return nil
}
var val []byte
switch vv := v.(type) {
case []byte:
val = vv
case string:
val = []byte(vv)
default:
return fmt.Errorf("unsupported type: %T", vv)
}
if len(val) == 2 && val[0] == '{' && val[1] == '}' {
*l = []string{}
return nil
}
// TODO: wrong! Need to properly parse, deal with quotes, etc.
// This is why Ulrich Drepper errors out, because e.g. commit
// 5ab7f7c12a6c0 has file 1619242 with a comma:
// /localedata/fr_CA,2.13.in
//
var (
q, bs bool
cur = make([]rune, 0, 16)
strs = make([]string, 0, 8)
)
for _, c := range []rune(string(val[1 : len(val)-1])) {
switch {
case !q && c == ',':
if len(cur) > 0 {
strs = append(strs, string(cur))
}
cur = cur[:0]
case !bs && c == '"':
if q {
strs = append(strs, string(cur))
cur = cur[:0]
}
q = !q
case c == '\\':
bs = true
default:
cur = append(cur, c)
bs = false
}
}
if len(cur) > 0 {
strs = append(strs, string(cur))
}
//split := bytes.Split(val[1:len(val)-1], []byte{','})
//strs := make([]string, 0, len(split))
//for _, s := range split {
// //s = strings.TrimSpace(s)
// //if len(s) == 0 {
// // continue
// //}
// strs = append(strs, strings.Trim(string(s), `"`))
//}
*l = strs
return nil
}
type Ints []int64
func (l Ints) Sum() int64 {
var s int64
for _, n := range l {
s += n
}
return s
}
func (l Ints) String() string { return zint.Join(l, ", ") }
func (l Ints) Value() (driver.Value, error) {
return "{" + zint.Join(l, ",") + "}", nil
}
func (l *Ints) Scan(v any) error {
if v == nil {
return nil
}
var val []byte
switch vv := v.(type) {
case []byte:
val = vv
case string:
val = []byte(vv)
default:
return fmt.Errorf("unsupported type: %T", vv)
}
if len(val) == 2 && val[0] == '{' && val[1] == '}' {
*l = []int64{}
return nil
}
split := bytes.Split(val[1:len(val)-1], []byte{','})
strs := make([]int64, 0, len(split))
for _, s := range split {
i, err := strconv.ParseInt(string(s), 10, 64)
if err != nil {
return err
}
strs = append(strs, i)
}
*l = strs
return nil
}
type Hash [20]byte
func NewHash(s string) Hash {
if len(s) != 40 {
panic(fmt.Sprintf("wrong sha: %q", s))
}
var h Hash
for i := 0; i < 40; i += 2 {
n, err := strconv.ParseUint(s[i:i+2], 16, 8)
if err != nil {
panic(fmt.Sprintf("wrong sha: %q: %s", h, err))
}
h[i/2] = byte(n)
}
return h
}
func (h Hash) Short() string {
// TODO: git is a bit smarter about this, using a short hash length
// proportional to the number of commits or some such. Look up how this
// works and duplicate.
b := new(strings.Builder)
for _, hh := range h[:4] {
fmt.Fprintf(b, "%02x", hh)
}
return b.String()
}
func (h Hash) Link(repoURL string) template.HTML {
l := h.URL(repoURL)
if l == "" {
return template.HTML(h.Short())
}
return template.HTML(`<a href="` + string(l) + `">` + h.Short() + `</a>`)
}
func (h Hash) URL(repoURL string) template.URL {
switch {
case strings.Contains(repoURL, "gitlab"):
return template.URL(repoURL + "/-/commit/" + h.String())
case strings.Contains(repoURL, "github.org") || strings.Contains(repoURL, "codeberg.org"):
return template.URL(repoURL + "/commit/" + h.String())
case strings.Contains(repoURL, "savannah.gnu.org"):
n := path.Base(repoURL)
return template.URL(fmt.Sprintf("https://git.savannah.gnu.org/cgit/%s.git/commit/?id=%s", n, h))
default:
return ""
}
}
func (h Hash) String() string {
b := new(strings.Builder)
for _, hh := range h {
fmt.Fprintf(b, "%02x", hh)
}
return b.String()
}
func (h *Hash) Scan(v any) error {
switch vv := v.(type) {
case string:
*h = NewHash(vv)
case []byte:
*h = [20]byte(vv)
default:
return fmt.Errorf("Hash.Scan: unsupported type %T: %[1]q", vv)
}
return nil
}
func (h Hash) Value() (driver.Value, error) {
return h[:], nil
}