Skip to content

Commit 8120df7

Browse files
authored
feat(git): support shallow fetch for Git CLI backend (#16156)
### What does this PR try to resolve? Fixes #14956 ### How to test and review this PR? This is done with some test generalization, so that we can test more backend interop in the future when libgit2 support `file://` protocol shallow clones. In the future we should add container tests for at least gitoxide and libgit2 backend to ensure they really work with https protocol
2 parents 28b79ea + 1bb9ade commit 8120df7

File tree

2 files changed

+199
-162
lines changed

2 files changed

+199
-162
lines changed

src/cargo/sources/git/utils.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,8 +1030,9 @@ pub fn fetch(
10301030
}
10311031
}
10321032

1033+
debug!("doing a fetch for {remote_url}");
10331034
let result = if let Some(true) = gctx.net_config()?.git_fetch_with_cli {
1034-
fetch_with_cli(repo, remote_url, &refspecs, tags, gctx)
1035+
fetch_with_cli(repo, remote_url, &refspecs, tags, shallow, gctx)
10351036
} else if gctx.cli_unstable().gitoxide.map_or(false, |git| git.fetch) {
10361037
fetch_with_gitoxide(repo, remote_url, refspecs, tags, shallow, gctx)
10371038
} else {
@@ -1075,15 +1076,22 @@ fn fetch_with_cli(
10751076
url: &str,
10761077
refspecs: &[String],
10771078
tags: bool,
1079+
shallow: gix::remote::fetch::Shallow,
10781080
gctx: &GlobalContext,
10791081
) -> CargoResult<()> {
1082+
debug!(target: "git-fetch", backend = "git-cli");
1083+
10801084
let mut cmd = ProcessBuilder::new("git");
10811085
cmd.arg("fetch");
10821086
if tags {
10831087
cmd.arg("--tags");
10841088
} else {
10851089
cmd.arg("--no-tags");
10861090
}
1091+
if let gix::remote::fetch::Shallow::DepthAtRemote(depth) = shallow {
1092+
let depth = 0i32.saturating_add_unsigned(depth.get());
1093+
cmd.arg(format!("--depth={depth}"));
1094+
}
10871095
match gctx.shell().verbosity() {
10881096
Verbosity::Normal => {}
10891097
Verbosity::Verbose => {
@@ -1126,6 +1134,8 @@ fn fetch_with_gitoxide(
11261134
shallow: gix::remote::fetch::Shallow,
11271135
gctx: &GlobalContext,
11281136
) -> CargoResult<()> {
1137+
debug!(target: "git-fetch", backend = "gitoxide");
1138+
11291139
let git2_repo = repo;
11301140
let config_overrides = cargo_config_to_gitoxide_overrides(gctx)?;
11311141
let repo_reinitialized = AtomicBool::default();
@@ -1234,7 +1244,8 @@ fn fetch_with_libgit2(
12341244
shallow: gix::remote::fetch::Shallow,
12351245
gctx: &GlobalContext,
12361246
) -> CargoResult<()> {
1237-
debug!("doing a fetch for {remote_url}");
1247+
debug!(target: "git-fetch", backend = "libgit2");
1248+
12381249
let git_config = git2::Config::open_default()?;
12391250
with_fetch_options(&git_config, remote_url, gctx, &mut |mut opts| {
12401251
if tags {

0 commit comments

Comments
 (0)