| 1 | package server |
| 2 | |
| 3 | import ( |
| 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 | |
| 20 | func (s *Server) fillOverview(r *http.Request, v *View, repo store.Repo) { |
| 21 | v.Page = "overview" |
| 22 | s.branchList(r, v, repo) |
| 23 | lang := i18n.Parse(v.Lang) |
| 24 | if c, err := s.cLastCommit(r.Context(), repo, v.Rev, ""); err == nil && c.SHA != "" { |
| 25 | if rows := s.presentCommits(r.Context(), repo.ID, []gitrepo.Commit{c}, lang); len(rows) > 0 { |
| 26 | s.attachHeadPipeline(r.Context(), repo, &rows[0], lang) |
| 27 | v.Head = &rows[0] |
| 28 | } |
| 29 | } |
| 30 | entries, err := s.cTree(r.Context(), repo, v.Rev, "") |
| 31 | touch, _ := s.cTouch(r.Context(), repo, v.Rev) |
| 32 | if err == nil { |
| 33 | v.Files = fileRows(entries, "", touch, lang) |
| 34 | } |
| 35 | readme, license := findDocs(entries) |
| 36 | v.LicensePath = license |
| 37 | if readme != "" { |
| 38 | text, binary, err := s.cDoc(r.Context(), repo, v.Rev, readme, 200_000) |
| 39 | if err == nil && !binary { |
| 40 | v.ReadmeName = readme |
| 41 | base := "/" + repo.FullPath() + "/-/blob/" |
| 42 | v.Readme = template.HTML(markdown.RenderTasks(text, base, v.Rev, "/"+repo.FullPath(), markdown.Tasks{Count: s.tr(r, "tpl.tasks")})) |
| 43 | } |
| 44 | } |
| 45 | commits, branches, tags, _ := s.cStats(r.Context(), repo, v.Rev) |
| 46 | v.NCommits, v.NBranches, v.NTags = commits, branches, tags |
| 47 | if rels, err := s.Store.ListReleases(r.Context(), repo.ID); err == nil { |
| 48 | v.NReleases = len(rels) |
| 49 | } |
| 50 | v.Created = i18n.FormatDate(lang, repo.CreatedAt) |
| 51 | } |
| 52 | |
| 53 | func (s *Server) fillFiles(r *http.Request, v *View, repo store.Repo, rel string, isFile bool) { |
| 54 | v.Page = "files" |
| 55 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 56 | lang := i18n.Parse(v.Lang) |
| 57 | s.branchList(r, v, repo) |
| 58 | all, _ := s.cTreeAll(r.Context(), repo, v.Rev) |
| 59 | v.Tree = treeRows(all, rel) |
| 60 | if !isFile { |
| 61 | v.TreePath = rel |
| 62 | entries, err := s.cTree(r.Context(), repo, v.Rev, rel) |
| 63 | touch, _ := s.cTouch(r.Context(), repo, v.Rev) |
| 64 | if err == nil { |
| 65 | v.Files = fileRows(entries, rel, touch, lang) |
| 66 | } |
| 67 | return |
| 68 | } |
| 69 | v.BlobPath = rel |
| 70 | v.FileName = path.Base(rel) |
| 71 | text, binary, err := gitrepo.Blob(r.Context(), s.Git, dir, v.Rev, rel, 200_000) |
| 72 | if err != nil { |
| 73 | return |
| 74 | } |
| 75 | v.Binary = binary |
| 76 | if !binary { |
| 77 | for i, line := range highlight.Lines(rel, text) { |
| 78 | v.Lines = append(v.Lines, CodeLine{N: i + 1, HTML: template.HTML(line)}) |
| 79 | } |
| 80 | } |
| 81 | if n, err := gitrepo.BlobSize(r.Context(), s.Git, dir, v.Rev, rel); err == nil { |
| 82 | v.BlobSize = formatSize(n) |
| 83 | } |
| 84 | if c, err := s.cLastCommit(r.Context(), repo, v.Rev, rel); err == nil && c.SHA != "" { |
| 85 | if rows := s.presentCommits(r.Context(), repo.ID, []gitrepo.Commit{c}, lang); len(rows) > 0 { |
| 86 | s.attachHeadPipeline(r.Context(), repo, &rows[0], lang) |
| 87 | v.Head = &rows[0] |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func (s *Server) branchList(r *http.Request, v *View, repo store.Repo) { |
| 93 | patterns, _ := s.Store.ProtectedPatterns(r.Context(), v.Repo.ID) |
| 94 | v.RevProtected = access.BranchProtected(patterns, v.Rev) |
| 95 | names, _ := s.cBranches(r.Context(), repo) |
| 96 | seen := false |
| 97 | for _, name := range names { |
| 98 | if name == v.Rev { |
| 99 | seen = true |
| 100 | } |
| 101 | v.Branches = append(v.Branches, BranchRow{Name: name, Protected: access.BranchProtected(patterns, name)}) |
| 102 | } |
| 103 | if v.Rev != "" && !seen { |
| 104 | v.Branches = append([]BranchRow{{Name: v.Rev, Protected: v.RevProtected}}, v.Branches...) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | func (s *Server) attachHeadPipeline(ctx context.Context, repo store.Repo, row *CommitRow, lang i18n.Lang) { |
| 109 | if row == nil || row.SHA == "" || s.Store == nil { |
| 110 | return |
| 111 | } |
| 112 | p, err := s.Store.LatestPipelineByCommit(ctx, repo.ID, row.SHA) |
| 113 | if err != nil { |
| 114 | return |
| 115 | } |
| 116 | jobs, _ := s.Store.JobsByPipeline(ctx, p.ID) |
| 117 | status := displayStatus(s.pipeStatusFor(p), jobs) |
| 118 | row.PipeID = p.ID |
| 119 | row.PipeStatus = status |
| 120 | row.PipeLabel = i18n.T(lang, "pipe."+status) |
| 121 | row.PipeDuration = pipelineClock(p) |
| 122 | tr := func(key string, args ...any) string { return i18n.T(lang, key, args...) } |
| 123 | row.PipeStages = stageViews(jobs, s.queueRanks(ctx), tr) |
| 124 | row.PipeLive = jobsLive(p.Status, jobs) |
| 125 | row.PipeFailed = jobsFailed(jobs) |
| 126 | } |
| 127 | |
| 128 | func 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 | |
| 152 | func 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 | |
| 177 | func 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 | |
| 207 | func 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 | |
| 231 | func 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 | } |