BlueGit

timfa bluegit

Files

bluegit / internal / server / project.go

project.go

feat: klap mappen in de bestandsboom in
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Passed f3820e63 History
project.go 6.8 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 row.PipeDuration = pipelineClock(p)
123 tr := func(key string, args ...any) string { return i18n.T(lang, key, args...) }
124 row.PipeStages = stageViews(jobs, s.queueRanks(ctx), tr)
125 row.PipeLive = jobsLive(p.Status, jobs)
126}
127
128func fileRows(entries []gitrepo.Entry, base string, touch map[string]gitrepo.Commit, lang i18n.Lang) []FileRow {
129 var out []FileRow
130 for _, e := range entries {
131 p := e.Name
132 if base != "" {
133 p = base + "/" + e.Name
134 }
135 row := FileRow{Name: e.Name, Path: p, Dir: e.Type == "tree", Icon: fileIcon(e.Name, e.Type)}
136 if c, ok := touch[p]; ok {
137 row.Subject = c.Subject
138 row.When = i18n.Ago(lang, c.Unix)
139 row.SHA = c.SHA
140 }
141 out = append(out, row)
142 }
143 sort.Slice(out, func(i, j int) bool {
144 if out[i].Dir != out[j].Dir {
145 return out[i].Dir
146 }
147 return strings.ToLower(out[i].Name) < strings.ToLower(out[j].Name)
148 })
149 return out
150}
151
152func treeRows(entries []gitrepo.Entry, current string) []FileRow {
153 var out []FileRow
154 for _, e := range entries {
155 base := e.Name
156 if i := strings.LastIndex(e.Name, "/"); i >= 0 {
157 base = e.Name[i+1:]
158 }
159 depth := strings.Count(e.Name, "/")
160 if depth > 8 {
161 depth = 8
162 }
163 dir := e.Type == "tree"
164 inside := current == e.Name || strings.HasPrefix(current, e.Name+"/")
165 active := e.Name == current
166 if dir && inside {
167 active = true
168 }
169 out = append(out, FileRow{
170 Name: base, Path: e.Name, Dir: dir, Icon: fileIcon(base, e.Type),
171 Depth: depth, Active: active, Open: dir && inside,
172 })
173 }
174 return out
175}
176
177func findDocs(entries []gitrepo.Entry) (readme, license string) {
178 readmeRank := 9
179 for _, e := range entries {
180 if e.Type != "blob" {
181 continue
182 }
183 low := strings.ToLower(e.Name)
184 rank := 9
185 switch low {
186 case "readme.md":
187 rank = 0
188 case "readme.markdown":
189 rank = 1
190 case "readme":
191 rank = 2
192 }
193 if rank < readmeRank {
194 readmeRank = rank
195 readme = e.Name
196 }
197 switch low {
198 case "license", "license.md", "license.txt", "licence", "copying":
199 if license == "" || low == "license" {
200 license = e.Name
201 }
202 }
203 }
204 return readme, license
205}
206
207func fileIcon(name, typ string) string {
208 if typ == "tree" {
209 return "dir"
210 }
211 switch strings.ToLower(path.Ext(name)) {
212 case ".md", ".markdown":
213 return "md"
214 case ".png", ".jpg", ".jpeg", ".gif", ".svg", ".webp":
215 return "img"
216 case ".go", ".js", ".ts", ".py", ".rs", ".c", ".h", ".rb", ".sh":
217 return "code"
218 case ".yml", ".yaml", ".toml", ".json", ".lock":
219 return "cfg"
220 }
221 base := strings.ToLower(name)
222 if base == "license" || base == "licence" || base == "copying" || strings.HasPrefix(base, "license.") {
223 return "lic"
224 }
225 if strings.HasPrefix(base, "readme") {
226 return "md"
227 }
228 return "file"
229}
230
231func formatSize(n int64) string {
232 if n < 1024 {
233 return fmt.Sprintf("%d B", n)
234 }
235 if n < 1024*1024 {
236 return fmt.Sprintf("%.1f KiB", float64(n)/1024)
237 }
238 return fmt.Sprintf("%.1f MiB", float64(n)/(1024*1024))
239}