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