File: test_objects.py

package info (click to toggle)
dulwich 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 7,388 kB
  • sloc: python: 99,991; makefile: 163; sh: 67
file content (2011 lines) | stat: -rw-r--r-- 74,908 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
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
# test_objects.py -- tests for objects.py
# Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
#
# 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 git base objects."""

# TODO: Round-trip parse-serialize-parse and serialize-parse-serialize tests.

import datetime
import os
import stat
from contextlib import contextmanager
from io import BytesIO
from itertools import permutations

from dulwich.errors import ObjectFormatException
from dulwich.objects import (
    MAX_TIME,
    ZERO_SHA,
    Blob,
    Commit,
    ShaFile,
    Tag,
    Tree,
    TreeEntry,
    _parse_tree_py,
    _sorted_tree_items_py,
    check_hexsha,
    check_identity,
    format_timezone,
    hex_to_filename,
    hex_to_sha,
    key_entry,
    object_class,
    parse_timezone,
    pretty_format_tree_entry,
    sha_to_hex,
)

try:
    from dulwich.objects import _parse_tree_rs, _sorted_tree_items_rs
except ImportError:
    _sorted_tree_items_rs = _parse_tree_rs = None
from dulwich.tests.utils import (
    ext_functest_builder,
    functest_builder,
    make_commit,
    make_object,
)

from . import TestCase

a_sha = b"6f670c0fb53f9463760b7295fbb814e965fb20c8"
b_sha = b"2969be3e8ee1c0222396a5611407e4769f14e54b"
c_sha = b"954a536f7819d40e6f637f849ee187dd10066349"
tree_sha = b"70c190eb48fa8bbb50ddc692a17b44cb781af7f6"
tag_sha = b"71033db03a03c6a36721efcf1968dd8f8e0cf023"


class TestHexToSha(TestCase):
    def test_simple(self) -> None:
        self.assertEqual(b"\xab\xcd" * 10, hex_to_sha(b"abcd" * 10))

    def test_reverse(self) -> None:
        self.assertEqual(b"abcd" * 10, sha_to_hex(b"\xab\xcd" * 10))


class BlobReadTests(TestCase):
    """Test decompression of blobs."""

    def get_sha_file(self, cls, base, sha):
        dir = os.path.join(os.path.dirname(__file__), "..", "testdata", base)
        return cls.from_path(hex_to_filename(dir, sha))

    def get_blob(self, sha):
        """Return the blob named sha from the test data dir."""
        return self.get_sha_file(Blob, "blobs", sha)

    def get_tree(self, sha):
        return self.get_sha_file(Tree, "trees", sha)

    def get_tag(self, sha):
        return self.get_sha_file(Tag, "tags", sha)

    def commit(self, sha):
        return self.get_sha_file(Commit, "commits", sha)

    def test_decompress_simple_blob(self) -> None:
        b = self.get_blob(a_sha)
        self.assertEqual(b.data, b"test 1\n")
        self.assertEqual(b.sha().hexdigest().encode("ascii"), a_sha)

    def test_hash(self) -> None:
        b = self.get_blob(a_sha)
        self.assertEqual(hash(b.id), hash(b))

    def test_parse_empty_blob_object(self) -> None:
        sha = b"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"
        b = self.get_blob(sha)
        self.assertEqual(b.data, b"")
        self.assertEqual(b.id, sha)
        self.assertEqual(b.sha().hexdigest().encode("ascii"), sha)

    def test_create_blob_from_string(self) -> None:
        string = b"test 2\n"
        b = Blob.from_string(string)
        self.assertEqual(b.data, string)
        self.assertEqual(b.sha().hexdigest().encode("ascii"), b_sha)

    def test_legacy_from_file(self) -> None:
        b1 = Blob.from_string(b"foo")
        b_raw = b1.as_legacy_object()
        b2 = b1.from_file(BytesIO(b_raw))
        self.assertEqual(b1, b2)

    def test_legacy_from_file_compression_level(self) -> None:
        b1 = Blob.from_string(b"foo")
        b_raw = b1.as_legacy_object(compression_level=6)
        b2 = b1.from_file(BytesIO(b_raw))
        self.assertEqual(b1, b2)

    def test_chunks(self) -> None:
        string = b"test 5\n"
        b = Blob.from_string(string)
        self.assertEqual([string], b.chunked)

    def test_splitlines(self) -> None:
        for case in [
            [],
            [b"foo\nbar\n"],
            [b"bl\na", b"blie"],
            [b"bl\na", b"blie", b"bloe\n"],
            [b"", b"bl\na", b"blie", b"bloe\n"],
            [b"", b"", b"", b"bla\n"],
            [b"", b"", b"", b"bla\n", b""],
            [b"bl", b"", b"a\naaa"],
            [b"a\naaa", b"a"],
        ]:
            b = Blob()
            b.chunked = case
            self.assertEqual(b.data.splitlines(True), b.splitlines())

    def test_set_chunks(self) -> None:
        b = Blob()
        b.chunked = [b"te", b"st", b" 5\n"]
        self.assertEqual(b"test 5\n", b.data)
        b.chunked = [b"te", b"st", b" 6\n"]
        self.assertEqual(b"test 6\n", b.as_raw_string())
        self.assertEqual(b"test 6\n", bytes(b))

    def test_parse_legacy_blob(self) -> None:
        string = b"test 3\n"
        b = self.get_blob(c_sha)
        self.assertEqual(b.data, string)
        self.assertEqual(b.sha().hexdigest().encode("ascii"), c_sha)

    def test_eq(self) -> None:
        blob1 = self.get_blob(a_sha)
        blob2 = self.get_blob(a_sha)
        self.assertEqual(blob1, blob2)

    def test_read_tree_from_file(self) -> None:
        t = self.get_tree(tree_sha)
        self.assertEqual(t.items()[0], (b"a", 33188, a_sha))
        self.assertEqual(t.items()[1], (b"b", 33188, b_sha))

    def test_read_tree_from_file_parse_count(self) -> None:
        old_deserialize = Tree._deserialize

        def reset_deserialize() -> None:
            Tree._deserialize = old_deserialize

        self.addCleanup(reset_deserialize)
        self.deserialize_count = 0

        def counting_deserialize(*args, **kwargs):
            self.deserialize_count += 1
            return old_deserialize(*args, **kwargs)

        Tree._deserialize = counting_deserialize
        t = self.get_tree(tree_sha)
        self.assertEqual(t.items()[0], (b"a", 33188, a_sha))
        self.assertEqual(t.items()[1], (b"b", 33188, b_sha))
        self.assertEqual(self.deserialize_count, 1)

    def test_read_tag_from_file(self) -> None:
        t = self.get_tag(tag_sha)
        self.assertEqual(
            t.object, (Commit, b"51b668fd5bf7061b7d6fa525f88803e6cfadaa51")
        )
        self.assertEqual(t.name, b"signed")
        self.assertEqual(t.tagger, b"Ali Sabil <ali.sabil@gmail.com>")
        self.assertEqual(t.tag_time, 1231203091)
        self.assertEqual(t.message, b"This is a signed tag\n")
        self.assertEqual(
            t.signature,
            b"-----BEGIN PGP SIGNATURE-----\n"
            b"Version: GnuPG v1.4.9 (GNU/Linux)\n"
            b"\n"
            b"iEYEABECAAYFAkliqx8ACgkQqSMmLy9u/"
            b"kcx5ACfakZ9NnPl02tOyYP6pkBoEkU1\n"
            b"5EcAn0UFgokaSvS371Ym/4W9iJj6vh3h\n"
            b"=ql7y\n"
            b"-----END PGP SIGNATURE-----\n",
        )
        self.assertEqual(t.raw_without_sig() + t.signature, bytes(t))

    def test_read_commit_from_file(self) -> None:
        sha = b"60dacdc733de308bb77bb76ce0fb0f9b44c9769e"
        c = self.commit(sha)
        self.assertEqual(c.tree, tree_sha)
        self.assertEqual(c.parents, [b"0d89f20333fbb1d2f3a94da77f4981373d8f4310"])
        self.assertEqual(c.author, b"James Westby <jw+debian@jameswestby.net>")
        self.assertEqual(c.committer, b"James Westby <jw+debian@jameswestby.net>")
        self.assertEqual(c.commit_time, 1174759230)
        self.assertEqual(c.commit_timezone, 0)
        self.assertEqual(c.author_timezone, 0)
        self.assertEqual(c.message, b"Test commit\n")

    def test_read_commit_no_parents(self) -> None:
        sha = b"0d89f20333fbb1d2f3a94da77f4981373d8f4310"
        c = self.commit(sha)
        self.assertEqual(c.tree, b"90182552c4a85a45ec2a835cadc3451bebdfe870")
        self.assertEqual(c.parents, [])
        self.assertEqual(c.author, b"James Westby <jw+debian@jameswestby.net>")
        self.assertEqual(c.committer, b"James Westby <jw+debian@jameswestby.net>")
        self.assertEqual(c.commit_time, 1174758034)
        self.assertEqual(c.commit_timezone, 0)
        self.assertEqual(c.author_timezone, 0)
        self.assertEqual(c.message, b"Test commit\n")

    def test_read_commit_two_parents(self) -> None:
        sha = b"5dac377bdded4c9aeb8dff595f0faeebcc8498cc"
        c = self.commit(sha)
        self.assertEqual(c.tree, b"d80c186a03f423a81b39df39dc87fd269736ca86")
        self.assertEqual(
            c.parents,
            [
                b"ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd",
                b"4cffe90e0a41ad3f5190079d7c8f036bde29cbe6",
            ],
        )
        self.assertEqual(c.author, b"James Westby <jw+debian@jameswestby.net>")
        self.assertEqual(c.committer, b"James Westby <jw+debian@jameswestby.net>")
        self.assertEqual(c.commit_time, 1174773719)
        self.assertEqual(c.commit_timezone, 0)
        self.assertEqual(c.author_timezone, 0)
        self.assertEqual(c.message, b"Merge ../b\n")

    def test_stub_sha(self) -> None:
        sha = b"5" * 40
        c = make_commit(id=sha, message=b"foo")
        self.assertIsInstance(c, Commit)
        self.assertEqual(sha, c.id)
        self.assertNotEqual(sha, c.sha())


