Skip to content

Commit e2fe395

Browse files
weihangloByron
authored andcommitted
refactor: determine how shallow clone is handled inside fetch fn
I think `RemoteKind::to_shallow_setting` should be an implementation detail of `fetch` fn.
1 parent 8b3508c commit e2fe395

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

src/cargo/sources/git/utils.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,12 @@ impl GitRemote {
9898
// populated the database with the latest version of `reference`, so
9999
// return that database and the rev we resolve to.
100100
if let Some(mut db) = db {
101-
let shallow =
102-
RemoteKind::GitDependency.to_shallow_setting(db.repo.is_shallow(), cargo_config);
103101
fetch(
104102
&mut db.repo,
105103
self.url.as_str(),
106104
reference,
107105
cargo_config,
108-
shallow,
106+
RemoteKind::GitDependency,
109107
locked_rev,
110108
)
111109
.with_context(|| format!("failed to fetch into: {}", into.display()))?;
@@ -127,13 +125,12 @@ impl GitRemote {
127125
}
128126
paths::create_dir_all(into)?;
129127
let mut repo = init(into, true)?;
130-
let shallow = RemoteKind::GitDependency.to_shallow_setting(repo.is_shallow(), cargo_config);
131128
fetch(
132129
&mut repo,
133130
self.url.as_str(),
134131
reference,
135132
cargo_config,
136-
shallow,
133+
RemoteKind::GitDependency,
137134
locked_rev,
138135
)
139136
.with_context(|| format!("failed to clone into: {}", into.display()))?;
@@ -456,9 +453,15 @@ impl<'a> GitCheckout<'a> {
456453
cargo_config
457454
.shell()
458455
.status("Updating", format!("git submodule `{}`", url))?;
459-
let shallow =
460-
RemoteKind::GitDependency.to_shallow_setting(repo.is_shallow(), cargo_config);
461-
fetch(&mut repo, &url, &reference, cargo_config, shallow, None).with_context(|| {
456+
fetch(
457+
&mut repo,
458+
&url,
459+
&reference,
460+
cargo_config,
461+
RemoteKind::GitDependency,
462+
None,
463+
)
464+
.with_context(|| {
462465
format!(
463466
"failed to fetch submodule `{}` from {}",
464467
child.name().unwrap_or(""),
@@ -837,7 +840,7 @@ pub fn fetch(
837840
orig_url: &str,
838841
reference: &GitReference,
839842
config: &Config,
840-
shallow: gix::remote::fetch::Shallow,
843+
remote_kind: RemoteKind,
841844
locked_rev: Option<git2::Oid>,
842845
) -> CargoResult<()> {
843846
if config.frozen() {
@@ -850,6 +853,9 @@ pub fn fetch(
850853
anyhow::bail!("can't update a git repository in the offline mode")
851854
}
852855

856+
let shallow = remote_kind.to_shallow_setting(repo.is_shallow(), config);
857+
let is_shallow = !matches!(shallow, gix::remote::fetch::Shallow::NoChange);
858+
853859
// If we're fetching from GitHub, attempt GitHub's special fast path for
854860
// testing if we've already got an up-to-date copy of the repository.
855861
let is_shallow = !matches!(shallow, gix::remote::fetch::Shallow::NoChange);
@@ -878,9 +884,7 @@ pub fn fetch(
878884
// The `+` symbol on the refspec means to allow a forced (fast-forward)
879885
// update which is needed if there is ever a force push that requires a
880886
// fast-forward.
881-
if let Some(rev) =
882-
locked_rev.filter(|_| !matches!(shallow, gix::remote::fetch::Shallow::NoChange))
883-
{
887+
if let Some(rev) = locked_rev.filter(|_| is_shallow) {
884888
// If we want a specific revision and know about, obtain that specifically.
885889
refspecs.push(format!("+{0}:refs/remotes/origin/HEAD", rev));
886890
} else {

src/cargo/sources/registry/remote.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,13 +305,12 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
305305
// checkout.
306306
let url = self.source_id.url();
307307
let repo = self.repo.borrow_mut().unwrap();
308-
let shallow = RemoteKind::Registry.to_shallow_setting(repo.is_shallow(), self.config);
309308
git::fetch(
310309
repo,
311310
url.as_str(),
312311
&self.index_git_ref,
313312
self.config,
314-
shallow,
313+
RemoteKind::Registry,
315314
None,
316315
)
317316
.with_context(|| format!("failed to fetch `{}`", url))?;

0 commit comments

Comments
 (0)