From 92dc812aeabd5976b51c522873fcda3fa7a38e53 Mon Sep 17 00:00:00 2001 From: Smit Barmase Date: Tue, 15 Apr 2025 23:17:04 +0530 Subject: [PATCH] git_ui: Fix commit/amend telemetry and amend click from commit modal (#28795) Release Notes: - N/A --- crates/git_ui/src/commit_modal.rs | 80 ++++++++++++++-------- crates/git_ui/src/git_panel.rs | 106 ++++++++++++++++++++---------- 2 files changed, 122 insertions(+), 64 deletions(-) diff --git a/crates/git_ui/src/commit_modal.rs b/crates/git_ui/src/commit_modal.rs index dd897eb46a..06d6fcb663 100644 --- a/crates/git_ui/src/commit_modal.rs +++ b/crates/git_ui/src/commit_modal.rs @@ -271,7 +271,7 @@ impl CommitModal { .when_some(keybinding_target.clone(), |el, keybinding_target| { el.context(keybinding_target.clone()) }) - .action("Amend...", Amend.boxed_clone()) + .action("Amend", Amend.boxed_clone()) })) }) .with_handle(self.commit_menu_handle.clone()) @@ -407,9 +407,18 @@ impl CommitModal { } }) .disabled(!can_commit) - .on_click(move |_, window, cx| { - window.dispatch_action(Box::new(git::Commit), cx); - }), + .on_click(cx.listener(move |this, _: &ClickEvent, window, cx| { + telemetry::event!("Git Amended", source = "Git Modal"); + this.git_panel.update(cx, |git_panel, cx| { + git_panel.set_amend_pending(false, cx); + git_panel.commit_changes( + CommitOptions { amend: true }, + window, + cx, + ); + }); + cx.emit(DismissEvent); + })), ) }) .when(!is_amend_pending, |this| { @@ -425,9 +434,17 @@ impl CommitModal { .child(Label::new(commit_label).size(LabelSize::Small)) .mr_0p5(), ) - .on_click(move |_, window, cx| { - window.dispatch_action(Box::new(git::Commit), cx); - }) + .on_click(cx.listener(move |this, _: &ClickEvent, window, cx| { + telemetry::event!("Git Committed", source = "Git Modal"); + this.git_panel.update(cx, |git_panel, cx| { + git_panel.commit_changes( + CommitOptions { amend: false }, + window, + cx, + ) + }); + cx.emit(DismissEvent); + })) .disabled(!can_commit) .tooltip({ let focus_handle = focus_handle.clone(); @@ -473,9 +490,22 @@ impl CommitModal { } }) .disabled(!can_commit) - .on_click(move |_, window, cx| { - window.dispatch_action(Box::new(git::Commit), cx); - }), + .on_click(cx.listener( + move |this, _: &ClickEvent, window, cx| { + telemetry::event!( + "Git Committed", + source = "Git Modal" + ); + this.git_panel.update(cx, |git_panel, cx| { + git_panel.commit_changes( + CommitOptions { amend: false }, + window, + cx, + ) + }); + cx.emit(DismissEvent); + }, + )), ) }) }), @@ -503,26 +533,18 @@ impl CommitModal { } fn amend(&mut self, _: &git::Amend, window: &mut Window, cx: &mut Context) { - if self - .commit_editor - .focus_handle(cx) - .contains_focused(window, cx) - { - if !self.git_panel.read(cx).amend_pending() { - self.git_panel.update(cx, |git_panel, cx| { - git_panel.set_amend_pending(true, cx); - git_panel.load_last_commit_message_if_empty(cx); - }); - } else { - telemetry::event!("Git Amended", source = "Git Panel"); - self.git_panel.update(cx, |git_panel, cx| { - git_panel.set_amend_pending(false, cx); - git_panel.commit_changes(CommitOptions { amend: true }, window, cx); - }); - cx.emit(DismissEvent); - } + if !self.git_panel.read(cx).amend_pending() { + self.git_panel.update(cx, |git_panel, cx| { + git_panel.set_amend_pending(true, cx); + git_panel.load_last_commit_message_if_empty(cx); + }); } else { - cx.propagate(); + telemetry::event!("Git Amended", source = "Git Modal"); + self.git_panel.update(cx, |git_panel, cx| { + git_panel.set_amend_pending(false, cx); + git_panel.commit_changes(CommitOptions { amend: true }, window, cx); + }); + cx.emit(DismissEvent); } } diff --git a/crates/git_ui/src/git_panel.rs b/crates/git_ui/src/git_panel.rs index 1026776eda..0f0aed8268 100644 --- a/crates/git_ui/src/git_panel.rs +++ b/crates/git_ui/src/git_panel.rs @@ -1434,6 +1434,25 @@ impl GitPanel { } } + fn amend(&mut self, _: &git::Amend, window: &mut Window, cx: &mut Context) { + if self + .commit_editor + .focus_handle(cx) + .contains_focused(window, cx) + { + if !self.amend_pending { + self.set_amend_pending(true, cx); + self.load_last_commit_message_if_empty(cx); + } else { + telemetry::event!("Git Amended", source = "Git Panel"); + self.set_amend_pending(false, cx); + self.commit_changes(CommitOptions { amend: true }, window, cx); + } + } else { + cx.propagate(); + } + } + pub fn load_last_commit_message_if_empty(&mut self, cx: &mut Context) { if !self.commit_editor.read(cx).is_empty(cx) { return; @@ -1467,30 +1486,9 @@ impl GitPanel { .detach(); } - fn amend(&mut self, _: &git::Amend, window: &mut Window, cx: &mut Context) { - if self - .commit_editor - .focus_handle(cx) - .contains_focused(window, cx) - { - if !self.amend_pending { - self.amend_pending = true; - cx.notify(); - self.load_last_commit_message_if_empty(cx); - } else { - telemetry::event!("Git Amended", source = "Git Panel"); - self.amend_pending = false; - self.commit_changes(CommitOptions { amend: true }, window, cx); - } - } else { - cx.propagate(); - } - } - fn cancel(&mut self, _: &git::Cancel, _: &mut Window, cx: &mut Context) { if self.amend_pending { - self.amend_pending = false; - cx.notify(); + self.set_amend_pending(false, cx); } } @@ -2816,7 +2814,7 @@ impl GitPanel { .when_some(keybinding_target.clone(), |el, keybinding_target| { el.context(keybinding_target.clone()) }) - .action("Amend...", Amend.boxed_clone()) + .action("Amend", Amend.boxed_clone()) })) }) .anchor(Corner::TopRight) @@ -3082,11 +3080,29 @@ impl GitPanel { } }) .disabled(!can_commit || self.modal_open) - .on_click(move |_, window, cx| { - window.dispatch_action( - Box::new(git::Amend), - cx, - ); + .on_click({ + let git_panel = git_panel.downgrade(); + move |_, window, cx| { + telemetry::event!( + "Git Amended", + source = "Git Panel" + ); + git_panel + .update(cx, |git_panel, cx| { + git_panel + .set_amend_pending( + false, cx, + ); + git_panel.commit_changes( + CommitOptions { + amend: true, + }, + window, + cx, + ); + }) + .ok(); + } }), ) } @@ -3110,6 +3126,10 @@ impl GitPanel { .on_click({ let git_panel = git_panel.downgrade(); move |_, window, cx| { + telemetry::event!( + "Git Committed", + source = "Git Panel" + ); git_panel .update(cx, |git_panel, cx| { git_panel.commit_changes( @@ -3170,11 +3190,25 @@ impl GitPanel { } }) .disabled(!can_commit || self.modal_open) - .on_click(move |_, window, cx| { - window.dispatch_action( - Box::new(git::Commit), - cx, - ); + .on_click({ + let git_panel = git_panel.downgrade(); + move |_, window, cx| { + telemetry::event!( + "Git Committed", + source = "Git Panel" + ); + git_panel + .update(cx, |git_panel, cx| { + git_panel.commit_changes( + CommitOptions { + amend: false, + }, + window, + cx, + ); + }) + .ok(); + } }), ) }, @@ -3234,8 +3268,10 @@ impl GitPanel { .px(px(8.)) .border_color(cx.theme().colors().border) .child( - Label::new("Your changes will modify your most recent commit. If you want to make these changes as a new commit, you can cancel the amend operation.") - .size(LabelSize::Small), + Label::new( + "This will update your most recent commit. Cancel to make a new one instead.", + ) + .size(LabelSize::Small), ) }