git: Disable commit message generation when commit not possible (#26329)

## Issue:

- `Generate Commit Message` will generate a random message if there are
no changes.
<img width="614" alt="image"
src="https://github.com/user-attachments/assets/c16cadac-01af-47c0-a2db-a5bbf62f84bb"
/>


## After Fixed:

- `Generate Commit Message` will be disabled if commit is not possible
<img width="610" alt="image"
src="https://github.com/user-attachments/assets/5ea9ca70-6fa3-4144-ab4e-be7a986d5496"
/>


## Release Notes:

- Fixed: Disable commit message generation when commit is not possible
This commit is contained in:
Richard Hao 2025-03-10 14:45:25 +08:00 committed by GitHub
parent 7c3eecc9c7
commit d732b8ba0f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1453,6 +1453,10 @@ impl GitPanel {
/// Generates a commit message using an LLM. /// Generates a commit message using an LLM.
pub fn generate_commit_message(&mut self, cx: &mut Context<Self>) { pub fn generate_commit_message(&mut self, cx: &mut Context<Self>) {
if !self.can_commit() {
return;
}
let model = match current_language_model(cx) { let model = match current_language_model(cx) {
Some(value) => value, Some(value) => value,
None => return, None => return,
@ -2291,14 +2295,28 @@ impl GitPanel {
.into_any_element(); .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) IconButton::new("generate-commit-message", IconName::AiEdit)
.shape(ui::IconButtonShape::Square) .shape(ui::IconButtonShape::Square)
.icon_color(Color::Muted) .icon_color(Color::Muted)
.tooltip(Tooltip::for_action_title_in( .tooltip(move |window, cx| {
"Generate Commit Message", if can_commit {
&git::GenerateCommitMessage, Tooltip::for_action_in(
&self.commit_editor.focus_handle(cx), "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| { .on_click(cx.listener(move |this, _event, _window, cx| {
this.generate_commit_message(cx); this.generate_commit_message(cx);
})) }))