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,131 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from enum import StrEnum
|
|
5
|
+
from typing import TYPE_CHECKING
|
|
6
|
+
|
|
7
|
+
if TYPE_CHECKING:
|
|
8
|
+
from typing import Final
|
|
9
|
+
|
|
10
|
+
_SECTION_MARKER: Final[str] = "diff --git "
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class ChangeStatus(StrEnum):
|
|
14
|
+
ADDED = "added"
|
|
15
|
+
DELETED = "deleted"
|
|
16
|
+
MODIFIED = "modified"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass(frozen=True, slots=True)
|
|
20
|
+
class FileChange:
|
|
21
|
+
path: str
|
|
22
|
+
status: ChangeStatus
|
|
23
|
+
added: int
|
|
24
|
+
removed: int
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def parse_diff(diff: str) -> tuple[FileChange, ...]:
|
|
28
|
+
if not diff:
|
|
29
|
+
return ()
|
|
30
|
+
sections = _split_sections(diff)
|
|
31
|
+
changes = [change for section in sections if (change := _parse_section(section)) is not None]
|
|
32
|
+
return tuple(changes)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def paths_from_diff(diff: str) -> tuple[str, ...]:
|
|
36
|
+
return tuple(sorted({change.path for change in parse_diff(diff)}))
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def change_verb(changes: tuple[FileChange, ...]) -> str:
|
|
40
|
+
if not changes:
|
|
41
|
+
return "update"
|
|
42
|
+
if all(change.status == ChangeStatus.ADDED for change in changes):
|
|
43
|
+
return "add"
|
|
44
|
+
if all(change.status == ChangeStatus.DELETED for change in changes):
|
|
45
|
+
return "remove"
|
|
46
|
+
added = sum(change.added for change in changes)
|
|
47
|
+
removed = sum(change.removed for change in changes)
|
|
48
|
+
if added and not removed:
|
|
49
|
+
return "add"
|
|
50
|
+
if removed and not added:
|
|
51
|
+
return "remove"
|
|
52
|
+
return "update"
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _split_sections(diff: str) -> list[str]:
|
|
56
|
+
if _SECTION_MARKER not in diff:
|
|
57
|
+
return [diff]
|
|
58
|
+
sections: list[str] = []
|
|
59
|
+
current: list[str] = []
|
|
60
|
+
for line in diff.splitlines(keepends=True):
|
|
61
|
+
if line.startswith(_SECTION_MARKER) and current:
|
|
62
|
+
sections.append("".join(current))
|
|
63
|
+
current = []
|
|
64
|
+
current.append(line)
|
|
65
|
+
if current:
|
|
66
|
+
sections.append("".join(current))
|
|
67
|
+
return sections
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
@dataclass(slots=True)
|
|
71
|
+
class _SectionState:
|
|
72
|
+
status: ChangeStatus = ChangeStatus.MODIFIED
|
|
73
|
+
path: str | None = None
|
|
74
|
+
old_path: str | None = None
|
|
75
|
+
added: int = 0
|
|
76
|
+
removed: int = 0
|
|
77
|
+
in_hunk: bool = False
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _parse_section(section: str) -> FileChange | None:
|
|
81
|
+
state = _SectionState()
|
|
82
|
+
for line in section.splitlines():
|
|
83
|
+
_apply_line(state, line)
|
|
84
|
+
resolved_path = state.path or state.old_path
|
|
85
|
+
if resolved_path is None:
|
|
86
|
+
return None
|
|
87
|
+
return FileChange(path=resolved_path, status=state.status, added=state.added, removed=state.removed)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _apply_line(state: _SectionState, line: str) -> None:
|
|
91
|
+
if line.startswith("@@"):
|
|
92
|
+
state.in_hunk = True
|
|
93
|
+
return
|
|
94
|
+
if line.startswith("new file mode"):
|
|
95
|
+
state.status = ChangeStatus.ADDED
|
|
96
|
+
return
|
|
97
|
+
if line.startswith("deleted file mode"):
|
|
98
|
+
state.status = ChangeStatus.DELETED
|
|
99
|
+
return
|
|
100
|
+
if line.startswith("--- "):
|
|
101
|
+
state.in_hunk = False
|
|
102
|
+
_apply_old_header(state, line[4:])
|
|
103
|
+
return
|
|
104
|
+
if line.startswith("+++ "):
|
|
105
|
+
state.in_hunk = False
|
|
106
|
+
_apply_new_header(state, line[4:])
|
|
107
|
+
return
|
|
108
|
+
if not state.in_hunk:
|
|
109
|
+
return
|
|
110
|
+
_apply_hunk_line(state, line)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _apply_old_header(state: _SectionState, target: str) -> None:
|
|
114
|
+
if target == "/dev/null":
|
|
115
|
+
state.status = ChangeStatus.ADDED
|
|
116
|
+
elif target.startswith("a/"):
|
|
117
|
+
state.old_path = target[2:]
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def _apply_new_header(state: _SectionState, target: str) -> None:
|
|
121
|
+
if target == "/dev/null":
|
|
122
|
+
state.status = ChangeStatus.DELETED
|
|
123
|
+
elif target.startswith("b/"):
|
|
124
|
+
state.path = target[2:]
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def _apply_hunk_line(state: _SectionState, line: str) -> None:
|
|
128
|
+
if line.startswith("+"):
|
|
129
|
+
state.added += 1
|
|
130
|
+
elif line.startswith("-"):
|
|
131
|
+
state.removed += 1
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
from typing import TYPE_CHECKING
|
|
5
|
+
|
|
6
|
+
from conventional_git.generation.diff import change_verb
|
|
7
|
+
from conventional_git.generation.diff import parse_diff
|
|
8
|
+
from conventional_git.generation.diff import paths_from_diff
|
|
9
|
+
from conventional_git.generation.protocol import CommitSuggestion
|
|
10
|
+
from conventional_git.generation.protocol import register_provider
|
|
11
|
+
|
|
12
|
+
if TYPE_CHECKING:
|
|
13
|
+
from collections.abc import Mapping
|
|
14
|
+
from typing import Final
|
|
15
|
+
|
|
16
|
+
_TEST_SUFFIXES: Final[tuple[str, ...]] = ("_test.py",)
|
|
17
|
+
_TEST_PREFIXES: Final[tuple[str, ...]] = ("tests/", "test/", "tests\\", "test\\")
|
|
18
|
+
_DOCS_SUFFIXES: Final[tuple[str, ...]] = (".md", ".rst", ".txt")
|
|
19
|
+
_BUILD_FILES: Final[frozenset[str]] = frozenset((
|
|
20
|
+
"pyproject.toml",
|
|
21
|
+
"uv.lock",
|
|
22
|
+
"package-lock.json",
|
|
23
|
+
"yarn.lock",
|
|
24
|
+
"pnpm-lock.yaml",
|
|
25
|
+
"poetry.lock",
|
|
26
|
+
"Cargo.toml",
|
|
27
|
+
"Cargo.lock",
|
|
28
|
+
"go.mod",
|
|
29
|
+
"go.sum",
|
|
30
|
+
"Makefile",
|
|
31
|
+
))
|
|
32
|
+
_CI_FILES: Final[frozenset[str]] = frozenset((
|
|
33
|
+
".gitlab-ci.yml",
|
|
34
|
+
".pre-commit-config.yaml",
|
|
35
|
+
))
|
|
36
|
+
|
|
37
|
+
_TRAILING_PUNCT: Final[re.Pattern[str]] = re.compile(r"[\s.,;:!?]+$")
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class HeuristicProvider:
|
|
41
|
+
name = "heuristic"
|
|
42
|
+
|
|
43
|
+
def suggest(
|
|
44
|
+
self,
|
|
45
|
+
diff: str,
|
|
46
|
+
*,
|
|
47
|
+
changed_paths: tuple[str, ...] = (),
|
|
48
|
+
types: Mapping[str, str] | None = None,
|
|
49
|
+
) -> CommitSuggestion | None:
|
|
50
|
+
del types
|
|
51
|
+
if not changed_paths and not diff:
|
|
52
|
+
return None
|
|
53
|
+
paths = changed_paths or paths_from_diff(diff)
|
|
54
|
+
if not paths:
|
|
55
|
+
return None
|
|
56
|
+
commit_type = infer_type(paths)
|
|
57
|
+
verb = change_verb(parse_diff(diff))
|
|
58
|
+
description = infer_description(paths, commit_type, verb)
|
|
59
|
+
return CommitSuggestion(
|
|
60
|
+
type=commit_type,
|
|
61
|
+
scope="",
|
|
62
|
+
description=description,
|
|
63
|
+
confidence=0.5,
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def infer_type(paths: tuple[str, ...]) -> str:
|
|
68
|
+
if all(_is_test(p) for p in paths):
|
|
69
|
+
return "test"
|
|
70
|
+
if all(_is_docs(p) for p in paths):
|
|
71
|
+
return "docs"
|
|
72
|
+
if all(_is_build(p) for p in paths):
|
|
73
|
+
return "build"
|
|
74
|
+
if all(_is_ci(p) for p in paths):
|
|
75
|
+
return "ci"
|
|
76
|
+
return "chore"
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def _is_test(path: str) -> bool:
|
|
80
|
+
leaf = path.rsplit("/", 1)[-1]
|
|
81
|
+
if any(path.startswith(prefix) for prefix in _TEST_PREFIXES):
|
|
82
|
+
return True
|
|
83
|
+
return leaf.endswith(_TEST_SUFFIXES) or leaf.startswith("test_")
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _is_docs(path: str) -> bool:
|
|
87
|
+
return path.endswith(_DOCS_SUFFIXES)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _is_build(path: str) -> bool:
|
|
91
|
+
leaf = path.rsplit("/", 1)[-1]
|
|
92
|
+
return leaf in _BUILD_FILES
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _is_ci(path: str) -> bool:
|
|
96
|
+
if path.startswith((".github/", ".circleci/")):
|
|
97
|
+
return True
|
|
98
|
+
leaf = path.rsplit("/", 1)[-1]
|
|
99
|
+
return leaf in _CI_FILES
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def infer_description(paths: tuple[str, ...], commit_type: str, verb: str = "update") -> str:
|
|
103
|
+
first = paths[0].rstrip("/")
|
|
104
|
+
leaf = first.rsplit("/", 1)[-1] if "/" in first else first
|
|
105
|
+
if commit_type == "docs":
|
|
106
|
+
description = f"{verb} {leaf}" if leaf else f"{verb} documentation"
|
|
107
|
+
elif commit_type == "test":
|
|
108
|
+
description = "add coverage"
|
|
109
|
+
elif commit_type == "build":
|
|
110
|
+
description = "update dependencies"
|
|
111
|
+
elif commit_type == "ci":
|
|
112
|
+
description = "update pipelines"
|
|
113
|
+
else:
|
|
114
|
+
description = f"{verb} {leaf}" if leaf else "apply changes"
|
|
115
|
+
cleaned = _TRAILING_PUNCT.sub("", description)
|
|
116
|
+
return cleaned[:1].lower() + cleaned[1:]
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
register_provider(HeuristicProvider())
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import contextlib
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from importlib import import_module
|
|
6
|
+
from typing import TYPE_CHECKING
|
|
7
|
+
from typing import Protocol
|
|
8
|
+
|
|
9
|
+
if TYPE_CHECKING:
|
|
10
|
+
from collections.abc import Mapping
|
|
11
|
+
from typing import Final
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass(frozen=True, slots=True)
|
|
15
|
+
class CommitSuggestion:
|
|
16
|
+
type: str
|
|
17
|
+
scope: str
|
|
18
|
+
description: str
|
|
19
|
+
confidence: float
|
|
20
|
+
breaking: bool = False
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class SuggestionProvider(Protocol):
|
|
24
|
+
name: str
|
|
25
|
+
|
|
26
|
+
def suggest(
|
|
27
|
+
self,
|
|
28
|
+
diff: str,
|
|
29
|
+
*,
|
|
30
|
+
changed_paths: tuple[str, ...] = (),
|
|
31
|
+
types: Mapping[str, str] | None = None,
|
|
32
|
+
) -> CommitSuggestion | None: ...
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class ProviderError(RuntimeError):
|
|
36
|
+
pass
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class MissingCredentialsError(ProviderError):
|
|
40
|
+
pass
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
_REGISTRY: Final[dict[str, SuggestionProvider]] = {}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def register_provider(provider: SuggestionProvider) -> None:
|
|
47
|
+
_REGISTRY[provider.name] = provider
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def get_provider(name: str | None = None) -> SuggestionProvider | None:
|
|
51
|
+
if name is None:
|
|
52
|
+
name = "heuristic"
|
|
53
|
+
return _REGISTRY.get(name)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def available_providers() -> tuple[str, ...]:
|
|
57
|
+
return tuple(_REGISTRY.keys())
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def enable_optional_providers() -> None:
|
|
61
|
+
with contextlib.suppress(ImportError):
|
|
62
|
+
import_module("conventional_git.generation.typesafe")
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@dataclass(frozen=True, slots=True)
|
|
66
|
+
class SuggestionResult:
|
|
67
|
+
provider: str | None
|
|
68
|
+
suggestion: CommitSuggestion | None
|
|
69
|
+
warning: str | None = None
|
|
70
|
+
error: str | None = None
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def suggest(
|
|
74
|
+
diff: str,
|
|
75
|
+
*,
|
|
76
|
+
changed_paths: tuple[str, ...] = (),
|
|
77
|
+
types: Mapping[str, str] | None = None,
|
|
78
|
+
preferred: str | None = None,
|
|
79
|
+
) -> SuggestionResult:
|
|
80
|
+
enable_optional_providers()
|
|
81
|
+
name = preferred or ("jev" if "jev" in available_providers() else "heuristic")
|
|
82
|
+
provider = get_provider(name)
|
|
83
|
+
if provider is None:
|
|
84
|
+
error = f"Unknown provider {name!r}. Available: {', '.join(available_providers())}"
|
|
85
|
+
return SuggestionResult(provider=None, suggestion=None, error=error)
|
|
86
|
+
try:
|
|
87
|
+
suggestion = provider.suggest(diff, changed_paths=changed_paths, types=types)
|
|
88
|
+
except ProviderError as error:
|
|
89
|
+
if preferred is not None:
|
|
90
|
+
return SuggestionResult(provider=name, suggestion=None, error=str(error))
|
|
91
|
+
heuristic = get_provider("heuristic")
|
|
92
|
+
fallback = heuristic.suggest(diff, changed_paths=changed_paths, types=types) if heuristic else None
|
|
93
|
+
return SuggestionResult(provider="heuristic", suggestion=fallback, warning=str(error))
|
|
94
|
+
return SuggestionResult(provider=name, suggestion=suggestion)
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
from typing import TYPE_CHECKING
|
|
5
|
+
|
|
6
|
+
from typesafe_sdk import Choice
|
|
7
|
+
from typesafe_sdk import Noul
|
|
8
|
+
from typesafe_sdk import TypeSafeClient
|
|
9
|
+
from typesafe_sdk import TypeSafeError
|
|
10
|
+
|
|
11
|
+
from conventional_git.commit import grammar as commit_grammar
|
|
12
|
+
from conventional_git.commit import vocabulary as commit_vocab
|
|
13
|
+
from conventional_git.generation import credentials
|
|
14
|
+
from conventional_git.generation.diff import change_verb
|
|
15
|
+
from conventional_git.generation.diff import parse_diff
|
|
16
|
+
from conventional_git.generation.diff import paths_from_diff
|
|
17
|
+
from conventional_git.generation.heuristic import infer_description
|
|
18
|
+
from conventional_git.generation.heuristic import infer_type
|
|
19
|
+
from conventional_git.generation.protocol import CommitSuggestion
|
|
20
|
+
from conventional_git.generation.protocol import MissingCredentialsError
|
|
21
|
+
from conventional_git.generation.protocol import ProviderError
|
|
22
|
+
from conventional_git.generation.protocol import register_provider
|
|
23
|
+
|
|
24
|
+
if TYPE_CHECKING:
|
|
25
|
+
from collections.abc import Callable
|
|
26
|
+
from collections.abc import Mapping
|
|
27
|
+
from typing import Final
|
|
28
|
+
|
|
29
|
+
_DIFF_MAX_CHARS: Final[int] = 12_000
|
|
30
|
+
_BASE_URL_ENV: Final[str] = "TYPESAFE_BASE_URL"
|
|
31
|
+
_MODEL_ENV: Final[str] = "TYPESAFE_DEFAULT_MODEL"
|
|
32
|
+
_OPENROUTER_BASE_URL: Final[str] = "https://openrouter.ai/api"
|
|
33
|
+
_OPENROUTER_MODEL: Final[str] = "typesafe/jev-1.13"
|
|
34
|
+
_NO_SCOPE: Final[str] = "none"
|
|
35
|
+
_BREAKING_THRESHOLD: Final[float] = 0.5
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _scope_candidates(paths: tuple[str, ...], types: Mapping[str, str]) -> list[str]:
|
|
39
|
+
seen: dict[str, None] = {}
|
|
40
|
+
for path in paths:
|
|
41
|
+
head = path.split("/", 1)[0] if "/" in path else ""
|
|
42
|
+
if head and head not in types and commit_grammar.is_valid_scope(head) and head not in seen:
|
|
43
|
+
seen[head] = None
|
|
44
|
+
return [*seen, _NO_SCOPE]
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _description_candidates(paths: tuple[str, ...], commit_type: str, diff: str) -> list[str]:
|
|
48
|
+
verb = change_verb(parse_diff(diff))
|
|
49
|
+
leaf = paths[0].rsplit("/", 1)[-1]
|
|
50
|
+
candidates = {infer_description(paths, commit_type, verb), f"{verb} {leaf}"}
|
|
51
|
+
return sorted(candidates)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _build_client() -> TypeSafeClient:
|
|
55
|
+
credential = credentials.resolve_active_credential()
|
|
56
|
+
if credential is None:
|
|
57
|
+
message = (
|
|
58
|
+
"No TypeSafe or OpenRouter API key found. Set TYPESAFE_API_KEY or "
|
|
59
|
+
"OPENROUTER_API_KEY, or run 'conventional-git auth login --provider "
|
|
60
|
+
"typesafe|openrouter'."
|
|
61
|
+
)
|
|
62
|
+
raise MissingCredentialsError(message)
|
|
63
|
+
if credential.provider == "typesafe":
|
|
64
|
+
return TypeSafeClient(api_key=credential.key)
|
|
65
|
+
base_url = os.environ.get(_BASE_URL_ENV, "").strip() or _OPENROUTER_BASE_URL
|
|
66
|
+
model = os.environ.get(_MODEL_ENV, "").strip() or _OPENROUTER_MODEL
|
|
67
|
+
return TypeSafeClient(api_key=credential.key, base_url=base_url, model=model)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class JevProvider:
|
|
71
|
+
name = "jev"
|
|
72
|
+
|
|
73
|
+
def __init__(self, client_factory: Callable[[], TypeSafeClient] = _build_client) -> None:
|
|
74
|
+
self._client_factory = client_factory
|
|
75
|
+
|
|
76
|
+
def suggest(
|
|
77
|
+
self,
|
|
78
|
+
diff: str,
|
|
79
|
+
*,
|
|
80
|
+
changed_paths: tuple[str, ...] = (),
|
|
81
|
+
types: Mapping[str, str] | None = None,
|
|
82
|
+
) -> CommitSuggestion | None:
|
|
83
|
+
paths = changed_paths or paths_from_diff(diff)
|
|
84
|
+
if not paths:
|
|
85
|
+
return None
|
|
86
|
+
criteria = dict(types) if types else commit_vocab.default_criteria()
|
|
87
|
+
allowed_types = set(criteria)
|
|
88
|
+
fallback_type = infer_type(paths)
|
|
89
|
+
scope_candidates = _scope_candidates(paths, criteria)
|
|
90
|
+
description_candidates = _description_candidates(paths, fallback_type, diff)
|
|
91
|
+
truncated_diff = diff[:_DIFF_MAX_CHARS]
|
|
92
|
+
|
|
93
|
+
try:
|
|
94
|
+
client = self._client_factory()
|
|
95
|
+
with client:
|
|
96
|
+
response = client.system_one(
|
|
97
|
+
state={"diff": truncated_diff, "changed_paths": list(paths)},
|
|
98
|
+
questions={
|
|
99
|
+
"type": Choice(
|
|
100
|
+
instructions="Which Conventional Commits type best matches this diff?",
|
|
101
|
+
criteria=criteria,
|
|
102
|
+
),
|
|
103
|
+
"scope": Choice(
|
|
104
|
+
instructions=(
|
|
105
|
+
"Pick the single module or area most affected by this diff, "
|
|
106
|
+
f"or {_NO_SCOPE!r} if it spans several unrelated areas."
|
|
107
|
+
),
|
|
108
|
+
criteria=dict.fromkeys(scope_candidates),
|
|
109
|
+
),
|
|
110
|
+
"description": Choice(
|
|
111
|
+
instructions=(
|
|
112
|
+
"Pick the imperative, present-tense commit description that best summarizes this diff."
|
|
113
|
+
),
|
|
114
|
+
criteria=dict.fromkeys(description_candidates),
|
|
115
|
+
),
|
|
116
|
+
"breaking": Noul(
|
|
117
|
+
instructions=(
|
|
118
|
+
"Does this diff remove or incompatibly change a public interface "
|
|
119
|
+
"(a public function signature, CLI flag, or API response shape)?"
|
|
120
|
+
),
|
|
121
|
+
),
|
|
122
|
+
},
|
|
123
|
+
)
|
|
124
|
+
except TypeSafeError as error:
|
|
125
|
+
message = f"jev provider failed: {type(error).__name__}: {error}"
|
|
126
|
+
raise ProviderError(message) from error
|
|
127
|
+
|
|
128
|
+
type_answer = response.choices["type"]
|
|
129
|
+
commit_type = type_answer.choice if type_answer.choice in allowed_types else fallback_type
|
|
130
|
+
scope_choice = response.choices["scope"].choice
|
|
131
|
+
return CommitSuggestion(
|
|
132
|
+
type=commit_type,
|
|
133
|
+
scope="" if scope_choice == _NO_SCOPE else scope_choice,
|
|
134
|
+
description=response.choices["description"].choice,
|
|
135
|
+
confidence=type_answer.confidence,
|
|
136
|
+
breaking=response.nouls["breaking"].noul >= _BREAKING_THRESHOLD,
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
register_provider(JevProvider())
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
from typing import TYPE_CHECKING
|
|
5
|
+
|
|
6
|
+
from conventional_git.commit import vocabulary as commit_vocabulary
|
|
7
|
+
from conventional_git.config import Config
|
|
8
|
+
|
|
9
|
+
if TYPE_CHECKING:
|
|
10
|
+
from typing import Final
|
|
11
|
+
|
|
12
|
+
# Stripping (used by `create commit`) is intentionally broader than the
|
|
13
|
+
# `commit.attribution` validation rule: it also removes bare mentions of the
|
|
14
|
+
# generating tool/vendor, which would be false positives if rejected outright.
|
|
15
|
+
_GENERATOR_ONLY_PATTERNS: Final[tuple[str, ...]] = ("claude", "anthropic")
|
|
16
|
+
DEFAULT_ATTRIBUTION_PATTERN: Final[re.Pattern[str]] = re.compile(
|
|
17
|
+
"|".join((*commit_vocabulary.default_attribution_patterns(), *_GENERATOR_ONLY_PATTERNS)),
|
|
18
|
+
re.IGNORECASE,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _compile(config: Config) -> re.Pattern[str]:
|
|
23
|
+
if not config.extra_attribution_patterns:
|
|
24
|
+
return DEFAULT_ATTRIBUTION_PATTERN
|
|
25
|
+
parts = [DEFAULT_ATTRIBUTION_PATTERN.pattern, *config.extra_attribution_patterns]
|
|
26
|
+
return re.compile("|".join(parts), re.IGNORECASE)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def strip_attribution(raw: str | None, *, config: Config | None = None) -> list[str]:
|
|
30
|
+
if not raw:
|
|
31
|
+
return []
|
|
32
|
+
pattern = _compile(config or Config.load())
|
|
33
|
+
lines: list[str] = []
|
|
34
|
+
for line in raw.splitlines():
|
|
35
|
+
stripped = line.strip()
|
|
36
|
+
if not stripped or pattern.search(stripped):
|
|
37
|
+
continue
|
|
38
|
+
lines.append(stripped)
|
|
39
|
+
return lines
|
|
File without changes
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import asdict
|
|
4
|
+
|
|
5
|
+
from mcp.server.fastmcp import FastMCP
|
|
6
|
+
|
|
7
|
+
from conventional_git import generation
|
|
8
|
+
from conventional_git.branch import rules as branch_rules
|
|
9
|
+
from conventional_git.branch import vocabulary as branch_vocab
|
|
10
|
+
from conventional_git.commit import rules as commit_rules
|
|
11
|
+
from conventional_git.commit import vocabulary as commit_vocab
|
|
12
|
+
from conventional_git.config import Config
|
|
13
|
+
|
|
14
|
+
# pylint: disable-next=app-require-final,app-module-const-naming
|
|
15
|
+
mcp = FastMCP("conventional-git")
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@mcp.tool()
|
|
19
|
+
def validate_commit_message(message: str) -> dict[str, object]:
|
|
20
|
+
policy = commit_vocab.resolve_policy(Config.load())
|
|
21
|
+
report = commit_rules.validate_message(
|
|
22
|
+
message,
|
|
23
|
+
allowed_types=policy.types,
|
|
24
|
+
attribution_patterns=policy.extra_attribution_patterns,
|
|
25
|
+
)
|
|
26
|
+
return report.to_dict()
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@mcp.tool()
|
|
30
|
+
def validate_branch_name(name: str) -> dict[str, object]:
|
|
31
|
+
policy = branch_vocab.resolve_policy(Config.load())
|
|
32
|
+
report = branch_rules.validate_name(name, allowed_types=policy.types, trunk_branches=policy.trunks)
|
|
33
|
+
return report.to_dict()
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@mcp.tool()
|
|
37
|
+
def describe_convention() -> dict[str, object]:
|
|
38
|
+
config = Config.load()
|
|
39
|
+
commit_policy = commit_vocab.resolve_policy(config)
|
|
40
|
+
branch_policy = branch_vocab.resolve_policy(config)
|
|
41
|
+
return {
|
|
42
|
+
"commit": {
|
|
43
|
+
"types": sorted(commit_policy.types),
|
|
44
|
+
"limits": asdict(commit_rules.limits()),
|
|
45
|
+
},
|
|
46
|
+
"branch": {
|
|
47
|
+
"types": sorted(branch_policy.types),
|
|
48
|
+
"trunks": sorted(branch_policy.trunks),
|
|
49
|
+
"limits": asdict(branch_rules.limits()),
|
|
50
|
+
},
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@mcp.tool()
|
|
55
|
+
def suggest_commit_message(diff: str, changed_paths: list[str] | None = None) -> dict[str, object]:
|
|
56
|
+
config = Config.load()
|
|
57
|
+
criteria = commit_vocab.merge_criteria(config.commit_type_overrides)
|
|
58
|
+
result = generation.suggest(diff, changed_paths=tuple(changed_paths or ()), types=criteria)
|
|
59
|
+
if result.error is not None:
|
|
60
|
+
return {"provider": None, "suggestion": None, "error": result.error}
|
|
61
|
+
payload: dict[str, object] = {
|
|
62
|
+
"provider": result.provider,
|
|
63
|
+
"suggestion": asdict(result.suggestion) if result.suggestion else None,
|
|
64
|
+
}
|
|
65
|
+
if result.warning is not None:
|
|
66
|
+
payload["warning"] = result.warning
|
|
67
|
+
return payload
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def run() -> None:
|
|
71
|
+
mcp.run(transport="stdio")
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
if __name__ == "__main__":
|
|
75
|
+
run()
|
|
File without changes
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from enum import StrEnum
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class Severity(StrEnum):
|
|
8
|
+
ERROR = "error"
|
|
9
|
+
WARNING = "warning"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class ViolationCode(StrEnum):
|
|
13
|
+
COMMIT_EMPTY = "commit.empty"
|
|
14
|
+
COMMIT_HEADER_FORMAT = "commit.header-format"
|
|
15
|
+
COMMIT_TYPE = "commit.type"
|
|
16
|
+
COMMIT_DESCRIPTION_FORMAT = "commit.description-format"
|
|
17
|
+
COMMIT_DESCRIPTION_TRAILING_PERIOD = "commit.description-trailing-period"
|
|
18
|
+
COMMIT_TITLE_LENGTH = "commit.title-length"
|
|
19
|
+
COMMIT_BODY_LINE_LENGTH = "commit.body-line-length"
|
|
20
|
+
COMMIT_BODY_BULLET = "commit.body-bullet"
|
|
21
|
+
COMMIT_MESSAGE_BYTES = "commit.message-bytes"
|
|
22
|
+
COMMIT_BREAKING_FOOTER = "commit.breaking-footer"
|
|
23
|
+
COMMIT_ATTRIBUTION = "commit.attribution"
|
|
24
|
+
BRANCH_FORMAT = "branch.format"
|
|
25
|
+
BRANCH_TYPE = "branch.type"
|
|
26
|
+
BRANCH_DESCRIPTION_EMPTY = "branch.description-empty"
|
|
27
|
+
BRANCH_DESCRIPTION_LENGTH = "branch.description-length"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclass(frozen=True, slots=True)
|
|
31
|
+
class Violation:
|
|
32
|
+
code: str
|
|
33
|
+
field: str
|
|
34
|
+
message: str
|
|
35
|
+
fix_hint: str
|
|
36
|
+
severity: Severity = Severity.ERROR
|
|
37
|
+
|
|
38
|
+
def to_dict(self) -> dict[str, str]:
|
|
39
|
+
return {
|
|
40
|
+
"code": self.code,
|
|
41
|
+
"field": self.field,
|
|
42
|
+
"message": self.message,
|
|
43
|
+
"fix_hint": self.fix_hint,
|
|
44
|
+
"severity": self.severity.value,
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
@dataclass(frozen=True, slots=True)
|
|
49
|
+
class Report:
|
|
50
|
+
violations: tuple[Violation, ...]
|
|
51
|
+
|
|
52
|
+
@property
|
|
53
|
+
def valid(self) -> bool:
|
|
54
|
+
return not any(v.severity is Severity.ERROR for v in self.violations)
|
|
55
|
+
|
|
56
|
+
@property
|
|
57
|
+
def errors(self) -> tuple[Violation, ...]:
|
|
58
|
+
return tuple(v for v in self.violations if v.severity is Severity.ERROR)
|
|
59
|
+
|
|
60
|
+
@property
|
|
61
|
+
def warnings(self) -> tuple[Violation, ...]:
|
|
62
|
+
return tuple(v for v in self.violations if v.severity is Severity.WARNING)
|
|
63
|
+
|
|
64
|
+
def to_dict(self) -> dict[str, object]:
|
|
65
|
+
return {
|
|
66
|
+
"valid": self.valid,
|
|
67
|
+
"violations": [v.to_dict() for v in self.violations],
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
@classmethod
|
|
71
|
+
def from_violations(cls, *violations: Violation) -> Report:
|
|
72
|
+
return cls(violations=tuple(violations))
|
|
73
|
+
|
|
74
|
+
def __bool__(self) -> bool:
|
|
75
|
+
return self.valid
|