BlueGit

timfa bluegit

Files

bluegit / internal / server / api_git.go

api_git.go

api_git.go 9.6 KiB Download Raw
1package server
2
3import (
4 "net/http"
5 "os/exec"
6 "strings"
7
8 "gitlab.com/Timfa/bluegit/internal/access"
9 "gitlab.com/Timfa/bluegit/internal/gitrepo"
10 "gitlab.com/Timfa/bluegit/internal/i18n"
11)
12
13func (s *Server) apiBranches(w http.ResponseWriter, r *http.Request) {
14 if r.Method == http.MethodDelete || r.Method == http.MethodPut {
15 s.apiBranchNamed(w, r, "", false)
16 return
17 }
18 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
19 if !ok {
20 return
21 }
22 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
23 if r.Method == http.MethodGet {
24 tips, err := gitrepo.BranchTips(r.Context(), s.Git, dir)
25 if err != nil {
26 tips = nil
27 }
28 page, per := pageQuery(r)
29 rows := make([]map[string]any, 0, len(tips))
30 for _, t := range tips {
31 rows = append(rows, map[string]any{
32 "name": t.Name, "commit": t.SHA, "subject": t.Subject, "date": t.Date,
33 "default": t.Name == repo.DefaultBranch,
34 })
35 }
36 writePage(w, r, slicePage(rows, page, per), len(rows), page, per)
37 return
38 }
39 var body struct {
40 Name string `json:"name"`
41 From string `json:"from"`
42 }
43 if err := readJSON(r, &body); err != nil {
44 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
45 return
46 }
47 if !s.apiOK(w, r, s.opCreateBranch(r.Context(), apiUser(r), repo, body.Name, body.From)) {
48 return
49 }
50 writeJSON(w, http.StatusCreated, map[string]any{"name": body.Name})
51}
52
53func (s *Server) apiBranch(w http.ResponseWriter, r *http.Request) {
54 s.apiBranchNamed(w, r, pathWildcard(r, "branch"), true)
55}
56
57// apiBranchNamed verwijdert of hernoemt een branch. nameFromPath is de tak uit
58// het URL-pad; anders komt de naam uit ?name= of uit de JSON-body (name).
59// Die laatste vorm vermijdt een slash in het pad, voor ingresses die %2F weigeren.
60func (s *Server) apiBranchNamed(w http.ResponseWriter, r *http.Request, name string, nameFromPath bool) {
61 repo, _, ok := s.apiRepo(w, r, access.RoleDeveloper)
62 if !ok {
63 return
64 }
65 if r.Method == http.MethodDelete {
66 var body struct {
67 Name string `json:"name"`
68 Confirm string `json:"confirm"`
69 }
70 if err := readJSON(r, &body); err != nil {
71 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
72 return
73 }
74 if !nameFromPath {
75 if name == "" {
76 name = decodeBranch(r.URL.Query().Get("name"))
77 }
78 if name == "" {
79 name = decodeBranch(body.Name)
80 }
81 }
82 confirm := decodeBranch(body.Confirm)
83 if !nameFromPath && confirm == "" {
84 confirm = name
85 }
86 if !s.apiOK(w, r, s.opDeleteBranch(r.Context(), apiUser(r), repo, name, confirm)) {
87 return
88 }
89 writeJSON(w, http.StatusNoContent, nil)
90 return
91 }
92 var body struct {
93 Name string `json:"name"`
94 Confirm string `json:"confirm"`
95 }
96 if err := readJSON(r, &body); err != nil {
97 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
98 return
99 }
100 if !nameFromPath && name == "" {
101 name = decodeBranch(r.URL.Query().Get("name"))
102 }
103 confirm := decodeBranch(body.Confirm)
104 if !nameFromPath && confirm == "" {
105 confirm = name
106 }
107 if !s.apiOK(w, r, s.opRenameBranch(r.Context(), apiUser(r), repo, name, body.Name, confirm)) {
108 return
109 }
110 writeJSON(w, http.StatusOK, map[string]any{"name": body.Name})
111}
112
113func (s *Server) apiTags(w http.ResponseWriter, r *http.Request) {
114 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
115 if !ok {
116 return
117 }
118 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
119 if r.Method == http.MethodGet {
120 names, err := gitrepo.Tags(r.Context(), s.Git, dir)
121 if err != nil {
122 names = nil
123 }
124 if names == nil {
125 names = []string{}
126 }
127 page, per := pageQuery(r)
128 writePage(w, r, slicePage(names, page, per), len(names), page, per)
129 return
130 }
131 var in struct {
132 Name string `json:"name"`
133 Target string `json:"target"`
134 Message string `json:"message"`
135 }
136 if err := readJSON(r, &in); err != nil {
137 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
138 return
139 }
140 if !s.apiOK(w, r, s.opCreateTag(r.Context(), apiUser(r), repo, in.Name, in.Target, in.Message)) {
141 return
142 }
143 writeJSON(w, http.StatusCreated, map[string]any{"name": in.Name})
144}
145
146func (s *Server) apiDeleteTag(w http.ResponseWriter, r *http.Request) {
147 repo, _, ok := s.apiRepo(w, r, access.RoleDeveloper)
148 if !ok {
149 return
150 }
151 if !s.apiOK(w, r, s.opDeleteTag(r.Context(), apiUser(r), repo, r.PathValue("tag"))) {
152 return
153 }
154 writeJSON(w, http.StatusNoContent, nil)
155}
156
157func (s *Server) apiTagArchive(w http.ResponseWriter, r *http.Request) {
158 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
159 if !ok {
160 return
161 }
162 name := r.PathValue("tag")
163 format := r.URL.Query().Get("format")
164 if format == "" {
165 format = "tar.gz"
166 }
167 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
168 if !gitrepo.HasTag(r.Context(), s.Git, dir, name) {
169 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
170 return
171 }
172 filename := archiveFilename(repo.Name, name, format)
173 if format == "zip" {
174 w.Header().Set("Content-Type", "application/zip")
175 } else {
176 w.Header().Set("Content-Type", "application/gzip")
177 }
178 w.Header().Set("Content-Disposition", "attachment; filename=\""+filename+"\"")
179 if err := gitrepo.Archive(r.Context(), w, s.Git, dir, repo.Name+"-"+strings.ReplaceAll(name, "/", "-"), name, format); err != nil {
180 s.writeAPIErr(w, r, fail(400, err))
181 }
182}
183
184func (s *Server) apiTree(w http.ResponseWriter, r *http.Request) {
185 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
186 if !ok {
187 return
188 }
189 ref := safeRev(r.URL.Query().Get("ref"), repo.DefaultBranch)
190 rel := strings.Trim(r.URL.Query().Get("path"), "/")
191 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
192 entries, err := gitrepo.Tree(r.Context(), s.Git, dir, ref, rel)
193 if err != nil {
194 writeJSON(w, http.StatusOK, []any{})
195 return
196 }
197 rows := make([]map[string]any, 0, len(entries))
198 for _, e := range entries {
199 rows = append(rows, map[string]any{"name": e.Name, "type": e.Type, "mode": e.Mode, "id": e.SHA})
200 }
201 page, per := pageQuery(r)
202 writePage(w, r, slicePage(rows, page, per), len(rows), page, per)
203}
204
205func (s *Server) apiFile(w http.ResponseWriter, r *http.Request) {
206 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
207 if !ok {
208 return
209 }
210 path, pathOK := repoPath(pathWildcard(r, "path"))
211 if path == "" {
212 path, pathOK = repoPath(r.URL.Query().Get("path"))
213 }
214 if !pathOK || path == "" {
215 s.writeAPIErr(w, r, fail(400, i18n.E("err.sugFile")))
216 return
217 }
218 ref := safeRev(r.URL.Query().Get("ref"), repo.DefaultBranch)
219 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
220 out, err := exec.CommandContext(r.Context(), s.Git, "-C", dir, "show", ref+":"+path).Output()
221 if err != nil {
222 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
223 return
224 }
225 if strings.Contains(string(out), "\x00") {
226 w.Header().Set("Content-Type", "application/octet-stream")
227 } else {
228 w.Header().Set("Content-Type", "text/plain; charset=utf-8")
229 }
230 w.WriteHeader(http.StatusOK)
231 _, _ = w.Write(out)
232}
233
234func (s *Server) apiCommits(w http.ResponseWriter, r *http.Request) {
235 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
236 if !ok {
237 return
238 }
239 if r.Method == http.MethodPost {
240 var body struct {
241 Branch string `json:"branch"`
242 StartBranch string `json:"start_branch"`
243 CommitMessage string `json:"commit_message"`
244 Actions []fileAction `json:"actions"`
245 }
246 if err := readJSON(r, &body); err != nil {
247 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
248 return
249 }
250 sha, err := s.opCommitActions(r.Context(), apiUser(r), repo, body.Branch, body.StartBranch, body.CommitMessage, "", body.Actions)
251 if !s.apiOK(w, r, err) {
252 return
253 }
254 writeJSON(w, http.StatusCreated, map[string]any{"id": sha, "branch": body.Branch})
255 return
256 }
257 ref := safeRev(r.URL.Query().Get("ref"), repo.DefaultBranch)
258 page, per := pageQuery(r)
259 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
260 limit := page * per
261 if limit > 200 {
262 limit = 200
263 }
264 list, err := gitrepo.Log(r.Context(), s.Git, dir, ref, limit)
265 if err != nil || list == nil {
266 list = nil
267 }
268 rows := make([]map[string]any, 0, len(list))
269 for _, c := range list {
270 rows = append(rows, commitJSON(c))
271 }
272 writePage(w, r, slicePage(rows, page, per), len(rows), page, per)
273}
274
275func commitJSON(c gitrepo.Commit) map[string]any {
276 return map[string]any{
277 "id": c.SHA, "short_id": c.Short, "title": c.Subject, "message": c.Body,
278 "author_name": c.Author, "author_email": c.Email, "authored_date": c.Date,
279 }
280}
281
282func (s *Server) apiCommit(w http.ResponseWriter, r *http.Request) {
283 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
284 if !ok {
285 return
286 }
287 sha := r.PathValue("sha")
288 if !safeSHA(sha) {
289 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
290 return
291 }
292 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
293 c, err := gitrepo.Show(r.Context(), s.Git, dir, sha)
294 if err != nil {
295 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
296 return
297 }
298 writeJSON(w, http.StatusOK, commitJSON(c))
299}
300
301func (s *Server) apiCommitDiff(w http.ResponseWriter, r *http.Request) {
302 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
303 if !ok {
304 return
305 }
306 sha := r.PathValue("sha")
307 if !safeSHA(sha) {
308 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
309 return
310 }
311 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
312 parents, files, err := gitrepo.CommitPatch(r.Context(), s.Git, dir, sha)
313 if err != nil {
314 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
315 return
316 }
317 out := make([]map[string]any, 0, len(files))
318 for _, f := range files {
319 var b strings.Builder
320 for _, ln := range f.Lines {
321 b.WriteString(ln.Kind)
322 b.WriteString(ln.Text)
323 b.WriteByte('\n')
324 }
325 out = append(out, map[string]any{
326 "status": f.Status, "path": f.Path, "old_path": f.OldPath,
327 "added": f.Added, "deleted": f.Deleted, "binary": f.Binary, "diff": b.String(),
328 })
329 }
330 if parents == nil {
331 parents = []string{}
332 }
333 writeJSON(w, http.StatusOK, map[string]any{"id": sha, "parents": parents, "files": out})
334}