From f7442319e7f1ea17b0c98136424cc83bb289c190 Mon Sep 17 00:00:00 2001
From: Shoyu Vanilla
Date: Fri, 13 Dec 2024 05:57:12 +0900
Subject: [PATCH] feat(tree): implement filtering logic for `--depth workspace`
flag
---
src/cargo/ops/tree/mod.rs | 13 ++++++++++---
src/doc/man/cargo-tree.md | 3 +++
src/doc/man/generated_txt/cargo-tree.txt | 3 +++
src/doc/src/commands/cargo-tree.md | 4 +++-
src/etc/man/cargo-tree.1 | 3 +++
tests/testsuite/tree.rs | 7 ++-----
6 files changed, 24 insertions(+), 9 deletions(-)
diff --git a/src/cargo/ops/tree/mod.rs b/src/cargo/ops/tree/mod.rs
index 60d7974c49c..e0579696f61 100644
--- a/src/cargo/ops/tree/mod.rs
+++ b/src/cargo/ops/tree/mod.rs
@@ -45,6 +45,7 @@ pub struct TreeOptions {
pub graph_features: bool,
/// Display depth of the dependency tree.
/// If non-negative integer, display dependencies with that amount of max depth.
+ /// If `workspace`, display dependencies from current workspace only.
pub display_depth: DisplayDepth,
/// Excludes proc-macro dependencies.
pub no_proc_macro: bool,
@@ -90,6 +91,7 @@ impl FromStr for Prefix {
#[derive(Clone, Copy)]
pub enum DisplayDepth {
MaxDisplayDepth(u32),
+ Workspace,
}
impl FromStr for DisplayDepth {
@@ -97,11 +99,12 @@ impl FromStr for DisplayDepth {
fn from_str(s: &str) -> Result {
match s {
+ "workspace" => Ok(Self::Workspace),
s => s.parse().map(Self::MaxDisplayDepth).map_err(|_| {
clap::Error::raw(
clap::error::ErrorKind::ValueValidation,
format!(
- "supported values for --depth are non-negative integers, \
+ "supported values for --depth are non-negative integers and `workspace`, \
but `{}` is unknown",
s
),
@@ -403,8 +406,9 @@ fn print_dependencies<'a>(
}
}
- let max_display_depth = match display_depth {
- DisplayDepth::MaxDisplayDepth(max) => max,
+ let (max_display_depth, filter_non_workspace_member) = match display_depth {
+ DisplayDepth::MaxDisplayDepth(max) => (max, false),
+ DisplayDepth::Workspace => (u32::MAX, true),
};
// Current level exceeds maximum display depth. Skip.
@@ -418,6 +422,9 @@ fn print_dependencies<'a>(
// Filter out packages to prune.
match graph.node(**dep) {
Node::Package { package_id, .. } => {
+ if filter_non_workspace_member && !ws.is_member_id(*package_id) {
+ return false;
+ }
!pkgs_to_prune.iter().any(|spec| spec.matches(*package_id))
}
_ => true,
diff --git a/src/doc/man/cargo-tree.md b/src/doc/man/cargo-tree.md
index ce99b761e7d..fc3521c9939 100644
--- a/src/doc/man/cargo-tree.md
+++ b/src/doc/man/cargo-tree.md
@@ -96,6 +96,9 @@ Prune the given package from the display of the dependency tree.
{{#option "`--depth` _depth_" }}
Maximum display depth of the dependency tree. A depth of 1 displays the direct
dependencies, for example.
+
+If the given value is `workspace`, only shows the dependencies that are member
+of the current workspace, instead.
{{/option}}
{{#option "`--no-dedupe`" }}
diff --git a/src/doc/man/generated_txt/cargo-tree.txt b/src/doc/man/generated_txt/cargo-tree.txt
index 1af730aff53..0fc542f429f 100644
--- a/src/doc/man/generated_txt/cargo-tree.txt
+++ b/src/doc/man/generated_txt/cargo-tree.txt
@@ -85,6 +85,9 @@ OPTIONS
Maximum display depth of the dependency tree. A depth of 1 displays
the direct dependencies, for example.
+ If the given value is workspace, only shows the dependencies that
+ are member of the current workspace, instead.
+
--no-dedupe
Do not de-duplicate repeated dependencies. Usually, when a package
has already displayed its dependencies, further occurrences will not
diff --git a/src/doc/src/commands/cargo-tree.md b/src/doc/src/commands/cargo-tree.md
index 154939a28ef..9418eaa9d1c 100644
--- a/src/doc/src/commands/cargo-tree.md
+++ b/src/doc/src/commands/cargo-tree.md
@@ -91,7 +91,9 @@ subtree of the package given to -p
.
--depth
depth
Maximum display depth of the dependency tree. A depth of 1 displays the direct
-dependencies, for example.
+dependencies, for example.
+If the given value is workspace
, only shows the dependencies that are member
+of the current workspace, instead.
--no-dedupe
diff --git a/src/etc/man/cargo-tree.1 b/src/etc/man/cargo-tree.1
index c951460b028..25045b3f92d 100644
--- a/src/etc/man/cargo-tree.1
+++ b/src/etc/man/cargo-tree.1
@@ -93,6 +93,9 @@ Prune the given package from the display of the dependency tree.
.RS 4
Maximum display depth of the dependency tree. A depth of 1 displays the direct
dependencies, for example.
+.sp
+If the given value is \fBworkspace\fR, only shows the dependencies that are member
+of the current workspace, instead.
.RE
.sp
\fB\-\-no\-dedupe\fR
diff --git a/tests/testsuite/tree.rs b/tests/testsuite/tree.rs
index 2e81f55ce0e..762cc4d9652 100644
--- a/tests/testsuite/tree.rs
+++ b/tests/testsuite/tree.rs
@@ -1888,15 +1888,12 @@ fn depth_workspace() {
.file("c/src/lib.rs", "")
.build();
- p.cargo("tree")
+ p.cargo("tree --depth workspace")
.with_stdout_data(str![[r#"
a v1.0.0 ([ROOT]/foo/a)
b v0.1.0 ([ROOT]/foo/b)
-├── c v0.1.0 ([ROOT]/foo/c)
-│ ├── otherdep v1.0.0
-│ └── somedep v1.0.0
-└── somedep v1.0.0
+└── c v0.1.0 ([ROOT]/foo/c)
c v0.1.0 ([ROOT]/foo/c) (*)