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=.
450 and again before every sync; git then connects only to the addresses 450 and again before every sync; git then connects only to the addresses
451 that were checked (=http.curloptResolve=) and does not follow 451 that were checked (=http.curloptResolve=) and does not follow
452 redirects, so a mirror of a renamed repository fails until its URL 452 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
454 the worker logs an error at start and syncs no mirror, recording the 454 the worker logs an error at start and syncs no mirror, recording the
455 reason on each. Sync ignores the system and global gitconfig. 455 reason on each. Sync ignores the system and global gitconfig.
456 456
.gitbay/wiki/Users.org +2
@@ -373,6 +373,8 @@ gitbay repo mirror add you/copy https://github.com/them/theirs.git \
373 --direction pull # follow upstream; local pushes refused 373 --direction pull # follow upstream; local pushes refused
374gitbay repo mirror list # sync status and last error, per mirror 374gitbay repo mirror list # sync status and last error, per mirror
375gitbay repo mirror sync / remove <id> 375gitbay 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
376#+end_src 378#+end_src
377 379
378Markdown and org files render on the web when opened, the way a README 380Markdown 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 {
594 pass("add", passOpts{server: []string{"repo", "mirror", "add"}, needsRepo: true, stdinOK: true}), 594 pass("add", passOpts{server: []string{"repo", "mirror", "add"}, needsRepo: true, stdinOK: true}),
595 pass("list", passOpts{server: []string{"repo", "mirror", "list"}, needsRepo: true}), 595 pass("list", passOpts{server: []string{"repo", "mirror", "list"}, needsRepo: true}),
596 pass("remove", passOpts{server: []string{"repo", "mirror", "remove"}, needsRepo: true}), 596 pass("remove", passOpts{server: []string{"repo", "mirror", "remove"}, needsRepo: true}),
597 pass("set-url", passOpts{server: []string{"repo", "mirror", "set-url"}, needsRepo: true}),
597 pass("sync", passOpts{server: []string{"repo", "mirror", "sync"}, needsRepo: true}), 598 pass("sync", passOpts{server: []string{"repo", "mirror", "sync"}, needsRepo: true}),
598 ), 599 ),
599 pass("secret-scan", passOpts{server: []string{"repo", "secret-scan"}, needsRepo: true}), 600 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{
185 "repo mirror add": "mirror to or from a remote", 185 "repo mirror add": "mirror to or from a remote",
186 "repo mirror list": "list mirrors with sync status", 186 "repo mirror list": "list mirrors with sync status",
187 "repo mirror remove": "remove a mirror", 187 "repo mirror remove": "remove a mirror",
188 "repo mirror set-url": "point a mirror at a new URL on the same host",
188 "repo mirror sync": "schedule an immediate sync", 189 "repo mirror sync": "schedule an immediate sync",
189 "repo mute": "mute a repository, including work you are part of", 190 "repo mute": "mute a repository, including work you are part of",
190 "repo pin": "pin a repository to your dashboard", 191 "repo pin": "pin a repository to your dashboard",
internal/control/mirrorcmd.go +59 −1
@@ -26,13 +26,19 @@ func init() {
26 {"--username", "<u>", "the remote's username", ""}, 26 {"--username", "<u>", "the remote's username", ""},
27 {"--token-stdin", "", "read a credential token from stdin", ""}, 27 {"--token-stdin", "", "read a credential token from stdin", ""},
28 }, 28 },
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"},
30 ReadsStdin: true, Run: runMirrorAdd}) 30 ReadsStdin: true, Run: runMirrorAdd})
31 register(Command{Path: []string{"repo", "mirror", "list"}, 31 register(Command{Path: []string{"repo", "mirror", "list"},
32 Summary: "list mirrors with sync status", 32 Summary: "list mirrors with sync status",
33 Usage: "repo mirror list <owner/name>", 33 Usage: "repo mirror list <owner/name>",
34 Examples: []string{"repo mirror list krz/gitbay"}, 34 Examples: []string{"repo mirror list krz/gitbay"},
35 ReadOnly: true, Run: runMirrorList}) 35 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})
36 register(Command{Path: []string{"repo", "mirror", "remove"}, 42 register(Command{Path: []string{"repo", "mirror", "remove"},
37 Summary: "remove a mirror", 43 Summary: "remove a mirror",
38 Usage: "repo mirror remove <owner/name> <id>", 44 Usage: "repo mirror remove <owner/name> <id>",
@@ -165,6 +171,58 @@ func orDash(s string) string {
165 return s 171 return s
166} 172}
167 173
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
168func runMirrorRemove(c *Ctx, args []string) int { 226func runMirrorRemove(c *Ctx, args []string) int {
169 return runRepoItemAction(c, args, "mirror", c.Store.RemoveMirror, "removed", "removed mirror %d") 227 return runRepoItemAction(c, args, "mirror", c.Store.RemoveMirror, "removed", "removed mirror %d")
170} 228}
internal/control/mirrorcmd_test.go +37
@@ -1,6 +1,7 @@
1package control 1package control
2 2
3import ( 3import (
4 "strconv"
4 "strings" 5 "strings"
5 "testing" 6 "testing"
6 7
@@ -27,3 +28,39 @@ func TestMirrorAddRefusesNumericHost(t *testing.T) {
27 } 28 }
28 } 29 }
29} 30}
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() {
28 register(Command{Path: []string{"org", "rename"}, 28 register(Command{Path: []string{"org", "rename"},
29 Summary: "rename an organization", 29 Summary: "rename an organization",
30 Usage: "org rename <old> <new> (clone URLs change)", 30 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})
32 register(Command{Path: []string{"org", "delete"}, 32 register(Command{Path: []string{"org", "delete"},
33 Summary: "delete an empty organization", 33 Summary: "delete an empty organization",
34 Usage: "org delete <name> --yes", 34 Usage: "org delete <name> --yes",
internal/control/reauth_test.go +1
@@ -125,6 +125,7 @@ func TestNeedsRecentSignInSet(t *testing.T) {
125 "repo delete", 125 "repo delete",
126 "repo deploy-key add", 126 "repo deploy-key add",
127 "repo mirror add", 127 "repo mirror add",
128 "repo mirror set-url",
128 "repo rename", 129 "repo rename",
129 "repo runner add", 130 "repo runner add",
130 "repo secret set", 131 "repo secret set",
internal/control/repo.go +1 −1
@@ -54,7 +54,7 @@ func init() {
54 NeedsRecentSignIn: true, 54 NeedsRecentSignIn: true,
55 Summary: "move a repository to another owner", 55 Summary: "move a repository to another owner",
56 Usage: "repo transfer <owner/name> <new-owner> (clone URLs change)", 56 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"},
58 Run: runRepoTransfer}) 58 Run: runRepoTransfer})
59 register(Command{Path: []string{"repo", "rename"}, 59 register(Command{Path: []string{"repo", "rename"},
60 NeedsRecentSignIn: true, 60 NeedsRecentSignIn: true,
internal/store/mirrors.go +16
@@ -102,6 +102,22 @@ func (s *Store) RemoveMirror(repoID, id int64) error {
102 return nil 102 return nil
103} 103}
104 104
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
105// MarkMirrorsDirty schedules a sync. An empty direction marks both. 121// MarkMirrorsDirty schedules a sync. An empty direction marks both.
106func (s *Store) MarkMirrorsDirty(repoID int64, direction string) error { 122func (s *Store) MarkMirrorsDirty(repoID int64, direction string) error {
107 q := "UPDATE mirrors SET dirty = 1 WHERE repo_id = ?" 123 q := "UPDATE mirrors SET dirty = 1 WHERE repo_id = ?"