BlueGit

timfa bluegit

Files

bluegit / internal / server / project.go

project.go

feat: koppel commits aan wie ze pushte
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Failed 6c3f521b History
project.go 6.3 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 (s *Server) tags(w http.ResponseWriter, r *http.Request) {
107 _, repo, role, ok := s.guard(w, r, access.RoleReadonly)
108 if !ok {
109 return
110 }
111 v := s.repoView(w, r, repo, role, "title.tags")
112 v.Wide = true
113 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
114 v.TagNames, _ = gitrepo.Tags(r.Context(), s.Git, dir)
115 s.render(w, "tags", v)
116}
117
118func fileRows(entries []gitrepo.Entry, base string, touch map[string]gitrepo.Commit, lang i18n.Lang) []FileRow {
119 var out []FileRow
120 for _, e := range entries {
121 p := e.Name
122 if base != "" {
123 p = base + "/" + e.Name
124 }
125 row := FileRow{Name: e.Name, Path: p, Dir: e.Type == "tree", Icon: fileIcon(e.Name, e.Type)}
126 if c, ok := touch[p]; ok {
127 row.Subject = c.Subject
128 row.When = i18n.Ago(lang, c.Unix)
129 row.SHA = c.SHA
130 }
131 out = append(out, row)
132 }
133 sort.Slice(out, func(i, j int) bool {
134 if out[i].Dir != out[j].Dir {
135 return out[i].Dir
136 }
137 return strings.ToLower(out[i].Name) < strings.ToLower(out[j].Name)
138 })
139 return out
140}
141
142func treeRows(entries []gitrepo.Entry, current string) []FileRow {
143 var out []FileRow
144 for _, e := range entries {
145 base := e.Name
146 if i := strings.LastIndex(e.Name, "/"); i >= 0 {
147 base = e.Name[i+1:]
148 }
149 depth := strings.Count(e.Name, "/")
150 if depth > 8 {
151 depth = 8
152 }
153 active := e.Name == current
154 if e.Type == "tree" && (current == e.Name || strings.HasPrefix(current, e.Name+"/")) {
155 active = true
156 }
157 out = append(out, FileRow{
158 Name: base, Path: e.Name, Dir: e.Type == "tree", Icon: fileIcon(base, e.Type),
159 Depth: depth, Active: active,
160 })
161 }
162 return out
163}
164
165func findDocs(entries []gitrepo.Entry) (readme, license string) {
166 readmeRank := 9
167 for _, e := range entries {
168 if e.Type != "blob" {
169 continue
170 }
171 low := strings.ToLower(e.Name)
172 rank := 9
173 switch low {
174 case "readme.md":
175 rank = 0
176 case "readme.markdown":
177 rank = 1
178 case "readme":
179 rank = 2
180 }
181 if rank < readmeRank {
182 readmeRank = rank
183 readme = e.Name
184 }
185 switch low {
186 case "license", "license.md", "license.txt", "licence", "copying":
187 if license == "" || low == "license" {
188 license = e.Name
189 }
190 }
191 }
192 return readme, license
193}
194
195func fileIcon(name, typ string) string {
196 if typ == "tree" {
197 return "dir"
198 }
199 switch strings.ToLower(path.Ext(name)) {
200 case ".md", ".markdown":
201 return "md"
202 case ".png", ".jpg", ".jpeg", ".gif", ".svg", ".webp":
203 return "img"
204 case ".go", ".js", ".ts", ".py", ".rs", ".c", ".h", ".rb", ".sh":
205 return "code"
206 case ".yml", ".yaml", ".toml", ".json", ".lock":
207 return "cfg"
208 }
209 base := strings.ToLower(name)
210 if base == "license" || base == "licence" || base == "copying" || strings.HasPrefix(base, "license.") {
211 return "lic"
212 }
213 if strings.HasPrefix(base, "readme") {
214 return "md"
215 }
216 return "file"
217}
218
219func formatSize(n int64) string {
220 if n < 1024 {
221 return fmt.Sprintf("%d B", n)
222 }
223 if n < 1024*1024 {
224 return fmt.Sprintf("%.1f KiB", float64(n)/1024)
225 }
226 return fmt.Sprintf("%.1f MiB", float64(n)/(1024*1024))
227}