From 3139c6b8a3811709e80f934478f61562b49d2ec3 Mon Sep 17 00:00:00 2001 From: filip Date: Sat, 14 Aug 2021 15:19:43 +0200 Subject: [PATCH] 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 --- src/context.rs | 39 ++++++++++++++------------------------- src/modules/git_state.rs | 2 +- src/modules/git_status.rs | 3 +++ 3 files changed, 18 insertions(+), 26 deletions(-) diff --git a/src/context.rs b/src/context.rs index ed63b34a..e70ffac0 100644 --- a/src/context.rs +++ b/src/context.rs @@ -221,31 +221,20 @@ 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 { - let repository = if env::var("GIT_DIR").is_ok() { - Repository::open_from_env().ok() - } 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)); - Ok(Repo { - branch, - root, - state, - remote, - }) + pub fn get_repo(&self) -> Result<&Repo, git2::Error> { + self.repo.get_or_try_init(|| -> Result { + let repository = if env::var("GIT_DIR").is_ok() { + Repository::open_from_env() + } else { + Repository::discover(&self.current_dir) + }?; + Ok(Repo { + branch: get_current_branch(&repository), + root: repository.workdir().map(Path::to_path_buf), + state: repository.state(), + remote: get_remote_repository_info(&repository), }) + }) } pub fn dir_contents(&self) -> Result<&DirContents, std::io::Error> { @@ -397,7 +386,7 @@ pub struct Repo { pub root: Option, /// State - pub state: Option, + pub state: RepositoryState, /// Remote repository pub remote: Option, diff --git a/src/modules/git_state.rs b/src/modules/git_state.rs index 8c68e9d2..62d395e7 100644 --- a/src/modules/git_state.rs +++ b/src/modules/git_state.rs @@ -15,7 +15,7 @@ pub fn module<'a>(context: &'a Context) -> Option> { 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)?; diff --git a/src/modules/git_status.rs b/src/modules/git_status.rs index 3f5ca02c..3d6a0775 100644 --- a/src/modules/git_status.rs +++ b/src/modules/git_status.rs @@ -31,6 +31,9 @@ pub fn module<'a>(context: &'a Context) -> Option> { 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 {