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 <noreply@anthropic.com>
This commit is contained in:
dasemuandClaude Opus 5.5 committed 2026-11-16 21:11:24 +01:00
1 parent f150f778c3
commit 6720390e3f
7 files changed
+123 -3

No files matched your search

+5
View File
@@ -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()),
}
}
+4
View File
@@ -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),
}
}
+2
View File
@@ -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<String>,
}
impl RemoteRepo {
+21
View File
@@ -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"}}"#;
+5
View File
@@ -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(),
+81 -3
View File
@@ -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<Option<(Forge, RemoteRepo)>> {
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<Option<String>> {
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();