| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "sort" |
| 6 | "strings" |
| 7 | |
| 8 | "gitlab.com/Timfa/bluegit/internal/access" |
| 9 | "gitlab.com/Timfa/bluegit/internal/gitrepo" |
| 10 | "gitlab.com/Timfa/bluegit/internal/names" |
| 11 | "gitlab.com/Timfa/bluegit/internal/store" |
| 12 | ) |
| 13 | |
| 14 | func (s *Server) branches(w http.ResponseWriter, r *http.Request) { |
| 15 | _, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 16 | if !ok { |
| 17 | return |
| 18 | } |
| 19 | v := s.repoView(w, r, repo, role, "title.branches") |
| 20 | v.Page = "branches" |
| 21 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 22 | tips, _ := gitrepo.BranchTips(r.Context(), s.Git, dir) |
| 23 | patterns, _ := s.Store.ProtectedPatterns(r.Context(), repo.ID) |
| 24 | for _, tip := range tips { |
| 25 | row := BranchRow{ |
| 26 | Name: tip.Name, SHA: tip.SHA, Short: shortSHA(tip.SHA), |
| 27 | Subject: tip.Subject, When: tip.Date, |
| 28 | Protected: access.BranchProtected(patterns, tip.Name), |
| 29 | Default: tip.Name == repo.DefaultBranch, |
| 30 | } |
| 31 | if !row.Default && repo.DefaultBranch != "" { |
| 32 | ahead, behind, err := gitrepo.AheadBehind(r.Context(), s.Git, dir, repo.DefaultBranch, tip.Name) |
| 33 | if err == nil { |
| 34 | row.Ahead, row.Behind = ahead, behind |
| 35 | } |
| 36 | } |
| 37 | if p, err := s.Store.LatestPipelineByCommit(r.Context(), repo.ID, tip.SHA); err == nil { |
| 38 | jobs, _ := s.Store.JobsByPipeline(r.Context(), p.ID) |
| 39 | row.PipeID = p.ID |
| 40 | row.PipeStatus = displayStatus(p.Status, jobs) |
| 41 | row.PipeLabel = s.tr(r, "pipe."+row.PipeStatus) |
| 42 | } |
| 43 | if mrs, err := s.Store.OpenMergeRequestsBySource(r.Context(), repo.ID, tip.Name); err == nil && len(mrs) > 0 { |
| 44 | row.MRNumber = mrs[0].Number |
| 45 | } |
| 46 | v.Branches = append(v.Branches, row) |
| 47 | } |
| 48 | sort.Slice(v.Branches, func(i, j int) bool { |
| 49 | if v.Branches[i].Default != v.Branches[j].Default { |
| 50 | return v.Branches[i].Default |
| 51 | } |
| 52 | return v.Branches[i].Name < v.Branches[j].Name |
| 53 | }) |
| 54 | tags, _ := gitrepo.Tags(r.Context(), s.Git, dir) |
| 55 | for _, name := range tags { |
| 56 | v.Tags = append(v.Tags, TagRow{Name: name}) |
| 57 | } |
| 58 | s.render(w, "branches", v) |
| 59 | } |
| 60 | |
| 61 | func (s *Server) createBranch(w http.ResponseWriter, r *http.Request) { |
| 62 | u, repo, role, ok := s.guard(w, r, access.RoleDeveloper) |
| 63 | if !ok || !s.csrfOK(w, r) { |
| 64 | return |
| 65 | } |
| 66 | back := repoURL(repo) + "/-/branches" |
| 67 | if !role.CanWriteCode() { |
| 68 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 69 | return |
| 70 | } |
| 71 | name := strings.TrimSpace(r.FormValue("name")) |
| 72 | if err := names.ValidBranch(name); err != nil { |
| 73 | s.redirect(w, r, back, "", s.trErr(r, err)) |
| 74 | return |
| 75 | } |
| 76 | from := safeRev(r.FormValue("from"), repo.DefaultBranch) |
| 77 | if from == "" { |
| 78 | s.redirect(w, r, back, "", s.tr(r, "err.branches")) |
| 79 | return |
| 80 | } |
| 81 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 82 | if gitOne(r.Context(), s.Git, dir, "rev-parse", "--verify", "--quiet", "refs/heads/"+name) != "" { |
| 83 | s.redirect(w, r, back, "", s.tr(r, "err.branchName", name)) |
| 84 | return |
| 85 | } |
| 86 | if err := s.postReceive(r.Context(), repo, u, dir, func() error { |
| 87 | return gitrepo.CreateBranch(r.Context(), s.Git, dir, name, from) |
| 88 | }); err != nil { |
| 89 | s.redirect(w, r, back, "", s.commitProblem(r, err)) |
| 90 | return |
| 91 | } |
| 92 | s.redirect(w, r, back, s.tr(r, "flash.branchCreated"), "") |
| 93 | } |
| 94 | |
| 95 | func (s *Server) deleteBranch(w http.ResponseWriter, r *http.Request) { |
| 96 | u, repo, role, ok := s.guard(w, r, access.RoleDeveloper) |
| 97 | if !ok || !s.csrfOK(w, r) { |
| 98 | return |
| 99 | } |
| 100 | back := repoURL(repo) + "/-/branches" |
| 101 | if !role.CanWriteCode() { |
| 102 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 103 | return |
| 104 | } |
| 105 | name := strings.TrimSpace(r.FormValue("name")) |
| 106 | if r.FormValue("confirm") != name || name == "" { |
| 107 | s.redirect(w, r, back, "", s.tr(r, "err.branchConfirm")) |
| 108 | return |
| 109 | } |
| 110 | if locked, text := s.branchLocked(r, repo, name); locked { |
| 111 | s.redirect(w, r, back, "", text) |
| 112 | return |
| 113 | } |
| 114 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 115 | if err := s.postReceive(r.Context(), repo, u, dir, func() error { |
| 116 | return gitrepo.DeleteBranch(r.Context(), s.Git, dir, name) |
| 117 | }); err != nil { |
| 118 | s.redirect(w, r, back, "", s.commitProblem(r, err)) |
| 119 | return |
| 120 | } |
| 121 | s.redirect(w, r, back, s.tr(r, "flash.branchDeleted"), "") |
| 122 | } |
| 123 | |
| 124 | func (s *Server) renameBranch(w http.ResponseWriter, r *http.Request) { |
| 125 | u, repo, role, ok := s.guard(w, r, access.RoleDeveloper) |
| 126 | if !ok || !s.csrfOK(w, r) { |
| 127 | return |
| 128 | } |
| 129 | back := repoURL(repo) + "/-/branches" |
| 130 | if !role.CanWriteCode() { |
| 131 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 132 | return |
| 133 | } |
| 134 | name := strings.TrimSpace(r.FormValue("name")) |
| 135 | newName := strings.TrimSpace(r.FormValue("new_name")) |
| 136 | if r.FormValue("confirm") != name || name == "" { |
| 137 | s.redirect(w, r, back, "", s.tr(r, "err.branchConfirm")) |
| 138 | return |
| 139 | } |
| 140 | if err := names.ValidBranch(newName); err != nil { |
| 141 | s.redirect(w, r, back, "", s.trErr(r, err)) |
| 142 | return |
| 143 | } |
| 144 | if locked, text := s.branchLocked(r, repo, name); locked { |
| 145 | s.redirect(w, r, back, "", text) |
| 146 | return |
| 147 | } |
| 148 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 149 | if gitOne(r.Context(), s.Git, dir, "rev-parse", "--verify", "--quiet", "refs/heads/"+newName) != "" { |
| 150 | s.redirect(w, r, back, "", s.tr(r, "err.branchName", newName)) |
| 151 | return |
| 152 | } |
| 153 | if err := s.postReceive(r.Context(), repo, u, dir, func() error { |
| 154 | if err := gitrepo.RenameBranch(r.Context(), s.Git, dir, name, newName); err != nil { |
| 155 | return err |
| 156 | } |
| 157 | return s.Store.RenameOpenMRBranches(r.Context(), repo.ID, name, newName) |
| 158 | }); err != nil { |
| 159 | s.redirect(w, r, back, "", s.commitProblem(r, err)) |
| 160 | return |
| 161 | } |
| 162 | s.redirect(w, r, back, s.tr(r, "flash.branchRenamed"), "") |
| 163 | } |
| 164 | |
| 165 | // branchLocked weigert de standaardbranch en elke beschermde branch, ook voor een beheerder. |
| 166 | func (s *Server) branchLocked(r *http.Request, repo store.Repo, name string) (bool, string) { |
| 167 | if name == repo.DefaultBranch { |
| 168 | return true, s.tr(r, "err.branchLocked", name) |
| 169 | } |
| 170 | patterns, _ := s.Store.ProtectedPatterns(r.Context(), repo.ID) |
| 171 | if access.BranchProtected(patterns, name) { |
| 172 | return true, s.tr(r, "err.branchLocked", name) |
| 173 | } |
| 174 | return false, "" |
| 175 | } |