Skip to content

Commit 87c7a6f

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 87aa78c commit 87c7a6f

File tree

3 files changed

+43
-19
lines changed

3 files changed

+43
-19
lines changed

git/refs/reference.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@
1111

1212
__all__ = ["Reference"]
1313

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

1527
class Reference(SymbolicReference, LazyMixin, Iterable):
1628
"""Represents a named reference to any object. Subclasses may apply restrictions though,
@@ -36,6 +48,8 @@ def __init__(self, repo, path, check_path = True):
3648

3749
def __str__(self):
3850
return self.name
51+
52+
#{ Interface
3953

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

git/refs/remote.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class RemoteReference(Head):
1212
"""Represents a reference pointing to a remote head."""
1313
__slots__ = tuple()
1414

15-
_common_path_default = "refs/remotes"
15+
_common_path_default = Head._remote_common_path_default
1616

1717

1818
@classmethod
@@ -24,24 +24,6 @@ def iter_items(cls, repo, common_path = None, remote=None):
2424
# END handle remote constraint
2525
return super(RemoteReference, cls).iter_items(repo, common_path)
2626

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

git/refs/symbolic.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class SymbolicReference(object):
3636
_resolve_ref_on_create = False
3737
_points_to_commits_only = True
3838
_common_path_default = ""
39+
_remote_common_path_default = "refs/remotes"
3940
_id_attribute_ = "name"
4041

4142
re_hexsha_only = re.compile('^[0-9A-Fa-f]{40}$')

0 commit comments

Comments
 (0)