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.
69 lines
1.9 KiB
69 lines
1.9 KiB
use std::error::Error;
|
|
use std::ffi::OsStr;
|
|
use std::fmt;
|
|
use std::path::Path;
|
|
|
|
use crate::command_runner::CommandRunner;
|
|
use crate::symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
|
|
|
|
pub struct GitSubmodules<'a, P: AsRef<Path>, C: CommandRunner> {
|
|
target: P,
|
|
command_runner: &'a C,
|
|
}
|
|
|
|
impl<'a, P: AsRef<Path>, C: CommandRunner> GitSubmodules<'a, P, C> {
|
|
pub fn new(target: P, command_runner: &'a C) -> Self {
|
|
GitSubmodules {
|
|
target,
|
|
command_runner,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<P: AsRef<Path>, C: CommandRunner> fmt::Display for GitSubmodules<'_, P, C> {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
write!(f, "Submodules for {}", self.target.as_ref().display())
|
|
}
|
|
}
|
|
|
|
impl<P: AsRef<Path>, C: CommandRunner> GitSubmodules<'_, P, C> {
|
|
fn _run_in_target_repo(&self, args: &[&OsStr]) -> Result<Vec<u8>, Box<dyn Error>> {
|
|
let mut new_args: Vec<&OsStr> = vec![];
|
|
new_args.extend_from_slice(args!["-C", self.target.as_ref()]);
|
|
new_args.extend_from_slice(args);
|
|
self.command_runner.get_output("git", &new_args)
|
|
}
|
|
}
|
|
|
|
impl<P: AsRef<Path>, C: CommandRunner> Symbol for GitSubmodules<'_, P, C> {
|
|
fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
|
|
if !self.target.as_ref().exists() {
|
|
return Ok(false);
|
|
}
|
|
let output = String::from_utf8(self._run_in_target_repo(args!["submodule", "status"])?)?;
|
|
Ok(
|
|
output
|
|
.lines()
|
|
.all(|line| line.is_empty() || line.starts_with(' ')),
|
|
)
|
|
}
|
|
|
|
fn execute(&self) -> Result<(), Box<dyn Error>> {
|
|
self._run_in_target_repo(args!["submodule", "update", "--init"])?;
|
|
Ok(())
|
|
}
|
|
|
|
fn as_action<'b>(&'b self, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> {
|
|
Box::new(SymbolAction::new(runner, self))
|
|
}
|
|
|
|
fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b>
|
|
where
|
|
Self: 'b,
|
|
{
|
|
Box::new(OwnedSymbolAction::new(runner, *self))
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {}
|