diff --git a/src/cargo/sources/git/utils.rs b/src/cargo/sources/git/utils.rs index d28931f86425..f1436db3683f 100644 --- a/src/cargo/sources/git/utils.rs +++ b/src/cargo/sources/git/utils.rs @@ -1152,6 +1152,13 @@ pub fn fetch( continue; } } + if matches!(err.class(), ErrorClass::Net) && err.code() == git2::ErrorCode::Eof { + let revspec_id: String = refspecs + .iter() + .map(|s| s.split(':').next().unwrap_or("").to_string()) + .collect(); + return Err(anyhow!("refspec {revspec_id} not found on {remote_url}")); + } return Err(err.into()); } @@ -1221,9 +1228,21 @@ fn fetch_with_cli( .env_remove("GIT_OBJECT_DIRECTORY") .env_remove("GIT_ALTERNATE_OBJECT_DIRECTORIES") .cwd(repo.path()); + gctx.shell() .verbose(|s| s.status("Running", &cmd.to_string()))?; - cmd.exec()?; + + let revspec_id: String = refspecs + .iter() + .map(|s| s.split(':').next().unwrap_or("").to_string()) + .collect(); + + cmd.exec().with_context(|| { + format!( + "Failed to fetch via Github fast path \nrevspec {:?} not found", + revspec_id + ) + })?; Ok(()) } diff --git a/tests/testsuite/git.rs b/tests/testsuite/git.rs index 6bc72423c72a..00afe403366a 100644 --- a/tests/testsuite/git.rs +++ b/tests/testsuite/git.rs @@ -4052,6 +4052,90 @@ src/lib.rs .run(); } +#[cargo_test(public_network_test)] +fn github_fastpath_error_message() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + edition = "2015" + + [dependencies] + bitflags = { git = "https://github.com/rust-lang/bitflags.git", rev="11111b376b93484341c68fbca3ca110ae5cd2790" } + "#, + ) + .file("src/lib.rs", "") + .build(); + p.cargo("fetch") + .env("CARGO_NET_GIT_FETCH_WITH_CLI", "true") + .with_status(101) + .with_stderr_data(str![[r#" +[UPDATING] git repository `https://github.com/rust-lang/bitflags.git` +fatal: remote [ERROR] upload-pack: not our ref 11111b376b93484341c68fbca3ca110ae5cd2790 +[ERROR] failed to get `bitflags` as a dependency of package `foo v0.1.0 ([ROOT]/foo)` + +Caused by: + failed to load source for dependency `bitflags` + +Caused by: + Unable to update https://github.com/rust-lang/bitflags.git?rev=11111b376b93484341c68fbca3ca110ae5cd2790 + +Caused by: + failed to clone into: [ROOT]/home/.cargo/git/db/bitflags-[HASH] + +Caused by: + Failed to fetch via Github fast path + revspec "+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) + +"#]]) + .run(); +} + +#[cargo_test] +fn git_fetch_cli_false() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + edition = "2015" + + [dependencies] + bitflags = { git = "https://github.com/rust-lang/bitflags.git", rev="11111b376b93484341c68fbca3ca110ae5cd2790" } + "#, + ) + .file("src/lib.rs", "") + .build(); + p.cargo("fetch") + .with_status(101) + .with_stderr_data(str![[r#" +[UPDATING] git repository `https://github.com/rust-lang/bitflags.git` +[ERROR] failed to get `bitflags` as a dependency of package `foo v0.1.0 ([ROOT]/foo)` + +Caused by: + failed to load source for dependency `bitflags` + +Caused by: + Unable to update https://github.com/rust-lang/bitflags.git?rev=11111b376b93484341c68fbca3ca110ae5cd2790 + +Caused by: + failed to clone into: [ROOT]/home/.cargo/git/db/bitflags-[HASH] + +Caused by: + refspec +11111b376b93484341c68fbca3ca110ae5cd2790 not found on https://github.com/rust-lang/bitflags.git + +"#]]) + .run(); +} + #[cargo_test] fn git_worktree_with_bare_original_repo() { let project = project().build();