2
0

test_objects.py 52 KB

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