diff --git a/libimagstorestdhook/src/vcs/git/mod.rs b/libimagstorestdhook/src/vcs/git/mod.rs index 8eb16f34..ca9f5c61 100644 --- a/libimagstorestdhook/src/vcs/git/mod.rs +++ b/libimagstorestdhook/src/vcs/git/mod.rs @@ -4,6 +4,7 @@ pub mod create; pub mod delete; mod error; mod result; +mod runtime; pub mod retrieve; pub mod update; pub mod util; diff --git a/libimagstorestdhook/src/vcs/git/runtime.rs b/libimagstorestdhook/src/vcs/git/runtime.rs new file mode 100644 index 00000000..44255613 --- /dev/null +++ b/libimagstorestdhook/src/vcs/git/runtime.rs @@ -0,0 +1,69 @@ +use std::path::PathBuf; + +use git2::{Repository, Signature}; + +use libimagerror::into::IntoError; +use libimagerror::trace::trace_error; + +use vcs::git::result::Result; +use vcs::git::error::GitHookErrorKind as GHEK; +use vcs::git::config::{author_name, author_mail, committer_name, committer_mail}; + +struct Person<'a> { + pub name: &'a str, + pub mail: &'a str, +} + +impl<'a> Person<'a> { + fn new(name: &'a str, mail: &'a str) -> Person<'a> { + Person { name: name, mail: mail } + } +} + +struct Runtime<'a> { + pub repository: Option, + pub author: Option>, + pub committer: Option>, +} + +impl<'a> Runtime<'a> { + + pub fn new(storepath: &PathBuf) -> Runtime<'a> { + Runtime { + repository: match Repository::open(storepath) { + Ok(r) => Some(r), + Err(e) => { + trace_error(&e); + None + }, + }, + + author: None, + committer: None, + } + } + + pub fn configure(&mut self, config: &Value) -> Result<()> { + author_name(cfg) + .and_then(|n| author_email(cfg).map(|m| Person::new(n, m))) + .and_then(|author| { + committer_name(cfg) + .and_then(|n| committer_email(cfg).map(|m| (author, Person::new(n, m)))) + }) + .map(|(author, committer)| { + self.author = Some(author); + self.committer = Some(committer); + }) + } + + pub fn new_committer_sig(&self) -> Option> { + self.committer + .as_ref() + .map(|c| { + Signature::now(c.name, c.mail) + .map_err(|e| GHEK::MkSignature.into_error_with_cause(Box::new(e))) + }) + } + +} +