mirror of
https://github.com/Llewellynvdm/starship.git
synced 2024-12-29 04:23:26 +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:
parent
fe42ec94bc
commit
3139c6b8a3
@ -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<Repo, std::io::Error> {
|
||||
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<Repo, git2::Error> {
|
||||
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<PathBuf>,
|
||||
|
||||
/// State
|
||||
pub state: Option<RepositoryState>,
|
||||
pub state: RepositoryState,
|
||||
|
||||
/// Remote repository
|
||||
pub remote: Option<Remote>,
|
||||
|
@ -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)?;
|
||||
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user