Commit 85f015e737

85f015e737ce697845c43980d0559a1da9660095

parent: 22770ebc07

Verified · cmc ci/build: success ci/quality: success ci/test: success ci/vuln: success

cmc <hello@cleberg.net> · 2026-11-21 21:14 UTC

repo mirror set-url: change a mirror's URL on the same host

Keeps the mirror id, so the sealed token stays valid, and marks the
mirror dirty. A different scheme or host is refused, so the stored
token is only sent where its owner sent it. Command examples name
krz-labs, the renamed GitHub org.

Closes #355

Layout: unified · split

.gitbay/wiki/Admin.org +1 −1
@@ -450,7 +450,7 @@ push=.
450450 and again before every sync; git then connects only to the addresses
451451 that were checked (=http.curloptResolve=) and does not follow
452452 redirects, so a mirror of a renamed repository fails until its URL
453 is updated. Needs git 2.37 or later on the server; with an older git
453 is updated with =repo mirror set-url=. Needs git 2.37 or later on the server; with an older git
454454 the worker logs an error at start and syncs no mirror, recording the
455455 reason on each. Sync ignores the system and global gitconfig.
456456
.gitbay/wiki/Users.org +2
@@ -373,6 +373,8 @@ gitbay repo mirror add you/copy https://github.com/them/theirs.git \
373373 --direction pull # follow upstream; local pushes refused
374374gitbay repo mirror list # sync status and last error, per mirror
375375gitbay repo mirror sync / remove <id>
376gitbay repo mirror set-url you/project <id> https://github.com/neworg/project.git
377 # same host; keeps the stored token
376378#+end_src
377379
378380Markdown and org files render on the web when opened, the way a README
cmd/gitbay/main.go +1
@@ -594,6 +594,7 @@ func repoCmd() *cobra.Command {
594594 pass("add", passOpts{server: []string{"repo", "mirror", "add"}, needsRepo: true, stdinOK: true}),
595595 pass("list", passOpts{server: []string{"repo", "mirror", "list"}, needsRepo: true}),
596596 pass("remove", passOpts{server: []string{"repo", "mirror", "remove"}, needsRepo: true}),
597 pass("set-url", passOpts{server: []string{"repo", "mirror", "set-url"}, needsRepo: true}),
597598 pass("sync", passOpts{server: []string{"repo", "mirror", "sync"}, needsRepo: true}),
598599 ),
599600 pass("secret-scan", passOpts{server: []string{"repo", "secret-scan"}, needsRepo: true}),
cmd/gitbay/summaries_gen.go +1
@@ -185,6 +185,7 @@ var summaries = map[string]string{
185185 "repo mirror add": "mirror to or from a remote",
186186 "repo mirror list": "list mirrors with sync status",
187187 "repo mirror remove": "remove a mirror",
188 "repo mirror set-url": "point a mirror at a new URL on the same host",
188189 "repo mirror sync": "schedule an immediate sync",
189190 "repo mute": "mute a repository, including work you are part of",
190191 "repo pin": "pin a repository to your dashboard",
internal/control/mirrorcmd.go +59 −1
@@ -26,13 +26,19 @@ func init() {
2626 {"--username", "<u>", "the remote's username", ""},
2727 {"--token-stdin", "", "read a credential token from stdin", ""},
2828 },
29 Examples: []string{"repo mirror add krz/gitbay https://github.com/krazywarez/gitbay.git --direction push"},
29 Examples: []string{"repo mirror add krz/gitbay https://github.com/krz-labs/gitbay.git --direction push"},
3030 ReadsStdin: true, Run: runMirrorAdd})
3131 register(Command{Path: []string{"repo", "mirror", "list"},
3232 Summary: "list mirrors with sync status",
3333 Usage: "repo mirror list <owner/name>",
3434 Examples: []string{"repo mirror list krz/gitbay"},
3535 ReadOnly: true, Run: runMirrorList})
36 register(Command{Path: []string{"repo", "mirror", "set-url"},
37 NeedsRecentSignIn: true,
38 Summary: "point a mirror at a new URL on the same host",
39 Usage: "repo mirror set-url <owner/name> <id> <https-url>",
40 Examples: []string{"repo mirror set-url krz/gitbay 3 https://github.com/krz-labs/gitbay.git"},
41 Run: runMirrorSetURL})
3642 register(Command{Path: []string{"repo", "mirror", "remove"},
3743 Summary: "remove a mirror",
3844 Usage: "repo mirror remove <owner/name> <id>",
@@ -165,6 +171,58 @@ func orDash(s string) string {
165171 return s
166172}
167173
174// runMirrorSetURL keeps the stored token, so it refuses a new host: the
175// token goes only where its owner sent it.
176func runMirrorSetURL(c *Ctx, args []string) int {
177 if len(args) != 3 {
178 return c.usage()
179 }
180 urlArg := args[2]
181 nu, err := url.Parse(urlArg)
182 if err != nil {
183 return c.fail(protocol.ExitUsage, "bad url %q", urlArg)
184 }
185 if err := gitpin.CheckHost(nu.Hostname()); err != nil {
186 return c.failInput(err)
187 }
188 if err := webhook.ValidateURL(urlArg, c.Cfg.Webhooks.AllowLocal); err != nil {
189 return c.failInput(err)
190 }
191 repo, code := resolveRepo(c, args[0], policy.CanAdmin)
192 if code >= 0 {
193 return code
194 }
195 id, err := strconv.ParseInt(args[1], 10, 64)
196 if err != nil {
197 return c.fail(protocol.ExitUsage, "bad mirror id %q", args[1])
198 }
199 ms, err := c.Store.ListMirrors(repo.ID)
200 if err != nil {
201 return c.fail(protocol.ExitFailure, "%v", err)
202 }
203 var cur *store.Mirror
204 for i := range ms {
205 if ms[i].ID == id {
206 cur = &ms[i]
207 }
208 }
209 if cur == nil {
210 return c.fail(protocol.ExitNotFound, "no mirror %d on %s", id, repo.Path())
211 }
212 if ou, err := url.Parse(cur.URL); err != nil || !strings.EqualFold(ou.Scheme, nu.Scheme) || !strings.EqualFold(ou.Host, nu.Host) {
213 return c.fail(protocol.ExitUsage, "set-url keeps the stored token, so the host cannot change; remove the mirror and add it again")
214 }
215 if err := c.Store.SetMirrorURL(repo.ID, id, urlArg); err != nil {
216 if errors.Is(err, store.ErrExists) {
217 return c.fail(protocol.ExitUsage, "that mirror already exists")
218 }
219 return c.fail(protocol.ExitFailure, "%v", err)
220 }
221 return c.emit(map[string]any{"id": id, "url": urlArg}, func(w io.Writer) {
222 fmt.Fprintf(w, "mirror %d now %s; sync scheduled\n", id, urlArg)
223 })
224}
225
168226func runMirrorRemove(c *Ctx, args []string) int {
169227 return runRepoItemAction(c, args, "mirror", c.Store.RemoveMirror, "removed", "removed mirror %d")
170228}
internal/control/mirrorcmd_test.go +37
@@ -1,6 +1,7 @@
11package control
22
33import (
4 "strconv"
45 "strings"
56 "testing"
67
@@ -27,3 +28,39 @@ func TestMirrorAddRefusesNumericHost(t *testing.T) {
2728 }
2829 }
2930}
31
32// set-url changes the URL in place, keeping the id and the sealed token,
33// and refuses a different host or an unknown id.
34func TestMirrorSetURL(t *testing.T) {
35 c, errOut, st, _ := importCtx(t, false)
36 repo, err := st.RepoByPath("alice/app")
37 if err != nil {
38 t.Fatal(err)
39 }
40 id, err := st.AddMirror(repo.ID, "push", "https://github.com/old/app.git", "u", "secret")
41 if err != nil {
42 t.Fatal(err)
43 }
44 if err := st.SetMirrorResult(id, ""); err != nil {
45 t.Fatal(err)
46 }
47 ids := strconv.FormatInt(id, 10)
48
49 if code := Dispatch(c, []string{"repo", "mirror", "set-url", "alice/app", ids, "https://example.com/new/app.git"}); code != protocol.ExitUsage || !strings.Contains(errOut.String(), "host cannot change") {
50 t.Fatalf("other host: exit %d, %q", code, errOut.String())
51 }
52 if code := Dispatch(c, []string{"repo", "mirror", "set-url", "alice/app", "999", "https://github.com/new/app.git"}); code != protocol.ExitNotFound {
53 t.Fatalf("unknown id: exit %d, %q", code, errOut.String())
54 }
55 if code := Dispatch(c, []string{"repo", "mirror", "set-url", "alice/app", ids, "https://github.com/new/app.git"}); code != protocol.ExitOK {
56 t.Fatalf("set-url: exit %d, %q", code, errOut.String())
57 }
58 ms, err := st.ListMirrors(repo.ID)
59 if err != nil || len(ms) != 1 {
60 t.Fatalf("mirrors %v, %v", ms, err)
61 }
62 m := ms[0]
63 if m.ID != id || m.URL != "https://github.com/new/app.git" || m.Token != "secret" || m.Username != "u" || !m.Dirty {
64 t.Fatalf("mirror after set-url: %+v", m)
65 }
66}
internal/control/org.go +1 −1
@@ -28,7 +28,7 @@ func init() {
2828 register(Command{Path: []string{"org", "rename"},
2929 Summary: "rename an organization",
3030 Usage: "org rename <old> <new> (clone URLs change)",
31 Examples: []string{"org rename krz krazywarez"}, Run: runOrgRename})
31 Examples: []string{"org rename krz krz-labs"}, Run: runOrgRename})
3232 register(Command{Path: []string{"org", "delete"},
3333 Summary: "delete an empty organization",
3434 Usage: "org delete <name> --yes",
internal/control/reauth_test.go +1
@@ -125,6 +125,7 @@ func TestNeedsRecentSignInSet(t *testing.T) {
125125 "repo delete",
126126 "repo deploy-key add",
127127 "repo mirror add",
128 "repo mirror set-url",
128129 "repo rename",
129130 "repo runner add",
130131 "repo secret set",
internal/control/repo.go +1 −1
@@ -54,7 +54,7 @@ func init() {
5454 NeedsRecentSignIn: true,
5555 Summary: "move a repository to another owner",
5656 Usage: "repo transfer <owner/name> <new-owner> (clone URLs change)",
57 Examples: []string{"repo transfer krz/gitbay krazywarez"},
57 Examples: []string{"repo transfer krz/gitbay krz-labs"},
5858 Run: runRepoTransfer})
5959 register(Command{Path: []string{"repo", "rename"},
6060 NeedsRecentSignIn: true,
internal/store/mirrors.go +16
@@ -102,6 +102,22 @@ func (s *Store) RemoveMirror(repoID, id int64) error {
102102 return nil
103103}
104104
105// SetMirrorURL points a mirror at a new URL and schedules a sync. The id,
106// and so the sealed token, is unchanged.
107func (s *Store) SetMirrorURL(repoID, id int64, url string) error {
108 res, err := s.DB.Exec("UPDATE mirrors SET url = ?, dirty = 1 WHERE repo_id = ? AND id = ?", url, repoID, id)
109 if err != nil {
110 if isUniqueErr(err) {
111 return ErrExists
112 }
113 return err
114 }
115 if n, _ := res.RowsAffected(); n == 0 {
116 return ErrNotFound
117 }
118 return nil
119}
120
105121// MarkMirrorsDirty schedules a sync. An empty direction marks both.
106122func (s *Store) MarkMirrorsDirty(repoID int64, direction string) error {
107123 q := "UPDATE mirrors SET dirty = 1 WHERE repo_id = ?"