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: Do not recompute git_access on every file change (#59521)

Lukas Geigerb77ec90b2e65
diff --git a/crates/git_ui/src/git_panel.rs b/crates/git_ui/src/git_panel.rs
index 60afc057b7a..6875ddea218 100644
--- a/crates/git_ui/src/git_panel.rs
+++ b/crates/git_ui/src/git_panel.rs
@@ -803,7 +803,7 @@ pub struct GitPanel {
_repo_subscriptions: Vec<Subscription>,
_settings_subscription: Subscription,
- git_access: GitAccess,
+ git_access: Option<GitAccess>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
@@ -1004,10 +1004,18 @@ impl GitPanel {
)
| GitStoreEvent::RepositoryAdded
| GitStoreEvent::RepositoryRemoved(_)
- | GitStoreEvent::GlobalConfigurationUpdated
| GitStoreEvent::ActiveRepositoryChanged(_) => {
this.schedule_update(window, cx);
}
+ GitStoreEvent::RepositoryUpdated(
+ _,
+ RepositoryEvent::GitDirectoryChanged,
+ true,
+ )
+ | GitStoreEvent::GlobalConfigurationUpdated => {
+ this.git_access = None;
+ this.schedule_update(window, cx);
+ }
GitStoreEvent::IndexWriteError(error) => {
this.workspace
.update(cx, |workspace, cx| {
@@ -1075,7 +1083,7 @@ impl GitPanel {
_commit_message_buffer_subscription: None,
_repo_subscriptions: Vec::new(),
_settings_subscription,
- git_access: GitAccess::Yes,
+ git_access: None,
};
this.schedule_update(window, cx);
@@ -4122,7 +4130,11 @@ impl GitPanel {
.as_ref()
.and_then(|op| self.entry_by_path(&op.anchor));
- self.active_repository = self.project.read(cx).active_repository(cx);
+ let active_repository = self.project.read(cx).active_repository(cx);
+ if active_repository != self.active_repository {
+ self.active_repository = active_repository;
+ self.git_access = None;
+ }
self.entries.clear();
self.entries_indices.clear();
self.single_staged_entry.take();
@@ -4137,7 +4149,6 @@ impl GitPanel {
self.tracked_staged_count = 0;
self.entry_count = 0;
self.max_width_item_index = None;
- self.git_access = GitAccess::Yes;
let settings = GitPanelSettings::get_global(cx);
let sort_by = settings.sort_by;
@@ -4145,28 +4156,30 @@ impl GitPanel {
let is_tree_view = matches!(self.view_mode, GitPanelViewMode::Tree(_));
if let Some(active_repo) = self.active_repository.as_ref() {
- let access = active_repo.update(cx, |active_repo, cx| active_repo.access(cx));
-
- cx.spawn_in(window, async move |git_panel, cx| {
- // When the user does not own the `.git` folder, the
- // `GitStore.spawn_local_git_worker` will fail to create the
- // receiver for Git jobs, so this access check will be
- // cancelled.
- //
- // We assume `GitAccess::No` on cancellation. I believe this is
- // imprecise, other failures could also cause cancellation, but
- // the consequence is just showing the "unsafe repo" UI, which
- // seems acceptable for this edge case.
- let access = match access.await {
- Ok(access) => access,
- Err(Canceled) => GitAccess::No,
- };
+ if self.git_access.is_none() {
+ let access = active_repo.update(cx, |active_repo, cx| active_repo.access(cx));
+
+ cx.spawn_in(window, async move |git_panel, cx| {
+ // When the user does not own the `.git` folder, the
+ // `GitStore.spawn_local_git_worker` will fail to create the
+ // receiver for Git jobs, so this access check will be
+ // cancelled.
+ //
+ // We assume `GitAccess::No` on cancellation. I believe this is
+ // imprecise, other failures could also cause cancellation, but
+ // the consequence is just showing the "unsafe repo" UI, which
+ // seems acceptable for this edge case.
+ let access = match access.await {
+ Ok(access) => access,
+ Err(Canceled) => GitAccess::No,
+ };
- git_panel.update(cx, |this, _cx| {
- this.git_access = access;
+ git_panel.update(cx, |this, _cx| {
+ this.git_access = Some(access);
+ })
})
- })
- .detach_and_log_err(cx);
+ .detach_and_log_err(cx);
+ }
}
let mut changed_entries = Vec::new();
@@ -5066,7 +5079,7 @@ impl GitPanel {
_window: &mut Window,
cx: &mut Context<Self>,
) -> Option<impl IntoElement> {
- if matches!(self.git_access, GitAccess::No) {
+ if matches!(self.git_access, Some(GitAccess::No)) {
return None;
}
@@ -6039,7 +6052,7 @@ impl GitPanel {
fn render_empty_state(&self, cx: &mut Context<Self>) -> impl IntoElement {
let content = match (self.git_access, &self.active_repository) {
- (GitAccess::No, Some(repository)) => self.render_unsafe_repo_ui(repository, cx),
+ (Some(GitAccess::No), Some(repository)) => self.render_unsafe_repo_ui(repository, cx),
(_, None) => self.render_uninitialized_ui(cx),
(_, Some(_)) => self.render_no_changes_ui(cx),
};
diff --git a/crates/project/src/git_store.rs b/crates/project/src/git_store.rs
index 778743163d6..fd3efdae5c8 100644
--- a/crates/project/src/git_store.rs
+++ b/crates/project/src/git_store.rs
@@ -170,14 +170,13 @@ enum DiffKind {
SinceOid(Option<git::Oid>),
}
-#[derive(Debug, Default, Clone, Copy)]
+#[derive(Debug, Clone, Copy)]
pub enum GitAccess {
/// Either:
/// - the user owns `.git`
/// - the user doesn't own `.git`, but has both of:
/// - OS-level read permissions
/// - the directory is marked as safe (git config safe.directory)
- #[default]
Yes,
/// The user is not the owner of `.git`, and one of the following is true:
@@ -486,6 +485,7 @@ pub enum RepositoryEvent {
GitWorktreeListChanged,
PendingOpsChanged { pending_ops: SumTree<PendingOps> },
GraphEvent((LogSource, LogOrder), GitGraphEvent),
+ GitDirectoryChanged,
}
#[derive(Clone, Debug)]
@@ -2134,6 +2134,7 @@ impl GitStore {
|| update.new_work_directory_abs_path.as_ref() == Some(repo_abs_path)
}) {
repository.reload_buffer_diff_bases(cx);
+ cx.emit(RepositoryEvent::GitDirectoryChanged);
}
});
}