2
0

diff_tree.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. # diff_tree.py -- Utilities for diffing files and trees.
  2. # Copyright (C) 2010 Google, Inc.
  3. #
  4. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. # General Public License as public by the Free Software Foundation; version 2.0
  6. # or (at your option) any later version. You can redistribute it and/or
  7. # modify it under the terms of either of these two licenses.
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # You should have received a copy of the licenses; if not, see
  16. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. # License, Version 2.0.
  19. #
  20. """Utilities for diffing files and trees."""
  21. from collections import (
  22. defaultdict,
  23. namedtuple,
  24. )
  25. from io import BytesIO
  26. from itertools import chain
  27. import stat
  28. from typing import List, Dict, Optional
  29. from dulwich.objects import (
  30. S_ISGITLINK,
  31. TreeEntry,
  32. Tree,
  33. )
  34. # TreeChange type constants.
  35. CHANGE_ADD = "add"
  36. CHANGE_MODIFY = "modify"
  37. CHANGE_DELETE = "delete"
  38. CHANGE_RENAME = "rename"
  39. CHANGE_COPY = "copy"
  40. CHANGE_UNCHANGED = "unchanged"
  41. RENAME_CHANGE_TYPES = (CHANGE_RENAME, CHANGE_COPY)
  42. _NULL_ENTRY = TreeEntry(None, None, None)
  43. _MAX_SCORE = 100
  44. RENAME_THRESHOLD = 60
  45. MAX_FILES = 200
  46. REWRITE_THRESHOLD = None
  47. class TreeChange(namedtuple("TreeChange", ["type", "old", "new"])):
  48. """Named tuple a single change between two trees."""
  49. @classmethod
  50. def add(cls, new):
  51. return cls(CHANGE_ADD, _NULL_ENTRY, new)
  52. @classmethod
  53. def delete(cls, old):
  54. return cls(CHANGE_DELETE, old, _NULL_ENTRY)
  55. def _tree_entries(path: str, tree: Tree) -> List[TreeEntry]:
  56. result: List[TreeEntry] = []
  57. if not tree:
  58. return result
  59. for entry in tree.iteritems(name_order=True):
  60. result.append(entry.in_path(path))
  61. return result
  62. def _merge_entries(path, tree1, tree2):
  63. """Merge the entries of two trees.
  64. Args:
  65. path: A path to prepend to all tree entry names.
  66. tree1: The first Tree object to iterate, or None.
  67. tree2: The second Tree object to iterate, or None.
  68. Returns:
  69. A list of pairs of TreeEntry objects for each pair of entries in
  70. the trees. If an entry exists in one tree but not the other, the other
  71. entry will have all attributes set to None. If neither entry's path is
  72. None, they are guaranteed to match.
  73. """
  74. entries1 = _tree_entries(path, tree1)
  75. entries2 = _tree_entries(path, tree2)
  76. i1 = i2 = 0
  77. len1 = len(entries1)
  78. len2 = len(entries2)
  79. result = []
  80. while i1 < len1 and i2 < len2:
  81. entry1 = entries1[i1]
  82. entry2 = entries2[i2]
  83. if entry1.path < entry2.path:
  84. result.append((entry1, _NULL_ENTRY))
  85. i1 += 1
  86. elif entry1.path > entry2.path:
  87. result.append((_NULL_ENTRY, entry2))
  88. i2 += 1
  89. else:
  90. result.append((entry1, entry2))
  91. i1 += 1
  92. i2 += 1
  93. for i in range(i1, len1):
  94. result.append((entries1[i], _NULL_ENTRY))
  95. for i in range(i2, len2):
  96. result.append((_NULL_ENTRY, entries2[i]))
  97. return result
  98. def _is_tree(entry):
  99. mode = entry.mode
  100. if mode is None:
  101. return False
  102. return stat.S_ISDIR(mode)
  103. def walk_trees(store, tree1_id, tree2_id, prune_identical=False):
  104. """Recursively walk all the entries of two trees.
  105. Iteration is depth-first pre-order, as in e.g. os.walk.
  106. Args:
  107. store: An ObjectStore for looking up objects.
  108. tree1_id: The SHA of the first Tree object to iterate, or None.
  109. tree2_id: The SHA of the second Tree object to iterate, or None.
  110. prune_identical: If True, identical subtrees will not be walked.
  111. Returns:
  112. Iterator over Pairs of TreeEntry objects for each pair of entries
  113. in the trees and their subtrees recursively. If an entry exists in one
  114. tree but not the other, the other entry will have all attributes set
  115. to None. If neither entry's path is None, they are guaranteed to
  116. match.
  117. """
  118. # This could be fairly easily generalized to >2 trees if we find a use
  119. # case.
  120. mode1 = tree1_id and stat.S_IFDIR or None
  121. mode2 = tree2_id and stat.S_IFDIR or None
  122. todo = [(TreeEntry(b"", mode1, tree1_id), TreeEntry(b"", mode2, tree2_id))]
  123. while todo:
  124. entry1, entry2 = todo.pop()
  125. is_tree1 = _is_tree(entry1)
  126. is_tree2 = _is_tree(entry2)
  127. if prune_identical and is_tree1 and is_tree2 and entry1 == entry2:
  128. continue
  129. tree1 = is_tree1 and store[entry1.sha] or None
  130. tree2 = is_tree2 and store[entry2.sha] or None
  131. path = entry1.path or entry2.path
  132. todo.extend(reversed(_merge_entries(path, tree1, tree2)))
  133. yield entry1, entry2
  134. def _skip_tree(entry, include_trees):
  135. if entry.mode is None or (not include_trees and stat.S_ISDIR(entry.mode)):
  136. return _NULL_ENTRY
  137. return entry
  138. def tree_changes(
  139. store,
  140. tree1_id,
  141. tree2_id,
  142. want_unchanged=False,
  143. rename_detector=None,
  144. include_trees=False,
  145. change_type_same=False,
  146. ):
  147. """Find the differences between the contents of two trees.
  148. Args:
  149. store: An ObjectStore for looking up objects.
  150. tree1_id: The SHA of the source tree.
  151. tree2_id: The SHA of the target tree.
  152. want_unchanged: If True, include TreeChanges for unmodified entries
  153. as well.
  154. include_trees: Whether to include trees
  155. rename_detector: RenameDetector object for detecting renames.
  156. change_type_same: Whether to report change types in the same
  157. entry or as delete+add.
  158. Returns:
  159. Iterator over TreeChange instances for each change between the
  160. source and target tree.
  161. """
  162. if rename_detector is not None and tree1_id is not None and tree2_id is not None:
  163. yield from rename_detector.changes_with_renames(
  164. tree1_id,
  165. tree2_id,
  166. want_unchanged=want_unchanged,
  167. include_trees=include_trees,
  168. )
  169. return
  170. entries = walk_trees(
  171. store, tree1_id, tree2_id, prune_identical=(not want_unchanged)
  172. )
  173. for entry1, entry2 in entries:
  174. if entry1 == entry2 and not want_unchanged:
  175. continue
  176. # Treat entries for trees as missing.
  177. entry1 = _skip_tree(entry1, include_trees)
  178. entry2 = _skip_tree(entry2, include_trees)
  179. if entry1 != _NULL_ENTRY and entry2 != _NULL_ENTRY:
  180. if (
  181. stat.S_IFMT(entry1.mode) != stat.S_IFMT(entry2.mode)
  182. and not change_type_same
  183. ):
  184. # File type changed: report as delete/add.
  185. yield TreeChange.delete(entry1)
  186. entry1 = _NULL_ENTRY
  187. change_type = CHANGE_ADD
  188. elif entry1 == entry2:
  189. change_type = CHANGE_UNCHANGED
  190. else:
  191. change_type = CHANGE_MODIFY
  192. elif entry1 != _NULL_ENTRY:
  193. change_type = CHANGE_DELETE
  194. elif entry2 != _NULL_ENTRY:
  195. change_type = CHANGE_ADD
  196. else:
  197. # Both were None because at least one was a tree.
  198. continue
  199. yield TreeChange(change_type, entry1, entry2)
  200. def _all_eq(seq, key, value):
  201. for e in seq:
  202. if key(e) != value:
  203. return False
  204. return True
  205. def _all_same(seq, key):
  206. return _all_eq(seq[1:], key, key(seq[0]))
  207. def tree_changes_for_merge(store, parent_tree_ids, tree_id, rename_detector=None):
  208. """Get the tree changes for a merge tree relative to all its parents.
  209. Args:
  210. store: An ObjectStore for looking up objects.
  211. parent_tree_ids: An iterable of the SHAs of the parent trees.
  212. tree_id: The SHA of the merge tree.
  213. rename_detector: RenameDetector object for detecting renames.
  214. Returns:
  215. Iterator over lists of TreeChange objects, one per conflicted path
  216. in the merge.
  217. Each list contains one element per parent, with the TreeChange for that
  218. path relative to that parent. An element may be None if it never
  219. existed in one parent and was deleted in two others.
  220. A path is only included in the output if it is a conflict, i.e. its SHA
  221. in the merge tree is not found in any of the parents, or in the case of
  222. deletes, if not all of the old SHAs match.
  223. """
  224. all_parent_changes = [
  225. tree_changes(store, t, tree_id, rename_detector=rename_detector)
  226. for t in parent_tree_ids
  227. ]
  228. num_parents = len(parent_tree_ids)
  229. changes_by_path: Dict[str, List[Optional[TreeChange]]] = defaultdict(lambda: [None] * num_parents)
  230. # Organize by path.
  231. for i, parent_changes in enumerate(all_parent_changes):
  232. for change in parent_changes:
  233. if change.type == CHANGE_DELETE:
  234. path = change.old.path
  235. else:
  236. path = change.new.path
  237. changes_by_path[path][i] = change
  238. def old_sha(c):
  239. return c.old.sha
  240. def change_type(c):
  241. return c.type
  242. # Yield only conflicting changes.
  243. for _, changes in sorted(changes_by_path.items()):
  244. assert len(changes) == num_parents
  245. have = [c for c in changes if c is not None]
  246. if _all_eq(have, change_type, CHANGE_DELETE):
  247. if not _all_same(have, old_sha):
  248. yield changes
  249. elif not _all_same(have, change_type):
  250. yield changes
  251. elif None not in changes:
  252. # If no change was found relative to one parent, that means the SHA
  253. # must have matched the SHA in that parent, so it is not a
  254. # conflict.
  255. yield changes
  256. _BLOCK_SIZE = 64
  257. def _count_blocks(obj):
  258. """Count the blocks in an object.
  259. Splits the data into blocks either on lines or <=64-byte chunks of lines.
  260. Args:
  261. obj: The object to count blocks for.
  262. Returns:
  263. A dict of block hashcode -> total bytes occurring.
  264. """
  265. block_counts: Dict[int, int] = defaultdict(int)
  266. block = BytesIO()
  267. n = 0
  268. # Cache attrs as locals to avoid expensive lookups in the inner loop.
  269. block_write = block.write
  270. block_seek = block.seek
  271. block_truncate = block.truncate
  272. block_getvalue = block.getvalue
  273. for c in chain.from_iterable(obj.as_raw_chunks()):
  274. c = c.to_bytes(1, "big")
  275. block_write(c)
  276. n += 1
  277. if c == b"\n" or n == _BLOCK_SIZE:
  278. value = block_getvalue()
  279. block_counts[hash(value)] += len(value)
  280. block_seek(0)
  281. block_truncate()
  282. n = 0
  283. if n > 0:
  284. last_block = block_getvalue()
  285. block_counts[hash(last_block)] += len(last_block)
  286. return block_counts
  287. def _common_bytes(blocks1, blocks2):
  288. """Count the number of common bytes in two block count dicts.
  289. Args:
  290. blocks1: The first dict of block hashcode -> total bytes.
  291. blocks2: The second dict of block hashcode -> total bytes.
  292. Returns:
  293. The number of bytes in common between blocks1 and blocks2. This is
  294. only approximate due to possible hash collisions.
  295. """
  296. # Iterate over the smaller of the two dicts, since this is symmetrical.
  297. if len(blocks1) > len(blocks2):
  298. blocks1, blocks2 = blocks2, blocks1
  299. score = 0
  300. for block, count1 in blocks1.items():
  301. count2 = blocks2.get(block)
  302. if count2:
  303. score += min(count1, count2)
  304. return score
  305. def _similarity_score(obj1, obj2, block_cache=None):
  306. """Compute a similarity score for two objects.
  307. Args:
  308. obj1: The first object to score.
  309. obj2: The second object to score.
  310. block_cache: An optional dict of SHA to block counts to cache
  311. results between calls.
  312. Returns:
  313. The similarity score between the two objects, defined as the
  314. number of bytes in common between the two objects divided by the
  315. maximum size, scaled to the range 0-100.
  316. """
  317. if block_cache is None:
  318. block_cache = {}
  319. if obj1.id not in block_cache:
  320. block_cache[obj1.id] = _count_blocks(obj1)
  321. if obj2.id not in block_cache:
  322. block_cache[obj2.id] = _count_blocks(obj2)
  323. common_bytes = _common_bytes(block_cache[obj1.id], block_cache[obj2.id])
  324. max_size = max(obj1.raw_length(), obj2.raw_length())
  325. if not max_size:
  326. return _MAX_SCORE
  327. return int(float(common_bytes) * _MAX_SCORE / max_size)
  328. def _tree_change_key(entry):
  329. # Sort by old path then new path. If only one exists, use it for both keys.
  330. path1 = entry.old.path
  331. path2 = entry.new.path
  332. if path1 is None:
  333. path1 = path2
  334. if path2 is None:
  335. path2 = path1
  336. return (path1, path2)
  337. class RenameDetector:
  338. """Object for handling rename detection between two trees."""
  339. def __init__(
  340. self,
  341. store,
  342. rename_threshold=RENAME_THRESHOLD,
  343. max_files=MAX_FILES,
  344. rewrite_threshold=REWRITE_THRESHOLD,
  345. find_copies_harder=False,
  346. ):
  347. """Initialize the rename detector.
  348. Args:
  349. store: An ObjectStore for looking up objects.
  350. rename_threshold: The threshold similarity score for considering
  351. an add/delete pair to be a rename/copy; see _similarity_score.
  352. max_files: The maximum number of adds and deletes to consider,
  353. or None for no limit. The detector is guaranteed to compare no more
  354. than max_files ** 2 add/delete pairs. This limit is provided
  355. because rename detection can be quadratic in the project size. If
  356. the limit is exceeded, no content rename detection is attempted.
  357. rewrite_threshold: The threshold similarity score below which a
  358. modify should be considered a delete/add, or None to not break
  359. modifies; see _similarity_score.
  360. find_copies_harder: If True, consider unmodified files when
  361. detecting copies.
  362. """
  363. self._store = store
  364. self._rename_threshold = rename_threshold
  365. self._rewrite_threshold = rewrite_threshold
  366. self._max_files = max_files
  367. self._find_copies_harder = find_copies_harder
  368. self._want_unchanged = False
  369. def _reset(self):
  370. self._adds = []
  371. self._deletes = []
  372. self._changes = []
  373. def _should_split(self, change):
  374. if (
  375. self._rewrite_threshold is None
  376. or change.type != CHANGE_MODIFY
  377. or change.old.sha == change.new.sha
  378. ):
  379. return False
  380. old_obj = self._store[change.old.sha]
  381. new_obj = self._store[change.new.sha]
  382. return _similarity_score(old_obj, new_obj) < self._rewrite_threshold
  383. def _add_change(self, change):
  384. if change.type == CHANGE_ADD:
  385. self._adds.append(change)
  386. elif change.type == CHANGE_DELETE:
  387. self._deletes.append(change)
  388. elif self._should_split(change):
  389. self._deletes.append(TreeChange.delete(change.old))
  390. self._adds.append(TreeChange.add(change.new))
  391. elif (
  392. self._find_copies_harder and change.type == CHANGE_UNCHANGED
  393. ) or change.type == CHANGE_MODIFY:
  394. # Treat all modifies as potential deletes for rename detection,
  395. # but don't split them (to avoid spurious renames). Setting
  396. # find_copies_harder means we treat unchanged the same as
  397. # modified.
  398. self._deletes.append(change)
  399. else:
  400. self._changes.append(change)
  401. def _collect_changes(self, tree1_id, tree2_id):
  402. want_unchanged = self._find_copies_harder or self._want_unchanged
  403. for change in tree_changes(
  404. self._store,
  405. tree1_id,
  406. tree2_id,
  407. want_unchanged=want_unchanged,
  408. include_trees=self._include_trees,
  409. ):
  410. self._add_change(change)
  411. def _prune(self, add_paths, delete_paths):
  412. self._adds = [a for a in self._adds if a.new.path not in add_paths]
  413. self._deletes = [d for d in self._deletes if d.old.path not in delete_paths]
  414. def _find_exact_renames(self):
  415. add_map = defaultdict(list)
  416. for add in self._adds:
  417. add_map[add.new.sha].append(add.new)
  418. delete_map = defaultdict(list)
  419. for delete in self._deletes:
  420. # Keep track of whether the delete was actually marked as a delete.
  421. # If not, it needs to be marked as a copy.
  422. is_delete = delete.type == CHANGE_DELETE
  423. delete_map[delete.old.sha].append((delete.old, is_delete))
  424. add_paths = set()
  425. delete_paths = set()
  426. for sha, sha_deletes in delete_map.items():
  427. sha_adds = add_map[sha]
  428. for (old, is_delete), new in zip(sha_deletes, sha_adds):
  429. if stat.S_IFMT(old.mode) != stat.S_IFMT(new.mode):
  430. continue
  431. if is_delete:
  432. delete_paths.add(old.path)
  433. add_paths.add(new.path)
  434. new_type = is_delete and CHANGE_RENAME or CHANGE_COPY
  435. self._changes.append(TreeChange(new_type, old, new))
  436. num_extra_adds = len(sha_adds) - len(sha_deletes)
  437. # TODO(dborowitz): Less arbitrary way of dealing with extra copies.
  438. old = sha_deletes[0][0]
  439. if num_extra_adds > 0:
  440. for new in sha_adds[-num_extra_adds:]:
  441. add_paths.add(new.path)
  442. self._changes.append(TreeChange(CHANGE_COPY, old, new))
  443. self._prune(add_paths, delete_paths)
  444. def _should_find_content_renames(self):
  445. return len(self._adds) * len(self._deletes) <= self._max_files ** 2
  446. def _rename_type(self, check_paths, delete, add):
  447. if check_paths and delete.old.path == add.new.path:
  448. # If the paths match, this must be a split modify, so make sure it
  449. # comes out as a modify.
  450. return CHANGE_MODIFY
  451. elif delete.type != CHANGE_DELETE:
  452. # If it's in deletes but not marked as a delete, it must have been
  453. # added due to find_copies_harder, and needs to be marked as a
  454. # copy.
  455. return CHANGE_COPY
  456. return CHANGE_RENAME
  457. def _find_content_rename_candidates(self):
  458. candidates = self._candidates = []
  459. # TODO: Optimizations:
  460. # - Compare object sizes before counting blocks.
  461. # - Skip if delete's S_IFMT differs from all adds.
  462. # - Skip if adds or deletes is empty.
  463. # Match C git's behavior of not attempting to find content renames if
  464. # the matrix size exceeds the threshold.
  465. if not self._should_find_content_renames():
  466. return
  467. block_cache = {}
  468. check_paths = self._rename_threshold is not None
  469. for delete in self._deletes:
  470. if S_ISGITLINK(delete.old.mode):
  471. continue # Git links don't exist in this repo.
  472. old_sha = delete.old.sha
  473. old_obj = self._store[old_sha]
  474. block_cache[old_sha] = _count_blocks(old_obj)
  475. for add in self._adds:
  476. if stat.S_IFMT(delete.old.mode) != stat.S_IFMT(add.new.mode):
  477. continue
  478. new_obj = self._store[add.new.sha]
  479. score = _similarity_score(old_obj, new_obj, block_cache=block_cache)
  480. if score > self._rename_threshold:
  481. new_type = self._rename_type(check_paths, delete, add)
  482. rename = TreeChange(new_type, delete.old, add.new)
  483. candidates.append((-score, rename))
  484. def _choose_content_renames(self):
  485. # Sort scores from highest to lowest, but keep names in ascending
  486. # order.
  487. self._candidates.sort()
  488. delete_paths = set()
  489. add_paths = set()
  490. for _, change in self._candidates:
  491. new_path = change.new.path
  492. if new_path in add_paths:
  493. continue
  494. old_path = change.old.path
  495. orig_type = change.type
  496. if old_path in delete_paths:
  497. change = TreeChange(CHANGE_COPY, change.old, change.new)
  498. # If the candidate was originally a copy, that means it came from a
  499. # modified or unchanged path, so we don't want to prune it.
  500. if orig_type != CHANGE_COPY:
  501. delete_paths.add(old_path)
  502. add_paths.add(new_path)
  503. self._changes.append(change)
  504. self._prune(add_paths, delete_paths)
  505. def _join_modifies(self):
  506. if self._rewrite_threshold is None:
  507. return
  508. modifies = {}
  509. delete_map = {d.old.path: d for d in self._deletes}
  510. for add in self._adds:
  511. path = add.new.path
  512. delete = delete_map.get(path)
  513. if delete is not None and stat.S_IFMT(delete.old.mode) == stat.S_IFMT(
  514. add.new.mode
  515. ):
  516. modifies[path] = TreeChange(CHANGE_MODIFY, delete.old, add.new)
  517. self._adds = [a for a in self._adds if a.new.path not in modifies]
  518. self._deletes = [a for a in self._deletes if a.new.path not in modifies]
  519. self._changes += modifies.values()
  520. def _sorted_changes(self):
  521. result = []
  522. result.extend(self._adds)
  523. result.extend(self._deletes)
  524. result.extend(self._changes)
  525. result.sort(key=_tree_change_key)
  526. return result
  527. def _prune_unchanged(self):
  528. if self._want_unchanged:
  529. return
  530. self._deletes = [d for d in self._deletes if d.type != CHANGE_UNCHANGED]
  531. def changes_with_renames(
  532. self, tree1_id, tree2_id, want_unchanged=False, include_trees=False
  533. ):
  534. """Iterate TreeChanges between two tree SHAs, with rename detection."""
  535. self._reset()
  536. self._want_unchanged = want_unchanged
  537. self._include_trees = include_trees
  538. self._collect_changes(tree1_id, tree2_id)
  539. self._find_exact_renames()
  540. self._find_content_rename_candidates()
  541. self._choose_content_renames()
  542. self._join_modifies()
  543. self._prune_unchanged()
  544. return self._sorted_changes()
  545. # Hold on to the pure-python implementations for testing.
  546. _is_tree_py = _is_tree
  547. _merge_entries_py = _merge_entries
  548. _count_blocks_py = _count_blocks
  549. try:
  550. # Try to import C versions
  551. from dulwich._diff_tree import ( # type: ignore
  552. _is_tree,
  553. _merge_entries,
  554. _count_blocks,
  555. )
  556. except ImportError:
  557. pass