Skip to content

Commit

Permalink
fix: separate warning and commit link (warning is activated but commi…
Browse files Browse the repository at this point in the history
…t link is not
  • Loading branch information
humbertoyusta authored and olegklimov committed Dec 12, 2024
1 parent 97dfae8 commit ce95611
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 27 deletions.
39 changes: 33 additions & 6 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ pub struct CommitInfo {
pub commit_message: String,
pub file_changes: Vec<FileChange>,
}
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 {
Expand Down Expand Up @@ -225,7 +232,6 @@ pub fn git_diff(repository: &Repository, file_changes: &Vec<FileChange>, max_siz
}

pub async fn get_commit_information_from_current_changes(gcx: Arc<ARwLock<GlobalContext>>) -> Vec<CommitInfo> {
const MAX_DIFF_SIZE: usize = 4096;
let mut commits = Vec::new();

for project_path in crate::files_correction::get_project_dirs(gcx.clone()).await {
Expand All @@ -240,7 +246,28 @@ pub async fn get_commit_information_from_current_changes(gcx: Arc<ARwLock<Global
Err(e) => { 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<ARwLock<GlobalContext>>, commits: Vec<CommitInfo>) -> Vec<CommitInfo> {
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; }
Expand All @@ -251,12 +278,12 @@ pub async fn get_commit_information_from_current_changes(gcx: Arc<ARwLock<Global
Err(e) => { 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
}
47 changes: 26 additions & 21 deletions src/http/routers/v1/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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::<Vec<_>>().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::<Vec<_>>().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 {
Expand All @@ -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::<Vec<_>>().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
Expand Down

0 comments on commit ce95611

Please sign in to comment.