fix: handle empty context lines in unified diffs#51
Merged
sergeyt merged 1 commit intosergeyt:masterfrom Apr 17, 2026
Merged
Conversation
Empty context lines (lines with no leading space) are silently dropped by the parser because the context line regex /^\s+/ requires one or more whitespace characters. This causes hunk line counters to desynchronize, which can cascade into losing entire hunks and files from parsed output. This commonly occurs when git is configured with diff.suppressBlankEmpty=true or when diff output is post-processed to strip trailing whitespace. Fix: change /^\s+/ to /^\s*/ so the context line pattern matches zero or more whitespace characters. This is safe because schemaContent patterns are checked in order — deletions and additions are matched first, so /^\s*/ acts as a catch-all for remaining hunk body lines. Fixes sergeyt#50 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
22e402a to
a3180a5
Compare
Contributor
Author
Thank you, Sergey! 🙇 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix empty context lines being silently dropped by the unified diff parser, which causes hunk line counter desynchronization and can cascade into losing entire hunks and files from parsed output.
Fixes #50
Problem
The
schemaContentarray defines the context (unchanged) line pattern as/^\s+/, which requires one or more whitespace characters. In the unified diff format, context lines are prefixed with a single space — but when blank context lines have that leading space stripped, they become empty strings ("") that match none of the four content patterns and are silently dropped.This commonly occurs when:
diff.suppressBlankEmpty = trueThe Cascade
Dropping a context line means the
oldLines/newLinescounters never reach 0, so the parser stays in content mode. Subsequent@@hunk headers anddiff --gitfile boundaries are consumed as content instead of triggering transitions — entire hunks and files are lost.Fix
One-character change in
parse.js:/^\s+/→/^\s*/This is safe because
schemaContentpatterns are checked in order:/^\ No newline/— eof marker/^-/— deletion/^\+/— addition/^\s*/— context line (catch-all for remaining hunk body lines)Within a correctly-formed hunk body, anything that isn't a deletion, addition, or eof marker can only be a context line.
Tests Added
7 new tests in a
"empty context lines (suppressBlankEmpty)"describe block:@@headerAll 30 tests pass (23 existing + 7 new), zero lint errors.