2
0

test_walk.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. # test_walk.py -- Tests for commit walking functionality.
  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. """Tests for commit walking functionality."""
  21. from itertools import permutations
  22. from unittest import expectedFailure
  23. from dulwich.diff_tree import CHANGE_MODIFY, CHANGE_RENAME, RenameDetector, TreeChange
  24. from dulwich.errors import MissingCommitError
  25. from dulwich.object_store import MemoryObjectStore
  26. from dulwich.objects import Blob, Commit
  27. from dulwich.tests.utils import F, build_commit_graph, make_object, make_tag
  28. from dulwich.walk import ORDER_TOPO, WalkEntry, Walker, _topo_reorder
  29. from . import TestCase
  30. class TestWalkEntry:
  31. def __init__(self, commit, changes) -> None:
  32. self.commit = commit
  33. self.changes = changes
  34. def __repr__(self) -> str:
  35. return f"<TestWalkEntry commit={self.commit.id}, changes={self.changes!r}>"
  36. def __eq__(self, other):
  37. if not isinstance(other, WalkEntry) or self.commit != other.commit:
  38. return False
  39. if self.changes is None:
  40. return True
  41. return self.changes == other.changes()
  42. class WalkerTest(TestCase):
  43. def setUp(self):
  44. super().setUp()
  45. self.store = MemoryObjectStore()
  46. def make_commits(self, commit_spec, **kwargs):
  47. times = kwargs.pop("times", [])
  48. attrs = kwargs.pop("attrs", {})
  49. for i, t in enumerate(times):
  50. attrs.setdefault(i + 1, {})["commit_time"] = t
  51. return build_commit_graph(self.store, commit_spec, attrs=attrs, **kwargs)
  52. def make_linear_commits(self, num_commits, **kwargs):
  53. commit_spec = []
  54. for i in range(1, num_commits + 1):
  55. c = [i]
  56. if i > 1:
  57. c.append(i - 1)
  58. commit_spec.append(c)
  59. return self.make_commits(commit_spec, **kwargs)
  60. def assertWalkYields(self, expected, *args, **kwargs):
  61. walker = Walker(self.store, *args, **kwargs)
  62. expected = list(expected)
  63. for i, entry in enumerate(expected):
  64. if isinstance(entry, Commit):
  65. expected[i] = TestWalkEntry(entry, None)
  66. actual = list(walker)
  67. self.assertEqual(expected, actual)
  68. def test_tag(self):
  69. c1, c2, c3 = self.make_linear_commits(3)
  70. t2 = make_tag(target=c2)
  71. self.store.add_object(t2)
  72. self.assertWalkYields([c2, c1], [t2.id])
  73. def test_linear(self):
  74. c1, c2, c3 = self.make_linear_commits(3)
  75. self.assertWalkYields([c1], [c1.id])
  76. self.assertWalkYields([c2, c1], [c2.id])
  77. self.assertWalkYields([c3, c2, c1], [c3.id])
  78. self.assertWalkYields([c3, c2, c1], [c3.id, c1.id])
  79. self.assertWalkYields([c3, c2], [c3.id], exclude=[c1.id])
  80. self.assertWalkYields([c3, c2], [c3.id, c1.id], exclude=[c1.id])
  81. self.assertWalkYields([c3], [c3.id, c1.id], exclude=[c2.id])
  82. def test_missing(self):
  83. cs = list(reversed(self.make_linear_commits(20)))
  84. self.assertWalkYields(cs, [cs[0].id])
  85. # Exactly how close we can get to a missing commit depends on our
  86. # implementation (in particular the choice of _MAX_EXTRA_COMMITS), but
  87. # we should at least be able to walk some history in a broken repo.
  88. del self.store[cs[-1].id]
  89. for i in range(1, 11):
  90. self.assertWalkYields(cs[:i], [cs[0].id], max_entries=i)
  91. self.assertRaises(MissingCommitError, Walker, self.store, [cs[-1].id])
  92. def test_branch(self):
  93. c1, x2, x3, y4 = self.make_commits([[1], [2, 1], [3, 2], [4, 1]])
  94. self.assertWalkYields([x3, x2, c1], [x3.id])
  95. self.assertWalkYields([y4, c1], [y4.id])
  96. self.assertWalkYields([y4, x2, c1], [y4.id, x2.id])
  97. self.assertWalkYields([y4, x2], [y4.id, x2.id], exclude=[c1.id])
  98. self.assertWalkYields([y4, x3], [y4.id, x3.id], exclude=[x2.id])
  99. self.assertWalkYields([y4], [y4.id], exclude=[x3.id])
  100. self.assertWalkYields([x3, x2], [x3.id], exclude=[y4.id])
  101. def test_merge(self):
  102. c1, c2, c3, c4 = self.make_commits([[1], [2, 1], [3, 1], [4, 2, 3]])
  103. self.assertWalkYields([c4, c3, c2, c1], [c4.id])
  104. self.assertWalkYields([c3, c1], [c3.id])
  105. self.assertWalkYields([c2, c1], [c2.id])
  106. self.assertWalkYields([c4, c3], [c4.id], exclude=[c2.id])
  107. self.assertWalkYields([c4, c2], [c4.id], exclude=[c3.id])
  108. def test_merge_of_new_branch_from_old_base(self):
  109. # The commit on the branch was made at a time after any of the
  110. # commits on master, but the branch was from an older commit.
  111. # See also test_merge_of_old_branch
  112. self.maxDiff = None
  113. c1, c2, c3, c4, c5 = self.make_commits(
  114. [[1], [2, 1], [3, 2], [4, 1], [5, 3, 4]],
  115. times=[1, 2, 3, 4, 5],
  116. )
  117. self.assertWalkYields([c5, c4, c3, c2, c1], [c5.id])
  118. self.assertWalkYields([c3, c2, c1], [c3.id])
  119. self.assertWalkYields([c2, c1], [c2.id])
  120. @expectedFailure
  121. def test_merge_of_old_branch(self):
  122. # The commit on the branch was made at a time before any of
  123. # the commits on master, but it was merged into master after
  124. # those commits.
  125. # See also test_merge_of_new_branch_from_old_base
  126. self.maxDiff = None
  127. c1, c2, c3, c4, c5 = self.make_commits(
  128. [[1], [2, 1], [3, 2], [4, 1], [5, 3, 4]],
  129. times=[1, 3, 4, 2, 5],
  130. )
  131. self.assertWalkYields([c5, c4, c3, c2, c1], [c5.id])
  132. self.assertWalkYields([c3, c2, c1], [c3.id])
  133. self.assertWalkYields([c2, c1], [c2.id])
  134. def test_reverse(self):
  135. c1, c2, c3 = self.make_linear_commits(3)
  136. self.assertWalkYields([c1, c2, c3], [c3.id], reverse=True)
  137. def test_max_entries(self):
  138. c1, c2, c3 = self.make_linear_commits(3)
  139. self.assertWalkYields([c3, c2, c1], [c3.id], max_entries=3)
  140. self.assertWalkYields([c3, c2], [c3.id], max_entries=2)
  141. self.assertWalkYields([c3], [c3.id], max_entries=1)
  142. def test_reverse_after_max_entries(self):
  143. c1, c2, c3 = self.make_linear_commits(3)
  144. self.assertWalkYields([c1, c2, c3], [c3.id], max_entries=3, reverse=True)
  145. self.assertWalkYields([c2, c3], [c3.id], max_entries=2, reverse=True)
  146. self.assertWalkYields([c3], [c3.id], max_entries=1, reverse=True)
  147. def test_changes_one_parent(self):
  148. blob_a1 = make_object(Blob, data=b"a1")
  149. blob_a2 = make_object(Blob, data=b"a2")
  150. blob_b2 = make_object(Blob, data=b"b2")
  151. c1, c2 = self.make_linear_commits(
  152. 2,
  153. trees={
  154. 1: [(b"a", blob_a1)],
  155. 2: [(b"a", blob_a2), (b"b", blob_b2)],
  156. },
  157. )
  158. e1 = TestWalkEntry(c1, [TreeChange.add((b"a", F, blob_a1.id))])
  159. e2 = TestWalkEntry(
  160. c2,
  161. [
  162. TreeChange(CHANGE_MODIFY, (b"a", F, blob_a1.id), (b"a", F, blob_a2.id)),
  163. TreeChange.add((b"b", F, blob_b2.id)),
  164. ],
  165. )
  166. self.assertWalkYields([e2, e1], [c2.id])
  167. def test_changes_multiple_parents(self):
  168. blob_a1 = make_object(Blob, data=b"a1")
  169. blob_b2 = make_object(Blob, data=b"b2")
  170. blob_a3 = make_object(Blob, data=b"a3")
  171. c1, c2, c3 = self.make_commits(
  172. [[1], [2], [3, 1, 2]],
  173. trees={
  174. 1: [(b"a", blob_a1)],
  175. 2: [(b"b", blob_b2)],
  176. 3: [(b"a", blob_a3), (b"b", blob_b2)],
  177. },
  178. )
  179. # a is a modify/add conflict and b is not conflicted.
  180. changes = [
  181. [
  182. TreeChange(CHANGE_MODIFY, (b"a", F, blob_a1.id), (b"a", F, blob_a3.id)),
  183. TreeChange.add((b"a", F, blob_a3.id)),
  184. ]
  185. ]
  186. self.assertWalkYields(
  187. [TestWalkEntry(c3, changes)], [c3.id], exclude=[c1.id, c2.id]
  188. )
  189. def test_path_matches(self):
  190. walker = Walker(None, [], paths=[b"foo", b"bar", b"baz/quux"])
  191. self.assertTrue(walker._path_matches(b"foo"))
  192. self.assertTrue(walker._path_matches(b"foo/a"))
  193. self.assertTrue(walker._path_matches(b"foo/a/b"))
  194. self.assertTrue(walker._path_matches(b"bar"))
  195. self.assertTrue(walker._path_matches(b"baz/quux"))
  196. self.assertTrue(walker._path_matches(b"baz/quux/a"))
  197. self.assertFalse(walker._path_matches(None))
  198. self.assertFalse(walker._path_matches(b"oops"))
  199. self.assertFalse(walker._path_matches(b"fool"))
  200. self.assertFalse(walker._path_matches(b"baz"))
  201. self.assertFalse(walker._path_matches(b"baz/quu"))
  202. def test_paths(self):
  203. blob_a1 = make_object(Blob, data=b"a1")
  204. blob_b2 = make_object(Blob, data=b"b2")
  205. blob_a3 = make_object(Blob, data=b"a3")
  206. blob_b3 = make_object(Blob, data=b"b3")
  207. c1, c2, c3 = self.make_linear_commits(
  208. 3,
  209. trees={
  210. 1: [(b"a", blob_a1)],
  211. 2: [(b"a", blob_a1), (b"x/b", blob_b2)],
  212. 3: [(b"a", blob_a3), (b"x/b", blob_b3)],
  213. },
  214. )
  215. self.assertWalkYields([c3, c2, c1], [c3.id])
  216. self.assertWalkYields([c3, c1], [c3.id], paths=[b"a"])
  217. self.assertWalkYields([c3, c2], [c3.id], paths=[b"x/b"])
  218. # All changes are included, not just for requested paths.
  219. changes = [
  220. TreeChange(CHANGE_MODIFY, (b"a", F, blob_a1.id), (b"a", F, blob_a3.id)),
  221. TreeChange(CHANGE_MODIFY, (b"x/b", F, blob_b2.id), (b"x/b", F, blob_b3.id)),
  222. ]
  223. self.assertWalkYields(
  224. [TestWalkEntry(c3, changes)], [c3.id], max_entries=1, paths=[b"a"]
  225. )
  226. def test_paths_subtree(self):
  227. blob_a = make_object(Blob, data=b"a")
  228. blob_b = make_object(Blob, data=b"b")
  229. c1, c2, c3 = self.make_linear_commits(
  230. 3,
  231. trees={
  232. 1: [(b"x/a", blob_a)],
  233. 2: [(b"b", blob_b), (b"x/a", blob_a)],
  234. 3: [(b"b", blob_b), (b"x/a", blob_a), (b"x/b", blob_b)],
  235. },
  236. )
  237. self.assertWalkYields([c2], [c3.id], paths=[b"b"])
  238. self.assertWalkYields([c3, c1], [c3.id], paths=[b"x"])
  239. def test_paths_max_entries(self):
  240. blob_a = make_object(Blob, data=b"a")
  241. blob_b = make_object(Blob, data=b"b")
  242. c1, c2 = self.make_linear_commits(
  243. 2, trees={1: [(b"a", blob_a)], 2: [(b"a", blob_a), (b"b", blob_b)]}
  244. )
  245. self.assertWalkYields([c2], [c2.id], paths=[b"b"], max_entries=1)
  246. self.assertWalkYields([c1], [c1.id], paths=[b"a"], max_entries=1)
  247. def test_paths_merge(self):
  248. blob_a1 = make_object(Blob, data=b"a1")
  249. blob_a2 = make_object(Blob, data=b"a2")
  250. blob_a3 = make_object(Blob, data=b"a3")
  251. x1, y2, m3, m4 = self.make_commits(
  252. [[1], [2], [3, 1, 2], [4, 1, 2]],
  253. trees={
  254. 1: [(b"a", blob_a1)],
  255. 2: [(b"a", blob_a2)],
  256. 3: [(b"a", blob_a3)],
  257. 4: [(b"a", blob_a1)],
  258. },
  259. ) # Non-conflicting
  260. self.assertWalkYields([m3, y2, x1], [m3.id], paths=[b"a"])
  261. self.assertWalkYields([y2, x1], [m4.id], paths=[b"a"])
  262. def test_changes_with_renames(self):
  263. blob = make_object(Blob, data=b"blob")
  264. c1, c2 = self.make_linear_commits(
  265. 2, trees={1: [(b"a", blob)], 2: [(b"b", blob)]}
  266. )
  267. entry_a = (b"a", F, blob.id)
  268. entry_b = (b"b", F, blob.id)
  269. changes_without_renames = [
  270. TreeChange.delete(entry_a),
  271. TreeChange.add(entry_b),
  272. ]
  273. changes_with_renames = [TreeChange(CHANGE_RENAME, entry_a, entry_b)]
  274. self.assertWalkYields(
  275. [TestWalkEntry(c2, changes_without_renames)],
  276. [c2.id],
  277. max_entries=1,
  278. )
  279. detector = RenameDetector(self.store)
  280. self.assertWalkYields(
  281. [TestWalkEntry(c2, changes_with_renames)],
  282. [c2.id],
  283. max_entries=1,
  284. rename_detector=detector,
  285. )
  286. def test_follow_rename(self):
  287. blob = make_object(Blob, data=b"blob")
  288. names = [b"a", b"a", b"b", b"b", b"c", b"c"]
  289. trees = {i + 1: [(n, blob, F)] for i, n in enumerate(names)}
  290. c1, c2, c3, c4, c5, c6 = self.make_linear_commits(6, trees=trees)
  291. self.assertWalkYields([c5], [c6.id], paths=[b"c"])
  292. def e(n):
  293. return (n, F, blob.id)
  294. self.assertWalkYields(
  295. [
  296. TestWalkEntry(c5, [TreeChange(CHANGE_RENAME, e(b"b"), e(b"c"))]),
  297. TestWalkEntry(c3, [TreeChange(CHANGE_RENAME, e(b"a"), e(b"b"))]),
  298. TestWalkEntry(c1, [TreeChange.add(e(b"a"))]),
  299. ],
  300. [c6.id],
  301. paths=[b"c"],
  302. follow=True,
  303. )
  304. def test_follow_rename_remove_path(self):
  305. blob = make_object(Blob, data=b"blob")
  306. _, _, _, c4, c5, c6 = self.make_linear_commits(
  307. 6,
  308. trees={
  309. 1: [(b"a", blob), (b"c", blob)],
  310. 2: [],
  311. 3: [],
  312. 4: [(b"b", blob)],
  313. 5: [(b"a", blob)],
  314. 6: [(b"c", blob)],
  315. },
  316. )
  317. def e(n):
  318. return (n, F, blob.id)
  319. # Once the path changes to b, we aren't interested in a or c anymore.
  320. self.assertWalkYields(
  321. [
  322. TestWalkEntry(c6, [TreeChange(CHANGE_RENAME, e(b"a"), e(b"c"))]),
  323. TestWalkEntry(c5, [TreeChange(CHANGE_RENAME, e(b"b"), e(b"a"))]),
  324. TestWalkEntry(c4, [TreeChange.add(e(b"b"))]),
  325. ],
  326. [c6.id],
  327. paths=[b"c"],
  328. follow=True,
  329. )
  330. def test_since(self):
  331. c1, c2, c3 = self.make_linear_commits(3)
  332. self.assertWalkYields([c3, c2, c1], [c3.id], since=-1)
  333. self.assertWalkYields([c3, c2, c1], [c3.id], since=0)
  334. self.assertWalkYields([c3, c2], [c3.id], since=1)
  335. self.assertWalkYields([c3, c2], [c3.id], since=99)
  336. self.assertWalkYields([c3, c2], [c3.id], since=100)
  337. self.assertWalkYields([c3], [c3.id], since=101)
  338. self.assertWalkYields([c3], [c3.id], since=199)
  339. self.assertWalkYields([c3], [c3.id], since=200)
  340. self.assertWalkYields([], [c3.id], since=201)
  341. self.assertWalkYields([], [c3.id], since=300)
  342. def test_until(self):
  343. c1, c2, c3 = self.make_linear_commits(3)
  344. self.assertWalkYields([], [c3.id], until=-1)
  345. self.assertWalkYields([c1], [c3.id], until=0)
  346. self.assertWalkYields([c1], [c3.id], until=1)
  347. self.assertWalkYields([c1], [c3.id], until=99)
  348. self.assertWalkYields([c2, c1], [c3.id], until=100)
  349. self.assertWalkYields([c2, c1], [c3.id], until=101)
  350. self.assertWalkYields([c2, c1], [c3.id], until=199)
  351. self.assertWalkYields([c3, c2, c1], [c3.id], until=200)
  352. self.assertWalkYields([c3, c2, c1], [c3.id], until=201)
  353. self.assertWalkYields([c3, c2, c1], [c3.id], until=300)
  354. def test_since_until(self):
  355. c1, c2, c3 = self.make_linear_commits(3)
  356. self.assertWalkYields([], [c3.id], since=100, until=99)
  357. self.assertWalkYields([c3, c2, c1], [c3.id], since=-1, until=201)
  358. self.assertWalkYields([c2], [c3.id], since=100, until=100)
  359. self.assertWalkYields([c2], [c3.id], since=50, until=150)
  360. def test_since_over_scan(self):
  361. commits = self.make_linear_commits(11, times=[9, 0, 1, 2, 3, 4, 5, 8, 6, 7, 9])
  362. c8, _, c10, c11 = commits[-4:]
  363. del self.store[commits[0].id]
  364. # c9 is older than we want to walk, but is out of order with its
  365. # parent, so we need to walk past it to get to c8.
  366. # c1 would also match, but we've deleted it, and it should get pruned
  367. # even with over-scanning.
  368. self.assertWalkYields([c11, c10, c8], [c11.id], since=7)
  369. def assertTopoOrderEqual(self, expected_commits, commits):
  370. entries = [TestWalkEntry(c, None) for c in commits]
  371. actual_ids = [e.commit.id for e in list(_topo_reorder(entries))]
  372. self.assertEqual([c.id for c in expected_commits], actual_ids)
  373. def test_topo_reorder_linear(self):
  374. commits = self.make_linear_commits(5)
  375. commits.reverse()
  376. for perm in permutations(commits):
  377. self.assertTopoOrderEqual(commits, perm)
  378. def test_topo_reorder_multiple_parents(self):
  379. c1, c2, c3 = self.make_commits([[1], [2], [3, 1, 2]])
  380. # Already sorted, so totally FIFO.
  381. self.assertTopoOrderEqual([c3, c2, c1], [c3, c2, c1])
  382. self.assertTopoOrderEqual([c3, c1, c2], [c3, c1, c2])
  383. # c3 causes one parent to be yielded.
  384. self.assertTopoOrderEqual([c3, c2, c1], [c2, c3, c1])
  385. self.assertTopoOrderEqual([c3, c1, c2], [c1, c3, c2])
  386. # c3 causes both parents to be yielded.
  387. self.assertTopoOrderEqual([c3, c2, c1], [c1, c2, c3])
  388. self.assertTopoOrderEqual([c3, c2, c1], [c2, c1, c3])
  389. def test_topo_reorder_multiple_children(self):
  390. c1, c2, c3 = self.make_commits([[1], [2, 1], [3, 1]])
  391. # c2 and c3 are FIFO but c1 moves to the end.
  392. self.assertTopoOrderEqual([c3, c2, c1], [c3, c2, c1])
  393. self.assertTopoOrderEqual([c3, c2, c1], [c3, c1, c2])
  394. self.assertTopoOrderEqual([c3, c2, c1], [c1, c3, c2])
  395. self.assertTopoOrderEqual([c2, c3, c1], [c2, c3, c1])
  396. self.assertTopoOrderEqual([c2, c3, c1], [c2, c1, c3])
  397. self.assertTopoOrderEqual([c2, c3, c1], [c1, c2, c3])
  398. def test_out_of_order_children(self):
  399. c1, c2, c3, c4, c5 = self.make_commits(
  400. [[1], [2, 1], [3, 2], [4, 1], [5, 3, 4]], times=[2, 1, 3, 4, 5]
  401. )
  402. self.assertWalkYields([c5, c4, c3, c1, c2], [c5.id])
  403. self.assertWalkYields([c5, c4, c3, c2, c1], [c5.id], order=ORDER_TOPO)
  404. def test_out_of_order_with_exclude(self):
  405. # Create the following graph:
  406. # c1-------x2---m6
  407. # \ /
  408. # \-y3--y4-/--y5
  409. # Due to skew, y5 is the oldest commit.
  410. c1, x2, y3, y4, y5, m6 = self.make_commits(
  411. [[1], [2, 1], [3, 1], [4, 3], [5, 4], [6, 2, 4]],
  412. times=[2, 3, 4, 5, 1, 6],
  413. )
  414. self.assertWalkYields([m6, y4, y3, x2, c1], [m6.id])
  415. # Ensure that c1..y4 get excluded even though they're popped from the
  416. # priority queue long before y5.
  417. self.assertWalkYields([m6, x2], [m6.id], exclude=[y5.id])
  418. def test_empty_walk(self):
  419. c1, c2, c3 = self.make_linear_commits(3)
  420. self.assertWalkYields([], [c3.id], exclude=[c3.id])
  421. class WalkEntryTest(TestCase):
  422. def setUp(self):
  423. super().setUp()
  424. self.store = MemoryObjectStore()
  425. def make_commits(self, commit_spec, **kwargs):
  426. times = kwargs.pop("times", [])
  427. attrs = kwargs.pop("attrs", {})
  428. for i, t in enumerate(times):
  429. attrs.setdefault(i + 1, {})["commit_time"] = t
  430. return build_commit_graph(self.store, commit_spec, attrs=attrs, **kwargs)
  431. def make_linear_commits(self, num_commits, **kwargs):
  432. commit_spec = []
  433. for i in range(1, num_commits + 1):
  434. c = [i]
  435. if i > 1:
  436. c.append(i - 1)
  437. commit_spec.append(c)
  438. return self.make_commits(commit_spec, **kwargs)
  439. def test_all_changes(self):
  440. # Construct a commit with 2 files in different subdirectories.
  441. blob_a = make_object(Blob, data=b"a")
  442. blob_b = make_object(Blob, data=b"b")
  443. c1 = self.make_linear_commits(
  444. 1,
  445. trees={1: [(b"x/a", blob_a), (b"y/b", blob_b)]},
  446. )[0]
  447. # Get the WalkEntry for the commit.
  448. walker = Walker(self.store, c1.id)
  449. walker_entry = next(iter(walker))
  450. changes = walker_entry.changes()
  451. # Compare the changes with the expected values.
  452. entry_a = (b"x/a", F, blob_a.id)
  453. entry_b = (b"y/b", F, blob_b.id)
  454. self.assertEqual(
  455. [TreeChange.add(entry_a), TreeChange.add(entry_b)],
  456. changes,
  457. )
  458. def test_all_with_merge(self):
  459. blob_a = make_object(Blob, data=b"a")
  460. blob_a2 = make_object(Blob, data=b"a2")
  461. blob_b = make_object(Blob, data=b"b")
  462. blob_b2 = make_object(Blob, data=b"b2")
  463. x1, y2, m3 = self.make_commits(
  464. [[1], [2], [3, 1, 2]],
  465. trees={
  466. 1: [(b"x/a", blob_a)],
  467. 2: [(b"y/b", blob_b)],
  468. 3: [(b"x/a", blob_a2), (b"y/b", blob_b2)],
  469. },
  470. )
  471. # Get the WalkEntry for the merge commit.
  472. walker = Walker(self.store, m3.id)
  473. entries = list(walker)
  474. walker_entry = entries[0]
  475. self.assertEqual(walker_entry.commit.id, m3.id)
  476. changes = walker_entry.changes()
  477. self.assertEqual(2, len(changes))
  478. entry_a = (b"x/a", F, blob_a.id)
  479. entry_a2 = (b"x/a", F, blob_a2.id)
  480. entry_b = (b"y/b", F, blob_b.id)
  481. entry_b2 = (b"y/b", F, blob_b2.id)
  482. self.assertEqual(
  483. [
  484. [
  485. TreeChange(CHANGE_MODIFY, entry_a, entry_a2),
  486. TreeChange.add(entry_a2),
  487. ],
  488. [
  489. TreeChange.add(entry_b2),
  490. TreeChange(CHANGE_MODIFY, entry_b, entry_b2),
  491. ],
  492. ],
  493. changes,
  494. )
  495. def test_filter_changes(self):
  496. # Construct a commit with 2 files in different subdirectories.
  497. blob_a = make_object(Blob, data=b"a")
  498. blob_b = make_object(Blob, data=b"b")
  499. c1 = self.make_linear_commits(
  500. 1,
  501. trees={1: [(b"x/a", blob_a), (b"y/b", blob_b)]},
  502. )[0]
  503. # Get the WalkEntry for the commit.
  504. walker = Walker(self.store, c1.id)
  505. walker_entry = next(iter(walker))
  506. changes = walker_entry.changes(path_prefix=b"x")
  507. # Compare the changes with the expected values.
  508. entry_a = (b"a", F, blob_a.id)
  509. self.assertEqual(
  510. [TreeChange.add(entry_a)],
  511. changes,
  512. )
  513. def test_filter_with_merge(self):
  514. blob_a = make_object(Blob, data=b"a")
  515. blob_a2 = make_object(Blob, data=b"a2")
  516. blob_b = make_object(Blob, data=b"b")
  517. blob_b2 = make_object(Blob, data=b"b2")
  518. x1, y2, m3 = self.make_commits(
  519. [[1], [2], [3, 1, 2]],
  520. trees={
  521. 1: [(b"x/a", blob_a)],
  522. 2: [(b"y/b", blob_b)],
  523. 3: [(b"x/a", blob_a2), (b"y/b", blob_b2)],
  524. },
  525. )
  526. # Get the WalkEntry for the merge commit.
  527. walker = Walker(self.store, m3.id)
  528. entries = list(walker)
  529. walker_entry = entries[0]
  530. self.assertEqual(walker_entry.commit.id, m3.id)
  531. changes = walker_entry.changes(b"x")
  532. self.assertEqual(1, len(changes))
  533. entry_a = (b"a", F, blob_a.id)
  534. entry_a2 = (b"a", F, blob_a2.id)
  535. self.assertEqual(
  536. [[TreeChange(CHANGE_MODIFY, entry_a, entry_a2)]],
  537. changes,
  538. )