summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Thiel <byronimo@gmail.com>2009-10-16 16:09:07 +0200
committerSebastian Thiel <byronimo@gmail.com>2009-10-16 16:09:07 +0200
commitbb24f67e64b4ebe11c4d3ce7df021a6ad7ca98f2 (patch)
tree391ba27fa04e7d6ef65fb7d371b07475eb4263c6
parent919164df96d9f956c8be712f33a9a037b097745b (diff)
downloadgitpython-bb24f67e64b4ebe11c4d3ce7df021a6ad7ca98f2.tar.gz
Fixed object bug that would cause object ids not to be resolved to sha's as this was assumed - now there is a test for it as well
repo: removed diff and commit_diff methods, added 'head' property returning the current head as Reference object
-rw-r--r--CHANGES2
-rw-r--r--lib/git/objects/base.py32
-rw-r--r--lib/git/refs.py2
-rw-r--r--lib/git/repo.py38
-rw-r--r--test/git/test_base.py3
-rw-r--r--test/git/test_repo.py3
6 files changed, 39 insertions, 41 deletions
diff --git a/CHANGES b/CHANGES
index 96a1ef56..f4a4bae0 100644
--- a/CHANGES
+++ b/CHANGES
@@ -60,6 +60,8 @@ Repo
related repositories, i.e. clones, git-rev-list would be sufficient to find
commits that would need to be transferred for example.
- 'create' method which equals the 'init' method's functionality
+ - 'diff' - it returned a mere string which still had to be parsed
+ - 'commit_diff' - moved to Commit, Tree and Diff types respectively
* Renamed the following methods:
- commits to iter_commits to improve the performance, adjusted signature
- init_bare to init, implying less about the options to be used
diff --git a/lib/git/objects/base.py b/lib/git/objects/base.py
index 3b48e066..d780c7b3 100644
--- a/lib/git/objects/base.py
+++ b/lib/git/objects/base.py
@@ -15,22 +15,12 @@ class Object(LazyMixin):
This Object also serves as a constructor for instances of the correct type::
- inst = Object(repo,id)
+ inst = Object.new(repo,id)
"""
TYPES = ("blob", "tree", "commit", "tag")
__slots__ = ("repo", "id", "size", "data" )
type = None # to be set by subclass
- def __new__(cls, repo, id, *args, **kwargs):
- if cls is Object:
- hexsha, typename, size = repo.git.get_object_header(id)
- obj_type = utils.get_object_type_by_name(typename)
- inst = super(Object,cls).__new__(obj_type, repo, hexsha, *args, **kwargs)
- inst.size = size
- return inst
- else:
- return super(Object,cls).__new__(cls, repo, id, *args, **kwargs)
-
def __init__(self, repo, id):
"""
Initialize an object by identifying it by its id. All keyword arguments
@@ -45,7 +35,25 @@ class Object(LazyMixin):
super(Object,self).__init__()
self.repo = repo
self.id = id
-
+
+ @classmethod
+ def new(cls, repo, id):
+ """
+ Return
+ New Object instance of a type appropriate to the object type behind
+ id. The id of the newly created object will be a hexsha even though
+ the input id may have been a Reference or Rev-Spec
+
+ Note
+ This cannot be a __new__ method as it would always call __init__
+ with the input id which is not necessarily a hexsha.
+ """
+ hexsha, typename, size = repo.git.get_object_header(id)
+ obj_type = utils.get_object_type_by_name(typename)
+ inst = obj_type(repo, hexsha)
+ inst.size = size
+ return inst
+
def _set_self_from_args_(self, args_dict):
"""
Initialize attributes on self from the given dict that was retrieved
diff --git a/lib/git/refs.py b/lib/git/refs.py
index a4d7bbb1..4445f252 100644
--- a/lib/git/refs.py
+++ b/lib/git/refs.py
@@ -72,7 +72,7 @@ class Reference(LazyMixin, Iterable):
"""
# have to be dynamic here as we may be a tag which can point to anything
# Our path will be resolved to the hexsha which will be used accordingly
- return Object(self.repo, self.path)
+ return Object.new(self.repo, self.path)
@classmethod
def iter_items(cls, repo, common_path = "refs", **kwargs):
diff --git a/lib/git/repo.py b/lib/git/repo.py
index 6edb7f62..a77ced3c 100644
--- a/lib/git/repo.py
+++ b/lib/git/repo.py
@@ -107,6 +107,15 @@ class Repo(object):
branches = heads
@property
+ def head(self):
+ """
+ Return
+ Head Object, reference pointing to the current head of the repository
+ """
+ return Head(self,'HEAD')
+
+
+ @property
def tags(self):
"""
A list of ``Tag`` objects that are available in this repo
@@ -129,7 +138,7 @@ class Repo(object):
if rev is None:
rev = self.active_branch
- c = Object(self, rev)
+ c = Object.new(self, rev)
assert c.type == "commit", "Revision %s did not point to a commit, but to %s" % (rev, c)
return c
@@ -327,34 +336,7 @@ class Repo(object):
"""
return Head( self, self.git.symbolic_ref('HEAD').strip() )
-
- def diff(self, a, b, *paths):
- """
- The diff from commit ``a`` to commit ``b``, optionally restricted to the given file(s)
-
- ``a``
- is the base commit
- ``b``
- is the other commit
-
- ``paths``
- is an optional list of file paths on which to restrict the diff
- Returns
- ``str``
- """
- return self.git.diff(a, b, '--', *paths)
-
- def commit_diff(self, commit):
- """
- The commit diff for the given commit
- ``commit`` is the commit name/id
-
- Returns
- ``git.Diff[]``
- """
- return Commit.diff(self, commit)
-
def blame(self, rev, file):
"""
The blame information for the given file at the given revision.
diff --git a/test/git/test_base.py b/test/git/test_base.py
index 04222e2e..71576048 100644
--- a/test/git/test_base.py
+++ b/test/git/test_base.py
@@ -90,3 +90,6 @@ class TestBase(object):
assert_raises( ValueError, get_object_type_by_name, "doesntexist" )
+ def test_object_resolution(self):
+ # objects must be resolved to shas so they compare equal
+ assert self.repo.head.object == self.repo.active_branch.object
diff --git a/test/git/test_repo.py b/test/git/test_repo.py
index 250974a5..e0bda1c5 100644
--- a/test/git/test_repo.py
+++ b/test/git/test_repo.py
@@ -199,6 +199,9 @@ class TestRepo(object):
assert_equal(self.repo.active_branch.name, 'major-refactoring')
assert_equal(git.call_args, (('symbolic_ref', 'HEAD'), {}))
+ def test_head(self):
+ assert self.repo.head.object == self.repo.active_branch.object
+
@patch_object(Git, '_call_process')
def test_should_display_blame_information(self, git):
git.return_value = fixture('blame')