diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 625eb1000..115ee7de6 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -61,6 +61,13 @@ repos: - repo: meta hooks: - id: check-useless-excludes + - repo: local + hooks: + - id: check-json-indentation + name: Check JSON Indentation Consistency + entry: pre-commit/check_json_indentation.py + language: python + files: \.json$ - repo: https://github.com/astral-sh/uv-pre-commit # uv version. rev: 0.8.15 diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml new file mode 100644 index 000000000..db2bc0325 --- /dev/null +++ b/.pre-commit-hooks.yaml @@ -0,0 +1,8 @@ +- id: check-json-indentation + name: Check JSON Indentation Consistency + description: This hook checks that JSON files do not have mixed tabs and spaces for indentation. + entry: pre-commit/check_json_indentation.py + language: python + files: \.json$ + types: [text] + verbose: true diff --git a/.taskcluster.yml b/.taskcluster.yml index 22e10e44d..3c12b6353 100644 --- a/.taskcluster.yml +++ b/.taskcluster.yml @@ -48,6 +48,7 @@ tasks: - "git clone --quiet ${repository} bugbot && cd bugbot && git -c advice.detachedHead=false checkout ${head_rev} && + chmod +x pre-commit/check_json_indentation.py && pip install --quiet -e '.[test]' && pre-commit run --all-files --show-diff-on-failure && tox -e $TOX_ENV && diff --git a/pre-commit/check_json_indentation.py b/pre-commit/check_json_indentation.py new file mode 100644 index 000000000..b31b277c4 --- /dev/null +++ b/pre-commit/check_json_indentation.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 +import sys + + +def check_json_indentation(filename): + with open(filename, "r") as file: + lines = file.readlines() + + tabs_found = False + spaces_found = False + + for line in lines: + stripped_line = line.lstrip() + if stripped_line and line.startswith("\t"): + tabs_found = True + elif stripped_line and line.startswith(" "): + spaces_found = True + + if tabs_found and spaces_found: + print(f"Error: {filename} contains mixed tabs and spaces for indentation.") + return False + + return True + + +if __name__ == "__main__": + files_to_check = sys.argv[1:] + exit_code = 0 + + for json_file in files_to_check: + if not check_json_indentation(json_file): + exit_code = 1 + + sys.exit(exit_code)