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.
87 lines
2.6 KiB
87 lines
2.6 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> {
|
|
target: &'a str,
|
|
source: &'a str,
|
|
branch: &'a str,
|
|
command_runner: &'a C
|
|
}
|
|
|
|
impl<'a, C: CommandRunner> GitCheckout<'a, C> {
|
|
pub fn new(target: &'a str, source: &'a str, branch: &'a str, command_runner: &'a C) -> Self {
|
|
GitCheckout {
|
|
target: target,
|
|
source: source,
|
|
branch: branch,
|
|
command_runner: command_runner
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'a, C: CommandRunner> fmt::Display for GitCheckout<'a, C> {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
write!(f, "Checkout {} (branch {}) into {}", self.source, self.branch, self.target)
|
|
}
|
|
}
|
|
|
|
use std::fs::metadata;
|
|
|
|
impl<'a, C: CommandRunner> GitCheckout<'a, C> {
|
|
fn _run_in_target_repo(&self, args: &[&str]) -> Result<Vec<u8>, Box<Error>> {
|
|
let mut new_args = vec!["-C", self.target];
|
|
new_args.extend_from_slice(args);
|
|
self.command_runner.get_output("git", &new_args)
|
|
}
|
|
}
|
|
|
|
impl<'a, C: CommandRunner> Symbol for GitCheckout<'a, C> {
|
|
fn target_reached(&self) -> Result<bool, Box<Error>> {
|
|
if let Err(e) = metadata(self.target) {
|
|
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]));
|
|
let fetch_head = try!(self._run_in_target_repo(&["rev-parse", "FETCH_HEAD"]));
|
|
let head = try!(self._run_in_target_repo(&["rev-parse", "HEAD"]));
|
|
Ok(fetch_head == head)
|
|
}
|
|
|
|
fn execute(&self) -> Result<(), Box<Error>> {
|
|
if !Path::new(self.target).exists() {
|
|
return self.command_runner.run_successfully("git", &["clone", "--depth", "1", "-b", self.branch, self.source, self.target]);
|
|
}
|
|
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).parent().unwrap().to_string_lossy()) ]
|
|
}
|
|
|
|
fn provides(&self) -> Option<Vec<Resource>> {
|
|
Some(vec![ Resource::new("dir", self.target.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 {
|
|
}
|