Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse github patch UnidiffParseError: Unexpected trailing newline character #74

Open
liziwl opened this issue Aug 23, 2020 · 2 comments

Comments

@liziwl
Copy link

liziwl commented Aug 23, 2020

test case

    def test1(self):
        import urllib.request
        from unidiff import PatchSet
        diff = urllib.request.urlopen('https://patch-diff.githubusercontent.com/raw/eXist-db/exist/pull/2644.patch')
        encoding = diff.headers.get_charsets()[0]
        patch = PatchSet(diff, encoding=encoding)

log

Traceback (most recent call last):
  File "c:\users\liziq\appdata\local\programs\python\python37\lib\unittest\case.py", line 59, in testPartExecutor
    yield
  File "c:\users\liziq\appdata\local\programs\python\python37\lib\unittest\case.py", line 628, in run
    testMethod()
  File "D:\A-work\test\test.py", line 92, in test1
    patch = PatchSet(diff, encoding=encoding)
  File "D:\A-work\test\venv\lib\site-packages\unidiff\patch.py", line 421, in __init__
    self._parse(data, encoding=encoding, metadata_only=metadata_only)
  File "D:\A-work\test\venv\lib\site-packages\unidiff\patch.py", line 513, in _parse
    current_file._append_trailing_empty_line()
  File "D:\A-work\test\venv\lib\site-packages\unidiff\patch.py", line 351, in _append_trailing_empty_line
    raise UnidiffParseError('Unexpected trailing newline character')
unidiff.errors.UnidiffParseError: Unexpected trailing newline character
@matiasb
Copy link
Owner

matiasb commented Aug 28, 2020

Note that passing the full patch data won't work (there are several metadata/information there the lib is not parsing, or ignoring). But if you use this URL instead 'https://patch-diff.githubusercontent.com/raw/eXist-db/exist/pull/2644.diff', it should work (since there you have the plain/raw diff from the patch only, which is what the library supports).

Makes sense?

@liziwl
Copy link
Author

liziwl commented Aug 29, 2020

yes, if I just want to get the path of the changed file.

However, I want to get commit hash and the paths from the patch. Maybe I can use regex to get the hash from 'https://patch-diff.githubusercontent.com/raw/eXist-db/exist/pull/2644.patch' and get the path from diff URL 'https://patch-diff.githubusercontent.com/raw/eXist-db/exist/pull/2644.diff'

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

No branches or pull requests

2 participants