test_repository.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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_notfound_unicode(self):
  94. r = self._repo = open_repo('a.git')
  95. # In the future, this might raise a TypeError since we don't
  96. # handle unicode strings properly (what encoding?) for refs.
  97. self.assertRaises(KeyError, r.__getitem__, u"11" * 19 + "--")
  98. def test_delitem(self):
  99. r = self._repo = open_repo('a.git')
  100. del r['refs/heads/master']
  101. self.assertRaises(KeyError, lambda: r['refs/heads/master'])
  102. del r['HEAD']
  103. self.assertRaises(KeyError, lambda: r['HEAD'])
  104. self.assertRaises(ValueError, r.__delitem__, 'notrefs/foo')
  105. def test_get_refs(self):
  106. r = self._repo = open_repo('a.git')
  107. self.assertEqual({
  108. 'HEAD': 'a90fa2d900a17e99b433217e988c4eb4a2e9a097',
  109. 'refs/heads/master': 'a90fa2d900a17e99b433217e988c4eb4a2e9a097',
  110. 'refs/tags/mytag': '28237f4dc30d0d462658d6b937b08a0f0b6ef55a',
  111. 'refs/tags/mytag-packed': 'b0931cadc54336e78a1d980420e3268903b57a50',
  112. }, r.get_refs())
  113. def test_head(self):
  114. r = self._repo = open_repo('a.git')
  115. self.assertEqual(r.head(), 'a90fa2d900a17e99b433217e988c4eb4a2e9a097')
  116. def test_get_object(self):
  117. r = self._repo = open_repo('a.git')
  118. obj = r.get_object(r.head())
  119. self.assertEqual(obj.type_name, 'commit')
  120. def test_get_object_non_existant(self):
  121. r = self._repo = open_repo('a.git')
  122. self.assertRaises(KeyError, r.get_object, missing_sha)
  123. def test_contains_object(self):
  124. r = self._repo = open_repo('a.git')
  125. self.assertTrue(r.head() in r)
  126. def test_contains_ref(self):
  127. r = self._repo = open_repo('a.git')
  128. self.assertTrue("HEAD" in r)
  129. def test_get_no_description(self):
  130. r = self._repo = open_repo('a.git')
  131. self.assertIs(None, r.get_description())
  132. def test_get_description(self):
  133. r = self._repo = open_repo('a.git')
  134. f = open(os.path.join(r.path, 'description'), 'w')
  135. try:
  136. f.write("Some description")
  137. finally:
  138. f.close()
  139. self.assertEquals("Some description", r.get_description())
  140. def test_set_description(self):
  141. r = self._repo = open_repo('a.git')
  142. description = "Some description"
  143. r.set_description(description)
  144. self.assertEquals(description, r.get_description())
  145. def test_contains_missing(self):
  146. r = self._repo = open_repo('a.git')
  147. self.assertFalse("bar" in r)
  148. def test_get_peeled(self):
  149. # unpacked ref
  150. r = self._repo = open_repo('a.git')
  151. tag_sha = '28237f4dc30d0d462658d6b937b08a0f0b6ef55a'
  152. self.assertNotEqual(r[tag_sha].sha().hexdigest(), r.head())
  153. self.assertEqual(r.get_peeled('refs/tags/mytag'), r.head())
  154. # packed ref with cached peeled value
  155. packed_tag_sha = 'b0931cadc54336e78a1d980420e3268903b57a50'
  156. parent_sha = r[r.head()].parents[0]
  157. self.assertNotEqual(r[packed_tag_sha].sha().hexdigest(), parent_sha)
  158. self.assertEqual(r.get_peeled('refs/tags/mytag-packed'), parent_sha)
  159. # TODO: add more corner cases to test repo
  160. def test_get_peeled_not_tag(self):
  161. r = self._repo = open_repo('a.git')
  162. self.assertEqual(r.get_peeled('HEAD'), r.head())
  163. def test_get_walker(self):
  164. r = self._repo = open_repo('a.git')
  165. # include defaults to [r.head()]
  166. self.assertEqual([e.commit.id for e in r.get_walker()],
  167. [r.head(), '2a72d929692c41d8554c07f6301757ba18a65d91'])
  168. self.assertEqual(
  169. [e.commit.id for e in r.get_walker(['2a72d929692c41d8554c07f6301757ba18a65d91'])],
  170. ['2a72d929692c41d8554c07f6301757ba18a65d91'])
  171. self.assertEqual(
  172. [e.commit.id for e in r.get_walker('2a72d929692c41d8554c07f6301757ba18a65d91')],
  173. ['2a72d929692c41d8554c07f6301757ba18a65d91'])
  174. def test_clone(self):
  175. r = self._repo = open_repo('a.git')
  176. tmp_dir = tempfile.mkdtemp()
  177. self.addCleanup(shutil.rmtree, tmp_dir)
  178. t = r.clone(tmp_dir, mkdir=False)
  179. self.assertEqual({
  180. 'HEAD': 'a90fa2d900a17e99b433217e988c4eb4a2e9a097',
  181. 'refs/remotes/origin/master':
  182. 'a90fa2d900a17e99b433217e988c4eb4a2e9a097',
  183. 'refs/heads/master': 'a90fa2d900a17e99b433217e988c4eb4a2e9a097',
  184. 'refs/tags/mytag': '28237f4dc30d0d462658d6b937b08a0f0b6ef55a',
  185. 'refs/tags/mytag-packed':
  186. 'b0931cadc54336e78a1d980420e3268903b57a50',
  187. }, t.refs.as_dict())
  188. shas = [e.commit.id for e in r.get_walker()]
  189. self.assertEqual(shas, [t.head(),
  190. '2a72d929692c41d8554c07f6301757ba18a65d91'])
  191. def test_clone_no_head(self):
  192. temp_dir = tempfile.mkdtemp()
  193. self.addCleanup(shutil.rmtree, temp_dir)
  194. repo_dir = os.path.join(os.path.dirname(__file__), 'data', 'repos')
  195. dest_dir = os.path.join(temp_dir, 'a.git')
  196. shutil.copytree(os.path.join(repo_dir, 'a.git'),
  197. dest_dir, symlinks=True)
  198. r = Repo(dest_dir)
  199. del r.refs["refs/heads/master"]
  200. del r.refs["HEAD"]
  201. t = r.clone(os.path.join(temp_dir, 'b.git'), mkdir=True)
  202. self.assertEqual({
  203. 'refs/tags/mytag': '28237f4dc30d0d462658d6b937b08a0f0b6ef55a',
  204. 'refs/tags/mytag-packed':
  205. 'b0931cadc54336e78a1d980420e3268903b57a50',
  206. }, t.refs.as_dict())
  207. def test_clone_empty(self):
  208. """Test clone() doesn't crash if HEAD points to a non-existing ref.
  209. This simulates cloning server-side bare repository either when it is
  210. still empty or if user renames master branch and pushes private repo
  211. to the server.
  212. Non-bare repo HEAD always points to an existing ref.
  213. """
  214. r = self._repo = open_repo('empty.git')
  215. tmp_dir = tempfile.mkdtemp()
  216. self.addCleanup(shutil.rmtree, tmp_dir)
  217. r.clone(tmp_dir, mkdir=False, bare=True)
  218. def test_merge_history(self):
  219. r = self._repo = open_repo('simple_merge.git')
  220. shas = [e.commit.id for e in r.get_walker()]
  221. self.assertEqual(shas, ['5dac377bdded4c9aeb8dff595f0faeebcc8498cc',
  222. 'ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd',
  223. '4cffe90e0a41ad3f5190079d7c8f036bde29cbe6',
  224. '60dacdc733de308bb77bb76ce0fb0f9b44c9769e',
  225. '0d89f20333fbb1d2f3a94da77f4981373d8f4310'])
  226. def test_out_of_order_merge(self):
  227. """Test that revision history is ordered by date, not parent order."""
  228. r = self._repo = open_repo('ooo_merge.git')
  229. shas = [e.commit.id for e in r.get_walker()]
  230. self.assertEqual(shas, ['7601d7f6231db6a57f7bbb79ee52e4d462fd44d1',
  231. 'f507291b64138b875c28e03469025b1ea20bc614',
  232. 'fb5b0425c7ce46959bec94d54b9a157645e114f5',
  233. 'f9e39b120c68182a4ba35349f832d0e4e61f485c'])
  234. def test_get_tags_empty(self):
  235. r = self._repo = open_repo('ooo_merge.git')
  236. self.assertEqual({}, r.refs.as_dict('refs/tags'))
  237. def test_get_config(self):
  238. r = self._repo = open_repo('ooo_merge.git')
  239. self.assertIsInstance(r.get_config(), Config)
  240. def test_get_config_stack(self):
  241. r = self._repo = open_repo('ooo_merge.git')
  242. self.assertIsInstance(r.get_config_stack(), Config)
  243. def test_submodule(self):
  244. temp_dir = tempfile.mkdtemp()
  245. repo_dir = os.path.join(os.path.dirname(__file__), 'data', 'repos')
  246. shutil.copytree(os.path.join(repo_dir, 'a.git'),
  247. os.path.join(temp_dir, 'a.git'), symlinks=True)
  248. rel = os.path.relpath(os.path.join(repo_dir, 'submodule'), temp_dir)
  249. os.symlink(os.path.join(rel, 'dotgit'), os.path.join(temp_dir, '.git'))
  250. r = Repo(temp_dir)
  251. self.assertEqual(r.head(), 'a90fa2d900a17e99b433217e988c4eb4a2e9a097')
  252. def test_common_revisions(self):
  253. """
  254. This test demonstrates that ``find_common_revisions()`` actually returns
  255. common heads, not revisions; dulwich already uses
  256. ``find_common_revisions()`` in such a manner (see
  257. ``Repo.fetch_objects()``).
  258. """
  259. expected_shas = set(['60dacdc733de308bb77bb76ce0fb0f9b44c9769e'])
  260. # Source for objects.
  261. r_base = open_repo('simple_merge.git')
  262. # Re-create each-side of the merge in simple_merge.git.
  263. #
  264. # Since the trees and blobs are missing, the repository created is
  265. # corrupted, but we're only checking for commits for the purpose of this
  266. # test, so it's immaterial.
  267. r1_dir = tempfile.mkdtemp()
  268. r1_commits = ['ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd', # HEAD
  269. '60dacdc733de308bb77bb76ce0fb0f9b44c9769e',
  270. '0d89f20333fbb1d2f3a94da77f4981373d8f4310']
  271. r2_dir = tempfile.mkdtemp()
  272. r2_commits = ['4cffe90e0a41ad3f5190079d7c8f036bde29cbe6', # HEAD
  273. '60dacdc733de308bb77bb76ce0fb0f9b44c9769e',
  274. '0d89f20333fbb1d2f3a94da77f4981373d8f4310']
  275. try:
  276. r1 = Repo.init_bare(r1_dir)
  277. map(lambda c: r1.object_store.add_object(r_base.get_object(c)), \
  278. r1_commits)
  279. r1.refs['HEAD'] = r1_commits[0]
  280. r2 = Repo.init_bare(r2_dir)
  281. map(lambda c: r2.object_store.add_object(r_base.get_object(c)), \
  282. r2_commits)
  283. r2.refs['HEAD'] = r2_commits[0]
  284. # Finally, the 'real' testing!
  285. shas = r2.object_store.find_common_revisions(r1.get_graph_walker())
  286. self.assertEqual(set(shas), expected_shas)
  287. shas = r1.object_store.find_common_revisions(r2.get_graph_walker())
  288. self.assertEqual(set(shas), expected_shas)
  289. finally:
  290. shutil.rmtree(r1_dir)
  291. shutil.rmtree(r2_dir)
  292. def test_shell_hook_pre_commit(self):
  293. if os.name != 'posix':
  294. self.skipTest('shell hook tests requires POSIX shell')
  295. pre_commit_fail = """#!/bin/sh
  296. exit 1
  297. """
  298. pre_commit_success = """#!/bin/sh
  299. exit 0
  300. """
  301. repo_dir = os.path.join(tempfile.mkdtemp())
  302. r = Repo.init(repo_dir)
  303. self.addCleanup(shutil.rmtree, repo_dir)
  304. pre_commit = os.path.join(r.controldir(), 'hooks', 'pre-commit')
  305. f = open(pre_commit, 'wb')
  306. try:
  307. f.write(pre_commit_fail)
  308. finally:
  309. f.close()
  310. os.chmod(pre_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  311. self.assertRaises(errors.CommitError, r.do_commit, 'failed commit',
  312. committer='Test Committer <test@nodomain.com>',
  313. author='Test Author <test@nodomain.com>',
  314. commit_timestamp=12345, commit_timezone=0,
  315. author_timestamp=12345, author_timezone=0)
  316. f = open(pre_commit, 'wb')
  317. try:
  318. f.write(pre_commit_success)
  319. finally:
  320. f.close()
  321. os.chmod(pre_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  322. commit_sha = r.do_commit(
  323. 'empty commit',
  324. committer='Test Committer <test@nodomain.com>',
  325. author='Test Author <test@nodomain.com>',
  326. commit_timestamp=12395, commit_timezone=0,
  327. author_timestamp=12395, author_timezone=0)
  328. self.assertEqual([], r[commit_sha].parents)
  329. def test_shell_hook_commit_msg(self):
  330. if os.name != 'posix':
  331. self.skipTest('shell hook tests requires POSIX shell')
  332. commit_msg_fail = """#!/bin/sh
  333. exit 1
  334. """
  335. commit_msg_success = """#!/bin/sh
  336. exit 0
  337. """
  338. repo_dir = os.path.join(tempfile.mkdtemp())
  339. r = Repo.init(repo_dir)
  340. self.addCleanup(shutil.rmtree, repo_dir)
  341. commit_msg = os.path.join(r.controldir(), 'hooks', 'commit-msg')
  342. f = open(commit_msg, 'wb')
  343. try:
  344. f.write(commit_msg_fail)
  345. finally:
  346. f.close()
  347. os.chmod(commit_msg, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  348. self.assertRaises(errors.CommitError, r.do_commit, 'failed commit',
  349. committer='Test Committer <test@nodomain.com>',
  350. author='Test Author <test@nodomain.com>',
  351. commit_timestamp=12345, commit_timezone=0,
  352. author_timestamp=12345, author_timezone=0)
  353. f = open(commit_msg, 'wb')
  354. try:
  355. f.write(commit_msg_success)
  356. finally:
  357. f.close()
  358. os.chmod(commit_msg, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  359. commit_sha = r.do_commit(
  360. 'empty commit',
  361. committer='Test Committer <test@nodomain.com>',
  362. author='Test Author <test@nodomain.com>',
  363. commit_timestamp=12395, commit_timezone=0,
  364. author_timestamp=12395, author_timezone=0)
  365. self.assertEqual([], r[commit_sha].parents)
  366. def test_shell_hook_post_commit(self):
  367. if os.name != 'posix':
  368. self.skipTest('shell hook tests requires POSIX shell')
  369. repo_dir = os.path.join(tempfile.mkdtemp())
  370. r = Repo.init(repo_dir)
  371. self.addCleanup(shutil.rmtree, repo_dir)
  372. (fd, path) = tempfile.mkstemp(dir=repo_dir)
  373. post_commit_msg = """#!/bin/sh
  374. unlink %(file)s
  375. """ % {'file': path}
  376. root_sha = r.do_commit(
  377. 'empty commit',
  378. committer='Test Committer <test@nodomain.com>',
  379. author='Test Author <test@nodomain.com>',
  380. commit_timestamp=12345, commit_timezone=0,
  381. author_timestamp=12345, author_timezone=0)
  382. self.assertEqual([], r[root_sha].parents)
  383. post_commit = os.path.join(r.controldir(), 'hooks', 'post-commit')
  384. f = open(post_commit, 'wb')
  385. try:
  386. f.write(post_commit_msg)
  387. finally:
  388. f.close()
  389. os.chmod(post_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  390. commit_sha = r.do_commit(
  391. 'empty commit',
  392. committer='Test Committer <test@nodomain.com>',
  393. author='Test Author <test@nodomain.com>',
  394. commit_timestamp=12345, commit_timezone=0,
  395. author_timestamp=12345, author_timezone=0)
  396. self.assertEqual([root_sha], r[commit_sha].parents)
  397. self.assertFalse(os.path.exists(path))
  398. post_commit_msg_fail = """#!/bin/sh
  399. exit 1
  400. """
  401. f = open(post_commit, 'wb')
  402. try:
  403. f.write(post_commit_msg_fail)
  404. finally:
  405. f.close()
  406. os.chmod(post_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  407. warnings.simplefilter("always", UserWarning)
  408. self.addCleanup(warnings.resetwarnings)
  409. warnings_list = setup_warning_catcher()
  410. commit_sha2 = r.do_commit(
  411. 'empty commit',
  412. committer='Test Committer <test@nodomain.com>',
  413. author='Test Author <test@nodomain.com>',
  414. commit_timestamp=12345, commit_timezone=0,
  415. author_timestamp=12345, author_timezone=0)
  416. self.assertEqual(len(warnings_list), 1)
  417. self.assertIsInstance(warnings_list[-1], UserWarning)
  418. self.assertTrue("post-commit hook failed: " in str(warnings_list[-1]))
  419. self.assertEqual([commit_sha], r[commit_sha2].parents)
  420. class BuildRepoTests(TestCase):
  421. """Tests that build on-disk repos from scratch.
  422. Repos live in a temp dir and are torn down after each test. They start with
  423. a single commit in master having single file named 'a'.
  424. """
  425. def setUp(self):
  426. super(BuildRepoTests, self).setUp()
  427. repo_dir = os.path.join(tempfile.mkdtemp(), 'test')
  428. os.makedirs(repo_dir)
  429. r = self._repo = Repo.init(repo_dir)
  430. self.assertFalse(r.bare)
  431. self.assertEqual('ref: refs/heads/master', r.refs.read_ref('HEAD'))
  432. self.assertRaises(KeyError, lambda: r.refs['refs/heads/master'])
  433. f = open(os.path.join(r.path, 'a'), 'wb')
  434. try:
  435. f.write('file contents')
  436. finally:
  437. f.close()
  438. r.stage(['a'])
  439. commit_sha = r.do_commit('msg',
  440. committer='Test Committer <test@nodomain.com>',
  441. author='Test Author <test@nodomain.com>',
  442. commit_timestamp=12345, commit_timezone=0,
  443. author_timestamp=12345, author_timezone=0)
  444. self.assertEqual([], r[commit_sha].parents)
  445. self._root_commit = commit_sha
  446. def tearDown(self):
  447. tear_down_repo(self._repo)
  448. super(BuildRepoTests, self).tearDown()
  449. def test_build_repo(self):
  450. r = self._repo
  451. self.assertEqual('ref: refs/heads/master', r.refs.read_ref('HEAD'))
  452. self.assertEqual(self._root_commit, r.refs['refs/heads/master'])
  453. expected_blob = objects.Blob.from_string('file contents')
  454. self.assertEqual(expected_blob.data, r[expected_blob.id].data)
  455. actual_commit = r[self._root_commit]
  456. self.assertEqual('msg', actual_commit.message)
  457. def test_commit_modified(self):
  458. r = self._repo
  459. f = open(os.path.join(r.path, 'a'), 'wb')
  460. try:
  461. f.write('new contents')
  462. finally:
  463. f.close()
  464. r.stage(['a'])
  465. commit_sha = r.do_commit('modified a',
  466. committer='Test Committer <test@nodomain.com>',
  467. author='Test Author <test@nodomain.com>',
  468. commit_timestamp=12395, commit_timezone=0,
  469. author_timestamp=12395, author_timezone=0)
  470. self.assertEqual([self._root_commit], r[commit_sha].parents)
  471. _, blob_id = tree_lookup_path(r.get_object, r[commit_sha].tree, 'a')
  472. self.assertEqual('new contents', r[blob_id].data)
  473. def test_commit_deleted(self):
  474. r = self._repo
  475. os.remove(os.path.join(r.path, 'a'))
  476. r.stage(['a'])
  477. commit_sha = r.do_commit('deleted a',
  478. committer='Test Committer <test@nodomain.com>',
  479. author='Test Author <test@nodomain.com>',
  480. commit_timestamp=12395, commit_timezone=0,
  481. author_timestamp=12395, author_timezone=0)
  482. self.assertEqual([self._root_commit], r[commit_sha].parents)
  483. self.assertEqual([], list(r.open_index()))
  484. tree = r[r[commit_sha].tree]
  485. self.assertEqual([], list(tree.iteritems()))
  486. def test_commit_encoding(self):
  487. r = self._repo
  488. commit_sha = r.do_commit('commit with strange character \xee',
  489. committer='Test Committer <test@nodomain.com>',
  490. author='Test Author <test@nodomain.com>',
  491. commit_timestamp=12395, commit_timezone=0,
  492. author_timestamp=12395, author_timezone=0,
  493. encoding="iso8859-1")
  494. self.assertEqual("iso8859-1", r[commit_sha].encoding)
  495. def test_commit_config_identity(self):
  496. # commit falls back to the users' identity if it wasn't specified
  497. r = self._repo
  498. c = r.get_config()
  499. c.set(("user", ), "name", "Jelmer")
  500. c.set(("user", ), "email", "jelmer@apache.org")
  501. c.write_to_path()
  502. commit_sha = r.do_commit('message')
  503. self.assertEqual(
  504. "Jelmer <jelmer@apache.org>",
  505. r[commit_sha].author)
  506. self.assertEqual(
  507. "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, 'failed commit',
  519. committer='Test Committer <test@nodomain.com>',
  520. author='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('failed commit', new_commit.message)
  529. def test_commit_branch(self):
  530. r = self._repo
  531. commit_sha = r.do_commit('commit to branch',
  532. committer='Test Committer <test@nodomain.com>',
  533. author='Test Author <test@nodomain.com>',
  534. commit_timestamp=12395, commit_timezone=0,
  535. author_timestamp=12395, author_timezone=0,
  536. ref="refs/heads/new_branch")
  537. self.assertEqual(self._root_commit, r["HEAD"].id)
  538. self.assertEqual(commit_sha, r["refs/heads/new_branch"].id)
  539. self.assertEqual([], r[commit_sha].parents)
  540. self.assertTrue("refs/heads/new_branch" in r)
  541. new_branch_head = commit_sha
  542. commit_sha = r.do_commit('commit to branch 2',
  543. committer='Test Committer <test@nodomain.com>',
  544. author='Test Author <test@nodomain.com>',
  545. commit_timestamp=12395, commit_timezone=0,
  546. author_timestamp=12395, author_timezone=0,
  547. ref="refs/heads/new_branch")
  548. self.assertEqual(self._root_commit, r["HEAD"].id)
  549. self.assertEqual(commit_sha, r["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('commit to branch 2',
  554. committer='Test Committer <test@nodomain.com>',
  555. author='Test Author <test@nodomain.com>',
  556. commit_timestamp=12395, commit_timezone=0,
  557. author_timestamp=12395, author_timezone=0,
  558. ref="refs/heads/new_branch")
  559. commit_sha = r.do_commit('commit with merge',
  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. merge_heads=[merge_1])
  565. self.assertEqual(
  566. [self._root_commit, merge_1],
  567. r[commit_sha].parents)
  568. def test_stage_deleted(self):
  569. r = self._repo
  570. os.remove(os.path.join(r.path, 'a'))
  571. r.stage(['a'])
  572. r.stage(['a']) # double-stage a deleted path