From e04acef036a2c3835fe679757d8a0838e84cef4c Mon Sep 17 00:00:00 2001 From: Dacian Pascu Date: Sun, 17 Nov 2024 19:48:25 +0200 Subject: [PATCH] fix(git): added extra error context for a revision that doesn't exist See issue rust-lang#14621 --- src/cargo/sources/git/utils.rs | 27 ++++++++++++++++++--------- tests/testsuite/git.rs | 8 +++++++- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/cargo/sources/git/utils.rs b/src/cargo/sources/git/utils.rs index 9f96c2bf74c8..ed2d759982e3 100644 --- a/src/cargo/sources/git/utils.rs +++ b/src/cargo/sources/git/utils.rs @@ -944,6 +944,9 @@ pub fn fetch( let shallow = remote_kind.to_shallow_setting(repo.is_shallow(), gctx); + // Flag to keep track if the rev is a full commit hash + let mut fast_path_rev: bool = false; + let oid_to_fetch = match github_fast_path(repo, remote_url, reference, gctx) { Ok(FastPathRev::UpToDate) => return Ok(()), Ok(FastPathRev::NeedsFetch(rev)) => Some(rev), @@ -984,6 +987,7 @@ pub fn fetch( if rev.starts_with("refs/") { refspecs.push(format!("+{0}:{0}", rev)); } else if let Some(oid_to_fetch) = oid_to_fetch { + fast_path_rev = true; refspecs.push(format!("+{0}:refs/commit/{0}", oid_to_fetch)); } else if !matches!(shallow, gix::remote::fetch::Shallow::NoChange) && rev.parse::().is_ok() @@ -1006,15 +1010,20 @@ pub fn fetch( } } - if let Some(true) = gctx.net_config()?.git_fetch_with_cli { - return fetch_with_cli(repo, remote_url, &refspecs, tags, gctx); - } - - if gctx.cli_unstable().gitoxide.map_or(false, |git| git.fetch) { - return fetch_with_gitoxide(repo, remote_url, refspecs, tags, shallow, gctx); + let result = if let Some(true) = gctx.net_config()?.git_fetch_with_cli { + fetch_with_cli(repo, remote_url, &refspecs, tags, gctx) + } else if gctx.cli_unstable().gitoxide.map_or(false, |git| git.fetch) { + fetch_with_gitoxide(repo, remote_url, refspecs, tags, shallow, gctx) } else { - return fetch_with_libgit2(repo, remote_url, refspecs, tags, shallow, gctx); + fetch_with_libgit2(repo, remote_url, refspecs, tags, shallow, gctx) + }; + + if fast_path_rev { + if let Some(oid) = oid_to_fetch { + return result.with_context(|| format!("revision {} not found", oid)); + } } + result } /// `gitoxide` uses shallow locks to assure consistency when fetching to and to avoid races, and to write @@ -1091,7 +1100,7 @@ fn fetch_with_gitoxide( tags: bool, shallow: gix::remote::fetch::Shallow, gctx: &GlobalContext, -) -> Result<(), anyhow::Error> { +) -> CargoResult<()> { let git2_repo = repo; let config_overrides = cargo_config_to_gitoxide_overrides(gctx)?; let repo_reinitialized = AtomicBool::default(); @@ -1199,7 +1208,7 @@ fn fetch_with_libgit2( tags: bool, shallow: gix::remote::fetch::Shallow, gctx: &GlobalContext, -) -> Result<(), anyhow::Error> { +) -> CargoResult<()> { debug!("doing a fetch for {remote_url}"); let git_config = git2::Config::open_default()?; with_fetch_options(&git_config, remote_url, gctx, &mut |mut opts| { diff --git a/tests/testsuite/git.rs b/tests/testsuite/git.rs index 793e24e7124a..a697b9a72618 100644 --- a/tests/testsuite/git.rs +++ b/tests/testsuite/git.rs @@ -4086,6 +4086,9 @@ Caused by: Caused by: failed to clone into: [ROOT]/home/.cargo/git/db/bitflags-[HASH] +Caused by: + revision 11111b376b93484341c68fbca3ca110ae5cd2790 not found + Caused by: process didn't exit successfully: `git fetch --no-tags --force --update-head-ok 'https://github.com/rust-lang/bitflags.git' '+11111b376b93484341c68fbca3ca110ae5cd2790:refs/commit/11111b376b93484341c68fbca3ca110ae5cd2790'` ([EXIT_STATUS]: 128) @@ -4093,7 +4096,7 @@ Caused by: .run(); } -#[cargo_test(public_network_test, requires_git)] +#[cargo_test(public_network_test)] fn git_fetch_libgit2_error_message() { let p = project() .file( @@ -4128,6 +4131,9 @@ Caused by: Caused by: failed to clone into: [ROOT]/home/.cargo/git/db/bitflags-[HASH] +Caused by: + revision 11111b376b93484341c68fbca3ca110ae5cd2790 not found + Caused by: network failure seems to have happened if a proxy or similar is necessary `net.git-fetch-with-cli` may help here