Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

git_ui: Disable remote button during operations (#59874)

Xiaobo Liue7afe9fcf937
diff --git a/crates/git_ui/src/git_panel.rs b/crates/git_ui/src/git_panel.rs
index b6e5b7e7634..de75268eb48 100644
--- a/crates/git_ui/src/git_panel.rs
+++ b/crates/git_ui/src/git_panel.rs
@@ -91,6 +91,11 @@ use workspace::{
};
use zed_actions::{DecreaseBufferFontSize, IncreaseBufferFontSize, ResetBufferFontSize};
+const GIT_PANEL_KEY: &str = "GitPanel";
+const UPDATE_DEBOUNCE: Duration = Duration::from_millis(50);
+// TODO: We should revise this part. It seems the indentation width is not aligned with the one in project panel
+const TREE_INDENT: f32 = 16.0;
+
actions!(
git_panel,
[
@@ -319,11 +324,14 @@ fn git_panel_view_options_menu(
})
}
-const GIT_PANEL_KEY: &str = "GitPanel";
-
-const UPDATE_DEBOUNCE: Duration = Duration::from_millis(50);
-// TODO: We should revise this part. It seems the indentation width is not aligned with the one in project panel
-const TREE_INDENT: f32 = 16.0;
+// We only allow a single remote operation at a time to avoid concurrent
+// credential prompts and competing ref/working-tree updates.
+#[derive(Clone, Copy)]
+pub(crate) enum RemoteOperationKind {
+ Fetch,
+ Pull,
+ Push,
+}
pub fn register(workspace: &mut Workspace) {
workspace.register_action(|workspace, _: &ToggleFocus, window, cx| {
@@ -749,6 +757,7 @@ pub struct GitPanel {
diff_stat_total: DiffStat,
new_staged_count: usize,
pending_commit: Option<Task<()>>,
+ pending_remote_operation: Option<RemoteOperationKind>,
amend_pending: bool,
original_commit_message: Option<String>,
signoff_enabled: bool,
@@ -977,6 +986,7 @@ impl GitPanel {
changes_count: 0,
diff_stat_total: DiffStat::default(),
pending_commit: None,
+ pending_remote_operation: None,
amend_pending: false,
original_commit_message: None,
signoff_enabled: false,
@@ -3106,6 +3116,10 @@ impl GitPanel {
let Some(repo) = self.active_repository.clone() else {
return;
};
+ if !self.start_remote_operation(RemoteOperationKind::Fetch, cx) {
+ return;
+ }
+
telemetry::event!("Git Fetched");
let askpass = self.askpass_delegate("git fetch", window, cx);
let this = cx.weak_entity();
@@ -3118,6 +3132,10 @@ impl GitPanel {
window
.spawn(cx, async move |cx| {
+ let _clear_pending_remote_operation = cx.on_drop(&this, |this, cx| {
+ this.clear_remote_operation(cx);
+ });
+
let Some(fetch_options) = fetch_options.await else {
return Ok(());
};
@@ -3248,13 +3266,20 @@ impl GitPanel {
let Some(repo) = self.active_repository.clone() else {
return;
};
- let Some(branch) = repo.read(cx).branch.as_ref() else {
+ let Some(branch) = repo.read(cx).branch.clone() else {
return;
};
+ if !self.start_remote_operation(RemoteOperationKind::Pull, cx) {
+ return;
+ }
+
telemetry::event!("Git Pulled");
- let branch = branch.clone();
let remote = self.get_remote(false, false, window, cx);
cx.spawn_in(window, async move |this, cx| {
+ let _clear_pending_remote_operation = cx.on_drop(&this, |this, cx| {
+ this.clear_remote_operation(cx);
+ });
+
let remote = match remote.await {
Ok(Some(remote)) => remote,
Ok(None) => {
@@ -3311,11 +3336,14 @@ impl GitPanel {
let Some(repo) = self.active_repository.clone() else {
return;
};
- let Some(branch) = repo.read(cx).branch.as_ref() else {
+ let Some(branch) = repo.read(cx).branch.clone() else {
return;
};
+ if !self.start_remote_operation(RemoteOperationKind::Push, cx) {
+ return;
+ }
+
telemetry::event!("Git Pushed");
- let branch = branch.clone();
let options = if force_push {
Some(PushOptions::Force)
@@ -3332,6 +3360,10 @@ impl GitPanel {
let remote = self.get_remote(select_remote, true, window, cx);
cx.spawn_in(window, async move |this, cx| {
+ let _clear_pending_remote_operation = cx.on_drop(&this, |this, cx| {
+ this.clear_remote_operation(cx);
+ });
+
let remote = match remote.await {
Ok(Some(remote)) => remote,
Ok(None) => {
@@ -3508,6 +3540,25 @@ impl GitPanel {
!self.project.read(cx).is_via_collab()
}
+ fn start_remote_operation(
+ &mut self,
+ kind: RemoteOperationKind,
+ cx: &mut Context<Self>,
+ ) -> bool {
+ if self.pending_remote_operation.is_some() {
+ return false;
+ }
+
+ self.pending_remote_operation = Some(kind);
+ cx.notify();
+ true
+ }
+
+ fn clear_remote_operation(&mut self, cx: &mut Context<Self>) {
+ self.pending_remote_operation.take();
+ cx.notify();
+ }
+
fn get_remote(
&mut self,
always_select: bool,
@@ -4921,6 +4972,7 @@ impl GitPanel {
&branch,
focus_handle,
true,
+ self.pending_remote_operation,
))
})
.into_any_element(),
@@ -8984,6 +9036,48 @@ mod tests {
});
}
+ #[gpui::test]
+ async fn test_remote_operation_serialization(cx: &mut TestAppContext) {
+ init_test(cx);
+
+ let fs = FakeFs::new(cx.background_executor.clone());
+ fs.insert_tree(
+ path!("/project"),
+ json!({
+ ".git": {},
+ }),
+ )
+ .await;
+
+ let project = Project::test(fs.clone(), [Path::new(path!("/project"))], cx).await;
+ let window_handle =
+ cx.add_window(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
+ let workspace = window_handle
+ .read_with(cx, |mw, _| mw.workspace().clone())
+ .unwrap();
+ let cx = &mut VisualTestContext::from_window(window_handle.into(), cx);
+ let panel = workspace.update_in(cx, GitPanel::new);
+
+ panel.update(cx, |panel, cx| {
+ // The first remote operation starts and records its kind, which the
+ // button uses to render an "in progress" tooltip.
+ assert!(panel.start_remote_operation(RemoteOperationKind::Fetch, cx));
+ assert!(matches!(
+ panel.pending_remote_operation,
+ Some(RemoteOperationKind::Fetch)
+ ));
+
+ // A second remote operation is refused while one is pending, even a
+ // different kind: we serialize all remote ops.
+ assert!(!panel.start_remote_operation(RemoteOperationKind::Push, cx));
+
+ // Clearing the pending operation re-opens the gate.
+ panel.clear_remote_operation(cx);
+ assert!(panel.pending_remote_operation.is_none());
+ assert!(panel.start_remote_operation(RemoteOperationKind::Pull, cx));
+ });
+ }
+
#[gpui::test]
async fn test_tree_view_without_status_grouping_combines_statuses(cx: &mut TestAppContext) {
init_test(cx);
diff --git a/crates/git_ui/src/git_ui.rs b/crates/git_ui/src/git_ui.rs
index b508ae5257e..c221b8814e2 100644
--- a/crates/git_ui/src/git_ui.rs
+++ b/crates/git_ui/src/git_ui.rs
@@ -32,7 +32,7 @@ use zed_actions;
use crate::{
commit_view::CommitView,
- git_panel::{GitPanel, GitStatusEntry},
+ git_panel::{GitPanel, GitStatusEntry, RemoteOperationKind},
solo_diff_view::SoloDiffView,
text_diff_view::TextDiffView,
};
@@ -764,6 +764,7 @@ fn render_remote_button(
branch: &Branch,
keybinding_target: Option<FocusHandle>,
show_fetch_button: bool,
+ in_progress_operation: Option<RemoteOperationKind>,
) -> Option<impl IntoElement> {
let id = id.into();
let upstream = branch.upstream.as_ref();
@@ -772,20 +773,24 @@ fn render_remote_button(
tracking: UpstreamTracking::Tracked(UpstreamTrackingStatus { ahead, behind }),
..
}) => match (*ahead, *behind) {
- (0, 0) if show_fetch_button => {
- Some(remote_button::render_fetch_button(keybinding_target, id))
- }
+ (0, 0) if show_fetch_button => Some(remote_button::render_fetch_button(
+ keybinding_target,
+ id,
+ in_progress_operation,
+ )),
(0, 0) => None,
(ahead, 0) => Some(remote_button::render_push_button(
keybinding_target,
id,
ahead,
+ in_progress_operation,
)),
(ahead, behind) => Some(remote_button::render_pull_button(
keybinding_target,
id,
ahead,
behind,
+ in_progress_operation,
)),
},
Some(Upstream {
@@ -794,22 +799,25 @@ fn render_remote_button(
}) => Some(remote_button::render_republish_button(
keybinding_target,
id,
+ in_progress_operation,
+ )),
+ None => Some(remote_button::render_publish_button(
+ keybinding_target,
+ id,
+ in_progress_operation,
)),
- None => Some(remote_button::render_publish_button(keybinding_target, id)),
}
}
mod remote_button {
+ use crate::git_panel::RemoteOperationKind;
use gpui::{Action, Anchor, AnyView, ClickEvent, FocusHandle};
- use ui::{
- App, ButtonCommon, Clickable, ContextMenu, ElementId, FluentBuilder, Icon, IconName,
- IconSize, IntoElement, Label, LabelCommon, LabelSize, LineHeightStyle, ParentElement,
- PopoverMenu, SharedString, SplitButton, Styled, Tooltip, Window, div, h_flex, rems,
- };
+ use ui::{CommonAnimationExt, ContextMenu, PopoverMenu, SplitButton, Tooltip, prelude::*};
pub fn render_fetch_button(
keybinding_target: Option<FocusHandle>,
id: SharedString,
+ in_progress_operation: Option<RemoteOperationKind>,
) -> SplitButton {
split_button(
id,
@@ -818,6 +826,7 @@ mod remote_button {
0,
Some(IconName::ArrowCircle),
keybinding_target.clone(),
+ in_progress_operation,
move |_, window, cx| {
window.dispatch_action(Box::new(git::Fetch), cx);
},
@@ -837,6 +846,7 @@ mod remote_button {
keybinding_target: Option<FocusHandle>,
id: SharedString,
ahead: u32,
+ in_progress_operation: Option<RemoteOperationKind>,
) -> SplitButton {
split_button(
id,
@@ -845,6 +855,7 @@ mod remote_button {
0,
None,
keybinding_target.clone(),
+ in_progress_operation,
move |_, window, cx| {
window.dispatch_action(Box::new(git::Push), cx);
},
@@ -865,6 +876,7 @@ mod remote_button {
id: SharedString,
ahead: u32,
behind: u32,
+ in_progress_operation: Option<RemoteOperationKind>,
) -> SplitButton {
split_button(
id,
@@ -873,6 +885,7 @@ mod remote_button {
behind as usize,
None,
keybinding_target.clone(),
+ in_progress_operation,
move |_, window, cx| {
window.dispatch_action(Box::new(git::Pull), cx);
},
@@ -891,6 +904,7 @@ mod remote_button {
pub fn render_publish_button(
keybinding_target: Option<FocusHandle>,
id: SharedString,
+ in_progress_operation: Option<RemoteOperationKind>,
) -> SplitButton {
split_button(
id,
@@ -899,6 +913,7 @@ mod remote_button {
0,
Some(IconName::ExpandUp),
keybinding_target.clone(),
+ in_progress_operation,
move |_, window, cx| {
window.dispatch_action(Box::new(git::Push), cx);
},
@@ -917,6 +932,7 @@ mod remote_button {
pub fn render_republish_button(
keybinding_target: Option<FocusHandle>,
id: SharedString,
+ in_progress_operation: Option<RemoteOperationKind>,
) -> SplitButton {
split_button(
id,
@@ -925,6 +941,7 @@ mod remote_button {
0,
Some(IconName::ExpandUp),
keybinding_target.clone(),
+ in_progress_operation,
move |_, window, cx| {
window.dispatch_action(Box::new(git::Push), cx);
},
@@ -940,6 +957,14 @@ mod remote_button {
)
}
+ fn in_progress_tooltip(operation: RemoteOperationKind) -> &'static str {
+ match operation {
+ RemoteOperationKind::Fetch => "Fetch in Progress…",
+ RemoteOperationKind::Pull => "Pull in Progress…",
+ RemoteOperationKind::Push => "Push in Progress…",
+ }
+ }
+
fn git_action_tooltip(
label: impl Into<SharedString>,
action: &dyn Action,
@@ -999,6 +1024,7 @@ mod remote_button {
behind_count: usize,
left_icon: Option<IconName>,
keybinding_target: Option<FocusHandle>,
+ in_progress_operation: Option<RemoteOperationKind>,
left_on_click: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
tooltip: impl Fn(&mut Window, &mut App) -> AnyView + 'static,
) -> SplitButton {
@@ -1017,12 +1043,14 @@ mod remote_button {
}
let should_render_counts = left_icon.is_none() && (ahead_count > 0 || behind_count > 0);
+ let is_in_progress = in_progress_operation.is_some();
let left = ui::ButtonLike::new_rounded_left(ElementId::Name(
format!("split-button-left-{}", id).into(),
))
.layer(ui::ElevationIndex::ModalSurface)
.size(ui::ButtonSize::Compact)
+ .disabled(is_in_progress)
.when(should_render_counts, |this| {
this.child(
h_flex()
@@ -1038,19 +1066,33 @@ mod remote_button {
)
})
.when_some(left_icon, |this, left_icon| {
- this.child(
- h_flex()
- .ml_neg_0p5()
- .child(Icon::new(left_icon).size(IconSize::XSmall)),
- )
+ this.map(|this| {
+ if is_in_progress {
+ this.child(
+ Icon::new(IconName::LoadCircle)
+ .size(IconSize::XSmall)
+ .color(Color::Disabled)
+ .with_rotate_animation(2),
+ )
+ } else {
+ this.child(Icon::new(left_icon).size(IconSize::XSmall))
+ }
+ })
})
.child(
- div()
- .child(Label::new(left_label).size(LabelSize::Small))
+ Label::new(left_label)
+ .size(LabelSize::Small)
+ .when(is_in_progress, |this| this.color(Color::Disabled))
.mr_0p5(),
)
.on_click(left_on_click)
- .tooltip(tooltip);
+ .tooltip(move |window, cx| {
+ if let Some(operation) = in_progress_operation {
+ Tooltip::simple(in_progress_tooltip(operation), cx)
+ } else {
+ tooltip(window, cx)
+ }
+ });
let right = render_git_action_menu(
ElementId::Name(format!("split-button-right-{}", id).into()),