diff options
author | Sebastian Thiel <byronimo@gmail.com> | 2011-07-04 23:26:13 +0200 |
---|---|---|
committer | Sebastian Thiel <byronimo@gmail.com> | 2011-07-04 23:26:13 +0200 |
commit | 1ddf05a78475a194ed1aa082d26b3d27ecc77475 (patch) | |
tree | b5727ac49ff7a8ac2a9560cf85de60d60ad10e8f /git/refs | |
parent | e9bd04844725661c8aa2aef11091f01eeab69486 (diff) | |
parent | a92ab8028c7780db728d6aad40ea1f511945fc8e (diff) | |
download | gitpython-1ddf05a78475a194ed1aa082d26b3d27ecc77475.tar.gz |
Merge branch 'remote-fixes' into 0.3
Diffstat (limited to 'git/refs')
-rw-r--r-- | git/refs/reference.py | 51 | ||||
-rw-r--r-- | git/refs/remote.py | 20 | ||||
-rw-r--r-- | git/refs/symbolic.py | 1 |
3 files changed, 49 insertions, 23 deletions
diff --git a/git/refs/reference.py b/git/refs/reference.py index 1a745ee9..29d051a6 100644 --- a/git/refs/reference.py +++ b/git/refs/reference.py @@ -13,6 +13,18 @@ from gitdb.util import ( __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, @@ -22,20 +34,24 @@ class Reference(SymbolicReference, LazyMixin, Iterable): _resolve_ref_on_create = True _common_path_default = "refs" - def __init__(self, repo, path): + def __init__(self, repo, path, check_path = True): """Initialize this instance :param repo: Our parent repository :param path: Path relative to the .git/ directory pointing to the ref in question, i.e. - refs/heads/master""" - if not path.startswith(self._common_path_default+'/'): - raise ValueError("Cannot instantiate %r from path %s" % ( self.__class__.__name__, path )) + refs/heads/master + :param check_path: if False, you can provide any path. Otherwise the path must start with the + default path prefix of this type.""" + if check_path and not path.startswith(self._common_path_default+'/'): + raise ValueError("Cannot instantiate %r from path %s" % (self.__class__.__name__, path)) super(Reference, self).__init__(repo, path) 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""" @@ -82,3 +98,30 @@ class Reference(SymbolicReference, LazyMixin, Iterable): """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 diff --git a/git/refs/remote.py b/git/refs/remote.py index b7b07d4b..1d45d23f 100644 --- a/git/refs/remote.py +++ b/git/refs/remote.py @@ -10,7 +10,7 @@ __all__ = ["RemoteReference"] 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 @@ -22,24 +22,6 @@ class RemoteReference(Head): # 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. diff --git a/git/refs/symbolic.py b/git/refs/symbolic.py index 65327a2f..8556a65e 100644 --- a/git/refs/symbolic.py +++ b/git/refs/symbolic.py @@ -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): |