| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent a1a5976 commit d82a6c5
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,9 +18,53 @@ | |||
| 18 | 18 | tree_to_stream | |
| 19 | 19 | ) | |
| 20 | 20 | ||
| 21 | + from gitdb.utils.compat import PY3 | ||
| 22 | + | ||
| 23 | + if PY3: | ||
| 24 | + cmp = lambda a, b: (a > b) - (a < b) | ||
| 25 | + | ||
| 21 | 26 | __all__ = ("TreeModifier", "Tree") | |
| 22 | 27 | ||
| 23 | 28 | ||
| 29 | + def git_cmp(t1, t2): | ||
| 30 | + a, b = t1[2], t2[2] | ||
| 31 | + len_a, len_b = len(a), len(b) | ||
| 32 | + min_len = min(len_a, len_b) | ||
| 33 | + min_cmp = cmp(a[:min_len], b[:min_len]) | ||
| 34 | + | ||
| 35 | + if min_cmp: | ||
| 36 | + return min_cmp | ||
| 37 | + | ||
| 38 | + # return len_a - len_b | ||
| 39 | + return len_b - len_a | ||
| 40 | + | ||
| 41 | + if PY3: | ||
| 42 | + # taken from https://wiki.python.org/moin/HowTo/Sorting#The_Old_Way_Using_the_cmp_Parameter | ||
| 43 | + class CmpToKey(object): | ||
| 44 | + __slots__ = 'obj' | ||
| 45 | + | ||
| 46 | + def __init__(self, obj, *args): | ||
| 47 | + self.obj = obj | ||
| 48 | + | ||
| 49 | + def __lt__(self, other): | ||
| 50 | + return git_cmp(self.obj, other.obj) < 0 | ||
| 51 | + | ||
| 52 | + def __gt__(self, other): | ||
| 53 | + return git_cmp(self.obj, other.obj) > 0 | ||
| 54 | + | ||
| 55 | + def __eq__(self, other): | ||
| 56 | + return git_cmp(self.obj, other.obj) == 0 | ||
| 57 | + | ||
| 58 | + def __le__(self, other): | ||
| 59 | + return git_cmp(self.obj, other.obj) <= 0 | ||
| 60 | + | ||
| 61 | + def __ge__(self, other): | ||
| 62 | + return git_cmp(self.obj, other.obj) >= 0 | ||
| 63 | + | ||
| 64 | + def __ne__(self, other): | ||
| 65 | + return git_cmp(self.obj, other.obj) != 0 | ||
| 66 | + | ||
| 67 | + | ||
| 24 | 68 | class TreeModifier(object): | |
| 25 | 69 | ||
| 26 | 70 | """A utility class providing methods to alter the underlying cache in a list-like fashion. | |
@@ -47,7 +91,10 @@ def set_done(self): | |||
| 47 | 91 | It may be called several times, but be aware that each call will cause | |
| 48 | 92 | a sort operation | |
| 49 | 93 | :return self:""" | |
| 50 | - self._cache.sort(key=lambda t: t[2]) # sort by name | ||
| 94 | + if PY3: | ||
| 95 | + self._cache.sort(key=CmpToKey) | ||
| 96 | + else: | ||
| 97 | + self._cache.sort(cmp=git_cmp) | ||
| 51 | 98 | return self | |
| 52 | 99 | #} END interface | |
| 53 | 100 | ||
@@ -286,3 +333,4 @@ def _deserialize(self, stream): | |||
| 286 | 333 | ||
| 287 | 334 | # finalize map definition | |
| 288 | 335 | Tree._map_id_to_type[Tree.tree_id] = Tree | |
| 336 | + # | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -77,10 +77,30 @@ def test_serializable(self): | |||
| 77 | 77 | # del again, its fine | |
| 78 | 78 | del(mod[invalid_name]) | |
| 79 | 79 | ||
| 80 | + # SPECIAL ORDERING ! | ||
| 81 | + # Add items which should sort differently from standard alum sort order | ||
| 82 | + mod.add(hexsha, Tree.blob_id << 12, 'fild') | ||
| 83 | + mod.add(hexsha, Tree.blob_id << 12, 'file') | ||
| 84 | + mod.add(hexsha, Tree.blob_id << 12, 'file.second') | ||
| 85 | + mod.add(hexsha, Tree.blob_id << 12, 'filf') | ||
| 86 | + | ||
| 87 | + def names_in_mod_cache(): | ||
| 88 | + return [t[2] for t in mod._cache] | ||
| 89 | + | ||
| 90 | + def chunk_from(a, name, size): | ||
| 91 | + index = a.index(name) | ||
| 92 | + return a[index:index + size] | ||
| 93 | + | ||
| 94 | + assert chunk_from(names_in_mod_cache(), 'fild', 4) == ['fild', 'file', 'file.second', | ||
| 95 | + 'filf'] | ||
| 96 | + | ||
| 80 | 97 | # have added one item, we are done | |
| 81 | 98 | mod.set_done() | |
| 82 | 99 | mod.set_done() # multiple times are okay | |
| 83 | 100 | ||
| 101 | + assert chunk_from(names_in_mod_cache(), 'fild', 4) == ['fild', 'file.second', 'file', | ||
| 102 | + 'filf'] | ||
| 103 | + | ||
| 84 | 104 | # serialize, its different now | |
| 85 | 105 | stream = BytesIO() | |
| 86 | 106 | testtree._serialize(stream) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments