Skip to content

Commit

Permalink
Merge pull request #1799 from et-repositories/main
Browse files Browse the repository at this point in the history
fix: treeNotSorted issue
  • Loading branch information
Byron committed Jan 16, 2024
2 parents 6978325 + bda5a17 commit d28c20b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 49 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ Contributors are:
-Santos Gallegos <stsewd _at_ proton.me>
-Wenhan Zhu <wzhu.cosmos _at_ gmail.com>
-Eliah Kagan <eliah.kagan _at_ gmail.com>
-Ethan Lin <et.repositories _at_ gmail.com>

Portions derived from other open source works and are clearly marked.
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)
self._cache.sort(key=lambda x: (x[2] + "/") if x[1] == Tree.tree_id << 12 else x[2])
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
import os.path as osp
import subprocess


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_tree_modifier_ordering(self):
def setup_git_repository_and_get_ordered_files():
os.mkdir("tmp")
os.chdir("tmp")
subprocess.run(["git", "init", "-q"], check=True)
os.mkdir("file")
for filename in [
"bin",
"bin.d",
"file.to",
"file.toml",
"file.toml.bin",
"file0",
"file/a",
]:
open(filename, "a").close()

subprocess.run(["git", "add", "."], check=True)
subprocess.run(["git", "commit", "-m", "c1"], check=True)
tree_hash = subprocess.check_output(["git", "rev-parse", "HEAD^{tree}"]).decode().strip()
cat_file_output = subprocess.check_output(["git", "cat-file", "-p", tree_hash]).decode()
return [line.split()[-1] for line in cat_file_output.split("\n") if line]

hexsha = "6c1faef799095f3990e9970bc2cb10aa0221cf9c"
roottree = self.rorepo.tree(hexsha)
blob_mode = Tree.blob_id << 12
tree_mode = Tree.tree_id << 12

files_in_desired_order = [
(blob_mode, "bin"),
(blob_mode, "bin.d"),
(blob_mode, "file.to"),
(blob_mode, "file.toml"),
(blob_mode, "file.toml.bin"),
(blob_mode, "file0"),
(tree_mode, "file"),
]
mod = roottree.cache
for file_mode, file_name in files_in_desired_order:
mod.add(hexsha, file_mode, file_name)
# end for each file

def file_names_in_order():
return [t[1] for t in files_in_desired_order]

def names_in_mod_cache():
a = [t[2] for t in mod._cache]
here = file_names_in_order()
return [e for e in a if e in here]

git_file_names_in_order = setup_git_repository_and_get_ordered_files()
os.chdir("..")

mod.set_done()
assert names_in_mod_cache() == git_file_names_in_order, "set_done() performs git-sorting"

def test_traverse(self):
root = self.rorepo.tree("0.1.6")
num_recursive = 0
Expand Down

0 comments on commit d28c20b

Please sign in to comment.