@@ -19,9 +19,53 @@ pub fn args_os() -> impl Iterator<Item = OsString> {
19
19
} )
20
20
}
21
21
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.
23
23
pub fn os_str_to_bstring ( input : & OsStr ) -> Result < BString , String > {
24
24
Vec :: from_os_string ( input. into ( ) )
25
25
. map ( Into :: into)
26
26
. map_err ( |_| input. to_string_lossy ( ) . into_owned ( ) )
27
27
}
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\n file:/Users/byron/.gitconfig push.default=simple\n " ;
46
+ let win_msys =
47
+ "file:C:/git-sdk-64/etc/gitconfig core.symlinks=false\r \n file: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 \n file: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