Skip to content

Commit

Permalink
Parse git version
Browse files Browse the repository at this point in the history
  • Loading branch information
dandavison committed Aug 13, 2024
1 parent aae8943 commit aeeaaff
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/utils/git.rs
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);
}
}
1 change: 1 addition & 0 deletions src/utils/mod.rs
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;
Expand Down

0 comments on commit aeeaaff

Please sign in to comment.