Answer stability round 2: keep the line break that ends a heredoc, read a run of separators as one, check every untagged compose image, and reject a non-object tool input
This commit is contained in:
+14
-19
@@ -8,11 +8,11 @@ RULES = {
|
||||
"action-pin": "a GitHub or Gitea Action is pinned to a semver tag, such as @v3 or @v3.1.2, never a commit SHA or a branch; SHA-pin only in a pipeline that tests our own specific builds",
|
||||
"commit-one-line": "a commit message is one line, prefixed with the ticket ID when the work has one",
|
||||
"ignorefile-sorted": "each block of an ignore file is sorted, since its order carries no meaning; where order matters, such as a re-include after its exclude, split the block with a blank line or a comment",
|
||||
"image-pin": "a container image is pinned to its full patch version, such as node:24.18.1-bookworm-slim, so the same commit builds the same image on every day; a bot such as Renovate raises the bumps as reviewable PRs",
|
||||
"image-pin": "a container image is pinned to its full patch version, such as node:24.18.1-bookworm-slim, or to a date-stamped tag or an @sha256: digest, so the same commit builds the same image on every day; a bot such as Renovate raises the bumps as reviewable PRs. An image that publishes no three-part tag, such as postgres:16.4, takes an override",
|
||||
"no-ai-attribution": "no commit, PR, issue comment or file carries a Claude or Anthropic attribution, such as a co-author trailer or a generated-with line",
|
||||
"todo-rank-check": "a committed todo.md passes ~/.claude/bin/rank_todo.py --check",
|
||||
}
|
||||
OVERRIDE = re.compile(r"#\s*rule-override ([a-z-]+):[ \t]*(\S[^\n]*)")
|
||||
OVERRIDE = re.compile(r"(?:#|<!--)\s*rule-override ([a-z-]+):([^\n]*)")
|
||||
# [-] keeps this file from matching itself.
|
||||
ATTRIBUTION = re.compile(r"co-authored[-]by:[^\n]*\b(?:claude|anthropic)\b|generated with \[?claude code", re.IGNORECASE)
|
||||
SEMVER = re.compile(r"v?\d+(?:\.\d+){0,2}")
|
||||
@@ -21,11 +21,10 @@ PATCH = re.compile(r"v?\d+\.\d+\.\d+(?:[-+_.].*)?|.*\d{8}.*")
|
||||
FROM = re.compile(r"^[ \t]*FROM[ \t]+(?:--\S+[ \t]+)*(\S+)(?:[ \t]+AS[ \t]+(\S+))?", re.IGNORECASE | re.MULTILINE)
|
||||
COPIED = re.compile(r"^[ \t]*COPY[ \t].*?--from=(\S+)", re.IGNORECASE | re.MULTILINE)
|
||||
IMAGE = re.compile(r"^[ \t]*(?:-[ \t]+)?(?:image|container):[ \t]*[\"']?([^\s\"'#{]+)", re.MULTILINE)
|
||||
BUILD = re.compile(r"^[ \t]*build:", re.MULTILINE)
|
||||
HEREDOC = re.compile(r"<<-?[ \t]*([\"']?)(\w+)\1[^\n]*\n.*?^[ \t]*\2[ \t]*$", re.DOTALL | re.MULTILINE)
|
||||
HEREDOC = re.compile(r"<<-?[ \t]*([\"']?)([A-Za-z_]\w*)\1[^\n]*\n.*?^[ \t]*\2[ \t]*$", re.DOTALL | re.MULTILINE)
|
||||
USES = re.compile(r"^[ \t]*(?:-[ \t]+)?uses:[ \t]*[\"']?([^\s\"'#]+)", re.MULTILINE)
|
||||
ASSIGNMENT = re.compile(r"[A-Za-z_][A-Za-z0-9_]*=")
|
||||
SEPARATORS = {"\n", "&&", "||", ";", "|", "&", "(", ")", ";;", "|&"}
|
||||
SEPARATING = set("\n();&|")
|
||||
# git's global options that take a value as the next word.
|
||||
GIT_VALUED = {"-c", "--exec-path", "--git-dir", "--namespace", "--work-tree"}
|
||||
|
||||
@@ -38,20 +37,20 @@ class Breach:
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Commit:
|
||||
"""A `git commit` in a command: the directory it runs in, its messages and whether it stages tracked changes."""
|
||||
"""A `git commit` in a command: the directory it runs in, and its messages."""
|
||||
directory: str
|
||||
messages: list[str]
|
||||
from_file: bool
|
||||
all: bool
|
||||
|
||||
|
||||
def overrides(text: str) -> dict[str, str]:
|
||||
return {rule: reason.strip() for rule, reason in OVERRIDE.findall(text) if rule in RULES}
|
||||
given = {rule: reason.strip().removesuffix("-->").strip() for rule, reason in OVERRIDE.findall(text)}
|
||||
return {rule: reason for rule, reason in given.items() if rule in RULES and reason}
|
||||
|
||||
|
||||
def segments(command: str) -> list[list[str]] | None:
|
||||
"""The command's simple commands as words, each heredoc's body cut to a line break, or None when it does not parse."""
|
||||
lexer = shlex.shlex(HEREDOC.sub(lambda heredoc: heredoc.group(0).split("\n", 1)[0] + "\n", command), posix=True, punctuation_chars="();<>|&\n")
|
||||
lexer = shlex.shlex(HEREDOC.sub(lambda heredoc: heredoc.group(0).split("\n", 1)[0], command), posix=True, punctuation_chars="();<>|&\n")
|
||||
lexer.whitespace = " \t\r"
|
||||
lexer.whitespace_split = True
|
||||
# shlex also ends a word at a # inside it, as in fix#2, which bash does not; after lexing, a quoted # cannot be told apart.
|
||||
@@ -61,7 +60,8 @@ def segments(command: str) -> list[list[str]] | None:
|
||||
return None
|
||||
split: list[list[str]] = [[]]
|
||||
for word in words:
|
||||
if word in SEPARATORS:
|
||||
# Unquoted, a run such as "&&\n" lexes as one word.
|
||||
if set(word) <= SEPARATING:
|
||||
split.append([])
|
||||
else:
|
||||
split[-1].append(word)
|
||||
@@ -91,7 +91,7 @@ def git_commit(args: list[str], directory: str) -> Commit | None:
|
||||
if args[at:at + 1] != ["commit"]:
|
||||
return None
|
||||
messages: list[str] = []
|
||||
from_file = staged_all = False
|
||||
from_file = False
|
||||
rest = args[at + 1:]
|
||||
at = 0
|
||||
while at < len(rest):
|
||||
@@ -107,8 +107,6 @@ def git_commit(args: list[str], directory: str) -> Commit | None:
|
||||
messages.append(word.removeprefix("--message="))
|
||||
elif word.startswith("--file="):
|
||||
from_file = True
|
||||
elif word == "--all":
|
||||
staged_all = True
|
||||
elif word.startswith("-") and not word.startswith("--"):
|
||||
# A cluster such as -am: a valued flag takes the rest of the word, or else the next word.
|
||||
for place, flag in enumerate(word[1:], 1):
|
||||
@@ -120,9 +118,8 @@ def git_commit(args: list[str], directory: str) -> Commit | None:
|
||||
messages += [value] if flag == "m" else []
|
||||
from_file = from_file or flag == "F"
|
||||
break
|
||||
staged_all = staged_all or flag == "a"
|
||||
at += 1
|
||||
return Commit(directory, messages, from_file, staged_all)
|
||||
return Commit(directory, messages, from_file)
|
||||
|
||||
|
||||
def command_breaches(command: str, cwd: str) -> list[Breach]:
|
||||
@@ -151,14 +148,12 @@ def file_breaches(path: str, before: str, added: str, after: str) -> list[Breach
|
||||
breaches += [Breach("image-pin", f"{path}: {image}") for image in images if image.lower() not in stages | {"scratch"} and not image.isdigit() and not pinned(image)]
|
||||
workflow = any(folder in "/" + path for folder in ("/.github/workflows/", "/.gitea/workflows/")) or name in ("action.yml", "action.yaml")
|
||||
if "compose" in name and name.endswith((".yml", ".yaml")) or workflow:
|
||||
# A compose service that builds names its own image with image:, untagged.
|
||||
built = "compose" in name and BUILD.search(after) is not None
|
||||
breaches += [Breach("image-pin", f"{path}: {image}") for image in IMAGE.findall(added) if not pinned(image) and not (built and ":" not in image)]
|
||||
breaches += [Breach("image-pin", f"{path}: {image}") for image in IMAGE.findall(added) if not pinned(image)]
|
||||
if workflow:
|
||||
breaches += [Breach("action-pin", f"{path}: uses: {action}") for action in USES.findall(added) if not tagged(action)]
|
||||
if name.startswith(".") and name.endswith("ignore"):
|
||||
already = [block for block in blocks(before) if block != sorted(block)]
|
||||
breaches += [Breach("ignorefile-sorted", f"{path}: {block[0]!r} through {block[-1]!r} are out of order") for block in blocks(after) if block != sorted(block) and block not in already]
|
||||
breaches += [Breach("ignorefile-sorted", f"{path}: sort the block {block[0]!r} through {block[-1]!r} in code-point order, as LC_ALL=C sort does: {', '.join(sorted(block))}") for block in blocks(after) if block != sorted(block) and block not in already]
|
||||
return breaches
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user