Skip to content

Commit 5bea4ab

Browse files
committed
Add ability to silence Source updates.
This adds the ability to silence Source status messages and progress bars. This is intended to be used with the publish "wait for available" system, which causes sources to repeatedly update which ends up spamming the terminal.
1 parent 261249a commit 5bea4ab

File tree

9 files changed

+81
-16
lines changed

9 files changed

+81
-16
lines changed

src/cargo/core/source/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ pub trait Source {
4444
/// Ensure that the source is fully up-to-date for the current session on the next query.
4545
fn invalidate_cache(&mut self);
4646

47+
/// If quiet, the source should not display any progress or status messages.
48+
fn set_quiet(&mut self, quiet: bool);
49+
4750
/// Fetches the full package for each name and version specified.
4851
fn download(&mut self, package: PackageId) -> CargoResult<MaybePackage>;
4952

@@ -163,6 +166,10 @@ impl<'a, T: Source + ?Sized + 'a> Source for Box<T> {
163166
(**self).invalidate_cache()
164167
}
165168

169+
fn set_quiet(&mut self, quiet: bool) {
170+
(**self).set_quiet(quiet)
171+
}
172+
166173
/// Forwards to `Source::download`.
167174
fn download(&mut self, id: PackageId) -> CargoResult<MaybePackage> {
168175
(**self).download(id)
@@ -233,6 +240,10 @@ impl<'a, T: Source + ?Sized + 'a> Source for &'a mut T {
233240
(**self).invalidate_cache()
234241
}
235242

243+
fn set_quiet(&mut self, quiet: bool) {
244+
(**self).set_quiet(quiet)
245+
}
246+
236247
fn download(&mut self, id: PackageId) -> CargoResult<MaybePackage> {
237248
(**self).download(id)
238249
}

src/cargo/sources/directory.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ impl<'cfg> Source for DirectorySource<'cfg> {
217217
}
218218

219219
fn invalidate_cache(&mut self) {
220-
// Path source has no local cache.
220+
// Directory source has no local cache.
221+
}
222+
223+
fn set_quiet(&mut self, _quiet: bool) {
224+
// Directory source does not display status
221225
}
222226
}

src/cargo/sources/git/source.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub struct GitSource<'cfg> {
2121
path_source: Option<PathSource<'cfg>>,
2222
ident: String,
2323
config: &'cfg Config,
24+
quiet: bool,
2425
}
2526

2627
impl<'cfg> GitSource<'cfg> {
@@ -43,6 +44,7 @@ impl<'cfg> GitSource<'cfg> {
4344
path_source: None,
4445
ident,
4546
config,
47+
quiet: false,
4648
};
4749

4850
Ok(source)
@@ -162,10 +164,12 @@ impl<'cfg> Source for GitSource<'cfg> {
162164
self.remote.url()
163165
);
164166
}
165-
self.config.shell().status(
166-
"Updating",
167-
format!("git repository `{}`", self.remote.url()),
168-
)?;
167+
if !self.quiet {
168+
self.config.shell().status(
169+
"Updating",
170+
format!("git repository `{}`", self.remote.url()),
171+
)?;
172+
}
169173

170174
trace!("updating git source `{:?}`", self.remote);
171175

@@ -233,6 +237,10 @@ impl<'cfg> Source for GitSource<'cfg> {
233237
}
234238

235239
fn invalidate_cache(&mut self) {}
240+
241+
fn set_quiet(&mut self, quiet: bool) {
242+
self.quiet = quiet;
243+
}
236244
}
237245

238246
#[cfg(test)]

src/cargo/sources/path.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,4 +574,8 @@ impl<'cfg> Source for PathSource<'cfg> {
574574
fn invalidate_cache(&mut self) {
575575
// Path source has no local cache.
576576
}
577+
578+
fn set_quiet(&mut self, _quiet: bool) {
579+
// Path source does not display status
580+
}
577581
}

src/cargo/sources/registry/http_remote.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,13 @@ pub struct HttpRegistry<'cfg> {
8989

9090
/// Url to get a token for the registry.
9191
login_url: Option<Url>,
92+
93+
/// Disables status messages.
94+
quiet: bool,
9295
}
9396

9497
/// Helper for downloading crates.
95-
pub struct Downloads<'cfg> {
98+
struct Downloads<'cfg> {
9699
/// When a download is started, it is added to this map. The key is a
97100
/// "token" (see `Download::token`). It is removed once the download is
98101
/// finished.
@@ -196,6 +199,7 @@ impl<'cfg> HttpRegistry<'cfg> {
196199
registry_config: None,
197200
auth_required: false,
198201
login_url: None,
202+
quiet: false,
199203
})
200204
}
201205

@@ -231,9 +235,11 @@ impl<'cfg> HttpRegistry<'cfg> {
231235
// let's not flood the server with connections
232236
self.multi.set_max_host_connections(2)?;
233237

234-
self.config
235-
.shell()
236-
.status("Updating", self.source_id.display_index())?;
238+
if !self.quiet {
239+
self.config
240+
.shell()
241+
.status("Updating", self.source_id.display_index())?;
242+
}
237243

238244
Ok(())
239245
}
@@ -681,6 +687,11 @@ impl<'cfg> RegistryData for HttpRegistry<'cfg> {
681687
self.requested_update = true;
682688
}
683689

