@@ -309,6 +309,47 @@ def create(cls, repo, path, reference='HEAD', force=False ):
309
309
This does not alter the current HEAD, index or Working Tree
310
310
"""
311
311
return cls ._create (repo , path , False , reference , force )
312
+
313
+ def rename (self , new_path , force = False ):
314
+ """
315
+ Rename self to a new path
316
+
317
+ ``new_path``
318
+ Either a simple name or a full path, i.e. new_name or features/new_name.
319
+ The prefix refs/ is implied for references and will be set as needed.
320
+ In case this is a symbolic ref, there is no implied prefix
321
+
322
+ ``force``
323
+ If True, the rename will succeed even if a head with the target name
324
+ already exists. It will be overwritten in that case
325
+
326
+ Returns
327
+ self
328
+
329
+ Raises OSError:
330
+ In case a file at path with that name already exists
331
+ """
332
+ new_path = self ._to_full_path (self .repo , new_path )
333
+ if self .path == new_path :
334
+ return self
335
+
336
+ new_abs_path = os .path .join (self .repo .git_dir , new_path )
337
+ if os .path .isfile (new_abs_path ):
338
+ if not force :
339
+ raise OSError ("File at path %r already exists" % new_abs_path )
340
+ os .remove (new_abs_path )
341
+ # END handle existing target file
342
+
343
+ dirname = os .path .dirname (new_abs_path )
344
+ if not os .path .isdir (dirname ):
345
+ os .makedirs (dirname )
346
+ # END create directory
347
+
348
+ cur_abs_path = os .path .join (self .repo .git_dir , self .path )
349
+ os .rename (cur_abs_path , new_abs_path )
350
+ self .path = new_path
351
+
352
+ return self
312
353
313
354
314
355
class Reference (SymbolicReference , LazyMixin , Iterable ):
@@ -330,7 +371,7 @@ def __init__(self, repo, path):
330
371
refs/heads/master
331
372
332
373
"""
333
- if not path .startswith (self ._common_path_default ):
374
+ if not path .startswith (self ._common_path_default + '/' ):
334
375
raise ValueError ("Cannot instantiate %s from path %s" % ( self .__class__ .__name__ , path ))
335
376
super (Reference , self ).__init__ (repo , path )
336
377
@@ -472,6 +513,7 @@ def create(cls, repo, path, commit='HEAD', force=False ):
472
513
"""
473
514
return cls ._create (repo , path , True , commit , force )
474
515
516
+
475
517
476
518
class HEAD (SymbolicReference ):
477
519
"""
@@ -623,6 +665,9 @@ def rename(self, new_path, force=False):
623
665
624
666
Returns
625
667
self
668
+
669
+ Note
670
+ respects the ref log as git commands are used
626
671
"""
627
672
flag = "-m"
628
673
if force :
0 commit comments