Chaque commit d'une demande arrive tel quel sur main (commit de fusion) : titre conventionnel, Signed-off-by (DCO) de l'auteur, un outil jamais co-auteur. scripts/verifier_commits.py sert le hook commit-msg (make git-hooks), make commits-verifier et la CI des demandes. Assisted-by: Claude Code Signed-off-by: Daniel Allaire <allaire.dan@chezlepro.ca>
102 lines
4.5 KiB
Python
Executable file
102 lines
4.5 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
"""Un message de commit respecte-t-il la convention du depot ? (CONTRIBUTING.md)
|
|
|
|
POURQUOI (2026-11-23). Les demandes de fusion entrent par un commit de fusion : chaque commit
|
|
d'une branche arrive TEL QUEL sur `main`. L'historique doit dire sans ambiguite qui a ecrit quoi
|
|
— le mainteneur, un contributeur, l'assistance d'un outil. Ce que la convention exige :
|
|
|
|
- un titre conventionnel : `type(portee): description` (feat, fix, docs, test, refactor, ci,
|
|
chore, perf, build, style, revert) ;
|
|
- un `Signed-off-by:` (DCO), celui de l'auteur du commit quand on verifie une plage ;
|
|
- un outil se declare par `Assisted-by: <outil>` et n'est jamais co-auteur : un
|
|
`Co-authored-by:` qui nomme un outil ou un robot est refuse. Celui d'une personne reste permis.
|
|
|
|
Usage :
|
|
python3 scripts/verifier_commits.py --message <fichier> # hook commit-msg (make git-hooks)
|
|
python3 scripts/verifier_commits.py <base>..<tete> # une plage (CI d'une demande de fusion)
|
|
|
|
Les commits de fusion ne sont pas verifies : Forgejo les ecrit. `fixup!` et `squash!` passent au
|
|
hook (ils disparaissent au rebase) mais pas dans une plage : une branche se nettoie avant la revue.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
TYPES = ("feat", "fix", "docs", "test", "refactor", "ci", "chore", "perf", "build", "style", "revert")
|
|
TITRE = re.compile(rf"^({'|'.join(TYPES)})(\([^()\s][^()]*\))?!?: \S")
|
|
TRAVAIL = re.compile(r"^(fixup|squash|amend)! ")
|
|
SIGNE = re.compile(r"(?m)^Signed-off-by: (.+) <([^<>@\s]+@[^<>\s]+)>\s*$")
|
|
ASSISTE = re.compile(r"(?mi)^assisted-by:(.*)$")
|
|
OUTIL = re.compile(r"claude|anthropic|codex|openai|copilot|chatgpt|gpt-|gemini|cursor|\[bot\]|-bot\b|bot@", re.I)
|
|
COAUTEUR = re.compile(r"(?mi)^co-authored-by:(.*)$")
|
|
|
|
|
|
def defauts(message: str, auteur_courriel: str = "", travail_permis: bool = False) -> list[str]:
|
|
lignes = [l for l in message.splitlines() if not l.startswith("#")]
|
|
texte = "\n".join(lignes).strip()
|
|
if not texte:
|
|
return ["message vide"]
|
|
titre = texte.splitlines()[0]
|
|
out = []
|
|
if not (TITRE.match(titre) or (travail_permis and TRAVAIL.match(titre))):
|
|
out.append(f"titre non conventionnel : « {titre[:70]} » — attendu `type(portee): description`, "
|
|
f"type parmi {', '.join(TYPES)}")
|
|
signes = SIGNE.findall(texte)
|
|
if not signes:
|
|
out.append("aucun `Signed-off-by: Nom <courriel>` (DCO) — `git commit -s`")
|
|
elif auteur_courriel and auteur_courriel.lower() not in {c.lower() for _n, c in signes}:
|
|
out.append(f"aucun `Signed-off-by` de l'auteur du commit ({auteur_courriel})")
|
|
for valeur in COAUTEUR.findall(texte):
|
|
if OUTIL.search(valeur):
|
|
out.append(f"un outil n'est jamais co-auteur : « Co-authored-by:{valeur.strip()[:50]} » — "
|
|
"le declarer par `Assisted-by: <outil>`")
|
|
for valeur in ASSISTE.findall(texte):
|
|
if not valeur.strip():
|
|
out.append("`Assisted-by:` sans nom d'outil")
|
|
return out
|
|
|
|
|
|
def plage(intervalle: str) -> int:
|
|
r = subprocess.run(["git", "log", "--no-merges", "--format=%H%x00%ae%x00%B%x1e", intervalle],
|
|
capture_output=True, text=True)
|
|
if r.returncode != 0:
|
|
print(f"plage illisible : {intervalle} — {r.stderr.strip()[:200]}")
|
|
return 2
|
|
echecs = 0
|
|
commits = [c for c in r.stdout.split("\x1e") if c.strip()]
|
|
for c in commits:
|
|
sha, courriel, message = c.strip("\n").split("\x00", 2)
|
|
d = defauts(message, courriel)
|
|
titre = message.strip().splitlines()[0][:70]
|
|
if d:
|
|
echecs += 1
|
|
print(f"ECHEC {sha[:7]} {titre}")
|
|
for x in d:
|
|
print(f" - {x}")
|
|
else:
|
|
print(f"OK {sha[:7]} {titre}")
|
|
print(f"{len(commits) - echecs}/{len(commits)} commit(s) conformes a la convention (CONTRIBUTING.md).")
|
|
return 1 if echecs else 0
|
|
|
|
|
|
def main() -> int:
|
|
args = sys.argv[1:]
|
|
if len(args) == 2 and args[0] == "--message":
|
|
d = defauts(Path(args[1]).read_text(encoding="utf-8"), travail_permis=True)
|
|
if d:
|
|
print("Commit refuse — convention du depot (CONTRIBUTING.md) :", file=sys.stderr)
|
|
for x in d:
|
|
print(f" - {x}", file=sys.stderr)
|
|
return 1
|
|
return 0
|
|
if len(args) == 1 and ".." in args[0]:
|
|
return plage(args[0])
|
|
print(__doc__.split("Usage :")[1].split("Les commits")[0].rstrip(), file=sys.stderr)
|
|
return 2
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|