Skip to content

Commit

Permalink
Auto merge of #13647 - weihanglo:shell, r=Muscraft
Browse files Browse the repository at this point in the history
fix: do not borrow shell across registry query
  • Loading branch information
bors committed Mar 26, 2024
2 parents fa619a9 + 667803c commit 499a61c
Showing 1 changed file with 35 additions and 21 deletions.
56 changes: 35 additions & 21 deletions src/cargo/ops/cargo_generate_lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,14 @@ fn print_lockfile_generation(
resolve: &Resolve,
registry: &mut PackageRegistry<'_>,
) -> CargoResult<()> {
let mut shell = gctx.shell();

let diff = PackageDiff::new(&resolve);
let num_pkgs: usize = diff.iter().map(|d| d.added.len()).sum();
if num_pkgs <= 1 {
// just ourself, nothing worth reporting
return Ok(());
}
shell.status("Locking", format!("{num_pkgs} packages"))?;
gctx.shell()
.status("Locking", format!("{num_pkgs} packages"))?;

for diff in diff {
fn format_latest(version: semver::Version) -> String {
Expand Down Expand Up @@ -226,7 +225,11 @@ fn print_lockfile_generation(
};

if let Some(latest) = latest {
shell.status_with_color("Adding", format!("{package}{latest}"), &style::NOTE)?;
gctx.shell().status_with_color(
"Adding",
format!("{package}{latest}"),
&style::NOTE,
)?;
}
}
}
Expand All @@ -240,15 +243,14 @@ fn print_lockfile_sync(
resolve: &Resolve,
registry: &mut PackageRegistry<'_>,
) -> CargoResult<()> {
let mut shell = gctx.shell();

let diff = PackageDiff::diff(&previous_resolve, &resolve);
let num_pkgs: usize = diff.iter().map(|d| d.added.len()).sum();
if num_pkgs == 0 {
return Ok(());
}
let plural = if num_pkgs == 1 { "" } else { "s" };
shell.status("Locking", format!("{num_pkgs} package{plural}"))?;
gctx.shell()
.status("Locking", format!("{num_pkgs} package{plural}"))?;

for diff in diff {
fn format_latest(version: semver::Version) -> String {
Expand Down Expand Up @@ -296,9 +298,11 @@ fn print_lockfile_sync(
// This metadata is often stuff like git commit hashes, which are
// not meaningfully ordered.
if removed.version().cmp_precedence(added.version()) == Ordering::Greater {
shell.status_with_color("Downgrading", msg, &style::WARN)?;
gctx.shell()
.status_with_color("Downgrading", msg, &style::WARN)?;
} else {
shell.status_with_color("Updating", msg, &style::GOOD)?;
gctx.shell()
.status_with_color("Updating", msg, &style::GOOD)?;
}
} else {
for package in diff.added.iter() {
Expand All @@ -315,7 +319,11 @@ fn print_lockfile_sync(
}
.unwrap_or_default();

shell.status_with_color("Adding", format!("{package}{latest}"), &style::NOTE)?;
gctx.shell().status_with_color(
"Adding",
format!("{package}{latest}"),
&style::NOTE,
)?;
}
}
}
Expand All @@ -329,8 +337,6 @@ pub fn print_lockfile_updates(
resolve: &Resolve,
registry: &mut PackageRegistry<'_>,
) -> CargoResult<()> {
let mut shell = gctx.shell();

let mut unchanged_behind = 0;
for diff in PackageDiff::diff(&previous_resolve, &resolve) {
fn format_latest(version: semver::Version) -> String {
Expand Down Expand Up @@ -378,13 +384,16 @@ pub fn print_lockfile_updates(
// This metadata is often stuff like git commit hashes, which are
// not meaningfully ordered.
if removed.version().cmp_precedence(added.version()) == Ordering::Greater {
shell.status_with_color("Downgrading", msg, &style::WARN)?;
gctx.shell()
.status_with_color("Downgrading", msg, &style::WARN)?;
} else {
shell.status_with_color("Updating", msg, &style::GOOD)?;
gctx.shell()
.status_with_color("Updating", msg, &style::GOOD)?;
}
} else {
for package in diff.removed.iter() {
shell.status_with_color("Removing", format!("{package}"), &style::ERROR)?;
gctx.shell()
.status_with_color("Removing", format!("{package}"), &style::ERROR)?;
}
for package in diff.added.iter() {
let latest = if !possibilities.is_empty() {
Expand All @@ -400,7 +409,11 @@ pub fn print_lockfile_updates(
}
.unwrap_or_default();

shell.status_with_color("Adding", format!("{package}{latest}"), &style::NOTE)?;
gctx.shell().status_with_color(
"Adding",
format!("{package}{latest}"),
&style::NOTE,
)?;
}
}
for package in &diff.unchanged {
Expand All @@ -418,8 +431,8 @@ pub fn print_lockfile_updates(

if let Some(latest) = latest {
unchanged_behind += 1;
if shell.verbosity() == Verbosity::Verbose {
shell.status_with_color(
if gctx.shell().verbosity() == Verbosity::Verbose {
gctx.shell().status_with_color(
"Unchanged",
format!("{package}{latest}"),
&anstyle::Style::new().bold(),
Expand All @@ -428,13 +441,14 @@ pub fn print_lockfile_updates(
}
}
}
if shell.verbosity() == Verbosity::Verbose {
shell.note(

if gctx.shell().verbosity() == Verbosity::Verbose {
gctx.shell().note(
"to see how you depend on a package, run `cargo tree --invert --package <dep>@<ver>`",
)?;
} else {
if 0 < unchanged_behind {
shell.note(format!(
gctx.shell().note(format!(
"pass `--verbose` to see {unchanged_behind} unchanged dependencies behind latest"
))?;
}
Expand Down

0 comments on commit 499a61c

Please sign in to comment.