Skip to content

Commit b5998ae

Browse files
committed
feat(carog-tree): new --prune option
1 parent 8a747ab commit b5998ae

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/bin/cargo/commands/tree.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ pub fn cli() -> App {
5656
)
5757
.short("i"),
5858
)
59+
.arg(multi_opt(
60+
"prune",
61+
"SPEC",
62+
"Prune the given package from the display of the dependency tree",
63+
))
5964
.arg(opt("depth", "Maximum display depth of the dependency tree").value_name("DEPTH"))
6065
// Deprecated, use --prefix=none instead.
6166
.arg(Arg::with_name("no-indent").long("no-indent").hidden(true))
@@ -152,6 +157,16 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
152157
let (edge_kinds, no_proc_macro) = parse_edge_kinds(config, args)?;
153158
let graph_features = edge_kinds.contains(&EdgeKind::Feature);
154159

160+
let pkgs_to_prune = args.values_of("prune").map_or_else(
161+
|| Vec::new(),
162+
|ps| {
163+
let mut specs = ps.map(|p| p.to_string()).collect::<Vec<_>>();
164+
specs.sort_unstable();
165+
specs.dedup();
166+
specs
167+
},
168+
);
169+
155170
let packages = args.packages_from_flags()?;
156171
let mut invert = args
157172
.values_of("invert")
@@ -197,6 +212,7 @@ subtree of the package given to -p.\n\
197212
target,
198213
edge_kinds,
199214
invert,
215+
pkgs_to_prune,
200216
prefix,
201217
no_dedupe,
202218
duplicates: args.is_present("duplicates"),

src/cargo/ops/tree/mod.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ pub struct TreeOptions {
2727
/// The dependency kinds to display.
2828
pub edge_kinds: HashSet<EdgeKind>,
2929
pub invert: Vec<String>,
30+
/// The packages to prune from the display of the dependency tree.
31+
pub pkgs_to_prune: Vec<String>,
3032
/// The style of prefix for each line.
3133
pub prefix: Prefix,
3234
/// If `true`, duplicates will be repeated.
@@ -199,7 +201,19 @@ pub fn build_and_print(ws: &Workspace<'_>, opts: &TreeOptions) -> CargoResult<()
199201
graph.invert();
200202
}
201203

202-
print(ws.config(), opts, root_indexes, &graph)?;
204+
// Packages to prune.
205+
let pkgs_to_prune = opts
206+
.pkgs_to_prune
207+
.iter()
208+
.map(|p| PackageIdSpec::parse(p))
209+
.map(|r| {
210+
// Provide a error message if pkgid is not within the resolved
211+
// dependencies graph.
212+
r.and_then(|spec| spec.query(ws_resolve.targeted_resolve.iter()).and(Ok(spec)))
213+
})
214+
.collect::<CargoResult<Vec<PackageIdSpec>>>()?;
215+
216+
print(ws.config(), opts, root_indexes, &pkgs_to_prune, &graph)?;
203217
Ok(())
204218
}
205219

@@ -208,6 +222,7 @@ fn print(
208222
config: &Config,
209223
opts: &TreeOptions,
210224
roots: Vec<usize>,
225+
pkgs_to_prune: &[PackageIdSpec],
211226
graph: &Graph<'_>,
212227
) -> CargoResult<()> {
213228
let format = Pattern::new(&opts.format)
@@ -240,6 +255,7 @@ fn print(
240255
root_index,
241256
&format,
242257
symbols,
258+
pkgs_to_prune,
243259
opts.prefix,
244260
opts.no_dedupe,
245261
opts.max_display_depth,
@@ -260,6 +276,7 @@ fn print_node<'a>(
260276
node_index: usize,
261277
format: &Pattern,
262278
symbols: &Symbols,
279+
pkgs_to_prune: &[PackageIdSpec],
263280
prefix: Prefix,
264281
no_dedupe: bool,
265282
max_display_depth: u32,
@@ -319,6 +336,7 @@ fn print_node<'a>(
319336
node_index,
320337
format,
321338
symbols,
339+
pkgs_to_prune,
322340
prefix,
323341
no_dedupe,
324342
max_display_depth,
@@ -339,6 +357,7 @@ fn print_dependencies<'a>(
339357
node_index: usize,
340358
format: &Pattern,
341359
symbols: &Symbols,
360+
pkgs_to_prune: &[PackageIdSpec],
342361
prefix: Prefix,
343362
no_dedupe: bool,
344363
max_display_depth: u32,
@@ -391,6 +410,19 @@ fn print_dependencies<'a>(
391410
true
392411
}
393412
})
413+
.filter(|dep| {
414+
// Filter out packages to prune.
415+
if !pkgs_to_prune.is_empty() {
416+
match graph.node(**dep) {
417+
Node::Package { package_id, .. } => {
418+
!pkgs_to_prune.iter().any(|spec| spec.matches(*package_id))
419+
}
420+
_ => true,
421+
}
422+
} else {
423+
true
424+
}
425+
})
394426
.peekable();
395427

396428
while let Some(dependency) = it.next() {
@@ -401,6 +433,7 @@ fn print_dependencies<'a>(
401433
*dependency,
402434
format,
403435
symbols,
436+
pkgs_to_prune,
404437
prefix,
405438
no_dedupe,
406439
max_display_depth,

0 commit comments

Comments
 (0)