File: test_patch.py

package info (click to toggle)
dulwich 0.24.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,392 kB
  • sloc: python: 87,517; makefile: 162; sh: 11
file content (904 lines) | stat: -rw-r--r-- 30,770 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
# test_patch.py -- tests for patch.py
# Copyright (C) 2010 Jelmer Vernooij <jelmer@jelmer.uk>
#
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
# Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
# General Public License as published by the Free Software Foundation; version 2.0
# or (at your option) any later version. You can redistribute it and/or
# modify it under the terms of either of these two licenses.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# You should have received a copy of the licenses; if not, see
# <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
# and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
# License, Version 2.0.
#

"""Tests for patch.py."""

from io import BytesIO, StringIO
from typing import NoReturn

from dulwich.object_store import MemoryObjectStore
from dulwich.objects import S_IFGITLINK, Blob, Commit, Tree
from dulwich.patch import (
    DiffAlgorithmNotAvailable,
    commit_patch_id,
    get_summary,
    git_am_patch_split,
    patch_id,
    unified_diff_with_algorithm,
    write_blob_diff,
    write_commit_patch,
    write_object_diff,
    write_tree_diff,
)
from dulwich.tests.utils import make_commit

from . import DependencyMissing, SkipTest, TestCase


class WriteCommitPatchTests(TestCase):
    def test_simple_bytesio(self) -> None:
        f = BytesIO()
        c = make_commit(
            author=b"Jelmer <jelmer@samba.org>",
            committer=b"Jelmer <jelmer@samba.org>",
            author_time=1271350201,
            commit_time=1271350201,
            author_timezone=0,
            commit_timezone=0,
            message=b"This is the first line\nAnd this is the second line.\n",
            tree=Tree().id,
        )
        write_commit_patch(f, c, b"CONTENTS", (1, 1), version="custom")
        f.seek(0)
        lines = f.readlines()
        self.assertTrue(
            lines[0].startswith(b"From 0b0d34d1b5b596c928adc9a727a4b9e03d025298")
        )
        self.assertEqual(lines[1], b"From: Jelmer <jelmer@samba.org>\n")
        self.assertTrue(lines[2].startswith(b"Date: "))
        self.assertEqual(
            [
                b"Subject: [PATCH 1/1] This is the first line\n",
                b"And this is the second line.\n",
                b"\n",
                b"\n",
                b"---\n",
            ],
            lines[3:8],
        )
        self.assertEqual([b"CONTENTS-- \n", b"custom\n"], lines[-2:])
        if len(lines) >= 12:
            # diffstat may not be present
            self.assertEqual(lines[8], b" 0 files changed\n")


