summaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/cwd.rs26
-rw-r--r--src/utils/mod.rs2
-rw-r--r--src/utils/path.rs109
-rw-r--r--src/utils/process.rs12
4 files changed, 122 insertions, 27 deletions
diff --git a/src/utils/cwd.rs b/src/utils/cwd.rs
deleted file mode 100644
index 78b47719..00000000
--- a/src/utils/cwd.rs
+++ /dev/null
@@ -1,26 +0,0 @@
-use std::path::PathBuf;
-
-/// Return current working directory of the user's shell process. I.e. the directory which they are
-/// in when delta exits. This is the directory relative to which the file paths in delta output are
-/// constructed if they are using either (a) delta's relative-paths option or (b) git's --relative
-/// flag.
-pub fn cwd_of_user_shell_process(
- cwd_of_delta_process: Option<&PathBuf>,
- cwd_relative_to_repo_root: Option<&str>,
-) -> Option<PathBuf> {
- match (cwd_of_delta_process, cwd_relative_to_repo_root) {
- (Some(cwd), None) => {
- // We are not a child process of git
- Some(PathBuf::from(cwd))
- }
- (Some(repo_root), Some(cwd_relative_to_repo_root)) => {
- // We are a child process of git; git spawned us from repo_root and preserved the user's
- // original cwd in the GIT_PREFIX env var (available as config.cwd_relative_to_repo_root)
- Some(PathBuf::from(repo_root).join(cwd_relative_to_repo_root))
- }
- (None, _) => {
- // Unexpected
- None
- }
- }
-}
diff --git a/src/utils/mod.rs b/src/utils/mod.rs
index 63089531..20adef24 100644
--- a/src/utils/mod.rs
+++ b/src/utils/mod.rs
@@ -1,6 +1,6 @@
#[cfg(not(tarpaulin_include))]
pub mod bat;
-pub mod cwd;
+pub mod path;
pub mod process;
pub mod regex_replacement;
pub mod syntect;
diff --git a/src/utils/path.rs b/src/utils/path.rs
new file mode 100644
index 00000000..6891e81a
--- /dev/null
+++ b/src/utils/path.rs
@@ -0,0 +1,109 @@
+use std::path::{Component, Path, PathBuf};
+
+use crate::config::Config;
+
+use super::process::calling_process;
+
+// Infer absolute path to `relative_path`.
+pub fn absolute_path(relative_path: &str, config: &Config) -> Option<PathBuf> {
+ match (
+ &config.cwd_of_delta_process,
+ &config.cwd_of_user_shell_process,
+ calling_process().paths_in_input_are_relative_to_cwd() || config.relative_paths,
+ ) {
+ // Note that if we were invoked by git then cwd_of_delta_process == repo_root
+ (Some(cwd_of_delta_process), _, false) => Some(cwd_of_delta_process.join(relative_path)),
+ (_, Some(cwd_of_user_shell_process), true) => {
+ Some(cwd_of_user_shell_process.join(relative_path))
+ }
+ (Some(cwd_of_delta_process), None, true) => {
+ // This might occur when piping from git to delta?
+ Some(cwd_of_delta_process.join(relative_path))
+ }
+ _ => None,
+ }
+ .map(normalize_path)
+}
+
+/// Relativize path if delta config demands that and paths are not already relativized by git.
+pub fn relativize_path_maybe(path: &str, config: &Config) -> Option<PathBuf> {
+ if config.relative_paths && !calling_process().paths_in_input_are_relative_to_cwd() {
+ if let Some(base) = config.cwd_relative_to_repo_root.as_deref() {
+ pathdiff::diff_paths(&path, base)
+ } else {
+ None
+ }
+ } else {
+ None
+ }
+}
+
+/// Return current working directory of the user's shell process. I.e. the directory which they are
+/// in when delta exits. This is the directory relative to which the file paths in delta output are
+/// constructed if they are using either (a) delta's relative-paths option or (b) git's --relative
+/// flag.
+pub fn cwd_of_user_shell_process(
+ cwd_of_delta_process: Option<&PathBuf>,
+ cwd_relative_to_repo_root: Option<&str>,
+) -> Option<PathBuf> {
+ match (cwd_of_delta_process, cwd_relative_to_repo_root) {
+ (Some(cwd), None) => {
+ // We are not a child process of git
+ Some(PathBuf::from(cwd))
+ }
+ (Some(repo_root), Some(cwd_relative_to_repo_root)) => {
+ // We are a child process of git; git spawned us from repo_root and preserved the user's
+ // original cwd in the GIT_PREFIX env var (available as config.cwd_relative_to_repo_root)
+ Some(PathBuf::from(repo_root).join(cwd_relative_to_repo_root))
+ }
+ (None, _) => {
+ // Unexpected
+ None
+ }
+ }
+}
+
+// Copied from
+// https://github.com/rust-lang/cargo/blob/c6745a3d7fcea3a949c3e13e682b8ddcbd213add/crates/cargo-util/src/paths.rs#L73-L106
+// as suggested by matklad: https://www.reddit.com/r/rust/comments/hkkquy/comment/fwtw53s/?utm_source=share&utm_medium=web2x&context=3
+fn normalize_path<P>(path: P) -> PathBuf
+where
+ P: AsRef<Path>,
+{
+ let mut components = path.as_ref().components().peekable();
+ let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
+ components.next();
+ PathBuf::from(c.as_os_str())
+ } else {
+ PathBuf::new()
+ };
+
+ for component in components {
+ match component {
+ Component::Prefix(..) => unreachable!(),
+ Component::RootDir => {
+ ret.push(component.as_os_str());
+ }
+ Component::CurDir => {}
+ Component::ParentDir => {
+ ret.pop();
+ }
+ Component::Normal(c) => {
+ ret.push(c);
+ }
+ }
+ }
+ ret
+}
+
+#[cfg(test)]
+pub fn fake_delta_cwd_for_tests() -> PathBuf {
+ #[cfg(not(target_os = "windows"))]
+ {
+ PathBuf::from("/fake/delta/cwd")
+ }
+ #[cfg(target_os = "windows")]
+ {
+ PathBuf::from(r"C:\fake\delta\cwd")
+ }
+}
diff --git a/src/utils/process.rs b/src/utils/process.rs
index 0e98024a..a2984090 100644
--- a/src/utils/process.rs
+++ b/src/utils/process.rs
@@ -20,6 +20,18 @@ pub enum CallingProcess {
}
// TODO: Git blame is currently handled differently
+impl CallingProcess {
+ pub fn paths_in_input_are_relative_to_cwd(&self) -> bool {
+ match self {
+ CallingProcess::GitDiff(cmd) if cmd.long_options.contains("--relative") => true,
+ CallingProcess::GitShow(cmd, _) if cmd.long_options.contains("--relative") => true,
+ CallingProcess::GitLog(cmd) if cmd.long_options.contains("--relative") => true,
+ CallingProcess::GitGrep(_) | CallingProcess::OtherGrep => true,
+ _ => false,
+ }
+ }
+}
+
#[derive(Clone, Debug, PartialEq)]
pub struct CommandLine {
pub long_options: HashSet<String>,