From 375885e6eca177cf4af61306a9b796efd5221fdb Mon Sep 17 00:00:00 2001 From: "gcp-cherry-pick-bot[bot]" <98988430+gcp-cherry-pick-bot[bot]@users.noreply.github.com> Date: Tue, 4 Mar 2025 01:03:07 -0500 Subject: [PATCH] Disable Git panel button to open commit editor in certain cases (cherry-pick #26000) (#26001) Cherry-picked Disable Git panel button to open commit editor in certain cases (#26000) Also: - Internally renames a bit of code to make it easy to identify between when we are disabling the buttons that open and close the modal editor (in Git Panel and Project Diff) vs when we are disabling the commit buttons (in Git Panel and Git commit editor modal). - Deletes some unused code. Release Notes: - Unified disabling / enabling the button to open the Git commit editor modal in the Git panel with the Project Diff commit button. - Unified disabling / enabling the commit buttons, for the same cases, between the Git panel and Git commit editor modal. Co-authored-by: Joseph T. Lyons --- crates/git_ui/src/commit_modal.rs | 21 ++++++++++----------- crates/git_ui/src/git_panel.rs | 4 +++- crates/git_ui/src/project_diff.rs | 10 +++++----- crates/project/src/git.rs | 17 +---------------- 4 files changed, 19 insertions(+), 33 deletions(-) diff --git a/crates/git_ui/src/commit_modal.rs b/crates/git_ui/src/commit_modal.rs index 7096ad9fe9de7e89921971af65b90fcc6a6198dc..410ce7f6c7b7e3f884f0a4f560d74eb45bd5949a 100644 --- a/crates/git_ui/src/commit_modal.rs +++ b/crates/git_ui/src/commit_modal.rs @@ -115,15 +115,15 @@ impl CommitModal { return; }; - let (can_commit, conflict) = git_panel.update(cx, |git_panel, cx| { - let can_commit = git_panel.can_commit(); + let (can_open_commit_editor, conflict) = git_panel.update(cx, |git_panel, cx| { + let can_open_commit_editor = git_panel.can_open_commit_editor(); let conflict = git_panel.has_unstaged_conflicts(); - if can_commit { + if can_open_commit_editor { git_panel.set_modal_open(true, cx); } - (can_commit, conflict) + (can_open_commit_editor, conflict) }); - if !can_commit { + if !can_open_commit_editor { let message = if conflict { "There are still conflicts. You must stage these before committing." } else { @@ -250,7 +250,7 @@ impl CommitModal { pub fn render_footer(&self, window: &mut Window, cx: &mut Context) -> impl IntoElement { let git_panel = self.git_panel.clone(); - let (branch, tooltip, commit_label, co_authors) = + let (branch, can_commit, tooltip, commit_label, co_authors) = self.git_panel.update(cx, |git_panel, cx| { let branch = git_panel .active_repository @@ -262,10 +262,10 @@ impl CommitModal { .map(|b| b.name.clone()) }) .unwrap_or_else(|| "".into()); - let (_, tooltip) = git_panel.configure_commit_button(cx); + let (can_commit, tooltip) = git_panel.configure_commit_button(cx); let title = git_panel.commit_button_title(); let co_authors = git_panel.render_co_authors(cx); - (branch, tooltip, title, co_authors) + (branch, can_commit, tooltip, title, co_authors) }); let branch_picker_button = panel_button(branch) @@ -300,9 +300,8 @@ impl CommitModal { None }; - let (panel_editor_focus_handle, can_commit) = git_panel.update(cx, |git_panel, cx| { - (git_panel.editor_focus_handle(cx), git_panel.can_commit()) - }); + let panel_editor_focus_handle = + git_panel.update(cx, |git_panel, cx| git_panel.editor_focus_handle(cx)); let commit_button = panel_filled_button(commit_label) .tooltip(move |window, cx| { diff --git a/crates/git_ui/src/git_panel.rs b/crates/git_ui/src/git_panel.rs index 8caed2fc11819c03cb411e4dc17e43bf979568a9..d8d8000215ac0452a36e4038cc780a543ba3e57f 100644 --- a/crates/git_ui/src/git_panel.rs +++ b/crates/git_ui/src/git_panel.rs @@ -1932,7 +1932,7 @@ impl GitPanel { }) } - pub fn can_commit(&self) -> bool { + pub fn can_open_commit_editor(&self) -> bool { (self.has_staged_changes() || self.has_tracked_changes()) && !self.has_unstaged_conflicts() } @@ -2015,6 +2015,7 @@ impl GitPanel { let panel_editor_style = panel_editor_style(true, window, cx); if let Some(active_repo) = active_repository { + let can_open_commit_editor = self.can_open_commit_editor(); let (can_commit, tooltip) = self.configure_commit_button(cx); let enable_coauthors = self.render_co_authors(cx); @@ -2107,6 +2108,7 @@ impl GitPanel { .icon_size(IconSize::Small) .style(ButtonStyle::Transparent) .width(expand_button_size.into()) + .disabled(!can_open_commit_editor) .on_click(cx.listener({ move |_, _, window, cx| { window.dispatch_action( diff --git a/crates/git_ui/src/project_diff.rs b/crates/git_ui/src/project_diff.rs index c61fefedfe31e52fec07e6f1b4b5a6681becb310..9bc6c6a7c0d3ea62f618594125c2d9efef9f5107 100644 --- a/crates/git_ui/src/project_diff.rs +++ b/crates/git_ui/src/project_diff.rs @@ -257,14 +257,14 @@ impl ProjectDiff { } } } - let mut commit = false; + let mut can_open_commit_editor = false; let mut stage_all = false; let mut unstage_all = false; self.workspace .read_with(cx, |workspace, cx| { if let Some(git_panel) = workspace.panel::(cx) { let git_panel = git_panel.read(cx); - commit = git_panel.can_commit(); + can_open_commit_editor = git_panel.can_open_commit_editor(); stage_all = git_panel.can_stage_all(); unstage_all = git_panel.can_unstage_all(); } @@ -276,7 +276,7 @@ impl ProjectDiff { unstage: has_staged_hunks, prev_next, selection, - commit, + can_open_commit_editor, stage_all, unstage_all, }; @@ -773,7 +773,7 @@ struct ButtonStates { selection: bool, stage_all: bool, unstage_all: bool, - commit: bool, + can_open_commit_editor: bool, } impl Render for ProjectDiffToolbar { @@ -949,7 +949,7 @@ impl Render for ProjectDiffToolbar { ) .child( Button::new("commit", "Commit") - .disabled(!button_states.commit) + .disabled(!button_states.can_open_commit_editor) .tooltip(Tooltip::for_action_title_in( "Commit", &ShowCommitEditor, diff --git a/crates/project/src/git.rs b/crates/project/src/git.rs index 43b69ccdb2fe941a496b842472f7bcdedcde8e32..0f1cc5a5562752c853a428043105e772b7e956d5 100644 --- a/crates/project/src/git.rs +++ b/crates/project/src/git.rs @@ -6,10 +6,7 @@ use client::ProjectId; use futures::channel::{mpsc, oneshot}; use futures::StreamExt as _; use git::repository::{Branch, CommitDetails, PushOptions, Remote, RemoteCommandOutput, ResetMode}; -use git::{ - repository::{GitRepository, RepoPath}, - status::{GitSummary, TrackedSummary}, -}; +use git::repository::{GitRepository, RepoPath}; use gpui::{ App, AppContext, AsyncApp, Context, Entity, EventEmitter, SharedString, Subscription, Task, WeakEntity, @@ -1046,18 +1043,6 @@ impl Repository { self.repository_entry.status_len() } - fn have_changes(&self) -> bool { - self.repository_entry.status_summary() != GitSummary::UNCHANGED - } - - fn have_staged_changes(&self) -> bool { - self.repository_entry.status_summary().index != TrackedSummary::UNCHANGED - } - - pub fn can_commit(&self, commit_all: bool) -> bool { - return self.have_changes() && (commit_all || self.have_staged_changes()); - } - pub fn commit( &self, message: SharedString,