From 366440d799da99718eba2c8e194e8d5eef447fb9 Mon Sep 17 00:00:00 2001 From: Dacian Pascu Date: Sun, 17 Nov 2024 16:50:33 +0200 Subject: [PATCH] fix(git): added extra error context for a revision that doesn't exist See issue #14621 --- src/cargo/sources/git/utils.rs | 40 ++++++++++++++++++++++++++++++---- tests/testsuite/git.rs | 6 +++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/cargo/sources/git/utils.rs b/src/cargo/sources/git/utils.rs index 9f96c2bf74c8..e9ac91daf465 100644 --- a/src/cargo/sources/git/utils.rs +++ b/src/cargo/sources/git/utils.rs @@ -944,9 +944,15 @@ 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), + Ok(FastPathRev::NeedsFetch(rev)) => { + fast_path_rev = true; + Some(rev) + } Ok(FastPathRev::Indeterminate) => None, Err(e) => { debug!("failed to check github {:?}", e); @@ -958,6 +964,8 @@ pub fn fetch( clean_repo_temp_files(repo); + let mut revision: String = String::new(); + // Translate the reference desired here into an actual list of refspecs // which need to get fetched. Additionally record if we're fetching tags. let mut refspecs = Vec::new(); @@ -981,6 +989,7 @@ pub fn fetch( } GitReference::Rev(rev) => { + revision = rev.to_string(); if rev.starts_with("refs/") { refspecs.push(format!("+{0}:{0}", rev)); } else if let Some(oid_to_fetch) = oid_to_fetch { @@ -1007,13 +1016,36 @@ pub fn fetch( } if let Some(true) = gctx.net_config()?.git_fetch_with_cli { - return fetch_with_cli(repo, remote_url, &refspecs, tags, gctx); + match fetch_with_cli(repo, remote_url, &refspecs, tags, gctx) { + Ok(result) => return Ok(result), + Err(e) => { + return Err(e).context(format!("Revision {:?} not found", revision)); + } + }; } if gctx.cli_unstable().gitoxide.map_or(false, |git| git.fetch) { - return fetch_with_gitoxide(repo, remote_url, refspecs, tags, shallow, gctx); + match fetch_with_gitoxide(repo, remote_url, refspecs, tags, shallow, gctx) { + Ok(result) => return Ok(result), + Err(e) => { + if fast_path_rev == true { + return Err(e).context(format!("Revision {:?} not found", revision)); + } else { + return Err(e); + } + } + }; } else { - return fetch_with_libgit2(repo, remote_url, refspecs, tags, shallow, gctx); + match fetch_with_libgit2(repo, remote_url, refspecs, tags, shallow, gctx) { + Ok(result) => return Ok(result), + Err(e) => { + if fast_path_rev == true { + return Err(e).context(format!("Revision {:?} not found", revision)); + } else { + return Err(e); + } + } + }; } } diff --git a/tests/testsuite/git.rs b/tests/testsuite/git.rs index 793e24e7124a..f239d8cc630c 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) @@ -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