BlueGit

timfa bluegit

Files

bluegit / internal / server / ops_git.go

ops_git.go

ops_git.go 8.2 KiB Download Raw
1package server
2
3import (
4 "context"
5 "net/http"
6 "net/url"
7 "os"
8 "strings"
9
10 "gitlab.com/Timfa/bluegit/internal/access"
11 "gitlab.com/Timfa/bluegit/internal/gitrepo"
12 "gitlab.com/Timfa/bluegit/internal/i18n"
13 "gitlab.com/Timfa/bluegit/internal/names"
14 "gitlab.com/Timfa/bluegit/internal/store"
15)
16
17// decodeBranch maakt een branchnaam uit een pad of een bevestiging.
18// ServeMux decodeert %2F één keer. Een client die de slash nóg een keer
19// encodeert (%252F) levert anders cursor%2F… en de bevestiging wijkt af.
20func decodeBranch(raw string) string {
21 name := strings.TrimSpace(raw)
22 for i := 0; i < 3; i++ {
23 next, err := url.PathUnescape(name)
24 if err != nil || next == name {
25 return name
26 }
27 name = next
28 }
29 return name
30}
31
32// pathWildcard leest een ServeMux-wildcard. De mux matcht op de escaped path
33// (EscapedPath/RawPath), zodat zowel feature/x als feature%2Fx bij {name...}
34// horen. PathValue laat %2F ongecodeerd; decodeBranch maakt daar een taknaam van.
35func pathWildcard(r *http.Request, key string) string {
36 return decodeBranch(r.PathValue(key))
37}
38
39func (s *Server) repoRole(ctx context.Context, user store.User, repo store.Repo) (access.Role, error) {
40 role, err := s.Store.EffectiveRole(ctx, user.ID, repo.ID)
41 if err != nil {
42 return "", fail(500, err)
43 }
44 return role, nil
45}
46
47func (s *Server) opCreateBranch(ctx context.Context, user store.User, repo store.Repo, name, from string) error {
48 role, err := s.repoRole(ctx, user, repo)
49 if err != nil {
50 return err
51 }
52 if !role.CanWriteCode() {
53 return fail(403, i18n.E("err.forbidden"))
54 }
55 name = strings.TrimSpace(name)
56 if err := names.ValidBranch(name); err != nil {
57 return fail(400, err)
58 }
59 from = safeRev(from, repo.DefaultBranch)
60 if from == "" {
61 return fail(400, i18n.E("err.branches"))
62 }
63 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
64 if gitOne(ctx, s.Git, dir, "rev-parse", "--verify", "--quiet", "refs/heads/"+name) != "" {
65 return fail(400, i18n.E("err.branchName", name))
66 }
67 if err := s.postReceive(ctx, repo, user, dir, func() error {
68 return gitrepo.CreateBranch(ctx, s.Git, dir, name, from)
69 }); err != nil {
70 return fail(400, err)
71 }
72 return nil
73}
74
75func (s *Server) branchLockError(ctx context.Context, repo store.Repo, name string) error {
76 if name == repo.DefaultBranch {
77 return i18n.E("err.branchLocked", name)
78 }
79 patterns, _ := s.Store.ProtectedPatterns(ctx, repo.ID)
80 if access.BranchProtected(patterns, name) {
81 return i18n.E("err.branchLocked", name)
82 }
83 return nil
84}
85
86func (s *Server) opDeleteBranch(ctx context.Context, user store.User, repo store.Repo, name, confirm string) error {
87 role, err := s.repoRole(ctx, user, repo)
88 if err != nil {
89 return err
90 }
91 if !role.CanWriteCode() {
92 return fail(403, i18n.E("err.forbidden"))
93 }
94 name = decodeBranch(name)
95 confirm = decodeBranch(confirm)
96 if confirm != name || name == "" {
97 return fail(400, i18n.E("err.branchConfirm"))
98 }
99 if err := s.branchLockError(ctx, repo, name); err != nil {
100 return fail(400, err)
101 }
102 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
103 if err := s.postReceive(ctx, repo, user, dir, func() error {
104 return gitrepo.DeleteBranch(ctx, s.Git, dir, name)
105 }); err != nil {
106 return fail(400, err)
107 }
108 return nil
109}
110
111func (s *Server) opRenameBranch(ctx context.Context, user store.User, repo store.Repo, name, newName, confirm string) error {
112 role, err := s.repoRole(ctx, user, repo)
113 if err != nil {
114 return err
115 }
116 if !role.CanWriteCode() {
117 return fail(403, i18n.E("err.forbidden"))
118 }
119 name = decodeBranch(name)
120 newName = decodeBranch(newName)
121 confirm = decodeBranch(confirm)
122 if confirm != name || name == "" {
123 return fail(400, i18n.E("err.branchConfirm"))
124 }
125 if err := names.ValidBranch(newName); err != nil {
126 return fail(400, err)
127 }
128 if err := s.branchLockError(ctx, repo, name); err != nil {
129 return fail(400, err)
130 }
131 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
132 if gitOne(ctx, s.Git, dir, "rev-parse", "--verify", "--quiet", "refs/heads/"+newName) != "" {
133 return fail(400, i18n.E("err.branchName", newName))
134 }
135 if err := s.postReceive(ctx, repo, user, dir, func() error {
136 if err := gitrepo.RenameBranch(ctx, s.Git, dir, name, newName); err != nil {
137 return err
138 }
139 return s.Store.RenameOpenMRBranches(ctx, repo.ID, name, newName)
140 }); err != nil {
141 return fail(400, err)
142 }
143 return nil
144}
145
146func (s *Server) opCreateTag(ctx context.Context, user store.User, repo store.Repo, name, target, message string) error {
147 role, err := s.repoRole(ctx, user, repo)
148 if err != nil {
149 return err
150 }
151 if !role.CanWriteCode() {
152 return fail(403, i18n.E("err.forbidden"))
153 }
154 name = strings.TrimSpace(name)
155 if err := names.ValidTag(name); err != nil {
156 return fail(400, err)
157 }
158 patterns, _ := s.Store.ProtectedTagPatterns(ctx, repo.ID)
159 if access.BranchProtected(patterns, name) && !role.CanManageSettings() {
160 return fail(403, i18n.E("err.tagProtected", name))
161 }
162 if strings.TrimSpace(target) == "" {
163 target = repo.DefaultBranch
164 }
165 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
166 if err := s.postReceive(ctx, repo, user, dir, func() error {
167 return gitrepo.CreateTag(ctx, s.Git, dir, name, target, message, user.Username, userEmail(user.Username))
168 }); err != nil {
169 return fail(400, err)
170 }
171 return nil
172}
173
174func (s *Server) opDeleteTag(ctx context.Context, user store.User, repo store.Repo, name string) error {
175 role, err := s.repoRole(ctx, user, repo)
176 if err != nil {
177 return err
178 }
179 if !role.CanWriteCode() {
180 return fail(403, i18n.E("err.forbidden"))
181 }
182 name = strings.TrimSpace(name)
183 patterns, _ := s.Store.ProtectedTagPatterns(ctx, repo.ID)
184 if access.BranchProtected(patterns, name) && !role.CanManageSettings() {
185 return fail(403, i18n.E("err.tagProtected", name))
186 }
187 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
188 if err := gitrepo.DeleteTag(ctx, s.Git, dir, name); err != nil {
189 return fail(400, err)
190 }
191 s.refsChanged(repo)
192 return nil
193}
194
195type fileAction struct {
196 Action string `json:"action"`
197 Path string `json:"file_path"`
198 Content string `json:"content"`
199}
200
201func (s *Server) opWriteFiles(ctx context.Context, user store.User, repo store.Repo, role access.Role, branch, base, title, description string, files map[string]string, deletes []string) (string, error) {
202 if role == "" {
203 var err error
204 role, err = s.repoRole(ctx, user, repo)
205 if err != nil {
206 return "", err
207 }
208 }
209 if !role.CanWriteCode() {
210 return "", fail(403, i18n.E("err.forbidden"))
211 }
212 branch = strings.TrimSpace(branch)
213 if !validBranchName(branch) {
214 return "", fail(400, i18n.E("edit.badBranch"))
215 }
216 title = strings.TrimSpace(title)
217 if title == "" || strings.ContainsAny(title, "\r\n") {
218 return "", fail(400, i18n.E("edit.needMessage"))
219 }
220 if base == "" {
221 base = repo.DefaultBranch
222 }
223 patterns, _ := s.Store.ProtectedPatterns(ctx, repo.ID)
224 if access.BranchProtected(patterns, branch) && !role.CanPushBranch(true) {
225 return "", fail(403, i18n.E("edit.protected"))
226 }
227 if s.StoragePath() == "" {
228 return "", fail(503, i18n.E("err.noStorage"))
229 }
230 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
231 work, err := s.scratchDir("api-commit")
232 if err != nil {
233 return "", fail(500, err)
234 }
235 defer os.RemoveAll(work)
236 if err := s.ensureBranch(ctx, dir, branch, base, work); err != nil {
237 return "", fail(400, err)
238 }
239 if err := s.postReceive(ctx, repo, user, dir, func() error {
240 return gitrepo.CommitChanges(ctx, s.Git, dir, work, branch, user.Username, userEmail(user.Username), title, description, files, deletes, role, patterns)
241 }); err != nil {
242 return "", fail(400, err)
243 }
244 return gitOne(ctx, s.Git, dir, "rev-parse", "--verify", "refs/heads/"+branch), nil
245}
246
247func (s *Server) opCommitActions(ctx context.Context, user store.User, repo store.Repo, branch, base, title, description string, actions []fileAction) (string, error) {
248 files := map[string]string{}
249 var deletes []string
250 for _, a := range actions {
251 path, ok := repoPath(a.Path)
252 if !ok || path == "" {
253 return "", fail(400, i18n.E("err.sugFile"))
254 }
255 switch a.Action {
256 case "", "create", "update":
257 files[path] = a.Content
258 case "delete":
259 deletes = append(deletes, path)
260 default:
261 return "", fail(400, i18n.E("err.sugFile"))
262 }
263 }
264 return s.opWriteFiles(ctx, user, repo, "", branch, base, title, description, files, deletes)
265}