diff --git a/gst-plugin-version-helper/src/git.rs b/gst-plugin-version-helper/src/git.rs new file mode 100644 index 00000000..7e013113 --- /dev/null +++ b/gst-plugin-version-helper/src/git.rs @@ -0,0 +1,36 @@ +use std::path::Path; +use std::process::Command; + +pub fn repo_hash>(path: P) -> Option { + 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>(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, + } +}