Skip to content

Commit ea52e4e

Browse files
committed
A way to parse the first git-config file path (#450)
1 parent cc78473 commit ea52e4e

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

Cargo.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

git-repository/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ git-prompt = { version = "0.1.0", path = "../git-prompt" }
110110
git-index = { version = "^0.4.3", path = "../git-index" }
111111
git-worktree = { version = "^0.4.3", path = "../git-worktree" }
112112

113+
once_cell = "1.14.0"
113114
signal-hook = { version = "0.3.9", default-features = false }
114115
thiserror = "1.0.26"
115116
clru = "0.5.0"

git-repository/src/env.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,21 @@ pub fn os_str_to_bstring(input: &OsStr) -> Result<BString, String> {
2828

2929
/// Environment information involving the `git` program itself.
3030
pub mod git {
31-
use crate::bstr::{BStr, ByteSlice};
31+
use crate::bstr::{BStr, BString, ByteSlice};
32+
use std::process::{Command, Stdio};
33+
34+
/// Returns the file that contains git configuration coming with the installation of the `git` file in the current `PATH`, or `None`
35+
/// if no `git` executable was found or there were other errors during execution.
36+
pub fn install_config_path() -> Option<&'static BStr> {
37+
static PATH: once_cell::sync::Lazy<Option<BString>> = once_cell::sync::Lazy::new(|| {
38+
let mut cmd = Command::new(if cfg!(windows) { "git.exe" } else { "git" });
39+
cmd.args(["config", "-l", "--show-origin"])
40+
.stdin(Stdio::null())
41+
.stderr(Stdio::null());
42+
first_file_from_config_with_origin(cmd.output().ok()?.stdout.as_slice().into()).map(ToOwned::to_owned)
43+
});
44+
PATH.as_ref().map(|b| b.as_ref())
45+
}
3246

3347
fn first_file_from_config_with_origin(source: &BStr) -> Option<&BStr> {
3448
let file = source.strip_prefix(b"file:")?;

0 commit comments

Comments
 (0)