Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8775b64

Browse files
committed
Traversable.traverse: Added as_edge option allowing to receive the source of the item as well to enable predicates to do more proper checking
1 parent 7ba46b8 commit 8775b64

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

lib/git/objects/utils.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def _get_intermediate_items(cls, item):
9292

9393
def traverse( self, predicate = lambda i,d: True,
9494
prune = lambda i,d: False, depth = -1, branch_first=True,
95-
visit_once = True, ignore_self=1 ):
95+
visit_once = True, ignore_self=1, as_edge = False ):
9696
"""
9797
``Returns``
9898
iterator yieling of items found when traversing self
@@ -119,41 +119,49 @@ def traverse( self, predicate = lambda i,d: True,
119119
120120
``ignore_self``
121121
if True, self will be ignored and automatically pruned from
122-
the result. Otherwise it will be the first item to be returned"""
122+
the result. Otherwise it will be the first item to be returned.
123+
If as_edge is True, the source of the first edge is None
124+
125+
``as_edge``
126+
if True, return a pair of items, first being the source, second the
127+
destinatination, i.e. tuple(src, dest) with the edge spanning from
128+
source to destination"""
123129
visited = set()
124130
stack = Deque()
125-
stack.append( ( 0 ,self ) ) # self is always depth level 0
131+
stack.append( ( 0 ,self, None ) ) # self is always depth level 0
126132

127-
def addToStack( stack, lst, branch_first, dpth ):
133+
def addToStack( stack, item, branch_first, depth ):
134+
lst = self._get_intermediate_items( item )
128135
if not lst:
129136
return
130137
if branch_first:
131-
stack.extendleft( ( dpth , item ) for item in lst )
138+
stack.extendleft( ( depth , i, item ) for i in lst )
132139
else:
133-
reviter = ( ( dpth , lst[i] ) for i in range( len( lst )-1,-1,-1) )
140+
reviter = ( ( depth , lst[i], item ) for i in range( len( lst )-1,-1,-1) )
134141
stack.extend( reviter )
135142
# END addToStack local method
136143

137144
while stack:
138-
d, item = stack.pop() # depth of item, item
145+
d, item, src = stack.pop() # depth of item, item, item_source
139146

140147
if visit_once and item in visited:
141148
continue
142149

143150
if visit_once:
144151
visited.add(item)
145152

146-
if prune( item, d ):
153+
rval = ( as_edge and (src, item) ) or item
154+
if prune( rval, d ):
147155
continue
148156

149157
skipStartItem = ignore_self and ( item == self )
150-
if not skipStartItem and predicate( item, d ):
151-
yield item
158+
if not skipStartItem and predicate( rval, d ):
159+
yield rval
152160

153161
# only continue to next level if this is appropriate !
154162
nd = d + 1
155163
if depth > -1 and nd > depth:
156164
continue
157165

158-
addToStack( stack, self._get_intermediate_items( item ), branch_first, nd )
166+
addToStack( stack, item, branch_first, nd )
159167
# END for each item on work stack

test/git/test_commit.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ def test_traversal(self):
6969

7070
# at some point, both iterations should stop
7171
assert list(bfirst)[-1] == first
72-
stoptraverse = self.rorepo.commit("254d04aa3180eb8b8daf7b7ff25f010cd69b4e7d").traverse()
73-
list(stoptraverse)
72+
stoptraverse = self.rorepo.commit("254d04aa3180eb8b8daf7b7ff25f010cd69b4e7d").traverse(as_edge=True)
73+
l = list(stoptraverse)
74+
assert len(l[0]) == 2
7475

7576
# ignore self
7677
assert start.traverse(ignore_self=False).next() == start

0 commit comments

Comments
 (0)