Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion apps/origin-src/src/workspace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 <url>`")]
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,
Expand Down
22 changes: 18 additions & 4 deletions apps/origin-src/src/workspace/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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<Remote, WorkspaceError> {
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(
Expand Down
21 changes: 21 additions & 0 deletions apps/origin-src/tests/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
}
Loading