From 11d8962ee2132f869c1b0861f2eec8e0d3f571a4 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Wed, 15 Mar 2023 19:11:29 +0100 Subject: [PATCH] place shallow git dependencies into a different directory. That way, we avoid any danger with older cargo's not being able to handle such a repository correctly. --- src/cargo/sources/git/source.rs | 16 +++++++++++- tests/testsuite/git.rs | 45 +++++++++++++++++++++++---------- 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/src/cargo/sources/git/source.rs b/src/cargo/sources/git/source.rs index 90c47093dce0..b549a5877588 100644 --- a/src/cargo/sources/git/source.rs +++ b/src/cargo/sources/git/source.rs @@ -29,7 +29,13 @@ impl<'cfg> GitSource<'cfg> { assert!(source_id.is_git(), "id is not git, id={}", source_id); let remote = GitRemote::new(source_id.url()); - let ident = ident(&source_id); + let ident = ident_shallow( + &source_id, + config + .cli_unstable() + .gitoxide + .map_or(false, |gix| gix.fetch && gix.shallow_deps), + ); let source = GitSource { remote, @@ -76,6 +82,14 @@ fn ident(id: &SourceId) -> String { format!("{}-{}", ident, short_hash(id.canonical_url())) } +fn ident_shallow(id: &SourceId, is_shallow: bool) -> String { + let mut ident = ident(id); + if is_shallow { + ident.push_str("-shallow"); + } + ident +} + impl<'cfg> Debug for GitSource<'cfg> { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { write!(f, "git repo at {}", self.remote.url())?; diff --git a/tests/testsuite/git.rs b/tests/testsuite/git.rs index 3982380956f1..a33f6de603bb 100644 --- a/tests/testsuite/git.rs +++ b/tests/testsuite/git.rs @@ -2092,15 +2092,20 @@ fn gitoxide_clones_git_dependency_with_shallow_protocol_and_follow_up_fetch_main .masquerade_as_nightly_cargo(&["unstable features must be available for -Z gitoxide"]) .run(); - let db_clone = gix::open_opts( - glob::glob(paths::home().join(".cargo/git/db/bar-*").to_str().unwrap())? - .next() - .unwrap()?, + let shallow_db_clone = gix::open_opts( + glob::glob( + paths::home() + .join(".cargo/git/db/bar-*-shallow") + .to_str() + .unwrap(), + )? + .next() + .unwrap()?, gix::open::Options::isolated(), )?; - assert!(db_clone.is_shallow()); + assert!(shallow_db_clone.is_shallow()); assert_eq!( - db_clone + shallow_db_clone .rev_parse_single("origin/master")? .ancestors() .all()? @@ -2145,16 +2150,24 @@ fn gitoxide_clones_git_dependency_with_shallow_protocol_and_follow_up_fetch_main .masquerade_as_nightly_cargo(&["unstable features must be available for -Z gitoxide"]) .run(); + let db_clone = gix::open_opts( + glob::glob(paths::home().join(".cargo/git/db/bar-*").to_str().unwrap())? + .map(Result::unwrap) + .filter(|p| !p.to_string_lossy().ends_with("-shallow")) + .next() + .unwrap(), + gix::open::Options::isolated(), + )?; assert_eq!( db_clone .rev_parse_single("origin/master")? .ancestors() .all()? .count(), - 2, - "the new commit was fetched into our DB clone" + 3, + "we created an entirely new non-shallow clone" ); - assert!(db_clone.is_shallow()); + assert!(!db_clone.is_shallow()); assert_eq!( dep_checkout.head_id()?.ancestors().all()?.count(), 1, @@ -2168,8 +2181,14 @@ fn gitoxide_clones_git_dependency_with_shallow_protocol_and_follow_up_fetch_main .unwrap(), )? .map(|path| -> anyhow::Result { - let dep_checkout = gix::open_opts(path?, gix::open::Options::isolated())?; - assert!(dep_checkout.is_shallow()); + let path = path?; + let dep_checkout = gix::open_opts(&path, gix::open::Options::isolated())?; + dbg!(dep_checkout.git_dir()); + assert_eq!( + dep_checkout.is_shallow(), + path.to_string_lossy().contains("-shallow"), + "checkouts of shallow db repos are shallow as well" + ); let depth = dep_checkout.head_id()?.ancestors().all()?.count(); Ok(depth) }) @@ -2178,8 +2197,8 @@ fn gitoxide_clones_git_dependency_with_shallow_protocol_and_follow_up_fetch_main .expect("two checkout repos"); assert_eq!( - max_history_depth, 2, - "the new checkout sees all commits of the DB" + max_history_depth, 3, + "we see the previous shallow checkout as well as new new unshallow one" ); Ok(())