Skip to content

Commit

Permalink
refactor(git_status): simplify git status with once-cell (starship#2150)
Browse files Browse the repository at this point in the history
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 starship#1777.
  • Loading branch information
davidkna authored Jan 24, 2021
1 parent a67bf5b commit b2e8252
Showing 1 changed file with 27 additions and 68 deletions.
95 changes: 27 additions & 68 deletions src/modules/git_status.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use git2::{Repository, Status};
use once_cell::sync::OnceCell;

use super::{Context, Module, RootModuleConfig};

use crate::configs::git_status::GitStatusConfig;
use crate::context::Repo;
use crate::formatter::StringFormatter;
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";

Expand Down Expand Up @@ -108,18 +109,18 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {

struct GitStatusInfo<'a> {
repo: &'a Repo,
ahead_behind: RwLock<Option<Result<(usize, usize), git2::Error>>>,
repo_status: RwLock<Option<Result<RepoStatus, git2::Error>>>,
stashed_count: RwLock<Option<Result<usize, git2::Error>>>,
ahead_behind: OnceCell<Option<(usize, usize)>>,
repo_status: OnceCell<Option<RepoStatus>>,
stashed_count: OnceCell<Option<usize>>,
}

impl<'a> GitStatusInfo<'a> {
pub fn load(repo: &'a Repo) -> Self {
Self {
repo,
ahead_behind: RwLock::new(None),
repo_status: RwLock::new(None),
stashed_count: RwLock::new(None),
ahead_behind: OnceCell::new(),
repo_status: OnceCell::new(),
stashed_count: OnceCell::new(),
}
}

Expand All @@ -137,89 +138,47 @@ impl<'a> GitStatusInfo<'a> {
Repository::open(repo_root).ok()
}

pub fn get_ahead_behind(&self) -> Option<(usize, usize)> {
{
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();
pub fn get_ahead_behind(&self) -> &Option<(usize, usize)> {
self.ahead_behind.get_or_init(|| {
let repo = self.get_repository()?;
let branch_name = self.get_branch_name();
*data = Some(get_ahead_behind(&repo, &branch_name));
match data.as_ref().unwrap() {
Ok(ahead_behind) => Some(*ahead_behind),

match get_ahead_behind(&repo, &branch_name) {
Ok(ahead_behind) => Some(ahead_behind),
Err(error) => {
log::debug!("get_ahead_behind: {}", error);
None
}
}
}
})
}

pub fn get_repo_status(&self) -> Option<RepoStatus> {
{
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();
pub fn get_repo_status(&self) -> &Option<RepoStatus> {
self.repo_status.get_or_init(|| {
let mut repo = self.get_repository()?;
*data = Some(get_repo_status(&mut repo));
match data.as_ref().unwrap() {
Ok(repo_status) => Some(*repo_status),

match get_repo_status(&mut repo) {
Ok(repo_status) => Some(repo_status),
Err(error) => {
log::debug!(" get_repo_status: {}", error);
log::debug!("get_repo_status: {}", error);
None
}
}
}
})
}

pub fn get_stashed(&self) -> Option<usize> {
{
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();
pub fn get_stashed(&self) -> &Option<usize> {
self.stashed_count.get_or_init(|| {
let mut repo = self.get_repository()?;
*data = Some(get_stashed_count(&mut repo));
match data.as_ref().unwrap() {
Ok(stashed_count) => Some(*stashed_count),

match get_stashed_count(&mut repo) {
Ok(stashed_count) => Some(stashed_count),
Err(error) => {
log::debug!("get_stashed_count: {}", error);
None
}
}
}
})
}

pub fn get_conflicted(&self) -> Option<usize> {
Expand Down

0 comments on commit b2e8252

Please sign in to comment.