forked from k2-fsa/k2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_style_cpplint.sh
executable file
·104 lines (85 loc) · 2.08 KB
/
check_style_cpplint.sh
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash
# Copyright 2020 Fangjun Kuang (csukuangfj@gmail.com)
# See ../LICENSE for clarification regarding multiple authors
# Usage:
# ./scripts/check_style_cpplint.sh
# check modified files using the default build directory "build"
#
# ./scripts/check_style_cpplint.sh ./build
# check modified files using the specified build directory "./build"
#
# ./scripts/check_style_cpplint.sh ./build 1
# check modified files of last commit using the specified build directory "./build"
#
# You can also go to the build directory and do the following:
# cd build
# cmake ..
# make check_style
cur_dir=$(cd $(dirname $BASH_SOURCE) && pwd)
k2_dir=$(cd $cur_dir/.. && pwd)
if [ $# -ge 1 ]; then
build_dir=$(cd $1 && pwd)
shift
else
# we assume that the build dir is "./build"; cpplint
# is downloaded automatically when the project is configured.
build_dir=$k2_dir/build
fi
cpplint_src=$build_dir/_deps/cpplint-src/cpplint.py
source $k2_dir/scripts/utils.sh
# return true if the given file is a c++ source file
# return false otherwise
function is_source_code_file() {
case "$1" in
*.cc|*.h)
echo true;;
*)
echo false;;
esac
}
function check_style() {
python3 $cpplint_src $1 || abort $1
}
function check_last_commit() {
files=$(git diff HEAD^1 --name-only --diff-filter=ACDMRUXB)
echo $files
}
function check_current_dir() {
files=$(git status -s -uno --porcelain | awk '{
if (NF == 4) {
# a file has been renamed
print $NF
} else {
print $2
}}')
echo $files
}
function do_check() {
if [ $# -eq 1 ]; then
echo "checking last commit"
files=$(check_last_commit)
else
echo "checking current dir"
files=$(check_current_dir)
fi
for f in $files; do
need_check=$(is_source_code_file $f)
if $need_check; then
[[ -f $f ]] && check_style $f
fi
done
}
function main() {
if [ ! -f $cpplint_src ]; then
abort "\n$cpplint_src does not exist.\n\
Please run
mkdir build
cd build
cmake ..
before running this script."
fi
do_check $1
ok "Great! Style check passed!"
}
cd $k2_dir
main $1