diff --git a/crates/fs/src/repository.rs b/crates/fs/src/repository.rs index 75f9ca34dde98c445add2b84056c3eda583d6a88..d22d670a8bd874f37d3a22bcb2208bc27bca4ee7 100644 --- a/crates/fs/src/repository.rs +++ b/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; + + fn branch_name(&self) -> Option; } impl std::fmt::Debug for dyn GitRepository { @@ -52,6 +55,12 @@ impl GitRepository for LibGitRepository { } None } + + fn branch_name(&self) -> Option { + 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 { + None + } } fn check_path_to_repo_path_errors(relative_file_path: &Path) -> Result<()> { diff --git a/crates/project/src/worktree.rs b/crates/project/src/worktree.rs index e197e0ffad50577a4db3fbf96114a39af07735a6..d663fc8b1145038cf05a6a50f4e3f2ef63cfc98f 100644 --- a/crates/project/src/worktree.rs +++ b/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, + pub(crate) branch: Option>, } 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, } }