BlueGit

timfa bluegit

Files

bluegit / internal / server / project.go

project.go

feat: lees .bluegit-sjablonen en klikbare takenlijsten
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
79722e46 History
project.go 6.5 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 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
23 s.branchList(r, v, dir)
24 lang := i18n.Parse(v.Lang)
25 if c, err := gitrepo.LastCommit(r.Context(), s.Git, dir, 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 := gitrepo.Tree(r.Context(), s.Git, dir, v.Rev, "")
32 touch, _ := gitrepo.Touch(r.Context(), s.Git, dir, 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 := gitrepo.Blob(r.Context(), s.Git, dir, 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, _ := gitrepo.Stats(r.Context(), s.Git, dir, 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 {
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, dir)
59 all, _ := gitrepo.TreeAll(r.Context(), s.Git, dir, v.Rev)
60 v.Tree = treeRows(all, rel)
61 if !isFile {
62 v.TreePath = rel
63 entries, err := gitrepo.Tree(r.Context(), s.Git, dir, v.Rev, rel)
64 touch, _ := gitrepo.Touch(r.Context(), s.Git, dir, 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 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 }
85 if c, err := gitrepo.LastCommit(r.Context(), s.Git, dir, v.Rev, rel); err == nil && c.SHA != "" {
86 if rows := s.presentCommits(r.Context(), repo.ID, []gitrepo.Commit{c}, lang); len(rows) > 0 {
87 s.attachHeadPipeline(r.Context(), repo, &rows[0], lang)
88 v.Head = &rows[0]
89 }
90 }
91}
92
93func (s *Server) branchList(r *http.Request, v *View, dir string) {
94 patterns, _ := s.Store.ProtectedPatterns(r.Context(), v.Repo.ID)
95 v.RevProtected = access.BranchProtected(patterns, v.Rev)
96 names, _ := gitrepo.Branches(r.Context(), s.Git, dir)
97 seen := false
98 for _, name := range names {
99 if name == v.Rev {
100 seen = true
101 }
102 v.Branches = append(v.Branches, BranchRow{Name: name, Protected: access.BranchProtected(patterns, name)})
103 }
104 if v.Rev != "" && !seen {
105 v.Branches = append([]BranchRow{{Name: v.Rev, Protected: v.RevProtected}}, v.Branches...)
106 }
107}
108
109func (s *Server) attachHeadPipeline(ctx context.Context, repo store.Repo, row *CommitRow, lang i18n.Lang) {
110 if row == nil || row.SHA == "" || s.Store == nil {
111 return
112 }
113 p, err := s.Store.LatestPipelineByCommit(ctx, repo.ID, row.SHA)
114 if err != nil {
115 return
116 }
117 jobs, _ := s.Store.JobsByPipeline(ctx, p.ID)
118 status := displayStatus(p.Status, jobs)
119 row.PipeID = p.ID
120 row.PipeStatus = status
121 row.PipeLabel = i18n.T(lang, "pipe."+status)
122}
123
124func fileRows(entries []gitrepo.Entry, base string, touch map[string]gitrepo.Commit, lang i18n.Lang) []FileRow {
125 var out []FileRow
126 for _, e := range entries {
127 p := e.Name
128 if base != "" {
129 p = base + "/" + e.Name
130 }
131 row := FileRow{Name: e.Name, Path: p, Dir: e.Type == "tree", Icon: fileIcon(e.Name, e.Type)}
132 if c, ok := touch[p]; ok {
133 row.Subject = c.Subject
134 row.When = i18n.Ago(lang, c.Unix)
135 row.SHA = c.SHA
136 }
137 out = append(out, row)
138 }
139 sort.Slice(out, func(i, j int) bool {
140 if out[i].Dir != out[j].Dir {
141 return out[i].Dir
142 }
143 return strings.ToLower(out[i].Name) < strings.ToLower(out[j].Name)
144 })
145 return out
146}
147
148func treeRows(entries []gitrepo.Entry, current string) []FileRow {
149 var out []FileRow
150 for _, e := range entries {
151 base := e.Name
152 if i := strings.LastIndex(e.Name, "/"); i >= 0 {
153 base = e.Name[i+1:]
154 }
155 depth := strings.Count(e.Name, "/")
156 if depth > 8 {
157 depth = 8
158 }
159 active := e.Name == current
160 if e.Type == "tree" && (current == e.Name || strings.HasPrefix(current, e.Name+"/")) {
161 active = true
162 }
163 out = append(out, FileRow{
164 Name: base, Path: e.Name, Dir: e.Type == "tree", Icon: fileIcon(base, e.Type),
165 Depth: depth, Active: active,
166 })
167 }
168 return out
169}
170
171func findDocs(entries []gitrepo.Entry) (readme, license string) {
172 readmeRank := 9
173 for _, e := range entries {
174 if e.Type != "blob" {
175 continue
176 }
177 low := strings.ToLower(e.Name)
178 rank := 9
179 switch low {
180 case "readme.md":
181 rank = 0
182 case "readme.markdown":
183 rank = 1
184 case "readme":
185 rank = 2
186 }
187 if rank < readmeRank {
188 readmeRank = rank
189 readme = e.Name
190 }
191 switch low {
192 case "license", "license.md", "license.txt", "licence", "copying":
193 if license == "" || low == "license" {
194 license = e.Name
195 }
196 }
197 }
198 return readme, license
199}
200
201func fileIcon(name, typ string) string {
202 if typ == "tree" {
203 return "dir"
204 }
205 switch strings.ToLower(path.Ext(name)) {
206 case ".md", ".markdown":
207 return "md"
208 case ".png", ".jpg", ".jpeg", ".gif", ".svg", ".webp":
209 return "img"
210 case ".go", ".js", ".ts", ".py", ".rs", ".c", ".h", ".rb", ".sh":
211 return "code"
212 case ".yml", ".yaml", ".toml", ".json", ".lock":
213 return "cfg"
214 }
215 base := strings.ToLower(name)
216 if base == "license" || base == "licence" || base == "copying" || strings.HasPrefix(base, "license.") {
217 return "lic"
218 }
219 if strings.HasPrefix(base, "readme") {
220 return "md"
221 }
222 return "file"
223}
224
225func formatSize(n int64) string {
226 if n < 1024 {
227 return fmt.Sprintf("%d B", n)
228 }
229 if n < 1024*1024 {
230 return fmt.Sprintf("%.1f KiB", float64(n)/1024)
231 }
232 return fmt.Sprintf("%.1f MiB", float64(n)/(1024*1024))
233}