Refactor Index fetching into helper function

This commit is contained in:
Matthias Beyer 2016-09-07 21:47:39 +02:00
parent 032110e6d8
commit 4e2a980599
3 changed files with 18 additions and 23 deletions

View File

@ -92,7 +92,7 @@ impl StoreIdAccessor for CreateHook {
use vcs::git::action::StoreAction; use vcs::git::action::StoreAction;
use vcs::git::config::commit_message; use vcs::git::config::commit_message;
use vcs::git::error::MapIntoHookError; use vcs::git::error::MapIntoHookError;
use vcs::git::util::fetch_repo; use vcs::git::util::{fetch_repo, fetch_index};
debug!("[GIT CREATE HOOK]: {:?}", id); debug!("[GIT CREATE HOOK]: {:?}", id);
@ -113,16 +113,9 @@ impl StoreIdAccessor for CreateHook {
try!(self.runtime.ensure_cfg_branch_is_checked_out()); try!(self.runtime.ensure_cfg_branch_is_checked_out());
debug!("[GIT CREATE HOOK]: Branch checked out"); debug!("[GIT CREATE HOOK]: Branch checked out");
let action = StoreAction::Create; let action = StoreAction::Create;
let repo = try!(fetch_repo(&self.runtime, &action));
let repo = try!(fetch_repo(&self.runtime, &action)); let mut index = try!(fetch_index(repo, &action));
let mut index = try!(
repo
.index()
.map_err_into(GHEK::RepositoryIndexFetchingError)
.map_into_hook_error()
);
let file_status = try!( let file_status = try!(
repo repo

View File

@ -87,7 +87,7 @@ impl StoreIdAccessor for UpdateHook {
use vcs::git::action::StoreAction; use vcs::git::action::StoreAction;
use vcs::git::config::commit_message; use vcs::git::config::commit_message;
use vcs::git::error::MapIntoHookError; use vcs::git::error::MapIntoHookError;
use vcs::git::util::fetch_repo; use vcs::git::util::{fetch_repo, fetch_index};
debug!("[GIT UPDATE HOOK]: {:?}", id); debug!("[GIT UPDATE HOOK]: {:?}", id);
@ -97,16 +97,9 @@ impl StoreIdAccessor for UpdateHook {
.map_dbg_err_str("[GIT UPDATE HOOK]: Couldn't get Value object from config") .map_dbg_err_str("[GIT UPDATE HOOK]: Couldn't get Value object from config")
); );
let action = StoreAction::Update; let action = StoreAction::Update;
let repo = try!(fetch_repo(&self.runtime, &action));
let repo = try!(fetch_repo(&self.runtime, &action)); let mut index = try!(fetch_index(repo, &action));
let mut index = try!(
repo
.index()
.map_err_into(GHEK::RepositoryIndexFetchingError)
.map_into_hook_error()
);
let tree_id = try!( let tree_id = try!(
index.write_tree() index.write_tree()

View File

@ -2,7 +2,7 @@
//! //!
//! Contains primitives to create a repository within the store path //! Contains primitives to create a repository within the store path
use git2::Repository; use git2::{Repository, Index};
use vcs::git::error::GitHookErrorKind as GHEK; use vcs::git::error::GitHookErrorKind as GHEK;
use vcs::git::error::MapErrInto; use vcs::git::error::MapErrInto;
@ -24,3 +24,12 @@ pub fn fetch_repo<'a>(runtime: &'a GRuntime, action: &StoreAction) -> Result<&'a
.map_into_hook_error() .map_into_hook_error()
} }
pub fn fetch_index(repo: &Repository, action: &StoreAction) -> Result<Index, HookError> {
debug!("[GIT {} HOOK]: Getting Index", action.uppercase());
repo.index()
.map_dbg_err(|_| format!("[GIT {} HOOK]: Couldn't fetch Index", action.uppercase()))
.map_dbg(|_| format!("[GIT {} HOOK]: Index object fetched", action.uppercase()))
.map_err_into(GHEK::RepositoryIndexFetchingError)
.map_into_hook_error()
}