Skip to content

Commit

Permalink
feat(tree): implement filtering logic for --depth workspace flag
Browse files Browse the repository at this point in the history
  • Loading branch information
ShoyuVanilla committed Dec 12, 2024
1 parent 65931bc commit f744231
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 9 deletions.
13 changes: 10 additions & 3 deletions src/cargo/ops/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -90,18 +91,20 @@ impl FromStr for Prefix {
#[derive(Clone, Copy)]
pub enum DisplayDepth {
MaxDisplayDepth(u32),
Workspace,
}

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

fn from_str(s: &str) -> Result<Self, Self::Err> {
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
),
Expand Down Expand Up @@ -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.
Expand All @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions src/doc/man/cargo-tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`" }}
Expand Down
3 changes: 3 additions & 0 deletions src/doc/man/generated_txt/cargo-tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/doc/src/commands/cargo-tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ subtree of the package given to <code>-p</code>.</dd>

<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>
<dd class="option-desc">Maximum display depth of the dependency tree. A depth of 1 displays the direct
dependencies, for example.</dd>
dependencies, for example.</p>
<p>If the given value is <code>workspace</code>, only shows the dependencies that are member
of the current workspace, instead.</dd>


<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>
Expand Down
3 changes: 3 additions & 0 deletions src/etc/man/cargo-tree.1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 2 additions & 5 deletions tests/testsuite/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) (*)
Expand Down

0 comments on commit f744231

Please sign in to comment.