gix-path-0.10.13/.cargo_vcs_info.json0000644000000001460000000000100127200ustar { "git": { "sha1": "8ce49129a75e21346ceedf7d5f87fa3a34b024e1" }, "path_in_vcs": "gix-path" }gix-path-0.10.13/Cargo.toml0000644000000071510000000000100107210ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" rust-version = "1.65" name = "gix-path" version = "0.10.13" authors = ["Sebastian Thiel "] build = false include = [ "src/**/*", "LICENSE-*", ] autobins = false autoexamples = false autotests = false autobenches = false description = "A crate of the gitoxide project dealing paths and their conversions" readme = false license = "MIT OR Apache-2.0" repository = "https://github.com/GitoxideLabs/gitoxide" [lib] name = "gix_path" path = "src/lib.rs" doctest = false [dependencies.bstr] version = "1.3.0" features = ["std"] default-features = false [dependencies.gix-trace] version = "^0.1.11" [dependencies.once_cell] version = "1.17.1" [dependencies.thiserror] version = "2.0.0" [dev-dependencies.serial_test] version = "3.1.0" default-features = false [target.'cfg(not(target_family = "wasm"))'.dependencies.home] version = "0.5.5" [target."cfg(windows)".dev-dependencies.known-folders] version = "1.1.0" [target."cfg(windows)".dev-dependencies.windows] version = "0.58.0" features = ["Win32_System_Threading"] [target."cfg(windows)".dev-dependencies.winreg] version = "0.52.0" [lints.clippy] bool_to_int_with_if = "allow" borrow_as_ptr = "allow" cast_lossless = "allow" cast_possible_truncation = "allow" cast_possible_wrap = "allow" cast_precision_loss = "allow" cast_sign_loss = "allow" checked_conversions = "allow" copy_iterator = "allow" default_trait_access = "allow" doc_markdown = "allow" empty_docs = "allow" enum_glob_use = "allow" explicit_deref_methods = "allow" explicit_into_iter_loop = "allow" explicit_iter_loop = "allow" filter_map_next = "allow" fn_params_excessive_bools = "allow" from_iter_instead_of_collect = "allow" if_not_else = "allow" ignored_unit_patterns = "allow" implicit_clone = "allow" inconsistent_struct_constructor = "allow" inefficient_to_string = "allow" inline_always = "allow" items_after_statements = "allow" iter_not_returning_iterator = "allow" iter_without_into_iter = "allow" manual_assert = "allow" manual_is_variant_and = "allow" manual_let_else = "allow" manual_string_new = "allow" many_single_char_names = "allow" match_bool = "allow" match_same_arms = "allow" match_wild_err_arm = "allow" match_wildcard_for_single_variants = "allow" missing_errors_doc = "allow" missing_panics_doc = "allow" module_name_repetitions = "allow" must_use_candidate = "allow" mut_mut = "allow" naive_bytecount = "allow" needless_for_each = "allow" needless_pass_by_value = "allow" needless_raw_string_hashes = "allow" no_effect_underscore_binding = "allow" option_option = "allow" range_plus_one = "allow" redundant_else = "allow" return_self_not_must_use = "allow" should_panic_without_expect = "allow" similar_names = "allow" single_match_else = "allow" stable_sort_primitive = "allow" struct_excessive_bools = "allow" struct_field_names = "allow" too_long_first_doc_paragraph = "allow" too_many_lines = "allow" transmute_ptr_to_ptr = "allow" trivially_copy_pass_by_ref = "allow" unnecessary_join = "allow" unnecessary_wraps = "allow" unreadable_literal = "allow" unused_self = "allow" used_underscore_binding = "allow" wildcard_imports = "allow" [lints.clippy.pedantic] level = "warn" priority = -1 [lints.rust] gix-path-0.10.13/Cargo.toml.orig000064400000000000000000000016611046102023000144020ustar 00000000000000lints.workspace = true [package] name = "gix-path" version = "0.10.13" repository = "https://github.com/GitoxideLabs/gitoxide" license = "MIT OR Apache-2.0" description = "A crate of the gitoxide project dealing paths and their conversions" authors = ["Sebastian Thiel "] edition = "2021" include = ["src/**/*", "LICENSE-*"] rust-version = "1.65" [lib] doctest = false [dependencies] gix-trace = { version = "^0.1.11", path = "../gix-trace" } bstr = { version = "1.3.0", default-features = false, features = ["std"] } thiserror = "2.0.0" once_cell = "1.17.1" [target.'cfg(not(target_family = "wasm"))'.dependencies] home = "0.5.5" [dev-dependencies] gix-testtools = { path = "../tests/tools" } serial_test = { version = "3.1.0", default-features = false } [target.'cfg(windows)'.dev-dependencies] known-folders = "1.1.0" windows = { version = "0.58.0", features = ["Win32_System_Threading"] } winreg = "0.52.0" gix-path-0.10.13/LICENSE-APACHE000064400000000000000000000247461046102023000134500ustar 00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS 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 http://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. gix-path-0.10.13/LICENSE-MIT000064400000000000000000000017771046102023000131570ustar 00000000000000Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. gix-path-0.10.13/src/convert.rs000064400000000000000000000306341046102023000143320ustar 00000000000000use std::path::Component; use std::{ borrow::Cow, ffi::{OsStr, OsString}, path::{Path, PathBuf}, }; use bstr::{BStr, BString}; #[derive(Debug)] /// The error type returned by [`into_bstr()`] and others may suffer from failed conversions from or to bytes. pub struct Utf8Error; impl std::fmt::Display for Utf8Error { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str("Could not convert to UTF8 or from UTF8 due to ill-formed input") } } impl std::error::Error for Utf8Error {} /// Like [`into_bstr()`], but takes `OsStr` as input for a lossless, but fallible, conversion. pub fn os_str_into_bstr(path: &OsStr) -> Result<&BStr, Utf8Error> { let path = try_into_bstr(Cow::Borrowed(path.as_ref()))?; match path { Cow::Borrowed(path) => Ok(path), Cow::Owned(_) => unreachable!("borrowed cows stay borrowed"), } } /// Like [`into_bstr()`], but takes `OsString` as input for a lossless, but fallible, conversion. pub fn os_string_into_bstring(path: OsString) -> Result { let path = try_into_bstr(Cow::Owned(path.into()))?; match path { Cow::Borrowed(_path) => unreachable!("borrowed cows stay borrowed"), Cow::Owned(path) => Ok(path), } } /// Like [`into_bstr()`], but takes `Cow` as input for a lossless, but fallible, conversion. pub fn try_os_str_into_bstr(path: Cow<'_, OsStr>) -> Result, Utf8Error> { match path { Cow::Borrowed(path) => os_str_into_bstr(path).map(Cow::Borrowed), Cow::Owned(path) => os_string_into_bstring(path).map(Cow::Owned), } } /// Convert the given path either into its raw bytes on unix or its UTF8 encoded counterpart on windows. /// /// On windows, if the source Path contains ill-formed, lone surrogates, the UTF-8 conversion will fail /// causing `Utf8Error` to be returned. pub fn try_into_bstr<'a>(path: impl Into>) -> Result, Utf8Error> { let path = path.into(); let path_str = match path { Cow::Owned(path) => Cow::Owned({ #[cfg(unix)] let p: BString = { use std::os::unix::ffi::OsStringExt; path.into_os_string().into_vec().into() }; #[cfg(target_os = "wasi")] let p: BString = { use std::os::wasi::ffi::OsStringExt; path.into_os_string().into_vec().into() }; #[cfg(not(any(unix, target_os = "wasi")))] let p: BString = path.into_os_string().into_string().map_err(|_| Utf8Error)?.into(); p }), Cow::Borrowed(path) => Cow::Borrowed({ #[cfg(unix)] let p: &BStr = { use std::os::unix::ffi::OsStrExt; path.as_os_str().as_bytes().into() }; #[cfg(target_os = "wasi")] let p: &BStr = { use std::os::wasi::ffi::OsStrExt; path.as_os_str().as_bytes().into() }; #[cfg(not(any(unix, target_os = "wasi")))] let p: &BStr = path.to_str().ok_or(Utf8Error)?.as_bytes().into(); p }), }; Ok(path_str) } /// Similar to [`try_into_bstr()`] but **panics** if malformed surrogates are encountered on windows. pub fn into_bstr<'a>(path: impl Into>) -> Cow<'a, BStr> { try_into_bstr(path).expect("prefix path doesn't contain ill-formed UTF-8") } /// Join `path` to `base` such that they are separated with a `/`, i.e. `base/path`. pub fn join_bstr_unix_pathsep<'a, 'b>(base: impl Into>, path: impl Into<&'b BStr>) -> Cow<'a, BStr> { let mut base = base.into(); if !base.is_empty() && base.last() != Some(&b'/') { base.to_mut().push(b'/'); } base.to_mut().extend_from_slice(path.into()); base } /// Given `input` bytes, produce a `Path` from them ignoring encoding entirely if on unix. /// /// On windows, the input is required to be valid UTF-8, which is guaranteed if we wrote it before. There are some potential /// git versions and windows installation which produce mal-formed UTF-16 if certain emojies are in the path. It's as rare as /// it sounds, but possible. pub fn try_from_byte_slice(input: &[u8]) -> Result<&Path, Utf8Error> { #[cfg(unix)] let p = { use std::os::unix::ffi::OsStrExt; OsStr::from_bytes(input).as_ref() }; #[cfg(target_os = "wasi")] let p: &Path = { use std::os::wasi::ffi::OsStrExt; OsStr::from_bytes(input).as_ref() }; #[cfg(not(any(unix, target_os = "wasi")))] let p = Path::new(std::str::from_utf8(input).map_err(|_| Utf8Error)?); Ok(p) } /// Similar to [`from_byte_slice()`], but takes either borrowed or owned `input`. pub fn try_from_bstr<'a>(input: impl Into>) -> Result, Utf8Error> { let input = input.into(); match input { Cow::Borrowed(input) => try_from_byte_slice(input).map(Cow::Borrowed), Cow::Owned(input) => try_from_bstring(input).map(Cow::Owned), } } /// Similar to [`try_from_bstr()`], but **panics** if malformed surrogates are encountered on windows. pub fn from_bstr<'a>(input: impl Into>) -> Cow<'a, Path> { try_from_bstr(input).expect("prefix path doesn't contain ill-formed UTF-8") } /// Similar to [`try_from_bstr()`], but takes and produces owned data. pub fn try_from_bstring(input: impl Into) -> Result { let input = input.into(); #[cfg(unix)] let p = { use std::os::unix::ffi::OsStringExt; std::ffi::OsString::from_vec(input.into()).into() }; #[cfg(target_os = "wasi")] let p: PathBuf = { use std::os::wasi::ffi::OsStringExt; std::ffi::OsString::from_vec(input.into()).into() }; #[cfg(not(any(unix, target_os = "wasi")))] let p = { use bstr::ByteVec; PathBuf::from( { let v: Vec<_> = input.into(); v } .into_string() .map_err(|_| Utf8Error)?, ) }; Ok(p) } /// Similar to [`try_from_bstring()`], but will **panic** if there is ill-formed UTF-8 in the `input`. pub fn from_bstring(input: impl Into) -> PathBuf { try_from_bstring(input).expect("well-formed UTF-8 on windows") } /// Similar to [`try_from_byte_slice()`], but will **panic** if there is ill-formed UTF-8 in the `input`. pub fn from_byte_slice(input: &[u8]) -> &Path { try_from_byte_slice(input).expect("well-formed UTF-8 on windows") } fn replace<'a>(path: impl Into>, find: u8, replace: u8) -> Cow<'a, BStr> { let path = path.into(); match path { Cow::Owned(mut path) => { for b in path.iter_mut().filter(|b| **b == find) { *b = replace; } path.into() } Cow::Borrowed(path) => { if !path.contains(&find) { return path.into(); } let mut path = path.to_owned(); for b in path.iter_mut().filter(|b| **b == find) { *b = replace; } path.into() } } } /// Assures the given bytes use the native path separator. pub fn to_native_separators<'a>(path: impl Into>) -> Cow<'a, BStr> { #[cfg(not(windows))] let p = to_unix_separators(path); #[cfg(windows)] let p = to_windows_separators(path); p } /// Convert paths with slashes to backslashes on windows and do nothing on unix, but **panics** if malformed surrogates are encountered on windows. pub fn to_native_path_on_windows<'a>(path: impl Into>) -> Cow<'a, std::path::Path> { #[cfg(not(windows))] { crate::from_bstr(path) } #[cfg(windows)] { crate::from_bstr(to_windows_separators(path)) } } /// Replaces windows path separators with slashes, but only do so on windows. pub fn to_unix_separators_on_windows<'a>(path: impl Into>) -> Cow<'a, BStr> { #[cfg(windows)] { replace(path, b'\\', b'/') } #[cfg(not(windows))] { path.into() } } /// Replaces windows path separators with slashes, unconditionally. /// /// **Note** Do not use these and prefer the conditional versions of this method. // TODO: use https://lib.rs/crates/path-slash to handle escapes pub fn to_unix_separators<'a>(path: impl Into>) -> Cow<'a, BStr> { replace(path, b'\\', b'/') } /// Find backslashes and replace them with slashes, which typically resembles a unix path, unconditionally. /// /// **Note** Do not use these and prefer the conditional versions of this method. // TODO: use https://lib.rs/crates/path-slash to handle escapes pub fn to_windows_separators<'a>(path: impl Into>) -> Cow<'a, BStr> { replace(path, b'/', b'\\') } /// Resolve relative components virtually without accessing the file system, e.g. turn `a/./b/c/.././..` into `a`, /// without keeping intermediate `..` and `/a/../b/..` becomes `/`. /// If the input path was relative and ends up being the `current_dir`, `.` is returned instead of the full path to `current_dir`. /// Note that single `.` components as well as duplicate separators are left untouched. /// /// This is particularly useful when manipulating paths that are based on user input, and not resolving intermediate /// symlinks keeps the path similar to what the user provided. If that's not desirable, use `[realpath()][crate::realpath()` /// instead. /// /// Note that we might access the `current_dir` if we run out of path components to pop off, which is expected to be absolute /// as typical return value of `std::env::current_dir()` or `gix_fs::current_dir(…)` when `core.precomposeUnicode` is known. /// As a `current_dir` like `/c` can be exhausted by paths like `../../r`, `None` will be returned to indicate the inability /// to produce a logically consistent path. pub fn normalize<'a>(path: Cow<'a, Path>, current_dir: &Path) -> Option> { use std::path::Component::ParentDir; if !path.components().any(|c| matches!(c, ParentDir)) { return Some(path); } let mut current_dir_opt = Some(current_dir); let was_relative = path.is_relative(); let components = path.components(); let mut path = PathBuf::new(); for component in components { if let ParentDir = component { let path_was_dot = path == Path::new("."); if path.as_os_str().is_empty() || path_was_dot { path.push(current_dir_opt.take()?); } if !path.pop() { return None; } } else { path.push(component); } } if (path.as_os_str().is_empty() || path == current_dir) && was_relative { Cow::Borrowed(Path::new(".")) } else { path.into() } .into() } /// Rebuild the worktree-relative `relative_path` to be relative to `prefix`, which is the worktree-relative /// path equivalent to the position of the user, or current working directory. /// This is a no-op if `prefix` is empty. /// /// Note that both `relative_path` and `prefix` are assumed to be [normalized](normalize()), and failure to do so /// will lead to incorrect results. /// /// Note that both input paths are expected to be equal in terms of case too, as comparisons will be case-sensitive. pub fn relativize_with_prefix<'a>(relative_path: &'a Path, prefix: &Path) -> Cow<'a, Path> { if prefix.as_os_str().is_empty() { return Cow::Borrowed(relative_path); } debug_assert!( relative_path.components().all(|c| matches!(c, Component::Normal(_))), "BUG: all input is expected to be normalized, but relative_path was not" ); debug_assert!( prefix.components().all(|c| matches!(c, Component::Normal(_))), "BUG: all input is expected to be normalized, but prefix was not" ); let mut buf = PathBuf::new(); let mut rpc = relative_path.components().peekable(); let mut equal_thus_far = true; for pcomp in prefix.components() { if equal_thus_far { if let (Component::Normal(pname), Some(Component::Normal(rpname))) = (pcomp, rpc.peek()) { if &pname == rpname { rpc.next(); continue; } else { equal_thus_far = false; } } } buf.push(Component::ParentDir); } buf.extend(rpc); if buf.as_os_str().is_empty() { Cow::Borrowed(Path::new(".")) } else { Cow::Owned(buf) } } gix-path-0.10.13/src/env/git/mod.rs000064400000000000000000000204371046102023000150040ustar 00000000000000use std::env; use std::path::{Path, PathBuf}; use std::process::{Command, Stdio}; use bstr::{BStr, BString, ByteSlice}; use once_cell::sync::Lazy; /// Other places to find Git in. #[cfg(windows)] pub(super) static ALTERNATIVE_LOCATIONS: Lazy> = Lazy::new(|| locations_under_program_files(|key| std::env::var_os(key))); #[cfg(not(windows))] pub(super) static ALTERNATIVE_LOCATIONS: Lazy> = Lazy::new(Vec::new); #[cfg(windows)] fn locations_under_program_files(var_os_func: F) -> Vec where F: Fn(&str) -> Option, { // Should give a 64-bit program files path from a 32-bit or 64-bit process on a 64-bit system. let varname_64bit = "ProgramW6432"; // Should give a 32-bit program files path from a 32-bit or 64-bit process on a 64-bit system. // This variable is x86-specific, but neither Git nor Rust target 32-bit ARM on Windows. let varname_x86 = "ProgramFiles(x86)"; // Should give a 32-bit program files path on a 32-bit system. We also check this on a 64-bit // system, even though it *should* equal the process's architecture specific variable, so that // we cover the case of a parent process that passes down an overly sanitized environment that // lacks the architecture-specific variable. On a 64-bit system, because parent and child // processes' architectures can be different, Windows sets the child's ProgramFiles variable // from the ProgramW6432 or ProgramFiles(x86) variable applicable to the child's architecture. // Only if the parent does not pass that down is the passed-down ProgramFiles variable even // used. But this behavior is not well known, so that situation does sometimes happen. let varname_current = "ProgramFiles"; // 64-bit relative bin dir. So far, this is always mingw64, not ucrt64, clang64, or clangarm64. let suffix_64 = Path::new(r"Git\mingw64\bin"); // 32-bit relative bin dir. So far, this is always mingw32, not clang32. let suffix_32 = Path::new(r"Git\mingw32\bin"); // Whichever of the 64-bit or 32-bit relative bin better matches this process's architecture. // Unlike the system architecture, the process architecture is always known at compile time. #[cfg(target_pointer_width = "64")] let suffix_current = suffix_64; #[cfg(target_pointer_width = "32")] let suffix_current = suffix_32; let rules = [ (varname_64bit, suffix_64), (varname_x86, suffix_32), (varname_current, suffix_current), ]; let mut locations = vec![]; for (name, suffix) in rules { let Some(pf) = var_os_func(name) else { continue }; let pf = Path::new(&pf); if pf.is_relative() { // This shouldn't happen, but if it does then don't use the path. This is mainly in // case we are accidentally invoked with the environment variable set but empty. continue; } let location = pf.join(suffix); if !locations.contains(&location) { locations.push(location); } } locations } #[cfg(windows)] pub(super) const EXE_NAME: &str = "git.exe"; #[cfg(not(windows))] pub(super) const EXE_NAME: &str = "git"; /// Invoke the git executable to obtain the origin configuration, which is cached and returned. /// /// The git executable is the one found in PATH or an alternative location. pub(super) static GIT_HIGHEST_SCOPE_CONFIG_PATH: Lazy> = Lazy::new(exe_info); #[cfg(windows)] const NULL_DEVICE: &str = "NUL"; #[cfg(not(windows))] const NULL_DEVICE: &str = "/dev/null"; fn exe_info() -> Option { let mut cmd = git_cmd(EXE_NAME.into()); gix_trace::debug!(cmd = ?cmd, "invoking git for installation config path"); let cmd_output = match cmd.output() { Ok(out) => out.stdout, #[cfg(windows)] Err(err) if err.kind() == std::io::ErrorKind::NotFound => { let executable = ALTERNATIVE_LOCATIONS.iter().find_map(|prefix| { let candidate = prefix.join(EXE_NAME); candidate.is_file().then_some(candidate) })?; gix_trace::debug!(cmd = ?cmd, "invoking git for installation config path in alternate location"); git_cmd(executable).output().ok()?.stdout } Err(_) => return None, }; first_file_from_config_with_origin(cmd_output.as_slice().into()).map(ToOwned::to_owned) } fn git_cmd(executable: PathBuf) -> Command { let mut cmd = Command::new(executable); #[cfg(windows)] { use std::os::windows::process::CommandExt; const CREATE_NO_WINDOW: u32 = 0x08000000; cmd.creation_flags(CREATE_NO_WINDOW); } // We will try to run `git` from a location fairly high in the filesystem, in the hope it may // be faster if we are deeply nested, on a slow disk, or in a directory that has been deleted. let cwd = if cfg!(windows) { // We try the Windows directory (usually `C:\Windows`) first. It is given by `SystemRoot`, // except in rare cases where our own parent has not passed down that environment variable. env::var_os("SystemRoot") .or_else(|| env::var_os("windir")) .map(PathBuf::from) .filter(|p| p.is_absolute()) .unwrap_or_else(env::temp_dir) } else { "/".into() }; // Git 2.8.0 and higher support --show-origin. The -l, -z, and --name-only options were // supported even before that. In contrast, --show-scope was introduced later, in Git 2.26.0. // Low versions of Git are still sometimes used, and this is sometimes reasonable because // downstream distributions often backport security patches without adding most new features. // So for now, we forgo the convenience of --show-scope for greater backward compatibility. // // Separately from that, we can't use --system here, because scopes treated higher than the // system scope are possible. This commonly happens on macOS with Apple Git, where the config // file under `/Library` or `/Applications` is shown as an "unknown" scope but takes precedence // over the system scope. Although `GIT_CONFIG_NOSYSTEM` suppresses this scope along with the // system scope, passing --system selects only the system scope and omit this "unknown" scope. cmd.args(["config", "-lz", "--show-origin", "--name-only"]) .current_dir(cwd) .env_remove("GIT_CONFIG") .env_remove("GIT_DISCOVERY_ACROSS_FILESYSTEM") .env_remove("GIT_OBJECT_DIRECTORY") .env_remove("GIT_ALTERNATE_OBJECT_DIRECTORIES") .env_remove("GIT_COMMON_DIR") .env("GIT_DIR", NULL_DEVICE) // Avoid getting local-scope config. .env("GIT_WORK_TREE", NULL_DEVICE) // Avoid confusion when debugging. .stdin(Stdio::null()) .stderr(Stdio::null()); cmd } fn first_file_from_config_with_origin(source: &BStr) -> Option<&BStr> { let file = source.strip_prefix(b"file:")?; let end_pos = file.find_byte(b'\0')?; file[..end_pos].as_bstr().into() } /// Try to find the file that contains git configuration coming with the git installation. /// /// This returns the configuration associated with the `git` executable found in the current `PATH` /// or an alternative location, or `None` if no `git` executable was found or there were other /// errors during execution. pub(super) fn install_config_path() -> Option<&'static BStr> { let _span = gix_trace::detail!("gix_path::git::install_config_path()"); static PATH: Lazy> = Lazy::new(|| { // Shortcut: Specifically in Git for Windows 'Git Bash' shells, this variable is set. It // may let us deduce the installation directory, so we can save the `git` invocation. #[cfg(windows)] if let Some(mut exec_path) = std::env::var_os("EXEPATH").map(PathBuf::from) { exec_path.push("etc"); exec_path.push("gitconfig"); return crate::os_string_into_bstring(exec_path.into()).ok(); } GIT_HIGHEST_SCOPE_CONFIG_PATH.clone() }); PATH.as_ref().map(AsRef::as_ref) } /// Given `config_path` as obtained from `install_config_path()`, return the path of the git installation base. pub(super) fn config_to_base_path(config_path: &Path) -> &Path { config_path .parent() .expect("config file paths always have a file name to pop") } #[cfg(test)] mod tests; gix-path-0.10.13/src/env/git/tests.rs000064400000000000000000000602001046102023000153570ustar 00000000000000use std::path::Path; #[cfg(windows)] mod locations { use std::ffi::{OsStr, OsString}; use std::io::ErrorKind; use std::path::{Path, PathBuf}; use known_folders::{get_known_folder_path, KnownFolder}; use windows::core::Result as WindowsResult; use windows::Win32::Foundation::BOOL; use windows::Win32::System::Threading::{GetCurrentProcess, IsWow64Process}; use winreg::enums::{HKEY_LOCAL_MACHINE, KEY_QUERY_VALUE}; use winreg::RegKey; macro_rules! var_os_stub { { $($name:expr => $value:expr),* $(,)? } => { |key| { match key { $( $name => Some(OsString::from($value)), )* _ => None, } } } } macro_rules! locations_from { ($($name:expr => $value:expr),* $(,)?) => { super::super::locations_under_program_files(var_os_stub! { $( $name => $value, )* }) } } macro_rules! pathbuf_vec { [$($path:expr),* $(,)?] => { vec![$( PathBuf::from($path), )*] } } #[test] fn locations_under_program_files_ordinary() { assert_eq!( locations_from!( "ProgramFiles" => r"C:\Program Files", ), if cfg!(target_pointer_width = "64") { pathbuf_vec![r"C:\Program Files\Git\mingw64\bin"] } else { pathbuf_vec![r"C:\Program Files\Git\mingw32\bin"] }, ); assert_eq!( locations_from!( "ProgramFiles" => { if cfg!(target_pointer_width = "64") { r"C:\Program Files" } else { r"C:\Program Files (x86)" } }, "ProgramFiles(x86)" => r"C:\Program Files (x86)", "ProgramW6432" => r"C:\Program Files", ), pathbuf_vec![ r"C:\Program Files\Git\mingw64\bin", r"C:\Program Files (x86)\Git\mingw32\bin", ], ); assert_eq!(locations_from!(), Vec::::new()); } #[test] fn locations_under_program_files_strange() { assert_eq!( locations_from!( "ProgramFiles" => r"X:\cur\rent", "ProgramFiles(x86)" => r"Y:\nar\row", "ProgramW6432" => r"Z:\wi\de", ), pathbuf_vec![ r"Z:\wi\de\Git\mingw64\bin", r"Y:\nar\row\Git\mingw32\bin", if cfg!(target_pointer_width = "64") { r"X:\cur\rent\Git\mingw64\bin" } else { r"X:\cur\rent\Git\mingw32\bin" }, ], ); assert_eq!( locations_from!( "ProgramW6432" => r"Z:\wi\de", ), pathbuf_vec![r"Z:\wi\de\Git\mingw64\bin"], ); assert_eq!( locations_from!( "ProgramFiles" => r"Z:/wi//de/", "ProgramFiles(x86)" => r"Y:/\nar\/row", "ProgramW6432" => r"Z:\wi\.\de", ), if cfg!(target_pointer_width = "64") { pathbuf_vec![r"Z:\wi\de\Git\mingw64\bin", r"Y:\nar\row\Git\mingw32\bin"] } else { pathbuf_vec![ r"Z:\wi\de\Git\mingw64\bin", r"Y:\nar\row\Git\mingw32\bin", r"Z:\wi\de\Git\mingw32\bin", ] }, ); assert_eq!( locations_from!( "ProgramFiles" => r"foo\bar", "ProgramFiles(x86)" => r"\\host\share\subdir", "ProgramW6432" => r"", ), pathbuf_vec![r"\\host\share\subdir\Git\mingw32\bin"], ); } #[derive(Clone, Copy, Debug)] enum PlatformArchitecture { Is32on32, Is32on64, Is64on64, } impl PlatformArchitecture { fn current() -> WindowsResult { // Ordinarily, we would check the target pointer width first to avoid doing extra work, // because if this is a 64-bit executable then the operating system is 64-bit. But this // is for the test suite, and doing it this way allows problems to be caught earlier if // a change made on a 64-bit development machine breaks the IsWow64Process() call. let mut wow64process = BOOL::default(); unsafe { IsWow64Process(GetCurrentProcess(), &mut wow64process)? }; let platform_architecture = if wow64process.as_bool() { Self::Is32on64 } else if cfg!(target_pointer_width = "32") { Self::Is32on32 } else { assert!(cfg!(target_pointer_width = "64")); Self::Is64on64 }; Ok(platform_architecture) } } fn ends_with_case_insensitive(full_text: &OsStr, literal_pattern: &str) -> Option { let folded_text = full_text.to_str()?.to_lowercase(); let folded_pattern = literal_pattern.to_lowercase(); Some(folded_text.ends_with(&folded_pattern)) } /// The common global program files paths on this system, by process and system architecture. #[derive(Clone, Debug)] struct ProgramFilesPaths { /// The program files directory used for whatever architecture this program was built for. current: PathBuf, /// The x86 program files directory regardless of the architecture of the program. /// /// If Rust gains Windows targets like ARMv7 where this is unavailable, this could fail. x86: PathBuf, /// The 64-bit program files directory if there is one. /// /// This is present on x64 and also ARM64 systems. On an ARM64 system, ARM64 and AMD64 /// programs use the same program files directory while 32-bit x86 and ARM programs use /// two others. Only a 32-bit has no 64-bit program files directory. maybe_64bit: Option, } impl ProgramFilesPaths { /// Gets the three common kinds of global program files paths without environment variables. /// /// The idea here is to obtain this information, which the `alternative_locations()` unit /// test uses to learn the expected alternative locations, without duplicating *any* of the /// approach used for `ALTERNATIVE_LOCATIONS`, so it can be used to test that. The approach /// here is also more reliable than using environment variables, but it is a bit more /// complex, and it requires either additional dependencies or the use of unsafe code. /// /// This gets `pf_current` and `pf_x86` by the [known folders][known-folders] system. But /// it gets `maybe_pf_64bit` from the registry, as the corresponding known folder is not /// available to 32-bit processes. See the [`KNOWNFOLDDERID`][knownfolderid] documentation. /// /// If in the future the implementation of `ALTERNATIVE_LOCATIONS` uses these techniques, /// then this function can be changed to use environment variables and renamed accordingly. /// /// [known-folders]: https://learn.microsoft.com/en-us/windows/win32/shell/known-folders /// [knownfolderid]: https://learn.microsoft.com/en-us/windows/win32/shell/knownfolderid#remarks fn obtain_envlessly() -> Self { let pf_current = get_known_folder_path(KnownFolder::ProgramFiles) .expect("The process architecture specific program files folder is always available"); let pf_x86 = get_known_folder_path(KnownFolder::ProgramFilesX86) .expect("The x86 program files folder will in practice always be available"); let maybe_pf_64bit = RegKey::predef(HKEY_LOCAL_MACHINE) .open_subkey_with_flags(r"SOFTWARE\Microsoft\Windows\CurrentVersion", KEY_QUERY_VALUE) .expect("The `CurrentVersion` registry key exists and allows reading") .get_value::("ProgramW6432Dir") .map(PathBuf::from) .map_err(|error| { assert_eq!(error.kind(), ErrorKind::NotFound); error }) .ok(); Self { current: pf_current, x86: pf_x86, maybe_64bit: maybe_pf_64bit, } } /// Checks that the paths we got for testing are reasonable. /// /// This checks that `obtain_envlessly()` returned paths that are likely to be correct and /// that satisfy the most important properties based on the current system and process. fn validated(self) -> Self { match PlatformArchitecture::current().expect("Process and system 'bitness' should be available") { PlatformArchitecture::Is32on32 => { assert_eq!( self.current.as_os_str(), self.x86.as_os_str(), "Our program files path is exactly identical to the 32-bit one.", ); for trailing_arch in [" (x86)", " (Arm)"] { let is_adorned = ends_with_case_insensitive(self.current.as_os_str(), trailing_arch) .expect("Assume the test system's important directories are valid Unicode"); assert!( !is_adorned, "The 32-bit program files directory name on a 32-bit system mentions no architecture.", ); } assert_eq!( self.maybe_64bit, None, "A 32-bit system has no 64-bit program files directory.", ); } PlatformArchitecture::Is32on64 => { assert_eq!( self.current.as_os_str(), self.x86.as_os_str(), "Our program files path is exactly identical to the 32-bit one.", ); let pf_64bit = self .maybe_64bit .as_ref() .expect("The 64-bit program files directory exists"); assert_ne!( &self.x86, pf_64bit, "The 32-bit and 64-bit program files directories have different locations.", ); } PlatformArchitecture::Is64on64 => { let pf_64bit = self .maybe_64bit .as_ref() .expect("The 64-bit program files directory exists"); assert_eq!( self.current.as_os_str(), pf_64bit.as_os_str(), "Our program files path is exactly identical to the 64-bit one.", ); assert_ne!( &self.x86, pf_64bit, "The 32-bit and 64-bit program files directories have different locations.", ); } } self } } /// Paths relative to process architecture specific program files directories. #[derive(Clone, Debug)] struct RelativeGitBinPaths<'a> { x86: &'a Path, maybe_64bit: Option<&'a Path>, } impl<'a> RelativeGitBinPaths<'a> { /// Assert that `locations` has the given path prefixes, and extract the suffixes. fn assert_from(pf: &'a ProgramFilesPaths, locations: &'static [PathBuf]) -> Self { match locations { [primary, secondary] => { let prefix_64bit = pf .maybe_64bit .as_ref() .expect("It gives two paths only if one can be 64-bit"); let suffix_64bit = primary .strip_prefix(prefix_64bit) .expect("It gives the 64-bit path and lists it first"); let suffix_x86 = secondary .strip_prefix(pf.x86.as_path()) .expect("It gives the 32-bit path and lists it second"); Self { x86: suffix_x86, maybe_64bit: Some(suffix_64bit), } } [only] => { assert_eq!(pf.maybe_64bit, None, "It gives one path only if none can be 64-bit."); let suffix_x86 = only .strip_prefix(pf.x86.as_path()) .expect("The one path it gives is the 32-bit path"); Self { x86: suffix_x86, maybe_64bit: None, } } other => panic!("{:?} has length {}, expected 1 or 2.", other, other.len()), } } /// Assert that the suffixes (relative subdirectories) are the common per-architecture Git install locations. fn assert_architectures(&self) { assert_eq!(self.x86, Path::new("Git/mingw32/bin")); if let Some(suffix_64bit) = self.maybe_64bit { // When Git for Windows releases ARM64 builds, there will be another 64-bit suffix, // likely clangarm64. In that case, this and other assertions will need updating, // as there will be two separate paths to check under the same 64-bit program files // directory. (See the definition of ProgramFilesPaths::maybe_64bit for details.) assert_eq!(suffix_64bit, Path::new("Git/mingw64/bin")); } } } #[test] fn alternative_locations() { // Obtain program files directory paths by other means and check that they seem correct. let pf = ProgramFilesPaths::obtain_envlessly().validated(); // Check that `ALTERNATIVE_LOCATIONS` correspond to them, with the correct subdirectories. let locations = super::super::ALTERNATIVE_LOCATIONS.as_slice(); RelativeGitBinPaths::assert_from(&pf, locations).assert_architectures(); } } #[cfg(not(windows))] mod locations { #[test] fn alternative_locations() { assert!(super::super::ALTERNATIVE_LOCATIONS.is_empty()); } } mod exe_info { use std::path::{Path, PathBuf}; use crate::env::git::{exe_info, NULL_DEVICE}; use gix_testtools::tempfile; use serial_test::serial; /// Wrapper for a valid path to a plausible location, kept from accidentally existing (until drop). #[derive(Debug)] struct NonexistentLocation { _empty: tempfile::TempDir, nonexistent: PathBuf, } impl NonexistentLocation { fn new() -> Self { let empty = tempfile::tempdir().expect("can create new temporary subdirectory"); let nonexistent = empty .path() .canonicalize() .expect("path to the new directory works") .join("nonexistent"); assert!(!nonexistent.exists(), "Test bug: Need nonexistent directory"); Self { _empty: empty, nonexistent, } } fn path(&self) -> &Path { &self.nonexistent } } fn set_temp_env_vars<'a>(path: &Path) -> gix_testtools::Env<'a> { let path_str = path.to_str().expect("valid Unicode"); let env = gix_testtools::Env::new() .set("TMPDIR", path_str) // Mainly for Unix. .set("TMP", path_str) // Mainly for Windows. .set("TEMP", path_str); // Mainly for Windows, too. assert_eq!( std::env::temp_dir(), path, "Possible test bug: Temp dir path may not have been customized successfully" ); env } fn unset_windows_directory_vars<'a>() -> gix_testtools::Env<'a> { gix_testtools::Env::new().unset("windir").unset("SystemRoot") } fn check_exe_info() { let path = exe_info() .map(crate::from_bstring) .expect("It is present in the test environment (nonempty config)"); assert!( path.is_absolute(), "It is absolute (unless overridden such as with GIT_CONFIG_SYSTEM)" ); assert!( path.exists(), "It should exist on disk, since `git config` just found an entry there" ); } #[test] #[serial] fn with_unmodified_environment() { check_exe_info(); } #[test] #[serial] fn tolerates_broken_temp() { let non = NonexistentLocation::new(); let _env = set_temp_env_vars(non.path()); check_exe_info(); } #[test] #[serial] fn tolerates_oversanitized_env() { // This test runs on all systems, but it is only checking for a Windows regression. Also, on // Windows, having both a broken temp dir and an over-sanitized environment is not supported. let _env = unset_windows_directory_vars(); check_exe_info(); } #[test] #[serial] fn tolerates_git_config_env_var() { let _env = gix_testtools::Env::new().set("GIT_CONFIG", NULL_DEVICE); check_exe_info(); } #[test] #[serial] fn same_result_with_broken_temp() { let with_unmodified_temp = exe_info(); let with_nonexistent_temp = { let non = NonexistentLocation::new(); let _env = set_temp_env_vars(non.path()); exe_info() }; assert_eq!(with_unmodified_temp, with_nonexistent_temp); } #[test] #[serial] fn same_result_with_oversanitized_env() { let with_unmodified_env = exe_info(); let with_oversanitized_env = { let _env = unset_windows_directory_vars(); exe_info() }; assert_eq!(with_unmodified_env, with_oversanitized_env); } #[test] #[serial] fn same_result_with_git_config_env_var() { let with_unmodified_env = exe_info(); let with_git_config_env_var = { let _env = gix_testtools::Env::new().set("GIT_CONFIG", NULL_DEVICE); exe_info() }; assert_eq!(with_unmodified_env, with_git_config_env_var); } #[test] #[serial] #[cfg(not(target_os = "macos"))] // Assumes no higher "unknown" scope. The `nosystem` case works. fn never_from_local_scope() { let repo = gix_testtools::scripted_fixture_read_only("local_config.sh").expect("script succeeds"); let _cwd = gix_testtools::set_current_dir(repo).expect("can change to repo dir"); let _env = gix_testtools::Env::new() .set("GIT_CONFIG_SYSTEM", NULL_DEVICE) .set("GIT_CONFIG_GLOBAL", NULL_DEVICE); let maybe_path = exe_info(); assert_eq!( maybe_path, None, "Should find no config path if the config would be local (empty system config)" ); } #[test] #[serial] fn never_from_local_scope_nosystem() { let repo = gix_testtools::scripted_fixture_read_only("local_config.sh").expect("script succeeds"); let _cwd = gix_testtools::set_current_dir(repo).expect("can change to repo dir"); let _env = gix_testtools::Env::new() .set("GIT_CONFIG_NOSYSTEM", "1") .set("GIT_CONFIG_GLOBAL", NULL_DEVICE); let maybe_path = exe_info(); assert_eq!( maybe_path, None, "Should find no config path if the config would be local (suppressed system config)" ); } #[test] #[serial] #[cfg(not(target_os = "macos"))] // Assumes no higher "unknown" scope. The `nosystem` case works. fn never_from_local_scope_even_if_temp_is_here() { let repo = gix_testtools::scripted_fixture_read_only("local_config.sh") .expect("script succeeds") .canonicalize() .expect("repo path is valid and exists"); let _cwd = gix_testtools::set_current_dir(&repo).expect("can change to repo dir"); let _env = set_temp_env_vars(&repo) .set("GIT_CONFIG_SYSTEM", NULL_DEVICE) .set("GIT_CONFIG_GLOBAL", NULL_DEVICE); let maybe_path = exe_info(); assert_eq!( maybe_path, None, "Should find no config path if the config would be local even in a `/tmp`-like dir (empty system config)" ); } #[test] #[serial] fn never_from_local_scope_even_if_temp_is_here_nosystem() { let repo = gix_testtools::scripted_fixture_read_only("local_config.sh") .expect("script succeeds") .canonicalize() .expect("repo path is valid and exists"); let _cwd = gix_testtools::set_current_dir(&repo).expect("can change to repo dir"); let _env = set_temp_env_vars(&repo) .set("GIT_CONFIG_NOSYSTEM", "1") .set("GIT_CONFIG_GLOBAL", NULL_DEVICE); let maybe_path = exe_info(); assert_eq!( maybe_path, None, "Should find no config path if the config would be local even in a `/tmp`-like dir (suppressed system config)" ); } #[test] #[serial] fn never_from_git_config_env_var() { let repo = gix_testtools::scripted_fixture_read_only("local_config.sh").expect("script succeeds"); // Get an absolute path to a config file that is non-UNC if possible so any Git accepts it. let config_path = std::env::current_dir() .expect("got CWD") .join(repo) .join(".git") .join("config") .to_str() .expect("valid UTF-8") .to_owned(); let _env = gix_testtools::Env::new() .set("GIT_CONFIG_NOSYSTEM", "1") .set("GIT_CONFIG_GLOBAL", NULL_DEVICE) .set("GIT_CONFIG", config_path); let maybe_path = exe_info(); assert_eq!( maybe_path, None, "Should find no config path from GIT_CONFIG (even if nonempty)" ); } #[test] fn first_file_from_config_with_origin() { let macos = "file:/Applications/Xcode.app/Contents/Developer/usr/share/git-core/gitconfig\0credential.helper\0file:/Users/byron/.gitconfig\0push.default\0"; let win_msys = "file:C:/git-sdk-64/etc/gitconfig\0core.symlinks\0file:C:/git-sdk-64/etc/gitconfig\0core.autocrlf\0"; let win_cmd = "file:C:/Program Files/Git/etc/gitconfig\0diff.astextplain.textconv\0file:C:/Program Files/Git/etc/gitconfig\0filter.lfs.clean\0"; let win_msys_old = "file:C:\\ProgramData/Git/config\0diff.astextplain.textconv\0file:C:\\ProgramData/Git/config\0filter.lfs.clean\0"; let linux = "file:/home/parallels/.gitconfig\0core.excludesfile\0"; let bogus = "something unexpected"; let empty = ""; for (source, expected) in [ ( macos, Some("/Applications/Xcode.app/Contents/Developer/usr/share/git-core/gitconfig"), ), (win_msys, Some("C:/git-sdk-64/etc/gitconfig")), (win_msys_old, Some("C:\\ProgramData/Git/config")), (win_cmd, Some("C:/Program Files/Git/etc/gitconfig")), (linux, Some("/home/parallels/.gitconfig")), (bogus, None), (empty, None), ] { assert_eq!( crate::env::git::first_file_from_config_with_origin(source.into()), expected.map(Into::into) ); } } } #[test] fn config_to_base_path() { for (input, expected) in [ ( "/Applications/Xcode.app/Contents/Developer/usr/share/git-core/gitconfig", "/Applications/Xcode.app/Contents/Developer/usr/share/git-core", ), ("C:/git-sdk-64/etc/gitconfig", "C:/git-sdk-64/etc"), ("C:\\ProgramData/Git/config", "C:\\ProgramData/Git"), ("C:/Program Files/Git/etc/gitconfig", "C:/Program Files/Git/etc"), ] { assert_eq!(super::config_to_base_path(Path::new(input)), Path::new(expected)); } } gix-path-0.10.13/src/env/mod.rs000064400000000000000000000146121046102023000142170ustar 00000000000000use std::ffi::OsString; use std::path::{Path, PathBuf}; use bstr::{BString, ByteSlice}; use once_cell::sync::Lazy; use crate::env::git::EXE_NAME; mod git; /// Return the location at which installation specific git configuration file can be found, or `None` /// if the binary could not be executed or its results could not be parsed. /// /// ### Performance /// /// This invokes the git binary which is slow on windows. pub fn installation_config() -> Option<&'static Path> { git::install_config_path().and_then(|p| crate::try_from_byte_slice(p).ok()) } /// Return the location at which git installation specific configuration files are located, or `None` if the binary /// could not be executed or its results could not be parsed. /// /// ### Performance /// /// This invokes the git binary which is slow on windows. pub fn installation_config_prefix() -> Option<&'static Path> { installation_config().map(git::config_to_base_path) } /// Return the name of the Git executable to invoke it. /// If it's in the `PATH`, it will always be a short name. /// /// Note that on Windows, we will find the executable in the `PATH` if it exists there, or search it /// in alternative locations which when found yields the full path to it. pub fn exe_invocation() -> &'static Path { if cfg!(windows) { /// The path to the Git executable as located in the `PATH` or in other locations that it's known to be installed to. /// It's `None` if environment variables couldn't be read or if no executable could be found. static EXECUTABLE_PATH: Lazy> = Lazy::new(|| { std::env::split_paths(&std::env::var_os("PATH")?) .chain(git::ALTERNATIVE_LOCATIONS.iter().map(Into::into)) .find_map(|prefix| { let full_path = prefix.join(EXE_NAME); full_path.is_file().then_some(full_path) }) .map(|exe_path| { let is_in_alternate_location = git::ALTERNATIVE_LOCATIONS .iter() .any(|prefix| exe_path.strip_prefix(prefix).is_ok()); if is_in_alternate_location { exe_path } else { EXE_NAME.into() } }) }); EXECUTABLE_PATH.as_deref().unwrap_or(Path::new(git::EXE_NAME)) } else { Path::new("git") } } /// Returns the fully qualified path in the *xdg-home* directory (or equivalent in the home dir) to `file`, /// accessing `env_var()` to learn where these bases are. /// /// Note that the `HOME` directory should ultimately come from [`home_dir()`] as it handles windows correctly. /// The same can be achieved by using [`var()`] as `env_var`. pub fn xdg_config(file: &str, env_var: &mut dyn FnMut(&str) -> Option) -> Option { env_var("XDG_CONFIG_HOME") .map(|home| { let mut p = PathBuf::from(home); p.push("git"); p.push(file); p }) .or_else(|| { env_var("HOME").map(|home| { let mut p = PathBuf::from(home); p.push(".config"); p.push("git"); p.push(file); p }) }) } /// Returns the platform dependent system prefix or `None` if it cannot be found (right now only on windows). /// /// ### Performance /// /// On windows, the slowest part is the launch of the Git executable in the PATH, which only happens when launched /// from outside of the `msys2` shell. /// /// ### When `None` is returned /// /// This happens only windows if the git binary can't be found at all for obtaining its executable path, or if the git binary /// wasn't built with a well-known directory structure or environment. pub fn system_prefix() -> Option<&'static Path> { if cfg!(windows) { static PREFIX: Lazy> = Lazy::new(|| { if let Some(root) = std::env::var_os("EXEPATH").map(PathBuf::from) { for candidate in ["mingw64", "mingw32"] { let candidate = root.join(candidate); if candidate.is_dir() { return Some(candidate); } } } let mut cmd = std::process::Command::new(exe_invocation()); #[cfg(windows)] { use std::os::windows::process::CommandExt; const CREATE_NO_WINDOW: u32 = 0x08000000; cmd.creation_flags(CREATE_NO_WINDOW); } cmd.arg("--exec-path").stderr(std::process::Stdio::null()); gix_trace::debug!(cmd = ?cmd, "invoking git to get system prefix/exec path"); let path = cmd.output().ok()?.stdout; let path = BString::new(path) .trim_with(|b| b.is_ascii_whitespace()) .to_path() .ok()? .to_owned(); let one_past_prefix = path.components().enumerate().find_map(|(idx, c)| { matches!(c,std::path::Component::Normal(name) if name.to_str() == Some("libexec")).then_some(idx) })?; Some(path.components().take(one_past_prefix.checked_sub(1)?).collect()) }); PREFIX.as_deref() } else { Path::new("/").into() } } /// Returns `$HOME` or `None` if it cannot be found. #[cfg(target_family = "wasm")] pub fn home_dir() -> Option { std::env::var("HOME").map(PathBuf::from).ok() } /// Tries to obtain the home directory from `HOME` on all platforms, but falls back to [`home::home_dir()`] for /// more complex ways of obtaining a home directory, particularly useful on Windows. /// /// The reason `HOME` is tried first is to allow Windows users to have a custom location for their linux-style /// home, as otherwise they would have to accumulate dot files in a directory these are inconvenient and perceived /// as clutter. #[cfg(not(target_family = "wasm"))] pub fn home_dir() -> Option { std::env::var_os("HOME").map(Into::into).or_else(home::home_dir) } /// Returns the contents of an environment variable of `name` with some special handling /// for certain environment variables (like `HOME`) for platform compatibility. pub fn var(name: &str) -> Option { if name == "HOME" { home_dir().map(PathBuf::into_os_string) } else { std::env::var_os(name) } } gix-path-0.10.13/src/lib.rs000064400000000000000000000104171046102023000134150ustar 00000000000000//! This crate contains an assortment of utilities to deal with paths and their conversions. //! //! Generally `git` treats paths as bytes, but inherently assumes non-illformed UTF-8 as encoding on windows. Internally, it expects //! slashes to be used as path separators and paths in files must have slashes, with conversions being performed on windows accordingly. //! //!
//! //! ### Research //! //! * **windows** //! - [`dirent.c`](https://github.com/git/git/blob/main/compat/win32/dirent.c#L31:L31) contains all implementation (seemingly) of opening directories and reading their entries, along with all path conversions (UTF-16 for windows). This is done on the fly so git can work with [in UTF-8](https://github.com/git/git/blob/main/compat/win32/dirent.c#L12:L12). //! - mingw [is used for the conversion](https://github.com/git/git/blob/main/compat/mingw.h#L579:L579) and it appears they handle surrogates during the conversion, maybe some sort of non-strict UTF-8 converter? Actually it uses [WideCharToMultiByte](https://docs.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-widechartomultibyte) //! under the hood which by now does fail if the UTF-8 would be invalid unicode, i.e. unicode pairs. //! - `OsString` on windows already stores strings as WTF-8, which supports [surrogate pairs](https://unicodebook.readthedocs.io/unicode_encodings.html), //! something that UTF-8 isn't allowed do it for security reasons, after all it's UTF-16 specific and exists only to extend //! the encodable code-points. //! - informative reading on [WTF-8](https://simonsapin.github.io/wtf-8/#motivation) which is the encoding used by Rust //! internally that deals with surrogates and non-wellformed surrogates (those that aren't in pairs). //! * **unix** //! - It uses [opendir](https://man7.org/linux/man-pages/man3/opendir.3.html) and [readdir](https://man7.org/linux/man-pages/man3/readdir.3.html) //! respectively. There is no encoding specified, except that these paths are null-terminated. //! //! ### Learnings //! //! Surrogate pairs are a way to extend the encodable value range in UTF-16 encodings, used primarily on windows and in Javascript. //! For a long time these codepoints used for surrogates, always to be used in pairs, were not assigned, until…they were for rare //! emojies and the likes. The unicode standard does not require surrogates to happen in pairs, even though by now unpaired surrogates //! in UTF-16 are considered ill-formed, which aren't supposed to be converted to UTF-8 for example. //! //! This is the reason we have to deal with `to_string_lossy()`, it's _just_ for that quirk. //! //! This also means the only platform ever eligible to see conversion errors is windows, and there it's only older pre-vista //! windows versions which incorrectly allow ill-formed UTF-16 strings. Newer versions don't perform such conversions anymore, for //! example when going from UTF-16 to UTF-8, they will trigger an error. //! //! ### Conclusions //! //! Since [WideCharToMultiByte](https://docs.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-widechartomultibyte) by now is //! fixed (Vista onward) to produce valid UTF-8, lone surrogate codepoints will cause failure, which `git` //! [doesn't care about](https://github.com/git/git/blob/main/compat/win32/dirent.c#L12:L12). //! //! We will, though, which means from now on we can just convert to UTF-8 on windows and bubble up errors where necessary, //! preventing potential mismatched surrogate pairs to ever be saved on disk by gitoxide. //! //! Even though the error only exists on older windows versions, we will represent it in the type system through fallible function calls. //! Callers may `.expect()` on the result to indicate they don't wish to handle this special and rare case. Note that servers should not //! ever get into a code-path which does panic though. //!
#![deny(missing_docs, rust_2018_idioms)] #![cfg_attr(not(test), forbid(unsafe_code))] /// A dummy type to represent path specs and help finding all spots that take path specs once it is implemented. mod convert; pub use convert::*; mod util; pub use util::is_absolute; /// pub mod realpath; pub use realpath::function::{realpath, realpath_opts}; /// Information about the environment in terms of locations of resources. pub mod env; gix-path-0.10.13/src/realpath.rs000064400000000000000000000102631046102023000144460ustar 00000000000000/// The error returned by [`realpath()`][super::realpath()]. #[derive(Debug, thiserror::Error)] #[allow(missing_docs)] pub enum Error { #[error("The maximum allowed number {} of symlinks in path is exceeded", .max_symlinks)] MaxSymlinksExceeded { max_symlinks: u8 }, #[error("Cannot resolve symlinks in path with more than {max_symlink_checks} components (takes too long)")] ExcessiveComponentCount { max_symlink_checks: usize }, #[error(transparent)] ReadLink(std::io::Error), #[error(transparent)] CurrentWorkingDir(std::io::Error), #[error("Empty is not a valid path")] EmptyPath, #[error("Ran out of path components while following parent component '..'")] MissingParent, } /// The default amount of symlinks we may follow when resolving a path in [`realpath()`][crate::realpath()]. pub const MAX_SYMLINKS: u8 = 32; pub(crate) mod function { use std::path::{ Component::{CurDir, Normal, ParentDir, Prefix, RootDir}, Path, PathBuf, }; use super::Error; use crate::realpath::MAX_SYMLINKS; /// Check each component of `path` and see if it is a symlink. If so, resolve it. /// Do not fail for non-existing components, but assume these are as is. /// /// If `path` is relative, the current working directory be used to make it absolute. /// Note that the returned path will be verbatim, and repositories with `core.precomposeUnicode` /// set will probably want to precompose the paths unicode. pub fn realpath(path: impl AsRef) -> Result { let path = path.as_ref(); let cwd = path .is_relative() .then(std::env::current_dir) .unwrap_or_else(|| Ok(PathBuf::default())) .map_err(Error::CurrentWorkingDir)?; realpath_opts(path, &cwd, MAX_SYMLINKS) } /// The same as [`realpath()`], but allow to configure `max_symlinks` to configure how many symbolic links we are going to follow. /// This serves to avoid running into cycles or doing unreasonable amounts of work. pub fn realpath_opts(path: &Path, cwd: &Path, max_symlinks: u8) -> Result { if path.as_os_str().is_empty() { return Err(Error::EmptyPath); } let mut real_path = PathBuf::new(); if path.is_relative() { real_path.push(cwd); } let mut num_symlinks = 0; let mut path_backing: PathBuf; let mut components = path.components(); const MAX_SYMLINK_CHECKS: usize = 2048; let mut symlink_checks = 0; while let Some(component) = components.next() { match component { part @ (RootDir | Prefix(_)) => real_path.push(part), CurDir => {} ParentDir => { if !real_path.pop() { return Err(Error::MissingParent); } } Normal(part) => { real_path.push(part); symlink_checks += 1; if real_path.is_symlink() { num_symlinks += 1; if num_symlinks > max_symlinks { return Err(Error::MaxSymlinksExceeded { max_symlinks }); } let mut link_destination = std::fs::read_link(real_path.as_path()).map_err(Error::ReadLink)?; if link_destination.is_absolute() { // pushing absolute path to real_path resets it to the pushed absolute path } else { assert!(real_path.pop(), "we just pushed a component"); } link_destination.extend(components); path_backing = link_destination; components = path_backing.components(); } if symlink_checks > MAX_SYMLINK_CHECKS { return Err(Error::ExcessiveComponentCount { max_symlink_checks: MAX_SYMLINK_CHECKS, }); } } } } Ok(real_path) } } gix-path-0.10.13/src/util.rs000064400000000000000000000005271046102023000136250ustar 00000000000000use std::path::Path; /// return true if `path` is absolute, which depends on the platform but is always true if it starts with a `slash`, hence looks like /// a linux path. pub fn is_absolute(path: impl AsRef) -> bool { let path = path.as_ref(); path.is_absolute() || path.to_str().and_then(|s| s.chars().next()) == Some('/') }