debianize 0.163.0

Create Debian packaging from upstream sources
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
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
use breezyshim::branch::{Branch, PyBranch};
use breezyshim::commit::NullCommitReporter;
use breezyshim::debian::error::Error as BrzDebianError;
use breezyshim::debian::merge_upstream::{
    do_import, get_existing_imported_upstream_revids, get_tarballs,
};
use breezyshim::debian::upstream::{
    get_pristine_tar_source, // Use the standard version
    upstream_version_add_revision,
    PristineTarSource,
    UpstreamBranchSource,
    UpstreamSource,
};
use breezyshim::debian::{TarballKind, VersionKind, DEFAULT_ORIG_DIR};
use breezyshim::error::Error as BrzError;
use breezyshim::tree::MutableTree;
use breezyshim::tree::{PyTree, Tree};
use breezyshim::workingtree::{GenericWorkingTree, PyWorkingTree, WorkingTree};
use breezyshim::RevisionId;
use debian_analyzer::versions::debianize_upstream_version;
use debian_analyzer::wnpp::{BugId, BugKind};
use debian_analyzer::Certainty;
use debversion::Version;
use ognibuild::dependencies::debian::valid_debian_package_name;
use ognibuild::dependencies::debian::DebianDependency;
use std::ffi::OsString;
// use ognibuild::buildsystem::{InstallTarget, DependencyCategory};
// use pyo3::prelude::*;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::process::Command;
// use std::str::FromStr;
use upstream_ontologist::{get_upstream_info, ProviderError, UpstreamMetadata};

pub mod fixer;
pub mod names;
pub mod processors;
pub mod simple_apt_repo;
pub mod vcs;

pub fn default_debianize_cache_dir() -> std::io::Result<std::path::PathBuf> {
    xdg::BaseDirectories::with_prefix("debianize")?.create_cache_directory("")
}

/// Default implementation for creating distribution tarballs
pub fn default_create_dist(
    tree: &GenericWorkingTree,
    package: &str,
    version: &str,
    session: &mut dyn ognibuild::session::Session,
    target_dir: &Path,
    subpath: &Path,
) -> Result<OsString, Error> {
    log::info!(
        "Creating distribution tarball for {} version {} using ognibuild",
        package,
        version
    );

    // Create a simple log manager
    struct SimpleLogManager;
    impl ognibuild::logs::LogManager for SimpleLogManager {
        fn start(&mut self) -> std::io::Result<()> {
            // No-op implementation - just let output go to normal stdout/stderr
            Ok(())
        }
    }

    let mut log_manager = SimpleLogManager;

    // Try to create distribution tarball using ognibuild
    match ognibuild::dist::create_dist(
        session,
        tree,
        target_dir,
        Some(false), // include_controldir = false (don't include .git etc.)
        &mut log_manager,
        Some(version),
        subpath,
        Some(package), // temp_subdir
    ) {
        Ok(filename) => {
            log::info!("Successfully created distribution tarball: {:?}", filename);
            Ok(filename)
        }
        Err(e) => {
            log::warn!(
                "ognibuild dist creation failed: {}, falling back to simple export",
                e
            );

            // Fallback: create a simple tar.gz export
            create_simple_tarball(tree, package, version, target_dir, subpath)
        }
    }
}

/// Fallback function to create a simple tarball by exporting the tree
fn create_simple_tarball(
    tree: &GenericWorkingTree,
    package: &str,
    version: &str,
    target_dir: &Path,
    subpath: &Path,
) -> Result<OsString, Error> {
    use std::process::Command;

    log::info!(
        "Creating simple export tarball for {} version {}",
        package,
        version
    );

    // Create temporary directory for export
    let temp_dir = tempfile::tempdir()
        .map_err(|e| Error::Other(format!("Failed to create temp directory: {}", e)))?;

    let export_dir = temp_dir.path().join(format!("{}-{}", package, version));
    std::fs::create_dir_all(&export_dir)
        .map_err(|e| Error::Other(format!("Failed to create export directory: {}", e)))?;

    // Export the tree to the temporary directory
    let tree_path = tree.basedir().join(subpath);

    // Use tar to create an archive directly from the working tree
    let tarball_name = format!("{}_{}.orig.tar.gz", package, version);
    let tarball_path = target_dir.join(&tarball_name);

    // Create the target directory if it doesn't exist
    if let Some(parent) = tarball_path.parent() {
        std::fs::create_dir_all(parent)
            .map_err(|e| Error::Other(format!("Failed to create target directory: {}", e)))?;
    }

    // Use tar to create the tarball, excluding VCS directories
    let tar_output = Command::new("tar")
        .args([
            "-czf",
            tarball_path.to_str().unwrap(),
            "--exclude=.git",
            "--exclude=.bzr",
            "--exclude=.hg",
            "--exclude=.svn",
            "--exclude=_darcs",
            "--transform",
            &format!("s,^\\./,{}-{}/,", package, version),
            "-C",
            tree_path.to_str().unwrap(),
            ".",
        ])
        .output()
        .map_err(|e| Error::Other(format!("Failed to run tar command: {}", e)))?;

    if !tar_output.status.success() {
        return Err(Error::Other(format!(
            "tar command failed: {}",
            String::from_utf8_lossy(&tar_output.stderr)
        )));
    }

    log::info!("Created simple export tarball: {}", tarball_path.display());
    Ok(OsString::from(tarball_path))
}

pub fn write_changelog_template(
    path: &std::path::Path,
    source_name: &str,
    version: &Version,
    author: Option<(String, String)>,
    wnpp_bugs: Vec<(BugId, BugKind)>,
) -> Result<(), std::io::Error> {
    let author = author.unwrap_or_else(|| debian_changelog::get_maintainer().unwrap());
    let closes = if !wnpp_bugs.is_empty() {
        format!(
            " Closes: {}",
            wnpp_bugs
                .iter()
                .map(|(n, _k)| format!("#{}", n))
                .collect::<Vec<_>>()
                .join(", ")
        )
    } else {
        "".to_string()
    };
    let mut cl = debian_changelog::ChangeLog::new();

    cl.new_entry()
        .package(source_name.to_string())
        .version(version.clone())
        .distribution("UNRELEASED".to_string())
        .urgency(debian_changelog::Urgency::Low)
        .change_line(format!("  * Initial release.{}", closes))
        .maintainer(author)
        .finish();

    let buf = cl.to_string();

    std::fs::write(path, buf)?;

    Ok(())
}

pub fn use_packaging_branch(wt: &GenericWorkingTree, branch_name: &str) -> Result<(), BrzError> {
    let last_revision = wt.last_revision()?;
    let target_branch = match wt.controldir().open_branch(Some(branch_name)) {
        Ok(b) => b,
        Err(BrzError::NotBranchError { .. }) => wt.controldir().create_branch(Some(branch_name))?,
        Err(e) => return Err(e),
    };

    target_branch.generate_revision_history(&last_revision)?;
    log::info!("Switching to packaging branch {}.", branch_name);
    wt.controldir()
        .set_branch_reference(target_branch.as_ref(), Some(""))?;
    // TODO(jelmer): breezy bug?
    pyo3::Python::attach(|py| -> pyo3::PyResult<()> {
        use pyo3::IntoPyObject;
        let wt_py = wt.to_object(py);
        let branch_py = target_branch.into_pyobject(py)?;
        wt_py.setattr(py, "_branch", branch_py)?;
        Ok(())
    })
    .unwrap();
    Ok(())
}

/// Enhanced upstream version import from distribution tarball
pub fn import_upstream_version_from_dist(
    wt: &dyn PyWorkingTree,
    subpath: &std::path::Path,
    upstream_source: &UpstreamBranchSource,
    source_name: &str,
    upstream_version: &str,
    files_excluded: Option<&[&std::path::Path]>,
) -> Result<
    (
        HashMap<TarballKind, (RevisionId, PathBuf)>,
        HashMap<TarballKind, String>,
        String,
    ),
    BrzDebianError,
> {
    let orig_dir = Path::new(DEFAULT_ORIG_DIR).canonicalize().unwrap();

    let mut tag_names = HashMap::new();
    let td = tempfile::tempdir().unwrap();
    let locations = upstream_source.fetch_tarballs(
        Some(source_name),
        upstream_version,
        td.path(),
        Some(&[TarballKind::Orig]),
    )?;
    let tarball_filenames = match get_tarballs(
        &orig_dir,
        wt,
        source_name,
        upstream_version,
        locations
            .iter()
            .map(|x| x.as_ref())
            .collect::<Vec<_>>()
            .as_slice(),
    ) {
        Ok(filenames) => filenames,
        Err(BrzDebianError::BrzError(BrzError::FileExists(path, _))) => {
            log::warn!("Tarball {} exists, reusing existing file.", path.display());
            vec![orig_dir.join(path)]
        }
        Err(e) => return Err(e),
    };
    let upstream_revisions =
        upstream_source.version_as_revisions(Some(source_name), upstream_version, None)?;

    log::info!(
        "Importing upstream version {} from distribution tarball",
        upstream_version
    );

    let imported_revids = match do_import(
        wt,
        subpath,
        tarball_filenames
            .iter()
            .map(|x| x.as_path())
            .collect::<Vec<_>>()
            .as_slice(),
        source_name,
        upstream_version,
        None,
        upstream_source.upstream_branch().as_ref(),
        upstream_revisions,
        None,
        false,
        false,
        None,
        files_excluded,
    ) {
        Ok(revids) => {
            log::info!(
                "Successfully imported upstream version {}",
                upstream_version
            );
            revids
        }
        Err(BrzDebianError::UpstreamAlreadyImported(version)) => {
            log::warn!(
                "Upstream release {} already imported, reusing existing import",
                version
            );
            get_existing_imported_upstream_revids(upstream_source, source_name, upstream_version)?
        }
        Err(e) => {
            log::error!(
                "Failed to import upstream version {}: {}",
                upstream_version,
                e
            );
            return Err(e);
        }
    };
    let mut pristine_revids = HashMap::new();
    for (component, tag_name, revid, _pristine_tar_imported, subpath) in imported_revids {
        pristine_revids.insert(component.clone(), (revid, subpath));
        tag_names.insert(component, tag_name);
    }
    std::mem::drop(td);

    let upstream_branch_name = "upstream";
    match wt.controldir().create_branch(Some(upstream_branch_name)) {
        Ok(branch) => {
            branch
                .generate_revision_history(&pristine_revids.get(&TarballKind::Orig).unwrap().0)?;
            log::info!("Created upstream branch.");
        }
        Err(BrzError::AlreadyBranch(..)) => {
            log::info!("Upstream branch already exists; not creating.");
            wt.controldir().open_branch(Some(upstream_branch_name))?;
        }
        Err(e) => return Err(e.into()),
    }

    Ok((pristine_revids, tag_names, upstream_branch_name.to_string()))
}

