@@ -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 ,
@@ -371,6 +390,11 @@ fn print_dependencies<'a>(
371390 }
372391 }
373392
393+ // Current level exceeds maximum display depth. Skip.
394+ if levels_continue. len ( ) + 1 > max_display_depth as usize {
395+ return ;
396+ }
397+
374398 let mut it = deps
375399 . iter ( )
376400 . filter ( |dep| {
@@ -386,26 +410,34 @@ fn print_dependencies<'a>(
386410 true
387411 }
388412 } )
413+ . filter ( |dep| {
414+ // Filter out packages to prune.
415+ match graph. node ( * * dep) {
416+ Node :: Package { package_id, .. } => {
417+ !pkgs_to_prune. iter ( ) . any ( |spec| spec. matches ( * package_id) )
418+ }
419+ _ => true ,
420+ }
421+ } )
389422 . peekable ( ) ;
390423
391424 while let Some ( dependency) = it. next ( ) {
392- if levels_continue. len ( ) + 1 <= max_display_depth as usize {
393- levels_continue. push ( it. peek ( ) . is_some ( ) ) ;
394- print_node (
395- config,
396- graph,
397- * dependency,
398- format,
399- symbols,
400- prefix,
401- no_dedupe,
402- max_display_depth,
403- no_proc_macro,
404- visited_deps,
405- levels_continue,
406- print_stack,
407- ) ;
408- levels_continue. pop ( ) ;
409- }
425+ levels_continue. push ( it. peek ( ) . is_some ( ) ) ;
426+ print_node (
427+ config,
428+ graph,
429+ * dependency,
430+ format,
431+ symbols,
432+ pkgs_to_prune,
433+ prefix,
434+ no_dedupe,
435+ max_display_depth,
436+ no_proc_macro,
437+ visited_deps,
438+ levels_continue,
439+ print_stack,
440+ ) ;
441+ levels_continue. pop ( ) ;
410442 }
411443}
0 commit comments