| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -62,10 +62,34 @@ def __del__(self): | |||
| 62 | 62 | os.rename(self.tmp_file_path, self.file_path) | |
| 63 | 63 | # END temp file exists | |
| 64 | 64 | ||
| 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 | + | ||
| 65 | 90 | ||
| 66 | 91 | class BaseIndexEntry(tuple): | |
| 67 | 92 | """ | |
| 68 | - | ||
| 69 | 93 | Small Brother of an index entry which can be created to describe changes | |
| 70 | 94 | done to the index in which case plenty of additional information is not requried. | |
| 71 | 95 | ||
@@ -621,7 +645,8 @@ def iter_blobs(self, predicate = lambda t: True): | |||
| 621 | 645 | ||
| 622 | 646 | ``predicate`` | |
| 623 | 647 | 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. | ||
| 625 | 650 | """ | |
| 626 | 651 | for entry in self.entries.itervalues(): | |
| 627 | 652 | mode = self._index_mode_to_tree_index_mode(entry.mode) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -121,6 +121,11 @@ def test_index_file_from_tree(self): | |||
| 121 | 121 | assert merge_blobs[0][0] in (1,2,3) | |
| 122 | 122 | assert isinstance(merge_blobs[0][1], Blob) | |
| 123 | 123 | ||
| 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 | + | ||
| 124 | 129 | ||
| 125 | 130 | # writing a tree should fail with an unmerged index | |
| 126 | 131 | self.failUnlessRaises(GitCommandError, three_way_index.write_tree) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments