Collect git repositories

This commit is contained in:
Roey Darwish Dror
2018-05-30 07:53:19 +03:00
parent 080b042d64
commit d67a60d0f3
2 changed files with 83 additions and 10 deletions

50
src/git.rs Normal file
View File

@@ -0,0 +1,50 @@
use super::error::*;
use std::path::{Path, PathBuf};
use std::process::Command;
use which::which;
pub struct Git {
git: Option<PathBuf>,
}
impl Git {
pub fn new() -> Self {
Self {
git: which("git").ok(),
}
}
pub fn get_repo_root<P: AsRef<Path>>(&self, path: P) -> Option<String> {
if !path.as_ref().exists() {
return None;
}
if let Some(git) = &self.git {
let output = Command::new(&git)
.arg("rev-parse")
.arg("--show-toplevel")
.current_dir(path)
.output();
if let Ok(output) = output {
if !output.status.success() {
return None;
}
return Some(String::from_utf8_lossy(&output.stdout).trim().to_string());
}
}
None
}
pub fn pull<P: AsRef<Path>>(&self, path: P) -> Result<()> {
if let Some(git) = &self.git {
if let Ok(mut command) = Command::new(&git).arg("pull").current_dir(path).spawn() {
command.wait()?;
}
}
Ok(())
}
}