project: Change Git repo automatically with change in file buffer (#36796)

### Summary

* Auto-activates the active repository when opening a buffer.
* Prepares branching for future support of a user choice (e.g.,
`auto_activate_repo_on_open` flag).

### Release Notes

* **Improved**: Opening a buffer now automatically updates the active
repository.
This commit is contained in:
rufevean
2025-10-02 10:42:08 -04:00
committed by GitHub
parent bf44dc5ff5
commit d8698dffe3
2 changed files with 19 additions and 3 deletions
+9 -1
View File
@@ -439,6 +439,15 @@ impl GitStore {
pub fn is_local(&self) -> bool {
matches!(self.state, GitStoreState::Local { .. })
}
pub fn set_active_repo_for_path(&mut self, project_path: &ProjectPath, cx: &mut Context<Self>) {
if let Some((repo, _)) = self.repository_and_path_for_project_path(project_path, cx) {
let id = repo.read(cx).id;
if self.active_repo_id != Some(id) {
self.active_repo_id = Some(id);
cx.emit(GitStoreEvent::ActiveRepositoryChanged(Some(id)));
}
}
}
pub fn shared(&mut self, project_id: u64, client: AnyProtoClient, cx: &mut Context<Self>) {
match &mut self.state {
@@ -1111,7 +1120,6 @@ impl GitStore {
_ => {}
}
}
fn on_repository_event(
&mut self,
repo: Entity<Repository>,
+10 -2
View File
@@ -4400,8 +4400,16 @@ impl Workspace {
fn active_item_path_changed(&mut self, window: &mut Window, cx: &mut Context<Self>) {
cx.emit(Event::ActiveItemChanged);
let active_entry = self.active_project_path(cx);
self.project
.update(cx, |project, cx| project.set_active_path(active_entry, cx));
self.project.update(cx, |project, cx| {
project.set_active_path(active_entry.clone(), cx)
});
if let Some(project_path) = &active_entry {
let git_store_entity = self.project.read(cx).git_store().clone();
git_store_entity.update(cx, |git_store, cx| {
git_store.set_active_repo_for_path(project_path, cx);
});
}
self.update_window_title(window, cx);
}