| 1 | use std::path::{Component, Path, PathBuf}; |
| 2 | |
| 3 | use crate::config::Config; |
| 4 | |
| 5 | use super::process::calling_process; |
| 6 | |
| 7 | // Infer absolute path to `relative_path`. |
| 8 | pub fn absolute_path(relative_path: &str, config: &Config) -> Option<PathBuf> { |
| 9 | match ( |
| 10 | &config.cwd_of_delta_process, |
| 11 | &config.cwd_of_user_shell_process, |
| 12 | calling_process().paths_in_input_are_relative_to_cwd() || config.relative_paths, |
| 13 | ) { |
| 14 | // Note that if we were invoked by git then cwd_of_delta_process == repo_root |
| 15 | (Some(cwd_of_delta_process), _, false) => Some(cwd_of_delta_process.join(relative_path)), |
| 16 | (_, Some(cwd_of_user_shell_process), true) => { |
| 17 | Some(cwd_of_user_shell_process.join(relative_path)) |
| 18 | } |
| 19 | (Some(cwd_of_delta_process), None, true) => { |
| 20 | // This might occur when piping from git to delta? |
| 21 | Some(cwd_of_delta_process.join(relative_path)) |
| 22 | } |
| 23 | _ => None, |
| 24 | } |
| 25 | .map(normalize_path) |
| 26 | } |
| 27 | |
| 28 | #[allow(clippy::needless_borrows_for_generic_args)] // Lint has known problems, &path != path |
| 29 | /// Relativize `path` if delta `config` demands that and paths are not already relativized by git. |
| 30 | pub fn relativize_path_maybe(path: &mut String, config: &Config) { |
| 31 | let mut inner_relativize = || -> Option<()> { |
| 32 | let base = config.cwd_relative_to_repo_root.as_deref()?; |
| 33 | let relative_path = pathdiff::diff_paths(&path, base)?; |
| 34 | if relative_path.is_relative() { |
| 35 | #[cfg(target_os = "windows")] |
| 36 | // '/dev/null' is converted to '\dev\null' and considered relative. Work |
| 37 | // around that by leaving all paths like that untouched: |
| 38 | if relative_path.starts_with(Path::new(r"\")) { |
| 39 | return None; |
| 40 | } |
| 41 | *path = relative_path.to_string_lossy().into_owned(); |
| 42 | } |
| 43 | Some(()) |
| 44 | }; |
| 45 | if config.relative_paths && !calling_process().paths_in_input_are_relative_to_cwd() { |
| 46 | let _ = inner_relativize(); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /// Return current working directory of the user's shell process. I.e. the directory which they are |
| 51 | /// in when delta exits. This is the directory relative to which the file paths in delta output are |
| 52 | /// constructed if they are using either (a) delta's relative-paths option or (b) git's --relative |
| 53 | /// flag. |
| 54 | pub fn cwd_of_user_shell_process( |
| 55 | cwd_of_delta_process: Option<&PathBuf>, |
| 56 | cwd_relative_to_repo_root: Option<&str>, |
| 57 | ) -> Option<PathBuf> { |
| 58 | match (cwd_of_delta_process, cwd_relative_to_repo_root) { |
| 59 | (Some(cwd), None) => { |
| 60 | // We are not a child process of git |
| 61 | Some(PathBuf::from(cwd)) |
| 62 | } |
| 63 | (Some(repo_root), Some(cwd_relative_to_repo_root)) => { |
| 64 | // We are a child process of git; git spawned us from repo_root and preserved the user's |
| 65 | // original cwd in the GIT_PREFIX env var (available as config.cwd_relative_to_repo_root) |
| 66 | Some(PathBuf::from(repo_root).join(cwd_relative_to_repo_root)) |
| 67 | } |
| 68 | (None, _) => { |
| 69 | // Unexpected |
| 70 | None |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // Copied from |
| 76 | // https://github.com/rust-lang/cargo/blob/c6745a3d7fcea3a949c3e13e682b8ddcbd213add/crates/cargo-util/src/paths.rs#L73-L106 |
| 77 | // as suggested by matklad: https://www.reddit.com/r/rust/comments/hkkquy/comment/fwtw53s/?utm_source=share&utm_medium=web2x&context=3 |
| 78 | fn normalize_path<P>(path: P) -> PathBuf |
| 79 | where |
| 80 | P: AsRef<Path>, |
| 81 | { |
| 82 | let mut components = path.as_ref().components().peekable(); |
| 83 | let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() { |
| 84 | components.next(); |
| 85 | PathBuf::from(c.as_os_str()) |
| 86 | } else { |
| 87 | PathBuf::new() |
| 88 | }; |
| 89 | |
| 90 | for component in components { |
| 91 | match component { |
| 92 | Component::Prefix(..) => unreachable!(), |
| 93 | Component::RootDir => { |
| 94 | ret.push(component.as_os_str()); |
| 95 | } |
| 96 | Component::CurDir => {} |
| 97 | Component::ParentDir => { |
| 98 | ret.pop(); |
| 99 | } |
| 100 | Component::Normal(c) => { |
| 101 | ret.push(c); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | ret |
| 106 | } |
| 107 | |
| 108 | #[cfg(test)] |
| 109 | pub fn fake_delta_cwd_for_tests() -> PathBuf { |
| 110 | #[cfg(not(target_os = "windows"))] |
| 111 | { |
| 112 | PathBuf::from("/fake/delta/cwd") |
| 113 | } |
| 114 | #[cfg(target_os = "windows")] |
| 115 | { |
| 116 | PathBuf::from(r"C:\fake\delta\cwd") |
| 117 | } |
| 118 | } |