Permalink
Browse files

Refs are now truly dynamic - this costs a little bit of (persistent c…

…ommand) work, but assures refs behave as expected
  • Loading branch information...
1 parent c5df444 commit 832b56394b079c9f6e4c777934447a9e224facfe @Byron Byron committed Oct 14, 2009
Showing with 23 additions and 17 deletions.
  1. +6 −0 CHANGES
  2. +13 −14 lib/git/refs.py
  3. +4 −3 test/git/test_base.py
View
@@ -63,6 +63,12 @@ Tree
* now mimics behaviour of a read-only list instead of a dict to maintain order.
* content_from_string method is now private and not part of the public API anymore
+Refs
+----
+* Will dynmically retrieve their object at the time of query to assure the information
+ is actual. Recently objects would be cached, hence ref object not be safely kept
+ persistent.
+
0.1.6
=====
View
@@ -14,7 +14,7 @@ class Ref(LazyMixin, Iterable):
"""
Represents a named reference to any object
"""
- __slots__ = ("repo", "path", "object")
+ __slots__ = ("repo", "path")
def __init__(self, repo, path, object = None):
"""
@@ -34,18 +34,6 @@ def __init__(self, repo, path, object = None):
if object is not None:
self.object = object
- def _set_cache_(self, attr):
- if attr == "object":
- # have to be dynamic here as we may be a tag which can point to anything
- # it uses our path to stay dynamic
- hexsha, typename, size = self.repo.git.get_object_header(self.path)
- # pin-point our object to a specific sha, even though it might not
- # reflect the our cached object anymore in case our rev now points
- # to a different commit
- self.object = get_object_type_by_name(typename)(self.repo, hexsha)
- else:
- super(Ref, self)._set_cache_(attr)
-
def __str__(self):
return self.name
@@ -74,7 +62,18 @@ def name(self):
return self.path # could be refs/HEAD
return '/'.join(tokens[2:])
-
+
+ @property
+ def object(self):
+ """
+ Returns
+ The object our ref currently refers to. Refs can be cached, they will
+ always point to the actual object as it gets re-created on each query
+ """
+ # have to be dynamic here as we may be a tag which can point to anything
+ hexsha, typename, size = self.repo.git.get_object_header(self.path)
+ return get_object_type_by_name(typename)(self.repo, hexsha)
+
@classmethod
def iter_items(cls, repo, common_path = "refs", **kwargs):
"""
@@ -78,9 +78,10 @@ def test_heads(self):
for head in self.repo.heads:
head.name
head.path
- cur_obj = head.object
- del( head.object )
- assert cur_obj == head.object
+ prev_object = head.object
+ cur_object = head.object
+ assert prev_object == cur_object # represent the same git object
+ assert prev_object is not cur_object # but are different instances
# END for each head
def test_get_object_type_by_name(self):

0 comments on commit 832b563

Please sign in to comment.