BlueGit

timfa bluegit

Files

bluegit / internal / server / project.go

project.go

project.go 7.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 lang := i18n.Parse(v.Lang)
24 if c, err := s.cLastCommit(r.Context(), repo, v.Rev, ""); err == nil && c.SHA != "" {
25 if rows := s.presentCommits(r.Context(), repo.ID, []gitrepo.Commit{c}, lang); len(rows) > 0 {
26 s.attachHeadPipeline(r.Context(), repo, &rows[0], lang)
27 v.Head = &rows[0]
28 }
29 }
30 entries, err := s.cTree(r.Context(), repo, v.Rev, "")
31 touch, _ := s.cTouch(r.Context(), repo, v.Rev)
32 if err == nil {
33 v.Files = fileRows(entries, "", touch, lang)
34 }
35 readme, license := findDocs(entries)
36 v.LicensePath = license
37 if readme != "" {
38 text, binary, err := s.cDoc(r.Context(), repo, v.Rev, readme, 200_000)
39 if err == nil && !binary {
40 v.ReadmeName = readme
41 base := "/" + repo.FullPath() + "/-/blob/"
42 v.Readme = template.HTML(markdown.RenderTasks(text, base, v.Rev, "/"+repo.FullPath(), markdown.Tasks{Count: s.tr(r, "tpl.tasks")}))
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 }
67 return
68 }
69 v.BlobPath = rel
70 v.FileName = path.Base(rel)
71 text, binary, err := gitrepo.Blob(r.Context(), s.Git, dir, v.Rev, rel, 200_000)
72 if err != nil {
73 return
74 }
75 v.Binary = binary
76 v.IsSVG = strings.EqualFold(path.Ext(rel), ".svg")
77 if !binary {
78 for i, line := range highlight.Lines(rel, text) {
79 v.Lines = append(v.Lines, CodeLine{N: i + 1, HTML: template.HTML(line)})
80 }
81 }
82 if n, err := gitrepo.BlobSize(r.Context(), s.Git, dir, v.Rev, rel); err == nil {
83 v.BlobSize = formatSize(n)
84 if binary && n <= rawMax && rawImageTypes[strings.ToLower(path.Ext(rel))] != "" {
85 if data, err := gitrepo.BlobText(r.Context(), s.Git, dir, v.Rev, rel); err == nil {
86 v.IsImage = rasterImageType(rel, data) != ""
87 }
88 }
89 }
90 if c, err := s.cLastCommit(r.Context(), repo, v.Rev, rel); err == nil && c.SHA != "" {
91 if rows := s.presentCommits(r.Context(), repo.ID, []gitrepo.Commit{c}, lang); len(rows) > 0 {
92 s.attachHeadPipeline(r.Context(), repo, &rows[0], lang)
93 v.Head = &rows[0]
94 }
95 }
96}
97
98func (s *Server) branchList(r *http.Request, v *View, repo store.Repo) {
99 patterns, _ := s.Store.ProtectedPatterns(r.Context(), v.Repo.ID)
100 v.RevProtected = access.BranchProtected(patterns, v.Rev)
101 names, _ := s.cBranches(r.Context(), repo)
102 seen := false
103 for _, name := range names {
104 if name == v.Rev {
105 seen = true
106 }
107 v.Branches = append(v.Branches, BranchRow{Name: name, Protected: access.BranchProtected(patterns, name)})
108 }
109 if v.Rev != "" && !seen {
110 v.Branches = append([]BranchRow{{Name: v.Rev, Protected: v.RevProtected}}, v.Branches...)
111 }
112}
113
114func (s *Server) attachHeadPipeline(ctx context.Context, repo store.Repo, row *CommitRow, lang i18n.Lang) {
115 if row == nil || row.SHA == "" || s.Store == nil {
116 return
117 }
118 p, err := s.Store.LatestPipelineByCommit(ctx, repo.ID, row.SHA)
119 if err != nil {
120 return
121 }
122 jobs, _ := s.Store.JobsByPipeline(ctx, p.ID)
123 status := displayStatus(s.pipeStatusFor(p), jobs)
124 row.PipeID = p.ID
125 row.PipeStatus = status
126 row.PipeLabel = i18n.T(lang, "pipe."+status)
127 row.PipeDuration = pipelineClock(p)
128 tr := func(key string, args ...any) string { return i18n.T(lang, key, args...) }
129 row.PipeStages = stageViews(jobs, s.queueRanks(ctx), tr)
130 row.PipeLive = jobsLive(p.Status, jobs)
131 row.PipeFailed = jobsFailed(jobs)
132}
133
134func fileRows(entries []gitrepo.Entry, base string, touch map[string]gitrepo.Commit, lang i18n.Lang) []FileRow {
135 var out []FileRow
136 for _, e := range entries {
137 p := e.Name
138 if base != "" {
139 p = base + "/" + e.Name
140 }
141 row := FileRow{Name: e.Name, Path: p, Dir: e.Type == "tree", Icon: fileIcon(e.Name, e.Type)}
142 if c, ok := touch[p]; ok {
143 row.Subject = c.Subject
144 row.When = i18n.Ago(lang, c.Unix)
145 row.SHA = c.SHA
146 }
147 out = append(out, row)
148 }
149 sort.Slice(out, func(i, j int) bool {
150 if out[i].Dir != out[j].Dir {
151 return out[i].Dir
152 }
153 return strings.ToLower(out[i].Name) < strings.ToLower(out[j].Name)
154 })
155 return out
156}
157
158func treeRows(entries []gitrepo.Entry, current string) []FileRow {
159 var out []FileRow
160 for _, e := range entries {
161 base := e.Name
162 if i := strings.LastIndex(e.Name, "/"); i >= 0 {
163 base = e.Name[i+1:]
164 }
165 depth := strings.Count(e.Name, "/")
166 if depth > 8 {
167 depth = 8
168 }
169 dir := e.Type == "tree"
170 inside := current == e.Name || strings.HasPrefix(current, e.Name+"/")
171 active := e.Name == current
172 if dir && inside {
173 active = true
174 }
175 out = append(out, FileRow{
176 Name: base, Path: e.Name, Dir: dir, Icon: fileIcon(base, e.Type),
177 Depth: depth, Active: active, Open: dir && inside,
178 })
179 }
180 return out
181}
182
183func findDocs(entries []gitrepo.Entry) (readme, license string) {
184 readmeRank := 9
185 for _, e := range entries {
186 if e.Type != "blob" {
187 continue
188 }
189 low := strings.ToLower(e.Name)
190 rank := 9
191 switch low {
192 case "readme.md":
193 rank = 0
194 case "readme.markdown":
195 rank = 1
196 case "readme":
197 rank = 2
198 }
199 if rank < readmeRank {
200 readmeRank = rank
201 readme = e.Name
202 }
203 switch low {
204 case "license", "license.md", "license.txt", "licence", "copying":
205 if license == "" || low == "license" {
206 license = e.Name
207 }
208 }
209 }
210 return readme, license
211}
212
213func fileIcon(name, typ string) string {
214 if typ == "tree" {
215 return "dir"
216 }
217 switch strings.ToLower(path.Ext(name)) {
218 case ".md", ".markdown":
219 return "md"
220 case ".png", ".jpg", ".jpeg", ".gif", ".svg", ".webp":
221 return "img"
222 case ".go", ".js", ".ts", ".py", ".rs", ".c", ".h", ".rb", ".sh":
223 return "code"
224 case ".yml", ".yaml", ".toml", ".json", ".lock":
225 return "cfg"
226 }
227 base := strings.ToLower(name)
228 if base == "license" || base == "licence" || base == "copying" || strings.HasPrefix(base, "license.") {
229 return "lic"
230 }
231 if strings.HasPrefix(base, "readme") {
232 return "md"
233 }
234 return "file"
235}
236
237func formatSize(n int64) string {
238 if n < 1024 {
239 return fmt.Sprintf("%d B", n)
240 }
241 if n < 1024*1024 {
242 return fmt.Sprintf("%.1f KiB", float64(n)/1024)
243 }
244 return fmt.Sprintf("%.1f MiB", float64(n)/(1024*1024))
245}