Use `Entry` API in lockfile-to-requirements.txt construction (#12561)

This commit is contained in:
Charlie Marsh 2025-03-30 14:49:36 -04:00 committed by GitHub
parent 2a28dacf28
commit 56914c9c60
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 14 additions and 20 deletions

View File

@ -75,12 +75,9 @@ impl<'lock> RequirementsTxtExport<'lock> {
if dev.prod() { if dev.prod() {
// Add the workspace package to the graph. // Add the workspace package to the graph.
if let Entry::Vacant(entry) = inverse.entry(&dist.id) { let index = *inverse
entry.insert(graph.add_node(Node::Package(dist))); .entry(&dist.id)
} .or_insert_with(|| graph.add_node(Node::Package(dist)));
// Add an edge from the root.
let index = inverse[&dist.id];
graph.add_edge(root, index, Edge::Prod(MarkerTree::TRUE)); graph.add_edge(root, index, Edge::Prod(MarkerTree::TRUE));
// Push its dependencies on the queue. // Push its dependencies on the queue.
@ -126,14 +123,13 @@ impl<'lock> RequirementsTxtExport<'lock> {
let dep_dist = target.lock().find_by_id(&dep.package_id); let dep_dist = target.lock().find_by_id(&dep.package_id);
// Add the dependency to the graph. // Add the dependency to the graph.
if let Entry::Vacant(entry) = inverse.entry(&dep.package_id) { let dep_index = *inverse
entry.insert(graph.add_node(Node::Package(dep_dist))); .entry(&dep.package_id)
} .or_insert_with(|| graph.add_node(Node::Package(dep_dist)));
// Add an edge from the root. Development dependencies may be installed without // Add an edge from the root. Development dependencies may be installed without
// installing the workspace package itself (which can never have markers on it // installing the workspace package itself (which can never have markers on it
// anyway), so they're directly connected to the root. // anyway), so they're directly connected to the root.
let dep_index = inverse[&dep.package_id];
graph.add_edge( graph.add_edge(
root, root,
dep_index, dep_index,
@ -215,13 +211,12 @@ impl<'lock> RequirementsTxtExport<'lock> {
// Simplify the marker. // Simplify the marker.
let marker = target.lock().simplify_environment(marker); let marker = target.lock().simplify_environment(marker);
// Add the dependency to the graph. // Add the dependency to the graph and get its index.
if let Entry::Vacant(entry) = inverse.entry(&dist.id) { let dep_index = *inverse
entry.insert(graph.add_node(Node::Package(dist))); .entry(&dist.id)
} .or_insert_with(|| graph.add_node(Node::Package(dist)));
// Add an edge from the root. // Add an edge from the root.
let dep_index = inverse[&dist.id];
graph.add_edge(root, dep_index, Edge::Prod(marker)); graph.add_edge(root, dep_index, Edge::Prod(marker));
// Push its dependencies on the queue. // Push its dependencies on the queue.
@ -262,12 +257,11 @@ impl<'lock> RequirementsTxtExport<'lock> {
let dep_dist = target.lock().find_by_id(&dep.package_id); let dep_dist = target.lock().find_by_id(&dep.package_id);
// Add the dependency to the graph. // Add the dependency to the graph.
if let Entry::Vacant(entry) = inverse.entry(&dep.package_id) { let dep_index = *inverse
entry.insert(graph.add_node(Node::Package(dep_dist))); .entry(&dep.package_id)
} .or_insert_with(|| graph.add_node(Node::Package(dep_dist)));
// Add the edge. // Add an edge from the dependency.
let dep_index = inverse[&dep.package_id];
graph.add_edge( graph.add_edge(
index, index,
dep_index, dep_index,