class ReadGitAmPatch(TestCase):
    def test_extract_string(self) -> None:
        text = b"""\
From ff643aae102d8870cac88e8f007e70f58f3a7363 Mon Sep 17 00:00:00 2001
From: Jelmer Vernooij <jelmer@samba.org>
Date: Thu, 15 Apr 2010 15:40:28 +0200
Subject: [PATCH 1/2] Remove executable bit from prey.ico (triggers a warning).

---
 pixmaps/prey.ico |  Bin 9662 -> 9662 bytes
 1 files changed, 0 insertions(+), 0 deletions(-)
 mode change 100755 => 100644 pixmaps/prey.ico

-- 
1.7.0.4
"""
        c, diff, version = git_am_patch_split(StringIO(text.decode("utf-8")), "utf-8")
        self.assertEqual(b"Jelmer Vernooij <jelmer@samba.org>", c.committer)
        self.assertEqual(b"Jelmer Vernooij <jelmer@samba.org>", c.author)
        self.assertEqual(
            b"Remove executable bit from prey.ico (triggers a warning).\n",
            c.message,
        )
        self.assertEqual(
            b""" pixmaps/prey.ico |  Bin 9662 -> 9662 bytes
 1 files changed, 0 insertions(+), 0 deletions(-)
 mode change 100755 => 100644 pixmaps/prey.ico

""",
            diff,
        )
        self.assertEqual(b"1.7.0.4", version)

    def test_extract_bytes(self) -> None:
        text = b"""\
From ff643aae102d8870cac88e8f007e70f58f3a7363 Mon Sep 17 00:00:00 2001
From: Jelmer Vernooij <jelmer@samba.org>
Date: Thu, 15 Apr 2010 15:40:28 +0200
Subject: [PATCH 1/2] Remove executable bit from prey.ico (triggers a warning).

---
 pixmaps/prey.ico |  Bin 9662 -> 9662 bytes
 1 files changed, 0 insertions(+), 0 deletions(-)
 mode change 100755 => 100644 pixmaps/prey.ico

-- 
1.7.0.4
"""
        c, diff, version = git_am_patch_split(BytesIO(text))
        self.assertEqual(b"Jelmer Vernooij <jelmer@samba.org>", c.committer)
        self.assertEqual(b"Jelmer Vernooij <jelmer@samba.org>", c.author)
        self.assertEqual(
            b"Remove executable bit from prey.ico (triggers a warning).\n",
            c.message,
        )
        self.assertEqual(
            b""" pixmaps/prey.ico |  Bin 9662 -> 9662 bytes
 1 files changed, 0 insertions(+), 0 deletions(-)
 mode change 100755 => 100644 pixmaps/prey.ico

""",
            diff,
        )
        self.assertEqual(b"1.7.0.4", version)

    def test_extract_spaces(self) -> None:
        text = b"""From ff643aae102d8870cac88e8f007e70f58f3a7363 Mon Sep 17 00:00:00 2001
From: Jelmer Vernooij <jelmer@samba.org>
Date: Thu, 15 Apr 2010 15:40:28 +0200
Subject:  [Dulwich-users] [PATCH] Added unit tests for
 dulwich.object_store.tree_lookup_path.

* dulwich/tests/test_object_store.py
  (TreeLookupPathTests): This test case contains a few tests that ensure the
   tree_lookup_path function works as expected.
---
 pixmaps/prey.ico |  Bin 9662 -> 9662 bytes
 1 files changed, 0 insertions(+), 0 deletions(-)
 mode change 100755 => 100644 pixmaps/prey.ico

-- 
1.7.0.4
"""
        c, _diff, _version = git_am_patch_split(BytesIO(text), "utf-8")
        self.assertEqual(
            b"""\
Added unit tests for dulwich.object_store.tree_lookup_path.

* dulwich/tests/test_object_store.py
  (TreeLookupPathTests): This test case contains a few tests that ensure the
   tree_lookup_path function works as expected.
""",
            c.message,
        )

    def test_extract_pseudo_from_header(self) -> None:
        text = b"""From ff643aae102d8870cac88e8f007e70f58f3a7363 Mon Sep 17 00:00:00 2001
From: Jelmer Vernooij <jelmer@samba.org>
Date: Thu, 15 Apr 2010 15:40:28 +0200
Subject:  [Dulwich-users] [PATCH] Added unit tests for
 dulwich.object_store.tree_lookup_path.

From: Jelmer Vernooij <jelmer@debian.org>

* dulwich/tests/test_object_store.py
  (TreeLookupPathTests): This test case contains a few tests that ensure the
   tree_lookup_path function works as expected.
---
 pixmaps/prey.ico |  Bin 9662 -> 9662 bytes
 1 files changed, 0 insertions(+), 0 deletions(-)
 mode change 100755 => 100644 pixmaps/prey.ico

-- 
1.7.0.4
"""
        c, _diff, _version = git_am_patch_split(BytesIO(text), "utf-8")
        self.assertEqual(b"Jelmer Vernooij <jelmer@debian.org>", c.author)
        self.assertEqual(
            b"""\
Added unit tests for dulwich.object_store.tree_lookup_path.

* dulwich/tests/test_object_store.py
  (TreeLookupPathTests): This test case contains a few tests that ensure the
   tree_lookup_path function works as expected.
""",
            c.message,
        )

    def test_extract_no_version_tail(self) -> None:
        text = b"""\
From ff643aae102d8870cac88e8f007e70f58f3a7363 Mon Sep 17 00:00:00 2001
From: Jelmer Vernooij <jelmer@samba.org>
Date: Thu, 15 Apr 2010 15:40:28 +0200
Subject:  [Dulwich-users] [PATCH] Added unit tests for
 dulwich.object_store.tree_lookup_path.

From: Jelmer Vernooij <jelmer@debian.org>

---
 pixmaps/prey.ico |  Bin 9662 -> 9662 bytes
 1 files changed, 0 insertions(+), 0 deletions(-)
 mode change 100755 => 100644 pixmaps/prey.ico

"""
        _c, _diff, version = git_am_patch_split(BytesIO(text), "utf-8")
        self.assertEqual(None, version)

    def test_extract_mercurial(self) -> NoReturn:
        raise SkipTest(
            "git_am_patch_split doesn't handle Mercurial patches properly yet"
        )
        expected_diff = """\
diff --git a/dulwich/tests/test_patch.py b/dulwich/tests/test_patch.py
--- a/dulwich/tests/test_patch.py
+++ b/dulwich/tests/test_patch.py
@@ -158,7 +158,7 @@
 
 '''
         c, diff, version = git_am_patch_split(BytesIO(text))
-        self.assertIs(None, version)
+        self.assertEqual(None, version)
 
 
 class DiffTests(TestCase):
"""
        text = f"""\
From dulwich-users-bounces+jelmer=samba.org@lists.launchpad.net \
Mon Nov 29 00:58:18 2010
Date: Sun, 28 Nov 2010 17:57:27 -0600
From: Augie Fackler <durin42@gmail.com>
To: dulwich-users <dulwich-users@lists.launchpad.net>
Subject: [Dulwich-users] [PATCH] test_patch: fix tests on Python 2.6
Content-Transfer-Encoding: 8bit

Change-Id: I5e51313d4ae3a65c3f00c665002a7489121bb0d6

{expected_diff}

_______________________________________________
Mailing list: https://launchpad.net/~dulwich-users
Post to     : dulwich-users@lists.launchpad.net
Unsubscribe : https://launchpad.net/~dulwich-users
More help   : https://help.launchpad.net/ListHelp

"""
        _c, diff, version = git_am_patch_split(BytesIO(text))
        self.assertEqual(expected_diff, diff)
        self.assertEqual(None, version)


