From 5ecacf7eb6c4bc87b233d2abd33a8653500077d6 Mon Sep 17 00:00:00 2001 From: Luiz Otavio Vilas Boas Oliveira Date: Mon, 22 Apr 2024 12:48:50 +0200 Subject: [PATCH] Add rule --- src/rules/git_pull_uncommitted_changes.rs | 83 +++++++++++++++++++++++ src/rules/mod.rs | 2 + 2 files changed, 85 insertions(+) create mode 100644 src/rules/git_pull_uncommitted_changes.rs diff --git a/src/rules/git_pull_uncommitted_changes.rs b/src/rules/git_pull_uncommitted_changes.rs new file mode 100644 index 0000000..20baa91 --- /dev/null +++ b/src/rules/git_pull_uncommitted_changes.rs @@ -0,0 +1,83 @@ +use crate::{ + cli::command::CrabCommand, + rules::{ + utils::git::{get_new_command_with_git_support, match_rule_with_git_support}, + Rule, + }, + shell::Shell, +}; + +fn auxiliary_match_rule(command: &CrabCommand) -> bool { + if let Some(stdout) = &command.output { + command.script.contains("pull") + && (stdout.contains("You have unstaged changes") + || stdout.contains("contains uncommitted changes")) + } else { + false + } +} + +pub fn match_rule(command: &mut CrabCommand, _system_shell: Option<&dyn Shell>) -> bool { + match_rule_with_git_support(auxiliary_match_rule, command) +} + +fn auxiliary_get_new_command( + command: &CrabCommand, + system_shell: Option<&dyn Shell>, +) -> Vec { + vec![system_shell + .unwrap() + .and(vec!["git stash", "git pull", "git stash pop"])] +} + +pub fn get_new_command( + command: &mut CrabCommand, + _system_shell: Option<&dyn Shell>, +) -> Vec { + get_new_command_with_git_support(auxiliary_get_new_command, command, _system_shell) +} + +pub fn get_rule() -> Rule { + Rule::new( + "git_pull_uncommitted_changes".to_owned(), + None, + None, + None, + match_rule, + get_new_command, + None, + ) +} + +#[cfg(test)] +mod tests { + use super::{get_new_command, match_rule}; + use crate::cli::command::CrabCommand; + use crate::shell::Bash; + use rstest::rstest; + const OUTPUT: &str = "error: Cannot pull with rebase: You have unstaged changes."; + + #[rstest] + #[case("git pull", OUTPUT, true)] + #[case("git pull", "", false)] + #[case("ls", OUTPUT, false)] + fn test_match(#[case] command: &str, #[case] output: &str, #[case] is_match: bool) { + let mut command = CrabCommand::new(command.to_owned(), Some(output.to_owned()), None); + assert_eq!(match_rule(&mut command, None), is_match); + } + + #[rstest] + #[case("git pull", "error: Cannot pull with rebase: You have unstaged changes.", vec!["git stash && git pull && git stash pop"])] + fn test_get_new_command( + #[case] command: &str, + #[case] output: &str, + #[case] expected: Vec<&str>, + ) { + let system_shell = Bash {}; + let mut command = CrabCommand::new(command.to_owned(), Some(output.to_owned()), None); + assert_eq!( + get_new_command(&mut command, Some(&system_shell)), + expected.iter().map(|&s| s.to_owned()).collect::>() + ); + } +} diff --git a/src/rules/mod.rs b/src/rules/mod.rs index 48100e6..7232510 100644 --- a/src/rules/mod.rs +++ b/src/rules/mod.rs @@ -58,6 +58,7 @@ mod git_merge; mod git_not_command; mod git_pull; mod git_pull_clone; +mod git_pull_uncommitted_changes; mod git_push; mod git_rebase_merge_dir; mod go_run; @@ -163,6 +164,7 @@ pub fn get_rules() -> Vec { git_not_command::get_rule(), git_pull::get_rule(), git_pull_clone::get_rule(), + git_pull_uncommitted_changes::get_rule(), git_push::get_rule(), git_rebase_merge_dir::get_rule(), go_run::get_rule(),