From 611e979113e2e2d877ba5c206541beef9ffaf393 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20David=20Ib=C3=A1=C3=B1ez?= Date: Wed, 17 Apr 2013 08:16:43 +0200 Subject: [PATCH] Now Repository.head returns a reference (#203) Now Repository.head behaves like libgit2's git_repository_head, it returns the resolved reference. These two lines are equivalent: ref = repo.head ref = repo.lookup_reference('HEAD').resolve() Before it returned a commit. --- docs/diff.rst | 13 ++++++------- docs/objects.rst | 3 --- docs/references.rst | 18 ++++++++++++------ docs/repository.rst | 3 --- src/repository.c | 5 +---- test/test_repository.py | 20 ++++++++++++++------ 6 files changed, 33 insertions(+), 29 deletions(-) diff --git a/docs/diff.rst b/docs/diff.rst index 53ace3a..513d08e 100644 --- a/docs/diff.rst +++ b/docs/diff.rst @@ -4,19 +4,18 @@ Diff A diff shows the changes between trees, an index or the working dir:: + >>> head = repo.revparse_single('HEAD') + # Diff two trees - >>> t0 = repo.head.tree - >>> t1 = repo.head.parents[0].tree + >>> t0 = head.tree + >>> t1 = head.parents[0].tree >>> diff = t1.diff(t0) - >>> diff # Diff a tree with the index - >>> tree = repo.head.tree - >>> diff = tree.diff(repo.index) + >>> diff = head.tree.diff(repo.index) # Diff a tree with the current working dir - >>> tree = repo.head.tree - >>> diff = tree.diff() + >>> diff = head.tree.diff() The Diff type diff --git a/docs/objects.rst b/docs/objects.rst index c49a0fb..aa7534c 100644 --- a/docs/objects.rst +++ b/docs/objects.rst @@ -10,9 +10,6 @@ In the first place Git is a key-value storage system. The values stored are called *objects*, there are four types (commits, trees, blobs and tags), for each type pygit2 has a Python class:: - >>> # Get the last commit - >>> head = repo.head - >>> # Show commits and trees >>> commit diff --git a/docs/references.rst b/docs/references.rst index 0725c9e..8cab025 100644 --- a/docs/references.rst +++ b/docs/references.rst @@ -17,12 +17,11 @@ Reference log:: >>> for entry in head.log(): ... print(entry.message) -The interface for RefLogEntry:: +Shortcuts:: - RefLogEntry.committer -- Signature of Committer - RefLogEntry.message -- the message of the RefLogEntry - RefLogEntry.oid_old -- oid of old reference - RefLogEntry.oid_new -- oid of new reference + >>> # These two lines are equivalent + >>> head = repo.head + >>> head = repo.lookup_reference('HEAD').resolve() The Reference type @@ -40,6 +39,14 @@ The Reference type .. automethod:: pygit2.Reference.log +The HEAD +-------------------- + +.. autoattribute:: pygit2.Repository.head +.. autoattribute:: pygit2.Repository.head_is_detached +.. autoattribute:: pygit2.Repository.head_is_orphaned + + The reference log -------------------- @@ -48,7 +55,6 @@ The reference log .. autoattribute:: pygit2.RefLogEntry.message .. autoattribute:: pygit2.RefLogEntry.committer - Notes ==================== diff --git a/docs/repository.rst b/docs/repository.rst index e13160b..08ab3dd 100644 --- a/docs/repository.rst +++ b/docs/repository.rst @@ -41,6 +41,3 @@ To open an existing repository:: .. autoattribute:: pygit2.Repository.is_empty .. automethod:: pygit2.Repository.read .. automethod:: pygit2.Repository.write -.. autoattribute:: pygit2.Repository.head -.. autoattribute:: pygit2.Repository.head_is_detached -.. autoattribute:: pygit2.Repository.head_is_orphaned diff --git a/src/repository.c b/src/repository.c index 02748cd..4df5047 100644 --- a/src/repository.c +++ b/src/repository.c @@ -183,10 +183,7 @@ Repository_head__get__(Repository *self) return NULL; } - oid = git_reference_target(head); - pyobj = lookup_object(self, oid, GIT_OBJ_COMMIT); - git_reference_free(head); - return pyobj; + return wrap_reference(head); } diff --git a/test/test_repository.py b/test/test_repository.py index 0e7503d..8805559 100644 --- a/test/test_repository.py +++ b/test/test_repository.py @@ -27,18 +27,21 @@ """Tests for Repository objects.""" +# Import from the future from __future__ import absolute_import from __future__ import unicode_literals + +# Import from the Standard Library import binascii import unittest import tempfile import os from os.path import join, realpath +# Import from pygit2 from pygit2 import GIT_OBJ_ANY, GIT_OBJ_BLOB, GIT_OBJ_COMMIT -from pygit2 import init_repository, discover_repository, Commit, hashfile +from pygit2 import init_repository, discover_repository, Reference, hashfile import pygit2 - from . import utils @@ -59,7 +62,7 @@ class RepositoryTest(utils.BareRepoTestCase): def test_head(self): head = self.repo.head self.assertEqual(HEAD_SHA, head.hex) - self.assertEqual(type(head), Commit) + self.assertEqual(type(head), Reference) self.assertFalse(self.repo.head_is_orphaned) self.assertFalse(self.repo.head_is_detached) @@ -191,10 +194,15 @@ class RepositoryTest_II(utils.RepoTestCase): lambda: self.repo.checkout(reference=ref_i18n)) # checkout i18n with GIT_CHECKOUT_FORCE - self.assertTrue('new' not in self.repo.head.tree) + head = self.repo.head + head = self.repo[head.target] + self.assertTrue('new' not in head.tree) self.repo.checkout(pygit2.GIT_CHECKOUT_FORCE, ref_i18n) - self.assertEqual(self.repo.head.hex, self.repo[ref_i18n.target].hex) - self.assertTrue('new' in self.repo.head.tree) + + head = self.repo.head + head = self.repo[head.target] + self.assertEqual(head.hex, self.repo[ref_i18n.target].hex) + self.assertTrue('new' in head.tree) self.assertTrue('bye.txt' not in self.repo.status()) def test_checkout_index(self):