Wire in the branch name

This commit is contained in:
Mikayla Maki 2023-05-03 08:51:58 -07:00
parent 5b4e58d1de
commit 26afd592c5
No known key found for this signature in database
2 changed files with 23 additions and 5 deletions

View File

@ -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<()> {

View File

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