From 144adc83abfd4ae23b465e1bddd42f9d35ab9982 Mon Sep 17 00:00:00 2001 From: ariasuni Date: Tue, 11 May 2021 22:27:33 +0200 Subject: [PATCH] Reduce code around path reorienting and add another test --- devtools/dev-create-test-filesystem.sh | 3 ++- src/fs/feature/git.rs | 10 +++------- src/fs/file.rs | 20 +------------------- xtests/outputs/git3_long.ansitxt | 1 + 4 files changed, 7 insertions(+), 27 deletions(-) diff --git a/devtools/dev-create-test-filesystem.sh b/devtools/dev-create-test-filesystem.sh index 112b5698..61ff8fdf 100755 --- a/devtools/dev-create-test-filesystem.sh +++ b/devtools/dev-create-test-filesystem.sh @@ -326,8 +326,9 @@ mkdir -p "$TEST_ROOT/git3" cd "$TEST_ROOT/git3" git init >/dev/null -# Create a symbolic link pointing to a non-existing file +# Create symbolic links pointing to non-existing files ln -s aaa/aaa/a b +ln -s /aaa # This normally fails with: find "$TEST_ROOT/git3" -exec touch {} -h -t $FIXED_DATE \; diff --git a/src/fs/feature/git.rs b/src/fs/feature/git.rs index 40bc80db..1e8ff997 100644 --- a/src/fs/feature/git.rs +++ b/src/fs/feature/git.rs @@ -289,13 +289,9 @@ impl Git { /// you’d ask a repo about “./README.md” but it only knows about /// “/vagrant/README.md”, prefixed by the workdir. fn reorient(path: &Path) -> PathBuf { - use std::env::current_dir; - - // TODO: I’m not 100% on this func tbh - let path = match current_dir() { - Err(_) => Path::new(".").join(&path), - Ok(dir) => dir.join(&path), - }; + let path = std::env::current_dir() + .unwrap() + .with_file_name(&path); path.canonicalize().unwrap_or(path) } diff --git a/src/fs/file.rs b/src/fs/file.rs index ea83f08b..b5111b26 100644 --- a/src/fs/file.rs +++ b/src/fs/file.rs @@ -205,24 +205,6 @@ impl<'dir> File<'dir> { } - /// Re-prefixes the path pointed to by this file, if it’s a symlink, to - /// make it an absolute path that can be accessed from whichever - /// directory exa is being run from. - fn reorient_target_path(&self, path: &Path) -> PathBuf { - if path.is_absolute() { - path.to_path_buf() - } - else if let Some(dir) = self.parent_dir { - dir.join(&*path) - } - else if let Some(parent) = self.path.parent() { - parent.join(&*path) - } - else { - self.path.join(&*path) - } - } - /// Again assuming this file is a symlink, follows that link and returns /// the result of following it. /// @@ -245,7 +227,7 @@ impl<'dir> File<'dir> { Err(e) => return FileTarget::Err(e), }; - let absolute_path = self.reorient_target_path(&path); + let absolute_path = self.path.with_file_name(&path); // Use plain `metadata` instead of `symlink_metadata` - we *want* to // follow links. diff --git a/xtests/outputs/git3_long.ansitxt b/xtests/outputs/git3_long.ansitxt index 908b7daa..47b31c22 100644 --- a/xtests/outputs/git3_long.ansitxt +++ b/xtests/outputs/git3_long.ansitxt @@ -1 +1,2 @@ +lrwxrwxrwx 4 cassowary  1 Jan 12:34 -N aaa -> /aaa lrwxrwxrwx 9 cassowary  1 Jan 12:34 -N b -> aaa/aaa/a