Skip to content

Commit

Permalink
[git] Use "backslashreplace" instead of "surrogateescape".
Browse files Browse the repository at this point in the history
When decoding as utf8, if the character cannnot be decoded,
use the backslashreplace error handler, instead of the
surrogateescape error handler.

Fixes #18 for git backend, maybe others should be fixed too.
  • Loading branch information
jgbarah committed Mar 19, 2016
1 parent 8201934 commit bfbeba5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
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

0 comments on commit bfbeba5

Please sign in to comment.