use crates::git_workarea::{GitWorkArea, Identity, SubmoduleConfig};
use error::*;
use std::ffi::OsStr;
use std::path::Path;
use std::process::Command;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AttributeState {
Unspecified,
Set,
Unset,
Value(String),
}
#[derive(Debug)]
pub struct CheckGitContext {
workarea: GitWorkArea,
topic_owner: Identity,
}
impl CheckGitContext {
pub fn new(workarea: GitWorkArea, topic_owner: Identity) -> Self {
CheckGitContext {
workarea: workarea,
topic_owner: topic_owner,
}
}
pub fn git(&self) -> Command {
self.workarea.git()
}
pub fn topic_owner(&self) -> &Identity {
&self.topic_owner
}
pub fn check_attr<A, P>(&self, attr: A, path: P) -> Result<AttributeState>
where A: AsRef<str>,
P: AsRef<OsStr>,
{
let check_attr = self.workarea
.git()
.arg("check-attr")
.arg(attr.as_ref())
.arg("--")
.arg(path.as_ref())
.output()
.chain_err(|| "failed to construct check-attr command")?;
if !check_attr.status.success() {
bail!(ErrorKind::Git(format!("failed to check the {} attribute of {}: {}",
attr.as_ref(),
path.as_ref().to_string_lossy(),
String::from_utf8_lossy(&check_attr.stderr))));
}
let attr_line = String::from_utf8_lossy(&check_attr.stdout);
let attr_value = attr_line.split_whitespace()
.last()
.expect("expected `git check-attr` to have a value for the attribute");
if attr_value == "set" {
Ok(AttributeState::Set)
} else if attr_value == "unset" {
Ok(AttributeState::Unset)
} else if attr_value == "unspecified" {
Ok(AttributeState::Unspecified)
} else {
Ok(AttributeState::Value(attr_value.to_owned()))
}
}
pub fn checkout<P>(&self, paths: &[P]) -> Result<()>
where P: AsRef<OsStr>,
{
Ok(self.workarea.checkout(paths)?)
}
pub fn cd_to_work_tree<'a>(&self, cmd: &'a mut Command) -> &'a mut Command {
self.workarea.cd_to_work_tree(cmd)
}
pub fn gitdir(&self) -> &Path {
self.workarea.gitdir()
}
pub fn submodule_config(&self) -> &SubmoduleConfig {
self.workarea.submodule_config()
}
}
#[cfg(test)]
mod tests {
use crates::git_workarea::{CommitId, GitContext, Identity};
use super::*;
use std::path::Path;
fn make_context() -> GitContext {
let gitdir = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/.git"));
if !gitdir.exists() {
panic!("The tests must be run from a git checkout.");
}
GitContext::new(gitdir)
}
#[test]
fn test_commit_attrs() {
let ctx = make_context();
let sha1 = "85b9551a672a34e1926d5010a9c9075eda0a6107";
let prep_ctx = ctx.prepare(&CommitId::new(sha1)).unwrap();
let ben = Identity::new("Ben Boeckel", "ben.boeckel@kitware.com");
let check_ctx = CheckGitContext::new(prep_ctx, ben);
assert_eq!(check_ctx.check_attr("foo", "file1").unwrap(),
AttributeState::Value("bar".to_owned()));
assert_eq!(check_ctx.check_attr("attr_set", "file1").unwrap(),
AttributeState::Set);
assert_eq!(check_ctx.check_attr("attr_unset", "file1").unwrap(),
AttributeState::Unspecified);
assert_eq!(check_ctx.check_attr("text", "file1").unwrap(),
AttributeState::Unspecified);
assert_eq!(check_ctx.check_attr("foo", "file2").unwrap(),
AttributeState::Unspecified);
assert_eq!(check_ctx.check_attr("attr_set", "file2").unwrap(),
AttributeState::Set);
assert_eq!(check_ctx.check_attr("attr_unset", "file2").unwrap(),
AttributeState::Unset);
assert_eq!(check_ctx.check_attr("text", "file2").unwrap(),
AttributeState::Unspecified);
assert_eq!(check_ctx.check_attr("foo", "file3").unwrap(),
AttributeState::Unspecified);
assert_eq!(check_ctx.check_attr("attr_set", "file3").unwrap(),
AttributeState::Unspecified);
assert_eq!(check_ctx.check_attr("attr_unset", "file3").unwrap(),
AttributeState::Unspecified);
assert_eq!(check_ctx.check_attr("text", "file3").unwrap(),
AttributeState::Value("yes".to_owned()));
}
}