Auto merge of #9499 - weihanglo:tree-depth, r=ehuss

Add `--depth` option for `cargo-tree`

Part of #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`?
This commit is contained in:
bors 2021-05-28 16:27:46 +00:00
commit b1684e2849
7 changed files with 139 additions and 15 deletions

View File

@ -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.
@ -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,
};

View File

@ -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,
}
@ -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,
@ -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>,
@ -317,6 +321,7 @@ fn print_node<'a>(
symbols,
prefix,
no_dedupe,
max_display_depth,
no_proc_macro,
visited_deps,
levels_continue,
@ -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>,
@ -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();
}
}
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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();
}