internal/control/repo.go
1180 lines · 42714 bytes
1package control
2
3import (
4 "errors"
5 "fmt"
6 "io"
7 "os"
8 "path"
9 "path/filepath"
10 "slices"
11 "strings"
12
13 "gitbay.org/gitbay/internal/gitutil"
14 "gitbay.org/gitbay/internal/policy"
15 "gitbay.org/gitbay/internal/protocol"
16 "gitbay.org/gitbay/internal/store"
17)
18
19// RepoDir returns the on-disk path for a repository.
20func RepoDir(root, owner, name string) string {
21 return filepath.Join(root, "repos", owner, name+".git")
22}
23
24// HooksDir is the shared core.hooksPath directory.
25func HooksDir(root string) string { return filepath.Join(root, "hooks") }
26
27func init() {
28 register(Command{Path: []string{"repo", "create"},
29 Summary: "create a repository",
30 Usage: "repo create <owner/name> [--private]", Run: runRepoCreate})
31 register(Command{Path: []string{"repo", "list"},
32 Summary: "list repositories you own or can access",
33 Usage: "repo list [--limit <n>] [--cursor <c>]", ReadOnly: true, Run: runRepoList})
34 register(Command{Path: []string{"repo", "show"},
35 Summary: "show repository details",
36 Usage: "repo show <owner/name>", ReadOnly: true, Run: runRepoShow})
37 register(Command{Path: []string{"repo", "transfer"},
38 Summary: "move a repository to another owner",
39 Usage: "repo transfer <owner/name> <new-owner> (clone URLs change)", Run: runRepoTransfer})
40 register(Command{Path: []string{"repo", "rename"},
41 Summary: "rename a repository",
42 Usage: "repo rename <owner/name> <new-name> (clone URLs change)", Run: runRepoRename})
43 register(Command{Path: []string{"repo", "delete"},
44 Summary: "delete a repository",
45 Usage: "repo delete <owner/name> --yes", Run: runRepoDelete})
46 register(Command{Path: []string{"repo", "access", "grant"},
47 Summary: "grant access",
48 Usage: "repo access grant <owner/name> <user> read|write|admin", Run: runAccessGrant})
49 register(Command{Path: []string{"repo", "access", "revoke"},
50 Summary: "revoke access",
51 Usage: "repo access revoke <owner/name> <user>", Run: runAccessRevoke})
52 register(Command{Path: []string{"repo", "access", "list"},
53 Summary: "list access grants",
54 Usage: "repo access list <owner/name>", ReadOnly: true, Run: runAccessList})
55 register(Command{Path: []string{"repo", "settings", "show"},
56 Summary: "show settings",
57 Usage: "repo settings show <owner/name>", ReadOnly: true, Run: runSettingsShow})
58 register(Command{Path: []string{"repo", "settings", "protect"},
59 Summary: "protect a branch",
60 Usage: "repo settings protect <owner/name> <branch>", Run: runProtect})
61 register(Command{Path: []string{"repo", "settings", "unprotect"},
62 Summary: "unprotect a branch",
63 Usage: "repo settings unprotect <owner/name> <branch>", Run: runUnprotect})
64 register(Command{Path: []string{"repo", "settings", "protect-tag"},
65 Summary: "protect tags matching a glob (created once, never moved or deleted)",
66 Usage: "repo settings protect-tag <owner/name> <glob>", Run: runProtectTag})
67 register(Command{Path: []string{"repo", "settings", "unprotect-tag"},
68 Summary: "drop a protected-tag glob",
69 Usage: "repo settings unprotect-tag <owner/name> <glob>", Run: runUnprotectTag})
70 register(Command{Path: []string{"repo", "settings", "description"},
71 Summary: "set the repository description",
72 Usage: "repo settings description <owner/name> <text> ('' clears)", Run: runSetDescription})
73 register(Command{Path: []string{"repo", "settings", "visibility"},
74 Summary: "set repository visibility",
75 Usage: "repo settings visibility <owner/name> public|private", Run: runSetVisibility})
76 register(Command{Path: []string{"repo", "settings", "website"},
77 Summary: "set the repository website",
78 Usage: "repo settings website <owner/name> <url> ('' clears)", Run: runSetWebsite})
79 register(Command{Path: []string{"repo", "settings", "default-branch"},
80 Summary: "set the default branch",
81 Usage: "repo settings default-branch <owner/name> <branch>", Run: runSetDefaultBranch})
82 register(Command{Path: []string{"repo", "settings", "git-daemon"},
83 Summary: "expose over git://",
84 Usage: "repo settings git-daemon <owner/name> on|off", Run: runGitDaemon})
85 register(Command{Path: []string{"repo", "archive"},
86 Summary: "archive a repository (read-only: pushes and issue/MR writes refused)",
87 Usage: "repo archive <owner/name>", Run: runArchive})
88 register(Command{Path: []string{"repo", "unarchive"},
89 Summary: "unarchive a repository",
90 Usage: "repo unarchive <owner/name>", Run: runUnarchive})
91 register(Command{Path: []string{"repo", "topics"},
92 Summary: "list topics",
93 Usage: "repo topics <owner/name>", ReadOnly: true, Run: runTopicsList})
94 register(Command{Path: []string{"repo", "topics", "add"},
95 Summary: "add topics",
96 Usage: "repo topics add <owner/name> <topic>...", Run: runTopicsAdd})
97 register(Command{Path: []string{"repo", "topics", "remove"},
98 Summary: "remove topics",
99 Usage: "repo topics remove <owner/name> <topic>...", Run: runTopicsRemove})
100 register(Command{Path: []string{"repo", "search"},
101 Summary: "find repositories by name, description, or topic",
102 Usage: "repo search <query>", ReadOnly: true, Run: runRepoSearch})
103 register(Command{Path: []string{"repo", "grep"},
104 Summary: "search file contents",
105 Usage: "repo grep <owner/name> <query> [--ref <ref>]", ReadOnly: true, Run: runRepoGrep})
106 register(Command{Path: []string{"repo", "diff"},
107 Summary: "the patch between two refs, from their merge base",
108 Usage: "repo diff <owner/name> <base> <head>", ReadOnly: true, Run: runRepoDiff})
109 register(Command{Path: []string{"repo", "pin"},
110 Summary: "pin a repository to your dashboard",
111 Usage: "repo pin <owner/name>", Run: runRepoPin})
112 register(Command{Path: []string{"repo", "unpin"},
113 Summary: "unpin a repository",
114 Usage: "repo unpin <owner/name>", Run: runRepoUnpin})
115 register(Command{Path: []string{"repo", "bookmark"},
116 Summary: "bookmark a repository to come back to",
117 Usage: "repo bookmark <owner/name>", Run: runRepoBookmark})
118 register(Command{Path: []string{"repo", "unbookmark"},
119 Summary: "remove a bookmark",
120 Usage: "repo unbookmark <owner/name>", Run: runRepoUnbookmark})
121 register(Command{Path: []string{"repo", "bookmarks"},
122 Summary: "list the repositories you have bookmarked",
123 Usage: "repo bookmarks", ReadOnly: true, Run: runRepoBookmarks})
124}
125
126const (
127 minQueryLen = 2
128 maxQueryLen = 200
129 maxGrepMatches = 200
130)
131
132func validQuery(q string) error {
133 if len(q) < minQueryLen || len(q) > maxQueryLen {
134 return fmt.Errorf("query must be %d to %d characters", minQueryLen, maxQueryLen)
135 }
136 return nil
137}
138
139// refuseArchived blocks content writes (pushes are refused in the transport
140// layer) on archived repositories. Settings, access, and lifecycle commands
141// stay available so an archived repo can be managed and unarchived.
142func refuseArchived(c *Ctx, repo store.Repo) int {
143 if repo.Settings.Archived {
144 return c.fail(protocol.ExitDenied, "%s is archived and read-only", repo.Path())
145 }
146 return -1
147}
148
149// resolveRepo loads a repo and checks the given permission for c.User.
150func resolveRepo(c *Ctx, path string, check func(store.User, store.Repo, string) bool) (store.Repo, int) {
151 repo, err := c.Store.RepoByPath(path)
152 if err != nil {
153 if errors.Is(err, store.ErrNotFound) {
154 // Same message whether it doesn't exist or is invisible.
155 return repo, c.fail(protocol.ExitNotFound, "repository %s not found", path)
156 }
157 return repo, c.fail(protocol.ExitFailure, "loading repository: %v", err)
158 }
159 grant, err := c.Store.AccessRole(repo.ID, c.User.ID)
160 if err != nil {
161 return repo, c.fail(protocol.ExitFailure, "checking access: %v", err)
162 }
163 if !check(c.User, repo, grant) {
164 if !policy.CanRead(c.User, repo, grant) {
165 // Invisible repos 404, per the enumeration rule.
166 return repo, c.fail(protocol.ExitNotFound, "repository %s not found", path)
167 }
168 return repo, c.fail(protocol.ExitDenied, "permission denied on %s", path)
169 }
170 return repo, -1
171}
172
173func runRepoCreate(c *Ctx, args []string) int {
174 f, err := parseFlags(args, flagSpec{Values: []string{"--description"}, Bools: []string{"--private"}, MaxPos: 1, Usage: "repo create <owner/name> [--private] [--description <text>]"})
175 if err != nil {
176 return c.fail(protocol.ExitUsage, "%v", err)
177 }
178 visibility, path, description := "public", f.pos(0), f.Value("--description")
179 if f.Has("--private") {
180 visibility = "private"
181 }
182 owner, name, ok := strings.Cut(path, "/")
183 if !ok {
184 return c.fail(protocol.ExitUsage, "usage: repo create <owner/name> [--private]")
185 }
186 if err := policyValidateRepoName(name); err != nil {
187 return c.failErr(err)
188 }
189 ownerKind, ownerID := "user", c.User.ID
190 if owner != c.User.Username {
191 org, err := c.Store.OrgByName(owner)
192 if err != nil {
193 return c.fail(protocol.ExitDenied, "cannot create repositories under %q: not you and not an organization you can see", owner)
194 }
195 role, err := c.Store.OrgRole(org.ID, c.User.ID)
196 if err != nil {
197 return c.fail(protocol.ExitFailure, "%v", err)
198 }
199 if role != "admin" {
200 return c.fail(protocol.ExitDenied, "only admins of %s can create repositories there", owner)
201 }
202 ownerKind, ownerID = "org", org.ID
203 }
204 repoCreateMu.Lock()
205 if ownerKind == "user" {
206 if code := checkRepoQuota(c); code >= 0 {
207 repoCreateMu.Unlock()
208 return code
209 }
210 }
211 id, err := c.Store.CreateRepo(ownerKind, ownerID, name, visibility)
212 repoCreateMu.Unlock()
213 if err != nil {
214 return c.fail(protocol.ExitFailure, "%v", err)
215 }
216 dir := RepoDir(c.Cfg.Server.Root, owner, name)
217 if err := gitutil.InitBare(dir, "main", HooksDir(c.Cfg.Server.Root)); err != nil {
218 c.Store.DeleteRepo(id)
219 return c.fail(protocol.ExitFailure, "initializing repository: %v", err)
220 }
221 if description != "" {
222 if err := gitutil.WriteDescription(dir, description); err != nil {
223 return c.fail(protocol.ExitFailure, "writing description: %v", err)
224 }
225 }
226 type out struct {
227 Path string `json:"path"`
228 Visibility string `json:"visibility"`
229 SSHURL string `json:"ssh_url"`
230 }
231 d := out{Path: path, Visibility: visibility, SSHURL: "ssh://git@" + hostOf(c.Cfg.Server.SiteURL) + "/" + path + ".git"}
232 return c.emit(d, func(w io.Writer) {
233 fmt.Fprintf(w, "created %s (%s)\nclone: git clone %s\n", d.Path, d.Visibility, d.SSHURL)
234 })
235}
236
237func policyValidateRepoName(name string) error { return policy.ValidateName(name) }
238
239func hostOf(siteURL string) string {
240 s := strings.TrimPrefix(strings.TrimPrefix(siteURL, "https://"), "http://")
241 return strings.TrimSuffix(s, "/")
242}
243
244func runRepoList(c *Ctx, args []string) int {
245 args, p, code := parsePageFlags(c, args, "repo", false)
246 if code >= 0 {
247 return code
248 }
249 if len(args) != 0 {
250 return c.fail(protocol.ExitUsage, "usage: repo list [--limit <n>] [--cursor <c>]")
251 }
252 repos, err := c.Store.ListReposForUser(c.User.ID, p.queryLimit(), p.key)
253 if err != nil {
254 return c.fail(protocol.ExitFailure, "%v", err)
255 }
256 repos, next := trimPage(p, repos, "repo", store.Repo.Path)
257 type out struct {
258 Path string `json:"path"`
259 Visibility string `json:"visibility"`
260 Description string `json:"description,omitempty"`
261 Archived bool `json:"archived,omitempty"`
262 }
263 var ds []out
264 for _, r := range repos {
265 desc := gitutil.ReadDescription(RepoDir(c.Cfg.Server.Root, r.OwnerName, r.Name))
266 ds = append(ds, out{r.Path(), r.Visibility, desc, r.Settings.Archived})
267 }
268 return c.emitPage(p, ds, next, func(w io.Writer) {
269 for _, d := range ds {
270 mark := ""
271 if d.Archived {
272 mark = "\t[archived]"
273 }
274 fmt.Fprintf(w, "%s\t%s\t%s%s\n", d.Path, d.Visibility, d.Description, mark)
275 }
276 })
277}
278
279func runRepoShow(c *Ctx, args []string) int {
280 if len(args) != 1 {
281 return c.fail(protocol.ExitUsage, "usage: repo show <owner/name>")
282 }
283 repo, code := resolveRepo(c, args[0], policy.CanRead)
284 if code >= 0 {
285 return code
286 }
287 type mirrorOut struct {
288 Direction string `json:"direction"`
289 URL string `json:"url"`
290 Pending bool `json:"pending"`
291 LastSync string `json:"last_sync,omitempty"`
292 LastError string `json:"last_error,omitempty"`
293 }
294 type out struct {
295 Path string `json:"path"`
296 Description string `json:"description,omitempty"`
297 Website string `json:"website,omitempty"`
298 Visibility string `json:"visibility"`
299 DefaultBranch string `json:"default_branch"`
300 ProtectedBranches []string `json:"protected_branches,omitempty"`
301 Archived bool `json:"archived,omitempty"`
302 Topics []string `json:"topics,omitempty"`
303 Domains []string `json:"domains,omitempty"`
304 Mirrors []mirrorOut `json:"mirrors,omitempty"`
305 // ForkOf names the parent only when the caller can read it: a
306 // private parent is not confirmed to exist, here as anywhere.
307 ForkOf string `json:"fork_of,omitempty"`
308 // Watch and Bookmarked are the caller's own state, so a client
309 // can draw a toggle rather than two stateless buttons (#178).
310 Watch string `json:"watch,omitempty"` // watching, muted, or absent
311 Bookmarked bool `json:"bookmarked,omitempty"`
312 }
313 desc := gitutil.ReadDescription(RepoDir(c.Cfg.Server.Root, repo.OwnerName, repo.Name))
314 topics, err := c.Store.ListTopics(repo.ID)
315 if err != nil {
316 return c.fail(protocol.ExitFailure, "%v", err)
317 }
318 var domains []string
319 if ds, err := c.Store.ListPageDomains(repo.ID); err == nil {
320 for _, pd := range ds {
321 if pd.Verified() {
322 domains = append(domains, pd.Domain)
323 }
324 }
325 }
326 d := out{Path: repo.Path(), Description: desc, Website: repo.Settings.Website, Visibility: repo.Visibility,
327 DefaultBranch: repo.DefaultBranch, ProtectedBranches: repo.Settings.ProtectedBranches,
328 Archived: repo.Settings.Archived, Topics: topics, Domains: domains}
329 if repo.ForkOf != 0 {
330 if parent, err := c.Store.RepoByID(repo.ForkOf); err == nil {
331 if grant, err := c.Store.AccessRole(parent.ID, c.User.ID); err == nil && policy.CanRead(c.User, parent, grant) {
332 d.ForkOf = parent.Path()
333 }
334 }
335 }
336 if c.User.ID != 0 {
337 d.Watch = c.Store.RepoWatchState(repo.ID, c.User.ID)
338 d.Bookmarked = c.Store.IsBookmarked(c.User.ID, repo.ID)
339 }
340 // Mirror status is admin-only, like repo mirror list. The token never
341 // leaves the server.
342 if grant, err := c.Store.AccessRole(repo.ID, c.User.ID); err == nil && policy.CanAdmin(c.User, repo, grant) {
343 ms, err := c.Store.ListMirrors(repo.ID)
344 if err != nil {
345 return c.fail(protocol.ExitFailure, "%v", err)
346 }
347 for _, m := range ms {
348 d.Mirrors = append(d.Mirrors, mirrorOut{m.Direction, m.URL, m.Dirty, m.LastSync, m.LastError})
349 }
350 }
351 return c.emit(d, func(w io.Writer) {
352 line := fmt.Sprintf("%s\t%s\tdefault: %s", d.Path, d.Visibility, d.DefaultBranch)
353 if d.Archived {
354 line += "\t[archived]"
355 }
356 fmt.Fprintln(w, line)
357 if d.Description != "" {
358 fmt.Fprintf(w, "%s\n", d.Description)
359 }
360 if d.Website != "" {
361 fmt.Fprintf(w, "website: %s\n", d.Website)
362 }
363 if len(d.Topics) > 0 {
364 fmt.Fprintf(w, "topics: %s\n", strings.Join(d.Topics, ", "))
365 }
366 if len(d.ProtectedBranches) > 0 {
367 fmt.Fprintf(w, "protected: %s\n", strings.Join(d.ProtectedBranches, ", "))
368 }
369 if len(d.Domains) > 0 {
370 fmt.Fprintf(w, "pages domains: %s\n", strings.Join(d.Domains, ", "))
371 }
372 if d.ForkOf != "" {
373 fmt.Fprintf(w, "fork of: %s\n", d.ForkOf)
374 }
375 if d.Watch != "" {
376 fmt.Fprintf(w, "watch: %s\n", d.Watch)
377 }
378 if d.Bookmarked {
379 fmt.Fprintln(w, "bookmarked")
380 }
381 for _, m := range d.Mirrors {
382 status := "ok"
383 if m.Pending {
384 status = "pending"
385 }
386 if m.LastError != "" {
387 status = "error: " + m.LastError
388 }
389 fmt.Fprintf(w, "mirror: %s %s\tlast %s\t%s\n", m.Direction, m.URL, orDash(m.LastSync), status)
390 }
391 })
392}
393
394func runRepoTransfer(c *Ctx, args []string) int {
395 if len(args) != 2 {
396 return c.fail(protocol.ExitUsage, "usage: repo transfer <owner/name> <new-owner>")
397 }
398 repo, code := resolveRepo(c, args[0], policy.CanAdmin)
399 if code >= 0 {
400 return code
401 }
402 newOwner := args[1]
403 if newOwner == repo.OwnerName {
404 return c.fail(protocol.ExitUsage, "%s already owns this repository", newOwner)
405 }
406
407 // Target: yourself, or an org you admin — same rule as repo create.
408 newKind, newID := "", int64(0)
409 if newOwner == c.User.Username {
410 newKind, newID = "user", c.User.ID
411 } else if org, err := c.Store.OrgByName(newOwner); err == nil {
412 role, err := c.Store.OrgRole(org.ID, c.User.ID)
413 if err != nil {
414 return c.fail(protocol.ExitFailure, "%v", err)
415 }
416 if role != "admin" {
417 return c.fail(protocol.ExitDenied, "only admins of %s can receive repositories there", newOwner)
418 }
419 newKind, newID = "org", org.ID
420 } else {
421 return c.fail(protocol.ExitDenied, "cannot transfer to %q: not you and not an organization you can see", newOwner)
422 }
423
424 oldDir := RepoDir(c.Cfg.Server.Root, repo.OwnerName, repo.Name)
425 newDir := RepoDir(c.Cfg.Server.Root, newOwner, repo.Name)
426 if _, err := os.Stat(newDir); err == nil {
427 return c.fail(protocol.ExitFailure, "repository directory already exists at %s/%s", newOwner, repo.Name)
428 }
429 if err := c.Store.TransferRepo(repo.ID, newKind, newID); err != nil {
430 return c.failErr(err)
431 }
432 if err := os.MkdirAll(filepath.Dir(newDir), 0o750); err != nil {
433 c.Store.TransferRepo(repo.ID, repo.OwnerKind, repo.OwnerID)
434 return c.fail(protocol.ExitFailure, "%v", err)
435 }
436 if err := os.Rename(oldDir, newDir); err != nil {
437 // Keep name and disk consistent: revert the database change, and
438 // say so if even that fails, since the operator then has a row
439 // pointing at a directory that is not there.
440 if rerr := c.Store.TransferRepo(repo.ID, repo.OwnerKind, repo.OwnerID); rerr != nil {
441 return c.fail(protocol.ExitFailure, "moving repository: %v; and reverting the record failed: %v (the record now names %s but the directory is still %s)", err, rerr, newOwner+"/"+repo.Name, repo.Path())
442 }
443 return c.fail(protocol.ExitFailure, "moving repository: %v", err)
444 }
445 newPath := newOwner + "/" + repo.Name
446 return c.emit(map[string]string{"repo": newPath, "was": repo.Path()}, func(w io.Writer) {
447 fmt.Fprintf(w, "transferred %s to %s — clone URLs now use %s\n", repo.Path(), newPath, newPath)
448 })
449}
450
451func runRepoRename(c *Ctx, args []string) int {
452 if len(args) != 2 {
453 return c.fail(protocol.ExitUsage, "usage: repo rename <owner/name> <new-name>")
454 }
455 repo, code := resolveRepo(c, args[0], policy.CanAdmin)
456 if code >= 0 {
457 return code
458 }
459 newName := args[1]
460 if newName == repo.Name {
461 return c.fail(protocol.ExitUsage, "%s is already named %s", repo.Path(), newName)
462 }
463 if err := policyValidateRepoName(newName); err != nil {
464 return c.failErr(err)
465 }
466 oldDir := RepoDir(c.Cfg.Server.Root, repo.OwnerName, repo.Name)
467 newDir := RepoDir(c.Cfg.Server.Root, repo.OwnerName, newName)
468 if _, err := os.Stat(newDir); err == nil {
469 return c.fail(protocol.ExitFailure, "repository directory already exists at %s/%s", repo.OwnerName, newName)
470 }
471 if err := c.Store.RenameRepo(repo.ID, newName); err != nil {
472 return c.failErr(err)
473 }
474 if err := os.Rename(oldDir, newDir); err != nil {
475 // Same rule as transfer: keep name and disk consistent, and say so
476 // if even the revert fails.
477 if rerr := c.Store.RenameRepo(repo.ID, repo.Name); rerr != nil {
478 return c.fail(protocol.ExitFailure, "moving repository: %v; and reverting the record failed: %v (the record now names %s/%s but the directory is still %s)", err, rerr, repo.OwnerName, newName, repo.Path())
479 }
480 return c.fail(protocol.ExitFailure, "moving repository: %v", err)
481 }
482 newPath := repo.OwnerName + "/" + newName
483 return c.emit(map[string]string{"repo": newPath, "was": repo.Path()}, func(w io.Writer) {
484 fmt.Fprintf(w, "renamed %s to %s — clone URLs now use %s\n", repo.Path(), newPath, newPath)
485 })
486}
487
488func runRepoDelete(c *Ctx, args []string) int {
489 var path string
490 var yes bool
491 for _, a := range args {
492 if a == "--yes" {
493 yes = true
494 } else if path == "" {
495 path = a
496 } else {
497 return c.fail(protocol.ExitUsage, "usage: repo delete <owner/name> --yes")
498 }
499 }
500 if path == "" {
501 return c.fail(protocol.ExitUsage, "usage: repo delete <owner/name> --yes")
502 }
503 repo, code := resolveRepo(c, path, policy.CanAdmin)
504 if code >= 0 {
505 return code
506 }
507 if !yes {
508 return c.fail(protocol.ExitUsage, "repo delete is permanent; re-run with --yes")
509 }
510 return deleteRepo(c, repo)
511}
512
513// deleteRepo removes a repository the caller has already been cleared to
514// delete: the database row, then the directory.
515//
516// There is deliberately no repo.deleted event. events.repo_id and
517// webhooks.repo_id both cascade from repos, so recording one would delete
518// it, and every webhook that could have subscribed, in the same
519// statement. A repository's deletion is not observable through its own
520// webhooks; an instance that needs to hear about it wants the audit log
521// (#112).
522func deleteRepo(c *Ctx, repo store.Repo) int {
523 // Open MRs sourced from this repo keep working (targets own the
524 // objects) but must show that the source is gone.
525 if err := c.Store.MarkSourceGoneForRepo(repo.ID); err != nil {
526 return c.fail(protocol.ExitFailure, "%v", err)
527 }
528 if err := c.Store.DeleteRepo(repo.ID); err != nil {
529 return c.fail(protocol.ExitFailure, "%v", err)
530 }
531 if err := os.RemoveAll(RepoDir(c.Cfg.Server.Root, repo.OwnerName, repo.Name)); err != nil {
532 return c.fail(protocol.ExitFailure, "database row removed but disk cleanup failed: %v", err)
533 }
534 return c.emit(map[string]string{"deleted": repo.Path()}, func(w io.Writer) {
535 fmt.Fprintf(w, "deleted %s\n", repo.Path())
536 })
537}
538
539func runAccessGrant(c *Ctx, args []string) int {
540 if len(args) != 3 || !slices.Contains([]string{"read", "write", "admin"}, args[2]) {
541 return c.fail(protocol.ExitUsage, "usage: repo access grant <owner/name> <user> read|write|admin")
542 }
543 repo, code := resolveRepo(c, args[0], policy.CanAdmin)
544 if code >= 0 {
545 return code
546 }
547 target, err := c.Store.UserByUsername(args[1])
548 if err != nil {
549 return c.fail(protocol.ExitNotFound, "no such user %q", args[1])
550 }
551 if err := c.Store.GrantAccess(repo.ID, target.ID, args[2]); err != nil {
552 return c.fail(protocol.ExitFailure, "%v", err)
553 }
554 return c.emit(map[string]string{"granted": args[2], "user": target.Username},
555 func(w io.Writer) { fmt.Fprintf(w, "granted %s to %s on %s\n", args[2], target.Username, repo.Path()) })
556}
557
558func runAccessRevoke(c *Ctx, args []string) int {
559 if len(args) != 2 {
560 return c.fail(protocol.ExitUsage, "usage: repo access revoke <owner/name> <user>")
561 }
562 repo, code := resolveRepo(c, args[0], policy.CanAdmin)
563 if code >= 0 {
564 return code
565 }
566 target, err := c.Store.UserByUsername(args[1])
567 if err != nil {
568 return c.fail(protocol.ExitNotFound, "no such user %q", args[1])
569 }
570 if err := c.Store.RevokeAccess(repo.ID, target.ID); err != nil {
571 if errors.Is(err, store.ErrNotFound) {
572 return c.fail(protocol.ExitNotFound, "%s has no grant on %s", target.Username, repo.Path())
573 }
574 return c.fail(protocol.ExitFailure, "%v", err)
575 }
576 return c.emit(map[string]string{"revoked": target.Username},
577 func(w io.Writer) { fmt.Fprintf(w, "revoked %s on %s\n", target.Username, repo.Path()) })
578}
579
580func runAccessList(c *Ctx, args []string) int {
581 if len(args) != 1 {
582 return c.fail(protocol.ExitUsage, "usage: repo access list <owner/name>")
583 }
584 repo, code := resolveRepo(c, args[0], policy.CanAdmin)
585 if code >= 0 {
586 return code
587 }
588 entries, err := c.Store.ListAccess(repo.ID)
589 if err != nil {
590 return c.fail(protocol.ExitFailure, "%v", err)
591 }
592 type out struct {
593 User string `json:"user"`
594 Role string `json:"role"`
595 }
596 var ds []out
597 for _, e := range entries {
598 ds = append(ds, out{e.Username, e.Role})
599 }
600 return c.emit(ds, func(w io.Writer) {
601 for _, d := range ds {
602 fmt.Fprintf(w, "%s\t%s\n", d.User, d.Role)
603 }
604 })
605}
606
607func runSettingsShow(c *Ctx, args []string) int {
608 if len(args) != 1 {
609 return c.fail(protocol.ExitUsage, "usage: repo settings show <owner/name>")
610 }
611 repo, code := resolveRepo(c, args[0], policy.CanAdmin)
612 if code >= 0 {
613 return code
614 }
615 return c.emit(repo.Settings, func(w io.Writer) {
616 fmt.Fprintf(w, "protected_branches: %s\nprotected_tags: %s\nrequire_mr: %v\nrequire_signed_commits: %v\ngit_daemon: %v\narchived: %v\n",
617 strings.Join(repo.Settings.ProtectedBranches, ", "), strings.Join(repo.Settings.ProtectedTags, ", "), repo.Settings.RequireMR, repo.Settings.RequireSignedCommits, repo.Settings.GitDaemon, repo.Settings.Archived)
618 })
619}
620
621func runSetDescription(c *Ctx, args []string) int {
622 if len(args) != 2 {
623 return c.fail(protocol.ExitUsage, "usage: repo settings description <owner/name> <text>")
624 }
625 repo, code := resolveRepo(c, args[0], policy.CanAdmin)
626 if code >= 0 {
627 return code
628 }
629 dir := RepoDir(c.Cfg.Server.Root, repo.OwnerName, repo.Name)
630 if err := gitutil.WriteDescription(dir, args[1]); err != nil {
631 return c.fail(protocol.ExitFailure, "%v", err)
632 }
633 return c.emit(map[string]string{"description": gitutil.ReadDescription(dir)}, func(w io.Writer) {
634 fmt.Fprintf(w, "description set on %s\n", repo.Path())
635 })
636}
637
638func runSetDefaultBranch(c *Ctx, args []string) int {
639 if len(args) != 2 {
640 return c.fail(protocol.ExitUsage, "usage: repo settings default-branch <owner/name> <branch>")
641 }
642 repo, code := resolveRepo(c, args[0], policy.CanAdmin)
643 if code >= 0 {
644 return code
645 }
646 branch := args[1]
647 dir := RepoDir(c.Cfg.Server.Root, repo.OwnerName, repo.Name)
648 if _, err := gitutil.ResolveRef(dir, "refs/heads/"+branch); err != nil {
649 return c.fail(protocol.ExitFailure, "no branch named %q on %s", branch, repo.Path())
650 }
651 if err := gitutil.SetHead(dir, branch); err != nil {
652 return c.fail(protocol.ExitFailure, "%v", err)
653 }
654 if err := c.Store.UpdateDefaultBranch(repo.ID, branch); err != nil {
655 return c.fail(protocol.ExitFailure, "%v", err)
656 }
657 return c.emit(map[string]string{"default_branch": branch}, func(w io.Writer) {
658 fmt.Fprintf(w, "default branch of %s is now %s\n", repo.Path(), branch)
659 })
660}
661
662func runSetWebsite(c *Ctx, args []string) int {
663 if len(args) != 2 {
664 return c.fail(protocol.ExitUsage, "usage: repo settings website <owner/name> <url>")
665 }
666 site := strings.TrimSpace(args[1])
667 if err := validateWebsite(site); err != nil {
668 return c.failErr(err)
669 }
670 if len(site) > 256 {
671 return c.fail(protocol.ExitUsage, "website URL too long (max 256)")
672 }
673 repo, code := resolveRepo(c, args[0], policy.CanAdmin)
674 if code >= 0 {
675 return code
676 }
677 if _, err := c.Store.UpdateRepoSettings(repo.ID, func(s *store.RepoSettings) { s.Website = site }); err != nil {
678 return c.fail(protocol.ExitFailure, "%v", err)
679 }
680 return c.emit(map[string]string{"website": site}, func(w io.Writer) {
681 if site == "" {
682 fmt.Fprintf(w, "website cleared on %s\n", repo.Path())
683 } else {
684 fmt.Fprintf(w, "website set on %s\n", repo.Path())
685 }
686 })
687}
688
689func runSetVisibility(c *Ctx, args []string) int {
690 if len(args) != 2 || (args[1] != "public" && args[1] != "private") {
691 return c.fail(protocol.ExitUsage, "usage: repo settings visibility <owner/name> public|private")
692 }
693 repo, code := resolveRepo(c, args[0], policy.CanAdmin)
694 if code >= 0 {
695 return code
696 }
697 return setRepoVisibility(c, repo, args[1])
698}
699
700// setRepoVisibility applies a visibility change the caller has already
701// been cleared to make.
702func setRepoVisibility(c *Ctx, repo store.Repo, visibility string) int {
703 if repo.Visibility == visibility {
704 return c.emit(map[string]string{"visibility": visibility}, func(w io.Writer) {
705 fmt.Fprintf(w, "%s is already %s\n", repo.Path(), visibility)
706 })
707 }
708 if err := c.Store.SetRepoVisibility(repo.ID, visibility); err != nil {
709 return c.fail(protocol.ExitFailure, "%v", err)
710 }
711 // Going private takes the repository off every anonymous surface, so
712 // git:// exposure cannot outlive the change.
713 if visibility == "private" && repo.Settings.GitDaemon {
714 c.Store.UpdateRepoSettings(repo.ID, func(s *store.RepoSettings) { s.GitDaemon = false })
715 }
716 c.Store.Audit(c.User.ID, "repo.visibility", map[string]any{"repo": repo.ID, "visibility": visibility})
717 return c.emit(map[string]string{"visibility": visibility}, func(w io.Writer) {
718 fmt.Fprintf(w, "%s is now %s\n", repo.Path(), visibility)
719 })
720}
721
722func runGitDaemon(c *Ctx, args []string) int {
723 if len(args) != 2 || (args[1] != "on" && args[1] != "off") {
724 return c.fail(protocol.ExitUsage, "usage: repo settings git-daemon <owner/name> on|off")
725 }
726 repo, code := resolveRepo(c, args[0], policy.CanAdmin)
727 if code >= 0 {
728 return code
729 }
730 on := args[1] == "on"
731 if on && repo.Visibility != "public" {
732 return c.fail(protocol.ExitUsage, "git:// serves only public repositories; %s is private", repo.Path())
733 }
734 if on && !c.Cfg.GitDaemon.Enabled {
735 return c.fail(protocol.ExitUsage, "this instance does not run the git:// daemon ([git_daemon] enabled = false)")
736 }
737 s, err := c.Store.UpdateRepoSettings(repo.ID, func(s *store.RepoSettings) { s.GitDaemon = on })
738 if err != nil {
739 return c.fail(protocol.ExitFailure, "%v", err)
740 }
741 return c.emit(s, func(w io.Writer) { fmt.Fprintf(w, "git-daemon %s on %s\n", args[1], repo.Path()) })
742}
743
744func runArchive(c *Ctx, args []string) int { return setArchived(c, args, true) }
745func runUnarchive(c *Ctx, args []string) int { return setArchived(c, args, false) }
746
747func setArchived(c *Ctx, args []string, archived bool) int {
748 verb := "archive"
749 if !archived {
750 verb = "unarchive"
751 }
752 if len(args) != 1 {
753 return c.fail(protocol.ExitUsage, "usage: repo %s <owner/name>", verb)
754 }
755 repo, code := resolveRepo(c, args[0], policy.CanAdmin)
756 if code >= 0 {
757 return code
758 }
759 return archiveRepo(c, repo, archived)
760}
761
762// archiveRepo flips the archived flag on a repository the caller has
763// already been cleared to manage.
764func archiveRepo(c *Ctx, repo store.Repo, archived bool) int {
765 verb := "archive"
766 if !archived {
767 verb = "unarchive"
768 }
769 if repo.Settings.Archived == archived {
770 return c.fail(protocol.ExitUsage, "%s is already %sd", repo.Path(), verb)
771 }
772 s, err := c.Store.UpdateRepoSettings(repo.ID, func(s *store.RepoSettings) { s.Archived = archived })
773 if err != nil {
774 return c.fail(protocol.ExitFailure, "%v", err)
775 }
776 c.Store.RecordEvent(repo.ID, c.User.ID, "repo."+verb+"d", "{}")
777 return c.emit(s, func(w io.Writer) { fmt.Fprintf(w, "%sd %s\n", verb, repo.Path()) })
778}
779
780func runTopicsList(c *Ctx, args []string) int {
781 if len(args) != 1 {
782 return c.fail(protocol.ExitUsage, "usage: repo topics <owner/name>")
783 }
784 repo, code := resolveRepo(c, args[0], policy.CanRead)
785 if code >= 0 {
786 return code
787 }
788 topics, err := c.Store.ListTopics(repo.ID)
789 if err != nil {
790 return c.fail(protocol.ExitFailure, "%v", err)
791 }
792 return c.emit(topics, func(w io.Writer) {
793 for _, t := range topics {
794 fmt.Fprintln(w, t)
795 }
796 })
797}
798
799func runTopicsAdd(c *Ctx, args []string) int { return editTopics(c, args, true) }
800func runTopicsRemove(c *Ctx, args []string) int { return editTopics(c, args, false) }
801
802func editTopics(c *Ctx, args []string, add bool) int {
803 verb := "add"
804 if !add {
805 verb = "remove"
806 }
807 if len(args) < 2 {
808 return c.fail(protocol.ExitUsage, "usage: repo topics %s <owner/name> <topic>...", verb)
809 }
810 repo, code := resolveRepo(c, args[0], policy.CanAdmin)
811 if code >= 0 {
812 return code
813 }
814 topics := args[1:]
815 if add {
816 for _, t := range topics {
817 if err := policy.ValidateTopic(t); err != nil {
818 return c.failErr(err)
819 }
820 }
821 have, err := c.Store.ListTopics(repo.ID)
822 if err != nil {
823 return c.fail(protocol.ExitFailure, "%v", err)
824 }
825 added := 0
826 for _, t := range topics {
827 if !slices.Contains(have, t) {
828 added++
829 }
830 }
831 if len(have)+added > policy.MaxTopics {
832 return c.fail(protocol.ExitUsage, "a repository can have at most %d topics", policy.MaxTopics)
833 }
834 for _, t := range topics {
835 if err := c.Store.AddTopic(repo.ID, t); err != nil {
836 return c.fail(protocol.ExitFailure, "%v", err)
837 }
838 }
839 } else {
840 for _, t := range topics {
841 if err := c.Store.RemoveTopic(repo.ID, t); err != nil {
842 if errors.Is(err, store.ErrNotFound) {
843 return c.fail(protocol.ExitNotFound, "%s has no topic %q", repo.Path(), t)
844 }
845 return c.fail(protocol.ExitFailure, "%v", err)
846 }
847 }
848 }
849 now, err := c.Store.ListTopics(repo.ID)
850 if err != nil {
851 return c.fail(protocol.ExitFailure, "%v", err)
852 }
853 return c.emit(now, func(w io.Writer) {
854 fmt.Fprintf(w, "topics on %s: %s\n", repo.Path(), strings.Join(now, ", "))
855 })
856}
857
858// runRepoSearch matches the query against name, owner/name, description,
859// and topics of every repository the caller can see.
860func runRepoSearch(c *Ctx, args []string) int {
861 if len(args) != 1 {
862 return c.fail(protocol.ExitUsage, "usage: repo search <query>")
863 }
864 if err := validQuery(args[0]); err != nil {
865 return c.failErr(err)
866 }
867 q := strings.ToLower(args[0])
868
869 public, err := c.Store.ListPublicRepos()
870 if err != nil {
871 return c.fail(protocol.ExitFailure, "%v", err)
872 }
873 own, err := c.Store.ListReposForUser(c.User.ID, 0, "")
874 if err != nil {
875 return c.fail(protocol.ExitFailure, "%v", err)
876 }
877 seen := map[int64]bool{}
878 type out struct {
879 Path string `json:"path"`
880 Visibility string `json:"visibility"`
881 Description string `json:"description,omitempty"`
882 Topics []string `json:"topics,omitempty"`
883 }
884 var ds []out
885 for _, r := range append(public, own...) {
886 if seen[r.ID] {
887 continue
888 }
889 seen[r.ID] = true
890 desc := gitutil.ReadDescription(RepoDir(c.Cfg.Server.Root, r.OwnerName, r.Name))
891 topics, _ := c.Store.ListTopics(r.ID)
892 if !MatchesRepo(q, r.Path(), desc, topics) {
893 continue
894 }
895 ds = append(ds, out{r.Path(), r.Visibility, desc, topics})
896 }
897 return c.emit(ds, func(w io.Writer) {
898 for _, d := range ds {
899 fmt.Fprintf(w, "%s\t%s\t%s\n", d.Path, d.Visibility, d.Description)
900 }
901 })
902}
903
904// MatchesRepo is the one rule for matching a repository against a text
905// query: its path, its description, or any of its topics. The web's
906// /explore filter and /search page call it too, so the three surfaces
907// cannot answer the same query differently.
908func MatchesRepo(q, path, desc string, topics []string) bool {
909 q = strings.ToLower(q)
910 if strings.Contains(strings.ToLower(path), q) ||
911 strings.Contains(strings.ToLower(desc), q) {
912 return true
913 }
914 for _, t := range topics {
915 if strings.Contains(strings.ToLower(t), q) {
916 return true
917 }
918 }
919 return false
920}
921
922func runRepoGrep(c *Ctx, args []string) int {
923 f, err := parseFlags(args, flagSpec{Values: []string{"--ref"}, MaxPos: 2, Usage: "repo grep <owner/name> <query> [--ref <ref>]"})
924 if err != nil {
925 return c.fail(protocol.ExitUsage, "%v", err)
926 }
927 path, query, ref := f.pos(0), f.pos(1), f.Value("--ref")
928 if path == "" || query == "" {
929 return c.fail(protocol.ExitUsage, "usage: repo grep <owner/name> <query> [--ref <ref>]")
930 }
931 if err := validQuery(query); err != nil {
932 return c.failErr(err)
933 }
934 repo, code := resolveRepo(c, path, policy.CanRead)
935 if code >= 0 {
936 return code
937 }
938 if ref == "" {
939 ref = repo.DefaultBranch
940 }
941 dir := RepoDir(c.Cfg.Server.Root, repo.OwnerName, repo.Name)
942 if _, err := gitutil.ResolveRef(dir, ref); err != nil {
943 return c.fail(protocol.ExitNotFound, "no ref %q in %s", ref, repo.Path())
944 }
945 matches, err := gitutil.Grep(dir, ref, query, maxGrepMatches)
946 if err != nil {
947 return c.fail(protocol.ExitFailure, "%v", err)
948 }
949 type out struct {
950 Path string `json:"path"`
951 Line int `json:"line"`
952 Text string `json:"text"`
953 }
954 var ds []out
955 for _, m := range matches {
956 ds = append(ds, out{m.Path, m.Line, m.Text})
957 }
958 return c.emit(ds, func(w io.Writer) {
959 for _, d := range ds {
960 fmt.Fprintf(w, "%s:%d:%s\n", d.Path, d.Line, d.Text)
961 }
962 })
963}
964
965func runRepoPin(c *Ctx, args []string) int { return setPinned(c, args, true) }
966func runRepoUnpin(c *Ctx, args []string) int { return setPinned(c, args, false) }
967
968func setPinned(c *Ctx, args []string, pin bool) int {
969 verb := "pin"
970 if !pin {
971 verb = "unpin"
972 }
973 if len(args) != 1 {
974 return c.fail(protocol.ExitUsage, "usage: repo %s <owner/name>", verb)
975 }
976 repo, code := resolveRepo(c, args[0], policy.CanRead)
977 if code >= 0 {
978 return code
979 }
980 if pin {
981 if err := c.Store.PinRepo(c.User.ID, repo.ID); err != nil {
982 return c.fail(protocol.ExitFailure, "%v", err)
983 }
984 } else if err := c.Store.UnpinRepo(c.User.ID, repo.ID); err != nil {
985 if errors.Is(err, store.ErrNotFound) {
986 return c.fail(protocol.ExitNotFound, "%s is not pinned", repo.Path())
987 }
988 return c.fail(protocol.ExitFailure, "%v", err)
989 }
990 return c.emit(map[string]string{verb + "ned": repo.Path()}, func(w io.Writer) {
991 fmt.Fprintf(w, "%sned %s\n", verb, repo.Path())
992 })
993}
994
995func runRepoBookmark(c *Ctx, args []string) int { return setBookmarked(c, args, true) }
996func runRepoUnbookmark(c *Ctx, args []string) int { return setBookmarked(c, args, false) }
997
998// setBookmarked mirrors setPinned. A bookmark needs only read access —
999// bookmarking is something you do to someone else's repository, which is
1000// the whole point of it — and a private repository you cannot read is
1001// not found, as everywhere.
1002func setBookmarked(c *Ctx, args []string, on bool) int {
1003 verb := "bookmark"
1004 if !on {
1005 verb = "unbookmark"
1006 }
1007 if len(args) != 1 {
1008 return c.fail(protocol.ExitUsage, "usage: repo %s <owner/name>", verb)
1009 }
1010 repo, code := resolveRepo(c, args[0], policy.CanRead)
1011 if code >= 0 {
1012 return code
1013 }
1014 if on {
1015 if err := c.Store.BookmarkRepo(c.User.ID, repo.ID); err != nil {
1016 return c.fail(protocol.ExitFailure, "%v", err)
1017 }
1018 } else if err := c.Store.UnbookmarkRepo(c.User.ID, repo.ID); err != nil {
1019 if errors.Is(err, store.ErrNotFound) {
1020 return c.fail(protocol.ExitNotFound, "%s is not bookmarked", repo.Path())
1021 }
1022 return c.fail(protocol.ExitFailure, "%v", err)
1023 }
1024 return c.emit(map[string]string{verb + "ed": repo.Path()}, func(w io.Writer) {
1025 fmt.Fprintf(w, "%sed %s\n", verb, repo.Path())
1026 })
1027}
1028
1029// BookmarkOut is one row of `repo bookmarks`: the repository and how many
1030// people have bookmarked it.
1031type BookmarkOut struct {
1032 Path string `json:"path"`
1033 Description string `json:"description,omitempty"`
1034 Visibility string `json:"visibility"`
1035 Bookmarks int `json:"bookmarks"`
1036}
1037
1038func runRepoBookmarks(c *Ctx, args []string) int {
1039 if len(args) != 0 {
1040 return c.fail(protocol.ExitUsage, "usage: repo bookmarks")
1041 }
1042 repos, err := c.Store.ListBookmarks(c.User.ID)
1043 if err != nil {
1044 return c.fail(protocol.ExitFailure, "%v", err)
1045 }
1046 out := []BookmarkOut{}
1047 for _, r := range repos {
1048 // A repository bookmarked while public and since made private
1049 // stays in the table and drops out of the listing, the same way
1050 // it disappears from every other surface.
1051 grant, err := c.Store.AccessRole(r.ID, c.User.ID)
1052 if err != nil {
1053 return c.fail(protocol.ExitFailure, "%v", err)
1054 }
1055 if !policy.CanRead(c.User, r, grant) {
1056 continue
1057 }
1058 out = append(out, BookmarkOut{
1059 Path: r.Path(),
1060 Description: gitutil.ReadDescription(RepoDir(c.Cfg.Server.Root, r.OwnerName, r.Name)),
1061 Visibility: r.Visibility,
1062 Bookmarks: c.Store.BookmarkCount(r.ID),
1063 })
1064 }
1065 return c.emit(out, func(w io.Writer) {
1066 for _, b := range out {
1067 fmt.Fprintf(w, "%s\t%d\t%s\n", b.Path, b.Bookmarks, b.Description)
1068 }
1069 })
1070}
1071
1072func runProtectTag(c *Ctx, args []string) int { return setProtectTag(c, args, true) }
1073func runUnprotectTag(c *Ctx, args []string) int { return setProtectTag(c, args, false) }
1074
1075func setProtectTag(c *Ctx, args []string, protect bool) int {
1076 if len(args) != 2 {
1077 return c.fail(protocol.ExitUsage, "usage: repo settings protect-tag|unprotect-tag <owner/name> <glob>")
1078 }
1079 glob := args[1]
1080 if _, err := path.Match(glob, "x"); err != nil || glob == "" {
1081 return c.fail(protocol.ExitUsage, "bad glob %q", glob)
1082 }
1083 repo, code := resolveRepo(c, args[0], policy.CanAdmin)
1084 if code >= 0 {
1085 return code
1086 }
1087 s, err := c.Store.UpdateRepoSettings(repo.ID, func(s *store.RepoSettings) {
1088 has := slices.Contains(s.ProtectedTags, glob)
1089 if protect && !has {
1090 s.ProtectedTags = append(s.ProtectedTags, glob)
1091 slices.Sort(s.ProtectedTags)
1092 }
1093 if !protect && has {
1094 s.ProtectedTags = slices.DeleteFunc(s.ProtectedTags, func(g string) bool { return g == glob })
1095 }
1096 })
1097 if err != nil {
1098 return c.fail(protocol.ExitFailure, "%v", err)
1099 }
1100 verb := "protected"
1101 if !protect {
1102 verb = "unprotected"
1103 }
1104 return c.emit(s, func(w io.Writer) {
1105 fmt.Fprintf(w, "tags %s %s on %s\n", glob, verb, repo.Path())
1106 })
1107}
1108
1109func runProtect(c *Ctx, args []string) int { return setProtect(c, args, true) }
1110func runUnprotect(c *Ctx, args []string) int { return setProtect(c, args, false) }
1111
1112func setProtect(c *Ctx, args []string, protect bool) int {
1113 if len(args) != 2 {
1114 return c.fail(protocol.ExitUsage, "usage: repo settings protect|unprotect <owner/name> <branch>")
1115 }
1116 repo, code := resolveRepo(c, args[0], policy.CanAdmin)
1117 if code >= 0 {
1118 return code
1119 }
1120 branch := args[1]
1121 // The list is read and rewritten inside the update, so two admins
1122 // protecting different branches at once both land.
1123 s, err := c.Store.UpdateRepoSettings(repo.ID, func(s *store.RepoSettings) {
1124 has := slices.Contains(s.ProtectedBranches, branch)
1125 if protect && !has {
1126 s.ProtectedBranches = append(s.ProtectedBranches, branch)
1127 slices.Sort(s.ProtectedBranches)
1128 }
1129 if !protect && has {
1130 s.ProtectedBranches = slices.DeleteFunc(s.ProtectedBranches, func(b string) bool { return b == branch })
1131 }
1132 })
1133 if err != nil {
1134 return c.fail(protocol.ExitFailure, "%v", err)
1135 }
1136 verb := "protected"
1137 if !protect {
1138 verb = "unprotected"
1139 }
1140 return c.emit(s, func(w io.Writer) { fmt.Fprintf(w, "%s %s on %s\n", verb, branch, repo.Path()) })
1141}
1142
1143// runRepoDiff is the compare view's command: what head adds on top of
1144// base, measured from their merge base the way a merge request diff is,
1145// so a base that moved on does not show up as removals (#118).
1146func runRepoDiff(c *Ctx, args []string) int {
1147 f, err := parseFlags(args, flagSpec{MaxPos: 3, Usage: "repo diff <owner/name> <base> <head>"})
1148 if err != nil || len(f.Pos) != 3 {
1149 return c.fail(protocol.ExitUsage, "usage: repo diff <owner/name> <base> <head>")
1150 }
1151 repo, code := resolveRepo(c, f.pos(0), policy.CanRead)
1152 if code >= 0 {
1153 return code
1154 }
1155 dir := RepoDir(c.Cfg.Server.Root, repo.OwnerName, repo.Name)
1156 base, err := gitutil.ResolveRef(dir, f.pos(1))
1157 if err != nil {
1158 return c.fail(protocol.ExitNotFound, "no ref %q in %s", f.pos(1), repo.Path())
1159 }
1160 head, err := gitutil.ResolveRef(dir, f.pos(2))
1161 if err != nil {
1162 return c.fail(protocol.ExitNotFound, "no ref %q in %s", f.pos(2), repo.Path())
1163 }
1164 mergeBase, err := gitutil.MergeBase(dir, base, head)
1165 if err != nil {
1166 return c.fail(protocol.ExitUsage, "%v", err)
1167 }
1168 patch, truncated, err := gitutil.Diff(dir, mergeBase, head, 4<<20)
1169 if err != nil {
1170 return c.fail(protocol.ExitFailure, "%v", err)
1171 }
1172 if c.JSON {
1173 return c.emit(map[string]any{"base": base, "head": head, "merge_base": mergeBase, "patch": patch, "truncated": truncated}, nil)
1174 }
1175 fmt.Fprint(c.Stdout, patch)
1176 if truncated {
1177 fmt.Fprintln(c.Stderr, "diff truncated at 4 MiB")
1178 }
1179 return protocol.ExitOK
1180}