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

Commit9ce1193

Browse files
committed
Added Commit.iter_parents to iterate all parents
Renamed Commit.commits to iter_commitsrepo: assured proper use of the terms revision ( rev ) and reference ( ref )
1 parent2da2b90 commit9ce1193

File tree

8 files changed

+109
-84
lines changed

8 files changed

+109
-84
lines changed

‎CHANGES‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ General
2323
* All dates, like authored_date and committer_date, are stored as seconds since epoc
2424
to consume less memory - they can be converted using time.gmtime in a more suitable
2525
presentation format if needed.
26+
* Named method parameters changed on a wide scale to unify their use. Now git specific
27+
terms are used everywhere, such as "Reference" ( ref ) and "Revision" ( rev ).
28+
Prevously multiple terms where used making it harder to know which type was allowed
29+
or not.
30+
2631

2732
Item Iteration
2833
--------------
@@ -48,6 +53,8 @@ Repo
4853
- 'log' method as it as effectively the same as the 'commits' method
4954
- 'commits_since' as it is just a flag given to rev-list in Commit.iter_items
5055
- 'commit_count' as it was just a redirection to the respective commit method
56+
* Renamed commits to iter_commits to improve the performance, adjusted signature
57+
to be more versatile
5158
* 'commits' method has no max-count of returned commits anymore, it now behaves
5259
like git-rev-list
5360

‎lib/git/objects/commit.py‎

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,39 @@ def summary(self):
100100
First line of the commit message.
101101
"""
102102
returnself.message.split('\n',1)[0]
103+
104+
defiter_parents(self,paths='',**kwargs):
105+
"""
106+
Iterate _all_ parents of this commit.
107+
108+
``paths``
109+
Optional path or list of paths limiting the Commits to those that
110+
contain at least one of the paths
111+
112+
``kwargs``
113+
All arguments allowed by git-rev-list
114+
115+
Return:
116+
Iterator yielding Commit objects which are parents of self
117+
"""
118+
# skip ourselves
119+
skip=kwargs.get("skip",1)
120+
ifskip==0:# skip ourselves
121+
skip=1
122+
kwargs['skip']=skip
123+
124+
returnself.iter_items(self.repo,self,paths,**kwargs )
103125

104126
@classmethod
105-
defcount(cls,repo,ref,paths=''):
127+
defcount(cls,repo,rev,paths=''):
106128
"""
107-
Count the number of commits reachable from thisref
129+
Count the number of commits reachable from thisrevision
108130
109131
``repo``
110132
is the Repo
111133
112-
``ref``
113-
is the ref from which to begin (SHA1 or name)
134+
``rev``
135+
revision specifier, see git-rev-parse for viable options
114136
115137
``paths``
116138
is an optinal path or a list of paths restricting the return value
@@ -119,18 +141,18 @@ def count(cls, repo, ref, paths=''):
119141
Returns
120142
int
121143
"""
122-
returnlen(repo.git.rev_list(ref,'--',paths).strip().splitlines())
144+
returnlen(repo.git.rev_list(rev,'--',paths).strip().splitlines())
123145

124146
@classmethod
125-
defiter_items(cls,repo,ref,paths='',**kwargs):
147+
defiter_items(cls,repo,rev,paths='',**kwargs):
126148
"""
127149
Find all commits matching the given criteria.
128150
129151
``repo``
130152
is the Repo
131153
132-
``ref``
133-
is the ref from which to begin (SHA1, Head or name)
154+
``rev``
155+
revision specifier, see git-rev-parse for viable options
134156
135157
``paths``
136158
is an optinal path or list of paths, if set only Commits that include the path
@@ -149,7 +171,7 @@ def iter_items(cls, repo, ref, paths='', **kwargs):
149171
options.update(kwargs)
150172

151173
# the test system might confront us with string values -
152-
proc=repo.git.rev_list(ref,'--',paths,**options)
174+
proc=repo.git.rev_list(rev,'--',paths,**options)
153175
returncls._iter_from_process_or_stream(repo,proc)
154176

155177
@classmethod
@@ -201,7 +223,7 @@ def _iter_from_process_or_stream(cls, repo, proc_or_stream):
201223
# END while there are message lines
202224
message='\n'.join(message_lines)
203225

