version-helper: copy git.rs from gtk-rs/gir

Bare copy from https://github.com/gtk-rs/gir/blob/master/src/git.rs
This commit is contained in:
Guillaume Desmottes 2020-04-03 12:14:43 +02:00 committed by Sebastian Dröge
parent 9ec6a02e13
commit bbdf90d67c

View file

@ -0,0 +1,36 @@
use std::path::Path;
use std::process::Command;
pub fn repo_hash<P: AsRef<Path>>(path: P) -> Option<String> {
let git_path = path.as_ref().to_str();
let mut args = match git_path {
Some(path) => vec!["-C", path],
None => vec![],
};
args.extend(&["rev-parse", "--short", "HEAD"]);
let output = Command::new("git").args(&args).output().ok()?;
if !output.status.success() {
return None;
}
let hash = String::from_utf8(output.stdout).ok()?;
let hash = hash.trim_end_matches('\n');
if dirty(path) {
Some(format!("{}+", hash))
} else {
Some(hash.into())
}
}
fn dirty<P: AsRef<Path>>(path: P) -> bool {
let path = path.as_ref().to_str();
let mut args = match path {
Some(path) => vec!["-C", path],
None => vec![],
};
args.extend(&["ls-files", "-m"]);
match Command::new("git").args(&args).output() {
Ok(modified_files) => !modified_files.stdout.is_empty(),
Err(_) => false,
}
}