fix: resolve module structure issues by separating CLI and git modules
Some checks failed
CI / build (push) Has been cancelled
Some checks failed
CI / build (push) Has been cancelled
- Removed duplicated git module code from cli.rs - Created dedicated git.rs module with GitRepo, StagedChanges, ChangedFile, FileStatus definitions - Updated all modules (analyzer, generator, prompt) to import from super::git - Fixed module organization to resolve compilation errors
This commit is contained in:
356
src/analyzer.rs
356
src/analyzer.rs
@@ -1,3 +1,7 @@
|
|||||||
|
use crate::git::{ChangedFile, FileStatus, StagedChanges};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub enum CommitType {
|
pub enum CommitType {
|
||||||
Feat,
|
Feat,
|
||||||
Fix,
|
Fix,
|
||||||
@@ -28,6 +32,7 @@ impl std::fmt::Display for CommitType {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct AnalysisResult {
|
pub struct AnalysisResult {
|
||||||
pub commit_type: CommitType,
|
pub commit_type: CommitType,
|
||||||
pub scope: Option<String>,
|
pub scope: Option<String>,
|
||||||
@@ -36,178 +41,303 @@ pub struct AnalysisResult {
|
|||||||
pub reasons: Vec<String>,
|
pub reasons: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn analyze_changes(staged: &super::git::StagedChanges) -> AnalysisResult {
|
const CONVENTIONAL_TYPES: [CommitType; 10] = [
|
||||||
let files: Vec<&super::git::ChangedFile> = staged.files.iter().collect();
|
CommitType::Feat,
|
||||||
|
CommitType::Fix,
|
||||||
|
CommitType::Docs,
|
||||||
|
CommitType::Style,
|
||||||
|
CommitType::Refactor,
|
||||||
|
CommitType::Test,
|
||||||
|
CommitType::Chore,
|
||||||
|
CommitType::Build,
|
||||||
|
CommitType::Ci,
|
||||||
|
CommitType::Perf,
|
||||||
|
];
|
||||||
|
|
||||||
if files.is_empty() {
|
pub fn analyze_changes(staged: &StagedChanges) -> AnalysisResult {
|
||||||
|
if staged.files.is_empty() {
|
||||||
return AnalysisResult {
|
return AnalysisResult {
|
||||||
commit_type: CommitType::Chore,
|
commit_type: CommitType::Chore,
|
||||||
scope: None,
|
scope: None,
|
||||||
confidence: 1.0,
|
confidence: 1.0,
|
||||||
description: "empty commit".to_string(),
|
description: String::from("empty commit"),
|
||||||
reasons: vec!["No files changed".to_string()],
|
reasons: vec![String::from("No staged changes detected")],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut type_scores: std::collections::HashMap<CommitType, (f64, Vec<String>)> = std::collections::HashMap::new();
|
let mut type_scores: HashMap<CommitType, (f64, Vec<String>)> = HashMap::new();
|
||||||
|
let mut scopes: HashMap<String, usize> = HashMap::new();
|
||||||
|
let mut all_new_files = true;
|
||||||
|
|
||||||
for file in &files {
|
for file in &staged.files {
|
||||||
let path = std::path::Path::new(&file.path);
|
all_new_files = all_new_files && file.is_new;
|
||||||
let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
|
|
||||||
let file_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
|
|
||||||
|
|
||||||
let (type_, reason) = classify_file(&file.path, &file.status, ext, file_name);
|
let (file_type, reasons) = classify_file(file);
|
||||||
type_scores
|
let entry = type_scores.entry(file_type).or_insert((0.0, Vec::new()));
|
||||||
.entry(type_)
|
entry.0 += 1.0;
|
||||||
.or_insert((0.0, Vec::new()))
|
entry.1.extend(reasons);
|
||||||
.0 += 1.0;
|
|
||||||
type_scores.get_mut(&type_).unwrap().1.push(reason);
|
if let Some(scope) = extract_scope(&file.path) {
|
||||||
|
*scopes.entry(scope).or_insert(0) += 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let (best_type, (max_score, reasons)) = type_scores
|
let dominant_type = type_scores
|
||||||
.into_iter()
|
.iter()
|
||||||
.max_by(|(a, (score_a, _)), (b, (score_b, _))| {
|
.max_by(|a, b| a.1 .0.partial_cmp(&b.1 .0).unwrap())
|
||||||
score_a.partial_cmp(&score_b).unwrap_or(std::cmp::Ordering::Equal)
|
.map(|(t, _)| *t)
|
||||||
})
|
.unwrap_or(CommitType::Chore);
|
||||||
.unwrap_or((CommitType::Chore, (1.0, vec!["default".to()])));
|
|
||||||
|
|
||||||
let scope = extract_scope(&files.first().unwrap().path);
|
let dominant_scope = scopes
|
||||||
let description = generate_description(&files);
|
.iter()
|
||||||
|
.max_by_key(|(_, count)| *count)
|
||||||
|
.map(|(scope, _)| scope.clone());
|
||||||
|
|
||||||
let confidence = (max_score as usize) as f64 / files.len() as f64;
|
let total_files = staged.files.len() as f64;
|
||||||
|
let confidence = type_scores
|
||||||
|
.get(&dominant_type)
|
||||||
|
.map(|(score, _)| *score / total_files)
|
||||||
|
.unwrap_or(0.5);
|
||||||
|
|
||||||
|
let description = generate_description(&staged.files);
|
||||||
|
|
||||||
|
let reasons = type_scores
|
||||||
|
.get(&dominant_type)
|
||||||
|
.map(|(_, r)| r.clone())
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
AnalysisResult {
|
AnalysisResult {
|
||||||
commit_type: best_type,
|
commit_type: dominant_type,
|
||||||
scope,
|
scope: dominant_scope,
|
||||||
confidence,
|
confidence,
|
||||||
description,
|
description,
|
||||||
reasons,
|
reasons,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn classify_file(
|
fn classify_file(file: &ChangedFile) -> (CommitType, Vec<String>) {
|
||||||
path: &str,
|
let path = &file.path;
|
||||||
status: &super::git::FileStatus,
|
let mut reasons = Vec::new();
|
||||||
ext: &str,
|
let mut score = 0.0;
|
||||||
file_name: &str,
|
|
||||||
) -> (CommitType, String) {
|
let ext = path.split('.').last().unwrap_or("").to_lowercase();
|
||||||
if ext == "md" || path.to_lowercase().contains("readme") || path.to_lowercase().contains("changelog") {
|
let path_lower = path.to_lowercase();
|
||||||
return (CommitType::Docs, format!("Documentation file: {}", path));
|
|
||||||
|
if path_lower.starts_with("tests/")
|
||||||
|
|| path_lower.ends_with("_test.rs")
|
||||||
|
|| path_lower.ends_with("_tests.rs")
|
||||||
|
{
|
||||||
|
reasons.push(format!("Test file detected: {}", path));
|
||||||
|
return (CommitType::Test, reasons);
|
||||||
}
|
}
|
||||||
|
|
||||||
if path.contains("test") || path.contains("_test") || ext == "test" {
|
if path_lower.starts_with("docs/") || path_lower.ends_with(".md") {
|
||||||
return (CommitType::Test, format!("Test file: {}", path));
|
reasons.push(format!("Documentation file detected: {}", path));
|
||||||
|
return (CommitType::Docs, reasons);
|
||||||
}
|
}
|
||||||
|
|
||||||
if path.contains(".github") || path.contains("workflow") || path.contains("ci") {
|
if path_lower.starts_with(".github/") || path_lower.contains("workflow") {
|
||||||
return (CommitType::Ci, format!("CI configuration: {}", path));
|
reasons.push(format!("CI/CD file detected: {}", path));
|
||||||
|
return (CommitType::Ci, reasons);
|
||||||
}
|
}
|
||||||
|
|
||||||
if file_name == "Cargo.toml" || file_name == "Cargo.lock" || ext == "toml" {
|
if path.contains("Cargo.toml") || path.contains("package.json") || path.contains("go.mod") {
|
||||||
return (CommitType::Build, format!("Build configuration: {}", path));
|
reasons.push(format!("Build configuration detected: {}", path));
|
||||||
|
return (CommitType::Build, reasons);
|
||||||
}
|
}
|
||||||
|
|
||||||
match status {
|
if file.is_new && !path.ends_with(".md") {
|
||||||
super::git::FileStatus::Added if ext == "rs" => {
|
reasons.push(format!("New feature file: {}", path));
|
||||||
(CommitType::Feat, format!("New feature file: {}", path))
|
return (CommitType::Feat, reasons);
|
||||||
}
|
|
||||||
super::git::FileStatus::Modified if ext == "rs" => {
|
|
||||||
(CommitType::Fix, format!("Modified source file: {}", path))
|
|
||||||
}
|
|
||||||
super::git::FileStatus::Deleted => {
|
|
||||||
(CommitType::Fix, format!("Deleted file: {}", path))
|
|
||||||
}
|
|
||||||
super::git::FileStatus::Renamed => {
|
|
||||||
(CommitType::Refactor, format!("Renamed file: {}", path))
|
|
||||||
}
|
|
||||||
_ => (CommitType::Chore, format!("Other change: {}", path)),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if file.is_deleted {
|
||||||
|
reasons.push(format!("Deleted file: {}", path));
|
||||||
|
return if path.ends_with(".md") {
|
||||||
|
(CommitType::Docs, reasons)
|
||||||
|
} else {
|
||||||
|
(CommitType::Chore, reasons)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if file.status == FileStatus::Modified && !path.ends_with(".md") {
|
||||||
|
return (CommitType::Fix, vec![format!("Modified file: {}", path)]);
|
||||||
|
}
|
||||||
|
|
||||||
|
(CommitType::Chore, vec![format!("Other changes: {}", path)])
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn extract_scope(path: &str) -> Option<String> {
|
fn extract_scope(path: &str) -> Option<String> {
|
||||||
let path = std::path::Path::new(path);
|
let parts: Vec<&str> = path.split('/').collect();
|
||||||
let components: Vec<String> = path
|
|
||||||
.components()
|
|
||||||
.filter_map(|c| c.as_os_str().to_str().map(|s| s.to_string()))
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
if components.is_empty() {
|
if parts.len() > 1 {
|
||||||
return None;
|
let potential_scope = parts[0];
|
||||||
|
|
||||||
|
let ignore = ["src", "tests", "docs", ".github", "target", "node_modules"];
|
||||||
|
if !ignore.contains(&potential_scope) {
|
||||||
|
return Some(potential_scope.to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
if potential_scope == "src" && parts.len() > 1 {
|
||||||
|
return Some(String::from("src"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let first = components.first().unwrap();
|
None
|
||||||
if first == "src" || first == "lib" || first == "tests" || first == "benches" {
|
|
||||||
Some(String::from("src"))
|
|
||||||
} else {
|
|
||||||
Some(first.clone())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_all_commit_types() -> Vec<CommitType> {
|
fn generate_description(files: &[ChangedFile]) -> String {
|
||||||
vec![
|
|
||||||
CommitType::Feat,
|
|
||||||
CommitType::Fix,
|
|
||||||
CommitType::Docs,
|
|
||||||
CommitType::Style,
|
|
||||||
CommitType::Refactor,
|
|
||||||
CommitType::Test,
|
|
||||||
CommitType::Chore,
|
|
||||||
CommitType::Build,
|
|
||||||
CommitType::Ci,
|
|
||||||
CommitType::Perf,
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
fn generate_description(files: &[&super::git::ChangedFile]) -> String {
|
|
||||||
if files.is_empty() {
|
if files.is_empty() {
|
||||||
return String::from("empty commit");
|
return String::from("no changes");
|
||||||
}
|
}
|
||||||
|
|
||||||
if files.len() == 1 {
|
if files.len() == 1 {
|
||||||
let f = files[0];
|
let file = &files[0];
|
||||||
let action = match f.status {
|
let filename = file.path.split('/').last().unwrap_or(&file.path);
|
||||||
super::git::FileStatus::Added => "add",
|
|
||||||
super::git::FileStatus::Deleted => "remove",
|
if file.is_new {
|
||||||
super::git::FileStatus::Modified => "update",
|
return format!("add {}", filename);
|
||||||
super::git::FileStatus::Renamed => "rename",
|
} else if file.is_deleted {
|
||||||
super::git::FileStatus::Unknown => "change",
|
return format!("remove {}", filename);
|
||||||
};
|
} else {
|
||||||
let file_name = std::path::Path::new(&f.path)
|
return format!("update {}", filename);
|
||||||
.file_name()
|
}
|
||||||
.and_then(|n| n.to_str())
|
|
||||||
.unwrap_or(&f.path);
|
|
||||||
return format!("{} {}", action, file_name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let total = files.len();
|
let extensions: std::collections::HashSet<&str> = files
|
||||||
let added: usize = files.iter().filter(|f| f.is_new).count();
|
.iter()
|
||||||
let modified: usize = files.iter().filter(|f| matches!(f.status, super::git::FileStatus::Modified)).count();
|
.map(|f| f.path.split('.').last().unwrap_or(""))
|
||||||
|
.collect();
|
||||||
|
|
||||||
if added == total {
|
if extensions.len() == 1 {
|
||||||
format!("add {} new files", total)
|
let ext = extensions.iter().next().unwrap();
|
||||||
} else if modified == total {
|
return format!("update {} {} files", files.len(), ext);
|
||||||
format!("update {} files", total)
|
|
||||||
} else {
|
|
||||||
format!("change {} files ({} new, {} modified)", total, added, modified)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
format!("update {} files", files.len())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_all_commit_types() -> Vec<CommitType> {
|
||||||
|
CONVENTIONAL_TYPES.to_vec()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::git::{ChangedFile, FileStatus, StagedChanges};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_extract_scope_src() {
|
fn test_classify_test_file() {
|
||||||
|
let file = ChangedFile {
|
||||||
|
path: String::from("tests/main_test.rs"),
|
||||||
|
status: FileStatus::Modified,
|
||||||
|
additions: 10,
|
||||||
|
deletions: 5,
|
||||||
|
is_new: false,
|
||||||
|
is_deleted: false,
|
||||||
|
is_renamed: false,
|
||||||
|
old_path: None,
|
||||||
|
};
|
||||||
|
let (commit_type, _) = classify_file(&file);
|
||||||
|
assert_eq!(commit_type, CommitType::Test);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_classify_docs_file() {
|
||||||
|
let file = ChangedFile {
|
||||||
|
path: String::from("README.md"),
|
||||||
|
status: FileStatus::Modified,
|
||||||
|
additions: 5,
|
||||||
|
deletions: 2,
|
||||||
|
is_new: false,
|
||||||
|
is_deleted: false,
|
||||||
|
is_renamed: false,
|
||||||
|
old_path: None,
|
||||||
|
};
|
||||||
|
let (commit_type, _) = classify_file(&file);
|
||||||
|
assert_eq!(commit_type, CommitType::Docs);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_classify_new_source_file() {
|
||||||
|
let file = ChangedFile {
|
||||||
|
path: String::from("src/new_feature.rs"),
|
||||||
|
status: FileStatus::Added,
|
||||||
|
additions: 50,
|
||||||
|
deletions: 0,
|
||||||
|
is_new: true,
|
||||||
|
is_deleted: false,
|
||||||
|
is_renamed: false,
|
||||||
|
old_path: None,
|
||||||
|
};
|
||||||
|
let (commit_type, _) = classify_file(&file);
|
||||||
|
assert_eq!(commit_type, CommitType::Feat);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_classify_github_workflow() {
|
||||||
|
let file = ChangedFile {
|
||||||
|
path: String::from(".github/workflows/ci.yml"),
|
||||||
|
status: FileStatus::Modified,
|
||||||
|
additions: 20,
|
||||||
|
deletions: 5,
|
||||||
|
is_new: false,
|
||||||
|
is_deleted: false,
|
||||||
|
is_renamed: false,
|
||||||
|
old_path: None,
|
||||||
|
};
|
||||||
|
let (commit_type, _) = classify_file(&file);
|
||||||
|
assert_eq!(commit_type, CommitType::Ci);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_extract_scope() {
|
||||||
assert_eq!(extract_scope("src/main.rs"), Some(String::from("src")));
|
assert_eq!(extract_scope("src/main.rs"), Some(String::from("src")));
|
||||||
|
assert_eq!(extract_scope("api/routes.rs"), Some(String::from("api")));
|
||||||
|
assert_eq!(extract_scope("README.md"), None);
|
||||||
|
assert_eq!(extract_scope("target/debug/build.rs"), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_extract_scope_nested() {
|
fn test_generate_description_single_file() {
|
||||||
assert_eq!(extract_scope("src/utils/helper.rs"), Some(String::from("src")));
|
let files = vec![ChangedFile {
|
||||||
|
path: String::from("src/main.rs"),
|
||||||
|
status: FileStatus::Modified,
|
||||||
|
additions: 10,
|
||||||
|
deletions: 5,
|
||||||
|
is_new: false,
|
||||||
|
is_deleted: false,
|
||||||
|
is_renamed: false,
|
||||||
|
old_path: None,
|
||||||
|
}];
|
||||||
|
assert_eq!(generate_description(&files), String::from("update main.rs"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_extract_scope_root() {
|
fn test_generate_description_multiple_files() {
|
||||||
assert_eq!(extract_scope("main.rs"), Some(String::from("main.rs")));
|
let files = vec![
|
||||||
|
ChangedFile {
|
||||||
|
path: String::from("src/a.rs"),
|
||||||
|
status: FileStatus::Modified,
|
||||||
|
additions: 10,
|
||||||
|
deletions: 5,
|
||||||
|
is_new: false,
|
||||||
|
is_deleted: false,
|
||||||
|
is_renamed: false,
|
||||||
|
old_path: None,
|
||||||
|
},
|
||||||
|
ChangedFile {
|
||||||
|
path: String::from("src/b.rs"),
|
||||||
|
status: FileStatus::Modified,
|
||||||
|
additions: 8,
|
||||||
|
deletions: 3,
|
||||||
|
is_new: false,
|
||||||
|
is_deleted: false,
|
||||||
|
is_renamed: false,
|
||||||
|
old_path: None,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
let desc = generate_description(&files);
|
||||||
|
assert!(desc.contains("update 2 files"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user