|
| 1 | +use std::borrow::Cow; |
1 | 2 | use std::collections::hash_map::Entry;
|
2 | 3 | use std::collections::VecDeque;
|
3 | 4 | use std::fmt::Formatter;
|
4 |
| -use std::path::{Path, PathBuf}; |
| 5 | +use std::path::{Component, Path, PathBuf}; |
5 | 6 |
|
6 | 7 | use either::Either;
|
7 | 8 | use petgraph::visit::IntoNodeReferences;
|
@@ -191,20 +192,14 @@ impl std::fmt::Display for RequirementsTxtExport<'_> {
|
191 | 192 | write!(f, "{} @ {}", package.id.name, url)?;
|
192 | 193 | }
|
193 | 194 | Source::Path(path) | Source::Directory(path) => {
|
194 |
| - if path.as_os_str().is_empty() { |
195 |
| - write!(f, ".")?; |
196 |
| - } else if path.is_absolute() { |
| 195 | + if path.is_absolute() { |
197 | 196 | write!(f, "{}", Url::from_file_path(path).unwrap())?;
|
198 | 197 | } else {
|
199 |
| - write!(f, "{}", path.portable_display())?; |
| 198 | + write!(f, "{}", anchor(path).portable_display())?; |
200 | 199 | }
|
201 | 200 | }
|
202 | 201 | Source::Editable(path) => {
|
203 |
| - if path.as_os_str().is_empty() { |
204 |
| - write!(f, "-e .")?; |
205 |
| - } else { |
206 |
| - write!(f, "-e {}", path.portable_display())?; |
207 |
| - } |
| 202 | + write!(f, "-e {}", anchor(path).portable_display())?; |
208 | 203 | }
|
209 | 204 | Source::Virtual(_) => {
|
210 | 205 | continue;
|
@@ -252,3 +247,14 @@ impl<'lock> From<&'lock Package> for NodeComparator<'lock> {
|
252 | 247 | }
|
253 | 248 | }
|
254 | 249 | }
|
| 250 | + |
| 251 | +/// Modify a relative [`Path`] to anchor it at the current working directory. |
| 252 | +/// |
| 253 | +/// For example, given `foo/bar`, returns `./foo/bar`. |
| 254 | +fn anchor(path: &Path) -> Cow<'_, Path> { |
| 255 | + match path.components().next() { |
| 256 | + None => Cow::Owned(PathBuf::from(".")), |
| 257 | + Some(Component::CurDir | Component::ParentDir) => Cow::Borrowed(path), |
| 258 | + _ => Cow::Owned(PathBuf::from("./").join(path)), |
| 259 | + } |
| 260 | +} |
0 commit comments