2
0

test_repository.py 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649
  1. # test_repository.py -- tests for repository.py
  2. # Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
  3. #
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  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 the repository."""
  22. import glob
  23. import locale
  24. import os
  25. import shutil
  26. import stat
  27. import sys
  28. import tempfile
  29. import warnings
  30. from dulwich import errors, objects, porcelain
  31. from dulwich.config import Config
  32. from dulwich.errors import NotGitRepository
  33. from dulwich.object_store import tree_lookup_path
  34. from dulwich.repo import (
  35. InvalidUserIdentity,
  36. MemoryRepo,
  37. Repo,
  38. UnsupportedExtension,
  39. UnsupportedVersion,
  40. check_user_identity,
  41. )
  42. from dulwich.tests.utils import open_repo, setup_warning_catcher, tear_down_repo
  43. from . import TestCase, skipIf
  44. missing_sha = b"b91fa4d900e17e99b433218e988c4eb4a3e9a097"
  45. class CreateRepositoryTests(TestCase):
  46. def assertFileContentsEqual(self, expected, repo, path) -> None:
  47. f = repo.get_named_file(path)
  48. if not f:
  49. self.assertEqual(expected, None)
  50. else:
  51. with f:
  52. self.assertEqual(expected, f.read())
  53. def _check_repo_contents(self, repo, expect_bare) -> None:
  54. self.assertEqual(expect_bare, repo.bare)
  55. self.assertFileContentsEqual(b"Unnamed repository", repo, "description")
  56. self.assertFileContentsEqual(b"", repo, os.path.join("info", "exclude"))
  57. self.assertFileContentsEqual(None, repo, "nonexistent file")
  58. barestr = b"bare = " + str(expect_bare).lower().encode("ascii")
  59. with repo.get_named_file("config") as f:
  60. config_text = f.read()
  61. self.assertIn(barestr, config_text, f"{config_text!r}")
  62. expect_filemode = sys.platform != "win32"
  63. barestr = b"filemode = " + str(expect_filemode).lower().encode("ascii")
  64. with repo.get_named_file("config") as f:
  65. config_text = f.read()
  66. self.assertIn(barestr, config_text, f"{config_text!r}")
  67. if isinstance(repo, Repo):
  68. expected_mode = "0o100644" if expect_filemode else "0o100666"
  69. expected = {
  70. "HEAD": expected_mode,
  71. "config": expected_mode,
  72. "description": expected_mode,
  73. }
  74. actual = {
  75. f[len(repo._controldir) + 1 :]: oct(os.stat(f).st_mode)
  76. for f in glob.glob(os.path.join(repo._controldir, "*"))
  77. if os.path.isfile(f)
  78. }
  79. self.assertEqual(expected, actual)
  80. def test_create_memory(self) -> None:
  81. repo = MemoryRepo.init_bare([], {})
  82. self._check_repo_contents(repo, True)
  83. def test_create_disk_bare(self) -> None:
  84. tmp_dir = tempfile.mkdtemp()
  85. self.addCleanup(shutil.rmtree, tmp_dir)
  86. repo = Repo.init_bare(tmp_dir)
  87. self.assertEqual(tmp_dir, repo._controldir)
  88. self._check_repo_contents(repo, True)
  89. def test_create_disk_non_bare(self) -> None:
  90. tmp_dir = tempfile.mkdtemp()
  91. self.addCleanup(shutil.rmtree, tmp_dir)
  92. repo = Repo.init(tmp_dir)
  93. self.assertEqual(os.path.join(tmp_dir, ".git"), repo._controldir)
  94. self._check_repo_contents(repo, False)
  95. def test_create_disk_non_bare_mkdir(self) -> None:
  96. tmp_dir = tempfile.mkdtemp()
  97. target_dir = os.path.join(tmp_dir, "target")
  98. self.addCleanup(shutil.rmtree, tmp_dir)
  99. repo = Repo.init(target_dir, mkdir=True)
  100. self.assertEqual(os.path.join(target_dir, ".git"), repo._controldir)
  101. self._check_repo_contents(repo, False)
  102. def test_create_disk_bare_mkdir(self) -> None:
  103. tmp_dir = tempfile.mkdtemp()
  104. target_dir = os.path.join(tmp_dir, "target")
  105. self.addCleanup(shutil.rmtree, tmp_dir)
  106. repo = Repo.init_bare(target_dir, mkdir=True)
  107. self.assertEqual(target_dir, repo._controldir)
  108. self._check_repo_contents(repo, True)
  109. class MemoryRepoTests(TestCase):
  110. def test_set_description(self) -> None:
  111. r = MemoryRepo.init_bare([], {})
  112. description = b"Some description"
  113. r.set_description(description)
  114. self.assertEqual(description, r.get_description())
  115. def test_pull_into(self) -> None:
  116. r = MemoryRepo.init_bare([], {})
  117. repo = open_repo("a.git")
  118. self.addCleanup(tear_down_repo, repo)
  119. repo.fetch(r)
  120. def test_fetch_from_git_cloned_repo(self) -> None:
  121. """Test fetching from a git-cloned repo into MemoryRepo (issue #1179)."""
  122. import tempfile
  123. from dulwich.client import LocalGitClient
  124. with tempfile.TemporaryDirectory() as tmpdir:
  125. # Create initial repo using dulwich
  126. initial_path = os.path.join(tmpdir, "initial")
  127. initial_repo = Repo.init(initial_path, mkdir=True)
  128. # Create some content
  129. test_file = os.path.join(initial_path, "test.txt")
  130. with open(test_file, "w") as f:
  131. f.write("test content\n")
  132. # Stage and commit using dulwich
  133. initial_repo.stage(["test.txt"])
  134. initial_repo.do_commit(
  135. b"Initial commit\n",
  136. committer=b"Test Committer <test@example.com>",
  137. author=b"Test Author <test@example.com>",
  138. )
  139. # Clone using dulwich
  140. cloned_path = os.path.join(tmpdir, "cloned")
  141. cloned_repo = initial_repo.clone(cloned_path, mkdir=True)
  142. initial_repo.close()
  143. cloned_repo.close()
  144. # Fetch from the cloned repo into MemoryRepo
  145. memory_repo = MemoryRepo()
  146. client = LocalGitClient()
  147. # This should not raise AssertionError
  148. result = client.fetch(cloned_path, memory_repo)
  149. # Verify the fetch worked
  150. self.assertIn(b"HEAD", result.refs)
  151. self.assertIn(b"refs/heads/master", result.refs)
  152. # Verify we can read the fetched objects
  153. head_sha = result.refs[b"HEAD"]
  154. commit = memory_repo[head_sha]
  155. self.assertEqual(commit.message, b"Initial commit\n")
  156. class RepositoryRootTests(TestCase):
  157. def mkdtemp(self):
  158. return tempfile.mkdtemp()
  159. def open_repo(self, name):
  160. temp_dir = self.mkdtemp()
  161. repo = open_repo(name, temp_dir)
  162. self.addCleanup(tear_down_repo, repo)
  163. return repo
  164. def test_simple_props(self) -> None:
  165. r = self.open_repo("a.git")
  166. self.assertEqual(r.controldir(), r.path)
  167. def test_setitem(self) -> None:
  168. r = self.open_repo("a.git")
  169. r[b"refs/tags/foo"] = b"a90fa2d900a17e99b433217e988c4eb4a2e9a097"
  170. self.assertEqual(
  171. b"a90fa2d900a17e99b433217e988c4eb4a2e9a097", r[b"refs/tags/foo"].id
  172. )
  173. def test_getitem_unicode(self) -> None:
  174. r = self.open_repo("a.git")
  175. test_keys = [
  176. (b"refs/heads/master", True),
  177. (b"a90fa2d900a17e99b433217e988c4eb4a2e9a097", True),
  178. (b"11" * 19 + b"--", False),
  179. ]
  180. for k, contained in test_keys:
  181. self.assertEqual(k in r, contained)
  182. # Avoid deprecation warning under Py3.2+
  183. if getattr(self, "assertRaisesRegex", None):
  184. assertRaisesRegexp = self.assertRaisesRegex
  185. else:
  186. assertRaisesRegexp = self.assertRaisesRegexp
  187. for k, _ in test_keys:
  188. assertRaisesRegexp(
  189. TypeError,
  190. "'name' must be bytestring, not int",
  191. r.__getitem__,
  192. 12,
  193. )
  194. def test_delitem(self) -> None:
  195. r = self.open_repo("a.git")
  196. del r[b"refs/heads/master"]
  197. self.assertRaises(KeyError, lambda: r[b"refs/heads/master"])
  198. del r[b"HEAD"]
  199. self.assertRaises(KeyError, lambda: r[b"HEAD"])
  200. self.assertRaises(ValueError, r.__delitem__, b"notrefs/foo")
  201. def test_get_refs(self) -> None:
  202. r = self.open_repo("a.git")
  203. self.assertEqual(
  204. {
  205. b"HEAD": b"a90fa2d900a17e99b433217e988c4eb4a2e9a097",
  206. b"refs/heads/master": b"a90fa2d900a17e99b433217e988c4eb4a2e9a097",
  207. b"refs/tags/mytag": b"28237f4dc30d0d462658d6b937b08a0f0b6ef55a",
  208. b"refs/tags/mytag-packed": b"b0931cadc54336e78a1d980420e3268903b57a50",
  209. },
  210. r.get_refs(),
  211. )
  212. def test_head(self) -> None:
  213. r = self.open_repo("a.git")
  214. self.assertEqual(r.head(), b"a90fa2d900a17e99b433217e988c4eb4a2e9a097")
  215. def test_get_object(self) -> None:
  216. r = self.open_repo("a.git")
  217. obj = r.get_object(r.head())
  218. self.assertEqual(obj.type_name, b"commit")
  219. def test_get_object_non_existant(self) -> None:
  220. r = self.open_repo("a.git")
  221. self.assertRaises(KeyError, r.get_object, missing_sha)
  222. def test_contains_object(self) -> None:
  223. r = self.open_repo("a.git")
  224. self.assertIn(r.head(), r)
  225. self.assertNotIn(b"z" * 40, r)
  226. def test_contains_ref(self) -> None:
  227. r = self.open_repo("a.git")
  228. self.assertIn(b"HEAD", r)
  229. def test_get_no_description(self) -> None:
  230. r = self.open_repo("a.git")
  231. self.assertIs(None, r.get_description())
  232. def test_get_description(self) -> None:
  233. r = self.open_repo("a.git")
  234. with open(os.path.join(r.path, "description"), "wb") as f:
  235. f.write(b"Some description")
  236. self.assertEqual(b"Some description", r.get_description())
  237. def test_set_description(self) -> None:
  238. r = self.open_repo("a.git")
  239. description = b"Some description"
  240. r.set_description(description)
  241. self.assertEqual(description, r.get_description())
  242. def test_contains_missing(self) -> None:
  243. r = self.open_repo("a.git")
  244. self.assertNotIn(b"bar", r)
  245. def test_get_peeled(self) -> None:
  246. # unpacked ref
  247. r = self.open_repo("a.git")
  248. tag_sha = b"28237f4dc30d0d462658d6b937b08a0f0b6ef55a"
  249. self.assertNotEqual(r[tag_sha].sha().hexdigest(), r.head())
  250. self.assertEqual(r.get_peeled(b"refs/tags/mytag"), r.head())
  251. # packed ref with cached peeled value
  252. packed_tag_sha = b"b0931cadc54336e78a1d980420e3268903b57a50"
  253. parent_sha = r[r.head()].parents[0]
  254. self.assertNotEqual(r[packed_tag_sha].sha().hexdigest(), parent_sha)
  255. self.assertEqual(r.get_peeled(b"refs/tags/mytag-packed"), parent_sha)
  256. # TODO: add more corner cases to test repo
  257. def test_get_peeled_not_tag(self) -> None:
  258. r = self.open_repo("a.git")
  259. self.assertEqual(r.get_peeled(b"HEAD"), r.head())
  260. def test_get_parents(self) -> None:
  261. r = self.open_repo("a.git")
  262. self.assertEqual(
  263. [b"2a72d929692c41d8554c07f6301757ba18a65d91"],
  264. r.get_parents(b"a90fa2d900a17e99b433217e988c4eb4a2e9a097"),
  265. )
  266. r.update_shallow([b"a90fa2d900a17e99b433217e988c4eb4a2e9a097"], None)
  267. self.assertEqual([], r.get_parents(b"a90fa2d900a17e99b433217e988c4eb4a2e9a097"))
  268. def test_get_walker(self) -> None:
  269. r = self.open_repo("a.git")
  270. # include defaults to [r.head()]
  271. self.assertEqual(
  272. [e.commit.id for e in r.get_walker()],
  273. [r.head(), b"2a72d929692c41d8554c07f6301757ba18a65d91"],
  274. )
  275. self.assertEqual(
  276. [
  277. e.commit.id
  278. for e in r.get_walker([b"2a72d929692c41d8554c07f6301757ba18a65d91"])
  279. ],
  280. [b"2a72d929692c41d8554c07f6301757ba18a65d91"],
  281. )
  282. self.assertEqual(
  283. [
  284. e.commit.id
  285. for e in r.get_walker(b"2a72d929692c41d8554c07f6301757ba18a65d91")
  286. ],
  287. [b"2a72d929692c41d8554c07f6301757ba18a65d91"],
  288. )
  289. def assertFilesystemHidden(self, path) -> None:
  290. if sys.platform != "win32":
  291. return
  292. import ctypes
  293. from ctypes.wintypes import DWORD, LPCWSTR
  294. GetFileAttributesW = ctypes.WINFUNCTYPE(DWORD, LPCWSTR)(
  295. ("GetFileAttributesW", ctypes.windll.kernel32)
  296. )
  297. self.assertTrue(2 & GetFileAttributesW(path))
  298. def test_init_existing(self) -> None:
  299. tmp_dir = self.mkdtemp()
  300. self.addCleanup(shutil.rmtree, tmp_dir)
  301. t = Repo.init(tmp_dir)
  302. self.addCleanup(t.close)
  303. self.assertEqual(os.listdir(tmp_dir), [".git"])
  304. self.assertFilesystemHidden(os.path.join(tmp_dir, ".git"))
  305. def test_init_mkdir(self) -> None:
  306. tmp_dir = self.mkdtemp()
  307. self.addCleanup(shutil.rmtree, tmp_dir)
  308. repo_dir = os.path.join(tmp_dir, "a-repo")
  309. t = Repo.init(repo_dir, mkdir=True)
  310. self.addCleanup(t.close)
  311. self.assertEqual(os.listdir(repo_dir), [".git"])
  312. self.assertFilesystemHidden(os.path.join(repo_dir, ".git"))
  313. def test_init_mkdir_unicode(self) -> None:
  314. repo_name = "\xa7"
  315. try:
  316. os.fsencode(repo_name)
  317. except UnicodeEncodeError:
  318. self.skipTest("filesystem lacks unicode support")
  319. tmp_dir = self.mkdtemp()
  320. self.addCleanup(shutil.rmtree, tmp_dir)
  321. repo_dir = os.path.join(tmp_dir, repo_name)
  322. t = Repo.init(repo_dir, mkdir=True)
  323. self.addCleanup(t.close)
  324. self.assertEqual(os.listdir(repo_dir), [".git"])
  325. self.assertFilesystemHidden(os.path.join(repo_dir, ".git"))
  326. def test_init_format(self) -> None:
  327. tmp_dir = self.mkdtemp()
  328. self.addCleanup(shutil.rmtree, tmp_dir)
  329. # Test format 0
  330. t0 = Repo.init(tmp_dir + "0", mkdir=True, format=0)
  331. self.addCleanup(t0.close)
  332. self.assertEqual(t0.get_config().get("core", "repositoryformatversion"), b"0")
  333. # Test format 1
  334. t1 = Repo.init(tmp_dir + "1", mkdir=True, format=1)
  335. self.addCleanup(t1.close)
  336. self.assertEqual(t1.get_config().get("core", "repositoryformatversion"), b"1")
  337. # Test default format
  338. td = Repo.init(tmp_dir + "d", mkdir=True)
  339. self.addCleanup(td.close)
  340. self.assertEqual(td.get_config().get("core", "repositoryformatversion"), b"0")
  341. # Test invalid format
  342. with self.assertRaises(ValueError):
  343. Repo.init(tmp_dir + "bad", mkdir=True, format=99)
  344. def test_init_bare_format(self) -> None:
  345. tmp_dir = self.mkdtemp()
  346. self.addCleanup(shutil.rmtree, tmp_dir)
  347. # Test format 1 for bare repo
  348. t = Repo.init_bare(tmp_dir + "bare", mkdir=True, format=1)
  349. self.addCleanup(t.close)
  350. self.assertEqual(t.get_config().get("core", "repositoryformatversion"), b"1")
  351. # Test invalid format for bare repo
  352. with self.assertRaises(ValueError):
  353. Repo.init_bare(tmp_dir + "badbr", mkdir=True, format=2)
  354. @skipIf(sys.platform == "win32", "fails on Windows")
  355. def test_fetch(self) -> None:
  356. r = self.open_repo("a.git")
  357. tmp_dir = self.mkdtemp()
  358. self.addCleanup(shutil.rmtree, tmp_dir)
  359. t = Repo.init(tmp_dir)
  360. self.addCleanup(t.close)
  361. r.fetch(t)
  362. self.assertIn(b"a90fa2d900a17e99b433217e988c4eb4a2e9a097", t)
  363. self.assertIn(b"a90fa2d900a17e99b433217e988c4eb4a2e9a097", t)
  364. self.assertIn(b"a90fa2d900a17e99b433217e988c4eb4a2e9a097", t)
  365. self.assertIn(b"28237f4dc30d0d462658d6b937b08a0f0b6ef55a", t)
  366. self.assertIn(b"b0931cadc54336e78a1d980420e3268903b57a50", t)
  367. @skipIf(sys.platform == "win32", "fails on Windows")
  368. def test_fetch_ignores_missing_refs(self) -> None:
  369. r = self.open_repo("a.git")
  370. missing = b"1234566789123456789123567891234657373833"
  371. r.refs[b"refs/heads/blah"] = missing
  372. tmp_dir = self.mkdtemp()
  373. self.addCleanup(shutil.rmtree, tmp_dir)
  374. t = Repo.init(tmp_dir)
  375. self.addCleanup(t.close)
  376. r.fetch(t)
  377. self.assertIn(b"a90fa2d900a17e99b433217e988c4eb4a2e9a097", t)
  378. self.assertIn(b"a90fa2d900a17e99b433217e988c4eb4a2e9a097", t)
  379. self.assertIn(b"a90fa2d900a17e99b433217e988c4eb4a2e9a097", t)
  380. self.assertIn(b"28237f4dc30d0d462658d6b937b08a0f0b6ef55a", t)
  381. self.assertIn(b"b0931cadc54336e78a1d980420e3268903b57a50", t)
  382. self.assertNotIn(missing, t)
  383. def test_clone(self) -> None:
  384. r = self.open_repo("a.git")
  385. tmp_dir = self.mkdtemp()
  386. self.addCleanup(shutil.rmtree, tmp_dir)
  387. with r.clone(tmp_dir, mkdir=False) as t:
  388. self.assertEqual(
  389. {
  390. b"HEAD": b"a90fa2d900a17e99b433217e988c4eb4a2e9a097",
  391. b"refs/remotes/origin/master": b"a90fa2d900a17e99b433217e988c4eb4a2e9a097",
  392. b"refs/remotes/origin/HEAD": b"a90fa2d900a17e99b433217e988c4eb4a2e9a097",
  393. b"refs/heads/master": b"a90fa2d900a17e99b433217e988c4eb4a2e9a097",
  394. b"refs/tags/mytag": b"28237f4dc30d0d462658d6b937b08a0f0b6ef55a",
  395. b"refs/tags/mytag-packed": b"b0931cadc54336e78a1d980420e3268903b57a50",
  396. },
  397. t.refs.as_dict(),
  398. )
  399. shas = [e.commit.id for e in r.get_walker()]
  400. self.assertEqual(
  401. shas, [t.head(), b"2a72d929692c41d8554c07f6301757ba18a65d91"]
  402. )
  403. c = t.get_config()
  404. encoded_path = r.path
  405. if not isinstance(encoded_path, bytes):
  406. encoded_path = os.fsencode(encoded_path)
  407. self.assertEqual(encoded_path, c.get((b"remote", b"origin"), b"url"))
  408. self.assertEqual(
  409. b"+refs/heads/*:refs/remotes/origin/*",
  410. c.get((b"remote", b"origin"), b"fetch"),
  411. )
  412. def test_clone_no_head(self) -> None:
  413. temp_dir = self.mkdtemp()
  414. self.addCleanup(shutil.rmtree, temp_dir)
  415. repo_dir = os.path.join(os.path.dirname(__file__), "..", "testdata", "repos")
  416. dest_dir = os.path.join(temp_dir, "a.git")
  417. shutil.copytree(os.path.join(repo_dir, "a.git"), dest_dir, symlinks=True)
  418. r = Repo(dest_dir)
  419. self.addCleanup(r.close)
  420. del r.refs[b"refs/heads/master"]
  421. del r.refs[b"HEAD"]
  422. t = r.clone(os.path.join(temp_dir, "b.git"), mkdir=True)
  423. self.addCleanup(t.close)
  424. self.assertEqual(
  425. {
  426. b"refs/tags/mytag": b"28237f4dc30d0d462658d6b937b08a0f0b6ef55a",
  427. b"refs/tags/mytag-packed": b"b0931cadc54336e78a1d980420e3268903b57a50",
  428. },
  429. t.refs.as_dict(),
  430. )
  431. def test_clone_empty(self) -> None:
  432. """Test clone() doesn't crash if HEAD points to a non-existing ref.
  433. This simulates cloning server-side bare repository either when it is
  434. still empty or if user renames master branch and pushes private repo
  435. to the server.
  436. Non-bare repo HEAD always points to an existing ref.
  437. """
  438. r = self.open_repo("empty.git")
  439. tmp_dir = self.mkdtemp()
  440. self.addCleanup(shutil.rmtree, tmp_dir)
  441. r.clone(tmp_dir, mkdir=False, bare=True)
  442. def test_reset_index_symlink_enabled(self) -> None:
  443. if sys.platform == "win32":
  444. self.skipTest("symlinks are not supported on Windows")
  445. tmp_dir = self.mkdtemp()
  446. self.addCleanup(shutil.rmtree, tmp_dir)
  447. o = Repo.init(os.path.join(tmp_dir, "s"), mkdir=True)
  448. os.symlink("foo", os.path.join(tmp_dir, "s", "bar"))
  449. o.stage("bar")
  450. o.do_commit(b"add symlink")
  451. t = o.clone(os.path.join(tmp_dir, "t"), symlinks=True)
  452. o.close()
  453. bar_path = os.path.join(tmp_dir, "t", "bar")
  454. if sys.platform == "win32":
  455. with open(bar_path) as f:
  456. self.assertEqual("foo", f.read())
  457. else:
  458. self.assertEqual("foo", os.readlink(bar_path))
  459. t.close()
  460. def test_reset_index_symlink_disabled(self) -> None:
  461. tmp_dir = self.mkdtemp()
  462. self.addCleanup(shutil.rmtree, tmp_dir)
  463. o = Repo.init(os.path.join(tmp_dir, "s"), mkdir=True)
  464. o.close()
  465. os.symlink("foo", os.path.join(tmp_dir, "s", "bar"))
  466. o.stage("bar")
  467. o.do_commit(b"add symlink")
  468. t = o.clone(os.path.join(tmp_dir, "t"), symlinks=False)
  469. with open(os.path.join(tmp_dir, "t", "bar")) as f:
  470. self.assertEqual("foo", f.read())
  471. t.close()
  472. def test_clone_bare(self) -> None:
  473. r = self.open_repo("a.git")
  474. tmp_dir = self.mkdtemp()
  475. self.addCleanup(shutil.rmtree, tmp_dir)
  476. t = r.clone(tmp_dir, mkdir=False)
  477. t.close()
  478. def test_clone_checkout_and_bare(self) -> None:
  479. r = self.open_repo("a.git")
  480. tmp_dir = self.mkdtemp()
  481. self.addCleanup(shutil.rmtree, tmp_dir)
  482. self.assertRaises(
  483. ValueError, r.clone, tmp_dir, mkdir=False, checkout=True, bare=True
  484. )
  485. def test_clone_branch(self) -> None:
  486. r = self.open_repo("a.git")
  487. r.refs[b"refs/heads/mybranch"] = b"28237f4dc30d0d462658d6b937b08a0f0b6ef55a"
  488. tmp_dir = self.mkdtemp()
  489. self.addCleanup(shutil.rmtree, tmp_dir)
  490. with r.clone(tmp_dir, mkdir=False, branch=b"mybranch") as t:
  491. # HEAD should point to specified branch and not origin HEAD
  492. chain, sha = t.refs.follow(b"HEAD")
  493. self.assertEqual(chain[-1], b"refs/heads/mybranch")
  494. self.assertEqual(sha, b"28237f4dc30d0d462658d6b937b08a0f0b6ef55a")
  495. self.assertEqual(
  496. t.refs[b"refs/remotes/origin/HEAD"],
  497. b"a90fa2d900a17e99b433217e988c4eb4a2e9a097",
  498. )
  499. def test_clone_tag(self) -> None:
  500. r = self.open_repo("a.git")
  501. tmp_dir = self.mkdtemp()
  502. self.addCleanup(shutil.rmtree, tmp_dir)
  503. with r.clone(tmp_dir, mkdir=False, branch=b"mytag") as t:
  504. # HEAD should be detached (and not a symbolic ref) at tag
  505. self.assertEqual(
  506. t.refs.read_ref(b"HEAD"),
  507. b"28237f4dc30d0d462658d6b937b08a0f0b6ef55a",
  508. )
  509. self.assertEqual(
  510. t.refs[b"refs/remotes/origin/HEAD"],
  511. b"a90fa2d900a17e99b433217e988c4eb4a2e9a097",
  512. )
  513. def test_clone_invalid_branch(self) -> None:
  514. r = self.open_repo("a.git")
  515. tmp_dir = self.mkdtemp()
  516. self.addCleanup(shutil.rmtree, tmp_dir)
  517. self.assertRaises(
  518. ValueError,
  519. r.clone,
  520. tmp_dir,
  521. mkdir=False,
  522. branch=b"mybranch",
  523. )
  524. def test_merge_history(self) -> None:
  525. r = self.open_repo("simple_merge.git")
  526. shas = [e.commit.id for e in r.get_walker()]
  527. self.assertEqual(
  528. shas,
  529. [
  530. b"5dac377bdded4c9aeb8dff595f0faeebcc8498cc",
  531. b"ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd",
  532. b"4cffe90e0a41ad3f5190079d7c8f036bde29cbe6",
  533. b"60dacdc733de308bb77bb76ce0fb0f9b44c9769e",
  534. b"0d89f20333fbb1d2f3a94da77f4981373d8f4310",
  535. ],
  536. )
  537. def test_out_of_order_merge(self) -> None:
  538. """Test that revision history is ordered by date, not parent order."""
  539. r = self.open_repo("ooo_merge.git")
  540. shas = [e.commit.id for e in r.get_walker()]
  541. self.assertEqual(
  542. shas,
  543. [
  544. b"7601d7f6231db6a57f7bbb79ee52e4d462fd44d1",
  545. b"f507291b64138b875c28e03469025b1ea20bc614",
  546. b"fb5b0425c7ce46959bec94d54b9a157645e114f5",
  547. b"f9e39b120c68182a4ba35349f832d0e4e61f485c",
  548. ],
  549. )
  550. def test_get_tags_empty(self) -> None:
  551. r = self.open_repo("ooo_merge.git")
  552. self.assertEqual({}, r.refs.as_dict(b"refs/tags"))
  553. def test_get_config(self) -> None:
  554. r = self.open_repo("ooo_merge.git")
  555. self.assertIsInstance(r.get_config(), Config)
  556. def test_get_config_stack(self) -> None:
  557. r = self.open_repo("ooo_merge.git")
  558. self.assertIsInstance(r.get_config_stack(), Config)
  559. def test_common_revisions(self) -> None:
  560. """This test demonstrates that ``find_common_revisions()`` actually
  561. returns common heads, not revisions; dulwich already uses
  562. ``find_common_revisions()`` in such a manner (see
  563. ``Repo.find_objects()``).
  564. """
  565. expected_shas = {b"60dacdc733de308bb77bb76ce0fb0f9b44c9769e"}
  566. # Source for objects.
  567. r_base = self.open_repo("simple_merge.git")
  568. # Re-create each-side of the merge in simple_merge.git.
  569. #
  570. # Since the trees and blobs are missing, the repository created is
  571. # corrupted, but we're only checking for commits for the purpose of
  572. # this test, so it's immaterial.
  573. r1_dir = self.mkdtemp()
  574. self.addCleanup(shutil.rmtree, r1_dir)
  575. r1_commits = [
  576. b"ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd", # HEAD
  577. b"60dacdc733de308bb77bb76ce0fb0f9b44c9769e",
  578. b"0d89f20333fbb1d2f3a94da77f4981373d8f4310",
  579. ]
  580. r2_dir = self.mkdtemp()
  581. self.addCleanup(shutil.rmtree, r2_dir)
  582. r2_commits = [
  583. b"4cffe90e0a41ad3f5190079d7c8f036bde29cbe6", # HEAD
  584. b"60dacdc733de308bb77bb76ce0fb0f9b44c9769e",
  585. b"0d89f20333fbb1d2f3a94da77f4981373d8f4310",
  586. ]
  587. r1 = Repo.init_bare(r1_dir)
  588. for c in r1_commits:
  589. r1.object_store.add_object(r_base.get_object(c))
  590. r1.refs[b"HEAD"] = r1_commits[0]
  591. r2 = Repo.init_bare(r2_dir)
  592. for c in r2_commits:
  593. r2.object_store.add_object(r_base.get_object(c))
  594. r2.refs[b"HEAD"] = r2_commits[0]
  595. # Finally, the 'real' testing!
  596. shas = r2.object_store.find_common_revisions(r1.get_graph_walker())
  597. self.assertEqual(set(shas), expected_shas)
  598. shas = r1.object_store.find_common_revisions(r2.get_graph_walker())
  599. self.assertEqual(set(shas), expected_shas)
  600. def test_shell_hook_pre_commit(self) -> None:
  601. if os.name != "posix":
  602. self.skipTest("shell hook tests requires POSIX shell")
  603. pre_commit_fail = """#!/bin/sh
  604. exit 1
  605. """
  606. pre_commit_success = """#!/bin/sh
  607. exit 0
  608. """
  609. repo_dir = os.path.join(self.mkdtemp())
  610. self.addCleanup(shutil.rmtree, repo_dir)
  611. r = Repo.init(repo_dir)
  612. self.addCleanup(r.close)
  613. pre_commit = os.path.join(r.controldir(), "hooks", "pre-commit")
  614. with open(pre_commit, "w") as f:
  615. f.write(pre_commit_fail)
  616. os.chmod(pre_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  617. self.assertRaises(
  618. errors.CommitError,
  619. r.do_commit,
  620. b"failed commit",
  621. committer=b"Test Committer <test@nodomain.com>",
  622. author=b"Test Author <test@nodomain.com>",
  623. commit_timestamp=12345,
  624. commit_timezone=0,
  625. author_timestamp=12345,
  626. author_timezone=0,
  627. )
  628. with open(pre_commit, "w") as f:
  629. f.write(pre_commit_success)
  630. os.chmod(pre_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  631. commit_sha = r.do_commit(
  632. b"empty commit",
  633. committer=b"Test Committer <test@nodomain.com>",
  634. author=b"Test Author <test@nodomain.com>",
  635. commit_timestamp=12395,
  636. commit_timezone=0,
  637. author_timestamp=12395,
  638. author_timezone=0,
  639. )
  640. self.assertEqual([], r[commit_sha].parents)
  641. def test_shell_hook_commit_msg(self) -> None:
  642. if os.name != "posix":
  643. self.skipTest("shell hook tests requires POSIX shell")
  644. commit_msg_fail = """#!/bin/sh
  645. exit 1
  646. """
  647. commit_msg_success = """#!/bin/sh
  648. exit 0
  649. """
  650. repo_dir = self.mkdtemp()
  651. self.addCleanup(shutil.rmtree, repo_dir)
  652. r = Repo.init(repo_dir)
  653. self.addCleanup(r.close)
  654. commit_msg = os.path.join(r.controldir(), "hooks", "commit-msg")
  655. with open(commit_msg, "w") as f:
  656. f.write(commit_msg_fail)
  657. os.chmod(commit_msg, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  658. self.assertRaises(
  659. errors.CommitError,
  660. r.do_commit,
  661. b"failed commit",
  662. committer=b"Test Committer <test@nodomain.com>",
  663. author=b"Test Author <test@nodomain.com>",
  664. commit_timestamp=12345,
  665. commit_timezone=0,
  666. author_timestamp=12345,
  667. author_timezone=0,
  668. )
  669. with open(commit_msg, "w") as f:
  670. f.write(commit_msg_success)
  671. os.chmod(commit_msg, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  672. commit_sha = r.do_commit(
  673. b"empty commit",
  674. committer=b"Test Committer <test@nodomain.com>",
  675. author=b"Test Author <test@nodomain.com>",
  676. commit_timestamp=12395,
  677. commit_timezone=0,
  678. author_timestamp=12395,
  679. author_timezone=0,
  680. )
  681. self.assertEqual([], r[commit_sha].parents)
  682. def test_shell_hook_pre_commit_add_files(self) -> None:
  683. if os.name != "posix":
  684. self.skipTest("shell hook tests requires POSIX shell")
  685. pre_commit_contents = """#!{executable}
  686. import sys
  687. sys.path.extend({path!r})
  688. from dulwich.repo import Repo
  689. with open('foo', 'w') as f:
  690. f.write('newfile')
  691. r = Repo('.')
  692. r.stage(['foo'])
  693. """.format(
  694. executable=sys.executable,
  695. path=[os.path.join(os.path.dirname(__file__), "..", ".."), *sys.path],
  696. )
  697. repo_dir = os.path.join(self.mkdtemp())
  698. self.addCleanup(shutil.rmtree, repo_dir)
  699. r = Repo.init(repo_dir)
  700. self.addCleanup(r.close)
  701. with open(os.path.join(repo_dir, "blah"), "w") as f:
  702. f.write("blah")
  703. r.stage(["blah"])
  704. pre_commit = os.path.join(r.controldir(), "hooks", "pre-commit")
  705. with open(pre_commit, "w") as f:
  706. f.write(pre_commit_contents)
  707. os.chmod(pre_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  708. commit_sha = r.do_commit(
  709. b"new commit",
  710. committer=b"Test Committer <test@nodomain.com>",
  711. author=b"Test Author <test@nodomain.com>",
  712. commit_timestamp=12395,
  713. commit_timezone=0,
  714. author_timestamp=12395,
  715. author_timezone=0,
  716. )
  717. self.assertEqual([], r[commit_sha].parents)
  718. tree = r[r[commit_sha].tree]
  719. self.assertEqual({b"blah", b"foo"}, set(tree))
  720. def test_shell_hook_post_commit(self) -> None:
  721. if os.name != "posix":
  722. self.skipTest("shell hook tests requires POSIX shell")
  723. repo_dir = self.mkdtemp()
  724. self.addCleanup(shutil.rmtree, repo_dir)
  725. r = Repo.init(repo_dir)
  726. self.addCleanup(r.close)
  727. (fd, path) = tempfile.mkstemp(dir=repo_dir)
  728. os.close(fd)
  729. post_commit_msg = (
  730. """#!/bin/sh
  731. rm """
  732. + path
  733. + """
  734. """
  735. )
  736. root_sha = r.do_commit(
  737. b"empty commit",
  738. committer=b"Test Committer <test@nodomain.com>",
  739. author=b"Test Author <test@nodomain.com>",
  740. commit_timestamp=12345,
  741. commit_timezone=0,
  742. author_timestamp=12345,
  743. author_timezone=0,
  744. )
  745. self.assertEqual([], r[root_sha].parents)
  746. post_commit = os.path.join(r.controldir(), "hooks", "post-commit")
  747. with open(post_commit, "wb") as f:
  748. f.write(post_commit_msg.encode(locale.getpreferredencoding()))
  749. os.chmod(post_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  750. commit_sha = r.do_commit(
  751. b"empty commit",
  752. committer=b"Test Committer <test@nodomain.com>",
  753. author=b"Test Author <test@nodomain.com>",
  754. commit_timestamp=12345,
  755. commit_timezone=0,
  756. author_timestamp=12345,
  757. author_timezone=0,
  758. )
  759. self.assertEqual([root_sha], r[commit_sha].parents)
  760. self.assertFalse(os.path.exists(path))
  761. post_commit_msg_fail = """#!/bin/sh
  762. exit 1
  763. """
  764. with open(post_commit, "w") as f:
  765. f.write(post_commit_msg_fail)
  766. os.chmod(post_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  767. warnings.simplefilter("always", UserWarning)
  768. self.addCleanup(warnings.resetwarnings)
  769. warnings_list, restore_warnings = setup_warning_catcher()
  770. self.addCleanup(restore_warnings)
  771. commit_sha2 = r.do_commit(
  772. b"empty commit",
  773. committer=b"Test Committer <test@nodomain.com>",
  774. author=b"Test Author <test@nodomain.com>",
  775. commit_timestamp=12345,
  776. commit_timezone=0,
  777. author_timestamp=12345,
  778. author_timezone=0,
  779. )
  780. expected_warning = UserWarning(
  781. "post-commit hook failed: Hook post-commit exited with non-zero status 1",
  782. )
  783. for w in warnings_list:
  784. if type(w) is type(expected_warning) and w.args == expected_warning.args:
  785. break
  786. else:
  787. raise AssertionError(
  788. f"Expected warning {expected_warning!r} not in {warnings_list!r}"
  789. )
  790. self.assertEqual([commit_sha], r[commit_sha2].parents)
  791. def test_as_dict(self) -> None:
  792. def check(repo) -> None:
  793. self.assertEqual(
  794. repo.refs.subkeys(b"refs/tags"),
  795. repo.refs.subkeys(b"refs/tags/"),
  796. )
  797. self.assertEqual(
  798. repo.refs.as_dict(b"refs/tags"),
  799. repo.refs.as_dict(b"refs/tags/"),
  800. )
  801. self.assertEqual(
  802. repo.refs.as_dict(b"refs/heads"),
  803. repo.refs.as_dict(b"refs/heads/"),
  804. )
  805. bare = self.open_repo("a.git")
  806. tmp_dir = self.mkdtemp()
  807. self.addCleanup(shutil.rmtree, tmp_dir)
  808. with bare.clone(tmp_dir, mkdir=False) as nonbare:
  809. check(nonbare)
  810. check(bare)
  811. def test_working_tree(self) -> None:
  812. temp_dir = tempfile.mkdtemp()
  813. self.addCleanup(shutil.rmtree, temp_dir)
  814. worktree_temp_dir = tempfile.mkdtemp()
  815. self.addCleanup(shutil.rmtree, worktree_temp_dir)
  816. r = Repo.init(temp_dir)
  817. self.addCleanup(r.close)
  818. root_sha = r.do_commit(
  819. b"empty commit",
  820. committer=b"Test Committer <test@nodomain.com>",
  821. author=b"Test Author <test@nodomain.com>",
  822. commit_timestamp=12345,
  823. commit_timezone=0,
  824. author_timestamp=12345,
  825. author_timezone=0,
  826. )
  827. r.refs[b"refs/heads/master"] = root_sha
  828. w = Repo._init_new_working_directory(worktree_temp_dir, r)
  829. self.addCleanup(w.close)
  830. new_sha = w.do_commit(
  831. b"new commit",
  832. committer=b"Test Committer <test@nodomain.com>",
  833. author=b"Test Author <test@nodomain.com>",
  834. commit_timestamp=12345,
  835. commit_timezone=0,
  836. author_timestamp=12345,
  837. author_timezone=0,
  838. )
  839. w.refs[b"HEAD"] = new_sha
  840. self.assertEqual(
  841. os.path.abspath(r.controldir()), os.path.abspath(w.commondir())
  842. )
  843. self.assertEqual(r.refs.keys(), w.refs.keys())
  844. self.assertNotEqual(r.head(), w.head())
  845. class BuildRepoRootTests(TestCase):
  846. """Tests that build on-disk repos from scratch.
  847. Repos live in a temp dir and are torn down after each test. They start with
  848. a single commit in master having single file named 'a'.
  849. """
  850. def get_repo_dir(self):
  851. return os.path.join(tempfile.mkdtemp(), "test")
  852. def setUp(self) -> None:
  853. super().setUp()
  854. self._repo_dir = self.get_repo_dir()
  855. os.makedirs(self._repo_dir)
  856. r = self._repo = Repo.init(self._repo_dir)
  857. self.addCleanup(tear_down_repo, r)
  858. self.assertFalse(r.bare)
  859. self.assertEqual(b"ref: refs/heads/master", r.refs.read_ref(b"HEAD"))
  860. self.assertRaises(KeyError, lambda: r.refs[b"refs/heads/master"])
  861. with open(os.path.join(r.path, "a"), "wb") as f:
  862. f.write(b"file contents")
  863. r.stage(["a"])
  864. commit_sha = r.do_commit(
  865. b"msg",
  866. committer=b"Test Committer <test@nodomain.com>",
  867. author=b"Test Author <test@nodomain.com>",
  868. commit_timestamp=12345,
  869. commit_timezone=0,
  870. author_timestamp=12345,
  871. author_timezone=0,
  872. )
  873. self.assertEqual([], r[commit_sha].parents)
  874. self._root_commit = commit_sha
  875. def test_get_shallow(self) -> None:
  876. self.assertEqual(set(), self._repo.get_shallow())
  877. with open(os.path.join(self._repo.path, ".git", "shallow"), "wb") as f:
  878. f.write(b"a90fa2d900a17e99b433217e988c4eb4a2e9a097\n")
  879. self.assertEqual(
  880. {b"a90fa2d900a17e99b433217e988c4eb4a2e9a097"},
  881. self._repo.get_shallow(),
  882. )
  883. def test_update_shallow(self) -> None:
  884. self._repo.update_shallow(None, None) # no op
  885. self.assertEqual(set(), self._repo.get_shallow())
  886. self._repo.update_shallow([b"a90fa2d900a17e99b433217e988c4eb4a2e9a097"], None)
  887. self.assertEqual(
  888. {b"a90fa2d900a17e99b433217e988c4eb4a2e9a097"},
  889. self._repo.get_shallow(),
  890. )
  891. self._repo.update_shallow(
  892. [b"a90fa2d900a17e99b433217e988c4eb4a2e9a097"],
  893. [b"f9e39b120c68182a4ba35349f832d0e4e61f485c"],
  894. )
  895. self.assertEqual(
  896. {b"a90fa2d900a17e99b433217e988c4eb4a2e9a097"},
  897. self._repo.get_shallow(),
  898. )
  899. self._repo.update_shallow(None, [b"a90fa2d900a17e99b433217e988c4eb4a2e9a097"])
  900. self.assertEqual(set(), self._repo.get_shallow())
  901. self.assertEqual(
  902. False,
  903. os.path.exists(os.path.join(self._repo.controldir(), "shallow")),
  904. )
  905. def test_build_repo(self) -> None:
  906. r = self._repo
  907. self.assertEqual(b"ref: refs/heads/master", r.refs.read_ref(b"HEAD"))
  908. self.assertEqual(self._root_commit, r.refs[b"refs/heads/master"])
  909. expected_blob = objects.Blob.from_string(b"file contents")
  910. self.assertEqual(expected_blob.data, r[expected_blob.id].data)
  911. actual_commit = r[self._root_commit]
  912. self.assertEqual(b"msg", actual_commit.message)
  913. def test_commit_modified(self) -> None:
  914. r = self._repo
  915. with open(os.path.join(r.path, "a"), "wb") as f:
  916. f.write(b"new contents")
  917. r.stage(["a"])
  918. commit_sha = r.do_commit(
  919. b"modified a",
  920. committer=b"Test Committer <test@nodomain.com>",
  921. author=b"Test Author <test@nodomain.com>",
  922. commit_timestamp=12395,
  923. commit_timezone=0,
  924. author_timestamp=12395,
  925. author_timezone=0,
  926. )
  927. self.assertEqual([self._root_commit], r[commit_sha].parents)
  928. a_mode, a_id = tree_lookup_path(r.get_object, r[commit_sha].tree, b"a")
  929. self.assertEqual(stat.S_IFREG | 0o644, a_mode)
  930. self.assertEqual(b"new contents", r[a_id].data)
  931. @skipIf(not getattr(os, "symlink", None), "Requires symlink support")
  932. def test_commit_symlink(self) -> None:
  933. r = self._repo
  934. os.symlink("a", os.path.join(r.path, "b"))
  935. r.stage(["a", "b"])
  936. commit_sha = r.do_commit(
  937. b"Symlink b",
  938. committer=b"Test Committer <test@nodomain.com>",
  939. author=b"Test Author <test@nodomain.com>",
  940. commit_timestamp=12395,
  941. commit_timezone=0,
  942. author_timestamp=12395,
  943. author_timezone=0,
  944. )
  945. self.assertEqual([self._root_commit], r[commit_sha].parents)
  946. b_mode, b_id = tree_lookup_path(r.get_object, r[commit_sha].tree, b"b")
  947. self.assertTrue(stat.S_ISLNK(b_mode))
  948. self.assertEqual(b"a", r[b_id].data)
  949. def test_commit_merge_heads_file(self) -> None:
  950. tmp_dir = tempfile.mkdtemp()
  951. self.addCleanup(shutil.rmtree, tmp_dir)
  952. r = Repo.init(tmp_dir)
  953. with open(os.path.join(r.path, "a"), "w") as f:
  954. f.write("initial text")
  955. c1 = r.do_commit(
  956. b"initial commit",
  957. committer=b"Test Committer <test@nodomain.com>",
  958. author=b"Test Author <test@nodomain.com>",
  959. commit_timestamp=12395,
  960. commit_timezone=0,
  961. author_timestamp=12395,
  962. author_timezone=0,
  963. )
  964. with open(os.path.join(r.path, "a"), "w") as f:
  965. f.write("merged text")
  966. with open(os.path.join(r.path, ".git", "MERGE_HEAD"), "w") as f:
  967. f.write("c27a2d21dd136312d7fa9e8baabb82561a1727d0\n")
  968. r.stage(["a"])
  969. commit_sha = r.do_commit(
  970. b"deleted a",
  971. committer=b"Test Committer <test@nodomain.com>",
  972. author=b"Test Author <test@nodomain.com>",
  973. commit_timestamp=12395,
  974. commit_timezone=0,
  975. author_timestamp=12395,
  976. author_timezone=0,
  977. )
  978. self.assertEqual(
  979. [c1, b"c27a2d21dd136312d7fa9e8baabb82561a1727d0"],
  980. r[commit_sha].parents,
  981. )
  982. def test_commit_deleted(self) -> None:
  983. r = self._repo
  984. os.remove(os.path.join(r.path, "a"))
  985. r.stage(["a"])
  986. commit_sha = r.do_commit(
  987. b"deleted a",
  988. committer=b"Test Committer <test@nodomain.com>",
  989. author=b"Test Author <test@nodomain.com>",
  990. commit_timestamp=12395,
  991. commit_timezone=0,
  992. author_timestamp=12395,
  993. author_timezone=0,
  994. )
  995. self.assertEqual([self._root_commit], r[commit_sha].parents)
  996. self.assertEqual([], list(r.open_index()))
  997. tree = r[r[commit_sha].tree]
  998. self.assertEqual([], list(tree.iteritems()))
  999. def test_commit_follows(self) -> None:
  1000. r = self._repo
  1001. r.refs.set_symbolic_ref(b"HEAD", b"refs/heads/bla")
  1002. commit_sha = r.do_commit(
  1003. b"commit with strange character",
  1004. committer=b"Test Committer <test@nodomain.com>",
  1005. author=b"Test Author <test@nodomain.com>",
  1006. commit_timestamp=12395,
  1007. commit_timezone=0,
  1008. author_timestamp=12395,
  1009. author_timezone=0,
  1010. ref=b"HEAD",
  1011. )
  1012. self.assertEqual(commit_sha, r[b"refs/heads/bla"].id)
  1013. def test_commit_encoding(self) -> None:
  1014. r = self._repo
  1015. commit_sha = r.do_commit(
  1016. b"commit with strange character \xee",
  1017. committer=b"Test Committer <test@nodomain.com>",
  1018. author=b"Test Author <test@nodomain.com>",
  1019. commit_timestamp=12395,
  1020. commit_timezone=0,
  1021. author_timestamp=12395,
  1022. author_timezone=0,
  1023. encoding=b"iso8859-1",
  1024. )
  1025. self.assertEqual(b"iso8859-1", r[commit_sha].encoding)
  1026. def test_compression_level(self) -> None:
  1027. r = self._repo
  1028. c = r.get_config()
  1029. c.set(("core",), "compression", "3")
  1030. c.set(("core",), "looseCompression", "4")
  1031. c.write_to_path()
  1032. r = Repo(self._repo_dir)
  1033. self.assertEqual(r.object_store.loose_compression_level, 4)
  1034. def test_repositoryformatversion_unsupported(self) -> None:
  1035. r = self._repo
  1036. c = r.get_config()
  1037. c.set(("core",), "repositoryformatversion", "2")
  1038. c.write_to_path()
  1039. self.assertRaises(UnsupportedVersion, Repo, self._repo_dir)
  1040. def test_repositoryformatversion_1(self) -> None:
  1041. r = self._repo
  1042. c = r.get_config()
  1043. c.set(("core",), "repositoryformatversion", "1")
  1044. c.write_to_path()
  1045. Repo(self._repo_dir)
  1046. def test_worktreeconfig_extension(self) -> None:
  1047. r = self._repo
  1048. c = r.get_config()
  1049. c.set(("core",), "repositoryformatversion", "1")
  1050. c.set(("extensions",), "worktreeconfig", True)
  1051. c.write_to_path()
  1052. c = r.get_worktree_config()
  1053. c.set(("user",), "repositoryformatversion", "1")
  1054. c.set((b"user",), b"name", b"Jelmer")
  1055. c.write_to_path()
  1056. cs = r.get_config_stack()
  1057. self.assertEqual(cs.get(("user",), "name"), b"Jelmer")
  1058. def test_worktreeconfig_extension_case(self) -> None:
  1059. """Test that worktree code does not error for alternate case format."""
  1060. r = self._repo
  1061. c = r.get_config()
  1062. c.set(("core",), "repositoryformatversion", "1")
  1063. # Capitalize "Config"
  1064. c.set(("extensions",), "worktreeConfig", True)
  1065. c.write_to_path()
  1066. c = r.get_worktree_config()
  1067. c.set(("user",), "repositoryformatversion", "1")
  1068. c.set((b"user",), b"name", b"Jelmer")
  1069. c.write_to_path()
  1070. # The following line errored before
  1071. # https://github.com/jelmer/dulwich/issues/1285 was addressed
  1072. Repo(self._repo_dir)
  1073. def test_repositoryformatversion_1_extension(self) -> None:
  1074. r = self._repo
  1075. c = r.get_config()
  1076. c.set(("core",), "repositoryformatversion", "1")
  1077. c.set(("extensions",), "unknownextension", True)
  1078. c.write_to_path()
  1079. self.assertRaises(UnsupportedExtension, Repo, self._repo_dir)
  1080. def test_commit_encoding_from_config(self) -> None:
  1081. r = self._repo
  1082. c = r.get_config()
  1083. c.set(("i18n",), "commitEncoding", "iso8859-1")
  1084. c.write_to_path()
  1085. commit_sha = r.do_commit(
  1086. b"commit with strange character \xee",
  1087. committer=b"Test Committer <test@nodomain.com>",
  1088. author=b"Test Author <test@nodomain.com>",
  1089. commit_timestamp=12395,
  1090. commit_timezone=0,
  1091. author_timestamp=12395,
  1092. author_timezone=0,
  1093. )
  1094. self.assertEqual(b"iso8859-1", r[commit_sha].encoding)
  1095. def test_commit_config_identity(self) -> None:
  1096. # commit falls back to the users' identity if it wasn't specified
  1097. r = self._repo
  1098. c = r.get_config()
  1099. c.set((b"user",), b"name", b"Jelmer")
  1100. c.set((b"user",), b"email", b"jelmer@apache.org")
  1101. c.write_to_path()
  1102. commit_sha = r.do_commit(b"message")
  1103. self.assertEqual(b"Jelmer <jelmer@apache.org>", r[commit_sha].author)
  1104. self.assertEqual(b"Jelmer <jelmer@apache.org>", r[commit_sha].committer)
  1105. def test_commit_config_identity_strips_than(self) -> None:
  1106. # commit falls back to the users' identity if it wasn't specified,
  1107. # and strips superfluous <>
  1108. r = self._repo
  1109. c = r.get_config()
  1110. c.set((b"user",), b"name", b"Jelmer")
  1111. c.set((b"user",), b"email", b"<jelmer@apache.org>")
  1112. c.write_to_path()
  1113. commit_sha = r.do_commit(b"message")
  1114. self.assertEqual(b"Jelmer <jelmer@apache.org>", r[commit_sha].author)
  1115. self.assertEqual(b"Jelmer <jelmer@apache.org>", r[commit_sha].committer)
  1116. def test_commit_config_identity_in_memoryrepo(self) -> None:
  1117. # commit falls back to the users' identity if it wasn't specified
  1118. r = MemoryRepo.init_bare([], {})
  1119. c = r.get_config()
  1120. c.set((b"user",), b"name", b"Jelmer")
  1121. c.set((b"user",), b"email", b"jelmer@apache.org")
  1122. commit_sha = r.do_commit(b"message", tree=objects.Tree().id)
  1123. self.assertEqual(b"Jelmer <jelmer@apache.org>", r[commit_sha].author)
  1124. self.assertEqual(b"Jelmer <jelmer@apache.org>", r[commit_sha].committer)
  1125. def test_commit_config_identity_from_env(self) -> None:
  1126. # commit falls back to the users' identity if it wasn't specified
  1127. self.overrideEnv("GIT_COMMITTER_NAME", "joe")
  1128. self.overrideEnv("GIT_COMMITTER_EMAIL", "joe@example.com")
  1129. r = self._repo
  1130. c = r.get_config()
  1131. c.set((b"user",), b"name", b"Jelmer")
  1132. c.set((b"user",), b"email", b"jelmer@apache.org")
  1133. c.write_to_path()
  1134. commit_sha = r.do_commit(b"message")
  1135. self.assertEqual(b"Jelmer <jelmer@apache.org>", r[commit_sha].author)
  1136. self.assertEqual(b"joe <joe@example.com>", r[commit_sha].committer)
  1137. def test_commit_fail_ref(self) -> None:
  1138. r = self._repo
  1139. def set_if_equals(name, old_ref, new_ref, **kwargs) -> bool:
  1140. return False
  1141. r.refs.set_if_equals = set_if_equals
  1142. def add_if_new(name, new_ref, **kwargs) -> None:
  1143. self.fail("Unexpected call to add_if_new")
  1144. r.refs.add_if_new = add_if_new
  1145. old_shas = set(r.object_store)
  1146. self.assertRaises(
  1147. errors.CommitError,
  1148. r.do_commit,
  1149. b"failed commit",
  1150. committer=b"Test Committer <test@nodomain.com>",
  1151. author=b"Test Author <test@nodomain.com>",
  1152. commit_timestamp=12345,
  1153. commit_timezone=0,
  1154. author_timestamp=12345,
  1155. author_timezone=0,
  1156. )
  1157. new_shas = set(r.object_store) - old_shas
  1158. self.assertEqual(1, len(new_shas))
  1159. # Check that the new commit (now garbage) was added.
  1160. new_commit = r[new_shas.pop()]
  1161. self.assertEqual(r[self._root_commit].tree, new_commit.tree)
  1162. self.assertEqual(b"failed commit", new_commit.message)
  1163. def test_commit_branch(self) -> None:
  1164. r = self._repo
  1165. commit_sha = r.do_commit(
  1166. b"commit to branch",
  1167. committer=b"Test Committer <test@nodomain.com>",
  1168. author=b"Test Author <test@nodomain.com>",
  1169. commit_timestamp=12395,
  1170. commit_timezone=0,
  1171. author_timestamp=12395,
  1172. author_timezone=0,
  1173. ref=b"refs/heads/new_branch",
  1174. )
  1175. self.assertEqual(self._root_commit, r[b"HEAD"].id)
  1176. self.assertEqual(commit_sha, r[b"refs/heads/new_branch"].id)
  1177. self.assertEqual([], r[commit_sha].parents)
  1178. self.assertIn(b"refs/heads/new_branch", r)
  1179. new_branch_head = commit_sha
  1180. commit_sha = r.do_commit(
  1181. b"commit to branch 2",
  1182. committer=b"Test Committer <test@nodomain.com>",
  1183. author=b"Test Author <test@nodomain.com>",
  1184. commit_timestamp=12395,
  1185. commit_timezone=0,
  1186. author_timestamp=12395,
  1187. author_timezone=0,
  1188. ref=b"refs/heads/new_branch",
  1189. )
  1190. self.assertEqual(self._root_commit, r[b"HEAD"].id)
  1191. self.assertEqual(commit_sha, r[b"refs/heads/new_branch"].id)
  1192. self.assertEqual([new_branch_head], r[commit_sha].parents)
  1193. def test_commit_merge_heads(self) -> None:
  1194. r = self._repo
  1195. merge_1 = r.do_commit(
  1196. b"commit to branch 2",
  1197. committer=b"Test Committer <test@nodomain.com>",
  1198. author=b"Test Author <test@nodomain.com>",
  1199. commit_timestamp=12395,
  1200. commit_timezone=0,
  1201. author_timestamp=12395,
  1202. author_timezone=0,
  1203. ref=b"refs/heads/new_branch",
  1204. )
  1205. commit_sha = r.do_commit(
  1206. b"commit with merge",
  1207. committer=b"Test Committer <test@nodomain.com>",
  1208. author=b"Test Author <test@nodomain.com>",
  1209. commit_timestamp=12395,
  1210. commit_timezone=0,
  1211. author_timestamp=12395,
  1212. author_timezone=0,
  1213. merge_heads=[merge_1],
  1214. )
  1215. self.assertEqual([self._root_commit, merge_1], r[commit_sha].parents)
  1216. def test_commit_dangling_commit(self) -> None:
  1217. r = self._repo
  1218. old_shas = set(r.object_store)
  1219. old_refs = r.get_refs()
  1220. commit_sha = r.do_commit(
  1221. b"commit with no ref",
  1222. committer=b"Test Committer <test@nodomain.com>",
  1223. author=b"Test Author <test@nodomain.com>",
  1224. commit_timestamp=12395,
  1225. commit_timezone=0,
  1226. author_timestamp=12395,
  1227. author_timezone=0,
  1228. ref=None,
  1229. )
  1230. new_shas = set(r.object_store) - old_shas
  1231. # New sha is added, but no new refs
  1232. self.assertEqual(1, len(new_shas))
  1233. new_commit = r[new_shas.pop()]
  1234. self.assertEqual(r[self._root_commit].tree, new_commit.tree)
  1235. self.assertEqual([], r[commit_sha].parents)
  1236. self.assertEqual(old_refs, r.get_refs())
  1237. def test_commit_dangling_commit_with_parents(self) -> None:
  1238. r = self._repo
  1239. old_shas = set(r.object_store)
  1240. old_refs = r.get_refs()
  1241. commit_sha = r.do_commit(
  1242. b"commit with no ref",
  1243. committer=b"Test Committer <test@nodomain.com>",
  1244. author=b"Test Author <test@nodomain.com>",
  1245. commit_timestamp=12395,
  1246. commit_timezone=0,
  1247. author_timestamp=12395,
  1248. author_timezone=0,
  1249. ref=None,
  1250. merge_heads=[self._root_commit],
  1251. )
  1252. new_shas = set(r.object_store) - old_shas
  1253. # New sha is added, but no new refs
  1254. self.assertEqual(1, len(new_shas))
  1255. new_commit = r[new_shas.pop()]
  1256. self.assertEqual(r[self._root_commit].tree, new_commit.tree)
  1257. self.assertEqual([self._root_commit], r[commit_sha].parents)
  1258. self.assertEqual(old_refs, r.get_refs())
  1259. def test_stage_absolute(self) -> None:
  1260. r = self._repo
  1261. os.remove(os.path.join(r.path, "a"))
  1262. self.assertRaises(ValueError, r.stage, [os.path.join(r.path, "a")])
  1263. def test_stage_deleted(self) -> None:
  1264. r = self._repo
  1265. os.remove(os.path.join(r.path, "a"))
  1266. r.stage(["a"])
  1267. r.stage(["a"]) # double-stage a deleted path
  1268. self.assertEqual([], list(r.open_index()))
  1269. def test_stage_directory(self) -> None:
  1270. r = self._repo
  1271. os.mkdir(os.path.join(r.path, "c"))
  1272. r.stage(["c"])
  1273. self.assertEqual([b"a"], list(r.open_index()))
  1274. def test_stage_submodule(self) -> None:
  1275. r = self._repo
  1276. s = Repo.init(os.path.join(r.path, "sub"), mkdir=True)
  1277. s.do_commit(b"message")
  1278. r.stage(["sub"])
  1279. self.assertEqual([b"a", b"sub"], list(r.open_index()))
  1280. def test_unstage_midify_file_with_dir(self) -> None:
  1281. os.mkdir(os.path.join(self._repo.path, "new_dir"))
  1282. full_path = os.path.join(self._repo.path, "new_dir", "foo")
  1283. with open(full_path, "w") as f:
  1284. f.write("hello")
  1285. porcelain.add(self._repo, paths=[full_path])
  1286. porcelain.commit(
  1287. self._repo,
  1288. message=b"unitest",
  1289. committer=b"Jane <jane@example.com>",
  1290. author=b"John <john@example.com>",
  1291. )
  1292. with open(full_path, "a") as f:
  1293. f.write("something new")
  1294. self._repo.unstage(["new_dir/foo"])
  1295. status = list(porcelain.status(self._repo))
  1296. self.assertEqual(
  1297. [{"add": [], "delete": [], "modify": []}, [b"new_dir/foo"], []], status
  1298. )
  1299. def test_unstage_while_no_commit(self) -> None:
  1300. file = "foo"
  1301. full_path = os.path.join(self._repo.path, file)
  1302. with open(full_path, "w") as f:
  1303. f.write("hello")
  1304. porcelain.add(self._repo, paths=[full_path])
  1305. self._repo.unstage([file])
  1306. status = list(porcelain.status(self._repo))
  1307. self.assertEqual([{"add": [], "delete": [], "modify": []}, [], ["foo"]], status)
  1308. def test_unstage_add_file(self) -> None:
  1309. file = "foo"
  1310. full_path = os.path.join(self._repo.path, file)
  1311. porcelain.commit(
  1312. self._repo,
  1313. message=b"unitest",
  1314. committer=b"Jane <jane@example.com>",
  1315. author=b"John <john@example.com>",
  1316. )
  1317. with open(full_path, "w") as f:
  1318. f.write("hello")
  1319. porcelain.add(self._repo, paths=[full_path])
  1320. self._repo.unstage([file])
  1321. status = list(porcelain.status(self._repo))
  1322. self.assertEqual([{"add": [], "delete": [], "modify": []}, [], ["foo"]], status)
  1323. def test_unstage_modify_file(self) -> None:
  1324. file = "foo"
  1325. full_path = os.path.join(self._repo.path, file)
  1326. with open(full_path, "w") as f:
  1327. f.write("hello")
  1328. porcelain.add(self._repo, paths=[full_path])
  1329. porcelain.commit(
  1330. self._repo,
  1331. message=b"unitest",
  1332. committer=b"Jane <jane@example.com>",
  1333. author=b"John <john@example.com>",
  1334. )
  1335. with open(full_path, "a") as f:
  1336. f.write("broken")
  1337. porcelain.add(self._repo, paths=[full_path])
  1338. self._repo.unstage([file])
  1339. status = list(porcelain.status(self._repo))
  1340. self.assertEqual(
  1341. [{"add": [], "delete": [], "modify": []}, [b"foo"], []], status
  1342. )
  1343. def test_unstage_remove_file(self) -> None:
  1344. file = "foo"
  1345. full_path = os.path.join(self._repo.path, file)
  1346. with open(full_path, "w") as f:
  1347. f.write("hello")
  1348. porcelain.add(self._repo, paths=[full_path])
  1349. porcelain.commit(
  1350. self._repo,
  1351. message=b"unitest",
  1352. committer=b"Jane <jane@example.com>",
  1353. author=b"John <john@example.com>",
  1354. )
  1355. os.remove(full_path)
  1356. self._repo.unstage([file])
  1357. status = list(porcelain.status(self._repo))
  1358. self.assertEqual(
  1359. [{"add": [], "delete": [], "modify": []}, [b"foo"], []], status
  1360. )
  1361. def test_reset_index(self) -> None:
  1362. r = self._repo
  1363. with open(os.path.join(r.path, "a"), "wb") as f:
  1364. f.write(b"changed")
  1365. with open(os.path.join(r.path, "b"), "wb") as f:
  1366. f.write(b"added")
  1367. r.stage(["a", "b"])
  1368. status = list(porcelain.status(self._repo))
  1369. self.assertEqual(
  1370. [{"add": [b"b"], "delete": [], "modify": [b"a"]}, [], []], status
  1371. )
  1372. r.reset_index()
  1373. status = list(porcelain.status(self._repo))
  1374. self.assertEqual([{"add": [], "delete": [], "modify": []}, [], ["b"]], status)
  1375. @skipIf(
  1376. sys.platform in ("win32", "darwin"),
  1377. "tries to implicitly decode as utf8",
  1378. )
  1379. def test_commit_no_encode_decode(self) -> None:
  1380. r = self._repo
  1381. repo_path_bytes = os.fsencode(r.path)
  1382. encodings = ("utf8", "latin1")
  1383. names = ["À".encode(encoding) for encoding in encodings]
  1384. for name, encoding in zip(names, encodings):
  1385. full_path = os.path.join(repo_path_bytes, name)
  1386. with open(full_path, "wb") as f:
  1387. f.write(encoding.encode("ascii"))
  1388. # These files are break tear_down_repo, so cleanup these files
  1389. # ourselves.
  1390. self.addCleanup(os.remove, full_path)
  1391. r.stage(names)
  1392. commit_sha = r.do_commit(
  1393. b"Files with different encodings",
  1394. committer=b"Test Committer <test@nodomain.com>",
  1395. author=b"Test Author <test@nodomain.com>",
  1396. commit_timestamp=12395,
  1397. commit_timezone=0,
  1398. author_timestamp=12395,
  1399. author_timezone=0,
  1400. ref=None,
  1401. merge_heads=[self._root_commit],
  1402. )
  1403. for name, encoding in zip(names, encodings):
  1404. mode, id = tree_lookup_path(r.get_object, r[commit_sha].tree, name)
  1405. self.assertEqual(stat.S_IFREG | 0o644, mode)
  1406. self.assertEqual(encoding.encode("ascii"), r[id].data)
  1407. def test_discover_intended(self) -> None:
  1408. path = os.path.join(self._repo_dir, "b/c")
  1409. r = Repo.discover(path)
  1410. self.assertEqual(r.head(), self._repo.head())
  1411. def test_discover_isrepo(self) -> None:
  1412. r = Repo.discover(self._repo_dir)
  1413. self.assertEqual(r.head(), self._repo.head())
  1414. def test_discover_notrepo(self) -> None:
  1415. with self.assertRaises(NotGitRepository):
  1416. Repo.discover("/")
  1417. class CheckUserIdentityTests(TestCase):
  1418. def test_valid(self) -> None:
  1419. check_user_identity(b"Me <me@example.com>")
  1420. def test_invalid(self) -> None:
  1421. self.assertRaises(InvalidUserIdentity, check_user_identity, b"No Email")
  1422. self.assertRaises(
  1423. InvalidUserIdentity, check_user_identity, b"Fullname <missing"
  1424. )
  1425. self.assertRaises(
  1426. InvalidUserIdentity, check_user_identity, b"Fullname missing>"
  1427. )
  1428. self.assertRaises(
  1429. InvalidUserIdentity, check_user_identity, b"Fullname >order<>"
  1430. )
  1431. self.assertRaises(
  1432. InvalidUserIdentity, check_user_identity, b"Contains\0null byte <>"
  1433. )
  1434. self.assertRaises(
  1435. InvalidUserIdentity, check_user_identity, b"Contains\nnewline byte <>"
  1436. )