test_patch.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. # test_patch.py -- tests for patch.py
  2. # Copyright (C) 2010 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. # General Public License as public by the Free Software Foundation; version 2.0
  6. # or (at your option) any later version. You can redistribute it and/or
  7. # modify it under the terms of either of these two licenses.
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # You should have received a copy of the licenses; if not, see
  16. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. # License, Version 2.0.
  19. #
  20. """Tests for patch.py."""
  21. from io import BytesIO, StringIO
  22. from dulwich.object_store import MemoryObjectStore
  23. from dulwich.objects import S_IFGITLINK, Blob, Commit, Tree
  24. from dulwich.patch import (
  25. get_summary,
  26. git_am_patch_split,
  27. write_blob_diff,
  28. write_commit_patch,
  29. write_object_diff,
  30. write_tree_diff,
  31. )
  32. from . import SkipTest, TestCase
  33. class WriteCommitPatchTests(TestCase):
  34. def test_simple_bytesio(self):
  35. f = BytesIO()
  36. c = Commit()
  37. c.committer = c.author = b"Jelmer <jelmer@samba.org>"
  38. c.commit_time = c.author_time = 1271350201
  39. c.commit_timezone = c.author_timezone = 0
  40. c.message = b"This is the first line\nAnd this is the second line.\n"
  41. c.tree = Tree().id
  42. write_commit_patch(f, c, b"CONTENTS", (1, 1), version="custom")
  43. f.seek(0)
  44. lines = f.readlines()
  45. self.assertTrue(
  46. lines[0].startswith(b"From 0b0d34d1b5b596c928adc9a727a4b9e03d025298")
  47. )
  48. self.assertEqual(lines[1], b"From: Jelmer <jelmer@samba.org>\n")
  49. self.assertTrue(lines[2].startswith(b"Date: "))
  50. self.assertEqual(
  51. [
  52. b"Subject: [PATCH 1/1] This is the first line\n",
  53. b"And this is the second line.\n",
  54. b"\n",
  55. b"\n",
  56. b"---\n",
  57. ],
  58. lines[3:8],
  59. )
  60. self.assertEqual([b"CONTENTS-- \n", b"custom\n"], lines[-2:])
  61. if len(lines) >= 12:
  62. # diffstat may not be present
  63. self.assertEqual(lines[8], b" 0 files changed\n")
  64. class ReadGitAmPatch(TestCase):
  65. def test_extract_string(self):
  66. text = b"""\
  67. From ff643aae102d8870cac88e8f007e70f58f3a7363 Mon Sep 17 00:00:00 2001
  68. From: Jelmer Vernooij <jelmer@samba.org>
  69. Date: Thu, 15 Apr 2010 15:40:28 +0200
  70. Subject: [PATCH 1/2] Remove executable bit from prey.ico (triggers a warning).
  71. ---
  72. pixmaps/prey.ico | Bin 9662 -> 9662 bytes
  73. 1 files changed, 0 insertions(+), 0 deletions(-)
  74. mode change 100755 => 100644 pixmaps/prey.ico
  75. --
  76. 1.7.0.4
  77. """
  78. c, diff, version = git_am_patch_split(StringIO(text.decode("utf-8")), "utf-8")
  79. self.assertEqual(b"Jelmer Vernooij <jelmer@samba.org>", c.committer)
  80. self.assertEqual(b"Jelmer Vernooij <jelmer@samba.org>", c.author)
  81. self.assertEqual(
  82. b"Remove executable bit from prey.ico " b"(triggers a warning).\n",
  83. c.message,
  84. )
  85. self.assertEqual(
  86. b""" pixmaps/prey.ico | Bin 9662 -> 9662 bytes
  87. 1 files changed, 0 insertions(+), 0 deletions(-)
  88. mode change 100755 => 100644 pixmaps/prey.ico
  89. """,
  90. diff,
  91. )
  92. self.assertEqual(b"1.7.0.4", version)
  93. def test_extract_bytes(self):
  94. text = b"""\
  95. From ff643aae102d8870cac88e8f007e70f58f3a7363 Mon Sep 17 00:00:00 2001
  96. From: Jelmer Vernooij <jelmer@samba.org>
  97. Date: Thu, 15 Apr 2010 15:40:28 +0200
  98. Subject: [PATCH 1/2] Remove executable bit from prey.ico (triggers a warning).
  99. ---
  100. pixmaps/prey.ico | Bin 9662 -> 9662 bytes
  101. 1 files changed, 0 insertions(+), 0 deletions(-)
  102. mode change 100755 => 100644 pixmaps/prey.ico
  103. --
  104. 1.7.0.4
  105. """
  106. c, diff, version = git_am_patch_split(BytesIO(text))
  107. self.assertEqual(b"Jelmer Vernooij <jelmer@samba.org>", c.committer)
  108. self.assertEqual(b"Jelmer Vernooij <jelmer@samba.org>", c.author)
  109. self.assertEqual(
  110. b"Remove executable bit from prey.ico " b"(triggers a warning).\n",
  111. c.message,
  112. )
  113. self.assertEqual(
  114. b""" pixmaps/prey.ico | Bin 9662 -> 9662 bytes
  115. 1 files changed, 0 insertions(+), 0 deletions(-)
  116. mode change 100755 => 100644 pixmaps/prey.ico
  117. """,
  118. diff,
  119. )
  120. self.assertEqual(b"1.7.0.4", version)
  121. def test_extract_spaces(self):
  122. text = b"""From ff643aae102d8870cac88e8f007e70f58f3a7363 Mon Sep 17 00:00:00 2001
  123. From: Jelmer Vernooij <jelmer@samba.org>
  124. Date: Thu, 15 Apr 2010 15:40:28 +0200
  125. Subject: [Dulwich-users] [PATCH] Added unit tests for
  126. dulwich.object_store.tree_lookup_path.
  127. * dulwich/tests/test_object_store.py
  128. (TreeLookupPathTests): This test case contains a few tests that ensure the
  129. tree_lookup_path function works as expected.
  130. ---
  131. pixmaps/prey.ico | Bin 9662 -> 9662 bytes
  132. 1 files changed, 0 insertions(+), 0 deletions(-)
  133. mode change 100755 => 100644 pixmaps/prey.ico
  134. --
  135. 1.7.0.4
  136. """
  137. c, diff, version = git_am_patch_split(BytesIO(text), "utf-8")
  138. self.assertEqual(
  139. b"""\
  140. Added unit tests for dulwich.object_store.tree_lookup_path.
  141. * dulwich/tests/test_object_store.py
  142. (TreeLookupPathTests): This test case contains a few tests that ensure the
  143. tree_lookup_path function works as expected.
  144. """,
  145. c.message,
  146. )
  147. def test_extract_pseudo_from_header(self):
  148. text = b"""From ff643aae102d8870cac88e8f007e70f58f3a7363 Mon Sep 17 00:00:00 2001
  149. From: Jelmer Vernooij <jelmer@samba.org>
  150. Date: Thu, 15 Apr 2010 15:40:28 +0200
  151. Subject: [Dulwich-users] [PATCH] Added unit tests for
  152. dulwich.object_store.tree_lookup_path.
  153. From: Jelmer Vernooij <jelmer@debian.org>
  154. * dulwich/tests/test_object_store.py
  155. (TreeLookupPathTests): This test case contains a few tests that ensure the
  156. tree_lookup_path function works as expected.
  157. ---
  158. pixmaps/prey.ico | Bin 9662 -> 9662 bytes
  159. 1 files changed, 0 insertions(+), 0 deletions(-)
  160. mode change 100755 => 100644 pixmaps/prey.ico
  161. --
  162. 1.7.0.4
  163. """
  164. c, diff, version = git_am_patch_split(BytesIO(text), "utf-8")
  165. self.assertEqual(b"Jelmer Vernooij <jelmer@debian.org>", c.author)
  166. self.assertEqual(
  167. b"""\
  168. Added unit tests for dulwich.object_store.tree_lookup_path.
  169. * dulwich/tests/test_object_store.py
  170. (TreeLookupPathTests): This test case contains a few tests that ensure the
  171. tree_lookup_path function works as expected.
  172. """,
  173. c.message,
  174. )
  175. def test_extract_no_version_tail(self):
  176. text = b"""\
  177. From ff643aae102d8870cac88e8f007e70f58f3a7363 Mon Sep 17 00:00:00 2001
  178. From: Jelmer Vernooij <jelmer@samba.org>
  179. Date: Thu, 15 Apr 2010 15:40:28 +0200
  180. Subject: [Dulwich-users] [PATCH] Added unit tests for
  181. dulwich.object_store.tree_lookup_path.
  182. From: Jelmer Vernooij <jelmer@debian.org>
  183. ---
  184. pixmaps/prey.ico | Bin 9662 -> 9662 bytes
  185. 1 files changed, 0 insertions(+), 0 deletions(-)
  186. mode change 100755 => 100644 pixmaps/prey.ico
  187. """
  188. c, diff, version = git_am_patch_split(BytesIO(text), "utf-8")
  189. self.assertEqual(None, version)
  190. def test_extract_mercurial(self):
  191. raise SkipTest(
  192. "git_am_patch_split doesn't handle Mercurial patches " "properly yet"
  193. )
  194. expected_diff = """\
  195. diff --git a/dulwich/tests/test_patch.py b/dulwich/tests/test_patch.py
  196. --- a/dulwich/tests/test_patch.py
  197. +++ b/dulwich/tests/test_patch.py
  198. @@ -158,7 +158,7 @@
  199. '''
  200. c, diff, version = git_am_patch_split(BytesIO(text))
  201. - self.assertIs(None, version)
  202. + self.assertEqual(None, version)
  203. class DiffTests(TestCase):
  204. """
  205. text = f"""\
  206. From dulwich-users-bounces+jelmer=samba.org@lists.launchpad.net \
  207. Mon Nov 29 00:58:18 2010
  208. Date: Sun, 28 Nov 2010 17:57:27 -0600
  209. From: Augie Fackler <durin42@gmail.com>
  210. To: dulwich-users <dulwich-users@lists.launchpad.net>
  211. Subject: [Dulwich-users] [PATCH] test_patch: fix tests on Python 2.6
  212. Content-Transfer-Encoding: 8bit
  213. Change-Id: I5e51313d4ae3a65c3f00c665002a7489121bb0d6
  214. {expected_diff}
  215. _______________________________________________
  216. Mailing list: https://launchpad.net/~dulwich-users
  217. Post to : dulwich-users@lists.launchpad.net
  218. Unsubscribe : https://launchpad.net/~dulwich-users
  219. More help : https://help.launchpad.net/ListHelp
  220. """
  221. c, diff, version = git_am_patch_split(BytesIO(text))
  222. self.assertEqual(expected_diff, diff)
  223. self.assertEqual(None, version)
  224. class DiffTests(TestCase):
  225. """Tests for write_blob_diff and write_tree_diff."""
  226. def test_blob_diff(self):
  227. f = BytesIO()
  228. write_blob_diff(
  229. f,
  230. (b"foo.txt", 0o644, Blob.from_string(b"old\nsame\n")),
  231. (b"bar.txt", 0o644, Blob.from_string(b"new\nsame\n")),
  232. )
  233. self.assertEqual(
  234. [
  235. b"diff --git a/foo.txt b/bar.txt",
  236. b"index 3b0f961..a116b51 644",
  237. b"--- a/foo.txt",
  238. b"+++ b/bar.txt",
  239. b"@@ -1,2 +1,2 @@",
  240. b"-old",
  241. b"+new",
  242. b" same",
  243. ],
  244. f.getvalue().splitlines(),
  245. )
  246. def test_blob_add(self):
  247. f = BytesIO()
  248. write_blob_diff(
  249. f,
  250. (None, None, None),
  251. (b"bar.txt", 0o644, Blob.from_string(b"new\nsame\n")),
  252. )
  253. self.assertEqual(
  254. [
  255. b"diff --git a/bar.txt b/bar.txt",
  256. b"new file mode 644",
  257. b"index 0000000..a116b51",
  258. b"--- /dev/null",
  259. b"+++ b/bar.txt",
  260. b"@@ -0,0 +1,2 @@",
  261. b"+new",
  262. b"+same",
  263. ],
  264. f.getvalue().splitlines(),
  265. )
  266. def test_blob_remove(self):
  267. f = BytesIO()
  268. write_blob_diff(
  269. f,
  270. (b"bar.txt", 0o644, Blob.from_string(b"new\nsame\n")),
  271. (None, None, None),
  272. )
  273. self.assertEqual(
  274. [
  275. b"diff --git a/bar.txt b/bar.txt",
  276. b"deleted file mode 644",
  277. b"index a116b51..0000000",
  278. b"--- a/bar.txt",
  279. b"+++ /dev/null",
  280. b"@@ -1,2 +0,0 @@",
  281. b"-new",
  282. b"-same",
  283. ],
  284. f.getvalue().splitlines(),
  285. )
  286. def test_tree_diff(self):
  287. f = BytesIO()
  288. store = MemoryObjectStore()
  289. added = Blob.from_string(b"add\n")
  290. removed = Blob.from_string(b"removed\n")
  291. changed1 = Blob.from_string(b"unchanged\nremoved\n")
  292. changed2 = Blob.from_string(b"unchanged\nadded\n")
  293. unchanged = Blob.from_string(b"unchanged\n")
  294. tree1 = Tree()
  295. tree1.add(b"removed.txt", 0o644, removed.id)
  296. tree1.add(b"changed.txt", 0o644, changed1.id)
  297. tree1.add(b"unchanged.txt", 0o644, changed1.id)
  298. tree2 = Tree()
  299. tree2.add(b"added.txt", 0o644, added.id)
  300. tree2.add(b"changed.txt", 0o644, changed2.id)
  301. tree2.add(b"unchanged.txt", 0o644, changed1.id)
  302. store.add_objects(
  303. [
  304. (o, None)
  305. for o in [
  306. tree1,
  307. tree2,
  308. added,
  309. removed,
  310. changed1,
  311. changed2,
  312. unchanged,
  313. ]
  314. ]
  315. )
  316. write_tree_diff(f, store, tree1.id, tree2.id)
  317. self.assertEqual(
  318. [
  319. b"diff --git a/added.txt b/added.txt",
  320. b"new file mode 644",
  321. b"index 0000000..76d4bb8",
  322. b"--- /dev/null",
  323. b"+++ b/added.txt",
  324. b"@@ -0,0 +1 @@",
  325. b"+add",
  326. b"diff --git a/changed.txt b/changed.txt",
  327. b"index bf84e48..1be2436 644",
  328. b"--- a/changed.txt",
  329. b"+++ b/changed.txt",
  330. b"@@ -1,2 +1,2 @@",
  331. b" unchanged",
  332. b"-removed",
  333. b"+added",
  334. b"diff --git a/removed.txt b/removed.txt",
  335. b"deleted file mode 644",
  336. b"index 2c3f0b3..0000000",
  337. b"--- a/removed.txt",
  338. b"+++ /dev/null",
  339. b"@@ -1 +0,0 @@",
  340. b"-removed",
  341. ],
  342. f.getvalue().splitlines(),
  343. )
  344. def test_tree_diff_submodule(self):
  345. f = BytesIO()
  346. store = MemoryObjectStore()
  347. tree1 = Tree()
  348. tree1.add(
  349. b"asubmodule",
  350. S_IFGITLINK,
  351. b"06d0bdd9e2e20377b3180e4986b14c8549b393e4",
  352. )
  353. tree2 = Tree()
  354. tree2.add(
  355. b"asubmodule",
  356. S_IFGITLINK,
  357. b"cc975646af69f279396d4d5e1379ac6af80ee637",
  358. )
  359. store.add_objects([(o, None) for o in [tree1, tree2]])
  360. write_tree_diff(f, store, tree1.id, tree2.id)
  361. self.assertEqual(
  362. [
  363. b"diff --git a/asubmodule b/asubmodule",
  364. b"index 06d0bdd..cc97564 160000",
  365. b"--- a/asubmodule",
  366. b"+++ b/asubmodule",
  367. b"@@ -1 +1 @@",
  368. b"-Subproject commit 06d0bdd9e2e20377b3180e4986b14c8549b393e4",
  369. b"+Subproject commit cc975646af69f279396d4d5e1379ac6af80ee637",
  370. ],
  371. f.getvalue().splitlines(),
  372. )
  373. def test_object_diff_blob(self):
  374. f = BytesIO()
  375. b1 = Blob.from_string(b"old\nsame\n")
  376. b2 = Blob.from_string(b"new\nsame\n")
  377. store = MemoryObjectStore()
  378. store.add_objects([(b1, None), (b2, None)])
  379. write_object_diff(
  380. f, store, (b"foo.txt", 0o644, b1.id), (b"bar.txt", 0o644, b2.id)
  381. )
  382. self.assertEqual(
  383. [
  384. b"diff --git a/foo.txt b/bar.txt",
  385. b"index 3b0f961..a116b51 644",
  386. b"--- a/foo.txt",
  387. b"+++ b/bar.txt",
  388. b"@@ -1,2 +1,2 @@",
  389. b"-old",
  390. b"+new",
  391. b" same",
  392. ],
  393. f.getvalue().splitlines(),
  394. )
  395. def test_object_diff_add_blob(self):
  396. f = BytesIO()
  397. store = MemoryObjectStore()
  398. b2 = Blob.from_string(b"new\nsame\n")
  399. store.add_object(b2)
  400. write_object_diff(f, store, (None, None, None), (b"bar.txt", 0o644, b2.id))
  401. self.assertEqual(
  402. [
  403. b"diff --git a/bar.txt b/bar.txt",
  404. b"new file mode 644",
  405. b"index 0000000..a116b51",
  406. b"--- /dev/null",
  407. b"+++ b/bar.txt",
  408. b"@@ -0,0 +1,2 @@",
  409. b"+new",
  410. b"+same",
  411. ],
  412. f.getvalue().splitlines(),
  413. )
  414. def test_object_diff_remove_blob(self):
  415. f = BytesIO()
  416. b1 = Blob.from_string(b"new\nsame\n")
  417. store = MemoryObjectStore()
  418. store.add_object(b1)
  419. write_object_diff(f, store, (b"bar.txt", 0o644, b1.id), (None, None, None))
  420. self.assertEqual(
  421. [
  422. b"diff --git a/bar.txt b/bar.txt",
  423. b"deleted file mode 644",
  424. b"index a116b51..0000000",
  425. b"--- a/bar.txt",
  426. b"+++ /dev/null",
  427. b"@@ -1,2 +0,0 @@",
  428. b"-new",
  429. b"-same",
  430. ],
  431. f.getvalue().splitlines(),
  432. )
  433. def test_object_diff_bin_blob_force(self):
  434. f = BytesIO()
  435. # Prepare two slightly different PNG headers
  436. b1 = Blob.from_string(
  437. b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a"
  438. b"\x00\x00\x00\x0d\x49\x48\x44\x52"
  439. b"\x00\x00\x01\xd5\x00\x00\x00\x9f"
  440. b"\x08\x04\x00\x00\x00\x05\x04\x8b"
  441. )
  442. b2 = Blob.from_string(
  443. b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a"
  444. b"\x00\x00\x00\x0d\x49\x48\x44\x52"
  445. b"\x00\x00\x01\xd5\x00\x00\x00\x9f"
  446. b"\x08\x03\x00\x00\x00\x98\xd3\xb3"
  447. )
  448. store = MemoryObjectStore()
  449. store.add_objects([(b1, None), (b2, None)])
  450. write_object_diff(
  451. f,
  452. store,
  453. (b"foo.png", 0o644, b1.id),
  454. (b"bar.png", 0o644, b2.id),
  455. diff_binary=True,
  456. )
  457. self.assertEqual(
  458. [
  459. b"diff --git a/foo.png b/bar.png",
  460. b"index f73e47d..06364b7 644",
  461. b"--- a/foo.png",
  462. b"+++ b/bar.png",
  463. b"@@ -1,4 +1,4 @@",
  464. b" \x89PNG",
  465. b" \x1a",
  466. b" \x00\x00\x00",
  467. b"-IHDR\x00\x00\x01\xd5\x00\x00\x00"
  468. b"\x9f\x08\x04\x00\x00\x00\x05\x04\x8b",
  469. b"\\ No newline at end of file",
  470. b"+IHDR\x00\x00\x01\xd5\x00\x00\x00\x9f"
  471. b"\x08\x03\x00\x00\x00\x98\xd3\xb3",
  472. b"\\ No newline at end of file",
  473. ],
  474. f.getvalue().splitlines(),
  475. )
  476. def test_object_diff_bin_blob(self):
  477. f = BytesIO()
  478. # Prepare two slightly different PNG headers
  479. b1 = Blob.from_string(
  480. b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a"
  481. b"\x00\x00\x00\x0d\x49\x48\x44\x52"
  482. b"\x00\x00\x01\xd5\x00\x00\x00\x9f"
  483. b"\x08\x04\x00\x00\x00\x05\x04\x8b"
  484. )
  485. b2 = Blob.from_string(
  486. b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a"
  487. b"\x00\x00\x00\x0d\x49\x48\x44\x52"
  488. b"\x00\x00\x01\xd5\x00\x00\x00\x9f"
  489. b"\x08\x03\x00\x00\x00\x98\xd3\xb3"
  490. )
  491. store = MemoryObjectStore()
  492. store.add_objects([(b1, None), (b2, None)])
  493. write_object_diff(
  494. f, store, (b"foo.png", 0o644, b1.id), (b"bar.png", 0o644, b2.id)
  495. )
  496. self.assertEqual(
  497. [
  498. b"diff --git a/foo.png b/bar.png",
  499. b"index f73e47d..06364b7 644",
  500. b"Binary files a/foo.png and b/bar.png differ",
  501. ],
  502. f.getvalue().splitlines(),
  503. )
  504. def test_object_diff_add_bin_blob(self):
  505. f = BytesIO()
  506. b2 = Blob.from_string(
  507. b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a"
  508. b"\x00\x00\x00\x0d\x49\x48\x44\x52"
  509. b"\x00\x00\x01\xd5\x00\x00\x00\x9f"
  510. b"\x08\x03\x00\x00\x00\x98\xd3\xb3"
  511. )
  512. store = MemoryObjectStore()
  513. store.add_object(b2)
  514. write_object_diff(f, store, (None, None, None), (b"bar.png", 0o644, b2.id))
  515. self.assertEqual(
  516. [
  517. b"diff --git a/bar.png b/bar.png",
  518. b"new file mode 644",
  519. b"index 0000000..06364b7",
  520. b"Binary files /dev/null and b/bar.png differ",
  521. ],
  522. f.getvalue().splitlines(),
  523. )
  524. def test_object_diff_remove_bin_blob(self):
  525. f = BytesIO()
  526. b1 = Blob.from_string(
  527. b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a"
  528. b"\x00\x00\x00\x0d\x49\x48\x44\x52"
  529. b"\x00\x00\x01\xd5\x00\x00\x00\x9f"
  530. b"\x08\x04\x00\x00\x00\x05\x04\x8b"
  531. )
  532. store = MemoryObjectStore()
  533. store.add_object(b1)
  534. write_object_diff(f, store, (b"foo.png", 0o644, b1.id), (None, None, None))
  535. self.assertEqual(
  536. [
  537. b"diff --git a/foo.png b/foo.png",
  538. b"deleted file mode 644",
  539. b"index f73e47d..0000000",
  540. b"Binary files a/foo.png and /dev/null differ",
  541. ],
  542. f.getvalue().splitlines(),
  543. )
  544. def test_object_diff_kind_change(self):
  545. f = BytesIO()
  546. b1 = Blob.from_string(b"new\nsame\n")
  547. store = MemoryObjectStore()
  548. store.add_object(b1)
  549. write_object_diff(
  550. f,
  551. store,
  552. (b"bar.txt", 0o644, b1.id),
  553. (
  554. b"bar.txt",
  555. 0o160000,
  556. b"06d0bdd9e2e20377b3180e4986b14c8549b393e4",
  557. ),
  558. )
  559. self.assertEqual(
  560. [
  561. b"diff --git a/bar.txt b/bar.txt",
  562. b"old file mode 644",
  563. b"new file mode 160000",
  564. b"index a116b51..06d0bdd 160000",
  565. b"--- a/bar.txt",
  566. b"+++ b/bar.txt",
  567. b"@@ -1,2 +1 @@",
  568. b"-new",
  569. b"-same",
  570. b"+Subproject commit 06d0bdd9e2e20377b3180e4986b14c8549b393e4",
  571. ],
  572. f.getvalue().splitlines(),
  573. )
  574. class GetSummaryTests(TestCase):
  575. def test_simple(self):
  576. c = Commit()
  577. c.committer = c.author = b"Jelmer <jelmer@samba.org>"
  578. c.commit_time = c.author_time = 1271350201
  579. c.commit_timezone = c.author_timezone = 0
  580. c.message = b"This is the first line\nAnd this is the second line.\n"
  581. c.tree = Tree().id
  582. self.assertEqual("This-is-the-first-line", get_summary(c))