pub fn import_upstream_dist(
    pristine_tar_source: &PristineTarSource,
    wt: &dyn PyWorkingTree,
    upstream_source: &UpstreamBranchSource,
    subpath: &Path,
    source_name: &str,
    upstream_version: &UpstreamVersion,
    files_excluded: Option<&[&std::path::Path]>,
) -> Result<(RevisionId, Option<String>, HashMap<TarballKind, String>), BrzDebianError> {
    let (mut pristine_revids, tag_names, upstream_branch_name) = if pristine_tar_source
        .has_version(Some(source_name), &upstream_version.version, None, false)?
    {
        log::warn!(
            "Upstream version {}/{} already imported.",
            source_name,
            upstream_version.version,
        );
        let pristine_revids = pristine_tar_source.version_as_revisions(
            Some(source_name),
            &upstream_version.version,
            None,
        )?;
        let upstream_branch_name = None;
        let tag_names = HashMap::new();
        (pristine_revids, tag_names, upstream_branch_name)
    } else {
        let (pristine_revids, tag_names, upstream_branch_name) = import_upstream_version_from_dist(
            wt,
            subpath,
            upstream_source,
            source_name,
            &upstream_version.version,
            files_excluded,
        )?;
        (pristine_revids, tag_names, Some(upstream_branch_name))
    };

    let orig_revid = pristine_revids.remove(&TarballKind::Orig).unwrap().0;
    Ok((orig_revid, upstream_branch_name, tag_names))
}

/// Enhanced upstream import with pristine-tar support
pub fn import_upstream_with_pristine_tar(
    wt: &dyn PyWorkingTree,
    subpath: &Path,
    upstream_source: &UpstreamBranchSource,
    source_name: &str,
    upstream_version: &UpstreamVersion,
    files_excluded: Option<&[&std::path::Path]>,
) -> Result<(RevisionId, Option<String>, HashMap<TarballKind, String>), BrzDebianError> {
    log::info!(
        "Importing upstream version {} with pristine-tar support",
        upstream_version.version
    );

    if let Some(excluded) = files_excluded {
        log::info!(
            "Excluding {} files/directories during import",
            excluded.len()
        );
    }

    // Get pristine-tar source
    let pristine_tar_source = match get_pristine_tar_source(wt, &wt.branch()) {
        Ok(pts) => pts,
        Err(e) => {
            log::warn!(
                "Failed to get pristine-tar source: {:?}. Falling back to basic import.",
                e
            );
            // Fall back to basic import without pristine-tar
            // For now, return an error since we need to update the basic import path
            return Err(BrzDebianError::BrzError(BrzError::UnknownFormat(
                "Failed to get pristine-tar source".to_string(),
            )));
        }
    };

    // Use the existing import_upstream_dist function with pristine-tar support
    import_upstream_dist(
        &pristine_tar_source,
        wt,
        upstream_source,
        subpath,
        source_name,
        upstream_version,
        files_excluded,
    )
}

/// Create kickstart function for distribution-based import
pub fn create_kickstart_from_dist<'a>(
    upstream_source: &'a UpstreamBranchSource,
    source_name: String,
    upstream_version: UpstreamVersion,
    files_excluded: Option<Vec<std::path::PathBuf>>,
) -> impl Fn(&dyn PyWorkingTree, &Path) -> Result<(), Error> + 'a {
    move |wt: &dyn PyWorkingTree, subpath: &Path| {
        log::info!(
            "Kickstarting from dist tarball. Using upstream version {}",
            upstream_version.version
        );

        // Convert files_excluded to the right format
        let files_excluded_refs: Option<Vec<&std::path::Path>> = files_excluded
            .as_ref()
            .map(|paths| paths.iter().map(|p| p.as_path()).collect());
        let files_excluded_slice: Option<&[&std::path::Path]> = files_excluded_refs.as_deref();

        // Import upstream with pristine-tar support
        let (upstream_dist_revid, _upstream_branch_name, _tag_names) =
            import_upstream_with_pristine_tar(
                wt,
                subpath,
                upstream_source,
                &source_name,
                &upstream_version,
                files_excluded_slice,
            )
            .map_err(|e| Error::Other(format!("Failed to import upstream: {}", e)))?;

        // Update the working tree to the upstream revision
        if wt.branch().last_revision() != upstream_dist_revid {
            wt.pull(
                upstream_source.upstream_branch().as_ref(),
                Some(true), // overwrite
                Some(&upstream_dist_revid),
                Some(false), // local
            )
            .map_err(Error::BrzError)?;

            log::info!(
                "Updated working tree to upstream revision {}",
                upstream_dist_revid
            );
        }

        // Create debian/source directory and format file
        let debian_path = subpath.join("debian");
        let source_path = debian_path.join("source");

        if !wt.has_filename(&source_path) {
            wt.mkdir(&source_path).map_err(Error::BrzError)?;
            wt.add(&[&source_path]).map_err(Error::BrzError)?;
        }

        let format_file = source_path.join("format");
        wt.put_file_bytes_non_atomic(&format_file, b"3.0 (quilt)\n")
            .map_err(Error::BrzError)?;

        log::info!("Created debian/source/format file");

        Ok(())
    }
}

