diff --git a/src/cargo/sources/git/utils.rs b/src/cargo/sources/git/utils.rs index 33093e2d160..d187f586ed3 100644 --- a/src/cargo/sources/git/utils.rs +++ b/src/cargo/sources/git/utils.rs @@ -12,6 +12,7 @@ use anyhow::{anyhow, Context as _}; use cargo_util::{paths, ProcessBuilder}; use curl::easy::List; use git2::{ErrorClass, ObjectType, Oid}; +use gix::protocol::fetch::Shallow; use serde::ser; use serde::Serialize; use std::borrow::Cow; @@ -1031,7 +1032,7 @@ pub fn fetch( } let result = if let Some(true) = gctx.net_config()?.git_fetch_with_cli { - fetch_with_cli(repo, remote_url, &refspecs, tags, gctx) + fetch_with_cli(repo, remote_url, &refspecs, tags, shallow, gctx) } else if gctx.cli_unstable().gitoxide.map_or(false, |git| git.fetch) { fetch_with_gitoxide(repo, remote_url, refspecs, tags, shallow, gctx) } else { @@ -1075,6 +1076,7 @@ fn fetch_with_cli( url: &str, refspecs: &[String], tags: bool, + shallow: Shallow, gctx: &GlobalContext, ) -> CargoResult<()> { let mut cmd = ProcessBuilder::new("git"); @@ -1084,6 +1086,17 @@ fn fetch_with_cli( } else { cmd.arg("--no-tags"); } + + match shallow { + Shallow::NoChange => {} + Shallow::DepthAtRemote(depth) => { + cmd.arg(format!("--depth={depth}")); + } + Shallow::Deepen(_) | Shallow::Exclude { .. } | Shallow::Since { .. } => { + unimplemented!("Cargo only supports Shallow::NoChange and Shallow::DepthAtRemote") + } + } + match gctx.shell().verbosity() { Verbosity::Normal => {} Verbosity::Verbose => { @@ -1119,7 +1132,7 @@ fn fetch_with_gitoxide( remote_url: &str, refspecs: Vec, tags: bool, - shallow: gix::remote::fetch::Shallow, + shallow: Shallow, gctx: &GlobalContext, ) -> CargoResult<()> { let git2_repo = repo; @@ -1227,7 +1240,7 @@ fn fetch_with_libgit2( remote_url: &str, refspecs: Vec, tags: bool, - shallow: gix::remote::fetch::Shallow, + shallow: Shallow, gctx: &GlobalContext, ) -> CargoResult<()> { debug!("doing a fetch for {remote_url}"); diff --git a/tests/testsuite/git_shallow.rs b/tests/testsuite/git_shallow.rs index 86291714cd9..80f3145dbe7 100644 --- a/tests/testsuite/git_shallow.rs +++ b/tests/testsuite/git_shallow.rs @@ -7,6 +7,7 @@ use crate::git_gc::find_index; enum Backend { Git2, Gitoxide, + GitCli, } impl Backend { @@ -14,6 +15,7 @@ impl Backend { match self { Backend::Git2 => "", Backend::Gitoxide => "-Zgitoxide=fetch", + Backend::GitCli => "", } } } @@ -42,6 +44,16 @@ fn git2_fetch_complete_dep_two_revs() { fetch_dep_two_revs(Backend::Git2, RepoMode::Complete) } +#[cargo_test] +fn git_cli_fetch_complete_dep_two_revs() { + fetch_dep_two_revs(Backend::GitCli, RepoMode::Complete); +} + +#[cargo_test] +fn git_cli_fetch_shallow_dep_two_revs() { + fetch_dep_two_revs(Backend::GitCli, RepoMode::Shallow); +} + fn fetch_dep_two_revs(backend: Backend, mode: RepoMode) { let bar = git::new("meta-dep", |project| { project