Skip to content

Commit eb4e348

Browse files
peterschrammeltautschnig
authored andcommitted
Pre-commit hook to run cpplint
To install the hook cp .githooks/pre-ommit .git/hooks/pre-commit or use a symbolic link. Then, when running git commit, you should get the linter output (if any) before being prompted to enter a commit message. To bypass the check (e.g. if there was a false positive), add the option --no-verify.
1 parent 3fb7b1f commit eb4e348

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

.githooks/pre-commit

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/bash
2+
# Runs scripts/cpplint.py on the modified files
3+
# Based on https://github.com/s0enke/git-hooks/
4+
#
5+
# @author Peter Schrammel <[email protected]>
6+
7+
gitRoot="$(dirname $0)/../.."
8+
9+
# this is the magic:
10+
# retrieve all files in staging area that are added, modified or renamed
11+
# but no deletions etc
12+
files=$(git diff-index --name-only --cached --diff-filter=ACMR HEAD -- )
13+
14+
if [ "$files" == "" ]; then
15+
exit 0
16+
fi
17+
18+
# create temporary copy of staging area and delete upon exit
19+
cleanup()
20+
{
21+
rm -rf $tmpStaging
22+
}
23+
24+
trap cleanup EXIT
25+
26+
tmpStaging=$(mktemp -d)
27+
28+
# Copy contents of staged version of files to temporary staging area
29+
# because we only want the staged version that will be commited and not
30+
# the version in the working directory
31+
stagedFiles=""
32+
for file in $files
33+
do
34+
id=$(git diff-index --cached HEAD $file | cut -d " " -f4)
35+
36+
# create staged version of file in temporary staging area with the same
37+
# path as the original file
38+
mkdir -p "$tmpStaging/$(dirname $file)"
39+
git cat-file blob $id > "$tmpStaging/$file"
40+
stagedFiles="$stagedFiles $tmpStaging/$file"
41+
done
42+
43+
output=$(cd $gitRoot; python scripts/cpplint.py $stagedFiles 2>&1)
44+
retval=$?
45+
46+
if [ $retval -ne 0 ]
47+
then
48+
echo "$output"
49+
exit 1
50+
fi

0 commit comments

Comments
 (0)