Skip to content

UnidiffParseError: Hunk is longer than expected when ^M is used #104

@marxin

Description

@marxin

The following patch file can't be parsed even though it can be properly applied by git am command:
0001-Add-M-character.patch.txt

In [4]: unidiff.PatchSet(open('0001-Add-M-character.patch'))
---------------------------------------------------------------------------
UnidiffParseError                         Traceback (most recent call last)
Cell In [4], line 1
----> 1 unidiff.PatchSet(open('0001-Add-M-character.patch'))

File /usr/lib/python3.10/site-packages/unidiff/patch.py:461, in PatchSet.__init__(self, f, encoding, metadata_only)
    456 data = iter(f)
    457 # if encoding is None, assume we are reading unicode data
    458 # when metadata_only is True, only perform a minimal metadata parsing
    459 # (ie. hunks without content) which is around 2.5-6 times faster;
    460 # it will still validate the diff metadata consistency and get counts
--> 461 self._parse(data, encoding=encoding, metadata_only=metadata_only)

File /usr/lib/python3.10/site-packages/unidiff/patch.py:549, in PatchSet._parse(self, diff, encoding, metadata_only)
    547     if current_file is None:
    548         raise UnidiffParseError('Unexpected hunk found: %s' % line)
--> 549     current_file._parse_hunk(line, diff, encoding, metadata_only)
    550     continue
    552 # check for no newline marker

File /usr/lib/python3.10/site-packages/unidiff/patch.py:346, in PatchedFile._parse_hunk(self, header, diff, encoding, metadata_only)
    343 # stop parsing if we got past expected number of lines
    344 if (source_line_no > expected_source_end or
    345         target_line_no > expected_target_end):
--> 346     raise UnidiffParseError('Hunk is longer than expected')
    348 if original_line:
    349     original_line.diff_line_no = diff_line_no

UnidiffParseError: Hunk is longer than expected

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions