1313
1414class Reference (LazyMixin , Iterable ):
1515 """
16- Represents a named reference to any object
16+ Represents a named reference to any object. Subclasses may apply restrictions though,
17+ i.e. Heads can only point to commits.
1718 """
1819 __slots__ = ("repo" , "path" )
1920 _common_path_default = "refs"
@@ -75,30 +76,16 @@ def _get_object(self):
7576 # Our path will be resolved to the hexsha which will be used accordingly
7677 return Object .new (self .repo , self .path )
7778
78- def _set_object (self , ref , type = None ):
79+ def _set_object (self , ref ):
7980 """
8081 Set our reference to point to the given ref. It will be converted
8182 to a specific hexsha.
8283
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-
87- Returns
88- Object we have set. This is used internally only to reduce the amount
89- of calls to the git command
84+ Note:
85+ TypeChecking is done by the git command
9086 """
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
87+ # do it safely by specifying the old value
88+ self .repo .git .update_ref (self .path , ref , self ._get_object ().sha )
10289
10390 object = property (_get_object , _set_object , doc = "Return the object our ref currently refers to" )
10491
@@ -109,7 +96,7 @@ def _set_commit(self, commit):
10996 Raise
11097 ValueError if commit does not actually point to a commit
11198 """
112- self ._set_object (commit , type = "commit" )
99+ self ._set_object (commit )
113100
114101 def _get_commit (self ):
115102 """
@@ -327,6 +314,15 @@ def _set_reference(self, ref):
327314 raise ValueError ("Could not extract object from %s" % ref )
328315 # END end try string
329316 # END try commit attribute
317+
318+ # if we are writing a ref, use symbolic ref to get the reflog and more
319+ # checking
320+ # Otherwise we detach it and have to do it manually
321+ if write_value .startswith ('ref:' ):
322+ self .repo .git .symbolic_ref (self .name , write_value [5 :])
323+ return
324+ # END non-detached handling
325+
330326 fp = open (self ._get_path (), "w" )
331327 try :
332328 fp .write (write_value )
0 commit comments