#[cfg(feature = "ast-grep")]
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use anyhow::{Context, Result};
#[cfg(feature = "ast-grep")]
use ast_grep_config::{GlobalRules, RuleCollection, RuleConfig};
#[cfg(feature = "ast-grep")]
use ast_grep_language::{LanguageExt, SupportLang};
use gix::Repository;
use gix::bstr::ByteSlice;
use gix::diff::index::ChangeRef;
use indexmap::IndexSet;
use regex::Regex;
use crate::config::Config;
struct StagedChange {
path: PathBuf,
#[cfg_attr(
not(feature = "ast-grep"),
expect(dead_code, reason = "only read by ast-grep detection")
)]
id: gix::ObjectId,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ScopeMatches {
pub matches: Vec<String>,
}
impl ScopeMatches {
pub fn suggested(&self) -> Option<&str> {
match self.matches.as_slice() {
[only] => Some(only),
_ => None,
}
}
}
#[cfg(feature = "ast-grep")]
struct CompiledAstGrepRules {
rules: RuleCollection<SupportLang>,
ids_to_scope: HashMap<String, String>,
}
impl Config {
fn compile_scope_patterns(&self) -> Result<Vec<(&str, Vec<Regex>)>> {
self.commit_scopes
.iter()
.filter(|(_, scope)| !scope.patterns.is_empty())
.map(|(name, scope)| {
let regexes = scope
.patterns
.iter()
.map(|pattern| {
Regex::new(pattern).with_context(|| {
format!("invalid scope path regex for `{name}`: `{pattern}`")
})
})
.collect::<Result<Vec<_>>>()?;
Ok((name.as_str(), regexes))
})
.collect()
}
pub fn validate_scope_patterns(&self) -> Result<()> {
self.compile_scope_patterns().map(|_| ())
}
#[cfg(feature = "ast-grep")]
pub fn validate_ast_grep_rules(&self) -> Result<()> {
self.compile_ast_grep_rules().map(|_| ())
}
#[cfg(feature = "ast-grep")]
fn compile_ast_grep_rules(&self) -> Result<CompiledAstGrepRules> {
let globals = GlobalRules::default();
let mut ids_to_scope = HashMap::new();
let mut compiled_rules = Vec::new();
for (index, (scope_name, scope)) in self.commit_scopes.iter().enumerate() {
let Some(ast_grep) = &scope.ast_grep else {
continue;
};
let mut serializable = ast_grep.clone();
let id = if serializable.id.is_empty() {
format!("{index:04}-{scope_name}")
} else {
format!("{index:04}-{}", serializable.id)
};
serializable.id = id.clone();
ids_to_scope.insert(id, scope_name.clone());
compiled_rules.push(RuleConfig::try_from(serializable, &globals)?);
}
Ok(CompiledAstGrepRules {
rules: RuleCollection::try_new(compiled_rules)?,
ids_to_scope,
})
}
}
pub fn detect_scope_matches(repo: &Repository, config: &Config) -> Result<ScopeMatches> {
let changed = staged_changes(repo)?;
if changed.is_empty() {
return Ok(ScopeMatches::default());
}
let compiled_patterns = config.compile_scope_patterns()?;
let pattern_scopes = changed
.iter()
.map(|change| normalize_relative_path(&change.path))
.flat_map(|path| {
compiled_patterns
.iter()
.filter(move |(_, regexes)| regexes.iter().any(|re| re.is_match(&path)))
.map(|(scope_name, _)| scope_name.to_string())
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
#[cfg(feature = "ast-grep")]
let ast_grep_scopes = detect_ast_grep_scopes(repo, config, &changed)?;
#[cfg(not(feature = "ast-grep"))]
let ast_grep_scopes = Vec::new();
let matched_scopes: IndexSet<String> =
pattern_scopes.into_iter().chain(ast_grep_scopes).collect();
Ok(ScopeMatches {
matches: matched_scopes.into_iter().collect(),
})
}
fn staged_changes(repo: &Repository) -> Result<Vec<StagedChange>> {
let index = repo.index_or_empty().context("could not read index")?;
let head_tree_id = repo
.head_tree_id_or_empty()
.context("could not resolve HEAD tree")?;
let mut changes: IndexSet<(PathBuf, gix::ObjectId)> = IndexSet::new();
let mut record = |location: &gix::bstr::BStr, id: gix::ObjectId| {
changes.insert((PathBuf::from(location.to_str_lossy().into_owned()), id));
};
repo.tree_index_status(
&head_tree_id,
&index,
None,
gix::status::tree_index::TrackRenames::Disabled,
|change, _, _| {
match change {
ChangeRef::Addition { location, id, .. }
| ChangeRef::Deletion { location, id, .. }
| ChangeRef::Modification { location, id, .. } => {
record(&location, id.into_owned());
}
ChangeRef::Rewrite {
source_location,
source_id,
location,
id,
..
} => {
record(&source_location, source_id.into_owned());
record(&location, id.into_owned());
}
}
Ok(gix::diff::index::Action::Continue(()))
},
)
.context("could not diff HEAD tree against index")?;
Ok(changes
.into_iter()
.map(|(path, id)| StagedChange { path, id })
.collect())
}
fn normalize_relative_path(path: &Path) -> String {
let path = path.to_string_lossy().replace('\\', "/");
if path.starts_with('/') {
path
} else {
format!("/{path}")
}
}
#[cfg(feature = "ast-grep")]
fn detect_ast_grep_scopes(
repo: &Repository,
config: &Config,
changed: &[StagedChange],
) -> Result<Vec<String>> {
if config.commit_scopes.values().all(|s| s.ast_grep.is_none()) {
return Ok(Vec::new());
}
let compiled_rules = config.compile_ast_grep_rules()?;
let matched_scopes: IndexSet<String> = changed
.iter()
.flat_map(|change| {
let applicable_rules = compiled_rules.rules.for_path(&change.path);
if applicable_rules.is_empty() {
return Vec::new();
}
let Ok(blob) = repo.find_blob(change.id) else {
return Vec::new();
};
let Ok(source) = std::str::from_utf8(&blob.data) else {
return Vec::new();
};
applicable_rules
.into_iter()
.filter(|rule| {
rule.language
.ast_grep(source)
.root()
.find(&rule.matcher)
.is_some()
})
.filter_map(|rule| compiled_rules.ids_to_scope.get(&rule.id).cloned())
.collect()
})
.collect();
Ok(matched_scopes.into_iter().collect())
}
#[cfg(test)]
mod tests {
use std::error::Error;
use indexmap::IndexMap;
use super::*;
use crate::config::CommitScope;
fn empty_config(workdir: PathBuf) -> Config {
Config {
autocomplete: false,
breaking_changes: false,
commit_types: IndexMap::new(),
commit_scopes: IndexMap::new(),
emoji: false,
issues: false,
sign: false,
force_config_scopes: false,
allow_empty_scope: true,
workdir,
}
}
#[test]
fn test_suggested_requires_exactly_one_match() {
let matches = |names: &[&str]| ScopeMatches {
matches: names.iter().map(|name| name.to_string()).collect(),
};
assert_eq!(matches(&[]).suggested(), None);
assert_eq!(matches(&["core"]).suggested(), Some("core"));
assert_eq!(matches(&["core", "build"]).suggested(), None);
}
#[test]
fn test_normalize_relative_path() {
assert_eq!(
normalize_relative_path(Path::new("src/lib.rs")),
"/src/lib.rs"
);
assert_eq!(
normalize_relative_path(Path::new("/src/lib.rs")),
"/src/lib.rs"
);
}
fn config_with_patterns(patterns: Vec<String>) -> Config {
let mut config = empty_config(PathBuf::from("."));
config.commit_scopes.insert(
"core".into(),
CommitScope {
name: "core".into(),
description: None,
patterns,
#[cfg(feature = "ast-grep")]
ast_grep: None,
},
);
config
}
#[test]
fn test_validate_scope_patterns_accepts_valid_regexes() {
let config = config_with_patterns(vec![
"^/crates/core/.*\\.rs$".into(),
"^/src/.*\\.rs$".into(),
]);
assert!(config.validate_scope_patterns().is_ok());
}
#[test]
fn test_validate_scope_patterns_rejects_invalid_regex() {
let config = config_with_patterns(vec!["^/src/(".into()]);
let err = config.validate_scope_patterns().unwrap_err();
assert!(err.to_string().contains("core"), "error names the scope");
}
#[test]
fn test_detect_scope_matches_from_staged_paths() -> Result<(), Box<dyn Error>> {
let tempdir = tempfile::tempdir()?;
let repo = git2::Repository::init(tempdir.path())?;
let mut index = repo.index()?;
let tree_id = index.write_tree()?;
let tree = repo.find_tree(tree_id)?;
let sig = git2::Signature::now("Tester", "test@example.com")?;
repo.commit(Some("HEAD"), &sig, &sig, "chore: initial", &tree, &[])?;
let gix_repo = gix::discover(tempdir.path())?;
std::fs::create_dir_all(tempdir.path().join("crates/core/src"))?;
std::fs::write(
tempdir.path().join("crates/core/src/lib.rs"),
"pub fn core() {}\n",
)?;
let mut index = repo.index()?;
index.add_path(Path::new("crates/core/src/lib.rs"))?;
index.write()?;
let mut config = empty_config(tempdir.path().to_path_buf());
config.commit_scopes.insert(
"core".into(),
CommitScope {
name: "core".into(),
description: None,
patterns: vec!["^/crates/core/.*\\.rs$".into()],
#[cfg(feature = "ast-grep")]
ast_grep: None,
},
);
let matches = detect_scope_matches(&gix_repo, &config)?;
assert_eq!(matches.suggested(), Some("core"));
tempdir.close()?;
Ok(())
}
#[cfg(feature = "ast-grep")]
#[test]
fn test_detect_scope_matches_from_ast_grep_rules() -> Result<(), Box<dyn Error>> {
let tempdir = tempfile::tempdir()?;
let repo = git2::Repository::init(tempdir.path())?;
let gix_repo = gix::discover(tempdir.path())?;
std::fs::create_dir_all(tempdir.path().join("src"))?;
std::fs::write(
tempdir.path().join("src/lib.rs"),
"#[test]\nfn detects_test_scope() {}\n",
)?;
let mut index = repo.index()?;
index.add_path(Path::new("src/lib.rs"))?;
index.write()?;
let mut config = empty_config(tempdir.path().to_path_buf());
config
.commit_scopes
.insert("test".into(), function_scope("test", None));
let matches = detect_scope_matches(&gix_repo, &config)?;
assert_eq!(matches.suggested(), Some("test"));
tempdir.close()?;
Ok(())
}
#[cfg(feature = "ast-grep")]
fn function_scope(name: &str, file_globs: Option<Vec<String>>) -> CommitScope {
let files_yaml = match &file_globs {
Some(globs) => {
let items: Vec<String> = globs.iter().map(|g| format!(" - '{g}'")).collect();
format!("files:\n{}\n", items.join("\n"))
}
None => String::new(),
};
let yaml = format!(
"id: test-rule\nlanguage: Rust\nrule:\n pattern: fn $NAME() {{}}\n{files_yaml}"
);
let ast_grep = ast_grep_config::from_str::<
ast_grep_config::SerializableRuleConfig<SupportLang>,
>(&yaml)
.unwrap();
CommitScope {
name: name.into(),
description: None,
patterns: vec![],
ast_grep: Some(ast_grep),
}
}
#[cfg(feature = "ast-grep")]
#[test]
fn test_ast_grep_matches_staged_blob_not_worktree() -> Result<(), Box<dyn Error>> {
let tempdir = tempfile::tempdir()?;
let repo = git2::Repository::init(tempdir.path())?;
let gix_repo = gix::discover(tempdir.path())?;
std::fs::create_dir_all(tempdir.path().join("src"))?;
std::fs::write(tempdir.path().join("src/lib.rs"), "fn matches() {}\n")?;
let mut index = repo.index()?;
index.add_path(Path::new("src/lib.rs"))?;
index.write()?;
std::fs::write(tempdir.path().join("src/lib.rs"), "no match here\n")?;
let mut config = empty_config(tempdir.path().to_path_buf());
config
.commit_scopes
.insert("test".into(), function_scope("test", None));
let matches = detect_scope_matches(&gix_repo, &config)?;
assert_eq!(
matches.suggested(),
Some("test"),
"should match on staged content even though the worktree copy no longer matches"
);
tempdir.close()?;
Ok(())
}
#[cfg(feature = "ast-grep")]
#[test]
fn test_ast_grep_ignores_unstaged_worktree_edits() -> Result<(), Box<dyn Error>> {
let tempdir = tempfile::tempdir()?;
let repo = git2::Repository::init(tempdir.path())?;
let gix_repo = gix::discover(tempdir.path())?;
std::fs::create_dir_all(tempdir.path().join("src"))?;
std::fs::write(tempdir.path().join("src/lib.rs"), "no match here\n")?;
let mut index = repo.index()?;
index.add_path(Path::new("src/lib.rs"))?;
index.write()?;
std::fs::write(tempdir.path().join("src/lib.rs"), "fn matches() {}\n")?;
let mut config = empty_config(tempdir.path().to_path_buf());
config
.commit_scopes
.insert("test".into(), function_scope("test", None));
let matches = detect_scope_matches(&gix_repo, &config)?;
assert_eq!(
matches.suggested(),
None,
"unstaged worktree edits must not influence scope detection"
);
tempdir.close()?;
Ok(())
}
#[cfg(feature = "ast-grep")]
#[test]
fn test_ast_grep_matches_renamed_file_old_path_filter() -> Result<(), Box<dyn Error>> {
let tempdir = tempfile::tempdir()?;
let repo = git2::Repository::init(tempdir.path())?;
std::fs::create_dir_all(tempdir.path().join("tests"))?;
std::fs::write(tempdir.path().join("tests/a.rs"), "fn matches() {}\n")?;
let mut index = repo.index()?;
index.add_path(Path::new("tests/a.rs"))?;
let tree_id = index.write_tree()?;
index.write()?;
let tree = repo.find_tree(tree_id)?;
let sig = git2::Signature::now("Tester", "test@example.com")?;
repo.commit(Some("HEAD"), &sig, &sig, "chore: initial", &tree, &[])?;
std::fs::create_dir_all(tempdir.path().join("src"))?;
std::fs::rename(
tempdir.path().join("tests/a.rs"),
tempdir.path().join("src/a.rs"),
)?;
let mut index = repo.index()?;
index.remove_path(Path::new("tests/a.rs"))?;
index.add_path(Path::new("src/a.rs"))?;
index.write()?;
let gix_repo = gix::discover(tempdir.path())?;
let mut config = empty_config(tempdir.path().to_path_buf());
config.commit_scopes.insert(
"test".into(),
function_scope("test", Some(vec!["tests/**".into()])),
);
let matches = detect_scope_matches(&gix_repo, &config)?;
assert_eq!(
matches.suggested(),
Some("test"),
"a rule filtered on the old path should still match the renamed file's staged content"
);
tempdir.close()?;
Ok(())
}
#[cfg(feature = "ast-grep")]
#[test]
fn test_ast_grep_matches_staged_deletion() -> Result<(), Box<dyn Error>> {
let tempdir = tempfile::tempdir()?;
let repo = git2::Repository::init(tempdir.path())?;
std::fs::create_dir_all(tempdir.path().join("src"))?;
std::fs::write(tempdir.path().join("src/lib.rs"), "fn matches() {}\n")?;
let mut index = repo.index()?;
index.add_path(Path::new("src/lib.rs"))?;
let tree_id = index.write_tree()?;
index.write()?;
let tree = repo.find_tree(tree_id)?;
let sig = git2::Signature::now("Tester", "test@example.com")?;
repo.commit(Some("HEAD"), &sig, &sig, "chore: initial", &tree, &[])?;
std::fs::remove_file(tempdir.path().join("src/lib.rs"))?;
let mut index = repo.index()?;
index.remove_path(Path::new("src/lib.rs"))?;
index.write()?;
let gix_repo = gix::discover(tempdir.path())?;
let mut config = empty_config(tempdir.path().to_path_buf());
config
.commit_scopes
.insert("test".into(), function_scope("test", None));
let matches = detect_scope_matches(&gix_repo, &config)?;
assert_eq!(
matches.suggested(),
Some("test"),
"a staged deletion should still match against the removed content"
);
tempdir.close()?;
Ok(())
}
}