summaryrefslogtreecommitdiff
path: root/lib/git
diff options
context:
space:
mode:
Diffstat (limited to 'lib/git')
-rw-r--r--lib/git/objects/commit.py1
-rw-r--r--lib/git/refs.py1
-rw-r--r--lib/git/remote.py1
-rw-r--r--lib/git/repo.py13
-rw-r--r--lib/git/utils.py39
5 files changed, 47 insertions, 8 deletions
diff --git a/lib/git/objects/commit.py b/lib/git/objects/commit.py
index a68a7bed..4080305f 100644
--- a/lib/git/objects/commit.py
+++ b/lib/git/objects/commit.py
@@ -23,6 +23,7 @@ class Commit(base.Object, Iterable, diff.Diffable):
type = "commit"
__slots__ = ("tree", "author", "authored_date", "committer", "committed_date",
"message", "parents")
+ _id_attribute_ = "id"
def __init__(self, repo, id, tree=None, author=None, authored_date=None,
committer=None, committed_date=None, message=None, parents=None):
diff --git a/lib/git/refs.py b/lib/git/refs.py
index 3428915e..8c9d6672 100644
--- a/lib/git/refs.py
+++ b/lib/git/refs.py
@@ -16,6 +16,7 @@ class Reference(LazyMixin, Iterable):
"""
__slots__ = ("repo", "path")
_common_path_default = "refs"
+ _id_attribute_ = "name"
def __init__(self, repo, path, object = None):
"""
diff --git a/lib/git/remote.py b/lib/git/remote.py
index da37414e..e043e6db 100644
--- a/lib/git/remote.py
+++ b/lib/git/remote.py
@@ -47,6 +47,7 @@ class Remote(LazyMixin, Iterable):
"""
__slots__ = ( "repo", "name", "_config_reader" )
+ _id_attribute_ = "name"
def __init__(self, repo, name):
"""
diff --git a/lib/git/repo.py b/lib/git/repo.py
index c5b3d79b..0f4be1b1 100644
--- a/lib/git/repo.py
+++ b/lib/git/repo.py
@@ -105,7 +105,7 @@ class Repo(object):
this repo
Returns
- ``git.Head[]``
+ ``git.IterableList(Head, ...)``
"""
return Head.list_items(self)
@@ -127,6 +127,9 @@ class Repo(object):
def remotes(self):
"""
A list of Remote objects allowing to access and manipulate remotes
+
+ Returns
+ ``git.IterableList(Remote, ...)``
"""
return Remote.list_items(self)
@@ -138,11 +141,7 @@ class Repo(object):
Raise
ValueError if no remote with such a name exists
"""
- for remote in Remote.iter_items(self):
- if remote.name == name:
- return remote
- # END for each existing remote
- raise ValueError( "Remote named %s does not exist" % name )
+ return Remote(self, name)
@property
def tags(self):
@@ -150,7 +149,7 @@ class Repo(object):
A list of ``Tag`` objects that are available in this repo
Returns
- ``git.Tag[]``
+ ``git.IterableList(TagReference, ...)``
"""
return TagReference.list_items(self)
diff --git a/lib/git/utils.py b/lib/git/utils.py
index f84c247d..3e7e8e75 100644
--- a/lib/git/utils.py
+++ b/lib/git/utils.py
@@ -56,12 +56,46 @@ class LazyMixin(object):
pass
+class IterableList(list):
+ """
+ List of iterable objects allowing to query an object by id or by named index::
+
+ heads = repo.heads
+ heads.master
+ heads['master']
+ heads[0]
+ """
+ __slots__ = '_id_attr'
+
+ def __new__(cls, id_attr):
+ return super(IterableList,cls).__new__(cls)
+
+ def __init__(self, id_attr):
+ self._id_attr = id_attr
+
+ def __getattr__(self, attr):
+ for item in self:
+ if getattr(item, self._id_attr) == attr:
+ return item
+ # END for each item
+ return list.__getattribute__(self, attr)
+
+ def __getitem__(self, index):
+ if isinstance(index, int):
+ return list.__getitem__(self,index)
+
+ try:
+ return getattr(self, index)
+ except AttributeError:
+ raise IndexError( "No item found with id %r" % index )
+
class Iterable(object):
"""
Defines an interface for iterable items which is to assure a uniform
way to retrieve and iterate items within the git repository
"""
__slots__ = tuple()
+ _id_attribute_ = "attribute that most suitably identifies your instance"
@classmethod
def list_items(cls, repo, *args, **kwargs):
@@ -75,7 +109,10 @@ class Iterable(object):
Returns:
list(Item,...) list of item instances
"""
- return list(cls.iter_items(repo, *args, **kwargs))
+ #return list(cls.iter_items(repo, *args, **kwargs))
+ out_list = IterableList( cls._id_attribute_ )
+ out_list.extend(cls.iter_items(repo, *args, **kwargs))
+ return out_list
@classmethod