|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import re |
| 4 | +from typing import List, Tuple |
| 5 | + |
| 6 | +# Compile regexes once at module level for better performance |
| 7 | +TRAILER_RE = re.compile(r"^([A-Za-z0-9_-]+)(\s*:\s*)(.*)$") |
| 8 | +CONTINUATION_RE = re.compile(r"^\s+\S.*$") |
| 9 | + |
| 10 | +# Git-generated trailer prefixes |
| 11 | +GIT_GENERATED_PREFIXES = ["Signed-off-by: ", "(cherry picked from commit "] |
| 12 | + |
| 13 | + |
| 14 | +def parse_message(message: str) -> Tuple[str, str, str]: |
| 15 | + """ |
| 16 | + Parse a Git commit message into subject, body, and trailers. |
| 17 | +
|
| 18 | + According to the Git documentation, trailers are: |
| 19 | + - A group of one or more lines that is all trailers, or contains at least one |
| 20 | + Git-generated or user-configured trailer and consists of at least 25% trailers. |
| 21 | + - The group must be preceded by one or more empty (or whitespace-only) lines. |
| 22 | + - The group must either be at the end of the message or be the last non-whitespace |
| 23 | + lines before a line that starts with "---" (the "divider"). |
| 24 | +
|
| 25 | + Args: |
| 26 | + message: The commit message to parse. |
| 27 | +
|
| 28 | + Returns: |
| 29 | + A tuple containing: |
| 30 | + - subject: The first line of the message |
| 31 | + - body: The body of the message (may be empty) |
| 32 | + - trailers: The trailer block as a raw string (may be empty) |
| 33 | + """ |
| 34 | + if not message: |
| 35 | + return "", "", "" |
| 36 | + |
| 37 | + # Split into lines and get the subject (first line) |
| 38 | + lines = message.splitlines() |
| 39 | + subject = lines[0] if lines else "" |
| 40 | + |
| 41 | + if len(lines) <= 1: |
| 42 | + return subject, "", "" |
| 43 | + |
| 44 | + # Remove subject |
| 45 | + message_lines = lines[1:] |
| 46 | + |
| 47 | + if not message_lines: |
| 48 | + return subject, "", "" |
| 49 | + |
| 50 | + # Find where the trailer block starts |
| 51 | + trailer_start = find_trailer_block_start(message_lines) |
| 52 | + |
| 53 | + if trailer_start == -1: |
| 54 | + # No trailer block found, everything after subject is body |
| 55 | + body = "\n".join(message_lines).strip() |
| 56 | + return subject, body, "" |
| 57 | + |
| 58 | + # Body is everything between subject and trailers (with empty lines trimmed) |
| 59 | + body = "\n".join(message_lines[:trailer_start]).strip() |
| 60 | + |
| 61 | + # Keep trailers as a raw string |
| 62 | + trailers = "\n".join(message_lines[trailer_start:]).strip() |
| 63 | + |
| 64 | + return subject, body, trailers |
| 65 | + |
| 66 | + |
| 67 | +def find_trailer_block_start(lines: List[str]) -> int: |
| 68 | + """ |
| 69 | + Find the start index of the trailer block in a list of lines. |
| 70 | +
|
| 71 | + Args: |
| 72 | + lines: List of message lines (without subject and divider). |
| 73 | +
|
| 74 | + Returns: |
| 75 | + Index of the first line of the trailer block, or -1 if no trailer block is found. |
| 76 | + """ |
| 77 | + # Remove trailing empty lines |
| 78 | + trimmed_lines = list(reversed([line for line in reversed(lines) if line.strip()])) |
| 79 | + |
| 80 | + if not trimmed_lines: |
| 81 | + return -1 |
| 82 | + |
| 83 | + # Find the last non-empty block |
| 84 | + block_indices = [-1] + [i for i, line in enumerate(lines) if not line.strip()] |
| 85 | + |
| 86 | + # Try blocks from last to first |
| 87 | + for i in range(len(block_indices) - 1, -1, -1): |
| 88 | + start_idx = block_indices[i] + 1 |
| 89 | + # If we're at the beginning or checking the whole message |
| 90 | + if i == 0 or start_idx == 0: |
| 91 | + # Check if the whole remaining content is a trailer block |
| 92 | + if is_trailer_block(lines[start_idx:]): |
| 93 | + return start_idx |
| 94 | + # No more blocks to check |
| 95 | + return -1 |
| 96 | + |
| 97 | + # Check if the block after this blank line is a trailer block |
| 98 | + end_idx = block_indices[i + 1] if i + 1 < len(block_indices) else len(lines) |
| 99 | + if is_trailer_block(lines[start_idx:end_idx]): |
| 100 | + return start_idx |
| 101 | + |
| 102 | + return -1 |
| 103 | + |
| 104 | + |
| 105 | +def is_trailer_block(lines: List[str]) -> bool: |
| 106 | + """ |
| 107 | + Determine if the given lines form a trailer block. |
| 108 | +
|
| 109 | + A block is a trailer block if: |
| 110 | + 1. All lines are trailers, or |
| 111 | + 2. At least one Git-generated trailer exists and at least 25% of lines are trailers |
| 112 | +
|
| 113 | + Args: |
| 114 | + lines: List of lines to check. |
| 115 | +
|
| 116 | + Returns: |
| 117 | + True if the lines form a trailer block, False otherwise. |
| 118 | + """ |
| 119 | + # Filter out empty lines |
| 120 | + content_lines = [line for line in lines if line.strip()] |
| 121 | + |
| 122 | + if not content_lines: |
| 123 | + return False |
| 124 | + |
| 125 | + trailer_lines = 0 |
| 126 | + non_trailer_lines = 0 |
| 127 | + has_git_generated_trailer = False |
| 128 | + |
| 129 | + i = 0 |
| 130 | + while i < len(content_lines): |
| 131 | + line = content_lines[i] |
| 132 | + |
| 133 | + # Skip continuation lines (they belong to the previous trailer) |
| 134 | + if CONTINUATION_RE.match(line): |
| 135 | + i += 1 |
| 136 | + continue |
| 137 | + |
| 138 | + # Check if it's a git-generated trailer |
| 139 | + if any(line.startswith(prefix) for prefix in GIT_GENERATED_PREFIXES): |
| 140 | + has_git_generated_trailer = True |
| 141 | + trailer_lines += 1 |
| 142 | + elif TRAILER_RE.match(line): |
| 143 | + # Regular trailer |
| 144 | + trailer_lines += 1 |
| 145 | + else: |
| 146 | + # Not a trailer line |
| 147 | + non_trailer_lines += 1 |
| 148 | + |
| 149 | + i += 1 |
| 150 | + |
| 151 | + # A block is a trailer block if all lines are trailers OR |
| 152 | + # it has at least one git-generated trailer and >= 25% of lines are trailers |
| 153 | + return (trailer_lines > 0 and non_trailer_lines == 0) or ( |
| 154 | + has_git_generated_trailer and trailer_lines * 3 >= non_trailer_lines |
| 155 | + ) |
| 156 | + |
| 157 | + |
| 158 | +def interpret_trailers(message: str, trailers_to_add: List[str]) -> str: |
| 159 | + """ |
| 160 | + Add trailers to a commit message, mimicking git interpret-trailers. |
| 161 | +
|
| 162 | + Args: |
| 163 | + message: The commit message to add trailers to |
| 164 | + trailers_to_add: List of trailers to add in the format "Key: Value" |
| 165 | +
|
| 166 | + Returns: |
| 167 | + The commit message with trailers added |
| 168 | + """ |
| 169 | + subject, body, existing_trailers = parse_message(message) |
| 170 | + |
| 171 | + # Create a new list with all trailers (existing + new) |
| 172 | + all_trailers = [] |
| 173 | + if existing_trailers: |
| 174 | + all_trailers.append(existing_trailers) |
| 175 | + |
| 176 | + all_trailers.extend(trailers_to_add) |
| 177 | + |
| 178 | + # Build the new message |
| 179 | + new_message = subject |
| 180 | + |
| 181 | + if body: |
| 182 | + new_message += "\n\n" + body |
| 183 | + |
| 184 | + if all_trailers: |
| 185 | + if body or (not body and existing_trailers): |
| 186 | + new_message += "\n" |
| 187 | + if not existing_trailers: |
| 188 | + new_message += "\n" |
| 189 | + new_message += "\n" + "\n".join(all_trailers) |
| 190 | + |
| 191 | + return new_message |
0 commit comments