diff --git a/libimagstorestdhook/src/vcs/git/action.rs b/libimagstorestdhook/src/vcs/git/action.rs index 26caf5a0..277260f0 100644 --- a/libimagstorestdhook/src/vcs/git/action.rs +++ b/libimagstorestdhook/src/vcs/git/action.rs @@ -8,6 +8,18 @@ pub enum StoreAction { Delete, } +impl StoreAction { + + pub fn uppercase(&self) -> &str { + match *self { + StoreAction::Create => "CREATE", + StoreAction::Retrieve => "RETRIEVE", + StoreAction::Update => "UPDATE", + StoreAction::Delete => "DELETE", + } + } +} + impl Display for StoreAction { fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> { diff --git a/libimagstorestdhook/src/vcs/git/create.rs b/libimagstorestdhook/src/vcs/git/create.rs index 54845cdd..fcba4535 100644 --- a/libimagstorestdhook/src/vcs/git/create.rs +++ b/libimagstorestdhook/src/vcs/git/create.rs @@ -92,6 +92,7 @@ impl StoreIdAccessor for CreateHook { use vcs::git::action::StoreAction; use vcs::git::config::commit_message; use vcs::git::error::MapIntoHookError; + use vcs::git::util::fetch_index; debug!("[GIT CREATE HOOK]: {:?}", id); @@ -102,32 +103,12 @@ impl StoreIdAccessor for CreateHook { .map_into_hook_error() ); - let cfg = try!( - self.runtime - .config_value_or_err() - .map_dbg_err_str("[GIT CREATE HOOK]: Couldn't get Value object from config") - ); + let action = StoreAction::Create; + try!(self.runtime.ensure_cfg_branch_is_checked_out(&action)); - debug!("[GIT CREATE HOOK]: Ensuring branch checkout"); - try!(self.runtime.ensure_cfg_branch_is_checked_out()); - debug!("[GIT CREATE HOOK]: Branch checked out"); - - debug!("[GIT CREATE HOOK]: Getting repository"); - let repo = try!( - self.runtime - .repository() - .map_dbg_err_str("[GIT CREATE HOOK]: Couldn't fetch Repository") - .map_err_into(GHEK::RepositoryError) - .map_into_hook_error() - ); - debug!("[GIT CREATE HOOK]: Repository object fetched"); - - let mut index = try!( - repo - .index() - .map_err_into(GHEK::RepositoryIndexFetchingError) - .map_into_hook_error() - ); + let cfg = try!(self.runtime.config_value_or_err(&action)); + let repo = try!(self.runtime.repository(&action)); + let mut index = try!(fetch_index(repo, &action)); let file_status = try!( repo diff --git a/libimagstorestdhook/src/vcs/git/runtime.rs b/libimagstorestdhook/src/vcs/git/runtime.rs index 54334f44..13902b7d 100644 --- a/libimagstorestdhook/src/vcs/git/runtime.rs +++ b/libimagstorestdhook/src/vcs/git/runtime.rs @@ -10,6 +10,7 @@ use libimagstore::hook::error::HookErrorKind as HEK; use libimagstore::hook::result::HookResult; use libimagutil::debug_result::*; +use vcs::git::action::StoreAction; use vcs::git::result::Result; use vcs::git::error::{MapErrInto, GitHookErrorKind as GHEK}; @@ -47,7 +48,7 @@ impl Runtime { self.config.is_some() } - pub fn config_value_or_err(&self) -> HookResult<&Value> { + pub fn config_value_or_err(&self, action: &StoreAction) -> HookResult<&Value> { self.config .as_ref() .ok_or(GHEK::NoConfigError.into_error()) @@ -55,21 +56,30 @@ impl Runtime { .map_err(Box::new) .map_err(|e| HEK::HookExecutionError.into_error_with_cause(e)) .map_err(|mut e| e.with_custom_data(CustomData::default().aborting(false))) + .map_dbg_err(|_| { + format!("[GIT {} HOOK]: Couldn't get Value object from config", action.uppercase()) + }) } - pub fn repository(&self) -> HookResult<&Repository> { + pub fn repository(&self, action: &StoreAction) -> HookResult<&Repository> { + use vcs::git::error::MapIntoHookError; + + debug!("[GIT {} HOOK]: Getting repository", action.uppercase()); self.repository .as_ref() .ok_or(GHEK::MkRepo.into_error()) - .map_err(Box::new) - .map_err(|e| HEK::HookExecutionError.into_error_with_cause(e)) + .map_err_into(GHEK::RepositoryError) + .map_into_hook_error() + .map_dbg_err(|_| format!("[GIT {} HOOK]: Couldn't fetch Repository", action.uppercase())) + .map_dbg(|_| format!("[GIT {} HOOK]: Repository object fetched", action.uppercase())) } - pub fn ensure_cfg_branch_is_checked_out(&self) -> HookResult<()> { + pub fn ensure_cfg_branch_is_checked_out(&self, action: &StoreAction) -> HookResult<()> { use vcs::git::config::ensure_branch; + debug!("[GIT CREATE HOOK]: Ensuring branch checkout"); let head = try!(self - .repository() + .repository(action) .and_then(|r| { debug!("Repository fetched, getting head"); r.head() @@ -119,6 +129,7 @@ impl Runtime { } .map_err(Box::new) .map_err(|e| HEK::HookExecutionError.into_error_with_cause(e)) + .map_dbg_str("[GIT CREATE HOOK]: Branch checked out") } } diff --git a/libimagstorestdhook/src/vcs/git/update.rs b/libimagstorestdhook/src/vcs/git/update.rs index b0d2d05e..fa4af2c9 100644 --- a/libimagstorestdhook/src/vcs/git/update.rs +++ b/libimagstorestdhook/src/vcs/git/update.rs @@ -87,31 +87,14 @@ impl StoreIdAccessor for UpdateHook { use vcs::git::action::StoreAction; use vcs::git::config::commit_message; use vcs::git::error::MapIntoHookError; + use vcs::git::util::fetch_index; debug!("[GIT UPDATE HOOK]: {:?}", id); - let cfg = try!( - self.runtime - .config_value_or_err() - .map_dbg_err_str("[GIT UPDATE HOOK]: Couldn't get Value object from config") - ); - - debug!("[GIT UPDATE HOOK]: Getting repository"); - let repo = try!( - self.runtime - .repository() - .map_dbg_err_str("[GIT UPDATE HOOK]: Couldn't fetch Repository") - .map_err_into(GHEK::RepositoryError) - .map_into_hook_error() - ); - debug!("[GIT UPDATE HOOK]: Repository object fetched"); - - let mut index = try!( - repo - .index() - .map_err_into(GHEK::RepositoryIndexFetchingError) - .map_into_hook_error() - ); + let action = StoreAction::Update; + let cfg = try!(self.runtime.config_value_or_err(&action)); + let repo = try!(self.runtime.repository(&action)); + let mut index = try!(fetch_index(repo, &action)); let tree_id = try!( index.write_tree() diff --git a/libimagstorestdhook/src/vcs/git/util.rs b/libimagstorestdhook/src/vcs/git/util.rs index e57e6acc..56b66323 100644 --- a/libimagstorestdhook/src/vcs/git/util.rs +++ b/libimagstorestdhook/src/vcs/git/util.rs @@ -2,27 +2,23 @@ //! //! Contains primitives to create a repository within the store path -use git2::Repository; -use git2::RepositoryInitOptions; - -use libimagstore::store::Store; +use git2::{Repository, Index}; use vcs::git::error::GitHookErrorKind as GHEK; use vcs::git::error::MapErrInto; -use vcs::git::result::Result; +use vcs::git::runtime::Runtime as GRuntime; +use vcs::git::action::StoreAction; +use vcs::git::error::MapIntoHookError; -pub fn mkrepo(store: &Store) -> Result<()> { - let mut opts = RepositoryInitOptions::new(); - opts.bare(false); - opts.no_reinit(true); - opts.mkdir(false); - opts.external_template(false); - Repository::init_opts(store.path(), &opts) - .map(|_| ()) - .map_err_into(GHEK::MkRepo) -} - -pub fn hasrepo(store: &Store) -> bool { - Repository::open(store.path()).is_ok() +use libimagutil::debug_result::*; +use libimagstore::hook::error::HookError; + +pub fn fetch_index(repo: &Repository, action: &StoreAction) -> Result { + 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() }