/// Generate an upstream version for a package if all else fails.
pub fn last_resort_upstream_version(
    upstream_source: &UpstreamBranchSource,
    upstream_revision: &RevisionId,
) -> Result<String, BrzDebianError> {
    let upstream_version = upstream_version_add_revision(
        upstream_source.upstream_branch().as_ref(),
        "0",
        upstream_revision,
        Some("+"),
    )?;
    log::warn!(
        "Unable to determine upstream version, using {}.",
        upstream_version
    );
    Ok(upstream_version)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;
    use std::fs;
    use tempfile::tempdir;

    #[test]
    fn test_write_changelog_template() {
        let td = tempdir().unwrap();
        let path = td.path().join("changelog");

        let source_name = "test-package";
        let version = Version {
            epoch: None,
            upstream_version: "1.0".to_string(),
            debian_revision: Some("1".to_string()),
        };
        let author = Some(("Test Author".to_string(), "test@example.com".to_string()));
        let wnpp_bugs = vec![(123456, BugKind::ITP)];

        write_changelog_template(&path, source_name, &version, author, wnpp_bugs).unwrap();

        let content = fs::read_to_string(&path).unwrap();
        assert!(content.contains("test-package (1.0-1) UNRELEASED"));
        assert!(content.contains("* Initial release. Closes: #123456"));
        assert!(content.contains("Test Author <test@example.com>"));
    }

    #[test]
    fn test_write_changelog_template_no_bugs() {
        let td = tempdir().unwrap();
        let path = td.path().join("changelog");

        let source_name = "test-package";
        let version = Version {
            epoch: None,
            upstream_version: "1.0".to_string(),
            debian_revision: Some("1".to_string()),
        };
        let author = Some(("Test Author".to_string(), "test@example.com".to_string()));
        let wnpp_bugs = vec![];

        write_changelog_template(&path, source_name, &version, author, wnpp_bugs).unwrap();

        let content = fs::read_to_string(&path).unwrap();
        assert!(content.contains("test-package (1.0-1) UNRELEASED"));
        assert!(content.contains("* Initial release."));
        assert!(!content.contains("Closes:"));
        assert!(content.contains("Test Author <test@example.com>"));
    }

    #[test]
    fn test_default_debianize_cache_dir() {
        // This test accesses the real XDG dirs which may not be available
        // in restricted environments. We just verify it doesn't panic.
        let _result = default_debianize_cache_dir();
        // Don't assert on the result - it may fail in restricted environments like sbuild
    }

    // We'll skip testing the debianize stub function since it requires a WorkingTree
    // which is difficult to create in a test environment without actual repository data

    #[test]
    fn test_upstream_version_from() {
        // Test the From<String> implementation for UpstreamVersion
        let version_str = "1.2.3".to_string();
        let upstream_version = UpstreamVersion::from(version_str.clone());

        assert_eq!(upstream_version.version, "1.2.3");
        assert_eq!(
            upstream_version.mangled_version,
            debianize_upstream_version(&version_str)
        );
    }

    #[test]
    fn test_debianize_preferences_default() {
        // Test the default implementation of DebianizePreferences
        let prefs = DebianizePreferences::default();

        assert_eq!(prefs.use_inotify, None);
        assert_eq!(prefs.diligence, 0);
        assert!(!prefs.trust);
        assert!(!prefs.check);
        assert!(prefs.net_access);
        assert!(!prefs.force_subprocess);
        assert!(!prefs.force_new_directory);
        assert_eq!(prefs.compat_release, None);
        assert_eq!(prefs.minimum_certainty, Certainty::Confident);
        assert!(prefs.consult_external_directory);
        assert!(!prefs.verbose);
        // Default session should be isolated (Unshare on Linux, Plain on other platforms)
        match prefs.session {
            #[cfg(target_os = "linux")]
            SessionPreferences::Unshare(ref path) if path.as_os_str().is_empty() => {}
            #[cfg(not(target_os = "linux"))]
            SessionPreferences::Plain => {}
            _ => panic!("Expected default isolated session (Unshare on Linux, Plain otherwise)"),
        }
        assert!(prefs.create_dist.is_none());
        assert!(prefs.committer.is_none());
        assert_eq!(prefs.upstream_version_kind, VersionKind::Auto);
        assert_eq!(prefs.debian_revision, "1".to_string());
        assert!(prefs.team.is_none());
    }

    #[test]
    fn test_debianize_preferences_into_fixer_preferences() {
        // Test the conversion from DebianizePreferences to lintian_brush::FixerPreferences
        let debianize_prefs = DebianizePreferences {
            use_inotify: Some(true),
            diligence: 2,
            trust: true,
            check: true,
            net_access: false,
            force_subprocess: true,
            force_new_directory: true,
            compat_release: Some("stable".to_string()),
            minimum_certainty: Certainty::Certain,
            consult_external_directory: false,
            verbose: true,
            session: SessionPreferences::Plain,
            create_dist: None,
            committer: None,
            upstream_version_kind: VersionKind::Release,
            debian_revision: "2".to_string(),
            team: None,
            author: None,
            compat_level: Some(13),
            check_wnpp: true,
            run_fixers: true,
        };

        let fixer_prefs: lintian_brush::FixerPreferences = debianize_prefs.into();

        assert_eq!(fixer_prefs.diligence, Some(2));
        assert_eq!(fixer_prefs.net_access, Some(false));
        assert_eq!(fixer_prefs.compat_release, Some("stable".to_string()));
        assert_eq!(fixer_prefs.minimum_certainty, Some(Certainty::Certain));
        assert_eq!(fixer_prefs.trust_package, Some(true));
        assert_eq!(fixer_prefs.opinionated, Some(true));
        assert_eq!(fixer_prefs.allow_reformatting, Some(true));
    }

    #[test]
    fn test_error_display() {
        // Test the Display implementation for Error
        let error = Error::NoVcsLocation;
        assert_eq!(format!("{}", error), "No VCS location found.");

        let error = Error::SourcePackageNameInvalid("invalid:name".to_string());
        assert_eq!(
            format!("{}", error),
            "Invalid source package name: invalid:name."
        );

        let error = Error::IoError(std::io::Error::new(
            std::io::ErrorKind::NotFound,
            "File not found",
        ));
        assert_eq!(format!("{}", error), "I/O error: File not found");

        let error = Error::NoUpstreamReleases(Some("test-package".to_string()));
        assert_eq!(
            format!("{}", error),
            "No upstream releases found for test-package."
        );

        let error = Error::NoUpstreamReleases(None);
        assert_eq!(
            format!("{}", error),
            "No upstream releases found for unknown."
        );
    }

    #[test]
    fn test_session_preferences_create_session() {
        // Test Plain session creation
        let plain_pref = SessionPreferences::Plain;
        let session = plain_pref.create_session().unwrap();
        // Test that we can get the pwd (current working directory)
        let _pwd = session.pwd();

        // Test error variants for non-plain sessions
        // We can't test actual creation without proper setup

        // Check if schroot is installed before testing
        if std::process::Command::new("which")
            .arg("schroot")
            .output()
            .map(|o| o.status.success())
            .unwrap_or(false)
        {
            // Test that Schroot returns an error without valid chroot
            let schroot_pref = SessionPreferences::Schroot("test-chroot".to_string());
            let result = schroot_pref.create_session();
            assert!(result.is_err());
        }

        // Test that Unshare returns an error with dummy tarball
        let temp_dir = tempdir().unwrap();
        let tarball_path = temp_dir.path().join("test.tar.gz");
        std::fs::write(&tarball_path, b"dummy tarball content").unwrap();

        let unshare_pref = SessionPreferences::Unshare(tarball_path.clone());
        let result = unshare_pref.create_session();
        assert!(result.is_err());
    }

    #[test]
    fn test_detect_buildsystem_name() {
        use std::fs;

        let temp_dir = tempdir().unwrap();
        let temp_path = temp_dir.path();

        // Create a mock working tree implementation for testing
        // Since we can't easily create a real WorkingTree, we'll test the logic directly
        // by creating files and checking the expected behavior

        // Test Python setup.py detection
        fs::write(temp_path.join("setup.py"), "#!/usr/bin/env python").unwrap();
        // We can't test the actual function without a WorkingTree implementation
        // But we can test the logic by verifying the expected order

        let expected_buildfiles = [
            ("setup.py", "setup.py"),
            ("pyproject.toml", "setup.py"),
            ("package.json", "npm"),
            ("pom.xml", "maven"),
            ("dist.ini", "dist-zilla"),
            ("Makefile.PL", "makefile.pl"),
            ("Build.PL", "perl-build-tiny"),
            ("Cargo.toml", "cargo"),
            ("go.mod", "golang"),
            ("DESCRIPTION", "R"),
            ("DESCRIPTION.in", "octave"),
            ("Makefile", "make"),
            ("CMakeLists.txt", "cmake"),
            ("configure.ac", "autotools"),
            ("configure.in", "autotools"),
        ];

        // Test that we have the expected build files in the right order
        // This is a structural test since we can't easily mock WorkingTree
        assert_eq!(expected_buildfiles.len(), 15);
        assert_eq!(expected_buildfiles[0], ("setup.py", "setup.py"));
        assert_eq!(expected_buildfiles[2], ("package.json", "npm"));
        assert_eq!(expected_buildfiles[7], ("Cargo.toml", "cargo"));
    }

    #[test]
    fn test_determine_browser_url() {
        // Test GitHub HTTPS URL
        let github_https = url::Url::parse("https://github.com/user/repo.git").unwrap();
        let browser_url = determine_browser_url("git", &github_https);
        assert_eq!(
            browser_url,
            Some("https://github.com/user/repo".to_string())
        );

        // Test GitHub git protocol URL
        let github_git = url::Url::parse("git://github.com/user/repo.git").unwrap();
        let browser_url = determine_browser_url("git", &github_git);
        assert_eq!(
            browser_url,
            Some("https://github.com/user/repo".to_string())
        );

        // Test URL without .git extension
        let github_no_git = url::Url::parse("https://github.com/user/repo").unwrap();
        let browser_url = determine_browser_url("git", &github_no_git);
        assert_eq!(
            browser_url,
            Some("https://github.com/user/repo".to_string())
        );

        // Test GitLab URL conversion
        let gitlab_url = url::Url::parse("https://gitlab.com/user/repo.git").unwrap();
        let browser_url = determine_browser_url("git", &gitlab_url);
        assert_eq!(
            browser_url,
            Some("https://gitlab.com/user/repo".to_string())
        );

        // Test Salsa URL conversion
        let salsa_url = url::Url::parse("https://salsa.debian.org/user/repo.git").unwrap();
        let browser_url = determine_browser_url("git", &salsa_url);
        assert_eq!(
            browser_url,
            Some("https://salsa.debian.org/user/repo".to_string())
        );

        // Test unknown URL
        let unknown_url = url::Url::parse("https://example.com/user/repo.git").unwrap();
        let browser_url = determine_browser_url("git", &unknown_url);
        assert_eq!(browser_url, None);

        // Test non-git VCS
        let svn_url = url::Url::parse("svn://example.com/repo").unwrap();
        let browser_url = determine_browser_url("svn", &svn_url);
        assert_eq!(browser_url, None);
    }

    #[test]
    fn test_unsplit_vcs_url() {
        // Test basic URL
        let url = url::Url::parse("https://github.com/user/repo").unwrap();
        let result = unsplit_vcs_url("git", &url);
        assert_eq!(result, "https://github.com/user/repo");

        // Test URL with path
        let url = url::Url::parse("https://github.com/user/repo/tree/main").unwrap();
        let result = unsplit_vcs_url("git", &url);
        assert_eq!(result, "https://github.com/user/repo/tree/main");
    }

    #[test]
    fn test_get_maintainer() {
        // Test that get_maintainer returns something reasonable
        let (name, email) = get_maintainer();

        // We can't test the exact values since they depend on the environment,
        // but we can test that they're not empty and have reasonable format
        assert!(!name.is_empty());
        assert!(!email.is_empty());
        assert!(email.contains("@"));
    }

    #[test]
    fn test_find_wnpp_bugs_for_package() {
        // Test that find_wnpp_bugs_for_package returns without error
        // We can't easily test the actual functionality without network access
        // but we can test that the function signature is correct
        let result = find_wnpp_bugs_for_package("test-package", Some("upstream-name"));
        // The function should return a Result<Vec<(BugId, BugKind)>, Error>
        // We'll just verify it's callable and returns the right type
        assert!(result.is_ok() || result.is_err()); // Either is fine for this test

        // Test with None upstream name
        let result2 = find_wnpp_bugs_for_package("test-package", None);
        assert!(result2.is_ok() || result2.is_err()); // Either is fine for this test
    }

    #[test]
    fn test_import_metadata_from_path() {
        // Test that the function signature is correct
        // We can't easily test the full functionality without a WorkingTree and network
        let temp_dir = tempdir().unwrap();
        let temp_path = temp_dir.path();

        // Create a simple metadata file that upstream-ontologist can read
        let pyproject_content = r#"
[project]
name = "test-package"
version = "1.0.0"
description = "A test package"
"#;
        std::fs::write(temp_path.join("pyproject.toml"), pyproject_content).unwrap();

        // Test that we can create the basic structures
        let _metadata = UpstreamMetadata::new();
        let prefs = DebianizePreferences::default();

        // The function requires a WorkingTree, so we can't test it directly
        // But we can verify the setup is correct
        assert!(!prefs.trust);
        assert!(prefs.net_access);
        assert!(!prefs.check);
    }

    #[test]
    fn test_write_initial_changelog() {
        let temp_dir = tempdir().unwrap();
        let debian_path = temp_dir.path().join("debian");
        std::fs::create_dir_all(&debian_path).unwrap();

        let changelog_path = debian_path.join("changelog");
        let source_name = "test-package";
        let version = UpstreamVersion::from("1.0.0".to_string());
        let author = ("Test Author".to_string(), "test@example.com".to_string());
        let wnpp_bugs = vec![(123456, BugKind::ITP)];

        // We can't test the function directly without a WorkingTree,
        // but we can test write_changelog_template which it uses
        write_changelog_template(
            &changelog_path,
            source_name,
            &version.as_debian_version(),
            Some(author),
            wnpp_bugs,
        )
        .unwrap();

        let content = std::fs::read_to_string(&changelog_path).unwrap();
        assert!(content.contains("test-package"));
        assert!(content.contains("1.0.0-1"));
        assert!(content.contains("Test Author"));
        assert!(content.contains("Closes: #123456"));
    }

    #[test]
    fn test_upstream_version_as_debian_version() {
        let upstream_version = UpstreamVersion::from("1.0.0".to_string());
        let debian_version = upstream_version.as_debian_version();

        assert_eq!(debian_version.upstream_version, "1.0.0");
        assert_eq!(debian_version.debian_revision, Some("1".to_string()));
        assert_eq!(debian_version.epoch, None);
    }

    #[test]
    fn test_upstream_version_mangled() {
        // Test that mangled version is created correctly
        let upstream_version = UpstreamVersion::from("1.0.0-beta1".to_string());

        // The mangled version should be processed by debianize_upstream_version
        assert_eq!(upstream_version.version, "1.0.0-beta1");
        assert!(!upstream_version.mangled_version.is_empty());

        // Test that the mangled version is different for versions that need mangling
        let upstream_version2 = UpstreamVersion::from("1.0.0~beta1".to_string());
        assert_eq!(upstream_version2.version, "1.0.0~beta1");
        assert!(!upstream_version2.mangled_version.is_empty());
    }

    #[test]
    fn test_debianize_result_structure() {
        // Test that DebianizeResult has all expected fields
        let result = DebianizeResult {
            vcs_url: Some(url::Url::parse("https://github.com/user/repo").unwrap()),
            wnpp_bugs: vec![(123456, BugKind::ITP)],
            upstream_version: Some("1.0.0".to_string()),
            tag_names: HashMap::new(),
            upstream_branch_name: Some("upstream".to_string()),
        };

        assert!(result.vcs_url.is_some());
        assert_eq!(result.wnpp_bugs.len(), 1);
        assert_eq!(result.wnpp_bugs[0].0, 123456);
        assert_eq!(result.wnpp_bugs[0].1, BugKind::ITP);
        assert_eq!(result.upstream_version, Some("1.0.0".to_string()));
        assert_eq!(result.tag_names.len(), 0);
        assert_eq!(result.upstream_branch_name, Some("upstream".to_string()));
    }

    #[test]
    fn test_version_kind_default() {
        // Test that version kind defaults work correctly
        let prefs = DebianizePreferences::default();
        assert_eq!(prefs.upstream_version_kind, VersionKind::Auto);

        // Test that other version kinds can be set
        let prefs = DebianizePreferences {
            upstream_version_kind: VersionKind::Release,
            ..Default::default()
        };
        assert_eq!(prefs.upstream_version_kind, VersionKind::Release);
    }

    #[test]
    fn test_error_variants() {
        // Test all error variants to ensure completeness
        let errors = vec![
            Error::NoVcsLocation,
            Error::SourceNameUnknown(Some("test".to_string())),
            Error::SourceNameUnknown(None),
            Error::SourcePackageNameInvalid("invalid".to_string()),
            Error::MissingUpstreamInfo("test".to_string()),
            Error::NoUpstreamReleases(Some("test".to_string())),
            Error::NoUpstreamReleases(None),
            Error::Other("test".to_string()),
        ];

        for error in errors {
            // Test that each error can be displayed
            let _display = format!("{}", error);

            // Test that each error can be debugged
            let _debug = format!("{:?}", error);
        }
    }

    #[test]
    fn test_session_preferences_all_variants() {
        // Test all SessionPreferences variants
        let plain = SessionPreferences::Plain;
        let schroot = SessionPreferences::Schroot("test".to_string());
        let temp_dir = tempdir().unwrap();
        let tarball_path = temp_dir.path().join("test.tar.gz");
        std::fs::write(&tarball_path, b"test").unwrap();
        let unshare = SessionPreferences::Unshare(tarball_path);

        // Test that all variants are distinct
        assert_ne!(
            std::mem::discriminant(&plain),
            std::mem::discriminant(&schroot)
        );
        assert_ne!(
            std::mem::discriminant(&plain),
            std::mem::discriminant(&unshare)
        );
        assert_ne!(
            std::mem::discriminant(&schroot),
            std::mem::discriminant(&unshare)
        );
    }

    #[test]
    fn test_bug_kind_usage() {
        // Test that BugKind enum works as expected
        let itp_bug = BugKind::ITP;
        let rfp_bug = BugKind::RFP;

        // Test that bug kinds can be used in vectors
        let bugs = [(123456, itp_bug), (789012, rfp_bug)];
        assert_eq!(bugs.len(), 2);

        // Test that bug kinds are distinct
        assert_ne!(
            std::mem::discriminant(&BugKind::ITP),
            std::mem::discriminant(&BugKind::RFP)
        );
    }

    #[test]
    fn test_upstream_metadata_basic_usage() {
        // Test basic upstream metadata functionality
        let metadata = UpstreamMetadata::new();

        // Test that new metadata is empty
        assert_eq!(metadata.name(), None);
        assert_eq!(metadata.summary(), None);
        assert_eq!(metadata.description(), None);
        assert_eq!(metadata.homepage(), None);
        assert_eq!(metadata.repository(), None);
        assert_eq!(metadata.archive(), None);
        assert_eq!(metadata.license(), None);
        assert_eq!(metadata.maintainer(), None);
        assert_eq!(metadata.author(), None);
        assert_eq!(metadata.version(), None);
    }

    #[test]
    fn test_compat_level_handling() {
        // Test that compat level is handled correctly
        let mut prefs = DebianizePreferences::default();
        assert_eq!(prefs.compat_level, None);

        prefs.compat_level = Some(13);
        assert_eq!(prefs.compat_level, Some(13));

        prefs.compat_level = Some(14);
        assert_eq!(prefs.compat_level, Some(14));
    }

    #[test]
    fn test_run_fixers_flag() {
        // Test that run_fixers flag works as expected
        let mut prefs = DebianizePreferences::default();
        assert!(prefs.run_fixers);

        prefs.run_fixers = false;
        assert!(!prefs.run_fixers);

        prefs.run_fixers = true;
        assert!(prefs.run_fixers);
    }

    #[test]
    fn test_check_wnpp_flag() {
        // Test that check_wnpp flag works as expected
        let mut prefs = DebianizePreferences::default();
        assert!(prefs.check_wnpp);

        prefs.check_wnpp = false;
        assert!(!prefs.check_wnpp);

        prefs.check_wnpp = true;
        assert!(prefs.check_wnpp);
    }
}