class DiffTests(TestCase):
    """Tests for write_blob_diff and write_tree_diff."""

    def test_blob_diff(self) -> None:
        f = BytesIO()
        write_blob_diff(
            f,
            (b"foo.txt", 0o644, Blob.from_string(b"old\nsame\n")),
            (b"bar.txt", 0o644, Blob.from_string(b"new\nsame\n")),
        )
        self.assertEqual(
            [
                b"diff --git a/foo.txt b/bar.txt",
                b"index 3b0f961..a116b51 644",
                b"--- a/foo.txt",
                b"+++ b/bar.txt",
                b"@@ -1,2 +1,2 @@",
                b"-old",
                b"+new",
                b" same",
            ],
            f.getvalue().splitlines(),
        )

    def test_blob_add(self) -> None:
        f = BytesIO()
        write_blob_diff(
            f,
            (None, None, None),
            (b"bar.txt", 0o644, Blob.from_string(b"new\nsame\n")),
        )
        self.assertEqual(
            [
                b"diff --git a/bar.txt b/bar.txt",
                b"new file mode 644",
                b"index 0000000..a116b51",
                b"--- /dev/null",
                b"+++ b/bar.txt",
                b"@@ -0,0 +1,2 @@",
                b"+new",
                b"+same",
            ],
            f.getvalue().splitlines(),
        )

    def test_blob_remove(self) -> None:
        f = BytesIO()
        write_blob_diff(
            f,
            (b"bar.txt", 0o644, Blob.from_string(b"new\nsame\n")),
            (None, None, None),
        )
        self.assertEqual(
            [
                b"diff --git a/bar.txt b/bar.txt",
                b"deleted file mode 644",
                b"index a116b51..0000000",
                b"--- a/bar.txt",
                b"+++ /dev/null",
                b"@@ -1,2 +0,0 @@",
                b"-new",
                b"-same",
            ],
            f.getvalue().splitlines(),
        )

    def test_tree_diff(self) -> None:
        f = BytesIO()
        store = MemoryObjectStore()
        added = Blob.from_string(b"add\n")
        removed = Blob.from_string(b"removed\n")
        changed1 = Blob.from_string(b"unchanged\nremoved\n")
        changed2 = Blob.from_string(b"unchanged\nadded\n")
        unchanged = Blob.from_string(b"unchanged\n")
        tree1 = Tree()
        tree1.add(b"removed.txt", 0o644, removed.id)
        tree1.add(b"changed.txt", 0o644, changed1.id)
        tree1.add(b"unchanged.txt", 0o644, changed1.id)
        tree2 = Tree()
        tree2.add(b"added.txt", 0o644, added.id)
        tree2.add(b"changed.txt", 0o644, changed2.id)
        tree2.add(b"unchanged.txt", 0o644, changed1.id)
        store.add_objects(
            [
                (o, None)
                for o in [
                    tree1,
                    tree2,
                    added,
                    removed,
                    changed1,
                    changed2,
                    unchanged,
                ]
            ]
        )
        write_tree_diff(f, store, tree1.id, tree2.id)
        self.assertEqual(
            [
                b"diff --git a/added.txt b/added.txt",
                b"new file mode 644",
                b"index 0000000..76d4bb8",
                b"--- /dev/null",
                b"+++ b/added.txt",
                b"@@ -0,0 +1 @@",
                b"+add",
                b"diff --git a/changed.txt b/changed.txt",
                b"index bf84e48..1be2436 644",
                b"--- a/changed.txt",
                b"+++ b/changed.txt",
                b"@@ -1,2 +1,2 @@",
                b" unchanged",
                b"-removed",
                b"+added",
                b"diff --git a/removed.txt b/removed.txt",
                b"deleted file mode 644",
                b"index 2c3f0b3..0000000",
                b"--- a/removed.txt",
                b"+++ /dev/null",
                b"@@ -1 +0,0 @@",
                b"-removed",
            ],
            f.getvalue().splitlines(),
        )

    def test_tree_diff_submodule(self) -> None:
        f = BytesIO()
        store = MemoryObjectStore()
        tree1 = Tree()
        tree1.add(
            b"asubmodule",
            S_IFGITLINK,
            b"06d0bdd9e2e20377b3180e4986b14c8549b393e4",
        )
        tree2 = Tree()
        tree2.add(
            b"asubmodule",
            S_IFGITLINK,
            b"cc975646af69f279396d4d5e1379ac6af80ee637",
        )
        store.add_objects([(o, None) for o in [tree1, tree2]])
        write_tree_diff(f, store, tree1.id, tree2.id)
        self.assertEqual(
            [
                b"diff --git a/asubmodule b/asubmodule",
                b"index 06d0bdd..cc97564 160000",
                b"--- a/asubmodule",
                b"+++ b/asubmodule",
                b"@@ -1 +1 @@",
                b"-Subproject commit 06d0bdd9e2e20377b3180e4986b14c8549b393e4",
                b"+Subproject commit cc975646af69f279396d4d5e1379ac6af80ee637",
            ],
            f.getvalue().splitlines(),
        )

    def test_object_diff_blob(self) -> None:
        f = BytesIO()
        b1 = Blob.from_string(b"old\nsame\n")
        b2 = Blob.from_string(b"new\nsame\n")
        store = MemoryObjectStore()
        store.add_objects([(b1, None), (b2, None)])
        write_object_diff(
            f, store, (b"foo.txt", 0o644, b1.id), (b"bar.txt", 0o644, b2.id)
        )
        self.assertEqual(
            [
                b"diff --git a/foo.txt b/bar.txt",
                b"index 3b0f961..a116b51 644",
                b"--- a/foo.txt",
                b"+++ b/bar.txt",
                b"@@ -1,2 +1,2 @@",
                b"-old",
                b"+new",
                b" same",
            ],
            f.getvalue().splitlines(),
        )

    def test_object_diff_add_blob(self) -> None:
        f = BytesIO()
        store = MemoryObjectStore()
        b2 = Blob.from_string(b"new\nsame\n")
        store.add_object(b2)
        write_object_diff(f, store, (None, None, None), (b"bar.txt", 0o644, b2.id))
        self.assertEqual(
            [
                b"diff --git a/bar.txt b/bar.txt",
                b"new file mode 644",
                b"index 0000000..a116b51",
                b"--- /dev/null",
                b"+++ b/bar.txt",
                b"@@ -0,0 +1,2 @@",
                b"+new",
                b"+same",
            ],
            f.getvalue().splitlines(),
        )

    def test_object_diff_remove_blob(self) -> None:
        f = BytesIO()
        b1 = Blob.from_string(b"new\nsame\n")
        store = MemoryObjectStore()
        store.add_object(b1)
        write_object_diff(f, store, (b"bar.txt", 0o644, b1.id), (None, None, None))
        self.assertEqual(
            [
                b"diff --git a/bar.txt b/bar.txt",
                b"deleted file mode 644",
                b"index a116b51..0000000",
                b"--- a/bar.txt",
                b"+++ /dev/null",
                b"@@ -1,2 +0,0 @@",
                b"-new",
                b"-same",
            ],
            f.getvalue().splitlines(),
        )

    def test_object_diff_bin_blob_force(self) -> None:
        f = BytesIO()
        # Prepare two slightly different PNG headers
        b1 = Blob.from_string(
            b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a"
            b"\x00\x00\x00\x0d\x49\x48\x44\x52"
            b"\x00\x00\x01\xd5\x00\x00\x00\x9f"
            b"\x08\x04\x00\x00\x00\x05\x04\x8b"
        )
        b2 = Blob.from_string(
            b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a"
            b"\x00\x00\x00\x0d\x49\x48\x44\x52"
            b"\x00\x00\x01\xd5\x00\x00\x00\x9f"
            b"\x08\x03\x00\x00\x00\x98\xd3\xb3"
        )
        store = MemoryObjectStore()
        store.add_objects([(b1, None), (b2, None)])
        write_object_diff(
            f,
            store,
            (b"foo.png", 0o644, b1.id),
            (b"bar.png", 0o644, b2.id),
            diff_binary=True,
        )
        self.assertEqual(
            [
                b"diff --git a/foo.png b/bar.png",
                b"index f73e47d..06364b7 644",
                b"--- a/foo.png",
                b"+++ b/bar.png",
                b"@@ -1,4 +1,4 @@",
                b" \x89PNG",
                b" \x1a",
                b" \x00\x00\x00",
                b"-IHDR\x00\x00\x01\xd5\x00\x00\x00"
                b"\x9f\x08\x04\x00\x00\x00\x05\x04\x8b",
                b"\\ No newline at end of file",
                b"+IHDR\x00\x00\x01\xd5\x00\x00\x00\x9f"
                b"\x08\x03\x00\x00\x00\x98\xd3\xb3",
                b"\\ No newline at end of file",
            ],
            f.getvalue().splitlines(),
        )

    def test_object_diff_bin_blob(self) -> None:
        f = BytesIO()
        # Prepare two slightly different PNG headers
        b1 = Blob.from_string(
            b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a"
            b"\x00\x00\x00\x0d\x49\x48\x44\x52"
            b"\x00\x00\x01\xd5\x00\x00\x00\x9f"
            b"\x08\x04\x00\x00\x00\x05\x04\x8b"
        )
        b2 = Blob.from_string(
            b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a"
            b"\x00\x00\x00\x0d\x49\x48\x44\x52"
            b"\x00\x00\x01\xd5\x00\x00\x00\x9f"
            b"\x08\x03\x00\x00\x00\x98\xd3\xb3"
        )
        store = MemoryObjectStore()
        store.add_objects([(b1, None), (b2, None)])
        write_object_diff(
            f, store, (b"foo.png", 0o644, b1.id), (b"bar.png", 0o644, b2.id)
        )
        self.assertEqual(
            [
                b"diff --git a/foo.png b/bar.png",
                b"index f73e47d..06364b7 644",
                b"Binary files a/foo.png and b/bar.png differ",
            ],
            f.getvalue().splitlines(),
        )

    def test_object_diff_add_bin_blob(self) -> None:
        f = BytesIO()
        b2 = Blob.from_string(
            b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a"
            b"\x00\x00\x00\x0d\x49\x48\x44\x52"
            b"\x00\x00\x01\xd5\x00\x00\x00\x9f"
            b"\x08\x03\x00\x00\x00\x98\xd3\xb3"
        )
        store = MemoryObjectStore()
        store.add_object(b2)
        write_object_diff(f, store, (None, None, None), (b"bar.png", 0o644, b2.id))
        self.assertEqual(
            [
                b"diff --git a/bar.png b/bar.png",
                b"new file mode 644",
                b"index 0000000..06364b7",
                b"Binary files /dev/null and b/bar.png differ",
            ],
            f.getvalue().splitlines(),
        )

    def test_object_diff_remove_bin_blob(self) -> None:
        f = BytesIO()
        b1 = Blob.from_string(
            b"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a"
            b"\x00\x00\x00\x0d\x49\x48\x44\x52"
            b"\x00\x00\x01\xd5\x00\x00\x00\x9f"
            b"\x08\x04\x00\x00\x00\x05\x04\x8b"
        )
        store = MemoryObjectStore()
        store.add_object(b1)
        write_object_diff(f, store, (b"foo.png", 0o644, b1.id), (None, None, None))
        self.assertEqual(
            [
                b"diff --git a/foo.png b/foo.png",
                b"deleted file mode 644",
                b"index f73e47d..0000000",
                b"Binary files a/foo.png and /dev/null differ",
            ],
            f.getvalue().splitlines(),
        )

    def test_object_diff_kind_change(self) -> None:
        f = BytesIO()
        b1 = Blob.from_string(b"new\nsame\n")
        store = MemoryObjectStore()
        store.add_object(b1)
        write_object_diff(
            f,
            store,
            (b"bar.txt", 0o644, b1.id),
            (
                b"bar.txt",
                0o160000,
                b"06d0bdd9e2e20377b3180e4986b14c8549b393e4",
            ),
        )
        self.assertEqual(
            [
                b"diff --git a/bar.txt b/bar.txt",
                b"old file mode 644",
                b"new file mode 160000",
                b"index a116b51..06d0bdd 160000",
                b"--- a/bar.txt",
                b"+++ b/bar.txt",
                b"@@ -1,2 +1 @@",
                b"-new",
                b"-same",
                b"+Subproject commit 06d0bdd9e2e20377b3180e4986b14c8549b393e4",
            ],
            f.getvalue().splitlines(),
        )


