From e2fe3956f9bb131bb736fd95941caca5da353938 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Fri, 28 Apr 2023 22:45:16 +0100 Subject: [PATCH] refactor: determine how shallow clone is handled inside `fetch` fn I think `RemoteKind::to_shallow_setting` should be an implementation detail of `fetch` fn. --- src/cargo/sources/git/utils.rs | 28 ++++++++++++++++------------ src/cargo/sources/registry/remote.rs | 3 +-- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/cargo/sources/git/utils.rs b/src/cargo/sources/git/utils.rs index e0a66fdee..ffb7a0cd8 100644 --- a/src/cargo/sources/git/utils.rs +++ b/src/cargo/sources/git/utils.rs @@ -98,14 +98,12 @@ impl GitRemote { // populated the database with the latest version of `reference`, so // return that database and the rev we resolve to. if let Some(mut db) = db { - let shallow = - RemoteKind::GitDependency.to_shallow_setting(db.repo.is_shallow(), cargo_config); fetch( &mut db.repo, self.url.as_str(), reference, cargo_config, - shallow, + RemoteKind::GitDependency, locked_rev, ) .with_context(|| format!("failed to fetch into: {}", into.display()))?; @@ -127,13 +125,12 @@ impl GitRemote { } paths::create_dir_all(into)?; let mut repo = init(into, true)?; - let shallow = RemoteKind::GitDependency.to_shallow_setting(repo.is_shallow(), cargo_config); fetch( &mut repo, self.url.as_str(), reference, cargo_config, - shallow, + RemoteKind::GitDependency, locked_rev, ) .with_context(|| format!("failed to clone into: {}", into.display()))?; @@ -456,9 +453,15 @@ impl<'a> GitCheckout<'a> { cargo_config .shell() .status("Updating", format!("git submodule `{}`", url))?; - let shallow = - RemoteKind::GitDependency.to_shallow_setting(repo.is_shallow(), cargo_config); - fetch(&mut repo, &url, &reference, cargo_config, shallow, None).with_context(|| { + fetch( + &mut repo, + &url, + &reference, + cargo_config, + RemoteKind::GitDependency, + None, + ) + .with_context(|| { format!( "failed to fetch submodule `{}` from {}", child.name().unwrap_or(""), @@ -837,7 +840,7 @@ pub fn fetch( orig_url: &str, reference: &GitReference, config: &Config, - shallow: gix::remote::fetch::Shallow, + remote_kind: RemoteKind, locked_rev: Option, ) -> CargoResult<()> { if config.frozen() { @@ -850,6 +853,9 @@ pub fn fetch( anyhow::bail!("can't update a git repository in the offline mode") } + let shallow = remote_kind.to_shallow_setting(repo.is_shallow(), config); + let is_shallow = !matches!(shallow, gix::remote::fetch::Shallow::NoChange); + // If we're fetching from GitHub, attempt GitHub's special fast path for // testing if we've already got an up-to-date copy of the repository. let is_shallow = !matches!(shallow, gix::remote::fetch::Shallow::NoChange); @@ -878,9 +884,7 @@ pub fn fetch( // The `+` symbol on the refspec means to allow a forced (fast-forward) // update which is needed if there is ever a force push that requires a // fast-forward. - if let Some(rev) = - locked_rev.filter(|_| !matches!(shallow, gix::remote::fetch::Shallow::NoChange)) - { + if let Some(rev) = locked_rev.filter(|_| is_shallow) { // If we want a specific revision and know about, obtain that specifically. refspecs.push(format!("+{0}:refs/remotes/origin/HEAD", rev)); } else { diff --git a/src/cargo/sources/registry/remote.rs b/src/cargo/sources/registry/remote.rs index 757c7871c..febd6f49f 100644 --- a/src/cargo/sources/registry/remote.rs +++ b/src/cargo/sources/registry/remote.rs @@ -305,13 +305,12 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> { // checkout. let url = self.source_id.url(); let repo = self.repo.borrow_mut().unwrap(); - let shallow = RemoteKind::Registry.to_shallow_setting(repo.is_shallow(), self.config); git::fetch( repo, url.as_str(), &self.index_git_ref, self.config, - shallow, + RemoteKind::Registry, None, ) .with_context(|| format!("failed to fetch `{}`", url))?;