Skip to content

Commit fbad877

Browse files
committed
Merge remote-tracking branch 'JacobSwanson/signature-repr-fix'
2 parents 0ebbda2 + 3631a6d commit fbad877

File tree

5 files changed

+44
-14
lines changed

5 files changed

+44
-14
lines changed

pygit2/_pygit2.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -506,15 +506,15 @@ class RevSpec:
506506
to_object: Object
507507

508508
class Signature:
509-
_encoding: str
509+
_encoding: str | None
510510
_pointer: bytes
511511
email: str
512512
name: str
513513
offset: int
514514
raw_email: bytes
515515
raw_name: bytes
516516
time: int
517-
def __init__(self, name: str, email: str, time: int, offset: int, encoding: str) -> None: ...
517+
def __init__(self, name: str, email: str, time: int, offset: int, encoding: str | None) -> None: ...
518518
def __eq__(self, other) -> bool: ...
519519
def __ge__(self, other) -> bool: ...
520520
def __gt__(self, other) -> bool: ...

src/signature.c

+16-10
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@ Signature_init(Signature *self, PyObject *args, PyObject *kwds)
4545
int offset = 0;
4646

4747
if (!PyArg_ParseTupleAndKeywords(
48-
args, kwds, "Os|Lis", keywords,
48+
args, kwds, "Os|Liz", keywords,
4949
&py_name, &email, &time, &offset, &encoding))
5050
return -1;
5151

5252
PyObject *tname;
53-
const char *name = pgit_borrow_encoding(py_name, encoding, NULL, &tname);
53+
const char *name = pgit_borrow_encoding(
54+
py_name, value_or_default(encoding, "utf-8"), NULL, &tname);
5455
if (name == NULL)
5556
return -1;
5657

@@ -104,13 +105,10 @@ PyDoc_STRVAR(Signature__encoding__doc__, "Encoding.");
104105
PyObject *
105106
Signature__encoding__get__(Signature *self)
106107
{
107-
const char *encoding;
108+
if (self->encoding)
109+
return to_encoding(self->encoding);
108110

109-
encoding = self->encoding;
110-
if (encoding == NULL)
111-
encoding = "utf-8";
112-
113-
return to_encoding(encoding);
111+
Py_RETURN_NONE;
114112
}
115113

116114

@@ -199,7 +197,9 @@ Signature_richcompare(PyObject *a, PyObject *b, int op)
199197
strcmp(sa->signature->email, sb->signature->email) == 0 &&
200198
sa->signature->when.time == sb->signature->when.time &&
201199
sa->signature->when.offset == sb->signature->when.offset &&
202-
sa->signature->when.sign == sb->signature->when.sign);
200+
sa->signature->when.sign == sb->signature->when.sign &&
201+
strcmp(value_or_default(sa->encoding, "utf-8"),
202+
value_or_default(sb->encoding, "utf-8")) == 0);
203203

204204
switch (op) {
205205
case Py_EQ:
@@ -239,7 +239,13 @@ Signature__repr__(Signature *self)
239239
PyObject *name, *email, *encoding, *str;
240240
name = to_unicode(self->signature->name, self->encoding, NULL);
241241
email = to_unicode(self->signature->email, self->encoding, NULL);
242-
encoding = to_unicode(self->encoding, self->encoding, NULL);
242+
243+
if (self->encoding) {
244+
encoding = to_unicode(self->encoding, self->encoding, NULL);
245+
} else {
246+
encoding = Py_None;
247+
}
248+
243249
str = PyUnicode_FromFormat(
244250
"pygit2.Signature(%R, %R, %lld, %ld, %R)",
245251
name,

src/utils.h

+2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ to_unicode_n(const char *value, size_t len, const char *encoding,
8484
return PyUnicode_Decode(value, len, encoding, errors);
8585
}
8686

87+
#define value_or_default(x, _default) ((x) == NULL ? (_default) : (x))
88+
8789
const char* pgit_borrow(PyObject *value);
8890
const char* pgit_borrow_encoding(PyObject *value, const char *encoding, const char *errors, PyObject **tvalue);
8991
char* pgit_encode(PyObject *value, const char *encoding);

test/test_commit.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,10 @@ def test_amend_commit_metadata(barerepo):
142142

143143
encoding = 'iso-8859-1'
144144
amended_message = "Amended commit message.\n\nMessage with non-ascii chars: ééé.\n"
145-
amended_author = Signature('Jane Author', '[email protected]', 12345, 0)
146-
amended_committer = Signature('John Committer', '[email protected]', 12346, 0)
145+
amended_author = Signature(
146+
'Jane Author', '[email protected]', 12345, 0, encoding=encoding)
147+
amended_committer = Signature(
148+
'John Committer', '[email protected]', 12346, 0, encoding=encoding)
147149

148150
amended_oid = repo.amend_commit(
149151
commit, 'HEAD', message=amended_message, author=amended_author,

test/test_signature.py

+20
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ def test_latin1():
5454
assert signature.email == signature.raw_email.decode(encoding)
5555
assert signature.email.encode(encoding) == signature.raw_email
5656

57+
def test_none():
58+
signature = Signature('Foo Ibáñez', '[email protected]', encoding=None)
59+
assert signature._encoding is None
60+
assert signature.name == signature.raw_name.decode('utf-8')
61+
assert signature.name.encode('utf-8') == signature.raw_name
62+
assert signature.email == signature.raw_email.decode('utf-8')
63+
assert signature.email.encode('utf-8') == signature.raw_email
64+
5765
def test_now():
5866
encoding = 'utf-8'
5967
signature = Signature(
@@ -75,3 +83,15 @@ def test_repr():
7583
expected_signature = \
7684
"pygit2.Signature('Foo Ibáñez', '[email protected]', 1322174594, 60, 'utf-8')"
7785
assert repr(signature) == expected_signature
86+
87+
def test_repr_from_commit(barerepo):
88+
repo = barerepo
89+
signature = Signature('Foo Ibáñez', '[email protected]', encoding=None)
90+
tree = '967fce8df97cc71722d3c2a5930ef3e6f1d27b12'
91+
parents = ['5fe808e8953c12735680c257f56600cb0de44b10']
92+
sha = repo.create_commit(
93+
None, signature, signature, 'New commit.', tree, parents)
94+
commit = repo[sha]
95+
96+
assert repr(signature) == repr(commit.author)
97+
assert repr(signature) == repr(commit.committer)

0 commit comments

Comments
 (0)