Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 122 additions & 36 deletions tests/testsuite/git_shallow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use cargo_test_support::{basic_manifest, git, paths, project};

use crate::git_gc::find_index;

#[derive(Copy, Clone, Debug)]
enum Backend {
Git2,
Gitoxide,
Expand All @@ -28,6 +29,7 @@ impl Backend {
}
}

#[derive(Copy, Clone, Debug)]
enum RepoMode {
Shallow,
Complete,
Expand All @@ -40,6 +42,34 @@ impl RepoMode {
RepoMode::Shallow => "-Zgit=shallow-deps",
}
}

fn to_index_arg(&self) -> &'static str {
match self {
RepoMode::Complete => "",
RepoMode::Shallow => "-Zgit=shallow-index",
}
}

#[track_caller]
fn assert_index(self, repo: &gix::Repository, shallow_depth: usize, complete_depth: usize) {
let commit_count = repo
.rev_parse_single("origin/HEAD")
.unwrap()
.ancestors()
.all()
.unwrap()
.count();
match self {
RepoMode::Shallow => {
assert_eq!(commit_count, shallow_depth,);
assert!(repo.is_shallow());
}
RepoMode::Complete => {
assert_eq!(commit_count, complete_depth,);
assert!(!repo.is_shallow());
}
}
}
}

