test_objects.py 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532
  1. # test_objects.py -- tests for objects.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 git base objects."""
  22. # TODO: Round-trip parse-serialize-parse and serialize-parse-serialize tests.
  23. import datetime
  24. import os
  25. import stat
  26. from contextlib import contextmanager
  27. from io import BytesIO
  28. from itertools import permutations
  29. from dulwich.errors import ObjectFormatException
  30. from dulwich.objects import (
  31. MAX_TIME,
  32. Blob,
  33. Commit,
  34. ShaFile,
  35. Tag,
  36. Tree,
  37. TreeEntry,
  38. _parse_tree_py,
  39. _sorted_tree_items_py,
  40. check_hexsha,
  41. check_identity,
  42. format_timezone,
  43. hex_to_filename,
  44. hex_to_sha,
  45. key_entry,
  46. object_class,
  47. parse_timezone,
  48. pretty_format_tree_entry,
  49. sha_to_hex,
  50. )
  51. try:
  52. from dulwich.objects import _parse_tree_rs, _sorted_tree_items_rs
  53. except ImportError:
  54. _sorted_tree_items_rs = _parse_tree_rs = None
  55. from dulwich.tests.utils import (
  56. ext_functest_builder,
  57. functest_builder,
  58. make_commit,
  59. make_object,
  60. )
  61. from . import TestCase
  62. a_sha = b"6f670c0fb53f9463760b7295fbb814e965fb20c8"
  63. b_sha = b"2969be3e8ee1c0222396a5611407e4769f14e54b"
  64. c_sha = b"954a536f7819d40e6f637f849ee187dd10066349"
  65. tree_sha = b"70c190eb48fa8bbb50ddc692a17b44cb781af7f6"
  66. tag_sha = b"71033db03a03c6a36721efcf1968dd8f8e0cf023"
  67. class TestHexToSha(TestCase):
  68. def test_simple(self) -> None:
  69. self.assertEqual(b"\xab\xcd" * 10, hex_to_sha(b"abcd" * 10))
  70. def test_reverse(self) -> None:
  71. self.assertEqual(b"abcd" * 10, sha_to_hex(b"\xab\xcd" * 10))
  72. class BlobReadTests(TestCase):
  73. """Test decompression of blobs."""
  74. def get_sha_file(self, cls, base, sha):
  75. dir = os.path.join(os.path.dirname(__file__), "..", "testdata", base)
  76. return cls.from_path(hex_to_filename(dir, sha))
  77. def get_blob(self, sha):
  78. """Return the blob named sha from the test data dir."""
  79. return self.get_sha_file(Blob, "blobs", sha)
  80. def get_tree(self, sha):
  81. return self.get_sha_file(Tree, "trees", sha)
  82. def get_tag(self, sha):
  83. return self.get_sha_file(Tag, "tags", sha)
  84. def commit(self, sha):
  85. return self.get_sha_file(Commit, "commits", sha)
  86. def test_decompress_simple_blob(self) -> None:
  87. b = self.get_blob(a_sha)
  88. self.assertEqual(b.data, b"test 1\n")
  89. self.assertEqual(b.sha().hexdigest().encode("ascii"), a_sha)
  90. def test_hash(self) -> None:
  91. b = self.get_blob(a_sha)
  92. self.assertEqual(hash(b.id), hash(b))
  93. def test_parse_empty_blob_object(self) -> None:
  94. sha = b"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"
  95. b = self.get_blob(sha)
  96. self.assertEqual(b.data, b"")
  97. self.assertEqual(b.id, sha)
  98. self.assertEqual(b.sha().hexdigest().encode("ascii"), sha)
  99. def test_create_blob_from_string(self) -> None:
  100. string = b"test 2\n"
  101. b = Blob.from_string(string)
  102. self.assertEqual(b.data, string)
  103. self.assertEqual(b.sha().hexdigest().encode("ascii"), b_sha)
  104. def test_legacy_from_file(self) -> None:
  105. b1 = Blob.from_string(b"foo")
  106. b_raw = b1.as_legacy_object()
  107. b2 = b1.from_file(BytesIO(b_raw))
  108. self.assertEqual(b1, b2)
  109. def test_legacy_from_file_compression_level(self) -> None:
  110. b1 = Blob.from_string(b"foo")
  111. b_raw = b1.as_legacy_object(compression_level=6)
  112. b2 = b1.from_file(BytesIO(b_raw))
  113. self.assertEqual(b1, b2)
  114. def test_chunks(self) -> None:
  115. string = b"test 5\n"
  116. b = Blob.from_string(string)
  117. self.assertEqual([string], b.chunked)
  118. def test_splitlines(self) -> None:
  119. for case in [
  120. [],
  121. [b"foo\nbar\n"],
  122. [b"bl\na", b"blie"],
  123. [b"bl\na", b"blie", b"bloe\n"],
  124. [b"", b"bl\na", b"blie", b"bloe\n"],
  125. [b"", b"", b"", b"bla\n"],
  126. [b"", b"", b"", b"bla\n", b""],
  127. [b"bl", b"", b"a\naaa"],
  128. [b"a\naaa", b"a"],
  129. ]:
  130. b = Blob()
  131. b.chunked = case
  132. self.assertEqual(b.data.splitlines(True), b.splitlines())
  133. def test_set_chunks(self) -> None:
  134. b = Blob()
  135. b.chunked = [b"te", b"st", b" 5\n"]
  136. self.assertEqual(b"test 5\n", b.data)
  137. b.chunked = [b"te", b"st", b" 6\n"]
  138. self.assertEqual(b"test 6\n", b.as_raw_string())
  139. self.assertEqual(b"test 6\n", bytes(b))
  140. def test_parse_legacy_blob(self) -> None:
  141. string = b"test 3\n"
  142. b = self.get_blob(c_sha)
  143. self.assertEqual(b.data, string)
  144. self.assertEqual(b.sha().hexdigest().encode("ascii"), c_sha)
  145. def test_eq(self) -> None:
  146. blob1 = self.get_blob(a_sha)
  147. blob2 = self.get_blob(a_sha)
  148. self.assertEqual(blob1, blob2)
  149. def test_read_tree_from_file(self) -> None:
  150. t = self.get_tree(tree_sha)
  151. self.assertEqual(t.items()[0], (b"a", 33188, a_sha))
  152. self.assertEqual(t.items()[1], (b"b", 33188, b_sha))
  153. def test_read_tree_from_file_parse_count(self) -> None:
  154. old_deserialize = Tree._deserialize
  155. def reset_deserialize() -> None:
  156. Tree._deserialize = old_deserialize
  157. self.addCleanup(reset_deserialize)
  158. self.deserialize_count = 0
  159. def counting_deserialize(*args, **kwargs):
  160. self.deserialize_count += 1
  161. return old_deserialize(*args, **kwargs)
  162. Tree._deserialize = counting_deserialize
  163. t = self.get_tree(tree_sha)
  164. self.assertEqual(t.items()[0], (b"a", 33188, a_sha))
  165. self.assertEqual(t.items()[1], (b"b", 33188, b_sha))
  166. self.assertEqual(self.deserialize_count, 1)
  167. def test_read_tag_from_file(self) -> None:
  168. t = self.get_tag(tag_sha)
  169. self.assertEqual(
  170. t.object, (Commit, b"51b668fd5bf7061b7d6fa525f88803e6cfadaa51")
  171. )
  172. self.assertEqual(t.name, b"signed")
  173. self.assertEqual(t.tagger, b"Ali Sabil <ali.sabil@gmail.com>")
  174. self.assertEqual(t.tag_time, 1231203091)
  175. self.assertEqual(t.message, b"This is a signed tag\n")
  176. self.assertEqual(
  177. t.signature,
  178. b"-----BEGIN PGP SIGNATURE-----\n"
  179. b"Version: GnuPG v1.4.9 (GNU/Linux)\n"
  180. b"\n"
  181. b"iEYEABECAAYFAkliqx8ACgkQqSMmLy9u/"
  182. b"kcx5ACfakZ9NnPl02tOyYP6pkBoEkU1\n"
  183. b"5EcAn0UFgokaSvS371Ym/4W9iJj6vh3h\n"
  184. b"=ql7y\n"
  185. b"-----END PGP SIGNATURE-----\n",
  186. )
  187. self.assertEqual(t.raw_without_sig() + t.signature, bytes(t))
  188. def test_read_commit_from_file(self) -> None:
  189. sha = b"60dacdc733de308bb77bb76ce0fb0f9b44c9769e"
  190. c = self.commit(sha)
  191. self.assertEqual(c.tree, tree_sha)
  192. self.assertEqual(c.parents, [b"0d89f20333fbb1d2f3a94da77f4981373d8f4310"])
  193. self.assertEqual(c.author, b"James Westby <jw+debian@jameswestby.net>")
  194. self.assertEqual(c.committer, b"James Westby <jw+debian@jameswestby.net>")
  195. self.assertEqual(c.commit_time, 1174759230)
  196. self.assertEqual(c.commit_timezone, 0)
  197. self.assertEqual(c.author_timezone, 0)
  198. self.assertEqual(c.message, b"Test commit\n")
  199. def test_read_commit_no_parents(self) -> None:
  200. sha = b"0d89f20333fbb1d2f3a94da77f4981373d8f4310"
  201. c = self.commit(sha)
  202. self.assertEqual(c.tree, b"90182552c4a85a45ec2a835cadc3451bebdfe870")
  203. self.assertEqual(c.parents, [])
  204. self.assertEqual(c.author, b"James Westby <jw+debian@jameswestby.net>")
  205. self.assertEqual(c.committer, b"James Westby <jw+debian@jameswestby.net>")
  206. self.assertEqual(c.commit_time, 1174758034)
  207. self.assertEqual(c.commit_timezone, 0)
  208. self.assertEqual(c.author_timezone, 0)
  209. self.assertEqual(c.message, b"Test commit\n")
  210. def test_read_commit_two_parents(self) -> None:
  211. sha = b"5dac377bdded4c9aeb8dff595f0faeebcc8498cc"
  212. c = self.commit(sha)
  213. self.assertEqual(c.tree, b"d80c186a03f423a81b39df39dc87fd269736ca86")
  214. self.assertEqual(
  215. c.parents,
  216. [
  217. b"ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd",
  218. b"4cffe90e0a41ad3f5190079d7c8f036bde29cbe6",
  219. ],
  220. )
  221. self.assertEqual(c.author, b"James Westby <jw+debian@jameswestby.net>")
  222. self.assertEqual(c.committer, b"James Westby <jw+debian@jameswestby.net>")
  223. self.assertEqual(c.commit_time, 1174773719)
  224. self.assertEqual(c.commit_timezone, 0)
  225. self.assertEqual(c.author_timezone, 0)
  226. self.assertEqual(c.message, b"Merge ../b\n")
  227. def test_stub_sha(self) -> None:
  228. sha = b"5" * 40
  229. c = make_commit(id=sha, message=b"foo")
  230. self.assertIsInstance(c, Commit)
  231. self.assertEqual(sha, c.id)
  232. self.assertNotEqual(sha, c.sha())
  233. class ShaFileCheckTests(TestCase):
  234. def assertCheckFails(self, cls, data) -> None:
  235. obj = cls()
  236. def do_check() -> None:
  237. obj.set_raw_string(data)
  238. obj.check()
  239. self.assertRaises(ObjectFormatException, do_check)
  240. def assertCheckSucceeds(self, cls, data) -> None:
  241. obj = cls()
  242. obj.set_raw_string(data)
  243. self.assertEqual(None, obj.check())
  244. small_buffer_zlib_object = (
  245. b"\x48\x89\x15\xcc\x31\x0e\xc2\x30\x0c\x40\x51\xe6"
  246. b"\x9c\xc2\x3b\xaa\x64\x37\xc4\xc1\x12\x42\x5c\xc5"
  247. b"\x49\xac\x52\xd4\x92\xaa\x78\xe1\xf6\x94\xed\xeb"
  248. b"\x0d\xdf\x75\x02\xa2\x7c\xea\xe5\x65\xd5\x81\x8b"
  249. b"\x9a\x61\xba\xa0\xa9\x08\x36\xc9\x4c\x1a\xad\x88"
  250. b"\x16\xba\x46\xc4\xa8\x99\x6a\x64\xe1\xe0\xdf\xcd"
  251. b"\xa0\xf6\x75\x9d\x3d\xf8\xf1\xd0\x77\xdb\xfb\xdc"
  252. b"\x86\xa3\x87\xf1\x2f\x93\xed\x00\xb7\xc7\xd2\xab"
  253. b"\x2e\xcf\xfe\xf1\x3b\x50\xa4\x91\x53\x12\x24\x38"
  254. b"\x23\x21\x86\xf0\x03\x2f\x91\x24\x52"
  255. )
  256. class ShaFileTests(TestCase):
  257. def test_deflated_smaller_window_buffer(self) -> None:
  258. # zlib on some systems uses smaller buffers,
  259. # resulting in a different header.
  260. # See https://github.com/libgit2/libgit2/pull/464
  261. sf = ShaFile.from_file(BytesIO(small_buffer_zlib_object))
  262. self.assertEqual(sf.type_name, b"tag")
  263. self.assertEqual(sf.tagger, b" <@localhost>")
  264. class CommitSerializationTests(TestCase):
  265. def make_commit(self, **kwargs):
  266. attrs = {
  267. "tree": b"d80c186a03f423a81b39df39dc87fd269736ca86",
  268. "parents": [
  269. b"ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd",
  270. b"4cffe90e0a41ad3f5190079d7c8f036bde29cbe6",
  271. ],
  272. "author": b"James Westby <jw+debian@jameswestby.net>",
  273. "committer": b"James Westby <jw+debian@jameswestby.net>",
  274. "commit_time": 1174773719,
  275. "author_time": 1174773719,
  276. "commit_timezone": 0,
  277. "author_timezone": 0,
  278. "message": b"Merge ../b\n",
  279. }
  280. attrs.update(kwargs)
  281. return make_commit(**attrs)
  282. def test_encoding(self) -> None:
  283. c = self.make_commit(encoding=b"iso8859-1")
  284. self.assertIn(b"encoding iso8859-1\n", c.as_raw_string())
  285. def test_short_timestamp(self) -> None:
  286. c = self.make_commit(commit_time=30)
  287. c1 = Commit()
  288. c1.set_raw_string(c.as_raw_string())
  289. self.assertEqual(30, c1.commit_time)
  290. def test_full_tree(self) -> None:
  291. c = self.make_commit(commit_time=30)
  292. t = Tree()
  293. t.add(b"data-x", 0o644, Blob().id)
  294. c.tree = t
  295. c1 = Commit()
  296. c1.set_raw_string(c.as_raw_string())
  297. self.assertEqual(t.id, c1.tree)
  298. self.assertEqual(c.as_raw_string(), c1.as_raw_string())
  299. def test_raw_length(self) -> None:
  300. c = self.make_commit()
  301. self.assertEqual(len(c.as_raw_string()), c.raw_length())
  302. def test_simple(self) -> None:
  303. c = self.make_commit()
  304. self.assertEqual(c.id, b"5dac377bdded4c9aeb8dff595f0faeebcc8498cc")
  305. self.assertEqual(
  306. b"tree d80c186a03f423a81b39df39dc87fd269736ca86\n"
  307. b"parent ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd\n"
  308. b"parent 4cffe90e0a41ad3f5190079d7c8f036bde29cbe6\n"
  309. b"author James Westby <jw+debian@jameswestby.net> "
  310. b"1174773719 +0000\n"
  311. b"committer James Westby <jw+debian@jameswestby.net> "
  312. b"1174773719 +0000\n"
  313. b"\n"
  314. b"Merge ../b\n",
  315. c.as_raw_string(),
  316. )
  317. def test_timezone(self) -> None:
  318. c = self.make_commit(commit_timezone=(5 * 60))
  319. self.assertIn(b" +0005\n", c.as_raw_string())
  320. def test_neg_timezone(self) -> None:
  321. c = self.make_commit(commit_timezone=(-1 * 3600))
  322. self.assertIn(b" -0100\n", c.as_raw_string())
  323. def test_deserialize(self) -> None:
  324. c = self.make_commit()
  325. d = Commit()
  326. d._deserialize(c.as_raw_chunks())
  327. self.assertEqual(c, d)
  328. def test_serialize_gpgsig(self) -> None:
  329. gpgsig = b"""-----BEGIN PGP SIGNATURE-----
  330. Version: GnuPG v1
  331. iQIcBAABCgAGBQJULCdfAAoJEACAbyvXKaRXuKwP/RyP9PA49uAvu8tQVCC/uBa8
  332. vi975+xvO14R8Pp8k2nps7lSxCdtCd+xVT1VRHs0wNhOZo2YCVoU1HATkPejqSeV
  333. NScTHcxnk4/+bxyfk14xvJkNp7FlQ3npmBkA+lbV0Ubr33rvtIE5jiJPyz+SgWAg
  334. xdBG2TojV0squj00GoH/euK6aX7GgZtwdtpTv44haCQdSuPGDcI4TORqR6YSqvy3
  335. GPE+3ZqXPFFb+KILtimkxitdwB7CpwmNse2vE3rONSwTvi8nq3ZoQYNY73CQGkUy
  336. qoFU0pDtw87U3niFin1ZccDgH0bB6624sLViqrjcbYJeg815Htsu4rmzVaZADEVC
  337. XhIO4MThebusdk0AcNGjgpf3HRHk0DPMDDlIjm+Oao0cqovvF6VyYmcb0C+RmhJj
  338. dodLXMNmbqErwTk3zEkW0yZvNIYXH7m9SokPCZa4eeIM7be62X6h1mbt0/IU6Th+
  339. v18fS0iTMP/Viug5und+05C/v04kgDo0CPphAbXwWMnkE4B6Tl9sdyUYXtvQsL7x
  340. 0+WP1gL27ANqNZiI07Kz/BhbBAQI/+2TFT7oGr0AnFPQ5jHp+3GpUf6OKuT1wT3H
  341. ND189UFuRuubxb42vZhpcXRbqJVWnbECTKVUPsGZqat3enQUB63uM4i6/RdONDZA
  342. fDeF1m4qYs+cUXKNUZ03
  343. =X6RT
  344. -----END PGP SIGNATURE-----"""
  345. pre_sig = b"""\
  346. tree d80c186a03f423a81b39df39dc87fd269736ca86
  347. parent ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd
  348. parent 4cffe90e0a41ad3f5190079d7c8f036bde29cbe6
  349. author James Westby <jw+debian@jameswestby.net> 1174773719 +0000
  350. committer James Westby <jw+debian@jameswestby.net> 1174773719 +0000
  351. """
  352. git_sig = b"""\
  353. gpgsig -----BEGIN PGP SIGNATURE-----
  354. Version: GnuPG v1
  355. iQIcBAABCgAGBQJULCdfAAoJEACAbyvXKaRXuKwP/RyP9PA49uAvu8tQVCC/uBa8
  356. vi975+xvO14R8Pp8k2nps7lSxCdtCd+xVT1VRHs0wNhOZo2YCVoU1HATkPejqSeV
  357. NScTHcxnk4/+bxyfk14xvJkNp7FlQ3npmBkA+lbV0Ubr33rvtIE5jiJPyz+SgWAg
  358. xdBG2TojV0squj00GoH/euK6aX7GgZtwdtpTv44haCQdSuPGDcI4TORqR6YSqvy3
  359. GPE+3ZqXPFFb+KILtimkxitdwB7CpwmNse2vE3rONSwTvi8nq3ZoQYNY73CQGkUy
  360. qoFU0pDtw87U3niFin1ZccDgH0bB6624sLViqrjcbYJeg815Htsu4rmzVaZADEVC
  361. XhIO4MThebusdk0AcNGjgpf3HRHk0DPMDDlIjm+Oao0cqovvF6VyYmcb0C+RmhJj
  362. dodLXMNmbqErwTk3zEkW0yZvNIYXH7m9SokPCZa4eeIM7be62X6h1mbt0/IU6Th+
  363. v18fS0iTMP/Viug5und+05C/v04kgDo0CPphAbXwWMnkE4B6Tl9sdyUYXtvQsL7x
  364. 0+WP1gL27ANqNZiI07Kz/BhbBAQI/+2TFT7oGr0AnFPQ5jHp+3GpUf6OKuT1wT3H
  365. ND189UFuRuubxb42vZhpcXRbqJVWnbECTKVUPsGZqat3enQUB63uM4i6/RdONDZA
  366. fDeF1m4qYs+cUXKNUZ03
  367. =X6RT
  368. -----END PGP SIGNATURE-----
  369. """
  370. post_sig = b"""\
  371. Merge ../b
  372. """
  373. commit = self.make_commit(gpgsig=gpgsig)
  374. self.maxDiff = None
  375. self.assertEqual(pre_sig + git_sig + post_sig, commit.as_raw_string())
  376. self.assertEqual(pre_sig + post_sig, commit.raw_without_sig())
  377. self.assertEqual(gpgsig, commit.gpgsig)
  378. self.assertEqual(b"Merge ../b\n", commit.message)
  379. def test_serialize_mergetag(self) -> None:
  380. tag = make_object(
  381. Tag,
  382. object=(Commit, b"a38d6181ff27824c79fc7df825164a212eff6a3f"),
  383. object_type_name=b"commit",
  384. name=b"v2.6.22-rc7",
  385. tag_time=1183319674,
  386. tag_timezone=0,
  387. tagger=b"Linus Torvalds <torvalds@woody.linux-foundation.org>",
  388. message=default_message,
  389. )
  390. commit = self.make_commit(mergetag=[tag])
  391. self.assertEqual(
  392. b"""tree d80c186a03f423a81b39df39dc87fd269736ca86
  393. parent ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd
  394. parent 4cffe90e0a41ad3f5190079d7c8f036bde29cbe6
  395. author James Westby <jw+debian@jameswestby.net> 1174773719 +0000
  396. committer James Westby <jw+debian@jameswestby.net> 1174773719 +0000
  397. mergetag object a38d6181ff27824c79fc7df825164a212eff6a3f
  398. type commit
  399. tag v2.6.22-rc7
  400. tagger Linus Torvalds <torvalds@woody.linux-foundation.org> 1183319674 +0000
  401. Linux 2.6.22-rc7
  402. -----BEGIN PGP SIGNATURE-----
  403. Version: GnuPG v1.4.7 (GNU/Linux)
  404. iD8DBQBGiAaAF3YsRnbiHLsRAitMAKCiLboJkQECM/jpYsY3WPfvUgLXkACgg3ql
  405. OK2XeQOiEeXtT76rV4t2WR4=
  406. =ivrA
  407. -----END PGP SIGNATURE-----
  408. Merge ../b
  409. """,
  410. commit.as_raw_string(),
  411. )
  412. def test_serialize_mergetags(self) -> None:
  413. tag = make_object(
  414. Tag,
  415. object=(Commit, b"a38d6181ff27824c79fc7df825164a212eff6a3f"),
  416. object_type_name=b"commit",
  417. name=b"v2.6.22-rc7",
  418. tag_time=1183319674,
  419. tag_timezone=0,
  420. tagger=b"Linus Torvalds <torvalds@woody.linux-foundation.org>",
  421. message=default_message,
  422. )
  423. commit = self.make_commit(mergetag=[tag, tag])
  424. self.assertEqual(
  425. b"""tree d80c186a03f423a81b39df39dc87fd269736ca86
  426. parent ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd
  427. parent 4cffe90e0a41ad3f5190079d7c8f036bde29cbe6
  428. author James Westby <jw+debian@jameswestby.net> 1174773719 +0000
  429. committer James Westby <jw+debian@jameswestby.net> 1174773719 +0000
  430. mergetag object a38d6181ff27824c79fc7df825164a212eff6a3f
  431. type commit
  432. tag v2.6.22-rc7
  433. tagger Linus Torvalds <torvalds@woody.linux-foundation.org> 1183319674 +0000
  434. Linux 2.6.22-rc7
  435. -----BEGIN PGP SIGNATURE-----
  436. Version: GnuPG v1.4.7 (GNU/Linux)
  437. iD8DBQBGiAaAF3YsRnbiHLsRAitMAKCiLboJkQECM/jpYsY3WPfvUgLXkACgg3ql
  438. OK2XeQOiEeXtT76rV4t2WR4=
  439. =ivrA
  440. -----END PGP SIGNATURE-----
  441. mergetag object a38d6181ff27824c79fc7df825164a212eff6a3f
  442. type commit
  443. tag v2.6.22-rc7
  444. tagger Linus Torvalds <torvalds@woody.linux-foundation.org> 1183319674 +0000
  445. Linux 2.6.22-rc7
  446. -----BEGIN PGP SIGNATURE-----
  447. Version: GnuPG v1.4.7 (GNU/Linux)
  448. iD8DBQBGiAaAF3YsRnbiHLsRAitMAKCiLboJkQECM/jpYsY3WPfvUgLXkACgg3ql
  449. OK2XeQOiEeXtT76rV4t2WR4=
  450. =ivrA
  451. -----END PGP SIGNATURE-----
  452. Merge ../b
  453. """,
  454. commit.as_raw_string(),
  455. )
  456. def test_deserialize_mergetag(self) -> None:
  457. tag = make_object(
  458. Tag,
  459. object=(Commit, b"a38d6181ff27824c79fc7df825164a212eff6a3f"),
  460. object_type_name=b"commit",
  461. name=b"v2.6.22-rc7",
  462. tag_time=1183319674,
  463. tag_timezone=0,
  464. tagger=b"Linus Torvalds <torvalds@woody.linux-foundation.org>",
  465. message=default_message,
  466. )
  467. commit = self.make_commit(mergetag=[tag])
  468. d = Commit()
  469. d._deserialize(commit.as_raw_chunks())
  470. self.assertEqual(commit, d)
  471. def test_deserialize_mergetags(self) -> None:
  472. tag = make_object(
  473. Tag,
  474. object=(Commit, b"a38d6181ff27824c79fc7df825164a212eff6a3f"),
  475. object_type_name=b"commit",
  476. name=b"v2.6.22-rc7",
  477. tag_time=1183319674,
  478. tag_timezone=0,
  479. tagger=b"Linus Torvalds <torvalds@woody.linux-foundation.org>",
  480. message=default_message,
  481. )
  482. commit = self.make_commit(mergetag=[tag, tag])
  483. d = Commit()
  484. d._deserialize(commit.as_raw_chunks())
  485. self.assertEqual(commit, d)
  486. default_committer = b"James Westby <jw+debian@jameswestby.net> 1174773719 +0000"
  487. class CommitParseTests(ShaFileCheckTests):
  488. def make_commit_lines(
  489. self,
  490. tree=b"d80c186a03f423a81b39df39dc87fd269736ca86",
  491. parents=[
  492. b"ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd",
  493. b"4cffe90e0a41ad3f5190079d7c8f036bde29cbe6",
  494. ],
  495. author=default_committer,
  496. committer=default_committer,
  497. encoding=None,
  498. message=b"Merge ../b\n",
  499. extra=None,
  500. ):
  501. lines = []
  502. if tree is not None:
  503. lines.append(b"tree " + tree)
  504. if parents is not None:
  505. lines.extend(b"parent " + p for p in parents)
  506. if author is not None:
  507. lines.append(b"author " + author)
  508. if committer is not None:
  509. lines.append(b"committer " + committer)
  510. if encoding is not None:
  511. lines.append(b"encoding " + encoding)
  512. if extra is not None:
  513. for name, value in sorted(extra.items()):
  514. lines.append(name + b" " + value)
  515. lines.append(b"")
  516. if message is not None:
  517. lines.append(message)
  518. return lines
  519. def make_commit_text(self, **kwargs):
  520. return b"\n".join(self.make_commit_lines(**kwargs))
  521. def test_simple(self) -> None:
  522. c = Commit.from_string(self.make_commit_text())
  523. self.assertEqual(b"Merge ../b\n", c.message)
  524. self.assertEqual(b"James Westby <jw+debian@jameswestby.net>", c.author)
  525. self.assertEqual(b"James Westby <jw+debian@jameswestby.net>", c.committer)
  526. self.assertEqual(b"d80c186a03f423a81b39df39dc87fd269736ca86", c.tree)
  527. self.assertEqual(
  528. [
  529. b"ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd",
  530. b"4cffe90e0a41ad3f5190079d7c8f036bde29cbe6",
  531. ],
  532. c.parents,
  533. )
  534. expected_time = datetime.datetime(2007, 3, 24, 22, 1, 59)
  535. self.assertEqual(
  536. expected_time, datetime.datetime.utcfromtimestamp(c.commit_time)
  537. )
  538. self.assertEqual(0, c.commit_timezone)
  539. self.assertEqual(
  540. expected_time, datetime.datetime.utcfromtimestamp(c.author_time)
  541. )
  542. self.assertEqual(0, c.author_timezone)
  543. self.assertEqual(None, c.encoding)
  544. def test_custom(self) -> None:
  545. c = Commit.from_string(self.make_commit_text(extra={b"extra-field": b"data"}))
  546. self.assertEqual([(b"extra-field", b"data")], c._extra)
  547. def test_encoding(self) -> None:
  548. c = Commit.from_string(self.make_commit_text(encoding=b"UTF-8"))
  549. self.assertEqual(b"UTF-8", c.encoding)
  550. def test_check(self) -> None:
  551. self.assertCheckSucceeds(Commit, self.make_commit_text())
  552. self.assertCheckSucceeds(Commit, self.make_commit_text(parents=None))
  553. self.assertCheckSucceeds(Commit, self.make_commit_text(encoding=b"UTF-8"))
  554. self.assertCheckFails(Commit, self.make_commit_text(tree=b"xxx"))
  555. self.assertCheckFails(Commit, self.make_commit_text(parents=[a_sha, b"xxx"]))
  556. bad_committer = b"some guy without an email address 1174773719 +0000"
  557. self.assertCheckFails(Commit, self.make_commit_text(committer=bad_committer))
  558. self.assertCheckFails(Commit, self.make_commit_text(author=bad_committer))
  559. self.assertCheckFails(Commit, self.make_commit_text(author=None))
  560. self.assertCheckFails(Commit, self.make_commit_text(committer=None))
  561. self.assertCheckFails(
  562. Commit, self.make_commit_text(author=None, committer=None)
  563. )
  564. def test_check_duplicates(self) -> None:
  565. # duplicate each of the header fields
  566. for i in range(5):
  567. lines = self.make_commit_lines(parents=[a_sha], encoding=b"UTF-8")
  568. lines.insert(i, lines[i])
  569. text = b"\n".join(lines)
  570. if lines[i].startswith(b"parent"):
  571. # duplicate parents are ok for now
  572. self.assertCheckSucceeds(Commit, text)
  573. else:
  574. self.assertCheckFails(Commit, text)
  575. def test_check_order(self) -> None:
  576. lines = self.make_commit_lines(parents=[a_sha], encoding=b"UTF-8")
  577. headers = lines[:5]
  578. rest = lines[5:]
  579. # of all possible permutations, ensure only the original succeeds
  580. for perm in permutations(headers):
  581. perm = list(perm)
  582. text = b"\n".join(perm + rest)
  583. if perm == headers:
  584. self.assertCheckSucceeds(Commit, text)
  585. else:
  586. self.assertCheckFails(Commit, text)
  587. def test_check_commit_with_unparseable_time(self) -> None:
  588. identity_with_wrong_time = (
  589. b"Igor Sysoev <igor@sysoev.ru> 18446743887488505614+42707004"
  590. )
  591. # Those fail at reading time
  592. self.assertCheckFails(
  593. Commit,
  594. self.make_commit_text(
  595. author=default_committer, committer=identity_with_wrong_time
  596. ),
  597. )
  598. self.assertCheckFails(
  599. Commit,
  600. self.make_commit_text(
  601. author=identity_with_wrong_time, committer=default_committer
  602. ),
  603. )
  604. def test_check_commit_with_overflow_date(self) -> None:
  605. """Date with overflow should raise an ObjectFormatException when checked."""
  606. identity_with_wrong_time = (
  607. b"Igor Sysoev <igor@sysoev.ru> 18446743887488505614 +42707004"
  608. )
  609. commit0 = Commit.from_string(
  610. self.make_commit_text(
  611. author=identity_with_wrong_time, committer=default_committer
  612. )
  613. )
  614. commit1 = Commit.from_string(
  615. self.make_commit_text(
  616. author=default_committer, committer=identity_with_wrong_time
  617. )
  618. )
  619. # Those fails when triggering the check() method
  620. for commit in [commit0, commit1]:
  621. with self.assertRaises(ObjectFormatException):
  622. commit.check()
  623. def test_mangled_author_line(self) -> None:
  624. """Mangled author line should successfully parse."""
  625. author_line = (
  626. b'Karl MacMillan <kmacmill@redhat.com> <"Karl MacMillan '
  627. b'<kmacmill@redhat.com>"> 1197475547 -0500'
  628. )
  629. expected_identity = (
  630. b'Karl MacMillan <kmacmill@redhat.com> <"Karl MacMillan '
  631. b'<kmacmill@redhat.com>">'
  632. )
  633. commit = Commit.from_string(self.make_commit_text(author=author_line))
  634. # The commit parses properly
  635. self.assertEqual(commit.author, expected_identity)
  636. # But the check fails because the author identity is bogus
  637. with self.assertRaises(ObjectFormatException):
  638. commit.check()
  639. def test_parse_gpgsig(self) -> None:
  640. pre_sig = b"""tree aaff74984cccd156a469afa7d9ab10e4777beb24
  641. author Jelmer Vernooij <jelmer@samba.org> 1412179807 +0200
  642. committer Jelmer Vernooij <jelmer@samba.org> 1412179807 +0200
  643. """
  644. git_sig = b"""\
  645. gpgsig -----BEGIN PGP SIGNATURE-----
  646. Version: GnuPG v1
  647. iQIcBAABCgAGBQJULCdfAAoJEACAbyvXKaRXuKwP/RyP9PA49uAvu8tQVCC/uBa8
  648. vi975+xvO14R8Pp8k2nps7lSxCdtCd+xVT1VRHs0wNhOZo2YCVoU1HATkPejqSeV
  649. NScTHcxnk4/+bxyfk14xvJkNp7FlQ3npmBkA+lbV0Ubr33rvtIE5jiJPyz+SgWAg
  650. xdBG2TojV0squj00GoH/euK6aX7GgZtwdtpTv44haCQdSuPGDcI4TORqR6YSqvy3
  651. GPE+3ZqXPFFb+KILtimkxitdwB7CpwmNse2vE3rONSwTvi8nq3ZoQYNY73CQGkUy
  652. qoFU0pDtw87U3niFin1ZccDgH0bB6624sLViqrjcbYJeg815Htsu4rmzVaZADEVC
  653. XhIO4MThebusdk0AcNGjgpf3HRHk0DPMDDlIjm+Oao0cqovvF6VyYmcb0C+RmhJj
  654. dodLXMNmbqErwTk3zEkW0yZvNIYXH7m9SokPCZa4eeIM7be62X6h1mbt0/IU6Th+
  655. v18fS0iTMP/Viug5und+05C/v04kgDo0CPphAbXwWMnkE4B6Tl9sdyUYXtvQsL7x
  656. 0+WP1gL27ANqNZiI07Kz/BhbBAQI/+2TFT7oGr0AnFPQ5jHp+3GpUf6OKuT1wT3H
  657. ND189UFuRuubxb42vZhpcXRbqJVWnbECTKVUPsGZqat3enQUB63uM4i6/RdONDZA
  658. fDeF1m4qYs+cUXKNUZ03
  659. =X6RT
  660. -----END PGP SIGNATURE-----
  661. """
  662. post_sig = b"""\
  663. foo
  664. """
  665. c = Commit.from_string(pre_sig + git_sig + post_sig)
  666. self.assertEqual(pre_sig + post_sig, c.raw_without_sig())
  667. self.assertEqual(pre_sig + git_sig + post_sig, bytes(c))
  668. self.assertEqual(b"foo\n", c.message)
  669. self.assertEqual([], c._extra)
  670. self.assertEqual(
  671. b"""-----BEGIN PGP SIGNATURE-----
  672. Version: GnuPG v1
  673. iQIcBAABCgAGBQJULCdfAAoJEACAbyvXKaRXuKwP/RyP9PA49uAvu8tQVCC/uBa8
  674. vi975+xvO14R8Pp8k2nps7lSxCdtCd+xVT1VRHs0wNhOZo2YCVoU1HATkPejqSeV
  675. NScTHcxnk4/+bxyfk14xvJkNp7FlQ3npmBkA+lbV0Ubr33rvtIE5jiJPyz+SgWAg
  676. xdBG2TojV0squj00GoH/euK6aX7GgZtwdtpTv44haCQdSuPGDcI4TORqR6YSqvy3
  677. GPE+3ZqXPFFb+KILtimkxitdwB7CpwmNse2vE3rONSwTvi8nq3ZoQYNY73CQGkUy
  678. qoFU0pDtw87U3niFin1ZccDgH0bB6624sLViqrjcbYJeg815Htsu4rmzVaZADEVC
  679. XhIO4MThebusdk0AcNGjgpf3HRHk0DPMDDlIjm+Oao0cqovvF6VyYmcb0C+RmhJj
  680. dodLXMNmbqErwTk3zEkW0yZvNIYXH7m9SokPCZa4eeIM7be62X6h1mbt0/IU6Th+
  681. v18fS0iTMP/Viug5und+05C/v04kgDo0CPphAbXwWMnkE4B6Tl9sdyUYXtvQsL7x
  682. 0+WP1gL27ANqNZiI07Kz/BhbBAQI/+2TFT7oGr0AnFPQ5jHp+3GpUf6OKuT1wT3H
  683. ND189UFuRuubxb42vZhpcXRbqJVWnbECTKVUPsGZqat3enQUB63uM4i6/RdONDZA
  684. fDeF1m4qYs+cUXKNUZ03
  685. =X6RT
  686. -----END PGP SIGNATURE-----""",
  687. c.gpgsig,
  688. )
  689. def test_parse_header_trailing_newline(self) -> None:
  690. pre_sig = b"""\
  691. tree a7d6277f78d3ecd0230a1a5df6db00b1d9c521ac
  692. parent c09b6dec7a73760fbdb478383a3c926b18db8bbe
  693. author Neil Matatall <oreoshake@github.com> 1461964057 -1000
  694. committer Neil Matatall <oreoshake@github.com> 1461964057 -1000
  695. """
  696. git_sig = b"""\
  697. gpgsig -----BEGIN PGP SIGNATURE-----
  698. wsBcBAABCAAQBQJXI80ZCRA6pcNDcVZ70gAAarcIABs72xRX3FWeox349nh6ucJK
  699. CtwmBTusez2Zwmq895fQEbZK7jpaGO5TRO4OvjFxlRo0E08UFx3pxZHSpj6bsFeL
  700. hHsDXnCaotphLkbgKKRdGZo7tDqM84wuEDlh4MwNe7qlFC7bYLDyysc81ZX5lpMm
  701. 2MFF1TvjLAzSvkT7H1LPkuR3hSvfCYhikbPOUNnKOo0sYjeJeAJ/JdAVQ4mdJIM0
  702. gl3REp9+A+qBEpNQI7z94Pg5Bc5xenwuDh3SJgHvJV6zBWupWcdB3fAkVd4TPnEZ
  703. nHxksHfeNln9RKseIDcy4b2ATjhDNIJZARHNfr6oy4u3XPW4svRqtBsLoMiIeuI=
  704. =ms6q
  705. -----END PGP SIGNATURE-----
  706. """
  707. post_sig = b"""\
  708. 3.3.0 version bump and docs
  709. """
  710. gpgsig = b"""\
  711. -----BEGIN PGP SIGNATURE-----
  712. wsBcBAABCAAQBQJXI80ZCRA6pcNDcVZ70gAAarcIABs72xRX3FWeox349nh6ucJK
  713. CtwmBTusez2Zwmq895fQEbZK7jpaGO5TRO4OvjFxlRo0E08UFx3pxZHSpj6bsFeL
  714. hHsDXnCaotphLkbgKKRdGZo7tDqM84wuEDlh4MwNe7qlFC7bYLDyysc81ZX5lpMm
  715. 2MFF1TvjLAzSvkT7H1LPkuR3hSvfCYhikbPOUNnKOo0sYjeJeAJ/JdAVQ4mdJIM0
  716. gl3REp9+A+qBEpNQI7z94Pg5Bc5xenwuDh3SJgHvJV6zBWupWcdB3fAkVd4TPnEZ
  717. nHxksHfeNln9RKseIDcy4b2ATjhDNIJZARHNfr6oy4u3XPW4svRqtBsLoMiIeuI=
  718. =ms6q
  719. -----END PGP SIGNATURE-----\n"""
  720. c = Commit.from_string(pre_sig + git_sig + post_sig)
  721. self.assertEqual([], c._extra)
  722. self.assertEqual(pre_sig + git_sig + post_sig, c.as_raw_string())
  723. self.assertEqual(pre_sig + post_sig, c.raw_without_sig())
  724. self.assertEqual(gpgsig, c.gpgsig)
  725. self.assertEqual(b"3.3.0 version bump and docs\n", c.message)
  726. _TREE_ITEMS = {
  727. b"a-c": (0o100755, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
  728. b"a.c": (0o100755, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
  729. b"aoc": (0o100755, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
  730. b"a": (stat.S_IFDIR, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
  731. b"a/c": (stat.S_IFDIR, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
  732. }
  733. _SORTED_TREE_ITEMS = [
  734. TreeEntry(b"a-c", 0o100755, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
  735. TreeEntry(b"a.c", 0o100755, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
  736. TreeEntry(b"a", stat.S_IFDIR, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
  737. TreeEntry(b"a/c", stat.S_IFDIR, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
  738. TreeEntry(b"aoc", 0o100755, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
  739. ]
  740. _TREE_ITEMS_BUG_1325 = {
  741. b"dir": (stat.S_IFDIR | 0o644, b"5944b31ff85b415573d1a43eb942e2dea30ab8be"),
  742. b"dira": (0o100644, b"cf7a729ca69bfabd0995fc9b083e86a18215bd91"),
  743. }
  744. _SORTED_TREE_ITEMS_BUG_1325 = [
  745. TreeEntry(
  746. path=b"dir",
  747. mode=stat.S_IFDIR | 0o644,
  748. sha=b"5944b31ff85b415573d1a43eb942e2dea30ab8be",
  749. ),
  750. TreeEntry(
  751. path=b"dira", mode=0o100644, sha=b"cf7a729ca69bfabd0995fc9b083e86a18215bd91"
  752. ),
  753. ]
  754. class TreeTests(ShaFileCheckTests):
  755. def test_add(self) -> None:
  756. myhexsha = b"d80c186a03f423a81b39df39dc87fd269736ca86"
  757. x = Tree()
  758. x.add(b"myname", 0o100755, myhexsha)
  759. self.assertEqual(x[b"myname"], (0o100755, myhexsha))
  760. self.assertEqual(b"100755 myname\0" + hex_to_sha(myhexsha), x.as_raw_string())
  761. def test_simple(self) -> None:
  762. myhexsha = b"d80c186a03f423a81b39df39dc87fd269736ca86"
  763. x = Tree()
  764. x[b"myname"] = (0o100755, myhexsha)
  765. self.assertEqual(b"100755 myname\0" + hex_to_sha(myhexsha), x.as_raw_string())
  766. self.assertEqual(b"100755 myname\0" + hex_to_sha(myhexsha), bytes(x))
  767. def test_tree_update_id(self) -> None:
  768. x = Tree()
  769. x[b"a.c"] = (0o100755, b"d80c186a03f423a81b39df39dc87fd269736ca86")
  770. self.assertEqual(b"0c5c6bc2c081accfbc250331b19e43b904ab9cdd", x.id)
  771. x[b"a.b"] = (stat.S_IFDIR, b"d80c186a03f423a81b39df39dc87fd269736ca86")
  772. self.assertEqual(b"07bfcb5f3ada15bbebdfa3bbb8fd858a363925c8", x.id)
  773. def test_tree_iteritems_dir_sort(self) -> None:
  774. x = Tree()
  775. for name, item in _TREE_ITEMS.items():
  776. x[name] = item
  777. self.assertEqual(_SORTED_TREE_ITEMS, x.items())
  778. def test_tree_items_dir_sort(self) -> None:
  779. x = Tree()
  780. for name, item in _TREE_ITEMS.items():
  781. x[name] = item
  782. self.assertEqual(_SORTED_TREE_ITEMS, x.items())
  783. def _do_test_parse_tree(self, parse_tree) -> None:
  784. dir = os.path.join(os.path.dirname(__file__), "..", "testdata", "trees")
  785. o = Tree.from_path(hex_to_filename(dir, tree_sha))
  786. self.assertEqual(
  787. [(b"a", 0o100644, a_sha), (b"b", 0o100644, b_sha)],
  788. list(parse_tree(o.as_raw_string())),
  789. )
  790. # test a broken tree that has a leading 0 on the file mode
  791. broken_tree = b"0100644 foo\0" + hex_to_sha(a_sha)
  792. def eval_parse_tree(*args, **kwargs):
  793. return list(parse_tree(*args, **kwargs))
  794. self.assertEqual([(b"foo", 0o100644, a_sha)], eval_parse_tree(broken_tree))
  795. self.assertRaises(
  796. ObjectFormatException, eval_parse_tree, broken_tree, strict=True
  797. )
  798. test_parse_tree = functest_builder(_do_test_parse_tree, _parse_tree_py)
  799. test_parse_tree_extension = ext_functest_builder(
  800. _do_test_parse_tree, _parse_tree_rs
  801. )
  802. def _do_test_sorted_tree_items(self, sorted_tree_items) -> None:
  803. def do_sort(entries, name_order):
  804. return list(sorted_tree_items(entries, name_order))
  805. actual = do_sort(_TREE_ITEMS, False)
  806. self.assertEqual(_SORTED_TREE_ITEMS, actual)
  807. self.assertIsInstance(actual[0], TreeEntry)
  808. actual = do_sort(_TREE_ITEMS_BUG_1325, False)
  809. self.assertEqual(
  810. key_entry((b"a", (0o40644, b"cf7a729ca69bfabd0995fc9b083e86a18215bd91"))),
  811. b"a/",
  812. )
  813. self.assertEqual(_SORTED_TREE_ITEMS_BUG_1325, actual)
  814. self.assertIsInstance(actual[0], TreeEntry)
  815. # C/Python implementations may differ in specific error types, but
  816. # should all error on invalid inputs.
  817. # For example, the Rust implementation has stricter type checks, so may
  818. # raise TypeError where the Python implementation raises
  819. # AttributeError.
  820. errors = (TypeError, ValueError, AttributeError)
  821. self.assertRaises(errors, do_sort, b"foo", False)
  822. self.assertRaises(errors, do_sort, {b"foo": (1, 2, 3)}, False)
  823. myhexsha = b"d80c186a03f423a81b39df39dc87fd269736ca86"
  824. self.assertRaises(errors, do_sort, {b"foo": (b"xxx", myhexsha)}, False)
  825. self.assertRaises(errors, do_sort, {b"foo": (0o100755, 12345)}, False)
  826. test_sorted_tree_items = functest_builder(
  827. _do_test_sorted_tree_items, _sorted_tree_items_py
  828. )
  829. if _sorted_tree_items_rs is not None:
  830. assert _sorted_tree_items_rs != _sorted_tree_items_py
  831. test_sorted_tree_items_extension = ext_functest_builder(
  832. _do_test_sorted_tree_items, _sorted_tree_items_rs
  833. )
  834. def _do_test_sorted_tree_items_name_order(self, sorted_tree_items) -> None:
  835. self.assertEqual(
  836. [
  837. TreeEntry(
  838. b"a",
  839. stat.S_IFDIR,
  840. b"d80c186a03f423a81b39df39dc87fd269736ca86",
  841. ),
  842. TreeEntry(
  843. b"a-c",
  844. 0o100755,
  845. b"d80c186a03f423a81b39df39dc87fd269736ca86",
  846. ),
  847. TreeEntry(
  848. b"a.c",
  849. 0o100755,
  850. b"d80c186a03f423a81b39df39dc87fd269736ca86",
  851. ),
  852. TreeEntry(
  853. b"a/c",
  854. stat.S_IFDIR,
  855. b"d80c186a03f423a81b39df39dc87fd269736ca86",
  856. ),
  857. TreeEntry(
  858. b"aoc",
  859. 0o100755,
  860. b"d80c186a03f423a81b39df39dc87fd269736ca86",
  861. ),
  862. ],
  863. list(sorted_tree_items(_TREE_ITEMS, True)),
  864. )
  865. test_sorted_tree_items_name_order = functest_builder(
  866. _do_test_sorted_tree_items_name_order, _sorted_tree_items_py
  867. )
  868. if _sorted_tree_items_rs is not None:
  869. test_sorted_tree_items_name_order_extension = ext_functest_builder(
  870. _do_test_sorted_tree_items_name_order, _sorted_tree_items_rs
  871. )
  872. def test_check(self) -> None:
  873. t = Tree
  874. sha = hex_to_sha(a_sha)
  875. # filenames
  876. self.assertCheckSucceeds(t, b"100644 .a\0" + sha)
  877. self.assertCheckFails(t, b"100644 \0" + sha)
  878. self.assertCheckFails(t, b"100644 .\0" + sha)
  879. self.assertCheckFails(t, b"100644 a/a\0" + sha)
  880. self.assertCheckFails(t, b"100644 ..\0" + sha)
  881. self.assertCheckFails(t, b"100644 .git\0" + sha)
  882. # modes
  883. self.assertCheckSucceeds(t, b"100644 a\0" + sha)
  884. self.assertCheckSucceeds(t, b"100755 a\0" + sha)
  885. self.assertCheckSucceeds(t, b"160000 a\0" + sha)
  886. # TODO more whitelisted modes
  887. self.assertCheckFails(t, b"123456 a\0" + sha)
  888. self.assertCheckFails(t, b"123abc a\0" + sha)
  889. # should fail check, but parses ok
  890. self.assertCheckFails(t, b"0100644 foo\0" + sha)
  891. # shas
  892. self.assertCheckFails(t, b"100644 a\0" + (b"x" * 5))
  893. self.assertCheckFails(t, b"100644 a\0" + (b"x" * 18) + b"\0")
  894. self.assertCheckFails(t, b"100644 a\0" + (b"x" * 21) + b"\n100644 b\0" + sha)
  895. # ordering
  896. sha2 = hex_to_sha(b_sha)
  897. self.assertCheckSucceeds(t, b"100644 a\0" + sha + b"100644 b\0" + sha)
  898. self.assertCheckSucceeds(t, b"100644 a\0" + sha + b"100644 b\0" + sha2)
  899. self.assertCheckFails(t, b"100644 a\0" + sha + b"100755 a\0" + sha2)
  900. self.assertCheckFails(t, b"100644 b\0" + sha2 + b"100644 a\0" + sha)
  901. def test_iter(self) -> None:
  902. t = Tree()
  903. t[b"foo"] = (0o100644, a_sha)
  904. self.assertEqual({b"foo"}, set(t))
  905. class TagSerializeTests(TestCase):
  906. def test_serialize_simple(self) -> None:
  907. x = make_object(
  908. Tag,
  909. tagger=b"Jelmer Vernooij <jelmer@samba.org>",
  910. name=b"0.1",
  911. message=b"Tag 0.1",
  912. object=(Blob, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
  913. tag_time=423423423,
  914. tag_timezone=0,
  915. )
  916. self.assertEqual(
  917. (
  918. b"object d80c186a03f423a81b39df39dc87fd269736ca86\n"
  919. b"type blob\n"
  920. b"tag 0.1\n"
  921. b"tagger Jelmer Vernooij <jelmer@samba.org> "
  922. b"423423423 +0000\n"
  923. b"\n"
  924. b"Tag 0.1"
  925. ),
  926. x.as_raw_string(),
  927. )
  928. def test_serialize_none_message(self) -> None:
  929. x = make_object(
  930. Tag,
  931. tagger=b"Jelmer Vernooij <jelmer@samba.org>",
  932. name=b"0.1",
  933. message=None,
  934. object=(Blob, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
  935. tag_time=423423423,
  936. tag_timezone=0,
  937. )
  938. self.assertEqual(
  939. (
  940. b"object d80c186a03f423a81b39df39dc87fd269736ca86\n"
  941. b"type blob\n"
  942. b"tag 0.1\n"
  943. b"tagger Jelmer Vernooij <jelmer@samba.org> "
  944. b"423423423 +0000\n\n"
  945. ),
  946. x.as_raw_string(),
  947. )
  948. default_tagger = (
  949. b"Linus Torvalds <torvalds@woody.linux-foundation.org> " b"1183319674 -0700"
  950. )
  951. default_message = b"""Linux 2.6.22-rc7
  952. -----BEGIN PGP SIGNATURE-----
  953. Version: GnuPG v1.4.7 (GNU/Linux)
  954. iD8DBQBGiAaAF3YsRnbiHLsRAitMAKCiLboJkQECM/jpYsY3WPfvUgLXkACgg3ql
  955. OK2XeQOiEeXtT76rV4t2WR4=
  956. =ivrA
  957. -----END PGP SIGNATURE-----
  958. """
  959. class TagParseTests(ShaFileCheckTests):
  960. def make_tag_lines(
  961. self,
  962. object_sha=b"a38d6181ff27824c79fc7df825164a212eff6a3f",
  963. object_type_name=b"commit",
  964. name=b"v2.6.22-rc7",
  965. tagger=default_tagger,
  966. message=default_message,
  967. ):
  968. lines = []
  969. if object_sha is not None:
  970. lines.append(b"object " + object_sha)
  971. if object_type_name is not None:
  972. lines.append(b"type " + object_type_name)
  973. if name is not None:
  974. lines.append(b"tag " + name)
  975. if tagger is not None:
  976. lines.append(b"tagger " + tagger)
  977. if message is not None:
  978. lines.append(b"")
  979. lines.append(message)
  980. return lines
  981. def make_tag_text(self, **kwargs):
  982. return b"\n".join(self.make_tag_lines(**kwargs))
  983. def test_parse(self) -> None:
  984. x = Tag()
  985. x.set_raw_string(self.make_tag_text())
  986. self.assertEqual(
  987. b"Linus Torvalds <torvalds@woody.linux-foundation.org>", x.tagger
  988. )
  989. self.assertEqual(b"v2.6.22-rc7", x.name)
  990. object_type, object_sha = x.object
  991. self.assertEqual(b"a38d6181ff27824c79fc7df825164a212eff6a3f", object_sha)
  992. self.assertEqual(Commit, object_type)
  993. self.assertEqual(
  994. datetime.datetime.utcfromtimestamp(x.tag_time),
  995. datetime.datetime(2007, 7, 1, 19, 54, 34),
  996. )
  997. self.assertEqual(-25200, x.tag_timezone)
  998. def test_parse_no_tagger(self) -> None:
  999. x = Tag()
  1000. x.set_raw_string(self.make_tag_text(tagger=None))
  1001. self.assertEqual(None, x.tagger)
  1002. self.assertEqual(b"v2.6.22-rc7", x.name)
  1003. self.assertEqual(None, x.tag_time)
  1004. def test_parse_no_message(self) -> None:
  1005. x = Tag()
  1006. x.set_raw_string(self.make_tag_text(message=None))
  1007. self.assertEqual(None, x.message)
  1008. self.assertEqual(
  1009. b"Linus Torvalds <torvalds@woody.linux-foundation.org>", x.tagger
  1010. )
  1011. self.assertEqual(
  1012. datetime.datetime.utcfromtimestamp(x.tag_time),
  1013. datetime.datetime(2007, 7, 1, 19, 54, 34),
  1014. )
  1015. self.assertEqual(-25200, x.tag_timezone)
  1016. self.assertEqual(b"v2.6.22-rc7", x.name)
  1017. def test_check(self) -> None:
  1018. self.assertCheckSucceeds(Tag, self.make_tag_text())
  1019. self.assertCheckFails(Tag, self.make_tag_text(object_sha=None))
  1020. self.assertCheckFails(Tag, self.make_tag_text(object_type_name=None))
  1021. self.assertCheckFails(Tag, self.make_tag_text(name=None))
  1022. self.assertCheckFails(Tag, self.make_tag_text(name=b""))
  1023. self.assertCheckFails(Tag, self.make_tag_text(object_type_name=b"foobar"))
  1024. self.assertCheckFails(
  1025. Tag,
  1026. self.make_tag_text(
  1027. tagger=b"some guy without an email address 1183319674 -0700"
  1028. ),
  1029. )
  1030. self.assertCheckFails(
  1031. Tag,
  1032. self.make_tag_text(
  1033. tagger=(
  1034. b"Linus Torvalds <torvalds@woody.linux-foundation.org> "
  1035. b"Sun 7 Jul 2007 12:54:34 +0700"
  1036. )
  1037. ),
  1038. )
  1039. self.assertCheckFails(Tag, self.make_tag_text(object_sha=b"xxx"))
  1040. def test_check_tag_with_unparseable_field(self) -> None:
  1041. self.assertCheckFails(
  1042. Tag,
  1043. self.make_tag_text(
  1044. tagger=(
  1045. b"Linus Torvalds <torvalds@woody.linux-foundation.org> "
  1046. b"423423+0000"
  1047. )
  1048. ),
  1049. )
  1050. def test_check_tag_with_overflow_time(self) -> None:
  1051. """Date with overflow should raise an ObjectFormatException when checked."""
  1052. author = f"Some Dude <some@dude.org> {MAX_TIME + 1} +0000"
  1053. tag = Tag.from_string(self.make_tag_text(tagger=(author.encode())))
  1054. with self.assertRaises(ObjectFormatException):
  1055. tag.check()
  1056. def test_check_duplicates(self) -> None:
  1057. # duplicate each of the header fields
  1058. for i in range(4):
  1059. lines = self.make_tag_lines()
  1060. lines.insert(i, lines[i])
  1061. self.assertCheckFails(Tag, b"\n".join(lines))
  1062. def test_check_order(self) -> None:
  1063. lines = self.make_tag_lines()
  1064. headers = lines[:4]
  1065. rest = lines[4:]
  1066. # of all possible permutations, ensure only the original succeeds
  1067. for perm in permutations(headers):
  1068. perm = list(perm)
  1069. text = b"\n".join(perm + rest)
  1070. if perm == headers:
  1071. self.assertCheckSucceeds(Tag, text)
  1072. else:
  1073. self.assertCheckFails(Tag, text)
  1074. def test_tree_copy_after_update(self) -> None:
  1075. """Check Tree.id is correctly updated when the tree is copied after updated."""
  1076. shas = []
  1077. tree = Tree()
  1078. shas.append(tree.id)
  1079. tree.add(b"data", 0o644, Blob().id)
  1080. copied = tree.copy()
  1081. shas.append(tree.id)
  1082. shas.append(copied.id)
  1083. self.assertNotIn(shas[0], shas[1:])
  1084. self.assertEqual(shas[1], shas[2])
  1085. def test_tag_withough_sig(self) -> None:
  1086. x = Tag()
  1087. x.set_raw_string(self.make_tag_text())
  1088. self.assertEqual(bytes(x), x.raw_without_sig() + x.signature)
  1089. self.assertEqual(
  1090. b"""\
  1091. -----BEGIN PGP SIGNATURE-----
  1092. Version: GnuPG v1.4.7 (GNU/Linux)
  1093. iD8DBQBGiAaAF3YsRnbiHLsRAitMAKCiLboJkQECM/jpYsY3WPfvUgLXkACgg3ql
  1094. OK2XeQOiEeXtT76rV4t2WR4=
  1095. =ivrA
  1096. -----END PGP SIGNATURE-----
  1097. """,
  1098. x.signature,
  1099. )
  1100. class CheckTests(TestCase):
  1101. def test_check_hexsha(self) -> None:
  1102. check_hexsha(a_sha, "failed to check good sha")
  1103. self.assertRaises(
  1104. ObjectFormatException, check_hexsha, b"1" * 39, "sha too short"
  1105. )
  1106. self.assertRaises(
  1107. ObjectFormatException, check_hexsha, b"1" * 41, "sha too long"
  1108. )
  1109. self.assertRaises(
  1110. ObjectFormatException,
  1111. check_hexsha,
  1112. b"x" * 40,
  1113. "invalid characters",
  1114. )
  1115. def test_check_identity(self) -> None:
  1116. check_identity(
  1117. b"Dave Borowitz <dborowitz@google.com>",
  1118. "failed to check good identity",
  1119. )
  1120. check_identity(b" <dborowitz@google.com>", "failed to check good identity")
  1121. self.assertRaises(
  1122. ObjectFormatException,
  1123. check_identity,
  1124. b"<dborowitz@google.com>",
  1125. "no space before email",
  1126. )
  1127. self.assertRaises(
  1128. ObjectFormatException, check_identity, b"Dave Borowitz", "no email"
  1129. )
  1130. self.assertRaises(
  1131. ObjectFormatException,
  1132. check_identity,
  1133. b"Dave Borowitz <dborowitz",
  1134. "incomplete email",
  1135. )
  1136. self.assertRaises(
  1137. ObjectFormatException,
  1138. check_identity,
  1139. b"dborowitz@google.com>",
  1140. "incomplete email",
  1141. )
  1142. self.assertRaises(
  1143. ObjectFormatException,
  1144. check_identity,
  1145. b"Dave Borowitz <<dborowitz@google.com>",
  1146. "typo",
  1147. )
  1148. self.assertRaises(
  1149. ObjectFormatException,
  1150. check_identity,
  1151. b"Dave Borowitz <dborowitz@google.com>>",
  1152. "typo",
  1153. )
  1154. self.assertRaises(
  1155. ObjectFormatException,
  1156. check_identity,
  1157. b"Dave Borowitz <dborowitz@google.com>xxx",
  1158. "trailing characters",
  1159. )
  1160. self.assertRaises(
  1161. ObjectFormatException,
  1162. check_identity,
  1163. b"Dave Borowitz <dborowitz@google.com>xxx",
  1164. "trailing characters",
  1165. )
  1166. self.assertRaises(
  1167. ObjectFormatException,
  1168. check_identity,
  1169. b"Dave<Borowitz <dborowitz@google.com>",
  1170. "reserved byte in name",
  1171. )
  1172. self.assertRaises(
  1173. ObjectFormatException,
  1174. check_identity,
  1175. b"Dave>Borowitz <dborowitz@google.com>",
  1176. "reserved byte in name",
  1177. )
  1178. self.assertRaises(
  1179. ObjectFormatException,
  1180. check_identity,
  1181. b"Dave\0Borowitz <dborowitz@google.com>",
  1182. "null byte",
  1183. )
  1184. self.assertRaises(
  1185. ObjectFormatException,
  1186. check_identity,
  1187. b"Dave\nBorowitz <dborowitz@google.com>",
  1188. "newline byte",
  1189. )
  1190. class TimezoneTests(TestCase):
  1191. def test_parse_timezone_utc(self) -> None:
  1192. self.assertEqual((0, False), parse_timezone(b"+0000"))
  1193. def test_parse_timezone_utc_negative(self) -> None:
  1194. self.assertEqual((0, True), parse_timezone(b"-0000"))
  1195. def test_generate_timezone_utc(self) -> None:
  1196. self.assertEqual(b"+0000", format_timezone(0))
  1197. def test_generate_timezone_utc_negative(self) -> None:
  1198. self.assertEqual(b"-0000", format_timezone(0, True))
  1199. def test_parse_timezone_cet(self) -> None:
  1200. self.assertEqual((60 * 60, False), parse_timezone(b"+0100"))
  1201. def test_format_timezone_cet(self) -> None:
  1202. self.assertEqual(b"+0100", format_timezone(60 * 60))
  1203. def test_format_timezone_pdt(self) -> None:
  1204. self.assertEqual(b"-0400", format_timezone(-4 * 60 * 60))
  1205. def test_parse_timezone_pdt(self) -> None:
  1206. self.assertEqual((-4 * 60 * 60, False), parse_timezone(b"-0400"))
  1207. def test_format_timezone_pdt_half(self) -> None:
  1208. self.assertEqual(b"-0440", format_timezone(int(((-4 * 60) - 40) * 60)))
  1209. def test_format_timezone_double_negative(self) -> None:
  1210. self.assertEqual(b"--700", format_timezone(int((7 * 60) * 60), True))
  1211. def test_parse_timezone_pdt_half(self) -> None:
  1212. self.assertEqual((((-4 * 60) - 40) * 60, False), parse_timezone(b"-0440"))
  1213. def test_parse_timezone_double_negative(self) -> None:
  1214. self.assertEqual((int((7 * 60) * 60), False), parse_timezone(b"+700"))
  1215. self.assertEqual((int((7 * 60) * 60), True), parse_timezone(b"--700"))
  1216. class ShaFileCopyTests(TestCase):
  1217. def assert_copy(self, orig) -> None:
  1218. oclass = object_class(orig.type_num)
  1219. copy = orig.copy()
  1220. self.assertIsInstance(copy, oclass)
  1221. self.assertEqual(copy, orig)
  1222. self.assertIsNot(copy, orig)
  1223. def test_commit_copy(self) -> None:
  1224. attrs = {
  1225. "tree": b"d80c186a03f423a81b39df39dc87fd269736ca86",
  1226. "parents": [
  1227. b"ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd",
  1228. b"4cffe90e0a41ad3f5190079d7c8f036bde29cbe6",
  1229. ],
  1230. "author": b"James Westby <jw+debian@jameswestby.net>",
  1231. "committer": b"James Westby <jw+debian@jameswestby.net>",
  1232. "commit_time": 1174773719,
  1233. "author_time": 1174773719,
  1234. "commit_timezone": 0,
  1235. "author_timezone": 0,
  1236. "message": b"Merge ../b\n",
  1237. }
  1238. commit = make_commit(**attrs)
  1239. self.assert_copy(commit)
  1240. def test_blob_copy(self) -> None:
  1241. blob = make_object(Blob, data=b"i am a blob")
  1242. self.assert_copy(blob)
  1243. def test_tree_copy(self) -> None:
  1244. blob = make_object(Blob, data=b"i am a blob")
  1245. tree = Tree()
  1246. tree[b"blob"] = (stat.S_IFREG, blob.id)
  1247. self.assert_copy(tree)
  1248. def test_tag_copy(self) -> None:
  1249. tag = make_object(
  1250. Tag,
  1251. name=b"tag",
  1252. message=b"",
  1253. tagger=b"Tagger <test@example.com>",
  1254. tag_time=12345,
  1255. tag_timezone=0,
  1256. object=(Commit, b"0" * 40),
  1257. )
  1258. self.assert_copy(tag)
  1259. class ShaFileSerializeTests(TestCase):
  1260. """`ShaFile` objects only gets serialized once if they haven't changed."""
  1261. @contextmanager
  1262. def assert_serialization_on_change(
  1263. self, obj, needs_serialization_after_change=True
  1264. ):
  1265. old_id = obj.id
  1266. self.assertFalse(obj._needs_serialization)
  1267. yield obj
  1268. if needs_serialization_after_change:
  1269. self.assertTrue(obj._needs_serialization)
  1270. else:
  1271. self.assertFalse(obj._needs_serialization)
  1272. new_id = obj.id
  1273. self.assertFalse(obj._needs_serialization)
  1274. self.assertNotEqual(old_id, new_id)
  1275. def test_commit_serialize(self) -> None:
  1276. attrs = {
  1277. "tree": b"d80c186a03f423a81b39df39dc87fd269736ca86",
  1278. "parents": [
  1279. b"ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd",
  1280. b"4cffe90e0a41ad3f5190079d7c8f036bde29cbe6",
  1281. ],
  1282. "author": b"James Westby <jw+debian@jameswestby.net>",
  1283. "committer": b"James Westby <jw+debian@jameswestby.net>",
  1284. "commit_time": 1174773719,
  1285. "author_time": 1174773719,
  1286. "commit_timezone": 0,
  1287. "author_timezone": 0,
  1288. "message": b"Merge ../b\n",
  1289. }
  1290. commit = make_commit(**attrs)
  1291. with self.assert_serialization_on_change(commit):
  1292. commit.parents = [b"ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd"]
  1293. def test_blob_serialize(self) -> None:
  1294. blob = make_object(Blob, data=b"i am a blob")
  1295. with self.assert_serialization_on_change(
  1296. blob, needs_serialization_after_change=False
  1297. ):
  1298. blob.data = b"i am another blob"
  1299. def test_tree_serialize(self) -> None:
  1300. blob = make_object(Blob, data=b"i am a blob")
  1301. tree = Tree()
  1302. tree[b"blob"] = (stat.S_IFREG, blob.id)
  1303. with self.assert_serialization_on_change(tree):
  1304. tree[b"blob2"] = (stat.S_IFREG, blob.id)
  1305. def test_tag_serialize(self) -> None:
  1306. tag = make_object(
  1307. Tag,
  1308. name=b"tag",
  1309. message=b"",
  1310. tagger=b"Tagger <test@example.com>",
  1311. tag_time=12345,
  1312. tag_timezone=0,
  1313. object=(Commit, b"0" * 40),
  1314. )
  1315. with self.assert_serialization_on_change(tag):
  1316. tag.message = b"new message"
  1317. def test_tag_serialize_time_error(self) -> None:
  1318. with self.assertRaises(ObjectFormatException):
  1319. tag = make_object(
  1320. Tag,
  1321. name=b"tag",
  1322. message=b"some message",
  1323. tagger=b"Tagger <test@example.com> 1174773719+0000",
  1324. object=(Commit, b"0" * 40),
  1325. )
  1326. tag._deserialize(tag._serialize())
  1327. class PrettyFormatTreeEntryTests(TestCase):
  1328. def test_format(self) -> None:
  1329. self.assertEqual(
  1330. "40000 tree 40820c38cfb182ce6c8b261555410d8382a5918b\tfoo\n",
  1331. pretty_format_tree_entry(
  1332. b"foo", 0o40000, b"40820c38cfb182ce6c8b261555410d8382a5918b"
  1333. ),
  1334. )