BlueGit

timfa bluegit

Files

bluegit / internal / server / branches.go

branches.go

feat: geef de branches- en tagspagina de GitLab-opbouw
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Passed 6e85d750 History
branches.go 6.2 KiB Download Raw
1package server
2
3import (
4 "net/http"
5 "net/url"
6 "sort"
7 "strconv"
8 "strings"
9 "time"
10
11 "gitlab.com/Timfa/bluegit/internal/access"
12 "gitlab.com/Timfa/bluegit/internal/gitrepo"
13 "gitlab.com/Timfa/bluegit/internal/i18n"
14 "gitlab.com/Timfa/bluegit/internal/names"
15 "gitlab.com/Timfa/bluegit/internal/store"
16)
17
18// Een branch zonder commit in drie maanden is inactief, zoals bij GitLab.
19const branchStaleAfter = 90 * 24 * time.Hour
20
21func (s *Server) branches(w http.ResponseWriter, r *http.Request) {
22 _, repo, role, ok := s.guard(w, r, access.RoleReadonly)
23 if !ok {
24 return
25 }
26 s.absorbMergedMRs(r.Context(), repo)
27 v := s.repoView(w, r, repo, role, "title.branches")
28 v.Page = "branches"
29 v.Wide = true
30 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
31 tips, _ := gitrepo.BranchTips(r.Context(), s.Git, dir)
32 patterns, _ := s.Store.ProtectedPatterns(r.Context(), repo.ID)
33 lang := s.langOf(r)
34 now := time.Now()
35 for _, tip := range tips {
36 when, title := branchWhen(lang, tip.Date)
37 unix, _ := strconv.ParseInt(strings.TrimSpace(tip.Date), 10, 64)
38 row := BranchRow{
39 Name: tip.Name, SHA: tip.SHA, Short: shortSHA(tip.SHA),
40 Subject: tip.Subject, When: when, WhenTitle: title, Unix: unix,
41 Protected: access.BranchProtected(patterns, tip.Name),
42 Default: tip.Name == repo.DefaultBranch,
43 RepoPath: repo.FullPath(),
44 Target: repo.DefaultBranch,
45 ZipURL: branchArchiveURL(repo, tip.Name, "zip"),
46 TarURL: branchArchiveURL(repo, tip.Name, "tar.gz"),
47 }
48 row.CanEdit = role.CanWriteCode() && !row.Default && !row.Protected
49 if !row.Default && repo.DefaultBranch != "" {
50 ahead, behind, err := gitrepo.AheadBehind(r.Context(), s.Git, dir, repo.DefaultBranch, tip.Name)
51 if err == nil {
52 row.Ahead, row.Behind = ahead, behind
53 }
54 }
55 if p, err := s.Store.LatestPipelineByCommit(r.Context(), repo.ID, tip.SHA); err == nil {
56 jobs, _ := s.Store.JobsByPipeline(r.Context(), p.ID)
57 row.PipeID = p.ID
58 row.PipeStatus = displayStatus(p.Status, jobs)
59 row.PipeLabel = s.tr(r, "pipe."+row.PipeStatus)
60 }
61 if mrs, err := s.Store.OpenMergeRequestsBySource(r.Context(), repo.ID, tip.Name); err == nil {
62 for _, mr := range mrs {
63 row.OpenMRs = append(row.OpenMRs, mr.Number)
64 }
65 }
66 v.Branches = append(v.Branches, row)
67 }
68 sort.Slice(v.Branches, func(i, j int) bool {
69 if v.Branches[i].Default != v.Branches[j].Default {
70 return v.Branches[i].Default
71 }
72 if v.Branches[i].Unix != v.Branches[j].Unix {
73 return v.Branches[i].Unix > v.Branches[j].Unix
74 }
75 return v.Branches[i].Name < v.Branches[j].Name
76 })
77 state := branchTab(r.URL.Query().Get("state"))
78 board := &BranchBoard{State: state}
79 for _, row := range v.Branches {
80 if branchIsStale(row.Unix, row.Default, now) {
81 board.Stale = append(board.Stale, row)
82 } else {
83 board.Active = append(board.Active, row)
84 }
85 }
86 switch state {
87 case "active":
88 board.ShowActive = true
89 case "stale":
90 board.ShowStale = true
91 case "all":
92 board.ShowActive, board.ShowStale = true, true
93 default:
94 board.ShowActive = len(board.Active) > 0 || len(board.Stale) == 0
95 board.ShowStale = len(board.Stale) > 0
96 }
97 v.Branch = board
98 tags, _ := gitrepo.Tags(r.Context(), s.Git, dir)
99 for _, name := range tags {
100 v.Tags = append(v.Tags, TagRow{Name: name})
101 }
102 s.render(w, "branches", v)
103}
104
105func (s *Server) branchArchive(w http.ResponseWriter, r *http.Request) {
106 _, repo, _, ok := s.guard(w, r, access.RoleReadonly)
107 if !ok {
108 return
109 }
110 name := r.URL.Query().Get("name")
111 format := r.URL.Query().Get("format")
112 if names.ValidBranch(name) != nil || (format != "zip" && format != "tar.gz") {
113 http.NotFound(w, r)
114 return
115 }
116 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
117 if gitOne(r.Context(), s.Git, dir, "rev-parse", "--verify", "--quiet", "refs/heads/"+name) == "" {
118 http.NotFound(w, r)
119 return
120 }
121 s.writeArchive(w, r, repo, dir, "refs/heads/"+name, name, format)
122}
123
124func (s *Server) createBranch(w http.ResponseWriter, r *http.Request) {
125 u, repo, _, ok := s.guard(w, r, access.RoleDeveloper)
126 if !ok || !s.csrfOK(w, r) {
127 return
128 }
129 back := repoURL(repo) + "/-/branches"
130 if !s.uiOK(w, r, back, s.opCreateBranch(r.Context(), u, repo, r.FormValue("name"), r.FormValue("from"))) {
131 return
132 }
133 s.redirect(w, r, back, s.tr(r, "flash.branchCreated"), "")
134}
135
136func (s *Server) deleteBranch(w http.ResponseWriter, r *http.Request) {
137 u, repo, _, ok := s.guard(w, r, access.RoleDeveloper)
138 if !ok || !s.csrfOK(w, r) {
139 return
140 }
141 back := repoURL(repo) + "/-/branches"
142 if !s.uiOK(w, r, back, s.opDeleteBranch(r.Context(), u, repo, r.FormValue("name"), r.FormValue("confirm"))) {
143 return
144 }
145 s.redirect(w, r, back, s.tr(r, "flash.branchDeleted"), "")
146}
147
148func (s *Server) renameBranch(w http.ResponseWriter, r *http.Request) {
149 u, repo, _, ok := s.guard(w, r, access.RoleDeveloper)
150 if !ok || !s.csrfOK(w, r) {
151 return
152 }
153 back := repoURL(repo) + "/-/branches"
154 if !s.uiOK(w, r, back, s.opRenameBranch(r.Context(), u, repo, r.FormValue("name"), r.FormValue("new_name"), r.FormValue("confirm"))) {
155 return
156 }
157 s.redirect(w, r, back, s.tr(r, "flash.branchRenamed"), "")
158}
159
160func branchTab(raw string) string {
161 switch raw {
162 case "active", "stale", "all":
163 return raw
164 default:
165 return "overview"
166 }
167}
168
169func branchIsStale(unix int64, isDefault bool, now time.Time) bool {
170 if isDefault || unix <= 0 {
171 return false
172 }
173 return now.Sub(time.Unix(unix, 0)) > branchStaleAfter
174}
175
176func branchArchiveURL(repo interface{ FullPath() string }, name, format string) string {
177 return "/" + repo.FullPath() + "/-/branches/archive?name=" + url.QueryEscape(name) + "&format=" + format
178}
179
180func branchWhen(lang i18n.Lang, unixText string) (short, title string) {
181 unix, err := strconv.ParseInt(strings.TrimSpace(unixText), 10, 64)
182 if err != nil || unix <= 0 {
183 return "", ""
184 }
185 at := time.Unix(unix, 0).UTC()
186 return i18n.Ago(lang, unix), at.Format("2006-01-02 15:04 UTC")
187}
188
189// branchLocked weigert de standaardbranch en elke beschermde branch, ook voor een beheerder.
190func (s *Server) branchLocked(r *http.Request, repo store.Repo, name string) (bool, string) {
191 if name == repo.DefaultBranch {
192 return true, s.tr(r, "err.branchLocked", name)
193 }
194 patterns, _ := s.Store.ProtectedPatterns(r.Context(), repo.ID)
195 if access.BranchProtected(patterns, name) {
196 return true, s.tr(r, "err.branchLocked", name)
197 }
198 return false, ""
199}