Skip to content

Commit

Permalink
name the shallow registry differently
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Byron committed Mar 15, 2023
1 parent ee2e9bf commit 0ee9c5d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 15 deletions.
19 changes: 15 additions & 4 deletions src/cargo/sources/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -559,7 +563,14 @@ impl<'cfg> RegistrySource<'cfg> {
config: &'cfg Config,
) -> CargoResult<RegistrySource<'cfg>> {
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 {
Expand All @@ -581,7 +592,7 @@ impl<'cfg> RegistrySource<'cfg> {
yanked_whitelist: &HashSet<PackageId>,
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)
}
Expand Down
52 changes: 41 additions & 11 deletions tests/testsuite/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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<()> {
Expand Down Expand Up @@ -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();
Expand All @@ -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(())
}
Expand Down

0 comments on commit 0ee9c5d

Please sign in to comment.