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

rufevean created

### 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.

Change summary

crates/project/src/git_store.rs   | 10 +++++++++-
crates/workspace/src/workspace.rs | 12 ++++++++++--
2 files changed, 19 insertions(+), 3 deletions(-)

Detailed changes

crates/project/src/git_store.rs 🔗

@@ -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>,

crates/workspace/src/workspace.rs 🔗

@@ -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);
     }