From abd8d292a0d747087d731c3cfa22b0926d6d4d6d Mon Sep 17 00:00:00 2001 From: Chuck Atkins <chuck.atkins@kitware.com> Date: Thu, 20 Apr 2017 15:32:16 -0400 Subject: [PATCH] Allow a regex to exclude files from format checks --- flake8.cfg | 1 + scripts/developer/git/git-clang-format | 13 +++++++++++++ scripts/travis/run-format.sh | 2 +- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/flake8.cfg b/flake8.cfg index a3e81f96b..48317219e 100644 --- a/flake8.cfg +++ b/flake8.cfg @@ -3,3 +3,4 @@ max-line-length = 80 max-complexity = 14 format = pylint ignore = F403,F405,F999 +exclude = thirdparty/ diff --git a/scripts/developer/git/git-clang-format b/scripts/developer/git/git-clang-format index 74fd451a8..e0557e503 100755 --- a/scripts/developer/git/git-clang-format +++ b/scripts/developer/git/git-clang-format @@ -112,6 +112,8 @@ def main(): help='passed to clang-format'), p.add_argument('-v', '--verbose', action='count', default=0, help='print extra information') + p.add_argument('-x', '--exclude', + help='Regex to match for files to exclude from checking') # We gather all the remaining positional arguments into 'args' since we need # to use some heuristics to determine whether or not <commit> was present. # However, to print pretty messages, we make use of metavar and help. @@ -134,6 +136,7 @@ def main(): changed_lines = compute_diff_and_extract_lines(commits, files) if opts.verbose >= 1: ignored_files = set(changed_lines) + filter_by_exclude(changed_lines, opts.exclude) filter_by_extension(changed_lines, opts.extensions.lower().split(',')) if opts.verbose >= 1: ignored_files.difference_update(changed_lines) @@ -314,6 +317,16 @@ def extract_lines(patch_file): return matches +def filter_by_exclude(dictionary, exclude_re): + """Delete every key in `dictionary` that matches the exclusion regex. + + `exclude_re` must be a valid regex.""" + exre = re.compile(exclude_re) + for filename in dictionary.keys(): + if exre.match(filename): + del dictionary[filename] + + def filter_by_extension(dictionary, allowed_extensions): """Delete every key in `dictionary` that doesn't have an allowed extension. diff --git a/scripts/travis/run-format.sh b/scripts/travis/run-format.sh index 79f393f7a..ac203f2ce 100755 --- a/scripts/travis/run-format.sh +++ b/scripts/travis/run-format.sh @@ -15,7 +15,7 @@ cd ${SOURCE_DIR} # Check C and C++ code with clang-format echo "Checking formatting for commit range: ${COMMIT_RANGE}" -DIFF="$(./scripts/developer/git/git-clang-format --diff ${COMMIT_RANGE})" +DIFF="$(./scripts/developer/git/git-clang-format -x '^thirdparty/' --diff ${COMMIT_RANGE})" if [ -n "${DIFF}" ] && [ "${DIFF}" != "no modified files to format" ] then echo "clang-format:" -- GitLab