test_refs.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. # test_refs.py -- tests for refs.py
  2. # encoding: utf-8
  3. # Copyright (C) 2013 Jelmer Vernooij <jelmer@jelmer.uk>
  4. #
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as public by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. """Tests for dulwich.refs."""
  22. from io import BytesIO
  23. import os
  24. import sys
  25. import tempfile
  26. from dulwich import errors
  27. from dulwich.file import (
  28. GitFile,
  29. )
  30. from dulwich.objects import ZERO_SHA
  31. from dulwich.refs import (
  32. DictRefsContainer,
  33. InfoRefsContainer,
  34. check_ref_format,
  35. _split_ref_line,
  36. parse_symref_value,
  37. read_packed_refs_with_peeled,
  38. read_packed_refs,
  39. strip_peeled_refs,
  40. write_packed_refs,
  41. )
  42. from dulwich.repo import Repo
  43. from dulwich.tests import (
  44. SkipTest,
  45. TestCase,
  46. )
  47. from dulwich.tests.utils import (
  48. open_repo,
  49. tear_down_repo,
  50. )
  51. class CheckRefFormatTests(TestCase):
  52. """Tests for the check_ref_format function.
  53. These are the same tests as in the git test suite.
  54. """
  55. def test_valid(self):
  56. self.assertTrue(check_ref_format(b'heads/foo'))
  57. self.assertTrue(check_ref_format(b'foo/bar/baz'))
  58. self.assertTrue(check_ref_format(b'refs///heads/foo'))
  59. self.assertTrue(check_ref_format(b'foo./bar'))
  60. self.assertTrue(check_ref_format(b'heads/foo@bar'))
  61. self.assertTrue(check_ref_format(b'heads/fix.lock.error'))
  62. def test_invalid(self):
  63. self.assertFalse(check_ref_format(b'foo'))
  64. self.assertFalse(check_ref_format(b'heads/foo/'))
  65. self.assertFalse(check_ref_format(b'./foo'))
  66. self.assertFalse(check_ref_format(b'.refs/foo'))
  67. self.assertFalse(check_ref_format(b'heads/foo..bar'))
  68. self.assertFalse(check_ref_format(b'heads/foo?bar'))
  69. self.assertFalse(check_ref_format(b'heads/foo.lock'))
  70. self.assertFalse(check_ref_format(b'heads/v@{ation'))
  71. self.assertFalse(check_ref_format(b'heads/foo\bar'))
  72. ONES = b'1' * 40
  73. TWOS = b'2' * 40
  74. THREES = b'3' * 40
  75. FOURS = b'4' * 40
  76. class PackedRefsFileTests(TestCase):
  77. def test_split_ref_line_errors(self):
  78. self.assertRaises(errors.PackedRefsException, _split_ref_line,
  79. b'singlefield')
  80. self.assertRaises(errors.PackedRefsException, _split_ref_line,
  81. b'badsha name')
  82. self.assertRaises(errors.PackedRefsException, _split_ref_line,
  83. ONES + b' bad/../refname')
  84. def test_read_without_peeled(self):
  85. f = BytesIO(b'\n'.join([
  86. b'# comment',
  87. ONES + b' ref/1',
  88. TWOS + b' ref/2']))
  89. self.assertEqual([(ONES, b'ref/1'), (TWOS, b'ref/2')],
  90. list(read_packed_refs(f)))
  91. def test_read_without_peeled_errors(self):
  92. f = BytesIO(b'\n'.join([
  93. ONES + b' ref/1',
  94. b'^' + TWOS]))
  95. self.assertRaises(errors.PackedRefsException, list,
  96. read_packed_refs(f))
  97. def test_read_with_peeled(self):
  98. f = BytesIO(b'\n'.join([
  99. ONES + b' ref/1',
  100. TWOS + b' ref/2',
  101. b'^' + THREES,
  102. FOURS + b' ref/4']))
  103. self.assertEqual([
  104. (ONES, b'ref/1', None),
  105. (TWOS, b'ref/2', THREES),
  106. (FOURS, b'ref/4', None),
  107. ], list(read_packed_refs_with_peeled(f)))
  108. def test_read_with_peeled_errors(self):
  109. f = BytesIO(b'\n'.join([
  110. b'^' + TWOS,
  111. ONES + b' ref/1']))
  112. self.assertRaises(errors.PackedRefsException, list,
  113. read_packed_refs(f))
  114. f = BytesIO(b'\n'.join([
  115. ONES + b' ref/1',
  116. b'^' + TWOS,
  117. b'^' + THREES]))
  118. self.assertRaises(errors.PackedRefsException, list,
  119. read_packed_refs(f))
  120. def test_write_with_peeled(self):
  121. f = BytesIO()
  122. write_packed_refs(f, {b'ref/1': ONES, b'ref/2': TWOS},
  123. {b'ref/1': THREES})
  124. self.assertEqual(
  125. b'\n'.join([b'# pack-refs with: peeled',
  126. ONES + b' ref/1',
  127. b'^' + THREES,
  128. TWOS + b' ref/2']) + b'\n',
  129. f.getvalue())
  130. def test_write_without_peeled(self):
  131. f = BytesIO()
  132. write_packed_refs(f, {b'ref/1': ONES, b'ref/2': TWOS})
  133. self.assertEqual(b'\n'.join([ONES + b' ref/1',
  134. TWOS + b' ref/2']) + b'\n',
  135. f.getvalue())
  136. # Dict of refs that we expect all RefsContainerTests subclasses to define.
  137. _TEST_REFS = {
  138. b'HEAD': b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  139. b'refs/heads/40-char-ref-aaaaaaaaaaaaaaaaaa':
  140. b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  141. b'refs/heads/master': b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  142. b'refs/heads/packed': b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  143. b'refs/tags/refs-0.1': b'df6800012397fb85c56e7418dd4eb9405dee075c',
  144. b'refs/tags/refs-0.2': b'3ec9c43c84ff242e3ef4a9fc5bc111fd780a76a8',
  145. b'refs/heads/loop': b'ref: refs/heads/loop',
  146. }
  147. class RefsContainerTests(object):
  148. def test_keys(self):
  149. actual_keys = set(self._refs.keys())
  150. self.assertEqual(set(self._refs.allkeys()), actual_keys)
  151. self.assertEqual(set(_TEST_REFS.keys()), actual_keys)
  152. actual_keys = self._refs.keys(b'refs/heads')
  153. actual_keys.discard(b'loop')
  154. self.assertEqual(
  155. [b'40-char-ref-aaaaaaaaaaaaaaaaaa', b'master', b'packed'],
  156. sorted(actual_keys))
  157. self.assertEqual([b'refs-0.1', b'refs-0.2'],
  158. sorted(self._refs.keys(b'refs/tags')))
  159. def test_iter(self):
  160. actual_keys = set(self._refs.keys())
  161. self.assertEqual(set(self._refs), actual_keys)
  162. self.assertEqual(set(_TEST_REFS.keys()), actual_keys)
  163. def test_as_dict(self):
  164. # refs/heads/loop does not show up even if it exists
  165. expected_refs = dict(_TEST_REFS)
  166. del expected_refs[b'refs/heads/loop']
  167. self.assertEqual(expected_refs, self._refs.as_dict())
  168. def test_get_symrefs(self):
  169. self._refs.set_symbolic_ref(b'refs/heads/src', b'refs/heads/dst')
  170. symrefs = self._refs.get_symrefs()
  171. if b'HEAD' in symrefs:
  172. symrefs.pop(b'HEAD')
  173. self.assertEqual({b'refs/heads/src': b'refs/heads/dst',
  174. b'refs/heads/loop': b'refs/heads/loop'},
  175. symrefs)
  176. def test_setitem(self):
  177. self._refs[b'refs/some/ref'] = (
  178. b'42d06bd4b77fed026b154d16493e5deab78f02ec')
  179. self.assertEqual(b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  180. self._refs[b'refs/some/ref'])
  181. self.assertRaises(
  182. errors.RefFormatError, self._refs.__setitem__,
  183. b'notrefs/foo', b'42d06bd4b77fed026b154d16493e5deab78f02ec')
  184. def test_set_if_equals(self):
  185. nines = b'9' * 40
  186. self.assertFalse(self._refs.set_if_equals(b'HEAD', b'c0ffee', nines))
  187. self.assertEqual(b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  188. self._refs[b'HEAD'])
  189. self.assertTrue(self._refs.set_if_equals(
  190. b'HEAD', b'42d06bd4b77fed026b154d16493e5deab78f02ec', nines))
  191. self.assertEqual(nines, self._refs[b'HEAD'])
  192. self.assertTrue(self._refs.set_if_equals(b'refs/heads/master', None,
  193. nines))
  194. self.assertEqual(nines, self._refs[b'refs/heads/master'])
  195. self.assertTrue(self._refs.set_if_equals(
  196. b'refs/heads/nonexistant', ZERO_SHA, nines))
  197. self.assertEqual(nines, self._refs[b'refs/heads/nonexistant'])
  198. def test_add_if_new(self):
  199. nines = b'9' * 40
  200. self.assertFalse(self._refs.add_if_new(b'refs/heads/master', nines))
  201. self.assertEqual(b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  202. self._refs[b'refs/heads/master'])
  203. self.assertTrue(self._refs.add_if_new(b'refs/some/ref', nines))
  204. self.assertEqual(nines, self._refs[b'refs/some/ref'])
  205. def test_set_symbolic_ref(self):
  206. self._refs.set_symbolic_ref(b'refs/heads/symbolic',
  207. b'refs/heads/master')
  208. self.assertEqual(b'ref: refs/heads/master',
  209. self._refs.read_loose_ref(b'refs/heads/symbolic'))
  210. self.assertEqual(b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  211. self._refs[b'refs/heads/symbolic'])
  212. def test_set_symbolic_ref_overwrite(self):
  213. nines = b'9' * 40
  214. self.assertFalse(b'refs/heads/symbolic' in self._refs)
  215. self._refs[b'refs/heads/symbolic'] = nines
  216. self.assertEqual(nines,
  217. self._refs.read_loose_ref(b'refs/heads/symbolic'))
  218. self._refs.set_symbolic_ref(b'refs/heads/symbolic',
  219. b'refs/heads/master')
  220. self.assertEqual(b'ref: refs/heads/master',
  221. self._refs.read_loose_ref(b'refs/heads/symbolic'))
  222. self.assertEqual(b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  223. self._refs[b'refs/heads/symbolic'])
  224. def test_check_refname(self):
  225. self._refs._check_refname(b'HEAD')
  226. self._refs._check_refname(b'refs/stash')
  227. self._refs._check_refname(b'refs/heads/foo')
  228. self.assertRaises(errors.RefFormatError, self._refs._check_refname,
  229. b'refs')
  230. self.assertRaises(errors.RefFormatError, self._refs._check_refname,
  231. b'notrefs/foo')
  232. def test_contains(self):
  233. self.assertTrue(b'refs/heads/master' in self._refs)
  234. self.assertFalse(b'refs/heads/bar' in self._refs)
  235. def test_delitem(self):
  236. self.assertEqual(b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  237. self._refs[b'refs/heads/master'])
  238. del self._refs[b'refs/heads/master']
  239. self.assertRaises(KeyError, lambda: self._refs[b'refs/heads/master'])
  240. def test_remove_if_equals(self):
  241. self.assertFalse(self._refs.remove_if_equals(b'HEAD', b'c0ffee'))
  242. self.assertEqual(b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  243. self._refs[b'HEAD'])
  244. self.assertTrue(self._refs.remove_if_equals(
  245. b'refs/tags/refs-0.2',
  246. b'3ec9c43c84ff242e3ef4a9fc5bc111fd780a76a8'))
  247. self.assertTrue(self._refs.remove_if_equals(
  248. b'refs/tags/refs-0.2', ZERO_SHA))
  249. self.assertFalse(b'refs/tags/refs-0.2' in self._refs)
  250. def test_import_refs_name(self):
  251. self._refs[b'refs/remotes/origin/other'] = (
  252. b'48d01bd4b77fed026b154d16493e5deab78f02ec')
  253. self._refs.import_refs(
  254. b'refs/remotes/origin',
  255. {b'master': b'42d06bd4b77fed026b154d16493e5deab78f02ec'})
  256. self.assertEqual(
  257. b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  258. self._refs[b'refs/remotes/origin/master'])
  259. self.assertEqual(
  260. b'48d01bd4b77fed026b154d16493e5deab78f02ec',
  261. self._refs[b'refs/remotes/origin/other'])
  262. def test_import_refs_name_prune(self):
  263. self._refs[b'refs/remotes/origin/other'] = (
  264. b'48d01bd4b77fed026b154d16493e5deab78f02ec')
  265. self._refs.import_refs(
  266. b'refs/remotes/origin',
  267. {b'master': b'42d06bd4b77fed026b154d16493e5deab78f02ec'},
  268. prune=True)
  269. self.assertEqual(
  270. b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  271. self._refs[b'refs/remotes/origin/master'])
  272. self.assertNotIn(
  273. b'refs/remotes/origin/other', self._refs)
  274. class DictRefsContainerTests(RefsContainerTests, TestCase):
  275. def setUp(self):
  276. TestCase.setUp(self)
  277. self._refs = DictRefsContainer(dict(_TEST_REFS))
  278. def test_invalid_refname(self):
  279. # FIXME: Move this test into RefsContainerTests, but requires
  280. # some way of injecting invalid refs.
  281. self._refs._refs[b'refs/stash'] = b'00' * 20
  282. expected_refs = dict(_TEST_REFS)
  283. del expected_refs[b'refs/heads/loop']
  284. expected_refs[b'refs/stash'] = b'00' * 20
  285. self.assertEqual(expected_refs, self._refs.as_dict())
  286. class DiskRefsContainerTests(RefsContainerTests, TestCase):
  287. def setUp(self):
  288. TestCase.setUp(self)
  289. self._repo = open_repo('refs.git')
  290. self.addCleanup(tear_down_repo, self._repo)
  291. self._refs = self._repo.refs
  292. def test_get_packed_refs(self):
  293. self.assertEqual({
  294. b'refs/heads/packed': b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  295. b'refs/tags/refs-0.1': b'df6800012397fb85c56e7418dd4eb9405dee075c',
  296. }, self._refs.get_packed_refs())
  297. def test_get_peeled_not_packed(self):
  298. # not packed
  299. self.assertEqual(None, self._refs.get_peeled(b'refs/tags/refs-0.2'))
  300. self.assertEqual(b'3ec9c43c84ff242e3ef4a9fc5bc111fd780a76a8',
  301. self._refs[b'refs/tags/refs-0.2'])
  302. # packed, known not peelable
  303. self.assertEqual(self._refs[b'refs/heads/packed'],
  304. self._refs.get_peeled(b'refs/heads/packed'))
  305. # packed, peeled
  306. self.assertEqual(b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  307. self._refs.get_peeled(b'refs/tags/refs-0.1'))
  308. def test_setitem(self):
  309. RefsContainerTests.test_setitem(self)
  310. path = os.path.join(self._refs.path, b'refs', b'some', b'ref')
  311. with open(path, 'rb') as f:
  312. self.assertEqual(b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  313. f.read()[:40])
  314. self.assertRaises(
  315. OSError, self._refs.__setitem__,
  316. b'refs/some/ref/sub', b'42d06bd4b77fed026b154d16493e5deab78f02ec')
  317. def test_setitem_packed(self):
  318. with open(os.path.join(self._refs.path, b'packed-refs'), 'w') as f:
  319. f.write('# pack-refs with: peeled fully-peeled sorted \n')
  320. f.write(
  321. '42d06bd4b77fed026b154d16493e5deab78f02ec refs/heads/packed\n')
  322. # It's allowed to set a new ref on a packed ref, the new ref will be
  323. # placed outside on refs/
  324. self._refs[b'refs/heads/packed'] = (
  325. b'3ec9c43c84ff242e3ef4a9fc5bc111fd780a76a8'
  326. )
  327. packed_ref_path = os.path.join(
  328. self._refs.path, b'refs', b'heads', b'packed')
  329. with open(packed_ref_path, 'rb') as f:
  330. self.assertEqual(
  331. b'3ec9c43c84ff242e3ef4a9fc5bc111fd780a76a8',
  332. f.read()[:40])
  333. self.assertRaises(
  334. OSError, self._refs.__setitem__,
  335. b'refs/heads/packed/sub',
  336. b'42d06bd4b77fed026b154d16493e5deab78f02ec')
  337. def test_setitem_symbolic(self):
  338. ones = b'1' * 40
  339. self._refs[b'HEAD'] = ones
  340. self.assertEqual(ones, self._refs[b'HEAD'])
  341. # ensure HEAD was not modified
  342. f = open(os.path.join(self._refs.path, b'HEAD'), 'rb')
  343. v = next(iter(f)).rstrip(b'\n\r')
  344. f.close()
  345. self.assertEqual(b'ref: refs/heads/master', v)
  346. # ensure the symbolic link was written through
  347. f = open(os.path.join(self._refs.path, b'refs', b'heads', b'master'),
  348. 'rb')
  349. self.assertEqual(ones, f.read()[:40])
  350. f.close()
  351. def test_set_if_equals(self):
  352. RefsContainerTests.test_set_if_equals(self)
  353. # ensure symref was followed
  354. self.assertEqual(b'9' * 40, self._refs[b'refs/heads/master'])
  355. # ensure lockfile was deleted
  356. self.assertFalse(os.path.exists(
  357. os.path.join(self._refs.path, b'refs', b'heads', b'master.lock')))
  358. self.assertFalse(os.path.exists(
  359. os.path.join(self._refs.path, b'HEAD.lock')))
  360. def test_add_if_new_packed(self):
  361. # don't overwrite packed ref
  362. self.assertFalse(self._refs.add_if_new(b'refs/tags/refs-0.1',
  363. b'9' * 40))
  364. self.assertEqual(b'df6800012397fb85c56e7418dd4eb9405dee075c',
  365. self._refs[b'refs/tags/refs-0.1'])
  366. def test_add_if_new_symbolic(self):
  367. # Use an empty repo instead of the default.
  368. repo_dir = os.path.join(tempfile.mkdtemp(), 'test')
  369. os.makedirs(repo_dir)
  370. repo = Repo.init(repo_dir)
  371. self.addCleanup(tear_down_repo, repo)
  372. refs = repo.refs
  373. nines = b'9' * 40
  374. self.assertEqual(b'ref: refs/heads/master', refs.read_ref(b'HEAD'))
  375. self.assertFalse(b'refs/heads/master' in refs)
  376. self.assertTrue(refs.add_if_new(b'HEAD', nines))
  377. self.assertEqual(b'ref: refs/heads/master', refs.read_ref(b'HEAD'))
  378. self.assertEqual(nines, refs[b'HEAD'])
  379. self.assertEqual(nines, refs[b'refs/heads/master'])
  380. self.assertFalse(refs.add_if_new(b'HEAD', b'1' * 40))
  381. self.assertEqual(nines, refs[b'HEAD'])
  382. self.assertEqual(nines, refs[b'refs/heads/master'])
  383. def test_follow(self):
  384. self.assertEqual(([b'HEAD', b'refs/heads/master'],
  385. b'42d06bd4b77fed026b154d16493e5deab78f02ec'),
  386. self._refs.follow(b'HEAD'))
  387. self.assertEqual(([b'refs/heads/master'],
  388. b'42d06bd4b77fed026b154d16493e5deab78f02ec'),
  389. self._refs.follow(b'refs/heads/master'))
  390. self.assertRaises(KeyError, self._refs.follow, b'refs/heads/loop')
  391. def test_delitem(self):
  392. RefsContainerTests.test_delitem(self)
  393. ref_file = os.path.join(self._refs.path, b'refs', b'heads', b'master')
  394. self.assertFalse(os.path.exists(ref_file))
  395. self.assertFalse(b'refs/heads/master' in self._refs.get_packed_refs())
  396. def test_delitem_symbolic(self):
  397. self.assertEqual(b'ref: refs/heads/master',
  398. self._refs.read_loose_ref(b'HEAD'))
  399. del self._refs[b'HEAD']
  400. self.assertRaises(KeyError, lambda: self._refs[b'HEAD'])
  401. self.assertEqual(b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  402. self._refs[b'refs/heads/master'])
  403. self.assertFalse(
  404. os.path.exists(os.path.join(self._refs.path, b'HEAD')))
  405. def test_remove_if_equals_symref(self):
  406. # HEAD is a symref, so shouldn't equal its dereferenced value
  407. self.assertFalse(self._refs.remove_if_equals(
  408. b'HEAD', b'42d06bd4b77fed026b154d16493e5deab78f02ec'))
  409. self.assertTrue(self._refs.remove_if_equals(
  410. b'refs/heads/master', b'42d06bd4b77fed026b154d16493e5deab78f02ec'))
  411. self.assertRaises(KeyError, lambda: self._refs[b'refs/heads/master'])
  412. # HEAD is now a broken symref
  413. self.assertRaises(KeyError, lambda: self._refs[b'HEAD'])
  414. self.assertEqual(b'ref: refs/heads/master',
  415. self._refs.read_loose_ref(b'HEAD'))
  416. self.assertFalse(os.path.exists(
  417. os.path.join(self._refs.path, b'refs', b'heads', b'master.lock')))
  418. self.assertFalse(os.path.exists(
  419. os.path.join(self._refs.path, b'HEAD.lock')))
  420. def test_remove_packed_without_peeled(self):
  421. refs_file = os.path.join(self._repo.path, 'packed-refs')
  422. f = GitFile(refs_file)
  423. refs_data = f.read()
  424. f.close()
  425. f = GitFile(refs_file, 'wb')
  426. f.write(b'\n'.join(l for l in refs_data.split(b'\n')
  427. if not l or l[0] not in b'#^'))
  428. f.close()
  429. self._repo = Repo(self._repo.path)
  430. refs = self._repo.refs
  431. self.assertTrue(refs.remove_if_equals(
  432. b'refs/heads/packed', b'42d06bd4b77fed026b154d16493e5deab78f02ec'))
  433. def test_remove_if_equals_packed(self):
  434. # test removing ref that is only packed
  435. self.assertEqual(b'df6800012397fb85c56e7418dd4eb9405dee075c',
  436. self._refs[b'refs/tags/refs-0.1'])
  437. self.assertTrue(
  438. self._refs.remove_if_equals(
  439. b'refs/tags/refs-0.1',
  440. b'df6800012397fb85c56e7418dd4eb9405dee075c'))
  441. self.assertRaises(KeyError, lambda: self._refs[b'refs/tags/refs-0.1'])
  442. def test_remove_parent(self):
  443. self._refs[b'refs/heads/foo/bar'] = (
  444. b'df6800012397fb85c56e7418dd4eb9405dee075c'
  445. )
  446. del self._refs[b'refs/heads/foo/bar']
  447. ref_file = os.path.join(
  448. self._refs.path, b'refs', b'heads', b'foo', b'bar',
  449. )
  450. self.assertFalse(os.path.exists(ref_file))
  451. ref_file = os.path.join(self._refs.path, b'refs', b'heads', b'foo')
  452. self.assertFalse(os.path.exists(ref_file))
  453. ref_file = os.path.join(self._refs.path, b'refs', b'heads')
  454. self.assertTrue(os.path.exists(ref_file))
  455. self._refs[b'refs/heads/foo'] = (
  456. b'df6800012397fb85c56e7418dd4eb9405dee075c'
  457. )
  458. def test_read_ref(self):
  459. self.assertEqual(b'ref: refs/heads/master',
  460. self._refs.read_ref(b'HEAD'))
  461. self.assertEqual(b'42d06bd4b77fed026b154d16493e5deab78f02ec',
  462. self._refs.read_ref(b'refs/heads/packed'))
  463. self.assertEqual(None, self._refs.read_ref(b'nonexistant'))
  464. def test_read_loose_ref(self):
  465. self._refs[b'refs/heads/foo'] = (
  466. b'df6800012397fb85c56e7418dd4eb9405dee075c'
  467. )
  468. self.assertEqual(None, self._refs.read_ref(b'refs/heads/foo/bar'))
  469. def test_non_ascii(self):
  470. try:
  471. encoded_ref = u'refs/tags/schön'.encode(
  472. sys.getfilesystemencoding())
  473. except UnicodeEncodeError:
  474. raise SkipTest(
  475. "filesystem encoding doesn't support special character")
  476. p = os.path.join(
  477. self._repo.path.encode(sys.getfilesystemencoding()),
  478. encoded_ref)
  479. with open(p, 'w') as f:
  480. f.write('00' * 20)
  481. expected_refs = dict(_TEST_REFS)
  482. expected_refs[encoded_ref] = b'00' * 20
  483. del expected_refs[b'refs/heads/loop']
  484. self.assertEqual(expected_refs, self._repo.get_refs())
  485. def test_cyrillic(self):
  486. if sys.platform == 'win32':
  487. raise SkipTest(
  488. "filesystem encoding doesn't support arbitrary bytes")
  489. # reported in https://github.com/dulwich/dulwich/issues/608
  490. name = b'\xcd\xee\xe2\xe0\xff\xe2\xe5\xf2\xea\xe01'
  491. encoded_ref = b'refs/heads/' + name
  492. with open(os.path.join(
  493. self._repo.path.encode(
  494. sys.getfilesystemencoding()), encoded_ref), 'w') as f:
  495. f.write('00' * 20)
  496. expected_refs = set(_TEST_REFS.keys())
  497. expected_refs.add(encoded_ref)
  498. self.assertEqual(expected_refs,
  499. set(self._repo.refs.allkeys()))
  500. self.assertEqual({r[len(b'refs/'):] for r in expected_refs
  501. if r.startswith(b'refs/')},
  502. set(self._repo.refs.subkeys(b'refs/')))
  503. expected_refs.remove(b'refs/heads/loop')
  504. expected_refs.add(b'HEAD')
  505. self.assertEqual(expected_refs,
  506. set(self._repo.get_refs().keys()))
  507. _TEST_REFS_SERIALIZED = (
  508. b'42d06bd4b77fed026b154d16493e5deab78f02ec\t'
  509. b'refs/heads/40-char-ref-aaaaaaaaaaaaaaaaaa\n'
  510. b'42d06bd4b77fed026b154d16493e5deab78f02ec\trefs/heads/master\n'
  511. b'42d06bd4b77fed026b154d16493e5deab78f02ec\trefs/heads/packed\n'
  512. b'df6800012397fb85c56e7418dd4eb9405dee075c\trefs/tags/refs-0.1\n'
  513. b'3ec9c43c84ff242e3ef4a9fc5bc111fd780a76a8\trefs/tags/refs-0.2\n')
  514. class InfoRefsContainerTests(TestCase):
  515. def test_invalid_refname(self):
  516. text = _TEST_REFS_SERIALIZED + b'00' * 20 + b'\trefs/stash\n'
  517. refs = InfoRefsContainer(BytesIO(text))
  518. expected_refs = dict(_TEST_REFS)
  519. del expected_refs[b'HEAD']
  520. expected_refs[b'refs/stash'] = b'00' * 20
  521. del expected_refs[b'refs/heads/loop']
  522. self.assertEqual(expected_refs, refs.as_dict())
  523. def test_keys(self):
  524. refs = InfoRefsContainer(BytesIO(_TEST_REFS_SERIALIZED))
  525. actual_keys = set(refs.keys())
  526. self.assertEqual(set(refs.allkeys()), actual_keys)
  527. expected_refs = dict(_TEST_REFS)
  528. del expected_refs[b'HEAD']
  529. del expected_refs[b'refs/heads/loop']
  530. self.assertEqual(set(expected_refs.keys()), actual_keys)
  531. actual_keys = refs.keys(b'refs/heads')
  532. actual_keys.discard(b'loop')
  533. self.assertEqual(
  534. [b'40-char-ref-aaaaaaaaaaaaaaaaaa', b'master', b'packed'],
  535. sorted(actual_keys))
  536. self.assertEqual([b'refs-0.1', b'refs-0.2'],
  537. sorted(refs.keys(b'refs/tags')))
  538. def test_as_dict(self):
  539. refs = InfoRefsContainer(BytesIO(_TEST_REFS_SERIALIZED))
  540. # refs/heads/loop does not show up even if it exists
  541. expected_refs = dict(_TEST_REFS)
  542. del expected_refs[b'HEAD']
  543. del expected_refs[b'refs/heads/loop']
  544. self.assertEqual(expected_refs, refs.as_dict())
  545. def test_contains(self):
  546. refs = InfoRefsContainer(BytesIO(_TEST_REFS_SERIALIZED))
  547. self.assertTrue(b'refs/heads/master' in refs)
  548. self.assertFalse(b'refs/heads/bar' in refs)
  549. def test_get_peeled(self):
  550. refs = InfoRefsContainer(BytesIO(_TEST_REFS_SERIALIZED))
  551. # refs/heads/loop does not show up even if it exists
  552. self.assertEqual(
  553. _TEST_REFS[b'refs/heads/master'],
  554. refs.get_peeled(b'refs/heads/master'))
  555. class ParseSymrefValueTests(TestCase):
  556. def test_valid(self):
  557. self.assertEqual(
  558. b'refs/heads/foo',
  559. parse_symref_value(b'ref: refs/heads/foo'))
  560. def test_invalid(self):
  561. self.assertRaises(ValueError, parse_symref_value, b'foobar')
  562. class StripPeeledRefsTests(TestCase):
  563. all_refs = {
  564. b'refs/heads/master': b'8843d7f92416211de9ebb963ff4ce28125932878',
  565. b'refs/heads/testing': b'186a005b134d8639a58b6731c7c1ea821a6eedba',
  566. b'refs/tags/1.0.0': b'a93db4b0360cc635a2b93675010bac8d101f73f0',
  567. b'refs/tags/1.0.0^{}': b'a93db4b0360cc635a2b93675010bac8d101f73f0',
  568. b'refs/tags/2.0.0': b'0749936d0956c661ac8f8d3483774509c165f89e',
  569. b'refs/tags/2.0.0^{}': b'0749936d0956c661ac8f8d3483774509c165f89e',
  570. }
  571. non_peeled_refs = {
  572. b'refs/heads/master': b'8843d7f92416211de9ebb963ff4ce28125932878',
  573. b'refs/heads/testing': b'186a005b134d8639a58b6731c7c1ea821a6eedba',
  574. b'refs/tags/1.0.0': b'a93db4b0360cc635a2b93675010bac8d101f73f0',
  575. b'refs/tags/2.0.0': b'0749936d0956c661ac8f8d3483774509c165f89e',
  576. }
  577. def test_strip_peeled_refs(self):
  578. # Simple check of two dicts
  579. self.assertEqual(
  580. strip_peeled_refs(self.all_refs),
  581. self.non_peeled_refs)