Move hook impl to update hook as Store::create() is lazy

As Store::create() is lazy and does not write to disk, we do not have a
file on disk when the post-create-hook runs.

Therefor, the implementation of the git logic has to be in the update
hook.
This commit is contained in:
Matthias Beyer 2016-09-08 09:32:25 +02:00
parent 157f4e79aa
commit 8f01cf15e4
2 changed files with 35 additions and 49 deletions

View file

@ -89,56 +89,9 @@ impl StoreIdAccessor for CreateHook {
/// After that, the UpdateHook will take care of committing the changes or new file.
///
fn access(&self, id: &StoreId) -> HookResult<()> {
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);
let path = try!(
id.clone()
.into_pathbuf()
.map_err_into(GHEK::StoreIdHandlingError)
.map_into_hook_error()
);
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));
let file_status = try!(
repo
.status_file(&path)
.map_err_into(GHEK::RepositoryFileStatusError)
.map_into_hook_error()
);
let cb = &mut |path: &Path, _matched_spec: &[u8]| -> i32 {
if file_status.contains(STATUS_WT_MODIFIED) ||
file_status.contains(STATUS_WT_NEW) {
debug!("[GIT CREATE HOOK]: File is new or modified: {}", path.display());
0
} else {
debug!("[GIT CREATE HOOK]: Ignoring file: {}", path.display());
1
}
};
try!(
index.add_all(&[path], ADD_DEFAULT, Some(cb as &mut IndexMatchedPath))
.map_err_into(GHEK::RepositoryPathAddingError)
.map_into_hook_error()
);
index
.write()
.map_err_into(GHEK::RepositoryIndexWritingError)
.map_into_hook_error()
debug!("[GIT CREATE HOOK]: Doing nothing as Store::create() is lazy and does not write to disk");
Ok(())
}
}

View file

@ -1,4 +1,5 @@
use std::path::PathBuf;
use std::path::Path;
use std::fmt::{Debug, Formatter, Error as FmtError};
use std::result::Result as RResult;
@ -88,6 +89,8 @@ impl StoreIdAccessor for UpdateHook {
use vcs::git::config::commit_message;
use vcs::git::error::MapIntoHookError;
use vcs::git::util::fetch_index;
use git2::{Reference as GitReference, Repository, Error as Git2Error};
use git2::{ADD_DEFAULT, STATUS_WT_NEW, STATUS_WT_MODIFIED, IndexMatchedPath};
debug!("[GIT UPDATE HOOK]: {:?}", id);
@ -96,6 +99,13 @@ impl StoreIdAccessor for UpdateHook {
let repo = try!(self.runtime.repository(&action));
let mut index = try!(fetch_index(repo, &action));
let path = try!(
id.clone()
.into_pathbuf()
.map_err_into(GHEK::StoreIdHandlingError)
.map_into_hook_error()
);
let tree_id = try!(
index.write_tree()
.map_err_into(GHEK::RepositoryIndexWritingError)
@ -114,6 +124,29 @@ impl StoreIdAccessor for UpdateHook {
.map_into_hook_error()
);
let file_status = try!(
repo
.status_file(&path)
.map_err_into(GHEK::RepositoryFileStatusError)
.map_into_hook_error()
);
let cb = &mut |path: &Path, _matched_spec: &[u8]| -> i32 {
if file_status.contains(STATUS_WT_NEW) || file_status.contains(STATUS_WT_MODIFIED) {
debug!("[GIT CREATE HOOK]: File is modified/new: {}", path.display());
0
} else {
debug!("[GIT CREATE HOOK]: Ignoring file: {}", path.display());
1
}
};
try!(
index.add_all(&[path], ADD_DEFAULT, Some(cb as &mut IndexMatchedPath))
.map_err_into(GHEK::RepositoryPathAddingError)
.map_into_hook_error()
);
let mut parents = Vec::new();
{
let commit = try!(