1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-12-01 01:04:00 +00:00

feat(git_status): dont render module output if not in git repository (#2897)

* refactor get_repo method to not swallow error

* module git status don't render if not in git repository
This commit is contained in:
filip 2021-08-14 15:19:43 +02:00 committed by GitHub
parent fe42ec94bc
commit 3139c6b8a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 26 deletions

View File

@ -221,29 +221,18 @@ impl<'a> Context<'a> {
}
/// Will lazily get repo root and branch when a module requests it.
pub fn get_repo(&self) -> Result<&Repo, std::io::Error> {
self.repo
.get_or_try_init(|| -> Result<Repo, std::io::Error> {
pub fn get_repo(&self) -> Result<&Repo, git2::Error> {
self.repo.get_or_try_init(|| -> Result<Repo, git2::Error> {
let repository = if env::var("GIT_DIR").is_ok() {
Repository::open_from_env().ok()
Repository::open_from_env()
} else {
Repository::discover(&self.current_dir).ok()
};
let branch = repository
.as_ref()
.and_then(|repo| get_current_branch(repo));
let root = repository
.as_ref()
.and_then(|repo| repo.workdir().map(Path::to_path_buf));
let state = repository.as_ref().map(git2::Repository::state);
let remote = repository
.as_ref()
.and_then(|repo| get_remote_repository_info(repo));
Repository::discover(&self.current_dir)
}?;
Ok(Repo {
branch,
root,
state,
remote,
branch: get_current_branch(&repository),
root: repository.workdir().map(Path::to_path_buf),
state: repository.state(),
remote: get_remote_repository_info(&repository),
})
})
}
@ -397,7 +386,7 @@ pub struct Repo {
pub root: Option<PathBuf>,
/// State
pub state: Option<RepositoryState>,
pub state: RepositoryState,
/// Remote repository
pub remote: Option<Remote>,

View File

@ -15,7 +15,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let repo = context.get_repo().ok()?;
let repo_root = repo.root.as_ref()?;
let repo_state = repo.state?;
let repo_state = repo.state;
let state_description = get_state_description(repo_state, repo_root, &config)?;

View File

@ -31,6 +31,9 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("git_status");
let config: GitStatusConfig = GitStatusConfig::try_load(module.config);
//Return None if not in git repository
context.get_repo().ok()?;
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter
.map_meta(|variable, _| match variable {