summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Thiel <byronimo@gmail.com>2016-08-21 09:24:23 +0200
committerSebastian Thiel <byronimo@gmail.com>2016-08-21 09:32:20 +0200
commit31ad0a0e2588819e791f4269a5d7d7e81a67f8cc (patch)
tree6c4ef86e3277eb9e89dfb8bfb657cb19ae8b21bf
parentdf5095c16894e6f4da814302349e8e32f84c8c13 (diff)
downloadgitpython-31ad0a0e2588819e791f4269a5d7d7e81a67f8cc.tar.gz
fix(commit): handle gpgsig properly
Assure that gpgsig is not initialized with None to allow the automatic deserialization to kick in. Fixes #500
-rw-r--r--git/objects/commit.py15
-rw-r--r--git/test/test_commit.py30
2 files changed, 39 insertions, 6 deletions
diff --git a/git/objects/commit.py b/git/objects/commit.py
index 9e434c92..000ab3d0 100644
--- a/git/objects/commit.py
+++ b/git/objects/commit.py
@@ -130,7 +130,8 @@ class Commit(base.Object, Iterable, Diffable, Traversable, Serializable):
self.parents = parents
if encoding is not None:
self.encoding = encoding
- self.gpgsig = gpgsig
+ if gpgsig is not None:
+ self.gpgsig = gpgsig
@classmethod
def _get_intermediate_items(cls, commit):
@@ -425,10 +426,13 @@ class Commit(base.Object, Iterable, Diffable, Traversable, Serializable):
if self.encoding != self.default_encoding:
write(("encoding %s\n" % self.encoding).encode('ascii'))
- if self.gpgsig:
- write(b"gpgsig")
- for sigline in self.gpgsig.rstrip("\n").split("\n"):
- write((" " + sigline + "\n").encode('ascii'))
+ try:
+ if self.__getattribute__('gpgsig') is not None:
+ write(b"gpgsig")
+ for sigline in self.gpgsig.rstrip("\n").split("\n"):
+ write((" " + sigline + "\n").encode('ascii'))
+ except AttributeError:
+ pass
write(b"\n")
@@ -473,6 +477,7 @@ class Commit(base.Object, Iterable, Diffable, Traversable, Serializable):
# now we can have the encoding line, or an empty line followed by the optional
# message.
self.encoding = self.default_encoding
+ self.gpgsig = None
# read headers
enc = next_line
diff --git a/git/test/test_commit.py b/git/test/test_commit.py
index ea8cd9af..c0599503 100644
--- a/git/test/test_commit.py
+++ b/git/test/test_commit.py
@@ -34,6 +34,7 @@ import re
import os
from datetime import datetime
from git.objects.util import tzoffset, utc
+from mock import Mock
def assert_commit_serialization(rwrepo, commit_id, print_performance_info=False):
@@ -342,7 +343,9 @@ JzJMZDRLQLFvnzqZuCjE
cstream = BytesIO()
cmt._serialize(cstream)
assert re.search(r"^gpgsig <test\n dummy\n sig>$", cstream.getvalue().decode('ascii'), re.MULTILINE)
-
+
+ self.assert_gpgsig_deserialization(cstream)
+
cstream.seek(0)
cmt.gpgsig = None
cmt._deserialize(cstream)
@@ -352,6 +355,31 @@ JzJMZDRLQLFvnzqZuCjE
cstream = BytesIO()
cmt._serialize(cstream)
assert not re.search(r"^gpgsig ", cstream.getvalue().decode('ascii'), re.MULTILINE)
+
+ def assert_gpgsig_deserialization(self, cstream):
+ assert 'gpgsig' in 'precondition: need gpgsig'
+
+ class RepoMock:
+ def __init__(self, bytestr):
+ self.bytestr = bytestr
+
+ @property
+ def odb(self):
+ class ODBMock:
+ def __init__(self, bytestr):
+ self.bytestr = bytestr
+
+ def stream(self, *args):
+ stream = Mock(spec_set=['read'], return_value=self.bytestr)
+ stream.read.return_value = self.bytestr
+ return ('binsha', 'typename', 'size', stream)
+
+ return ODBMock(self.bytestr)
+
+ repo_mock = RepoMock(cstream.getvalue())
+ for field in Commit.__slots__:
+ c = Commit(repo_mock, b'x' * 20)
+ assert getattr(c, field) is not None
def test_datetimes(self):
commit = self.rorepo.commit('4251bd5')