rustferry-github 0.1.0

Secure GitHub Actions workflow generation for RustFerry iPhone builds
Documentation
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
//! Private bare Git repository layout for isolated temporary-ref publication.

#[cfg(windows)]
mod windows {
    use std::{
        error::Error,
        ffi::OsString,
        fmt,
        fs::{self, File},
        io::{Read, Seek, SeekFrom, Write},
        os::windows::{
            fs::{MetadataExt as _, OpenOptionsExt as _},
            io::AsHandle as _,
        },
        path::{Component, Path, PathBuf},
    };

    use rustferry_core::{DirectoryFilesystemIdentity, RetainedDirectoryIdentity};
    use same_file::Handle as FileIdentityHandle;
    use sha2::{Digest, Sha256};
    use windows_sys::Win32::Storage::FileSystem::{
        FILE_ATTRIBUTE_REPARSE_POINT, FILE_FLAG_OPEN_REPARSE_POINT, FILE_SHARE_READ,
    };

    use crate::git_process::{
        GitNetworkPolicy, GitProcessContext, GitProcessPolicyError, GitProcessSpec,
        WindowsGitToolchain, external_path,
    };

    const BARE_DIRECTORY_NAME: &str = "repository.git";
    const HOME_DIRECTORY_NAME: &str = "home";
    const XDG_DIRECTORY_NAME: &str = "xdg";
    const TEMP_DIRECTORY_NAME: &str = "tmp";
    const TEMPLATE_DIRECTORY_NAME: &str = "empty-template";
    const SSH_DIRECTORY_NAME: &str = ".ssh";
    const MAX_CONTROL_FILE_BYTES: usize = 64 * 1024;
    const UNBORN_HEAD: &[u8] = b"ref: refs/heads/rustferry-unborn\n";

    /// Pinned GitHub.com host keys published by GitHub for noninteractive SSH verification.
    pub const GITHUB_KNOWN_HOSTS_V1: &str = concat!(
        "github.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl\n",
        "github.com ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBEmKSENjQEezOmxkZMy7opKgwFB9nkt5YRrYMjNuG5N87uRgg6CLrbo5wAdT/y6v0mKV0U2w0WZ2YB/++Tpockg=\n",
        "github.com ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCj7ndNxQowgcQnjshcLrqPEiiphnt+VTTvDP6mHBL9j1aNUkY4Ue1gvwnGLVlOhGeYrnZaMgRK6+PKCUXaDbC7qtbW8gIkhL7aGCsOr/C56SJMy/BCZfxd1nWzAOxSDPgVsmerOBYfNqltV9/hWCqBywINIR+5dIg6JTJ72pcEpEjcYgXkE2YEFXV1JHnsKgbLWNlhScqb2UmyRkQyytRLtL+38TGxkxCflmO+5Z8CSSNY7GidjMIZ7Q4zMjA2n1nGrlTDkzwDCsw+wqFPGQA179cnfGWOWRVruj16z6XyvxvjJwbz0wQZ75XK5tKSb7FNyeIEs4TT4jk+S4dhPeAUC5y+bDYirYgM4GC7uEnztnZyaVWQ7B381AK4Qdrwt51ZqExKbQpTUNn+EjqoTwvqNj4kqx5QUCI0ThS/YkOxJCXmPUWZbhjpCg56i+2aB6CmK2JGhn57K5mj0MNdBXA4/WnwH6XoPWJzK5Nyu2zB3nAZp+S5hpQs+p1vN1/wsjk=\n",
    );

    const SSH_CONFIG_V1_TEMPLATE: &str = concat!(
        "Host *\n",
        "    BatchMode yes\n",
        "    StrictHostKeyChecking yes\n",
        "    UserKnownHostsFile @RUSTFERRY_KNOWN_HOSTS@\n",
        "    GlobalKnownHostsFile none\n",
        "    HostKeyAlgorithms ssh-ed25519,ecdsa-sha2-nistp256,rsa-sha2-512,rsa-sha2-256\n",
        "    PreferredAuthentications publickey\n",
        "    IdentityFile none\n",
        "    PasswordAuthentication no\n",
        "    KbdInteractiveAuthentication no\n",
        "    ProxyCommand none\n",
        "    ProxyJump none\n",
        "    PermitLocalCommand no\n",
        "    ForwardAgent no\n",
        "    ClearAllForwardings yes\n",
        "    RequestTTY no\n",
        "    AddKeysToAgent no\n",
        "    UpdateHostKeys no\n",
        "    VerifyHostKeyDNS no\n",
        "    CheckHostIP no\n",
        "    CanonicalizeHostname no\n",
        "    ConnectionAttempts 1\n",
        "    ConnectTimeout 30\n",
        "    LogLevel ERROR\n",
        "Host github.com\n",
        "    HostName github.com\n",
        "    HostKeyAlias github.com\n",
        "    User git\n",
        "    Port 22\n",
    );

    fn ssh_config_v1(known_hosts: &Path) -> Result<Vec<u8>, PrivateGitRepositoryError> {
        let external = external_path(known_hosts);
        let value = external
            .to_str()
            .ok_or(PrivateGitRepositoryError::InvalidControlFile)?
            .replace('\\', "/");
        if !known_hosts.is_absolute()
            || value.is_empty()
            || value
                .bytes()
                .any(|byte| byte.is_ascii_control() || matches!(byte, b'%' | b'$' | b'"'))
        {
            return Err(PrivateGitRepositoryError::InvalidControlFile);
        }
        let rendered =
            SSH_CONFIG_V1_TEMPLATE.replace("@RUSTFERRY_KNOWN_HOSTS@", &format!("\"{value}\""));
        if rendered.len() > MAX_CONTROL_FILE_BYTES {
            return Err(PrivateGitRepositoryError::InvalidControlFile);
        }
        Ok(rendered.into_bytes())
    }

    /// Canonical paths inside one private Git isolation root.
    #[derive(Clone, Debug, Eq, PartialEq)]
    pub struct PrivateGitRepositoryPaths {
        root: PathBuf,
        bare: PathBuf,
        home: PathBuf,
        xdg: PathBuf,
        temp: PathBuf,
        template: PathBuf,
        ssh: PathBuf,
        ssh_config: PathBuf,
        known_hosts: PathBuf,
    }

    impl PrivateGitRepositoryPaths {
        fn new(root: PathBuf) -> Self {
            let bare = root.join(BARE_DIRECTORY_NAME);
            let home = root.join(HOME_DIRECTORY_NAME);
            let xdg = root.join(XDG_DIRECTORY_NAME);
            let temp = root.join(TEMP_DIRECTORY_NAME);
            let template = root.join(TEMPLATE_DIRECTORY_NAME);
            let ssh = home.join(SSH_DIRECTORY_NAME);
            let ssh_config = ssh.join("config");
            let known_hosts = ssh.join("known_hosts");
            Self {
                root,
                bare,
                home,
                xdg,
                temp,
                template,
                ssh,
                ssh_config,
                known_hosts,
            }
        }

        /// Private isolation root.
        pub fn root(&self) -> &Path {
            &self.root
        }

        /// Private bare repository directory.
        pub fn bare(&self) -> &Path {
            &self.bare
        }

        /// Empty private home used for Git, GCM, and OpenSSH.
        pub fn home(&self) -> &Path {
            &self.home
        }

        /// Private XDG configuration root.
        pub fn xdg(&self) -> &Path {
            &self.xdg
        }

        /// Private process temporary directory.
        pub fn temp(&self) -> &Path {
            &self.temp
        }

        /// Empty template directory used only by `git init`.
        pub fn template(&self) -> &Path {
            &self.template
        }

        /// Pinned private OpenSSH configuration file.
        pub fn ssh_config(&self) -> &Path {
            &self.ssh_config
        }

        /// Pinned private GitHub known-hosts file.
        pub fn known_hosts(&self) -> &Path {
            &self.known_hosts
        }
    }

    struct PrivateDirectoryGuard {
        path: PathBuf,
        identity: DirectoryFilesystemIdentity,
        retained: RetainedDirectoryIdentity,
        private_handle: Option<File>,
    }

    impl PrivateDirectoryGuard {
        fn open(path: &Path) -> Result<Self, PrivateGitRepositoryError> {
            let path = canonical_private_directory(path)?;
            let private_handle =
                rustferry_core::windows_private_directory::open_private_directory_read_guard(&path)
                    .map_err(|_| PrivateGitRepositoryError::UnsafeDirectory)?;
            let retained = RetainedDirectoryIdentity::open(&path)
                .map_err(|_| PrivateGitRepositoryError::UnsafeDirectory)?;
            let identity = retained.identity().clone();
            Ok(Self {
                path,
                identity,
                retained,
                private_handle: Some(private_handle),
            })
        }

        fn open_git_generated(path: &Path) -> Result<Self, PrivateGitRepositoryError> {
            let path = canonical_directory(path)?;
            let retained = RetainedDirectoryIdentity::open(&path)
                .map_err(|_| PrivateGitRepositoryError::UnsafeDirectory)?;
            let identity = retained.identity().clone();
            Ok(Self {
                path,
                identity,
                retained,
                private_handle: None,
            })
        }

