feat: add mod git
This commit is contained in:
@@ -10,6 +10,8 @@ impl CommandDefault {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(_arg_matches: &ArgMatches) -> CommandError {
|
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!");
|
error!("Default command is not implemented!");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
56
src/git.rs
Normal file
56
src/git.rs
Normal 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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ use clap::{ App, ArgMatches };
|
|||||||
mod cmd;
|
mod cmd;
|
||||||
mod cmd_default;
|
mod cmd_default;
|
||||||
mod cmd_new;
|
mod cmd_new;
|
||||||
|
mod git;
|
||||||
|
|
||||||
use cmd::{ Command, CommandError };
|
use cmd::{ Command, CommandError };
|
||||||
use cmd_default::CommandDefault;
|
use cmd_default::CommandDefault;
|
||||||
|
|||||||
Reference in New Issue
Block a user