From d732b8ba0fe1989dbafa06a5afe0da5cb3fcbc67 Mon Sep 17 00:00:00 2001 From: Richard Hao Date: Mon, 10 Mar 2025 14:45:25 +0800 Subject: [PATCH] git: Disable commit message generation when commit not possible (#26329) ## Issue: - `Generate Commit Message` will generate a random message if there are no changes. image ## After Fixed: - `Generate Commit Message` will be disabled if commit is not possible image ## Release Notes: - Fixed: Disable commit message generation when commit is not possible --- crates/git_ui/src/git_panel.rs | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/crates/git_ui/src/git_panel.rs b/crates/git_ui/src/git_panel.rs index df384cffcb..b9418af604 100644 --- a/crates/git_ui/src/git_panel.rs +++ b/crates/git_ui/src/git_panel.rs @@ -1453,6 +1453,10 @@ impl GitPanel { /// Generates a commit message using an LLM. pub fn generate_commit_message(&mut self, cx: &mut Context) { + if !self.can_commit() { + return; + } + let model = match current_language_model(cx) { Some(value) => value, None => return, @@ -2291,14 +2295,28 @@ impl GitPanel { .into_any_element(); } + let can_commit = self.can_commit(); + let editor_focus_handle = self.commit_editor.focus_handle(cx); IconButton::new("generate-commit-message", IconName::AiEdit) .shape(ui::IconButtonShape::Square) .icon_color(Color::Muted) - .tooltip(Tooltip::for_action_title_in( - "Generate Commit Message", - &git::GenerateCommitMessage, - &self.commit_editor.focus_handle(cx), - )) + .tooltip(move |window, cx| { + if can_commit { + Tooltip::for_action_in( + "Generate Commit Message", + &git::GenerateCommitMessage, + &editor_focus_handle, + window, + cx, + ) + } else { + Tooltip::simple( + "You must have either staged changes or tracked files to generate a commit message", + cx, + ) + } + }) + .disabled(!can_commit) .on_click(cx.listener(move |this, _event, _window, cx| { this.generate_commit_message(cx); }))