Script to check the diff content of files in output of git status for foul words, variants of those words and repeated chars.
Will not work on Windhose.
Personally, I don't like to mess around with git history, so I try to keep the project history as readable and clean as I can.
Now, I'm also someone who likes to use the same non-sensical words to test stuff or during quick implementations:
make it work, make it right, make it fast.
The problem is I don't always catch everything before committing stuff when I make it right.
This is me accepting I have bad habits, and working around them.
So what if I write let dink = '';
?
The hook will catch this and make me aware of it, giving me the option to either stop the commit or to go ahead and push it into the repository's history.
Now I can at least be aware when commiting sinful (oh so sinful) things into the repo's history.
- Install requirements.txt
pip install -r requirements.txt
- Copy
config.toml.default
toconfig.toml
andassets/violations.toml.default
toassets/violations.toml
. - Create pre-commit hook somewhere you can easily access
mkdir -p ~/.git-templates/hooks
vim ~/.git-templates/hooks/pre-commit
- Call the script from inside the hook
#!/bin/sh
# Add other pre-commit hooks
python /PATH/TO/slapper.py
- Make hook executable
chmod u+x ~/.git-templates/hooks/pre-commit
- Set global rule in git to call this on commit in every repository Reminder: You could also set this per repository
git config --global core.hooksPath ~/.git-templates/hooks/
The rules for all will be used as baseline.
Different words and patterns (= basic regex) can be added to specific file extension identifiers, to overwrite the eventual result.
foul
sections will be appended to the list of violations, while acceptable
sections will remove them from the list.
Note: The regex patterns are provided as literal strings; no need to escape them.
[all]
[all.foul]
words = [
"dink",
"poop",
]
patterns = [
'([a-zA-Z])\\1{3,}'
]
[css]
[css.foul]
words = ["test"]
[css.acceptable]
words = ["poop"]
patterns = ['#[a-zA-Z0-9]*']
The resulting foul_words for .css
files will be dink, test
, all other files will have dink, poop
as foul_words.
For patterns it's a bit trickier: the first (= foul) pattern makes sure we don't repeat the same textcharacter more than 3 times.
The second one allows character repetitions when they are preceded by a #
character in a .css
file.
This results in a situation where .py
files cannot contain the following text: # aaaaAAAaa
but .css
files can,
because the violation is overruled by the acceptable pattern.
Tests are located in slapper/tests
and require the py.test module.
Optional: Create a virtual environment to test.
python -m virtualenv .venv
source .venv/bin/activate
Install pytest
pip install pytest
Navigate to the slapper
directory and run the tests.
python -m pytest -v
Smile as you watch the tests pass.