Skip to content

Commit 7c40344

Browse files
committed
Switch --invert to take the package name as an argument.
1 parent 96ff434 commit 7c40344

File tree

8 files changed

+177
-93
lines changed

8 files changed

+177
-93
lines changed

src/bin/cargo/commands/tree.rs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::command_prelude::*;
22
use anyhow::{bail, format_err};
33
use cargo::core::dependency::DepKind;
44
use cargo::ops::tree::{self, EdgeKind};
5+
use cargo::ops::Packages;
56
use cargo::util::CargoResult;
67
use std::collections::HashSet;
78
use std::str::FromStr;
@@ -40,7 +41,14 @@ pub fn cli() -> App {
4041
)
4142
.short("e"),
4243
)
43-
.arg(opt("invert", "Invert the tree direction").short("i"))
44+
.arg(
45+
optional_multi_opt(
46+
"invert",
47+
"SPEC",
48+
"Invert the tree direction and focus on the given package",
49+
)
50+
.short("i"),
51+
)
4452
.arg(Arg::with_name("no-indent").long("no-indent").hidden(true))
4553
.arg(
4654
Arg::with_name("prefix-depth")
@@ -119,17 +127,48 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
119127
let edge_kinds = parse_edge_kinds(config, args)?;
120128
let graph_features = edge_kinds.contains(&EdgeKind::Feature);
121129

130+
let packages = args.packages_from_flags()?;
131+
let mut invert = args
132+
.values_of("invert")
133+
.map_or_else(|| Vec::new(), |is| is.map(|s| s.to_string()).collect());
134+
if args.is_present_with_zero_values("invert") {
135+
match &packages {
136+
Packages::Packages(ps) => {
137+
// Backwards compatibility with old syntax of `cargo tree -i -p foo`.
138+
invert.extend(ps.clone());
139+
}
140+
_ => {
141+
return Err(format_err!(
142+
"The `-i` flag requires a package name.\n\
143+
\n\
144+
The `-i` flag is used to inspect the reverse dependencies of a specific\n\
145+
package. It will invert the tree and display the packages that depend on the\n\
146+
given package.\n\
147+
\n\
148+
Note that in a workspace, by default it will only display the package's\n\
149+
reverse dependencies inside the tree of the workspace member in the current\n\
150+
directory. The --workspace flag can be used to extend it so that it will show\n\
151+
the package's reverse dependencies across the entire workspace. The -p flag\n\
152+
can be used to display the package's reverse dependencies only with the\n\
153+
subtree of the package given to -p.\n\
154+
"
155+
)
156+
.into());
157+
}
158+
}
159+
}
160+
122161
let ws = args.workspace(config)?;
123162
let charset = tree::Charset::from_str(args.value_of("charset").unwrap())
124163
.map_err(|e| anyhow::anyhow!("{}", e))?;
125164
let opts = tree::TreeOptions {
126165
features: values(args, "features"),
127166
all_features: args.is_present("all-features"),
128167
no_default_features: args.is_present("no-default-features"),
129-
packages: args.packages_from_flags()?,
168+
packages,
130169
target,
131170
edge_kinds,
132-
invert: args.is_present("invert"),
171+
invert,
133172
prefix,
134173
no_dedupe: args.is_present("no-dedupe"),
135174
duplicates: args.is_present("duplicates"),

src/cargo/ops/tree/mod.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use self::format::Pattern;
44
use crate::core::compiler::{CompileKind, RustcTargetData};
55
use crate::core::dependency::DepKind;
66
use crate::core::resolver::{HasDevUnits, ResolveOpts};
7-
use crate::core::{Package, PackageId, Workspace};
7+
use crate::core::{Package, PackageId, PackageIdSpec, Workspace};
88
use crate::ops::{self, Packages};
99
use crate::util::CargoResult;
1010
use anyhow::{bail, Context};
@@ -27,7 +27,7 @@ pub struct TreeOptions {
2727
pub target: Target,
2828
/// The dependency kinds to display.
2929
pub edge_kinds: HashSet<EdgeKind>,
30-
pub invert: bool,
30+
pub invert: Vec<String>,
3131
/// The style of prefix for each line.
3232
pub prefix: Prefix,
3333
/// If `true`, duplicates will be repeated.
@@ -177,7 +177,15 @@ pub fn build_and_print(ws: &Workspace<'_>, opts: &TreeOptions) -> CargoResult<()
177177
opts,
178178
)?;
179179

180-
let root_ids = ws_resolve.targeted_resolve.specs_to_ids(&specs)?;
180+
let root_specs = if opts.invert.is_empty() {
181+
specs
182+
} else {
183+
opts.invert
184+
.iter()
185+
.map(|p| PackageIdSpec::parse(p))
186+
.collect::<CargoResult<Vec<PackageIdSpec>>>()?
187+
};
188+
let root_ids = ws_resolve.targeted_resolve.specs_to_ids(&root_specs)?;
181189
let root_indexes = graph.indexes_from_ids(&root_ids);
182190

183191
let root_indexes = if opts.duplicates {
@@ -188,7 +196,7 @@ pub fn build_and_print(ws: &Workspace<'_>, opts: &TreeOptions) -> CargoResult<()
188196
root_indexes
189197
};
190198

191-
if opts.invert || opts.duplicates {
199+
if !opts.invert.is_empty() || opts.duplicates {
192200
graph.invert();
193201
}
194202

src/doc/asciidoc-extension.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ def process document, output
8484
end
8585
end
8686

87+
# Man pages are ASCII only. Unfortunately asciidoc doesn't process these
88+
# characters for us. The `cargo tree` manpage needs a little assistance.
89+
class SpecialCharPostprocessor < Extensions::Postprocessor
90+
def process document, output
91+
if document.basebackend? 'manpage'
92+
output = output.gsub(/│/, '|')
93+
.gsub(/├/, '|')
94+
.gsub(/└/, '`')
95+
.gsub(/─/, '\-')
96+
end
97+
output
98+
end
99+
end
100+
87101
# General utility for converting text. Example:
88102
#
89103
# convert:lowercase[{somevar}]
@@ -107,4 +121,5 @@ def process parent, target, attrs
107121
inline_macro LinkCargoInlineMacro
108122
inline_macro ConvertInlineMacro
109123
postprocessor MonoPostprocessor
124+
postprocessor SpecialCharPostprocessor
110125
end

src/doc/man/cargo-tree.adoc

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,24 @@ myproject v0.1.0 (/myproject)
5252

5353
In this tree, `myproject` depends on `log` with the `serde` feature. `log` in
5454
turn depends on `cfg-if` with "default" features. When using `-e features` it
55-
can be helpful to use `-i` and `-p` flags to show how the features flow into a
56-
package. See the examples below for more detail.
55+
can be helpful to use `-i` flag to show how the features flow into a package.
56+
See the examples below for more detail.
5757

5858
== OPTIONS
5959

6060
=== Tree Options
6161

62-
*-i*::
63-
*--invert*::
64-
Invert the tree. Typically this flag is used with the `-p` flag to show
65-
the dependencies for a specific package.
62+
*-i* _SPEC_::
63+
*--invert* _SPEC_::
64+
Show the reverse dependencies for the given package. This flag will invert
65+
the tree and display the packages that depend on the given package.
66+
+
67+
Note that in a workspace, by default it will only display the package's
68+
reverse dependencies inside the tree of the workspace member in the current
69+
directory. The `--workspace` flag can be used to extend it so that it will
70+
show the package's reverse dependencies across the entire workspace. The `-p`
71+
flag can be used to display the package's reverse dependencies only with the
72+
subtree of the package given to `-p`.
6673

6774
*--no-dedupe*::
6875
Do not de-duplicate repeated dependencies. Usually, when a package has
@@ -161,9 +168,9 @@ include::section-exit-status.adoc[]
161168

162169
cargo tree
163170

164-
. Display all the packages that depend on the specified package:
171+
. Display all the packages that depend on the `syn` package:
165172

166-
cargo tree -i -p syn
173+
cargo tree -i syn
167174

168175
. Show the features enabled on each package:
169176

@@ -174,13 +181,13 @@ include::section-exit-status.adoc[]
174181

175182
cargo tree -d
176183

177-
. Explain why features are enabled for the given package:
184+
. Explain why features are enabled for the `syn` package:
178185

179-
cargo tree -e features -i -p syn
186+
cargo tree -e features -i syn
180187
+
181188
The `-e features` flag is used to show features. The `-i` flag is used to
182-
invert the graph so that it displays the packages that depend on `syn` (not
183-
what `syn` depends on). An example of what this would display:
189+
invert the graph so that it displays the packages that depend on `syn`. An
190+
example of what this would display:
184191
+
185192
----
186193
syn v1.0.17

src/doc/man/generated/cargo-tree.html

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ <h2 id="cargo_tree_description">DESCRIPTION</h2>
5757
<div class="paragraph">
5858
<p>In this tree, <code>myproject</code> depends on <code>log</code> with the <code>serde</code> feature. <code>log</code> in
5959
turn depends on <code>cfg-if</code> with "default" features. When using <code>-e features</code> it
60-
can be helpful to use <code>-i</code> and <code>-p</code> flags to show how the features flow into a
61-
package. See the examples below for more detail.</p>
60+
can be helpful to use <code>-i</code> flag to show how the features flow into a package.
61+
See the examples below for more detail.</p>
6262
</div>
6363
</div>
6464
</div>
@@ -69,11 +69,19 @@ <h2 id="cargo_tree_options">OPTIONS</h2>
6969
<h3 id="cargo_tree_tree_options">Tree Options</h3>
7070
<div class="dlist">
7171
<dl>
72-
<dt class="hdlist1"><strong>-i</strong></dt>
73-
<dt class="hdlist1"><strong>--invert</strong></dt>
72+
<dt class="hdlist1"><strong>-i</strong> <em>SPEC</em></dt>
73+
<dt class="hdlist1"><strong>--invert</strong> <em>SPEC</em></dt>
7474
<dd>
75-
<p>Invert the tree. Typically this flag is used with the <code>-p</code> flag to show
76-
the dependencies for a specific package.</p>
75+
<p>Show the reverse dependencies for the given package. This flag will invert
76+
the tree and display the packages that depend on the given package.</p>
77+
<div class="paragraph">
78+
<p>Note that in a workspace, by default it will only display the package&#8217;s
79+
reverse dependencies inside the tree of the workspace member in the current
80+
directory. The <code>--workspace</code> flag can be used to extend it so that it will
81+
show the package&#8217;s reverse dependencies across the entire workspace. The <code>-p</code>
82+
flag can be used to display the package&#8217;s reverse dependencies only with the
83+
subtree of the package given to <code>-p</code>.</p>
84+
</div>
7785
</dd>
7886
<dt class="hdlist1"><strong>--no-dedupe</strong></dt>
7987
<dd>
@@ -412,10 +420,10 @@ <h2 id="cargo_tree_examples">EXAMPLES</h2>
412420
</div>
413421
</li>
414422
<li>
415-
<p>Display all the packages that depend on the specified package:</p>
423+
<p>Display all the packages that depend on the <code>syn</code> package:</p>
416424
<div class="literalblock">
417425
<div class="content">
418-
<pre>cargo tree -i -p syn</pre>
426+
<pre>cargo tree -i syn</pre>
419427
</div>
420428
</div>
421429
</li>
@@ -437,16 +445,16 @@ <h2 id="cargo_tree_examples">EXAMPLES</h2>
437445
</div>
438446
</li>
439447
<li>
440-
<p>Explain why features are enabled for the given package:</p>
448+
<p>Explain why features are enabled for the <code>syn</code> package:</p>
441449
<div class="literalblock">
442450
<div class="content">
443-
<pre>cargo tree -e features -i -p syn</pre>
451+
<pre>cargo tree -e features -i syn</pre>
444452
</div>
445453
</div>
446454
<div class="paragraph">
447455
<p>The <code>-e features</code> flag is used to show features. The <code>-i</code> flag is used to
448-
invert the graph so that it displays the packages that depend on <code>syn</code> (not
449-
what <code>syn</code> depends on). An example of what this would display:</p>
456+
invert the graph so that it displays the packages that depend on <code>syn</code>. An
457+
example of what this would display:</p>
450458
</div>
451459
<div class="listingblock">
452460
<div class="content">

0 commit comments

Comments
 (0)