lgit-cli 4.2.0__py3-none-any.whl → 4.3.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.
- lgit/__init__.py +1 -1
- lgit/changelog.py +513 -58
- lgit/compose.py +31 -9
- lgit/config.py +2 -0
- lgit/git.py +22 -8
- lgit/markdown_output.py +4 -0
- lgit/resources/prompts/changelog.md +46 -2
- lgit/templates.py +20 -2
- {lgit_cli-4.2.0.dist-info → lgit_cli-4.3.0.dist-info}/METADATA +1 -1
- {lgit_cli-4.2.0.dist-info → lgit_cli-4.3.0.dist-info}/RECORD +13 -13
- {lgit_cli-4.2.0.dist-info → lgit_cli-4.3.0.dist-info}/WHEEL +1 -1
- {lgit_cli-4.2.0.dist-info → lgit_cli-4.3.0.dist-info}/entry_points.txt +0 -0
- {lgit_cli-4.2.0.dist-info → lgit_cli-4.3.0.dist-info}/licenses/LICENSE +0 -0
lgit/__init__.py
CHANGED
lgit/changelog.py
CHANGED
|
@@ -4,21 +4,26 @@ from __future__ import annotations
|
|
|
4
4
|
|
|
5
5
|
import json
|
|
6
6
|
import os
|
|
7
|
-
|
|
8
|
-
from
|
|
9
|
-
from
|
|
7
|
+
import re
|
|
8
|
+
from collections.abc import Collection, Mapping, Sequence
|
|
9
|
+
from dataclasses import dataclass, replace
|
|
10
10
|
from pathlib import Path
|
|
11
11
|
from typing import TYPE_CHECKING, Any
|
|
12
12
|
|
|
13
|
+
from . import style
|
|
13
14
|
from .diffing import smart_truncate_diff
|
|
14
15
|
from .errors import GitError, ValidationFailure
|
|
15
16
|
from .git import run_git
|
|
16
17
|
from .markdown_output import parse_changelog_response
|
|
17
18
|
from .models import ChangelogCategory, resolve_model_name
|
|
19
|
+
from .templates import render_changelog_prompt
|
|
18
20
|
|
|
19
21
|
if TYPE_CHECKING:
|
|
20
22
|
from .config import CommitConfig
|
|
21
23
|
|
|
24
|
+
_REVISE_BLOCK_RE = re.compile(r"<revise\b[^>]*>(.*?)(?:</revise>|$)", re.IGNORECASE | re.DOTALL)
|
|
25
|
+
_REVISION_LINE_RE = re.compile(r"^\s*(OLD|NEW)\s*:(.*)$", re.IGNORECASE)
|
|
26
|
+
|
|
22
27
|
|
|
23
28
|
@dataclass(frozen=True, slots=True)
|
|
24
29
|
class UnreleasedSection:
|
|
@@ -30,6 +35,14 @@ class UnreleasedSection:
|
|
|
30
35
|
entries: dict[ChangelogCategory, list[str]]
|
|
31
36
|
|
|
32
37
|
|
|
38
|
+
@dataclass(frozen=True, slots=True)
|
|
39
|
+
class ChangelogRevision:
|
|
40
|
+
"""One reconcile operation that replaces or drops an existing Unreleased entry."""
|
|
41
|
+
|
|
42
|
+
old: str
|
|
43
|
+
new: str | None
|
|
44
|
+
|
|
45
|
+
|
|
33
46
|
@dataclass(frozen=True, slots=True)
|
|
34
47
|
class ChangelogBoundary:
|
|
35
48
|
"""A changelog and the staged files governed by it."""
|
|
@@ -84,21 +97,51 @@ async def run_changelog_flow(args: Any, config: CommitConfig) -> list[ChangelogB
|
|
|
84
97
|
except ValidationFailure:
|
|
85
98
|
continue
|
|
86
99
|
|
|
87
|
-
|
|
100
|
+
head_unreleased = _head_unreleased(rel_path, boundary.changelog_path, repo_dir)
|
|
101
|
+
if head_unreleased is None:
|
|
102
|
+
existing_entries = _format_existing_entries(unreleased)
|
|
103
|
+
authored_entries = None
|
|
104
|
+
else:
|
|
105
|
+
existing_entries = _format_entry_map(head_unreleased.entries)
|
|
106
|
+
authored_entries = _format_entry_map(_entries_added_since(head_unreleased, unreleased, worktree_unreleased))
|
|
107
|
+
|
|
108
|
+
can_revise = bool(config.changelog_revise) and head_unreleased is not None and bool(existing_entries)
|
|
109
|
+
|
|
110
|
+
entries, revisions = await generate_changelog_entries(
|
|
88
111
|
boundary.changelog_path,
|
|
89
112
|
_is_package_changelog(boundary.changelog_path, repo_dir),
|
|
90
113
|
stat,
|
|
91
114
|
diff,
|
|
92
|
-
|
|
115
|
+
existing_entries,
|
|
116
|
+
authored_entries,
|
|
117
|
+
can_revise,
|
|
93
118
|
config,
|
|
94
119
|
)
|
|
95
|
-
|
|
120
|
+
staged_entries = unreleased.entries
|
|
121
|
+
worktree_entries = worktree_unreleased.entries if worktree_unreleased is not None else None
|
|
122
|
+
applied: list[ChangelogRevision] = []
|
|
123
|
+
if can_revise:
|
|
124
|
+
assert head_unreleased is not None
|
|
125
|
+
revisable = {entry.casefold() for values in head_unreleased.entries.values() for entry in values}
|
|
126
|
+
staged_entries, applied = apply_revisions(staged_entries, revisions, revisable)
|
|
127
|
+
if worktree_entries is not None:
|
|
128
|
+
worktree_entries, _ = apply_revisions(worktree_entries, revisions, revisable)
|
|
129
|
+
entries = _drop_duplicate_entries(entries, staged_entries, worktree_entries)
|
|
130
|
+
if not entries and not applied:
|
|
96
131
|
continue
|
|
97
132
|
|
|
98
|
-
updated_staged = write_entries(
|
|
133
|
+
updated_staged = write_entries(
|
|
134
|
+
changelog_content,
|
|
135
|
+
replace(unreleased, entries=staged_entries),
|
|
136
|
+
entries,
|
|
137
|
+
)
|
|
99
138
|
updated_worktree = (
|
|
100
|
-
write_entries(
|
|
101
|
-
|
|
139
|
+
write_entries(
|
|
140
|
+
worktree_content,
|
|
141
|
+
replace(worktree_unreleased, entries=worktree_entries),
|
|
142
|
+
entries,
|
|
143
|
+
)
|
|
144
|
+
if worktree_unreleased is not None and worktree_entries is not None
|
|
102
145
|
else updated_staged
|
|
103
146
|
)
|
|
104
147
|
boundary.changelog_path.write_text(updated_worktree, encoding="utf-8")
|
|
@@ -107,6 +150,7 @@ async def run_changelog_flow(args: Any, config: CommitConfig) -> list[ChangelogB
|
|
|
107
150
|
stage_changelog_blob(rel_path, updated_staged, repo_dir)
|
|
108
151
|
else:
|
|
109
152
|
untracked_to_stage.append(rel_path)
|
|
153
|
+
_report_applied_revisions(applied)
|
|
110
154
|
updated.append(ChangelogBoundary(boundary.changelog_path, boundary.files, diff, stat))
|
|
111
155
|
|
|
112
156
|
if untracked_to_stage:
|
|
@@ -114,6 +158,256 @@ async def run_changelog_flow(args: Any, config: CommitConfig) -> list[ChangelogB
|
|
|
114
158
|
return updated
|
|
115
159
|
|
|
116
160
|
|
|
161
|
+
@dataclass(slots=True)
|
|
162
|
+
class _ComposeBoundaryState:
|
|
163
|
+
rel_path: str
|
|
164
|
+
changelog_path: Path
|
|
165
|
+
is_package: bool
|
|
166
|
+
original_head_oid: str | None
|
|
167
|
+
target_mode: str
|
|
168
|
+
target_oid: str
|
|
169
|
+
target_content: str
|
|
170
|
+
head_unreleased: UnreleasedSection
|
|
171
|
+
authored: dict[ChangelogCategory, list[str]]
|
|
172
|
+
chain_content: str
|
|
173
|
+
worktree_content: str
|
|
174
|
+
last_written_worktree: str
|
|
175
|
+
staged_index_oid: str
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
@dataclass(slots=True)
|
|
179
|
+
class ChangelogWeaver:
|
|
180
|
+
"""Weave generated changelog entries into each commit of one compose run."""
|
|
181
|
+
|
|
182
|
+
boundaries: list[_ComposeBoundaryState]
|
|
183
|
+
config: CommitConfig
|
|
184
|
+
|
|
185
|
+
@classmethod
|
|
186
|
+
def create(
|
|
187
|
+
cls,
|
|
188
|
+
repo_dir: str | os.PathLike[str],
|
|
189
|
+
config: CommitConfig,
|
|
190
|
+
target_tree: str,
|
|
191
|
+
) -> ChangelogWeaver | None:
|
|
192
|
+
"""Claim parseable changelogs from the compose target tree."""
|
|
193
|
+
|
|
194
|
+
if not config.changelog_enabled:
|
|
195
|
+
return None
|
|
196
|
+
|
|
197
|
+
repo_path = Path(repo_dir)
|
|
198
|
+
boundaries: list[_ComposeBoundaryState] = []
|
|
199
|
+
for path in _find_changelogs(repo_path):
|
|
200
|
+
rel_path = _relative_to(path, repo_path)
|
|
201
|
+
target_entry = run_git(["ls-tree", target_tree, "--", rel_path], cwd=repo_path).stdout
|
|
202
|
+
target_fields = target_entry.split(maxsplit=3)
|
|
203
|
+
if len(target_fields) < 3:
|
|
204
|
+
continue
|
|
205
|
+
target_mode, target_oid = target_fields[0], target_fields[2]
|
|
206
|
+
target_content = run_git(["show", f"{target_tree}:{rel_path}"], cwd=repo_path).stdout
|
|
207
|
+
|
|
208
|
+
head_result = run_git(["rev-parse", f"HEAD:{rel_path}"], cwd=repo_path, check=False)
|
|
209
|
+
original_head_oid = head_result.stdout.strip() if head_result.returncode == 0 else None
|
|
210
|
+
head_unreleased = _head_unreleased(rel_path, path, repo_path)
|
|
211
|
+
if head_unreleased is None:
|
|
212
|
+
continue
|
|
213
|
+
|
|
214
|
+
try:
|
|
215
|
+
target_unreleased = parse_unreleased_section(target_content, path)
|
|
216
|
+
worktree_content = path.read_text(encoding="utf-8")
|
|
217
|
+
worktree_unreleased = parse_unreleased_section(worktree_content, path)
|
|
218
|
+
except OSError, UnicodeError, ValidationFailure:
|
|
219
|
+
continue
|
|
220
|
+
|
|
221
|
+
authored = _entries_added_since(
|
|
222
|
+
head_unreleased,
|
|
223
|
+
target_unreleased,
|
|
224
|
+
worktree_unreleased if worktree_content != target_content else None,
|
|
225
|
+
)
|
|
226
|
+
boundaries.append(
|
|
227
|
+
_ComposeBoundaryState(
|
|
228
|
+
rel_path=rel_path,
|
|
229
|
+
changelog_path=path,
|
|
230
|
+
is_package=_is_package_changelog(path, repo_path),
|
|
231
|
+
original_head_oid=original_head_oid,
|
|
232
|
+
target_mode=target_mode,
|
|
233
|
+
target_oid=target_oid,
|
|
234
|
+
target_content=target_content,
|
|
235
|
+
head_unreleased=head_unreleased,
|
|
236
|
+
authored=authored,
|
|
237
|
+
chain_content=target_content,
|
|
238
|
+
worktree_content=worktree_content,
|
|
239
|
+
last_written_worktree=worktree_content,
|
|
240
|
+
staged_index_oid=target_oid,
|
|
241
|
+
)
|
|
242
|
+
)
|
|
243
|
+
|
|
244
|
+
return cls(boundaries, config) if boundaries else None
|
|
245
|
+
|
|
246
|
+
def exclude_pathspecs(self) -> list[str]:
|
|
247
|
+
"""Return root-anchored pathspecs for changelogs owned by this run."""
|
|
248
|
+
|
|
249
|
+
return [f":(exclude,top){boundary.rel_path}" for boundary in self.boundaries]
|
|
250
|
+
|
|
251
|
+
def seed_temp_index(
|
|
252
|
+
self,
|
|
253
|
+
repo_dir: str | os.PathLike[str],
|
|
254
|
+
index_file: str | os.PathLike[str],
|
|
255
|
+
) -> None:
|
|
256
|
+
"""Seed authored target changelog blobs unless an earlier round already wove them."""
|
|
257
|
+
|
|
258
|
+
for state in self.boundaries:
|
|
259
|
+
current_result = run_git(
|
|
260
|
+
["rev-parse", f"HEAD:{state.rel_path}"],
|
|
261
|
+
cwd=repo_dir,
|
|
262
|
+
check=False,
|
|
263
|
+
)
|
|
264
|
+
current_oid = current_result.stdout.strip() if current_result.returncode == 0 else None
|
|
265
|
+
if current_oid == state.original_head_oid:
|
|
266
|
+
run_git(
|
|
267
|
+
[
|
|
268
|
+
"update-index",
|
|
269
|
+
"--add",
|
|
270
|
+
"--cacheinfo",
|
|
271
|
+
f"{state.target_mode},{state.target_oid},{state.rel_path}",
|
|
272
|
+
],
|
|
273
|
+
cwd=repo_dir,
|
|
274
|
+
index_file=index_file,
|
|
275
|
+
)
|
|
276
|
+
state.chain_content = state.target_content
|
|
277
|
+
else:
|
|
278
|
+
state.chain_content = run_git(
|
|
279
|
+
["show", f"HEAD:{state.rel_path}"],
|
|
280
|
+
cwd=repo_dir,
|
|
281
|
+
).stdout
|
|
282
|
+
|
|
283
|
+
async def weave_group(
|
|
284
|
+
self,
|
|
285
|
+
files: Sequence[str],
|
|
286
|
+
diff: str,
|
|
287
|
+
stat: str,
|
|
288
|
+
repo_dir: str | os.PathLike[str],
|
|
289
|
+
index_file: str | os.PathLike[str],
|
|
290
|
+
) -> None:
|
|
291
|
+
"""Generate, reconcile, and stage changelog entries for one compose group."""
|
|
292
|
+
|
|
293
|
+
matched = detect_boundaries(
|
|
294
|
+
files,
|
|
295
|
+
[state.changelog_path for state in self.boundaries],
|
|
296
|
+
repo_dir,
|
|
297
|
+
)
|
|
298
|
+
states = {state.changelog_path: state for state in self.boundaries}
|
|
299
|
+
group_diff = (
|
|
300
|
+
smart_truncate_diff(diff, self.config.max_diff_length, self.config)
|
|
301
|
+
if len(diff) > self.config.max_diff_length
|
|
302
|
+
else diff
|
|
303
|
+
)
|
|
304
|
+
for boundary in matched:
|
|
305
|
+
state = states[boundary.changelog_path]
|
|
306
|
+
try:
|
|
307
|
+
parent_unreleased = parse_unreleased_section(state.chain_content, state.changelog_path)
|
|
308
|
+
except ValidationFailure:
|
|
309
|
+
continue
|
|
310
|
+
|
|
311
|
+
authored_casefolds = {entry.casefold() for values in state.authored.values() for entry in values}
|
|
312
|
+
existing_map: dict[ChangelogCategory, list[str]] = {}
|
|
313
|
+
for category, values in parent_unreleased.entries.items():
|
|
314
|
+
existing = [entry for entry in values if entry.casefold() not in authored_casefolds]
|
|
315
|
+
if existing:
|
|
316
|
+
existing_map[category] = existing
|
|
317
|
+
existing_entries = _format_entry_map(existing_map)
|
|
318
|
+
authored_entries = _format_entry_map(state.authored)
|
|
319
|
+
can_revise = bool(self.config.changelog_revise) and bool(existing_entries)
|
|
320
|
+
|
|
321
|
+
entries, revisions = await generate_changelog_entries(
|
|
322
|
+
state.changelog_path,
|
|
323
|
+
state.is_package,
|
|
324
|
+
stat,
|
|
325
|
+
group_diff,
|
|
326
|
+
existing_entries,
|
|
327
|
+
authored_entries,
|
|
328
|
+
can_revise,
|
|
329
|
+
self.config,
|
|
330
|
+
)
|
|
331
|
+
revisable = {entry.casefold() for values in existing_map.values() for entry in values}
|
|
332
|
+
if can_revise:
|
|
333
|
+
revised_map, applied = apply_revisions(
|
|
334
|
+
parent_unreleased.entries,
|
|
335
|
+
revisions,
|
|
336
|
+
revisable,
|
|
337
|
+
)
|
|
338
|
+
else:
|
|
339
|
+
revised_map = {category: list(values) for category, values in parent_unreleased.entries.items()}
|
|
340
|
+
applied = []
|
|
341
|
+
|
|
342
|
+
shadow_unreleased = parse_unreleased_section(
|
|
343
|
+
state.worktree_content,
|
|
344
|
+
state.changelog_path,
|
|
345
|
+
)
|
|
346
|
+
if can_revise:
|
|
347
|
+
shadow_revised_map, _ = apply_revisions(
|
|
348
|
+
shadow_unreleased.entries,
|
|
349
|
+
revisions,
|
|
350
|
+
revisable,
|
|
351
|
+
)
|
|
352
|
+
else:
|
|
353
|
+
shadow_revised_map = {category: list(values) for category, values in shadow_unreleased.entries.items()}
|
|
354
|
+
|
|
355
|
+
entries = _drop_duplicate_entries(entries, revised_map, shadow_revised_map)
|
|
356
|
+
if not entries and not applied:
|
|
357
|
+
continue
|
|
358
|
+
|
|
359
|
+
state.chain_content = write_entries(
|
|
360
|
+
state.chain_content,
|
|
361
|
+
replace(parent_unreleased, entries=revised_map),
|
|
362
|
+
entries,
|
|
363
|
+
)
|
|
364
|
+
state.worktree_content = write_entries(
|
|
365
|
+
state.worktree_content,
|
|
366
|
+
replace(shadow_unreleased, entries=shadow_revised_map),
|
|
367
|
+
entries,
|
|
368
|
+
)
|
|
369
|
+
stage_changelog_blob(
|
|
370
|
+
state.rel_path,
|
|
371
|
+
state.chain_content,
|
|
372
|
+
repo_dir,
|
|
373
|
+
index_file=index_file,
|
|
374
|
+
)
|
|
375
|
+
_report_applied_revisions(applied)
|
|
376
|
+
|
|
377
|
+
def flush(self, repo_dir: str | os.PathLike[str]) -> None:
|
|
378
|
+
"""Publish woven content unless the user changed the worktree or index mid-run."""
|
|
379
|
+
|
|
380
|
+
for state in self.boundaries:
|
|
381
|
+
try:
|
|
382
|
+
current_worktree = state.changelog_path.read_text(encoding="utf-8")
|
|
383
|
+
except OSError, UnicodeError:
|
|
384
|
+
current_worktree = None
|
|
385
|
+
if current_worktree == state.last_written_worktree:
|
|
386
|
+
if current_worktree != state.worktree_content:
|
|
387
|
+
state.changelog_path.write_text(state.worktree_content, encoding="utf-8")
|
|
388
|
+
state.last_written_worktree = state.worktree_content
|
|
389
|
+
else:
|
|
390
|
+
style.status(
|
|
391
|
+
f"{style.info('›')} "
|
|
392
|
+
f"{style.dim('changelog: worktree edited mid-run; left untouched:')} "
|
|
393
|
+
f"{state.rel_path}"
|
|
394
|
+
)
|
|
395
|
+
|
|
396
|
+
current_index_oid = _staged_changelog_oid(state.rel_path, repo_dir)
|
|
397
|
+
if current_index_oid == state.staged_index_oid:
|
|
398
|
+
state.staged_index_oid = stage_changelog_blob(
|
|
399
|
+
state.rel_path,
|
|
400
|
+
state.chain_content,
|
|
401
|
+
repo_dir,
|
|
402
|
+
)
|
|
403
|
+
else:
|
|
404
|
+
style.status(
|
|
405
|
+
f"{style.info('›')} "
|
|
406
|
+
f"{style.dim('changelog: index staged mid-run; left untouched:')} "
|
|
407
|
+
f"{state.rel_path}"
|
|
408
|
+
)
|
|
409
|
+
|
|
410
|
+
|
|
117
411
|
def parse_unreleased_section(content: str, path: str | os.PathLike[str] = "CHANGELOG.md") -> UnreleasedSection:
|
|
118
412
|
"""Parse the `[Unreleased]` section boundaries and existing entries."""
|
|
119
413
|
|
|
@@ -157,11 +451,9 @@ def write_entries(
|
|
|
157
451
|
unreleased: UnreleasedSection,
|
|
158
452
|
new_entries: Mapping[ChangelogCategory, Sequence[str]] | Mapping[str, Sequence[str]],
|
|
159
453
|
) -> str:
|
|
160
|
-
"""
|
|
454
|
+
"""Rebuild the Unreleased section with new entries before existing entries."""
|
|
161
455
|
|
|
162
456
|
normalized_new = _coerce_entries(new_entries)
|
|
163
|
-
if not normalized_new:
|
|
164
|
-
return content
|
|
165
457
|
|
|
166
458
|
lines = content.split("\n")
|
|
167
459
|
result: list[str] = list(lines[: unreleased.header_line + 1])
|
|
@@ -177,8 +469,6 @@ def write_entries(
|
|
|
177
469
|
result.append(f"### {category.value}")
|
|
178
470
|
result.append("")
|
|
179
471
|
result.extend(fresh)
|
|
180
|
-
if fresh and old:
|
|
181
|
-
result.append("")
|
|
182
472
|
result.extend(old)
|
|
183
473
|
result.append("")
|
|
184
474
|
|
|
@@ -195,15 +485,25 @@ def stage_changelog_blob(
|
|
|
195
485
|
rel_path: str | os.PathLike[str],
|
|
196
486
|
content: str,
|
|
197
487
|
dir: str | os.PathLike[str] = ".",
|
|
488
|
+
index_file: str | os.PathLike[str] | None = None,
|
|
198
489
|
) -> str:
|
|
199
490
|
"""Stage exact changelog content as an index blob without git-adding the worktree copy."""
|
|
200
491
|
|
|
201
492
|
path_text = os.fspath(rel_path)
|
|
202
|
-
oid = run_git(
|
|
493
|
+
oid = run_git(
|
|
494
|
+
["hash-object", "-w", "--stdin"],
|
|
495
|
+
cwd=dir,
|
|
496
|
+
input_text=content,
|
|
497
|
+
index_file=index_file,
|
|
498
|
+
).stdout.strip()
|
|
203
499
|
if not oid:
|
|
204
500
|
raise GitError(f"git hash-object returned no oid for {path_text}")
|
|
205
|
-
mode = _staged_changelog_mode(path_text, dir)
|
|
206
|
-
run_git(
|
|
501
|
+
mode = _staged_changelog_mode(path_text, dir, index_file=index_file)
|
|
502
|
+
run_git(
|
|
503
|
+
["update-index", "--add", "--cacheinfo", f"{mode},{oid},{path_text}"],
|
|
504
|
+
cwd=dir,
|
|
505
|
+
index_file=index_file,
|
|
506
|
+
)
|
|
207
507
|
return oid
|
|
208
508
|
|
|
209
509
|
|
|
@@ -257,19 +557,20 @@ async def generate_changelog_entries(
|
|
|
257
557
|
stat: str,
|
|
258
558
|
diff: str,
|
|
259
559
|
existing_entries: str | None,
|
|
560
|
+
authored_entries: str | None,
|
|
561
|
+
can_revise: bool,
|
|
260
562
|
config: CommitConfig,
|
|
261
|
-
) -> dict[ChangelogCategory, list[str]]:
|
|
262
|
-
"""Ask the configured model for Keep a Changelog entries."""
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
},
|
|
563
|
+
) -> tuple[dict[ChangelogCategory, list[str]], list[ChangelogRevision]]:
|
|
564
|
+
"""Ask the configured model for Keep a Changelog entries and revision operations."""
|
|
565
|
+
|
|
566
|
+
prompt = render_changelog_prompt(
|
|
567
|
+
os.fspath(changelog_path),
|
|
568
|
+
is_package_changelog,
|
|
569
|
+
stat,
|
|
570
|
+
diff,
|
|
571
|
+
existing_entries=existing_entries,
|
|
572
|
+
authored_entries=authored_entries,
|
|
573
|
+
can_revise=can_revise,
|
|
273
574
|
)
|
|
274
575
|
from .api import OneShotSpec, run_oneshot
|
|
275
576
|
|
|
@@ -279,8 +580,8 @@ async def generate_changelog_entries(
|
|
|
279
580
|
operation="changelog",
|
|
280
581
|
model=resolve_model_name(config.analysis_model),
|
|
281
582
|
prompt_family="changelog",
|
|
282
|
-
system_prompt=
|
|
283
|
-
user_prompt=
|
|
583
|
+
system_prompt=prompt.system,
|
|
584
|
+
user_prompt=prompt.user,
|
|
284
585
|
tool_name="create_changelog_entries",
|
|
285
586
|
progress_label="changelog",
|
|
286
587
|
cacheable=True,
|
|
@@ -289,7 +590,8 @@ async def generate_changelog_entries(
|
|
|
289
590
|
output = response.output if hasattr(response, "output") else response
|
|
290
591
|
payload = _parse_jsonish(output)
|
|
291
592
|
entries = payload.get("entries", payload) if isinstance(payload, dict) else {}
|
|
292
|
-
|
|
593
|
+
revisions = parse_changelog_revisions(response.text_content or "") if can_revise else []
|
|
594
|
+
return _coerce_entries(entries if isinstance(entries, Mapping) else {}), revisions
|
|
293
595
|
|
|
294
596
|
|
|
295
597
|
def normalize_changelog_entry(entry: str) -> str | None:
|
|
@@ -301,6 +603,28 @@ def normalize_changelog_entry(entry: str) -> str | None:
|
|
|
301
603
|
return f"- {stripped}" if stripped else None
|
|
302
604
|
|
|
303
605
|
|
|
606
|
+
def parse_changelog_revisions(text: str) -> list[ChangelogRevision]:
|
|
607
|
+
"""Parse revision pairs from the first ``<revise>`` block."""
|
|
608
|
+
|
|
609
|
+
block = _REVISE_BLOCK_RE.search(text)
|
|
610
|
+
if block is None:
|
|
611
|
+
return []
|
|
612
|
+
|
|
613
|
+
revisions: list[ChangelogRevision] = []
|
|
614
|
+
pending_old: str | None = None
|
|
615
|
+
for line in block.group(1).splitlines():
|
|
616
|
+
match = _REVISION_LINE_RE.fullmatch(line)
|
|
617
|
+
if match is None:
|
|
618
|
+
continue
|
|
619
|
+
prefix, entry = match.groups()
|
|
620
|
+
if prefix.casefold() == "old":
|
|
621
|
+
pending_old = normalize_changelog_entry(entry)
|
|
622
|
+
elif pending_old is not None:
|
|
623
|
+
revisions.append(ChangelogRevision(pending_old, normalize_changelog_entry(entry)))
|
|
624
|
+
pending_old = None
|
|
625
|
+
return revisions
|
|
626
|
+
|
|
627
|
+
|
|
304
628
|
def _staged_files(dir: Path) -> list[str]:
|
|
305
629
|
stdout = run_git(["diff", "--cached", "--name-only"], cwd=dir).stdout
|
|
306
630
|
return [line for line in stdout.splitlines() if line]
|
|
@@ -310,10 +634,16 @@ def _find_changelogs(dir: Path) -> list[Path]:
|
|
|
310
634
|
paths = {
|
|
311
635
|
dir / path
|
|
312
636
|
for path in run_git(["ls-files", "--", "CHANGELOG.md", "**/CHANGELOG.md"], cwd=dir).stdout.splitlines()
|
|
637
|
+
if Path(path).name == "CHANGELOG.md"
|
|
313
638
|
}
|
|
314
639
|
for path in dir.rglob("CHANGELOG.md"):
|
|
315
|
-
if ".git"
|
|
316
|
-
|
|
640
|
+
if ".git" in path.parts:
|
|
641
|
+
continue
|
|
642
|
+
try:
|
|
643
|
+
exact_path = next(child for child in path.parent.iterdir() if child.name == "CHANGELOG.md")
|
|
644
|
+
except OSError, StopIteration:
|
|
645
|
+
continue
|
|
646
|
+
paths.add(exact_path)
|
|
317
647
|
return sorted(paths, key=lambda path: _relative_to(path, dir))
|
|
318
648
|
|
|
319
649
|
|
|
@@ -322,11 +652,146 @@ def _staged_changelog_content(rel_path: str, dir: str | os.PathLike[str]) -> str
|
|
|
322
652
|
return result.stdout if result.returncode == 0 else None
|
|
323
653
|
|
|
324
654
|
|
|
325
|
-
def _staged_changelog_mode(
|
|
326
|
-
|
|
655
|
+
def _staged_changelog_mode(
|
|
656
|
+
rel_path: str,
|
|
657
|
+
dir: str | os.PathLike[str],
|
|
658
|
+
index_file: str | os.PathLike[str] | None = None,
|
|
659
|
+
) -> str:
|
|
660
|
+
result = run_git(
|
|
661
|
+
["ls-files", "-s", "--", rel_path],
|
|
662
|
+
cwd=dir,
|
|
663
|
+
index_file=index_file,
|
|
664
|
+
).stdout.strip()
|
|
327
665
|
return result.split(maxsplit=1)[0] if result else "100644"
|
|
328
666
|
|
|
329
667
|
|
|
668
|
+
def _staged_changelog_oid(rel_path: str, dir: str | os.PathLike[str]) -> str | None:
|
|
669
|
+
result = run_git(["ls-files", "-s", "--", rel_path], cwd=dir).stdout.strip()
|
|
670
|
+
fields = result.split(maxsplit=3)
|
|
671
|
+
return fields[1] if len(fields) >= 2 else None
|
|
672
|
+
|
|
673
|
+
|
|
674
|
+
def _head_unreleased(
|
|
675
|
+
rel_path: str,
|
|
676
|
+
path: str | os.PathLike[str],
|
|
677
|
+
dir: str | os.PathLike[str],
|
|
678
|
+
) -> UnreleasedSection | None:
|
|
679
|
+
"""Parse HEAD's copy of the changelog.
|
|
680
|
+
|
|
681
|
+
Returns an empty section when the file is absent from HEAD (every current
|
|
682
|
+
entry was authored in this change), or None when HEAD's copy is unparseable.
|
|
683
|
+
"""
|
|
684
|
+
result = run_git(["show", f"HEAD:{rel_path}"], cwd=dir, check=False, allow_exit_codes=(128,))
|
|
685
|
+
if result.returncode != 0:
|
|
686
|
+
return UnreleasedSection(Path(path), 0, 0, {})
|
|
687
|
+
try:
|
|
688
|
+
return parse_unreleased_section(result.stdout, path)
|
|
689
|
+
except ValidationFailure:
|
|
690
|
+
return None
|
|
691
|
+
|
|
692
|
+
|
|
693
|
+
def _entries_added_since(
|
|
694
|
+
head: UnreleasedSection,
|
|
695
|
+
*sections: UnreleasedSection | None,
|
|
696
|
+
) -> dict[ChangelogCategory, list[str]]:
|
|
697
|
+
"""Unreleased entries present now but absent from HEAD — hand-written for this change."""
|
|
698
|
+
baseline = {entry.casefold() for values in head.entries.values() for entry in values}
|
|
699
|
+
added: dict[ChangelogCategory, list[str]] = {}
|
|
700
|
+
for section in sections:
|
|
701
|
+
if section is None:
|
|
702
|
+
continue
|
|
703
|
+
for category, values in section.entries.items():
|
|
704
|
+
bucket = added.setdefault(category, [])
|
|
705
|
+
for entry in values:
|
|
706
|
+
if entry.casefold() not in baseline and entry not in bucket:
|
|
707
|
+
bucket.append(entry)
|
|
708
|
+
return {category: values for category, values in added.items() if values}
|
|
709
|
+
|
|
710
|
+
|
|
711
|
+
def apply_revisions(
|
|
712
|
+
entries: Mapping[ChangelogCategory, Sequence[str]],
|
|
713
|
+
revisions: Sequence[ChangelogRevision],
|
|
714
|
+
revisable: Collection[str],
|
|
715
|
+
) -> tuple[dict[ChangelogCategory, list[str]], list[ChangelogRevision]]:
|
|
716
|
+
"""Apply safe reconciliation operations and return the operations that matched."""
|
|
717
|
+
|
|
718
|
+
revised = {category: list(values) for category, values in entries.items()}
|
|
719
|
+
applied: list[ChangelogRevision] = []
|
|
720
|
+
for revision in revisions:
|
|
721
|
+
target = revision.old.casefold()
|
|
722
|
+
if target not in revisable:
|
|
723
|
+
continue
|
|
724
|
+
matched = False
|
|
725
|
+
for values in revised.values():
|
|
726
|
+
for index, entry in enumerate(values):
|
|
727
|
+
if entry.casefold() != target:
|
|
728
|
+
continue
|
|
729
|
+
if revision.new is None:
|
|
730
|
+
values.pop(index)
|
|
731
|
+
else:
|
|
732
|
+
values[index] = revision.new
|
|
733
|
+
applied.append(revision)
|
|
734
|
+
matched = True
|
|
735
|
+
break
|
|
736
|
+
if matched:
|
|
737
|
+
break
|
|
738
|
+
return revised, applied
|
|
739
|
+
|
|
740
|
+
|
|
741
|
+
def _report_applied_revisions(applied: Sequence[ChangelogRevision]) -> None:
|
|
742
|
+
for revision in applied:
|
|
743
|
+
if revision.new is None:
|
|
744
|
+
style.status(f"{style.info('›')} {style.dim('changelog: dropped:')} {style.dim(revision.old)}")
|
|
745
|
+
else:
|
|
746
|
+
style.status(
|
|
747
|
+
f"{style.info('›')} {style.dim('changelog: revised:')} "
|
|
748
|
+
f"{style.dim(revision.old)} {style.dim('→')} {revision.new}"
|
|
749
|
+
)
|
|
750
|
+
|
|
751
|
+
|
|
752
|
+
def _drop_duplicate_entries(
|
|
753
|
+
entries: Mapping[ChangelogCategory, Sequence[str]],
|
|
754
|
+
*existing: Mapping[ChangelogCategory, Sequence[str]] | None,
|
|
755
|
+
) -> dict[ChangelogCategory, list[str]]:
|
|
756
|
+
"""Drop generated entries that restate ones already in the Unreleased section.
|
|
757
|
+
|
|
758
|
+
Catches verbatim repeats and near-duplicates (most of the entry's content
|
|
759
|
+
words already appear in a single existing entry, e.g. a reworded or
|
|
760
|
+
recategorized copy of a hand-written line).
|
|
761
|
+
"""
|
|
762
|
+
existing_words = [
|
|
763
|
+
_entry_words(entry)
|
|
764
|
+
for entry_map in existing
|
|
765
|
+
if entry_map is not None
|
|
766
|
+
for values in entry_map.values()
|
|
767
|
+
for entry in values
|
|
768
|
+
]
|
|
769
|
+
|
|
770
|
+
def is_duplicate(entry: str) -> bool:
|
|
771
|
+
words = _entry_words(entry)
|
|
772
|
+
if not words:
|
|
773
|
+
return True
|
|
774
|
+
return any(len(words & seen) / len(words) >= 0.7 for seen in existing_words)
|
|
775
|
+
|
|
776
|
+
deduped: dict[ChangelogCategory, list[str]] = {}
|
|
777
|
+
for category, values in entries.items():
|
|
778
|
+
fresh = [value for value in values if not is_duplicate(value)]
|
|
779
|
+
if fresh:
|
|
780
|
+
deduped[category] = fresh
|
|
781
|
+
return deduped
|
|
782
|
+
|
|
783
|
+
|
|
784
|
+
_ENTRY_STOPWORDS = frozenset(
|
|
785
|
+
"the and for was were now not that this with from are has had have been its when then than into also".split()
|
|
786
|
+
)
|
|
787
|
+
|
|
788
|
+
|
|
789
|
+
def _entry_words(entry: str) -> set[str]:
|
|
790
|
+
"""Significant, lightly-stemmed content words of a changelog bullet."""
|
|
791
|
+
words = re.findall(r"[a-z0-9]+", entry.casefold())
|
|
792
|
+
return {word.removesuffix("s") for word in words if len(word) > 2 and word not in _ENTRY_STOPWORDS}
|
|
793
|
+
|
|
794
|
+
|
|
330
795
|
def _diff_for_files(files: Sequence[str], dir: Path, max_len: int) -> str:
|
|
331
796
|
if not files:
|
|
332
797
|
return ""
|
|
@@ -365,18 +830,22 @@ def _coerce_entries(
|
|
|
365
830
|
return coerced
|
|
366
831
|
|
|
367
832
|
|
|
368
|
-
def
|
|
833
|
+
def _format_entry_map(entries: Mapping[ChangelogCategory, Sequence[str]]) -> str | None:
|
|
369
834
|
lines: list[str] = []
|
|
370
835
|
for category in ChangelogCategory.render_order():
|
|
371
|
-
|
|
372
|
-
if not
|
|
836
|
+
values = entries.get(category, [])
|
|
837
|
+
if not values:
|
|
373
838
|
continue
|
|
374
839
|
lines.append(f"### {category.value}")
|
|
375
|
-
lines.extend(
|
|
840
|
+
lines.extend(values)
|
|
376
841
|
lines.append("")
|
|
377
842
|
return "\n".join(lines).strip() or None
|
|
378
843
|
|
|
379
844
|
|
|
845
|
+
def _format_existing_entries(unreleased: UnreleasedSection) -> str | None:
|
|
846
|
+
return _format_entry_map(unreleased.entries)
|
|
847
|
+
|
|
848
|
+
|
|
380
849
|
def _relative_to(path: Path, dir: Path | str | os.PathLike[str]) -> str:
|
|
381
850
|
repo_dir = Path(dir)
|
|
382
851
|
try:
|
|
@@ -393,24 +862,6 @@ def _is_package_changelog(path: Path, dir: Path) -> bool:
|
|
|
393
862
|
return parent != dir
|
|
394
863
|
|
|
395
864
|
|
|
396
|
-
def _render_prompt(family: str, values: Mapping[str, Any]) -> tuple[str, str]:
|
|
397
|
-
text = (resources.files("lgit.resources") / "prompts" / f"{family}.md").read_text(encoding="utf-8")
|
|
398
|
-
try:
|
|
399
|
-
from jinja2 import Template
|
|
400
|
-
|
|
401
|
-
text = Template(text).render(**values)
|
|
402
|
-
except Exception:
|
|
403
|
-
for key, value in values.items():
|
|
404
|
-
text = text.replace("{{ " + key + " }}", str(value))
|
|
405
|
-
text = text.replace("{{" + key + "}}", str(value))
|
|
406
|
-
marker = "<!-- USER -->"
|
|
407
|
-
if marker in text:
|
|
408
|
-
system, user = text.split(marker, 1)
|
|
409
|
-
else:
|
|
410
|
-
system, user = text, ""
|
|
411
|
-
return system.strip(), user.strip()
|
|
412
|
-
|
|
413
|
-
|
|
414
865
|
def _parse_jsonish(value: Any) -> Any:
|
|
415
866
|
"""Coerce a model changelog response into a mapping.
|
|
416
867
|
|
|
@@ -439,10 +890,14 @@ def _parse_jsonish(value: Any) -> Any:
|
|
|
439
890
|
|
|
440
891
|
__all__ = [
|
|
441
892
|
"ChangelogBoundary",
|
|
893
|
+
"ChangelogWeaver",
|
|
894
|
+
"ChangelogRevision",
|
|
442
895
|
"UnreleasedSection",
|
|
896
|
+
"apply_revisions",
|
|
443
897
|
"detect_boundaries",
|
|
444
898
|
"generate_changelog_entries",
|
|
445
899
|
"normalize_changelog_entry",
|
|
900
|
+
"parse_changelog_revisions",
|
|
446
901
|
"parse_unreleased_section",
|
|
447
902
|
"run_changelog_flow",
|
|
448
903
|
"stage_changelog_blob",
|
lgit/compose.py
CHANGED
|
@@ -15,6 +15,7 @@ from types import SimpleNamespace
|
|
|
15
15
|
from typing import TYPE_CHECKING, Any, Protocol
|
|
16
16
|
|
|
17
17
|
from . import api, diffing, git, map_reduce, style, templates, tokens
|
|
18
|
+
from .changelog import ChangelogWeaver
|
|
18
19
|
from .errors import GitError, NoChanges, ValidationFailure
|
|
19
20
|
from .models import (
|
|
20
21
|
CommitSummary,
|
|
@@ -284,15 +285,17 @@ async def run_compose_mode(args: _ComposeArgs, config: CommitConfig) -> list[str
|
|
|
284
285
|
"""
|
|
285
286
|
repo_dir = args.dir
|
|
286
287
|
target_tree = git.write_real_index_tree(repo_dir)
|
|
288
|
+
weaver = ChangelogWeaver.create(repo_dir, config, target_tree)
|
|
289
|
+
exclude = weaver.exclude_pathspecs() if weaver else ()
|
|
287
290
|
all_hashes: list[str] = []
|
|
288
291
|
round_number = 1
|
|
289
292
|
while True:
|
|
290
|
-
hashes = await run_compose_round(args, config, target_tree, round_number)
|
|
293
|
+
hashes = await run_compose_round(args, config, target_tree, round_number, weaver)
|
|
291
294
|
all_hashes.extend(hashes)
|
|
292
295
|
if args.compose_preview:
|
|
293
296
|
break
|
|
294
297
|
try:
|
|
295
|
-
git.get_compose_diff(repo_dir, config, target_tree)
|
|
298
|
+
git.get_compose_diff(repo_dir, config, target_tree, exclude=exclude)
|
|
296
299
|
except NoChanges:
|
|
297
300
|
break
|
|
298
301
|
if not hashes:
|
|
@@ -329,13 +332,18 @@ def _print_executable_plan(snapshot: ComposeSnapshot, plan: ComposeExecutablePla
|
|
|
329
332
|
|
|
330
333
|
|
|
331
334
|
async def run_compose_round(
|
|
332
|
-
args: _ComposeArgs,
|
|
335
|
+
args: _ComposeArgs,
|
|
336
|
+
config: CommitConfig,
|
|
337
|
+
target_tree: str,
|
|
338
|
+
round_number: int = 1,
|
|
339
|
+
weaver: ChangelogWeaver | None = None,
|
|
333
340
|
) -> list[str]:
|
|
334
341
|
"""Plan one immutable compose snapshot against the fixed target tree and execute it."""
|
|
335
342
|
repo_dir = args.dir
|
|
336
343
|
base_state = capture_compose_base_state(repo_dir)
|
|
337
|
-
|
|
338
|
-
|
|
344
|
+
exclude = weaver.exclude_pathspecs() if weaver else ()
|
|
345
|
+
diff = git.get_compose_diff(repo_dir, config, target_tree, exclude=exclude)
|
|
346
|
+
stat = git.get_compose_stat(repo_dir, target_tree, exclude=exclude)
|
|
339
347
|
snapshot = build_compose_snapshot(diff, stat)
|
|
340
348
|
snapshot = pin_snapshot_staged_state(snapshot, repo_dir, target_tree)
|
|
341
349
|
_save_debug_artifact(args, f"compose_round_{round_number}_snapshot.json", _snapshot_to_jsonable(snapshot))
|
|
@@ -365,7 +373,7 @@ async def run_compose_round(
|
|
|
365
373
|
preview_message = f"{style.icons.SUCCESS} Preview complete (use --compose without --compose-preview to execute)"
|
|
366
374
|
print(f"\n{style.success(preview_message)}")
|
|
367
375
|
return []
|
|
368
|
-
return await execute_compose(snapshot, plan, config, args, base_state)
|
|
376
|
+
return await execute_compose(snapshot, plan, config, args, base_state, weaver)
|
|
369
377
|
|
|
370
378
|
|
|
371
379
|
async def plan_compose_snapshot(
|
|
@@ -389,6 +397,7 @@ async def execute_compose(
|
|
|
389
397
|
config: CommitConfig,
|
|
390
398
|
args: _ComposeArgs,
|
|
391
399
|
base_state: ComposeBaseState,
|
|
400
|
+
weaver: ChangelogWeaver | None = None,
|
|
392
401
|
) -> list[str]:
|
|
393
402
|
"""Create compose commits from a temp index, then checked-update the real ref."""
|
|
394
403
|
if args.compose_preview:
|
|
@@ -401,6 +410,8 @@ async def execute_compose(
|
|
|
401
410
|
|
|
402
411
|
with git.TempGitIndex(repo_dir) as index:
|
|
403
412
|
git.read_tree_into_index(index.path, base_state.head_hash, repo_dir)
|
|
413
|
+
if weaver:
|
|
414
|
+
weaver.seed_temp_index(repo_dir, index.path)
|
|
404
415
|
parent_hash = base_state.head_hash
|
|
405
416
|
commit_hashes: list[str] = []
|
|
406
417
|
for position, group in enumerate(ordered_groups):
|
|
@@ -415,6 +426,15 @@ async def execute_compose(
|
|
|
415
426
|
staged_anything = True
|
|
416
427
|
if not staged_anything:
|
|
417
428
|
continue
|
|
429
|
+
if weaver:
|
|
430
|
+
patch = group_patches[position]
|
|
431
|
+
group_paths = [
|
|
432
|
+
file.path
|
|
433
|
+
for file_id in group.file_ids
|
|
434
|
+
for file in [snapshot.file_by_id(file_id)]
|
|
435
|
+
if file is not None
|
|
436
|
+
]
|
|
437
|
+
await weaver.weave_group(group_paths, patch.diff, patch.stat, repo_dir, index.path)
|
|
418
438
|
message = prepared_messages[position]
|
|
419
439
|
tree = git.write_index_tree(index.path, repo_dir)
|
|
420
440
|
sign = bool(args.sign or config.gpg_sign)
|
|
@@ -428,9 +448,11 @@ async def execute_compose(
|
|
|
428
448
|
return []
|
|
429
449
|
|
|
430
450
|
git.update_ref_checked(base_state.head_ref, parent_hash, base_state.head_hash, repo_dir)
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
#
|
|
451
|
+
if weaver:
|
|
452
|
+
weaver.flush(repo_dir)
|
|
453
|
+
# Source paths stay untouched in the real index: committed paths now match HEAD while
|
|
454
|
+
# uncovered or mid-run staging remains staged for the next round. The weaver separately
|
|
455
|
+
# synchronizes claimed changelogs under compare-before-write guards.
|
|
434
456
|
return commit_hashes
|
|
435
457
|
|
|
436
458
|
|
lgit/config.py
CHANGED
|
@@ -105,6 +105,7 @@ class CommitConfig:
|
|
|
105
105
|
classifier_hint: str = field(default_factory=default_classifier_hint)
|
|
106
106
|
categories: list[CategoryConfig] = field(default_factory=default_categories)
|
|
107
107
|
changelog_enabled: bool = True
|
|
108
|
+
changelog_revise: bool = True
|
|
108
109
|
map_reduce_enabled: bool = True
|
|
109
110
|
map_reduce_threshold: int = 5000
|
|
110
111
|
map_batch_token_budget: int = 16000
|
|
@@ -410,6 +411,7 @@ _FIELD_COERCERS = {
|
|
|
410
411
|
"signoff": _to_bool,
|
|
411
412
|
"classifier_hint": _to_str,
|
|
412
413
|
"changelog_enabled": _to_bool,
|
|
414
|
+
"changelog_revise": _to_bool,
|
|
413
415
|
"map_reduce_enabled": _to_bool,
|
|
414
416
|
"map_reduce_threshold": _to_int,
|
|
415
417
|
"map_batch_token_budget": _to_int,
|
lgit/git.py
CHANGED
|
@@ -404,14 +404,18 @@ def get_git_numstat(
|
|
|
404
404
|
|
|
405
405
|
|
|
406
406
|
def get_compose_diff(
|
|
407
|
-
dir: str | os.PathLike[str] = ".",
|
|
407
|
+
dir: str | os.PathLike[str] = ".",
|
|
408
|
+
config: object | None = None,
|
|
409
|
+
target_tree: str | None = None,
|
|
410
|
+
exclude: Sequence[str] = (),
|
|
408
411
|
) -> str:
|
|
409
412
|
"""Return the compose-mode diff with rename detection.
|
|
410
413
|
|
|
411
414
|
With ``target_tree`` (the staged tree captured at invocation), diff ``HEAD`` against that
|
|
412
415
|
fixed tree, so each loop round sees only the changes still needed to reach it — never
|
|
413
416
|
anything the user staged mid-run. Without it, diff the live index (``--cached``); this is
|
|
414
|
-
what a normal commit would commit, since callers auto-stage first.
|
|
417
|
+
what a normal commit would commit, since callers auto-stage first. ``exclude`` contains
|
|
418
|
+
pathspecs omitted from compose planning and convergence.
|
|
415
419
|
"""
|
|
416
420
|
|
|
417
421
|
max_len = int(getattr(config, "max_diff_length", 200_000))
|
|
@@ -424,21 +428,31 @@ def get_compose_diff(
|
|
|
424
428
|
"--src-prefix=a/",
|
|
425
429
|
"--dst-prefix=b/",
|
|
426
430
|
]
|
|
427
|
-
if target_tree is None
|
|
428
|
-
|
|
429
|
-
else
|
|
430
|
-
|
|
431
|
+
scope = ["--cached"] if target_tree is None else ["HEAD", target_tree]
|
|
432
|
+
pathspecs = ["--", *exclude] if exclude else []
|
|
433
|
+
retry_anchor = "HEAD" if target_tree is not None else "--" if exclude else None
|
|
434
|
+
diff = _diff_with_retry(
|
|
435
|
+
[*args, *scope, *pathspecs],
|
|
436
|
+
dir,
|
|
437
|
+
max_len,
|
|
438
|
+
insert_u1_before=retry_anchor,
|
|
439
|
+
)
|
|
431
440
|
if not diff.strip():
|
|
432
441
|
raise NoChanges("compose")
|
|
433
442
|
return diff
|
|
434
443
|
|
|
435
444
|
|
|
436
|
-
def get_compose_stat(
|
|
445
|
+
def get_compose_stat(
|
|
446
|
+
dir: str | os.PathLike[str] = ".",
|
|
447
|
+
target_tree: str | None = None,
|
|
448
|
+
exclude: Sequence[str] = (),
|
|
449
|
+
) -> str:
|
|
437
450
|
"""Return the compose-mode --stat output with rename detection (see :func:`get_compose_diff`)."""
|
|
438
451
|
|
|
439
452
|
args = ["diff", "--no-ext-diff", "--no-textconv", "--no-color", "--find-renames"]
|
|
440
453
|
scope = ["--cached"] if target_tree is None else ["HEAD", target_tree]
|
|
441
|
-
|
|
454
|
+
pathspecs = ["--", *exclude] if exclude else []
|
|
455
|
+
stat = run_git([*args, *scope, "--stat", *pathspecs], cwd=dir).stdout
|
|
442
456
|
if not stat.strip():
|
|
443
457
|
raise NoChanges("compose")
|
|
444
458
|
return stat
|
lgit/markdown_output.py
CHANGED
|
@@ -22,6 +22,7 @@ _PREFIX_RE = re.compile(
|
|
|
22
22
|
re.IGNORECASE,
|
|
23
23
|
)
|
|
24
24
|
_SUMMARY_TAG_RE = re.compile(r"<summary\b[^>]*>\s*(.*?)(?:\s*</[^>]+>|$)", re.IGNORECASE | re.DOTALL)
|
|
25
|
+
_REVISE_RE = re.compile(r"<revise\b.*?(?:</revise>|$)", re.IGNORECASE | re.DOTALL)
|
|
25
26
|
_ISSUE_RE = re.compile(r"#\d+(?:\s*-\s*#?\d+)?")
|
|
26
27
|
_CATEGORY_RE = re.compile(
|
|
27
28
|
r"^\s*(?:\[(?P<bracket>[^\]]+)\]|(?P<prefix>Added|Changed|Fixed|Deprecated|Removed|Security|Breaking Changes)\s*:)\s*(?P<text>.*)$",
|
|
@@ -573,6 +574,7 @@ def parse_changelog_response(text: str) -> dict[str, dict[str, list[str]]]:
|
|
|
573
574
|
"""Parse markdown changelog output into an ``entries`` mapping."""
|
|
574
575
|
|
|
575
576
|
cleaned = _clean_markdown_text(text)
|
|
577
|
+
cleaned, revise_blocks = _REVISE_RE.subn("", cleaned)
|
|
576
578
|
if _has_exception_tag(cleaned):
|
|
577
579
|
return {"entries": {}}
|
|
578
580
|
known = {"Added", "Changed", "Fixed", "Deprecated", "Removed", "Security", "Breaking Changes"}
|
|
@@ -608,6 +610,8 @@ def parse_changelog_response(text: str) -> dict[str, dict[str, list[str]]]:
|
|
|
608
610
|
elif current is not None:
|
|
609
611
|
entries.setdefault(current, []).append(line)
|
|
610
612
|
if not any(values for values in entries.values()):
|
|
613
|
+
if revise_blocks:
|
|
614
|
+
return {"entries": {}}
|
|
611
615
|
raise ValueError("No changelog entries found in response")
|
|
612
616
|
return {"entries": entries}
|
|
613
617
|
|
|
@@ -6,8 +6,19 @@ You are a changelog maintainer. Analyze the diff and return changelog entries fo
|
|
|
6
6
|
3. Each category is a `# CategoryName` section; each entry is a bullet (`- entry text`)
|
|
7
7
|
4. Entries are past-tense, active voice, one concise line under 100 characters, no trailing period
|
|
8
8
|
5. Skip anything already covered by `existing_entries`
|
|
9
|
-
6.
|
|
10
|
-
|
|
9
|
+
6. Entries in `authored_entries` were hand-written by the author for this exact diff: treat the changes they describe as already documented; never restate, reword, or recategorize them — but still document user-visible changes they do not describe
|
|
10
|
+
{% if can_revise %}
|
|
11
|
+
7. `Fixed` entries are only for bugs that exist in a released version; a fix or adjustment to behavior introduced by an entry in `existing_entries` is not a new entry — revise that entry if its text is now wrong, otherwise return nothing for it
|
|
12
|
+
8. When the diff extends a feature already described in `existing_entries`, do not add a sibling bullet — revise the existing entry into one bullet covering the final behavior; to merge several related entries, revise one and drop the rest
|
|
13
|
+
9. When the diff removes or reverts behavior described in `existing_entries`, drop that entry; do not add a `Removed` entry for something that never shipped
|
|
14
|
+
10. Describe what the consumer experiences, not the mechanism; internal helpers, protocol plumbing, refactors, and test churn are not user-visible
|
|
15
|
+
11. Omit categories with no entries; group similar entries and avoid duplication
|
|
16
|
+
12. If nothing is user-visible, or the authored entries already cover everything, return `<exception>brief reason</exception>` and no sections; when returning revisions without sections, place the exception after the `<revise>` block
|
|
17
|
+
{% else %}
|
|
18
|
+
7. Describe what the consumer experiences, not the mechanism; internal helpers, protocol plumbing, refactors, and test churn are not user-visible
|
|
19
|
+
8. Omit categories with no entries; group similar entries and avoid duplication
|
|
20
|
+
9. If nothing is user-visible (internal refactors, test-only churn, dependency churn without user impact), or the authored entries already cover everything, return only `<exception>brief reason</exception>` explaining why, and no sections
|
|
21
|
+
{% endif %}
|
|
11
22
|
|
|
12
23
|
Categories:
|
|
13
24
|
- Added: new features or user-visible capabilities
|
|
@@ -21,6 +32,18 @@ Categories:
|
|
|
21
32
|
|
|
22
33
|
<output_format>
|
|
23
34
|
You MUST return the result in this format WITHOUT the fences:
|
|
35
|
+
{% if can_revise %}
|
|
36
|
+
When earlier entries need reconciliation, place this optional block before any category sections:
|
|
37
|
+
```
|
|
38
|
+
<revise>
|
|
39
|
+
OLD: - Added task label generation for subagent assignments
|
|
40
|
+
NEW: - Added task label generation for subagent assignments, with automatic retry on empty labels
|
|
41
|
+
OLD: - Added experimental foo command
|
|
42
|
+
NEW:
|
|
43
|
+
</revise>
|
|
44
|
+
```
|
|
45
|
+
Use only `OLD:`/`NEW:` pairs. An empty `NEW:` drops the entry. Every `OLD:` must quote one line verbatim from `existing_entries`; never target any other line. A response with revisions but no category entries MUST put `<exception>brief reason</exception>` after the closing `</revise>` tag.
|
|
46
|
+
{% endif %}
|
|
24
47
|
```
|
|
25
48
|
# Added
|
|
26
49
|
- Added new authentication endpoint with JWT support
|
|
@@ -32,10 +55,18 @@ You MUST return the result in this format WITHOUT the fences:
|
|
|
32
55
|
- Added rate limiting on auth endpoints
|
|
33
56
|
```
|
|
34
57
|
|
|
58
|
+
{% if can_revise %}
|
|
59
|
+
If nothing is changelog-worthy and no revisions are needed, return exactly (without fences) a single exception tag whose body explains why:
|
|
60
|
+
```
|
|
61
|
+
<exception>internal refactor only, no user-facing change</exception>
|
|
62
|
+
```
|
|
63
|
+
If revisions are needed but no category entries are, return the `<revise>` block followed by that exception tag.
|
|
64
|
+
{% else %}
|
|
35
65
|
If nothing is changelog-worthy, return exactly (without fences) a single exception tag whose body explains why:
|
|
36
66
|
```
|
|
37
67
|
<exception>internal refactor only, no user-facing change</exception>
|
|
38
68
|
```
|
|
69
|
+
{% endif %}
|
|
39
70
|
</output_format>
|
|
40
71
|
|
|
41
72
|
<!-- USER -->
|
|
@@ -46,7 +77,11 @@ Changelog: {{ changelog_path }}
|
|
|
46
77
|
{% if existing_entries %}
|
|
47
78
|
|
|
48
79
|
<existing_entries>
|
|
80
|
+
{% if can_revise %}
|
|
81
|
+
Entries from earlier commits in this same unreleased release cycle. Never restate them; replace or drop them only through the `<revise>` block:
|
|
82
|
+
{% else %}
|
|
49
83
|
Already documented—skip these:
|
|
84
|
+
{% endif %}
|
|
50
85
|
{{ existing_entries }}
|
|
51
86
|
</existing_entries>
|
|
52
87
|
{% endif %}
|
|
@@ -58,3 +93,12 @@ Already documented—skip these:
|
|
|
58
93
|
<diff>
|
|
59
94
|
{{ diff }}
|
|
60
95
|
</diff>
|
|
96
|
+
{% if authored_entries %}
|
|
97
|
+
|
|
98
|
+
<authored_entries>
|
|
99
|
+
The author already hand-wrote these entries for this exact diff:
|
|
100
|
+
{{ authored_entries }}
|
|
101
|
+
|
|
102
|
+
These lines are final—do not repeat, reword, recategorize, or expand them. Then work through the diff: for each user-visible change, decide whether one of the authored entries describes it. Entries for changes none of them describe are real and required—list each as usual. If every user-visible change is already described, return the exception tag; never manufacture entries just to return something.
|
|
103
|
+
</authored_entries>
|
|
104
|
+
{% endif %}
|
lgit/templates.py
CHANGED
|
@@ -188,8 +188,11 @@ def render_changelog_prompt(
|
|
|
188
188
|
stat: str,
|
|
189
189
|
diff: str,
|
|
190
190
|
existing_entries: str | None = None,
|
|
191
|
+
authored_entries: str | None = None,
|
|
192
|
+
can_revise: bool = False,
|
|
191
193
|
) -> PromptParts:
|
|
192
|
-
"""Render the changelog prompt."""
|
|
194
|
+
"""Render the changelog prompt, selecting its bounded reconciliation variant."""
|
|
195
|
+
|
|
193
196
|
template_content = load_template_file("changelog")
|
|
194
197
|
context = {
|
|
195
198
|
"changelog_path": changelog_path,
|
|
@@ -197,8 +200,23 @@ def render_changelog_prompt(
|
|
|
197
200
|
"stat": stat,
|
|
198
201
|
"diff": diff,
|
|
199
202
|
"existing_entries": existing_entries,
|
|
203
|
+
"authored_entries": authored_entries,
|
|
204
|
+
"can_revise": can_revise,
|
|
200
205
|
}
|
|
201
|
-
|
|
206
|
+
system_template, user_template = split_prompt_template(template_content)
|
|
207
|
+
if system_template is None:
|
|
208
|
+
return render_prompt_parts("changelog.md", template_content, context)
|
|
209
|
+
|
|
210
|
+
static_system = system_template
|
|
211
|
+
for tag in ("{% if can_revise %}", "{% else %}", "{% endif %}"):
|
|
212
|
+
static_system = static_system.replace(tag, "")
|
|
213
|
+
_ensure_static_system_prompt(static_system, "changelog.md")
|
|
214
|
+
try:
|
|
215
|
+
system = _ENV.from_string(system_template).render(can_revise=can_revise)
|
|
216
|
+
user = _ENV.from_string(user_template).render(**context)
|
|
217
|
+
except Exception as exc: # jinja2 has several template-specific subclasses.
|
|
218
|
+
raise ConfigError(f"Failed to render changelog.md prompt template: {exc}") from exc
|
|
219
|
+
return PromptParts(system=system.strip(), user=user.strip())
|
|
202
220
|
|
|
203
221
|
|
|
204
222
|
def render_map_prompt(
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: lgit-cli
|
|
3
|
-
Version: 4.
|
|
3
|
+
Version: 4.3.0
|
|
4
4
|
Summary: AI-powered git commit message generator using Claude and other LLMs via OpenAI-compatible APIs
|
|
5
5
|
Project-URL: Homepage, https://github.com/can1357/llm-git
|
|
6
6
|
Project-URL: Repository, https://github.com/can1357/llm-git
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
lgit/__init__.py,sha256=
|
|
1
|
+
lgit/__init__.py,sha256=ryUk9YfJ4pu2KDOeu7XXAyrTmGUWAEIOc8nwhsse59E,1767
|
|
2
2
|
lgit/__main__.py,sha256=VbaRO8-0ehWv7zA27BpyHYWTQVvt5M-aG5HnWr7ws0U,165
|
|
3
3
|
lgit/analysis.py,sha256=j8bduuV6pqvMuza-r_RNJDe5SMQGKTtU6gwktaAeSS4,10671
|
|
4
4
|
lgit/api.py,sha256=GOx7mZNHfYgMq8_FKxlZcVgYiqK2cyQ6_ETHa9oclb8,33920
|
|
5
5
|
lgit/cache.py,sha256=XCBWTslpT3NAqSu3H6K0m49HtyofaRUtzmgf_mCSbKw,10234
|
|
6
|
-
lgit/changelog.py,sha256=
|
|
6
|
+
lgit/changelog.py,sha256=gtfTeIJVgsGgAcEQxKwntJilLwXV0Lq11Dsps7T0e2g,33247
|
|
7
7
|
lgit/cli.py,sha256=tJhptV09ZXZfFRMaqiNFs8kJHUZ-wUrtF_nYCBDLi8E,47087
|
|
8
|
-
lgit/compose.py,sha256=
|
|
9
|
-
lgit/config.py,sha256=
|
|
8
|
+
lgit/compose.py,sha256=bWIFUMzsmTLA9hHZ_WTEtm_887ZEizIwHhV5XoHCI8c,76635
|
|
9
|
+
lgit/config.py,sha256=yOuRgEmBn9pth60aLMfqCYpJTtg8l_Z8crcwLRXjl-0,14026
|
|
10
10
|
lgit/diffing.py,sha256=dVogyBNt8TzG-TaTTThwQEWora1Hnz1RuCUS5u18MNs,13289
|
|
11
11
|
lgit/errors.py,sha256=DkPJKA2ag5KbBoyuRk1d8zKgz9Mp1qRoLVBGRudlqkg,3457
|
|
12
|
-
lgit/git.py,sha256=
|
|
12
|
+
lgit/git.py,sha256=OR9lTia6w_wKeIsa8j1ztUkQE7-5uGfCFR_sDi8OUlg,31122
|
|
13
13
|
lgit/map_reduce.py,sha256=xoIxQUbb_NTNn5RSv5sdiVEQt9SpieWJM4WOsopwJlE,17484
|
|
14
|
-
lgit/markdown_output.py,sha256=
|
|
14
|
+
lgit/markdown_output.py,sha256=FZ1u85RyhYpktSSNWuDQg8BfUSj7kHDM4CSwukI24sc,26333
|
|
15
15
|
lgit/models.py,sha256=hc3AJ37YXoLBGgSbdLQN5EEnDNViXf-wywlkUW92_Z8,28942
|
|
16
16
|
lgit/normalization.py,sha256=Kj_xqLw_fLDkN7wTFtAbwlEAD8t_s8v8JEGcLWLG8DM,11889
|
|
17
17
|
lgit/patch.py,sha256=6Nx66t8pFZf4iNJS8nxxdtbSjiYk737EZVPpZscW4G0,30579
|
|
@@ -20,14 +20,14 @@ lgit/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
|
20
20
|
lgit/repo.py,sha256=eflCUY_XvTx8NY-KES5_BsyxpQczFEiT46eV2Fi4itQ,8926
|
|
21
21
|
lgit/rewrite.py,sha256=kGvrucYGnrTPtDHd5l_KBTnWK4ztQOUR7LCqxhTiigQ,8725
|
|
22
22
|
lgit/style.py,sha256=gfQYsUU-6nZFVhFkDBw6kWpsC3y7cGjOyUCJTV9ModY,7504
|
|
23
|
-
lgit/templates.py,sha256=
|
|
23
|
+
lgit/templates.py,sha256=7GT_5Kfkh31CvqWR9CYo3gEQqkgL_kzKXoA00Q3M_wo,13129
|
|
24
24
|
lgit/tokens.py,sha256=BbAhjIUtCXVBStPXzq4ZeuZa8H5r9ZqZJUnA1XHD_MI,3123
|
|
25
25
|
lgit/validation.py,sha256=hMTD6wCw1MRqAqo6fYoep5BzKsVimvgDm8AbO2ZeLQ8,18813
|
|
26
26
|
lgit/resources/__init__.py,sha256=w6eXZjQeAp_h24IJu_GhNIZelcSMuM3qk8GOmWLQPaQ,53
|
|
27
27
|
lgit/resources/commit_types.json,sha256=IPtUig1U8UAIFnfMVaszQFI_LOZIiaZ2yyE20J1FG1E,11900
|
|
28
28
|
lgit/resources/validation_data.json,sha256=PJ1Iocq2qPKvh-zC1V_WyPrlDFFsXyTdnIxa2bokK5I,14634
|
|
29
29
|
lgit/resources/prompts/analysis.md,sha256=3JsJ4C3pXaJW_93uZMdYUmMMZ-5j8-f7aIjrXXqOwIs,3685
|
|
30
|
-
lgit/resources/prompts/changelog.md,sha256=
|
|
30
|
+
lgit/resources/prompts/changelog.md,sha256=SZuKz-s5iYZTeF-V9ygsRjakZix1Ln3eF4b8mfzleGQ,5416
|
|
31
31
|
lgit/resources/prompts/compose-bind.md,sha256=PEEeq9J2dWgXF3tL7uT2mpuws-u1rw_5m4IOSta4Oi4,832
|
|
32
32
|
lgit/resources/prompts/compose-intent.md,sha256=OCUwji8cuf7LD351yryevIJ8Kyyh-4t6tBGjyplwtVo,1485
|
|
33
33
|
lgit/resources/prompts/fast.md,sha256=OLrJqaTjS7-F4jeXa5LI4I1g-wh0EDKxA-D4Vfxeqek,2209
|
|
@@ -39,8 +39,8 @@ lgit/testing/compare.py,sha256=jRY98uLclVPyQ7e1TMqFVz2cjUi6kwwn2dkQyR_xsgY,1834
|
|
|
39
39
|
lgit/testing/fixture.py,sha256=VXk02ZzFTBHHhYax6bsoGowdyz06nmrJm3ylenyfGbU,13654
|
|
40
40
|
lgit/testing/report.py,sha256=wb7YnP_lsYwsg5OIFtsIC1bMHtK42Mttul_zQcwRNpk,9036
|
|
41
41
|
lgit/testing/runner.py,sha256=5Sr4QvEOTdE7CAxB-CKcNJClEZARQGqB-t_6P6-UA2c,9514
|
|
42
|
-
lgit_cli-4.
|
|
43
|
-
lgit_cli-4.
|
|
44
|
-
lgit_cli-4.
|
|
45
|
-
lgit_cli-4.
|
|
46
|
-
lgit_cli-4.
|
|
42
|
+
lgit_cli-4.3.0.dist-info/METADATA,sha256=a9Va7_dUKjBAScrj6zEM6zANX3Ifx_lr6NKQKVMNCfI,9565
|
|
43
|
+
lgit_cli-4.3.0.dist-info/WHEEL,sha256=lCkmxWfQsSc9CfIClYeavTdQeEX2toPqufh9gI35EQA,87
|
|
44
|
+
lgit_cli-4.3.0.dist-info/entry_points.txt,sha256=CUSsokc0V3oVHQ2Z1ocL09wV7Krhm0SSfLZpnRMBdA0,39
|
|
45
|
+
lgit_cli-4.3.0.dist-info/licenses/LICENSE,sha256=6dqVzjngLuIcUMd4wWLXPLpv-Pi8gySZAEE3hSN5iKw,1076
|
|
46
|
+
lgit_cli-4.3.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|