690+
fn set_quiet(&mut self, quiet: bool) {
691+
self.quiet = quiet;
692+
self.downloads.progress.replace(None);
693+
}
694+
684695
fn download(&mut self, pkg: PackageId, checksum: &str) -> CargoResult<MaybeLock> {
685696
let registry_config = loop {
686697
match self.config()? {
@@ -747,7 +758,7 @@ impl<'cfg> RegistryData for HttpRegistry<'cfg> {
747758
impl<'cfg> Downloads<'cfg> {
748759
fn tick(&self) -> CargoResult<()> {
749760
let mut progress = self.progress.borrow_mut();
750-
let progress = progress.as_mut().unwrap();
761+
let Some(progress) = progress.as_mut() else { return Ok(()); };
751762

752763
// Since the sparse protocol discovers dependencies as it goes,
753764
// it's not possible to get an accurate progress indication.
@@ -780,7 +791,7 @@ mod tls {
780791

781792
thread_local!(static PTR: Cell<usize> = Cell::new(0));
782793

783-
pub(crate) fn with<R>(f: impl FnOnce(Option<&Downloads<'_>>) -> R) -> R {
794+
pub(super) fn with<R>(f: impl FnOnce(Option<&Downloads<'_>>) -> R) -> R {
784795
let ptr = PTR.with(|p| p.get());
785796
if ptr == 0 {
786797
f(None)
@@ -791,7 +802,7 @@ mod tls {
791802
}
792803
}
793804

794-
pub(crate) fn set<R>(dl: &Downloads<'_>, f: impl FnOnce() -> R) -> R {
805+
pub(super) fn set<R>(dl: &Downloads<'_>, f: impl FnOnce() -> R) -> R {
795806
struct Reset<'a, T: Copy>(&'a Cell<T>, T);
796807

797808
impl<'a, T: Copy> Drop for Reset<'a, T> {

src/cargo/sources/registry/local.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub struct LocalRegistry<'cfg> {
1818
src_path: Filesystem,
1919
config: &'cfg Config,
2020
updated: bool,
21+
quiet: bool,
2122
}
2223

2324
impl<'cfg> LocalRegistry<'cfg> {
@@ -28,6 +29,7 @@ impl<'cfg> LocalRegistry<'cfg> {
2829
root: Filesystem::new(root.to_path_buf()),
2930
config,
3031
updated: false,
32+
quiet: false,
3133
}
3234
}
3335
}
@@ -104,6 +106,10 @@ impl<'cfg> RegistryData for LocalRegistry<'cfg> {
104106
// Local registry has no cache - just reads from disk.
105107
}
106108

109+
fn set_quiet(&mut self, _quiet: bool) {
110+
self.quiet = true;
111+
}
112+
107113
fn is_updated(&self) -> bool {
108114
self.updated
109115
}
@@ -124,7 +130,9 @@ impl<'cfg> RegistryData for LocalRegistry<'cfg> {
124130
return Ok(MaybeLock::Ready(crate_file));
125131
}
126132

127-
self.config.shell().status("Unpacking", pkg)?;
133+
if !self.quiet {
134+
self.config.shell().status("Unpacking", pkg)?;
135+
}
128136

129137
// We don't actually need to download anything per-se, we just need to
130138
// verify the checksum matches the .crate file itself.

src/cargo/sources/registry/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,9 @@ pub trait RegistryData {
474474
/// Invalidates locally cached data.
475475
fn invalidate_cache(&mut self);
476476

477+
/// If quiet, the source should not display any progress or status messages.
478+
fn set_quiet(&mut self, quiet: bool);
479+
477480
/// Is the local cached data up-to-date?
478481
fn is_updated(&self) -> bool;
479482

@@ -832,6 +835,10 @@ impl<'cfg> Source for RegistrySource<'cfg> {
832835
self.ops.invalidate_cache();
833836
}
834837

838+
fn set_quiet(&mut self, quiet: bool) {
839+
self.ops.set_quiet(quiet);
840+
}
841+
835842
fn download(&mut self, package: PackageId) -> CargoResult<MaybePackage> {
836843
let hash = loop {
837844
match self.index.hash(package, &mut *self.ops)? {

src/cargo/sources/registry/remote.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub struct RemoteRegistry<'cfg> {
3232
head: Cell<Option<git2::Oid>>,
3333
current_sha: Cell<Option<InternedString>>,
3434
needs_update: bool, // Does this registry need to be updated?
35+
quiet: bool,
3536
}
3637

3738
impl<'cfg> RemoteRegistry<'cfg> {
@@ -48,6 +49,7 @@ impl<'cfg> RemoteRegistry<'cfg> {
4849
head: Cell::new(None),
4950
current_sha: Cell::new(None),
5051
needs_update: false,
52+
quiet: false,
5153
}
5254
}
5355

@@ -292,9 +294,11 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
292294
*self.tree.borrow_mut() = None;
293295
self.current_sha.set(None);
294296
let path = self.config.assert_package_cache_locked(&self.index_path);
295-
self.config
296-
.shell()
297-
.status("Updating", self.source_id.display_index())?;
297+
if !self.quiet {
298+
self.config
299+
.shell()
300+
.status("Updating", self.source_id.display_index())?;
301+
}
298302

299303
// Fetch the latest version of our `index_git_ref` into the index
300304
// checkout.
@@ -315,6 +319,10 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
315319
self.needs_update = true;
316320
}
317321

322+
fn set_quiet(&mut self, quiet: bool) {
323+
self.quiet = quiet;
324+
}
325+
318326
fn is_updated(&self) -> bool {
319327
self.is_updated()
320328
}

src/cargo/sources/replaced.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ impl<'cfg> Source for ReplacedSource<'cfg> {
6767
self.inner.invalidate_cache()
6868
}
6969

70+
fn set_quiet(&mut self, quiet: bool) {
71+
self.inner.set_quiet(quiet);
72+
}
73+
7074
fn download(&mut self, id: PackageId) -> CargoResult<MaybePackage> {
7175
let id = id.with_source_id(self.replace_with);
7276
let pkg = self

0 commit comments

Comments
 (0)