@@ -178,9 +178,14 @@ def _set_reference(self, ref):
178178 if write_value .startswith ('ref:' ):
179179 self .repo .git .symbolic_ref (self .path , write_value [5 :])
180180 return
181- # END non-detached handling
181+ # END non-detached handling
182182
183- fp = open (self ._get_path (), "w" )
183+ path = self ._get_path ()
184+ directory = os .path .dirname (path )
185+ if not os .path .isdir (directory ):
186+ os .makedirs (directory )
187+
188+ fp = open (path , "w" )
184189 try :
185190 fp .write (write_value )
186191 finally :
@@ -362,6 +367,68 @@ def from_path(cls, repo, path):
362367 raise ValueError ("Could not find reference type suitable to handle path %r" % path )
363368
364369
370+ @classmethod
371+ def _to_full_path (cls , repo , path ):
372+ full_ref_path = path
373+ if not path .startswith (cls ._common_path_default + "/" ):
374+ full_ref_path = '%s/%s' % (cls ._common_path_default , path )
375+ return full_ref_path
376+
377+ @classmethod
378+ def create (cls , repo , path , commit = 'HEAD' , force = False ):
379+ """
380+ Create a new reference.
381+ ``repo``
382+ Repository to create the reference in
383+
384+ ``path``
385+ The relative path of the reference, i.e. 'new_branch' or
386+ feature/feature1. The path prefix 'refs/' is implied if not
387+ given explicitly
388+
389+ ``commit``
390+ Commit to which the new reference should point, defaults to the
391+ current HEAD
392+
393+ ``force``
394+ if True, force creation even if a reference with that name already exists.
395+ Raise OSError otherwise
396+
397+ Returns
398+ Newly created Reference
399+
400+ Note
401+ This does not alter the current HEAD, index or Working Tree
402+ """
403+ full_ref_path = cls ._to_full_path (repo , path )
404+
405+ abs_ref_path = os .path .join (repo .path , full_ref_path )
406+ if not force and os .path .isfile (abs_ref_path ):
407+ raise OSError ("Reference at %s does already exist" % full_ref_path )
408+
409+ obj = Object .new (repo , commit )
410+ ref = cls (repo , full_ref_path )
411+ ref .reference = obj
412+
413+ return ref
414+
415+ @classmethod
416+ def delete (cls , repo , path ):
417+ """Delete the reference at the given path
418+
419+ ``repo``
420+ Repository to delete the reference from
421+
422+ ``path``
423+ Short or full path pointing to the reference, i.e. refs/myreference
424+ or just "myreference", hence 'refs/' is implied.
425+ """
426+ full_ref_path = cls ._to_full_path (repo , path )
427+ abs_path = os .path .join (repo .path , full_ref_path )
428+ if os .path .exists (abs_path ):
429+ os .remove (abs_path )
430+
431+
365432class HEAD (SymbolicReference ):
366433 """
367434 Special case of a Symbolic Reference as it represents the repository's
0 commit comments