From e2c8a033079ba5d6e934242b96710d5d3b847f53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Humberto=20Yusta=20G=C3=B3mez?= Date: Thu, 12 Dec 2024 12:27:45 +0100 Subject: [PATCH] fix: separate warning and commit link (warning is activated but commit link is not --- src/git.rs | 39 +++++++++++++++++++++++++----- src/http/routers/v1/links.rs | 47 ++++++++++++++++++++---------------- 2 files changed, 59 insertions(+), 27 deletions(-) diff --git a/src/git.rs b/src/git.rs index b6b98a7ac..28100fe51 100644 --- a/src/git.rs +++ b/src/git.rs @@ -15,6 +15,13 @@ pub struct CommitInfo { pub commit_message: String, pub file_changes: Vec, } +impl CommitInfo { + pub fn get_project_name(&self) -> String { + self.project_path.to_file_path().ok() + .and_then(|path| path.file_name().map(|name| name.to_string_lossy().into_owned())) + .unwrap_or_else(|| "".to_string()) + } +} #[derive(Serialize, Deserialize, Debug, Clone)] pub struct FileChange { @@ -225,7 +232,6 @@ pub fn git_diff(repository: &Repository, file_changes: &Vec, max_siz } pub async fn get_commit_information_from_current_changes(gcx: Arc>) -> Vec { - const MAX_DIFF_SIZE: usize = 4096; let mut commits = Vec::new(); for project_path in crate::files_correction::get_project_dirs(gcx.clone()).await { @@ -240,7 +246,28 @@ pub async fn get_commit_information_from_current_changes(gcx: Arc { error!("{}", e); continue; } }; - let diff = match git_diff(&repository, &file_changes, MAX_DIFF_SIZE) { + commits.push(CommitInfo { + project_path: Url::from_file_path(&project_path).ok().unwrap_or_else(|| Url::parse("file:///").unwrap()), + commit_message: "".to_string(), + file_changes, + }); + } + + commits +} + +pub async fn generate_commit_messages(gcx: Arc>, commits: Vec) -> Vec { + const MAX_DIFF_SIZE: usize = 4096; + let mut commits_with_messages = Vec::new(); + for commit in commits { + let project_path = commit.project_path.to_file_path().ok().unwrap_or_default(); + + let repository = match git2::Repository::open(&project_path) { + Ok(repo) => repo, + Err(e) => { error!("{}", e); continue; } + }; + + let diff = match git_diff(&repository, &commit.file_changes, MAX_DIFF_SIZE) { Ok(d) if d.is_empty() => { continue; } Ok(d) => d, Err(e) => { error!("{}", e); continue; } @@ -251,12 +278,12 @@ pub async fn get_commit_information_from_current_changes(gcx: Arc { error!("{}", e); continue; } }; - commits.push(CommitInfo { - project_path: Url::from_file_path(&project_path).ok().unwrap_or_else(|| Url::parse("file:///").unwrap()), + commits_with_messages.push(CommitInfo { + project_path: commit.project_path, commit_message: commit_msg, - file_changes, + file_changes: commit.file_changes, }); } - commits + commits_with_messages } diff --git a/src/http/routers/v1/links.rs b/src/http/routers/v1/links.rs index 7cf47258b..17c609f81 100644 --- a/src/http/routers/v1/links.rs +++ b/src/http/routers/v1/links.rs @@ -12,7 +12,7 @@ use crate::global_context::GlobalContext; use crate::integrations::go_to_configuration_message; use crate::tools::tool_patch_aux::tickets_parsing::get_tickets_from_messages; use crate::agentic::generate_follow_up_message::generate_follow_up_message; -use crate::git::get_commit_information_from_current_changes; +use crate::git::{get_commit_information_from_current_changes, generate_commit_messages}; use crate::http::routers::v1::git::GitCommitPost; #[derive(Deserialize, Clone, Debug)] @@ -97,31 +97,17 @@ pub async fn handle_v1_links( } // GIT uncommitted - if post.meta.chat_mode == ChatMode::AGENT && false { + if post.meta.chat_mode == ChatMode::AGENT { + let commits = get_commit_information_from_current_changes(gcx.clone()).await; + let mut project_changes = Vec::new(); - for commit in get_commit_information_from_current_changes(gcx.clone()).await { - let project_name = commit.project_path.to_file_path().ok() - .and_then(|path| path.file_name().map(|name| name.to_string_lossy().into_owned())) - .unwrap_or_else(|| "".to_string()); - let tooltip_message = format!( - "git commit -m \"{}{}\"\n{}", - commit.commit_message.lines().next().unwrap_or(""), - if commit.commit_message.lines().count() > 1 { "..." } else { "" }, - commit.file_changes.iter().map(|f| format!("{} {}", f.status.initial(), f.path)).collect::>().join("\n"), - ); + for commit in &commits { project_changes.push(format!( - "In project {project_name}: {}{}", + "In project {}: {}{}", + commit.get_project_name(), commit.file_changes.iter().take(3).map(|f| format!("{} {}", f.status.initial(), f.path)).collect::>().join(", "), if commit.file_changes.len() > 3 { ", ..." } else { "" }, )); - links.push(Link { - action: LinkAction::Commit, - text: format!("Commit {} files in `{}`", commit.file_changes.len(), project_name), - goto: Some("LINKS_AGAIN".to_string()), - current_config_file: None, - link_tooltip: tooltip_message, - link_payload: Some(LinkPayload::CommitPayload(GitCommitPost { commits: vec![commit] })), - }); } if !project_changes.is_empty() && post.messages.is_empty() { if project_changes.len() > 4 { @@ -130,6 +116,25 @@ pub async fn handle_v1_links( } uncommited_changes_warning = format!("You have uncommitted changes:\n```\n{}\n```\n⚠️ You might have a problem rolling back agent's changes.", project_changes.join("\n")); } + + if false { + for commit_with_msg in generate_commit_messages(gcx.clone(), commits).await { + let tooltip_message = format!( + "git commit -m \"{}{}\"\n{}", + commit_with_msg.commit_message.lines().next().unwrap_or(""), + if commit_with_msg.commit_message.lines().count() > 1 { "..." } else { "" }, + commit_with_msg.file_changes.iter().map(|f| format!("{} {}", f.status.initial(), f.path)).collect::>().join("\n"), + ); + links.push(Link { + action: LinkAction::Commit, + text: format!("Commit {} files in `{}`", commit_with_msg.file_changes.len(), commit_with_msg.get_project_name()), + goto: Some("LINKS_AGAIN".to_string()), + current_config_file: None, + link_tooltip: tooltip_message, + link_payload: Some(LinkPayload::CommitPayload(GitCommitPost { commits: vec![commit_with_msg] })), + }); + } + } } // Failures above