Skip to content

Commit db75925

Browse files
committed
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.
1 parent ed35744 commit db75925

File tree

2 files changed

+56
-15
lines changed

2 files changed

+56
-15
lines changed

src/cargo/sources/registry/mod.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -546,10 +546,14 @@ mod index;
546546
mod local;
547547
mod remote;
548548

549-
fn short_name(id: SourceId) -> String {
549+
fn short_name(id: SourceId, is_shallow: bool) -> String {
550550
let hash = hex::short_hash(&id);
551551
let ident = id.url().host_str().unwrap_or("").to_string();
552-
format!("{}-{}", ident, hash)
552+
let mut name = format!("{}-{}", ident, hash);
553+
if is_shallow {
554+
name.push_str("-shallow");
555+
}
556+
name
553557
}
554558

555559
impl<'cfg> RegistrySource<'cfg> {
@@ -559,7 +563,14 @@ impl<'cfg> RegistrySource<'cfg> {
559563
config: &'cfg Config,
560564
) -> CargoResult<RegistrySource<'cfg>> {
561565
assert!(source_id.is_remote_registry());
562-
let name = short_name(source_id);
566+
let name = short_name(
567+
source_id,
568+
config
569+
.cli_unstable()
570+
.gitoxide
571+
.map_or(false, |gix| gix.fetch && gix.shallow_index)
572+
&& !source_id.is_sparse(),
573+
);
563574
let ops = if source_id.is_sparse() {
564575
Box::new(http_remote::HttpRegistry::new(source_id, config, &name)?) as Box<_>
565576
} else {
@@ -581,7 +592,7 @@ impl<'cfg> RegistrySource<'cfg> {
581592
yanked_whitelist: &HashSet<PackageId>,
582593
config: &'cfg Config,
583594
) -> RegistrySource<'cfg> {
584-
let name = short_name(source_id);
595+
let name = short_name(source_id, false);
585596
let ops = local::LocalRegistry::new(path, config, &name);
586597
RegistrySource::new(source_id, config, &name, Box::new(ops), yanked_whitelist)
587598
}

tests/testsuite/git.rs

+41-11
Original file line numberDiff line numberDiff line change
@@ -1868,30 +1868,31 @@ fn gitoxide_clones_registry_with_shallow_protocol_and_follow_up_with_git2_fetch(
18681868
.masquerade_as_nightly_cargo(&["unstable features must be available for -Z gitoxide"])
18691869
.run();
18701870

1871-
let repo = gix::open_opts(find_index(), gix::open::Options::isolated())?;
1871+
let shallow_repo = gix::open_opts(find_index(), gix::open::Options::isolated())?;
18721872
assert_eq!(
1873-
repo.rev_parse_single("origin/HEAD")?
1873+
shallow_repo
1874+
.rev_parse_single("origin/HEAD")?
18741875
.ancestors()
18751876
.all()?
18761877
.count(),
18771878
1,
18781879
"shallow clones always start at depth of 1 to minimize download size"
18791880
);
1880-
assert!(repo.is_shallow());
1881+
assert!(shallow_repo.is_shallow());
18811882

18821883
Package::new("bar", "1.1.0").publish();
18831884
p.cargo("update")
18841885
.env("__CARGO_USE_GITOXIDE_INSTEAD_OF_GIT2", "0")
18851886
.run();
18861887

1888+
let repo = gix::open_opts(find_remote_index(false), gix::open::Options::isolated())?;
18871889
assert_eq!(
18881890
repo.rev_parse_single("origin/HEAD")?
18891891
.ancestors()
18901892
.all()?
18911893
.count(),
18921894
3,
1893-
"the repo was forcefully reinitialized and fetch again with full history - that way we take control and know the state of the repo \
1894-
instead of allowing a non-shallow aware implementation to cause trouble later"
1895+
"an entirely new repo was cloned which is never shallow"
18951896
);
18961897
assert!(!repo.is_shallow());
18971898
Ok(())
@@ -2220,7 +2221,7 @@ fn gitoxide_clones_registry_with_shallow_protocol_and_follow_up_fetch_maintains_
22202221

22212222
Package::new("bar", "1.1.0").publish();
22222223
p.cargo("update")
2223-
.arg("-Zgitoxide=fetch") // NOTE: intentionally missing shallow flag
2224+
.arg("-Zgitoxide=fetch,shallow-index") // NOTE: the flag needs to be consistent or else a different index is created
22242225
.masquerade_as_nightly_cargo(&["unstable features must be available for -Z gitoxide"])
22252226
.run();
22262227

@@ -2254,6 +2255,20 @@ fn gitoxide_clones_registry_with_shallow_protocol_and_follow_up_fetch_maintains_
22542255
Ok(())
22552256
}
22562257

2258+
pub fn find_remote_index(shallow: bool) -> std::path::PathBuf {
2259+
glob::glob(
2260+
paths::home()
2261+
.join(".cargo/registry/index/*")
2262+
.to_str()
2263+
.unwrap(),
2264+
)
2265+
.unwrap()
2266+
.map(Result::unwrap)
2267+
.filter(|p| p.to_string_lossy().ends_with("-shallow") == shallow)
2268+
.next()
2269+
.unwrap()
2270+
}
2271+
22572272
#[cargo_test]
22582273
fn gitoxide_clones_registry_without_shallow_protocol_and_follow_up_fetch_uses_shallowness(
22592274
) -> anyhow::Result<()> {
@@ -2294,15 +2309,16 @@ fn gitoxide_clones_registry_without_shallow_protocol_and_follow_up_fetch_uses_sh
22942309
.masquerade_as_nightly_cargo(&["unstable features must be available for -Z gitoxide"])
22952310
.run();
22962311

2312+
let shallow_repo = gix::open_opts(find_remote_index(true), gix::open::Options::isolated())?;
22972313
assert_eq!(
2298-
repo.rev_parse_single("origin/HEAD")?
2314+
shallow_repo.rev_parse_single("origin/HEAD")?
22992315
.ancestors()
23002316
.all()?
23012317
.count(),
23022318
1,
2303-
"follow-up fetches maintain can shallow an existing unshallow repo - this doesn't have any benefit as we still have the objects locally"
2319+
"the follow up clones an entirely new index which is now shallow and which is in its own location"
23042320
);
2305-
assert!(repo.is_shallow());
2321+
assert!(shallow_repo.is_shallow());
23062322

23072323
Package::new("bar", "1.2.0").publish();
23082324
Package::new("bar", "1.3.0").publish();
@@ -2312,14 +2328,28 @@ fn gitoxide_clones_registry_without_shallow_protocol_and_follow_up_fetch_uses_sh
23122328
.run();
23132329

23142330
assert_eq!(
2315-
repo.rev_parse_single("origin/HEAD")?
2331+
shallow_repo.rev_parse_single("origin/HEAD")?
23162332
.ancestors()
23172333
.all()?
23182334
.count(),
23192335
3,
23202336
"even if depth (at remote) is specified again, the current shallow boundary is maintained and not moved"
23212337
);
2322-
assert!(repo.is_shallow());
2338+
assert!(shallow_repo.is_shallow());
2339+
2340+
p.cargo("update")
2341+
.arg("-Zgitoxide=fetch")
2342+
.masquerade_as_nightly_cargo(&["unstable features must be available for -Z gitoxide"])
2343+
.run();
2344+
2345+
assert_eq!(
2346+
repo.rev_parse_single("origin/HEAD")?
2347+
.ancestors()
2348+
.all()?
2349+
.count(),
2350+
5,
2351+
"we can separately fetch the non-shallow index as well and it sees all commits"
2352+
);
23232353

23242354
Ok(())
23252355
}

0 commit comments

Comments
 (0)