@@ -20,6 +20,8 @@ class SymbolicReference(object):
20
20
A typical example for a symbolic reference is HEAD.
21
21
"""
22
22
__slots__ = ("repo" , "path" )
23
+ _common_path_default = ""
24
+ _id_attribute_ = "name"
23
25
24
26
def __init__ (self , repo , path ):
25
27
self .repo = repo
@@ -231,6 +233,79 @@ def from_path(cls, repo, path):
231
233
return SymbolicReference (repo , path )
232
234
233
235
raise ValueError ("Could not find symbolic reference type suitable to handle path %r" % path )
236
+
237
+ @classmethod
238
+ def _to_full_path (cls , repo , path ):
239
+ full_ref_path = path
240
+ if not cls ._common_path_default :
241
+ return full_ref_path
242
+ if not path .startswith (cls ._common_path_default + "/" ):
243
+ full_ref_path = '%s/%s' % (cls ._common_path_default , path )
244
+ return full_ref_path
245
+
246
+ @classmethod
247
+ def delete (cls , repo , path ):
248
+ """Delete the reference at the given path
249
+
250
+ ``repo``
251
+ Repository to delete the reference from
252
+
253
+ ``path``
254
+ Short or full path pointing to the reference, i.e. refs/myreference
255
+ or just "myreference", hence 'refs/' is implied.
256
+ """
257
+ full_ref_path = cls ._to_full_path (repo , path )
258
+ abs_path = os .path .join (repo .path , full_ref_path )
259
+ if os .path .exists (abs_path ):
260
+ os .remove (abs_path )
261
+
262
+ @classmethod
263
+ def _create (cls , repo , path , resolve , reference , force ):
264
+ """internal method used to create a new symbolic reference.
265
+ If resolve is False,, the reference will be taken as is, creating
266
+ a proper symbolic reference. Otherwise it will be resolved to the
267
+ corresponding object and a detached symbolic reference will be created
268
+ instead"""
269
+ full_ref_path = cls ._to_full_path (repo , path )
270
+
271
+ abs_ref_path = os .path .join (repo .path , full_ref_path )
272
+ if not force and os .path .isfile (abs_ref_path ):
273
+ raise OSError ("Reference at %s does already exist" % full_ref_path )
274
+
275
+ ref = cls (repo , full_ref_path )
276
+ target = reference
277
+ if resolve :
278
+ target = Object .new (repo , reference )
279
+
280
+ ref .reference = target
281
+ return ref
282
+
283
+ @classmethod
284
+ def create (cls , repo , path , reference = 'HEAD' , force = False ):
285
+ """
286
+ Create a new symbolic reference, hence a reference pointing to another
287
+ reference.
288
+ ``repo``
289
+ Repository to create the reference in
290
+
291
+ ``path``
292
+ full path at which the new symbolic reference is supposed to be
293
+ created at, i.e. "NEW_HEAD" or "symrefs/my_new_symref"
294
+
295
+ ``reference``
296
+ The reference to which the new symbolic reference should point to
297
+
298
+ ``force``
299
+ if True, force creation even if a symbolic reference with that name already exists.
300
+ Raise OSError otherwise
301
+
302
+ Returns
303
+ Newly created symbolic Reference
304
+
305
+ Note
306
+ This does not alter the current HEAD, index or Working Tree
307
+ """
308
+ return cls ._create (repo , path , False , reference , force )
234
309
235
310
236
311
class Reference (SymbolicReference , LazyMixin , Iterable ):
@@ -240,7 +315,6 @@ class Reference(SymbolicReference, LazyMixin, Iterable):
240
315
"""
241
316
__slots__ = tuple ()
242
317
_common_path_default = "refs"
243
- _id_attribute_ = "name"
244
318
245
319
def __init__ (self , repo , path ):
246
320
"""
@@ -367,13 +441,6 @@ def from_path(cls, repo, path):
367
441
raise ValueError ("Could not find reference type suitable to handle path %r" % path )
368
442
369
443
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
444
@classmethod
378
445
def create (cls , repo , path , commit = 'HEAD' , force = False ):
379
446
"""
@@ -400,33 +467,7 @@ def create(cls, repo, path, commit='HEAD', force=False ):
400
467
Note
401
468
This does not alter the current HEAD, index or Working Tree
402
469
"""
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 )
470
+ return cls ._create (repo , path , True , commit , force )
430
471
431
472
432
473
class HEAD (SymbolicReference ):
0 commit comments