Last active
November 23, 2021 10:04
-
-
Save BuonOmo/aec340bf01b9b8408897bd8203f2d74e to your computer and use it in GitHub Desktop.
Git pre-commit hook that detects non-breaking spaces
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env zsh | |
# Look for non-breaking spaces hidden in code, refuse | |
# to commit if there are some. This hook depends on the | |
# great ripgrep (https://github.com/BurntSushi/ripgrep). | |
# You can use bash if you prefere it over zsh, and ag | |
# over rg. | |
! rg --colors 'match:bg:yellow' -C 1 ' ' $(git ls-files) && exit 0 | |
cat << TEXT | |
⚠️ There is a non breaking space in your code, see notice above. | |
You should remove it before committing, or you can ignore | |
this using \`git commit --no-verify\`. | |
To replace every non-breaking spaces in your code, you can copy paste | |
this snippet: | |
rg --files-with-matches ' ' \$(git ls-files) | xargs sed -i '' 's/ / /' | |
TEXT | |
exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment