Skip to content

Commit f744231

Browse files
committed
feat(tree): implement filtering logic for --depth workspace flag
1 parent 65931bc commit f744231

File tree

6 files changed

+24
-9
lines changed

6 files changed

+24
-9
lines changed

src/cargo/ops/tree/mod.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub struct TreeOptions {
4545
pub graph_features: bool,
4646
/// Display depth of the dependency tree.
4747
/// If non-negative integer, display dependencies with that amount of max depth.
48+
/// If `workspace`, display dependencies from current workspace only.
4849
pub display_depth: DisplayDepth,
4950
/// Excludes proc-macro dependencies.
5051
pub no_proc_macro: bool,
@@ -90,18 +91,20 @@ impl FromStr for Prefix {
9091
#[derive(Clone, Copy)]
9192
pub enum DisplayDepth {
9293
MaxDisplayDepth(u32),
94+
Workspace,
9395
}
9496

9597
impl FromStr for DisplayDepth {
9698
type Err = clap::Error;
9799

98100
fn from_str(s: &str) -> Result<Self, Self::Err> {
99101
match s {
102+
"workspace" => Ok(Self::Workspace),
100103
s => s.parse().map(Self::MaxDisplayDepth).map_err(|_| {
101104
clap::Error::raw(
102105
clap::error::ErrorKind::ValueValidation,
103106
format!(
104-
"supported values for --depth are non-negative integers, \
107+
"supported values for --depth are non-negative integers and `workspace`, \
105108
but `{}` is unknown",
106109
s
107110
),
@@ -403,8 +406,9 @@ fn print_dependencies<'a>(
403406
}
404407
}
405408

406-
let max_display_depth = match display_depth {
407-
DisplayDepth::MaxDisplayDepth(max) => max,
409+
let (max_display_depth, filter_non_workspace_member) = match display_depth {
410+
DisplayDepth::MaxDisplayDepth(max) => (max, false),
411+
DisplayDepth::Workspace => (u32::MAX, true),
408412
};
409413

410414
// Current level exceeds maximum display depth. Skip.
@@ -418,6 +422,9 @@ fn print_dependencies<'a>(
418422
// Filter out packages to prune.
419423
match graph.node(**dep) {
420424
Node::Package { package_id, .. } => {
425+
if filter_non_workspace_member && !ws.is_member_id(*package_id) {
426+
return false;
427+
}
421428
!pkgs_to_prune.iter().any(|spec| spec.matches(*package_id))
422429
}
423430
_ => true,

src/doc/man/cargo-tree.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ Prune the given package from the display of the dependency tree.
9696
{{#option "`--depth` _depth_" }}
9797
Maximum display depth of the dependency tree. A depth of 1 displays the direct
9898
dependencies, for example.
99+
100+
If the given value is `workspace`, only shows the dependencies that are member
101+
of the current workspace, instead.
99102
{{/option}}
100103

101104
{{#option "`--no-dedupe`" }}

src/doc/man/generated_txt/cargo-tree.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ OPTIONS
8585
Maximum display depth of the dependency tree. A depth of 1 displays
8686
the direct dependencies, for example.
8787

88+
If the given value is workspace, only shows the dependencies that
89+
are member of the current workspace, instead.
90+
8891
--no-dedupe
8992
Do not de-duplicate repeated dependencies. Usually, when a package
9093
has already displayed its dependencies, further occurrences will not

src/doc/src/commands/cargo-tree.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ subtree of the package given to <code>-p</code>.</dd>
9191

9292
<dt class="option-term" id="option-cargo-tree---depth"><a class="option-anchor" href="#option-cargo-tree---depth"></a><code>--depth</code> <em>depth</em></dt>
9393
<dd class="option-desc">Maximum display depth of the dependency tree. A depth of 1 displays the direct
94-
dependencies, for example.</dd>
94+
dependencies, for example.</p>
95+
<p>If the given value is <code>workspace</code>, only shows the dependencies that are member
96+
of the current workspace, instead.</dd>
9597

9698

9799
<dt class="option-term" id="option-cargo-tree---no-dedupe"><a class="option-anchor" href="#option-cargo-tree---no-dedupe"></a><code>--no-dedupe</code></dt>

src/etc/man/cargo-tree.1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ Prune the given package from the display of the dependency tree.
9393
.RS 4
9494
Maximum display depth of the dependency tree. A depth of 1 displays the direct
9595
dependencies, for example.
96+
.sp
97+
If the given value is \fBworkspace\fR, only shows the dependencies that are member
98+
of the current workspace, instead.
9699
.RE
97100
.sp
98101
\fB\-\-no\-dedupe\fR

tests/testsuite/tree.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,15 +1888,12 @@ fn depth_workspace() {
18881888
.file("c/src/lib.rs", "")
18891889
.build();
18901890

1891-
p.cargo("tree")
1891+
p.cargo("tree --depth workspace")
18921892
.with_stdout_data(str![[r#"
18931893
a v1.0.0 ([ROOT]/foo/a)
18941894
18951895
b v0.1.0 ([ROOT]/foo/b)
1896-
├── c v0.1.0 ([ROOT]/foo/c)
1897-
│ ├── otherdep v1.0.0
1898-
│ └── somedep v1.0.0
1899-
└── somedep v1.0.0
1896+
└── c v0.1.0 ([ROOT]/foo/c)
19001897
19011898
c v0.1.0 ([ROOT]/foo/c) (*)
19021899

0 commit comments

Comments
 (0)