Skip to content

Commit

Permalink
Merge pull request #376 from rest-for-physics/lobis-pre-commit-script
Browse files Browse the repository at this point in the history
Update pre-commit check script to work for auto-updates
  • Loading branch information
lobis authored Feb 14, 2023
2 parents f958732 + 0082ebc commit fe59044
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions scripts/validatePreCommitConfig.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
# This script should be run from the root of a submodule repository.
# It will check that the pre-commit config file matches the one in the framework repository (master branch).
# TODO: if a branch with the same name as the current branch exists in the framework repository, use that instead of master.
# It will also check other files related to the pre-commit such as .clang-format and .cmake-format.yaml.

import os


def check_files(base_directory, file_name) -> bool:
def check_files(base_directory, file_name, branch) -> bool:
# Check that the pre-commit config file is the same as the one in the framework repository.
# If not, print a warning and exit with a non-zero exit code.
# base_directory: the directory where the file is located.
# file_names: a list of file names to check.
# Returns: True if the files are the same, False otherwise.
# Note: this function will exit the script if the files are not the same.

branch_name = "master"
github_prefix = (
f"https://raw.githubusercontent.com/rest-for-physics/framework/{branch_name}/"
f"https://raw.githubusercontent.com/rest-for-physics/framework/{branch}/"
)

with open(os.path.join(base_directory, file_name), "r") as f:
Expand All @@ -36,9 +34,35 @@ def check_files(base_directory, file_name) -> bool:
# check pre-commit config file
files = [".pre-commit-config.yaml", ".clang-format", ".cmake-format.yaml"]

# if the cwd is a git repository, get the branch name
branch_name = "master"
if os.path.exists(".git"):
this_branch_name = os.popen("git rev-parse --abbrev-ref HEAD").read().strip()
# Check this branch name also exists in framework
if (
len(
os.popen(
f"git ls-remote --heads https://github.com/rest-for-physics/framework.git {this_branch_name}"
)
.read()
.strip()
)
!= 0
):
branch_name = this_branch_name
else:
print(
f"WARNING: The branch '{this_branch_name}' does not exist in the framework repository."
)

print(f"Comparing files with branch={branch_name} in the framework repository.")

status = True # OK
for file in files:
status = check_files(base_directory=".", file_name=file) and status
status = (
check_files(base_directory=".", file_name=file, branch=branch_name)
and status
)

if not status:
exit(1) # ERROR
Expand Down

0 comments on commit fe59044

Please sign in to comment.