Repository navigation
π fix(log): handle CRLF log.md β normalize on read, preserve on write - #207
Conversation
A CRLF-checked-out log.md (Windows with core.autocrlf=true, or an editor that writes CRLF) hit the same bug class #185 fixed for index.md: AppendLog read the raw bytes and parseLog split on "\n", so every preserved line kept a trailing \r while the newly rendered date group and entry were pure LF. The result was a mangled mixed-ending file on every append (whole-file git churn) and a stray \r captured into a folded legacy bullet's message. Fixed at the single log read/write seam with the same decided policy as #205: NORMALIZE ON READ, PRESERVE ON WRITE, reusing the normalizeEOL / usesCRLF helpers #205 added rather than a second implementation. - Normalize on read: parseLog is fed normalizeEOL(existing), so no \r leaks into the verbatim preamble/entries or defeats the $-anchored legacy-bullet regex. Any non-line-ending difference is still real content, so a genuine legacy fold / newest-first insert still happens. - Preserve on write: an up-to-date CRLF log is written back CRLF (zero churn); a new file, a mixed-ending file, or a lone-CR file stays LF. The choice depends only on the one file, so it is deterministic on any machine and leaves LF/Linux behavior unchanged. Closes #185
cwest
left a comment
There was a problem hiding this comment.
No changes needed.
The log.md EOL handling mirrors the merged index seam line-for-line: normalize on read before the parse, then preserve the file's existing endings on write, gated on usesCRLF. It reuses the shared normalizeEOL and usesCRLF helpers rather than standing up a second copy, so the two reserved files now follow one policy.
I ran the five EOL tests and both revert controls. Removing preserve-on-write turns the two CRLF tests red on the usesCRLF assertion; removing normalize-on-read turns them red on a content mismatch, with a stray carriage return leaking into the preamble and the folded legacy message β which is the exact corruption the normalize step prevents. The LF and lone-CR arms stay green under both reverts, so they are independent controls rather than co-triggered.
Against the LF knowledge bundle, this binary produces byte-identical output to the pre-change binary across all sixteen log.md files, pure LF throughout, so the LF path is untouched. Conformance suite, full race suite, gofmt, and vet are all clean.
Why
A CRLF-checked-out
log.md(Windows withcore.autocrlf=true, or an editor that writes CRLF) hits the same bug class #185 fixed forindex.md: the log read path compared/parsed raw bytes.AppendLogfedparseLogthe raw file andparseLogsplits on"\n", so every preserved line kept a trailing\rwhile the newly rendered date group and entry were pure LF β a mangled mixed-ending file on every append (whole-file git churn) and a stray\rcaptured into a folded legacy bullet's message.Observed before the fix on a CRLF
log.md:What
Fixed at the single log read/write seam with the same decided policy as #205 β normalize on read, preserve on write β reusing the
normalizeEOL/usesCRLFhelpers #205 added rather than a second implementation.parseLogis fednormalizeEOL(existing), so no\rleaks into the verbatim preamble/entries or defeats the$-anchored legacy-bullet regex. Any non-line-ending difference is still real content, so a genuine legacy fold / newest-first insert still happens.LF/Linux behavior is unchanged β the only place line endings are decided is the single re-ending choice on write.
Tests β mirroring #205's EOL set for log.md
New
internal/okf/log_eol_test.go:TestAppendLog_CRLFPreservedNoCorruptionTestAppendLog_LFStaysLFTestAppendLog_NewLogIsLFTestAppendLog_CRLFLegacyFoldStillWorksTestAppendLog_LoneCRNotMangled\r(not\r\n) is preserved; non-CRLF file stays LFRevert controls (each seam goes RED when removed)
CRLFPreservedNoCorruption+CRLFLegacyFoldStillWorksFAIL on theusesCRLFassertion; LF/new/lone-CR controls stay green.CRLFPreservedNoCorruptionFAILs on the content-equality assertion (CRLF log content mismatchβ stray\rsurvives the parse);CRLFLegacyFoldStillWorksFAILs on the exact-content control (a\rcaptured into the folded message).Conformance gate β all layers, counts pinned
Before/after counts are the control, proven base-binary vs this change on the same pristine corpus copy:
validate 4β4,lint 0β0. The 4validatefindings are pre-existing legacy-dated-bullet drift in the corpus (not introduced here); the counts do not move, proving the change neither hides nor creates findings.Real-corpus byte-identity control: appended the same entry via the shipped CLI to 16
log.mdfiles across the LF knowledge bundle with the base binary and this binary β 0 differ, byte-identical output, pure LF preserved. LF/Linux behavior is unchanged at corpus scale.Builds with
CGO_ENABLED=0(no CGO).Closes #185 for log.md.