        fn verify(&self) -> Result<(), PrivateGitRepositoryError> {
            if let Some(private_handle) = &self.private_handle {
                rustferry_core::windows_private_directory::verify_private_directory_handle(
                    private_handle.as_handle(),
                )
                .map_err(|_| PrivateGitRepositoryError::DirectoryIdentityChanged)?;
            }
            self.retained
                .verify_path(&self.path)
                .map_err(|_| PrivateGitRepositoryError::DirectoryIdentityChanged)?;
            if self.retained.identity() != &self.identity {
                return Err(PrivateGitRepositoryError::DirectoryIdentityChanged);
            }
            Ok(())
        }
    }

    impl fmt::Debug for PrivateDirectoryGuard {
        fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
            formatter
                .debug_struct("PrivateDirectoryGuard")
                .field("name", &self.path.file_name())
                .field("identity", &self.identity)
                .finish_non_exhaustive()
        }
    }

    struct ExactPrivateFileGuard {
        path: PathBuf,
        identity: FileIdentityHandle,
        sha256: String,
        expected: Vec<u8>,
        file: File,
        require_private_acl: bool,
    }

    impl ExactPrivateFileGuard {
        fn open(path: &Path, expected: &[u8]) -> Result<Self, PrivateGitRepositoryError> {
            if expected.len() > MAX_CONTROL_FILE_BYTES {
                return Err(PrivateGitRepositoryError::InvalidControlFile);
            }
            let file = rustferry_core::windows_private_directory::open_private_file(path)
                .map_err(|_| PrivateGitRepositoryError::UnsafeControlFile)?;
            let identity = FileIdentityHandle::from_file(
                file.try_clone()
                    .map_err(|_| PrivateGitRepositoryError::UnsafeControlFile)?,
            )
            .map_err(|_| PrivateGitRepositoryError::UnsafeControlFile)?;
            if FileIdentityHandle::from_path(path)
                .map_err(|_| PrivateGitRepositoryError::UnsafeControlFile)?
                != identity
            {
                return Err(PrivateGitRepositoryError::ControlFileIdentityChanged);
            }
            let actual = read_bounded_file(&file)?;
            if actual != expected {
                return Err(PrivateGitRepositoryError::InvalidControlFile);
            }
            Ok(Self {
                path: path.to_owned(),
                identity,
                sha256: hex::encode(Sha256::digest(expected)),
                expected: expected.to_vec(),
                file,
                require_private_acl: true,
            })
        }

        fn open_git_generated(
            path: &Path,
            expected: &[u8],
        ) -> Result<Self, PrivateGitRepositoryError> {
            if expected.len() > MAX_CONTROL_FILE_BYTES {
                return Err(PrivateGitRepositoryError::InvalidControlFile);
            }
            let file = open_retained_generated_file(path)?;
            let identity = FileIdentityHandle::from_file(
                file.try_clone()
                    .map_err(|_| PrivateGitRepositoryError::UnsafeControlFile)?,
            )
            .map_err(|_| PrivateGitRepositoryError::UnsafeControlFile)?;
            if FileIdentityHandle::from_path(path)
                .map_err(|_| PrivateGitRepositoryError::UnsafeControlFile)?
                != identity
            {
                return Err(PrivateGitRepositoryError::ControlFileIdentityChanged);
            }
            let actual = read_bounded_file(&file)?;
            if actual != expected {
                return Err(PrivateGitRepositoryError::InvalidControlFile);
            }
            Ok(Self {
                path: path.to_owned(),
                identity,
                sha256: hex::encode(Sha256::digest(expected)),
                expected: expected.to_vec(),
                file,
                require_private_acl: false,
            })
        }

        fn verify(&self) -> Result<(), PrivateGitRepositoryError> {
            if self.require_private_acl {
                rustferry_core::windows_private_directory::verify_private_file_handle(
                    self.file.as_handle(),
                )
                .map_err(|_| PrivateGitRepositoryError::ControlFileIdentityChanged)?;
            }
            if FileIdentityHandle::from_path(&self.path)
                .map_err(|_| PrivateGitRepositoryError::ControlFileIdentityChanged)?
                != self.identity
                || read_bounded_file(&self.file)? != self.expected
                || hex::encode(Sha256::digest(&self.expected)) != self.sha256
            {
                return Err(PrivateGitRepositoryError::ControlFileIdentityChanged);
            }
            Ok(())
        }
    }

    impl fmt::Debug for ExactPrivateFileGuard {
        fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
            formatter
                .debug_struct("ExactPrivateFileGuard")
                .field("name", &self.path.file_name())
                .field("sha256", &self.sha256)
                .finish_non_exhaustive()
        }
    }

    /// Fresh or already initialized private layout retained across `git init`.
    #[derive(Debug)]
    pub struct PrivateGitRepositoryPreparation {
        paths: PrivateGitRepositoryPaths,
        directories: Vec<PrivateDirectoryGuard>,
        ssh_config: ExactPrivateFileGuard,
        known_hosts: ExactPrivateFileGuard,
        needs_initialization: bool,
    }

    impl PrivateGitRepositoryPreparation {
        /// Create or reopen fixed private directories and pinned SSH policy files.
        ///
        /// The isolation root must already satisfy `RustFerry`'s strict Windows private-directory
        /// policy. A partially initialized bare repository fails closed and is never reinitialized.
        ///
        /// # Errors
        ///
        /// Rejects unsafe ACLs, reparse points, path replacement, unexpected files, or partial Git
        /// initialization state.
        pub fn prepare(root: impl AsRef<Path>) -> Result<Self, PrivateGitRepositoryError> {
            let root = canonical_private_directory(root.as_ref())?;
            let paths = PrivateGitRepositoryPaths::new(root);
            let mut directories = vec![PrivateDirectoryGuard::open(&paths.root)?];
            for directory in [
                &paths.bare,
                &paths.home,
                &paths.xdg,
                &paths.temp,
                &paths.template,
                &paths.ssh,
            ] {
                create_or_open_private_directory(directory)?;
                directories.push(PrivateDirectoryGuard::open(directory)?);
            }
            ensure_directory_empty(&paths.template)?;
            let expected_ssh_config = ssh_config_v1(&paths.known_hosts)?;
            ensure_exact_private_file(&paths.ssh_config, &expected_ssh_config)?;
            ensure_exact_private_file(&paths.known_hosts, GITHUB_KNOWN_HOSTS_V1.as_bytes())?;
            let ssh_config = ExactPrivateFileGuard::open(&paths.ssh_config, &expected_ssh_config)?;
            let known_hosts =
                ExactPrivateFileGuard::open(&paths.known_hosts, GITHUB_KNOWN_HOSTS_V1.as_bytes())?;

            let config_exists = paths.bare.join("config").try_exists().unwrap_or(false);
            let head_exists = paths.bare.join("HEAD").try_exists().unwrap_or(false);
            let needs_initialization = match (config_exists, head_exists) {
                (false, false) => {
                    ensure_directory_empty(&paths.bare)?;
                    true
                }
                (true, true) => false,
                (true, false) | (false, true) => {
                    return Err(PrivateGitRepositoryError::PartialInitialization);
                }
            };
            let preparation = Self {
                paths,
                directories,
                ssh_config,
                known_hosts,
                needs_initialization,
            };
            preparation.verify_preparation()?;
            Ok(preparation)
        }

        /// Whether the caller must execute [`Self::initialization_spec`] before sealing.
        pub const fn needs_initialization(&self) -> bool {
            self.needs_initialization
        }

        /// Exact offline `git init --bare` process specification for this retained layout.
        ///
        /// # Errors
        ///
        /// Rejects an already initialized repository or changed tool/directory identity.
        pub fn initialization_spec<'a>(
            &self,
            toolchain: &'a WindowsGitToolchain,
        ) -> Result<GitProcessSpec<'a>, PrivateGitRepositoryError> {
            if !self.needs_initialization {
                return Err(PrivateGitRepositoryError::AlreadyInitialized);
            }
            self.verify_preparation()?;
            let context = GitProcessContext::new(
                &self.paths.root,
                None,
                &self.paths.home,
                &self.paths.xdg,
                &self.paths.temp,
            )?;
            toolchain
                .process_spec(
                    &context,
                    GitNetworkPolicy::Offline,
                    [
                        OsString::from("-c"),
                        OsString::from("init.defaultBranch=rustferry-unborn"),
                        OsString::from("init"),
                        OsString::from("--bare"),
                        OsString::from(format!(
                            "--template={}",
                            self.paths.template.to_string_lossy()
                        )),
                        self.paths.bare.as_os_str().to_owned(),
                    ],
                )
                .map_err(Into::into)
        }

        /// Validate and retain an initialized bare repository.
        ///
        /// # Errors
        ///
        /// Rejects missing/malformed Git state, unsafe inherited ACLs, unexpected config keys,
        /// alternates, path replacement, or any changed pinned policy file.
        pub fn finish(self) -> Result<PrivateBareGitRepository, PrivateGitRepositoryError> {
            self.verify_preparation()?;
            let config_path = self.paths.bare.join("config");
            let head_path = self.paths.bare.join("HEAD");
            let config_bytes = read_private_control_path(&config_path)?;
            validate_generated_git_config(&config_bytes)?;
            let config = ExactPrivateFileGuard::open_git_generated(&config_path, &config_bytes)?;
            let head = ExactPrivateFileGuard::open_git_generated(&head_path, UNBORN_HEAD)?;

            let mut directories = self.directories;
            for directory in [
                self.paths.bare.join("objects"),
                self.paths.bare.join("objects/info"),
                self.paths.bare.join("objects/pack"),
                self.paths.bare.join("refs"),
                self.paths.bare.join("refs/heads"),
                self.paths.bare.join("refs/tags"),
            ] {
                directories.push(PrivateDirectoryGuard::open_git_generated(&directory)?);
            }
            let alternates_path = self.paths.bare.join("objects/info/alternates");
            let http_alternates_path = self.paths.bare.join("objects/info/http-alternates");
            ensure_exact_private_file(&alternates_path, b"")?;
            ensure_exact_private_file(&http_alternates_path, b"")?;
            let alternates = ExactPrivateFileGuard::open(&alternates_path, b"")?;
            let http_alternates = ExactPrivateFileGuard::open(&http_alternates_path, b"")?;
            let repository = PrivateBareGitRepository {
                paths: self.paths,
                directories,
                config,
                head,
                alternates,
                http_alternates,
                ssh_config: self.ssh_config,
                known_hosts: self.known_hosts,
            };
            repository.verify()?;
            Ok(repository)
        }

        fn verify_preparation(&self) -> Result<(), PrivateGitRepositoryError> {
            for directory in &self.directories {
                directory.verify()?;
            }
            self.ssh_config.verify()?;
            self.known_hosts.verify()?;
            ensure_directory_empty(&self.paths.template)
        }
    }

    /// Sealed private bare repository and policy files retained for publisher lifetime.
    #[derive(Debug)]
    pub struct PrivateBareGitRepository {
        paths: PrivateGitRepositoryPaths,
        directories: Vec<PrivateDirectoryGuard>,
        config: ExactPrivateFileGuard,
        head: ExactPrivateFileGuard,
        alternates: ExactPrivateFileGuard,
        http_alternates: ExactPrivateFileGuard,
        ssh_config: ExactPrivateFileGuard,
        known_hosts: ExactPrivateFileGuard,
    }

    impl PrivateBareGitRepository {
        /// Reopen an already initialized private repository without running Git.
        ///
        /// # Errors
        ///
        /// Rejects absent, partial, changed, or unsafe layouts.
        pub fn open(root: impl AsRef<Path>) -> Result<Self, PrivateGitRepositoryError> {
            let preparation = PrivateGitRepositoryPreparation::prepare(root)?;
            if preparation.needs_initialization() {
                return Err(PrivateGitRepositoryError::InitializationRequired);
            }
            preparation.finish()
        }

        /// Canonical retained layout paths.
        pub const fn paths(&self) -> &PrivateGitRepositoryPaths {
            &self.paths
        }

        /// Process context that binds every Git command to this bare object database.
        ///
        /// # Errors
        ///
        /// Rejects any path that changed after sealing.
        pub fn process_context(&self) -> Result<GitProcessContext, PrivateGitRepositoryError> {
            self.verify()?;
            GitProcessContext::new(
                &self.paths.root,
                Some(&self.paths.bare),
                &self.paths.home,
                &self.paths.xdg,
                &self.paths.temp,
            )
            .map_err(Into::into)
        }

        /// Revalidate all retained directories and exact policy/control files.
        ///
        /// # Errors
        ///
        /// Fails closed on identity, byte, ACL, template, config, or alternates drift.
        pub fn verify(&self) -> Result<(), PrivateGitRepositoryError> {
            for directory in &self.directories {
                directory.verify()?;
            }
            for file in [
                &self.config,
                &self.head,
                &self.alternates,
                &self.http_alternates,
                &self.ssh_config,
                &self.known_hosts,
            ] {
                file.verify()?;
            }
            validate_generated_git_config(&self.config.expected)?;
            ensure_directory_empty(&self.paths.template)
        }
    }

    /// Stable, path-free private Git repository failure.
    #[derive(Clone, Copy, Debug, Eq, PartialEq)]
    pub enum PrivateGitRepositoryError {
        /// Root or child directory has an invalid path, type, ACL, or reparse state.
        UnsafeDirectory,
        /// Retained directory path no longer identifies the same object.
        DirectoryIdentityChanged,
        /// A fixed control file has an invalid ACL, type, or reparse state.
        UnsafeControlFile,
        /// Fixed control file contents are malformed or differ from policy.
        InvalidControlFile,
        /// Retained control file path or contents changed.
        ControlFileIdentityChanged,
        /// Directory expected to be empty contains unexpected state.
        UnexpectedDirectoryEntry,
        /// Bare repository contains only part of Git's initialization state.
        PartialInitialization,
        /// Initialization was requested for an existing repository.
        AlreadyInitialized,
        /// Existing-only open found an uninitialized repository.
        InitializationRequired,
        /// Git-generated local config contains an unexpected or unsafe key/value.
        InvalidGitConfig,
        /// Fixed process-policy construction failed.
        ProcessPolicy(GitProcessPolicyError),
        /// Bounded control-file I/O failed.
        ControlFileIo,
    }

    impl fmt::Display for PrivateGitRepositoryError {
        fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
            match self {
                Self::UnsafeDirectory => formatter.write_str("private Git directory is unsafe"),
                Self::DirectoryIdentityChanged => {
                    formatter.write_str("private Git directory identity changed")
                }
                Self::UnsafeControlFile => {
                    formatter.write_str("private Git control file is unsafe")
                }
                Self::InvalidControlFile => {
                    formatter.write_str("private Git control file is invalid")
                }
                Self::ControlFileIdentityChanged => {
                    formatter.write_str("private Git control file identity changed")
                }
                Self::UnexpectedDirectoryEntry => {
                    formatter.write_str("private Git directory contains unexpected state")
                }
                Self::PartialInitialization => {
                    formatter.write_str("private bare Git initialization is incomplete")
                }
                Self::AlreadyInitialized => {
                    formatter.write_str("private bare Git repository is already initialized")
                }
                Self::InitializationRequired => {
                    formatter.write_str("private bare Git repository requires initialization")
                }
                Self::InvalidGitConfig => formatter.write_str("private bare Git config is invalid"),
                Self::ProcessPolicy(error) => {
                    write!(formatter, "Git process policy failed: {error}")
                }
                Self::ControlFileIo => formatter.write_str("private Git control-file I/O failed"),
            }
        }
    }

    impl Error for PrivateGitRepositoryError {}

    impl From<GitProcessPolicyError> for PrivateGitRepositoryError {
        fn from(value: GitProcessPolicyError) -> Self {
            Self::ProcessPolicy(value)
        }
    }

    fn create_or_open_private_directory(path: &Path) -> Result<(), PrivateGitRepositoryError> {
        match fs::symlink_metadata(path) {
            Ok(metadata) if metadata.is_dir() && !metadata.file_type().is_symlink() => {}
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
                rustferry_core::windows_private_directory::create_private_directory(path)
                    .map(drop)
                    .map_err(|_| PrivateGitRepositoryError::UnsafeDirectory)?;
            }
            Ok(_) | Err(_) => return Err(PrivateGitRepositoryError::UnsafeDirectory),
        }
        PrivateDirectoryGuard::open(path).map(drop)
    }

    fn canonical_private_directory(path: &Path) -> Result<PathBuf, PrivateGitRepositoryError> {
        let canonical = canonical_directory(path)?;
        rustferry_core::windows_private_directory::open_private_directory_read_guard(&canonical)
            .map(drop)
            .map_err(|_| PrivateGitRepositoryError::UnsafeDirectory)?;
        Ok(canonical)
    }

    fn canonical_directory(path: &Path) -> Result<PathBuf, PrivateGitRepositoryError> {
        if !path.is_absolute()
            || path
                .components()
                .any(|component| matches!(component, Component::CurDir | Component::ParentDir))
        {
            return Err(PrivateGitRepositoryError::UnsafeDirectory);
        }
        let metadata =
            fs::symlink_metadata(path).map_err(|_| PrivateGitRepositoryError::UnsafeDirectory)?;
        if metadata.file_type().is_symlink() || !metadata.is_dir() {
            return Err(PrivateGitRepositoryError::UnsafeDirectory);
        }
        fs::canonicalize(path).map_err(|_| PrivateGitRepositoryError::UnsafeDirectory)
    }

    fn ensure_directory_empty(path: &Path) -> Result<(), PrivateGitRepositoryError> {
        let mut entries =
            fs::read_dir(path).map_err(|_| PrivateGitRepositoryError::UnsafeDirectory)?;
        if entries
            .next()
            .transpose()
            .map_err(|_| PrivateGitRepositoryError::UnsafeDirectory)?
            .is_some()
        {
            return Err(PrivateGitRepositoryError::UnexpectedDirectoryEntry);
        }
        Ok(())
    }

    fn ensure_exact_private_file(
        path: &Path,
        expected: &[u8],
    ) -> Result<(), PrivateGitRepositoryError> {
        match fs::symlink_metadata(path) {
            Ok(_) => ExactPrivateFileGuard::open(path, expected).map(drop),
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
                let mut file = rustferry_core::windows_private_directory::create_private_file(path)
                    .map_err(|_| PrivateGitRepositoryError::UnsafeControlFile)?;
                file.write_all(expected)
                    .and_then(|()| file.sync_all())
                    .map_err(|_| PrivateGitRepositoryError::ControlFileIo)?;
                drop(file);
                ExactPrivateFileGuard::open(path, expected).map(drop)
            }
            Err(_) => Err(PrivateGitRepositoryError::UnsafeControlFile),
        }
    }

    fn read_private_control_path(path: &Path) -> Result<Vec<u8>, PrivateGitRepositoryError> {
        let file = open_retained_generated_file(path)?;
        read_bounded_file(&file)
    }

    fn open_retained_generated_file(path: &Path) -> Result<File, PrivateGitRepositoryError> {
        let metadata =
            fs::symlink_metadata(path).map_err(|_| PrivateGitRepositoryError::UnsafeControlFile)?;
        if !metadata.is_file()
            || metadata.file_type().is_symlink()
            || metadata.file_attributes() & FILE_ATTRIBUTE_REPARSE_POINT != 0
        {
            return Err(PrivateGitRepositoryError::UnsafeControlFile);
        }
        let mut options = fs::OpenOptions::new();
        options
            .read(true)
            .share_mode(FILE_SHARE_READ)
            .custom_flags(FILE_FLAG_OPEN_REPARSE_POINT);
        options
            .open(path)
            .map_err(|_| PrivateGitRepositoryError::UnsafeControlFile)
    }

    fn read_bounded_file(file: &File) -> Result<Vec<u8>, PrivateGitRepositoryError> {
        let length = usize::try_from(
            file.metadata()
                .map_err(|_| PrivateGitRepositoryError::ControlFileIo)?
                .len(),
        )
        .map_err(|_| PrivateGitRepositoryError::InvalidControlFile)?;
        if length > MAX_CONTROL_FILE_BYTES {
            return Err(PrivateGitRepositoryError::InvalidControlFile);
        }
        let mut reader = file
            .try_clone()
            .map_err(|_| PrivateGitRepositoryError::ControlFileIo)?;
        reader
            .seek(SeekFrom::Start(0))
            .map_err(|_| PrivateGitRepositoryError::ControlFileIo)?;
        let mut bytes = Vec::with_capacity(length);
        reader
            .take(
                u64::try_from(MAX_CONTROL_FILE_BYTES)
                    .map_err(|_| PrivateGitRepositoryError::ControlFileIo)?
                    + 1,
            )
            .read_to_end(&mut bytes)
            .map_err(|_| PrivateGitRepositoryError::ControlFileIo)?;
        if bytes.len() != length || bytes.len() > MAX_CONTROL_FILE_BYTES {
            return Err(PrivateGitRepositoryError::InvalidControlFile);
        }
        Ok(bytes)
    }

    fn validate_generated_git_config(bytes: &[u8]) -> Result<(), PrivateGitRepositoryError> {
        let text =
            std::str::from_utf8(bytes).map_err(|_| PrivateGitRepositoryError::InvalidGitConfig)?;
        if text.is_empty()
            || text.len() > MAX_CONTROL_FILE_BYTES
            || text.bytes().any(|byte| {
                byte == 0
                    || byte == b'\r'
                    || byte.is_ascii_control() && !matches!(byte, b'\n' | b'\t')
            })
        {
            return Err(PrivateGitRepositoryError::InvalidGitConfig);
        }
        let mut in_core = false;
        let mut repository_format = false;
        let mut bare = false;
        let mut seen = std::collections::BTreeSet::new();
        for line in text.lines() {
            let trimmed = line.trim();
            if trimmed.is_empty() {
                continue;
            }
            if trimmed == "[core]" {
                if in_core {
                    return Err(PrivateGitRepositoryError::InvalidGitConfig);
                }
                in_core = true;
                continue;
            }
            if !in_core || trimmed.starts_with('[') || trimmed.starts_with(['#', ';']) {
                return Err(PrivateGitRepositoryError::InvalidGitConfig);
            }
            let (key, value) = trimmed
                .split_once('=')
                .ok_or(PrivateGitRepositoryError::InvalidGitConfig)?;
            let key = key.trim().to_ascii_lowercase();
            let value = value.trim().to_ascii_lowercase();
            if !seen.insert(key.clone()) {
                return Err(PrivateGitRepositoryError::InvalidGitConfig);
            }
            match (key.as_str(), value.as_str()) {
                ("repositoryformatversion", "0") => repository_format = true,
                ("bare", "true") => bare = true,
                ("filemode" | "symlinks" | "ignorecase", "true" | "false")
                | ("logallrefupdates", "false") => {}
                _ => return Err(PrivateGitRepositoryError::InvalidGitConfig),
            }
        }
        if in_core && repository_format && bare {
            Ok(())
        } else {
            Err(PrivateGitRepositoryError::InvalidGitConfig)
        }
    }

    #[cfg(test)]
    mod tests {
        use std::process::Stdio;

        use super::*;

        fn installed_git() -> Option<PathBuf> {
            let path = PathBuf::from(r"C:\Program Files\Git\cmd\git.exe");
            path.is_file().then_some(path)
        }

        fn private_root(temporary: &tempfile::TempDir) -> PathBuf {
            let root = temporary.path().join("isolation");
            rustferry_core::windows_private_directory::create_private_directory(&root)
                .expect("private isolation root");
            root
        }

        fn initialize(toolchain: &WindowsGitToolchain, root: &Path) -> PrivateBareGitRepository {
            let preparation =
                PrivateGitRepositoryPreparation::prepare(root).expect("private layout");
            assert!(preparation.needs_initialization());
            let spec = preparation
                .initialization_spec(toolchain)
                .expect("initialization spec");
            let status = spec
                .command()
                .expect("initialization command")
                .stdin(Stdio::null())
                .stdout(Stdio::null())
                .stderr(Stdio::null())
                .status()
                .expect("run git init");
            assert!(status.success());
            preparation.finish().expect("sealed bare repository")
        }

        #[test]
        fn private_bare_repository_is_offline_and_ignores_ambient_config() {
            let Some(git) = installed_git() else {
                return;
            };
            let toolchain = WindowsGitToolchain::new(git).expect("toolchain");
            let temporary = tempfile::tempdir().expect("fixture");
            let repository = initialize(&toolchain, &private_root(&temporary));
            repository.verify().expect("private repository");
            let context = repository.process_context().expect("process context");
            let spec = toolchain
                .process_spec(
                    &context,
                    GitNetworkPolicy::Offline,
                    ["config", "--local", "--no-includes", "--list"],
                )
                .expect("config query");
            let output = spec
                .command()
                .expect("config command")
                .output()
                .expect("config output");
            assert!(
                output.status.success(),
                "{}",
                String::from_utf8_lossy(&output.stderr)
            );
            let output = String::from_utf8(output.stdout).expect("UTF-8 config");
            assert!(output.contains("core.bare=true"));
            for forbidden in [
                "credential.helper",
                "core.sshcommand",
                "url.",
                "http.proxy",
                "http.extraheader",
                "include.path",
            ] {
                assert!(!output.to_ascii_lowercase().contains(forbidden));
            }
            let reopened = PrivateBareGitRepository::open(repository.paths().root())
                .expect("reopened repository");
            reopened.verify().expect("stable reopened repository");
        }

        #[test]
        fn managed_ssh_config_resolves_exact_private_known_hosts() {
            let Some(git) = installed_git() else {
                return;
            };
            let toolchain = WindowsGitToolchain::new(git).expect("toolchain");
            let temporary = tempfile::tempdir().expect("fixture");
            let repository = initialize(&toolchain, &private_root(&temporary));
            let paths = repository.paths();
            let config = String::from_utf8(fs::read(paths.ssh_config()).expect("SSH config"))
                .expect("UTF-8 SSH config");
            assert!(!config.contains('~'));
            assert!(!config.contains(r"\\?\"));
            assert!(!config.contains("//?/"));

            let ambient_home = temporary.path().join("ambient-home");
            let ambient_profile = temporary.path().join("ambient-profile");
            fs::create_dir(&ambient_home).expect("ambient HOME fixture");
            fs::create_dir(&ambient_profile).expect("ambient profile fixture");
            let system_root = external_path(toolchain.system_root());
            let command_interpreter =
                external_path(&toolchain.system_root().join("System32").join("cmd.exe"));
            let ambient_xdg = temporary.path().join("ambient-xdg");
            fs::create_dir(&ambient_xdg).expect("ambient XDG fixture");
            let output = std::process::Command::new(toolchain.ssh().path())
                .args(["-G", "-F"])
                .arg(external_path(paths.ssh_config()))
                .arg("github.com")
                .env_clear()
                .env("COMSPEC", command_interpreter)
                .env("HOME", &ambient_home)
                .env("LANG", "C")
                .env("LC_ALL", "C")
                .env("PATH", toolchain.fixed_path())
                .env("PATHEXT", ".COM;.EXE;.BAT;.CMD")
                .env("SystemRoot", &system_root)
                .env("TEMP", temporary.path())
                .env("TMP", temporary.path())
                .env("USERPROFILE", &ambient_profile)
                .env("WINDIR", system_root)
                .env("XDG_CONFIG_HOME", ambient_xdg)
                .env("PROGRAMDATA", &ambient_home)
                .output()
                .expect("ssh -G");
            assert!(
                output.status.success(),
                "status={:?}; {}",
                output.status.code(),
                String::from_utf8_lossy(&output.stderr)
            );
            let resolved = String::from_utf8(output.stdout).expect("UTF-8 ssh -G output");
            let values = resolved
                .lines()
                .filter_map(|line| line.split_once(' '))
                .filter(|(key, _)| key.eq_ignore_ascii_case("userknownhostsfile"))
                .map(|(_, value)| value.trim().replace('\\', "/"))
                .collect::<Vec<_>>();
            assert_eq!(values.len(), 1);
            let expected = external_path(paths.known_hosts())
                .to_string_lossy()
                .replace('\\', "/");
            assert!(values[0].eq_ignore_ascii_case(&expected));
            assert!(!values[0].contains("//?/"));
        }

        #[test]
        fn malicious_local_config_is_rejected_before_use() {
            let Some(git) = installed_git() else {
                return;
            };
            let toolchain = WindowsGitToolchain::new(git).expect("toolchain");
            let temporary = tempfile::tempdir().expect("fixture");
            let root = private_root(&temporary);
            let preparation = PrivateGitRepositoryPreparation::prepare(&root).expect("preparation");
            let spec = preparation
                .initialization_spec(&toolchain)
                .expect("initialization spec");
            assert!(
                spec.command()
                    .expect("command")
                    .stdout(Stdio::null())
                    .stderr(Stdio::null())
                    .status()
                    .expect("git init")
                    .success()
            );
            fs::OpenOptions::new()
                .append(true)
                .open(root.join(BARE_DIRECTORY_NAME).join("config"))
                .and_then(|mut file| {
                    file.write_all(b"[credential]\nhelper = !cmd /c echo compromised > canary\n")
                })
                .expect("inject malicious local config fixture");
            assert!(matches!(
                preparation.finish(),
                Err(PrivateGitRepositoryError::InvalidGitConfig)
            ));
            assert!(!root.join("canary").exists());
        }

        #[test]
        fn sealed_control_files_block_replacement() {
            let Some(git) = installed_git() else {
                return;
            };
            let toolchain = WindowsGitToolchain::new(git).expect("toolchain");
            let temporary = tempfile::tempdir().expect("fixture");
            let repository = initialize(&toolchain, &private_root(&temporary));
            let config = repository.paths().bare().join("config");
            let displaced = repository.paths().bare().join("config-old");
            assert!(fs::rename(&config, displaced).is_err());
            assert!(fs::OpenOptions::new().write(true).open(config).is_err());
            repository.verify().expect("stable repository");
        }
    }
}

#[cfg(windows)]
pub use windows::{
    GITHUB_KNOWN_HOSTS_V1, PrivateBareGitRepository, PrivateGitRepositoryError,
    PrivateGitRepositoryPaths, PrivateGitRepositoryPreparation,
};

#[cfg(unix)]
#[allow(missing_docs, clippy::missing_errors_doc)]
mod unix {
    use std::{
        collections::BTreeSet,
        error::Error,
        ffi::OsString,
        fmt,
        fs::{self, File},
        io::{Read, Seek, SeekFrom, Write},
        os::unix::fs::{
            DirBuilderExt as _, MetadataExt as _, OpenOptionsExt as _, PermissionsExt as _,
        },
        path::{Component, Path, PathBuf},
    };

    use crate::git_process::{
        GitNetworkPolicy, GitProcessContext, GitProcessPolicyError, GitProcessSpec,
        UnixGitToolchain,
    };
    use rustferry_core::{DirectoryFilesystemIdentity, verify_directory_identity};

    const BARE_DIRECTORY_NAME: &str = "repository.git";
    const HOME_DIRECTORY_NAME: &str = "home";
    const XDG_DIRECTORY_NAME: &str = "xdg";
    const TEMP_DIRECTORY_NAME: &str = "tmp";
    const TEMPLATE_DIRECTORY_NAME: &str = "empty-template";
    const SSH_DIRECTORY_NAME: &str = ".ssh";
    const MAX_CONTROL_FILE_BYTES: usize = 64 * 1024;
    const UNBORN_HEAD: &[u8] = b"ref: refs/heads/rustferry-unborn\n";

    pub const GITHUB_KNOWN_HOSTS_V1: &str = concat!(
        "github.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl\n",
        "github.com ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBEmKSENjQEezOmxkZMy7opKgwFB9nkt5YRrYMjNuG5N87uRgg6CLrbo5wAdT/y6v0mKV0U2w0WZ2YB/++Tpockg=\n",
        "github.com ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCj7ndNxQowgcQnjshcLrqPEiiphnt+VTTvDP6mHBL9j1aNUkY4Ue1gvwnGLVlOhGeYrnZaMgRK6+PKCUXaDbC7qtbW8gIkhL7aGCsOr/C56SJMy/BCZfxd1nWzAOxSDPgVsmerOBYfNqltV9/hWCqBywINIR+5dIg6JTJ72pcEpEjcYgXkE2YEFXV1JHnsKgbLWNlhScqb2UmyRkQyytRLtL+38TGxkxCflmO+5Z8CSSNY7GidjMIZ7Q4zMjA2n1nGrlTDkzwDCsw+wqFPGQA179cnfGWOWRVruj16z6XyvxvjJwbz0wQZ75XK5tKSb7FNyeIEs4TT4jk+S4dhPeAUC5y+bDYirYgM4GC7uEnztnZyaVWQ7B381AK4Qdrwt51ZqExKbQpTUNn+EjqoTwvqNj4kqx5QUCI0ThS/YkOxJCXmPUWZbhjpCg56i+2aB6CmK2JGhn57K5mj0MNdBXA4/WnwH6XoPWJzK5Nyu2zB3nAZp+S5hpQs+p1vN1/wsjk=\n",
    );

    const SSH_CONFIG_V1_TEMPLATE: &str = concat!(
        "Host *\n",
        "    BatchMode yes\n",
        "    StrictHostKeyChecking yes\n",
        "    UserKnownHostsFile @RUSTFERRY_KNOWN_HOSTS@\n",
        "    GlobalKnownHostsFile none\n",
        "    HostKeyAlgorithms ssh-ed25519,ecdsa-sha2-nistp256,rsa-sha2-512,rsa-sha2-256\n",
        "    PreferredAuthentications publickey\n",
        "    IdentityFile none\n",
        "    PasswordAuthentication no\n",
        "    KbdInteractiveAuthentication no\n",
        "    ProxyCommand none\n",
        "    ProxyJump none\n",
        "    PermitLocalCommand no\n",
        "    ForwardAgent no\n",
        "    ClearAllForwardings yes\n",
        "    RequestTTY no\n",
        "    AddKeysToAgent no\n",
        "    UpdateHostKeys no\n",
        "    VerifyHostKeyDNS no\n",
        "    CheckHostIP no\n",
        "    CanonicalizeHostname no\n",
        "    ConnectionAttempts 1\n",
        "    ConnectTimeout 30\n",
        "    LogLevel ERROR\n",
        "Host github.com\n",
        "    HostName github.com\n",
        "    HostKeyAlias github.com\n",
        "    User git\n",
        "    Port 22\n",
    );

    fn ssh_config_v1(known_hosts: &Path) -> Result<Vec<u8>, PrivateGitRepositoryError> {
        let value = known_hosts
            .to_str()
            .ok_or(PrivateGitRepositoryError::InvalidControlFile)?;
        if !known_hosts.is_absolute()
            || value.is_empty()
            || value
                .bytes()
                .any(|byte| byte.is_ascii_control() || matches!(byte, b'%' | b'$' | b'"' | b'\\'))
        {
            return Err(PrivateGitRepositoryError::InvalidControlFile);
        }
        let rendered =
            SSH_CONFIG_V1_TEMPLATE.replace("@RUSTFERRY_KNOWN_HOSTS@", &format!("\"{value}\""));
        if rendered.len() > MAX_CONTROL_FILE_BYTES {
            return Err(PrivateGitRepositoryError::InvalidControlFile);
        }
        Ok(rendered.into_bytes())
    }

    #[derive(Clone, Debug, Eq, PartialEq)]
    pub struct PrivateGitRepositoryPaths {
        root: PathBuf,
        bare: PathBuf,
        home: PathBuf,
        xdg: PathBuf,
        temp: PathBuf,
        template: PathBuf,
        ssh: PathBuf,
        ssh_config: PathBuf,
        known_hosts: PathBuf,
    }

    impl PrivateGitRepositoryPaths {
        fn new(root: PathBuf) -> Self {
            let bare = root.join(BARE_DIRECTORY_NAME);
            let home = root.join(HOME_DIRECTORY_NAME);
            let xdg = root.join(XDG_DIRECTORY_NAME);
            let temp = root.join(TEMP_DIRECTORY_NAME);
            let template = root.join(TEMPLATE_DIRECTORY_NAME);
            let ssh = home.join(SSH_DIRECTORY_NAME);
            let ssh_config = ssh.join("config");
            let known_hosts = ssh.join("known_hosts");
            Self {
                root,
                bare,
                home,
                xdg,
                temp,
                template,
                ssh,
                ssh_config,
                known_hosts,
            }
        }

        pub fn root(&self) -> &Path {
            &self.root
        }

        pub fn bare(&self) -> &Path {
            &self.bare
        }

        pub fn home(&self) -> &Path {
            &self.home
        }

        pub fn xdg(&self) -> &Path {
            &self.xdg
        }

        pub fn temp(&self) -> &Path {
            &self.temp
        }

        pub fn template(&self) -> &Path {
            &self.template
        }

        pub fn ssh_config(&self) -> &Path {
            &self.ssh_config
        }

        pub fn known_hosts(&self) -> &Path {
            &self.known_hosts
        }
    }

    #[derive(Debug)]
    struct ExactPrivateFile {
        path: PathBuf,
        device: u64,
        inode: u64,
        expected: Vec<u8>,
        _file: File,
    }

    impl ExactPrivateFile {
        fn open(path: &Path, expected: &[u8]) -> Result<Self, PrivateGitRepositoryError> {
            if expected.len() > MAX_CONTROL_FILE_BYTES {
                return Err(PrivateGitRepositoryError::InvalidControlFile);
            }
            let metadata = fs::symlink_metadata(path)
                .map_err(|_| PrivateGitRepositoryError::UnsafeControlFile)?;
            if !metadata.is_file()
                || metadata.file_type().is_symlink()
                || metadata.uid() != private_parent_owner(path)?
                || metadata.permissions().mode() & 0o077 != 0
                || metadata.nlink() != 1
            {
                return Err(PrivateGitRepositoryError::UnsafeControlFile);
            }
            let file =
                File::open(path).map_err(|_| PrivateGitRepositoryError::UnsafeControlFile)?;
            let opened = file
                .metadata()
                .map_err(|_| PrivateGitRepositoryError::UnsafeControlFile)?;
            if opened.dev() != metadata.dev()
                || opened.ino() != metadata.ino()
                || read_bounded_file(&file)? != expected
            {
                return Err(PrivateGitRepositoryError::InvalidControlFile);
            }
            Ok(Self {
                path: path.to_owned(),
                device: opened.dev(),
                inode: opened.ino(),
                expected: expected.to_vec(),
                _file: file,
            })
        }

        fn verify(&self) -> Result<(), PrivateGitRepositoryError> {
            let metadata = fs::symlink_metadata(&self.path)
                .map_err(|_| PrivateGitRepositoryError::ControlFileIdentityChanged)?;
            if !metadata.is_file()
                || metadata.file_type().is_symlink()
                || metadata.uid() != private_parent_owner(&self.path)?
                || metadata.permissions().mode() & 0o077 != 0
                || metadata.nlink() != 1
                || metadata.dev() != self.device
                || metadata.ino() != self.inode
            {
                return Err(PrivateGitRepositoryError::ControlFileIdentityChanged);
            }
            let file = File::open(&self.path)
                .map_err(|_| PrivateGitRepositoryError::ControlFileIdentityChanged)?;
            if read_bounded_file(&file)? != self.expected {
                return Err(PrivateGitRepositoryError::ControlFileIdentityChanged);
            }
            Ok(())
        }
    }

    #[derive(Debug)]
    pub struct PrivateGitRepositoryPreparation {
        paths: PrivateGitRepositoryPaths,
        root_identity: DirectoryFilesystemIdentity,
        owner_uid: u32,
        ssh_config: ExactPrivateFile,
        known_hosts: ExactPrivateFile,
        needs_initialization: bool,
    }

    impl PrivateGitRepositoryPreparation {
        pub fn prepare(root: impl AsRef<Path>) -> Result<Self, PrivateGitRepositoryError> {
            let root = canonical_private_directory(root.as_ref())?;
            let owner_uid = directory_owner(&root)?;
            let paths = PrivateGitRepositoryPaths::new(root.clone());
            for directory in [
                &paths.bare,
                &paths.home,
                &paths.xdg,
                &paths.temp,
                &paths.template,
                &paths.ssh,
            ] {
                create_or_open_private_directory(directory)?;
            }
            ensure_directory_empty(&paths.template)?;
            let expected_ssh_config = ssh_config_v1(&paths.known_hosts)?;
            ensure_exact_private_file(&paths.ssh_config, &expected_ssh_config)?;
            ensure_exact_private_file(&paths.known_hosts, GITHUB_KNOWN_HOSTS_V1.as_bytes())?;
            let ssh_config = ExactPrivateFile::open(&paths.ssh_config, &expected_ssh_config)?;
            let known_hosts =
                ExactPrivateFile::open(&paths.known_hosts, GITHUB_KNOWN_HOSTS_V1.as_bytes())?;
            let config_exists = paths.bare.join("config").try_exists().unwrap_or(false);
            let head_exists = paths.bare.join("HEAD").try_exists().unwrap_or(false);
            let needs_initialization = match (config_exists, head_exists) {
                (false, false) => {
                    ensure_directory_empty(&paths.bare)?;
                    true
                }
                (true, true) => false,
                _ => return Err(PrivateGitRepositoryError::PartialInitialization),
            };
            let preparation = Self {
                root_identity: DirectoryFilesystemIdentity::capture(&root)
                    .map_err(|_| PrivateGitRepositoryError::UnsafeDirectory)?,
                owner_uid,
                paths,
                ssh_config,
                known_hosts,
                needs_initialization,
            };
            preparation.verify_preparation()?;
            Ok(preparation)
        }

        pub const fn needs_initialization(&self) -> bool {
            self.needs_initialization
        }

        pub fn initialization_spec<'a>(
            &self,
            toolchain: &'a UnixGitToolchain,
        ) -> Result<GitProcessSpec<'a>, PrivateGitRepositoryError> {
            if !self.needs_initialization {
                return Err(PrivateGitRepositoryError::AlreadyInitialized);
            }
            self.verify_preparation()?;
            let context = GitProcessContext::new(
                &self.paths.root,
                None,
                &self.paths.home,
                &self.paths.xdg,
                &self.paths.temp,
            )?;
            toolchain
                .process_spec(
                    &context,
                    GitNetworkPolicy::Offline,
                    [
                        OsString::from("-c"),
                        OsString::from("init.defaultBranch=rustferry-unborn"),
                        OsString::from("init"),
                        OsString::from("--bare"),
                        OsString::from(format!(
                            "--template={}",
                            self.paths.template.to_string_lossy()
                        )),
                        self.paths.bare.as_os_str().to_owned(),
                    ],
                )
                .map_err(Into::into)
        }

        pub fn finish(self) -> Result<PrivateBareGitRepository, PrivateGitRepositoryError> {
            self.verify_preparation()?;
            let config_path = self.paths.bare.join("config");
            let head_path = self.paths.bare.join("HEAD");
            make_private_directory(&self.paths.bare.join("objects"), self.owner_uid)?;
            make_private_directory(&self.paths.bare.join("objects/info"), self.owner_uid)?;
            make_private_file(&config_path)?;
            make_private_file(&head_path)?;
            let config_bytes = read_control_path(&config_path)?;
            validate_generated_git_config(&config_bytes)?;
            let config = ExactPrivateFile::open(&config_path, &config_bytes)?;
            let head = ExactPrivateFile::open(&head_path, UNBORN_HEAD)?;
            let alternates_path = self.paths.bare.join("objects/info/alternates");
            let http_alternates_path = self.paths.bare.join("objects/info/http-alternates");
            ensure_exact_private_file(&alternates_path, b"")?;
            ensure_exact_private_file(&http_alternates_path, b"")?;
            let alternates = ExactPrivateFile::open(&alternates_path, b"")?;
            let http_alternates = ExactPrivateFile::open(&http_alternates_path, b"")?;
            let repository = PrivateBareGitRepository {
                paths: self.paths,
                root_identity: self.root_identity,
                owner_uid: self.owner_uid,
                config,
                head,
                alternates,
                http_alternates,
                ssh_config: self.ssh_config,
                known_hosts: self.known_hosts,
            };
            repository.verify()?;
            Ok(repository)
        }

        fn verify_preparation(&self) -> Result<(), PrivateGitRepositoryError> {
            verify_directory_identity(&self.paths.root, &self.root_identity)
                .map_err(|_| PrivateGitRepositoryError::DirectoryIdentityChanged)?;
            for directory in [
                &self.paths.root,
                &self.paths.bare,
                &self.paths.home,
                &self.paths.xdg,
                &self.paths.temp,
                &self.paths.template,
                &self.paths.ssh,
            ] {
                canonical_private_directory(directory)?;
                if directory_owner(directory)? != self.owner_uid {
                    return Err(PrivateGitRepositoryError::UnsafeDirectory);
                }
            }
            self.ssh_config.verify()?;
            self.known_hosts.verify()?;
            ensure_directory_empty(&self.paths.template)
        }
    }

    #[derive(Debug)]
    pub struct PrivateBareGitRepository {
        paths: PrivateGitRepositoryPaths,
        root_identity: DirectoryFilesystemIdentity,
        owner_uid: u32,
        config: ExactPrivateFile,
        head: ExactPrivateFile,
        alternates: ExactPrivateFile,
        http_alternates: ExactPrivateFile,
        ssh_config: ExactPrivateFile,
        known_hosts: ExactPrivateFile,
    }

    impl PrivateBareGitRepository {
        pub fn open(root: impl AsRef<Path>) -> Result<Self, PrivateGitRepositoryError> {
            let preparation = PrivateGitRepositoryPreparation::prepare(root)?;
            if preparation.needs_initialization() {
                return Err(PrivateGitRepositoryError::InitializationRequired);
            }
            preparation.finish()
        }

        pub const fn paths(&self) -> &PrivateGitRepositoryPaths {
            &self.paths
        }

        pub fn process_context(&self) -> Result<GitProcessContext, PrivateGitRepositoryError> {
            self.verify()?;
            GitProcessContext::new(
                &self.paths.root,
                Some(&self.paths.bare),
                &self.paths.home,
                &self.paths.xdg,
                &self.paths.temp,
            )
            .map_err(Into::into)
        }

        pub fn verify(&self) -> Result<(), PrivateGitRepositoryError> {
            verify_directory_identity(&self.paths.root, &self.root_identity)
                .map_err(|_| PrivateGitRepositoryError::DirectoryIdentityChanged)?;
            for directory in [
                &self.paths.root,
                &self.paths.bare,
                &self.paths.home,
                &self.paths.xdg,
                &self.paths.temp,
                &self.paths.template,
                &self.paths.ssh,
            ] {
                canonical_private_directory(directory)?;
                if directory_owner(directory)? != self.owner_uid {
                    return Err(PrivateGitRepositoryError::UnsafeDirectory);
                }
            }
            for file in [
                &self.config,
                &self.head,
                &self.alternates,
                &self.http_alternates,
                &self.ssh_config,
                &self.known_hosts,
            ] {
                file.verify()?;
            }
            validate_generated_git_config(&self.config.expected)?;
            ensure_directory_empty(&self.paths.template)
        }
    }

    #[derive(Clone, Copy, Debug, Eq, PartialEq)]
    pub enum PrivateGitRepositoryError {
        UnsafeDirectory,
        DirectoryIdentityChanged,
        UnsafeControlFile,
        InvalidControlFile,
        ControlFileIdentityChanged,
        UnexpectedDirectoryEntry,
        PartialInitialization,
        AlreadyInitialized,
        InitializationRequired,
        InvalidGitConfig,
        ProcessPolicy(GitProcessPolicyError),
        ControlFileIo,
    }

    impl fmt::Display for PrivateGitRepositoryError {
        fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
            match self {
                Self::UnsafeDirectory => formatter.write_str("private Git directory is unsafe"),
                Self::DirectoryIdentityChanged => {
                    formatter.write_str("private Git directory identity changed")
                }
                Self::UnsafeControlFile => {
                    formatter.write_str("private Git control file is unsafe")
                }
                Self::InvalidControlFile => {
                    formatter.write_str("private Git control file is invalid")
                }
                Self::ControlFileIdentityChanged => {
                    formatter.write_str("private Git control file identity changed")
                }
                Self::UnexpectedDirectoryEntry => {
                    formatter.write_str("private Git directory contains unexpected state")
                }
                Self::PartialInitialization => {
                    formatter.write_str("private bare Git repository is partially initialized")
                }
                Self::AlreadyInitialized => {
                    formatter.write_str("private Git repository already exists")
                }
                Self::InitializationRequired => {
                    formatter.write_str("private Git repository needs initialization")
                }
                Self::InvalidGitConfig => formatter.write_str("private Git config is invalid"),
                Self::ProcessPolicy(error) => {
                    write!(formatter, "private Git process policy failed: {error}")
                }
                Self::ControlFileIo => formatter.write_str("private Git control-file I/O failed"),
            }
        }
    }

    impl Error for PrivateGitRepositoryError {}

    impl From<GitProcessPolicyError> for PrivateGitRepositoryError {
        fn from(value: GitProcessPolicyError) -> Self {
            Self::ProcessPolicy(value)
        }
    }

    fn create_or_open_private_directory(path: &Path) -> Result<(), PrivateGitRepositoryError> {
        match fs::symlink_metadata(path) {
            Ok(_) => {
                canonical_private_directory(path)?;
            }
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
                let mut builder = fs::DirBuilder::new();
                builder.mode(0o700);
                builder
                    .create(path)
                    .map_err(|_| PrivateGitRepositoryError::UnsafeDirectory)?;
                canonical_private_directory(path)?;
                sync_parent(path)?;
            }
            Err(_) => return Err(PrivateGitRepositoryError::UnsafeDirectory),
        }
        Ok(())
    }

    fn canonical_private_directory(path: &Path) -> Result<PathBuf, PrivateGitRepositoryError> {
        if !is_absolute_normal(path) {
            return Err(PrivateGitRepositoryError::UnsafeDirectory);
        }
        let metadata =
            fs::symlink_metadata(path).map_err(|_| PrivateGitRepositoryError::UnsafeDirectory)?;
        if !metadata.is_dir()
            || metadata.file_type().is_symlink()
            || metadata.permissions().mode() & 0o077 != 0
        {
            return Err(PrivateGitRepositoryError::UnsafeDirectory);
        }
        fs::canonicalize(path).map_err(|_| PrivateGitRepositoryError::UnsafeDirectory)
    }

    fn ensure_directory_empty(path: &Path) -> Result<(), PrivateGitRepositoryError> {
        let mut entries =
            fs::read_dir(path).map_err(|_| PrivateGitRepositoryError::UnsafeDirectory)?;
        if entries.next().is_some() {
            return Err(PrivateGitRepositoryError::UnexpectedDirectoryEntry);
        }
        Ok(())
    }

    fn ensure_exact_private_file(
        path: &Path,
        expected: &[u8],
    ) -> Result<(), PrivateGitRepositoryError> {
        match fs::symlink_metadata(path) {
            Ok(_) => ExactPrivateFile::open(path, expected).map(drop),
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
                let mut options = fs::OpenOptions::new();
                let mut file = options
                    .write(true)
                    .create_new(true)
                    .mode(0o600)
                    .open(path)
                    .map_err(|_| PrivateGitRepositoryError::UnsafeControlFile)?;
                file.write_all(expected)
                    .and_then(|()| file.sync_all())
                    .map_err(|_| PrivateGitRepositoryError::ControlFileIo)?;
                sync_parent(path)?;
                ExactPrivateFile::open(path, expected).map(drop)
            }
            Err(_) => Err(PrivateGitRepositoryError::UnsafeControlFile),
        }
    }

    fn make_private_file(path: &Path) -> Result<(), PrivateGitRepositoryError> {
        let metadata =
            fs::symlink_metadata(path).map_err(|_| PrivateGitRepositoryError::UnsafeControlFile)?;
        if !metadata.is_file()
            || metadata.file_type().is_symlink()
            || metadata.uid() != private_parent_owner(path)?
            || metadata.nlink() != 1
        {
            return Err(PrivateGitRepositoryError::UnsafeControlFile);
        }
        fs::set_permissions(path, fs::Permissions::from_mode(0o600))
            .map_err(|_| PrivateGitRepositoryError::UnsafeControlFile)
    }

    fn make_private_directory(
        path: &Path,
        expected_owner: u32,
    ) -> Result<(), PrivateGitRepositoryError> {
        let metadata =
            fs::symlink_metadata(path).map_err(|_| PrivateGitRepositoryError::UnsafeDirectory)?;
        if !metadata.is_dir()
            || metadata.file_type().is_symlink()
            || metadata.uid() != expected_owner
        {
            return Err(PrivateGitRepositoryError::UnsafeDirectory);
        }
        fs::set_permissions(path, fs::Permissions::from_mode(0o700))
            .map_err(|_| PrivateGitRepositoryError::UnsafeDirectory)
    }

    fn read_control_path(path: &Path) -> Result<Vec<u8>, PrivateGitRepositoryError> {
        let file = File::open(path).map_err(|_| PrivateGitRepositoryError::UnsafeControlFile)?;
        read_bounded_file(&file)
    }

    fn read_bounded_file(file: &File) -> Result<Vec<u8>, PrivateGitRepositoryError> {
        let length = usize::try_from(
            file.metadata()
                .map_err(|_| PrivateGitRepositoryError::ControlFileIo)?
                .len(),
        )
        .map_err(|_| PrivateGitRepositoryError::InvalidControlFile)?;
        if length > MAX_CONTROL_FILE_BYTES {
            return Err(PrivateGitRepositoryError::InvalidControlFile);
        }
        let mut reader = file
            .try_clone()
            .map_err(|_| PrivateGitRepositoryError::ControlFileIo)?;
        reader
            .seek(SeekFrom::Start(0))
            .map_err(|_| PrivateGitRepositoryError::ControlFileIo)?;
        let mut bytes = Vec::with_capacity(length);
        reader
            .take(MAX_CONTROL_FILE_BYTES as u64 + 1)
            .read_to_end(&mut bytes)
            .map_err(|_| PrivateGitRepositoryError::ControlFileIo)?;
        if bytes.len() != length || bytes.len() > MAX_CONTROL_FILE_BYTES {
            return Err(PrivateGitRepositoryError::InvalidControlFile);
        }
        Ok(bytes)
    }

    fn validate_generated_git_config(bytes: &[u8]) -> Result<(), PrivateGitRepositoryError> {
        let text =
            std::str::from_utf8(bytes).map_err(|_| PrivateGitRepositoryError::InvalidGitConfig)?;
        if text.is_empty()
            || text.len() > MAX_CONTROL_FILE_BYTES
            || text.bytes().any(|byte| {
                byte == 0
                    || byte == b'\r'
                    || byte.is_ascii_control() && !matches!(byte, b'\n' | b'\t')
            })
        {
            return Err(PrivateGitRepositoryError::InvalidGitConfig);
        }
        let mut in_core = false;
        let mut repository_format = false;
        let mut bare = false;
        let mut seen = BTreeSet::new();
        for line in text.lines() {
            let trimmed = line.trim();
            if trimmed.is_empty() {
                continue;
            }
            if trimmed == "[core]" {
                if in_core {
                    return Err(PrivateGitRepositoryError::InvalidGitConfig);
                }
                in_core = true;
                continue;
            }
            if !in_core || trimmed.starts_with('[') || trimmed.starts_with(['#', ';']) {
                return Err(PrivateGitRepositoryError::InvalidGitConfig);
            }
            let (key, value) = trimmed
                .split_once('=')
                .ok_or(PrivateGitRepositoryError::InvalidGitConfig)?;
            let key = key.trim().to_ascii_lowercase();
            let value = value.trim().to_ascii_lowercase();
            if !seen.insert(key.clone()) {
                return Err(PrivateGitRepositoryError::InvalidGitConfig);
            }
            match (key.as_str(), value.as_str()) {
                ("repositoryformatversion", "0") => repository_format = true,
                ("bare", "true") => bare = true,
                ("filemode" | "symlinks" | "ignorecase", "true" | "false")
                | ("logallrefupdates", "false") => {}
                _ => return Err(PrivateGitRepositoryError::InvalidGitConfig),
            }
        }
        if in_core && repository_format && bare {
            Ok(())
        } else {
            Err(PrivateGitRepositoryError::InvalidGitConfig)
        }
    }

    fn sync_parent(path: &Path) -> Result<(), PrivateGitRepositoryError> {
        let parent = path
            .parent()
            .ok_or(PrivateGitRepositoryError::ControlFileIo)?;
        File::open(parent)
            .and_then(|directory| directory.sync_all())
            .map_err(|_| PrivateGitRepositoryError::ControlFileIo)
    }

    fn directory_owner(path: &Path) -> Result<u32, PrivateGitRepositoryError> {
        fs::metadata(path)
            .map(|metadata| metadata.uid())
            .map_err(|_| PrivateGitRepositoryError::UnsafeDirectory)
    }

    fn private_parent_owner(path: &Path) -> Result<u32, PrivateGitRepositoryError> {
        let parent = path
            .parent()
            .ok_or(PrivateGitRepositoryError::UnsafeControlFile)?;
        let metadata =
            fs::metadata(parent).map_err(|_| PrivateGitRepositoryError::UnsafeControlFile)?;
        if !metadata.is_dir() || metadata.permissions().mode() & 0o077 != 0 {
            return Err(PrivateGitRepositoryError::UnsafeControlFile);
        }
        Ok(metadata.uid())
    }

    fn is_absolute_normal(path: &Path) -> bool {
        path.is_absolute()
            && path
                .components()
                .all(|component| !matches!(component, Component::CurDir | Component::ParentDir))
    }

    #[cfg(test)]
    mod tests {
        use std::process::Stdio;

        use super::*;

        fn system_toolchain() -> Option<UnixGitToolchain> {
            match UnixGitToolchain::new("/usr/bin/git") {
                Ok(toolchain) => Some(toolchain),
                Err(GitProcessPolicyError::InvalidToolLayout) => None,
                Err(error) => panic!("system toolchain: {error:?}"),
            }
        }

        #[test]
        fn private_bare_repository_is_offline_and_ignores_ambient_config() {
            if !Path::new("/usr/bin/git").is_file() || !Path::new("/usr/bin/ssh").is_file() {
                return;
            }
            let Some(toolchain) = system_toolchain() else {
                return;
            };
            let temporary = tempfile::tempdir().expect("fixture");
            let root = temporary.path().join("isolation");
            let mut builder = fs::DirBuilder::new();
            builder.mode(0o700).create(&root).expect("private root");
            let preparation =
                PrivateGitRepositoryPreparation::prepare(&root).expect("private layout");
            let mut command = preparation
                .initialization_spec(&toolchain)
                .expect("init spec")
                .command()
                .expect("init command");
            assert!(
                command
                    .stdin(Stdio::null())
                    .stdout(Stdio::null())
                    .stderr(Stdio::null())
                    .status()
                    .expect("git init")
                    .success()
            );
            let repository = preparation.finish().expect("sealed repository");
            repository.verify().expect("verified repository");
            let context = repository.process_context().expect("process context");
            let output = toolchain
                .process_spec(
                    &context,
                    GitNetworkPolicy::Offline,
                    ["config", "--local", "--no-includes", "--list"],
                )
                .expect("config spec")
                .command()
                .expect("config command")
                .output()
                .expect("config output");
            assert!(output.status.success());
            let output = String::from_utf8(output.stdout).expect("UTF-8 config");
            assert!(output.contains("core.bare=true"));
            for forbidden in [
                "credential.helper",
                "core.sshcommand",
                "url.",
                "http.proxy",
                "include.path",
            ] {
                assert!(!output.to_ascii_lowercase().contains(forbidden));
            }
            PrivateBareGitRepository::open(repository.paths().root())
                .expect("reopened repository")
                .verify()
                .expect("reopened verification");
        }

        #[test]
        fn managed_ssh_config_resolves_exact_private_known_hosts() {
            if !Path::new("/usr/bin/git").is_file() || !Path::new("/usr/bin/ssh").is_file() {
                return;
            }
            let Some(toolchain) = system_toolchain() else {
                return;
            };
            let temporary = tempfile::tempdir().expect("fixture");
            let root = temporary.path().join("isolation");
            fs::DirBuilder::new()
                .mode(0o700)
                .create(&root)
                .expect("private root");
            let preparation =
                PrivateGitRepositoryPreparation::prepare(&root).expect("private layout");
            let mut command = preparation
                .initialization_spec(&toolchain)
                .expect("init spec")
                .command()
                .expect("init command");
            assert!(
                command
                    .stdin(Stdio::null())
                    .stdout(Stdio::null())
                    .stderr(Stdio::null())
                    .status()
                    .expect("git init")
                    .success()
            );
            let repository = preparation.finish().expect("sealed repository");
            let paths = repository.paths();
            let config = String::from_utf8(fs::read(paths.ssh_config()).expect("SSH config"))
                .expect("UTF-8 SSH config");
            assert!(!config.contains('~'));

            let output = std::process::Command::new("/usr/bin/ssh")
                .args(["-G", "-F"])
                .arg(paths.ssh_config())
                .arg("github.com")
                .env_clear()
                .env("HOME", temporary.path().join("ambient-home"))
                .output()
                .expect("ssh -G");
            assert!(output.status.success());
            let resolved = String::from_utf8(output.stdout).expect("UTF-8 ssh -G output");
            let values = resolved
                .lines()
                .filter_map(|line| line.split_once(' '))
                .filter(|(key, _)| key.eq_ignore_ascii_case("userknownhostsfile"))
                .map(|(_, value)| value.trim())
                .collect::<Vec<_>>();
            let expected = paths.known_hosts().to_string_lossy().into_owned();
            assert_eq!(values, [expected]);
        }
    }
}

#[cfg(unix)]
pub use unix::{
    GITHUB_KNOWN_HOSTS_V1, PrivateBareGitRepository, PrivateGitRepositoryError,
    PrivateGitRepositoryPaths, PrivateGitRepositoryPreparation,
};