| 1 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::{Componentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Component#, Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#}; |
| 2 | |
| 3 | use craterust-analyzer cargo git-delta 0.19.2 crate/::configrust-analyzer cargo git-delta 0.19.2 config/::Configrust-analyzer cargo git-delta 0.19.2 config/Config#; |
| 4 | |
| 5 | use superrust-analyzer cargo git-delta 0.19.2 utils/::processrust-analyzer cargo git-delta 0.19.2 utils/process/::calling_processrust-analyzer cargo git-delta 0.19.2 utils/process/calling_process().; |
| 6 | |
| 7 | // Infer absolute path to `relative_path`. |
| 8 | pub fn absolute_pathrust-analyzer cargo git-delta 0.19.2 utils/path/absolute_path().(relative_path: &str, config: &Configrust-analyzer cargo git-delta 0.19.2 config/Config#) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#> { |
| 9 | match ( |
| 10 | &config.cwd_of_delta_processrust-analyzer cargo git-delta 0.19.2 config/Config#cwd_of_delta_process., |
| 11 | &config.cwd_of_user_shell_processrust-analyzer cargo git-delta 0.19.2 config/Config#cwd_of_user_shell_process., |
| 12 | calling_processrust-analyzer cargo git-delta 0.19.2 utils/process/calling_process().().paths_in_input_are_relative_to_cwdrust-analyzer cargo git-delta 0.19.2 utils/process/impl#[CallingProcess]paths_in_input_are_relative_to_cwd().() || config.relative_pathsrust-analyzer cargo git-delta 0.19.2 config/Config#relative_paths., |
| 13 | ) { |
| 14 | // Note that if we were invoked by git then cwd_of_delta_process == repo_root |
| 15 | (Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(cwd_of_delta_process), _, false) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(cwd_of_delta_process.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().(relative_path)), |
| 16 | (_, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(cwd_of_user_shell_process), true) => { |
| 17 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(cwd_of_user_shell_process.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().(relative_path)) |
| 18 | } |
| 19 | (Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(cwd_of_delta_process), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, true) => { |
| 20 | // This might occur when piping from git to delta? |
| 21 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(cwd_of_delta_process.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().(relative_path)) |
| 22 | } |
| 23 | _ => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 24 | } |
| 25 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(normalize_pathrust-analyzer cargo git-delta 0.19.2 utils/path/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_mayberust-analyzer cargo git-delta 0.19.2 utils/path/relativize_path_maybe().(path: &mut Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, config: &Configrust-analyzer cargo git-delta 0.19.2 config/Config#) { |
| 31 | let mut inner_relativize = || -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<()> { |
| 32 | let base = config.cwd_relative_to_repo_rootrust-analyzer cargo git-delta 0.19.2 config/Config#cwd_relative_to_repo_root..as_derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_deref().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 33 | let relative_path = pathdiffrust-analyzer cargo pathdiff 0.2.1 crate/::diff_pathsrust-analyzer cargo pathdiff 0.2.1 diff_paths().(&path, base)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 34 | if relative_path.is_relativerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[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(Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#::new(r"\")) { |
| 39 | return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 40 | } |
| 41 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().path = relative_path.to_string_lossyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_string_lossy().().into_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/impl#[`Cow<'_, B>`]into_owned().(); |
| 42 | } |
| 43 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(()) |
| 44 | }; |
| 45 | if config.relative_pathsrust-analyzer cargo git-delta 0.19.2 config/Config#relative_paths. && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().calling_processrust-analyzer cargo git-delta 0.19.2 utils/process/calling_process().().paths_in_input_are_relative_to_cwdrust-analyzer cargo git-delta 0.19.2 utils/process/impl#[CallingProcess]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_processrust-analyzer cargo git-delta 0.19.2 utils/path/cwd_of_user_shell_process().( |
| 55 | cwd_of_delta_process: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#>, |
| 56 | cwd_relative_to_repo_root: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&str>, |
| 57 | ) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#> { |
| 58 | match (cwd_of_delta_process, cwd_relative_to_repo_root) { |
| 59 | (Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(cwd), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#) => { |
| 60 | // We are not a child process of git |
| 61 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf][`From<&T>`]from().(cwd)) |
| 62 | } |
| 63 | (Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(repo_root), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#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 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf][`From<&T>`]from().(repo_root).joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().(cwd_relative_to_repo_root)) |
| 67 | } |
| 68 | (Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, _) => { |
| 69 | // Unexpected |
| 70 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#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_pathrust-analyzer cargo git-delta 0.19.2 utils/path/normalize_path().<P>(path: P) -> PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf# |
| 79 | where |
| 80 | P: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>, |
| 81 | { |
| 82 | let mut components = path.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().componentsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]components().().peekablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#peekable().(); |
| 83 | let mut ret = if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(c @ Componentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Component#::Prefixrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Component#Prefix#(..)) = components.peekrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/impl#[`Peekable<I>`]peek().().clonedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<&T>`]cloned().() { |
| 84 | components.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/impl#[`Peekable<I>`][Iterator]next().(); |
| 85 | PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf][`From<&T>`]from().(c.as_os_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[`Component<'a>`]as_os_str().()) |
| 86 | } else { |
| 87 | PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]new().() |
| 88 | }; |
| 89 | |
| 90 | for component in components { |
| 91 | match component { |
| 92 | Componentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Component#::Prefixrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Component#Prefix#(..) => unreachablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/unreachable!!(), |
| 93 | Componentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Component#::RootDirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Component#RootDir# => { |
| 94 | ret.pushrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]push().(component.as_os_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[`Component<'a>`]as_os_str().()); |
| 95 | } |
| 96 | Componentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Component#::CurDirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Component#CurDir# => {} |
| 97 | Componentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Component#::ParentDirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Component#ParentDir# => { |
| 98 | ret.poprust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]pop().(); |
| 99 | } |
| 100 | Componentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Component#::Normalrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Component#Normal#(c) => { |
| 101 | ret.pushrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]push().(c); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | ret |
| 106 | } |
| 107 | |
| 108 | #[cfg(test)] |
| 109 | pub fn fake_delta_cwd_for_testsrust-analyzer cargo git-delta 0.19.2 utils/path/fake_delta_cwd_for_tests().() -> PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf# { |
| 110 | #[cfg(not(target_os = "windows"))] |
| 111 | { |
| 112 | PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf][`From<&T>`]from().("/fake/delta/cwd") |
| 113 | } |
| 114 | #[cfg(target_os = "windows")] |
| 115 | { |
| 116 | PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#::from(r"C:\fake\delta\cwd") |
| 117 | } |
| 118 | } |