BlueGit

timfa bluegit

Files

bluegit / internal / server / project.go

project.go

project.go 8.0 KiB Download Raw
1package server
2
3import (
4 "context"
5 "fmt"
6 "html/template"
7 "net/http"
8 "path"
9 "sort"
10 "strings"
11
12 "gitlab.com/Timfa/bluegit/internal/access"
13 "gitlab.com/Timfa/bluegit/internal/gitrepo"
14 "gitlab.com/Timfa/bluegit/internal/highlight"
15 "gitlab.com/Timfa/bluegit/internal/i18n"
16 "gitlab.com/Timfa/bluegit/internal/markdown"
17 "gitlab.com/Timfa/bluegit/internal/store"
18)
19
20func (s *Server) fillOverview(r *http.Request, v *View, repo store.Repo) {
21 v.Page = "overview"
22 s.branchList(r, v, repo)
23 s.fillGraph(r, v, repo)
24 lang := i18n.Parse(v.Lang)
25 if c, err := s.cLastCommit(r.Context(), repo, v.Rev, ""); err == nil && c.SHA != "" {
26 if rows := s.presentCommits(r.Context(), repo.ID, []gitrepo.Commit{c}, lang); len(rows) > 0 {
27 s.attachHeadPipeline(r.Context(), repo, &rows[0], lang)
28 v.Head = &rows[0]
29 }
30 }
31 entries, err := s.cTree(r.Context(), repo, v.Rev, "")
32 touch, _ := s.cTouch(r.Context(), repo, v.Rev)
33 if err == nil {
34 v.Files = fileRows(entries, "", touch, lang)
35 }
36 readme, license := findDocs(entries)
37 v.LicensePath = license
38 if readme != "" {
39 text, binary, err := s.cDoc(r.Context(), repo, v.Rev, readme, 200_000)
40 if err == nil && !binary {
41 v.ReadmeName = readme
42 v.Readme = s.renderDoc(r, repo, v.Rev, "", text)
43 }
44 }
45 commits, branches, tags, _ := s.cStats(r.Context(), repo, v.Rev)
46 v.NCommits, v.NBranches, v.NTags = commits, branches, tags
47 if rels, err := s.Store.ListReleases(r.Context(), repo.ID); err == nil && v.ShowReleases {
48 v.NReleases = len(rels)
49 }
50 v.Created = i18n.FormatDate(lang, repo.CreatedAt)
51}
52
53func (s *Server) fillFiles(r *http.Request, v *View, repo store.Repo, rel string, isFile bool) {
54 v.Page = "files"
55 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
56 lang := i18n.Parse(v.Lang)
57 s.branchList(r, v, repo)
58 all, _ := s.cTreeAll(r.Context(), repo, v.Rev)
59 v.Tree = treeRows(all, rel)
60 if !isFile {
61 v.TreePath = rel
62 entries, err := s.cTree(r.Context(), repo, v.Rev, rel)
63 touch, _ := s.cTouch(r.Context(), repo, v.Rev)
64 if err == nil {
65 v.Files = fileRows(entries, rel, touch, lang)
66 // Een README in een submap staat onder de bestandslijst, met
67 // links en afbeeldingen relatief aan die map.
68 if readme, _ := findDocs(entries); readme != "" {
69 if text, binary, err := s.cDoc(r.Context(), repo, v.Rev, path.Join(rel, readme), 200_000); err == nil && !binary {
70 v.ReadmeName = readme
71 v.Readme = s.renderDoc(r, repo, v.Rev, rel, text)
72 }
73 }
74 }
75 return
76 }
77 v.BlobPath = rel
78 v.FileName = path.Base(rel)
79 text, binary, err := gitrepo.Blob(r.Context(), s.Git, dir, v.Rev, rel, 200_000)
80 if err != nil {
81 return
82 }
83 v.Binary = binary
84 v.IsSVG = strings.EqualFold(path.Ext(rel), ".svg")
85 if !binary && isMarkdownName(v.FileName) {
86 v.MDView = true
87 v.Readme = s.renderDoc(r, repo, v.Rev, path.Dir(rel), text)
88 }
89 if !binary {
90 for i, line := range highlight.Lines(rel, text) {
91 v.Lines = append(v.Lines, CodeLine{N: i + 1, HTML: template.HTML(line)})
92 }
93 }
94 if n, err := gitrepo.BlobSize(r.Context(), s.Git, dir, v.Rev, rel); err == nil {
95 v.BlobSize = formatSize(n)
96 if binary && n <= rawMax && rawImageTypes[strings.ToLower(path.Ext(rel))] != "" {
97 if data, err := gitrepo.BlobText(r.Context(), s.Git, dir, v.Rev, rel); err == nil {
98 v.IsImage = rasterImageType(rel, data) != ""
99 }
100 }
101 }
102 if c, err := s.cLastCommit(r.Context(), repo, v.Rev, rel); err == nil && c.SHA != "" {
103 if rows := s.presentCommits(r.Context(), repo.ID, []gitrepo.Commit{c}, lang); len(rows) > 0 {
104 s.attachHeadPipeline(r.Context(), repo, &rows[0], lang)
105 v.Head = &rows[0]
106 }
107 }
108}
109
110func (s *Server) branchList(r *http.Request, v *View, repo store.Repo) {
111 patterns, _ := s.Store.ProtectedPatterns(r.Context(), v.Repo.ID)
112 v.RevProtected = access.BranchProtected(patterns, v.Rev)
113 names, _ := s.cBranches(r.Context(), repo)
114 seen := false
115 for _, name := range names {
116 if name == v.Rev {
117 seen = true
118 }
119 v.Branches = append(v.Branches, BranchRow{Name: name, Protected: access.BranchProtected(patterns, name)})
120 }
121 if v.Rev != "" && !seen {
122 v.Branches = append([]BranchRow{{Name: v.Rev, Protected: v.RevProtected}}, v.Branches...)
123 }
124}
125
126func (s *Server) attachHeadPipeline(ctx context.Context, repo store.Repo, row *CommitRow, lang i18n.Lang) {
127 if row == nil || row.SHA == "" || s.Store == nil {
128 return
129 }
130 p, err := s.Store.LatestPipelineByCommit(ctx, repo.ID, row.SHA)
131 if err != nil {
132 return
133 }
134 jobs, _ := s.Store.JobsByPipeline(ctx, p.ID)
135 status := displayStatus(s.pipeStatusFor(p), jobs)
136 row.PipeID = p.ID
137 row.PipeStatus = status
138 row.PipeLabel = i18n.T(lang, "pipe."+status)
139 row.PipeDuration = pipelineClock(p)
140 tr := func(key string, args ...any) string { return i18n.T(lang, key, args...) }
141 row.PipeStages = stageViews(jobs, s.queueRanks(ctx), tr)
142 row.PipeLive = jobsLive(p.Status, jobs)
143 row.PipeFailed = jobsFailed(jobs)
144}
145
146func fileRows(entries []gitrepo.Entry, base string, touch map[string]gitrepo.Commit, lang i18n.Lang) []FileRow {
147 var out []FileRow
148 for _, e := range entries {
149 p := e.Name
150 if base != "" {
151 p = base + "/" + e.Name
152 }
153 row := FileRow{Name: e.Name, Path: p, Dir: e.Type == "tree", Icon: fileIcon(e.Name, e.Type)}
154 if c, ok := touch[p]; ok {
155 row.Subject = c.Subject
156 row.When = i18n.Ago(lang, c.Unix)
157 row.SHA = c.SHA
158 }
159 out = append(out, row)
160 }
161 sort.Slice(out, func(i, j int) bool {
162 if out[i].Dir != out[j].Dir {
163 return out[i].Dir
164 }
165 return strings.ToLower(out[i].Name) < strings.ToLower(out[j].Name)
166 })
167 return out
168}
169
170func treeRows(entries []gitrepo.Entry, current string) []FileRow {
171 var out []FileRow
172 for _, e := range entries {
173 base := e.Name
174 if i := strings.LastIndex(e.Name, "/"); i >= 0 {
175 base = e.Name[i+1:]
176 }
177 depth := strings.Count(e.Name, "/")
178 if depth > 8 {
179 depth = 8
180 }
181 dir := e.Type == "tree"
182 inside := current == e.Name || strings.HasPrefix(current, e.Name+"/")
183 active := e.Name == current
184 if dir && inside {
185 active = true
186 }
187 out = append(out, FileRow{
188 Name: base, Path: e.Name, Dir: dir, Icon: fileIcon(base, e.Type),
189 Depth: depth, Active: active, Open: dir && inside,
190 })
191 }
192 return out
193}
194
195func findDocs(entries []gitrepo.Entry) (readme, license string) {
196 readmeRank := 9
197 for _, e := range entries {
198 if e.Type != "blob" {
199 continue
200 }
201 low := strings.ToLower(e.Name)
202 rank := 9
203 switch low {
204 case "readme.md":
205 rank = 0
206 case "readme.markdown":
207 rank = 1
208 case "readme":
209 rank = 2
210 }
211 if rank < readmeRank {
212 readmeRank = rank
213 readme = e.Name
214 }
215 switch low {
216 case "license", "license.md", "license.txt", "licence", "copying":
217 if license == "" || low == "license" {
218 license = e.Name
219 }
220 }
221 }
222 return readme, license
223}
224
225func fileIcon(name, typ string) string {
226 if typ == "tree" {
227 return "dir"
228 }
229 switch strings.ToLower(path.Ext(name)) {
230 case ".md", ".markdown":
231 return "md"
232 case ".png", ".jpg", ".jpeg", ".gif", ".svg", ".webp":
233 return "img"
234 case ".go", ".js", ".ts", ".py", ".rs", ".c", ".h", ".rb", ".sh":
235 return "code"
236 case ".yml", ".yaml", ".toml", ".json", ".lock":
237 return "cfg"
238 }
239 base := strings.ToLower(name)
240 if base == "license" || base == "licence" || base == "copying" || strings.HasPrefix(base, "license.") {
241 return "lic"
242 }
243 if strings.HasPrefix(base, "readme") {
244 return "md"
245 }
246 return "file"
247}
248
249func formatSize(n int64) string {
250 if n < 1024 {
251 return fmt.Sprintf("%d B", n)
252 }
253 if n < 1024*1024 {
254 return fmt.Sprintf("%.1f KiB", float64(n)/1024)
255 }
256 return fmt.Sprintf("%.1f MiB", float64(n)/(1024*1024))
257}
258
259// renderDoc rendert een Markdown-document uit map dir (leeg of "." is de
260// root). Relatieve links gaan naar /-/blob/, afbeeldingen naar /-/raw/,
261// beide op dezelfde ref.
262func (s *Server) renderDoc(r *http.Request, repo store.Repo, rev, dir, text string) template.HTML {
263 base := "/" + repo.FullPath() + "/-/blob/"
264 if dir = strings.Trim(dir, "/"); dir != "" && dir != "." {
265 base += dir + "/"
266 }
267 return template.HTML(markdown.RenderTasks(text, base, rev, "/"+repo.FullPath(), markdown.Tasks{Count: s.tr(r, "tpl.tasks")}))
268}
269
270func isMarkdownName(name string) bool {
271 switch strings.ToLower(path.Ext(name)) {
272 case ".md", ".markdown":
273 return true
274 }
275 return strings.EqualFold(name, "readme")
276}