#[derive(Debug, Clone)]
pub enum SessionPreferences {
    Plain,
    Schroot(String),
    Unshare(PathBuf),
}

impl SessionPreferences {
    /// Create a default isolated session preference.
    /// On Linux, this uses UnshareSession (ognibuild will handle setup).
    /// On other platforms, falls back to PlainSession.
    pub fn default_isolated() -> Self {
        #[cfg(target_os = "linux")]
        {
            SessionPreferences::Unshare(PathBuf::new()) // Empty path - ognibuild will handle setup
        }
        #[cfg(not(target_os = "linux"))]
        {
            SessionPreferences::Plain
        }
    }

    /// Acquire a session based on preferences. This is a convenience method for tests.
    pub fn acquire(&self) -> Result<Box<dyn ognibuild::session::Session>, Error> {
        self.create_session()
    }

    pub fn create_session(&self) -> Result<Box<dyn ognibuild::session::Session>, Error> {
        match self {
            SessionPreferences::Plain => {
                Ok(Box::new(ognibuild::session::plain::PlainSession::new()))
            }
            SessionPreferences::Schroot(name) => {
                #[cfg(target_os = "linux")]
                {
                    ognibuild::session::schroot::SchrootSession::new(name, None)
                        .map(Box::new)
                        .map(|b| b as Box<dyn ognibuild::session::Session>)
                        .map_err(|e| {
                            Error::Other(format!("Failed to create schroot session: {}", e))
                        })
                }
                #[cfg(not(target_os = "linux"))]
                {
                    Err(Error::Other(
                        "Schroot is only available on Linux".to_string(),
                    ))
                }
            }
            SessionPreferences::Unshare(path) => {
                #[cfg(target_os = "linux")]
                {
                    if path.as_os_str().is_empty() {
                        // Use ognibuild's cached Debian session API
                        // This will use ~/.cache/ognibuild/images/debian-sid-{arch}.tar.gz
                        log::info!("Creating unshare session from cached Debian sid image");
                        ognibuild::session::unshare::create_debian_session_for_testing(
                            "sid",
                            false // Whether to allow network access
                                  // TODO: Make this configurable
                        )
                        .map(Box::new)
                        .map(|b| b as Box<dyn ognibuild::session::Session>)
                        .map_err(|e| {
                            Error::Other(format!(
                                "Failed to create unshare session from cached image: {}. \
                                 Ensure the image is cached at ~/.cache/ognibuild/images/debian-sid-*.tar.gz",
                                e
                            ))
                        })
                    } else {
                        // Use specific tarball path
                        ognibuild::session::unshare::UnshareSession::from_tarball(path)
                            .map(Box::new)
                            .map(|b| b as Box<dyn ognibuild::session::Session>)
                            .map_err(|e| {
                                Error::Other(format!("Failed to create unshare session: {}", e))
                            })
                    }
                }
                #[cfg(not(target_os = "linux"))]
                {
                    Err(Error::Other(
                        "Unshare is only available on Linux".to_string(),
                    ))
                }
            }
        }
    }
}

