Skip to content

Commit

Permalink
Fix #21 read commit message UnicodeDecodeError
Browse files Browse the repository at this point in the history
  • Loading branch information
EdmondFrank committed Apr 27, 2022
1 parent 9b98c6d commit 3a80f5e
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ def pretty_date(time=False):
return str(day_diff // 30) + " months ago"
return str(day_diff // 365) + " years ago"

def bytes_decode(str_encode_bytes):
return str(from_bytes(str_encode_bytes).best())

def is_binary(filename_or_bytes):
"""
Return true if the given file or content appears to be binary.
Expand Down Expand Up @@ -239,7 +242,7 @@ def __init__(self, buffer_id, url, arguments):
else:
self.last_commit_id = str(self.repo.head.target)
self.last_commit = self.repo.revparse_single(str(self.repo.head.target))
self.last_commit_message = self.last_commit.message.splitlines()[0]
self.last_commit_message = bytes_decode(self.last_commit.raw_message).splitlines()[0]

self.highlight_style = "monokai"

Expand Down Expand Up @@ -631,7 +634,7 @@ def handle_log_reset_last(self, mode):
self.fetch_stash_info()

last_commit = self.repo.revparse_single(str(self.repo.head.target))
message_to_emacs("Current HEAD is: {}".format(last_commit.message.splitlines()[0]))
message_to_emacs("Current HEAD is: {}".format(bytes_decode(last_commit.raw_message)).splitlines()[0])

@QtCore.pyqtSlot(str, str)
def log_reset_to(self, commit_id, commit_message):
Expand Down Expand Up @@ -785,7 +788,7 @@ def update_diff(self, type, file):
patch_set = parse_diff_and_color(self.raw_patch_set)
else:
patches = [patch for patch in stage_diff if patch.delta.new_file.path == file]
diff_string = "\n".join(map(lambda patch : str(NO_PREVIEW if is_binary(patch.data) else from_bytes(patch.data).best()), patches))
diff_string = "\n".join(map(lambda patch : NO_PREVIEW if is_binary(patch.data) else bytes_decode(patch.data), patches))
self.raw_patch_set = PatchSet(diff_string)
patch_set = parse_diff_and_color(self.raw_patch_set)

Expand All @@ -797,7 +800,7 @@ def update_diff(self, type, file):
patch_set = parse_diff_and_color(self.raw_patch_set)
else:
patches = [patch for patch in unstage_diff if patch.delta.new_file.path == file]
diff_string = "\n".join(map(lambda patch : str(NO_PREVIEW if is_binary(patch.data) else from_bytes(patch.data).best()), patches))
diff_string = "\n".join(map(lambda patch : NO_PREVIEW if is_binary(patch.data) else bytes_decode(patch.data), patches))
self.raw_patch_set = PatchSet(diff_string)
patch_set = parse_diff_and_color(self.raw_patch_set)

Expand Down Expand Up @@ -875,7 +878,7 @@ def stage_unstage_hunk(self, patch_index, hunk_index):
blob_data = self.repo[blob_id].data
new_content = StringIO()
new_content.writelines(patch_stream(
StringIO(str(from_bytes(blob_data).best())),
StringIO(bytes_decode(blob_data)),
[self.raw_patch_set[patch_index][hunk_index]]
))
new_id = self.repo.write(pygit2.GIT_OBJ_BLOB, new_content.getvalue())
Expand Down Expand Up @@ -1607,8 +1610,8 @@ def run(self):

for commit in self.repo.walk(self.branch.target):
id = str(commit.id)
author = commit.author.name
message = commit.message.splitlines()[0]
author = bytes_decode(commit.author.raw_name)
message = bytes_decode(commit.raw_message).splitlines()[0]

git_log.append({
"id": id,
Expand Down

0 comments on commit 3a80f5e

Please sign in to comment.