use serde::{Deserialize, Serialize};
use super::recipe::{Recipe, RuleKind, ScriptLanguage, SignaturePolicy};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Tool {
Gpg,
SshKeygen,
Gpgsm,
GitLfs,
Python,
Lua,
Bash,
Pwsh,
Sh,
}
impl Tool {
pub const ALL: [Tool; 9] = [
Tool::Gpg,
Tool::SshKeygen,
Tool::Gpgsm,
Tool::GitLfs,
Tool::Python,
Tool::Lua,
Tool::Bash,
Tool::Pwsh,
Tool::Sh,
];
pub fn name(self) -> &'static str {
match self {
Tool::Gpg => "gpg",
Tool::SshKeygen => "ssh-keygen",
Tool::Gpgsm => "gpgsm",
Tool::GitLfs => "git-lfs",
Tool::Python => "python",
Tool::Lua => "lua",
Tool::Bash => "bash",
Tool::Pwsh => "pwsh",
Tool::Sh => "sh",
}
}
pub fn env_suffix(self) -> &'static str {
match self {
Tool::Gpg => "GPG",
Tool::SshKeygen => "SSH_KEYGEN",
Tool::Gpgsm => "GPGSM",
Tool::GitLfs => "GIT_LFS",
Tool::Python => "PYTHON",
Tool::Lua => "LUA",
Tool::Bash => "BASH",
Tool::Pwsh => "PWSH",
Tool::Sh => "SH",
}
}
pub fn min_version(self) -> Option<(u32, u32)> {
match self {
Tool::GitLfs => Some((3, 0)),
Tool::Python => Some((3, 8)),
_ => None,
}
}
pub fn needed_for(self) -> Vec<RewriteFeature> {
match self {
Tool::Gpg | Tool::SshKeygen | Tool::Gpgsm => vec![RewriteFeature::ReSign],
Tool::GitLfs => vec![RewriteFeature::LfsMigration],
Tool::Python | Tool::Lua => vec![RewriteFeature::ExternalScripts],
Tool::Bash | Tool::Pwsh | Tool::Sh => vec![RewriteFeature::ShellScripts],
}
}
pub fn hint_key(self, os: OsFamily) -> &'static str {
use OsFamily::{Linux, MacOs, Windows};
match (self, os) {
(Tool::Gpg, Windows) => "tool_hint_gpg_windows",
(Tool::Gpg, Linux) => "tool_hint_gpg_linux",
(Tool::Gpg, MacOs) => "tool_hint_gpg_macos",
(Tool::SshKeygen, Windows) => "tool_hint_ssh_keygen_windows",
(Tool::SshKeygen, Linux) => "tool_hint_ssh_keygen_linux",
(Tool::SshKeygen, MacOs) => "tool_hint_ssh_keygen_macos",
(Tool::Gpgsm, Windows) => "tool_hint_gpgsm_windows",
(Tool::Gpgsm, Linux) => "tool_hint_gpgsm_linux",
(Tool::Gpgsm, MacOs) => "tool_hint_gpgsm_macos",
(Tool::GitLfs, Windows) => "tool_hint_git_lfs_windows",
(Tool::GitLfs, Linux) => "tool_hint_git_lfs_linux",
(Tool::GitLfs, MacOs) => "tool_hint_git_lfs_macos",
(Tool::Python, Windows) => "tool_hint_python_windows",
(Tool::Python, Linux) => "tool_hint_python_linux",
(Tool::Python, MacOs) => "tool_hint_python_macos",
(Tool::Lua, Windows) => "tool_hint_lua_windows",
(Tool::Lua, Linux) => "tool_hint_lua_linux",
(Tool::Lua, MacOs) => "tool_hint_lua_macos",
(Tool::Bash, Windows) => "tool_hint_bash_windows",
(Tool::Bash, Linux) => "tool_hint_bash_linux",
(Tool::Bash, MacOs) => "tool_hint_bash_macos",
(Tool::Pwsh, Windows) => "tool_hint_pwsh_windows",
(Tool::Pwsh, Linux) => "tool_hint_pwsh_linux",
(Tool::Pwsh, MacOs) => "tool_hint_pwsh_macos",
(Tool::Sh, Windows) => "tool_hint_sh_windows",
(Tool::Sh, Linux) => "tool_hint_sh_linux",
(Tool::Sh, MacOs) => "tool_hint_sh_macos",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum OsFamily {
Windows,
Linux,
MacOs,
}
impl OsFamily {
pub const fn current() -> Self {
if cfg!(windows) {
OsFamily::Windows
} else if cfg!(target_os = "macos") {
OsFamily::MacOs
} else {
OsFamily::Linux
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RewriteFeature {
ReSign,
LfsMigration,
ExternalScripts,
ShellScripts,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SigningFormat {
#[default]
OpenPgp,
Ssh,
X509,
}
impl SigningFormat {
pub fn from_config(value: Option<&str>) -> Self {
match value.map(|v| v.trim().to_ascii_lowercase()).as_deref() {
Some("ssh") => Self::Ssh,
Some("x509") => Self::X509,
_ => Self::OpenPgp,
}
}
pub fn tool(self) -> Tool {
match self {
Self::OpenPgp => Tool::Gpg,
Self::Ssh => Tool::SshKeygen,
Self::X509 => Tool::Gpgsm,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ToolRequirement {
Tool(Tool),
AnyOf(&'static [Tool]),
}
pub const SHELLS: &[Tool] = &[Tool::Bash, Tool::Pwsh, Tool::Sh];
impl ToolRequirement {
pub fn met_by(&self, tools: &RewriteTools) -> bool {
match self {
Self::Tool(t) => tools.is_usable(*t),
Self::AnyOf(any) => any.iter().any(|t| tools.is_usable(*t)),
}
}
pub fn candidates(&self) -> Vec<Tool> {
match self {
Self::Tool(t) => vec![*t],
Self::AnyOf(any) => any.to_vec(),
}
}
}
impl RuleKind {
pub fn required_tools(&self) -> Vec<ToolRequirement> {
match self {
Self::LfsImport(_) | Self::LfsExport(_) => vec![ToolRequirement::Tool(Tool::GitLfs)],
Self::Script(s) => match s.language {
ScriptLanguage::Lua => Vec::new(),
ScriptLanguage::Python => vec![ToolRequirement::Tool(Tool::Python)],
ScriptLanguage::LuaExternal => vec![ToolRequirement::Tool(Tool::Lua)],
ScriptLanguage::Shell => vec![ToolRequirement::AnyOf(SHELLS)],
},
_ => Vec::new(),
}
}
}
impl Recipe {
pub fn required_tools(&self, format: SigningFormat) -> Vec<ToolRequirement> {
let mut out: Vec<ToolRequirement> = Vec::new();
let mut push = |r: ToolRequirement| {
if !out.contains(&r) {
out.push(r);
}
};
for (_, rule) in self.enabled_rules() {
for r in rule.kind.required_tools() {
push(r);
}
}
if matches!(
self.options.signatures,
Some(SignaturePolicy::ResignSigned | SignaturePolicy::ResignAll)
) {
push(ToolRequirement::Tool(format.tool()));
}
out
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "status", rename_all = "snake_case")]
pub enum ToolStatus {
Installed {
version: String,
},
TooOld {
version: String,
min: String,
},
Missing,
NotConfigured {
reason: String,
},
}
impl ToolStatus {
pub fn is_usable(&self) -> bool {
matches!(self, ToolStatus::Installed { .. })
}
pub fn from_version(tool: Tool, version: &str) -> Self {
let version = version.to_string();
let Some((min_major, min_minor)) = tool.min_version() else {
return ToolStatus::Installed { version };
};
let mut nums = version.split('.').map(|n| n.parse::<u32>().unwrap_or(0));
let have = (nums.next().unwrap_or(0), nums.next().unwrap_or(0));
if have >= (min_major, min_minor) {
ToolStatus::Installed { version }
} else {
ToolStatus::TooOld {
version,
min: format!("{min_major}.{min_minor}"),
}
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ToolCheck {
pub tool: Tool,
pub needed_for: Vec<RewriteFeature>,
pub status: ToolStatus,
pub hint_key: String,
}
impl ToolCheck {
pub fn new(tool: Tool, status: ToolStatus, os: OsFamily) -> Self {
Self {
tool,
needed_for: tool.needed_for(),
status,
hint_key: tool.hint_key(os).to_string(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SigningCheck {
pub format: SigningFormat,
pub tool: Tool,
pub hint_key: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reason: Option<super::recipe::RewriteRefusal>,
#[serde(default)]
pub detail: String,
#[serde(default)]
pub millis: u64,
}
impl SigningCheck {
pub fn is_ok(&self) -> bool {
self.reason.is_none()
}
pub fn estimate_millis(&self, commits: u64) -> u64 {
self.millis.saturating_mul(commits)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct RewriteTools {
pub tools: Vec<ToolCheck>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub external_scripts: Option<ScriptSwitches>,
#[serde(default)]
pub signing_format: SigningFormat,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct ScriptSwitches {
pub shell: bool,
pub python: bool,
pub lua: bool,
}
impl ScriptSwitches {
pub fn allows(&self, language: ScriptLanguage) -> bool {
match language {
ScriptLanguage::Lua => true,
ScriptLanguage::Shell => self.shell,
ScriptLanguage::Python => self.python,
ScriptLanguage::LuaExternal => self.lua,
}
}
}
impl RewriteTools {
pub fn get(&self, tool: Tool) -> Option<&ToolCheck> {
self.tools.iter().find(|c| c.tool == tool)
}
pub fn is_usable(&self, tool: Tool) -> bool {
self.get(tool).is_some_and(|c| c.status.is_usable())
}
pub fn unmet(&self, needs: &[ToolRequirement]) -> Vec<ToolRequirement> {
needs.iter().filter(|r| !r.met_by(self)).copied().collect()
}
}
#[cfg(test)]
mod tests {
use super::super::recipe::{
LfsImportRule, Rule, RuleKind, ScriptRef, ScriptSource, TrailerRule,
};
use super::super::{Recipe, Scope};
use super::*;
use std::collections::BTreeMap;
fn script(language: ScriptLanguage) -> RuleKind {
RuleKind::Script(ScriptRef {
language,
source: ScriptSource::Inline { text: "x".into() },
params: BTreeMap::new(),
})
}
#[test]
fn metadata_and_content_rules_need_no_tool() {
let kinds = [
RuleKind::Mailmap {
apply_to: Default::default(),
file: None,
},
RuleKind::CommitterFromAuthor,
RuleKind::DateShift {
by_seconds: 60,
apply_to: Default::default(),
},
RuleKind::RemovePaths {
globs: vec!["a".into()],
},
RuleKind::RemoveBlobsOver { bytes: 10 },
RuleKind::Trailer(TrailerRule::add("K", "v")),
script(ScriptLanguage::Lua),
];
for kind in kinds {
assert!(kind.required_tools().is_empty(), "{}", kind.name());
}
}
#[test]
fn lfs_external_and_shell_name_their_tools() {
let lfs = RuleKind::LfsImport(LfsImportRule {
patterns: vec!["*.psd".into()],
min_size: None,
});
assert_eq!(
lfs.required_tools(),
vec![ToolRequirement::Tool(Tool::GitLfs)]
);
assert_eq!(
script(ScriptLanguage::Python).required_tools(),
vec![ToolRequirement::Tool(Tool::Python)]
);
assert_eq!(
script(ScriptLanguage::LuaExternal).required_tools(),
vec![ToolRequirement::Tool(Tool::Lua)]
);
assert!(script(ScriptLanguage::Lua).required_tools().is_empty());
assert_eq!(
script(ScriptLanguage::Shell).required_tools(),
vec![ToolRequirement::AnyOf(SHELLS)]
);
}
#[test]
fn a_recipe_needs_the_signing_tool_only_when_it_re_signs() {
let mut recipe = Recipe::new(
"t",
Scope::default(),
vec![Rule::new(RuleKind::CommitterFromAuthor)],
);
assert!(recipe.required_tools(SigningFormat::Ssh).is_empty());
recipe.options.signatures = Some(SignaturePolicy::Strip);
assert!(recipe.required_tools(SigningFormat::Ssh).is_empty());
recipe.options.signatures = Some(SignaturePolicy::ResignAll);
assert_eq!(
recipe.required_tools(SigningFormat::Ssh),
vec![ToolRequirement::Tool(Tool::SshKeygen)]
);
assert_eq!(
recipe.required_tools(SigningFormat::X509),
vec![ToolRequirement::Tool(Tool::Gpgsm)]
);
assert_eq!(
recipe.required_tools(SigningFormat::from_config(None)),
vec![ToolRequirement::Tool(Tool::Gpg)]
);
}
#[test]
fn a_disabled_rule_needs_nothing() {
let mut rule = Rule::new(script(ScriptLanguage::Shell));
rule.enabled = false;
let recipe = Recipe::new("t", Scope::default(), vec![rule]);
assert!(recipe.required_tools(SigningFormat::OpenPgp).is_empty());
}
#[test]
fn versions_are_classified_against_the_minimum() {
assert!(ToolStatus::from_version(Tool::GitLfs, "3.5.1").is_usable());
assert!(matches!(
ToolStatus::from_version(Tool::GitLfs, "2.13.3"),
ToolStatus::TooOld { .. }
));
assert!(ToolStatus::from_version(Tool::Gpg, "1.0").is_usable());
}
#[test]
fn any_of_is_met_by_one_usable_shell() {
let tools = RewriteTools {
external_scripts: None,
signing_format: SigningFormat::default(),
tools: vec![
ToolCheck::new(Tool::Bash, ToolStatus::Missing, OsFamily::Linux),
ToolCheck::new(
Tool::Sh,
ToolStatus::Installed { version: "".into() },
OsFamily::Linux,
),
],
};
let need = ToolRequirement::AnyOf(SHELLS);
assert!(need.met_by(&tools));
assert!(tools.unmet(&[need]).is_empty());
assert_eq!(tools.unmet(&[ToolRequirement::Tool(Tool::Gpg)]).len(), 1);
}
#[test]
fn every_tool_has_a_hint_key_per_os_and_a_feature() {
for tool in Tool::ALL {
assert!(!tool.needed_for().is_empty());
for os in [OsFamily::Windows, OsFamily::Linux, OsFamily::MacOs] {
assert!(tool.hint_key(os).starts_with("tool_hint_"));
}
}
}
}