pub struct DebianizePreferences {
    pub use_inotify: Option<bool>,
    pub diligence: u8,
    pub trust: bool,
    pub check: bool,
    pub net_access: bool,
    pub force_subprocess: bool,
    pub force_new_directory: bool,
    pub compat_release: Option<String>,
    pub minimum_certainty: Certainty,
    pub consult_external_directory: bool,
    pub verbose: bool,
    pub session: SessionPreferences,
    pub create_dist: Option<
        Box<
            dyn for<'a, 'b, 'c, 'd, 'e> Fn(
                &'a dyn PyTree,
                &'b str,
                &'c Version,
                &'d Path,
                &'e Path,
            )
                -> Result<bool, breezyshim::debian::error::Error>,
        >,
    >,
    pub committer: Option<String>,
    pub upstream_version_kind: VersionKind,
    pub debian_revision: String,
    pub team: Option<String>,
    pub author: Option<String>,
    pub compat_level: Option<u32>,
    pub check_wnpp: bool,
    pub run_fixers: bool,
}

impl Default for DebianizePreferences {
    fn default() -> Self {
        let author = debian_changelog::get_maintainer();
        Self {
            use_inotify: None,
            diligence: 0,
            trust: false,
            check: false,
            net_access: true,
            force_subprocess: false,
            force_new_directory: false,
            compat_release: None,
            minimum_certainty: Certainty::Confident,
            consult_external_directory: true,
            verbose: false,
            session: SessionPreferences::default_isolated(),
            create_dist: None,
            committer: None,
            upstream_version_kind: VersionKind::Auto,
            debian_revision: "1".to_string(),
            team: None,
            author: author.map(|(name, email)| format!("{} <{}>", name, email)),
            compat_level: None,
            check_wnpp: true,
            run_fixers: true,
        }
    }
}

impl From<DebianizePreferences> for lintian_brush::FixerPreferences {
    fn from(p: DebianizePreferences) -> Self {
        Self {
            diligence: Some(p.diligence.into()),
            net_access: Some(p.net_access),
            compat_release: p.compat_release,
            minimum_certainty: Some(p.minimum_certainty),
            trust_package: Some(p.trust),
            opinionated: Some(true),
            allow_reformatting: Some(true),
            ..Default::default()
        }
    }
}

#[derive(Debug)]
pub enum Error {
    DebianDirectoryExists(PathBuf),
    DebianizedPackageRequirementMismatch {
        dep: DebianDependency,
        binary_names: Vec<String>,
        version: Version,
        branch: Option<url::Url>,
    },
    EditorError(debian_analyzer::editor::EditorError),
    MissingUpstreamInfo(String),
    NoVcsLocation,
    NoUpstreamReleases(Option<String>),
    SourcePackageNameInvalid(String),
    SubdirectoryNotFound {
        subpath: PathBuf,
        version: Option<String>,
    },
    IoError(std::io::Error),
    BrzError(BrzError),
    SqlxError(sqlx::Error),
    SourceNameUnknown(Option<String>),
    Other(String),
    ProviderError(ProviderError),
    UncommittedChanges,
}

impl From<sqlx::Error> for Error {
    fn from(e: sqlx::Error) -> Self {
        Error::SqlxError(e)
    }
}

impl From<BrzError> for Error {
    fn from(e: BrzError) -> Self {
        Error::BrzError(e)
    }
}

impl From<std::io::Error> for Error {
    fn from(e: std::io::Error) -> Self {
        Error::IoError(e)
    }
}

impl From<debian_analyzer::editor::EditorError> for Error {
    fn from(e: debian_analyzer::editor::EditorError) -> Self {
        match e {
            debian_analyzer::editor::EditorError::IoError(e) => Error::IoError(e),
            debian_analyzer::editor::EditorError::BrzError(e) => Error::BrzError(e),
            e => Error::EditorError(e),
        }
    }
}

impl From<ProviderError> for Error {
    fn from(e: ProviderError) -> Self {
        Error::ProviderError(e)
    }
}

impl From<BrzDebianError> for Error {
    fn from(e: BrzDebianError) -> Self {
        match e {
            BrzDebianError::BrzError(brz_err) => Error::BrzError(brz_err),
            _ => Error::Other(format!("Debian error: {:?}", e)),
        }
    }
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        use Error::*;
        match self {
            DebianDirectoryExists(path) => {
                write!(f, "Debian directory already exists at {}.", path.display())
            }
            DebianizedPackageRequirementMismatch {
                dep,
                binary_names,
                version,
                branch,
            } => {
                write!(
                f,
                "Debianized package {} (version: {}) from {} does not match requirements for {}.",
                binary_names.join(", "),
                version,
                branch.as_ref().map_or_else(|| "unknown branch".to_string(), |b| b.to_string()),
                dep.relation_string(),
            )
            }
            NoVcsLocation => {
                write!(f, "No VCS location found.")
            }
            NoUpstreamReleases(source_name) => {
                write!(
                    f,
                    "No upstream releases found for {}.",
                    source_name.as_deref().unwrap_or("unknown")
                )
            }
            SourcePackageNameInvalid(name) => write!(f, "Invalid source package name: {}.", name),
            SubdirectoryNotFound { subpath, version } => {
                write!(
                    f,
                    "Subdirectory {} not found in upstream source{}.",
                    subpath.display(),
                    version
                        .as_ref()
                        .map(|v| format!(" for version {}", v))
                        .unwrap_or_default()
                )
            }
            IoError(e) => write!(f, "I/O error: {}", e),
            BrzError(e) => write!(f, "Breezy error: {}", e),
            MissingUpstreamInfo(name) => write!(f, "Missing upstream information for {}.", name),
            EditorError(e) => write!(f, "Editor error: {}", e),
            SqlxError(e) => write!(f, "SQLx error: {}", e),
            SourceNameUnknown(name) => write!(
                f,
                "Unable to determine source name{}",
                name.as_ref()
                    .map(|n| format!(" from {}", n))
                    .unwrap_or_default()
            ),
            Other(msg) => write!(f, "{}", msg),
            ProviderError(e) => write!(f, "Provider error: {:?}", e),
            UncommittedChanges => write!(
                f,
                "Working tree has uncommitted changes. Use --force to override."
            ),
        }
    }
}

impl std::error::Error for Error {}

