From c9c535f46c8abb5c673706e638f50da69a1b26d5 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 7 Sep 2016 20:54:35 +0200 Subject: [PATCH 01/15] Add Runtime for UpdateHook --- libimagstorestdhook/src/vcs/git/update.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/libimagstorestdhook/src/vcs/git/update.rs b/libimagstorestdhook/src/vcs/git/update.rs index 4a5026d0..388a1929 100644 --- a/libimagstorestdhook/src/vcs/git/update.rs +++ b/libimagstorestdhook/src/vcs/git/update.rs @@ -9,21 +9,24 @@ use libimagstore::hook::position::HookPosition; use libimagstore::hook::accessor::{HookDataAccessor, HookDataAccessorProvider}; use libimagstore::hook::accessor::StoreIdAccessor; +use vcs::git::runtime::Runtime as GRuntime; + #[derive(Debug)] pub struct UpdateHook { storepath: PathBuf, + runtime: GRuntime, + position: HookPosition, - config: Option, } impl UpdateHook { pub fn new(storepath: PathBuf, p: HookPosition) -> UpdateHook { UpdateHook { + runtime: GRuntime::new(&storepath), storepath: storepath, position: p, - config: None, } } From bcf91780aaed3b7345e1ede3095c23200ac573b3 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 7 Sep 2016 20:54:44 +0200 Subject: [PATCH 02/15] impl Debug for UpdateHook --- libimagstorestdhook/src/vcs/git/update.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/libimagstorestdhook/src/vcs/git/update.rs b/libimagstorestdhook/src/vcs/git/update.rs index 388a1929..86cdb6d3 100644 --- a/libimagstorestdhook/src/vcs/git/update.rs +++ b/libimagstorestdhook/src/vcs/git/update.rs @@ -11,7 +11,6 @@ use libimagstore::hook::accessor::StoreIdAccessor; use vcs::git::runtime::Runtime as GRuntime; -#[derive(Debug)] pub struct UpdateHook { storepath: PathBuf, @@ -32,6 +31,16 @@ impl UpdateHook { } +impl Debug for UpdateHook { + fn fmt(&self, fmt: &mut Formatter) -> RResult<(), FmtError> { + write!(fmt, "UpdateHook(storepath={:?}, repository={}, pos={:?}, cfg={:?}", + self.storepath, + (if self.runtime.has_repository() { "Some(_)" } else { "None" }), + self.position, + self.runtime.has_config()) + } +} + impl Hook for UpdateHook { fn name(&self) -> &'static str { From 6444f95b58e4fcf9adc56e0b4d1f7a06ef08055d Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 7 Sep 2016 20:54:57 +0200 Subject: [PATCH 03/15] Reimplement Hook::set_config() for UpdateHook --- libimagstorestdhook/src/vcs/git/update.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libimagstorestdhook/src/vcs/git/update.rs b/libimagstorestdhook/src/vcs/git/update.rs index 86cdb6d3..df1fba18 100644 --- a/libimagstorestdhook/src/vcs/git/update.rs +++ b/libimagstorestdhook/src/vcs/git/update.rs @@ -48,7 +48,9 @@ impl Hook for UpdateHook { } fn set_config(&mut self, config: &Value) { - self.config = Some(config.clone()); + if let Err(e) = self.runtime.set_config(config) { + trace_error(&e); + } } } From 242fa863b2379063f09e33d15fbd69c8cebb1ae2 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 7 Sep 2016 21:13:05 +0200 Subject: [PATCH 04/15] The helper commit_message() really should return an Result --- libimagstorestdhook/src/vcs/git/config.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libimagstorestdhook/src/vcs/git/config.rs b/libimagstorestdhook/src/vcs/git/config.rs index 0a9d5907..a7e262d6 100644 --- a/libimagstorestdhook/src/vcs/git/config.rs +++ b/libimagstorestdhook/src/vcs/git/config.rs @@ -13,11 +13,11 @@ pub fn commit_interactive(config: &Value) -> bool { false } -pub fn commit_message(config: &Value, action: StoreAction) -> String { +pub fn commit_message(config: &Value, action: StoreAction) -> Result { if commit_interactive(config) { unimplemented!() } else { - String::from("Dummy commit") + Ok(String::from("Dummy commit")) } } From 6ef6262e54d8cb83fec2ad7f7f6410c180baf2a9 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 7 Sep 2016 21:13:29 +0200 Subject: [PATCH 05/15] Add error kind for parent fetching error --- libimagstorestdhook/src/vcs/git/error.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/libimagstorestdhook/src/vcs/git/error.rs b/libimagstorestdhook/src/vcs/git/error.rs index 1ad15a3d..a8a00e14 100644 --- a/libimagstorestdhook/src/vcs/git/error.rs +++ b/libimagstorestdhook/src/vcs/git/error.rs @@ -24,6 +24,7 @@ generate_error_module!( RepositoryCommittingError => "Error while committing", RepositoryHeadFetchingError => "Error while fetching HEAD", RepositoryHeadTargetFetchingError => "Error while fetching target of HEAD", + RepositoryParentFetchingError => "Error while fetching parent of commit", HeadFetchError => "Error while getting HEAD", NotOnBranch => "No Branch is checked out", MkRepo => "Repository creation error", From 513a99fca76dd3c12909c912771e48ecb678642c Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 7 Sep 2016 21:13:40 +0200 Subject: [PATCH 06/15] Add initial implementation for UpdateHook --- libimagstorestdhook/src/vcs/git/update.rs | 100 ++++++++++++++++++++-- 1 file changed, 95 insertions(+), 5 deletions(-) diff --git a/libimagstorestdhook/src/vcs/git/update.rs b/libimagstorestdhook/src/vcs/git/update.rs index df1fba18..b0d2d05e 100644 --- a/libimagstorestdhook/src/vcs/git/update.rs +++ b/libimagstorestdhook/src/vcs/git/update.rs @@ -1,14 +1,26 @@ use std::path::PathBuf; +use std::fmt::{Debug, Formatter, Error as FmtError}; +use std::result::Result as RResult; use toml::Value; -use libimagstore::storeid::StoreId; +use libimagerror::into::IntoError; +use libimagerror::trace::trace_error; use libimagstore::hook::Hook; -use libimagstore::hook::result::HookResult; -use libimagstore::hook::position::HookPosition; -use libimagstore::hook::accessor::{HookDataAccessor, HookDataAccessorProvider}; use libimagstore::hook::accessor::StoreIdAccessor; +use libimagstore::hook::accessor::{HookDataAccessor, HookDataAccessorProvider}; +use libimagstore::hook::error::HookError as HE; +use libimagstore::hook::error::HookErrorKind as HEK; +use libimagstore::hook::position::HookPosition; +use libimagstore::hook::result::HookResult; +use libimagstore::storeid::StoreId; +use libimagutil::debug_result::*; +use vcs::git::error::GitHookError as GHE; +use vcs::git::error::GitHookErrorKind as GHEK; +use vcs::git::error::MapErrInto; +use vcs::git::error::MapIntoHookError; +use vcs::git::result::Result; use vcs::git::runtime::Runtime as GRuntime; pub struct UpdateHook { @@ -64,9 +76,87 @@ impl HookDataAccessorProvider for UpdateHook { impl StoreIdAccessor for UpdateHook { + /// The implementation of the UpdateHook + /// + /// # Scope + /// + /// This hook takes the git index and commits it either interactively or with a default message, + /// if there is no configuration for an interactive commit. + /// fn access(&self, id: &StoreId) -> HookResult<()> { + use vcs::git::action::StoreAction; + use vcs::git::config::commit_message; + use vcs::git::error::MapIntoHookError; + debug!("[GIT UPDATE HOOK]: {:?}", id); - Ok(()) + + 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 tree_id = try!( + index.write_tree() + .map_err_into(GHEK::RepositoryIndexWritingError) + .map_into_hook_error() + ); + + let signature = try!( + repo.signature() + .map_err_into(GHEK::MkSignature) + .map_into_hook_error() + ); + + let head = try!( + repo.head() + .map_err_into(GHEK::HeadFetchError) + .map_into_hook_error() + ); + + let mut parents = Vec::new(); + { + let commit = try!( + repo.find_commit(head.target().unwrap()) + .map_err_into(GHEK::RepositoryParentFetchingError) + .map_into_hook_error() + ); + parents.push(commit); + } + + // for converting from Vec to Vec<&Commit> + let parents = parents.iter().collect::>(); + + let tree = try!( + repo.find_tree(tree_id) + .map_err_into(GHEK::RepositoryParentFetchingError) + .map_into_hook_error() + ); + + let message = try!(commit_message(cfg, StoreAction::Update)); + + repo.commit(Some("HEAD"), &signature, &signature, &message, &tree, &parents) + .map_err_into(GHEK::RepositoryCommittingError) + .map_into_hook_error() + .map(|_| ()) + } } From 9c2f4db03d3577243f53ca0da9c5bf6d7d856aec Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 7 Sep 2016 21:26:06 +0200 Subject: [PATCH 07/15] Remove unused fn: hasrepo() --- libimagstorestdhook/src/vcs/git/util.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/libimagstorestdhook/src/vcs/git/util.rs b/libimagstorestdhook/src/vcs/git/util.rs index e57e6acc..5a6f0b86 100644 --- a/libimagstorestdhook/src/vcs/git/util.rs +++ b/libimagstorestdhook/src/vcs/git/util.rs @@ -22,7 +22,3 @@ pub fn mkrepo(store: &Store) -> Result<()> { .map_err_into(GHEK::MkRepo) } -pub fn hasrepo(store: &Store) -> bool { - Repository::open(store.path()).is_ok() -} - From fddd6ec4cb8b36a163e955baa7214f48f34a9c25 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 7 Sep 2016 21:26:23 +0200 Subject: [PATCH 08/15] Remove unused fn: mkrepo() --- libimagstorestdhook/src/vcs/git/util.rs | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/libimagstorestdhook/src/vcs/git/util.rs b/libimagstorestdhook/src/vcs/git/util.rs index 5a6f0b86..0a022a9a 100644 --- a/libimagstorestdhook/src/vcs/git/util.rs +++ b/libimagstorestdhook/src/vcs/git/util.rs @@ -11,14 +11,3 @@ use vcs::git::error::GitHookErrorKind as GHEK; use vcs::git::error::MapErrInto; use vcs::git::result::Result; -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) -} - From 741ebff2da3c115e76ab7e732564628152b72de5 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 7 Sep 2016 21:32:11 +0200 Subject: [PATCH 09/15] Outsource repository fetching into helper fn --- libimagstorestdhook/src/vcs/git/create.rs | 13 +++++-------- libimagstorestdhook/src/vcs/git/update.rs | 13 +++++-------- libimagstorestdhook/src/vcs/git/util.rs | 19 +++++++++++++++---- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/libimagstorestdhook/src/vcs/git/create.rs b/libimagstorestdhook/src/vcs/git/create.rs index 54845cdd..d3df670d 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_repo; debug!("[GIT CREATE HOOK]: {:?}", id); @@ -112,15 +113,11 @@ impl StoreIdAccessor for CreateHook { 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() + let repo = try!(fetch_repo(&self.runtime, + "[GIT CREATE HOOK]: Getting repository", + "[GIT CREATE HOOK]: Couldn't fetch Repository", + "[GIT CREATE HOOK]: Repository object fetched") ); - debug!("[GIT CREATE HOOK]: Repository object fetched"); let mut index = try!( repo diff --git a/libimagstorestdhook/src/vcs/git/update.rs b/libimagstorestdhook/src/vcs/git/update.rs index b0d2d05e..e423c0de 100644 --- a/libimagstorestdhook/src/vcs/git/update.rs +++ b/libimagstorestdhook/src/vcs/git/update.rs @@ -87,6 +87,7 @@ 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_repo; debug!("[GIT UPDATE HOOK]: {:?}", id); @@ -96,15 +97,11 @@ impl StoreIdAccessor for UpdateHook { .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() + let repo = try!(fetch_repo(&self.runtime, + "[GIT UPDATE HOOK]: Getting repository", + "[GIT UPDATE HOOK]: Couldn't fetch Repository", + "[GIT UPDATE HOOK]: Repository object fetched") ); - debug!("[GIT UPDATE HOOK]: Repository object fetched"); let mut index = try!( repo diff --git a/libimagstorestdhook/src/vcs/git/util.rs b/libimagstorestdhook/src/vcs/git/util.rs index 0a022a9a..a87fc6dd 100644 --- a/libimagstorestdhook/src/vcs/git/util.rs +++ b/libimagstorestdhook/src/vcs/git/util.rs @@ -3,11 +3,22 @@ //! Contains primitives to create a repository within the store path use git2::Repository; -use git2::RepositoryInitOptions; - -use libimagstore::store::Store; 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; + +use libimagutil::debug_result::*; +use libimagstore::hook::error::HookError; + +pub fn fetch_repo<'a>(runtime: &'a GRuntime, action: &StoreAction) -> Result<&'a Repository, HookError> +{ + runtime + .repository() + .map_dbg_err_str(on_err_str) + .map_err_into(GHEK::RepositoryError) + .map_into_hook_error() +} From ca477441088b68a84dd8fa6f29a2f72686b78574 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 7 Sep 2016 21:37:08 +0200 Subject: [PATCH 10/15] Add StoreAction::uppercase() utility function For debugging output convenience --- libimagstorestdhook/src/vcs/git/action.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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> { From 032110e6d83229a8bc82b6e47f13ce91f155fa14 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 7 Sep 2016 21:37:56 +0200 Subject: [PATCH 11/15] Rewrite fetch_repo() so we have less overhead in calling this helper --- libimagstorestdhook/src/vcs/git/create.rs | 8 +++----- libimagstorestdhook/src/vcs/git/update.rs | 8 +++----- libimagstorestdhook/src/vcs/git/util.rs | 4 +++- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/libimagstorestdhook/src/vcs/git/create.rs b/libimagstorestdhook/src/vcs/git/create.rs index d3df670d..079b1b60 100644 --- a/libimagstorestdhook/src/vcs/git/create.rs +++ b/libimagstorestdhook/src/vcs/git/create.rs @@ -113,11 +113,9 @@ impl StoreIdAccessor for CreateHook { try!(self.runtime.ensure_cfg_branch_is_checked_out()); debug!("[GIT CREATE HOOK]: Branch checked out"); - let repo = try!(fetch_repo(&self.runtime, - "[GIT CREATE HOOK]: Getting repository", - "[GIT CREATE HOOK]: Couldn't fetch Repository", - "[GIT CREATE HOOK]: Repository object fetched") - ); + let action = StoreAction::Create; + + let repo = try!(fetch_repo(&self.runtime, &action)); let mut index = try!( repo diff --git a/libimagstorestdhook/src/vcs/git/update.rs b/libimagstorestdhook/src/vcs/git/update.rs index e423c0de..1c91fa78 100644 --- a/libimagstorestdhook/src/vcs/git/update.rs +++ b/libimagstorestdhook/src/vcs/git/update.rs @@ -97,11 +97,9 @@ impl StoreIdAccessor for UpdateHook { .map_dbg_err_str("[GIT UPDATE HOOK]: Couldn't get Value object from config") ); - let repo = try!(fetch_repo(&self.runtime, - "[GIT UPDATE HOOK]: Getting repository", - "[GIT UPDATE HOOK]: Couldn't fetch Repository", - "[GIT UPDATE HOOK]: Repository object fetched") - ); + let action = StoreAction::Update; + + let repo = try!(fetch_repo(&self.runtime, &action)); let mut index = try!( repo diff --git a/libimagstorestdhook/src/vcs/git/util.rs b/libimagstorestdhook/src/vcs/git/util.rs index a87fc6dd..6bdd954d 100644 --- a/libimagstorestdhook/src/vcs/git/util.rs +++ b/libimagstorestdhook/src/vcs/git/util.rs @@ -15,9 +15,11 @@ use libimagstore::hook::error::HookError; pub fn fetch_repo<'a>(runtime: &'a GRuntime, action: &StoreAction) -> Result<&'a Repository, HookError> { + debug!("[GIT {} HOOK]: Getting repository", action.uppercase()); runtime .repository() - .map_dbg_err_str(on_err_str) + .map_dbg_err(|_| format!("[GIT {} HOOK]: Couldn't fetch Repository", action.uppercase())) + .map_dbg(|_| format!("[GIT {} HOOK]: Repository object fetched", action.uppercase())) .map_err_into(GHEK::RepositoryError) .map_into_hook_error() } From 4e2a980599a506789b3994545fdbe17fcb858ea2 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 7 Sep 2016 21:47:39 +0200 Subject: [PATCH 12/15] Refactor Index fetching into helper function --- libimagstorestdhook/src/vcs/git/create.rs | 15 ++++----------- libimagstorestdhook/src/vcs/git/update.rs | 15 ++++----------- libimagstorestdhook/src/vcs/git/util.rs | 11 ++++++++++- 3 files changed, 18 insertions(+), 23 deletions(-) diff --git a/libimagstorestdhook/src/vcs/git/create.rs b/libimagstorestdhook/src/vcs/git/create.rs index 079b1b60..737c7402 100644 --- a/libimagstorestdhook/src/vcs/git/create.rs +++ b/libimagstorestdhook/src/vcs/git/create.rs @@ -92,7 +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_repo; + use vcs::git::util::{fetch_repo, fetch_index}; debug!("[GIT CREATE HOOK]: {:?}", id); @@ -113,16 +113,9 @@ impl StoreIdAccessor for CreateHook { try!(self.runtime.ensure_cfg_branch_is_checked_out()); debug!("[GIT CREATE HOOK]: Branch checked out"); - let action = StoreAction::Create; - - let repo = try!(fetch_repo(&self.runtime, &action)); - - let mut index = try!( - repo - .index() - .map_err_into(GHEK::RepositoryIndexFetchingError) - .map_into_hook_error() - ); + let action = StoreAction::Create; + let repo = try!(fetch_repo(&self.runtime, &action)); + let mut index = try!(fetch_index(repo, &action)); let file_status = try!( repo diff --git a/libimagstorestdhook/src/vcs/git/update.rs b/libimagstorestdhook/src/vcs/git/update.rs index 1c91fa78..9bd7f65a 100644 --- a/libimagstorestdhook/src/vcs/git/update.rs +++ b/libimagstorestdhook/src/vcs/git/update.rs @@ -87,7 +87,7 @@ 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_repo; + use vcs::git::util::{fetch_repo, fetch_index}; 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") ); - let action = StoreAction::Update; - - let repo = try!(fetch_repo(&self.runtime, &action)); - - let mut index = try!( - repo - .index() - .map_err_into(GHEK::RepositoryIndexFetchingError) - .map_into_hook_error() - ); + let action = StoreAction::Update; + let repo = try!(fetch_repo(&self.runtime, &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 6bdd954d..7e0a9f4d 100644 --- a/libimagstorestdhook/src/vcs/git/util.rs +++ b/libimagstorestdhook/src/vcs/git/util.rs @@ -2,7 +2,7 @@ //! //! 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::MapErrInto; @@ -24,3 +24,12 @@ pub fn fetch_repo<'a>(runtime: &'a GRuntime, action: &StoreAction) -> Result<&'a .map_into_hook_error() } +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() +} + From f86b450cfa6bbc5e11ec575d659f9567f4cd347d Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 7 Sep 2016 21:51:34 +0200 Subject: [PATCH 13/15] Simplify error printing if config value fetching failed --- libimagstorestdhook/src/vcs/git/create.rs | 7 +------ libimagstorestdhook/src/vcs/git/runtime.rs | 5 ++++- libimagstorestdhook/src/vcs/git/update.rs | 7 +------ 3 files changed, 6 insertions(+), 13 deletions(-) diff --git a/libimagstorestdhook/src/vcs/git/create.rs b/libimagstorestdhook/src/vcs/git/create.rs index 737c7402..9708f0e9 100644 --- a/libimagstorestdhook/src/vcs/git/create.rs +++ b/libimagstorestdhook/src/vcs/git/create.rs @@ -103,17 +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") - ); - debug!("[GIT CREATE HOOK]: Ensuring branch checkout"); try!(self.runtime.ensure_cfg_branch_is_checked_out()); debug!("[GIT CREATE HOOK]: Branch checked out"); let action = StoreAction::Create; + let cfg = try!(self.runtime.config_value_or_err(&action)); let repo = try!(fetch_repo(&self.runtime, &action)); let mut index = try!(fetch_index(repo, &action)); diff --git a/libimagstorestdhook/src/vcs/git/runtime.rs b/libimagstorestdhook/src/vcs/git/runtime.rs index 54334f44..dd53de95 100644 --- a/libimagstorestdhook/src/vcs/git/runtime.rs +++ b/libimagstorestdhook/src/vcs/git/runtime.rs @@ -47,7 +47,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,6 +55,9 @@ 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> { diff --git a/libimagstorestdhook/src/vcs/git/update.rs b/libimagstorestdhook/src/vcs/git/update.rs index 9bd7f65a..9d5d18eb 100644 --- a/libimagstorestdhook/src/vcs/git/update.rs +++ b/libimagstorestdhook/src/vcs/git/update.rs @@ -91,13 +91,8 @@ impl StoreIdAccessor for UpdateHook { 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") - ); - let action = StoreAction::Update; + let cfg = try!(self.runtime.config_value_or_err(&action)); let repo = try!(fetch_repo(&self.runtime, &action)); let mut index = try!(fetch_index(repo, &action)); From b50aacbf55ec13653320fd1bcc1aa12303c3f93f Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 7 Sep 2016 21:54:20 +0200 Subject: [PATCH 14/15] Move error output to Runtime::repository() getter --- libimagstorestdhook/src/vcs/git/create.rs | 4 ++-- libimagstorestdhook/src/vcs/git/runtime.rs | 12 +++++++++--- libimagstorestdhook/src/vcs/git/update.rs | 4 ++-- libimagstorestdhook/src/vcs/git/util.rs | 11 ----------- 4 files changed, 13 insertions(+), 18 deletions(-) diff --git a/libimagstorestdhook/src/vcs/git/create.rs b/libimagstorestdhook/src/vcs/git/create.rs index 9708f0e9..c8ada38a 100644 --- a/libimagstorestdhook/src/vcs/git/create.rs +++ b/libimagstorestdhook/src/vcs/git/create.rs @@ -92,7 +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_repo, fetch_index}; + use vcs::git::util::fetch_index; debug!("[GIT CREATE HOOK]: {:?}", id); @@ -109,7 +109,7 @@ impl StoreIdAccessor for CreateHook { let action = StoreAction::Create; let cfg = try!(self.runtime.config_value_or_err(&action)); - let repo = try!(fetch_repo(&self.runtime, &action)); + let repo = try!(self.runtime.repository(&action)); let mut index = try!(fetch_index(repo, &action)); let file_status = try!( diff --git a/libimagstorestdhook/src/vcs/git/runtime.rs b/libimagstorestdhook/src/vcs/git/runtime.rs index dd53de95..74ed8fb3 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}; @@ -60,12 +61,17 @@ impl Runtime { }) } - 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<()> { diff --git a/libimagstorestdhook/src/vcs/git/update.rs b/libimagstorestdhook/src/vcs/git/update.rs index 9d5d18eb..fa4af2c9 100644 --- a/libimagstorestdhook/src/vcs/git/update.rs +++ b/libimagstorestdhook/src/vcs/git/update.rs @@ -87,13 +87,13 @@ 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_repo, fetch_index}; + use vcs::git::util::fetch_index; debug!("[GIT UPDATE HOOK]: {:?}", id); let action = StoreAction::Update; let cfg = try!(self.runtime.config_value_or_err(&action)); - let repo = try!(fetch_repo(&self.runtime, &action)); + let repo = try!(self.runtime.repository(&action)); let mut index = try!(fetch_index(repo, &action)); let tree_id = try!( diff --git a/libimagstorestdhook/src/vcs/git/util.rs b/libimagstorestdhook/src/vcs/git/util.rs index 7e0a9f4d..56b66323 100644 --- a/libimagstorestdhook/src/vcs/git/util.rs +++ b/libimagstorestdhook/src/vcs/git/util.rs @@ -13,17 +13,6 @@ use vcs::git::error::MapIntoHookError; use libimagutil::debug_result::*; use libimagstore::hook::error::HookError; -pub fn fetch_repo<'a>(runtime: &'a GRuntime, action: &StoreAction) -> Result<&'a Repository, HookError> -{ - debug!("[GIT {} HOOK]: Getting repository", action.uppercase()); - runtime - .repository() - .map_dbg_err(|_| format!("[GIT {} HOOK]: Couldn't fetch Repository", action.uppercase())) - .map_dbg(|_| format!("[GIT {} HOOK]: Repository object fetched", action.uppercase())) - .map_err_into(GHEK::RepositoryError) - .map_into_hook_error() -} - pub fn fetch_index(repo: &Repository, action: &StoreAction) -> Result { debug!("[GIT {} HOOK]: Getting Index", action.uppercase()); repo.index() From c19f269f59e99db69e4c5673d82450df12e2c5a4 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 7 Sep 2016 21:57:10 +0200 Subject: [PATCH 15/15] Adapt ensure_cfg_branch_is_checked_out() for new Runtime::repository() interface --- libimagstorestdhook/src/vcs/git/create.rs | 6 ++---- libimagstorestdhook/src/vcs/git/runtime.rs | 6 ++++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/libimagstorestdhook/src/vcs/git/create.rs b/libimagstorestdhook/src/vcs/git/create.rs index c8ada38a..fcba4535 100644 --- a/libimagstorestdhook/src/vcs/git/create.rs +++ b/libimagstorestdhook/src/vcs/git/create.rs @@ -103,11 +103,9 @@ impl StoreIdAccessor for CreateHook { .map_into_hook_error() ); - debug!("[GIT CREATE HOOK]: Ensuring branch checkout"); - try!(self.runtime.ensure_cfg_branch_is_checked_out()); - debug!("[GIT CREATE HOOK]: Branch checked out"); - let action = StoreAction::Create; + try!(self.runtime.ensure_cfg_branch_is_checked_out(&action)); + 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)); diff --git a/libimagstorestdhook/src/vcs/git/runtime.rs b/libimagstorestdhook/src/vcs/git/runtime.rs index 74ed8fb3..13902b7d 100644 --- a/libimagstorestdhook/src/vcs/git/runtime.rs +++ b/libimagstorestdhook/src/vcs/git/runtime.rs @@ -74,11 +74,12 @@ impl Runtime { .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() @@ -128,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") } }