Skip to content

Commit

Permalink
Merge branch 'main' into add-rule-git_stash
Browse files Browse the repository at this point in the history
  • Loading branch information
luizvbo committed Apr 28, 2024
2 parents a7f6b13 + 82ff32d commit 23523ca
Show file tree
Hide file tree
Showing 11 changed files with 498 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ohcrab"
version = "0.7.1"
version = "0.7.2"
edition = "2021"
description = "Fix your command line magically"
authors = ["Luiz Otavio Vilas Boas Oliveira <luiz.vbo@gmail.com>"]
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,19 +214,24 @@ new rules or improving the crate.
- [x] git_diff_staged
- [x] git_fix_stash
- [x] git_help_aliased
- [x] git_hook_bypass
- [x] git_main_master
- [x] git_merge
- [x] git_merge_unrelated
- [x] git_not_command
- [x] git_pull
- [x] git_pull_clone
- [x] git_pull_uncommitted_changes
- [x] git_push
- [x] git_push_different_branch_names
- [x] git_push_force
- [x] git_push_pull
- [x] git_push_without_commits
- [x] git_rebase_merge_dir
- [x] git_rebase_no_changes
- [x] git_remote_delete
- [x] git_remote_seturl_add
- [x] git_rm_local_modifications
- [x] go_run
- [x] gradle_wrapper
- [x] grep_arguments_order
Expand Down Expand Up @@ -288,12 +293,7 @@ new rules or improving the crate.
- [ ] fix_file
- [ ] gem_unknown_command
- [ ] git_flag_after_filename
- [ ] git_hook_bypass
- [ ] git_lfs_mistype
- [ ] git_merge_unrelated
- [ ] git_push_pull
- [ ] git_remote_seturl_add
- [ ] git_rm_local_modifications
- [ ] git_rm_recursive
- [ ] git_rm_staged
- [ ] git_stash
Expand Down
95 changes: 95 additions & 0 deletions src/rules/git_hook_bypass.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use crate::{
cli::command::CrabCommand,
rules::{
utils::git::{get_new_command_with_git_support, match_rule_with_git_support},
Rule,
},
shell::Shell,
utils::replace_argument,
};

static HOOKED_COMMANDS: &[&str] = &["am", "commit", "push"];

fn auxiliary_match_rule(command: &CrabCommand) -> bool {
HOOKED_COMMANDS
.iter()
.any(|&hooked_command| command.script_parts.contains(&hooked_command.to_owned()))
}

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<String> {
match HOOKED_COMMANDS
.iter()
.find(|&cmd| command.script.contains(cmd))
{
Some(hooked_command) => vec![replace_argument(
&command.script,
hooked_command,
&format!("{} --no-verify", hooked_command),
)],
None => vec![],
}
}

pub fn get_new_command(
command: &mut CrabCommand,
_system_shell: Option<&dyn Shell>,
) -> Vec<String> {
get_new_command_with_git_support(auxiliary_get_new_command, command, _system_shell)
}