pub fn debianize(
    wt: &GenericWorkingTree,
    subpath: &Path,
    upstream_branch: Option<&dyn PyBranch>,
    _upstream_subpath: Option<&Path>,
    preferences: &DebianizePreferences,
    version: Option<&str>,
    upstream_metadata: &UpstreamMetadata,
) -> Result<DebianizeResult, Error> {
    // Lock the working tree
    let _lock = wt.lock_write()?;

    // Check if debian directory already exists
    let debian_path = subpath.join("debian");
    if wt.has_filename(&debian_path) {
        if !preferences.force_new_directory {
            return Err(Error::DebianDirectoryExists(debian_path));
        }
        // Remove existing debian directory if force_new_directory is true
        wt.remove(&[&debian_path])?;
    }

    // Set up reset on failure
    let _reset_guard = ResetOnFailure::new(wt, subpath)?;

    // Gather metadata
    let mut metadata = upstream_metadata.clone();
    import_metadata_from_path(wt, subpath, &mut metadata, preferences)?;

    // Create upstream source
    if upstream_branch.is_none() {
        return Err(Error::Other("No upstream branch provided".to_string()));
    }

    // Create upstream source for enhanced import capabilities
    let upstream_source = if let Some(upstream_branch) = upstream_branch {
        log::info!("Creating UpstreamBranchSource for enhanced upstream metadata handling");

        // Get the control directory from the working tree
        let controldir = wt.controldir();

        // Try to downcast to GenericControlDir so we can use it as PyControlDir
        match controldir
            .as_any()
            .downcast_ref::<breezyshim::controldir::GenericControlDir>()
        {
            Some(generic_controldir) => {
                // Now we can use it as &dyn PyControlDir
                let py_controldir: &dyn breezyshim::controldir::PyControlDir = generic_controldir;

                // Create the create_dist function if available, adapting the signature
                let create_dist_fn: Option<
                    Box<
                        dyn Fn(
                                &dyn PyTree,
                                &str,
                                &str,
                                &Path,
                                &Path,
                            ) -> Result<OsString, BrzDebianError>
                            + Send
                            + Sync,
                    >,
                > = preferences.create_dist.as_ref().map(|_f| {
                    // Create a simple create_dist function that returns a standard tarball name
                    // The actual tarball creation will be handled elsewhere in the flow
                    Box::new(
                        |_tree: &dyn PyTree,
                         package: &str,
                         version: &str,
                         _target_dir: &Path,
                         _subpath: &Path| {
                            // Return the expected tarball name - this is what UpstreamBranchSource needs
                            Ok(OsString::from(format!("{}-{}.tar.gz", package, version)))
                        },
                    )
                        as Box<
                            dyn Fn(
                                    &dyn PyTree,
                                    &str,
                                    &str,
                                    &Path,
                                    &Path,
                                )
                                    -> Result<OsString, BrzDebianError>
                                + Send
                                + Sync,
                        >
                });

                // Create the UpstreamBranchSource
                match UpstreamBranchSource::from_branch(
                    upstream_branch,
                    Some(preferences.upstream_version_kind.clone()),
                    py_controldir,
                    create_dist_fn,
                ) {
                    Ok(source) => {
                        log::info!("Successfully created UpstreamBranchSource");
                        Some(source)
                    }
                    Err(e) => {
                        log::warn!("Failed to create UpstreamBranchSource: {:?}, falling back to basic version detection", e);
                        None
                    }
                }
            }
            None => {
                log::warn!("Failed to downcast ControlDir to GenericControlDir, falling back to basic version detection");
                None
            }
        }
    } else {
        None
    };
    // Determine upstream version
    let upstream_version = if let Some(v) = version {
        UpstreamVersion::from(v.to_string())
    } else if let Some(ref upstream_source) = upstream_source {
        // Use the enhanced upstream version determination
        determine_upstream_version(
            upstream_source,
            &metadata,
            preferences.upstream_version_kind.clone(),
        )?
    } else {
        return Err(Error::Other(
            "Cannot determine upstream version without upstream source".to_string(),
        ));
    };

    log::info!("Using upstream version: {}", upstream_version.version);

    // Create session for build operations
    let session = preferences.session.create_session()?;

    // Import upstream version
    // For now, create a basic implementation that imports content from the upstream branch
    let upstream_branch_ref = upstream_branch.unwrap();
    let _orig_revid = upstream_branch_ref.last_revision();

    // Create a basic upstream import by copying content from the upstream branch
    let source_name = generic_get_source_name(wt, subpath, &metadata)
        .ok_or_else(|| Error::SourceNameUnknown(metadata.name().map(|s| s.to_string())))?;
    let _upstream_branch_name = basic_import_upstream_version(
        wt,
        subpath,
        upstream_branch_ref,
        &source_name,
        &upstream_version.version,
    )?;

    let _import_tag_names: HashMap<TarballKind, String> = HashMap::new();

    // Create debian directory
    let buildsystem_subpath = if subpath == Path::new("") {
        PathBuf::new()
    } else {
        subpath.to_path_buf()
    };

    // Create debian directory if it doesn't exist
    if !wt.has_filename(&debian_path) {
        wt.mkdir(&debian_path)?;
    } else if !wt.is_versioned(&debian_path) {
        // If it exists but isn't versioned, add it
        wt.add(&[&debian_path])?;
    }

    // Get maintainer information
    let (maintainer_name, maintainer_email) = if let Some(ref author) = preferences.author {
        // Parse the author field which should be in "Name <email>" format
        let parts: Vec<&str> = author.splitn(2, '<').collect();
        if parts.len() == 2 {
            let name = parts[0].trim().to_string();
            let email = parts[1].trim_end_matches('>').to_string();
            (name, email)
        } else {
            get_maintainer()
        }
    } else {
        get_maintainer()
    };
    let maintainer = format!("{} <{}>", maintainer_name, maintainer_email);

    // Create buildsystem instance for enhanced dependency resolution
    // If subpath is empty, use the working tree's base directory
    let buildsystem_path = if subpath.as_os_str().is_empty() {
        wt.basedir()
    } else {
        wt.basedir().join(subpath)
    };
    log::debug!("Detecting buildsystems in path: {:?}", buildsystem_path);
    let buildsystems = ognibuild::buildsystem::detect_buildsystems(&buildsystem_path);
    let buildsystem = buildsystems
        .into_iter()
        .next()
        .ok_or_else(|| Error::Other("No buildsystem detected".to_string()))?;

    // Use processors to create proper control file
    let compat_release = preferences
        .compat_release
        .clone()
        .unwrap_or_else(|| "unstable".to_string());

    // Create enhanced kickstart function if we have an upstream source
    // TODO: Fix lifetime issues with closure capturing upstream_source reference
    let kickstart_from_dist: Option<
        Box<dyn FnOnce(&dyn PyWorkingTree, &Path) -> Result<(), Error>>,
    > = None;

    // Use the session created from preferences

    // Run the appropriate processor
    processors::process(
        session.as_ref(),
        wt,
        subpath.to_path_buf(),
        debian_path.clone(),
        upstream_version.version.clone(),
        &metadata,
        compat_release,
        buildsystem,
        buildsystem_subpath,
        Some(maintainer.clone()),
        kickstart_from_dist,
    )?;

    // TODO: Avoid re-parsing debian/control here; ideally the processor would
    // return the source name it chose.
    let control_path = wt.abspath(&debian_path.join("control"))?;
    let control = debian_analyzer::control::TemplatedControlEditor::open(&control_path)
        .map_err(|e| Error::Other(format!("Failed to open debian/control: {}", e)))?;
    let source_name = control
        .source()
        .and_then(|s| s.name())
        .unwrap_or(source_name);

    if !valid_debian_package_name(&source_name) {
        return Err(Error::SourcePackageNameInvalid(source_name));
    }

    log::info!("Using source package name: {}", source_name);

    // Create debian/source directory and format file
    let source_path = debian_path.join("source");
    if !wt.has_filename(&source_path) {
        wt.mkdir(&source_path)?;
        wt.add(&[&source_path])?;
    }
    let format_file = source_path.join("format");
    wt.put_file_bytes_non_atomic(&format_file, b"3.0 (quilt)\n")?;

    // Update VCS fields
    let mut vcs_url = None;
    // Try to update official VCS information
    match vcs::update_official_vcs(
        wt,
        subpath,
        None, // Let it guess the repository URL
        preferences.committer.as_deref(),
        false, // force
        false, // create
    ) {
        Ok(url) => {
            vcs_url = url.parse().ok();
            log::info!("Successfully set VCS URL: {}", url);
        }
        Err(e) => {
            log::debug!("Could not update VCS information: {}", e);
            // Fallback to the old method if the new method fails
            match determine_vcs_url(&wt.branch(), subpath) {
                Ok(url) => {
                    let vcs_type = match wt.branch().vcs_type() {
                        breezyshim::foreign::VcsType::Git => "git",
                        breezyshim::foreign::VcsType::Bazaar => "bzr",
                        breezyshim::foreign::VcsType::Svn => "svn",
                        breezyshim::foreign::VcsType::Hg => "hg",
                        breezyshim::foreign::VcsType::Cvs => "cvs",
                        breezyshim::foreign::VcsType::Darcs => "darcs",
                        breezyshim::foreign::VcsType::Fossil => "fossil",
                        breezyshim::foreign::VcsType::Arch => "arch",
                        breezyshim::foreign::VcsType::Svk => "svk",
                    };
                    vcs_url = Some(url.clone());
                    update_vcs_fields(wt, &debian_path, &url, vcs_type)?;
                }
                Err(e) => {
                    log::debug!("Could not determine VCS URL: {}", e);
                }
            }
        }
    }

    // Look for WNPP bugs
    let wnpp_bugs = if preferences.check_wnpp {
        find_wnpp_bugs_for_package(&source_name, metadata.name())?
    } else {
        vec![]
    };

    // Create changelog
    log::info!(
        "Writing initial changelog with source_name: {}",
        source_name
    );
    write_initial_changelog(
        wt,
        &debian_path,
        &source_name,
        &upstream_version.mangled_version,
        &maintainer_name,
        &maintainer_email,
        &wnpp_bugs,
    )?;

    // Commit the changes
    let upstream_revid = upstream_branch.unwrap().last_revision();
    let tag_names = commit_debianization(
        wt,
        subpath,
        &upstream_revid,
        &upstream_version.version,
        &upstream_version.mangled_version,
    )?;

    // Run lintian fixers if requested
    if preferences.run_fixers {
        run_debianize_fixers(wt, subpath, preferences)?;
    }

    Ok(DebianizeResult {
        vcs_url,
        wnpp_bugs,
        upstream_version: Some(upstream_version.version),
        tag_names,
        upstream_branch_name: upstream_branch.and_then(|b| b.name()),
    })
}

#[derive(Default, Debug, serde::Serialize)]
pub struct DebianizeResult {
    pub vcs_url: Option<url::Url>,
    pub wnpp_bugs: Vec<(BugId, BugKind)>,
    pub upstream_version: Option<String>,
    pub tag_names: HashMap<String, RevisionId>,
    pub upstream_branch_name: Option<String>,
}

pub(crate) struct ResetOnFailure<'a>(&'a dyn PyWorkingTree, PathBuf);

