From 0ee9c5d4e7c389d691d4d4cfc29bd196a49582f9 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Wed, 15 Mar 2023 14:44:01 +0100 Subject: [PATCH] name the shallow registry differently A couple of test expectations are adjusted accordingly. Is this desirable behaviour? Unfortunately, there is no alternative as adding shallow to an existing index most definitely breaks backwards compatibility. --- src/cargo/sources/registry/mod.rs | 19 ++++++++--- tests/testsuite/git.rs | 52 ++++++++++++++++++++++++------- 2 files changed, 56 insertions(+), 15 deletions(-) diff --git a/src/cargo/sources/registry/mod.rs b/src/cargo/sources/registry/mod.rs index 930a42f36783..83383e111486 100644 --- a/src/cargo/sources/registry/mod.rs +++ b/src/cargo/sources/registry/mod.rs @@ -546,10 +546,14 @@ mod index; mod local; mod remote; -fn short_name(id: SourceId) -> String { +fn short_name(id: SourceId, is_shallow: bool) -> String { let hash = hex::short_hash(&id); let ident = id.url().host_str().unwrap_or("").to_string(); - format!("{}-{}", ident, hash) + let mut name = format!("{}-{}", ident, hash); + if is_shallow { + name.push_str("-shallow"); + } + name } impl<'cfg> RegistrySource<'cfg> { @@ -559,7 +563,14 @@ impl<'cfg> RegistrySource<'cfg> { config: &'cfg Config, ) -> CargoResult> { assert!(source_id.is_remote_registry()); - let name = short_name(source_id); + let name = short_name( + source_id, + config + .cli_unstable() + .gitoxide + .map_or(false, |gix| gix.fetch && gix.shallow_index) + && !source_id.is_sparse(), + ); let ops = if source_id.is_sparse() { Box::new(http_remote::HttpRegistry::new(source_id, config, &name)?) as Box<_> } else { @@ -581,7 +592,7 @@ impl<'cfg> RegistrySource<'cfg> { yanked_whitelist: &HashSet, config: &'cfg Config, ) -> RegistrySource<'cfg> { - let name = short_name(source_id); + let name = short_name(source_id, false); let ops = local::LocalRegistry::new(path, config, &name); RegistrySource::new(source_id, config, &name, Box::new(ops), yanked_whitelist) } diff --git a/tests/testsuite/git.rs b/tests/testsuite/git.rs index 38a67695fd88..fb37edaaa028 100644 --- a/tests/testsuite/git.rs +++ b/tests/testsuite/git.rs @@ -1868,30 +1868,31 @@ fn gitoxide_clones_registry_with_shallow_protocol_and_follow_up_with_git2_fetch( .masquerade_as_nightly_cargo(&["unstable features must be available for -Z gitoxide"]) .run(); - let repo = gix::open_opts(find_index(), gix::open::Options::isolated())?; + let shallow_repo = gix::open_opts(find_index(), gix::open::Options::isolated())?; assert_eq!( - repo.rev_parse_single("origin/HEAD")? + shallow_repo + .rev_parse_single("origin/HEAD")? .ancestors() .all()? .count(), 1, "shallow clones always start at depth of 1 to minimize download size" ); - assert!(repo.is_shallow()); + assert!(shallow_repo.is_shallow()); Package::new("bar", "1.1.0").publish(); p.cargo("update") .env("__CARGO_USE_GITOXIDE_INSTEAD_OF_GIT2", "0") .run(); + let repo = gix::open_opts(find_remote_index(false), gix::open::Options::isolated())?; assert_eq!( repo.rev_parse_single("origin/HEAD")? .ancestors() .all()? .count(), 3, - "the repo was forcefully reinitialized and fetch again with full history - that way we take control and know the state of the repo \ - instead of allowing a non-shallow aware implementation to cause trouble later" + "an entirely new repo was cloned which is never shallow" ); assert!(!repo.is_shallow()); Ok(()) @@ -2221,7 +2222,7 @@ fn gitoxide_clones_registry_with_shallow_protocol_and_follow_up_fetch_maintains_ Package::new("bar", "1.1.0").publish(); p.cargo("update") - .arg("-Zgitoxide=fetch") // NOTE: intentionally missing shallow flag + .arg("-Zgitoxide=fetch,shallow-index") // NOTE: the flag needs to be consistent or else a different index is created .masquerade_as_nightly_cargo(&["unstable features must be available for -Z gitoxide"]) .run(); @@ -2255,6 +2256,20 @@ fn gitoxide_clones_registry_with_shallow_protocol_and_follow_up_fetch_maintains_ Ok(()) } +pub fn find_remote_index(shallow: bool) -> std::path::PathBuf { + glob::glob( + paths::home() + .join(".cargo/registry/index/*") + .to_str() + .unwrap(), + ) + .unwrap() + .map(Result::unwrap) + .filter(|p| p.to_string_lossy().ends_with("-shallow") == shallow) + .next() + .unwrap() +} + #[cargo_test] fn gitoxide_clones_registry_without_shallow_protocol_and_follow_up_fetch_uses_shallowness( ) -> anyhow::Result<()> { @@ -2295,15 +2310,16 @@ fn gitoxide_clones_registry_without_shallow_protocol_and_follow_up_fetch_uses_sh .masquerade_as_nightly_cargo(&["unstable features must be available for -Z gitoxide"]) .run(); + let shallow_repo = gix::open_opts(find_remote_index(true), gix::open::Options::isolated())?; assert_eq!( - repo.rev_parse_single("origin/HEAD")? + shallow_repo.rev_parse_single("origin/HEAD")? .ancestors() .all()? .count(), 1, - "follow-up fetches maintain can shallow an existing unshallow repo - this doesn't have any benefit as we still have the objects locally" + "the follow up clones an entirely new index which is now shallow and which is in its own location" ); - assert!(repo.is_shallow()); + assert!(shallow_repo.is_shallow()); Package::new("bar", "1.2.0").publish(); Package::new("bar", "1.3.0").publish(); @@ -2313,14 +2329,28 @@ fn gitoxide_clones_registry_without_shallow_protocol_and_follow_up_fetch_uses_sh .run(); assert_eq!( - repo.rev_parse_single("origin/HEAD")? + shallow_repo.rev_parse_single("origin/HEAD")? .ancestors() .all()? .count(), 3, "even if depth (at remote) is specified again, the current shallow boundary is maintained and not moved" ); - assert!(repo.is_shallow()); + assert!(shallow_repo.is_shallow()); + + p.cargo("update") + .arg("-Zgitoxide=fetch") + .masquerade_as_nightly_cargo(&["unstable features must be available for -Z gitoxide"]) + .run(); + + assert_eq!( + repo.rev_parse_single("origin/HEAD")? + .ancestors() + .all()? + .count(), + 5, + "we can separately fetch the non-shallow index as well and it sees all commits" + ); Ok(()) }