-
Notifications
You must be signed in to change notification settings - Fork 396
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aae8943
commit aeeaaff
Showing
2 changed files
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use std::process::Command; | ||
|
||
pub fn retrieve_git_version() -> Option<(usize, usize)> { | ||
if let Ok(git_path) = grep_cli::resolve_binary("git") { | ||
let cmd = Command::new(git_path).arg("--version").output().ok()?; | ||
parse_git_version(&cmd.stdout) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn parse_git_version(output: &[u8]) -> Option<(usize, usize)> { | ||
let mut parts = output.strip_prefix(b"git version ")?.split(|&b| b == b'.'); | ||
let major = std::str::from_utf8(parts.next()?).ok()?.parse().ok()?; | ||
let minor = std::str::from_utf8(parts.next()?).ok()?.parse().ok()?; | ||
Some((major, minor)) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::parse_git_version; | ||
use rstest::rstest; | ||
|
||
#[rstest] | ||
#[case(b"git version 2.46.0", Some((2, 46)))] | ||
#[case(b"git version 2.46.0 | ||
", Some((2, 46)))] | ||
#[case(b"", None)] | ||
fn test_parse_git_version(#[case] input: &[u8], #[case] expected: Option<(usize, usize)>) { | ||
assert_eq!(parse_git_version(input), expected); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#[cfg(not(tarpaulin_include))] | ||
pub mod bat; | ||
pub mod git; | ||
pub mod helpwrap; | ||
pub mod path; | ||
pub mod process; | ||
|