Merge remote branch 'cholin/head_getter'

This commit is contained in:
J. David Ibáñez 2012-05-31 09:52:54 +02:00
commit a55d4da18a
4 changed files with 38 additions and 1 deletions

@ -13,6 +13,7 @@ int Repository_contains(Repository *self, PyObject *value);
git_odb_object* Repository_read_raw(git_repository *repo, const git_oid *oid, size_t len);
PyObject* Repository_head(Repository *self);
PyObject* Repository_getitem(Repository *self, PyObject *value);
PyObject* Repository_read(Repository *self, PyObject *py_hex);
PyObject* Repository_write(Repository *self, PyObject *args);

@ -7,6 +7,8 @@
#include <pygit2/oid.h>
#include <pygit2/repository.h>
extern PyObject *GitError;
extern PyTypeObject IndexType;
extern PyTypeObject WalkerType;
extern PyTypeObject SignatureType;
@ -155,6 +157,29 @@ Repository_contains(Repository *self, PyObject *value)
return exists;
}
PyObject *
Repository_head(Repository *self)
{
git_reference *head;
const git_oid *oid;
int err;
err = git_repository_head(&head, self->repo);
if(err < 0) {
if(err == GIT_ENOTFOUND)
PyErr_SetString(GitError, "head reference does not exist");
else
Error_set(err);
return NULL;
}
oid = git_reference_oid(head);
return lookup_object(self, oid, GIT_OBJ_COMMIT);
}
PyObject *
Repository_getitem(Repository *self, PyObject *value)
{
@ -740,6 +765,8 @@ PyGetSetDef Repository_getseters[] = {
{"index", (getter)Repository_get_index, NULL, "index file. ", NULL},
{"path", (getter)Repository_get_path, NULL,
"The normalized path to the git repository.", NULL},
{"head", (getter)Repository_head, NULL,
"Current head reference of the repository.", NULL},
{"workdir", (getter)Repository_get_workdir, NULL,
"The normalized path to the working directory of the repository. "
"If the repository is bare, None will be returned.", NULL},

@ -61,6 +61,9 @@ class ReferencesTest(utils.RepoTestCase):
self.assertEqual(repo.listall_references(GIT_REF_SYMBOLIC),
('refs/tags/version1', ))
def test_head(self):
head = self.repo.head
self.assertEqual(LAST_COMMIT, self.repo[head.oid].hex)
def test_lookup_reference(self):
repo = self.repo

@ -35,18 +35,24 @@ import os
from os.path import join, realpath
from pygit2 import GIT_OBJ_ANY, GIT_OBJ_BLOB, GIT_OBJ_COMMIT, init_repository, \
discover_repository
discover_repository, Commit
from . import utils
__author__ = 'dborowitz@google.com (Dave Borowitz)'
HEAD_SHA = '5fe808e8953c12735680c257f56600cb0de44b10'
A_HEX_SHA = 'af431f20fc541ed6d5afede3e2dc7160f6f01f16'
A_BIN_SHA = binascii.unhexlify(A_HEX_SHA.encode('ascii'))
class RepositoryTest(utils.BareRepoTestCase):
def test_head(self):
head = self.repo.head
self.assertTrue(HEAD_SHA, head.hex)
self.assertTrue(type(head), Commit)
def test_read(self):
self.assertRaises(TypeError, self.repo.read, 123)
self.assertRaisesWithArg(KeyError, '1' * 40, self.repo.read, '1' * 40)