Skip to content

Commit

Permalink
Auto merge of rust-lang#9499 - weihanglo:tree-depth, r=ehuss
Browse files Browse the repository at this point in the history
Add `--depth` option for `cargo-tree`

Part of rust-lang#8105

Note that the `--depth` option only regards the "tree" depth but not dependency depth.

## To resolve

Bike-shedding naming problem: `-L,--level`  or `--depth`?
  • Loading branch information
bors committed May 28, 2021
2 parents 238a9fa + da2327f commit b1684e2
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/bin/cargo/commands/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub fn cli() -> App {
)
.short("i"),
)
.arg(opt("depth", "Maximum display depth of the dependency tree").value_name("DEPTH"))
// Deprecated, use --prefix=none instead.
.arg(Arg::with_name("no-indent").long("no-indent").hidden(true))
// Deprecated, use --prefix=depth instead.
Expand Down Expand Up @@ -202,6 +203,7 @@ subtree of the package given to -p.\n\
charset,
format: args.value_of("format").unwrap().to_string(),
graph_features,
max_display_depth: args.value_of_u32("depth")?.unwrap_or(u32::MAX),
no_proc_macro,
};

Expand Down
39 changes: 24 additions & 15 deletions src/cargo/ops/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ pub struct TreeOptions {
pub format: String,
/// Includes features in the tree as separate nodes.
pub graph_features: bool,
/// Maximum display depth of the dependency tree.
pub max_display_depth: u32,
/// Exculdes proc-macro dependencies.
pub no_proc_macro: bool,
}
Expand Down Expand Up @@ -240,6 +242,7 @@ fn print(
symbols,
opts.prefix,
opts.no_dedupe,
opts.max_display_depth,
opts.no_proc_macro,
&mut visited_deps,
&mut levels_continue,
Expand All @@ -259,6 +262,7 @@ fn print_node<'a>(
symbols: &Symbols,
prefix: Prefix,
no_dedupe: bool,
max_display_depth: u32,
no_proc_macro: bool,
visited_deps: &mut HashSet<usize>,
levels_continue: &mut Vec<bool>,
Expand Down Expand Up @@ -317,6 +321,7 @@ fn print_node<'a>(
symbols,
prefix,
no_dedupe,
max_display_depth,
no_proc_macro,
visited_deps,
levels_continue,
Expand All @@ -336,6 +341,7 @@ fn print_dependencies<'a>(
symbols: &Symbols,
prefix: Prefix,
no_dedupe: bool,
max_display_depth: u32,
no_proc_macro: bool,
visited_deps: &mut HashSet<usize>,
levels_continue: &mut Vec<bool>,
Expand Down Expand Up @@ -383,20 +389,23 @@ fn print_dependencies<'a>(
.peekable();

while let Some(dependency) = it.next() {
levels_continue.push(it.peek().is_some());
print_node(
config,
graph,
*dependency,
format,
symbols,
prefix,
no_dedupe,
no_proc_macro,
visited_deps,
levels_continue,
print_stack,
);
levels_continue.pop();
if levels_continue.len() + 1 <= max_display_depth as usize {
levels_continue.push(it.peek().is_some());
print_node(
config,
graph,
*dependency,
format,
symbols,
prefix,
no_dedupe,
max_display_depth,
no_proc_macro,
visited_deps,
levels_continue,
print_stack,
);
levels_continue.pop();
}
}
}
5 changes: 5 additions & 0 deletions src/doc/man/cargo-tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ flag can be used to display the package's reverse dependencies only with the
subtree of the package given to `-p`.
{{/option}}

{{#option "`--depth` _depth_" }}
Maximum display depth of the dependency tree. A depth of 1 displays the direct
dependencies, for example.
{{/option}}

{{#option "`--no-dedupe`" }}
Do not de-duplicate repeated dependencies. Usually, when a package has already
displayed its dependencies, further occurrences will not re-display its
Expand Down
4 changes: 4 additions & 0 deletions src/doc/man/generated_txt/cargo-tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ OPTIONS
package's reverse dependencies only with the 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.

--no-dedupe
Do not de-duplicate repeated dependencies. Usually, when a package
has already displayed its dependencies, further occurrences will not
Expand Down
5 changes: 5 additions & 0 deletions src/doc/src/commands/cargo-tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ flag can be used to display the package's reverse dependencies only with the
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>


<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>
<dd class="option-desc">Do not de-duplicate repeated dependencies. Usually, when a package has already
displayed its dependencies, further occurrences will not re-display its
Expand Down
6 changes: 6 additions & 0 deletions src/etc/man/cargo-tree.1
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ flag can be used to display the package's reverse dependencies only with the
subtree of the package given to \fB\-p\fR\&.
.RE
.sp
\fB\-\-depth\fR \fIdepth\fR
.RS 4
Maximum display depth of the dependency tree. A depth of 1 displays the direct
dependencies, for example.
.RE
.sp
\fB\-\-no\-dedupe\fR
.RS 4
Do not de\-duplicate repeated dependencies. Usually, when a package has already
Expand Down
93 changes: 93 additions & 0 deletions tests/testsuite/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1627,3 +1627,96 @@ foo v0.1.0 ([..]/foo)
)
.run();
}

#[cargo_test]
fn depth_limit() {
let p = make_simple_proj();

p.cargo("tree --depth 0")
.with_stdout(
"\
foo v0.1.0 ([..]/foo)
[build-dependencies]
[dev-dependencies]
",
)
.run();

p.cargo("tree --depth 1")
.with_stdout(
"\
foo v0.1.0 ([..]/foo)
├── a v1.0.0
└── c v1.0.0
[build-dependencies]
└── bdep v1.0.0
[dev-dependencies]
└── devdep v1.0.0
",
)
.run();

p.cargo("tree --depth 2")
.with_stdout(
"\
foo v0.1.0 ([..]/foo)
├── a v1.0.0
│ └── b v1.0.0
└── c v1.0.0
[build-dependencies]
└── bdep v1.0.0
└── b v1.0.0 (*)
[dev-dependencies]
└── devdep v1.0.0
└── b v1.0.0 (*)
",
)
.run();

// specify a package
p.cargo("tree -p bdep --depth 1")
.with_stdout(
"\
bdep v1.0.0
└── b v1.0.0
",
)
.run();

// different prefix
p.cargo("tree --depth 1 --prefix depth")
.with_stdout(
"\
0foo v0.1.0 ([..]/foo)
1a v1.0.0
1c v1.0.0
1bdep v1.0.0
1devdep v1.0.0
",
)
.run();

// with edge-kinds
p.cargo("tree --depth 1 -e no-dev")
.with_stdout(
"\
foo v0.1.0 ([..]/foo)
├── a v1.0.0
└── c v1.0.0
[build-dependencies]
└── bdep v1.0.0
",
)
.run();

// invert
p.cargo("tree --depth 1 --invert c")
.with_stdout(
"\
c v1.0.0
├── b v1.0.0
└── foo v0.1.0 ([..]/foo)
",
)
.run();
}

0 comments on commit b1684e2

Please sign in to comment.