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

View file

@ -21,12 +21,12 @@ use vcs::git::result::Result;
use vcs::git::error::MapErrInto; use vcs::git::error::MapErrInto;
use vcs::git::error::GitHookErrorKind as GHEK; use vcs::git::error::GitHookErrorKind as GHEK;
use vcs::git::error::GitHookError as GHE; use vcs::git::error::GitHookError as GHE;
use vcs::git::config::ensure_branch; use vcs::git::runtime::Runtime;
pub struct CreateHook<'a> { pub struct CreateHook<'a> {
storepath: &'a PathBuf, storepath: &'a PathBuf,
repository: Option<Repository>, runtime: Runtime<'a>,
position: HookPosition, position: HookPosition,
} }
@ -34,35 +34,13 @@ pub struct CreateHook<'a> {
impl<'a> CreateHook<'a> { impl<'a> CreateHook<'a> {
pub fn new(storepath: &'a PathBuf, p: HookPosition) -> 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 { CreateHook {
storepath: storepath, storepath: storepath,
repository: r, runtime: Runtime::new(storepath),
position: p, 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> { impl<'a> Debug for CreateHook<'a> {
@ -70,7 +48,7 @@ impl<'a> Debug for CreateHook<'a> {
fn fmt(&self, fmt: &mut Formatter) -> RResult<(), FmtError> { fn fmt(&self, fmt: &mut Formatter) -> RResult<(), FmtError> {
write!(fmt, "CreateHook(storepath={:?}, repository={}, pos={:?}, cfg={:?}", write!(fmt, "CreateHook(storepath={:?}, repository={}, pos={:?}, cfg={:?}",
self.storepath, self.storepath,
(if self.repository.is_some() { "Some(_)" } else { "None" }), (if self.runtime.has_repository() { "Some(_)" } else { "None" }),
self.position, self.position,
self.runtime.has_config()) self.runtime.has_config())
} }
@ -101,16 +79,12 @@ impl<'a> StoreIdAccessor for CreateHook<'a> {
fn access(&self, id: &StoreId) -> HookResult<()> { fn access(&self, id: &StoreId) -> HookResult<()> {
debug!("[GIT CREATE HOOK]: {:?}", id); debug!("[GIT CREATE HOOK]: {:?}", id);
let repository = try!(self.repository()); try!(self
let head = try!(repository.head().map_err_into(GHEK::HeadFetchError) .runtime
.map_err(|e| HEK::HookExecutionError.into_error_with_cause(Box::new(e)))); .ensure_cfg_branch_is_checked_out()
.map_err(Box::new)
if head.is_branch() { .map_err(|e| HEK::HookExecutionError.into_error_with_cause(e)));
return GHEK::NotOnBranch.into_error().inside_of(HEK::HookExecutionError) let repository = try!(self.runtime.repository());
}
try!(checkout_branch(self.config.as_ref(), &head)
.map_err(|e| HEK::HookExecutionError.into_error_with_cause(Box::new(e))));
// Now to the create() hook action // 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),
}
}