summaryrefslogtreecommitdiffstats
path: root/src/modules/git_state.rs
diff options
context:
space:
mode:
authorNeil Kistner <neil.kistner@gmail.com>2019-09-09 18:14:38 -0500
committerMatan Kushner <hello@matchai.me>2019-09-09 19:14:38 -0400
commit9f70ffb7a7d2eeef64c360c553e909d38e44adf0 (patch)
tree4dccf8a935e027dcf7bc77781eed1554a645d72a /src/modules/git_state.rs
parentdc8409333e194624896c74c5e5cdb4f664f53f9d (diff)
fix: Lazy load git repo and only run module if not disabled (#306)
A couple of optimizations are done in this PR. One, we now will check config ahead of time to see if a module is disabled before running any module code. Also, we won't try to discover a git repository unless the module requests access to it.
Diffstat (limited to 'src/modules/git_state.rs')
-rw-r--r--src/modules/git_state.rs41
1 files changed, 17 insertions, 24 deletions
diff --git a/src/modules/git_state.rs b/src/modules/git_state.rs
index 8d242b0ca..c26a6007b 100644
--- a/src/modules/git_state.rs
+++ b/src/modules/git_state.rs
@@ -1,6 +1,6 @@
use ansi_term::Color;
-use git2::{Repository, RepositoryState};
-use std::path::Path;
+use git2::RepositoryState;
+use std::path::{Path, PathBuf};
use super::{Context, Module};
@@ -9,11 +9,12 @@ use super::{Context, Module};
/// During a git operation it will show: REBASING, BISECTING, MERGING, etc.
/// If the progress information is available (e.g. rebasing 3/10), it will show that too.
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
- let mut module = context.new_module("git_state")?;
+ let mut module = context.new_module("git_state");
- let repo_root = context.repo_root.as_ref()?;
- let mut repository = Repository::open(repo_root).ok()?;
- let state_description = get_state_description(&mut repository);
+ let repo = context.get_repo().ok()?;
+ let repo_root = repo.root.as_ref()?;
+ let repo_state = repo.state?;
+ let state_description = get_state_description(repo_state, repo_root);
if let StateDescription::Clean = state_description {
return None;
@@ -79,8 +80,11 @@ static AM_OR_REBASE_LABEL: StateLabel = StateLabel {
message_default: "AM/REBASE",
};
-fn get_state_description(repository: &mut Repository) -> StateDescription {
- match repository.state() {
+/// Returns the state of the current repository
+///
+/// During a git operation it will show: REBASING, BISECTING, MERGING, etc.
+fn get_state_description(state: RepositoryState, root: &PathBuf) -> StateDescription {
+ match state {
RepositoryState::Clean => StateDescription::Clean,
RepositoryState::Merge => StateDescription::Label(&MERGE_LABEL),
RepositoryState::Revert => StateDescription::Label(&REVERT_LABEL),
@@ -90,13 +94,13 @@ fn get_state_description(repository: &mut Repository) -> StateDescription {
RepositoryState::Bisect => StateDescription::Label(&BISECT_LABEL),
RepositoryState::ApplyMailbox => StateDescription::Label(&AM_LABEL),
RepositoryState::ApplyMailboxOrRebase => StateDescription::Label(&AM_OR_REBASE_LABEL),
- RepositoryState::Rebase => describe_rebase(repository),
- RepositoryState::RebaseInteractive => describe_rebase(repository),
- RepositoryState::RebaseMerge => describe_rebase(repository),
+ RepositoryState::Rebase => describe_rebase(root),
+ RepositoryState::RebaseInteractive => describe_rebase(root),
+ RepositoryState::RebaseMerge => describe_rebase(root),
}
}
-fn describe_rebase(repository: &mut Repository) -> StateDescription {
+fn describe_rebase(root: &PathBuf) -> StateDescription {
/*
* Sadly, libgit2 seems to have some issues with reading the state of
* interactive rebases. So, instead, we'll poke a few of the .git files
@@ -107,18 +111,7 @@ fn describe_rebase(repository: &mut Repository) -> StateDescription {
let just_label = StateDescription::Label(&REBASE_LABEL);
- let dot_git = repository
- .workdir()
- .and_then(|d| Some(d.join(Path::new(".git"))));
-
- let dot_git = match dot_git {
- None => {
- // We didn't find the .git directory.
- // Something very odd is going on. We'll just back away slowly.
- return just_label;
- }
- Some(path) => path,
- };
+ let dot_git = root.join(".git");
let has_path = |relative_path: &str| {
let path = dot_git.join(Path::new(relative_path));