Wire in the branch name

Mikayla Maki created

Change summary

crates/fs/src/repository.rs    | 13 +++++++++++++
crates/project/src/worktree.rs | 15 ++++++++++-----
2 files changed, 23 insertions(+), 5 deletions(-)

Detailed changes

crates/fs/src/repository.rs 🔗

@@ -5,6 +5,7 @@ use std::{
     path::{Component, Path, PathBuf},
     sync::Arc,
 };
+use util::ResultExt;
 
 pub use git2::Repository as LibGitRepository;
 
@@ -13,6 +14,8 @@ pub trait GitRepository: Send {
     fn reload_index(&self);
 
     fn load_index_text(&self, relative_file_path: &Path) -> Option<String>;
+
+    fn branch_name(&self) -> Option<String>;
 }
 
 impl std::fmt::Debug for dyn GitRepository {
@@ -52,6 +55,12 @@ impl GitRepository for LibGitRepository {
         }
         None
     }
+
+    fn branch_name(&self) -> Option<String> {
+        let head = self.head().log_err()?;
+        let branch = String::from_utf8_lossy(head.shorthand_bytes());
+        Some(branch.to_string())
+    }
 }
 
 #[derive(Debug, Clone, Default)]
@@ -78,6 +87,10 @@ impl GitRepository for FakeGitRepository {
         let state = self.state.lock();
         state.index_contents.get(path).cloned()
     }
+
+    fn branch_name(&self) -> Option<String> {
+        None
+    }
 }
 
 fn check_path_to_repo_path_errors(relative_file_path: &Path) -> Result<()> {

crates/project/src/worktree.rs 🔗

@@ -125,7 +125,7 @@ pub struct RepositoryEntry {
     pub(crate) git_dir_entry_id: ProjectEntryId,
     pub(crate) work_directory: RepositoryWorkDirectory,
     pub(crate) scan_id: usize,
-    // TODO: pub(crate) head_ref: Arc<str>,
+    pub(crate) branch: Option<Arc<str>>,
 }
 
 impl RepositoryEntry {
@@ -1687,6 +1687,7 @@ impl LocalSnapshot {
                             git_dir_entry_id: parent_entry.id,
                             work_directory: key,
                             scan_id: 0,
+                            branch: None,
                         },
                     );
 
@@ -2678,11 +2679,14 @@ impl BackgroundScanner {
 
                     let repo_with_path_in_dotgit = snapshot.repo_for_metadata(&path);
                     if let Some((key, repo)) = repo_with_path_in_dotgit {
-                        repo.lock().reload_index();
+                        let repo = repo.lock();
+                        repo.reload_index();
+                        let branch = repo.branch_name();
 
-                        snapshot
-                            .repository_entries
-                            .update(&key, |entry| entry.scan_id = scan_id);
+                        snapshot.repository_entries.update(&key, |entry| {
+                            entry.scan_id = scan_id;
+                            entry.branch = branch.map(Into::into)
+                        });
                     }
 
                     if let Some(scan_queue_tx) = &scan_queue_tx {
@@ -3514,6 +3518,7 @@ mod tests {
                 work_directory: RepositoryWorkDirectory(
                     Path::new(&format!("don't-care-{}", scan_id)).into(),
                 ),
+                branch: None,
             }
         }