Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added custom pre-commit hook #2499

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,10 @@ 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$
8 changes: 8 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions .taskcluster.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 -r requirements-test.txt &&
pre-commit run --all-files --show-diff-on-failure &&
tox -e $TOX_ENV &&
Expand Down
34 changes: 34 additions & 0 deletions pre-commit/check_json_indentation.py
Original file line number Diff line number Diff line change
@@ -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)