diff options
author | 2017-03-31 09:20:02 -0700 | |
---|---|---|
committer | 2017-04-03 17:24:43 +0000 | |
commit | c29da374e5860a4b58623b86442877d16c34484e (patch) | |
tree | fd41fa81707e9ff3dbdae4b4e5aa4d730d24a298 | |
parent | 02311bd39d64b1417e8580c2a993620c2234254d (diff) |
Add a cpplint preupload hook.
This runs cpplint.py over modified .cc and .h files before uploading
them to gerrit. This generally takes ~1-2 seconds based on my testing
and the longest one I've been able to find was ~10 seconds.
Test: PREUPLOAD_COMMIT=d9911eeca ./tools/cpplint_presubmit.py
Test: PREUPLOAD_COMMIT=405284789 ./tools/cpplint_presubmit.py
Test: PREUPLOAD_COMMIT=9763f2eb7 ./tools/cpplint_presubmit.py
Test: Manual
Bug: 35810246
Bug: 36855589
Change-Id: Ie0ed9136bdde15d78f2aa40c73fef97e12d12dda
-rw-r--r-- | PREUPLOAD.cfg | 1 | ||||
-rwxr-xr-x | tools/cpplint_presubmit.py | 67 |
2 files changed, 68 insertions, 0 deletions
diff --git a/PREUPLOAD.cfg b/PREUPLOAD.cfg index cf1832beb4..0ed230c408 100644 --- a/PREUPLOAD.cfg +++ b/PREUPLOAD.cfg @@ -1,2 +1,3 @@ [Hook Scripts] check_generated_files_up_to_date = tools/cpp-define-generator/presubmit-check-files-up-to-date +check_cpplint_on_changed_files = tools/cpplint_presubmit.py diff --git a/tools/cpplint_presubmit.py b/tools/cpplint_presubmit.py new file mode 100755 index 0000000000..478151736f --- /dev/null +++ b/tools/cpplint_presubmit.py @@ -0,0 +1,67 @@ +#!/usr/bin/python3 +# +# Copyright 2017, The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# TODO We should unify this with build/Android.cpplint.mk. + +import os +import pathlib +import subprocess +import sys + +IGNORED_FILES = {"runtime/elf.h", "runtime/openjdkjvmti/include/jvmti.h"} + +INTERESTING_SUFFIXES = {".h", ".cc"} + +CPPLINT_FLAGS = [ + '--filter=-whitespace/line_length,-build/include,-readability/function,-readability/streams,-readability/todo,-runtime/references,-runtime/sizeof,-runtime/threadsafe_fn,-runtime/printf', + '--quiet', +] + +def is_interesting(f): + """ + Returns true if this is a file we want to run through cpplint before uploading. False otherwise. + """ + path = pathlib.Path(f) + return f not in IGNORED_FILES and path.suffix in INTERESTING_SUFFIXES and path.exists() + +def get_changed_files(commit): + """ + Gets the files changed in the given commit. + """ + return subprocess.check_output( + ["git", 'diff-tree', '--no-commit-id', '--name-only', '-r', commit], + stderr=subprocess.STDOUT, + universal_newlines=True).split() + +def run_cpplint(files): + """ + Runs cpplint on the given files. + """ + if len(files) == 0: + return + sys.exit(subprocess.call(['tools/cpplint.py'] + CPPLINT_FLAGS + files)) + +def main(): + if 'PREUPLOAD_COMMIT' in os.environ: + commit = os.environ['PREUPLOAD_COMMIT'] + else: + print("WARNING: Not running as a pre-upload hook. Assuming commit to check = 'HEAD'") + commit = "HEAD" + files_to_check = [f for f in get_changed_files(commit) if is_interesting(f)] + run_cpplint(files_to_check) + +if __name__ == '__main__': + main() |