feat: add mod git

This commit is contained in:
2020-07-25 21:36:15 +08:00
parent 96045ffcc6
commit 3e17d1f3a5
3 changed files with 59 additions and 0 deletions

View File

@@ -10,6 +10,8 @@ impl CommandDefault {
}
pub fn run(_arg_matches: &ArgMatches) -> CommandError {
println!("Git status: {}", crate::git::check_git_status());
println!("Git hash : {}", crate::git::get_git_hash().unwrap_or_else(|| "<none>".to_owned()));
error!("Default command is not implemented!");
Ok(())
}

56
src/git.rs Normal file
View File

@@ -0,0 +1,56 @@
use std::process::Command;
pub fn check_git_status() -> bool {
match Command::new("git").env("LANG", "en_US").args(&["fetch", "--dry-run"]).output() {
Err(e) => {
warn!("Error in run git fetch --dry-run: {}", e);
return false;
},
Ok(o) => match String::from_utf8(o.stdout) {
Err(e) => {
warn!("Error in get utf8 git fetch --dry-run stdout: {}", e);
return false;
},
Ok(stdout) => if !stdout.trim().is_empty() {
warn!("Std out is not empty git fetch --dry-run: {}", stdout);
return false;
},
},
}
match Command::new("git").env("LANG", "en_US").args(&["status"]).output() {
Err(e) => {
warn!("Error in run git status: {}", e);
return false;
},
Ok(o) => match String::from_utf8(o.stdout) {
Err(e) => {
warn!("Error in get utf8 git status: {}", e);
return false;
},
Ok(stdout) => if [
"Changes not staged for commit",
"Yourbranch is ahead of",
"Untracked files" ].iter().any(|s| stdout.contains(s)) {
warn!("Std out check failed git status: {}", stdout);
return false;
}
}
}
true
}
pub fn get_git_hash() -> Option<String> {
match Command::new("git").args(&["rev-parse", "HEAD"]).output(){
Err(e) => {
warn!("Error in run git rev-parse HEAD: {}", e);
None
},
Ok(o) => match String::from_utf8(o.stdout) {
Err(e) => {
warn!("Error in get utf8 git rev-parse HEAD: {}", e);
None
},
Ok(stdout) => Some(stdout),
}
}
}

View File

@@ -6,6 +6,7 @@ use clap::{ App, ArgMatches };
mod cmd;
mod cmd_default;
mod cmd_new;
mod git;
use cmd::{ Command, CommandError };
use cmd_default::CommandDefault;