◐ Shell
reader mode source ↗
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
50 changes: 1 addition & 49 deletions git/objects/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,54 +53,6 @@
__all__ = ("TreeModifier", "Tree")


def git_cmp(t1: TreeCacheTup, t2: TreeCacheTup) -> int:
a, b = t1[2], t2[2]
# assert isinstance(a, str) and isinstance(b, str)
len_a, len_b = len(a), len(b)
min_len = min(len_a, len_b)
min_cmp = cmp(a[:min_len], b[:min_len])

if min_cmp:
return min_cmp

return len_a - len_b


def merge_sort(a: List[TreeCacheTup], cmp: Callable[[TreeCacheTup, TreeCacheTup], int]) -> None:
if len(a) < 2:
return

mid = len(a) // 2
lefthalf = a[:mid]
righthalf = a[mid:]

merge_sort(lefthalf, cmp)
merge_sort(righthalf, cmp)

i = 0
j = 0
k = 0

while i < len(lefthalf) and j < len(righthalf):
if cmp(lefthalf[i], righthalf[j]) <= 0:
a[k] = lefthalf[i]
i = i + 1
else:
a[k] = righthalf[j]
j = j + 1
k = k + 1

while i < len(lefthalf):
a[k] = lefthalf[i]
i = i + 1
k = k + 1

while j < len(righthalf):
a[k] = righthalf[j]
j = j + 1
k = k + 1


class TreeModifier:
"""A utility class providing methods to alter the underlying cache in a list-like fashion.

Expand Down Expand Up @@ -131,7 +83,7 @@ def set_done(self) -> "TreeModifier":

:return self:
"""
merge_sort(self._cache, git_cmp)
return self

# } END interface
Expand Down
58 changes: 58 additions & 0 deletions test/test_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
from git.objects import Tree, Blob
from test.lib import TestBase

import os.path as osp


class TestTree(TestBase):
Expand Down Expand Up @@ -40,6 +42,62 @@ def test_serializable(self):
testtree._deserialize(stream)
# END for each item in tree

def test_traverse(self):
root = self.rorepo.tree("0.1.6")
num_recursive = 0
Expand Down
Toggle all file notes Toggle all file annotations