added head-getter for Repository

You do not have to lookup and resolve the HEAD reference anymore if you simply want
the last commit. Repository has now a head-getter which combines all these steps.

example:
  repo = Repository('.')
  head = repo.head
  print(head.message)
This commit is contained in:
Nico von Geyso 2012-05-28 12:41:43 +02:00
parent e7b5560590
commit e402d0e9eb
3 changed files with 31 additions and 0 deletions
include/pygit2
src/pygit2
test

@ -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, len;
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