Skip to content

Commit

Permalink
place shallow git dependencies into a different directory.
Browse files Browse the repository at this point in the history
That way, we avoid any danger with older cargo's not being able
to handle such a repository correctly.
  • Loading branch information
Byron committed Apr 1, 2023
1 parent e68a039 commit 518b909
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
16 changes: 15 additions & 1 deletion src/cargo/sources/git/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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())?;
Expand Down
45 changes: 32 additions & 13 deletions tests/testsuite/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?
Expand Down Expand Up @@ -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,
Expand All @@ -2168,8 +2181,14 @@ fn gitoxide_clones_git_dependency_with_shallow_protocol_and_follow_up_fetch_main
.unwrap(),
)?
.map(|path| -> anyhow::Result<usize> {
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)
})
Expand All @@ -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(())
Expand Down

0 comments on commit 518b909

Please sign in to comment.