class GetSummaryTests(TestCase):
    def test_simple(self) -> None:
        c = make_commit(
            author=b"Jelmer <jelmer@samba.org>",
            committer=b"Jelmer <jelmer@samba.org>",
            author_time=1271350201,
            commit_time=1271350201,
            author_timezone=0,
            commit_timezone=0,
            message=b"This is the first line\nAnd this is the second line.\n",
            tree=Tree().id,
        )
        self.assertEqual("This-is-the-first-line", get_summary(c))


class DiffAlgorithmTests(TestCase):
    """Tests for diff algorithm selection."""

    def test_unified_diff_with_myers(self) -> None:
        """Test unified_diff_with_algorithm with default myers algorithm."""
        a = [b"line1\n", b"line2\n", b"line3\n"]
        b = [b"line1\n", b"line2 modified\n", b"line3\n"]

        result = list(
            unified_diff_with_algorithm(
                a, b, fromfile=b"a.txt", tofile=b"b.txt", algorithm="myers"
            )
        )

        # Should contain diff headers and the change
        self.assertTrue(any(b"---" in line for line in result))
        self.assertTrue(any(b"+++" in line for line in result))
        self.assertTrue(any(b"-line2" in line for line in result))
        self.assertTrue(any(b"+line2 modified" in line for line in result))

    def test_unified_diff_with_patience_not_available(self) -> None:
        """Test that DiffAlgorithmNotAvailable is raised when patience not available."""
        # Temporarily mock _get_sequence_matcher to simulate ImportError
        import dulwich.patch

        original = dulwich.patch._get_sequence_matcher

        def mock_get_sequence_matcher(algorithm, a, b):
            if algorithm == "patience":
                raise DiffAlgorithmNotAvailable(
                    "patience", "Install with: pip install 'dulwich[patiencediff]'"
                )
            return original(algorithm, a, b)

        try:
            dulwich.patch._get_sequence_matcher = mock_get_sequence_matcher

            a = [b"line1\n", b"line2\n", b"line3\n"]
            b = [b"line1\n", b"line2 modified\n", b"line3\n"]

            with self.assertRaises(DiffAlgorithmNotAvailable) as cm:
                list(
                    unified_diff_with_algorithm(
                        a, b, fromfile=b"a.txt", tofile=b"b.txt", algorithm="patience"
                    )
                )

            self.assertIn("patience", str(cm.exception))
            self.assertIn("pip install", str(cm.exception))
        finally:
            dulwich.patch._get_sequence_matcher = original


