| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 0de827a commit 4f1110b
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -35,33 +35,42 @@ def git_cmp(t1, t2): | |||
| 35 | 35 | if min_cmp: | |
| 36 | 36 | return min_cmp | |
| 37 | 37 | ||
| 38 | - return len_b - len_a | ||
| 38 | + return len_a - len_b | ||
| 39 | 39 | ||
| 40 | - if PY3: | ||
| 41 | - # taken from https://wiki.python.org/moin/HowTo/Sorting#The_Old_Way_Using_the_cmp_Parameter | ||
| 42 | - class CmpToKey(object): | ||
| 43 | - __slots__ = 'obj' | ||
| 44 | 40 | ||
| 45 | - def __init__(self, obj, *args): | ||
| 46 | - self.obj = obj | ||
| 41 | + def merge_sort(a, cmp): | ||
| 42 | + if len(a) < 2: | ||
| 43 | + return | ||
| 47 | 44 | ||
| 48 | - def __lt__(self, other): | ||
| 49 | - return git_cmp(self.obj, other.obj) < 0 | ||
| 45 | + mid = len(a) // 2 | ||
| 46 | + lefthalf = a[:mid] | ||
| 47 | + righthalf = a[mid:] | ||
| 50 | 48 | ||
| 51 | - def __gt__(self, other): | ||
| 52 | - return git_cmp(self.obj, other.obj) > 0 | ||
| 49 | + merge_sort(lefthalf, cmp) | ||
| 50 | + merge_sort(righthalf, cmp) | ||
| 53 | 51 | ||
| 54 | - def __eq__(self, other): | ||
| 55 | - return git_cmp(self.obj, other.obj) == 0 | ||
| 52 | + i = 0 | ||
| 53 | + j = 0 | ||
| 54 | + k = 0 | ||
| 56 | 55 | ||
| 57 | - def __le__(self, other): | ||
| 58 | - return git_cmp(self.obj, other.obj) <= 0 | ||
| 56 | + while i < len(lefthalf) and j < len(righthalf): | ||
| 57 | + if cmp(lefthalf[i], righthalf[j]) <= 0: | ||
| 58 | + a[k] = lefthalf[i] | ||
| 59 | + i = i + 1 | ||
| 60 | + else: | ||
| 61 | + a[k] = righthalf[j] | ||
| 62 | + j = j + 1 | ||
| 63 | + k = k + 1 | ||
| 59 | 64 | ||
| 60 | - def __ge__(self, other): | ||
| 61 | - return git_cmp(self.obj, other.obj) >= 0 | ||
| 65 | + while i < len(lefthalf): | ||
| 66 | + a[k] = lefthalf[i] | ||
| 67 | + i = i + 1 | ||
| 68 | + k = k + 1 | ||
| 62 | 69 | ||
| 63 | - def __ne__(self, other): | ||
| 64 | - return git_cmp(self.obj, other.obj) != 0 | ||
| 70 | + while j < len(righthalf): | ||
| 71 | + a[k] = righthalf[j] | ||
| 72 | + j = j + 1 | ||
| 73 | + k = k + 1 | ||
| 65 | 74 | ||
| 66 | 75 | ||
| 67 | 76 | class TreeModifier(object): | |
@@ -90,10 +99,7 @@ def set_done(self): | |||
| 90 | 99 | It may be called several times, but be aware that each call will cause | |
| 91 | 100 | a sort operation | |
| 92 | 101 | :return self:""" | |
| 93 | - if PY3: | ||
| 94 | - self._cache.sort(key=CmpToKey) | ||
| 95 | - else: | ||
| 96 | - self._cache.sort(cmp=git_cmp) | ||
| 102 | + merge_sort(self._cache, git_cmp) | ||
| 97 | 103 | return self | |
| 98 | 104 | #} END interface | |
| 99 | 105 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -77,30 +77,10 @@ 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 | - | ||
| 97 | 80 | # have added one item, we are done | |
| 98 | 81 | mod.set_done() | |
| 99 | 82 | mod.set_done() # multiple times are okay | |
| 100 | 83 | ||
| 101 | - assert chunk_from(names_in_mod_cache(), 'fild', 4) == ['fild', 'file.second', 'file', | ||
| 102 | - 'filf'] | ||
| 103 | - | ||
| 104 | 84 | # serialize, its different now | |
| 105 | 85 | stream = BytesIO() | |
| 106 | 86 | testtree._serialize(stream) | |
@@ -114,6 +94,41 @@ def chunk_from(a, name, size): | |||
| 114 | 94 | assert invalid_name not in testtree | |
| 115 | 95 | # END for each item in tree | |
| 116 | 96 | ||
| 97 | + def test_tree_modifier_ordering(self): | ||
| 98 | + hexsha = '6c1faef799095f3990e9970bc2cb10aa0221cf9c' | ||
| 99 | + roottree = self.rorepo.tree(hexsha) | ||
| 100 | + blob_mode = Tree.blob_id << 12 | ||
| 101 | + tree_mode = Tree.tree_id << 12 | ||
| 102 | + | ||
| 103 | + files_in_desired_order = [ | ||
| 104 | + (blob_mode, '_.htaccess'), | ||
| 105 | + (tree_mode, 'fileadmin'), | ||
| 106 | + (blob_mode, 'index.php'), | ||
| 107 | + (tree_mode, 'typo3'), | ||
| 108 | + (tree_mode, 'typo3_src-6.2.12'), | ||
| 109 | + (tree_mode, 'typo3_src'), | ||
| 110 | + (blob_mode, 'typo3cms'), | ||
| 111 | + (tree_mode, 'typo3conf'), | ||
| 112 | + (tree_mode, 'uploads'), | ||
| 113 | + ] | ||
| 114 | + mod = roottree.cache | ||
| 115 | + for (file_mode, file_name) in files_in_desired_order: | ||
| 116 | + mod.add(hexsha, file_mode, file_name) | ||
| 117 | + # end for each file | ||
| 118 | + | ||
| 119 | + def file_names_in_order(): | ||
| 120 | + return [t[1] for t in files_in_desired_order] | ||
| 121 | + | ||
| 122 | + def names_in_mod_cache(): | ||
| 123 | + a = [t[2] for t in mod._cache] | ||
| 124 | + here = file_names_in_order() | ||
| 125 | + return [e for e in a if e in here] | ||
| 126 | + | ||
| 127 | + assert names_in_mod_cache() == file_names_in_order(), 'add() keeps order' | ||
| 128 | + | ||
| 129 | + mod.set_done() | ||
| 130 | + assert names_in_mod_cache() == file_names_in_order(), 'set_done() performs git-sorting' | ||
| 131 | + | ||
| 117 | 132 | def test_traverse(self): | |
| 118 | 133 | root = self.rorepo.tree('0.1.6') | |
| 119 | 134 | num_recursive = 0 | |
| Back | FazBrowse Home | New Git URL |
0 commit comments