summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Thiel <byronimo@gmail.com>2015-01-07 15:22:59 +0100
committerSebastian Thiel <byronimo@gmail.com>2015-01-07 15:35:25 +0100
commitc6ee00d0dadcd7b10d60a2985db4fe137ca7cfed (patch)
tree54ba82b7490038b911fc46bc1407cd7955bd7edf
parent73790919dbe038285a3612a191c377bc27ae6170 (diff)
downloadgitpython-c6ee00d0dadcd7b10d60a2985db4fe137ca7cfed.tar.gz
Made sure commits accept unicode or unicode characters
-rw-r--r--git/refs/log.py17
-rw-r--r--git/test/test_index.py14
-rw-r--r--git/util.py2
3 files changed, 23 insertions, 10 deletions
diff --git a/git/refs/log.py b/git/refs/log.py
index ec19c1e8..f8dc88da 100644
--- a/git/refs/log.py
+++ b/git/refs/log.py
@@ -18,6 +18,7 @@ from git.objects.util import (
altz_to_utctz_str,
)
from git.compat import (
+ PY3,
xrange,
string_types,
defenc
@@ -37,6 +38,16 @@ class RefLogEntry(tuple):
def __repr__(self):
"""Representation of ourselves in git reflog format"""
+ res = self.format()
+ if PY3:
+ return res
+ else:
+ # repr must return a string, which it will auto-encode from unicode using the default encoding.
+ # This usually fails, so we encode ourselves
+ return res.encode(defenc)
+
+ def format(self):
+ """:return: a string suitable to be placed in a reflog file"""
act = self.actor
time = self.time
return u"{0} {1} {2} <{3}> {4!s} {5}\t{6}\n".format(self.oldhexsha,
@@ -45,7 +56,7 @@ class RefLogEntry(tuple):
act.email,
time[0],
altz_to_utctz_str(time[1]),
- self.message).encode("utf-8")
+ self.message)
@property
def oldhexsha(self):
@@ -273,7 +284,7 @@ class RefLog(list, Serializable):
lf._obtain_lock_or_raise()
fd = open(filepath, 'ab')
try:
- fd.write(repr(entry).encode(defenc))
+ fd.write(entry.format().encode(defenc))
finally:
fd.close()
lf._release_lock()
@@ -298,7 +309,7 @@ class RefLog(list, Serializable):
# write all entries
for e in self:
- write(repr(e).encode(defenc))
+ write(e.format().encode(defenc))
# END for each entry
def _deserialize(self, stream):
diff --git a/git/test/test_index.py b/git/test/test_index.py
index f7d1cc6a..4fdd3d1b 100644
--- a/git/test/test_index.py
+++ b/git/test/test_index.py
@@ -1,3 +1,4 @@
+#-*-coding:utf-8-*-
# test_index.py
# Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors
#
@@ -433,7 +434,7 @@ class TestIndex(TestBase):
# TEST COMMITTING
# commit changed index
cur_commit = cur_head.commit
- commit_message = "commit default head"
+ commit_message = u"commit default head by Frèderic Çaufl€"
new_commit = index.commit(commit_message, head=False)
assert cur_commit != new_commit
@@ -449,18 +450,19 @@ class TestIndex(TestBase):
# commit with other actor
cur_commit = cur_head.commit
- my_author = Actor("An author", "author@example.com")
- my_committer = Actor("An committer", "committer@example.com")
+ my_author = Actor(u"Frèderic Çaufl€", "author@example.com")
+ my_committer = Actor(u"Committing Frèderic Çaufl€", "committer@example.com")
commit_actor = index.commit(commit_message, author=my_author, committer=my_committer)
assert cur_commit != commit_actor
- assert commit_actor.author.name == "An author"
+ assert commit_actor.author.name == u"Frèderic Çaufl€"
assert commit_actor.author.email == "author@example.com"
- assert commit_actor.committer.name == "An committer"
+ assert commit_actor.committer.name == u"Committing Frèderic Çaufl€"
assert commit_actor.committer.email == "committer@example.com"
assert commit_actor.message == commit_message
assert commit_actor.parents[0] == cur_commit
assert len(new_commit.parents) == 1
- assert cur_head.commit == cur_commit
+ assert cur_head.commit == commit_actor
+ assert cur_head.log()[-1].actor == my_committer
# same index, no parents
commit_message = "index without parents"
diff --git a/git/util.py b/git/util.py
index 4d1ea8d6..b1f3b38d 100644
--- a/git/util.py
+++ b/git/util.py
@@ -328,7 +328,7 @@ class Actor(object):
return self.name
def __repr__(self):
- return '<git.Actor "%s <%s>">' % (self.name, self.email)
+ return u'<git.Actor "%s <%s>">' % (self.name, self.email)
@classmethod
def _from_string(cls, string):