@@ -65,8 +65,7 @@ def name(self):
6565
6666 return '/' .join (tokens [2 :])
6767
68- @property
69- def object (self ):
68+ def _get_object (self ):
7069 """
7170 Returns
7271 The object our ref currently refers to. Refs can be cached, they will
@@ -76,17 +75,54 @@ def object(self):
7675 # Our path will be resolved to the hexsha which will be used accordingly
7776 return Object .new (self .repo , self .path )
7877
79- @property
80- def commit (self ):
78+ def _set_object (self , ref , type = None ):
8179 """
80+ Set our reference to point to the given ref. It will be converted
81+ to a specific hexsha.
82+
83+ ``type``
84+ If not None, string type of that the object must have, other we raise
85+ a type error. Only used internally
86+
8287 Returns
83- Commit object the head points to
88+ Object we have set. This is used internally only to reduce the amount
89+ of calls to the git command
90+ """
91+ obj = Object .new (self .repo , ref )
92+ if type is not None and obj .type != type :
93+ raise TypeError ("Reference %r cannot point to object of type %r" % (self ,obj .type ))
94+
95+ full_ref_path = os .path .join (self .repo .path , self .path )
96+ fp = open (full_ref_path , "w" )
97+ try :
98+ fp .write (str (obj ))
99+ finally :
100+ fp .close ()
101+ return obj
102+
103+ object = property (_get_object , _set_object , doc = "Return the object our ref currently refers to" )
104+
105+ def _set_commit (self , commit ):
106+ """
107+ Set ourselves to point to the given commit.
108+
109+ Raise
110+ ValueError if commit does not actually point to a commit
111+ """
112+ self ._set_object (commit , type = "commit" )
113+
114+ def _get_commit (self ):
115+ """
116+ Returns
117+ Commit object the reference points to
84118 """
85119 commit = self .object
86120 if commit .type != "commit" :
87121 raise TypeError ("Object of reference %s did not point to a commit" % self )
88122 return commit
89123
124+ commit = property (_get_commit , _set_commit , doc = "Return Commit object the reference points to" )
125+
90126 @classmethod
91127 def iter_items (cls , repo , common_path = None , ** kwargs ):
92128 """
@@ -244,7 +280,7 @@ def _get_reference(self):
244280 try :
245281 tokens = fp .readline ().rstrip ().split (' ' )
246282 if tokens [0 ] != 'ref:' :
247- raise TypeError ("%s is a detached symbolic reference as it points to %r" % tokens [0 ])
283+ raise TypeError ("%s is a detached symbolic reference as it points to %r" % ( self , tokens [0 ]) )
248284 return Reference .from_path (self .repo , tokens [1 ])
249285 finally :
250286 fp .close ()
0 commit comments