From a623f9a3e4c04c5a433ecab25071a67fd80ea2d8 Mon Sep 17 00:00:00 2001 From: jm Date: Mon, 29 Jan 2024 18:38:05 +0100 Subject: [PATCH] use checkstyle before commit --- .pre-commit-config.yaml | 9 +++++++++ scripts/pre-commit/checkstyle.py | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 .pre-commit-config.yaml create mode 100644 scripts/pre-commit/checkstyle.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 000000000000..f617c929f230 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,9 @@ +repos: +- repo: local + hooks: + - id: mvn-checkstyle + verbose: true + entry: python scripts/pre-commit/checkstyle.py + name: Runs maven checkstyle + language: python + files: \.(java)$ \ No newline at end of file diff --git a/scripts/pre-commit/checkstyle.py b/scripts/pre-commit/checkstyle.py new file mode 100644 index 000000000000..3d7c8623da52 --- /dev/null +++ b/scripts/pre-commit/checkstyle.py @@ -0,0 +1,23 @@ +import sys +import logging +import subprocess + +_logger = logging.getLogger() +logging.basicConfig(format='%(message)s', level=logging.DEBUG) + + +if __name__ == '__main__': + files = [x for x in sys.argv[1:] if x.lower().endswith('java')] + _logger.info(f'Found [{len(files)}] files from [{len(sys.argv) - 1}] input files') + + cmd = "mvn checkstyle:check" + + try: + with subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, text=True) as process: + for line in process.stdout: + print(line, end='') + except Exception as e: + _logger.critical(f'Error: {repr(e)}, ret code: {e.returncode}') + + # for filename in files: + # pass