Skip to content

Commit cc78473

Browse files
committed
first step towards efficiently obtaining git config information (#450)
1 parent 8e07784 commit cc78473

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

git-repository/src/env.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,53 @@ pub fn args_os() -> impl Iterator<Item = OsString> {
1919
})
2020
}
2121

22-
/// Convert the given `input` into a `BString`, useful as `parse(try_from_os_str = <me>)` function.
22+
/// Convert the given `input` into a `BString`, useful as `#[clap(parse(try_from_os_str = git::env::os_str_to_bstring))]` function.
2323
pub fn os_str_to_bstring(input: &OsStr) -> Result<BString, String> {
2424
Vec::from_os_string(input.into())
2525
.map(Into::into)
2626
.map_err(|_| input.to_string_lossy().into_owned())
2727
}
28+
29+
/// Environment information involving the `git` program itself.
30+
pub mod git {
31+
use crate::bstr::{BStr, ByteSlice};
32+
33+
fn first_file_from_config_with_origin(source: &BStr) -> Option<&BStr> {
34+
let file = source.strip_prefix(b"file:")?;
35+
let end_pos = file.find_byte(b'\t')?;
36+
file[..end_pos].as_bstr().into()
37+
}
38+
39+
#[cfg(test)]
40+
mod tests {
41+
use crate::env::git;
42+
43+
#[test]
44+
fn first_file_from_config_with_origin() {
45+
let macos = "file:/Applications/Xcode.app/Contents/Developer/usr/share/git-core/gitconfig credential.helper=osxkeychain\nfile:/Users/byron/.gitconfig push.default=simple\n";
46+
let win_msys =
47+
"file:C:/git-sdk-64/etc/gitconfig core.symlinks=false\r\nfile:C:/git-sdk-64/etc/gitconfig core.autocrlf=true";
48+
let win_cmd = "file:C:/Program Files/Git/etc/gitconfig diff.astextplain.textconv=astextplain\r\nfile:C:/Program Files/Git/etc/gitconfig filter.lfs.clean=git-lfs clean -- %f\r\n";
49+
let linux = "file:/home/parallels/.gitconfig core.excludesfile=~/.gitignore\n";
50+
let bogus = "something unexpected";
51+
let empty = "";
52+
53+
for (source, expected) in [
54+
(
55+
macos,
56+
Some("/Applications/Xcode.app/Contents/Developer/usr/share/git-core/gitconfig"),
57+
),
58+
(win_msys, Some("C:/git-sdk-64/etc/gitconfig")),
59+
(win_cmd, Some("C:/Program Files/Git/etc/gitconfig")),
60+
(linux, Some("/home/parallels/.gitconfig")),
61+
(bogus, None),
62+
(empty, None),
63+
] {
64+
assert_eq!(
65+
git::first_file_from_config_with_origin(source.into()),
66+
expected.map(Into::into)
67+
);
68+
}
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)