-
Notifications
You must be signed in to change notification settings - Fork 2
/
pre-commit.sample
executable file
·36 lines (29 loc) · 1.21 KB
/
pre-commit.sample
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/bin/bash
#
# This script checks for errors in whitespace and also untracked files and loose print statements.
# It helps to keep the git diffs clean by not comparing spurious whitespaces.
#
# Change this to a space-delimited list of dirs in the top level of your project that contain
# code to be checked.
CODE_DIRS="data horton doc scripts tools"
## Nothing to change below this line ##
red="\033[1;31m"
color_end="\033[0m"
# Check unwanted trailing whitespace or space/tab indents;
if [[ `git diff --cached --check` ]]; then
echo -e "${red}Commit failed: trailing whitespace, trailing empty lines, DOS/Windows line endings${color_end}"
git diff --cached --check
exit 1
fi
# Check for untracked files (not in .gitignore)
if [[ `git status -u ${CODE_DIRS} -s | grep "^??"` ]]; then
echo -e "${red}Commit failed: untracked files (not in .gitignore).${color_end}"
git status -u ${CODE_DIRS} -s | grep "^??"
exit 1
fi
# Check for new print statements
if [[ `git diff --cached | grep '^+' | sed 's/^.//' | sed 's:#.*$::g' | grep 'print '` ]]; then
echo -e "${red}Commit failed: print statements${color_end}"
git diff --cached | grep '^+' | sed 's/^.//' | sed 's:#.*$::g' | grep print
exit 1
fi