summaryrefslogtreecommitdiff
path: root/lib/git/refs.py
diff options
context:
space:
mode:
authorSebastian Thiel <byronimo@gmail.com>2009-10-23 16:07:45 +0200
committerSebastian Thiel <byronimo@gmail.com>2009-10-23 16:07:45 +0200
commit9b9776e88f7abb59cebac8733c04cccf6eee1c60 (patch)
treef7bc7af48ddb55eafde1d1c91d8014ec595208fa /lib/git/refs.py
parent1047b41e2e925617474e2e7c9927314f71ce7365 (diff)
downloadgitpython-9b9776e88f7abb59cebac8733c04cccf6eee1c60.tar.gz
Refs can now set the reference they are pointing to in a controlled fashion by writing their ref file directly
Diffstat (limited to 'lib/git/refs.py')
-rw-r--r--lib/git/refs.py48
1 files changed, 42 insertions, 6 deletions
diff --git a/lib/git/refs.py b/lib/git/refs.py
index 0efee52e..47c37af6 100644
--- a/lib/git/refs.py
+++ b/lib/git/refs.py
@@ -65,8 +65,7 @@ class Reference(LazyMixin, Iterable):
return '/'.join(tokens[2:])
- @property
- def object(self):
+ def _get_object(self):
"""
Returns
The object our ref currently refers to. Refs can be cached, they will
@@ -76,17 +75,54 @@ class Reference(LazyMixin, Iterable):
# Our path will be resolved to the hexsha which will be used accordingly
return Object.new(self.repo, self.path)
- @property
- def commit(self):
+ def _set_object(self, ref, type=None):
"""
+ Set our reference to point to the given ref. It will be converted
+ to a specific hexsha.
+
+ ``type``
+ If not None, string type of that the object must have, other we raise
+ a type error. Only used internally
+
Returns
- Commit object the head points to
+ Object we have set. This is used internally only to reduce the amount
+ of calls to the git command
+ """
+ obj = Object.new(self.repo, ref)
+ if type is not None and obj.type != type:
+ raise TypeError("Reference %r cannot point to object of type %r" % (self,obj.type))
+
+ full_ref_path = os.path.join(self.repo.path, self.path)
+ fp = open(full_ref_path, "w")
+ try:
+ fp.write(str(obj))
+ finally:
+ fp.close()
+ return obj
+
+ object = property(_get_object, _set_object, doc="Return the object our ref currently refers to")
+
+ def _set_commit(self, commit):
+ """
+ Set ourselves to point to the given commit.
+
+ Raise
+ ValueError if commit does not actually point to a commit
+ """
+ self._set_object(commit, type="commit")
+
+ def _get_commit(self):
+ """
+ Returns
+ Commit object the reference points to
"""
commit = self.object
if commit.type != "commit":
raise TypeError("Object of reference %s did not point to a commit" % self)
return commit
+ commit = property(_get_commit, _set_commit, doc="Return Commit object the reference points to")
+
@classmethod
def iter_items(cls, repo, common_path = None, **kwargs):
"""
@@ -244,7 +280,7 @@ class SymbolicReference(object):
try:
tokens = fp.readline().rstrip().split(' ')
if tokens[0] != 'ref:':
- raise TypeError("%s is a detached symbolic reference as it points to %r" % tokens[0])
+ raise TypeError("%s is a detached symbolic reference as it points to %r" % (self, tokens[0]))
return Reference.from_path(self.repo, tokens[1])
finally:
fp.close()