#[cargo_test]
Expand Down Expand Up @@ -306,17 +336,89 @@ fn fetch_shallow_dep_branch_to_rev(backend: Backend) -> anyhow::Result<()> {

#[cargo_test]
fn gitoxide_fetch_shallow_index_then_git2_fetch_complete() -> anyhow::Result<()> {
fetch_shallow_index_then_fetch_complete(Backend::Gitoxide, Backend::Git2)
fetch_index_then_fetch(
Backend::Gitoxide,
RepoMode::Shallow,
Backend::Git2,
RepoMode::Complete,
)
}

#[cargo_test]
fn gitoxide_fetch_shallow_index_then_git_cli_fetch_shallow() -> anyhow::Result<()> {
fetch_index_then_fetch(
Backend::Gitoxide,
RepoMode::Shallow,
Backend::GitCli,
RepoMode::Shallow,
)
}

#[cargo_test]
fn gitoxide_fetch_complete_index_then_git_cli_fetch_shallow() -> anyhow::Result<()> {
fetch_index_then_fetch(
Backend::Gitoxide,
RepoMode::Complete,
Backend::GitCli,
RepoMode::Shallow,
)
}

#[cargo_test]
fn gitoxide_fetch_shallow_index_then_git_cli_fetch_complete() -> anyhow::Result<()> {
fetch_index_then_fetch(
Backend::Gitoxide,
RepoMode::Shallow,
Backend::GitCli,
RepoMode::Complete,
)
}

#[cargo_test]
fn git_cli_fetch_shallow_index_then_git2_fetch_complete() -> anyhow::Result<()> {
fetch_shallow_index_then_fetch_complete(Backend::GitCli, Backend::Git2)
fetch_index_then_fetch(
Backend::GitCli,
RepoMode::Shallow,
Backend::Git2,
RepoMode::Complete,
)
}

#[cargo_test]
fn git_cli_fetch_shallow_index_then_gitoxide_fetch_shallow() -> anyhow::Result<()> {
fetch_index_then_fetch(
Backend::GitCli,
RepoMode::Shallow,
Backend::Gitoxide,
RepoMode::Shallow,
)
}

#[cargo_test]
fn git_cli_fetch_shallow_complete_then_gitoxide_fetch_complete() -> anyhow::Result<()> {
fetch_index_then_fetch(
Backend::GitCli,
RepoMode::Complete,
Backend::Gitoxide,
RepoMode::Shallow,
)
}

#[cargo_test]
fn git_cli_fetch_shallow_index_then_gitoxide_fetch_complete() -> anyhow::Result<()> {
fetch_index_then_fetch(
Backend::GitCli,
RepoMode::Shallow,
Backend::Gitoxide,
RepoMode::Complete,
)
}

fn fetch_shallow_index_then_fetch_complete(
fn fetch_index_then_fetch(
backend_1st: Backend,
mode_1st: RepoMode,
backend_2nd: Backend,
mode_2nd: RepoMode,
) -> anyhow::Result<()> {
Package::new("bar", "1.0.0").publish();
let p = project()
Expand All @@ -335,47 +437,31 @@ fn fetch_shallow_index_then_fetch_complete(
.build();
p.cargo("fetch")
.arg_line(backend_1st.to_arg())
.arg("-Zgit=shallow-index")
.arg_line(mode_1st.to_index_arg())
.env("__CARGO_USE_GITOXIDE_INSTEAD_OF_GIT2", "0")
.masquerade_as_nightly_cargo(&["gitoxide=fetch", "git=shallow-index"])
.env("CARGO_LOG", "git-fetch=debug")
.with_stderr_contains(backend_1st.to_trace_log())
.run();

let shallow_repo = gix::open_opts(find_index(), gix::open::Options::isolated())?;
assert_eq!(
shallow_repo
.rev_parse_single("origin/HEAD")?
.ancestors()
.all()?
.count(),
1,
"shallow fetch always start at depth of 1 to minimize download size"
);
assert!(shallow_repo.is_shallow());
let repo = gix::open_opts(find_remote_index(mode_1st), gix::open::Options::isolated())?;
let complete_depth = 2; // initial commmit, [email protected]
mode_1st.assert_index(&repo, 1, complete_depth);

Package::new("bar", "1.1.0").publish();
p.cargo("update")
.arg_line(backend_2nd.to_arg())
.arg_line(mode_2nd.to_index_arg())
.env("__CARGO_USE_GITOXIDE_INSTEAD_OF_GIT2", "0")
.masquerade_as_nightly_cargo(&["gitoxide=fetch"])
.masquerade_as_nightly_cargo(&["gitoxide=fetch", "git=shallow-index"])
.env("CARGO_LOG", "git-fetch=debug")
.with_stderr_contains(backend_2nd.to_trace_log())
.run();

let repo = gix::open_opts(
find_remote_index(RepoMode::Complete),
gix::open::Options::isolated(),
)?;
assert_eq!(
repo.rev_parse_single("origin/HEAD")?
.ancestors()
.all()?
.count(),
3,
"an entirely new repo was fetched which is never shallow"
);
assert!(!repo.is_shallow());
let repo = gix::open_opts(find_remote_index(mode_2nd), gix::open::Options::isolated())?;
let complete_depth = 3; // initial commmit, [email protected], and [email protected]
mode_2nd.assert_index(&repo, 1, complete_depth);

Ok(())
}

Expand Down Expand Up @@ -583,7 +669,7 @@ fn fetch_shallow_index_then_preserve_shallow(backend: Backend) -> anyhow::Result
.build();
p.cargo("fetch")
.arg_line(backend.to_arg())
.arg("-Zgit=shallow-index")
.arg(RepoMode::Shallow.to_index_arg())
.env("__CARGO_USE_GITOXIDE_INSTEAD_OF_GIT2", "0")
.masquerade_as_nightly_cargo(&["gitoxide=fetch", "git=shallow-index"])
.env("CARGO_LOG", "git-fetch=debug")
Expand All @@ -604,7 +690,7 @@ fn fetch_shallow_index_then_preserve_shallow(backend: Backend) -> anyhow::Result
Package::new("bar", "1.1.0").publish();
p.cargo("update")
.arg_line(backend.to_arg())
.arg("-Zgit=shallow-index") // NOTE: the flag needs to be consistent or else a different index is created
.arg(RepoMode::Shallow.to_index_arg()) // NOTE: the flag needs to be consistent or else a different index is created
.env("__CARGO_USE_GITOXIDE_INSTEAD_OF_GIT2", "0")
.masquerade_as_nightly_cargo(&["gitoxide=fetch", "git=shallow-index"])
.env("CARGO_LOG", "git-fetch=debug")
Expand All @@ -625,7 +711,7 @@ fn fetch_shallow_index_then_preserve_shallow(backend: Backend) -> anyhow::Result
Package::new("bar", "1.3.0").publish();
p.cargo("update")
.arg_line(backend.to_arg())
.arg("-Zgit=shallow-index")
.arg(RepoMode::Shallow.to_index_arg())
.env("__CARGO_USE_GITOXIDE_INSTEAD_OF_GIT2", "0")
.masquerade_as_nightly_cargo(&["gitoxide=fetch", "git=shallow-index"])
.env("CARGO_LOG", "git-fetch=debug")
Expand Down Expand Up @@ -694,7 +780,7 @@ fn fetch_complete_index_then_shallow(backend: Backend) -> anyhow::Result<()> {
Package::new("bar", "1.1.0").publish();
p.cargo("update")
.arg_line(backend.to_arg())
.arg("-Zgit=shallow-index")
.arg(RepoMode::Shallow.to_index_arg())
.env("__CARGO_USE_GITOXIDE_INSTEAD_OF_GIT2", "0")
.masquerade_as_nightly_cargo(&["gitoxide=fetch", "git=shallow-index"])
.env("CARGO_LOG", "git-fetch=debug")
Expand All @@ -720,7 +806,7 @@ fn fetch_complete_index_then_shallow(backend: Backend) -> anyhow::Result<()> {
Package::new("bar", "1.3.0").publish();
p.cargo("update")
.arg_line(backend.to_arg())
.arg("-Zgit=shallow-index")
.arg(RepoMode::Shallow.to_index_arg())
.env("__CARGO_USE_GITOXIDE_INSTEAD_OF_GIT2", "0")
.masquerade_as_nightly_cargo(&["gitoxide=fetch", "git=shallow-index"])
.env("CARGO_LOG", "git-fetch=debug")
Expand Down Expand Up @@ -800,7 +886,7 @@ fn fetch_shallow_index_then_abort_and_update(backend: Backend) -> anyhow::Result
.build();
p.cargo("fetch")
.arg_line(backend.to_arg())
.arg("-Zgit=shallow-index")
.arg(RepoMode::Shallow.to_index_arg())
.env("__CARGO_USE_GITOXIDE_INSTEAD_OF_GIT2", "0")
.masquerade_as_nightly_cargo(&["gitoxide=fetch", "git=shallow-index"])
.env("CARGO_LOG", "git-fetch=debug")
Expand All @@ -826,7 +912,7 @@ fn fetch_shallow_index_then_abort_and_update(backend: Backend) -> anyhow::Result
Package::new("bar", "1.1.0").publish();
p.cargo("update")
.arg_line(backend.to_arg())
.arg("-Zgit=shallow-index")
.arg(RepoMode::Shallow.to_index_arg())
.env("__CARGO_USE_GITOXIDE_INSTEAD_OF_GIT2", "0")
.masquerade_as_nightly_cargo(&["gitoxide=fetch", "git=shallow-index"])
.env("CARGO_LOG", "git-fetch=debug")
Expand Down