test_refs.py 30 KB

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