From cc52193b702078806e05d7b98374866d06f8f544 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Mon, 1 May 2023 18:55:17 +0200 Subject: [PATCH] Stripped down unix path --- .../turbopath/src/absolute_system_path_buf.rs | 9 +- crates/turbopath/src/lib.rs | 4 +- .../turbopath/src/relative_system_path_buf.rs | 4 + crates/turbopath/src/relative_unix_path.rs | 21 +++ .../turbopath/src/relative_unix_path_buf.rs | 130 ------------------ crates/turborepo-scm/src/git.rs | 18 +-- 6 files changed, 41 insertions(+), 145 deletions(-) create mode 100644 crates/turbopath/src/relative_unix_path.rs delete mode 100644 crates/turbopath/src/relative_unix_path_buf.rs diff --git a/crates/turbopath/src/absolute_system_path_buf.rs b/crates/turbopath/src/absolute_system_path_buf.rs index b695a47bbb942..1cb2a4b3b5fec 100644 --- a/crates/turbopath/src/absolute_system_path_buf.rs +++ b/crates/turbopath/src/absolute_system_path_buf.rs @@ -7,7 +7,9 @@ use std::{ use serde::Serialize; -use crate::{AnchoredSystemPathBuf, IntoSystem, PathValidationError, RelativeSystemPathBuf}; +use crate::{ + AnchoredSystemPathBuf, IntoSystem, PathValidationError, RelativeSystemPathBuf, RelativeUnixPath, +}; #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize)] pub struct AbsoluteSystemPathBuf(PathBuf); @@ -117,6 +119,11 @@ impl AbsoluteSystemPathBuf { AbsoluteSystemPathBuf(self.0.join(path.as_path())) } + pub fn join_unix_path(&self, unix_path: &RelativeUnixPath) -> AbsoluteSystemPathBuf { + let tail = unix_path.to_system_path(); + AbsoluteSystemPathBuf(self.0.join(tail.as_path())) + } + pub fn as_path(&self) -> &Path { self.0.as_path() } diff --git a/crates/turbopath/src/lib.rs b/crates/turbopath/src/lib.rs index fa0b3c902ce3b..3a08472d74f9e 100644 --- a/crates/turbopath/src/lib.rs +++ b/crates/turbopath/src/lib.rs @@ -3,7 +3,7 @@ mod absolute_system_path_buf; mod anchored_system_path_buf; mod relative_system_path_buf; -mod relative_unix_path_buf; +mod relative_unix_path; use std::path::{Path, PathBuf}; @@ -11,7 +11,7 @@ pub use absolute_system_path_buf::AbsoluteSystemPathBuf; pub use anchored_system_path_buf::AnchoredSystemPathBuf; use path_slash::{PathBufExt, PathExt}; pub use relative_system_path_buf::RelativeSystemPathBuf; -pub use relative_unix_path_buf::RelativeUnixPathBuf; +pub use relative_unix_path::RelativeUnixPath; use thiserror::Error; // Custom error type for path validation errors diff --git a/crates/turbopath/src/relative_system_path_buf.rs b/crates/turbopath/src/relative_system_path_buf.rs index 663c18692815c..5d205dfa12342 100644 --- a/crates/turbopath/src/relative_system_path_buf.rs +++ b/crates/turbopath/src/relative_system_path_buf.rs @@ -43,6 +43,10 @@ impl RelativeSystemPathBuf { Ok(RelativeSystemPathBuf(system_path)) } + pub fn new_unchecked(unchecked_path: impl Into) -> Self { + Self(unchecked_path.into()) + } + pub fn as_path(&self) -> &Path { &self.0 } diff --git a/crates/turbopath/src/relative_unix_path.rs b/crates/turbopath/src/relative_unix_path.rs new file mode 100644 index 0000000000000..fa5d0ad5bc3c3 --- /dev/null +++ b/crates/turbopath/src/relative_unix_path.rs @@ -0,0 +1,21 @@ +use std::path::PathBuf; + +use path_slash::PathBufExt; + +use crate::RelativeSystemPathBuf; + +pub struct RelativeUnixPath<'a> { + inner: &'a str, +} + +impl<'a> From<&'a str> for RelativeUnixPath<'a> { + fn from(value: &'a str) -> Self { + Self { inner: value } + } +} + +impl<'a> RelativeUnixPath<'a> { + pub fn to_system_path(&self) -> RelativeSystemPathBuf { + RelativeSystemPathBuf::new_unchecked(PathBuf::from_slash(self.inner)) + } +} diff --git a/crates/turbopath/src/relative_unix_path_buf.rs b/crates/turbopath/src/relative_unix_path_buf.rs deleted file mode 100644 index c20505dddd4e7..0000000000000 --- a/crates/turbopath/src/relative_unix_path_buf.rs +++ /dev/null @@ -1,130 +0,0 @@ -use std::{ - ffi::OsStr, - path::{Components, Path, PathBuf}, -}; - -use serde::Serialize; - -use crate::{IntoUnix, PathValidationError}; - -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize)] -pub struct RelativeUnixPathBuf(PathBuf); - -impl RelativeUnixPathBuf { - /// Create a new RelativeUnixPathBuf from a PathBuf by calling `into_unix()` - /// - /// NOTE: `into_unix` *only* converts Windows paths to Unix paths *on* a - /// Windows system. Do not pass a Windows path on a Unix system and - /// assume it'll be converted. - /// - /// # Arguments - /// - /// * `path`: - /// - /// returns: Result - /// - /// # Examples - /// - /// ``` - /// ``` - pub fn new(path: impl Into) -> Result { - let path = path.into(); - if path.is_absolute() { - return Err(PathValidationError::NotRelative(path)); - } - - Ok(RelativeUnixPathBuf(path.into_unix()?)) - } - - pub fn as_path(&self) -> &Path { - &self.0 - } - - pub fn components(&self) -> Components<'_> { - self.0.components() - } - - pub fn parent(&self) -> Option { - self.0 - .parent() - .map(|p| RelativeUnixPathBuf(p.to_path_buf())) - } - - pub fn starts_with>(&self, base: P) -> bool { - self.0.starts_with(base.as_ref()) - } - - pub fn ends_with>(&self, child: P) -> bool { - self.0.ends_with(child.as_ref()) - } - - pub fn join>(&self, path: P) -> RelativeUnixPathBuf { - RelativeUnixPathBuf(self.0.join(path)) - } - - pub fn to_str(&self) -> Result<&str, PathValidationError> { - self.0 - .to_str() - .ok_or_else(|| PathValidationError::InvalidUnicode(self.0.clone())) - } - - pub fn file_name(&self) -> Option<&OsStr> { - self.0.file_name() - } - - pub fn extension(&self) -> Option<&OsStr> { - self.0.extension() - } - - pub fn into_path_buf(self) -> PathBuf { - self.0 - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_relative_unix_path_buf() { - let path = RelativeUnixPathBuf::new(PathBuf::from("foo/bar")).unwrap(); - assert_eq!(path.as_path(), Path::new("foo/bar")); - assert_eq!(path.components().count(), 2); - assert_eq!(path.parent().unwrap().as_path(), Path::new("foo")); - assert!(path.starts_with("foo")); - assert!(path.ends_with("bar")); - assert_eq!(path.join("baz").as_path(), Path::new("foo/bar/baz")); - assert_eq!(path.to_str().unwrap(), "foo/bar"); - assert_eq!(path.file_name(), Some(OsStr::new("bar"))); - assert_eq!(path.extension(), None); - } - - #[test] - fn test_relative_unix_path_buf_with_extension() { - let path = RelativeUnixPathBuf::new(PathBuf::from("foo/bar.txt")).unwrap(); - assert_eq!(path.as_path(), Path::new("foo/bar.txt")); - assert_eq!(path.components().count(), 2); - assert_eq!(path.parent().unwrap().as_path(), Path::new("foo")); - assert!(path.starts_with("foo")); - assert!(path.ends_with("bar.txt")); - assert_eq!(path.join("baz").as_path(), Path::new("foo/bar.txt/baz")); - assert_eq!(path.to_str().unwrap(), "foo/bar.txt"); - assert_eq!(path.file_name(), Some(OsStr::new("bar.txt"))); - assert_eq!(path.extension(), Some(OsStr::new("txt"))); - } - - #[test] - fn test_relative_unix_path_buf_errors() { - #[cfg(not(windows))] - assert!(RelativeUnixPathBuf::new(PathBuf::from("/foo/bar")).is_err()); - #[cfg(windows)] - assert!(RelativeUnixPathBuf::new(PathBuf::from("C:\\foo\\bar")).is_err()); - } - - #[cfg(windows)] - #[test] - fn test_convert_from_windows_path() { - let path = RelativeUnixPathBuf::new(PathBuf::from("foo\\bar")).unwrap(); - assert_eq!(path.as_path(), Path::new("foo/bar")); - } -} diff --git a/crates/turborepo-scm/src/git.rs b/crates/turborepo-scm/src/git.rs index 7ad5648d24b5c..0b0778e1a2039 100644 --- a/crates/turborepo-scm/src/git.rs +++ b/crates/turborepo-scm/src/git.rs @@ -1,11 +1,6 @@ -use std::{ - backtrace::Backtrace, - collections::HashSet, - path::{Path, PathBuf}, - process::Command, -}; +use std::{backtrace::Backtrace, collections::HashSet, path::PathBuf, process::Command}; -use turbopath::{AbsoluteSystemPathBuf, AnchoredSystemPathBuf}; +use turbopath::{AbsoluteSystemPathBuf, AnchoredSystemPathBuf, RelativeUnixPath}; use crate::Error; @@ -102,9 +97,9 @@ fn add_files_from_stdout( ) { let stdout = String::from_utf8(stdout).unwrap(); for line in stdout.lines() { - let path = Path::new(line); + let path: RelativeUnixPath = line.into(); let anchored_to_turbo_root_file_path = - reanchor_path_from_git_root_to_turbo_root(git_root, turbo_root, path).unwrap(); + reanchor_path_from_git_root_to_turbo_root(git_root, turbo_root, &path).unwrap(); files.insert( anchored_to_turbo_root_file_path .to_str() @@ -117,10 +112,9 @@ fn add_files_from_stdout( fn reanchor_path_from_git_root_to_turbo_root( git_root: &AbsoluteSystemPathBuf, turbo_root: &AbsoluteSystemPathBuf, - path: &Path, + path: &RelativeUnixPath, ) -> Result { - let anchored_to_git_root_file_path: AnchoredSystemPathBuf = path.try_into()?; - let absolute_file_path = git_root.resolve(&anchored_to_git_root_file_path); + let absolute_file_path = git_root.join_unix_path(path); let anchored_to_turbo_root_file_path = turbo_root.anchor(&absolute_file_path)?; Ok(anchored_to_turbo_root_file_path) }