Deduplicate source edges in annotations (#4530)

## Summary

Not relevant today, but it will be once we support universal resolution,
in which a package can be repeated.
This commit is contained in:
Charlie Marsh 2024-06-25 17:10:09 -04:00 committed by GitHub
parent ad42206e50
commit 08bf6fb87c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -194,19 +194,24 @@ impl std::fmt::Display for DisplayResolutionGraph<'_> {
// If enabled, include annotations to indicate the dependencies that requested each // If enabled, include annotations to indicate the dependencies that requested each
// package (e.g., `# via mypy`). // package (e.g., `# via mypy`).
if self.include_annotations { if self.include_annotations {
// Display all dependencies. // Display all dependents (i.e., all packages that depend on the current package).
let mut edges = petgraph let dependents = {
.edges_directed(index, Direction::Incoming) let mut dependents = petgraph
.map(|edge| &petgraph[edge.source()]) .edges_directed(index, Direction::Incoming)
.collect::<Vec<_>>(); .map(|edge| &petgraph[edge.source()])
edges.sort_unstable_by_key(|package| package.name()); .map(distribution_types::Name::name)
.collect::<Vec<_>>();
dependents.sort_unstable();
dependents.dedup();
dependents
};
// Include all external sources (e.g., requirements files). // Include all external sources (e.g., requirements files).
let default = BTreeSet::default(); let default = BTreeSet::default();
let source = sources.get(node.name()).unwrap_or(&default); let source = sources.get(node.name()).unwrap_or(&default);
match self.annotation_style { match self.annotation_style {
AnnotationStyle::Line => match edges.as_slice() { AnnotationStyle::Line => match dependents.as_slice() {
[] if source.is_empty() => {} [] if source.is_empty() => {}
[] if source.len() == 1 => { [] if source.len() == 1 => {
let separator = if has_hashes { "\n " } else { " " }; let separator = if has_hashes { "\n " } else { " " };
@ -215,19 +220,19 @@ impl std::fmt::Display for DisplayResolutionGraph<'_> {
.to_string(); .to_string();
annotation = Some((separator, comment)); annotation = Some((separator, comment));
} }
edges => { dependents => {
let separator = if has_hashes { "\n " } else { " " }; let separator = if has_hashes { "\n " } else { " " };
let deps = edges let dependents = dependents
.iter() .iter()
.map(|dependency| format!("{}", dependency.name())) .map(ToString::to_string)
.chain(source.iter().map(ToString::to_string)) .chain(source.iter().map(ToString::to_string))
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(", "); .join(", ");
let comment = format!("# via {deps}").green().to_string(); let comment = format!("# via {dependents}").green().to_string();
annotation = Some((separator, comment)); annotation = Some((separator, comment));
} }
}, },
AnnotationStyle::Split => match edges.as_slice() { AnnotationStyle::Split => match dependents.as_slice() {
[] if source.is_empty() => {} [] if source.is_empty() => {}
[] if source.len() == 1 => { [] if source.len() == 1 => {
let separator = "\n"; let separator = "\n";
@ -236,25 +241,21 @@ impl std::fmt::Display for DisplayResolutionGraph<'_> {
.to_string(); .to_string();
annotation = Some((separator, comment)); annotation = Some((separator, comment));
} }
[edge] if source.is_empty() => { [dependent] if source.is_empty() => {
let separator = "\n"; let separator = "\n";
let comment = format!(" # via {}", edge.name()).green().to_string(); let comment = format!(" # via {dependent}").green().to_string();
annotation = Some((separator, comment)); annotation = Some((separator, comment));
} }
edges => { dependents => {
let separator = "\n"; let separator = "\n";
let deps = source let dependent = source
.iter() .iter()
.map(ToString::to_string) .map(ToString::to_string)
.chain( .chain(dependents.iter().map(ToString::to_string))
edges
.iter()
.map(|dependency| format!("{}", dependency.name())),
)
.map(|name| format!(" # {name}")) .map(|name| format!(" # {name}"))
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join("\n"); .join("\n");
let comment = format!(" # via\n{deps}").green().to_string(); let comment = format!(" # via\n{dependent}").green().to_string();
annotation = Some((separator, comment)); annotation = Some((separator, comment));
} }
}, },