-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathgit-pre-commit
executable file
·42 lines (35 loc) · 1.25 KB
/
git-pre-commit
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
37
38
39
40
41
42
#!/usr/bin/env bash
# This script is being invoked via <root dir>/.git/hooks, hence the
# base directory is up two levels, not just one.
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd ../.. && pwd )"
cd "$DIR"
# Make sure we can use git correctly
git rev-parse --verify HEAD >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Unable to determine revision of this git repo"
exit 1
fi
# Check whitespace problems
git diff-index --check --cached HEAD --
if [ $? -ne 0 ]; then
echo 'Please address these whitespace issues and then try committing again'
exit 1
fi
# Run rubocop on any ruby files that have been changed or added
TMPFILE=$(mktemp /tmp/rubocop-list.XXXXX)
function cleanup() {
rm -f "$TMPFILE"
}
trap cleanup exit
pattern='^(lib|rake|spec/octocatalog-diff/integration|spec/octocatalog-diff/tests)/.*\.rb$'
antipattern='^(lib/octocatalog-diff/external/|spec/octocatalog-diff/tests/external/)'
cd "$DIR" && git diff-index --diff-filter=AM --name-only --cached HEAD | egrep "$pattern" | egrep -v "$antipattern" > "$TMPFILE"
if [ -s "$TMPFILE" ]; then
files=$( cat "$TMPFILE" | tr "\n" " " )
"$DIR/script/fmt" ${files}
if [ $? -ne 0 ]; then
echo 'Please address these style or syntax issues and then try committing again'
exit 1
fi
fi
exit 0