-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathpre-commit
executable file
·51 lines (41 loc) · 1.22 KB
/
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
43
44
45
46
47
48
49
50
51
#!/bin/bash
# Runs scripts/cpplint.py on the modified files
# Based on https://github.com/s0enke/git-hooks/
#
# @author Peter Schrammel <[email protected]>
gitRoot="$(dirname $0)/../.."
# this is the magic:
# retrieve all files in staging area that are added, modified or renamed
# but no deletions etc
files=$(git diff-index --name-only --cached --diff-filter=ACMR HEAD -- )
if [ "$files" == "" ]; then
exit 0
fi
# create temporary copy of staging area and delete upon exit
cleanup()
{
rm -rf $tmpStaging
}
trap cleanup EXIT
tmpStaging=$(mktemp -d)
touch $tmpStaging/.git
# Copy contents of staged version of files to temporary staging area
# because we only want the staged version that will be commited and not
# the version in the working directory
stagedFiles=""
for file in $files
do
id=$(git diff-index --cached HEAD $file | cut -d " " -f4)
# create staged version of file in temporary staging area with the same
# path as the original file
mkdir -p "$tmpStaging/$(dirname $file)"
git cat-file blob $id > "$tmpStaging/$file"
stagedFiles="$stagedFiles $tmpStaging/$file"
done
output=$(cd $gitRoot; scripts/cpplint.py $stagedFiles 2>&1)
retval=$?
if [ $retval -ne 0 ]
then
echo "$output"
exit 1
fi