class ShaFileCheckTests(TestCase):
    def assertCheckFails(self, cls, data) -> None:
        obj = cls()

        def do_check() -> None:
            obj.set_raw_string(data)
            obj.check()

        self.assertRaises(ObjectFormatException, do_check)

    def assertCheckSucceeds(self, cls, data) -> None:
        obj = cls()
        obj.set_raw_string(data)
        self.assertEqual(None, obj.check())


small_buffer_zlib_object = (
    b"\x48\x89\x15\xcc\x31\x0e\xc2\x30\x0c\x40\x51\xe6"
    b"\x9c\xc2\x3b\xaa\x64\x37\xc4\xc1\x12\x42\x5c\xc5"
    b"\x49\xac\x52\xd4\x92\xaa\x78\xe1\xf6\x94\xed\xeb"
    b"\x0d\xdf\x75\x02\xa2\x7c\xea\xe5\x65\xd5\x81\x8b"
    b"\x9a\x61\xba\xa0\xa9\x08\x36\xc9\x4c\x1a\xad\x88"
    b"\x16\xba\x46\xc4\xa8\x99\x6a\x64\xe1\xe0\xdf\xcd"
    b"\xa0\xf6\x75\x9d\x3d\xf8\xf1\xd0\x77\xdb\xfb\xdc"
    b"\x86\xa3\x87\xf1\x2f\x93\xed\x00\xb7\xc7\xd2\xab"
    b"\x2e\xcf\xfe\xf1\x3b\x50\xa4\x91\x53\x12\x24\x38"
    b"\x23\x21\x86\xf0\x03\x2f\x91\x24\x52"
)


class ShaFileTests(TestCase):
    def test_deflated_smaller_window_buffer(self) -> None:
        # zlib on some systems uses smaller buffers,
        # resulting in a different header.
        # See https://github.com/libgit2/libgit2/pull/464
        sf = ShaFile.from_file(BytesIO(small_buffer_zlib_object))
        self.assertEqual(sf.type_name, b"tag")
        self.assertEqual(sf.tagger, b" <@localhost>")


class CommitSerializationTests(TestCase):
    def make_commit(self, **kwargs):
        attrs = {
            "tree": b"d80c186a03f423a81b39df39dc87fd269736ca86",
            "parents": [
                b"ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd",
                b"4cffe90e0a41ad3f5190079d7c8f036bde29cbe6",
            ],
            "author": b"James Westby <jw+debian@jameswestby.net>",
            "committer": b"James Westby <jw+debian@jameswestby.net>",
            "commit_time": 1174773719,
            "author_time": 1174773719,
            "commit_timezone": 0,
            "author_timezone": 0,
            "message": b"Merge ../b\n",
        }
        attrs.update(kwargs)
        return make_commit(**attrs)

    def test_encoding(self) -> None:
        c = self.make_commit(encoding=b"iso8859-1")
        self.assertIn(b"encoding iso8859-1\n", c.as_raw_string())

    def test_short_timestamp(self) -> None:
        c = self.make_commit(commit_time=30)
        c1 = Commit()
        c1.set_raw_string(c.as_raw_string())
        self.assertEqual(30, c1.commit_time)

    def test_full_tree(self) -> None:
        c = self.make_commit(commit_time=30)
        t = Tree()
        t.add(b"data-x", 0o644, Blob().id)
        c.tree = t
        c1 = Commit()
        c1.set_raw_string(c.as_raw_string())
        self.assertEqual(t.id, c1.tree)
        self.assertEqual(c.as_raw_string(), c1.as_raw_string())

    def test_raw_length(self) -> None:
        c = self.make_commit()
        self.assertEqual(len(c.as_raw_string()), c.raw_length())

    def test_simple(self) -> None:
        c = self.make_commit()
        self.assertEqual(c.id, b"5dac377bdded4c9aeb8dff595f0faeebcc8498cc")
        self.assertEqual(
            b"tree d80c186a03f423a81b39df39dc87fd269736ca86\n"
            b"parent ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd\n"
            b"parent 4cffe90e0a41ad3f5190079d7c8f036bde29cbe6\n"
            b"author James Westby <jw+debian@jameswestby.net> "
            b"1174773719 +0000\n"
            b"committer James Westby <jw+debian@jameswestby.net> "
            b"1174773719 +0000\n"
            b"\n"
            b"Merge ../b\n",
            c.as_raw_string(),
        )

    def test_timezone(self) -> None:
        c = self.make_commit(commit_timezone=(5 * 60))
        self.assertIn(b" +0005\n", c.as_raw_string())

    def test_neg_timezone(self) -> None:
        c = self.make_commit(commit_timezone=(-1 * 3600))
        self.assertIn(b" -0100\n", c.as_raw_string())

    def test_deserialize(self) -> None:
        c = self.make_commit()
        d = Commit()
        d._deserialize(c.as_raw_chunks())
        self.assertEqual(c, d)

    def test_serialize_gpgsig(self) -> None:
        gpgsig = b"""-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQIcBAABCgAGBQJULCdfAAoJEACAbyvXKaRXuKwP/RyP9PA49uAvu8tQVCC/uBa8
vi975+xvO14R8Pp8k2nps7lSxCdtCd+xVT1VRHs0wNhOZo2YCVoU1HATkPejqSeV
NScTHcxnk4/+bxyfk14xvJkNp7FlQ3npmBkA+lbV0Ubr33rvtIE5jiJPyz+SgWAg
xdBG2TojV0squj00GoH/euK6aX7GgZtwdtpTv44haCQdSuPGDcI4TORqR6YSqvy3
GPE+3ZqXPFFb+KILtimkxitdwB7CpwmNse2vE3rONSwTvi8nq3ZoQYNY73CQGkUy
qoFU0pDtw87U3niFin1ZccDgH0bB6624sLViqrjcbYJeg815Htsu4rmzVaZADEVC
XhIO4MThebusdk0AcNGjgpf3HRHk0DPMDDlIjm+Oao0cqovvF6VyYmcb0C+RmhJj
dodLXMNmbqErwTk3zEkW0yZvNIYXH7m9SokPCZa4eeIM7be62X6h1mbt0/IU6Th+
v18fS0iTMP/Viug5und+05C/v04kgDo0CPphAbXwWMnkE4B6Tl9sdyUYXtvQsL7x
0+WP1gL27ANqNZiI07Kz/BhbBAQI/+2TFT7oGr0AnFPQ5jHp+3GpUf6OKuT1wT3H
ND189UFuRuubxb42vZhpcXRbqJVWnbECTKVUPsGZqat3enQUB63uM4i6/RdONDZA
fDeF1m4qYs+cUXKNUZ03
=X6RT
-----END PGP SIGNATURE-----"""
        pre_sig = b"""\
tree d80c186a03f423a81b39df39dc87fd269736ca86
parent ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd
parent 4cffe90e0a41ad3f5190079d7c8f036bde29cbe6
author James Westby <jw+debian@jameswestby.net> 1174773719 +0000
committer James Westby <jw+debian@jameswestby.net> 1174773719 +0000
"""
        git_sig = b"""\
gpgsig -----BEGIN PGP SIGNATURE-----
 Version: GnuPG v1
 
 iQIcBAABCgAGBQJULCdfAAoJEACAbyvXKaRXuKwP/RyP9PA49uAvu8tQVCC/uBa8
 vi975+xvO14R8Pp8k2nps7lSxCdtCd+xVT1VRHs0wNhOZo2YCVoU1HATkPejqSeV
 NScTHcxnk4/+bxyfk14xvJkNp7FlQ3npmBkA+lbV0Ubr33rvtIE5jiJPyz+SgWAg
 xdBG2TojV0squj00GoH/euK6aX7GgZtwdtpTv44haCQdSuPGDcI4TORqR6YSqvy3
 GPE+3ZqXPFFb+KILtimkxitdwB7CpwmNse2vE3rONSwTvi8nq3ZoQYNY73CQGkUy
 qoFU0pDtw87U3niFin1ZccDgH0bB6624sLViqrjcbYJeg815Htsu4rmzVaZADEVC
 XhIO4MThebusdk0AcNGjgpf3HRHk0DPMDDlIjm+Oao0cqovvF6VyYmcb0C+RmhJj
 dodLXMNmbqErwTk3zEkW0yZvNIYXH7m9SokPCZa4eeIM7be62X6h1mbt0/IU6Th+
 v18fS0iTMP/Viug5und+05C/v04kgDo0CPphAbXwWMnkE4B6Tl9sdyUYXtvQsL7x
 0+WP1gL27ANqNZiI07Kz/BhbBAQI/+2TFT7oGr0AnFPQ5jHp+3GpUf6OKuT1wT3H
 ND189UFuRuubxb42vZhpcXRbqJVWnbECTKVUPsGZqat3enQUB63uM4i6/RdONDZA
 fDeF1m4qYs+cUXKNUZ03
 =X6RT
 -----END PGP SIGNATURE-----
"""
        post_sig = b"""\

Merge ../b
"""
        commit = self.make_commit(gpgsig=gpgsig)
        self.maxDiff = None
        self.assertEqual(pre_sig + git_sig + post_sig, commit.as_raw_string())
        self.assertEqual(pre_sig + post_sig, commit.raw_without_sig())
        self.assertEqual(gpgsig, commit.gpgsig)
        self.assertEqual(b"Merge ../b\n", commit.message)

    def test_serialize_mergetag(self) -> None:
        tag = make_object(
            Tag,
            object=(Commit, b"a38d6181ff27824c79fc7df825164a212eff6a3f"),
            object_type_name=b"commit",
            name=b"v2.6.22-rc7",
            tag_time=1183319674,
            tag_timezone=0,
            tagger=b"Linus Torvalds <torvalds@woody.linux-foundation.org>",
            message=default_message,
        )
        commit = self.make_commit(mergetag=[tag])

        self.assertEqual(
            b"""tree d80c186a03f423a81b39df39dc87fd269736ca86
parent ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd
parent 4cffe90e0a41ad3f5190079d7c8f036bde29cbe6
author James Westby <jw+debian@jameswestby.net> 1174773719 +0000
committer James Westby <jw+debian@jameswestby.net> 1174773719 +0000
mergetag object a38d6181ff27824c79fc7df825164a212eff6a3f
 type commit
 tag v2.6.22-rc7
 tagger Linus Torvalds <torvalds@woody.linux-foundation.org> 1183319674 +0000
 
 Linux 2.6.22-rc7
 -----BEGIN PGP SIGNATURE-----
 Version: GnuPG v1.4.7 (GNU/Linux)
 
 iD8DBQBGiAaAF3YsRnbiHLsRAitMAKCiLboJkQECM/jpYsY3WPfvUgLXkACgg3ql
 OK2XeQOiEeXtT76rV4t2WR4=
 =ivrA
 -----END PGP SIGNATURE-----

Merge ../b
""",
            commit.as_raw_string(),
        )

    def test_serialize_mergetags(self) -> None:
        tag = make_object(
            Tag,
            object=(Commit, b"a38d6181ff27824c79fc7df825164a212eff6a3f"),
            object_type_name=b"commit",
            name=b"v2.6.22-rc7",
            tag_time=1183319674,
            tag_timezone=0,
            tagger=b"Linus Torvalds <torvalds@woody.linux-foundation.org>",
            message=default_message,
        )
        commit = self.make_commit(mergetag=[tag, tag])

        self.assertEqual(
            b"""tree d80c186a03f423a81b39df39dc87fd269736ca86
parent ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd
parent 4cffe90e0a41ad3f5190079d7c8f036bde29cbe6
author James Westby <jw+debian@jameswestby.net> 1174773719 +0000
committer James Westby <jw+debian@jameswestby.net> 1174773719 +0000
mergetag object a38d6181ff27824c79fc7df825164a212eff6a3f
 type commit
 tag v2.6.22-rc7
 tagger Linus Torvalds <torvalds@woody.linux-foundation.org> 1183319674 +0000
 
 Linux 2.6.22-rc7
 -----BEGIN PGP SIGNATURE-----
 Version: GnuPG v1.4.7 (GNU/Linux)
 
 iD8DBQBGiAaAF3YsRnbiHLsRAitMAKCiLboJkQECM/jpYsY3WPfvUgLXkACgg3ql
 OK2XeQOiEeXtT76rV4t2WR4=
 =ivrA
 -----END PGP SIGNATURE-----
mergetag object a38d6181ff27824c79fc7df825164a212eff6a3f
 type commit
 tag v2.6.22-rc7
 tagger Linus Torvalds <torvalds@woody.linux-foundation.org> 1183319674 +0000
 
 Linux 2.6.22-rc7
 -----BEGIN PGP SIGNATURE-----
 Version: GnuPG v1.4.7 (GNU/Linux)
 
 iD8DBQBGiAaAF3YsRnbiHLsRAitMAKCiLboJkQECM/jpYsY3WPfvUgLXkACgg3ql
 OK2XeQOiEeXtT76rV4t2WR4=
 =ivrA
 -----END PGP SIGNATURE-----

Merge ../b
""",
            commit.as_raw_string(),
        )

    def test_deserialize_mergetag(self) -> None:
        tag = make_object(
            Tag,
            object=(Commit, b"a38d6181ff27824c79fc7df825164a212eff6a3f"),
            object_type_name=b"commit",
            name=b"v2.6.22-rc7",
            tag_time=1183319674,
            tag_timezone=0,
            tagger=b"Linus Torvalds <torvalds@woody.linux-foundation.org>",
            message=default_message,
        )
        commit = self.make_commit(mergetag=[tag])

        d = Commit()
        d._deserialize(commit.as_raw_chunks())
        self.assertEqual(commit, d)

    def test_deserialize_mergetags(self) -> None:
        tag = make_object(
            Tag,
            object=(Commit, b"a38d6181ff27824c79fc7df825164a212eff6a3f"),
            object_type_name=b"commit",
            name=b"v2.6.22-rc7",
            tag_time=1183319674,
            tag_timezone=0,
            tagger=b"Linus Torvalds <torvalds@woody.linux-foundation.org>",
            message=default_message,
        )
        commit = self.make_commit(mergetag=[tag, tag])

        d = Commit()
        d._deserialize(commit.as_raw_chunks())
        self.assertEqual(commit, d)


