test_walk.py 23 KB

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