conventional-git 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- conventional_git/__init__.py +0 -0
- conventional_git/adapters/__init__.py +0 -0
- conventional_git/adapters/commitizen_config.py +49 -0
- conventional_git/adapters/gitlint_rules.py +93 -0
- conventional_git/branch/__init__.py +7 -0
- conventional_git/branch/grammar.py +21 -0
- conventional_git/branch/rules.py +119 -0
- conventional_git/branch/vocabulary.py +89 -0
- conventional_git/cli/__init__.py +0 -0
- conventional_git/cli/app.py +41 -0
- conventional_git/cli/auth.py +107 -0
- conventional_git/cli/capabilities.py +111 -0
- conventional_git/cli/check.py +132 -0
- conventional_git/cli/create.py +172 -0
- conventional_git/cli/hook.py +132 -0
- conventional_git/cli/mcp.py +13 -0
- conventional_git/commit/__init__.py +8 -0
- conventional_git/commit/compose.py +39 -0
- conventional_git/commit/grammar.py +41 -0
- conventional_git/commit/rules.py +237 -0
- conventional_git/commit/vocabulary.py +94 -0
- conventional_git/config.py +51 -0
- conventional_git/csv_columns.py +28 -0
- conventional_git/data/__init__.py +0 -0
- conventional_git/data/branch-trunks.csv +4 -0
- conventional_git/data/branch-types.csv +8 -0
- conventional_git/data/commit-attribution.csv +4 -0
- conventional_git/data/commit-types.csv +12 -0
- conventional_git/generation/__init__.py +27 -0
- conventional_git/generation/credentials.py +112 -0
- conventional_git/generation/diff.py +131 -0
- conventional_git/generation/heuristic.py +119 -0
- conventional_git/generation/protocol.py +94 -0
- conventional_git/generation/typesafe.py +140 -0
- conventional_git/helpers.py +39 -0
- conventional_git/mcp/__init__.py +0 -0
- conventional_git/mcp/server.py +75 -0
- conventional_git/py.typed +0 -0
- conventional_git/violations.py +75 -0
- conventional_git-1.0.0.dist-info/METADATA +365 -0
- conventional_git-1.0.0.dist-info/RECORD +44 -0
- conventional_git-1.0.0.dist-info/WHEEL +4 -0
- conventional_git-1.0.0.dist-info/entry_points.txt +3 -0
- conventional_git-1.0.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import sys
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from typing import Annotated
|
|
7
|
+
|
|
8
|
+
import typer
|
|
9
|
+
|
|
10
|
+
from conventional_git.branch import rules as branch_rules
|
|
11
|
+
from conventional_git.branch import vocabulary as branch_vocab
|
|
12
|
+
from conventional_git.commit import rules as commit_rules
|
|
13
|
+
from conventional_git.commit import vocabulary as commit_vocab
|
|
14
|
+
from conventional_git.config import Config
|
|
15
|
+
from conventional_git.violations import Report
|
|
16
|
+
from conventional_git.violations import Severity
|
|
17
|
+
|
|
18
|
+
# pylint: disable-next=app-require-final,app-module-const-naming
|
|
19
|
+
app = typer.Typer(help="Validate messages and branch names against the rules.")
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _report_to_exit_code(*, valid: bool) -> None:
|
|
23
|
+
if not valid:
|
|
24
|
+
raise typer.Exit(1)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _print_report(target: str, report: Report) -> None:
|
|
28
|
+
if report.valid and not report.warnings:
|
|
29
|
+
typer.echo(f"{target}: ok")
|
|
30
|
+
return
|
|
31
|
+
for violation in report.violations:
|
|
32
|
+
level = "warning" if violation.severity is Severity.WARNING else "error"
|
|
33
|
+
typer.echo(
|
|
34
|
+
f"{target}:{violation.field}: [{level}] {violation.code}: {violation.message} (fix: {violation.fix_hint})",
|
|
35
|
+
err=True,
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@app.command("commit")
|
|
40
|
+
def check_commit(
|
|
41
|
+
message: Annotated[
|
|
42
|
+
str | None,
|
|
43
|
+
typer.Option(
|
|
44
|
+
"--message",
|
|
45
|
+
"-m",
|
|
46
|
+
help="Commit message string; reads stdin if omitted",
|
|
47
|
+
),
|
|
48
|
+
] = None,
|
|
49
|
+
message_file: Annotated[
|
|
50
|
+
Path | None,
|
|
51
|
+
typer.Option(
|
|
52
|
+
"--file",
|
|
53
|
+
"-f",
|
|
54
|
+
help="Path to a file containing the commit message",
|
|
55
|
+
),
|
|
56
|
+
] = None,
|
|
57
|
+
types_csv: Annotated[
|
|
58
|
+
Path | None,
|
|
59
|
+
typer.Option(
|
|
60
|
+
"--types-csv",
|
|
61
|
+
help="Optional CSV overriding the default commit type vocabulary",
|
|
62
|
+
),
|
|
63
|
+
] = None,
|
|
64
|
+
) -> None:
|
|
65
|
+
config = Config.load()
|
|
66
|
+
text = _resolve_message(message, message_file)
|
|
67
|
+
policy = commit_vocab.resolve_policy(config, extra_types_csv=types_csv)
|
|
68
|
+
report = commit_rules.validate_message(
|
|
69
|
+
text,
|
|
70
|
+
allowed_types=policy.types,
|
|
71
|
+
attribution_patterns=policy.extra_attribution_patterns,
|
|
72
|
+
)
|
|
73
|
+
_print_report("commit", report)
|
|
74
|
+
_report_to_exit_code(valid=report.valid)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
@app.command("branch")
|
|
78
|
+
def check_branch(
|
|
79
|
+
name: Annotated[
|
|
80
|
+
str | None,
|
|
81
|
+
typer.Option(
|
|
82
|
+
"--name",
|
|
83
|
+
"-n",
|
|
84
|
+
help="Branch name to validate; defaults to the current branch",
|
|
85
|
+
),
|
|
86
|
+
] = None,
|
|
87
|
+
types_csv: Annotated[
|
|
88
|
+
Path | None,
|
|
89
|
+
typer.Option(
|
|
90
|
+
"--types-csv",
|
|
91
|
+
help="Optional CSV overriding the default branch type vocabulary",
|
|
92
|
+
),
|
|
93
|
+
] = None,
|
|
94
|
+
trunks_csv: Annotated[
|
|
95
|
+
Path | None,
|
|
96
|
+
typer.Option(
|
|
97
|
+
"--trunks-csv",
|
|
98
|
+
help="Optional CSV adding to the default trunk branch names exempt from the <type>/<description> format",
|
|
99
|
+
),
|
|
100
|
+
] = None,
|
|
101
|
+
) -> None:
|
|
102
|
+
if name is None:
|
|
103
|
+
current = _current_branch()
|
|
104
|
+
if not current:
|
|
105
|
+
typer.echo("branch: skipped (detached HEAD)")
|
|
106
|
+
return
|
|
107
|
+
name = current
|
|
108
|
+
config = Config.load()
|
|
109
|
+
policy = branch_vocab.resolve_policy(config, extra_types_csv=types_csv, extra_trunks_csv=trunks_csv)
|
|
110
|
+
report = branch_rules.validate_name(name, allowed_types=policy.types, trunk_branches=policy.trunks)
|
|
111
|
+
_print_report("branch", report)
|
|
112
|
+
_report_to_exit_code(valid=report.valid)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def _resolve_message(message: str | None, message_file: Path | None) -> str:
|
|
116
|
+
if message is not None:
|
|
117
|
+
return message
|
|
118
|
+
if message_file is not None:
|
|
119
|
+
return message_file.read_text(encoding="utf-8")
|
|
120
|
+
return sys.stdin.read()
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def _current_branch() -> str:
|
|
124
|
+
head_ref = os.environ.get("GITHUB_HEAD_REF")
|
|
125
|
+
if head_ref:
|
|
126
|
+
return head_ref
|
|
127
|
+
head = Path(".git/HEAD")
|
|
128
|
+
if head.exists():
|
|
129
|
+
ref = head.read_text(encoding="utf-8").strip()
|
|
130
|
+
if ref.startswith("ref: refs/heads/"):
|
|
131
|
+
return ref[len("ref: refs/heads/") :]
|
|
132
|
+
return ""
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import shutil
|
|
4
|
+
import subprocess # ruff: ignore[suspicious-subprocess-import] — fixed argv, no shell, no user-controlled input
|
|
5
|
+
from pathlib import Path # ruff: ignore[typing-only-standard-library-import] — Typer resolves at runtime
|
|
6
|
+
from typing import Annotated
|
|
7
|
+
|
|
8
|
+
import typer
|
|
9
|
+
|
|
10
|
+
from conventional_git import generation
|
|
11
|
+
from conventional_git.branch import rules as branch_rules
|
|
12
|
+
from conventional_git.branch import vocabulary as branch_vocab
|
|
13
|
+
from conventional_git.commit import compose as commit_compose
|
|
14
|
+
from conventional_git.commit import rules as commit_rules
|
|
15
|
+
from conventional_git.commit import vocabulary as commit_vocab
|
|
16
|
+
from conventional_git.config import Config
|
|
17
|
+
from conventional_git.helpers import strip_attribution
|
|
18
|
+
|
|
19
|
+
# pylint: disable-next=app-require-final,app-module-const-naming
|
|
20
|
+
app = typer.Typer(help="Generate a Conventional Commits message or branch name from inputs.")
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@app.command("commit")
|
|
24
|
+
def create_commit(
|
|
25
|
+
commit_type: Annotated[str, typer.Option("--type", help="Conventional commit type (e.g. feat, fix)")],
|
|
26
|
+
description: Annotated[
|
|
27
|
+
str, typer.Option("--description", help="Imperative present-tense summary, lowercase first letter")
|
|
28
|
+
],
|
|
29
|
+
scope: Annotated[
|
|
30
|
+
str | None,
|
|
31
|
+
typer.Option(help="Optional scope; a single lowercase token (auth, api, cli)"),
|
|
32
|
+
] = None,
|
|
33
|
+
body: Annotated[
|
|
34
|
+
str | None,
|
|
35
|
+
typer.Option(help="Entire body as one newline-separated string"),
|
|
36
|
+
] = None,
|
|
37
|
+
breaking: Annotated[
|
|
38
|
+
bool,
|
|
39
|
+
typer.Option("--breaking/--no-breaking", help="Mark as breaking; appends ! and a footer"),
|
|
40
|
+
] = False,
|
|
41
|
+
types_csv: Annotated[
|
|
42
|
+
Path | None,
|
|
43
|
+
typer.Option("--types-csv", help="Optional CSV overriding the default commit type vocabulary"),
|
|
44
|
+
] = None,
|
|
45
|
+
dry_run: Annotated[
|
|
46
|
+
bool,
|
|
47
|
+
typer.Option("--dry-run/--no-dry-run", help="Print the message and exit without acting"),
|
|
48
|
+
] = False,
|
|
49
|
+
) -> None:
|
|
50
|
+
config = Config.load()
|
|
51
|
+
policy = commit_vocab.resolve_policy(config, extra_types_csv=types_csv)
|
|
52
|
+
_render_commit(
|
|
53
|
+
commit_type,
|
|
54
|
+
scope,
|
|
55
|
+
description,
|
|
56
|
+
body,
|
|
57
|
+
breaking=breaking,
|
|
58
|
+
types=policy.types,
|
|
59
|
+
config=config,
|
|
60
|
+
)
|
|
61
|
+
if dry_run:
|
|
62
|
+
raise typer.Exit(0)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _render_commit(
|
|
66
|
+
commit_type: str,
|
|
67
|
+
scope: str | None,
|
|
68
|
+
description: str,
|
|
69
|
+
body: str | None,
|
|
70
|
+
*,
|
|
71
|
+
breaking: bool,
|
|
72
|
+
types: frozenset[str],
|
|
73
|
+
config: Config,
|
|
74
|
+
) -> None:
|
|
75
|
+
body_lines = strip_attribution(body, config=config)
|
|
76
|
+
message = commit_compose.build_message(commit_type, scope, description, body_lines, breaking=breaking)
|
|
77
|
+
report = commit_rules.validate_message(message, allowed_types=types)
|
|
78
|
+
if not report.valid:
|
|
79
|
+
for violation in report.errors:
|
|
80
|
+
typer.echo(f"{violation.field}: {violation.message} (fix: {violation.fix_hint})", err=True)
|
|
81
|
+
raise typer.Exit(1)
|
|
82
|
+
typer.echo(message)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
@app.command("branch")
|
|
86
|
+
def create_branch(
|
|
87
|
+
branch_type: Annotated[str, typer.Option("--type", help="Conventional branch type (e.g. feature, bugfix)")],
|
|
88
|
+
description: Annotated[str, typer.Option("--description", help="Branch description in any format")],
|
|
89
|
+
types_csv: Annotated[
|
|
90
|
+
Path | None,
|
|
91
|
+
typer.Option("--types-csv", help="Optional CSV overriding the default branch type vocabulary"),
|
|
92
|
+
] = None,
|
|
93
|
+
dry_run: Annotated[
|
|
94
|
+
bool,
|
|
95
|
+
typer.Option("--dry-run/--no-dry-run", help="Print the branch name and exit"),
|
|
96
|
+
] = False,
|
|
97
|
+
) -> None:
|
|
98
|
+
config = Config.load()
|
|
99
|
+
policy = branch_vocab.resolve_policy(config, extra_types_csv=types_csv)
|
|
100
|
+
if branch_type not in policy.types:
|
|
101
|
+
typer.echo(
|
|
102
|
+
f"Branch type {branch_type!r} is not allowed. Allowed: {', '.join(sorted(policy.types))}",
|
|
103
|
+
err=True,
|
|
104
|
+
)
|
|
105
|
+
raise typer.Exit(1)
|
|
106
|
+
name = branch_rules.build_branch_name(branch_type, branch_rules.normalize_description(description))
|
|
107
|
+
typer.echo(name)
|
|
108
|
+
if dry_run:
|
|
109
|
+
raise typer.Exit(0)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
@app.command("suggest")
|
|
113
|
+
def suggest_commit(
|
|
114
|
+
diff_file: Annotated[
|
|
115
|
+
Path | None,
|
|
116
|
+
typer.Option("--diff-file", help="Read the diff from this file instead of 'git diff --cached'"),
|
|
117
|
+
] = None,
|
|
118
|
+
provider_name: Annotated[
|
|
119
|
+
str | None,
|
|
120
|
+
typer.Option("--provider", help="Force a specific suggestion provider (e.g. heuristic, jev)"),
|
|
121
|
+
] = None,
|
|
122
|
+
apply_suggestion: Annotated[
|
|
123
|
+
bool,
|
|
124
|
+
typer.Option("--apply/--no-apply", help="Render and validate the suggested commit instead of printing it"),
|
|
125
|
+
] = False,
|
|
126
|
+
) -> None:
|
|
127
|
+
diff = diff_file.read_text(encoding="utf-8") if diff_file is not None else _staged_diff()
|
|
128
|
+
suggestion = _suggest(diff, provider_name)
|
|
129
|
+
if suggestion is None:
|
|
130
|
+
typer.echo("No staged changes to suggest a commit for.", err=True)
|
|
131
|
+
raise typer.Exit(1)
|
|
132
|
+
if apply_suggestion:
|
|
133
|
+
config = Config.load()
|
|
134
|
+
criteria = commit_vocab.merge_criteria(config.commit_type_overrides)
|
|
135
|
+
_render_commit(
|
|
136
|
+
suggestion.type,
|
|
137
|
+
suggestion.scope or None,
|
|
138
|
+
suggestion.description,
|
|
139
|
+
None,
|
|
140
|
+
breaking=suggestion.breaking,
|
|
141
|
+
types=frozenset(criteria),
|
|
142
|
+
config=config,
|
|
143
|
+
)
|
|
144
|
+
return
|
|
145
|
+
typer.echo(f"type: {suggestion.type}")
|
|
146
|
+
typer.echo(f"scope: {suggestion.scope or '(none)'}")
|
|
147
|
+
typer.echo(f"description: {suggestion.description}")
|
|
148
|
+
typer.echo(f"breaking: {suggestion.breaking}")
|
|
149
|
+
typer.echo(f"confidence: {suggestion.confidence:.2f}")
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def _staged_diff() -> str:
|
|
153
|
+
git = shutil.which("git") or "git"
|
|
154
|
+
result = subprocess.run( # ruff: ignore[subprocess-without-shell-equals-true] — fixed argv, no shell, no user-controlled input
|
|
155
|
+
[git, "diff", "--cached"],
|
|
156
|
+
check=False,
|
|
157
|
+
capture_output=True,
|
|
158
|
+
text=True,
|
|
159
|
+
)
|
|
160
|
+
return result.stdout
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def _suggest(diff: str, provider_name: str | None) -> generation.CommitSuggestion | None:
|
|
164
|
+
config = Config.load()
|
|
165
|
+
criteria = commit_vocab.merge_criteria(config.commit_type_overrides)
|
|
166
|
+
result = generation.suggest(diff, types=criteria, preferred=provider_name)
|
|
167
|
+
if result.error is not None:
|
|
168
|
+
typer.echo(result.error, err=True)
|
|
169
|
+
raise typer.Exit(1)
|
|
170
|
+
if result.warning is not None:
|
|
171
|
+
typer.echo(f"{result.warning} Falling back to the heuristic provider.", err=True)
|
|
172
|
+
return result.suggestion
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import subprocess # ruff: ignore[suspicious-subprocess-import]
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from typing import TYPE_CHECKING
|
|
6
|
+
from typing import Annotated
|
|
7
|
+
|
|
8
|
+
import typer
|
|
9
|
+
|
|
10
|
+
if TYPE_CHECKING:
|
|
11
|
+
from typing import Final
|
|
12
|
+
|
|
13
|
+
# pylint: disable-next=app-require-final,app-module-const-naming
|
|
14
|
+
app = typer.Typer(help="Install / uninstall pre-commit hooks in any git repo.")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
_MANAGED_BY: Final[str] = "# managed-by: conventional-git"
|
|
18
|
+
_HOOKS: Final[dict[str, str]] = {
|
|
19
|
+
"commit-msg": (
|
|
20
|
+
f'#!/usr/bin/env bash\n{_MANAGED_BY}\nset -euo pipefail\nconventional-git check commit --file "$1"\n'
|
|
21
|
+
),
|
|
22
|
+
"pre-commit": (
|
|
23
|
+
"#!/usr/bin/env bash\n"
|
|
24
|
+
f"{_MANAGED_BY}\n"
|
|
25
|
+
"set -euo pipefail\n"
|
|
26
|
+
"# detached HEAD: no branch to validate\n"
|
|
27
|
+
"current=$(git symbolic-ref --quiet --short HEAD) || exit 0\n"
|
|
28
|
+
'conventional-git check branch --name "$current"\n'
|
|
29
|
+
),
|
|
30
|
+
"pre-push": (
|
|
31
|
+
"#!/usr/bin/env bash\n"
|
|
32
|
+
f"{_MANAGED_BY}\n"
|
|
33
|
+
"set -euo pipefail\n"
|
|
34
|
+
"while read -r local_ref local_sha remote_ref remote_sha; do\n"
|
|
35
|
+
' [ -z "$local_sha" ] && continue\n'
|
|
36
|
+
' [ "$local_sha" = "0000000000000000000000000000000000000000" ] && continue\n'
|
|
37
|
+
" branch=${local_ref#refs/heads/}\n"
|
|
38
|
+
' conventional-git check branch --name "$branch" < /dev/null\n'
|
|
39
|
+
"done\n"
|
|
40
|
+
),
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _repo_root() -> Path:
|
|
45
|
+
return Path.cwd()
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
@app.command("install")
|
|
49
|
+
def install_hook(
|
|
50
|
+
target: Annotated[
|
|
51
|
+
Path | None,
|
|
52
|
+
typer.Option(
|
|
53
|
+
"--target",
|
|
54
|
+
help="Target repo path; defaults to the current directory",
|
|
55
|
+
),
|
|
56
|
+
] = None,
|
|
57
|
+
force: Annotated[
|
|
58
|
+
bool,
|
|
59
|
+
typer.Option(
|
|
60
|
+
"--force/--no-force",
|
|
61
|
+
help="Overwrite existing hooks if present",
|
|
62
|
+
),
|
|
63
|
+
] = False,
|
|
64
|
+
) -> None:
|
|
65
|
+
cwd = (target or _repo_root()).resolve()
|
|
66
|
+
hooks_dir = _hooks_dir(cwd)
|
|
67
|
+
hooks_dir.mkdir(parents=True, exist_ok=True)
|
|
68
|
+
_note_for_external_hooks_dir(cwd, hooks_dir)
|
|
69
|
+
for name, content in _HOOKS.items():
|
|
70
|
+
_write_hook(hooks_dir / name, content, force=force)
|
|
71
|
+
typer.echo(f"Installed hooks in {hooks_dir}")
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
@app.command("uninstall")
|
|
75
|
+
def uninstall_hook(
|
|
76
|
+
target: Annotated[
|
|
77
|
+
Path | None,
|
|
78
|
+
typer.Option("--target", help="Target repo path; defaults to the current directory"),
|
|
79
|
+
] = None,
|
|
80
|
+
) -> None:
|
|
81
|
+
cwd = (target or _repo_root()).resolve()
|
|
82
|
+
hooks_dir = _hooks_dir(cwd)
|
|
83
|
+
for name in _HOOKS:
|
|
84
|
+
path = hooks_dir / name
|
|
85
|
+
if not path.exists():
|
|
86
|
+
continue
|
|
87
|
+
if _MANAGED_BY not in path.read_text(encoding="utf-8"):
|
|
88
|
+
typer.echo(f"Skipping {path}: not installed by conventional-git")
|
|
89
|
+
continue
|
|
90
|
+
path.unlink()
|
|
91
|
+
typer.echo(f"Removed hooks from {hooks_dir}")
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _hooks_dir(repo: Path) -> Path:
|
|
95
|
+
result = subprocess.run( # ruff: ignore[subprocess-without-shell-equals-true] — fixed argv, no shell, no user-controlled input
|
|
96
|
+
["git", "-C", str(repo), "rev-parse", "--git-path", "hooks"], # ruff: ignore[start-process-with-partial-path]
|
|
97
|
+
capture_output=True,
|
|
98
|
+
check=False,
|
|
99
|
+
text=True,
|
|
100
|
+
)
|
|
101
|
+
if result.returncode != 0:
|
|
102
|
+
typer.echo(f"Not a git repository: {repo}", err=True)
|
|
103
|
+
raise typer.Exit(1)
|
|
104
|
+
hooks_dir = Path(result.stdout.strip())
|
|
105
|
+
return hooks_dir if hooks_dir.is_absolute() else repo / hooks_dir
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _note_for_external_hooks_dir(repo: Path, hooks_dir: Path) -> None:
|
|
109
|
+
result = subprocess.run( # ruff: ignore[subprocess-without-shell-equals-true] — fixed argv, no shell, no user-controlled input
|
|
110
|
+
["git", "-C", str(repo), "rev-parse", "--git-common-dir"], # ruff: ignore[start-process-with-partial-path]
|
|
111
|
+
capture_output=True,
|
|
112
|
+
check=False,
|
|
113
|
+
text=True,
|
|
114
|
+
)
|
|
115
|
+
if result.returncode != 0:
|
|
116
|
+
return
|
|
117
|
+
git_common_dir = Path(result.stdout.strip())
|
|
118
|
+
if not git_common_dir.is_absolute():
|
|
119
|
+
git_common_dir = repo / git_common_dir
|
|
120
|
+
default_hooks_dir = (git_common_dir / "hooks").resolve()
|
|
121
|
+
if hooks_dir.resolve() != default_hooks_dir:
|
|
122
|
+
typer.echo(
|
|
123
|
+
f"Note: hooks path {hooks_dir} is outside {default_hooks_dir}; it may be managed by another tool.",
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def _write_hook(path: Path, content: str, *, force: bool) -> None:
|
|
128
|
+
if path.exists() and not force:
|
|
129
|
+
typer.echo(f"Refusing to overwrite existing hook: {path}", err=True)
|
|
130
|
+
raise typer.Exit(1)
|
|
131
|
+
path.write_text(content, encoding="utf-8")
|
|
132
|
+
path.chmod(0o755)
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import typer
|
|
4
|
+
|
|
5
|
+
from conventional_git.mcp import server
|
|
6
|
+
|
|
7
|
+
# pylint: disable-next=app-require-final,app-module-const-naming
|
|
8
|
+
app = typer.Typer(help="MCP server surface for conventional-git.")
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@app.command("serve")
|
|
12
|
+
def serve() -> None:
|
|
13
|
+
server.run()
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from conventional_git.commit import compose
|
|
4
|
+
from conventional_git.commit import grammar
|
|
5
|
+
from conventional_git.commit import rules
|
|
6
|
+
from conventional_git.commit import vocabulary
|
|
7
|
+
|
|
8
|
+
__all__ = ["compose", "grammar", "rules", "vocabulary"]
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import TYPE_CHECKING
|
|
4
|
+
|
|
5
|
+
if TYPE_CHECKING:
|
|
6
|
+
from collections.abc import Iterable
|
|
7
|
+
from typing import Final
|
|
8
|
+
|
|
9
|
+
_DEFAULT_DESCRIPTION: Final[str] = "update implementation"
|
|
10
|
+
_BULLET_PREFIXES: Final[tuple[str, ...]] = ("- ", "* ")
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def normalize_description(description: str | None) -> str:
|
|
14
|
+
normalized = (description or _DEFAULT_DESCRIPTION).strip().rstrip(".").strip() or _DEFAULT_DESCRIPTION
|
|
15
|
+
return normalized[:1].lower() + normalized[1:]
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _as_bullet(line: str) -> str:
|
|
19
|
+
if line.startswith(_BULLET_PREFIXES):
|
|
20
|
+
return line
|
|
21
|
+
return f"- {line.lstrip('-').strip()}"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def build_message(
|
|
25
|
+
commit_type: str,
|
|
26
|
+
scope: str | None,
|
|
27
|
+
description: str,
|
|
28
|
+
body_lines: Iterable[str],
|
|
29
|
+
*,
|
|
30
|
+
breaking: bool,
|
|
31
|
+
) -> str:
|
|
32
|
+
scope_segment = f"({scope})" if scope else ""
|
|
33
|
+
exclamation = "!" if breaking else ""
|
|
34
|
+
normalized_description = normalize_description(description)
|
|
35
|
+
title = f"{commit_type}{scope_segment}{exclamation}: {normalized_description}"
|
|
36
|
+
body_text = "\n".join(_as_bullet(line) for line in body_lines)
|
|
37
|
+
if breaking:
|
|
38
|
+
return f"{title}\n\n{body_text}\n\nBREAKING CHANGE: {normalized_description}"
|
|
39
|
+
return f"{title}\n\n{body_text}" if body_text else title
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
from typing import TYPE_CHECKING
|
|
5
|
+
|
|
6
|
+
if TYPE_CHECKING:
|
|
7
|
+
from typing import Final
|
|
8
|
+
|
|
9
|
+
MAX_TITLE_LENGTH: Final[int] = 120
|
|
10
|
+
|
|
11
|
+
_DESCRIPTION_PATTERN: Final[re.Pattern[str]] = re.compile(r"^[a-z0-9].*$")
|
|
12
|
+
_SCOPE: Final[str] = r"[a-z0-9.\-]+"
|
|
13
|
+
_SCOPE_PATTERN: Final[re.Pattern[str]] = re.compile(rf"^{_SCOPE}$")
|
|
14
|
+
# §16 allows BREAKING-CHANGE; line anchoring and spaces keep its nonblank value on that line.
|
|
15
|
+
_BREAKING_FOOTER_PATTERN: Final[re.Pattern[str]] = re.compile(r"^BREAKING[ -]CHANGE: *\S.*$", re.MULTILINE)
|
|
16
|
+
_TITLE_PATTERN: Final[re.Pattern[str]] = re.compile(
|
|
17
|
+
r"^(?P<type>[a-z]+)"
|
|
18
|
+
rf"(?:\((?P<scope>{_SCOPE})\))?"
|
|
19
|
+
r"(?P<breaking>!)?"
|
|
20
|
+
r": (?P<description>.+)$"
|
|
21
|
+
)
|
|
22
|
+
TRAILING_DOT: Final[re.Pattern[str]] = re.compile(r"\.\s*$")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def split_title(title: str) -> dict[str, str] | None:
|
|
26
|
+
match = _TITLE_PATTERN.match(title)
|
|
27
|
+
if not match:
|
|
28
|
+
return None
|
|
29
|
+
return match.groupdict()
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def is_valid_description(text: str) -> bool:
|
|
33
|
+
return bool(_DESCRIPTION_PATTERN.match(text))
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def is_valid_scope(text: str) -> bool:
|
|
37
|
+
return bool(_SCOPE_PATTERN.fullmatch(text))
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def has_breaking_footer(message: str) -> bool:
|
|
41
|
+
return bool(_BREAKING_FOOTER_PATTERN.search(message))
|