2
0

test_walk.py 16 KB

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