Skip to content

fix(parser): strip unified diff timestamps without timezone - #611

Open
hieuvu0611 wants to merge 1 commit into
rtfpessoa:masterfrom
hieuvu0611:fix/issue-557-bsd-diff-timestamps
Open

hieuvu0611 wants to merge 1 commit into
rtfpessoa:masterfrom
hieuvu0611:fix/issue-557-bsd-diff-timestamps

Conversation

@hieuvu0611

Copy link
Copy Markdown

Fixes #557

Summary

  • Strip unified diff header timestamps emitted by BSD/macOS /usr/bin/diff -u (which omit fractional seconds and numeric timezone offsets, e.g. --- file1.js\t2025-04-06 13:33:10).
  • Preserve existing behavior for GNU diff -u timestamps (with fractional seconds and numeric timezone offsets), Git diffs (isGitDiff), quoted filenames, and filenames containing date substrings followed by an extension.

Root Cause

In getHeaderFilename (src/diff-parser.ts), the unified diff timestamp regex required a trailing numeric timezone offset ( [+-]\d{4}.*$). Standard BSD/macOS diff -u headers omit both fractional seconds and timezone offsets, so the timestamp stayed attached to oldName / newName and broke file extension detection for language.

Changes

  • src/diff-parser.ts: Make the trailing numeric timezone portion optional ((?: [+-]\d{4}.*)?$) in getHeaderFilename while anchoring the timestamp to end-of-line when no timezone is present.
  • src/__tests__/diff-parser-tests.ts: Add unit tests covering:
    • BSD/macOS unified diff timestamps without timezone (file1.js\t2025-04-06 13:33:10)
    • Timestamps with fractional seconds and without timezone (2025-04-06 13:33:10.123456)
    • Filenames containing spaces or quotes followed by timezone-less timestamps
    • Preserving filenames containing a date and extension when no timestamp is appended (backup 2025-04-06 13:33:10.log)

Verification

  • Ran template build, Prettier (format:check), ESLint (lint:check), README TOC checks (node --test scripts/update-readme-toc-checks.mjs — 6/6 passed), full build (CSS, CommonJS, ESM, webpack bundles, website), and Jest coverage (jest --coverage — 12/12 suites, 142/142 tests, 76/76 snapshots passed).
  • Verified the compiled lib/diff2html.js output directly against real macOS /usr/bin/diff -u (Apple diff (based on FreeBSD diff)).
  • Note: Local checks ran on Node v24.4.1 / npm 11.4.2 (CI validates the Node 22.23.5 / 24.22.3 / 26.13.1 + npm 12.2.3 matrix).

Make the trailing numeric timezone optional in getHeaderFilename so
unified diff headers produced by BSD/macOS diff -u (which omit both
fractional seconds and timezone offsets) are stripped cleanly without
truncating filenames that contain date-like substrings.

Fixes rtfpessoa#557

Signed-off-by: hieuvu0611 <162115380+hieuvu0611@users.noreply.github.com>
@rtfpessoa

Copy link
Copy Markdown
Owner

Thanks for fixing #557! I reproduced the issue with macOS diff -u and verified that this PR strips the timestamps and restores JavaScript language detection.

Could you require a literal tab before the newly supported timezone-less timestamp? With \s+, this valid diff -u output loses both labels:

diff -u -L 'snapshot 2025-04-06 13:33:10' \
        -L 'snapshot 2025-04-07 09:10:40' old.js new.js

-L emits those labels as-is, with no appended timestamp, but the PR renders both as snapshot. The existing .log test doesn’t catch this because the label ends in an extension rather than the time itself.

Please add a regression test for this case and keep the existing GNU timestamp handling unchanged.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Diff parsing misinterprets file names for standard diff command

2 participants