Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/cargo/sources/git/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::sources::git::fetch::RemoteKind;
use crate::sources::git::oxide;
use crate::sources::git::oxide::cargo_config_to_gitoxide_overrides;
use crate::util::HumanBytes;
use crate::util::errors::CargoResult;
use crate::util::errors::{CargoResult, GitCliError};
use crate::util::{GlobalContext, IntoUrl, MetricsCounter, Progress, network};
use anyhow::{Context as _, anyhow};
use cargo_util::{ProcessBuilder, paths};
Expand Down Expand Up @@ -1110,7 +1110,11 @@ fn fetch_with_cli(
.cwd(repo.path());
gctx.shell()
.verbose(|s| s.status("Running", &cmd.to_string()))?;
cmd.exec()?;
network::retry::with_retry(gctx, || {
cmd.exec()
.map_err(|error| GitCliError::new(error, true).into())
})?;

Ok(())
}

Expand Down
40 changes: 40 additions & 0 deletions src/cargo/util/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,46 @@ impl From<std::io::Error> for CliError {
}
}

// =============================================================================
// Git CLI errors

pub type GitCliResult = Result<(), GitCliError>;

/// An error that occurred while invoking the `git` command-line tool.
///
/// This is used for errors from Git operations performed via CLI.
/// It wraps a lower-level error and indicates whether the failure
/// should be considered *spurious*.
///
/// Spurious failures might involve retry mechanism.
#[derive(Debug)]
pub struct GitCliError {
inner: Error,
is_spurious: bool,
}

impl GitCliError {
pub fn new(inner: Error, is_spurious: bool) -> GitCliError {
GitCliError { inner, is_spurious }
}

pub fn is_spurious(&self) -> bool {
self.is_spurious
}
}

impl std::error::Error for GitCliError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.inner.source()
}
}

impl fmt::Display for GitCliError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.inner.fmt(f)
}
}

// =============================================================================
// Construction helpers

Expand Down
17 changes: 16 additions & 1 deletion src/cargo/util/network/retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
//! - <https://en.wikipedia.org/wiki/Exponential_backoff>
//! - <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After>

use crate::util::errors::HttpNotSuccessful;
use crate::util::errors::{GitCliError, HttpNotSuccessful};
use crate::{CargoResult, GlobalContext};
use anyhow::Error;
use rand::Rng;
Expand Down Expand Up @@ -223,6 +223,12 @@ fn maybe_spurious(err: &Error) -> bool {
}
}

if let Some(err) = err.downcast_ref::<GitCliError>() {
if err.is_spurious() {
return true;
}
}

false
}

Expand Down Expand Up @@ -402,3 +408,12 @@ fn retry_after_parsing() {
_ => panic!("unexpected non-retry"),
}
}

#[test]
fn git_cli_error_spurious() {
let error = GitCliError::new(Error::msg("test-git-cli-error"), false);
assert!(!maybe_spurious(&error.into()));

let error = GitCliError::new(Error::msg("test-git-cli-error"), true);
assert!(maybe_spurious(&error.into()));
}
6 changes: 6 additions & 0 deletions tests/testsuite/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4092,6 +4092,12 @@ fn github_fastpath_error_message() {
.with_stderr_data(str![[r#"
[UPDATING] git repository `https://github.com/rust-lang/bitflags.git`
fatal: remote [ERROR] upload-pack: not our ref 11111b376b93484341c68fbca3ca110ae5cd2790
[WARNING] spurious network error (3 tries remaining): process didn't exit successfully: `git fetch --no-tags --force --update-head-ok [..]
fatal: remote [ERROR] upload-pack: not our ref 11111b376b93484341c68fbca3ca110ae5cd2790
[WARNING] spurious network error (2 tries remaining): process didn't exit successfully: `git fetch --no-tags --force --update-head-ok [..]
fatal: remote [ERROR] upload-pack: not our ref 11111b376b93484341c68fbca3ca110ae5cd2790
[WARNING] spurious network error (1 try remaining): process didn't exit successfully: `git fetch --no-tags --force --update-head-ok [..]
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:
Expand Down
Loading