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

Skip to content

Commit 615de50

Browse files
committed
index: Added BlobFilter utility class to make filtering of blobs from the index easier
1 parent f22ddca commit 615de50

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

lib/git/index.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,34 @@ def __del__(self):
6262
os.rename(self.tmp_file_path, self.file_path)
6363
# END temp file exists
6464

65+
class BlobFilter(object):
66+
"""
67+
Predicate to be used by iter_blobs allowing to filter only return blobs which
68+
match the given list of directories or files.
69+
70+
The given paths are given relative to the repository.
71+
"""
72+
__slots__ = 'paths'
73+
74+
def __init__(self, paths):
75+
"""
76+
``paths``
77+
tuple or list of paths which are either pointing to directories or
78+
to files relative to the current repository
79+
"""
80+
self.paths = paths
81+
82+
def __call__(self, stage_blob):
83+
path = stage_blob[1].path
84+
for p in self.paths:
85+
if path.startswith(p):
86+
return True
87+
# END for each path in filter paths
88+
return False
89+
6590

6691
class BaseIndexEntry(tuple):
6792
"""
68-
6993
Small Brother of an index entry which can be created to describe changes
7094
done to the index in which case plenty of additional information is not requried.
7195
@@ -621,7 +645,8 @@ def iter_blobs(self, predicate = lambda t: True):
621645
622646
``predicate``
623647
Function(t) returning True if tuple(stage, Blob) should be yielded by the
624-
iterator
648+
iterator. A default filter, the BlobFilter, allows you to yield blobs
649+
only if they match a given list of paths.
625650
"""
626651
for entry in self.entries.itervalues():
627652
mode = self._index_mode_to_tree_index_mode(entry.mode)

test/git/test_index.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ def test_index_file_from_tree(self):
121121
assert merge_blobs[0][0] in (1,2,3)
122122
assert isinstance(merge_blobs[0][1], Blob)
123123

124+
# test BlobFilter
125+
prefix = 'lib/git'
126+
for stage, blob in base_index.iter_blobs(BlobFilter([prefix])):
127+
assert blob.path.startswith(prefix)
128+
124129

125130
# writing a tree should fail with an unmerged index
126131
self.failUnlessRaises(GitCommandError, three_way_index.write_tree)

0 commit comments

Comments
 (0)