default_committer = b"James Westby <jw+debian@jameswestby.net> 1174773719 +0000"


class CommitParseTests(ShaFileCheckTests):
    def make_commit_lines(
        self,
        tree=b"d80c186a03f423a81b39df39dc87fd269736ca86",
        parents=[
            b"ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd",
            b"4cffe90e0a41ad3f5190079d7c8f036bde29cbe6",
        ],
        author=default_committer,
        committer=default_committer,
        encoding=None,
        message=b"Merge ../b\n",
        extra=None,
    ):
        lines = []
        if tree is not None:
            lines.append(b"tree " + tree)
        if parents is not None:
            lines.extend(b"parent " + p for p in parents)
        if author is not None:
            lines.append(b"author " + author)
        if committer is not None:
            lines.append(b"committer " + committer)
        if encoding is not None:
            lines.append(b"encoding " + encoding)
        if extra is not None:
            for name, value in sorted(extra.items()):
                lines.append(name + b" " + value)
        lines.append(b"")
        if message is not None:
            lines.append(message)
        return lines

    def make_commit_text(self, **kwargs):
        return b"\n".join(self.make_commit_lines(**kwargs))

    def test_simple(self) -> None:
        c = Commit.from_string(self.make_commit_text())
        self.assertEqual(b"Merge ../b\n", c.message)
        self.assertEqual(b"James Westby <jw+debian@jameswestby.net>", c.author)
        self.assertEqual(b"James Westby <jw+debian@jameswestby.net>", c.committer)
        self.assertEqual(b"d80c186a03f423a81b39df39dc87fd269736ca86", c.tree)
        self.assertEqual(
            [
                b"ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd",
                b"4cffe90e0a41ad3f5190079d7c8f036bde29cbe6",
            ],
            c.parents,
        )
        expected_time = datetime.datetime(2007, 3, 24, 22, 1, 59)
        self.assertEqual(
            expected_time,
            datetime.datetime.fromtimestamp(
                c.commit_time, datetime.timezone.utc
            ).replace(tzinfo=None),
        )
        self.assertEqual(0, c.commit_timezone)
        self.assertEqual(
            expected_time,
            datetime.datetime.fromtimestamp(
                c.author_time, datetime.timezone.utc
            ).replace(tzinfo=None),
        )
        self.assertEqual(0, c.author_timezone)
        self.assertEqual(None, c.encoding)

    def test_custom(self) -> None:
        c = Commit.from_string(self.make_commit_text(extra={b"extra-field": b"data"}))
        self.assertEqual([(b"extra-field", b"data")], c._extra)

    def test_encoding(self) -> None:
        c = Commit.from_string(self.make_commit_text(encoding=b"UTF-8"))
        self.assertEqual(b"UTF-8", c.encoding)

    def test_check(self) -> None:
        self.assertCheckSucceeds(Commit, self.make_commit_text())
        self.assertCheckSucceeds(Commit, self.make_commit_text(parents=None))
        self.assertCheckSucceeds(Commit, self.make_commit_text(encoding=b"UTF-8"))

        self.assertCheckFails(Commit, self.make_commit_text(tree=b"xxx"))
        self.assertCheckFails(Commit, self.make_commit_text(parents=[a_sha, b"xxx"]))
        bad_committer = b"some guy without an email address 1174773719 +0000"
        self.assertCheckFails(Commit, self.make_commit_text(committer=bad_committer))
        self.assertCheckFails(Commit, self.make_commit_text(author=bad_committer))
        self.assertCheckFails(Commit, self.make_commit_text(author=None))
        self.assertCheckFails(Commit, self.make_commit_text(committer=None))
        self.assertCheckFails(
            Commit, self.make_commit_text(author=None, committer=None)
        )

    def test_check_duplicates(self) -> None:
        # duplicate each of the header fields
        for i in range(5):
            lines = self.make_commit_lines(parents=[a_sha], encoding=b"UTF-8")
            lines.insert(i, lines[i])
            text = b"\n".join(lines)
            if lines[i].startswith(b"parent"):
                # duplicate parents are ok for now
                self.assertCheckSucceeds(Commit, text)
            else:
                self.assertCheckFails(Commit, text)

    def test_check_order(self) -> None:
        lines = self.make_commit_lines(parents=[a_sha], encoding=b"UTF-8")
        headers = lines[:5]
        rest = lines[5:]
        # of all possible permutations, ensure only the original succeeds
        for perm in permutations(headers):
            perm = list(perm)
            text = b"\n".join(perm + rest)
            if perm == headers:
                self.assertCheckSucceeds(Commit, text)
            else:
                self.assertCheckFails(Commit, text)

    def test_check_commit_with_unparseable_time(self) -> None:
        identity_with_wrong_time = (
            b"Igor Sysoev <igor@sysoev.ru> 18446743887488505614+42707004"
        )

        # Those fail at reading time
        self.assertCheckFails(
            Commit,
            self.make_commit_text(
                author=default_committer, committer=identity_with_wrong_time
            ),
        )
        self.assertCheckFails(
            Commit,
            self.make_commit_text(
                author=identity_with_wrong_time, committer=default_committer
            ),
        )

    def test_check_commit_with_overflow_date(self) -> None:
        """Date with overflow should raise an ObjectFormatException when checked."""
        identity_with_wrong_time = (
            b"Igor Sysoev <igor@sysoev.ru> 18446743887488505614 +42707004"
        )
        commit0 = Commit.from_string(
            self.make_commit_text(
                author=identity_with_wrong_time, committer=default_committer
            )
        )
        commit1 = Commit.from_string(
            self.make_commit_text(
                author=default_committer, committer=identity_with_wrong_time
            )
        )

        # Those fails when triggering the check() method
        for commit in [commit0, commit1]:
            with self.assertRaises(ObjectFormatException):
                commit.check()

    def test_mangled_author_line(self) -> None:
        """Mangled author line should successfully parse."""
        author_line = (
            b'Karl MacMillan <kmacmill@redhat.com> <"Karl MacMillan '
            b'<kmacmill@redhat.com>"> 1197475547 -0500'
        )
        expected_identity = (
            b'Karl MacMillan <kmacmill@redhat.com> <"Karl MacMillan '
            b'<kmacmill@redhat.com>">'
        )
        commit = Commit.from_string(self.make_commit_text(author=author_line))

        # The commit parses properly
        self.assertEqual(commit.author, expected_identity)

        # But the check fails because the author identity is bogus
        with self.assertRaises(ObjectFormatException):
            commit.check()

    def test_parse_gpgsig(self) -> None:
        pre_sig = b"""tree aaff74984cccd156a469afa7d9ab10e4777beb24
author Jelmer Vernooij <jelmer@samba.org> 1412179807 +0200
committer Jelmer Vernooij <jelmer@samba.org> 1412179807 +0200
"""
        git_sig = b"""\
gpgsig -----BEGIN PGP SIGNATURE-----
 Version: GnuPG v1
 
 iQIcBAABCgAGBQJULCdfAAoJEACAbyvXKaRXuKwP/RyP9PA49uAvu8tQVCC/uBa8
 vi975+xvO14R8Pp8k2nps7lSxCdtCd+xVT1VRHs0wNhOZo2YCVoU1HATkPejqSeV
 NScTHcxnk4/+bxyfk14xvJkNp7FlQ3npmBkA+lbV0Ubr33rvtIE5jiJPyz+SgWAg
 xdBG2TojV0squj00GoH/euK6aX7GgZtwdtpTv44haCQdSuPGDcI4TORqR6YSqvy3
 GPE+3ZqXPFFb+KILtimkxitdwB7CpwmNse2vE3rONSwTvi8nq3ZoQYNY73CQGkUy
 qoFU0pDtw87U3niFin1ZccDgH0bB6624sLViqrjcbYJeg815Htsu4rmzVaZADEVC
 XhIO4MThebusdk0AcNGjgpf3HRHk0DPMDDlIjm+Oao0cqovvF6VyYmcb0C+RmhJj
 dodLXMNmbqErwTk3zEkW0yZvNIYXH7m9SokPCZa4eeIM7be62X6h1mbt0/IU6Th+
 v18fS0iTMP/Viug5und+05C/v04kgDo0CPphAbXwWMnkE4B6Tl9sdyUYXtvQsL7x
 0+WP1gL27ANqNZiI07Kz/BhbBAQI/+2TFT7oGr0AnFPQ5jHp+3GpUf6OKuT1wT3H
 ND189UFuRuubxb42vZhpcXRbqJVWnbECTKVUPsGZqat3enQUB63uM4i6/RdONDZA
 fDeF1m4qYs+cUXKNUZ03
 =X6RT
 -----END PGP SIGNATURE-----
"""
        post_sig = b"""\

foo
"""
        c = Commit.from_string(pre_sig + git_sig + post_sig)
        self.assertEqual(pre_sig + post_sig, c.raw_without_sig())
        self.assertEqual(pre_sig + git_sig + post_sig, bytes(c))
        self.assertEqual(b"foo\n", c.message)
        self.assertEqual([], c._extra)
        self.assertEqual(
            b"""-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQIcBAABCgAGBQJULCdfAAoJEACAbyvXKaRXuKwP/RyP9PA49uAvu8tQVCC/uBa8
vi975+xvO14R8Pp8k2nps7lSxCdtCd+xVT1VRHs0wNhOZo2YCVoU1HATkPejqSeV
NScTHcxnk4/+bxyfk14xvJkNp7FlQ3npmBkA+lbV0Ubr33rvtIE5jiJPyz+SgWAg
xdBG2TojV0squj00GoH/euK6aX7GgZtwdtpTv44haCQdSuPGDcI4TORqR6YSqvy3
GPE+3ZqXPFFb+KILtimkxitdwB7CpwmNse2vE3rONSwTvi8nq3ZoQYNY73CQGkUy
qoFU0pDtw87U3niFin1ZccDgH0bB6624sLViqrjcbYJeg815Htsu4rmzVaZADEVC
XhIO4MThebusdk0AcNGjgpf3HRHk0DPMDDlIjm+Oao0cqovvF6VyYmcb0C+RmhJj
dodLXMNmbqErwTk3zEkW0yZvNIYXH7m9SokPCZa4eeIM7be62X6h1mbt0/IU6Th+
v18fS0iTMP/Viug5und+05C/v04kgDo0CPphAbXwWMnkE4B6Tl9sdyUYXtvQsL7x
0+WP1gL27ANqNZiI07Kz/BhbBAQI/+2TFT7oGr0AnFPQ5jHp+3GpUf6OKuT1wT3H
ND189UFuRuubxb42vZhpcXRbqJVWnbECTKVUPsGZqat3enQUB63uM4i6/RdONDZA
fDeF1m4qYs+cUXKNUZ03
=X6RT
-----END PGP SIGNATURE-----""",
            c.gpgsig,
        )

    def test_parse_header_trailing_newline(self) -> None:
        pre_sig = b"""\
tree a7d6277f78d3ecd0230a1a5df6db00b1d9c521ac
parent c09b6dec7a73760fbdb478383a3c926b18db8bbe
author Neil Matatall <oreoshake@github.com> 1461964057 -1000
committer Neil Matatall <oreoshake@github.com> 1461964057 -1000
"""
        git_sig = b"""\
gpgsig -----BEGIN PGP SIGNATURE-----
 
 wsBcBAABCAAQBQJXI80ZCRA6pcNDcVZ70gAAarcIABs72xRX3FWeox349nh6ucJK
 CtwmBTusez2Zwmq895fQEbZK7jpaGO5TRO4OvjFxlRo0E08UFx3pxZHSpj6bsFeL
 hHsDXnCaotphLkbgKKRdGZo7tDqM84wuEDlh4MwNe7qlFC7bYLDyysc81ZX5lpMm
 2MFF1TvjLAzSvkT7H1LPkuR3hSvfCYhikbPOUNnKOo0sYjeJeAJ/JdAVQ4mdJIM0
 gl3REp9+A+qBEpNQI7z94Pg5Bc5xenwuDh3SJgHvJV6zBWupWcdB3fAkVd4TPnEZ
 nHxksHfeNln9RKseIDcy4b2ATjhDNIJZARHNfr6oy4u3XPW4svRqtBsLoMiIeuI=
 =ms6q
 -----END PGP SIGNATURE-----
 
"""
        post_sig = b"""\

3.3.0 version bump and docs
"""
        gpgsig = b"""\
-----BEGIN PGP SIGNATURE-----

wsBcBAABCAAQBQJXI80ZCRA6pcNDcVZ70gAAarcIABs72xRX3FWeox349nh6ucJK
CtwmBTusez2Zwmq895fQEbZK7jpaGO5TRO4OvjFxlRo0E08UFx3pxZHSpj6bsFeL
hHsDXnCaotphLkbgKKRdGZo7tDqM84wuEDlh4MwNe7qlFC7bYLDyysc81ZX5lpMm
2MFF1TvjLAzSvkT7H1LPkuR3hSvfCYhikbPOUNnKOo0sYjeJeAJ/JdAVQ4mdJIM0
gl3REp9+A+qBEpNQI7z94Pg5Bc5xenwuDh3SJgHvJV6zBWupWcdB3fAkVd4TPnEZ
nHxksHfeNln9RKseIDcy4b2ATjhDNIJZARHNfr6oy4u3XPW4svRqtBsLoMiIeuI=
=ms6q
-----END PGP SIGNATURE-----\n"""

        c = Commit.from_string(pre_sig + git_sig + post_sig)
        self.assertEqual([], c._extra)
        self.assertEqual(pre_sig + git_sig + post_sig, c.as_raw_string())
        self.assertEqual(pre_sig + post_sig, c.raw_without_sig())
        self.assertEqual(gpgsig, c.gpgsig)
        self.assertEqual(b"3.3.0 version bump and docs\n", c.message)

    def test_commit_extract_signature_pgp(self) -> None:
        from dulwich.objects import SIGNATURE_PGP

        gpgsig = b"""-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQIcBAABCgAGBQJULCdfAAoJEACAbyvXKaRXuKwP/RyP9PA49uAvu8tQVCC/uBa8
vi975+xvO14R8Pp8k2nps7lSxCdtCd+xVT1VRHs0wNhOZo2YCVoU1HATkPejqSeV
NScTHcxnk4/+bxyfk14xvJkNp7FlQ3npmBkA+lbV0Ubr33rvtIE5jiJPyz+SgWAg
-----END PGP SIGNATURE-----"""

        c = Commit()
        c.tree = b"d80c186a03f423a81b39df39dc87fd269736ca86"
        c.parents = [
            b"ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd",
            b"4cffe90e0a41ad3f5190079d7c8f036bde29cbe6",
        ]
        c.author = c.committer = b"James Westby <jw+debian@jameswestby.net>"
        c.commit_time = c.author_time = 1174773719
        c.commit_timezone = c.author_timezone = 0
        c.message = b"Merge ../b\n"
        c.gpgsig = gpgsig

        payload, signature, sig_type = c.extract_signature()
        self.assertEqual(payload, c.raw_without_sig())
        self.assertEqual(signature, gpgsig)
        self.assertEqual(sig_type, SIGNATURE_PGP)

    def test_commit_extract_signature_ssh(self) -> None:
        from dulwich.objects import SIGNATURE_SSH

        ssh_sig = b"""-----BEGIN SSH SIGNATURE-----
U1NIU0lHAAAAAQAAADMAAAALc3NoLWVkMjU1MTkAAAAgJwKO3yOmR5JlXCyN5bys
ZTpDKBGsVP6ydcKdZxAvJlUAAAAEZmlsZQAAAAAAAAAGc2hhNTEyAAAAUwAAAAtz
-----END SSH SIGNATURE-----"""

        c = Commit()
        c.tree = b"d80c186a03f423a81b39df39dc87fd269736ca86"
        c.parents = []
        c.author = c.committer = b"Test User <test@example.com>"
        c.commit_time = c.author_time = 1234567890
        c.commit_timezone = c.author_timezone = 0
        c.message = b"Test commit with SSH signature\n"
        c.gpgsig = ssh_sig

        payload, signature, sig_type = c.extract_signature()
        self.assertEqual(payload, c.raw_without_sig())
        self.assertEqual(signature, ssh_sig)
        self.assertEqual(sig_type, SIGNATURE_SSH)

    def test_commit_extract_signature_none(self) -> None:
        c = Commit()
        c.tree = b"d80c186a03f423a81b39df39dc87fd269736ca86"
        c.parents = []
        c.author = c.committer = b"Test User <test@example.com>"
        c.commit_time = c.author_time = 1234567890
        c.commit_timezone = c.author_timezone = 0
        c.message = b"Test commit without signature\n"

        payload, signature, sig_type = c.extract_signature()
        self.assertEqual(payload, c.as_raw_string())
        self.assertIsNone(signature)
        self.assertIsNone(sig_type)

    def test_commit_extract_signature_unknown(self) -> None:
        from dulwich.objects import ObjectFormatException

        unknown_sig = b"UNKNOWN SIGNATURE FORMAT DATA"

        c = Commit()
        c.tree = b"d80c186a03f423a81b39df39dc87fd269736ca86"
        c.parents = []
        c.author = c.committer = b"Test User <test@example.com>"
        c.commit_time = c.author_time = 1234567890
        c.commit_timezone = c.author_timezone = 0
        c.message = b"Test commit with unknown signature\n"
        c.gpgsig = unknown_sig

        # Unknown signature format should raise an exception
        with self.assertRaises(ObjectFormatException):
            c.extract_signature()

    def test_parse_time_entry_broken_negative_date(self) -> None:
        from dulwich.objects import parse_time_entry_broken

        author_line = b"Jane Doe <jdoe@example.org> -12345 +0100"
        expected_identity = b"Jane Doe <jdoe@example.org>"
        expected_time = -12345
        expected_timezone = +1 * 60 * 60

        person, time, (timezone, timezone_neg_utc) = parse_time_entry_broken(
            author_line
        )

        self.assertEqual(person, expected_identity)
        self.assertEqual(time, expected_time)
        self.assertEqual(timezone, expected_timezone)
        self.assertFalse(timezone_neg_utc)

    def test_parse_time_entry_broken_double_negative_timezone(self) -> None:
        from dulwich.objects import parse_time_entry_broken

        author_line = b"Jane Doe <jdoe@example.org> 12345 --700"
        expected_identity = b"Jane Doe <jdoe@example.org>"
        expected_time = 12345
        expected_timezone = +7 * 60 * 60

        person, time, (timezone, timezone_neg_utc) = parse_time_entry_broken(
            author_line
        )

        self.assertEqual(person, expected_identity)
        self.assertEqual(time, expected_time)
        self.assertEqual(timezone, expected_timezone)
        self.assertTrue(timezone_neg_utc)

    def test_parse_time_entry_broken_long_timezone(self) -> None:
        from dulwich.objects import parse_time_entry_broken

        author_line = (
            b"Geoff Cant <nem@lisp.geek.nz> 1170648114 -72000"  # codespell:ignore
        )
        expected_identity = b"Geoff Cant <nem@lisp.geek.nz>"  # codespell:ignore
        expected_time = 1170648114
        expected_timezone = -720 * 60 * 60

        person, time, (timezone, _timezone_neg_utc) = parse_time_entry_broken(
            author_line
        )

        self.assertEqual(person, expected_identity)
        self.assertEqual(time, expected_time)
        self.assertEqual(timezone, expected_timezone)

    def test_parse_time_entry_broken_short_timezone(self) -> None:
        from dulwich.objects import parse_time_entry_broken

        author_line = (
            b"Pl\xc3\xa1cidoMonteiro <Pl\xc3\xa1cidoMonteiro@.(none)> 1380083482 +02"
        )
        expected_identity = b"Pl\xc3\xa1cidoMonteiro <Pl\xc3\xa1cidoMonteiro@.(none)>"
        expected_time = 1380083482
        expected_timezone = +2 * 60

        person, time, (timezone, _timezone_neg_utc) = parse_time_entry_broken(
            author_line
        )

        self.assertEqual(person, expected_identity)
        self.assertEqual(time, expected_time)
        self.assertEqual(timezone, expected_timezone)

    def test_parse_time_entry_broken_unsigned_timezone(self) -> None:
        from dulwich.objects import parse_time_entry_broken

        author_line = (
            b"applehq <applehq@203d044e-caa7-11dc-91ec-67e1038599e7> 1205785941 0000"
        )
        expected_identity = b"applehq <applehq@203d044e-caa7-11dc-91ec-67e1038599e7>"
        expected_time = 1205785941
        expected_timezone = 0

        person, time, (timezone, _timezone_neg_utc) = parse_time_entry_broken(
            author_line
        )

        self.assertEqual(person, expected_identity)
        self.assertEqual(time, expected_time)
        self.assertEqual(timezone, expected_timezone)

    def test_parse_time_entry_broken_nonsensical_timezone(self) -> None:
        """Timezone is 'UTC + 5 hours and 75 minutes'."""
        from dulwich.objects import parse_time_entry_broken

        author_line = b"acpmasquerade <d@picovico.com> 1460127297 +0575"
        expected_identity = b"acpmasquerade <d@picovico.com>"
        expected_time = 1460127297
        expected_timezone = +6 * 60 * 60 + 15 * 60

        person, time, (timezone, _timezone_neg_utc) = parse_time_entry_broken(
            author_line
        )

        self.assertEqual(person, expected_identity)
        self.assertEqual(time, expected_time)
        self.assertEqual(timezone, expected_timezone)

    def test_parse_time_entry_broken_missing_brackets(self) -> None:
        from dulwich.objects import parse_time_entry_broken

        author_line = b"kapil.foss@gmail.com 1297013737 -0500"
        expected_identity = b"kapil.foss@gmail.com"
        expected_time = 1297013737
        expected_timezone = -5 * 60 * 60

        person, time, (timezone, _timezone_neg_utc) = parse_time_entry_broken(
            author_line
        )

        self.assertEqual(person, expected_identity)
        self.assertEqual(time, expected_time)
        self.assertEqual(timezone, expected_timezone)


class BrokenCommitParseTests(TestCase):
    """Tests for parsing commits with broken author/committer lines using parse_commit_broken."""

    def make_commit_text(
        self,
        tree=b"d80c186a03f423a81b39df39dc87fd269736ca86",
        parents=None,
        author=b"Test User <test@example.com> 1234567890 +0000",
        committer=b"Test User <test@example.com> 1234567890 +0000",
        encoding=None,
        message=b"Test commit\n",
        extra=None,
    ):
        lines = []
        if tree is not None:
            lines.append(b"tree " + tree)
        if parents is not None:
            lines.extend(b"parent " + p for p in parents)
        if author is not None:
            lines.append(b"author " + author)
        if committer is not None:
            lines.append(b"committer " + committer)
        if encoding is not None:
            lines.append(b"encoding " + encoding)
        if extra is not None:
            for name, value in sorted(extra.items()):
                lines.append(name + b" " + value)
        lines.append(b"")
        if message is not None:
            lines.append(message)
        return b"\n".join(lines)

    def test_negative_timestamp(self) -> None:
        from dulwich.objects import parse_commit_broken

        author_line = b"Jane Doe <jdoe@example.org> -12345 +0100"
        commit_text = self.make_commit_text(author=author_line, committer=author_line)
        commit = parse_commit_broken(commit_text)

        self.assertEqual(commit.author, b"Jane Doe <jdoe@example.org>")
        self.assertEqual(commit.author_time, -12345)
        self.assertEqual(commit.author_timezone, +1 * 60 * 60)

    def test_double_negative_timezone(self) -> None:
        from dulwich.objects import parse_commit_broken

        author_line = b"Jane Doe <jdoe@example.org> 12345 --700"
        commit_text = self.make_commit_text(author=author_line, committer=author_line)
        commit = parse_commit_broken(commit_text)

        self.assertEqual(commit.author, b"Jane Doe <jdoe@example.org>")
        self.assertEqual(commit.author_time, 12345)
        self.assertEqual(commit.author_timezone, +7 * 60 * 60)
        self.assertTrue(commit._author_timezone_neg_utc)

    def test_long_timezone(self) -> None:
        from dulwich.objects import parse_commit_broken

        # Real example from https://github.com/lisp/geek-nz
        author_line = (
            b"Geoff Cant <nem@lisp.geek.nz> 1170648114 -72000"  # codespell:ignore
        )
        commit_text = self.make_commit_text(author=author_line, committer=author_line)
        commit = parse_commit_broken(commit_text)

        self.assertEqual(
            commit.author,
            b"Geoff Cant <nem@lisp.geek.nz>",  # codespell:ignore
        )
        self.assertEqual(commit.author_time, 1170648114)
        self.assertEqual(commit.author_timezone, -720 * 60 * 60)

    def test_short_timezone(self) -> None:
        from dulwich.objects import parse_commit_broken

        author_line = (
            b"Pl\xc3\xa1cidoMonteiro <Pl\xc3\xa1cidoMonteiro@.(none)> 1380083482 +02"
        )
        commit_text = self.make_commit_text(author=author_line, committer=author_line)
        commit = parse_commit_broken(commit_text)

        self.assertEqual(
            commit.author, b"Pl\xc3\xa1cidoMonteiro <Pl\xc3\xa1cidoMonteiro@.(none)>"
        )
        self.assertEqual(commit.author_time, 1380083482)
        self.assertEqual(commit.author_timezone, +2 * 60)

    def test_unsigned_timezone(self) -> None:
        from dulwich.objects import parse_commit_broken

        author_line = (
            b"applehq <applehq@203d044e-caa7-11dc-91ec-67e1038599e7> 1205785941 0000"
        )
        commit_text = self.make_commit_text(author=author_line, committer=author_line)
        commit = parse_commit_broken(commit_text)

        self.assertEqual(
            commit.author, b"applehq <applehq@203d044e-caa7-11dc-91ec-67e1038599e7>"
        )
        self.assertEqual(commit.author_time, 1205785941)
        self.assertEqual(commit.author_timezone, 0)

    def test_nonsensical_timezone(self) -> None:
        from dulwich.objects import parse_commit_broken

        # Timezone is 'UTC + 5 hours and 75 minutes'
        author_line = b"acpmasquerade <d@picovico.com> 1460127297 +0575"
        commit_text = self.make_commit_text(author=author_line, committer=author_line)
        commit = parse_commit_broken(commit_text)

        self.assertEqual(commit.author, b"acpmasquerade <d@picovico.com>")
        self.assertEqual(commit.author_time, 1460127297)
        self.assertEqual(commit.author_timezone, +6 * 60 * 60 + 15 * 60)

    def test_missing_angle_brackets(self) -> None:
        from dulwich.objects import parse_commit_broken

        # Real example from https://github.com/noderabbit-team/tasks
        author_line = b"kapil.foss@gmail.com 1297013737 -0500"
        commit_text = self.make_commit_text(author=author_line, committer=author_line)
        commit = parse_commit_broken(commit_text)

        self.assertEqual(commit.author, b"kapil.foss@gmail.com")
        self.assertEqual(commit.author_time, 1297013737)
        self.assertEqual(commit.author_timezone, -5 * 60 * 60)


_TREE_ITEMS = {
    b"a-c": (0o100755, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
    b"a.c": (0o100755, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
    b"aoc": (0o100755, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
    b"a": (stat.S_IFDIR, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
    b"a/c": (stat.S_IFDIR, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
}

_SORTED_TREE_ITEMS = [
    TreeEntry(b"a-c", 0o100755, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
    TreeEntry(b"a.c", 0o100755, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
    TreeEntry(b"a", stat.S_IFDIR, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
    TreeEntry(b"a/c", stat.S_IFDIR, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
    TreeEntry(b"aoc", 0o100755, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
]


_TREE_ITEMS_BUG_1325 = {
    b"dir": (stat.S_IFDIR | 0o644, b"5944b31ff85b415573d1a43eb942e2dea30ab8be"),
    b"dira": (0o100644, b"cf7a729ca69bfabd0995fc9b083e86a18215bd91"),
}


_SORTED_TREE_ITEMS_BUG_1325 = [
    TreeEntry(
        path=b"dir",
        mode=stat.S_IFDIR | 0o644,
        sha=b"5944b31ff85b415573d1a43eb942e2dea30ab8be",
    ),
    TreeEntry(
        path=b"dira", mode=0o100644, sha=b"cf7a729ca69bfabd0995fc9b083e86a18215bd91"
    ),
]


class TreeTests(ShaFileCheckTests):
    def test_add(self) -> None:
        myhexsha = b"d80c186a03f423a81b39df39dc87fd269736ca86"
        x = Tree()
        x.add(b"myname", 0o100755, myhexsha)
        self.assertEqual(x[b"myname"], (0o100755, myhexsha))
        self.assertEqual(b"100755 myname\0" + hex_to_sha(myhexsha), x.as_raw_string())

    def test_simple(self) -> None:
        myhexsha = b"d80c186a03f423a81b39df39dc87fd269736ca86"
        x = Tree()
        x[b"myname"] = (0o100755, myhexsha)
        self.assertEqual(b"100755 myname\0" + hex_to_sha(myhexsha), x.as_raw_string())
        self.assertEqual(b"100755 myname\0" + hex_to_sha(myhexsha), bytes(x))

    def test_tree_update_id(self) -> None:
        x = Tree()
        x[b"a.c"] = (0o100755, b"d80c186a03f423a81b39df39dc87fd269736ca86")
        self.assertEqual(b"0c5c6bc2c081accfbc250331b19e43b904ab9cdd", x.id)
        x[b"a.b"] = (stat.S_IFDIR, b"d80c186a03f423a81b39df39dc87fd269736ca86")
        self.assertEqual(b"07bfcb5f3ada15bbebdfa3bbb8fd858a363925c8", x.id)

    def test_tree_iteritems_dir_sort(self) -> None:
        x = Tree()
        for name, item in _TREE_ITEMS.items():
            x[name] = item
        self.assertEqual(_SORTED_TREE_ITEMS, x.items())

    def test_tree_items_dir_sort(self) -> None:
        x = Tree()
        for name, item in _TREE_ITEMS.items():
            x[name] = item
        self.assertEqual(_SORTED_TREE_ITEMS, x.items())

    def _do_test_parse_tree(self, parse_tree) -> None:
        dir = os.path.join(os.path.dirname(__file__), "..", "testdata", "trees")
        o = Tree.from_path(hex_to_filename(dir, tree_sha))
        self.assertEqual(
            [(b"a", 0o100644, a_sha), (b"b", 0o100644, b_sha)],
            list(parse_tree(o.as_raw_string(), 20)),
        )
        # test a broken tree that has a leading 0 on the file mode
        broken_tree = b"0100644 foo\0" + hex_to_sha(a_sha)

        def eval_parse_tree(*args, **kwargs):
            return list(parse_tree(*args, **kwargs))

        self.assertEqual([(b"foo", 0o100644, a_sha)], eval_parse_tree(broken_tree, 20))
        self.assertRaises(
            ObjectFormatException, eval_parse_tree, broken_tree, 20, strict=True
        )

    test_parse_tree = functest_builder(_do_test_parse_tree, _parse_tree_py)
    test_parse_tree_extension = ext_functest_builder(
        _do_test_parse_tree, _parse_tree_rs
    )

    def _do_test_sorted_tree_items(self, sorted_tree_items) -> None:
        def do_sort(entries, name_order):
            return list(sorted_tree_items(entries, name_order))

        actual = do_sort(_TREE_ITEMS, False)
        self.assertEqual(_SORTED_TREE_ITEMS, actual)
        self.assertIsInstance(actual[0], TreeEntry)

        actual = do_sort(_TREE_ITEMS_BUG_1325, False)
        self.assertEqual(
            key_entry((b"a", (0o40644, b"cf7a729ca69bfabd0995fc9b083e86a18215bd91"))),
            b"a/",
        )
        self.assertEqual(_SORTED_TREE_ITEMS_BUG_1325, actual)
        self.assertIsInstance(actual[0], TreeEntry)

        # C/Python implementations may differ in specific error types, but
        # should all error on invalid inputs.
        # For example, the Rust implementation has stricter type checks, so may
        # raise TypeError where the Python implementation raises
        # AttributeError.
        errors = (TypeError, ValueError, AttributeError)
        self.assertRaises(errors, do_sort, b"foo", False)
        self.assertRaises(errors, do_sort, {b"foo": (1, 2, 3)}, False)

        myhexsha = b"d80c186a03f423a81b39df39dc87fd269736ca86"
        self.assertRaises(errors, do_sort, {b"foo": (b"xxx", myhexsha)}, False)
        self.assertRaises(errors, do_sort, {b"foo": (0o100755, 12345)}, False)

    test_sorted_tree_items = functest_builder(
        _do_test_sorted_tree_items, _sorted_tree_items_py
    )
    if _sorted_tree_items_rs is not None:
        assert _sorted_tree_items_rs != _sorted_tree_items_py
        test_sorted_tree_items_extension = ext_functest_builder(
            _do_test_sorted_tree_items, _sorted_tree_items_rs
        )

    def _do_test_sorted_tree_items_name_order(self, sorted_tree_items) -> None:
        self.assertEqual(
            [
                TreeEntry(
                    b"a",
                    stat.S_IFDIR,
                    b"d80c186a03f423a81b39df39dc87fd269736ca86",
                ),
                TreeEntry(
                    b"a-c",
                    0o100755,
                    b"d80c186a03f423a81b39df39dc87fd269736ca86",
                ),
                TreeEntry(
                    b"a.c",
                    0o100755,
                    b"d80c186a03f423a81b39df39dc87fd269736ca86",
                ),
                TreeEntry(
                    b"a/c",
                    stat.S_IFDIR,
                    b"d80c186a03f423a81b39df39dc87fd269736ca86",
                ),
                TreeEntry(
                    b"aoc",
                    0o100755,
                    b"d80c186a03f423a81b39df39dc87fd269736ca86",
                ),
            ],
            list(sorted_tree_items(_TREE_ITEMS, True)),
        )

    test_sorted_tree_items_name_order = functest_builder(
        _do_test_sorted_tree_items_name_order, _sorted_tree_items_py
    )
    if _sorted_tree_items_rs is not None:
        test_sorted_tree_items_name_order_extension = ext_functest_builder(
            _do_test_sorted_tree_items_name_order, _sorted_tree_items_rs
        )

    def _do_test_sorted_tree_items_issue_1325(self, sorted_tree_items) -> None:
        """Test case to reproduce issue #1325: submodules incorrectly sorted as directories.

        The bug: Rust uses (mode & 0o40000 != 0) which incorrectly matches
        submodules (0o160000) since 0o160000 & 0o40000 = 0o40000
        """
        # Test case 1: Minimal test - submodule vs file
        entries = {
            b"sub": (
                0o160000,
                b"a03f423a81b39df39dc87fd269736ca86d80c186",
            ),  # submodule
            b"sub.txt": (0o100644, b"81b39df39dc87fd269736ca86d80c186a03f423a"),  # file
        }

        result = list(sorted_tree_items(entries, False))
        paths = [entry.path for entry in result]

        # Submodules should sort as regular files, not directories
        # Expected order: sub, sub.txt
        # Bug causes: sub.txt, sub (because sub is treated as sub/)
        self.assertEqual([b"sub", b"sub.txt"], paths)

        # Test case 2: Scenario from issue - file rename + submodule
        # This simulates the "gamma" scenario mentioned in the issue
        entries2 = {
            b"alpha": (0o100644, b"a03f423a81b39df39dc87fd269736ca86d80c186"),
            b"beta": (0o100644, b"81b39df39dc87fd269736ca86d80c186a03f423a"),
            b"gamma": (
                0o160000,
                b"d80c186a03f423a81b39df39dc87fd269736ca86",
            ),  # submodule (was file)
            b"delta": (0o100644, b"cf7a729ca69bfabd0995fc9b083e86a18215bd91"),
        }

        result2 = list(sorted_tree_items(entries2, False))
        paths2 = [entry.path for entry in result2]

        # All entries should sort in alphabetical order since none are directories
        self.assertEqual([b"alpha", b"beta", b"delta", b"gamma"], paths2)

    test_sorted_tree_items_issue_1325 = functest_builder(
        _do_test_sorted_tree_items_issue_1325, _sorted_tree_items_py
    )
    if _sorted_tree_items_rs is not None:
        test_sorted_tree_items_issue_1325_extension = ext_functest_builder(
            _do_test_sorted_tree_items_issue_1325, _sorted_tree_items_rs
        )

    def test_sorted_tree_items_issue_1325_comparison(self) -> None:
        """Direct comparison test to show the difference between Python and Rust implementations."""
        if _sorted_tree_items_rs is None:
            self.skipTest("Rust extension not available")

        # Minimal test case: submodule vs file
        entries = {
            b"sub": (
                0o160000,
                b"a03f423a81b39df39dc87fd269736ca86d80c186",
            ),  # submodule
            b"sub.txt": (0o100644, b"81b39df39dc87fd269736ca86d80c186a03f423a"),  # file
        }

        # Get results from both implementations
        py_result = list(_sorted_tree_items_py(entries, False))
        rs_result = list(_sorted_tree_items_rs(entries, False))

        # Show the actual ordering from each
        py_paths = [entry.path for entry in py_result]
        rs_paths = [entry.path for entry in rs_result]

        # This test shows the bug: Rust treats submodules as directories
        self.assertEqual(
            py_paths, rs_paths, "Bug: Rust treats submodules (0o160000) as directories"
        )

    def test_check(self) -> None:
        t = Tree
        sha = hex_to_sha(a_sha)

        # filenames
        self.assertCheckSucceeds(t, b"100644 .a\0" + sha)
        self.assertCheckFails(t, b"100644 \0" + sha)
        self.assertCheckFails(t, b"100644 .\0" + sha)
        self.assertCheckFails(t, b"100644 a/a\0" + sha)
        self.assertCheckFails(t, b"100644 ..\0" + sha)
        self.assertCheckFails(t, b"100644 .git\0" + sha)

        # modes
        self.assertCheckSucceeds(t, b"100644 a\0" + sha)
        self.assertCheckSucceeds(t, b"100755 a\0" + sha)
        self.assertCheckSucceeds(t, b"160000 a\0" + sha)
        # TODO more whitelisted modes
        self.assertCheckFails(t, b"123456 a\0" + sha)
        self.assertCheckFails(t, b"123abc a\0" + sha)
        # should fail check, but parses ok
        self.assertCheckFails(t, b"0100644 foo\0" + sha)

        # shas
        self.assertCheckFails(t, b"100644 a\0" + (b"x" * 5))
        self.assertCheckFails(t, b"100644 a\0" + (b"x" * 18) + b"\0")
        self.assertCheckFails(t, b"100644 a\0" + (b"x" * 21) + b"\n100644 b\0" + sha)

        # ordering
        sha2 = hex_to_sha(b_sha)
        self.assertCheckSucceeds(t, b"100644 a\0" + sha + b"100644 b\0" + sha)
        self.assertCheckSucceeds(t, b"100644 a\0" + sha + b"100644 b\0" + sha2)
        self.assertCheckFails(t, b"100644 a\0" + sha + b"100755 a\0" + sha2)
        self.assertCheckFails(t, b"100644 b\0" + sha2 + b"100644 a\0" + sha)

    def test_iter(self) -> None:
        t = Tree()
        t[b"foo"] = (0o100644, a_sha)
        self.assertEqual({b"foo"}, set(t))


class TagSerializeTests(TestCase):
    def test_serialize_simple(self) -> None:
        x = make_object(
            Tag,
            tagger=b"Jelmer Vernooij <jelmer@samba.org>",
            name=b"0.1",
            message=b"Tag 0.1",
            object=(Blob, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
            tag_time=423423423,
            tag_timezone=0,
        )
        self.assertEqual(
            (
                b"object d80c186a03f423a81b39df39dc87fd269736ca86\n"
                b"type blob\n"
                b"tag 0.1\n"
                b"tagger Jelmer Vernooij <jelmer@samba.org> "
                b"423423423 +0000\n"
                b"\n"
                b"Tag 0.1"
            ),
            x.as_raw_string(),
        )

    def test_serialize_none_message(self) -> None:
        x = make_object(
            Tag,
            tagger=b"Jelmer Vernooij <jelmer@samba.org>",
            name=b"0.1",
            message=None,
            object=(Blob, b"d80c186a03f423a81b39df39dc87fd269736ca86"),
            tag_time=423423423,
            tag_timezone=0,
        )
        self.assertEqual(
            (
                b"object d80c186a03f423a81b39df39dc87fd269736ca86\n"
                b"type blob\n"
                b"tag 0.1\n"
                b"tagger Jelmer Vernooij <jelmer@samba.org> "
                b"423423423 +0000\n\n"
            ),
            x.as_raw_string(),
        )


default_tagger = (
    b"Linus Torvalds <torvalds@woody.linux-foundation.org> 1183319674 -0700"
)
default_message = b"""Linux 2.6.22-rc7
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBGiAaAF3YsRnbiHLsRAitMAKCiLboJkQECM/jpYsY3WPfvUgLXkACgg3ql
OK2XeQOiEeXtT76rV4t2WR4=
=ivrA
-----END PGP SIGNATURE-----
"""


class TagParseTests(ShaFileCheckTests):
    def make_tag_lines(
        self,
        object_sha=b"a38d6181ff27824c79fc7df825164a212eff6a3f",
        object_type_name=b"commit",
        name=b"v2.6.22-rc7",
        tagger=default_tagger,
        message=default_message,
    ):
        lines = []
        if object_sha is not None:
            lines.append(b"object " + object_sha)
        if object_type_name is not None:
            lines.append(b"type " + object_type_name)
        if name is not None:
            lines.append(b"tag " + name)
        if tagger is not None:
            lines.append(b"tagger " + tagger)
        if message is not None:
            lines.append(b"")
            lines.append(message)
        return lines

    def make_tag_text(self, **kwargs):
        return b"\n".join(self.make_tag_lines(**kwargs))

    def test_parse(self) -> None:
        x = Tag()
        x.set_raw_string(self.make_tag_text())
        self.assertEqual(
            b"Linus Torvalds <torvalds@woody.linux-foundation.org>", x.tagger
        )
        self.assertEqual(b"v2.6.22-rc7", x.name)
        object_type, object_sha = x.object
        self.assertEqual(b"a38d6181ff27824c79fc7df825164a212eff6a3f", object_sha)
        self.assertEqual(Commit, object_type)
        self.assertEqual(
            datetime.datetime.fromtimestamp(x.tag_time, datetime.timezone.utc).replace(
                tzinfo=None
            ),
            datetime.datetime(2007, 7, 1, 19, 54, 34),
        )
        self.assertEqual(-25200, x.tag_timezone)

    def test_parse_no_tagger(self) -> None:
        x = Tag()
        x.set_raw_string(self.make_tag_text(tagger=None))
        self.assertEqual(None, x.tagger)
        self.assertEqual(b"v2.6.22-rc7", x.name)
        self.assertEqual(None, x.tag_time)

    def test_parse_no_message(self) -> None:
        x = Tag()
        x.set_raw_string(self.make_tag_text(message=None))
        self.assertEqual(None, x.message)
        self.assertEqual(
            b"Linus Torvalds <torvalds@woody.linux-foundation.org>", x.tagger
        )
        self.assertEqual(
            datetime.datetime.fromtimestamp(x.tag_time, datetime.timezone.utc).replace(
                tzinfo=None
            ),
            datetime.datetime(2007, 7, 1, 19, 54, 34),
        )
        self.assertEqual(-25200, x.tag_timezone)
        self.assertEqual(b"v2.6.22-rc7", x.name)

    def test_check(self) -> None:
        self.assertCheckSucceeds(Tag, self.make_tag_text())
        self.assertCheckFails(Tag, self.make_tag_text(object_sha=None))
        self.assertCheckFails(Tag, self.make_tag_text(object_type_name=None))
        self.assertCheckFails(Tag, self.make_tag_text(name=None))
        self.assertCheckFails(Tag, self.make_tag_text(name=b""))
        self.assertCheckFails(Tag, self.make_tag_text(object_type_name=b"foobar"))
        self.assertCheckFails(
            Tag,
            self.make_tag_text(
                tagger=b"some guy without an email address 1183319674 -0700"
            ),
        )
        self.assertCheckFails(
            Tag,
            self.make_tag_text(
                tagger=(
                    b"Linus Torvalds <torvalds@woody.linux-foundation.org> "
                    b"Sun 7 Jul 2007 12:54:34 +0700"
                )
            ),
        )
        self.assertCheckFails(Tag, self.make_tag_text(object_sha=b"xxx"))

    def test_check_tag_with_unparseable_field(self) -> None:
        self.assertCheckFails(
            Tag,
            self.make_tag_text(
                tagger=(
                    b"Linus Torvalds <torvalds@woody.linux-foundation.org> 423423+0000"
                )
            ),
        )

    def test_check_tag_with_overflow_time(self) -> None:
        """Date with overflow should raise an ObjectFormatException when checked."""
        author = f"Some Dude <some@dude.org> {MAX_TIME + 1} +0000"
        tag = Tag.from_string(self.make_tag_text(tagger=(author.encode())))
        with self.assertRaises(ObjectFormatException):
            tag.check()

    def test_check_duplicates(self) -> None:
        # duplicate each of the header fields
        for i in range(4):
            lines = self.make_tag_lines()
            lines.insert(i, lines[i])
            self.assertCheckFails(Tag, b"\n".join(lines))

    def test_check_order(self) -> None:
        lines = self.make_tag_lines()
        headers = lines[:4]
        rest = lines[4:]
        # of all possible permutations, ensure only the original succeeds
        for perm in permutations(headers):
            perm = list(perm)
            text = b"\n".join(perm + rest)
            if perm == headers:
                self.assertCheckSucceeds(Tag, text)
            else:
                self.assertCheckFails(Tag, text)

    def test_tree_copy_after_update(self) -> None:
        """Check Tree.id is correctly updated when the tree is copied after updated."""
        shas = []
        tree = Tree()
        shas.append(tree.id)
        tree.add(b"data", 0o644, Blob().id)
        copied = tree.copy()
        shas.append(tree.id)
        shas.append(copied.id)

        self.assertNotIn(shas[0], shas[1:])
        self.assertEqual(shas[1], shas[2])

    def test_tag_withough_sig(self) -> None:
        x = Tag()
        x.set_raw_string(self.make_tag_text())
        self.assertEqual(bytes(x), x.raw_without_sig() + x.signature)
        self.assertEqual(
            b"""\
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBGiAaAF3YsRnbiHLsRAitMAKCiLboJkQECM/jpYsY3WPfvUgLXkACgg3ql
OK2XeQOiEeXtT76rV4t2WR4=
=ivrA
-----END PGP SIGNATURE-----
""",
            x.signature,
        )

    def test_tag_extract_signature_pgp(self) -> None:
        from dulwich.objects import SIGNATURE_PGP

        x = Tag()
        x.set_raw_string(self.make_tag_text())
        payload, signature, sig_type = x.extract_signature()
        self.assertEqual(payload, x.raw_without_sig())
        self.assertEqual(signature, x.signature)
        self.assertEqual(sig_type, SIGNATURE_PGP)

    def test_tag_extract_signature_ssh(self) -> None:
        from dulwich.objects import SIGNATURE_SSH

        tag_text_lines = self.make_tag_lines()
        # Replace PGP signature with SSH signature
        tag_text_lines[-1] = b"""\
-----BEGIN SSH SIGNATURE-----
U1NIU0lHAAAAAQAAADMAAAALc3NoLWVkMjU1MTkAAAAgJwKO3yOmR5JlXCyN5bys
ZTpDKBGsVP6ydcKdZxAvJlUAAAAEZmlsZQAAAAAAAAAGc2hhNTEyAAAAUwAAAAtz
-----END SSH SIGNATURE-----
"""
        tag_text = b"\n".join(tag_text_lines)
        x = Tag()
        x.set_raw_string(tag_text)
        payload, signature, sig_type = x.extract_signature()
        self.assertEqual(payload, x.raw_without_sig())
        self.assertEqual(signature, x.signature)
        self.assertEqual(sig_type, SIGNATURE_SSH)

    def test_tag_extract_signature_none(self) -> None:
        tag_lines = self.make_tag_lines(message=b"Test tag\n")
        x = Tag()
        x.set_raw_string(b"\n".join(tag_lines))
        payload, signature, sig_type = x.extract_signature()
        self.assertEqual(payload, bytes(x))
        self.assertIsNone(signature)
        self.assertIsNone(sig_type)

    def test_tag_extract_signature_unknown(self) -> None:
        from dulwich.objects import ObjectFormatException

        # Create a tag with a signature that has an unknown format
        # It needs to look like a signature to be detected but not be PGP or SSH
        tag_text = b"""object a38d6181ff27824c79fc7df825164a212eff6a3f
type commit
tag v2.6.22-rc7
tagger Linus Torvalds <torvalds@woody.linux-foundation.org> 1183319674 +0000

Linux 2.6.22-rc7
-----BEGIN UNKNOWN SIGNATURE-----
Some unknown signature format
-----END UNKNOWN SIGNATURE-----
"""
        x = Tag()
        # First we need to manually set the signature to test the extract_signature method
        x.set_raw_string(tag_text[: tag_text.index(b"-----BEGIN")])
        x._signature = b"-----BEGIN UNKNOWN SIGNATURE-----\nSome unknown signature format\n-----END UNKNOWN SIGNATURE-----\n"
        x._needs_serialization = False

        # Unknown signature format should raise an exception
        with self.assertRaises(ObjectFormatException):
            x.extract_signature()


class CheckTests(TestCase):
    def test_check_hexsha(self) -> None:
        check_hexsha(a_sha, "failed to check good sha")
        self.assertRaises(
            ObjectFormatException, check_hexsha, b"1" * 39, "sha too short"
        )
        self.assertRaises(
            ObjectFormatException, check_hexsha, b"1" * 41, "sha too long"
        )
        self.assertRaises(
            ObjectFormatException,
            check_hexsha,
            b"x" * 40,
            "invalid characters",
        )

    def test_check_identity(self) -> None:
        check_identity(
            b"Dave Borowitz <dborowitz@google.com>",
            "failed to check good identity",
        )
        check_identity(b" <dborowitz@google.com>", "failed to check good identity")
        self.assertRaises(
            ObjectFormatException,
            check_identity,
            b"<dborowitz@google.com>",
            "no space before email",
        )
        self.assertRaises(
            ObjectFormatException, check_identity, b"Dave Borowitz", "no email"
        )
        self.assertRaises(
            ObjectFormatException,
            check_identity,
            b"Dave Borowitz <dborowitz",
            "incomplete email",
        )
        self.assertRaises(
            ObjectFormatException,
            check_identity,
            b"dborowitz@google.com>",
            "incomplete email",
        )
        self.assertRaises(
            ObjectFormatException,
            check_identity,
            b"Dave Borowitz <<dborowitz@google.com>",
            "typo",
        )
        self.assertRaises(
            ObjectFormatException,
            check_identity,
            b"Dave Borowitz <dborowitz@google.com>>",
            "typo",
        )
        self.assertRaises(
            ObjectFormatException,
            check_identity,
            b"Dave Borowitz <dborowitz@google.com>xxx",
            "trailing characters",
        )
        self.assertRaises(
            ObjectFormatException,
            check_identity,
            b"Dave Borowitz <dborowitz@google.com>xxx",
            "trailing characters",
        )
        self.assertRaises(
            ObjectFormatException,
            check_identity,
            b"Dave<Borowitz <dborowitz@google.com>",
            "reserved byte in name",
        )
        self.assertRaises(
            ObjectFormatException,
            check_identity,
            b"Dave>Borowitz <dborowitz@google.com>",
            "reserved byte in name",
        )
        self.assertRaises(
            ObjectFormatException,
            check_identity,
            b"Dave\0Borowitz <dborowitz@google.com>",
            "null byte",
        )
        self.assertRaises(
            ObjectFormatException,
            check_identity,
            b"Dave\nBorowitz <dborowitz@google.com>",
            "newline byte",
        )


class TimezoneTests(TestCase):
    def test_parse_timezone_utc(self) -> None:
        self.assertEqual((0, False), parse_timezone(b"+0000"))

    def test_parse_timezone_utc_negative(self) -> None:
        self.assertEqual((0, True), parse_timezone(b"-0000"))

    def test_generate_timezone_utc(self) -> None:
        self.assertEqual(b"+0000", format_timezone(0))

    def test_generate_timezone_utc_negative(self) -> None:
        self.assertEqual(b"-0000", format_timezone(0, True))

    def test_parse_timezone_cet(self) -> None:
        self.assertEqual((60 * 60, False), parse_timezone(b"+0100"))

    def test_format_timezone_cet(self) -> None:
        self.assertEqual(b"+0100", format_timezone(60 * 60))

    def test_format_timezone_pdt(self) -> None:
        self.assertEqual(b"-0400", format_timezone(-4 * 60 * 60))

    def test_parse_timezone_pdt(self) -> None:
        self.assertEqual((-4 * 60 * 60, False), parse_timezone(b"-0400"))

    def test_format_timezone_pdt_half(self) -> None:
        self.assertEqual(b"-0440", format_timezone(((-4 * 60) - 40) * 60))

    def test_format_timezone_double_negative(self) -> None:
        self.assertEqual(b"--700", format_timezone(((7 * 60) * 60), True))

    def test_parse_timezone_pdt_half(self) -> None:
        self.assertEqual((((-4 * 60) - 40) * 60, False), parse_timezone(b"-0440"))

    def test_parse_timezone_double_negative(self) -> None:
        self.assertEqual((((7 * 60) * 60), False), parse_timezone(b"+700"))
        self.assertEqual((((7 * 60) * 60), True), parse_timezone(b"--700"))


class ShaFileCopyTests(TestCase):
    def assert_copy(self, orig) -> None:
        oclass = object_class(orig.type_num)

        copy = orig.copy()
        self.assertIsInstance(copy, oclass)
        self.assertEqual(copy, orig)
        self.assertIsNot(copy, orig)

    def test_commit_copy(self) -> None:
        attrs = {
            "tree": b"d80c186a03f423a81b39df39dc87fd269736ca86",
            "parents": [
                b"ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd",
                b"4cffe90e0a41ad3f5190079d7c8f036bde29cbe6",
            ],
            "author": b"James Westby <jw+debian@jameswestby.net>",
            "committer": b"James Westby <jw+debian@jameswestby.net>",
            "commit_time": 1174773719,
            "author_time": 1174773719,
            "commit_timezone": 0,
            "author_timezone": 0,
            "message": b"Merge ../b\n",
        }
        commit = make_commit(**attrs)
        self.assert_copy(commit)

    def test_blob_copy(self) -> None:
        blob = make_object(Blob, data=b"i am a blob")
        self.assert_copy(blob)

    def test_tree_copy(self) -> None:
        blob = make_object(Blob, data=b"i am a blob")
        tree = Tree()
        tree[b"blob"] = (stat.S_IFREG, blob.id)
        self.assert_copy(tree)

    def test_tag_copy(self) -> None:
        tag = make_object(
            Tag,
            name=b"tag",
            message=b"",
            tagger=b"Tagger <test@example.com>",
            tag_time=12345,
            tag_timezone=0,
            object=(Commit, ZERO_SHA),
        )
        self.assert_copy(tag)


class ShaFileSerializeTests(TestCase):
    """`ShaFile` objects only gets serialized once if they haven't changed."""

    @contextmanager
    def assert_serialization_on_change(
        self, obj, needs_serialization_after_change=True
    ):
        old_id = obj.id
        self.assertFalse(obj._needs_serialization)

        yield obj

        if needs_serialization_after_change:
            self.assertTrue(obj._needs_serialization)
        else:
            self.assertFalse(obj._needs_serialization)
        new_id = obj.id
        self.assertFalse(obj._needs_serialization)
        self.assertNotEqual(old_id, new_id)

    def test_commit_serialize(self) -> None:
        attrs = {
            "tree": b"d80c186a03f423a81b39df39dc87fd269736ca86",
            "parents": [
                b"ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd",
                b"4cffe90e0a41ad3f5190079d7c8f036bde29cbe6",
            ],
            "author": b"James Westby <jw+debian@jameswestby.net>",
            "committer": b"James Westby <jw+debian@jameswestby.net>",
            "commit_time": 1174773719,
            "author_time": 1174773719,
            "commit_timezone": 0,
            "author_timezone": 0,
            "message": b"Merge ../b\n",
        }
        commit = make_commit(**attrs)

        with self.assert_serialization_on_change(commit):
            commit.parents = [b"ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd"]

    def test_blob_serialize(self) -> None:
        blob = make_object(Blob, data=b"i am a blob")

        with self.assert_serialization_on_change(
            blob, needs_serialization_after_change=False
        ):
            blob.data = b"i am another blob"

    def test_tree_serialize(self) -> None:
        blob = make_object(Blob, data=b"i am a blob")
        tree = Tree()
        tree[b"blob"] = (stat.S_IFREG, blob.id)

        with self.assert_serialization_on_change(tree):
            tree[b"blob2"] = (stat.S_IFREG, blob.id)

    def test_tag_serialize(self) -> None:
        tag = make_object(
            Tag,
            name=b"tag",
            message=b"",
            tagger=b"Tagger <test@example.com>",
            tag_time=12345,
            tag_timezone=0,
            object=(Commit, ZERO_SHA),
        )

        with self.assert_serialization_on_change(tag):
            tag.message = b"new message"

    def test_tag_serialize_time_error(self) -> None:
        with self.assertRaises(ObjectFormatException):
            tag = make_object(
                Tag,
                name=b"tag",
                message=b"some message",
                tagger=b"Tagger <test@example.com> 1174773719+0000",
                object=(Commit, ZERO_SHA),
            )
            tag._deserialize(tag._serialize())


class PrettyFormatTreeEntryTests(TestCase):
    def test_format(self) -> None:
        self.assertEqual(
            "40000 tree 40820c38cfb182ce6c8b261555410d8382a5918b\tfoo\n",
            pretty_format_tree_entry(
                b"foo", 0o40000, b"40820c38cfb182ce6c8b261555410d8382a5918b"
            ),
        )