diff --git a/Cargo.lock b/Cargo.lock index a8f0e5e5eed..dc0d9844db4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -607,6 +607,7 @@ dependencies = [ "anstyle-hyperlink", "anstyle-progress", "anyhow", + "cargo-util", "libc", "serde", "serde_json", diff --git a/crates/cargo-util-terminal/Cargo.toml b/crates/cargo-util-terminal/Cargo.toml index 428c7d4bc41..7957b7945c0 100644 --- a/crates/cargo-util-terminal/Cargo.toml +++ b/crates/cargo-util-terminal/Cargo.toml @@ -14,6 +14,7 @@ anstyle.workspace = true anstyle-hyperlink = { workspace = true, features = ["file"] } anstyle-progress.workspace = true anyhow.workspace = true +cargo-util.workspace = true serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true, features = ["raw_value"] } supports-hyperlinks.workspace = true diff --git a/crates/cargo-util-terminal/src/shell.rs b/crates/cargo-util-terminal/src/shell.rs index e39d231e7dd..ecea8547a61 100644 --- a/crates/cargo-util-terminal/src/shell.rs +++ b/crates/cargo-util-terminal/src/shell.rs @@ -363,6 +363,21 @@ impl Shell { url.map(|u| self.err_hyperlink(u)).unwrap_or_default() } + /// Query terminal capability, independent of user configuration + pub fn progress_supported(&self) -> bool { + // report no progress when -q (for quiet) or TERM=dumb are set + // or if running on Continuous Integration service like Travis where the + // output logs get mangled. + if self.verbosity == Verbosity::Quiet + || std::env::var("TERM").as_deref() == Ok("dumb") + || cargo_util::is_ci() + { + false + } else { + true + } + } + fn unstable_flags_rustc_unicode(&self) -> bool { match &self.output { ShellOut::Write(_) => false, diff --git a/src/sources/git/utils.rs b/src/sources/git/utils.rs index 69f33ae3f16..ee20d194ae6 100644 --- a/src/sources/git/utils.rs +++ b/src/sources/git/utils.rs @@ -1,6 +1,7 @@ //! Utilities for handling git repositories, mainly around //! authentication/cloning. +use crate::context::ProgressWhen; use crate::sources::git::fetch::RemoteKind; use crate::sources::git::oxide; use crate::sources::git::oxide::cargo_config_to_gitoxide_overrides; @@ -1156,15 +1157,25 @@ fn fetch_with_cli( let depth = 0i32.saturating_add_unsigned(depth.get()); cmd.arg(format!("--depth={depth}")); } - match gctx.shell().verbosity() { - Verbosity::Normal => {} - Verbosity::Verbose => { - cmd.arg("--verbose"); - } - Verbosity::Quiet => { - cmd.arg("--quiet"); + + let progress_config = gctx.progress_config(); + let progress = match progress_config.when { + ProgressWhen::Always => true, + ProgressWhen::Never => false, + ProgressWhen::Auto => { + // Recreate the same conditions used with `Progress` + let width = progress_config + .width + .or_else(|| gctx.shell().err_width().progress_max_width()); + gctx.shell().progress_supported() && width.is_some() } + }; + if gctx.shell().verbosity() == Verbosity::Verbose { + cmd.arg("--verbose"); + } else if !progress { + cmd.arg("--quiet"); } + cmd.arg("--force") // handle force pushes .arg("--update-head-ok") // see discussion in #2078 .arg(url) diff --git a/src/util/progress.rs b/src/util/progress.rs index e938e5b7116..64464450adc 100644 --- a/src/util/progress.rs +++ b/src/util/progress.rs @@ -6,9 +6,7 @@ use std::time::{Duration, Instant}; use crate::context::ProgressWhen; use crate::util::{CargoResult, GlobalContext}; use anstyle_progress::TermProgress; -use cargo_util::is_ci; use cargo_util_terminal::Shell; -use cargo_util_terminal::Verbosity; use unicode_width::UnicodeWidthChar; /// CLI progress bar. @@ -56,23 +54,17 @@ impl<'gctx> Progress<'gctx> { style: ProgressStyle, gctx: &'gctx GlobalContext, ) -> Progress<'gctx> { - // report no progress when -q (for quiet) or TERM=dumb are set - // or if running on Continuous Integration service like Travis where the - // output logs get mangled. - let dumb = match gctx.get_env("TERM") { - Ok(term) => term == "dumb", - Err(_) => false, - }; let progress_config = gctx.progress_config(); - match progress_config.when { - ProgressWhen::Always => return Progress::new_priv(name, style, gctx), - ProgressWhen::Never => return Progress { gctx, state: None }, - ProgressWhen::Auto => {} - } - if gctx.shell().verbosity() == Verbosity::Quiet || dumb || is_ci() { + let progress = match progress_config.when { + ProgressWhen::Always => true, + ProgressWhen::Never => false, + ProgressWhen::Auto => gctx.shell().progress_supported(), + }; + if progress { + Progress::new_priv(name, style, gctx) + } else { return Progress { gctx, state: None }; } - Progress::new_priv(name, style, gctx) } fn new_priv(name: &str, style: ProgressStyle, gctx: &'gctx GlobalContext) -> Progress<'gctx> { diff --git a/tests/testsuite/git.rs b/tests/testsuite/git.rs index 3c4771eeada..d90ecc97dcf 100644 --- a/tests/testsuite/git.rs +++ b/tests/testsuite/git.rs @@ -4237,11 +4237,11 @@ 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 [..] +[WARNING] spurious network error (3 tries remaining): process didn't exit successfully: `git fetch --no-tags --quiet --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 [..] +[WARNING] spurious network error (2 tries remaining): process didn't exit successfully: `git fetch --no-tags --quiet --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 [..] +[WARNING] spurious network error (1 try remaining): process didn't exit successfully: `git fetch --no-tags --quiet --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)` @@ -4258,7 +4258,7 @@ Caused by: revision 11111b376b93484341c68fbca3ca110ae5cd2790 not found Caused by: - process didn't exit successfully: `git fetch --no-tags --force --update-head-ok [..] + process didn't exit successfully: `git fetch --no-tags --quiet --force --update-head-ok [..] "#]]) .run();