122 lines
3.6 KiB
Rust
122 lines
3.6 KiB
Rust
use super::analyzer::{AnalysisResult, CommitType};
|
|
use super::git::StagedChanges;
|
|
|
|
pub fn generate_message(analysis: &AnalysisResult, _staged: &StagedChanges) -> String {
|
|
let scope = analysis.scope.clone().unwrap_or_default();
|
|
let description = &analysis.description;
|
|
|
|
if !scope.is_empty() {
|
|
format!("{} ({}) : {}", analysis.commit_type, scope, description)
|
|
} else {
|
|
format!("{}: {}", analysis.commit_type, description)
|
|
}
|
|
}
|
|
|
|
pub fn generate_alternative_messages(
|
|
analysis: &AnalysisResult,
|
|
_staged: &StagedChanges,
|
|
) -> Vec<String> {
|
|
let mut messages = Vec::new();
|
|
|
|
let scope = analysis.scope.clone().unwrap_or_default();
|
|
let base_description = &analysis.description;
|
|
|
|
let verb_alternatives = vec!["update", "modify", "change", "revise"];
|
|
|
|
if !scope.is_empty() {
|
|
for verb in &verb_alternatives {
|
|
let desc = format!("{} {}", verb, base_description);
|
|
messages.push(format!("{} ({}) : {}", analysis.commit_type, scope, desc));
|
|
}
|
|
} else {
|
|
for verb in &verb_alternatives {
|
|
let desc = format!("{} {}", verb, base_description);
|
|
messages.push(format!("{}: {}", analysis.commit_type, desc));
|
|
}
|
|
}
|
|
|
|
messages
|
|
}
|
|
|
|
pub fn format_message(message: &str) -> String {
|
|
if message.len() <= 72 {
|
|
return message.to_string();
|
|
}
|
|
|
|
let words: Vec<&str> = message.split_whitespace().collect();
|
|
let mut lines = Vec::new();
|
|
let mut current_line = String::new();
|
|
|
|
for word in words {
|
|
if current_line.is_empty() {
|
|
current_line = word.to_string();
|
|
} else if current_line.len() + 1 + word.len() <= 72 {
|
|
current_line.push(' ');
|
|
current_line.push_str(word);
|
|
} else {
|
|
lines.push(current_line);
|
|
current_line = word.to_string();
|
|
}
|
|
}
|
|
|
|
if !current_line.is_empty() {
|
|
lines.push(current_line);
|
|
}
|
|
|
|
lines.join("\n")
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use super::super::analyzer::AnalysisResult;
|
|
use super::super::git::{ChangedFile, FileStatus, StagedChanges};
|
|
|
|
#[test]
|
|
fn test_message_format_with_scope() {
|
|
let analysis = AnalysisResult {
|
|
commit_type: CommitType::Feat,
|
|
scope: Some(String::from("auth")),
|
|
confidence: 0.9,
|
|
description: String::from("add login functionality"),
|
|
reasons: vec![],
|
|
};
|
|
let staged = StagedChanges {
|
|
files: vec![],
|
|
diff_text: String::new(),
|
|
};
|
|
let message = generate_message(&analysis, &staged);
|
|
assert_eq!(message, "feat(auth) : add login functionality");
|
|
}
|
|
|
|
#[test]
|
|
fn test_message_format_without_scope() {
|
|
let analysis = AnalysisResult {
|
|
commit_type: CommitType::Fix,
|
|
scope: None,
|
|
confidence: 0.9,
|
|
description: String::from("resolve null pointer exception"),
|
|
reasons: vec![],
|
|
};
|
|
let staged = StagedChanges {
|
|
files: vec![],
|
|
diff_text: String::new(),
|
|
};
|
|
let message = generate_message(&analysis, &staged);
|
|
assert_eq!(message, "fix: resolve null pointer exception");
|
|
}
|
|
|
|
#[test]
|
|
fn test_format_message_short() {
|
|
let message = format_message("feat: add new feature");
|
|
assert_eq!(message, "feat: add new feature");
|
|
}
|
|
|
|
#[test]
|
|
fn test_format_message_long() {
|
|
let long_desc = "this is a very long description that should be wrapped because it exceeds seventy two characters in length";
|
|
let message = format_message(&format!("feat: {}", long_desc));
|
|
assert!(message.contains('\n'));
|
|
}
|
|
}
|