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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
package main
import (
"net/http"
"path"
"slices"
"strconv"
"strings"
"sync"
)
type language struct {
Name string
Colour string
Bytes int64
Percent float64
}
var languages = map[string][2]string{
".go": {"Go", "#00add8"}, ".ml": {"OCaml", "#ef7a08"}, ".mli": {"OCaml", "#ef7a08"}, ".mly": {"OCaml", "#ef7a08"}, ".mll": {"OCaml", "#ef7a08"},
".thy": {"Isabelle", "#fefe00"}, ".ML": {"Standard ML", "#dc566d"}, ".sml": {"Standard ML", "#dc566d"},
".v": {"Rocq", "#d0b68c"}, ".lean": {"Lean", "#4a4a8c"}, ".agda": {"Agda", "#315665"}, ".hs": {"Haskell", "#5e5086"}, ".idr": {"Idris", "#b30000"},
".c": {"C", "#555555"}, ".h": {"C", "#555555"}, ".cc": {"C++", "#f34b7d"}, ".cpp": {"C++", "#f34b7d"}, ".hpp": {"C++", "#f34b7d"},
".rs": {"Rust", "#dea584"}, ".zig": {"Zig", "#ec915c"}, ".S": {"Assembly", "#6e4c13"}, ".s": {"Assembly", "#6e4c13"}, ".asm": {"Assembly", "#6e4c13"},
".HC": {"HolyC", "#ffefaf"}, ".smt2": {"SMT", "#7b8d9a"}, ".py": {"Python", "#3572a5"}, ".rb": {"Ruby", "#701516"},
".js": {"JavaScript", "#f1e05a"}, ".mjs": {"JavaScript", "#f1e05a"}, ".ts": {"TypeScript", "#3178c6"}, ".tsx": {"TypeScript", "#3178c6"},
".java": {"Java", "#b07219"}, ".kt": {"Kotlin", "#a97bff"}, ".swift": {"Swift", "#f05138"}, ".scala": {"Scala", "#c22d40"},
".ex": {"Elixir", "#6e4a7e"}, ".exs": {"Elixir", "#6e4a7e"}, ".erl": {"Erlang", "#b83998"}, ".gleam": {"Gleam", "#ffaff3"},
".lua": {"Lua", "#000080"}, ".nix": {"Nix", "#7e7eff"}, ".sh": {"Shell", "#89e051"}, ".tex": {"TeX", "#3d6117"},
".html": {"HTML", "#e34c26"}, ".css": {"CSS", "#663399"}, ".scm": {"Scheme", "#1e4aec"}, ".rkt": {"Racket", "#3c5caa"},
".lisp": {"Common Lisp", "#3fb68b"}, ".clj": {"Clojure", "#db5855"}, ".fs": {"F#", "#b845fc"}, ".nim": {"Nim", "#ffc200"},
}
var vendorDirs = []string{"vendor/", "node_modules/", "third_party/", "thirdparty/", "external/", "deps/", "dist/", "katex/"}
func vendored(name string) bool {
base := path.Base(name)
if strings.Contains(base, ".min.") || strings.HasSuffix(base, ".pb.go") || strings.HasSuffix(base, "_generated.go") {
return true
}
for _, d := range vendorDirs {
if strings.HasPrefix(name, d) || strings.Contains(name, "/"+d) {
return true
}
}
return false
}
type langEntry struct {
head string
langs []language
}
var langCache = struct {
sync.Mutex
m map[int64]langEntry
}{m: map[int64]langEntry{}}
func (s *Server) repoLanguages(repo Repo) []language {
g := Git{Dir: s.repoDir(repo)}
head, err := g.Resolve("HEAD")
if err != nil {
return nil
}
langCache.Lock()
e, ok := langCache.m[repo.ID]
langCache.Unlock()
if ok && e.head == head {
return e.langs
}
out, err := g.run(nil, "ls-tree", "-r", "-l", "-z", head)
if err != nil {
return nil
}
bytes := map[string]int64{}
var total int64
for _, rec := range strings.Split(string(out), "\x00") {
meta, name, ok := strings.Cut(rec, "\t")
f := strings.Fields(meta)
if !ok || len(f) != 4 || f[1] != "blob" || vendored(name) {
continue
}
l, known := languages[path.Ext(name)]
if !known {
l, known = languages[strings.ToLower(path.Ext(name))]
}
if !known {
continue
}
size, _ := strconv.ParseInt(f[3], 10, 64)
bytes[l[0]] += size
total += size
}
var langs []language
for _, l := range languages {
if n, ok := bytes[l[0]]; ok {
langs = append(langs, language{Name: l[0], Colour: l[1], Bytes: n, Percent: float64(n) * 100 / float64(total)})
delete(bytes, l[0])
}
}
slices.SortFunc(langs, func(a, b language) int {
if a.Bytes != b.Bytes {
return int(b.Bytes - a.Bytes)
}
return strings.Compare(a.Name, b.Name)
})
langCache.Lock()
langCache.m[repo.ID] = langEntry{head, langs}
langCache.Unlock()
return langs
}
type repoHit struct {
Repo
Language *language
score int
}
type userHit struct {
User
Profile Profile
Repos int
score int
}
type langCount struct {
Name string
Colour string
Count int
}
func matchScore(q string, fields ...string) int {
if q == "" {
return 1
}
best := 0
for i, f := range fields {
f = strings.ToLower(f)
weight := len(fields) - i
switch {
case f == q:
best = max(best, 100*weight)
case strings.HasPrefix(f, q):
best = max(best, 50*weight)
case strings.Contains(f, q):
best = max(best, 10*weight)
}
}
return best
}
func (s *Server) find(w http.ResponseWriter, r *http.Request) {
query := strings.TrimSpace(r.URL.Query().Get("q"))
kind := r.URL.Query().Get("type")
lang := r.URL.Query().Get("lang")
sort := r.URL.Query().Get("sort")
q := strings.ToLower(query)
if kind != "users" {
kind = "repos"
}
all, _ := s.store.PublicRepos("", "")
var repos []repoHit
counts := map[string]*langCount{}
for _, repo := range all {
langs := s.repoLanguages(repo)
var names []string
for _, l := range langs {
names = append(names, l.Name)
}
score := matchScore(q, repo.Name, repo.Owner.Name, strings.Join(repo.Tags, " "), strings.Join(names, " "), repo.Description)
if score == 0 {
continue
}
hit := repoHit{Repo: repo, score: score}
if len(langs) > 0 {
hit.Language = &langs[0]
c, ok := counts[langs[0].Name]
if !ok {
c = &langCount{Name: langs[0].Name, Colour: langs[0].Colour}
counts[langs[0].Name] = c
}
c.Count++
}
if lang != "" && (hit.Language == nil || hit.Language.Name != lang) {
continue
}
hit.Updated = repoUpdated(s.repoDir(repo))
repos = append(repos, hit)
}
switch sort {
case "updated":
slices.SortStableFunc(repos, func(a, b repoHit) int { return b.Updated.Compare(a.Updated) })
case "stars":
slices.SortStableFunc(repos, func(a, b repoHit) int { return b.Stars - a.Stars })
default:
sort = ""
slices.SortStableFunc(repos, func(a, b repoHit) int {
if a.score != b.score {
return b.score - a.score
}
return b.Updated.Compare(a.Updated)
})
}
var facets []langCount
for _, c := range counts {
facets = append(facets, *c)
}
slices.SortFunc(facets, func(a, b langCount) int {
if a.Count != b.Count {
return b.Count - a.Count
}
return strings.Compare(a.Name, b.Name)
})
var users []userHit
for _, u := range s.store.SearchableUsers() {
p := s.store.Profile(u.ID)
score := matchScore(q, u.Name, p.Display, p.Location, p.Workplace, p.Bio)
if score == 0 {
continue
}
owned, _ := s.store.ReposByOwner(u.ID, false)
hit := userHit{User: u, Profile: p, score: score}
for _, repo := range owned {
if repo.Unlisted {
continue
}
hit.Repos++
}
if lang != "" {
found := false
for _, repo := range repos {
if repo.Owner.ID == u.ID {
found = true
break
}
}
if !found {
continue
}
}
users = append(users, hit)
}
slices.SortStableFunc(users, func(a, b userHit) int {
if a.score != b.score {
return b.score - a.score
}
return b.Repos - a.Repos
})
if len(repos) > 100 {
repos = repos[:100]
}
if len(users) > 100 {
users = users[:100]
}
title := "Search"
if query != "" {
title = query + " ยท search"
}
s.render(w, r, "search", page{Title: title, User: s.current(r), Data: map[string]any{
"Query": query, "Type": kind, "Lang": lang, "Sort": sort, "Repos": repos, "Users": users, "Languages": facets, "Facets": true,
}})
}
|