From ec610167c20ece6d14c6639a1d3b63bb23573d33 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Mon, 27 Oct 2025 15:00:53 -0400 Subject: [PATCH 1/3] test(git): extract setting `-Zgit=shallow-index` to a method Co-authored-by: Ed Page --- tests/testsuite/git_shallow.rs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/tests/testsuite/git_shallow.rs b/tests/testsuite/git_shallow.rs index 959e30990c6..91abd7356d1 100644 --- a/tests/testsuite/git_shallow.rs +++ b/tests/testsuite/git_shallow.rs @@ -40,6 +40,13 @@ impl RepoMode { RepoMode::Shallow => "-Zgit=shallow-deps", } } + + fn to_index_arg(&self) -> &'static str { + match self { + RepoMode::Complete => "", + RepoMode::Shallow => "-Zgit=shallow-index", + } + } } #[cargo_test] @@ -335,7 +342,7 @@ fn fetch_shallow_index_then_fetch_complete( .build(); p.cargo("fetch") .arg_line(backend_1st.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") @@ -583,7 +590,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") @@ -604,7 +611,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") @@ -625,7 +632,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") @@ -694,7 +701,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") @@ -720,7 +727,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") @@ -800,7 +807,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") @@ -826,7 +833,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") From ff4d929ae1e9e2e85d59fa00c4e447fcaede2244 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Mon, 27 Oct 2025 22:07:29 -0400 Subject: [PATCH 2/3] test(git): generialize fetch-index tests Co-authored-by: Ed Page --- tests/testsuite/git_shallow.rs | 77 +++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 29 deletions(-) diff --git a/tests/testsuite/git_shallow.rs b/tests/testsuite/git_shallow.rs index 91abd7356d1..67cb717ab84 100644 --- a/tests/testsuite/git_shallow.rs +++ b/tests/testsuite/git_shallow.rs @@ -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, @@ -28,6 +29,7 @@ impl Backend { } } +#[derive(Copy, Clone, Debug)] enum RepoMode { Shallow, Complete, @@ -47,6 +49,27 @@ impl RepoMode { 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] @@ -313,17 +336,29 @@ 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 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, + ) } -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() @@ -342,47 +377,31 @@ fn fetch_shallow_index_then_fetch_complete( .build(); p.cargo("fetch") .arg_line(backend_1st.to_arg()) - .arg(RepoMode::Shallow.to_index_arg()) + .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, bar@1.0.0 + 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, bar@1.0.0, and bar@1.1.0 + mode_2nd.assert_index(&repo, 1, complete_depth); + Ok(()) } From bd1a02ce9a47194a0132851577dc16698a2f5abc Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Mon, 27 Oct 2025 22:20:42 -0400 Subject: [PATCH 3/3] test(git): add more fetch-index backend interop Co-authored-by: Ed Page --- tests/testsuite/git_shallow.rs | 60 ++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/tests/testsuite/git_shallow.rs b/tests/testsuite/git_shallow.rs index 67cb717ab84..49fbb60e1dd 100644 --- a/tests/testsuite/git_shallow.rs +++ b/tests/testsuite/git_shallow.rs @@ -344,6 +344,36 @@ fn gitoxide_fetch_shallow_index_then_git2_fetch_complete() -> anyhow::Result<()> ) } +#[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_index_then_fetch( @@ -354,6 +384,36 @@ fn git_cli_fetch_shallow_index_then_git2_fetch_complete() -> anyhow::Result<()> ) } +#[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_index_then_fetch( backend_1st: Backend, mode_1st: RepoMode,