Skip to content
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

Commit: retrieve tree and parents' ids directly #311

Merged
merged 2 commits into from
Jan 19, 2014
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
33 changes: 32 additions & 1 deletion src/commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "signature.h"
#include "commit.h"
#include "object.h"
#include "oid.h"

extern PyTypeObject TreeType;

Expand Down Expand Up @@ -120,7 +121,6 @@ Commit_author__get__(Commit *self)
return build_signature((Object*)self, signature, encoding);
}


PyDoc_STRVAR(Commit_tree__doc__, "The tree object attached to the commit.");

PyObject *
Expand All @@ -146,6 +146,13 @@ Commit_tree__get__(Commit *commit)
return (PyObject*)py_tree;
}

PyDoc_STRVAR(Commit_tree_id__doc__, "The id of the tree attached to the commit.");

PyObject *
Commit_tree_id__get__(Commit *commit)
{
return git_oid_to_python(git_commit_tree_id(commit->commit));
}

PyDoc_STRVAR(Commit_parents__doc__, "The list of parent commits.");

Expand Down Expand Up @@ -192,6 +199,28 @@ Commit_parents__get__(Commit *self)
return list;
}

PyDoc_STRVAR(Commit_parent_ids__doc__, "The list of parent commits' ids.");

PyObject *
Commit_parent_ids__get__(Commit *self)
{
unsigned int i, parent_count;
const git_oid *id;
PyObject *list;

parent_count = git_commit_parentcount(self->commit);
list = PyList_New(parent_count);
if (!list)
return NULL;

for (i=0; i < parent_count; i++) {
id = git_commit_parent_id(self->commit, i);
PyList_SET_ITEM(list, i, git_oid_to_python(id));
}

return list;
}

PyGetSetDef Commit_getseters[] = {
GETTER(Commit, message_encoding),
GETTER(Commit, message),
Expand All @@ -201,7 +230,9 @@ PyGetSetDef Commit_getseters[] = {
GETTER(Commit, committer),
GETTER(Commit, author),
GETTER(Commit, tree),
GETTER(Commit, tree_id),
GETTER(Commit, parents),
GETTER(Commit, parent_ids),
{NULL}
};

Expand Down
6 changes: 5 additions & 1 deletion test/test_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from __future__ import unicode_literals
import unittest

from pygit2 import GIT_OBJ_COMMIT, Signature
from pygit2 import GIT_OBJ_COMMIT, Signature, Oid
from . import utils


Expand Down Expand Up @@ -92,8 +92,10 @@ def test_new_commit(self):
self.assertEqualSignature(committer, commit.committer)
self.assertEqualSignature(author, commit.author)
self.assertEqual(tree, commit.tree.hex)
self.assertEqual(Oid(hex=tree), commit.tree_id)
self.assertEqual(1, len(commit.parents))
self.assertEqual(COMMIT_SHA, commit.parents[0].hex)
self.assertEqual(Oid(hex=COMMIT_SHA), commit.parent_ids[0])

def test_new_commit_encoding(self):
repo = self.repo
Expand All @@ -118,8 +120,10 @@ def test_new_commit_encoding(self):
self.assertEqualSignature(committer, commit.committer)
self.assertEqualSignature(author, commit.author)
self.assertEqual(tree, commit.tree.hex)
self.assertEqual(Oid(hex=tree), commit.tree_id)
self.assertEqual(1, len(commit.parents))
self.assertEqual(COMMIT_SHA, commit.parents[0].hex)
self.assertEqual(Oid(hex=COMMIT_SHA), commit.parent_ids[0])

def test_modify_commit(self):
message = 'New commit.\n\nMessage.\n'
Expand Down