Skip to content

Instantly share code, notes, and snippets.

@boscomonkey
Last active April 17, 2019 02:09
Show Gist options
  • Save boscomonkey/bfb08a7e9b2e5b5e91969c1d06f25b88 to your computer and use it in GitHub Desktop.
Save boscomonkey/bfb08a7e9b2e5b5e91969c1d06f25b88 to your computer and use it in GitHub Desktop.
Git pre-commit hook to warn about CSV, TSV, or TXT files with 50 lines or more
#!/bin/bash
# Git pre-commit hook to warn about CSV, TSV, or TXT files with 50
# lines or more. Original example at:
#
# https://codeinthehole.com/tips/tips-for-using-a-git-pre-commit-hook/
# Make sure this script is executable. Installation is simply:
# ln -s pre-commit.sh .git/hooks/pre-commit
FILES_PATTERN='\.(csv|tsv|txt)(\..+)?$'
FORBIDDEN='\b([5-9][0-9]|\d\d\d+)\b'
git diff --cached --name-only \
| grep -E $FILES_PATTERN \
| xargs wc -l \
| grep -v -E '^\s+\d+ total$' \
| grep --color -E $FORBIDDEN \
&& echo 'COMMIT REJECTED: files with 50 lines or longer. Please verify them before commiting.' \
&& exit 1
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment