Answer stability round 1: sort imports, share rank_todo's check, scope YAML to compose and workflows, judge only newly unsorted ignore blocks, cut heredoc bodies, check COPY --from and container:, log what each override let through, keep CLAUDE.md lines for what the hook cannot see
This commit is contained in:
+20
-10
@@ -10,15 +10,19 @@ RULES = {
|
||||
"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",
|
||||
"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; run rank_todo.py on it",
|
||||
"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]*)")
|
||||
# [-] 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}")
|
||||
PATCH = re.compile(r"v?\d+\.\d+\.\d+(?:[-+_.].*)?")
|
||||
# Two-part tags such as postgres:16.4 stay blocked: the rule bans node:24.18, which reads the same, so such an image takes an override.
|
||||
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)
|
||||
IMAGE = re.compile(r"^[ \t]*(?:-[ \t]+)?image:[ \t]*[\"']?([^\s\"'#]+)", 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)
|
||||
USES = re.compile(r"^[ \t]*(?:-[ \t]+)?uses:[ \t]*[\"']?([^\s\"'#]+)", re.MULTILINE)
|
||||
ASSIGNMENT = re.compile(r"[A-Za-z_][A-Za-z0-9_]*=")
|
||||
SEPARATORS = {"\n", "&&", "||", ";", "|", "&", "(", ")", ";;", "|&"}
|
||||
@@ -46,10 +50,11 @@ def overrides(text: str) -> dict[str, str]:
|
||||
|
||||
|
||||
def segments(command: str) -> list[list[str]] | None:
|
||||
"""The command's simple commands as words, or None when it does not parse."""
|
||||
lexer = shlex.shlex(command, posix=True, punctuation_chars="();<>|&\n")
|
||||
"""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.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.
|
||||
try:
|
||||
words = list(lexer)
|
||||
except ValueError:
|
||||
@@ -142,13 +147,18 @@ def file_breaches(path: str, before: str, added: str, after: str) -> list[Breach
|
||||
breaches.append(Breach("no-ai-attribution", f"{path} would carry an attribution"))
|
||||
if name.startswith(("Dockerfile", "Containerfile")) or name.endswith((".dockerfile", ".Dockerfile", ".containerfile")):
|
||||
stages = {stage.lower() for _, stage in FROM.findall(before + "\n" + after) if stage}
|
||||
images = [image for image, _ in FROM.findall(added) if image.lower() not in stages | {"scratch"}]
|
||||
breaches += [Breach("image-pin", f"{path}: FROM {image}") for image in images if not pinned(image)]
|
||||
if name.endswith((".yml", ".yaml")):
|
||||
breaches += [Breach("image-pin", f"{path}: image: {image}") for image in IMAGE.findall(added) if not pinned(image)]
|
||||
images = [image for image, _ in FROM.findall(added)] + COPIED.findall(added)
|
||||
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)]
|
||||
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"):
|
||||
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)]
|
||||
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]
|
||||
return breaches
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user