test_repository.py 29 KB

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