impl<'a> ResetOnFailure<'a> {
    pub fn new(wt: &'a dyn PyWorkingTree, subpath: &Path) -> Result<Self, BrzError> {
        // Try to check if tree is clean, but handle dirstate errors gracefully
        match wt.basis_tree() {
            Ok(basis_tree) => {
                match breezyshim::workspace::check_clean_tree(wt, &basis_tree, subpath) {
                    Ok(_) => {}
                    Err(BrzError::Other(ref py_err))
                        if py_err.to_string().contains("IndexError") =>
                    {
                        // Ignore IndexError from dirstate issues in test environments
                        log::warn!("Ignoring dirstate IndexError during clean tree check");
                    }
                    Err(e) => return Err(e),
                }
            }
            Err(e) => {
                log::warn!("Could not get basis tree: {:?}", e);
            }
        }
        Ok(Self(wt, subpath.to_path_buf()))
    }
}

impl<'a> Drop for ResetOnFailure<'a> {
    fn drop(&mut self) {
        if std::thread::panicking() {
            match breezyshim::workspace::reset_tree(self.0, None, Some(&self.1)) {
                Ok(_) => log::info!("Reset tree after failure"),
                Err(e) => log::error!("Failed to reset tree: {:?}", e),
            }
        }
    }
}

/// Run lintian fixers on the debianized package
fn run_debianize_fixers(
    wt: &GenericWorkingTree,
    subpath: &Path,
    preferences: &DebianizePreferences,
) -> Result<(), Error> {
    log::info!("Running lintian fixers on debianized package");

    // Get available fixers
    let fixers: Vec<Box<dyn lintian_brush::Fixer>> = lintian_brush::all_lintian_fixers().collect();

    log::info!("Found {} lintian fixers", fixers.len());

    // Convert DebianizePreferences to FixerPreferences
    let fixer_preferences: lintian_brush::FixerPreferences = lintian_brush::FixerPreferences {
        compat_release: preferences.compat_release.clone(),
        minimum_certainty: Some(preferences.minimum_certainty),
        trust_package: Some(preferences.trust),
        allow_reformatting: Some(true),
        net_access: Some(preferences.net_access),
        opinionated: Some(true),
        diligence: Some(preferences.diligence as i32),
        ..Default::default()
    };

    // Run the fixers
    match lintian_brush::run_lintian_fixers(
        wt,
        &fixers,
        None::<fn() -> bool>, // No custom changelog update logic
        preferences.verbose,
        preferences.committer.as_deref(),
        &fixer_preferences,
        Some(true), // use_dirty_tracker
        Some(subpath),
        None, // changes_by
        None, // timeout
        None, // multi_progress
    ) {
        Ok(result) => {
            // Check the actual methods available on ManyResult
            log::info!("Lintian fixers completed successfully");

            // Count successful fixers from the result
            let success_count = result.success.len();
            let failed_count = result.failed_fixers.len();

            log::info!(
                "Lintian fixers completed: {} applied, {} failed",
                success_count,
                failed_count
            );

            if failed_count > 0 {
                log::warn!("Some lintian fixers failed to apply");
                for (fixer_name, error) in &result.failed_fixers {
                    log::debug!("Fixer {} failed: {:?}", fixer_name, error);
                }
            }
        }
        Err(e) => {
            log::warn!("Failed to run lintian fixers: {:?}", e);
            // Don't fail the entire debianize process if lintian fixers fail
        }
    }

    Ok(())
}

fn generic_get_source_name(
    wt: &dyn PyWorkingTree,
    subpath: &Path,
    metadata: &UpstreamMetadata,
) -> Option<String> {
    let mut source_name = if let Some(name) = metadata.name() {
        let mut source_name = names::upstream_name_to_debian_source_name(name);
        if !valid_debian_package_name(source_name.as_ref().unwrap()) {
            source_name = None;
        }
        source_name
    } else {
        None
    };

    if source_name.is_none() {
        source_name = names::upstream_name_to_debian_source_name(
            wt.abspath(subpath).unwrap().to_str().unwrap(),
        );
        if !valid_debian_package_name(source_name.as_ref().unwrap()) {
            source_name = None;
        }
    }
    source_name
}

fn import_metadata_from_path(
    tree: &dyn PyWorkingTree,
    subpath: &Path,
    metadata: &mut UpstreamMetadata,
    preferences: &DebianizePreferences,
) -> Result<(), ProviderError> {
    let p = tree.abspath(subpath).unwrap();
    let rt = tokio::runtime::Runtime::new().unwrap();
    metadata.update(rt.block_on(get_upstream_info(
        &p,
        Some(preferences.trust),
        Some(preferences.net_access),
        Some(preferences.consult_external_directory),
        Some(preferences.check),
    ))?);
    Ok(())
}

#[derive(Clone)]
pub struct UpstreamVersion {
    pub version: String,
    pub mangled_version: String,
}

impl From<String> for UpstreamVersion {
    fn from(v: String) -> Self {
        Self {
            version: v.clone(),
            mangled_version: debianize_upstream_version(&v),
        }
    }
}

impl UpstreamVersion {
    /// Convert the upstream version to a debian version with revision "1"
    pub fn as_debian_version(&self) -> Version {
        Version {
            epoch: None,
            upstream_version: self.mangled_version.clone(),
            debian_revision: Some("1".to_string()),
        }
    }
}

/// Determine the upstream version to use.
pub fn determine_upstream_version(
    upstream_source: &UpstreamBranchSource,
    metadata: &UpstreamMetadata,
    version_kind: VersionKind,
) -> Result<UpstreamVersion, Error> {
    let name = metadata.name();

    // Ask the upstream source for the latest version.
    if let Some((upstream_version, mangled_version)) =
        upstream_source.get_latest_version(name, None).unwrap()
    {
        return Ok(UpstreamVersion {
            version: upstream_version,
            mangled_version,
        });
    }

    if version_kind == VersionKind::Release {
        return Err(Error::NoUpstreamReleases(
            metadata.name().map(|x| x.to_string()),
        ));
    }

    let upstream_revision = upstream_source.upstream_branch().last_revision();

    if let Some(next_upstream_version) = metadata.version() {
        // They haven't done any releases yet. Assume we're ahead of the next announced release?
        let next_upstream_version = debianize_upstream_version(next_upstream_version);
        let upstream_version = upstream_version_add_revision(
            upstream_source.upstream_branch().as_ref(),
            &next_upstream_version,
            &upstream_revision,
            Some("~"),
        )
        .map_err(|e| match e {
            BrzDebianError::BrzError(brz_err) => Error::BrzError(brz_err),
            _ => Error::BrzError(BrzError::Other(pyo3::PyErr::new::<
                pyo3::exceptions::PyRuntimeError,
                _,
            >(format!(
                "Debian error: {:?}",
                e
            )))),
        })?;
        return Ok(UpstreamVersion::from(upstream_version));
    }

    let upstream_version = upstream_version_add_revision(
        upstream_source.upstream_branch().as_ref(),
        "0",
        &upstream_revision,
        Some("+"),
    )
    .map_err(|e| match e {
        BrzDebianError::BrzError(brz_err) => Error::BrzError(brz_err),
        _ => Error::BrzError(BrzError::Other(pyo3::PyErr::new::<
            pyo3::exceptions::PyRuntimeError,
            _,
        >(format!("Debian error: {:?}", e)))),
    })?;
    log::warn!(
        "Unable to determine upstream version, using {}.",
        upstream_version
    );
    Ok(UpstreamVersion::from(upstream_version))
}

/// Get maintainer information from environment or git config
fn get_maintainer() -> (String, String) {
    // Try to get from environment variables first
    if let (Ok(name), Ok(email)) = (std::env::var("DEBFULLNAME"), std::env::var("DEBEMAIL")) {
        return (name, email);
    }

    // Fall back to git config
    let name = Command::new("git")
        .args(["config", "user.name"])
        .output()
        .ok()
        .and_then(|o| String::from_utf8(o.stdout).ok())
        .map(|s| s.trim().to_string())
        .unwrap_or_else(|| "Debian Maintainer".to_string());

    let email = Command::new("git")
        .args(["config", "user.email"])
        .output()
        .ok()
        .and_then(|o| String::from_utf8(o.stdout).ok())
        .map(|s| s.trim().to_string())
        .unwrap_or_else(|| "maintainer@debian.org".to_string());

    (name, email)
}

/// Determine VCS URL for the branch
fn determine_vcs_url(branch: &dyn Branch, subpath: &Path) -> Result<url::Url, Error> {
    // Try to get the public branch URL
    let branch_url = match branch.get_public_branch() {
        Some(url) => url,
        None => {
            // If no public branch is set, try to use the push location
            match branch.get_push_location() {
                Some(url) => url,
                None => {
                    // As a last resort, use the current branch URL
                    branch.get_user_url().to_string()
                }
            }
        }
    };

    // Parse the URL
    let url = branch_url
        .parse::<url::Url>()
        .map_err(|e| Error::Other(format!("Failed to parse branch URL: {}", e)))?;

    // If we have a subpath, append it to the URL path
    if !subpath.as_os_str().is_empty() && subpath != Path::new(".") {
        let mut url = url;
        let current_path = url.path();
        let new_path = if current_path.ends_with('/') {
            format!("{}{}", current_path, subpath.display())
        } else if current_path.is_empty() || current_path == "/" {
            format!("/{}", subpath.display())
        } else {
            format!("{}/{}", current_path, subpath.display())
        };
        url.set_path(&new_path);
        log::debug!("Branch URL with subpath: {}", url);
        return Ok(url);
    }

    Ok(url)
}

