Skip to content

Commit

Permalink
Basic remote functionality moved to Reference type, as it can in fact…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
Byron committed Jul 4, 2011
1 parent c555840 commit a92ab80
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 19 deletions.
41 changes: 41 additions & 0 deletions git/refs/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@

__all__ = ["Reference"]

#{ Utilities
def require_remote_ref_path(func):
"""A decorator raising a TypeError if we are not a valid remote, based on the path"""
def wrapper(self, *args):
if not self.path.startswith(self._remote_common_path_default + "/"):
raise ValueError("ref path does not point to a remote reference: %s" % path)
return func(self, *args)
#END wrapper
wrapper.__name__ = func.__name__
return wrapper
#}END utilites


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

def __str__(self):
return self.name

#{ Interface

def set_object(self, object, logmsg = None):
"""Special version which checks if the head-log needs an update as well"""
Expand Down Expand Up @@ -84,3 +98,30 @@ def iter_items(cls, repo, common_path = None):
"""Equivalent to SymbolicReference.iter_items, but will return non-detached
references as well."""
return cls._iter_items(repo, common_path)

#}END interface


#{ Remote Interface

@property
@require_remote_ref_path
def remote_name(self):
"""
:return:
Name of the remote we are a reference of, such as 'origin' for a reference
named 'origin/master'"""
tokens = self.path.split('/')
# /refs/remotes/<remote name>/<branch_name>
return tokens[2]

@property
@require_remote_ref_path
def remote_head(self):
""":return: Name of the remote head itself, i.e. master.
:note: The returned name is usually not qualified enough to uniquely identify
a branch"""
tokens = self.path.split('/')
return '/'.join(tokens[3:])

#} END remote interface
20 changes: 1 addition & 19 deletions git/refs/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class RemoteReference(Head):
"""Represents a reference pointing to a remote head."""
_common_path_default = "refs/remotes"
_common_path_default = Head._remote_common_path_default


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

@property
def remote_name(self):
"""
:return:
Name of the remote we are a reference of, such as 'origin' for a reference
named 'origin/master'"""
tokens = self.path.split('/')
# /refs/remotes/<remote name>/<branch_name>
return tokens[2]

@property
def remote_head(self):
""":return: Name of the remote head itself, i.e. master.
:note: The returned name is usually not qualified enough to uniquely identify
a branch"""
tokens = self.path.split('/')
return '/'.join(tokens[3:])

@classmethod
def delete(cls, repo, *refs, **kwargs):
"""Delete the given remote references.
Expand Down
1 change: 1 addition & 0 deletions git/refs/symbolic.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class SymbolicReference(object):
_resolve_ref_on_create = False
_points_to_commits_only = True
_common_path_default = ""
_remote_common_path_default = "refs/remotes"
_id_attribute_ = "name"

def __init__(self, repo, path):
Expand Down

0 comments on commit a92ab80

Please sign in to comment.