diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a85959..9384668 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ and this project adheres to - Support for getting command output from iTerm2. - rules: `sudo` now reacts to `operation not permitted` messages. +- rules: react to `nothing added to commit but untracked files present` with + `git commit`. ### Fixed diff --git a/src/rules/git_commit_untracked_files.rs b/src/rules/git_commit_untracked_files.rs new file mode 100644 index 0000000..d68319c --- /dev/null +++ b/src/rules/git_commit_untracked_files.rs @@ -0,0 +1,38 @@ +pub fn git_commit_untracked_files(mut cmd: Vec, error: &str) -> Option> { + if !(error.contains("no changes added to commit") || error.contains("untracked files present")) + { + log::debug!("does not contain a matching error message"); + return None; + } + let mut new_cmd = vec![ + "git".to_string(), + "add".to_string(), + "-A".to_string(), + "&&".to_string(), + ]; + new_cmd.append(&mut cmd); + Some(new_cmd) +} + +#[cfg(test)] +mod test { + use crate::shlex::shlex; + + use super::*; + + #[test] + fn git_commit_no_changes_test() { + let cmd = shlex("git commit -m 'initial commit'"); + let error = "no changes added to commit (use \"git add\" and/or \"git commit -a\")"; + let expected = shlex("git add -A && git commit -m 'initial commit'"); + assert_eq!(Some(expected), git_commit_untracked_files(cmd, error)); + } + + #[test] + fn git_commit_untracked_files_test() { + let cmd = shlex("git commit -m 'initial commit'"); + let error = "nothing added to commit but untracked files present"; + let expected = shlex("git add -A && git commit -m 'initial commit'"); + assert_eq!(Some(expected), git_commit_untracked_files(cmd, error)); + } +} diff --git a/src/rules/mod.rs b/src/rules/mod.rs index 841af32..cb45446 100644 --- a/src/rules/mod.rs +++ b/src/rules/mod.rs @@ -49,6 +49,7 @@ define_rules!( cp_dir, git_add_all_lowercase, git_commit_no_changes, + git_commit_untracked_files, git_no_upstream, git_wrong_command, mkdir_missing_parent,