test_repository.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. # test_repository.py -- tests for repository.py
  2. # Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
  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. # of the License or (at your option) any later version of
  8. # the License.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. # MA 02110-1301, USA.
  19. """Tests for the repository."""
  20. import os
  21. import stat
  22. import shutil
  23. import sys
  24. import tempfile
  25. import warnings
  26. from dulwich import errors
  27. from dulwich.object_store import (
  28. tree_lookup_path,
  29. )
  30. from dulwich import objects
  31. from dulwich.config import Config
  32. from dulwich.repo import (
  33. Repo,
  34. MemoryRepo,
  35. )
  36. from dulwich.tests import (
  37. TestCase,
  38. )
  39. from dulwich.tests.utils import (
  40. open_repo,
  41. tear_down_repo,
  42. setup_warning_catcher,
  43. )
  44. missing_sha = b'b91fa4d900e17e99b433218e988c4eb4a3e9a097'
  45. class CreateRepositoryTests(TestCase):
  46. def assertFileContentsEqual(self, expected, repo, path):
  47. f = repo.get_named_file(path)
  48. if not f:
  49. self.assertEqual(expected, None)
  50. else:
  51. with f:
  52. self.assertEqual(expected, f.read())
  53. def _check_repo_contents(self, repo, expect_bare):
  54. self.assertEqual(expect_bare, repo.bare)
  55. self.assertFileContentsEqual(b'Unnamed repository', repo, 'description')
  56. self.assertFileContentsEqual(b'', repo, os.path.join('info', 'exclude'))
  57. self.assertFileContentsEqual(None, repo, 'nonexistent file')
  58. barestr = b'bare = ' + str(expect_bare).lower().encode('ascii')
  59. config_text = repo.get_named_file('config').read()
  60. self.assertTrue(barestr in config_text, "%r" % config_text)
  61. def test_create_disk_bare(self):
  62. tmp_dir = tempfile.mkdtemp()
  63. self.addCleanup(shutil.rmtree, tmp_dir)
  64. repo = Repo.init_bare(tmp_dir)
  65. self.assertEqual(tmp_dir, repo._controldir)
  66. self._check_repo_contents(repo, True)
  67. def test_create_disk_non_bare(self):
  68. tmp_dir = tempfile.mkdtemp()
  69. self.addCleanup(shutil.rmtree, tmp_dir)
  70. repo = Repo.init(tmp_dir)
  71. self.assertEqual(os.path.join(tmp_dir, '.git'), repo._controldir)
  72. self._check_repo_contents(repo, False)
  73. def test_create_memory(self):
  74. repo = MemoryRepo.init_bare([], {})
  75. self._check_repo_contents(repo, True)
  76. class RepositoryTests(TestCase):
  77. def setUp(self):
  78. super(RepositoryTests, self).setUp()
  79. self._repo = None
  80. def tearDown(self):
  81. if self._repo is not None:
  82. tear_down_repo(self._repo)
  83. super(RepositoryTests, self).tearDown()
  84. def test_simple_props(self):
  85. r = self._repo = open_repo('a.git')
  86. self.assertEqual(r.controldir(), r.path)
  87. def test_setitem(self):
  88. r = self._repo = open_repo('a.git')
  89. r[b"refs/tags/foo"] = b'a90fa2d900a17e99b433217e988c4eb4a2e9a097'
  90. self.assertEqual(b'a90fa2d900a17e99b433217e988c4eb4a2e9a097',
  91. r[b"refs/tags/foo"].id)
  92. def test_getitem_unicode(self):
  93. r = self._repo = open_repo('a.git')
  94. test_keys = [
  95. (b'refs/heads/master', True),
  96. (b'a90fa2d900a17e99b433217e988c4eb4a2e9a097', True),
  97. (b'11' * 19 + b'--', False),
  98. ]
  99. for k, contained in test_keys:
  100. self.assertEqual(k in r, contained)
  101. for k, _ in test_keys:
  102. self.assertRaisesRegexp(
  103. TypeError, "'name' must be bytestring, not int",
  104. r.__getitem__, 12
  105. )
  106. def test_delitem(self):
  107. r = self._repo = open_repo('a.git')
  108. del r[b'refs/heads/master']
  109. self.assertRaises(KeyError, lambda: r[b'refs/heads/master'])
  110. del r[b'HEAD']
  111. self.assertRaises(KeyError, lambda: r[b'HEAD'])
  112. self.assertRaises(ValueError, r.__delitem__, b'notrefs/foo')
  113. def test_get_refs(self):
  114. r = self._repo = open_repo('a.git')
  115. self.assertEqual({
  116. b'HEAD': b'a90fa2d900a17e99b433217e988c4eb4a2e9a097',
  117. b'refs/heads/master': b'a90fa2d900a17e99b433217e988c4eb4a2e9a097',
  118. b'refs/tags/mytag': b'28237f4dc30d0d462658d6b937b08a0f0b6ef55a',
  119. b'refs/tags/mytag-packed': b'b0931cadc54336e78a1d980420e3268903b57a50',
  120. }, r.get_refs())
  121. def test_head(self):
  122. r = self._repo = open_repo('a.git')
  123. self.assertEqual(r.head(), b'a90fa2d900a17e99b433217e988c4eb4a2e9a097')
  124. def test_get_object(self):
  125. r = self._repo = open_repo('a.git')
  126. obj = r.get_object(r.head())
  127. self.assertEqual(obj.type_name, b'commit')
  128. def test_get_object_non_existant(self):
  129. r = self._repo = open_repo('a.git')
  130. self.assertRaises(KeyError, r.get_object, missing_sha)
  131. def test_contains_object(self):
  132. r = self._repo = open_repo('a.git')
  133. self.assertTrue(r.head() in r)
  134. def test_contains_ref(self):
  135. r = self._repo = open_repo('a.git')
  136. self.assertTrue(b"HEAD" in r)
  137. def test_get_no_description(self):
  138. r = self._repo = open_repo('a.git')
  139. self.assertIs(None, r.get_description())
  140. def test_get_description(self):
  141. r = self._repo = open_repo('a.git')
  142. with open(os.path.join(r.path, 'description'), 'wb') as f:
  143. f.write(b"Some description")
  144. self.assertEqual(b"Some description", r.get_description())
  145. def test_set_description(self):
  146. r = self._repo = open_repo('a.git')
  147. description = b"Some description"
  148. r.set_description(description)
  149. self.assertEqual(description, r.get_description())
  150. def test_contains_missing(self):
  151. r = self._repo = open_repo('a.git')
  152. self.assertFalse(b"bar" in r)
  153. def test_get_peeled(self):
  154. # unpacked ref
  155. r = self._repo = open_repo('a.git')
  156. tag_sha = b'28237f4dc30d0d462658d6b937b08a0f0b6ef55a'
  157. self.assertNotEqual(r[tag_sha].sha().hexdigest(), r.head())
  158. self.assertEqual(r.get_peeled(b'refs/tags/mytag'), r.head())
  159. # packed ref with cached peeled value
  160. packed_tag_sha = b'b0931cadc54336e78a1d980420e3268903b57a50'
  161. parent_sha = r[r.head()].parents[0]
  162. self.assertNotEqual(r[packed_tag_sha].sha().hexdigest(), parent_sha)
  163. self.assertEqual(r.get_peeled(b'refs/tags/mytag-packed'), parent_sha)
  164. # TODO: add more corner cases to test repo
  165. def test_get_peeled_not_tag(self):
  166. r = self._repo = open_repo('a.git')
  167. self.assertEqual(r.get_peeled(b'HEAD'), r.head())
  168. def test_get_walker(self):
  169. r = self._repo = open_repo('a.git')
  170. # include defaults to [r.head()]
  171. self.assertEqual([e.commit.id for e in r.get_walker()],
  172. [r.head(), b'2a72d929692c41d8554c07f6301757ba18a65d91'])
  173. self.assertEqual(
  174. [e.commit.id for e in r.get_walker([b'2a72d929692c41d8554c07f6301757ba18a65d91'])],
  175. [b'2a72d929692c41d8554c07f6301757ba18a65d91'])
  176. self.assertEqual(
  177. [e.commit.id for e in r.get_walker(b'2a72d929692c41d8554c07f6301757ba18a65d91')],
  178. [b'2a72d929692c41d8554c07f6301757ba18a65d91'])
  179. def test_clone(self):
  180. r = self._repo = open_repo('a.git')
  181. tmp_dir = tempfile.mkdtemp()
  182. self.addCleanup(shutil.rmtree, tmp_dir)
  183. t = r.clone(tmp_dir, mkdir=False)
  184. self.assertEqual({
  185. b'HEAD': b'a90fa2d900a17e99b433217e988c4eb4a2e9a097',
  186. b'refs/remotes/origin/master':
  187. b'a90fa2d900a17e99b433217e988c4eb4a2e9a097',
  188. b'refs/heads/master': b'a90fa2d900a17e99b433217e988c4eb4a2e9a097',
  189. b'refs/tags/mytag': b'28237f4dc30d0d462658d6b937b08a0f0b6ef55a',
  190. b'refs/tags/mytag-packed':
  191. b'b0931cadc54336e78a1d980420e3268903b57a50',
  192. }, t.refs.as_dict())
  193. shas = [e.commit.id for e in r.get_walker()]
  194. self.assertEqual(shas, [t.head(),
  195. b'2a72d929692c41d8554c07f6301757ba18a65d91'])
  196. def test_clone_no_head(self):
  197. temp_dir = tempfile.mkdtemp()
  198. self.addCleanup(shutil.rmtree, temp_dir)
  199. repo_dir = os.path.join(os.path.dirname(__file__), 'data', 'repos')
  200. dest_dir = os.path.join(temp_dir, 'a.git')
  201. shutil.copytree(os.path.join(repo_dir, 'a.git'),
  202. dest_dir, symlinks=True)
  203. r = Repo(dest_dir)
  204. del r.refs[b"refs/heads/master"]
  205. del r.refs[b"HEAD"]
  206. t = r.clone(os.path.join(temp_dir, 'b.git'), mkdir=True)
  207. self.assertEqual({
  208. b'refs/tags/mytag': b'28237f4dc30d0d462658d6b937b08a0f0b6ef55a',
  209. b'refs/tags/mytag-packed':
  210. b'b0931cadc54336e78a1d980420e3268903b57a50',
  211. }, t.refs.as_dict())
  212. def test_clone_empty(self):
  213. """Test clone() doesn't crash if HEAD points to a non-existing ref.
  214. This simulates cloning server-side bare repository either when it is
  215. still empty or if user renames master branch and pushes private repo
  216. to the server.
  217. Non-bare repo HEAD always points to an existing ref.
  218. """
  219. r = self._repo = open_repo('empty.git')
  220. tmp_dir = tempfile.mkdtemp()
  221. self.addCleanup(shutil.rmtree, tmp_dir)
  222. r.clone(tmp_dir, mkdir=False, bare=True)
  223. def test_merge_history(self):
  224. r = self._repo = open_repo('simple_merge.git')
  225. shas = [e.commit.id for e in r.get_walker()]
  226. self.assertEqual(shas, [b'5dac377bdded4c9aeb8dff595f0faeebcc8498cc',
  227. b'ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd',
  228. b'4cffe90e0a41ad3f5190079d7c8f036bde29cbe6',
  229. b'60dacdc733de308bb77bb76ce0fb0f9b44c9769e',
  230. b'0d89f20333fbb1d2f3a94da77f4981373d8f4310'])
  231. def test_out_of_order_merge(self):
  232. """Test that revision history is ordered by date, not parent order."""
  233. r = self._repo = open_repo('ooo_merge.git')
  234. shas = [e.commit.id for e in r.get_walker()]
  235. self.assertEqual(shas, [b'7601d7f6231db6a57f7bbb79ee52e4d462fd44d1',
  236. b'f507291b64138b875c28e03469025b1ea20bc614',
  237. b'fb5b0425c7ce46959bec94d54b9a157645e114f5',
  238. b'f9e39b120c68182a4ba35349f832d0e4e61f485c'])
  239. def test_get_tags_empty(self):
  240. r = self._repo = open_repo('ooo_merge.git')
  241. self.assertEqual({}, r.refs.as_dict(b'refs/tags'))
  242. def test_get_config(self):
  243. r = self._repo = open_repo('ooo_merge.git')
  244. self.assertIsInstance(r.get_config(), Config)
  245. def test_get_config_stack(self):
  246. r = self._repo = open_repo('ooo_merge.git')
  247. self.assertIsInstance(r.get_config_stack(), Config)
  248. def test_submodule(self):
  249. temp_dir = tempfile.mkdtemp()
  250. repo_dir = os.path.join(os.path.dirname(__file__), 'data', 'repos')
  251. shutil.copytree(os.path.join(repo_dir, 'a.git'),
  252. os.path.join(temp_dir, 'a.git'), symlinks=True)
  253. rel = os.path.relpath(os.path.join(repo_dir, 'submodule'), temp_dir)
  254. os.symlink(os.path.join(rel, 'dotgit'), os.path.join(temp_dir, '.git'))
  255. r = Repo(temp_dir)
  256. self.assertEqual(r.head(), b'a90fa2d900a17e99b433217e988c4eb4a2e9a097')
  257. def test_common_revisions(self):
  258. """
  259. This test demonstrates that ``find_common_revisions()`` actually returns
  260. common heads, not revisions; dulwich already uses
  261. ``find_common_revisions()`` in such a manner (see
  262. ``Repo.fetch_objects()``).
  263. """
  264. expected_shas = set([b'60dacdc733de308bb77bb76ce0fb0f9b44c9769e'])
  265. # Source for objects.
  266. r_base = open_repo('simple_merge.git')
  267. # Re-create each-side of the merge in simple_merge.git.
  268. #
  269. # Since the trees and blobs are missing, the repository created is
  270. # corrupted, but we're only checking for commits for the purpose of this
  271. # test, so it's immaterial.
  272. r1_dir = tempfile.mkdtemp()
  273. r1_commits = [b'ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd', # HEAD
  274. b'60dacdc733de308bb77bb76ce0fb0f9b44c9769e',
  275. b'0d89f20333fbb1d2f3a94da77f4981373d8f4310']
  276. r2_dir = tempfile.mkdtemp()
  277. r2_commits = [b'4cffe90e0a41ad3f5190079d7c8f036bde29cbe6', # HEAD
  278. b'60dacdc733de308bb77bb76ce0fb0f9b44c9769e',
  279. b'0d89f20333fbb1d2f3a94da77f4981373d8f4310']
  280. try:
  281. r1 = Repo.init_bare(r1_dir)
  282. for c in r1_commits:
  283. r1.object_store.add_object(r_base.get_object(c))
  284. r1.refs[b'HEAD'] = r1_commits[0]
  285. r2 = Repo.init_bare(r2_dir)
  286. for c in r2_commits:
  287. r2.object_store.add_object(r_base.get_object(c))
  288. r2.refs[b'HEAD'] = r2_commits[0]
  289. # Finally, the 'real' testing!
  290. shas = r2.object_store.find_common_revisions(r1.get_graph_walker())
  291. self.assertEqual(set(shas), expected_shas)
  292. shas = r1.object_store.find_common_revisions(r2.get_graph_walker())
  293. self.assertEqual(set(shas), expected_shas)
  294. finally:
  295. shutil.rmtree(r1_dir)
  296. shutil.rmtree(r2_dir)
  297. def test_shell_hook_pre_commit(self):
  298. if os.name != 'posix':
  299. self.skipTest('shell hook tests requires POSIX shell')
  300. pre_commit_fail = b"""#!/bin/sh
  301. exit 1
  302. """
  303. pre_commit_success = b"""#!/bin/sh
  304. exit 0
  305. """
  306. repo_dir = os.path.join(tempfile.mkdtemp())
  307. r = Repo.init(repo_dir)
  308. self.addCleanup(shutil.rmtree, repo_dir)
  309. pre_commit = os.path.join(r.controldir(), 'hooks', 'pre-commit')
  310. with open(pre_commit, 'wb') as f:
  311. f.write(pre_commit_fail)
  312. os.chmod(pre_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  313. self.assertRaises(errors.CommitError, r.do_commit, 'failed commit',
  314. committer='Test Committer <test@nodomain.com>',
  315. author='Test Author <test@nodomain.com>',
  316. commit_timestamp=12345, commit_timezone=0,
  317. author_timestamp=12345, author_timezone=0)
  318. with open(pre_commit, 'wb') as f:
  319. f.write(pre_commit_success)
  320. os.chmod(pre_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  321. commit_sha = r.do_commit(
  322. b'empty commit',
  323. committer=b'Test Committer <test@nodomain.com>',
  324. author=b'Test Author <test@nodomain.com>',
  325. commit_timestamp=12395, commit_timezone=0,
  326. author_timestamp=12395, author_timezone=0)
  327. self.assertEqual([], r[commit_sha].parents)
  328. def test_shell_hook_commit_msg(self):
  329. if os.name != 'posix':
  330. self.skipTest('shell hook tests requires POSIX shell')
  331. commit_msg_fail = b"""#!/bin/sh
  332. exit 1
  333. """
  334. commit_msg_success = b"""#!/bin/sh
  335. exit 0
  336. """
  337. repo_dir = os.path.join(tempfile.mkdtemp())
  338. r = Repo.init(repo_dir)
  339. self.addCleanup(shutil.rmtree, repo_dir)
  340. commit_msg = os.path.join(r.controldir(), 'hooks', 'commit-msg')
  341. with open(commit_msg, 'wb') as f:
  342. f.write(commit_msg_fail)
  343. os.chmod(commit_msg, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  344. self.assertRaises(errors.CommitError, r.do_commit, b'failed commit',
  345. committer=b'Test Committer <test@nodomain.com>',
  346. author=b'Test Author <test@nodomain.com>',
  347. commit_timestamp=12345, commit_timezone=0,
  348. author_timestamp=12345, author_timezone=0)
  349. with open(commit_msg, 'wb') as f:
  350. f.write(commit_msg_success)
  351. os.chmod(commit_msg, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  352. commit_sha = r.do_commit(
  353. b'empty commit',
  354. committer=b'Test Committer <test@nodomain.com>',
  355. author=b'Test Author <test@nodomain.com>',
  356. commit_timestamp=12395, commit_timezone=0,
  357. author_timestamp=12395, author_timezone=0)
  358. self.assertEqual([], r[commit_sha].parents)
  359. def test_shell_hook_post_commit(self):
  360. if os.name != 'posix':
  361. self.skipTest('shell hook tests requires POSIX shell')
  362. repo_dir = os.path.join(tempfile.mkdtemp())
  363. r = Repo.init(repo_dir)
  364. self.addCleanup(shutil.rmtree, repo_dir)
  365. (fd, path) = tempfile.mkstemp(dir=repo_dir)
  366. post_commit_msg = b"""#!/bin/sh
  367. rm """ + path.encode(sys.getfilesystemencoding()) + b"""
  368. """
  369. root_sha = r.do_commit(
  370. b'empty commit',
  371. committer=b'Test Committer <test@nodomain.com>',
  372. author=b'Test Author <test@nodomain.com>',
  373. commit_timestamp=12345, commit_timezone=0,
  374. author_timestamp=12345, author_timezone=0)
  375. self.assertEqual([], r[root_sha].parents)
  376. post_commit = os.path.join(r.controldir(), 'hooks', 'post-commit')
  377. with open(post_commit, 'wb') as f:
  378. f.write(post_commit_msg)
  379. os.chmod(post_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  380. commit_sha = r.do_commit(
  381. b'empty commit',
  382. committer=b'Test Committer <test@nodomain.com>',
  383. author=b'Test Author <test@nodomain.com>',
  384. commit_timestamp=12345, commit_timezone=0,
  385. author_timestamp=12345, author_timezone=0)
  386. self.assertEqual([root_sha], r[commit_sha].parents)
  387. self.assertFalse(os.path.exists(path))
  388. post_commit_msg_fail = b"""#!/bin/sh
  389. exit 1
  390. """
  391. with open(post_commit, 'wb') as f:
  392. f.write(post_commit_msg_fail)
  393. os.chmod(post_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  394. warnings.simplefilter("always", UserWarning)
  395. self.addCleanup(warnings.resetwarnings)
  396. warnings_list, restore_warnings = setup_warning_catcher()
  397. self.addCleanup(restore_warnings)
  398. commit_sha2 = r.do_commit(
  399. b'empty commit',
  400. committer=b'Test Committer <test@nodomain.com>',
  401. author=b'Test Author <test@nodomain.com>',
  402. commit_timestamp=12345, commit_timezone=0,
  403. author_timestamp=12345, author_timezone=0)
  404. self.assertEqual(len(warnings_list), 1)
  405. self.assertIsInstance(warnings_list[-1], UserWarning)
  406. self.assertTrue("post-commit hook failed: " in str(warnings_list[-1]))
  407. self.assertEqual([commit_sha], r[commit_sha2].parents)
  408. class BuildRepoTests(TestCase):
  409. """Tests that build on-disk repos from scratch.
  410. Repos live in a temp dir and are torn down after each test. They start with
  411. a single commit in master having single file named 'a'.
  412. """
  413. def setUp(self):
  414. super(BuildRepoTests, self).setUp()
  415. self._repo_dir = os.path.join(tempfile.mkdtemp(), 'test')
  416. os.makedirs(self._repo_dir)
  417. r = self._repo = Repo.init(self._repo_dir)
  418. self.assertFalse(r.bare)
  419. self.assertEqual(b'ref: refs/heads/master', r.refs.read_ref(b'HEAD'))
  420. self.assertRaises(KeyError, lambda: r.refs[b'refs/heads/master'])
  421. with open(os.path.join(r.path, 'a'), 'wb') as f:
  422. f.write(b'file contents')
  423. r.stage(['a'])
  424. commit_sha = r.do_commit(b'msg',
  425. committer=b'Test Committer <test@nodomain.com>',
  426. author=b'Test Author <test@nodomain.com>',
  427. commit_timestamp=12345, commit_timezone=0,
  428. author_timestamp=12345, author_timezone=0)
  429. self.assertEqual([], r[commit_sha].parents)
  430. self._root_commit = commit_sha
  431. def tearDown(self):
  432. tear_down_repo(self._repo)
  433. super(BuildRepoTests, self).tearDown()
  434. def test_build_repo(self):
  435. r = self._repo
  436. self.assertEqual(b'ref: refs/heads/master', r.refs.read_ref(b'HEAD'))
  437. self.assertEqual(self._root_commit, r.refs[b'refs/heads/master'])
  438. expected_blob = objects.Blob.from_string(b'file contents')
  439. self.assertEqual(expected_blob.data, r[expected_blob.id].data)
  440. actual_commit = r[self._root_commit]
  441. self.assertEqual(b'msg', actual_commit.message)
  442. def test_commit_modified(self):
  443. r = self._repo
  444. with open(os.path.join(r.path, 'a'), 'wb') as f:
  445. f.write(b'new contents')
  446. os.symlink('a', os.path.join(self._repo_dir, 'b'))
  447. r.stage(['a', 'b'])
  448. commit_sha = r.do_commit(b'modified a',
  449. committer=b'Test Committer <test@nodomain.com>',
  450. author=b'Test Author <test@nodomain.com>',
  451. commit_timestamp=12395, commit_timezone=0,
  452. author_timestamp=12395, author_timezone=0)
  453. self.assertEqual([self._root_commit], r[commit_sha].parents)
  454. a_mode, a_id = tree_lookup_path(r.get_object, r[commit_sha].tree, b'a')
  455. self.assertEqual(stat.S_IFREG | 0o644, a_mode)
  456. self.assertEqual(b'new contents', r[a_id].data)
  457. b_mode, b_id = tree_lookup_path(r.get_object, r[commit_sha].tree, b'b')
  458. self.assertTrue(stat.S_ISLNK(b_mode))
  459. self.assertEqual(b'a', r[b_id].data)
  460. def test_commit_deleted(self):
  461. r = self._repo
  462. os.remove(os.path.join(r.path, 'a'))
  463. r.stage(['a'])
  464. commit_sha = r.do_commit(b'deleted a',
  465. committer=b'Test Committer <test@nodomain.com>',
  466. author=b'Test Author <test@nodomain.com>',
  467. commit_timestamp=12395, commit_timezone=0,
  468. author_timestamp=12395, author_timezone=0)
  469. self.assertEqual([self._root_commit], r[commit_sha].parents)
  470. self.assertEqual([], list(r.open_index()))
  471. tree = r[r[commit_sha].tree]
  472. self.assertEqual([], list(tree.iteritems()))
  473. def test_commit_encoding(self):
  474. r = self._repo
  475. commit_sha = r.do_commit(b'commit with strange character \xee',
  476. committer=b'Test Committer <test@nodomain.com>',
  477. author=b'Test Author <test@nodomain.com>',
  478. commit_timestamp=12395, commit_timezone=0,
  479. author_timestamp=12395, author_timezone=0,
  480. encoding=b"iso8859-1")
  481. self.assertEqual(b"iso8859-1", r[commit_sha].encoding)
  482. def test_commit_config_identity(self):
  483. # commit falls back to the users' identity if it wasn't specified
  484. r = self._repo
  485. c = r.get_config()
  486. c.set((b"user", ), b"name", b"Jelmer")
  487. c.set((b"user", ), b"email", b"jelmer@apache.org")
  488. c.write_to_path()
  489. commit_sha = r.do_commit(b'message')
  490. self.assertEqual(
  491. b"Jelmer <jelmer@apache.org>",
  492. r[commit_sha].author)
  493. self.assertEqual(
  494. b"Jelmer <jelmer@apache.org>",
  495. r[commit_sha].committer)
  496. def test_commit_config_identity_in_memoryrepo(self):
  497. # commit falls back to the users' identity if it wasn't specified
  498. r = MemoryRepo.init_bare([], {})
  499. c = r.get_config()
  500. c.set((b"user", ), b"name", b"Jelmer")
  501. c.set((b"user", ), b"email", b"jelmer@apache.org")
  502. commit_sha = r.do_commit(b'message', tree=objects.Tree().id)
  503. self.assertEqual(
  504. b"Jelmer <jelmer@apache.org>",
  505. r[commit_sha].author)
  506. self.assertEqual(
  507. b"Jelmer <jelmer@apache.org>",
  508. r[commit_sha].committer)
  509. def test_commit_fail_ref(self):
  510. r = self._repo
  511. def set_if_equals(name, old_ref, new_ref):
  512. return False
  513. r.refs.set_if_equals = set_if_equals
  514. def add_if_new(name, new_ref):
  515. self.fail('Unexpected call to add_if_new')
  516. r.refs.add_if_new = add_if_new
  517. old_shas = set(r.object_store)
  518. self.assertRaises(errors.CommitError, r.do_commit, b'failed commit',
  519. committer=b'Test Committer <test@nodomain.com>',
  520. author=b'Test Author <test@nodomain.com>',
  521. commit_timestamp=12345, commit_timezone=0,
  522. author_timestamp=12345, author_timezone=0)
  523. new_shas = set(r.object_store) - old_shas
  524. self.assertEqual(1, len(new_shas))
  525. # Check that the new commit (now garbage) was added.
  526. new_commit = r[new_shas.pop()]
  527. self.assertEqual(r[self._root_commit].tree, new_commit.tree)
  528. self.assertEqual(b'failed commit', new_commit.message)
  529. def test_commit_branch(self):
  530. r = self._repo
  531. commit_sha = r.do_commit(b'commit to branch',
  532. committer=b'Test Committer <test@nodomain.com>',
  533. author=b'Test Author <test@nodomain.com>',
  534. commit_timestamp=12395, commit_timezone=0,
  535. author_timestamp=12395, author_timezone=0,
  536. ref=b"refs/heads/new_branch")
  537. self.assertEqual(self._root_commit, r[b"HEAD"].id)
  538. self.assertEqual(commit_sha, r[b"refs/heads/new_branch"].id)
  539. self.assertEqual([], r[commit_sha].parents)
  540. self.assertTrue(b"refs/heads/new_branch" in r)
  541. new_branch_head = commit_sha
  542. commit_sha = r.do_commit(b'commit to branch 2',
  543. committer=b'Test Committer <test@nodomain.com>',
  544. author=b'Test Author <test@nodomain.com>',
  545. commit_timestamp=12395, commit_timezone=0,
  546. author_timestamp=12395, author_timezone=0,
  547. ref=b"refs/heads/new_branch")
  548. self.assertEqual(self._root_commit, r[b"HEAD"].id)
  549. self.assertEqual(commit_sha, r[b"refs/heads/new_branch"].id)
  550. self.assertEqual([new_branch_head], r[commit_sha].parents)
  551. def test_commit_merge_heads(self):
  552. r = self._repo
  553. merge_1 = r.do_commit(b'commit to branch 2',
  554. committer=b'Test Committer <test@nodomain.com>',
  555. author=b'Test Author <test@nodomain.com>',
  556. commit_timestamp=12395, commit_timezone=0,
  557. author_timestamp=12395, author_timezone=0,
  558. ref=b"refs/heads/new_branch")
  559. commit_sha = r.do_commit(b'commit with merge',
  560. committer=b'Test Committer <test@nodomain.com>',
  561. author=b'Test Author <test@nodomain.com>',
  562. commit_timestamp=12395, commit_timezone=0,
  563. author_timestamp=12395, author_timezone=0,
  564. merge_heads=[merge_1])
  565. self.assertEqual(
  566. [self._root_commit, merge_1],
  567. r[commit_sha].parents)
  568. def test_commit_dangling_commit(self):
  569. r = self._repo
  570. old_shas = set(r.object_store)
  571. old_refs = r.get_refs()
  572. commit_sha = r.do_commit(b'commit with no ref',
  573. committer=b'Test Committer <test@nodomain.com>',
  574. author=b'Test Author <test@nodomain.com>',
  575. commit_timestamp=12395, commit_timezone=0,
  576. author_timestamp=12395, author_timezone=0,
  577. ref=None)
  578. new_shas = set(r.object_store) - old_shas
  579. # New sha is added, but no new refs
  580. self.assertEqual(1, len(new_shas))
  581. new_commit = r[new_shas.pop()]
  582. self.assertEqual(r[self._root_commit].tree, new_commit.tree)
  583. self.assertEqual([], r[commit_sha].parents)
  584. self.assertEqual(old_refs, r.get_refs())
  585. def test_commit_dangling_commit_with_parents(self):
  586. r = self._repo
  587. old_shas = set(r.object_store)
  588. old_refs = r.get_refs()
  589. commit_sha = r.do_commit(b'commit with no ref',
  590. committer=b'Test Committer <test@nodomain.com>',
  591. author=b'Test Author <test@nodomain.com>',
  592. commit_timestamp=12395, commit_timezone=0,
  593. author_timestamp=12395, author_timezone=0,
  594. ref=None, merge_heads=[self._root_commit])
  595. new_shas = set(r.object_store) - old_shas
  596. # New sha is added, but no new refs
  597. self.assertEqual(1, len(new_shas))
  598. new_commit = r[new_shas.pop()]
  599. self.assertEqual(r[self._root_commit].tree, new_commit.tree)
  600. self.assertEqual([self._root_commit], r[commit_sha].parents)
  601. self.assertEqual(old_refs, r.get_refs())
  602. def test_stage_deleted(self):
  603. r = self._repo
  604. os.remove(os.path.join(r.path, 'a'))
  605. r.stage(['a'])
  606. r.stage(['a']) # double-stage a deleted path