BlueGit

timfa bluegit

Files

bluegit / internal / server / branches.go

branches.go

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