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