fix(changelog-gen): raise on git failure and remove unreachable fallback regex (#1247)

## Description

Fixes two silent-failure bugs in scripts/changelog-gen.py that could let
the release
pipeline produce an empty changelog with exit code 0.

1. run_git_log() never checked subprocess.returncode, so a failed git
log
(e.g. a non-existent --since ref) silently returned empty output and
wrote a blank
.changelog.md, allowing the pipeline to continue as if nothing went
wrong.
2. iter_commit_entries() had a dead fallback branch using
COMMIT_ENTRY_PATTERN — a
regex that expected | as a field separator, but the actual git log
format uses
\x1f/\x1e. The fallback could never match real output and would silently
return
zero commits if the primary parse path was skipped.

Closes #

## Type of Change

- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)

## Changes Made

- scripts/changelog-gen.py: run_git_log() now raises RuntimeError on
non-zero git exit, with the stderr included in the message.
- scripts/changelog-gen.py: removed the unused COMMIT_ENTRY_PATTERN
constant; replaced the dead fallback in iter_commit_entries() with an
explicit ValueError so format mismatches
surface immediately instead of returning empty results.

## Testing

- [x] Unit tests pass (pytest)
- [ ] Linting passes (ruff check .)
- [ ] Type checking passes (mypy headroom)
- [ ] New tests added for new functionality
- [ ] Manual testing performed

### Test Output

 ```
$ uv run python -m pytest scripts/tests/test_changelog_gen.py -q
============================= test session starts
==============================
platform linux -- Python 3.14.0, pytest-9.1.1, pluggy-1.6.0
rootdir: /home/sepurisaikrishna/Documents/calude-here/headroom
configfile: pyproject.toml
collected 28 items

scripts/tests/test_changelog_gen.py ............................ [100%]

=============================== warnings summary
===============================
PytestConfigWarning: Unknown config option: asyncio_mode

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
======================== 28 passed, 1 warning in 0.03s
=========================

$ git diff --check origin/main..HEAD
# no output; command exited 0
 ```

## Real Behavior Proof

- Environment: Linux, Python 3.14.0, uv-managed .venv, branch based on
current origin/main.
- Exact command / steps: invoke python scripts/changelog-gen.py
--version 0.1.0 --since v99.99.99 (non-existent tag); before the fix the
script exits 0 with an empty .changelog.md;
after the fix it raises RuntimeError: git log failed and exits non-zero.
- Observed result: all 28 test_changelog_gen.py tests pass; script now
fails loudly on bad git input.
- Not tested: full repository pytest, ruff, and mypy; live CI run
against a real release tag.

## Review Readiness

- [x] I have performed a self-review
- [x] This PR is ready for human review

## Checklist 

- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [ ] I have updated the CHANGELOG.md if applicable

## Screenshots (if applicable)

N/A.

## Additional Notes

- Both bugs share the same failure mode (silent empty output instead of
a clear error) and are in the same file, so they are fixed together in
one PR.
- The COMMIT_ENTRY_PATTERN constant is removed as it is now
unreferenced.
- No production behaviour changes — these scripts are only invoked
during the release pipeline.

---------

Co-authored-by: JerrettDavis <mxjerrett@gmail.com>
This commit is contained in:
Sepuri Sai Krishna
2026-09-09 23:18:51 -05:00
committed by GitHub
co-authored by JerrettDavis
parent 60bfe8be93
commit 4e1f7769d7
+19 -22
View File
@@ -16,7 +16,6 @@ COMMIT_PATTERN = re.compile(
r"^(feat|fix|ci|chore|perf|refactor|docs|style|test)(\(.+\))?(!)?:\s*(.+)$"
)
BREAKING_CHANGE_PATTERN = re.compile(r"^BREAKING CHANGE:\s*(.+)$", re.MULTILINE)
COMMIT_ENTRY_PATTERN = re.compile(r"^(.+?)(?:\n(.+))?\|(\w+)$", re.MULTILINE)
FIELD_SEP = "\x1f"
RECORD_SEP = "\x1e"
GIT_LOG_FORMAT = "%s%x1f%b%x1f%h%x1e"
@@ -49,28 +48,24 @@ def iter_commit_entries(log_output: str) -> list[tuple[str, str, str]]:
if not log_output.strip():
return []
if RECORD_SEP in log_output and FIELD_SEP in log_output:
entries: list[tuple[str, str, str]] = []
for raw_entry in log_output.split(RECORD_SEP):
if not raw_entry:
continue
if FIELD_SEP not in raw_entry:
continue
subject, body_and_hash = raw_entry.split(FIELD_SEP, 1)
if FIELD_SEP not in body_and_hash:
continue
body, commit_hash = body_and_hash.rsplit(FIELD_SEP, 1)
entries.append((subject.strip(), body.strip(), commit_hash.strip()))
return entries
return [
(
match.group(1).strip(),
(match.group(2) or "").strip(),
match.group(3).strip(),
if RECORD_SEP not in log_output or FIELD_SEP not in log_output:
raise ValueError(
"git log output does not contain expected field/record separators "
"(\\x1f / \\x1e). Check GIT_LOG_FORMAT."
)
for match in COMMIT_ENTRY_PATTERN.finditer(log_output)
]
entries: list[tuple[str, str, str]] = []
for raw_entry in log_output.split(RECORD_SEP):
if not raw_entry:
continue
if FIELD_SEP not in raw_entry:
continue
subject, body_and_hash = raw_entry.split(FIELD_SEP, 1)
if FIELD_SEP not in body_and_hash:
continue
body, commit_hash = body_and_hash.rsplit(FIELD_SEP, 1)
entries.append((subject.strip(), body.strip(), commit_hash.strip()))
return entries
def get_merge_summary(subject: str, body: str) -> str:
@@ -177,6 +172,8 @@ def run_git_log(since: str | None, cwd: Path) -> str:
else:
cmd.append("HEAD")
result = subprocess.run(cmd, cwd=cwd, capture_output=True, text=True)
if result.returncode != 0:
raise RuntimeError(f"git log failed (exit {result.returncode}): {result.stderr.strip()}")
return result.stdout