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

[git] Use "backslashreplace" instead of "surrogateescape". #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions perceval/backends/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def parse_git_log_from_file(filepath):
:raises OSError: raised when an error occurs reading the
given file
"""
with open(filepath, 'r', errors='surrogateescape') as f:
with open(filepath, 'r', errors='backslashreplace') as f:
parser = GitParser(f)

for commit in parser.parse():
Expand Down Expand Up @@ -648,7 +648,7 @@ def log(self, from_date=None, encoding='utf-8'):
self.uri, self.dirpath)

for line in gitlog:
line = line.decode(encoding, errors='surrogateescape')
line = line.decode(encoding, errors='backslashreplace')
yield line

@staticmethod
Expand All @@ -675,10 +675,10 @@ def _exec(cmd, cwd=None, env=None):
raise RepositoryError(cause=str(e))

if proc.returncode != 0:
err = errs.decode('utf-8', errors='surrogateescape')
err = errs.decode('utf-8', errors='backslashreplace')
cause = "git command - %s" % err
raise RepositoryError(cause=cause)
else:
logging.debug(errs.decode('utf-8', errors='surrogateescape'))
logging.debug(errs.decode('utf-8', errors='backslashreplace'))

return outs
10 changes: 10 additions & 0 deletions tests/data/git_bad_utf8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
commit c4c8ea948aa21527d502e87227b2f1d951bc506d d69332b875efb52ea5276d5638ce572fcd7375f2
Author: Jason Gaston <[email protected]>
AuthorDate: Sat Apr 16 15:24:43 2005 -0700
Commit: Linus Torvalds <[email protected]>
CommitDate: Sat Apr 16 15:24:43 2005 -0700

[PATCH] intel8x0: AC'97 audio patch for Intel ESB2

Signed-off-by: �Jason Gaston <[email protected]>

18 changes: 17 additions & 1 deletion tests/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,23 @@ def test_git_encoding_error(self):

commit = result[0]
self.assertEqual(commit['commit'], 'cb24e4f2f7b2a7f3450bfb15d1cbaa97371e93fb')
self.assertEqual(commit['message'], 'Calling \udc93Open Type\udc94 (CTRL+SHIFT+T) after startup - performance improvement.')
self.assertEqual(commit['message'], 'Calling \\x93Open Type\\x94 (CTRL+SHIFT+T) after startup - performance improvement.')

def test_git_utf8_error(self):
"""Characters that cannot decoded as utf8 can be later encoded as utf8.

This test raised the following exception before being fixed:
"UnicodeEncodeError: 'utf-8' codec can't encode character '\udca0'
in position 153: surrogates not allowed"

"""

message_ok = b"[PATCH] intel8x0: AC'97 audio patch for Intel ESB2\n" \
+ b"\nSigned-off-by: \\xa0Jason Gaston <[email protected]>"

commits = Git.parse_git_log_from_file("data/git_bad_utf8.txt")
commit = [commit for commit in commits][0]
self.assertEqual(commit['message'].encode('utf8'), message_ok)

def test_git_parser_from_iter(self):
"""Test if the static method parses a git log from a repository"""
Expand Down