/// Update VCS fields in debian/control
fn update_vcs_fields(
    wt: &dyn PyWorkingTree,
    debian_path: &Path,
    vcs_url: &url::Url,
    vcs_type: &str,
) -> Result<(), Error> {
    use debian_analyzer::editor::{Editor, TreeEditor};
    use debian_control::lossless::Control;
    use std::ops::Deref;

    // Open the control file for editing
    let editor = TreeEditor::<Control>::new(wt, &debian_path.join("control"), false, false)?;

    // Get the control and modify it
    let control = editor.deref();
    let mut source = control
        .source()
        .ok_or_else(|| Error::Other("No source package found".to_string()))?;

    // Set the appropriate VCS field based on type
    match vcs_type {
        "git" => source.set_vcs_git(vcs_url.as_str()),
        "bzr" => source.set_vcs_bzr(vcs_url.as_str()),
        "svn" => source.set_vcs_svn(vcs_url.as_str()),
        "hg" => source.set_vcs_hg(vcs_url.as_str()),
        "cvs" => source.set_vcs_cvs(vcs_url.as_str()),
        "darcs" => source.set_vcs_darcs(vcs_url.as_str()),
        _ => {
            log::warn!("Unknown VCS type: {}, defaulting to Git", vcs_type);
            source.set_vcs_git(vcs_url.as_str())
        }
    }

    // Set browser URL if we can determine it
    if let Some(browser_url) = determine_browser_url(vcs_type, vcs_url) {
        source.set_vcs_browser(Some(&browser_url));
    }

    // Commit the changes
    editor.commit()?;
    Ok(())
}

/// Determine browser URL from VCS URL
fn determine_browser_url(vcs_type: &str, vcs_url: &url::Url) -> Option<String> {
    match vcs_type {
        "git" => {
            let url_str = vcs_url.as_str();
            // Handle common git hosting services
            if url_str.contains("github.com") {
                // Convert git URL to https browser URL
                Some(
                    url_str
                        .replace("git@github.com:", "https://github.com/")
                        .replace("git://github.com/", "https://github.com/")
                        .replace("git+ssh://git@github.com/", "https://github.com/")
                        .replace(".git", ""),
                )
            } else if url_str.contains("gitlab") {
                if url_str.starts_with("https://") {
                    // Already an HTTPS URL, just remove .git
                    Some(url_str.replace(".git", ""))
                } else {
                    // SSH URL, convert to HTTPS
                    Some(
                        url_str
                            .replace("git@", "https://")
                            .replace(":", "/")
                            .replace(".git", ""),
                    )
                }
            } else if url_str.contains("salsa.debian.org") {
                if url_str.starts_with("https://") {
                    // Already an HTTPS URL, just remove .git
                    Some(url_str.replace(".git", ""))
                } else {
                    // SSH or git protocol URL, convert to HTTPS
                    Some(
                        url_str
                            .replace("git@salsa.debian.org:", "https://salsa.debian.org/")
                            .replace("git://salsa.debian.org/", "https://salsa.debian.org/")
                            .replace(".git", ""),
                    )
                }
            } else {
                None
            }
        }
        _ => None,
    }
}

/// Find WNPP bugs for a package using the debian_analyzer functionality
async fn find_wnpp_bugs_for_package_async(
    source_name: &str,
    upstream_name: Option<&str>,
) -> Result<Vec<(BugId, BugKind)>, Error> {
    // Prepare the list of names to search for
    let mut names = vec![source_name];
    if let Some(upstream) = upstream_name {
        if upstream != source_name {
            names.push(upstream);
        }
    }

    // Convert &str to &str for the API
    let name_refs: Vec<&str> = names.to_vec();

    // Use the existing analyzer functionality
    match debian_analyzer::wnpp::find_wnpp_bugs_harder(&name_refs).await {
        Ok(bugs) => Ok(bugs),
        Err(e) => {
            log::warn!("Failed to query WNPP bugs: {}", e);
            Ok(vec![])
        }
    }
}

/// Find WNPP bugs for a package (synchronous wrapper)
fn find_wnpp_bugs_for_package(
    source_name: &str,
    upstream_name: Option<&str>,
) -> Result<Vec<(BugId, BugKind)>, Error> {
    // Create a Tokio runtime to run the async function
    let rt = tokio::runtime::Runtime::new()
        .map_err(|e| Error::Other(format!("Failed to create async runtime: {}", e)))?;

    rt.block_on(find_wnpp_bugs_for_package_async(source_name, upstream_name))
}

/// Enhanced project-wide dependency resolution with session support
/// This is a simplified version that focuses on integrating our session system
pub fn get_project_wide_deps_with_session(
    session: &dyn ognibuild::session::Session,
    _wt: &dyn PyWorkingTree,
    _subpath: &Path,
    buildsystem: &dyn ognibuild::buildsystem::BuildSystem,
) -> Result<
    (
        Vec<ognibuild::dependencies::debian::DebianDependency>,
        Vec<ognibuild::dependencies::debian::DebianDependency>,
    ),
    Error,
> {
    // The ognibuild session already handles the execution environment

    log::debug!("Setting up dependency resolution with ognibuild session");

    // Use the provided session for dependency resolution
    let (build_deps, test_deps) =
        ognibuild::debian::upstream_deps::get_project_wide_deps(session, buildsystem);

    log::debug!(
        "Found {} build dependencies and {} test dependencies",
        build_deps.len(),
        test_deps.len()
    );

    Ok((build_deps, test_deps))
}

/// Commit the debianization
fn commit_debianization(
    wt: &dyn PyWorkingTree,
    subpath: &Path,
    upstream_revid: &RevisionId,
    upstream_version: &str,
    mangled_version: &str,
) -> Result<HashMap<String, RevisionId>, Error> {
    let mut tag_names = HashMap::new();

    // Add all files in debian directory
    let debian_path = subpath.join("debian");
    let debian_path_ref = debian_path.as_path();

    // Use absolute path for smart_add to work around breezyshim issue where
    // smart_add resolves relative paths against cwd instead of tree root
    let debian_path_abs = wt.basedir().join(&debian_path);

    wt.smart_add(&[debian_path_abs.as_path()])?;

    // Commit the changes
    let message = format!("Import upstream version {}", upstream_version);
    let _reporter = NullCommitReporter::new();

    match wt.commit(&message, None, None, Some(false), Some(&[debian_path_ref])) {
        Ok(revid) => {
            let tag_name = format!("upstream/{}", mangled_version);
            tag_names.insert(tag_name, upstream_revid.clone());

            let debian_tag = format!("debian/{}-1", mangled_version);
            tag_names.insert(debian_tag, revid);
        }
        Err(e) if e.to_string().contains("PointlessCommit") => {
            // No changes to commit
        }
        Err(e) => return Err(Error::BrzError(e)),
    }

    Ok(tag_names)
}

/// Write initial changelog template for debianization
fn write_initial_changelog(
    wt: &dyn PyWorkingTree,
    debian_path: &Path,
    source_name: &str,
    version: &str,
    maintainer_name: &str,
    maintainer_email: &str,
    wnpp_bugs: &[(BugId, BugKind)],
) -> Result<(), Error> {
    let changelog_path = debian_path.join("changelog");

    let mut content = format!(
        "{} ({}-1) UNRELEASED; urgency=low\n\n",
        source_name, version
    );

    content.push_str("  * Initial release.");

    // Add WNPP bug references
    for (bug_id, bug_kind) in wnpp_bugs {
        match bug_kind {
            BugKind::ITP => content.push_str(&format!(" (Closes: #{})", bug_id)),
            BugKind::RFP => content.push_str(&format!(" (Closes: #{})", bug_id)),
        }
    }

    content.push_str("\n\n");

    // Add maintainer signature
    let timestamp = chrono::Local::now().format("%a, %d %b %Y %H:%M:%S %z");
    content.push_str(&format!(
        " -- {} <{}>  {}\n",
        maintainer_name, maintainer_email, timestamp
    ));

    wt.put_file_bytes_non_atomic(&changelog_path, content.as_bytes())?;
    Ok(())
}

/// Basic upstream version import without pristine tar
fn basic_import_upstream_version(
    wt: &dyn PyWorkingTree,
    _subpath: &Path,
    upstream_branch: &dyn PyBranch,
    _source_name: &str,
    upstream_version: &str,
) -> Result<String, Error> {
    let upstream_branch_name = "upstream";

    // Create an upstream branch if it doesn't exist
    match wt.controldir().create_branch(Some(upstream_branch_name)) {
        Ok(branch) => {
            // Set the upstream branch to point to the same revision as the upstream branch
            let upstream_revid = upstream_branch.last_revision();
            branch.generate_revision_history(&upstream_revid)?;
            log::info!(
                "Created upstream branch pointing to revision {}",
                upstream_revid
            );
        }
        Err(BrzError::AlreadyBranch(..)) => {
            log::info!("Upstream branch already exists");
        }
        Err(e) => return Err(Error::BrzError(e)),
    }

    // Create upstream tag
    let upstream_tag = format!("upstream/{}", upstream_version);
    let upstream_revid = upstream_branch.last_revision();

    // Try to create the tag
    match wt.branch().tags() {
        Ok(tags) => match tags.set_tag(&upstream_tag, &upstream_revid) {
            Ok(_) => {
                log::info!("Created upstream tag: {}", upstream_tag);
            }
            Err(BrzError::TagAlreadyExists(..)) => {
                log::info!("Upstream tag {} already exists", upstream_tag);
            }
            Err(e) => {
                log::warn!("Failed to create upstream tag {}: {}", upstream_tag, e);
            }
        },
        Err(e) => {
            log::warn!("Failed to get tags: {}", e);
        }
    }

    Ok(upstream_branch_name.to_string())
}

/// Unsplit VCS URL into type and URL string
#[cfg(test)]
fn unsplit_vcs_url(_vcs_type: &str, url: &url::Url) -> String {
    // Format the VCS URL as it should appear in debian/control
    // This is just the URL for most VCS types
    url.to_string()
}