From 2f57e63a640810c31ff704d38d74ccbba05f8ebb Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 20:33:58 +0100 Subject: [PATCH] fix(remote): refuse a repository with a remote as a path remote A push to a repository on a local path left out the snapshot objects this node holds whole, taking the remote to hold them too. A clone doesn't: it only holds the snapshots it read, so the push failed the remote's dependency checks, and a clone of it failed to fetch. A workspace now refuses, as its remote or the one it clones, a repository on a path that has a remote of its own. `origin ci`, which opens the working copy's own repository in-process, still can. --- apps/origin-src/src/workspace/mod.rs | 10 +++++++++- apps/origin-src/src/workspace/remote.rs | 22 ++++++++++++++++++---- apps/origin-src/tests/sync.rs | 21 +++++++++++++++++++++ 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/apps/origin-src/src/workspace/mod.rs b/apps/origin-src/src/workspace/mod.rs index 2ff899d..1fe5a5f 100644 --- a/apps/origin-src/src/workspace/mod.rs +++ b/apps/origin-src/src/workspace/mod.rs @@ -101,7 +101,7 @@ use crate::output::FileDiff; use crate::plugin::Context; use crate::policy::{Policy, PolicyError}; use crate::queue::{LandError, Queue}; -use crate::remote::{Remote, RemoteError}; +use crate::remote::{Remote, RemoteError, RemoteUrl}; use crate::service::{Caller, Node, OriginService, ServiceError}; use crate::store::{Store, StoreError}; use crate::sync::SyncError; @@ -1636,6 +1636,14 @@ pub enum WorkspaceError { /// A command needs a remote, and the repository has none. #[error("this repository has no remote: set one with `origin remote `")] NoRemote, + /// The remote is a repository on a local path that has a remote of its + /// own, such as a clone: it only holds the snapshots it read, and + /// syncing takes a remote to hold them all. + #[error( + "{0} has a remote of its own, so it lacks the snapshots it never read: use that remote \ + instead" + )] + RemoteWithRemote(RemoteUrl), /// `clone` was given no path, and the URL names no directory. #[error("cannot tell the repository's name from its URL: pass a path")] Unnamed, diff --git a/apps/origin-src/src/workspace/remote.rs b/apps/origin-src/src/workspace/remote.rs index a128705..5719d46 100644 --- a/apps/origin-src/src/workspace/remote.rs +++ b/apps/origin-src/src/workspace/remote.rs @@ -2,7 +2,8 @@ //! fetching the snapshots a lazy clone lacks. //! //! A repository has at most one remote, named in `.origin/remote`, which -//! its workspaces share. A clone holds the chains of the targets it +//! its workspaces share. One on a local path has none of its own, so it +//! holds every snapshot. A clone holds the chains of the targets it //! follows, the records of the changes it knows, and only the snapshots it //! read: whenever a command needs one it lacks, such as a checkout, it is //! fetched from the remote, skipping what the working copy already holds. @@ -76,8 +77,7 @@ impl Workspace { .last() .map(Path::to_owned); Self::claim(&root)?; - let cloned = Remote::open_for(&url, &config) - .map_err(WorkspaceError::from) + let cloned = Self::open_remote(&url, &config) .and_then(|remote| Self::clone_into(root.clone(), &url, remote, target, config, actor)); if cloned.is_err() { // Best effort: the error says what went wrong. @@ -234,12 +234,26 @@ impl Workspace { return Ok(remote.as_ref()); } let remote = match Self::remote_url(&self.store)? { - Some(url) => Some(Remote::open_for(&url, &self.config)?), + Some(url) => Some(Self::open_remote(&url, &self.config)?), None => None, }; Ok(self.remote.get_or_init(|| remote).as_ref()) } + /// Opens the remote at `url` for the user of `config`, refusing a + /// repository on a local path that has a remote of its own, such as a + /// clone: it only holds the snapshots it read, and syncing takes a + /// remote to hold the snapshot of every record it has. + fn open_remote(url: &RemoteUrl, config: &UserConfig) -> Result { + let remote = Remote::open_for(url, config)?; + if let Remote::Local(service) = &remote + && Self::remote_url(service.store())?.is_some() + { + return Err(WorkspaceError::RemoteWithRemote(url.clone())); + } + Ok(remote) + } + /// Syncs with `remote`, as this node, for its user. pub(super) fn sync<'s>(&'s self, remote: &'s Remote) -> Sync<'s> { Sync::new( diff --git a/apps/origin-src/tests/sync.rs b/apps/origin-src/tests/sync.rs index d4d7ccf..6295fe7 100644 --- a/apps/origin-src/tests/sync.rs +++ b/apps/origin-src/tests/sync.rs @@ -257,3 +257,24 @@ fn a_clone_converges_heads_whose_merge_base_it_never_read() { "{file}" ); } + +#[test] +fn a_repository_with_a_remote_is_no_remote() { + let ada = User::new("ada"); + let origin = Repo::init(&ada); + let clone = Repo::clone(&ada, origin.root().to_str().unwrap(), &[]); + let url = clone.root().to_str().unwrap(); + // The clone lacks snapshots a push to it would take it to have. + let dir = tempfile::tempdir().unwrap(); + let cloned = ada + .command(dir.path(), &["clone", url, "again"]) + .output() + .unwrap(); + let error = String::from_utf8_lossy(&cloned.stderr); + assert!(error.contains("has a remote of its own"), "{error}"); + assert!(!dir.path().join("again").exists()); + let other = Repo::init(&ada); + other.ok(&["remote", url]); + let error = other.fails(&["push"]); + assert!(error.contains("has a remote of its own"), "{error}"); +}