Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CodeStyle] fix cpplint hook not working on CI #46136

Merged
merged 6 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ repos:
entry: bash ./tools/codestyle/cpplint_pre_commit.hook
language: system
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx)$
args:
- --extensions=c,cc,cxx,cpp,cu,cuh,h,hpp,hxx,kps
Copy link
Member Author

@SigureMo SigureMo Sep 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

默认 extensions 无法处理 kps,因此将 kps 后缀添加到 --extensions 参数,不过因为现在 files 字段没有传入 kps 文件,因此还不会检测 kps 文件,如果需要检测的话,在 files 字段加入 kps 即可

- --filter=-readability/fn_size,-build/include_what_you_use,-build/c++11,-whitespace/parens
- --quiet
Copy link
Member Author

@SigureMo SigureMo Sep 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cpplint 默认行为是所有传入的文件都会打印 Done processing <file_path>,使用 --quiet 参数可以避免打印 Success 了的文件信息(Failed 还会打印),使得 log 更加清晰

- repo: local
hooks:
- id: pylint-doc-string
Expand Down
25 changes: 2 additions & 23 deletions tools/codestyle/cpplint_pre_commit.hook
Original file line number Diff line number Diff line change
@@ -1,32 +1,11 @@
#!/bin/bash

TOTAL_ERRORS=0

readonly VERSION="1.6.0"

version=$(cpplint --version)

if [[ ! $TRAVIS_BRANCH ]]; then
# install cpplint on local machine.
if ! [[ $version == *"$VERSION"* ]]; then
if ! [[ $version == *"$VERSION"* ]]; then
pip install cpplint==1.6.0
fi
# diff files on local machine.
files=$(git diff --cached --name-status | awk '$1 != "D" {print $2}')
else
# diff files between PR and latest commit on Travis CI.
branch_ref=$(git rev-parse "$TRAVIS_BRANCH")
head_ref=$(git rev-parse HEAD)
files=$(git diff --name-status $branch_ref $head_ref | awk '$1 != "D" {print $2}')
fi
# The trick to remove deleted files: https://stackoverflow.com/a/2413151
for file in $files; do
if [[ $file =~ ^(patches/.*) ]]; then
continue;
else
cpplint --filter=-readability/fn_size,-build/include_what_you_use,-build/c++11,-whitespace/parens $file;
TOTAL_ERRORS=$(expr $TOTAL_ERRORS + $?);
fi
done

exit $TOTAL_ERRORS
cpplint $@
Copy link
Member Author

@SigureMo SigureMo Sep 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

相关参数写在 pre-commit-config.yaml 里,以便维护,本 hook 仅做安装 + 调用