BlueGit

timfa bluegit

Files

bluegit / internal / server / project.go

project.go

feat: voeg een tagspagina per repository toe
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
2ec9a856 History
project.go 5.9 KiB Download Raw
1package server
2
3import (
4 "fmt"
5 "html/template"
6 "net/http"
7 "path"
8 "sort"
9 "strings"
10
11 "gitlab.com/Timfa/bluegit/internal/access"
12 "gitlab.com/Timfa/bluegit/internal/gitrepo"
13 "gitlab.com/Timfa/bluegit/internal/highlight"
14 "gitlab.com/Timfa/bluegit/internal/i18n"
15 "gitlab.com/Timfa/bluegit/internal/markdown"
16 "gitlab.com/Timfa/bluegit/internal/store"
17)
18
19func (s *Server) fillOverview(r *http.Request, v *View, repo store.Repo) {
20 v.Page = "overview"
21 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
22 s.branchList(r, v, dir)
23 lang := i18n.Parse(v.Lang)
24 if c, err := gitrepo.LastCommit(r.Context(), s.Git, dir, v.Rev, ""); err == nil && c.SHA != "" {
25 if rows := s.presentCommits(r.Context(), repo.ID, []gitrepo.Commit{c}, lang); len(rows) > 0 {
26 v.Head = &rows[0]
27 }
28 }
29 entries, err := gitrepo.Tree(r.Context(), s.Git, dir, v.Rev, "")
30 touch, _ := gitrepo.Touch(r.Context(), s.Git, dir, v.Rev)
31 if err == nil {
32 v.Files = fileRows(entries, "", touch, lang)
33 }
34 readme, license := findDocs(entries)
35 v.LicensePath = license
36 if readme != "" {
37 text, binary, err := gitrepo.Blob(r.Context(), s.Git, dir, v.Rev, readme, 200_000)
38 if err == nil && !binary {
39 v.ReadmeName = readme
40 base := "/" + repo.FullPath() + "/-/blob/"
41 v.Readme = template.HTML(markdown.Render(text, base, v.Rev))
42 }
43 }
44 commits, branches, tags, _ := gitrepo.Stats(r.Context(), s.Git, dir, v.Rev)
45 v.NCommits, v.NBranches, v.NTags = commits, branches, tags
46 if rels, err := s.Store.ListReleases(r.Context(), repo.ID); err == nil {
47 v.NReleases = len(rels)
48 }
49 v.Created = i18n.FormatDate(lang, repo.CreatedAt)
50}
51
52func (s *Server) fillFiles(r *http.Request, v *View, repo store.Repo, rel string, isFile bool) {
53 v.Page = "files"
54 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
55 lang := i18n.Parse(v.Lang)
56 s.branchList(r, v, dir)
57 all, _ := gitrepo.TreeAll(r.Context(), s.Git, dir, v.Rev)
58 v.Tree = treeRows(all, rel)
59 if !isFile {
60 v.TreePath = rel
61 entries, err := gitrepo.Tree(r.Context(), s.Git, dir, v.Rev, rel)
62 touch, _ := gitrepo.Touch(r.Context(), s.Git, dir, v.Rev)
63 if err == nil {
64 v.Files = fileRows(entries, rel, touch, lang)
65 }
66 return
67 }
68 v.BlobPath = rel
69 v.FileName = path.Base(rel)
70 text, binary, err := gitrepo.Blob(r.Context(), s.Git, dir, v.Rev, rel, 200_000)
71 if err != nil {
72 return
73 }
74 v.Binary = binary
75 if !binary {
76 for i, line := range highlight.Lines(rel, text) {
77 v.Lines = append(v.Lines, CodeLine{N: i + 1, HTML: template.HTML(line)})
78 }
79 }
80 if n, err := gitrepo.BlobSize(r.Context(), s.Git, dir, v.Rev, rel); err == nil {
81 v.BlobSize = formatSize(n)
82 }
83 if c, err := gitrepo.LastCommit(r.Context(), s.Git, dir, v.Rev, rel); err == nil && c.SHA != "" {
84 if rows := s.presentCommits(r.Context(), repo.ID, []gitrepo.Commit{c}, lang); len(rows) > 0 {
85 v.Head = &rows[0]
86 }
87 }
88}
89
90func (s *Server) branchList(r *http.Request, v *View, dir string) {
91 patterns, _ := s.Store.ProtectedPatterns(r.Context(), v.Repo.ID)
92 v.RevProtected = access.BranchProtected(patterns, v.Rev)
93 names, _ := gitrepo.Branches(r.Context(), s.Git, dir)
94 seen := false
95 for _, name := range names {
96 if name == v.Rev {
97 seen = true
98 }
99 v.Branches = append(v.Branches, BranchRow{Name: name, Protected: access.BranchProtected(patterns, name)})
100 }
101 if v.Rev != "" && !seen {
102 v.Branches = append([]BranchRow{{Name: v.Rev, Protected: v.RevProtected}}, v.Branches...)
103 }
104}
105
106func fileRows(entries []gitrepo.Entry, base string, touch map[string]gitrepo.Commit, lang i18n.Lang) []FileRow {
107 var out []FileRow
108 for _, e := range entries {
109 p := e.Name
110 if base != "" {
111 p = base + "/" + e.Name
112 }
113 row := FileRow{Name: e.Name, Path: p, Dir: e.Type == "tree", Icon: fileIcon(e.Name, e.Type)}
114 if c, ok := touch[p]; ok {
115 row.Subject = c.Subject
116 row.When = i18n.Ago(lang, c.Unix)
117 row.SHA = c.SHA
118 }
119 out = append(out, row)
120 }
121 sort.Slice(out, func(i, j int) bool {
122 if out[i].Dir != out[j].Dir {
123 return out[i].Dir
124 }
125 return strings.ToLower(out[i].Name) < strings.ToLower(out[j].Name)
126 })
127 return out
128}
129
130func treeRows(entries []gitrepo.Entry, current string) []FileRow {
131 var out []FileRow
132 for _, e := range entries {
133 base := e.Name
134 if i := strings.LastIndex(e.Name, "/"); i >= 0 {
135 base = e.Name[i+1:]
136 }
137 depth := strings.Count(e.Name, "/")
138 if depth > 8 {
139 depth = 8
140 }
141 active := e.Name == current
142 if e.Type == "tree" && (current == e.Name || strings.HasPrefix(current, e.Name+"/")) {
143 active = true
144 }
145 out = append(out, FileRow{
146 Name: base, Path: e.Name, Dir: e.Type == "tree", Icon: fileIcon(base, e.Type),
147 Depth: depth, Active: active,
148 })
149 }
150 return out
151}
152
153func findDocs(entries []gitrepo.Entry) (readme, license string) {
154 readmeRank := 9
155 for _, e := range entries {
156 if e.Type != "blob" {
157 continue
158 }
159 low := strings.ToLower(e.Name)
160 rank := 9
161 switch low {
162 case "readme.md":
163 rank = 0
164 case "readme.markdown":
165 rank = 1
166 case "readme":
167 rank = 2
168 }
169 if rank < readmeRank {
170 readmeRank = rank
171 readme = e.Name
172 }
173 switch low {
174 case "license", "license.md", "license.txt", "licence", "copying":
175 if license == "" || low == "license" {
176 license = e.Name
177 }
178 }
179 }
180 return readme, license
181}
182
183func fileIcon(name, typ string) string {
184 if typ == "tree" {
185 return "dir"
186 }
187 switch strings.ToLower(path.Ext(name)) {
188 case ".md", ".markdown":
189 return "md"
190 case ".png", ".jpg", ".jpeg", ".gif", ".svg", ".webp":
191 return "img"
192 case ".go", ".js", ".ts", ".py", ".rs", ".c", ".h", ".rb", ".sh":
193 return "code"
194 case ".yml", ".yaml", ".toml", ".json", ".lock":
195 return "cfg"
196 }
197 base := strings.ToLower(name)
198 if base == "license" || base == "licence" || base == "copying" || strings.HasPrefix(base, "license.") {
199 return "lic"
200 }
201 if strings.HasPrefix(base, "readme") {
202 return "md"
203 }
204 return "file"
205}
206
207func formatSize(n int64) string {
208 if n < 1024 {
209 return fmt.Sprintf("%d B", n)
210 }
211 if n < 1024*1024 {
212 return fmt.Sprintf("%.1f KiB", float64(n)/1024)
213 }
214 return fmt.Sprintf("%.1f MiB", float64(n)/(1024*1024))
215}