8000 Add type attribute to TreeEntry by olasd · Pull Request #560 · libgit2/pygit2 · GitHub
[go: up one dir, main page]

Skip to content

Add type attribute to TreeEntry #560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions docs/objects.rst
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ Tree entries
.. autoattribute:: pygit2.TreeEntry.id
.. autoattribute:: pygit2.TreeEntry.hex
.. autoattribute:: pygit2.TreeEntry.filemode
.. autoattribute:: pygit2.TreeEntry.type

.. method:: TreeEntry.__cmp__(TreeEntry)

Expand All @@ -188,14 +189,14 @@ Example::
6

>>> for entry in tree: # Iteration
... print(entry.id, entry.name)
... print(entry.id, entry.type, entry.name)
...
7151ca7cd3e59f3eab19c485cfbf3cb30928d7fa .gitignore
c36f4cf1e38ec1bb9d9ad146ed572b89ecfc9f18 COPYING
32b30b90b062f66957d6790c3c155c289c34424e README.md
c87dae4094b3a6d10e08bc6c5ef1f55a7e448659 pygit2.c
85a67270a49ef16cdd3d328f06a3e4b459f09b27 setup.py
3d8985bbec338eb4d47c5b01b863ee89d044bd53 test
7151ca7cd3e59f3eab19c485cfbf3cb30928d7fa blob .gitignore
c36f4cf1e38ec1bb9d9ad146ed572b89ecfc9f18 blob COPYING
32b30b90b062f66957d6790c3c155c289c34424e blob README.md
c87dae4094b3a6d10e08bc6c5ef1f55a7e448659 blob pygit2.c
85a67270a49ef16cdd3d328f06a3e4b459f09b27 blob setup.py
3d8985bbec338eb4d47c5b01b863ee89d044bd53 tree test

>>> entry = tree['pygit2.c'] # Get an entry by name
>>> entry
Expand Down
10 changes: 10 additions & 0 deletions src/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ TreeEntry_name__get__(TreeEntry *self)
}


PyDoc_STRVAR(TreeEntry_type__doc__, "Type.");

PyObject *
TreeEntry_type__get__(TreeEntry *self)
{
return to_path(git_object_type2string(git_tree_entry_type(self->entry)));
}


PyDoc_STRVAR(TreeEntry_id__doc__, "Object id.");

PyObject *
Expand Down Expand Up @@ -171,6 +180,7 @@ PyGetSetDef TreeEntry_getseters[] = {
GETTER(TreeEntry, oid),
GETTER(TreeEntry, id),
GETTER(TreeEntry, hex),
GETTER(TreeEntry, type),
{NULL}
};

Expand Down
16 changes: 15 additions & 1 deletion test/test_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import operator
import unittest

from pygit2 import TreeEntry
from pygit2 import TreeEntry, GIT_FILEMODE_TREE
from . import utils


Expand Down Expand Up @@ -89,6 +89,7 @@ def test_read_subtree(self):
tree = self.repo[TREE_SHA]
subtree_entry = tree['c']
self.assertTreeEntryEqual(subtree_entry, SUBTREE_SHA, 'c', 0o0040000)
self.assertEquals(subtree_entry.type, 'tree')

subtree = self.repo[subtree_entry.id]
self.assertEqual(1, len(subtree))
Expand All @@ -100,21 +101,34 @@ def test_new_tree(self):
repo = self.repo
b0 = repo.create_blob('1')
b1 = repo.create_blob('2')
st = repo.TreeBuilder()
st.insert('a', b0, 0o0100644)
subtree = repo[st.write()]

t = repo.TreeBuilder()
t.insert('x', b0, 0o0100644)
t.insert('y', b1, 0o0100755)
t.insert('z', subtree.id, GIT_FILEMODE_TREE)
tree = repo[t.write()]

self.assertTrue('x' in tree)
self.assertTrue('y' in tree)
self.assertTrue('z' in tree)

x = tree['x']
y = tree['y']
z = tree['z']
self.assertEqual(x.filemode, 0o0100644)
self.assertEqual(y.filemode, 0o0100755)
self.assertEqual(z.filemode, GIT_FILEMODE_TREE)

self.assertEqual(repo[x.id].id, b0)
self.assertEqual(repo[y.id].id, b1)
self.assertEqual(repo[z.id].id, subtree.id)

self.assertEqual(x.type, 'blob')
self.assertEqual(y.type, 'blob')
self.assertEqual(z.type, 'tree')


def test_modify_tree(self):
Expand Down
0