pub fn get_rule() -> Rule {
Rule::new(
"git_hook_bypass".to_owned(),
None,
Some(1100),
Some(false),
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;

#[rstest]
#[case("git am", "", true)]
#[case("git commit", "", true)]
#[case("git commit -m 'foo bar'", "", true)]
#[case("git push", "", true)]
#[case("git push -u foo bar", "", true)]
#[case("git add foo", "", false)]
#[case("git status", "", false)]
#[case("git diff foo bar", "", false)]
fn test_match(#[case] command: &str, #[case] stdout: &str, #[case] is_match: bool) {
let mut command = CrabCommand::new(command.to_owned(), Some(stdout.to_owned()), None);
assert_eq!(match_rule(&mut command, None), is_match);
}

#[rstest]
#[case("git am", "", vec!["git am --no-verify"])]
#[case("git commit", "", vec!["git commit --no-verify"])]
#[case("git commit -m 'foo bar'", "", vec!["git commit --no-verify -m 'foo bar'"])]
#[case("git push", "", vec!["git push --no-verify"])]
#[case("git push -p", "", vec!["git push --no-verify -p"])]
fn test_get_new_command(
#[case] command: &str,
#[case] stdout: &str,
#[case] expected: Vec<&str>,
) {
let system_shell = Bash {};
let mut command = CrabCommand::new(command.to_owned(), Some(stdout.to_owned()), None);
assert_eq!(get_new_command(&mut command, Some(&system_shell)), expected);
}
}
75 changes: 75 additions & 0 deletions src/rules/git_merge_unrelated.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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(output) = &command.output {
command.script.contains("merge")
&& output.contains("fatal: refusing to merge unrelated histories")
} 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<String> {
vec![format!("{} --allow-unrelated-histories", command.script)]
}

pub fn get_new_command(command: &mut CrabCommand, system_shell: Option<&dyn Shell>) -> Vec<String> {
get_new_command_with_git_support(auxiliary_get_new_command, command, system_shell)
}

pub fn get_rule() -> Rule {
Rule::new(
"git_merge_unrelated".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 rstest::rstest;

const OUTPUT: &str = "fatal: refusing to merge unrelated histories";

#[rstest]
#[case("git merge test", OUTPUT, true)]
#[case("git merge master", "", false)]
#[case("ls", OUTPUT, false)]
fn test_match(#[case] command: &str, #[case] stdout: &str, #[case] is_match: bool) {
let mut command = CrabCommand::new(command.to_owned(), Some(stdout.to_owned()), None);
assert_eq!(match_rule(&mut command, None), is_match);
}

#[rstest]
#[case("git merge local", OUTPUT, vec!["git merge local --allow-unrelated-histories"])]
#[case("git merge -m \"test\" local", OUTPUT, vec!["git merge -m \"test\" local --allow-unrelated-histories"])]
#[case("git merge -m \"test local\" local", OUTPUT, vec!["git merge -m \"test local\" local --allow-unrelated-histories"])]
fn test_get_new_command(
#[case] command: &str,
#[case] stdout: &str,
#[case] expected: Vec<&str>,
) {
let mut command = CrabCommand::new(command.to_owned(), Some(stdout.to_owned()), None);
assert_eq!(get_new_command(&mut command, None), expected);
}
}
30 changes: 19 additions & 11 deletions src/rules/git_pull.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ pub fn get_rule() -> Rule {
#[cfg(test)]
mod tests {
use super::{get_new_command, match_rule};
use crate::cli::command::CrabCommand;
use crate::shell::Bash;
use crate::{parameterized_get_new_command_tests, parameterized_match_rule_tests};
use crate::{cli::command::CrabCommand, shell::Bash};
use rstest::rstest;

const OUTPUT: &str = r#"There is no tracking information for the current branch.
Please specify which branch you want to merge with.
Expand All @@ -73,15 +72,24 @@ If you wish to set tracking information for this branch you can do so with:
"#;

parameterized_match_rule_tests! {
match_rule,
match_rule_01: ("git pull", OUTPUT, true),
unmatch_rule_01: ("git pull", "", false),
unmatch_rule_02: ("ls", OUTPUT, false),
#[rstest]
#[case("git pull", OUTPUT, true)]
#[case("git pull", "", false)]
#[case("ls", OUTPUT, false)]
fn test_match(#[case] command: &str, #[case] stdout: &str, #[case] is_match: bool) {
let mut command = CrabCommand::new(command.to_owned(), Some(stdout.to_owned()), None);
assert_eq!(match_rule(&mut command, None), is_match);
}

parameterized_get_new_command_tests! {
get_new_command,
get_new_command_1: ("git pull", OUTPUT, "git branch --set-upstream-to=origin/master master && git pull"),
#[rstest]
#[case("git pull", OUTPUT, vec!["git branch --set-upstream-to=origin/master master && git pull"])]
fn test_get_new_command(
#[case] command: &str,
#[case] stdout: &str,
#[case] expected: Vec<&str>,
) {
let system_shell = Bash {};
let mut command = CrabCommand::new(command.to_owned(), Some(stdout.to_owned()), None);
assert_eq!(get_new_command(&mut command, Some(&system_shell)), expected);
}
}
8 changes: 5 additions & 3 deletions src/rules/git_push.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use super::{utils::git::get_new_command_with_git_support, Rule};
use crate::utils::replace_argument;
use crate::{
cli::command::CrabCommand, rules::utils::git::match_rule_with_git_support, shell::Shell,
cli::command::CrabCommand,
rules::utils::git::{get_new_command_with_git_support, match_rule_with_git_support},
rules::Rule,
shell::Shell,
utils::replace_argument,
};
use regex::Regex;

Expand Down
Loading

0 comments on commit 23523ca

Please sign in to comment.