65 lines
3.1 KiB
Python
65 lines
3.1 KiB
Python
"""The templates a project's config names for its commit titles, pull request titles and branches.
|
|
|
|
A template is literal text around two placeholders: `{id}`, the ID of the chunk's first plan item, and `{summary}`, the one line that says what the commit or pull request does. Three readers, seated as the human who writes the config, picked that spelling from four candidates. A template has no escape for a literal brace: one is a fault, which keeps a misspelt placeholder such as `{ID}` out of every title."""
|
|
import re
|
|
|
|
ID = "{id}"
|
|
SUMMARY = "{summary}"
|
|
# What each setting holds where the config leaves it out: a bare summary, and the branch `CLAUDE.md` → "Git, commits, and PRs" names.
|
|
TITLE = SUMMARY
|
|
BRANCH = "item-{id}"
|
|
PLACEHOLDER = re.compile(r"(\{id\}|\{summary\})")
|
|
# The characters a branch template may hold around `{id}`: a subset of what `git check-ref-format` accepts, and nothing a shell or a forge's URL reads specially.
|
|
BRANCH_TEXT = re.compile(r"[A-Za-z0-9._/-]+")
|
|
|
|
|
|
def title_fault(template: object) -> str | None:
|
|
"""Why template is no title template; None where it is one."""
|
|
if not isinstance(template, str):
|
|
return f"not text such as \"PAY-{ID} - {SUMMARY}\""
|
|
if "\n" in template:
|
|
return "not one line"
|
|
if template.count(SUMMARY) != 1:
|
|
return f"without exactly one {SUMMARY}"
|
|
if template.count(ID) > 1:
|
|
return f"with more than one {ID}"
|
|
return stray_brace(template)
|
|
|
|
|
|
def branch_fault(template: object) -> str | None:
|
|
"""Why template is no branch template; None where it is one. A branch needs the ID, or two chunks would share one."""
|
|
if not isinstance(template, str):
|
|
return f"not text such as \"feature/PAY-{ID}\""
|
|
if template.count(ID) != 1 or SUMMARY in template:
|
|
return f"without exactly one {ID}, or with a {SUMMARY}"
|
|
branch = template.replace(ID, "1")
|
|
parts = branch.split("/")
|
|
if not BRANCH_TEXT.fullmatch(branch) or branch.startswith("-") or ".." in branch or any(not part or part.startswith(".") or part.endswith((".", ".lock")) for part in parts):
|
|
return f"not a branch name git accepts, of letters, digits and . _ / - around {ID}"
|
|
return None
|
|
|
|
|
|
def stray_brace(template: str) -> str | None:
|
|
stray = re.search(r"\{[^{}]*\}?|\}", PLACEHOLDER.sub("", template))
|
|
return f"with {stray[0]}, which is neither {ID} nor {SUMMARY}" if stray else None
|
|
|
|
|
|
def branch_of(template: str, id_: int) -> str:
|
|
return template.replace(ID, str(id_))
|
|
|
|
|
|
def pattern(template: str) -> re.Pattern[str]:
|
|
"""What a title made from template looks like: its literal text, digits for the ID, and a summary that opens and ends with something other than a space."""
|
|
shapes = {ID: "[0-9]+", SUMMARY: r"(?P<summary>\S(?:.*\S)?)"}
|
|
return re.compile("".join(shapes.get(part) or re.escape(part) for part in PLACEHOLDER.split(template)))
|
|
|
|
|
|
def fits(template: str, title: str) -> bool:
|
|
return pattern(template).fullmatch(title) is not None
|
|
|
|
|
|
def summary_of(template: str, title: str) -> str:
|
|
"""title's summary. A title that doesn't fit template is its own summary, so a commit from before the config named the template reads as it always did."""
|
|
found = pattern(template).fullmatch(title)
|
|
return found["summary"] if found else title
|