class PatienceDiffTests(TestCase):
    """Tests for patience diff algorithm support."""

    def setUp(self) -> None:
        super().setUp()
        # Skip all patience diff tests if patiencediff is not available
        try:
            import patiencediff  # noqa: F401
        except ImportError:
            raise DependencyMissing("patiencediff")

    def test_unified_diff_with_patience_available(self) -> None:
        """Test unified_diff_with_algorithm with patience if available."""
        a = [b"line1\n", b"line2\n", b"line3\n"]
        b = [b"line1\n", b"line2 modified\n", b"line3\n"]

        result = list(
            unified_diff_with_algorithm(
                a, b, fromfile=b"a.txt", tofile=b"b.txt", algorithm="patience"
            )
        )

        # Should contain diff headers and the change
        self.assertTrue(any(b"---" in line for line in result))
        self.assertTrue(any(b"+++" in line for line in result))
        self.assertTrue(any(b"-line2" in line for line in result))
        self.assertTrue(any(b"+line2 modified" in line for line in result))

    def test_unified_diff_with_patience_not_available(self) -> None:
        """Test that DiffAlgorithmNotAvailable is raised when patience not available."""
        # Temporarily mock _get_sequence_matcher to simulate ImportError
        import dulwich.patch

        original = dulwich.patch._get_sequence_matcher

        def mock_get_sequence_matcher(algorithm, a, b):
            if algorithm == "patience":
                raise DiffAlgorithmNotAvailable(
                    "patience", "Install with: pip install 'dulwich[patiencediff]'"
                )
            return original(algorithm, a, b)

        try:
            dulwich.patch._get_sequence_matcher = mock_get_sequence_matcher

            a = [b"line1\n", b"line2\n", b"line3\n"]
            b = [b"line1\n", b"line2 modified\n", b"line3\n"]

            with self.assertRaises(DiffAlgorithmNotAvailable) as cm:
                list(
                    unified_diff_with_algorithm(
                        a, b, fromfile=b"a.txt", tofile=b"b.txt", algorithm="patience"
                    )
                )

            self.assertIn("patience", str(cm.exception))
            self.assertIn("pip install", str(cm.exception))
        finally:
            dulwich.patch._get_sequence_matcher = original

    def test_write_blob_diff_with_patience(self) -> None:
        """Test write_blob_diff with patience algorithm if available."""
        f = BytesIO()
        old_blob = Blob()
        old_blob.data = b"line1\nline2\nline3\n"
        new_blob = Blob()
        new_blob.data = b"line1\nline2 modified\nline3\n"

        write_blob_diff(
            f,
            (b"file.txt", 0o100644, old_blob),
            (b"file.txt", 0o100644, new_blob),
            diff_algorithm="patience",
        )

        diff = f.getvalue()
        self.assertIn(b"diff --git", diff)
        self.assertIn(b"-line2", diff)
        self.assertIn(b"+line2 modified", diff)

    def test_write_object_diff_with_patience(self) -> None:
        """Test write_object_diff with patience algorithm if available."""
        f = BytesIO()
        store = MemoryObjectStore()

        old_blob = Blob()
        old_blob.data = b"line1\nline2\nline3\n"
        store.add_object(old_blob)

        new_blob = Blob()
        new_blob.data = b"line1\nline2 modified\nline3\n"
        store.add_object(new_blob)

        write_object_diff(
            f,
            store,
            (b"file.txt", 0o100644, old_blob.id),
            (b"file.txt", 0o100644, new_blob.id),
            diff_algorithm="patience",
        )

        diff = f.getvalue()
        self.assertIn(b"diff --git", diff)
        self.assertIn(b"-line2", diff)
        self.assertIn(b"+line2 modified", diff)


