(issue #56) Remove TreeEntry.to_object
This is one way to enable implementing 'TreeBuilder.get'. It also makes pygit2 a little lower-level. And makes TreeEntry consistent with IndexEntry, which lacks such a function.
This commit is contained in:
parent
b1cb51c9d1
commit
4cdb1a83b4
@ -165,7 +165,6 @@ This is the interface of a tree entry::
|
|||||||
TreeEntry.oid -- the id of the git object
|
TreeEntry.oid -- the id of the git object
|
||||||
TreeEntry.hex -- hexadecimal representation of the oid
|
TreeEntry.hex -- hexadecimal representation of the oid
|
||||||
TreeEntry.attributes -- the Unix file attributes
|
TreeEntry.attributes -- the Unix file attributes
|
||||||
TreeEntry.to_object() -- returns the git object (equivalent to repo[entry.oid])
|
|
||||||
|
|
||||||
Blobs
|
Blobs
|
||||||
-----------------
|
-----------------
|
||||||
|
24
pygit2.c
24
pygit2.c
@ -108,7 +108,6 @@ OBJECT_STRUCT(Walker, git_revwalk, walk)
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
PyObject_HEAD
|
PyObject_HEAD
|
||||||
const git_tree_entry *entry;
|
const git_tree_entry *entry;
|
||||||
Tree *tree;
|
|
||||||
} TreeEntry;
|
} TreeEntry;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@ -1327,7 +1326,6 @@ static PyTypeObject CommitType = {
|
|||||||
static void
|
static void
|
||||||
TreeEntry_dealloc(TreeEntry *self)
|
TreeEntry_dealloc(TreeEntry *self)
|
||||||
{
|
{
|
||||||
Py_XDECREF(self->tree);
|
|
||||||
PyObject_Del(self);
|
PyObject_Del(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1358,15 +1356,6 @@ TreeEntry_get_hex(TreeEntry *self)
|
|||||||
return git_oid_to_py_str(git_tree_entry_id(self->entry));
|
return git_oid_to_py_str(git_tree_entry_id(self->entry));
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *
|
|
||||||
TreeEntry_to_object(TreeEntry *self)
|
|
||||||
{
|
|
||||||
const git_oid *entry_oid;
|
|
||||||
|
|
||||||
entry_oid = git_tree_entry_id(self->entry);
|
|
||||||
return lookup_object(self->tree->repo, entry_oid, GIT_OBJ_ANY);
|
|
||||||
}
|
|
||||||
|
|
||||||
static PyGetSetDef TreeEntry_getseters[] = {
|
static PyGetSetDef TreeEntry_getseters[] = {
|
||||||
{"attributes", (getter)TreeEntry_get_attributes, NULL, "attributes", NULL},
|
{"attributes", (getter)TreeEntry_get_attributes, NULL, "attributes", NULL},
|
||||||
{"name", (getter)TreeEntry_get_name, NULL, "name", NULL},
|
{"name", (getter)TreeEntry_get_name, NULL, "name", NULL},
|
||||||
@ -1375,12 +1364,6 @@ static PyGetSetDef TreeEntry_getseters[] = {
|
|||||||
{NULL}
|
{NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
static PyMethodDef TreeEntry_methods[] = {
|
|
||||||
{"to_object", (PyCFunction)TreeEntry_to_object, METH_NOARGS,
|
|
||||||
"Look up the corresponding object in the repo."},
|
|
||||||
{NULL, NULL, 0, NULL}
|
|
||||||
};
|
|
||||||
|
|
||||||
static PyTypeObject TreeEntryType = {
|
static PyTypeObject TreeEntryType = {
|
||||||
PyVarObject_HEAD_INIT(NULL, 0)
|
PyVarObject_HEAD_INIT(NULL, 0)
|
||||||
"pygit2.TreeEntry", /* tp_name */
|
"pygit2.TreeEntry", /* tp_name */
|
||||||
@ -1409,7 +1392,7 @@ static PyTypeObject TreeEntryType = {
|
|||||||
0, /* tp_weaklistoffset */
|
0, /* tp_weaklistoffset */
|
||||||
0, /* tp_iter */
|
0, /* tp_iter */
|
||||||
0, /* tp_iternext */
|
0, /* tp_iternext */
|
||||||
TreeEntry_methods, /* tp_methods */
|
0, /* tp_methods */
|
||||||
0, /* tp_members */
|
0, /* tp_members */
|
||||||
TreeEntry_getseters, /* tp_getset */
|
TreeEntry_getseters, /* tp_getset */
|
||||||
0, /* tp_base */
|
0, /* tp_base */
|
||||||
@ -1447,11 +1430,8 @@ wrap_tree_entry(const git_tree_entry *entry, Tree *tree)
|
|||||||
TreeEntry *py_entry;
|
TreeEntry *py_entry;
|
||||||
|
|
||||||
py_entry = PyObject_New(TreeEntry, &TreeEntryType);
|
py_entry = PyObject_New(TreeEntry, &TreeEntryType);
|
||||||
if (py_entry) {
|
if (py_entry)
|
||||||
py_entry->entry = entry;
|
py_entry->entry = entry;
|
||||||
py_entry->tree = tree;
|
|
||||||
Py_INCREF(tree);
|
|
||||||
}
|
|
||||||
return py_entry;
|
return py_entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,20 +71,21 @@ class TreeTest(utils.BareRepoTestCase):
|
|||||||
self.assertTreeEntryEqual(tree['b'], sha, 'b', 0o0100644)
|
self.assertTreeEntryEqual(tree['b'], sha, 'b', 0o0100644)
|
||||||
|
|
||||||
def test_read_subtree(self):
|
def test_read_subtree(self):
|
||||||
tree = self.repo[TREE_SHA]
|
repo = self.repo
|
||||||
|
tree = repo[TREE_SHA]
|
||||||
subtree_entry = tree['c']
|
subtree_entry = tree['c']
|
||||||
self.assertTreeEntryEqual(subtree_entry, SUBTREE_SHA, 'c', 0o0040000)
|
self.assertTreeEntryEqual(subtree_entry, SUBTREE_SHA, 'c', 0o0040000)
|
||||||
|
|
||||||
subtree = subtree_entry.to_object()
|
subtree = repo[subtree_entry.oid]
|
||||||
self.assertEqual(1, len(subtree))
|
self.assertEqual(1, len(subtree))
|
||||||
sha = '297efb891a47de80be0cfe9c639e4b8c9b450989'
|
sha = '297efb891a47de80be0cfe9c639e4b8c9b450989'
|
||||||
self.assertTreeEntryEqual(subtree[0], sha, 'd', 0o0100644)
|
self.assertTreeEntryEqual(subtree[0], sha, 'd', 0o0100644)
|
||||||
|
|
||||||
# XXX Creating new trees was removed from libgit2 by v0.11.0, we
|
# TODO This test worked with libgit2 v0.10.0, update to use the
|
||||||
# deactivate this test temporarily, since the feature may come back in
|
# tree-builder
|
||||||
# a near feature (if it does not this test will be removed).
|
|
||||||
def xtest_new_tree(self):
|
def xtest_new_tree(self):
|
||||||
tree = pygit2.Tree(self.repo)
|
repo = self.repo
|
||||||
|
tree = pygit2.Tree(repo)
|
||||||
self.assertEqual(0, len(tree))
|
self.assertEqual(0, len(tree))
|
||||||
tree.add_entry('1' * 40, 'x', 0o0100644)
|
tree.add_entry('1' * 40, 'x', 0o0100644)
|
||||||
tree.add_entry('2' * 40, 'y', 0o0100755)
|
tree.add_entry('2' * 40, 'y', 0o0100755)
|
||||||
@ -103,8 +104,7 @@ class TreeTest(utils.BareRepoTestCase):
|
|||||||
self.assertEqual(None, tree.hex)
|
self.assertEqual(None, tree.hex)
|
||||||
tree.write()
|
tree.write()
|
||||||
contents = '100644 x\0%s100755 y\0%s' % ('\x11' * 20, '\x22' * 20)
|
contents = '100644 x\0%s100755 y\0%s' % ('\x11' * 20, '\x22' * 20)
|
||||||
self.assertEqual((pygit2.GIT_OBJ_TREE, contents),
|
self.assertEqual((pygit2.GIT_OBJ_TREE, contents), repo.read(tree.hex))
|
||||||
self.repo.read(tree.hex))
|
|
||||||
|
|
||||||
def test_modify_tree(self):
|
def test_modify_tree(self):
|
||||||
tree = self.repo[TREE_SHA]
|
tree = self.repo[TREE_SHA]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user