Feeback on snippet of code to return git commit shas (string and array)


I am trying to implement two methods that outputs the git commit shas of a repository one as a string:


The other as an array:


The code is the following:

pub fn git_shas() -> Vec<u8> {
    let output = Command::new("git")
        .output().unwrap_or_else(|e| {
            panic!("failed to execute command: {}", e);


pub fn shas_string() -> String {
    let string_output = git_shas();
    let output = match String::from_utf8(string_output) {
        Ok(v) => v,
        Err(e) => panic!("Invalid Utf8 character: {}", e),


pub fn shas_slice() -> Vec<&'static str> {

Any feedback, things to try would be much appreciated.

The git2 crate exposes the SHA in the Commit struct, which might be more flexible that calling the command line tools.

1 Like