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

Update pre-commit check script to work for auto-updates #376

Merged
merged 1 commit into from
Feb 14, 2023
Merged
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
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