Skip to content

Commit

Permalink
Stripped down unix path
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra authored and Greg Soltis committed May 1, 2023
1 parent d4cb88f commit cc52193
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 145 deletions.
9 changes: 8 additions & 1 deletion crates/turbopath/src/absolute_system_path_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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()
}
Expand Down
4 changes: 2 additions & 2 deletions crates/turbopath/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
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};

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
Expand Down
4 changes: 4 additions & 0 deletions crates/turbopath/src/relative_system_path_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ impl RelativeSystemPathBuf {
Ok(RelativeSystemPathBuf(system_path))
}

pub fn new_unchecked(unchecked_path: impl Into<PathBuf>) -> Self {
Self(unchecked_path.into())
}

pub fn as_path(&self) -> &Path {
&self.0
}
Expand Down
21 changes: 21 additions & 0 deletions crates/turbopath/src/relative_unix_path.rs
Original file line number Diff line number Diff line change
@@ -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))
}
}
130 changes: 0 additions & 130 deletions crates/turbopath/src/relative_unix_path_buf.rs

This file was deleted.

18 changes: 6 additions & 12 deletions crates/turborepo-scm/src/git.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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()
Expand All @@ -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<AnchoredSystemPathBuf, Error> {
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)
}
Expand Down

0 comments on commit cc52193

Please sign in to comment.