diff --git a/src/args.rs b/src/args.rs index 67ad9ba..e94f756 100644 --- a/src/args.rs +++ b/src/args.rs @@ -49,7 +49,12 @@ pub fn get_args() -> clap::ArgMatches { .arg(arg!( "Branch name").required(true)) .alias("r"), ) - .subcommand(command!("branch").about("Show branches").alias("b")) + .subcommand( + command!("branch") + .about("Show branches or switch to one") + .alias("b") + .arg(arg!( "Branch name").required(false)), + ) .subcommand( command!() .name("push") @@ -60,6 +65,7 @@ pub fn get_args() -> clap::ArgMatches { command!() .name("status") .about("Show git status") + .alias("s") .alias("stat") .alias("stats"), ) @@ -83,11 +89,6 @@ pub fn get_args() -> clap::ArgMatches { .about("Show git commit log") .alias("l"), ) - .subcommand( - command!() - .name("save") - .about("Create a WIP commit") - .alias("s"), - ) + .subcommand(command!().name("save").about("Create a WIP commit")) .get_matches() } diff --git a/src/git.rs b/src/git.rs index c0ca396..f0c25a4 100644 --- a/src/git.rs +++ b/src/git.rs @@ -143,6 +143,15 @@ pub fn get_languages() -> Vec { } } +pub fn git_staged() -> Vec { + let files = read_stdout(Exec::cmd("git").arg("status").arg("--porcelain")); + + files + .split("\n") + .map(|x| x.split_whitespace().nth(1).unwrap().to_string()) + .collect() +} + /// Commit changes to the repository. pub fn commit(all: bool, done: bool, forced: bool, msg: &str) { // Work In Progress Save Commit @@ -193,7 +202,7 @@ pub fn commit(all: bool, done: bool, forced: bool, msg: &str) { } } - // TODO : Add all files in git selection if they got changed + let staged = git_staged(); // Laguage specific pre-commit hooks for lang in get_languages() { @@ -205,6 +214,10 @@ pub fn commit(all: bool, done: bool, forced: bool, msg: &str) { } } + for file in staged { + git_add(&file); + } + if all { Exec::cmd("git") .arg("add") diff --git a/src/main.rs b/src/main.rs index 809b849..e2f7fa9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -126,13 +126,25 @@ fn main() { let n: u64 = n.parse().unwrap(); revert_commits(n); } - Some(("branch", _)) => { - Exec::cmd("git") - .arg("branch") - .popen() - .unwrap() - .wait() - .unwrap(); + Some(("branch", branch_args)) => { + let branch: Option<&String> = branch_args.get_one("BRANCH"); + + if let Some(branch) = branch { + Exec::cmd("git") + .arg("checkout") + .arg(branch) + .popen() + .unwrap() + .wait() + .unwrap(); + } else { + Exec::cmd("git") + .arg("branch") + .popen() + .unwrap() + .wait() + .unwrap(); + } } Some(("push", push_args)) => { let force = push_args.get_flag("force"); @@ -145,6 +157,15 @@ fn main() { let msg: Option<&String> = commit_args.get_one("message"); let interactive = commit_args.get_flag("interactive"); + if !forced { + // Impossible commits + if no_commit_amount() > 0 { + println!("{}", "Unable to commit because of:".paint(Color::Red)); + show_rg(NO_COMMIT_REGEX); + std::process::exit(1); + } + } + if interactive || msg.is_none() { let gitmoji = select_gitmoji(); let title = inquire::prompt_text("Commit Title").unwrap();