|
| 1 | +import argparse |
| 2 | +import logging |
| 3 | +import re |
| 4 | +import subprocess |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +log = logging.getLogger(__name__) |
| 8 | + |
| 9 | + |
| 10 | +def gitignore_to_regex(pattern) -> str: |
| 11 | + # Replace .gitignore-style patterns with regex equivalents |
| 12 | + pattern = pattern.replace("*", ".*") # * -> .* |
| 13 | + pattern = pattern.replace("?", ".") # ? -> . |
| 14 | + pattern = pattern.replace("[!", "[^") # [!abc] -> [^abc] |
| 15 | + |
| 16 | + # If the pattern ends with '/', it matches directories |
| 17 | + if pattern.endswith("/"): |
| 18 | + pattern = f"{pattern}.*" |
| 19 | + |
| 20 | + return rf"^{pattern}" |
| 21 | + |
| 22 | + |
| 23 | +def get_protected_files(file_name: str) -> list[str]: |
| 24 | + # Check to see if the .gitprotect file exists |
| 25 | + config_path = Path(file_name) |
| 26 | + if not config_path.exists(): |
| 27 | + log.error(f"ERROR: Could not find .gitprotect at {config_path.absolute()}") |
| 28 | + exit(1) |
| 29 | + |
| 30 | + # Open the file and read in file paths |
| 31 | + with open(file_name, "r") as file: |
| 32 | + return [gitignore_to_regex(line.strip()) for line in file] |
| 33 | + |
| 34 | + |
| 35 | +def get_changed_files(base_ref: str, head_ref: str) -> list[str]: |
| 36 | + result = subprocess.run( |
| 37 | + [ |
| 38 | + "git", |
| 39 | + "diff", |
| 40 | + "--name-only", |
| 41 | + base_ref, |
| 42 | + head_ref, |
| 43 | + ], |
| 44 | + capture_output=True, |
| 45 | + text=True, |
| 46 | + ) |
| 47 | + return result.stdout.splitlines() |
| 48 | + |
| 49 | + |
| 50 | +def check_changes_against_protect_list( |
| 51 | + changed_files: list[str], protected_files: list[str], comment_only: bool |
| 52 | +): |
| 53 | + violations = set() |
| 54 | + |
| 55 | + # If any modified file is one in the protect list, add the files to the violations list |
| 56 | + for protected_file in protected_files: |
| 57 | + pattern = re.compile(protected_file) |
| 58 | + files_with_pattern = [f for f in changed_files if pattern.search(f)] |
| 59 | + violations.update(files_with_pattern) |
| 60 | + |
| 61 | + violations_list = "\n".join(violations) |
| 62 | + if violations: |
| 63 | + log.error( |
| 64 | + f"The following files are protected and cannot be modified:\n{violations_list}" |
| 65 | + ) |
| 66 | + if comment_only: |
| 67 | + exit_code = 0 |
| 68 | + else: |
| 69 | + exit_code = 1 |
| 70 | + exit(exit_code) |
| 71 | + else: |
| 72 | + log.debug("No changes to protected files were detected.") |
| 73 | + |
| 74 | + |
| 75 | +def main(args): |
| 76 | + changed_files = get_changed_files( |
| 77 | + args.base_ref, |
| 78 | + args.head_ref, |
| 79 | + ) |
| 80 | + protected_files = get_protected_files(".gitprotect") |
| 81 | + check_changes_against_protect_list( |
| 82 | + protected_files=protected_files, |
| 83 | + changed_files=changed_files, |
| 84 | + comment_only=args.comment_only |
| 85 | + ) |
| 86 | + |
| 87 | + |
| 88 | +if __name__ == "__main__": |
| 89 | + parser = argparse.ArgumentParser( |
| 90 | + description="A utility function to check if protected files have been modified." |
| 91 | + ) |
| 92 | + parser.add_argument( |
| 93 | + "base_ref", help="The git SHA for the most recent merged commit." |
| 94 | + ) |
| 95 | + parser.add_argument("head_ref", help="The git SHA for the incoming commit") |
| 96 | + parser.add_argument( |
| 97 | + "--comment-only", |
| 98 | + action="store_true", |
| 99 | + help="Sets git-protect to not exit with an error code", |
| 100 | + ) |
| 101 | + |
| 102 | + args = parser.parse_args() |
| 103 | + main(args) |
0 commit comments