Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions crates/uv-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3851,6 +3851,13 @@ pub struct ExportArgs {
#[arg(long, conflicts_with_all = ["only_group", "only_dev"])]
pub all_groups: bool,

/// Exclude comment annotations indicating the source of each package.
#[arg(long, overrides_with("annotate"))]
pub no_annotate: bool,

#[arg(long, overrides_with("no_annotate"), hide = true)]
pub annotate: bool,

/// Exclude the comment header at the top of the generated output file.
#[arg(long, overrides_with("header"))]
pub no_header: bool,
Expand Down
39 changes: 38 additions & 1 deletion crates/uv-resolver/src/lock/requirements_txt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::fmt::Formatter;
use std::path::{Component, Path, PathBuf};

use either::Either;
use owo_colors::OwoColorize;
use petgraph::graph::NodeIndex;
use petgraph::prelude::EdgeRef;
use petgraph::visit::IntoNodeReferences;
Expand Down Expand Up @@ -41,6 +42,7 @@ impl<'lock> RequirementsTxtExport<'lock> {
prune: &[PackageName],
extras: &ExtrasSpecification,
dev: &DependencyGroupsWithDefaults,
annotate: bool,
editable: EditableMode,
hashes: bool,
install_options: &'lock InstallOptions,
Expand Down Expand Up @@ -312,6 +314,21 @@ impl<'lock> RequirementsTxtExport<'lock> {
.map(|(index, package)| Requirement {
package,
marker: reachability.remove(&index).unwrap_or_default(),
dependents: if annotate {
let mut dependents = graph
.edges_directed(index, Direction::Incoming)
.map(|edge| &graph[edge.source()])
.filter_map(|node| match node {
Node::Package(package) => Some(*package),
Node::Root => None,
})
.collect::<Vec<_>>();
dependents.sort_unstable_by_key(|package| package.name());
dependents.dedup_by_key(|package| package.name());
dependents
} else {
Vec::new()
},
})
.filter(|requirement| !requirement.marker.is_false())
.collect::<Vec<_>>();
Expand Down Expand Up @@ -496,7 +513,12 @@ fn conflict_marker_reachability<'lock>(
impl std::fmt::Display for RequirementsTxtExport<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
// Write out each package.
for Requirement { package, marker } in &self.nodes {
for Requirement {
package,
marker,
dependents,
} in &self.nodes
{
match &package.id.source {
Source::Registry(_) => {
let version = package
Expand Down Expand Up @@ -586,6 +608,20 @@ impl std::fmt::Display for RequirementsTxtExport<'_> {
}

writeln!(f)?;

// Add "via ..." comments for all dependents.
match dependents.as_slice() {
[] => {}
[dependent] => {
writeln!(f, "{}", format!(" # via {}", dependent.id.name).green())?;
}
_ => {
writeln!(f, "{}", " # via".green())?;
for &dependent in dependents {
writeln!(f, "{}", format!(" # {}", dependent.id.name).green())?;
}
}
}
}

Ok(())
Expand Down Expand Up @@ -637,6 +673,7 @@ impl Reachable<MarkerTree> for Edge<'_> {
struct Requirement<'lock> {
package: &'lock Package,
marker: MarkerTree,
dependents: Vec<&'lock Package>,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
Expand Down
2 changes: 2 additions & 0 deletions crates/uv/src/commands/project/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub(crate) async fn export(
editable: EditableMode,
locked: bool,
frozen: bool,
include_annotations: bool,
include_header: bool,
script: Option<Pep723Script>,
python: Option<String>,
Expand Down Expand Up @@ -259,6 +260,7 @@ pub(crate) async fn export(
&prune,
&extras,
&dev,
include_annotations,
editable,
hashes,
&install_options,
Expand Down
1 change: 1 addition & 0 deletions crates/uv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1916,6 +1916,7 @@ async fn run_project(
args.editable,
args.locked,
args.frozen,
args.include_annotations,
args.include_header,
script,
args.python,
Expand Down
4 changes: 4 additions & 0 deletions crates/uv/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1523,6 +1523,7 @@ pub(crate) struct ExportSettings {
pub(crate) output_file: Option<PathBuf>,
pub(crate) locked: bool,
pub(crate) frozen: bool,
pub(crate) include_annotations: bool,
pub(crate) include_header: bool,
pub(crate) script: Option<PathBuf>,
pub(crate) python: Option<String>,
Expand Down Expand Up @@ -1552,6 +1553,8 @@ impl ExportSettings {
no_default_groups,
only_group,
all_groups,
annotate,
no_annotate,
header,
no_header,
no_editable,
Expand Down Expand Up @@ -1604,6 +1607,7 @@ impl ExportSettings {
output_file,
locked,
frozen,
include_annotations: flag(annotate, no_annotate).unwrap_or(true),
include_header: flag(header, no_header).unwrap_or(true),
script,
python: python.and_then(Maybe::into_option),
Expand Down
Loading
Loading