From 6720390e3f3e166f60cc6bf74e04ceb84ab33a57 Mon Sep 17 00:00:00 2001 From: Dasemu Date: Mon, 16 Nov 2026 21:11:24 +0100 Subject: [PATCH] fix: never use a read-only pull mirror as origin `grove add` repoints an origin that is a Gitea/GitHub pull mirror to the mirror's writable source (on a configured provider) and fetches it; `grove import` clones the source instead of the mirror, keeping the mirror's name and refusing a visibility mismatch. Co-Authored-By: Claude Opus 5.5 --- crates/grove-providers/src/gitea.rs | 5 ++ crates/grove-providers/src/github.rs | 4 ++ crates/grove-providers/src/lib.rs | 2 + crates/grove-providers/tests/api.rs | 21 +++++++ crates/grove/src/main.rs | 5 ++ crates/grove/src/workflow.rs | 84 +++++++++++++++++++++++++++- docs/phase6.md | 5 ++ 7 files changed, 123 insertions(+), 3 deletions(-) diff --git a/crates/grove-providers/src/gitea.rs b/crates/grove-providers/src/gitea.rs index a446351..574a4b4 100644 --- a/crates/grove-providers/src/gitea.rs +++ b/crates/grove-providers/src/gitea.rs @@ -32,6 +32,11 @@ fn repo(v: &Value) -> RemoteRepo { clone_url: s("clone_url"), ssh_url: s("ssh_url"), default_branch: v["default_branch"].as_str().unwrap_or("main").to_string(), + mirror_of: v["mirror"] + .as_bool() + .unwrap_or(false) + .then(|| s("original_url")) + .filter(|u| !u.is_empty()), } } diff --git a/crates/grove-providers/src/github.rs b/crates/grove-providers/src/github.rs index 6af2adf..07cc98d 100644 --- a/crates/grove-providers/src/github.rs +++ b/crates/grove-providers/src/github.rs @@ -31,6 +31,10 @@ fn repo(v: &Value) -> RemoteRepo { clone_url: v["clone_url"].as_str().unwrap_or_default().to_string(), ssh_url: v["ssh_url"].as_str().unwrap_or_default().to_string(), default_branch: v["default_branch"].as_str().unwrap_or("main").to_string(), + mirror_of: v["mirror_url"] + .as_str() + .filter(|u| !u.is_empty()) + .map(str::to_string), } } diff --git a/crates/grove-providers/src/lib.rs b/crates/grove-providers/src/lib.rs index 303bb20..762e31b 100644 --- a/crates/grove-providers/src/lib.rs +++ b/crates/grove-providers/src/lib.rs @@ -57,6 +57,8 @@ pub struct RemoteRepo { /// SSH clone URL (empty when the forge did not report one). pub ssh_url: String, pub default_branch: String, + /// Where the repo is mirrored from when it is a read-only pull mirror (pushes are refused). + pub mirror_of: Option, } impl RemoteRepo { diff --git a/crates/grove-providers/tests/api.rs b/crates/grove-providers/tests/api.rs index 8df2a7f..964e993 100644 --- a/crates/grove-providers/tests/api.rs +++ b/crates/grove-providers/tests/api.rs @@ -367,6 +367,27 @@ fn get_repo_reports_visibility_on_both_forges() { ); } +#[test] +fn pull_mirrors_report_their_source() { + let (url, _rx) = mock(vec![ + ( + 200, + r#"{"name":"r","private":false,"mirror":true,"original_url":"https://github.com/o/r","owner":{"login":"o"}}"#, + ), + ( + 200, + r#"{"name":"r","private":false,"mirror":false,"original_url":"https://github.com/o/r","owner":{"login":"o"}}"#, + ), + ]); + let g = Gitea::new(ProviderKind::Gitea, &url, "t".into()); + assert_eq!( + g.get_repo("o", "r").unwrap().mirror_of.as_deref(), + Some("https://github.com/o/r") + ); + // A migrated (no longer mirroring) repo keeps `original_url` but is writable. + assert_eq!(g.get_repo("o", "r").unwrap().mirror_of, None); +} + const REPO_A: &str = r#"{"name":"a","private":false,"clone_url":"https://h/o/a.git","ssh_url":"git@h:o/a.git","default_branch":"main","owner":{"login":"Org"}}"#; const REPO_B: &str = r#"{"name":"b","private":true,"clone_url":"https://h/me/b.git","ssh_url":"ssh://gitea@h:2222/me/b.git","default_branch":"main","owner":{"login":"me"}}"#; const REPO_C: &str = r#"{"name":"c","private":true,"clone_url":"https://h/x/c.git","ssh_url":"","default_branch":"main","owner":{"login":"x"}}"#; diff --git a/crates/grove/src/main.rs b/crates/grove/src/main.rs index a9883bc..91cd252 100644 --- a/crates/grove/src/main.rs +++ b/crates/grove/src/main.rs @@ -316,6 +316,11 @@ fn main() -> Result<()> { .context("cannot determine backup dir (set --backup-dir)")?; let r = migrate(path, &cfg.machine.name, &dir, &stamp())?; println!("migrated: {}", r.worktree.display()); + match workflow::repoint_mirror_origin(&cfg, &r.worktree) { + Ok(Some(u)) => println!("origin was a read-only mirror; now {u}"), + Ok(None) => {} + Err(e) => println!("warning: read-only mirror check of origin: {e:#}"), + } println!( "backup: {} (ref {})", r.backup_tar.display(), diff --git a/crates/grove/src/workflow.rs b/crates/grove/src/workflow.rs index 5822007..6836a4b 100644 --- a/crates/grove/src/workflow.rs +++ b/crates/grove/src/workflow.rs @@ -385,6 +385,50 @@ impl Local { } } +/// The writable source of `r` when `r` is a read-only pull mirror: the source's forge (which +/// must be a configured provider) and its repo. `Ok(None)` when `r` is no mirror. +fn mirror_source(cfg: &Config, r: &RemoteRepo) -> Result> { + let Some(src) = &r.mirror_of else { + return Ok(None); + }; + let (pid, _) = provider_for_url(cfg, src).with_context(|| { + format!( + "{}/{} is a read-only mirror of {src:?}, which is on no configured provider", + r.owner, r.name + ) + })?; + let (owner, name) = url::owner_and_name(src) + .with_context(|| format!("cannot read owner/name from mirror source {src:?}"))?; + let f = connect(cfg, pid)?; + let sr = f.api.get_repo(&owner, &name)?; + Ok(Some((f, sr))) +} + +/// Points `origin` of the repo at `path` to the writable source when it is a read-only pull +/// mirror on a configured provider (pushes to a mirror are refused), then fetches it. Returns +/// the new URL, or `None` when origin is no mirror or on no configured provider. +pub fn repoint_mirror_origin(cfg: &Config, path: &Path) -> Result> { + let g = Git::new(path); + let Some(origin) = g.probe(&["config", "--get", "remote.origin.url"])? else { + return Ok(None); + }; + let Some((pid, _)) = provider_for_url(cfg, &origin) else { + return Ok(None); + }; + let (owner, name) = url::owner_and_name(&origin) + .with_context(|| format!("cannot read owner/name from origin {origin:?}"))?; + let here = connect(cfg, pid)?; + let Some((src, sr)) = mirror_source(cfg, &here.api.get_repo(&owner, &name)?)? else { + return Ok(None); + }; + let target = src.clone_url(&sr)?; + g.run(&["remote", "set-url", "origin", &target])?; + with_env(Git::new(path), &src.git_env) + .run(&["fetch", "-q", "origin"]) + .with_context(|| format!("origin repointed to {target}, but fetching it failed"))?; + Ok(Some(target)) +} + /// Lists the group's remote repos that are not on disk; clones the named ones. pub fn import(cfg: &Config, group_name: &str, wanted: &[String]) -> Result<()> { let grp = group(cfg, group_name)?; @@ -477,16 +521,39 @@ pub fn import(cfg: &Config, group_name: &str, wanted: &[String]) -> Result<()> { }; let forge = &forges[*i].1; let res = (|| -> Result<()> { + // A read-only pull mirror is cloned from its writable source instead, under the + // mirror's name (the one listed, matched against what is on disk and registered). + let source; + let (forge, r) = match mirror_source(cfg, r)? { + Some(s) => { + source = s; + if source.1.private != r.private { + bail!( + "{}/{} is a mirror of {}/{}, whose visibility differs; import the source by hand", + r.owner, + r.name, + source.1.owner, + source.1.name + ); + } + println!( + "{w}: {}/{} is a read-only mirror; cloning its source {}/{}", + r.owner, r.name, source.1.owner, source.1.name + ); + (&source.0, &source.1) + } + None => (forge, r), + }; let url = forge.clone_url(r)?; - let dir = gdir.join(&r.name); + let dir = gdir.join(w); // Refuses a non-empty directory and cleans up after itself on failure. clone_bare_layout(&url, &dir, &forge.git_env)?; - println!("imported {} -> {}", r.name, dir.display()); + println!("imported {w} -> {}", dir.display()); register( cfg, &Entry { group: grp.name.clone(), - name: r.name.clone(), + name: w.clone(), url: forge.registry_url(r)?, }, ) @@ -745,6 +812,7 @@ mod tests { clone_url: format!("u/{name}.git"), ssh_url: String::new(), default_branch: "main".into(), + mirror_of: None, } } @@ -819,6 +887,16 @@ mod tests { assert_eq!(id("https://evil.example/dasemu/x.git"), None); } + #[test] + fn a_mirror_of_an_unconfigured_host_is_refused_and_a_plain_repo_is_no_mirror() { + let cfg = two_provider_cfg(); + assert!(mirror_source(&cfg, &r("me", "x", false)).unwrap().is_none()); + let mut m = r("me", "x", false); + m.mirror_of = Some("https://evil.example/me/x.git".into()); + let e = mirror_source(&cfg, &m).err().unwrap().to_string(); + assert!(e.contains("read-only mirror"), "{e}"); + } + #[test] fn bootstrap_refuses_entries_on_hosts_no_provider_serves() { let cfg = two_provider_cfg(); diff --git a/docs/phase6.md b/docs/phase6.md index 7257ed3..7728c75 100644 --- a/docs/phase6.md +++ b/docs/phase6.md @@ -35,6 +35,11 @@ mirror_to = ["gitea"] other owners (orgs you belong to) no longer show up in every group: give an org group its `owners` entry. If one route's provider is unreachable, the whole group's import fails. - An owner equal to the authenticated login means "my account" (`/user/repos`), not an org. +- Read-only pull mirrors (Gitea `mirror: true` + `original_url`, GitHub `mirror_url`) are never + used as `origin`: `grove import` clones the mirror's source instead, and `grove add` (also the + GUI's Migrate) repoints an existing `origin` to it and fetches. The source must be on a + configured provider and, for import, have the mirror's visibility; otherwise import fails for + that repo (it keeps the mirror's name locally) and `add` only warns. - Wizard git over HTTPS authenticates with the resolved token on both forge kinds (`http.extraHeader`, process env only; GitHub as Basic `x-access-token`).