Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9896717

Browse files
committed
Implemneted IterableLists for refs, commits and remote objects including simple tests
1 parentb9cb007 commit9896717

File tree

11 files changed

+56
-10
lines changed

11 files changed

+56
-10
lines changed

‎CHANGES‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Item Iteration
3838
hurt performance and memory consumption and reduce response times.
3939
iter_items method provide an iterator that will return items on demand as parsed
4040
from a stream. This way any amount of objects can be handled.
41+
* list_items method returns IterableList allowing to access list members by name
4142

4243
objects Package
4344
----------------

‎TODO‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ General
1313
* Overhaul command caching - currently its possible to create many instances of
1414
the std-in command types, as it appears they are not killed when the repo gets
1515
deleted.
16-
* ReferenceList being a list that can also be accessed by name, such as rlist.HEAD
17-
or rlist.master, allowing repo.heads.master
1816
* References should be parsed 'manually' to get around command invocation, but
1917
be sure to be able to read packed refs.
2018

‎lib/git/objects/commit.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Commit(base.Object, Iterable, diff.Diffable):
2323
type="commit"
2424
__slots__= ("tree","author","authored_date","committer","committed_date",
2525
"message","parents")
26+
_id_attribute_="id"
2627

2728
def__init__(self,repo,id,tree=None,author=None,authored_date=None,
2829
committer=None,committed_date=None,message=None,parents=None):

‎lib/git/refs.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Reference(LazyMixin, Iterable):
1616
"""
1717
__slots__= ("repo","path")
1818
_common_path_default="refs"
19+
_id_attribute_="name"
1920

2021
def__init__(self,repo,path,object=None):
2122
"""

‎lib/git/remote.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class Remote(LazyMixin, Iterable):
4747
"""
4848

4949
__slots__= ("repo","name","_config_reader" )
50+
_id_attribute_="name"
5051

5152
def__init__(self,repo,name):
5253
"""

‎lib/git/repo.py‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def heads(self):
105105
this repo
106106
107107
Returns
108-
``git.Head[]``
108+
``git.IterableList(Head, ...)``
109109
"""
110110
returnHead.list_items(self)
111111

@@ -127,6 +127,9 @@ def head(self,path="HEAD"):
127127
defremotes(self):
128128
"""
129129
A list of Remote objects allowing to access and manipulate remotes
130+
131+
Returns
132+
``git.IterableList(Remote, ...)``
130133
"""
131134
returnRemote.list_items(self)
132135

@@ -138,19 +141,15 @@ def remote(self, name='origin'):
138141
Raise
139142
ValueError if no remote with such a name exists
140143
"""
141-
forremoteinRemote.iter_items(self):
142-
ifremote.name==name:
143-
returnremote
144-
# END for each existing remote
145-
raiseValueError("Remote named %s does not exist"%name )
144+
returnRemote(self,name)
146145

147146
@property
148147
deftags(self):
149148
"""
150149
A list of ``Tag`` objects that are available in this repo
151150
152151
Returns
153-
``git.Tag[]``
152+
``git.IterableList(TagReference, ...)``
154153
"""
155154
returnTagReference.list_items(self)
156155

‎lib/git/utils.py‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,46 @@ def _set_cache_(self, attr):
5656
pass
5757

5858

59+
classIterableList(list):
60+
"""
61+
List of iterable objects allowing to query an object by id or by named index::
62+
63+
heads = repo.heads
64+
heads.master
65+
heads['master']
66+
heads[0]
67+
"""
68+
__slots__='_id_attr'
69+
70+
def__new__(cls,id_attr):
71+
returnsuper(IterableList,cls).__new__(cls)
72+
73+
def__init__(self,id_attr):
74+
self._id_attr=id_attr
75+
76+
def__getattr__(self,attr):
77+
foriteminself:
78+
ifgetattr(item,self._id_attr)==attr:
79+
returnitem
80+
# END for each item
81+
returnlist.__getattribute__(self,attr)
82+
83+
def__getitem__(self,index):
84+
ifisinstance(index,int):
85+
returnlist.__getitem__(self,index)
86+
87+
try:
88+
returngetattr(self,index)
89+
exceptAttributeError:
90+
raiseIndexError("No item found with id %r"%index )
91+
5992
classIterable(object):
6093
"""
6194
Defines an interface for iterable items which is to assure a uniform
6295
way to retrieve and iterate items within the git repository
6396
"""
6497
__slots__=tuple()
98+
_id_attribute_="attribute that most suitably identifies your instance"
6599

66100
@classmethod
67101
deflist_items(cls,repo,*args,**kwargs):
@@ -75,7 +109,10 @@ def list_items(cls, repo, *args, **kwargs):
75109
Returns:
76110
list(Item,...) list of item instances
77111
"""
78-
returnlist(cls.iter_items(repo,*args,**kwargs))
112+
#return list(cls.iter_items(repo, *args, **kwargs))
113+
out_list=IterableList(cls._id_attribute_ )
114+
out_list.extend(cls.iter_items(repo,*args,**kwargs))
115+
returnout_list
79116

80117

81118
@classmethod

‎test/git/test_commit.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ def test_rev_list_bisect_all(self, git):
6767

6868
deftest_count(self):
6969
assertself.repo.tag('0.1.5').commit.count( )==141
70+
71+
deftest_list(self):
72+
assertisinstance(Commit.list_items(self.repo,'0.1.5',max_count=5)['5117c9c8a4d3af19a9958677e45cda9269de1541'],Commit)
7073

7174
deftest_str(self):
7275
commit=Commit(self.repo,id='abc')

‎test/git/test_remote.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def test_base(self):
6868
assertnum_remotes==len(remote_set)
6969

7070
origin=self.repo.remote('origin')
71+
assertorigin==self.repo.remotes.origin
7172

7273
deftest_creation_and_removal(self):
7374
new_name="test_new_one"

‎test/git/test_repo.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ def test_heads_should_populate_head_data(self):
3939
asserthead.name
4040
assertisinstance(head.commit,Commit)
4141
# END for each head
42+
43+
assertisinstance(self.repo.heads.master,Head)
44+
assertisinstance(self.repo.heads['master'],Head)
4245

4346
@patch_object(Git,'_call_process')
4447
deftest_commits(self,git):

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2026 Movatter.jp