|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Part of the EDG Compiler Project, under the Apache License v2.0 with LLVM |
| 4 | +# Exceptions. |
| 5 | +# See https://edgcpp.org/LICENSE.txt for license information. |
| 6 | +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | + |
| 8 | +'''Check a range of commits against the commit policy: the subject (see |
| 9 | +edgchanges.check_commit_subject), and no merge commits. |
| 10 | +
|
| 11 | +The arguments are handed to git log, so any revision range it accepts works: |
| 12 | +
|
| 13 | + edg-check-commit-subjects origin/main..HEAD |
| 14 | + edg-check-commit-subjects main HEAD ^origin/main |
| 15 | +
|
| 16 | +A merge commit is a violation by itself; --allow-merges (for forks that use |
| 17 | +merge commits) skips merge commits instead, subject and all. |
| 18 | +
|
| 19 | +Only commits with a violation are reported. The exit status is 1 if any |
| 20 | +commit was reported, and 0 otherwise. |
| 21 | +''' |
| 22 | + |
| 23 | +import argparse |
| 24 | +import shutil |
| 25 | +import subprocess |
| 26 | +import sys |
| 27 | + |
| 28 | +from typing import List, NamedTuple, Optional |
| 29 | + |
| 30 | +import edgchanges |
| 31 | +import edgutil |
| 32 | + |
| 33 | +TOOL_PATH_GIT = shutil.which('git') |
| 34 | + |
| 35 | +Commit = NamedTuple( |
| 36 | + 'Commit', |
| 37 | + [('hash', str), ('is_merge', bool), ('message', str)] |
| 38 | +) |
| 39 | + |
| 40 | +def _list_commits(log_args: List[str]) -> List[Commit]: |
| 41 | + '''Return the commits git rev-list selects. |
| 42 | +
|
| 43 | + Commit messages are arbitrary bytes, so nothing here depends on their |
| 44 | + content: rev-list (hashes only) feeds cat-file --batch, which gives the size |
| 45 | + of each object it prints.''' |
| 46 | + assert TOOL_PATH_GIT is not None |
| 47 | + rev_list_proc = subprocess.Popen( |
| 48 | + [TOOL_PATH_GIT, 'rev-list', *log_args], |
| 49 | + stdout = subprocess.PIPE, |
| 50 | + stderr = subprocess.PIPE |
| 51 | + ) |
| 52 | + cat_file_proc = subprocess.Popen( |
| 53 | + [TOOL_PATH_GIT, 'cat-file', '--batch'], |
| 54 | + stdin = rev_list_proc.stdout, |
| 55 | + stdout = subprocess.PIPE, |
| 56 | + stderr = subprocess.PIPE |
| 57 | + ) |
| 58 | + # Leave the pipe to cat-file, so rev-list sees SIGPIPE if it exits early. |
| 59 | + assert rev_list_proc.stdout is not None |
| 60 | + rev_list_proc.stdout.close() |
| 61 | + data, cat_file_stderr = cat_file_proc.communicate() |
| 62 | + assert rev_list_proc.stderr is not None |
| 63 | + rev_list_stderr = rev_list_proc.stderr.read() |
| 64 | + rev_list_proc.stderr.close() |
| 65 | + |
| 66 | + for returncode, stderr in ( |
| 67 | + (rev_list_proc.wait(), rev_list_stderr), |
| 68 | + (cat_file_proc.returncode, cat_file_stderr) |
| 69 | + ): |
| 70 | + if returncode != 0: |
| 71 | + raise RuntimeError(stderr.decode('utf-8', errors = 'replace').strip()) |
| 72 | + |
| 73 | + commits = [] |
| 74 | + position = 0 |
| 75 | + while position < len(data): |
| 76 | + line_end = data.index(b'\n', position) |
| 77 | + header = data[position:line_end].split() |
| 78 | + if len(header) != 3 or header[1] != b'commit': |
| 79 | + raise RuntimeError(f"unexpected object: {data[position:line_end]!r}") |
| 80 | + content_start = line_end + 1 |
| 81 | + content_end = content_start + int(header[2]) |
| 82 | + # The message follows the first blank line, after the headers. |
| 83 | + headers, _, message = ( |
| 84 | + data[content_start:content_end].partition(b'\n\n') |
| 85 | + ) |
| 86 | + parent_count = sum( |
| 87 | + 1 for line in headers.split(b'\n') if line.startswith(b'parent ') |
| 88 | + ) |
| 89 | + commits.append( |
| 90 | + Commit( |
| 91 | + header[0].decode(), |
| 92 | + parent_count > 1, |
| 93 | + message.decode('utf-8', errors = 'replace') |
| 94 | + ) |
| 95 | + ) |
| 96 | + position = content_end + 1 |
| 97 | + return commits |
| 98 | + |
| 99 | +def check_commits(log_args: List[str], *, allow_merges: bool) -> int: |
| 100 | + '''Report each commit selected by log_args that violates the policy; return |
| 101 | + the number of such commits.''' |
| 102 | + violations = 0 |
| 103 | + for commit in _list_commits(log_args): |
| 104 | + if commit.is_merge and allow_merges: |
| 105 | + continue |
| 106 | + |
| 107 | + problem: Optional[str] |
| 108 | + if commit.is_merge: |
| 109 | + problem = 'merge commits are not allowed' |
| 110 | + else: |
| 111 | + problem = edgchanges.check_commit_subject(commit.message) |
| 112 | + if problem is not None: |
| 113 | + print(f"{commit.hash[:12]} {commit.message.partition(chr(10))[0]}") |
| 114 | + print(f" {problem}") |
| 115 | + violations += 1 |
| 116 | + return violations |
| 117 | + |
| 118 | +def _parse_args() -> argparse.Namespace: |
| 119 | + parser = argparse.ArgumentParser( |
| 120 | + description = ( |
| 121 | + 'Report the commits in a range that violate the commit policy ' |
| 122 | + '(subject format, no merge commits).' |
| 123 | + ) |
| 124 | + ) |
| 125 | + parser.add_argument( |
| 126 | + '--allow-merges', |
| 127 | + action = 'store_true', |
| 128 | + help = 'permit merge commits (and skip them) instead of reporting them' |
| 129 | + ) |
| 130 | + parser.add_argument( |
| 131 | + 'log_args', |
| 132 | + nargs = '+', |
| 133 | + metavar = 'GIT_LOG_ARG', |
| 134 | + help = 'arguments for git log, e.g. a range such as origin/main..HEAD' |
| 135 | + ) |
| 136 | + return parser.parse_args() |
| 137 | + |
| 138 | +def main() -> int: |
| 139 | + args = _parse_args() |
| 140 | + if TOOL_PATH_GIT is None: |
| 141 | + edgutil.eprint('error: git not found') |
| 142 | + return 1 |
| 143 | + |
| 144 | + try: |
| 145 | + violations = check_commits( |
| 146 | + args.log_args, |
| 147 | + allow_merges = args.allow_merges |
| 148 | + ) |
| 149 | + except RuntimeError as ex: |
| 150 | + edgutil.eprint(f"error: {ex}") |
| 151 | + return 1 |
| 152 | + return 1 if violations != 0 else 0 |
| 153 | + |
| 154 | +if __name__ == '__main__': |
| 155 | + sys.exit(main()) |
0 commit comments