Use Runtime object rather than own Repository object

This commit is contained in:
Matthias Beyer 2016-07-22 15:06:58 +02:00
parent 9ed13f265d
commit d498ed75d9
1 changed files with 10 additions and 65 deletions

View File

@ -21,12 +21,12 @@ use vcs::git::result::Result;
use vcs::git::error::MapErrInto;
use vcs::git::error::GitHookErrorKind as GHEK;
use vcs::git::error::GitHookError as GHE;
use vcs::git::config::ensure_branch;
use vcs::git::runtime::Runtime;
pub struct CreateHook<'a> {
storepath: &'a PathBuf,
repository: Option<Repository>,
runtime: Runtime<'a>,
position: HookPosition,
}
@ -34,35 +34,13 @@ pub struct CreateHook<'a> {
impl<'a> CreateHook<'a> {
pub fn new(storepath: &'a PathBuf, p: HookPosition) -> CreateHook<'a> {
let r = match Repository::open(storepath) {
Ok(r) => Some(r),
Err(e) => {
trace_error(&e);
None
},
};
CreateHook {
storepath: storepath,
repository: r,
runtime: Runtime::new(storepath),
position: p,
}
}
fn repository(&self) -> HookResult<&Repository> {
use vcs::git::config::abort_on_repo_init_err;
match self.repository.as_ref() {
Some(r) => Ok(r),
None => {
debug!("Repository isn't initialized... creating error object now");
let he = GHEK::MkRepo.into_error();
let he = HE::new(HEK::HookExecutionError, Some(Box::new(he)));
let custom = HECD::default().aborting(abort_on_repo_init_err(self.config.as_ref()));
return Err(he.with_custom_data(custom));
}
}
}
}
impl<'a> Debug for CreateHook<'a> {
@ -70,7 +48,7 @@ impl<'a> Debug for CreateHook<'a> {
fn fmt(&self, fmt: &mut Formatter) -> RResult<(), FmtError> {
write!(fmt, "CreateHook(storepath={:?}, repository={}, pos={:?}, cfg={:?}",
self.storepath,
(if self.repository.is_some() { "Some(_)" } else { "None" }),
(if self.runtime.has_repository() { "Some(_)" } else { "None" }),
self.position,
self.runtime.has_config())
}
@ -101,16 +79,12 @@ impl<'a> StoreIdAccessor for CreateHook<'a> {
fn access(&self, id: &StoreId) -> HookResult<()> {
debug!("[GIT CREATE HOOK]: {:?}", id);
let repository = try!(self.repository());
let head = try!(repository.head().map_err_into(GHEK::HeadFetchError)
.map_err(|e| HEK::HookExecutionError.into_error_with_cause(Box::new(e))));
if head.is_branch() {
return GHEK::NotOnBranch.into_error().inside_of(HEK::HookExecutionError)
}
try!(checkout_branch(self.config.as_ref(), &head)
.map_err(|e| HEK::HookExecutionError.into_error_with_cause(Box::new(e))));
try!(self
.runtime
.ensure_cfg_branch_is_checked_out()
.map_err(Box::new)
.map_err(|e| HEK::HookExecutionError.into_error_with_cause(e)));
let repository = try!(self.runtime.repository());
// Now to the create() hook action
@ -121,32 +95,3 @@ impl<'a> StoreIdAccessor for CreateHook<'a> {
}
fn checkout_branch(config: Option<&Value>, head: &GitReference) -> Result<()> {
// Check out appropriate branch ... or fail
match ensure_branch(config) {
Ok(Some(s)) => {
match head.name().map(|name| name == s) {
Some(b) => {
if b {
debug!("Branch already checked out.");
Ok(())
} else {
debug!("Branch not checked out.");
unimplemented!()
}
},
None => Err(GHEK::RepositoryBranchNameFetchingError.into_error())
.map_err_into(GHEK::RepositoryBranchError)
.map_err_into(GHEK::RepositoryError),
}
},
Ok(None) => {
debug!("No branch to checkout");
Ok(())
},
Err(e) => Err(e).map_err_into(GHEK::RepositoryError),
}
}