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.
This commit is contained in:
J. David Ibáñez
2013-04-17 08:16:43 +02:00
parent 072cc38607
commit 611e979113
6 changed files with 33 additions and 29 deletions

View File

@@ -4,19 +4,18 @@ Diff
A diff shows the changes between trees, an index or the working dir:: A diff shows the changes between trees, an index or the working dir::
>>> head = repo.revparse_single('HEAD')
# Diff two trees # Diff two trees
>>> t0 = repo.head.tree >>> t0 = head.tree
>>> t1 = repo.head.parents[0].tree >>> t1 = head.parents[0].tree
>>> diff = t1.diff(t0) >>> diff = t1.diff(t0)
>>> diff
# Diff a tree with the index # Diff a tree with the index
>>> tree = repo.head.tree >>> diff = head.tree.diff(repo.index)
>>> diff = tree.diff(repo.index)
# Diff a tree with the current working dir # Diff a tree with the current working dir
>>> tree = repo.head.tree >>> diff = head.tree.diff()
>>> diff = tree.diff()
The Diff type The Diff type

View File

@@ -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), called *objects*, there are four types (commits, trees, blobs and tags),
for each type pygit2 has a Python class:: for each type pygit2 has a Python class::
>>> # Get the last commit
>>> head = repo.head
>>> # Show commits and trees >>> # Show commits and trees
>>> commit >>> commit
<pygit2.Commit object at 0x7f9d2f3000b0> <pygit2.Commit object at 0x7f9d2f3000b0>

View File

@@ -17,12 +17,11 @@ Reference log::
>>> for entry in head.log(): >>> for entry in head.log():
... print(entry.message) ... print(entry.message)
The interface for RefLogEntry:: Shortcuts::
RefLogEntry.committer -- Signature of Committer >>> # These two lines are equivalent
RefLogEntry.message -- the message of the RefLogEntry >>> head = repo.head
RefLogEntry.oid_old -- oid of old reference >>> head = repo.lookup_reference('HEAD').resolve()
RefLogEntry.oid_new -- oid of new reference
The Reference type The Reference type
@@ -40,6 +39,14 @@ The Reference type
.. automethod:: pygit2.Reference.log .. 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 The reference log
-------------------- --------------------
@@ -48,7 +55,6 @@ The reference log
.. autoattribute:: pygit2.RefLogEntry.message .. autoattribute:: pygit2.RefLogEntry.message
.. autoattribute:: pygit2.RefLogEntry.committer .. autoattribute:: pygit2.RefLogEntry.committer
Notes Notes
==================== ====================

View File

@@ -41,6 +41,3 @@ To open an existing repository::
.. autoattribute:: pygit2.Repository.is_empty .. autoattribute:: pygit2.Repository.is_empty
.. automethod:: pygit2.Repository.read .. automethod:: pygit2.Repository.read
.. automethod:: pygit2.Repository.write .. automethod:: pygit2.Repository.write
.. autoattribute:: pygit2.Repository.head
.. autoattribute:: pygit2.Repository.head_is_detached
.. autoattribute:: pygit2.Repository.head_is_orphaned

View File

@@ -183,10 +183,7 @@ Repository_head__get__(Repository *self)
return NULL; return NULL;
} }
oid = git_reference_target(head); return wrap_reference(head);
pyobj = lookup_object(self, oid, GIT_OBJ_COMMIT);
git_reference_free(head);
return pyobj;
} }

View File

@@ -27,18 +27,21 @@
"""Tests for Repository objects.""" """Tests for Repository objects."""
# Import from the future
from __future__ import absolute_import from __future__ import absolute_import
from __future__ import unicode_literals from __future__ import unicode_literals
# Import from the Standard Library
import binascii import binascii
import unittest import unittest
import tempfile import tempfile
import os import os
from os.path import join, realpath from os.path import join, realpath
# Import from pygit2
from pygit2 import GIT_OBJ_ANY, GIT_OBJ_BLOB, GIT_OBJ_COMMIT 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 import pygit2
from . import utils from . import utils
@@ -59,7 +62,7 @@ class RepositoryTest(utils.BareRepoTestCase):
def test_head(self): def test_head(self):
head = self.repo.head head = self.repo.head
self.assertEqual(HEAD_SHA, head.hex) 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_orphaned)
self.assertFalse(self.repo.head_is_detached) self.assertFalse(self.repo.head_is_detached)
@@ -191,10 +194,15 @@ class RepositoryTest_II(utils.RepoTestCase):
lambda: self.repo.checkout(reference=ref_i18n)) lambda: self.repo.checkout(reference=ref_i18n))
# checkout i18n with GIT_CHECKOUT_FORCE # 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.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()) self.assertTrue('bye.txt' not in self.repo.status())
def test_checkout_index(self): def test_checkout_index(self):