204-
yieldCommit(repo,id=id,parents=parents,tree=tree,author=author,authored_date=authored_date,
226+
yieldCommit(repo,id=id,parents=tuple(parents),tree=tree,author=author,authored_date=authored_date,
205227
committer=committer,committed_date=committed_date,message=message)
206228
# END for each line in stream
207229

‎lib/git/refs.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
fromobjects.utilsimportget_object_type_by_name
1111
fromutilsimportLazyMixin,Iterable
1212

13-
classRef(LazyMixin,Iterable):
13+
classReference(LazyMixin,Iterable):
1414
"""
1515
Represents a named reference to any object
1616
"""
@@ -138,7 +138,7 @@ def _from_string(cls, repo, line):
138138
# return cls(repo, full_path, obj)
139139

140140

141-
classHead(Ref):
141+
classHead(Reference):
142142
"""
143143
A Head is a named reference to a Commit. Every Head instance contains a name
144144
and a Commit object.
@@ -181,7 +181,7 @@ def __repr__(self):
181181

182182

183183

184-
classTagRef(Ref):
184+
classTagRef(Reference):
185185
"""
186186
Class representing a lightweight tag reference which either points to a commit
187187
or to a tag object. In the latter case additional information, like the signature

‎lib/git/repo.py‎

Lines changed: 54 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,12 @@ def tags(self):
116116
"""
117117
returnTag.list_items(self)
118118

119-
defblame(self,ref,file):
119+
defblame(self,rev,file):
120120
"""
121-
The blame information for the given file at the givenref.
121+
The blame information for the given file at the givenrevision.
122122
123-
``ref``
124-
Ref object or Commit
123+
``rev``
124+
revision specifier, see git-rev-parse for viable options.
125125
126126
Returns
127127
list: [git.Commit, list: [<line>]]
@@ -199,37 +199,29 @@ def blame(self, ref, file):
199199
# END distinguish hexsha vs other information
200200
returnblames
201201

202-
defcommits(self,start=None,paths='',max_count=None,skip=0):
202+
defiter_commits(self,rev=None,paths='',**kwargs):
203203
"""
204204
A list of Commit objects representing the history of a given ref/commit
205205
206-
``start``
207-
is a Ref or Commit to start the commits from. If start is None,
208-
the active branch will be used
206+
``rev``
207+
revision specifier, see git-rev-parse for viable options.
208+
If None,the active branch will be used.
209209
210210
``paths``
211211
is an optional path or a list of paths to limit the returned commits to
212212
Commits that do not contain that path or the paths will not be returned.
213-
214-
``max_count``
215-
is the maximum number of commits to return (default None)
216-
217-
``skip``
218-
is the number of commits to skip (default 0) which will effectively
219-
move your commit-window by the given number.
213+
214+
``kwargs``
215+
Arguments to be passed to git-rev-parse - common ones are
216+
max_count and skip
220217
221218
Returns
222219
``git.Commit[]``
223220
"""
224-
options= {'max_count':max_count,
225-
'skip':skip}
226-
227-
ifmax_countisNone:
228-
options.pop('max_count')
229-
ifstartisNone:
230-
start=self.active_branch
221+
ifrevisNone:
222+
rev=self.active_branch
231223

232-
returnCommit.list_items(self,start,paths,**options)
224+
returnCommit.list_items(self,rev,paths,**kwargs)
233225

234226
defcommits_between(self,frm,to,*args,**kwargs):
235227
"""
@@ -248,50 +240,31 @@ def commits_between(self, frm, to, *args, **kwargs):
248240
returnreversed(Commit.list_items(self,"%s..%s"% (frm,to)))
249241

250242

251-
defcommit(self,id=None,paths=''):
243+
defcommit(self,rev=None):
252244
"""
253-
The Commit object for the specifiedid
245+
The Commit object for the specifiedrevision
254246
255-
``id``
256-
is the SHA1 identifier of the commit or a ref or a ref name
257-
if None, it defaults to the active branch
247+
``rev``
248+
revision specifier, see git-rev-parse for viable options.
258249
259-
260-
``paths``
261-
is an optional path or a list of paths,
262-
if set the returned commit must contain the path or paths
263-
264250
Returns
265251
``git.Commit``
266252
"""
267-
ifidisNone:
268-
id=self.active_branch
269-
options= {'max_count':1}
270-
271-
commits=Commit.list_items(self,id,paths,**options)
272-
273-
ifnotcommits:
274-
raiseValueError,"Invalid identifier %s, or given path '%s' too restrictive"% (id,path )
275-
returncommits[0]
276-
277-
defcommit_deltas_from(self,other_repo,ref='master',other_ref='master'):
278-
"""
279-
Returns a list of commits that is in ``other_repo`` but not in self
280-
281-
Returns
282-
git.Commit[]
283-
"""
284-
repo_refs=self.git.rev_list(ref,'--').strip().splitlines()
285-
other_repo_refs=other_repo.git.rev_list(other_ref,'--').strip().splitlines()
253+
ifrevisNone:
254+
rev=self.active_branch
255+
256+
# NOTE: currently we are not checking wheter rev really points to a commit
257+
# If not, the system will barf on access of the object, but we don't do that
258+
# here to safe cycles
259+
c=Commit(self,rev)
260+
returnc
286261

287-
diff_refs=list(set(other_repo_refs)-set(repo_refs))
288-
returnmap(lambdaref:Commit(other_repo,ref ),diff_refs)
289262

290-
deftree(self,treeish=None):
263+
deftree(self,ref=None):
291264
"""
292265
The Tree object for the given treeish reference
293266
294-
``treeish``
267+
``ref``
295268
is a Ref instance defaulting to the active_branch if None.
296269
297270
Examples::
@@ -305,27 +278,42 @@ def tree(self, treeish=None):
305278
A ref is requried here to assure you point to a commit or tag. Otherwise
306279
it is not garantueed that you point to the root-level tree.
307280
308-
If you need a non-root level tree, find it by iterating the root tree.
309-
"""
310-
iftreeishisNone:
311-
treeish=self.active_branch
312-
ifnotisinstance(treeish,Ref):
313-
raiseValueError("Treeish reference required, got %r"%treeish )
281+
If you need a non-root level tree, find it by iterating the root tree. Otherwise
282+
it cannot know about its path relative to the repository root and subsequent
283+
operations might have unexpected results.
284+
"""
285+
ifrefisNone:
286+
ref=self.active_branch
287+
ifnotisinstance(ref,Reference):
288+
raiseValueError("Reference required, got %r"%ref )
314289

315290

316291
# As we are directly reading object information, we must make sure
317292
# we truly point to a tree object. We resolve the ref to a sha in all cases
318293
# to assure the returned tree can be compared properly. Except for
319294
# heads, ids should always be hexshas
320-
hexsha,typename,size=self.git.get_object_header(treeish )
295+
hexsha,typename,size=self.git.get_object_header(ref )
321296
iftypename!="tree":
322-
hexsha,typename,size=self.git.get_object_header(str(treeish)+'^{tree}' )
297+
# will raise if this is not a valid tree
298+
hexsha,typename,size=self.git.get_object_header(str(ref)+'^{tree}' )
323299
# END tree handling
324-
treeish=hexsha
300+
ref=hexsha
325301

326302
# the root has an empty relative path and the default mode
327-
returnTree(self,treeish,0,'')
303+
returnTree(self,ref,0,'')
304+
305+
defcommit_deltas_from(self,other_repo,ref='master',other_ref='master'):
306+
"""
307+
Returns a list of commits that is in ``other_repo`` but not in self
328308
309+
Returns
310+
git.Commit[]
311+
"""
312+
repo_refs=self.git.rev_list(ref,'--').strip().splitlines()
313+
other_repo_refs=other_repo.git.rev_list(other_ref,'--').strip().splitlines()
314+
315+
diff_refs=list(set(other_repo_refs)-set(repo_refs))
316+
returnmap(lambdaref:Commit(other_repo,ref ),diff_refs)
329317

330318
defdiff(self,a,b,*paths):
331319
"""

‎test/git/test_base.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def test_tags(self):
6262
ref_count=0
6363
forrefinchain(self.repo.tags,self.repo.heads):
6464
ref_count+=1
65-
assertisinstance(ref,refs.Ref)
65+
assertisinstance(ref,refs.Reference)
6666
assertstr(ref)==ref.name
6767
assertrepr(ref)
6868
assertref==ref

‎test/git/test_commit.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,13 @@ def test_equality(self):
233233
assert_equal(commit1,commit2)
234234
assert_not_equal(commit2,commit3)
235235

236+
deftest_iter_parents(self):
237+
# should return all but ourselves, even if skip is defined
238+
c=self.repo.commit('0.1.5')
239+
forskipin (0,1):
240+
piter=c.iter_parents(skip=skip)
241+
first_parent=piter.next()
242+
assertfirst_parent!=c
243+
assertfirst_parent==c.parents[0]
244+
# END for each
245+

‎test/git/test_performance.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def test_iteration(self):
2121
# return quite a lot of commits, we just take one and hence abort the operation
2222

2323
st=time()
24-
forcinself.repo.commits():
24+
forcinself.repo.iter_commits('0.1.6'):
2525
num_commits+=1
2626
c.author
2727
c.authored_date

‎test/git/test_repo.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_heads_should_populate_head_data(self):
4242
deftest_commits(self,git):
4343
git.return_value=ListProcessAdapter(fixture('rev_list'))
4444

45-
commits=self.repo.commits('master',max_count=10)
45+
commits=list(self.repo.iter_commits('master',max_count=10))
4646

4747
c=commits[0]
4848
assert_equal('4c8124ffcf4039d292442eeccabdeca5af5c5017',c.id)
@@ -73,8 +73,6 @@ def test_commit(self, git):
7373

7474
assert_equal("4c8124ffcf4039d292442eeccabdeca5af5c5017",commit.id)
7575

76-
assert_true(git.called)
77-
7876
@patch_object(Repo,'__init__')
7977
@patch_object(Git,'_call_process')
8078
deftest_init_bare(self,git,repo):

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2026 Movatter.jp