You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.7 KiB
83 lines
2.7 KiB
use std::error::Error;
|
|
use std::fmt;
|
|
use std::io;
|
|
use std::path::Path;
|
|
|
|
use command_runner::CommandRunner;
|
|
use resources::Resource;
|
|
use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
|
|
|
|
pub struct GitCheckout<'a, C: 'a + CommandRunner, T: AsRef<str>> {
|
|
target: T,
|
|
source: &'a str,
|
|
branch: &'a str,
|
|
command_runner: &'a C
|
|
}
|
|
|
|
impl<'a, C: CommandRunner, T: AsRef<str>> GitCheckout<'a, C, T> {
|
|
pub fn new(target: T, source: &'a str, branch: &'a str, command_runner: &'a C) -> Self {
|
|
GitCheckout { target, source, branch, command_runner }
|
|
}
|
|
}
|
|
|
|
impl<'a, C: CommandRunner, T: AsRef<str>> fmt::Display for GitCheckout<'a, C, T> {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
write!(f, "Checkout {} (branch {}) into {}", self.source, self.branch, self.target.as_ref())
|
|
}
|
|
}
|
|
|
|
use std::fs::metadata;
|
|
|
|
impl<'a, C: CommandRunner, T: AsRef<str>> GitCheckout<'a, C, T> {
|
|
fn _run_in_target_repo(&self, args: &[&str]) -> Result<Vec<u8>, Box<Error>> {
|
|
let mut new_args = vec!["-C", self.target.as_ref()];
|
|
new_args.extend_from_slice(args);
|
|
self.command_runner.get_output("git", &new_args)
|
|
}
|
|
}
|
|
|
|
impl<'a, C: CommandRunner, T: AsRef<str>> Symbol for GitCheckout<'a, C, T> {
|
|
fn target_reached(&self) -> Result<bool, Box<Error>> {
|
|
if let Err(e) = metadata(self.target.as_ref()) {
|
|
return if e.kind() == io::ErrorKind::NotFound {
|
|
Ok(false)
|
|
} else {
|
|
Err(Box::new(e))
|
|
};
|
|
}
|
|
try!(self._run_in_target_repo(&["fetch", self.source, self.branch]));
|
|
// git rev-list resolves tag objects
|
|
let fetch_head = try!(self._run_in_target_repo(&["rev-list", "-1", "FETCH_HEAD"]));
|
|
let head = try!(self._run_in_target_repo(&["rev-list", "-1", "HEAD"]));
|
|
Ok(fetch_head == head)
|
|
}
|
|
|
|
fn execute(&self) -> Result<(), Box<Error>> {
|
|
if !Path::new(self.target.as_ref()).exists() {
|
|
return self.command_runner.run_successfully("git", &["clone", "--depth", "1", "-b", self.branch, self.source, self.target.as_ref()]);
|
|
}
|
|
try!(self._run_in_target_repo(&["fetch", self.source, self.branch]));
|
|
try!(self._run_in_target_repo(&["merge", "FETCH_HEAD"]));
|
|
Ok(())
|
|
}
|
|
|
|
fn get_prerequisites(&self) -> Vec<Resource> {
|
|
vec![ Resource::new("dir", Path::new(self.target.as_ref()).parent().unwrap().to_string_lossy()) ]
|
|
}
|
|
|
|
fn provides(&self) -> Option<Vec<Resource>> {
|
|
Some(vec![ Resource::new("dir", self.target.as_ref().to_string()) ])
|
|
}
|
|
|
|
fn as_action<'b>(&'b self, runner: &'b SymbolRunner) -> Box<Action + 'b> {
|
|
Box::new(SymbolAction::new(runner, self))
|
|
}
|
|
|
|
fn into_action<'b>(self: Box<Self>, runner: &'b SymbolRunner) -> Box<Action + 'b> where Self: 'b {
|
|
Box::new(OwnedSymbolAction::new(runner, *self))
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
}
|