-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgit.rs
More file actions
192 lines (160 loc) · 5.43 KB
/
Copy pathgit.rs
File metadata and controls
192 lines (160 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use anyhow::Result;
use clap::Subcommand;
use std::path::PathBuf;
use tracing::info;
use crate::adapters;
use crate::config;
use crate::core;
use crate::output::OutputFormat;
use crate::review::review_diff_content_with_repo;
#[derive(Subcommand)]
pub enum GitCommands {
Uncommitted,
Staged,
Branch {
#[arg(help = "Base branch/ref (defaults to repo default)")]
base: Option<String>,
},
Suggest,
PrTitle,
}
pub async fn git_command(
command: GitCommands,
config: config::Config,
format: OutputFormat,
) -> Result<()> {
let git = core::GitIntegration::new(".")?;
let diff_content = match command {
GitCommands::Uncommitted => {
info!("Analyzing uncommitted changes");
git.get_uncommitted_diff()?
}
GitCommands::Staged => {
info!("Analyzing staged changes");
git.get_staged_diff()?
}
GitCommands::Branch { base } => {
let base_branch = base.unwrap_or_else(|| {
git.get_default_branch()
.unwrap_or_else(|_| "main".to_string())
});
core::validate_ref_name(&base_branch)?;
info!("Analyzing changes from branch: {}", base_branch);
git.get_branch_diff(&base_branch)?
}
GitCommands::Suggest => {
return suggest_commit_message(config).await;
}
GitCommands::PrTitle => {
return suggest_pr_title(config).await;
}
};
if diff_content.is_empty() {
println!("No changes found");
return Ok(());
}
let repo_root = git.workdir().unwrap_or_else(|| PathBuf::from("."));
review_diff_content_with_repo(&diff_content, config, format, &repo_root).await
}
async fn suggest_commit_message(config: config::Config) -> Result<()> {
let git = core::GitIntegration::new(".")?;
let diff_content = git.get_staged_diff()?;
if diff_content.is_empty() {
println!("No staged changes found. Stage your changes with 'git add' first.");
return Ok(());
}
// Use Fast model for commit message suggestion (lightweight task)
let model_config = config.to_model_config_for_role(config::ModelRole::Fast);
let adapter = adapters::llm::create_adapter(&model_config)?;
let (system_prompt, user_prompt) =
core::CommitPromptBuilder::build_commit_prompt(&diff_content);
let request = adapters::llm::LLMRequest {
system_prompt,
user_prompt,
temperature: Some(0.3),
max_tokens: Some(500),
};
let response = adapter.complete(request).await?;
let commit_message = core::CommitPromptBuilder::extract_commit_message(&response.content);
println!("\nSuggested commit message:");
println!("{}", commit_message);
if commit_message.len() > 72 {
println!(
"\n⚠️ Warning: Commit message exceeds 72 characters ({})",
commit_message.len()
);
}
Ok(())
}
async fn suggest_pr_title(config: config::Config) -> Result<()> {
let git = core::GitIntegration::new(".")?;
let base_branch = git
.get_default_branch()
.unwrap_or_else(|_| "main".to_string());
let diff_content = git.get_branch_diff(&base_branch)?;
if diff_content.is_empty() {
println!("No changes found compared to {} branch.", base_branch);
return Ok(());
}
// Use Fast model for PR title suggestion (lightweight task)
let model_config = config.to_model_config_for_role(config::ModelRole::Fast);
let adapter = adapters::llm::create_adapter(&model_config)?;
let (system_prompt, user_prompt) =
core::CommitPromptBuilder::build_pr_title_prompt(&diff_content);
let request = adapters::llm::LLMRequest {
system_prompt,
user_prompt,
temperature: Some(0.3),
max_tokens: Some(200),
};
let response = adapter.complete(request).await?;
let title = extract_title_from_response(&response.content);
println!("\nSuggested PR title:");
println!("{}", title);
if title.len() > 65 {
println!(
"\n⚠️ Warning: PR title exceeds 65 characters ({})",
title.len()
);
}
Ok(())
}
fn extract_title_from_response(content: &str) -> String {
if let Some(start) = content.find("<title>") {
let after_tag = start + 7;
if let Some(end) = content[after_tag..].find("</title>") {
content[after_tag..after_tag + end].trim().to_string()
} else {
content.trim().to_string()
}
} else {
content
.lines()
.find(|line| !line.trim().is_empty())
.unwrap_or("")
.trim()
.to_string()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_extract_title_normal() {
let content = "<title>Fix login bug</title>";
assert_eq!(extract_title_from_response(content), "Fix login bug");
}
#[test]
fn test_extract_title_malformed_closing_before_opening() {
// Malformed: closing tag appears before opening tag
// This should NOT panic
let content = "Some text</title> more <title>Real Title</title>";
let title = extract_title_from_response(content);
assert!(!title.is_empty());
}
#[test]
fn test_extract_title_no_tags() {
let content = "Just a plain title\nSecond line";
assert_eq!(extract_title_from_response(content), "Just a plain title");
}
}