From bbdf90d67c812eb65e342f0b1b607bf921559503 Mon Sep 17 00:00:00 2001 From: Guillaume Desmottes Date: Fri, 3 Apr 2020 12:14:43 +0200 Subject: [PATCH] version-helper: copy git.rs from gtk-rs/gir Bare copy from https://github.com/gtk-rs/gir/blob/master/src/git.rs --- gst-plugin-version-helper/src/git.rs | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 gst-plugin-version-helper/src/git.rs 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, + } +}