class PatchIdTests(TestCase):
    """Tests for patch_id and commit_patch_id functions."""

    def test_patch_id_simple(self) -> None:
        """Test patch_id computation with a simple diff."""
        diff = b"""diff --git a/file.txt b/file.txt
index 3b0f961..a116b51 644
--- a/file.txt
+++ b/file.txt
@@ -1,2 +1,2 @@
-old
+new
 same
"""
        pid = patch_id(diff)
        # Patch ID should be a 40-byte hex string
        self.assertEqual(40, len(pid))
        self.assertTrue(all(c in b"0123456789abcdef" for c in pid))

    def test_patch_id_same_for_equivalent_diffs(self) -> None:
        """Test that equivalent patches have the same ID."""
        # Two diffs with different line numbers but same changes
        diff1 = b"""diff --git a/file.txt b/file.txt
--- a/file.txt
+++ b/file.txt
@@ -1,3 +1,3 @@
 context
-old line
+new line
 context
"""
        diff2 = b"""diff --git a/file.txt b/file.txt
--- a/file.txt
+++ b/file.txt
@@ -10,3 +10,3 @@
 context
-old line
+new line
 context
"""
        pid1 = patch_id(diff1)
        pid2 = patch_id(diff2)
        # Same patch content should give same patch ID
        self.assertEqual(pid1, pid2)

    def test_commit_patch_id(self) -> None:
        """Test commit_patch_id computation."""
        store = MemoryObjectStore()

        # Create two trees
        blob1 = Blob.from_string(b"content1\n")
        blob2 = Blob.from_string(b"content2\n")
        store.add_objects([(blob1, None), (blob2, None)])

        tree1 = Tree()
        tree1.add(b"file.txt", 0o644, blob1.id)
        store.add_object(tree1)

        tree2 = Tree()
        tree2.add(b"file.txt", 0o644, blob2.id)
        store.add_object(tree2)

        # Create a commit
        commit = Commit()
        commit.tree = tree2.id
        commit.parents = [b"0" * 40]  # Fake parent
        commit.author = commit.committer = b"Test <test@example.com>"
        commit.author_time = commit.commit_time = 1234567890
        commit.author_timezone = commit.commit_timezone = 0
        commit.message = b"Test commit\n"
        commit.encoding = b"UTF-8"
        store.add_object(commit)

        # Create parent commit
        parent_commit = Commit()
        parent_commit.tree = tree1.id
        parent_commit.parents = []
        parent_commit.author = parent_commit.committer = b"Test <test@example.com>"
        parent_commit.author_time = parent_commit.commit_time = 1234567880
        parent_commit.author_timezone = parent_commit.commit_timezone = 0
        parent_commit.message = b"Parent commit\n"
        parent_commit.encoding = b"UTF-8"
        store.add_object(parent_commit)

        # Update commit to have real parent
        commit.parents = [parent_commit.id]
        store.add_object(commit)

        # Compute patch ID
        pid = commit_patch_id(store, commit.id)
        self.assertEqual(40, len(pid))
        self.assertTrue(all(c in b"0123456789abcdef" for c in pid))