Skip to content

Commit 7ff6f6e

Browse files
committed
Add repr output for TreeEntry
When iterating over a tree, its entries show up as objects with an address, which makes it hard to distinguish. Add a method to handle repr and make it easier to play around with them in the console.
1 parent 6939b9b commit 7ff6f6e

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/tree.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,16 @@ TreeEntry_hex__get__(TreeEntry *self)
154154
return git_oid_to_py_str(git_tree_entry_id(self->entry));
155155
}
156156

157+
PyObject *
158+
TreeEntry_repr(TreeEntry *self)
159+
{
160+
char str[GIT_OID_HEXSZ + 1] = { 0 };
161+
const char *typename;
162+
163+
typename = git_object_type2string(git_tree_entry_type(self->entry));
164+
git_oid_fmt(str, git_tree_entry_id(self->entry));
165+
return PyString_FromFormat("pygit2.TreeEntry('%s', %s, %s)", git_tree_entry_name(self->entry), typename, str);
166+
}
157167

158168
PyGetSetDef TreeEntry_getseters[] = {
159169
GETTER(TreeEntry, filemode),
@@ -176,7 +186,7 @@ PyTypeObject TreeEntryType = {
176186
0, /* tp_getattr */
177187
0, /* tp_setattr */
178188
0, /* tp_compare */
179-
0, /* tp_repr */
189+
(reprfunc)TreeEntry_repr, /* tp_repr */
180190
0, /* tp_as_number */
181191
0, /* tp_as_sequence */
182192
0, /* tp_as_mapping */

0 commit comments

Comments
 (0)