2022-11-26 23:57:50 +00:00
|
|
|
// Copyright 2020 The Jujutsu Authors
|
2020-12-12 08:00:42 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2023-07-10 15:17:00 +00:00
|
|
|
#![allow(missing_docs)]
|
|
|
|
|
2023-11-25 09:22:09 +00:00
|
|
|
use std::borrow::Borrow;
|
2020-12-12 08:00:42 +00:00
|
|
|
use std::fmt::{Debug, Error, Formatter};
|
2023-11-25 09:22:09 +00:00
|
|
|
use std::ops::Deref;
|
2022-10-21 04:50:03 +00:00
|
|
|
use std::path::{Component, Path, PathBuf};
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2021-06-09 20:57:48 +00:00
|
|
|
use itertools::Itertools;
|
2023-11-25 09:22:09 +00:00
|
|
|
use ref_cast::{ref_cast_custom, RefCastCustom};
|
2022-10-23 04:39:38 +00:00
|
|
|
use thiserror::Error;
|
2021-06-09 20:57:48 +00:00
|
|
|
|
2022-10-21 04:50:03 +00:00
|
|
|
use crate::file_util;
|
|
|
|
|
2022-11-12 19:19:03 +00:00
|
|
|
content_hash! {
|
2023-11-25 09:22:09 +00:00
|
|
|
/// Owned `RepoPath` component.
|
2022-11-12 19:19:03 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
|
2023-11-25 09:22:09 +00:00
|
|
|
pub struct RepoPathComponentBuf {
|
|
|
|
// Don't add more fields. Eq, Hash, and Ord must be compatible with the
|
|
|
|
// borrowed RepoPathComponent type.
|
2022-11-12 19:19:03 +00:00
|
|
|
value: String,
|
|
|
|
}
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2023-11-25 09:22:09 +00:00
|
|
|
/// Borrowed `RepoPath` component.
|
|
|
|
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Hash, RefCastCustom)]
|
|
|
|
#[repr(transparent)]
|
|
|
|
pub struct RepoPathComponent {
|
|
|
|
value: str,
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
impl RepoPathComponent {
|
2023-11-25 09:22:09 +00:00
|
|
|
/// Wraps `value` as `RepoPathComponent`.
|
|
|
|
///
|
|
|
|
/// The input `value` must not be empty and not contain path separator.
|
|
|
|
pub fn new(value: &str) -> &Self {
|
|
|
|
assert!(is_valid_repo_path_component_str(value));
|
|
|
|
Self::new_unchecked(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[ref_cast_custom]
|
|
|
|
const fn new_unchecked(value: &str) -> &Self;
|
|
|
|
|
2021-06-06 05:48:09 +00:00
|
|
|
pub fn as_str(&self) -> &str {
|
2020-12-12 08:00:42 +00:00
|
|
|
&self.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-25 08:46:17 +00:00
|
|
|
impl From<&str> for RepoPathComponentBuf {
|
2020-12-12 08:00:42 +00:00
|
|
|
fn from(value: &str) -> Self {
|
2023-11-25 08:46:17 +00:00
|
|
|
RepoPathComponentBuf::from(value.to_owned())
|
2022-12-21 19:14:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-25 08:46:17 +00:00
|
|
|
impl From<String> for RepoPathComponentBuf {
|
2022-12-21 19:14:46 +00:00
|
|
|
fn from(value: String) -> Self {
|
2023-11-25 22:34:54 +00:00
|
|
|
assert!(is_valid_repo_path_component_str(&value));
|
2023-11-25 08:46:17 +00:00
|
|
|
RepoPathComponentBuf { value }
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-25 09:22:09 +00:00
|
|
|
impl AsRef<RepoPathComponent> for RepoPathComponent {
|
|
|
|
fn as_ref(&self) -> &RepoPathComponent {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<RepoPathComponent> for RepoPathComponentBuf {
|
|
|
|
fn as_ref(&self) -> &RepoPathComponent {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Borrow<RepoPathComponent> for RepoPathComponentBuf {
|
|
|
|
fn borrow(&self) -> &RepoPathComponent {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for RepoPathComponentBuf {
|
|
|
|
type Target = RepoPathComponent;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
RepoPathComponent::new_unchecked(&self.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToOwned for RepoPathComponent {
|
|
|
|
type Owned = RepoPathComponentBuf;
|
|
|
|
|
|
|
|
fn to_owned(&self) -> Self::Owned {
|
|
|
|
let value = self.value.to_owned();
|
|
|
|
RepoPathComponentBuf { value }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn clone_into(&self, target: &mut Self::Owned) {
|
|
|
|
self.value.clone_into(&mut target.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
pub struct RepoPath {
|
2023-11-25 08:46:17 +00:00
|
|
|
components: Vec<RepoPathComponentBuf>,
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Debug for RepoPath {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
|
2021-05-17 06:18:33 +00:00
|
|
|
f.write_fmt(format_args!("{:?}", &self.to_internal_file_string()))
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RepoPath {
|
|
|
|
pub fn root() -> Self {
|
2021-05-17 06:40:39 +00:00
|
|
|
RepoPath { components: vec![] }
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2023-11-25 22:34:54 +00:00
|
|
|
/// Creates `RepoPath` from valid string representation.
|
|
|
|
///
|
|
|
|
/// The input `value` must not contain empty path components. For example,
|
|
|
|
/// `"/"`, `"/foo"`, `"foo/"`, `"foo//bar"` are all invalid.
|
2021-05-17 05:47:31 +00:00
|
|
|
pub fn from_internal_string(value: &str) -> Self {
|
2023-11-25 22:34:54 +00:00
|
|
|
assert!(is_valid_repo_path_str(value));
|
2021-05-17 06:40:39 +00:00
|
|
|
if value.is_empty() {
|
|
|
|
RepoPath::root()
|
|
|
|
} else {
|
|
|
|
let components = value
|
|
|
|
.split('/')
|
2023-11-25 08:46:17 +00:00
|
|
|
.map(|value| RepoPathComponentBuf {
|
2021-05-17 06:40:39 +00:00
|
|
|
value: value.to_string(),
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
RepoPath { components }
|
2021-05-17 05:47:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-18 06:15:07 +00:00
|
|
|
/// Converts repo-relative `Path` to `RepoPath`.
|
|
|
|
///
|
|
|
|
/// The input path should not contain `.` or `..`.
|
|
|
|
pub fn from_relative_path(relative_path: impl AsRef<Path>) -> Option<Self> {
|
|
|
|
let relative_path = relative_path.as_ref();
|
|
|
|
let components = relative_path
|
|
|
|
.components()
|
|
|
|
.map(|c| match c {
|
2023-11-25 08:46:17 +00:00
|
|
|
Component::Normal(a) => Some(RepoPathComponentBuf::from(a.to_str().unwrap())),
|
2023-11-18 06:15:07 +00:00
|
|
|
// TODO: better to return Err instead of None?
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.collect::<Option<_>>()?;
|
|
|
|
Some(RepoPath::from_components(components))
|
|
|
|
}
|
|
|
|
|
2023-11-25 08:46:17 +00:00
|
|
|
pub fn from_components(components: Vec<RepoPathComponentBuf>) -> Self {
|
2021-05-17 04:21:45 +00:00
|
|
|
RepoPath { components }
|
|
|
|
}
|
|
|
|
|
2022-10-21 04:50:03 +00:00
|
|
|
/// Parses an `input` path into a `RepoPath` relative to `base`.
|
|
|
|
///
|
|
|
|
/// The `cwd` and `base` paths are supposed to be absolute and normalized in
|
|
|
|
/// the same manner. The `input` path may be either relative to `cwd` or
|
|
|
|
/// absolute.
|
2023-07-04 22:25:00 +00:00
|
|
|
pub fn parse_fs_path(
|
|
|
|
cwd: &Path,
|
|
|
|
base: &Path,
|
|
|
|
input: impl AsRef<Path>,
|
|
|
|
) -> Result<Self, FsPathParseError> {
|
|
|
|
let input = input.as_ref();
|
2022-10-21 04:50:03 +00:00
|
|
|
let abs_input_path = file_util::normalize_path(&cwd.join(input));
|
|
|
|
let repo_relative_path = file_util::relative_path(base, &abs_input_path);
|
|
|
|
if repo_relative_path == Path::new(".") {
|
|
|
|
return Ok(RepoPath::root());
|
|
|
|
}
|
2023-11-18 06:15:07 +00:00
|
|
|
Self::from_relative_path(repo_relative_path)
|
|
|
|
.ok_or_else(|| FsPathParseError::InputNotInRepo(input.to_owned()))
|
2022-10-21 04:50:03 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 16:16:23 +00:00
|
|
|
/// The full string form used internally, not for presenting to users (where
|
|
|
|
/// we may want to use the platform's separator). This format includes a
|
|
|
|
/// trailing slash, unless this path represents the root directory. That
|
|
|
|
/// way it can be concatenated with a basename and produce a valid path.
|
|
|
|
pub fn to_internal_dir_string(&self) -> String {
|
|
|
|
let mut result = String::new();
|
|
|
|
for component in &self.components {
|
2021-06-06 05:48:09 +00:00
|
|
|
result.push_str(component.as_str());
|
2021-05-19 16:16:23 +00:00
|
|
|
result.push('/');
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
/// The full string form used internally, not for presenting to users (where
|
|
|
|
/// we may want to use the platform's separator).
|
2021-05-17 06:18:33 +00:00
|
|
|
pub fn to_internal_file_string(&self) -> String {
|
2021-06-09 20:57:48 +00:00
|
|
|
let strings = self
|
2021-05-17 06:40:39 +00:00
|
|
|
.components
|
|
|
|
.iter()
|
|
|
|
.map(|component| component.value.clone())
|
2021-06-09 20:57:48 +00:00
|
|
|
.collect_vec();
|
2021-05-17 06:40:39 +00:00
|
|
|
strings.join("/")
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2021-05-16 18:06:44 +00:00
|
|
|
pub fn to_fs_path(&self, base: &Path) -> PathBuf {
|
2023-09-09 01:19:47 +00:00
|
|
|
let repo_path_len: usize = self.components.iter().map(|x| x.as_str().len() + 1).sum();
|
|
|
|
let mut result = PathBuf::with_capacity(base.as_os_str().len() + repo_path_len);
|
|
|
|
result.push(base);
|
|
|
|
result.extend(self.components.iter().map(|dir| &dir.value));
|
2021-05-17 06:40:39 +00:00
|
|
|
result
|
2021-05-16 18:06:44 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
pub fn is_root(&self) -> bool {
|
2021-05-17 06:40:39 +00:00
|
|
|
self.components.is_empty()
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 16:16:23 +00:00
|
|
|
pub fn contains(&self, other: &RepoPath) -> bool {
|
|
|
|
other.components.starts_with(&self.components)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parent(&self) -> Option<RepoPath> {
|
|
|
|
if self.is_root() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(RepoPath {
|
|
|
|
components: self.components[0..self.components.len() - 1].to_vec(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 16:41:25 +00:00
|
|
|
pub fn split(&self) -> Option<(RepoPath, &RepoPathComponent)> {
|
2020-12-12 08:00:42 +00:00
|
|
|
if self.is_root() {
|
|
|
|
None
|
|
|
|
} else {
|
2021-05-19 16:41:25 +00:00
|
|
|
Some((self.parent().unwrap(), self.components.last().unwrap()))
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-25 08:46:17 +00:00
|
|
|
pub fn components(&self) -> &Vec<RepoPathComponentBuf> {
|
2021-05-19 16:41:25 +00:00
|
|
|
&self.components
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
2021-05-19 16:16:23 +00:00
|
|
|
|
2023-11-25 08:38:27 +00:00
|
|
|
pub fn join(&self, entry: &RepoPathComponent) -> RepoPath {
|
2023-11-25 09:22:09 +00:00
|
|
|
let components =
|
|
|
|
itertools::chain(self.components.iter().cloned(), [entry.to_owned()]).collect();
|
2021-05-19 16:16:23 +00:00
|
|
|
RepoPath { components }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-23 04:39:38 +00:00
|
|
|
#[derive(Clone, Debug, Eq, Error, PartialEq)]
|
2022-10-21 04:50:03 +00:00
|
|
|
pub enum FsPathParseError {
|
2023-07-04 22:25:00 +00:00
|
|
|
#[error(r#"Path "{}" is not in the repo"#, .0.display())]
|
|
|
|
InputNotInRepo(PathBuf),
|
2022-10-21 04:50:03 +00:00
|
|
|
}
|
|
|
|
|
2023-11-25 22:34:54 +00:00
|
|
|
fn is_valid_repo_path_component_str(value: &str) -> bool {
|
|
|
|
!value.is_empty() && !value.contains('/')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_valid_repo_path_str(value: &str) -> bool {
|
|
|
|
!value.starts_with('/') && !value.ends_with('/') && !value.contains("//")
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2023-11-25 22:34:54 +00:00
|
|
|
use std::panic;
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
use super::*;
|
|
|
|
|
2023-11-26 01:27:37 +00:00
|
|
|
fn repo_path(value: &str) -> RepoPath {
|
|
|
|
RepoPath::from_internal_string(value)
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
#[test]
|
2021-05-15 16:32:35 +00:00
|
|
|
fn test_is_root() {
|
2021-04-28 15:57:30 +00:00
|
|
|
assert!(RepoPath::root().is_root());
|
2023-11-26 01:27:37 +00:00
|
|
|
assert!(repo_path("").is_root());
|
|
|
|
assert!(!repo_path("foo").is_root());
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-11-25 22:34:54 +00:00
|
|
|
fn test_from_internal_string() {
|
|
|
|
assert_eq!(repo_path(""), RepoPath::root());
|
|
|
|
assert!(panic::catch_unwind(|| repo_path("/")).is_err());
|
|
|
|
assert!(panic::catch_unwind(|| repo_path("/x")).is_err());
|
|
|
|
assert!(panic::catch_unwind(|| repo_path("x/")).is_err());
|
|
|
|
assert!(panic::catch_unwind(|| repo_path("x//y")).is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-05-17 05:47:31 +00:00
|
|
|
fn test_to_internal_string() {
|
2021-05-17 06:18:33 +00:00
|
|
|
assert_eq!(RepoPath::root().to_internal_file_string(), "");
|
2023-11-26 01:27:37 +00:00
|
|
|
assert_eq!(repo_path("dir").to_internal_file_string(), "dir");
|
|
|
|
assert_eq!(repo_path("dir/file").to_internal_file_string(), "dir/file");
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-05-15 16:32:35 +00:00
|
|
|
fn test_order() {
|
2023-11-26 01:27:37 +00:00
|
|
|
assert!(RepoPath::root() < repo_path("dir"));
|
|
|
|
assert!(repo_path("dir") < repo_path("dirx"));
|
2020-12-12 08:00:42 +00:00
|
|
|
// '#' < '/'
|
2023-11-26 01:27:37 +00:00
|
|
|
assert!(repo_path("dir") < repo_path("dir#"));
|
|
|
|
assert!(repo_path("dir") < repo_path("dir/sub"));
|
|
|
|
|
|
|
|
assert!(repo_path("abc") < repo_path("dir/file"));
|
|
|
|
assert!(repo_path("dir") < repo_path("dir/file"));
|
|
|
|
assert!(repo_path("dis") > repo_path("dir/file"));
|
|
|
|
assert!(repo_path("xyz") > repo_path("dir/file"));
|
|
|
|
assert!(repo_path("dir1/xyz") < repo_path("dir2/abc"));
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-05-15 16:32:35 +00:00
|
|
|
fn test_join() {
|
2021-05-19 16:41:25 +00:00
|
|
|
let root = RepoPath::root();
|
2023-11-25 09:22:09 +00:00
|
|
|
let dir = root.join(RepoPathComponent::new("dir"));
|
2023-11-26 01:27:37 +00:00
|
|
|
assert_eq!(dir, repo_path("dir"));
|
2023-11-25 09:22:09 +00:00
|
|
|
let subdir = dir.join(RepoPathComponent::new("subdir"));
|
2023-11-26 01:27:37 +00:00
|
|
|
assert_eq!(subdir, repo_path("dir/subdir"));
|
2020-12-12 08:00:42 +00:00
|
|
|
assert_eq!(
|
2023-11-25 09:22:09 +00:00
|
|
|
subdir.join(RepoPathComponent::new("file")),
|
2023-11-26 01:27:37 +00:00
|
|
|
repo_path("dir/subdir/file")
|
2020-12-12 08:00:42 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-05-15 16:32:35 +00:00
|
|
|
fn test_parent() {
|
2021-05-19 16:41:25 +00:00
|
|
|
let root = RepoPath::root();
|
2023-11-25 09:22:09 +00:00
|
|
|
let dir_component = RepoPathComponent::new("dir");
|
|
|
|
let subdir_component = RepoPathComponent::new("subdir");
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2023-11-25 08:46:17 +00:00
|
|
|
let dir = root.join(dir_component);
|
|
|
|
let subdir = dir.join(subdir_component);
|
2020-12-12 08:00:42 +00:00
|
|
|
|
|
|
|
assert_eq!(root.parent(), None);
|
|
|
|
assert_eq!(dir.parent(), Some(root));
|
|
|
|
assert_eq!(subdir.parent(), Some(dir));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-05-19 16:41:25 +00:00
|
|
|
fn test_split() {
|
|
|
|
let root = RepoPath::root();
|
2023-11-25 09:22:09 +00:00
|
|
|
let dir_component = RepoPathComponent::new("dir");
|
|
|
|
let file_component = RepoPathComponent::new("file");
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2023-11-25 08:46:17 +00:00
|
|
|
let dir = root.join(dir_component);
|
|
|
|
let file = dir.join(file_component);
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2021-05-19 16:41:25 +00:00
|
|
|
assert_eq!(root.split(), None);
|
2023-11-25 08:46:17 +00:00
|
|
|
assert_eq!(dir.split(), Some((root, dir_component)));
|
|
|
|
assert_eq!(file.split(), Some((dir, file_component)));
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-05-15 16:32:35 +00:00
|
|
|
fn test_components() {
|
2021-05-19 16:41:25 +00:00
|
|
|
assert_eq!(RepoPath::root().components(), &vec![]);
|
2020-12-12 08:00:42 +00:00
|
|
|
assert_eq!(
|
2023-11-26 01:27:37 +00:00
|
|
|
repo_path("dir").components(),
|
2023-11-25 08:46:17 +00:00
|
|
|
&vec![RepoPathComponentBuf::from("dir")]
|
2020-12-12 08:00:42 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-11-26 01:27:37 +00:00
|
|
|
repo_path("dir/subdir").components(),
|
2020-12-12 08:00:42 +00:00
|
|
|
&vec![
|
2023-11-25 08:46:17 +00:00
|
|
|
RepoPathComponentBuf::from("dir"),
|
|
|
|
RepoPathComponentBuf::from("subdir")
|
2020-12-12 08:00:42 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-05-15 16:32:35 +00:00
|
|
|
fn test_to_fs_path() {
|
|
|
|
assert_eq!(
|
2023-11-26 01:27:37 +00:00
|
|
|
repo_path("").to_fs_path(Path::new("base/dir")),
|
2021-05-17 04:55:51 +00:00
|
|
|
Path::new("base/dir")
|
|
|
|
);
|
2023-11-26 01:27:37 +00:00
|
|
|
assert_eq!(repo_path("").to_fs_path(Path::new("")), Path::new(""));
|
2021-05-17 04:55:51 +00:00
|
|
|
assert_eq!(
|
2023-11-26 01:27:37 +00:00
|
|
|
repo_path("file").to_fs_path(Path::new("base/dir")),
|
2021-05-15 16:32:35 +00:00
|
|
|
Path::new("base/dir/file")
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-11-26 01:27:37 +00:00
|
|
|
repo_path("some/deep/dir/file").to_fs_path(Path::new("base/dir")),
|
2021-05-15 16:32:35 +00:00
|
|
|
Path::new("base/dir/some/deep/dir/file")
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-11-26 01:27:37 +00:00
|
|
|
repo_path("dir/file").to_fs_path(Path::new("")),
|
2021-05-15 16:32:35 +00:00
|
|
|
Path::new("dir/file")
|
|
|
|
);
|
|
|
|
}
|
2022-10-21 04:50:03 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_fs_path_wc_in_cwd() {
|
|
|
|
let temp_dir = testutils::new_temp_dir();
|
|
|
|
let cwd_path = temp_dir.path().join("repo");
|
2023-03-31 23:43:10 +00:00
|
|
|
let wc_path = &cwd_path;
|
2022-10-21 04:50:03 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2023-03-31 23:43:10 +00:00
|
|
|
RepoPath::parse_fs_path(&cwd_path, wc_path, ""),
|
2022-10-21 04:50:03 +00:00
|
|
|
Ok(RepoPath::root())
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-03-31 23:43:10 +00:00
|
|
|
RepoPath::parse_fs_path(&cwd_path, wc_path, "."),
|
2022-10-21 04:50:03 +00:00
|
|
|
Ok(RepoPath::root())
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-03-31 23:43:10 +00:00
|
|
|
RepoPath::parse_fs_path(&cwd_path, wc_path, "file"),
|
2023-11-26 01:27:37 +00:00
|
|
|
Ok(repo_path("file"))
|
2022-10-21 04:50:03 +00:00
|
|
|
);
|
|
|
|
// Both slash and the platform's separator are allowed
|
|
|
|
assert_eq!(
|
|
|
|
RepoPath::parse_fs_path(
|
|
|
|
&cwd_path,
|
2023-03-31 23:43:10 +00:00
|
|
|
wc_path,
|
2023-07-04 22:25:00 +00:00
|
|
|
format!("dir{}file", std::path::MAIN_SEPARATOR)
|
2022-10-21 04:50:03 +00:00
|
|
|
),
|
2023-11-26 01:27:37 +00:00
|
|
|
Ok(repo_path("dir/file"))
|
2022-10-21 04:50:03 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-03-31 23:43:10 +00:00
|
|
|
RepoPath::parse_fs_path(&cwd_path, wc_path, "dir/file"),
|
2023-11-26 01:27:37 +00:00
|
|
|
Ok(repo_path("dir/file"))
|
2022-10-21 04:50:03 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-03-31 23:43:10 +00:00
|
|
|
RepoPath::parse_fs_path(&cwd_path, wc_path, ".."),
|
2023-07-04 22:25:00 +00:00
|
|
|
Err(FsPathParseError::InputNotInRepo("..".into()))
|
2022-10-21 04:50:03 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
RepoPath::parse_fs_path(&cwd_path, &cwd_path, "../repo"),
|
|
|
|
Ok(RepoPath::root())
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
RepoPath::parse_fs_path(&cwd_path, &cwd_path, "../repo/file"),
|
2023-11-26 01:27:37 +00:00
|
|
|
Ok(repo_path("file"))
|
2022-10-21 04:50:03 +00:00
|
|
|
);
|
|
|
|
// Input may be absolute path with ".."
|
|
|
|
assert_eq!(
|
|
|
|
RepoPath::parse_fs_path(
|
|
|
|
&cwd_path,
|
|
|
|
&cwd_path,
|
|
|
|
cwd_path.join("../repo").to_str().unwrap()
|
|
|
|
),
|
|
|
|
Ok(RepoPath::root())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_fs_path_wc_in_cwd_parent() {
|
|
|
|
let temp_dir = testutils::new_temp_dir();
|
|
|
|
let cwd_path = temp_dir.path().join("dir");
|
|
|
|
let wc_path = cwd_path.parent().unwrap().to_path_buf();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
RepoPath::parse_fs_path(&cwd_path, &wc_path, ""),
|
2023-11-26 01:27:37 +00:00
|
|
|
Ok(repo_path("dir"))
|
2022-10-21 04:50:03 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
RepoPath::parse_fs_path(&cwd_path, &wc_path, "."),
|
2023-11-26 01:27:37 +00:00
|
|
|
Ok(repo_path("dir"))
|
2022-10-21 04:50:03 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
RepoPath::parse_fs_path(&cwd_path, &wc_path, "file"),
|
2023-11-26 01:27:37 +00:00
|
|
|
Ok(repo_path("dir/file"))
|
2022-10-21 04:50:03 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
RepoPath::parse_fs_path(&cwd_path, &wc_path, "subdir/file"),
|
2023-11-26 01:27:37 +00:00
|
|
|
Ok(repo_path("dir/subdir/file"))
|
2022-10-21 04:50:03 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
RepoPath::parse_fs_path(&cwd_path, &wc_path, ".."),
|
|
|
|
Ok(RepoPath::root())
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
RepoPath::parse_fs_path(&cwd_path, &wc_path, "../.."),
|
2023-07-04 22:25:00 +00:00
|
|
|
Err(FsPathParseError::InputNotInRepo("../..".into()))
|
2022-10-21 04:50:03 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
RepoPath::parse_fs_path(&cwd_path, &wc_path, "../other-dir/file"),
|
2023-11-26 01:27:37 +00:00
|
|
|
Ok(repo_path("other-dir/file"))
|
2022-10-21 04:50:03 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_fs_path_wc_in_cwd_child() {
|
|
|
|
let temp_dir = testutils::new_temp_dir();
|
|
|
|
let cwd_path = temp_dir.path().join("cwd");
|
|
|
|
let wc_path = cwd_path.join("repo");
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
RepoPath::parse_fs_path(&cwd_path, &wc_path, ""),
|
2023-07-04 22:25:00 +00:00
|
|
|
Err(FsPathParseError::InputNotInRepo("".into()))
|
2022-10-21 04:50:03 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
RepoPath::parse_fs_path(&cwd_path, &wc_path, "not-repo"),
|
2023-07-04 22:25:00 +00:00
|
|
|
Err(FsPathParseError::InputNotInRepo("not-repo".into()))
|
2022-10-21 04:50:03 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
RepoPath::parse_fs_path(&cwd_path, &wc_path, "repo"),
|
|
|
|
Ok(RepoPath::root())
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
RepoPath::parse_fs_path(&cwd_path, &wc_path, "repo/file"),
|
2023-11-26 01:27:37 +00:00
|
|
|
Ok(repo_path("file"))
|
2022-10-21 04:50:03 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
RepoPath::parse_fs_path(&cwd_path, &wc_path, "repo/dir/file"),
|
2023-11-26 01:27:37 +00:00
|
|
|
Ok(repo_path("dir/file"))
|
2022-10-21 04:50:03 +00:00
|
|
|
);
|
|
|
|
}
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|