test_walk.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. # test_walk.py -- Tests for commit walking functionality.
  2. # Copyright (C) 2010 Google, Inc.
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2
  7. # or (at your option) any later version of the License.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. """Tests for commit walking functionality."""
  19. from itertools import (
  20. permutations,
  21. )
  22. from dulwich.diff_tree import (
  23. CHANGE_MODIFY,
  24. CHANGE_RENAME,
  25. TreeChange,
  26. RenameDetector,
  27. )
  28. from dulwich.errors import (
  29. MissingCommitError,
  30. )
  31. from dulwich.object_store import (
  32. MemoryObjectStore,
  33. )
  34. from dulwich.objects import (
  35. Commit,
  36. Blob,
  37. )
  38. from dulwich.walk import (
  39. ORDER_TOPO,
  40. WalkEntry,
  41. Walker,
  42. _topo_reorder
  43. )
  44. from dulwich.tests import TestCase
  45. from dulwich.tests.utils import (
  46. F,
  47. make_object,
  48. build_commit_graph,
  49. skipIfPY3,
  50. )
  51. class TestWalkEntry(object):
  52. def __init__(self, commit, changes):
  53. self.commit = commit
  54. self.changes = changes
  55. def __repr__(self):
  56. return '<TestWalkEntry commit=%s, changes=%r>' % (
  57. self.commit.id, self.changes)
  58. def __eq__(self, other):
  59. if not isinstance(other, WalkEntry) or self.commit != other.commit:
  60. return False
  61. if self.changes is None:
  62. return True
  63. return self.changes == other.changes()
  64. @skipIfPY3
  65. class WalkerTest(TestCase):
  66. def setUp(self):
  67. super(WalkerTest, self).setUp()
  68. self.store = MemoryObjectStore()
  69. def make_commits(self, commit_spec, **kwargs):
  70. times = kwargs.pop('times', [])
  71. attrs = kwargs.pop('attrs', {})
  72. for i, t in enumerate(times):
  73. attrs.setdefault(i + 1, {})['commit_time'] = t
  74. return build_commit_graph(self.store, commit_spec, attrs=attrs,
  75. **kwargs)
  76. def make_linear_commits(self, num_commits, **kwargs):
  77. commit_spec = []
  78. for i in range(1, num_commits + 1):
  79. c = [i]
  80. if i > 1:
  81. c.append(i - 1)
  82. commit_spec.append(c)
  83. return self.make_commits(commit_spec, **kwargs)
  84. def assertWalkYields(self, expected, *args, **kwargs):
  85. walker = Walker(self.store, *args, **kwargs)
  86. expected = list(expected)
  87. for i, entry in enumerate(expected):
  88. if isinstance(entry, Commit):
  89. expected[i] = TestWalkEntry(entry, None)
  90. actual = list(walker)
  91. self.assertEqual(expected, actual)
  92. def test_linear(self):
  93. c1, c2, c3 = self.make_linear_commits(3)
  94. self.assertWalkYields([c1], [c1.id])
  95. self.assertWalkYields([c2, c1], [c2.id])
  96. self.assertWalkYields([c3, c2, c1], [c3.id])
  97. self.assertWalkYields([c3, c2, c1], [c3.id, c1.id])
  98. self.assertWalkYields([c3, c2], [c3.id], exclude=[c1.id])
  99. self.assertWalkYields([c3, c2], [c3.id, c1.id], exclude=[c1.id])
  100. self.assertWalkYields([c3], [c3.id, c1.id], exclude=[c2.id])
  101. def test_missing(self):
  102. cs = list(reversed(self.make_linear_commits(20)))
  103. self.assertWalkYields(cs, [cs[0].id])
  104. # Exactly how close we can get to a missing commit depends on our
  105. # implementation (in particular the choice of _MAX_EXTRA_COMMITS), but
  106. # we should at least be able to walk some history in a broken repo.
  107. del self.store[cs[-1].id]
  108. for i in range(1, 11):
  109. self.assertWalkYields(cs[:i], [cs[0].id], max_entries=i)
  110. self.assertRaises(MissingCommitError, Walker, self.store, [cs[-1].id])
  111. def test_branch(self):
  112. c1, x2, x3, y4 = self.make_commits([[1], [2, 1], [3, 2], [4, 1]])
  113. self.assertWalkYields([x3, x2, c1], [x3.id])
  114. self.assertWalkYields([y4, c1], [y4.id])
  115. self.assertWalkYields([y4, x2, c1], [y4.id, x2.id])
  116. self.assertWalkYields([y4, x2], [y4.id, x2.id], exclude=[c1.id])
  117. self.assertWalkYields([y4, x3], [y4.id, x3.id], exclude=[x2.id])
  118. self.assertWalkYields([y4], [y4.id], exclude=[x3.id])
  119. self.assertWalkYields([x3, x2], [x3.id], exclude=[y4.id])
  120. def test_merge(self):
  121. c1, c2, c3, c4 = self.make_commits([[1], [2, 1], [3, 1], [4, 2, 3]])
  122. self.assertWalkYields([c4, c3, c2, c1], [c4.id])
  123. self.assertWalkYields([c3, c1], [c3.id])
  124. self.assertWalkYields([c2, c1], [c2.id])
  125. self.assertWalkYields([c4, c3], [c4.id], exclude=[c2.id])
  126. self.assertWalkYields([c4, c2], [c4.id], exclude=[c3.id])
  127. def test_reverse(self):
  128. c1, c2, c3 = self.make_linear_commits(3)
  129. self.assertWalkYields([c1, c2, c3], [c3.id], reverse=True)
  130. def test_max_entries(self):
  131. c1, c2, c3 = self.make_linear_commits(3)
  132. self.assertWalkYields([c3, c2, c1], [c3.id], max_entries=3)
  133. self.assertWalkYields([c3, c2], [c3.id], max_entries=2)
  134. self.assertWalkYields([c3], [c3.id], max_entries=1)
  135. def test_reverse_after_max_entries(self):
  136. c1, c2, c3 = self.make_linear_commits(3)
  137. self.assertWalkYields([c1, c2, c3], [c3.id], max_entries=3,
  138. reverse=True)
  139. self.assertWalkYields([c2, c3], [c3.id], max_entries=2, reverse=True)
  140. self.assertWalkYields([c3], [c3.id], max_entries=1, reverse=True)
  141. def test_changes_one_parent(self):
  142. blob_a1 = make_object(Blob, data='a1')
  143. blob_a2 = make_object(Blob, data='a2')
  144. blob_b2 = make_object(Blob, data='b2')
  145. c1, c2 = self.make_linear_commits(
  146. 2, trees={1: [('a', blob_a1)],
  147. 2: [('a', blob_a2), ('b', blob_b2)]})
  148. e1 = TestWalkEntry(c1, [TreeChange.add(('a', F, blob_a1.id))])
  149. e2 = TestWalkEntry(c2, [TreeChange(CHANGE_MODIFY, ('a', F, blob_a1.id),
  150. ('a', F, blob_a2.id)),
  151. TreeChange.add(('b', F, blob_b2.id))])
  152. self.assertWalkYields([e2, e1], [c2.id])
  153. def test_changes_multiple_parents(self):
  154. blob_a1 = make_object(Blob, data='a1')
  155. blob_b2 = make_object(Blob, data='b2')
  156. blob_a3 = make_object(Blob, data='a3')
  157. c1, c2, c3 = self.make_commits(
  158. [[1], [2], [3, 1, 2]],
  159. trees={1: [('a', blob_a1)], 2: [('b', blob_b2)],
  160. 3: [('a', blob_a3), ('b', blob_b2)]})
  161. # a is a modify/add conflict and b is not conflicted.
  162. changes = [[
  163. TreeChange(CHANGE_MODIFY, ('a', F, blob_a1.id), ('a', F, blob_a3.id)),
  164. TreeChange.add(('a', F, blob_a3.id)),
  165. ]]
  166. self.assertWalkYields([TestWalkEntry(c3, changes)], [c3.id],
  167. exclude=[c1.id, c2.id])
  168. def test_path_matches(self):
  169. walker = Walker(None, [], paths=['foo', 'bar', 'baz/quux'])
  170. self.assertTrue(walker._path_matches('foo'))
  171. self.assertTrue(walker._path_matches('foo/a'))
  172. self.assertTrue(walker._path_matches('foo/a/b'))
  173. self.assertTrue(walker._path_matches('bar'))
  174. self.assertTrue(walker._path_matches('baz/quux'))
  175. self.assertTrue(walker._path_matches('baz/quux/a'))
  176. self.assertFalse(walker._path_matches(None))
  177. self.assertFalse(walker._path_matches('oops'))
  178. self.assertFalse(walker._path_matches('fool'))
  179. self.assertFalse(walker._path_matches('baz'))
  180. self.assertFalse(walker._path_matches('baz/quu'))
  181. def test_paths(self):
  182. blob_a1 = make_object(Blob, data='a1')
  183. blob_b2 = make_object(Blob, data='b2')
  184. blob_a3 = make_object(Blob, data='a3')
  185. blob_b3 = make_object(Blob, data='b3')
  186. c1, c2, c3 = self.make_linear_commits(
  187. 3, trees={1: [('a', blob_a1)],
  188. 2: [('a', blob_a1), ('x/b', blob_b2)],
  189. 3: [('a', blob_a3), ('x/b', blob_b3)]})
  190. self.assertWalkYields([c3, c2, c1], [c3.id])
  191. self.assertWalkYields([c3, c1], [c3.id], paths=['a'])
  192. self.assertWalkYields([c3, c2], [c3.id], paths=['x/b'])
  193. # All changes are included, not just for requested paths.
  194. changes = [
  195. TreeChange(CHANGE_MODIFY, ('a', F, blob_a1.id),
  196. ('a', F, blob_a3.id)),
  197. TreeChange(CHANGE_MODIFY, ('x/b', F, blob_b2.id),
  198. ('x/b', F, blob_b3.id)),
  199. ]
  200. self.assertWalkYields([TestWalkEntry(c3, changes)], [c3.id],
  201. max_entries=1, paths=['a'])
  202. def test_paths_subtree(self):
  203. blob_a = make_object(Blob, data='a')
  204. blob_b = make_object(Blob, data='b')
  205. c1, c2, c3 = self.make_linear_commits(
  206. 3, trees={1: [('x/a', blob_a)],
  207. 2: [('b', blob_b), ('x/a', blob_a)],
  208. 3: [('b', blob_b), ('x/a', blob_a), ('x/b', blob_b)]})
  209. self.assertWalkYields([c2], [c3.id], paths=['b'])
  210. self.assertWalkYields([c3, c1], [c3.id], paths=['x'])
  211. def test_paths_max_entries(self):
  212. blob_a = make_object(Blob, data='a')
  213. blob_b = make_object(Blob, data='b')
  214. c1, c2 = self.make_linear_commits(
  215. 2, trees={1: [('a', blob_a)],
  216. 2: [('a', blob_a), ('b', blob_b)]})
  217. self.assertWalkYields([c2], [c2.id], paths=['b'], max_entries=1)
  218. self.assertWalkYields([c1], [c1.id], paths=['a'], max_entries=1)
  219. def test_paths_merge(self):
  220. blob_a1 = make_object(Blob, data='a1')
  221. blob_a2 = make_object(Blob, data='a2')
  222. blob_a3 = make_object(Blob, data='a3')
  223. x1, y2, m3, m4 = self.make_commits(
  224. [[1], [2], [3, 1, 2], [4, 1, 2]],
  225. trees={1: [('a', blob_a1)],
  226. 2: [('a', blob_a2)],
  227. 3: [('a', blob_a3)],
  228. 4: [('a', blob_a1)]}) # Non-conflicting
  229. self.assertWalkYields([m3, y2, x1], [m3.id], paths=['a'])
  230. self.assertWalkYields([y2, x1], [m4.id], paths=['a'])
  231. def test_changes_with_renames(self):
  232. blob = make_object(Blob, data='blob')
  233. c1, c2 = self.make_linear_commits(
  234. 2, trees={1: [('a', blob)], 2: [('b', blob)]})
  235. entry_a = ('a', F, blob.id)
  236. entry_b = ('b', F, blob.id)
  237. changes_without_renames = [TreeChange.delete(entry_a),
  238. TreeChange.add(entry_b)]
  239. changes_with_renames = [TreeChange(CHANGE_RENAME, entry_a, entry_b)]
  240. self.assertWalkYields(
  241. [TestWalkEntry(c2, changes_without_renames)], [c2.id], max_entries=1)
  242. detector = RenameDetector(self.store)
  243. self.assertWalkYields(
  244. [TestWalkEntry(c2, changes_with_renames)], [c2.id], max_entries=1,
  245. rename_detector=detector)
  246. def test_follow_rename(self):
  247. blob = make_object(Blob, data='blob')
  248. names = ['a', 'a', 'b', 'b', 'c', 'c']
  249. trees = dict((i + 1, [(n, blob, F)]) for i, n in enumerate(names))
  250. c1, c2, c3, c4, c5, c6 = self.make_linear_commits(6, trees=trees)
  251. self.assertWalkYields([c5], [c6.id], paths=['c'])
  252. e = lambda n: (n, F, blob.id)
  253. self.assertWalkYields(
  254. [TestWalkEntry(c5, [TreeChange(CHANGE_RENAME, e('b'), e('c'))]),
  255. TestWalkEntry(c3, [TreeChange(CHANGE_RENAME, e('a'), e('b'))]),
  256. TestWalkEntry(c1, [TreeChange.add(e('a'))])],
  257. [c6.id], paths=['c'], follow=True)
  258. def test_follow_rename_remove_path(self):
  259. blob = make_object(Blob, data='blob')
  260. _, _, _, c4, c5, c6 = self.make_linear_commits(
  261. 6, trees={1: [('a', blob), ('c', blob)],
  262. 2: [],
  263. 3: [],
  264. 4: [('b', blob)],
  265. 5: [('a', blob)],
  266. 6: [('c', blob)]})
  267. e = lambda n: (n, F, blob.id)
  268. # Once the path changes to b, we aren't interested in a or c anymore.
  269. self.assertWalkYields(
  270. [TestWalkEntry(c6, [TreeChange(CHANGE_RENAME, e('a'), e('c'))]),
  271. TestWalkEntry(c5, [TreeChange(CHANGE_RENAME, e('b'), e('a'))]),
  272. TestWalkEntry(c4, [TreeChange.add(e('b'))])],
  273. [c6.id], paths=['c'], follow=True)
  274. def test_since(self):
  275. c1, c2, c3 = self.make_linear_commits(3)
  276. self.assertWalkYields([c3, c2, c1], [c3.id], since=-1)
  277. self.assertWalkYields([c3, c2, c1], [c3.id], since=0)
  278. self.assertWalkYields([c3, c2], [c3.id], since=1)
  279. self.assertWalkYields([c3, c2], [c3.id], since=99)
  280. self.assertWalkYields([c3, c2], [c3.id], since=100)
  281. self.assertWalkYields([c3], [c3.id], since=101)
  282. self.assertWalkYields([c3], [c3.id], since=199)
  283. self.assertWalkYields([c3], [c3.id], since=200)
  284. self.assertWalkYields([], [c3.id], since=201)
  285. self.assertWalkYields([], [c3.id], since=300)
  286. def test_until(self):
  287. c1, c2, c3 = self.make_linear_commits(3)
  288. self.assertWalkYields([], [c3.id], until=-1)
  289. self.assertWalkYields([c1], [c3.id], until=0)
  290. self.assertWalkYields([c1], [c3.id], until=1)
  291. self.assertWalkYields([c1], [c3.id], until=99)
  292. self.assertWalkYields([c2, c1], [c3.id], until=100)
  293. self.assertWalkYields([c2, c1], [c3.id], until=101)
  294. self.assertWalkYields([c2, c1], [c3.id], until=199)
  295. self.assertWalkYields([c3, c2, c1], [c3.id], until=200)
  296. self.assertWalkYields([c3, c2, c1], [c3.id], until=201)
  297. self.assertWalkYields([c3, c2, c1], [c3.id], until=300)
  298. def test_since_until(self):
  299. c1, c2, c3 = self.make_linear_commits(3)
  300. self.assertWalkYields([], [c3.id], since=100, until=99)
  301. self.assertWalkYields([c3, c2, c1], [c3.id], since=-1, until=201)
  302. self.assertWalkYields([c2], [c3.id], since=100, until=100)
  303. self.assertWalkYields([c2], [c3.id], since=50, until=150)
  304. def test_since_over_scan(self):
  305. commits = self.make_linear_commits(
  306. 11, times=[9, 0, 1, 2, 3, 4, 5, 8, 6, 7, 9])
  307. c8, _, c10, c11 = commits[-4:]
  308. del self.store[commits[0].id]
  309. # c9 is older than we want to walk, but is out of order with its parent,
  310. # so we need to walk past it to get to c8.
  311. # c1 would also match, but we've deleted it, and it should get pruned
  312. # even with over-scanning.
  313. self.assertWalkYields([c11, c10, c8], [c11.id], since=7)
  314. def assertTopoOrderEqual(self, expected_commits, commits):
  315. entries = [TestWalkEntry(c, None) for c in commits]
  316. actual_ids = [e.commit.id for e in list(_topo_reorder(entries))]
  317. self.assertEqual([c.id for c in expected_commits], actual_ids)
  318. def test_topo_reorder_linear(self):
  319. commits = self.make_linear_commits(5)
  320. commits.reverse()
  321. for perm in permutations(commits):
  322. self.assertTopoOrderEqual(commits, perm)
  323. def test_topo_reorder_multiple_parents(self):
  324. c1, c2, c3 = self.make_commits([[1], [2], [3, 1, 2]])
  325. # Already sorted, so totally FIFO.
  326. self.assertTopoOrderEqual([c3, c2, c1], [c3, c2, c1])
  327. self.assertTopoOrderEqual([c3, c1, c2], [c3, c1, c2])
  328. # c3 causes one parent to be yielded.
  329. self.assertTopoOrderEqual([c3, c2, c1], [c2, c3, c1])
  330. self.assertTopoOrderEqual([c3, c1, c2], [c1, c3, c2])
  331. # c3 causes both parents to be yielded.
  332. self.assertTopoOrderEqual([c3, c2, c1], [c1, c2, c3])
  333. self.assertTopoOrderEqual([c3, c2, c1], [c2, c1, c3])
  334. def test_topo_reorder_multiple_children(self):
  335. c1, c2, c3 = self.make_commits([[1], [2, 1], [3, 1]])
  336. # c2 and c3 are FIFO but c1 moves to the end.
  337. self.assertTopoOrderEqual([c3, c2, c1], [c3, c2, c1])
  338. self.assertTopoOrderEqual([c3, c2, c1], [c3, c1, c2])
  339. self.assertTopoOrderEqual([c3, c2, c1], [c1, c3, c2])
  340. self.assertTopoOrderEqual([c2, c3, c1], [c2, c3, c1])
  341. self.assertTopoOrderEqual([c2, c3, c1], [c2, c1, c3])
  342. self.assertTopoOrderEqual([c2, c3, c1], [c1, c2, c3])
  343. def test_out_of_order_children(self):
  344. c1, c2, c3, c4, c5 = self.make_commits(
  345. [[1], [2, 1], [3, 2], [4, 1], [5, 3, 4]],
  346. times=[2, 1, 3, 4, 5])
  347. self.assertWalkYields([c5, c4, c3, c1, c2], [c5.id])
  348. self.assertWalkYields([c5, c4, c3, c2, c1], [c5.id], order=ORDER_TOPO)
  349. def test_out_of_order_with_exclude(self):
  350. # Create the following graph:
  351. # c1-------x2---m6
  352. # \ /
  353. # \-y3--y4-/--y5
  354. # Due to skew, y5 is the oldest commit.
  355. c1, x2, y3, y4, y5, m6 = self.make_commits(
  356. [[1], [2, 1], [3, 1], [4, 3], [5, 4], [6, 2, 4]],
  357. times=[2, 3, 4, 5, 1, 6])
  358. self.assertWalkYields([m6, y4, y3, x2, c1], [m6.id])
  359. # Ensure that c1..y4 get excluded even though they're popped from the
  360. # priority queue long before y5.
  361. self.assertWalkYields([m6, x2], [m6.id], exclude=[y5.id])
  362. def test_empty_walk(self):
  363. c1, c2, c3 = self.make_linear_commits(3)
  364. self.assertWalkYields([], [c3.id], exclude=[c3.id])