Skip to content

Commit a92ab80

Browse files
committed
Basic remote functionality moved to Reference type, as it can in fact be useful for tags as well, which might end up somewhere in the refs/remotes space. Its not likely that it will ever be used on a pure Reference instance though, but it is the smallest common base
1 parent c555840 commit a92ab80

File tree

3 files changed

+43
-19
lines changed

3 files changed

+43
-19
lines changed

‎git/refs/reference.py

+41
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@
1313

1414
__all__ = ["Reference"]
1515

16+
#{ Utilities
17+
def require_remote_ref_path(func):
18+
"""A decorator raising a TypeError if we are not a valid remote, based on the path"""
19+
def wrapper(self, *args):
20+
if not self.path.startswith(self._remote_common_path_default + "/"):
21+
raise ValueError("ref path does not point to a remote reference: %s" % path)
22+
return func(self, *args)
23+
#END wrapper
24+
wrapper.__name__ = func.__name__
25+
return wrapper
26+
#}END utilites
27+
1628

1729
class Reference(SymbolicReference, LazyMixin, Iterable):
1830
"""Represents a named reference to any object. Subclasses may apply restrictions though,
@@ -38,6 +50,8 @@ def __init__(self, repo, path, check_path = True):
3850

3951
def __str__(self):
4052
return self.name
53+
54+
#{ Interface
4155

4256
def set_object(self, object, logmsg = None):
4357
"""Special version which checks if the head-log needs an update as well"""
@@ -84,3 +98,30 @@ def iter_items(cls, repo, common_path = None):
8498
"""Equivalent to SymbolicReference.iter_items, but will return non-detached
8599
references as well."""
86100
return cls._iter_items(repo, common_path)
101+
102+
#}END interface
103+
104+
105+
#{ Remote Interface
106+
107+
@property
108+
@require_remote_ref_path
109+
def remote_name(self):
110+
"""
111+
:return:
112+
Name of the remote we are a reference of, such as 'origin' for a reference
113+
named 'origin/master'"""
114+
tokens = self.path.split('/')
115+
# /refs/remotes/<remote name>/<branch_name>
116+
return tokens[2]
117+
118+
@property
119+
@require_remote_ref_path
120+
def remote_head(self):
121+
""":return: Name of the remote head itself, i.e. master.
122+
:note: The returned name is usually not qualified enough to uniquely identify
123+
a branch"""
124+
tokens = self.path.split('/')
125+
return '/'.join(tokens[3:])
126+
127+
#} END remote interface

‎git/refs/remote.py

+1-19
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
class RemoteReference(Head):
1212
"""Represents a reference pointing to a remote head."""
13-
_common_path_default = "refs/remotes"
13+
_common_path_default = Head._remote_common_path_default
1414

1515

1616
@classmethod
@@ -22,24 +22,6 @@ def iter_items(cls, repo, common_path = None, remote=None):
2222
# END handle remote constraint
2323
return super(RemoteReference, cls).iter_items(repo, common_path)
2424

25-
@property
26-
def remote_name(self):
27-
"""
28-
:return:
29-
Name of the remote we are a reference of, such as 'origin' for a reference
30-
named 'origin/master'"""
31-
tokens = self.path.split('/')
32-
# /refs/remotes/<remote name>/<branch_name>
33-
return tokens[2]
34-
35-
@property
36-
def remote_head(self):
37-
""":return: Name of the remote head itself, i.e. master.
38-
:note: The returned name is usually not qualified enough to uniquely identify
39-
a branch"""
40-
tokens = self.path.split('/')
41-
return '/'.join(tokens[3:])
42-
4325
@classmethod
4426
def delete(cls, repo, *refs, **kwargs):
4527
"""Delete the given remote references.

‎git/refs/symbolic.py

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class SymbolicReference(object):
3333
_resolve_ref_on_create = False
3434
_points_to_commits_only = True
3535
_common_path_default = ""
36+
_remote_common_path_default = "refs/remotes"
3637
_id_attribute_ = "name"
3738

3839
def __init__(self, repo, path):

0 commit comments

Comments
 (0)