Merge branch 'libimagstorestdhook/git-refactor' into libimagstorestdhook/git-update
This commit is contained in:
commit
8d2c94b0b8
5 changed files with 54 additions and 71 deletions
|
@ -8,6 +8,18 @@ pub enum StoreAction {
|
||||||
Delete,
|
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 {
|
impl Display for StoreAction {
|
||||||
|
|
||||||
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
|
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
|
||||||
|
|
|
@ -92,6 +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_index;
|
||||||
|
|
||||||
debug!("[GIT CREATE HOOK]: {:?}", id);
|
debug!("[GIT CREATE HOOK]: {:?}", id);
|
||||||
|
|
||||||
|
@ -102,32 +103,12 @@ impl StoreIdAccessor for CreateHook {
|
||||||
.map_into_hook_error()
|
.map_into_hook_error()
|
||||||
);
|
);
|
||||||
|
|
||||||
let cfg = try!(
|
let action = StoreAction::Create;
|
||||||
self.runtime
|
try!(self.runtime.ensure_cfg_branch_is_checked_out(&action));
|
||||||
.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");
|
let cfg = try!(self.runtime.config_value_or_err(&action));
|
||||||
try!(self.runtime.ensure_cfg_branch_is_checked_out());
|
let repo = try!(self.runtime.repository(&action));
|
||||||
debug!("[GIT CREATE HOOK]: Branch checked out");
|
let mut index = try!(fetch_index(repo, &action));
|
||||||
|
|
||||||
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 file_status = try!(
|
let file_status = try!(
|
||||||
repo
|
repo
|
||||||
|
|
|
@ -10,6 +10,7 @@ use libimagstore::hook::error::HookErrorKind as HEK;
|
||||||
use libimagstore::hook::result::HookResult;
|
use libimagstore::hook::result::HookResult;
|
||||||
use libimagutil::debug_result::*;
|
use libimagutil::debug_result::*;
|
||||||
|
|
||||||
|
use vcs::git::action::StoreAction;
|
||||||
use vcs::git::result::Result;
|
use vcs::git::result::Result;
|
||||||
use vcs::git::error::{MapErrInto, GitHookErrorKind as GHEK};
|
use vcs::git::error::{MapErrInto, GitHookErrorKind as GHEK};
|
||||||
|
|
||||||
|
@ -47,7 +48,7 @@ impl Runtime {
|
||||||
self.config.is_some()
|
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
|
self.config
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.ok_or(GHEK::NoConfigError.into_error())
|
.ok_or(GHEK::NoConfigError.into_error())
|
||||||
|
@ -55,21 +56,30 @@ impl Runtime {
|
||||||
.map_err(Box::new)
|
.map_err(Box::new)
|
||||||
.map_err(|e| HEK::HookExecutionError.into_error_with_cause(e))
|
.map_err(|e| HEK::HookExecutionError.into_error_with_cause(e))
|
||||||
.map_err(|mut e| e.with_custom_data(CustomData::default().aborting(false)))
|
.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
|
self.repository
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.ok_or(GHEK::MkRepo.into_error())
|
.ok_or(GHEK::MkRepo.into_error())
|
||||||
.map_err(Box::new)
|
.map_err_into(GHEK::RepositoryError)
|
||||||
.map_err(|e| HEK::HookExecutionError.into_error_with_cause(e))
|
.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;
|
use vcs::git::config::ensure_branch;
|
||||||
|
|
||||||
|
debug!("[GIT CREATE HOOK]: Ensuring branch checkout");
|
||||||
let head = try!(self
|
let head = try!(self
|
||||||
.repository()
|
.repository(action)
|
||||||
.and_then(|r| {
|
.and_then(|r| {
|
||||||
debug!("Repository fetched, getting head");
|
debug!("Repository fetched, getting head");
|
||||||
r.head()
|
r.head()
|
||||||
|
@ -119,6 +129,7 @@ impl Runtime {
|
||||||
}
|
}
|
||||||
.map_err(Box::new)
|
.map_err(Box::new)
|
||||||
.map_err(|e| HEK::HookExecutionError.into_error_with_cause(e))
|
.map_err(|e| HEK::HookExecutionError.into_error_with_cause(e))
|
||||||
|
.map_dbg_str("[GIT CREATE HOOK]: Branch checked out")
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -87,31 +87,14 @@ 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_index;
|
||||||
|
|
||||||
debug!("[GIT UPDATE HOOK]: {:?}", id);
|
debug!("[GIT UPDATE HOOK]: {:?}", id);
|
||||||
|
|
||||||
let cfg = try!(
|
let action = StoreAction::Update;
|
||||||
self.runtime
|
let cfg = try!(self.runtime.config_value_or_err(&action));
|
||||||
.config_value_or_err()
|
let repo = try!(self.runtime.repository(&action));
|
||||||
.map_dbg_err_str("[GIT UPDATE HOOK]: Couldn't get Value object from config")
|
let mut index = try!(fetch_index(repo, &action));
|
||||||
);
|
|
||||||
|
|
||||||
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!(
|
let tree_id = try!(
|
||||||
index.write_tree()
|
index.write_tree()
|
||||||
|
|
|
@ -2,27 +2,23 @@
|
||||||
//!
|
//!
|
||||||
//! 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 git2::RepositoryInitOptions;
|
|
||||||
|
|
||||||
use libimagstore::store::Store;
|
|
||||||
|
|
||||||
use vcs::git::error::GitHookErrorKind as GHEK;
|
use vcs::git::error::GitHookErrorKind as GHEK;
|
||||||
use vcs::git::error::MapErrInto;
|
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<()> {
|
use libimagutil::debug_result::*;
|
||||||
let mut opts = RepositoryInitOptions::new();
|
use libimagstore::hook::error::HookError;
|
||||||
opts.bare(false);
|
|
||||||
opts.no_reinit(true);
|
pub fn fetch_index(repo: &Repository, action: &StoreAction) -> Result<Index, HookError> {
|
||||||
opts.mkdir(false);
|
debug!("[GIT {} HOOK]: Getting Index", action.uppercase());
|
||||||
opts.external_template(false);
|
repo.index()
|
||||||
Repository::init_opts(store.path(), &opts)
|
.map_dbg_err(|_| format!("[GIT {} HOOK]: Couldn't fetch Index", action.uppercase()))
|
||||||
.map(|_| ())
|
.map_dbg(|_| format!("[GIT {} HOOK]: Index object fetched", action.uppercase()))
|
||||||
.map_err_into(GHEK::MkRepo)
|
.map_err_into(GHEK::RepositoryIndexFetchingError)
|
||||||
}
|
.map_into_hook_error()
|
||||||
|
|
||||||
pub fn hasrepo(store: &Store) -> bool {
|
|
||||||
Repository::open(store.path()).is_ok()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue