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.
61 lines
1.7 KiB
61 lines
1.7 KiB
use std::error::Error;
|
|
use std::fmt;
|
|
use std::path::Path;
|
|
|
|
use command_runner::CommandRunner;
|
|
use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
|
|
|
|
pub struct GitSubmodules<'a, C: 'a + CommandRunner> {
|
|
target: &'a str,
|
|
command_runner: &'a C
|
|
}
|
|
|
|
impl<'a, C: CommandRunner> GitSubmodules<'a, C> {
|
|
pub fn new(target: &'a str, command_runner: &'a C) -> Self {
|
|
GitSubmodules {
|
|
target: target,
|
|
command_runner: command_runner
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'a, C: CommandRunner> fmt::Display for GitSubmodules<'a, C> {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
write!(f, "Submodules for {}", self.target)
|
|
}
|
|
}
|
|
|
|
impl<'a, C: CommandRunner> GitSubmodules<'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 GitSubmodules<'a, C> {
|
|
fn target_reached(&self) -> Result<bool, Box<Error>> {
|
|
if !Path::new(self.target).exists() {
|
|
return Ok(false);
|
|
}
|
|
let output = try!(String::from_utf8(try!(self._run_in_target_repo(&["submodule", "status"]))));
|
|
Ok(output.lines().all(|line| line.len() == 0 || line.starts_with(' ')))
|
|
}
|
|
|
|
fn execute(&self) -> Result<(), Box<Error>> {
|
|
try!(self._run_in_target_repo(&["submodule", "update", "--init"]));
|
|
Ok(())
|
|
}
|
|
|
|
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 {
|
|
}
|