2022-01-15 19:18:47 +00:00
|
|
|
// Copyright (C) 2020 Guillaume Desmottes <guillaume.desmottes@collabora.com>
|
|
|
|
//
|
|
|
|
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
2020-04-03 10:14:43 +00:00
|
|
|
use std::path::Path;
|
|
|
|
use std::process::Command;
|
|
|
|
|
2020-04-03 11:15:34 +00:00
|
|
|
pub fn repo_hash<P: AsRef<Path>>(path: P) -> Option<(String, String)> {
|
2020-04-03 10:14:43 +00:00
|
|
|
let git_path = path.as_ref().to_str();
|
|
|
|
let mut args = match git_path {
|
|
|
|
Some(path) => vec!["-C", path],
|
|
|
|
None => vec![],
|
|
|
|
};
|
2022-11-01 08:27:48 +00:00
|
|
|
args.extend(["log", "-1", "--format=%h_%cd", "--date=short"]);
|
2020-04-03 10:14:43 +00:00
|
|
|
let output = Command::new("git").args(&args).output().ok()?;
|
|
|
|
if !output.status.success() {
|
|
|
|
return None;
|
|
|
|
}
|
2020-04-03 11:15:34 +00:00
|
|
|
let output = String::from_utf8(output.stdout).ok()?;
|
|
|
|
let output = output.trim_end_matches('\n');
|
|
|
|
let mut s = output.split('_');
|
|
|
|
let hash = s.next()?;
|
|
|
|
let date = s.next()?;
|
2020-04-03 10:14:43 +00:00
|
|
|
|
2020-04-03 11:15:34 +00:00
|
|
|
let hash = if dirty(path) {
|
2023-01-25 08:23:46 +00:00
|
|
|
format!("{hash}+")
|
2020-04-03 10:14:43 +00:00
|
|
|
} else {
|
2020-04-03 11:15:34 +00:00
|
|
|
hash.into()
|
|
|
|
};
|
|
|
|
|
|
|
|
Some((hash, date.into()))
|
2020-04-03 10:14:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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![],
|
|
|
|
};
|
2022-11-01 08:27:48 +00:00
|
|
|
args.extend(["ls-files", "-m"]);
|
2020-04-03 10:14:43 +00:00
|
|
|
match Command::new("git").args(&args).output() {
|
|
|
|
Ok(modified_files) => !modified_files.stdout.is_empty(),
|
|
|
|
Err(_) => false,
|
|
|
|
}
|
|
|
|
}
|