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"), .short("i"),
) )
.arg(opt("depth", "Maximum display depth of the dependency tree").value_name("DEPTH"))
// Deprecated, use --prefix=none instead. // Deprecated, use --prefix=none instead.
.arg(Arg::with_name("no-indent").long("no-indent").hidden(true)) .arg(Arg::with_name("no-indent").long("no-indent").hidden(true))
// Deprecated, use --prefix=depth instead. // Deprecated, use --prefix=depth instead.
@ -202,6 +203,7 @@ subtree of the package given to -p.\n\
charset, charset,
format: args.value_of("format").unwrap().to_string(), format: args.value_of("format").unwrap().to_string(),
graph_features, graph_features,
max_display_depth: args.value_of_u32("depth")?.unwrap_or(u32::MAX),
no_proc_macro, no_proc_macro,
}; };

View File

@ -43,6 +43,8 @@ pub struct TreeOptions {
pub format: String, pub format: String,
/// Includes features in the tree as separate nodes. /// Includes features in the tree as separate nodes.
pub graph_features: bool, pub graph_features: bool,
/// Maximum display depth of the dependency tree.
pub max_display_depth: u32,
/// Exculdes proc-macro dependencies. /// Exculdes proc-macro dependencies.
pub no_proc_macro: bool, pub no_proc_macro: bool,
} }
@ -240,6 +242,7 @@ fn print(
symbols, symbols,
opts.prefix, opts.prefix,
opts.no_dedupe, opts.no_dedupe,
opts.max_display_depth,
opts.no_proc_macro, opts.no_proc_macro,
&mut visited_deps, &mut visited_deps,
&mut levels_continue, &mut levels_continue,
@ -259,6 +262,7 @@ fn print_node<'a>(
symbols: &Symbols, symbols: &Symbols,
prefix: Prefix, prefix: Prefix,
no_dedupe: bool, no_dedupe: bool,
max_display_depth: u32,
no_proc_macro: bool, no_proc_macro: bool,
visited_deps: &mut HashSet<usize>, visited_deps: &mut HashSet<usize>,
levels_continue: &mut Vec<bool>, levels_continue: &mut Vec<bool>,
@ -317,6 +321,7 @@ fn print_node<'a>(
symbols, symbols,
prefix, prefix,
no_dedupe, no_dedupe,
max_display_depth,
no_proc_macro, no_proc_macro,
visited_deps, visited_deps,
levels_continue, levels_continue,
@ -336,6 +341,7 @@ fn print_dependencies<'a>(
symbols: &Symbols, symbols: &Symbols,
prefix: Prefix, prefix: Prefix,
no_dedupe: bool, no_dedupe: bool,
max_display_depth: u32,
no_proc_macro: bool, no_proc_macro: bool,
visited_deps: &mut HashSet<usize>, visited_deps: &mut HashSet<usize>,
levels_continue: &mut Vec<bool>, levels_continue: &mut Vec<bool>,
@ -383,20 +389,23 @@ fn print_dependencies<'a>(
.peekable(); .peekable();
while let Some(dependency) = it.next() { while let Some(dependency) = it.next() {
levels_continue.push(it.peek().is_some()); if levels_continue.len() + 1 <= max_display_depth as usize {
print_node( levels_continue.push(it.peek().is_some());
config, print_node(
graph, config,
*dependency, graph,
format, *dependency,
symbols, format,
prefix, symbols,
no_dedupe, prefix,
no_proc_macro, no_dedupe,
visited_deps, max_display_depth,
levels_continue, no_proc_macro,
print_stack, visited_deps,
); levels_continue,
levels_continue.pop(); 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`. subtree of the package given to `-p`.
{{/option}} {{/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`" }} {{#option "`--no-dedupe`" }}
Do not de-duplicate repeated dependencies. Usually, when a package has already Do not de-duplicate repeated dependencies. Usually, when a package has already
displayed its dependencies, further occurrences will not re-display its 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 package's reverse dependencies only with the subtree of the package
given to -p. given to -p.
--depth depth
Maximum display depth of the dependency tree. A depth of 1 displays
the direct dependencies, for example.
--no-dedupe --no-dedupe
Do not de-duplicate repeated dependencies. Usually, when a package Do not de-duplicate repeated dependencies. Usually, when a package
has already displayed its dependencies, further occurrences will not 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> 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> <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 <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 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\&. subtree of the package given to \fB\-p\fR\&.
.RE .RE
.sp .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 \fB\-\-no\-dedupe\fR
.RS 4 .RS 4
Do not de\-duplicate repeated dependencies. Usually, when a package has already Do not de\-duplicate repeated dependencies. Usually, when a package has already

View File

@ -1627,3 +1627,96 @@ foo v0.1.0 ([..]/foo)
) )
.run(); .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();
}