refactor(git_status): simplify git status with once-cell (#2150)

I simplified the code in the git status module by moving everything from RwLock<_> to OnceCell<_>. I think this should also get rid of any remaining race conditions that remained after #1777.
This commit is contained in:
David Knaack 2021-01-24 21:19:22 +01:00 committed by GitHub
parent a67bf5b34f
commit b2e8252785
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 27 additions and 68 deletions

View File

@ -1,4 +1,5 @@
use git2::{Repository, Status}; use git2::{Repository, Status};
use once_cell::sync::OnceCell;
use super::{Context, Module, RootModuleConfig}; use super::{Context, Module, RootModuleConfig};
@ -6,7 +7,7 @@ use crate::configs::git_status::GitStatusConfig;
use crate::context::Repo; use crate::context::Repo;
use crate::formatter::StringFormatter; use crate::formatter::StringFormatter;
use crate::segment::Segment; use crate::segment::Segment;
use std::sync::{Arc, RwLock}; use std::sync::Arc;
const ALL_STATUS_FORMAT: &str = "$conflicted$stashed$deleted$renamed$modified$staged$untracked"; const ALL_STATUS_FORMAT: &str = "$conflicted$stashed$deleted$renamed$modified$staged$untracked";
@ -108,18 +109,18 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
struct GitStatusInfo<'a> { struct GitStatusInfo<'a> {
repo: &'a Repo, repo: &'a Repo,
ahead_behind: RwLock<Option<Result<(usize, usize), git2::Error>>>, ahead_behind: OnceCell<Option<(usize, usize)>>,
repo_status: RwLock<Option<Result<RepoStatus, git2::Error>>>, repo_status: OnceCell<Option<RepoStatus>>,
stashed_count: RwLock<Option<Result<usize, git2::Error>>>, stashed_count: OnceCell<Option<usize>>,
} }
impl<'a> GitStatusInfo<'a> { impl<'a> GitStatusInfo<'a> {
pub fn load(repo: &'a Repo) -> Self { pub fn load(repo: &'a Repo) -> Self {
Self { Self {
repo, repo,
ahead_behind: RwLock::new(None), ahead_behind: OnceCell::new(),
repo_status: RwLock::new(None), repo_status: OnceCell::new(),
stashed_count: RwLock::new(None), stashed_count: OnceCell::new(),
} }
} }
@ -137,89 +138,47 @@ impl<'a> GitStatusInfo<'a> {
Repository::open(repo_root).ok() Repository::open(repo_root).ok()
} }
pub fn get_ahead_behind(&self) -> Option<(usize, usize)> { pub fn get_ahead_behind(&self) -> &Option<(usize, usize)> {
{ self.ahead_behind.get_or_init(|| {
let data = self.ahead_behind.read().unwrap();
if let Some(result) = data.as_ref() {
return match result.as_ref() {
Ok(ahead_behind) => Some(*ahead_behind),
Err(error) => {
log::debug!("get_ahead_behind: {}", error);
None
}
};
};
}
{
let mut data = self.ahead_behind.write().unwrap();
let repo = self.get_repository()?; let repo = self.get_repository()?;
let branch_name = self.get_branch_name(); let branch_name = self.get_branch_name();
*data = Some(get_ahead_behind(&repo, &branch_name));
match data.as_ref().unwrap() { match get_ahead_behind(&repo, &branch_name) {
Ok(ahead_behind) => Some(*ahead_behind), Ok(ahead_behind) => Some(ahead_behind),
Err(error) => { Err(error) => {
log::debug!("get_ahead_behind: {}", error); log::debug!("get_ahead_behind: {}", error);
None None
} }
} }
} })
} }
pub fn get_repo_status(&self) -> Option<RepoStatus> { pub fn get_repo_status(&self) -> &Option<RepoStatus> {
{ self.repo_status.get_or_init(|| {
let data = self.repo_status.read().unwrap();
if let Some(result) = data.as_ref() {
return match result.as_ref() {
Ok(repo_status) => Some(*repo_status),
Err(error) => {
log::debug!("get_repo_status: {}", error);
None
}
};
};
}
{
let mut data = self.repo_status.write().unwrap();
let mut repo = self.get_repository()?; let mut repo = self.get_repository()?;
*data = Some(get_repo_status(&mut repo));
match data.as_ref().unwrap() { match get_repo_status(&mut repo) {
Ok(repo_status) => Some(*repo_status), Ok(repo_status) => Some(repo_status),
Err(error) => { Err(error) => {
log::debug!(" get_repo_status: {}", error); log::debug!("get_repo_status: {}", error);
None None
} }
} }
} })
} }
pub fn get_stashed(&self) -> Option<usize> { pub fn get_stashed(&self) -> &Option<usize> {
{ self.stashed_count.get_or_init(|| {
let data = self.stashed_count.read().unwrap();
if let Some(result) = data.as_ref() {
return match result.as_ref() {
Ok(stashed_count) => Some(*stashed_count),
Err(error) => {
log::debug!("get_stashed_count: {}", error);
None
}
};
};
}
{
let mut data = self.stashed_count.write().unwrap();
let mut repo = self.get_repository()?; let mut repo = self.get_repository()?;
*data = Some(get_stashed_count(&mut repo));
match data.as_ref().unwrap() { match get_stashed_count(&mut repo) {
Ok(stashed_count) => Some(*stashed_count), Ok(stashed_count) => Some(stashed_count),
Err(error) => { Err(error) => {
log::debug!("get_stashed_count: {}", error); log::debug!("get_stashed_count: {}", error);
None None
} }
} }
} })
} }
pub fn get_conflicted(&self) -> Option<usize> { pub fn get_conflicted(&self) -> Option<usize> {