@@ -27,6 +27,8 @@ pub struct TreeOptions {
27
27
/// The dependency kinds to display.
28
28
pub edge_kinds : HashSet < EdgeKind > ,
29
29
pub invert : Vec < String > ,
30
+ /// The packages to prune from the display of the dependency tree.
31
+ pub pkgs_to_prune : Vec < String > ,
30
32
/// The style of prefix for each line.
31
33
pub prefix : Prefix ,
32
34
/// If `true`, duplicates will be repeated.
@@ -199,7 +201,19 @@ pub fn build_and_print(ws: &Workspace<'_>, opts: &TreeOptions) -> CargoResult<()
199
201
graph. invert ( ) ;
200
202
}
201
203
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) ?;
203
217
Ok ( ( ) )
204
218
}
205
219
@@ -208,6 +222,7 @@ fn print(
208
222
config : & Config ,
209
223
opts : & TreeOptions ,
210
224
roots : Vec < usize > ,
225
+ pkgs_to_prune : & [ PackageIdSpec ] ,
211
226
graph : & Graph < ' _ > ,
212
227
) -> CargoResult < ( ) > {
213
228
let format = Pattern :: new ( & opts. format )
@@ -240,6 +255,7 @@ fn print(
240
255
root_index,
241
256
& format,
242
257
symbols,
258
+ pkgs_to_prune,
243
259
opts. prefix ,
244
260
opts. no_dedupe ,
245
261
opts. max_display_depth ,
@@ -260,6 +276,7 @@ fn print_node<'a>(
260
276
node_index : usize ,
261
277
format : & Pattern ,
262
278
symbols : & Symbols ,
279
+ pkgs_to_prune : & [ PackageIdSpec ] ,
263
280
prefix : Prefix ,
264
281
no_dedupe : bool ,
265
282
max_display_depth : u32 ,
@@ -319,6 +336,7 @@ fn print_node<'a>(
319
336
node_index,
320
337
format,
321
338
symbols,
339
+ pkgs_to_prune,
322
340
prefix,
323
341
no_dedupe,
324
342
max_display_depth,
@@ -339,6 +357,7 @@ fn print_dependencies<'a>(
339
357
node_index : usize ,
340
358
format : & Pattern ,
341
359
symbols : & Symbols ,
360
+ pkgs_to_prune : & [ PackageIdSpec ] ,
342
361
prefix : Prefix ,
343
362
no_dedupe : bool ,
344
363
max_display_depth : u32 ,
@@ -371,6 +390,11 @@ fn print_dependencies<'a>(
371
390
}
372
391
}
373
392
393
+ // Current level exceeds maximum display depth. Skip.
394
+ if levels_continue. len ( ) + 1 > max_display_depth as usize {
395
+ return ;
396
+ }
397
+
374
398
let mut it = deps
375
399
. iter ( )
376
400
. filter ( |dep| {
@@ -386,26 +410,34 @@ fn print_dependencies<'a>(
386
410
true
387
411
}
388
412
} )
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
+ } )
389
422
. peekable ( ) ;
390
423
391
424
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 ( ) ;
410
442
}
411
443
}
0 commit comments