Skip to content

Commit

Permalink
Setup pre-commit hooks.
Browse files Browse the repository at this point in the history
Adds pre-commit hooks to just Cobalt added directories and files.

b/372338856
  • Loading branch information
briantting committed Oct 11, 2024
1 parent 7e79f1a commit ab5fb1d
Show file tree
Hide file tree
Showing 8 changed files with 289 additions and 0 deletions.
101 changes: 101 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
default_stages: [pre-commit]

default_language_version:
python: python3

files: |
(?x)^(
.github/|
chrobalt/|
internal/|
starboard/|
docker-compose.yaml
)
repos:
- repo: https://cobalt.googlesource.com/pre-commit-hooks
rev: e1668fe86af3810fbca72b8653fe478e66a0afdc # v3.2.0
hooks:
- id: check-case-conflict
- id: check-xml
- id: check-yaml
- id: end-of-file-fixer
- id: mixed-line-ending
- id: trailing-whitespace

- repo: https://cobalt.googlesource.com/codespell
rev: 67c489d36dd4c52cbb9e4755d90c35c6231842ef # v2.0.0
hooks:
- id: codespell
name: Spell Check
args: [-x, chrobalt/precommit/.codespellignorelines,
# The --ignore-words-list argument has a bug where it needs to
# be lowercase, see
# https://github.com/codespell-project/codespell/issues/1390
--ignore-words-list, "atleast,varius",
]
exclude: |
(?x)^(
starboard/content/ssl/certs/|
starboard/loader_app/app_key_test.cc|
starboard/shared/starboard/player/testdata
)
- repo: local
hooks:
- id: clang-format
name: clang-format
entry: clang-format
language: system
types: [c++]
args: [-i, -style=file]
- id: cpplint
name: cpplint
entry: cpplint
language: system
types: [c++]
args: [--verbose=4, --quiet]
- id: yapf
name: yapf
entry: yapf
language: system
types: [python]
args: [-i, -vv]
- id: pylint
name: pylint
entry: pylint
language: system
types: [python]
args: [-d W0201]
- id: python3-compatibility-check
name: Python 3 Compatibility Check
entry: python3 ./chrobalt/precommit/python3_check.py
language: python
types: [python]
verbose: true
- id: google-java-format
name: google-java-format
entry: python3 ./chrobalt/precommit/google_java_format_wrapper.py
language: python
types: [java]
args: [-i]
- id: gcheckstyle
name: gcheckstyle
entry: python3 ./chrobalt/precommit/gcheckstyle_wrapper.py
language: python
types: [java]
verbose: true
- id: gn-format
name: gn-format
entry: gn format
language: system
files: '.*\.gni?$'
- id: bug-in-commit-message-check
name: Bug In Commit Message Check
entry: python3 ./chrobalt/precommit/bug_in_description_check.py
language: python
stages: [pre-push]
always_run: true
pass_filenames: false
2 changes: 2 additions & 0 deletions chrobalt/precommit/.codespellignorelines
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vp9AllowList.get("Technicolor").add("STING");
// Onces represent initializations that should only ever happen once per
57 changes: 57 additions & 0 deletions chrobalt/precommit/bug_in_description_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env python3
#
# Copyright 2024 The Cobalt Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Warns if commit message does not include a tracking bug."""

import os
import re
import subprocess
import sys

RE_BUG_DESCRIPTION = re.compile(r'(\s|^)b/[0-9]+(\s|$)')


def check_commits_for_bug_in_description() -> bool:
"""Checks commits for tracking bug in description."""
from_ref = os.environ.get('PRE_COMMIT_FROM_REF')
to_ref = os.environ.get('PRE_COMMIT_TO_REF')
if not from_ref or not to_ref:
print('Invalid from ref or to ref, exiting...')
sys.exit(0)

commits_with_errors = []
git_hashes = subprocess.check_output(
['git', 'log', f'{from_ref}..{to_ref}', '--format=%h'])
git_hashes = git_hashes.strip().decode('utf-8').split()

for git_hash in git_hashes:
commit_message = subprocess.check_output(
['git', 'show', git_hash, '--format=%b', '--quiet'])
commit_message = commit_message.strip().decode('utf-8')
if not RE_BUG_DESCRIPTION.search(commit_message):
commits_with_errors.append(git_hash)

if commits_with_errors:
print('Some changes do not reference a bug in the form b/########')
for commits_with_error in commits_with_errors:
subprocess.call(
['git', 'show', commits_with_error, '-s', '--oneline'])
return True

return False


if __name__ == '__main__':
sys.exit(check_commits_for_bug_in_description())
43 changes: 43 additions & 0 deletions chrobalt/precommit/gcheckstyle_wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env python3
#
# Copyright 2024 The Cobalt Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""See if internal tool gcheckstyle is available."""

import os
import subprocess
import sys

sys.path.append(os.path.join(os.path.dirname(__file__), '../../'))

try:
from internal.precommit.internal_tools_path import GCHECKSTYLE_PATH
except ImportError:
GCHECKSTYLE_PATH = None

if __name__ == '__main__':
gcheckstyle_args = sys.argv[1:]

if not GCHECKSTYLE_PATH:
print('Checkstyle not available, skipping.')
sys.exit(0)

try:
sys.exit(subprocess.call([GCHECKSTYLE_PATH] + gcheckstyle_args))
except FileNotFoundError:
print('Checkstyle not found, skipping.')
sys.exit(0)
except OSError as e:
print('You may need to run gcert.')
raise e
32 changes: 32 additions & 0 deletions chrobalt/precommit/google_java_format_wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python3
#
# Copyright 2024 The Cobalt Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Wrapper to check for google-java-format tool."""

import platform
import subprocess
import sys

if __name__ == '__main__':
if platform.system() != 'Linux':
sys.exit(0)

google_java_format_args = sys.argv[1:]
try:
sys.exit(
subprocess.call(['google-java-format'] + google_java_format_args))
except FileNotFoundError:
print('google-java-format not found, skipping.')
sys.exit(0)
52 changes: 52 additions & 0 deletions chrobalt/precommit/python3_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env python3
#
# Copyright 2024 The Cobalt Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Warns if Python code isn't compatible with Python 3."""

import os
import py_compile
import sys
import tempfile
from typing import List


def _check_file_for_python3_compatibility(filename: str) -> bool:
if not os.path.exists(filename):
print(f'{filename} is not a valid path, skipping.')
return False

temp_directory = tempfile.TemporaryDirectory() # pylint: disable=consider-using-with
temp_file = os.path.join(temp_directory.name, 'cfile')
had_errors = False

try:
py_compile.compile(filename, cfile=temp_file, doraise=True)
except py_compile.PyCompileError as e:
print(e)
print(f'{filename} is not valid in Python 3, consider updating it.')
had_errors = True

temp_directory.cleanup()
return had_errors


def check_files_for_python3_compatibility(files: List[str]) -> bool:
"""Checks files for Python 3 compatibility."""
rets = [_check_file_for_python3_compatibility(file) for file in files]
return any(rets)


if __name__ == '__main__':
sys.exit(check_files_for_python3_compatibility(sys.argv[1:]))
Empty file.
2 changes: 2 additions & 0 deletions internal/precommit/internal_tools_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"""List internal tools here so as not to expose them in existing scripts."""
GCHECKSTYLE_PATH = '/home/build/nonconf/google3/tools/java/checkstyle/gcheckstyle.sh'

0 comments on commit ab5fb1d

Please sign in to comment.