|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +skip_tests.py - Check if only documentation files were changed in a git branch |
| 4 | +
|
| 5 | +Usage: |
| 6 | + ./skip_tests.py <git_root> <reference_branch> |
| 7 | +
|
| 8 | +Arguments: |
| 9 | + git_root: The root directory of the git project |
| 10 | + reference_branch: The branch to compare against |
| 11 | +
|
| 12 | +Returns: |
| 13 | + 0: Skip |
| 14 | + 1: NoSkip |
| 15 | + *: Error |
| 16 | +""" |
| 17 | + |
| 18 | +import sys |
| 19 | +import os |
| 20 | +import re |
| 21 | +import git |
| 22 | +import textwrap |
| 23 | +import argparse |
| 24 | + |
| 25 | +DOC_PATTERNS = [ |
| 26 | + r"^docs/", |
| 27 | + r"\.md$", |
| 28 | + r"\.txt$", |
| 29 | + r"LICENSE.*", |
| 30 | + r"CHANGELOG.*", |
| 31 | + r"CHANGES.*", |
| 32 | + r"CONTRIBUTING.*", |
| 33 | +] |
| 34 | + |
| 35 | +# Exit codes |
| 36 | +CODE_SKIP = 0 |
| 37 | +CODE_NO_SKIP = 1 |
| 38 | +CODE_ERROR = 2 |
| 39 | + |
| 40 | + |
| 41 | +def main() -> int: |
| 42 | + git_root, reference_branch = get_args() |
| 43 | + changed_files = get_changed_files(git_root, reference_branch) |
| 44 | + if not changed_files: |
| 45 | + return CODE_SKIP |
| 46 | + doc_files = [f for f in changed_files if is_doc_file(f)] |
| 47 | + not_doc_files = set(changed_files) - set(doc_files) |
| 48 | + print_changes(doc_files, not_doc_files) |
| 49 | + if not_doc_files: |
| 50 | + return CODE_NO_SKIP |
| 51 | + else: |
| 52 | + return CODE_SKIP |
| 53 | + |
| 54 | + |
| 55 | +# Utils |
| 56 | + |
| 57 | + |
| 58 | +def get_changed_files(git_root: str, reference_branch: str) -> list[str]: |
| 59 | + """Get list of files changed between current branch and reference branch.""" |
| 60 | + repo = git.Repo(git_root) |
| 61 | + diff_index = repo.git.diff("--name-only", reference_branch).strip() |
| 62 | + if not diff_index: |
| 63 | + return [] |
| 64 | + return [f.strip() for f in diff_index.split("\n") if f.strip()] |
| 65 | + |
| 66 | + |
| 67 | +def is_doc_file(file_path: str) -> bool: |
| 68 | + """Check if a file is a documentation file.""" |
| 69 | + for pattern in DOC_PATTERNS: |
| 70 | + if re.search(pattern, file_path): |
| 71 | + return True |
| 72 | + return False |
| 73 | + |
| 74 | + |
| 75 | +def print_changes(doc_files: list[str], not_doc_files: list[str]) -> None: |
| 76 | + display_doc = " \n".join(doc_files) |
| 77 | + print(f"doc_files({len(doc_files)})") |
| 78 | + if doc_files: |
| 79 | + display_doc = "\n".join(doc_files) |
| 80 | + print(textwrap.indent(display_doc, " ")) |
| 81 | + |
| 82 | + print(f"non_doc_files({len(not_doc_files)})") |
| 83 | + if not_doc_files: |
| 84 | + display_non_doc = " \n".join(not_doc_files) |
| 85 | + print(textwrap.indent(display_non_doc, " ")) |
| 86 | + |
| 87 | + |
| 88 | +def get_args() -> tuple[str, str]: |
| 89 | + """Parse command line arguments and validate them.""" |
| 90 | + parser = argparse.ArgumentParser(description="Check if CI can skip tests for a git branch") |
| 91 | + parser.add_argument("git_root", help="The root directory of the git project") |
| 92 | + parser.add_argument("reference_branch", help="The branch to compare against") |
| 93 | + args = parser.parse_args() |
| 94 | + git_root = os.path.abspath(args.git_root) |
| 95 | + ref_branch = args.reference_branch |
| 96 | + |
| 97 | + if not os.path.exists(git_root): |
| 98 | + raise ValueError(f"Git root directory does not exist: {git_root}") |
| 99 | + if not os.path.isdir(git_root): |
| 100 | + raise ValueError(f"Git root is not a directory: {git_root}") |
| 101 | + try: |
| 102 | + git.Repo(git_root) |
| 103 | + except git.InvalidGitRepositoryError: |
| 104 | + raise ValueError(f"Directory is not a git repository: {git_root}") |
| 105 | + return git_root, ref_branch |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + try: |
| 110 | + sys.exit(main()) |
| 111 | + except Exception as e: |
| 112 | + print(e) |
| 113 | + sys.exit(CODE_ERROR) |
0 commit comments