titor 0.2.0

A high-performance checkpointing library for time-travel through directory states
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
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
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
//! Main Titor implementation
//!
//! This module provides the core Titor struct which is the main entry point
//! for all checkpoint operations including creating, restoring, forking, and
//! navigating through time.
//!
//! ## Overview
//!
//! The `Titor` struct manages the lifecycle of checkpoints and provides methods
//! for interacting with the checkpoint system. It coordinates between several
//! subsystems:
//!
//! - **Storage Backend**: Manages content-addressable storage of file objects
//! - **Timeline**: Tracks relationships between checkpoints
//! - **File Tracker**: Scans directories and detects changes
//! - **Compression Engine**: Handles file compression/decompression
//! - **Verification System**: Ensures checkpoint integrity
//!
//! ## Thread Safety
//!
//! `Titor` uses internal locking to ensure thread-safe operations. Multiple
//! operations can be performed concurrently, though some operations (like
//! checkpoint creation) may serialize access to maintain consistency.
//!
//! ## Examples
//!
//! ### Basic Usage
//!
//! ```rust,no_run
//! use titor::Titor;
//! use std::path::PathBuf;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Initialize a new Titor instance
//! let mut titor = Titor::init(
//!     PathBuf::from("./my_project"),
//!     PathBuf::from("./.titor")
//! )?;
//!
//! // Create checkpoints
//! let cp1 = titor.checkpoint(Some("Initial commit".to_string()))?;
//! let cp2 = titor.checkpoint(Some("Added features".to_string()))?;
//!
//! // Restore to previous state
//! titor.restore(&cp1.id)?;
//! # Ok(())
//! # }
//! ```

use crate::checkpoint::{Checkpoint, CheckpointMetadataBuilder};
use crate::compression::{CompressionEngine, CompressionStrategy};
use crate::error::{Result, TitorError};
use crate::file_tracking::{FileTracker, create_manifest, create_file_map};
use crate::merkle::{MerkleTree, FileEntryHashBuilder};
use crate::storage::Storage;
use crate::timeline::Timeline;
use crate::types::*;
use crate::utils;
use crate::verification::{CheckpointVerifier, TimelineVerificationReport, VerificationReport};
use parking_lot::{Mutex, RwLock};
use rayon::prelude::*;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Instant;
use tracing::{debug, info, instrument, trace, warn};
use serde_json;

/// Main Titor struct for checkpoint operations
///
/// `Titor` is the primary interface for interacting with the checkpoint system.
/// It manages the storage backend, timeline tracking, and file operations.
///
/// # Examples
///
/// ```rust,no_run
/// use titor::{Titor, TitorBuilder};
/// use std::path::PathBuf;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // Using direct initialization
/// let mut titor = Titor::init(
///     PathBuf::from("./project"),
///     PathBuf::from("./.titor")
/// )?;
///
/// // Using builder pattern for custom configuration
/// let mut titor = TitorBuilder::new()
///     .ignore_patterns(vec!["*.tmp".to_string()])
///     .build(
///         PathBuf::from("./project"),
///         PathBuf::from("./.titor")
///     )?;
/// # Ok(())
/// # }
/// ```
pub struct Titor {
    /// Root directory being tracked
    root_path: PathBuf,
    /// Storage backend
    storage: Arc<Storage>,
    /// Timeline structure
    timeline: Arc<RwLock<Timeline>>,
    /// Configuration
    config: TitorConfig,
    /// Auto-checkpoint strategy
    auto_checkpoint_strategy: Arc<Mutex<AutoCheckpointStrategy>>,
    /// Checkpoint hooks
    hooks: Arc<Mutex<Vec<Box<dyn CheckpointHook>>>>,
    /// File tracker
    file_tracker: FileTracker,
}

impl std::fmt::Debug for Titor {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Titor")
            .field("root_path", &self.root_path)
            .field("storage", &self.storage)
            .field("timeline", &self.timeline)
            .field("config", &self.config)
            .field("auto_checkpoint_strategy", &self.auto_checkpoint_strategy)
            .field("hooks", &format!("<{} hooks>", self.hooks.lock().len()))
            .field("file_tracker", &self.file_tracker)
            .finish()
    }
}

impl Titor {
    /// Initialize Titor for a directory
    ///
    /// Creates a new Titor instance and initializes the storage backend. If the
    /// storage directory already exists with a different configuration, this will
    /// fail. Use [`Titor::open`] to open existing storage.
    ///
    /// # Arguments
    ///
    /// * `root_path` - The directory to track. Must exist and be readable.
    /// * `storage_path` - Where to store checkpoint data. Will be created if it doesn't exist.
    ///
    /// # Returns
    ///
    /// Returns a new `Titor` instance on success.
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - The root path does not exist
    /// - The storage path cannot be created
    /// - Storage initialization fails
    /// - Insufficient permissions
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use titor::Titor;
    /// use std::path::PathBuf;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut titor = Titor::init(
    ///     PathBuf::from("/home/user/project"),
    ///     PathBuf::from("/home/user/.titor")
    /// )?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Panics
    ///
    /// This function should not panic under normal circumstances.
    #[instrument(skip(storage_path))]
    pub fn init(root_path: PathBuf, storage_path: PathBuf) -> Result<Self> {
        info!("Initializing Titor for {:?}", root_path);
        
        // Ensure root path exists
        if !root_path.exists() {
            return Err(TitorError::internal(format!(
                "Root path {:?} does not exist",
                root_path
            )));
        }
        
        // Create configuration
        let config = TitorConfig {
            root_path: root_path.clone(),
            storage_path: storage_path.clone(),
            max_file_size: 0,
            parallel_workers: num_cpus::get(),
            ignore_patterns: vec![],
            compression_strategy: "fast".to_string(),
            follow_symlinks: false,
            version: env!("CARGO_PKG_VERSION").to_string(),
        };
        
        // Initialize storage
        let compression = CompressionEngine::new(CompressionStrategy::Fast);
        let storage = Storage::init_or_open(storage_path, config.clone(), compression)?;
        
        // Create file tracker
        let file_tracker = FileTracker::new(root_path.clone());
        
        // Ensure .titor is in .gitignore
        let gitignore_path = root_path.join(".gitignore");
        if let Err(e) = utils::ensure_gitignore_has_entry(&gitignore_path, ".titor") {
            warn!("Failed to update .gitignore: {}", e);
            // Continue anyway - this is not a critical error
        } else {
            debug!("Ensured .titor is in .gitignore");
        }
        
        Ok(Self {
            root_path,
            storage: Arc::new(storage),
            timeline: Arc::new(RwLock::new(Timeline::new())),
            config,
            auto_checkpoint_strategy: Arc::new(Mutex::new(AutoCheckpointStrategy::Disabled)),
            hooks: Arc::new(Mutex::new(Vec::new())),
            file_tracker,
        })
    }
    
    /// Open existing Titor storage
    ///
    /// Opens an existing Titor storage directory and loads the configuration
    /// and timeline. The root path does not need to match the original initialization
    /// path, allowing for relocated directories.
    ///
    /// # Arguments
    ///
    /// * `root_path` - The current location of the tracked directory
    /// * `storage_path` - Path to existing Titor storage
    ///
    /// # Returns
    ///
    /// Returns a `Titor` instance connected to the existing storage.
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - The storage path does not exist or is not a valid Titor storage
    /// - The storage is corrupted or incompatible
    /// - Configuration cannot be loaded
    /// - Timeline data is corrupted
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use titor::Titor;
    /// use std::path::PathBuf;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// // Open existing storage
    /// let mut titor = Titor::open(
    ///     PathBuf::from("./relocated_project"),
    ///     PathBuf::from("./.titor")
    /// )?;
    /// # Ok(())
    /// # }
    /// ```
    #[instrument(skip(storage_path))]
    pub fn open(root_path: PathBuf, storage_path: PathBuf) -> Result<Self> {
        info!("Opening Titor storage at {:?}", storage_path);
        
        // Open storage
        let compression = CompressionEngine::new(CompressionStrategy::Fast);
        let storage = Storage::open(storage_path, compression)?;
        
        // Load configuration
        let config = {
            let metadata = storage.metadata().read();
            metadata.config.clone()
        };
        
        // Create file tracker
        let file_tracker = FileTracker::new(root_path.clone())
            .with_ignore_patterns(config.ignore_patterns.clone())
            .with_max_file_size(config.max_file_size)
            .with_follow_symlinks(config.follow_symlinks)
            .with_parallel_workers(config.parallel_workers);
        
        // Load timeline
        let timeline = Self::load_timeline(&storage)?;
        
        // Ensure .titor is in .gitignore
        let gitignore_path = root_path.join(".gitignore");
        if let Err(e) = utils::ensure_gitignore_has_entry(&gitignore_path, ".titor") {
            warn!("Failed to update .gitignore: {}", e);
            // Continue anyway - this is not a critical error
        } else {
            debug!("Ensured .titor is in .gitignore");
        }
        
        Ok(Self {
            root_path,
            storage: Arc::new(storage),
            timeline: Arc::new(RwLock::new(timeline)),
            config,
            auto_checkpoint_strategy: Arc::new(Mutex::new(AutoCheckpointStrategy::Disabled)),
            hooks: Arc::new(Mutex::new(Vec::new())),
            file_tracker,
        })
    }
    
    /// Create a new checkpoint
    ///
    /// Scans the tracked directory and creates an immutable snapshot of its current
    /// state. The checkpoint includes all file contents, metadata, and a Merkle tree
    /// for verification.
    ///
    /// # Arguments
    ///
    /// * `description` - Optional human-readable description of the checkpoint
    ///
    /// # Returns
    ///
    /// Returns the newly created `Checkpoint` containing its ID and metadata.
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - Directory scanning fails (permissions, I/O errors)
    /// - File reading fails
    /// - Storage write operations fail
    /// - Compression fails
    /// - Maximum file size is exceeded (if configured)
    ///
    /// # Performance
    ///
    /// Checkpoint creation performance depends on:
    /// - Number and size of files
    /// - Compression strategy
    /// - Storage backend performance
    /// - Available parallelism
    ///
    /// Files are processed in parallel for optimal performance.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use titor::Titor;
    /// # use std::path::PathBuf;
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let mut titor = Titor::init(PathBuf::from("."), PathBuf::from(".titor"))?;
    /// // Create checkpoint with description
    /// let checkpoint = titor.checkpoint(Some("Added login feature".to_string()))?;
    /// println!("Created checkpoint: {}", checkpoint.id);
    ///
    /// // Create checkpoint without description
    /// let checkpoint = titor.checkpoint(None)?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Notes
    ///
    /// - Checkpoints are immutable once created
    /// - The current timeline position is updated to the new checkpoint
    /// - Only changed files are stored (deduplication)
    /// - Symlinks are preserved but their targets are not followed by default
    /// - Empty directories are preserved
    #[instrument(skip(self))]
    pub fn checkpoint(&mut self, description: Option<String>) -> Result<Checkpoint> {
        info!("Creating checkpoint: {:?}", description);
        let start = Instant::now();
        
        // Get current checkpoint for parent
        let parent_id = self.timeline.read().current_checkpoint_id.clone();
        
        // Scan files
        debug!("Scanning directory for changes");
        let mut file_entries = self.file_tracker.scan_directory(Some(|info: ProgressInfo| {
            trace!("Scanned: {:?}", info.current_item);
        }))?;
        
        // Store file contents in parallel using rayon
        debug!("Storing {} files", file_entries.len());
        let storage = Arc::clone(&self.storage);
        let root_path = self.root_path.clone();
        
        // Process files in parallel and collect results
        let processing_results: Vec<Result<(usize, u64)>> = file_entries
            .par_iter_mut()
            .enumerate()
            .map(|(idx, entry)| -> Result<(usize, u64)> {
                let file_path = root_path.join(&entry.path);
                let mut compressed_size = 0u64;
                
                if entry.is_directory {
                    // For directories, we don't store any content
                    // Just ensure it exists during scanning
                    if !file_path.exists() {
                        fs::create_dir_all(&file_path)?;
                    }
                    entry.size = 0;
                } else if entry.is_symlink {
                    // For symlinks, store the target path as content
                    if let Some(target) = &entry.symlink_target {
                        let content_str = target.to_string_lossy();
                        let content = content_str.as_bytes();
                        trace!("Storing symlink {:?} -> {:?}", entry.path, target);
                        let (_, comp_size) = storage.store_object(content, &entry.path)?;
                        // Size of symlink object is the target path length
                        entry.size = content.len() as u64;
                        compressed_size = comp_size;
                    }
                } else if file_path.exists() {
                    // Read and store file content
                    let content = fs::read(&file_path)?;
                    
                    // Only re-hash if size changed (indicates file was modified)
                    if content.len() as u64 != entry.size {
                        // File changed after initial scan, update hash
                        let actual_hash = utils::hash_data(&content);
                        entry.content_hash = actual_hash.clone();
                        let mut builder = FileEntryHashBuilder::new();
                        entry.combined_hash = builder.combined_hash(&entry.content_hash, &entry.metadata_hash);
                        entry.size = content.len() as u64;
                    }
                    
                    let (_, comp_size) = storage.store_object(&content, &entry.path)?;
                    compressed_size = comp_size;
                }
                
                Ok((idx, compressed_size))
            })
            .collect();
        
        // Check for errors and accumulate sizes
        let mut compressed_size = 0u64;
        for result in processing_results {
            let (_, comp_size) = result?;
            compressed_size += comp_size;
        }
        
        // Calculate total size
        let total_size: u64 = file_entries.iter().map(|e| e.size).sum();
        
        // Build Merkle tree using the finalised entries
        debug!("Building Merkle tree for {} files (post-storage)", file_entries.len());
        let merkle_tree = MerkleTree::from_entries(&file_entries)?;
        let merkle_root = merkle_tree.root_hash().unwrap_or_default();
        
        // Calculate change statistics
        let change_stats = if let Some(parent_id) = &parent_id {
            let parent_manifest = self.storage.load_manifest(parent_id)?;
            self.file_tracker.detect_changes(&parent_manifest)?
        } else {
            ChangeStats {
                files_added: file_entries.len(),
                bytes_added: file_entries.iter().map(|e| e.size).sum(),
                ..Default::default()
            }
        };
        
        // Call pre-checkpoint hooks
        for hook in self.hooks.lock().iter() {
            hook.pre_checkpoint(&change_stats)?;
        }
        
        // Create checkpoint metadata
        let metadata = CheckpointMetadataBuilder::new()
            .file_count(file_entries.len())
            .total_size(total_size)
            .compressed_size(compressed_size)
            .files_changed(change_stats.total_operations())
            .bytes_changed(change_stats.net_size_change() as u64)
            .build();
        
        // Create checkpoint
        let checkpoint = Checkpoint::new(
            parent_id,
            description,
            metadata,
            merkle_root.clone(),
        );
        
        // Store checkpoint and manifest
        self.storage.store_checkpoint(&checkpoint)?;
        let manifest = create_manifest(
            checkpoint.id.clone(),
            file_entries,
            merkle_root,
        );
        self.storage.store_manifest(&manifest)?;
        
        // Flush reference count updates
        self.storage.flush_ref_counts()?;
        
        // Update timeline
        {
            let mut timeline = self.timeline.write();
            timeline.add_checkpoint(checkpoint.clone())?;
            // Mark the newly created checkpoint as current HEAD.
            // This mirrors typical VCS behaviour (e.g., git) and aligns with user expectations
            // that creating a checkpoint advances the timeline.
            timeline.set_current(&checkpoint.id)?;
        }

        // Persist timeline state
        self.save_timeline()?;
        
        // Call post-checkpoint hooks
        for hook in self.hooks.lock().iter() {
            hook.post_checkpoint(&checkpoint)?;
        }
        
        let duration = start.elapsed();
        info!(
            "Created checkpoint {} in {:?} ({} files, {} bytes)",
            checkpoint.short_id(),
            duration,
            manifest.file_count,
            utils::format_bytes(total_size)
        );
        
        Ok(checkpoint)
    }
    
    /// Restore to a specific checkpoint
    ///
    /// Restores the tracked directory to match the exact state captured in the
    /// specified checkpoint. This operation will:
    /// - Delete files that didn't exist in the checkpoint
    /// - Restore files that existed in the checkpoint
    /// - Update file permissions to match the checkpoint
    /// - Preserve symlinks as they were
    ///
    /// # Arguments
    ///
    /// * `checkpoint_id` - The ID (or short ID prefix) of the checkpoint to restore
    ///
    /// # Returns
    ///
    /// Returns a `RestoreResult` containing statistics about the restore operation:
    /// - Number of files restored
    /// - Number of files deleted
    /// - Bytes written/deleted
    /// - Any warnings encountered
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - The checkpoint ID is invalid or not found
    /// - File operations fail (permissions, disk space)
    /// - The checkpoint data is corrupted
    ///
    /// # Safety
    ///
    /// This operation modifies the filesystem and cannot be undone except by
    /// restoring to another checkpoint. Ensure you have a recent checkpoint
    /// before restoring to an older state.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use titor::Titor;
    /// # use std::path::PathBuf;
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let mut titor = Titor::init(PathBuf::from("."), PathBuf::from(".titor"))?;
    /// // Restore to a specific checkpoint
    /// let result = titor.restore("abc123")?;
    /// println!("Restored {} files, deleted {} files", 
    ///          result.files_restored, result.files_deleted);
    ///
    /// // Check for warnings
    /// if !result.warnings.is_empty() {
    ///     eprintln!("Warnings during restore:");
    ///     for warning in &result.warnings {
    ///         eprintln!("  - {}", warning);
    ///     }
    /// }
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Notes
    ///
    /// - Files are restored with their original permissions but not ownership
    /// - Symlinks are restored as they were (relative symlinks remain relative)
    /// - The timeline position is updated to the restored checkpoint
    /// - Restore operations are not atomic - interruption may leave partial state
    #[instrument(skip(self))]
    pub fn restore(&mut self, checkpoint_id: &str) -> Result<RestoreResult> {
        info!("Restoring to checkpoint {}", &checkpoint_id[..8.min(checkpoint_id.len())]);
        let start = Instant::now();
        
        // Load checkpoint
        let checkpoint = self.storage.load_checkpoint(checkpoint_id)?;
        let manifest = self.storage.load_manifest(checkpoint_id)?;
        
        // Get current checkpoint for hooks
        let current_checkpoint = self.timeline.read()
            .current_checkpoint()
            .cloned();
        
        // Call pre-restore hooks
        if let Some(current) = &current_checkpoint {
            for hook in self.hooks.lock().iter() {
                hook.pre_restore(current, &checkpoint)?;
            }
        }
        
        // Track restore statistics
        let mut files_restored = 0;
        let mut files_deleted = 0;
        let mut bytes_written = 0u64;
        let mut bytes_deleted = 0u64;
        let mut warnings = Vec::new();
        
        // Create map of target files
        let target_files = create_file_map(&manifest.files);
        
        // Scan current directory to find files to delete
        let current_files = self.file_tracker.scan_directory::<fn(ProgressInfo)>(None)?;
        let mut directories_to_check = std::collections::HashSet::new();
        
        for current_file in &current_files {
            if !target_files.contains_key(current_file.path.as_path()) {
                // Entry should be deleted (file or empty directory)
                let file_path = self.root_path.join(&current_file.path);
                if file_path.exists() {
                    // Track parent directories for cleanup
                    if let Some(parent) = file_path.parent() {
                        let mut parent = parent.to_path_buf();
                        while parent != self.root_path && parent.starts_with(&self.root_path) {
                            directories_to_check.insert(parent.clone());
                            if let Some(p) = parent.parent() {
                                parent = p.to_path_buf();
                            } else {
                                break;
                            }
                        }
                    }
                    
                    if current_file.is_directory {
                        // Remove empty directory entries without warning
                        if let Err(e) = utils::remove_dir_if_empty(&file_path) {
                            trace!("Could not remove directory {:?}: {}", file_path, e);
                        }
                    } else {
                        // Remove regular file or symlink
                        match fs::remove_file(&file_path) {
                            Ok(_) => {
                                files_deleted += 1;
                                bytes_deleted += current_file.size;
                                trace!("Deleted file: {:?}", current_file.path);
                            }
                            Err(e) => {
                                warnings.push(format!(
                                    "Failed to delete {:?}: {}",
                                    current_file.path, e
                                ));
                            }
                        }
                    }
                }
            }
        }
        
        // Clean up empty directories
        let mut dirs_to_check: Vec<_> = directories_to_check.into_iter().collect();
        dirs_to_check.sort_by(|a, b| b.components().count().cmp(&a.components().count())); // Sort deepest first
        
        for dir in dirs_to_check {
            if dir.exists() && dir != self.root_path {
                if let Err(e) = utils::remove_dir_if_empty(&dir) {
                    trace!("Could not remove directory {:?}: {}", dir, e);
                }
            }
        }
        
        // Restore files from checkpoint
        debug!("Restoring {} files", manifest.files.len());
        for entry in &manifest.files {
            let file_path = self.root_path.join(&entry.path);
            
            // Ensure parent directory exists
            if let Some(parent) = file_path.parent() {
                fs::create_dir_all(parent)?;
            }
            
            if entry.is_directory {
                // Restore directory
                if !file_path.exists() {
                    fs::create_dir_all(&file_path)?;
                    // Set directory permissions
                    utils::set_permissions(&file_path, entry.permissions)?;
                    files_restored += 1;
                }
            } else if entry.is_symlink {
                // Restore symbolic link
                if let Some(target) = &entry.symlink_target {
                    // Remove existing file/link if present
                    if file_path.exists() || file_path.symlink_metadata().is_ok() {
                        // Use symlink_metadata to check if it's a symlink even if broken
                        trace!("Removing existing file/symlink at {:?}", file_path);
                        fs::remove_file(&file_path).ok();
                    }
                    
                    // Ensure the target is relative to the symlink location for relative symlinks
                    // This handles cases where the working directory during checkpoint creation
                    // differs from restoration
                    let final_target = if target.is_relative() {
                        target.clone()
                    } else {
                        // For absolute paths, we keep them as-is but warn if they don't exist
                        if !target.exists() {
                            warnings.push(format!(
                                "Symlink target {:?} is absolute and does not exist",
                                target
                            ));
                        }
                        target.clone()
                    };
                    
                    trace!("Creating symlink {:?} -> {:?}", file_path, final_target);
                    match utils::create_symlink(&final_target, &file_path) {
                        Ok(_) => {
                            // Symlink successfully restored
                            files_restored += 1;
                            trace!("Successfully created symlink");
                        }
                        Err(e) => {
                            warnings.push(format!(
                                "Failed to create symlink {:?} -> {:?}: {}",
                                entry.path, final_target, e
                            ));
                        }
                    }
                }
            } else {
                // Restore regular file
                match self.storage.load_object(&entry.content_hash) {
                    Ok(content) => {
                        // Write file
                        fs::write(&file_path, &content)?;
                        
                        // Set permissions
                        utils::set_permissions(&file_path, entry.permissions)?;
                        
                        files_restored += 1;
                        bytes_written += content.len() as u64;
                    }
                    Err(e) => {
                        warnings.push(format!(
                            "Failed to restore {:?}: {}",
                            entry.path, e
                        ));
                    }
                }
            }
        }
        
        // Update current checkpoint
        self.timeline.write().set_current(checkpoint_id)?;
        self.save_timeline()?;
        
        // Create result
        let result = RestoreResult {
            checkpoint_id: checkpoint_id.to_string(),
            files_restored,
            files_deleted,
            bytes_written,
            bytes_deleted,
            duration_ms: start.elapsed().as_millis() as u64,
            warnings,
        };
        
        // Call post-restore hooks
        for hook in self.hooks.lock().iter() {
            hook.post_restore(&result)?;
        }
        
        info!(
            "Restored to checkpoint {} in {}ms ({} files restored, {} deleted)",
            &checkpoint_id[..8.min(checkpoint_id.len())],
            result.duration_ms,
            result.files_restored,
            result.files_deleted
        );
        
        Ok(result)
    }
    
    /// List all checkpoints
    ///
    /// Returns all checkpoints in the timeline, ordered by creation time (oldest first).
    /// This includes checkpoints from all branches if the timeline has been forked.
    ///
    /// # Returns
    ///
    /// A vector of all `Checkpoint` objects in the storage.
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - Timeline data cannot be accessed
    /// - Checkpoint data is corrupted
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use titor::Titor;
    /// # use std::path::PathBuf;
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let titor = Titor::init(PathBuf::from("."), PathBuf::from(".titor"))?;
    /// let checkpoints = titor.list_checkpoints()?;
    /// for cp in checkpoints {
    ///     println!("{}: {}", 
    ///              cp.timestamp.format("%Y-%m-%d %H:%M:%S"),
    ///              cp.description.as_ref().unwrap_or(&"No description".to_string()));
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub fn list_checkpoints(&self) -> Result<Vec<Checkpoint>> {
        let timeline = self.timeline.read();
        let mut checkpoints: Vec<_> = timeline.checkpoints.values().cloned().collect();
        checkpoints.sort_by(|a, b| a.timestamp.cmp(&b.timestamp));
        Ok(checkpoints)
    }
    
    /// Get the timeline tree structure
    pub fn get_timeline(&self) -> Result<Timeline> {
        Ok(self.timeline.read().clone())
    }
    
    /// Fork from a checkpoint
    ///
    /// Creates a new checkpoint that branches from an existing checkpoint, allowing
    /// for alternate timelines. This is useful for experimenting with changes without
    /// affecting the main timeline.
    ///
    /// # Arguments
    ///
    /// * `checkpoint_id` - The ID of the checkpoint to fork from
    /// * `description` - Optional description for the forked checkpoint
    ///
    /// # Returns
    ///
    /// Returns the newly created fork checkpoint.
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - The checkpoint ID is invalid or not found
    /// - Checkpoint creation fails
    /// - Storage operations fail
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use titor::Titor;
    /// # use std::path::PathBuf;
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let mut titor = Titor::init(PathBuf::from("."), PathBuf::from(".titor"))?;
    /// // Fork from an existing checkpoint
    /// let fork = titor.fork("main-branch-cp-id", 
    ///                        Some("Experimental feature branch".to_string()))?;
    /// 
    /// // The fork becomes the current checkpoint
    /// // Make changes and create more checkpoints on this branch
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Notes
    ///
    /// - The fork operation doesn't modify files, it only creates a new checkpoint
    /// - The forked checkpoint becomes the current checkpoint
    /// - Multiple forks from the same checkpoint create sibling branches
    #[instrument(skip(self))]
    pub fn fork(&mut self, checkpoint_id: &str, description: Option<String>) -> Result<Checkpoint> {
        info!("Forking from checkpoint {}", &checkpoint_id[..8.min(checkpoint_id.len())]);
        
        // First restore to the checkpoint
        self.restore(checkpoint_id)?;
        
        // Then create a new checkpoint with the fork description
        let fork_description = description.or_else(|| {
            Some(format!("Fork from {}", &checkpoint_id[..8.min(checkpoint_id.len())]))
        });
        
        self.checkpoint(fork_description)
    }
    
    /// Compare two checkpoints (file-level differences)
    ///
    /// Computes the differences between two checkpoints, showing which files
    /// were added, modified, or deleted. This comparison is based on content
    /// hashes rather than timestamps.
    ///
    /// # Arguments
    ///
    /// * `from_id` - Source checkpoint ID (older)
    /// * `to_id` - Target checkpoint ID (newer)
    ///
    /// # Returns
    ///
    /// Returns a [`CheckpointDiff`] containing:
    /// - Lists of added, modified, and deleted files
    /// - Change statistics (file counts and sizes)
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - Either checkpoint ID is not found
    /// - Checkpoint data cannot be loaded
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use titor::Titor;
    /// # use std::path::PathBuf;
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let titor = Titor::init(PathBuf::from("."), PathBuf::from(".titor"))?;
    /// let diff = titor.diff("checkpoint1", "checkpoint2")?;
    /// 
    /// println!("Files added: {}", diff.added_files.len());
    /// println!("Files modified: {}", diff.modified_files.len());
    /// println!("Files deleted: {}", diff.deleted_files.len());
    /// 
    /// // Show detailed changes
    /// for file in &diff.added_files {
    ///     println!("Added: {:?}", file.path);
    /// }
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Notes
    ///
    /// - Comparison is based on content hashes, not timestamps
    /// - Directory structure changes are tracked
    /// - The order of checkpoints matters for add/delete determination
    pub fn diff(&self, from_id: &str, to_id: &str) -> Result<CheckpointDiff> {
        debug!("Computing diff between {} and {}", 
               &from_id[..8.min(from_id.len())],
               &to_id[..8.min(to_id.len())]);
        
        // Load manifests
        let from_manifest = self.storage.load_manifest(from_id)?;
        let to_manifest = self.storage.load_manifest(to_id)?;
        
        // Create maps for efficient lookup
        let from_map = create_file_map(&from_manifest.files);
        let to_map = create_file_map(&to_manifest.files);
        
        let mut added_files = Vec::new();
        let mut modified_files = Vec::new();
        let mut deleted_files = Vec::new();
        let mut stats = ChangeStats::default();
        
        // Find added and modified files
        for (path, to_entry) in &to_map {
            match from_map.get(path) {
                Some(from_entry) => {
                    if to_entry.content_hash != from_entry.content_hash {
                        modified_files.push(((*from_entry).clone(), (*to_entry).clone()));
                        stats.files_modified += 1;
                        stats.bytes_modified += to_entry.size;
                        stats.changed_files.push((*path).to_path_buf());
                    }
                }
                None => {
                    added_files.push((*to_entry).clone());
                    stats.files_added += 1;
                    stats.bytes_added += to_entry.size;
                    stats.changed_files.push((*path).to_path_buf());
                }
            }
        }
        
        // Find deleted files
        for (path, from_entry) in &from_map {
            if !to_map.contains_key(path) {
                deleted_files.push((*from_entry).clone());
                stats.files_deleted += 1;
                stats.bytes_deleted += from_entry.size;
                stats.changed_files.push((*path).to_path_buf());
            }
        }
        
        Ok(CheckpointDiff {
            from_id: from_id.to_string(),
            to_id: to_id.to_string(),
            added_files,
            modified_files,
            deleted_files,
            stats,
        })
    }
    
    /// Compute line-level diff for a specific file between checkpoints
    ///
    /// This method provides detailed line-by-line differences for a text file
    /// between two checkpoints, similar to git diff output.
    ///
    /// # Arguments
    ///
    /// * `from_id` - Source checkpoint ID
    /// * `to_id` - Target checkpoint ID  
    /// * `file_path` - Path to the file to diff
    /// * `options` - Options controlling diff generation
    ///
    /// # Returns
    ///
    /// Returns a [`FileDiff`] containing:
    /// - Diff hunks with line-level changes
    /// - Line addition/deletion counts
    /// - Binary file detection
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - Either checkpoint doesn't exist
    /// - The file doesn't exist in one or both checkpoints
    /// - The file content cannot be loaded
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use titor::{Titor, types::DiffOptions};
    /// # use std::path::{Path, PathBuf};
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let titor = Titor::init(PathBuf::from("."), PathBuf::from(".titor"))?;
    /// let options = DiffOptions::default();
    /// let file_diff = titor.diff_file("checkpoint1", "checkpoint2", 
    ///                                  Path::new("src/main.rs"), options)?;
    /// 
    /// println!("Lines added: {}", file_diff.lines_added);
    /// println!("Lines deleted: {}", file_diff.lines_deleted);
    /// 
    /// for hunk in &file_diff.hunks {
    ///     println!("@@ -{},{} +{},{} @@", 
    ///              hunk.from_line, hunk.from_count,
    ///              hunk.to_line, hunk.to_count);
    ///     // Print the actual line changes...
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub fn diff_file(
        &self,
        from_id: &str,
        to_id: &str,
        file_path: &Path,
        options: crate::types::DiffOptions,
    ) -> Result<crate::types::FileDiff> {
        use crate::diff;
        
        debug!("Computing file diff for {:?} between {} and {}", 
               file_path,
               &from_id[..8.min(from_id.len())],
               &to_id[..8.min(to_id.len())]);
        
        // Load manifests
        let from_manifest = self.storage.load_manifest(from_id)?;
        let to_manifest = self.storage.load_manifest(to_id)?;
        
        // Find the file in both manifests
        let from_entry = from_manifest.files.iter()
            .find(|e| e.path == file_path)
            .ok_or_else(|| TitorError::internal(
                format!("File {:?} not found in checkpoint {}", file_path, from_id)
            ))?;
            
        let to_entry = to_manifest.files.iter()
            .find(|e| e.path == file_path)
            .ok_or_else(|| TitorError::internal(
                format!("File {:?} not found in checkpoint {}", file_path, to_id)
            ))?;
        
        // Check if the file actually changed
        if from_entry.content_hash == to_entry.content_hash {
            // No changes - return empty diff
            return Ok(crate::types::FileDiff {
                path: file_path.to_path_buf(),
                from_hash: from_entry.content_hash.clone(),
                to_hash: to_entry.content_hash.clone(),
                is_binary: false,
                hunks: vec![],
                lines_added: 0,
                lines_deleted: 0,
            });
        }
        
        // Check file size limits
        if from_entry.size > options.max_file_size || to_entry.size > options.max_file_size {
            return Err(TitorError::internal(
                format!("File {:?} exceeds maximum size for diff ({} bytes)", 
                        file_path, options.max_file_size)
            ));
        }
        
        // Load file contents
        let from_content = self.storage.load_object(&from_entry.content_hash)?;
        let to_content = self.storage.load_object(&to_entry.content_hash)?;
        
        // Create the file diff
        diff::create_file_diff(
            file_path,
            &from_entry.content_hash,
            &to_entry.content_hash,
            &from_content,
            &to_content,
            &options,
        )
    }
    
    /// Compare two checkpoints with detailed line-level differences
    ///
    /// This method extends the basic diff functionality by computing line-level
    /// differences for all modified text files, providing git-like diff output.
    ///
    /// # Arguments
    ///
    /// * `from_id` - Source checkpoint ID
    /// * `to_id` - Target checkpoint ID
    /// * `options` - Options controlling diff generation
    ///
    /// # Returns
    ///
    /// Returns a [`DetailedCheckpointDiff`] containing:
    /// - Basic file-level diff information
    /// - Line-level diffs for each modified text file
    /// - Total line addition/deletion counts
    ///
    /// # Notes
    ///
    /// - Binary files are detected and marked but not diffed
    /// - Large files may be skipped based on `options.max_file_size`
    /// - This operation may be memory intensive for large changesets
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use titor::{Titor, types::DiffOptions};
    /// # use std::path::PathBuf;
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let titor = Titor::init(PathBuf::from("."), PathBuf::from(".titor"))?;
    /// let options = DiffOptions {
    ///     context_lines: 3,
    ///     ignore_whitespace: false,
    ///     show_line_numbers: true,
    ///     max_file_size: 10 * 1024 * 1024, // 10MB
    /// };
    /// 
    /// let detailed_diff = titor.diff_detailed("checkpoint1", "checkpoint2", options)?;
    /// 
    /// println!("Total lines added: {}", detailed_diff.total_lines_added);
    /// println!("Total lines deleted: {}", detailed_diff.total_lines_deleted);
    /// 
    /// for file_diff in &detailed_diff.file_diffs {
    ///     if file_diff.is_binary {
    ///         println!("Binary file: {:?}", file_diff.path);
    ///     } else {
    ///         println!("Modified: {:?} (+{} -{})", 
    ///                  file_diff.path, 
    ///                  file_diff.lines_added,
    ///                  file_diff.lines_deleted);
    ///     }
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub fn diff_detailed(
        &self,
        from_id: &str,
        to_id: &str,
        options: crate::types::DiffOptions,
    ) -> Result<crate::types::DetailedCheckpointDiff> {
        use crate::diff;
        
        debug!("Computing detailed diff between {} and {}", 
               &from_id[..8.min(from_id.len())],
               &to_id[..8.min(to_id.len())]);
        
        // First get the basic diff
        let basic_diff = self.diff(from_id, to_id)?;
        
        // Now compute line-level diffs for modified files
        let mut file_diffs = Vec::new();
        let mut total_lines_added = 0;
        let mut total_lines_deleted = 0;
        
        for (from_entry, to_entry) in &basic_diff.modified_files {
            // Skip files that are too large
            if from_entry.size > options.max_file_size || to_entry.size > options.max_file_size {
                debug!("Skipping large file {:?} for line diff", from_entry.path);
                continue;
            }
            
            // Load file contents
            match (
                self.storage.load_object(&from_entry.content_hash),
                self.storage.load_object(&to_entry.content_hash)
            ) {
                (Ok(from_content), Ok(to_content)) => {
                    match diff::create_file_diff(
                        &from_entry.path,
                        &from_entry.content_hash,
                        &to_entry.content_hash,
                        &from_content,
                        &to_content,
                        &options,
                    ) {
                        Ok(file_diff) => {
                            total_lines_added += file_diff.lines_added;
                            total_lines_deleted += file_diff.lines_deleted;
                            file_diffs.push(file_diff);
                        }
                        Err(e) => {
                            warn!("Failed to compute diff for {:?}: {}", from_entry.path, e);
                        }
                    }
                }
                (Err(e), _) | (_, Err(e)) => {
                    warn!("Failed to load content for {:?}: {}", from_entry.path, e);
                }
            }
        }
        
        Ok(crate::types::DetailedCheckpointDiff {
            basic_diff,
            file_diffs,
            total_lines_added,
            total_lines_deleted,
        })
    }
    
    /// Configure auto-checkpoint behavior
    pub fn set_auto_checkpoint(&mut self, strategy: AutoCheckpointStrategy) {
        *self.auto_checkpoint_strategy.lock() = strategy;
    }
    
    /// Garbage collect unreferenced content
    ///
    /// Removes objects from storage that are no longer referenced by any checkpoint.
    /// This helps reclaim disk space after checkpoints have been deleted or when
    /// content has been deduplicated.
    ///
    /// # Returns
    ///
    /// Returns `GcStats` containing:
    /// - Number of objects examined and deleted
    /// - Bytes reclaimed
    /// - List of deleted object hashes
    /// - Operation duration
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - Storage access fails
    /// - Object deletion fails (partial cleanup may occur)
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use titor::Titor;
    /// # use std::path::PathBuf;
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let titor = Titor::init(PathBuf::from("."), PathBuf::from(".titor"))?;
    /// // Run garbage collection
    /// let stats = titor.gc()?;
    /// println!("Reclaimed {} bytes by deleting {} objects",
    ///          stats.bytes_reclaimed, stats.objects_deleted);
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Notes
    ///
    /// - This operation is irreversible - deleted objects cannot be recovered
    /// - Only unreferenced objects are deleted
    /// - Consider using `gc_analyze()` first to preview what would be deleted
    /// - Garbage collection may take significant time for large repositories
    #[instrument(skip(self))]
    pub fn gc(&self) -> Result<GcStats> {
        info!("Starting garbage collection");
        let start = Instant::now();
        
        let mut stats = GcStats::default();
        
        // Find unreferenced objects
        let unreferenced = self.storage.get_unreferenced_objects()?;
        stats.unreferenced_objects = unreferenced.clone();
        stats.objects_examined = self.storage.list_all_objects()?.len();
        
        // Delete unreferenced objects and track sizes
        for hash in &unreferenced {
            // Try to get object size before deletion
            match self.storage.get_object_size(hash) {
                Ok(size) => {
                    // Now delete the object
                    match self.storage.delete_object(hash) {
                        Ok(_) => {
                            stats.objects_deleted += 1;
                            stats.bytes_reclaimed += size;
                        }
                        Err(e) => {
                            warn!("Failed to delete object {}: {}", &hash[..8], e);
                        }
                    }
                }
                Err(e) => {
                    // Object might have been deleted already or is corrupted
                    warn!("Failed to get size for object {}: {}", &hash[..8], e);
                    // Try to delete anyway
                    if self.storage.delete_object(hash).is_ok() {
                        stats.objects_deleted += 1;
                    }
                }
            }
        }
        
        stats.duration_ms = start.elapsed().as_millis() as u64;
        
        info!(
            "Garbage collection complete in {}ms: {} objects deleted, {} bytes reclaimed",
            stats.duration_ms,
            stats.objects_deleted,
            stats.bytes_reclaimed
        );
        
        Ok(stats)
    }
    
    /// Analyze garbage collection without actually deleting anything (dry run)
    #[instrument(skip(self))]
    pub fn gc_analyze(&self) -> Result<GcStats> {
        info!("Analyzing garbage collection (dry run)");
        let start = Instant::now();
        
        let mut stats = GcStats::default();
        
        // Find unreferenced objects
        let unreferenced = self.storage.get_unreferenced_objects()?;
        stats.unreferenced_objects = unreferenced.clone();
        stats.objects_examined = self.storage.list_all_objects()?.len();
        
        // Calculate sizes without deleting
        for hash in &unreferenced {
            match self.storage.get_object_size(hash) {
                Ok(size) => {
                    stats.bytes_reclaimed += size;
                }
                Err(e) => {
                    warn!("Failed to get size for object {}: {}", &hash[..8], e);
                }
            }
        }
        
        stats.duration_ms = start.elapsed().as_millis() as u64;
        
        info!(
            "Garbage collection analysis complete in {}ms: {} objects would be deleted, {} bytes would be reclaimed",
            stats.duration_ms,
            unreferenced.len(),
            stats.bytes_reclaimed
        );
        
        Ok(stats)
    }
    
    /// Verify integrity of a specific checkpoint
    pub fn verify_checkpoint(&self, checkpoint_id: &str) -> Result<VerificationReport> {
        let checkpoint = self.storage.load_checkpoint(checkpoint_id)?;
        let verifier = CheckpointVerifier::new(&self.storage);
        verifier.verify_complete(&checkpoint)
    }
    
    /// Verify entire timeline integrity
    pub fn verify_timeline(&self) -> Result<TimelineVerificationReport> {
        let timeline = self.timeline.read();
        let verifier = CheckpointVerifier::new(&self.storage);
        verifier.verify_timeline(&timeline)
    }
    
    /// Compute merkle root for current state
    pub fn compute_current_merkle_root(&self) -> Result<String> {
        let entries = self.file_tracker.scan_directory::<fn(ProgressInfo)>(None)?;
        let tree = MerkleTree::from_entries(&entries)?;
        Ok(tree.root_hash().unwrap_or_default())
    }
    
    /// Add a checkpoint hook
    pub fn add_hook(&mut self, hook: Box<dyn CheckpointHook>) {
        self.hooks.lock().push(hook);
    }
    
    /// Load timeline from storage
    fn load_timeline(storage: &Storage) -> Result<Timeline> {
        let mut timeline = Timeline::new();
        
        // Load all checkpoints
        for checkpoint_id in storage.list_checkpoints()? {
            let checkpoint = storage.load_checkpoint(&checkpoint_id)?;
            timeline.add_checkpoint(checkpoint)?;
        }
        
        // Load current checkpoint from persistent storage
        let timeline_path = storage.root().join("timeline.json");
        if timeline_path.exists() {
            let timeline_data = fs::read_to_string(&timeline_path)?;
            if let Ok(timeline_state) = serde_json::from_str::<TimelineState>(&timeline_data) {
                timeline.current_checkpoint_id = timeline_state.current_checkpoint_id;
                debug!("Loaded current checkpoint: {:?}", timeline.current_checkpoint_id);
            }
        }
        
        Ok(timeline)
    }
    
    /// Save timeline to storage
    fn save_timeline(&self) -> Result<()> {
        // Save current checkpoint state
        let timeline_state = TimelineState {
            current_checkpoint_id: self.timeline.read().current_checkpoint_id.clone(),
            version: 1,
        };
        
        let timeline_path = self.storage.root().join("timeline.json");
        let timeline_json = serde_json::to_string_pretty(&timeline_state)?;
        utils::atomic_write(&timeline_path, timeline_json.as_bytes())?;
        
        debug!("Saved timeline state with current checkpoint: {:?}", timeline_state.current_checkpoint_id);
        Ok(())
    }
}

/// Builder pattern for Titor configuration
///
/// `TitorBuilder` provides a fluent interface for configuring Titor instances
/// with custom settings. This is the recommended way to create Titor instances
/// when you need non-default configuration.
///
/// # Examples
///
/// ```rust,no_run
/// use titor::{TitorBuilder, CompressionStrategy};
/// use std::path::PathBuf;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let titor = TitorBuilder::new()
///     .compression_strategy(CompressionStrategy::Fast)
///     .ignore_patterns(vec![
///         "*.log".to_string(),
///         "temp/**".to_string(),
///         ".git/**".to_string()
///     ])
///     .max_file_size(50 * 1024 * 1024) // 50MB
///     .parallel_workers(4)
///     .follow_symlinks(false)
///     .build(
///         PathBuf::from("./my_project"),
///         PathBuf::from("./.titor")
///     )?;
/// # Ok(())
/// # }
/// ```
///
/// # Default Values
///
/// - `compression_strategy`: `CompressionStrategy::Fast`
/// - `ignore_patterns`: Empty (but `.titor` is always ignored)
/// - `max_file_size`: 0 (no limit)
/// - `parallel_workers`: Number of CPU cores
/// - `follow_symlinks`: false
#[derive(Debug)]
pub struct TitorBuilder {
    compression_strategy: CompressionStrategy,
    ignore_patterns: Vec<String>,
    max_file_size: u64,
    parallel_workers: usize,
    follow_symlinks: bool,
}

impl TitorBuilder {
    /// Create a new builder with default settings
    ///
    /// # Examples
    ///
    /// ```rust
    /// use titor::TitorBuilder;
    ///
    /// let builder = TitorBuilder::new();
    /// ```
    pub fn new() -> Self {
        Self {
            compression_strategy: CompressionStrategy::default(),
            ignore_patterns: Vec::new(),
            max_file_size: 0,
            parallel_workers: num_cpus::get(),
            follow_symlinks: false,
        }
    }
    
    /// Set compression strategy
    ///
    /// Determines how aggressively files are compressed. Higher compression
    /// levels save storage space but increase CPU usage.
    ///
    /// # Arguments
    ///
    /// * `strategy` - The compression strategy to use
    ///
    /// # Examples
    ///
    /// ```rust
    /// use titor::{TitorBuilder, CompressionStrategy};
    ///
    /// let builder = TitorBuilder::new()
    ///     .compression_strategy(CompressionStrategy::Fast);
    /// ```
    pub fn compression_strategy(mut self, strategy: CompressionStrategy) -> Self {
        self.compression_strategy = strategy;
        self
    }
    
    /// Set ignore patterns
    ///
    /// Specifies glob patterns for files and directories to exclude from
    /// checkpoints. Patterns follow gitignore-style syntax.
    ///
    /// # Arguments
    ///
    /// * `patterns` - Vector of glob patterns to ignore
    ///
    /// # Examples
    ///
    /// ```rust
    /// use titor::TitorBuilder;
    ///
    /// let builder = TitorBuilder::new()
    ///     .ignore_patterns(vec![
    ///         "*.log".to_string(),
    ///         "node_modules/**".to_string(),
    ///         "target/**".to_string(),
    ///     ]);
    /// ```
    ///
    /// # Notes
    ///
    /// - The storage directory (`.titor`) is always ignored automatically
    /// - Patterns are relative to the root directory being tracked
    pub fn ignore_patterns(mut self, patterns: Vec<String>) -> Self {
        self.ignore_patterns = patterns;
        self
    }
    
    /// Set maximum file size
    ///
    /// Files larger than this size will be skipped during checkpoint creation.
    /// Use 0 for no limit.
    ///
    /// # Arguments
    ///
    /// * `size` - Maximum file size in bytes (0 = no limit)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use titor::TitorBuilder;
    ///
    /// let builder = TitorBuilder::new()
    ///     .max_file_size(100 * 1024 * 1024); // Skip files larger than 100MB
    /// ```
    pub fn max_file_size(mut self, size: u64) -> Self {
        self.max_file_size = size;
        self
    }
    
    /// Set number of parallel workers
    ///
    /// Controls how many threads are used for parallel operations like
    /// directory scanning and file processing.
    ///
    /// # Arguments
    ///
    /// * `count` - Number of parallel workers (minimum 1)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use titor::TitorBuilder;
    ///
    /// let builder = TitorBuilder::new()
    ///     .parallel_workers(4); // Use 4 threads
    /// ```
    ///
    /// # Notes
    ///
    /// - Defaults to the number of CPU cores
    /// - Values less than 1 are automatically set to 1
    pub fn parallel_workers(mut self, count: usize) -> Self {
        self.parallel_workers = count.max(1);
        self
    }
    
    /// Set whether to follow symbolic links
    ///
    /// When enabled, symlinks are followed and their targets are included
    /// in checkpoints. When disabled (default), symlinks are preserved as
    /// symlinks.
    ///
    /// # Arguments
    ///
    /// * `follow` - Whether to follow symbolic links
    ///
    /// # Examples
    ///
    /// ```rust
    /// use titor::TitorBuilder;
    ///
    /// let builder = TitorBuilder::new()
    ///     .follow_symlinks(false); // Preserve symlinks (default)
    /// ```
    ///
    /// # Security Considerations
    ///
    /// Following symlinks can lead to:
    /// - Including files outside the tracked directory
    /// - Circular references causing infinite loops
    /// - Increased checkpoint size
    pub fn follow_symlinks(mut self, follow: bool) -> Self {
        self.follow_symlinks = follow;
        self
    }
    
    /// Build Titor instance
    ///
    /// Creates a new Titor instance with the configured settings. If the
    /// storage path already contains Titor data, it will be opened instead
    /// of initialized.
    ///
    /// # Arguments
    ///
    /// * `root_path` - Directory to track
    /// * `storage_path` - Where to store checkpoint data
    ///
    /// # Returns
    ///
    /// Returns a configured `Titor` instance.
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - The root path does not exist
    /// - Storage initialization/opening fails
    /// - Invalid configuration
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use titor::TitorBuilder;
    /// use std::path::PathBuf;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let titor = TitorBuilder::new()
    ///     .compression_strategy(titor::CompressionStrategy::Adaptive { min_size: 4096, skip_extensions: vec![] })
    ///     .build(
    ///         PathBuf::from("./my_project"),
    ///         PathBuf::from("./.titor")
    ///     )?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn build(self, root_path: PathBuf, storage_path: PathBuf) -> Result<Titor> {
        // Compute effective ignore patterns. Always exclude the internal storage directory (".titor")
        // so that Titor never attempts to snapshot its own repository. This prevents exponential
        // growth and potential corruption when restoring checkpoints.
        let mut effective_ignore_patterns = self.ignore_patterns.clone();
        // Ignore the storage directory itself and all its contents
        effective_ignore_patterns.push(".titor".to_string());
        effective_ignore_patterns.push(".titor/".to_string());

        // Check if storage exists by looking for metadata.json
        let storage_metadata_path = storage_path.join("metadata.json");
        
        if storage_metadata_path.exists() {
            // Open existing
            Titor::open(root_path, storage_path)
        } else {
            // For initialization, ensure the storage directory doesn't exist
            // (Storage::init requires this)
            if storage_path.exists() && storage_path.read_dir()?.next().is_none() {
                // Directory exists but is empty (common with TempDir), remove it
                std::fs::remove_dir(&storage_path).ok();
            }
            
            // Initialize new
            let mut titor = Titor::init(root_path.clone(), storage_path)?;
            
            // Update configuration
            titor.config.ignore_patterns = effective_ignore_patterns.clone();
            titor.config.max_file_size = self.max_file_size;
            titor.config.parallel_workers = self.parallel_workers;
            titor.config.follow_symlinks = self.follow_symlinks;
            
            // Update file tracker
            titor.file_tracker = FileTracker::new(root_path)
                .with_ignore_patterns(effective_ignore_patterns.clone())
                .with_max_file_size(self.max_file_size)
                .with_follow_symlinks(self.follow_symlinks)
                .with_parallel_workers(self.parallel_workers);
            
            // Update storage metadata
            titor.storage.update_metadata(|metadata| {
                metadata.config = titor.config.clone();
            })?;
            
            Ok(titor)
        }
    }
}

impl Default for TitorBuilder {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;
    
    fn create_test_titor() -> (Titor, TempDir, TempDir) {
        let root_dir = TempDir::new().unwrap();
        let storage_dir = TempDir::new().unwrap();
        
        let titor = TitorBuilder::new()
            .build(
                root_dir.path().to_path_buf(),
                storage_dir.path().to_path_buf(),
            )
            .unwrap();
        
        (titor, root_dir, storage_dir)
    }
    
    #[test]
    fn test_titor_init() {
        let root_dir = TempDir::new().unwrap();
        let storage_dir = TempDir::new().unwrap();
        
        // Remove the directory created by TempDir
        std::fs::remove_dir_all(storage_dir.path()).ok();
        
        let _titor = Titor::init(
            root_dir.path().to_path_buf(),
            storage_dir.path().to_path_buf(),
        ).unwrap();
        
        // Check storage structure was created
        assert!(storage_dir.path().join("metadata.json").exists());
        assert!(storage_dir.path().join("checkpoints").exists());
        assert!(storage_dir.path().join("objects").exists());
    }
    
    #[test]
    fn test_checkpoint_creation() {
        let (mut titor, root_dir, _storage_dir) = create_test_titor();
        
        // Create some files
        fs::write(root_dir.path().join("file1.txt"), "content1").unwrap();
        fs::write(root_dir.path().join("file2.txt"), "content2").unwrap();
        
        // Create checkpoint
        let checkpoint = titor.checkpoint(Some("Initial state".to_string())).unwrap();
        
        assert!(checkpoint.parent_id.is_none());
        assert_eq!(checkpoint.metadata.file_count, 2);
        assert!(checkpoint.metadata.total_size > 0);
    }
    
    #[test]
    fn test_checkpoint_restore() {
        let (mut titor, root_dir, _storage_dir) = create_test_titor();
        
        // Initial state
        fs::write(root_dir.path().join("file1.txt"), "version1").unwrap();
        let checkpoint1 = titor.checkpoint(Some("Version 1".to_string())).unwrap();
        
        // Modify files
        fs::write(root_dir.path().join("file1.txt"), "version2").unwrap();
        fs::write(root_dir.path().join("file2.txt"), "new file").unwrap();
        let _checkpoint2 = titor.checkpoint(Some("Version 2".to_string())).unwrap();
        
        // Restore to version 1
        let result = titor.restore(&checkpoint1.id).unwrap();
        
        assert_eq!(result.files_restored, 1);
        assert_eq!(result.files_deleted, 1);
        
        // Verify content
        let content = fs::read_to_string(root_dir.path().join("file1.txt")).unwrap();
        assert_eq!(content, "version1");
        assert!(!root_dir.path().join("file2.txt").exists());
    }
    
    #[test]
    fn test_diff() {
        let (mut titor, root_dir, _storage_dir) = create_test_titor();
        
        // Create checkpoints
        fs::write(root_dir.path().join("file1.txt"), "content1").unwrap();
        let checkpoint1 = titor.checkpoint(None).unwrap();
        
        fs::write(root_dir.path().join("file1.txt"), "modified").unwrap();
        fs::write(root_dir.path().join("file2.txt"), "new").unwrap();
        let checkpoint2 = titor.checkpoint(None).unwrap();
        
        // Compute diff
        let diff = titor.diff(&checkpoint1.id, &checkpoint2.id).unwrap();
        
        assert_eq!(diff.added_files.len(), 1);
        assert_eq!(diff.modified_files.len(), 1);
        assert_eq!(diff.deleted_files.len(), 0);
    }

    #[test]
    fn test_storage_dir_ignored() {
        use std::fs;
        // Create root directory with nested .titor dir (storage path)
        let root_dir = TempDir::new().unwrap();
        let storage_path = root_dir.path().join(".titor");

        // Ensure storage directory exists so builder recognises it
        fs::create_dir_all(&storage_path).unwrap();

        // Build Titor instance
        let mut titor = TitorBuilder::new()
            .build(root_dir.path().to_path_buf(), storage_path.clone())
            .unwrap();

        // Add a real file outside the storage directory
        fs::write(root_dir.path().join("data.txt"), "hello").unwrap();

        // Create checkpoint
        let checkpoint = titor.checkpoint(None).unwrap();

        // The checkpoint should only contain the user file, not the storage contents
        assert_eq!(checkpoint.metadata.file_count, 1);
        
        // Manifest should not include any path inside .titor
        let manifest = titor.storage.load_manifest(&checkpoint.id).unwrap();
        assert!(manifest.files.iter().all(|e| !e.path.starts_with(".titor")));
    }

    #[test]
    fn test_current_checkpoint_updates() {
        use std::fs;
        let (mut titor, root_dir, _storage_dir) = create_test_titor();

        // First checkpoint
        fs::write(root_dir.path().join("file1.txt"), "one").unwrap();
        let cp1 = titor.checkpoint(None).unwrap();
        assert_eq!(titor.get_timeline().unwrap().current_checkpoint_id, Some(cp1.id.clone()));

        // Second checkpoint
        fs::write(root_dir.path().join("file2.txt"), "two").unwrap();
        let cp2 = titor.checkpoint(None).unwrap();
        assert_eq!(titor.get_timeline().unwrap().current_checkpoint_id, Some(cp2.id.clone()));
    }

    #[test]
    fn test_special_character_filenames() {
        use std::fs;
        let (mut titor, root_dir, _storage_dir) = create_test_titor();

        // Create files with special characters
        let special_files = vec![
            ("file with spaces.txt", "content1"),
            ("file-with-dashes.txt", "content2"),
            ("file_with_underscores.txt", "content3"),
            ("file$with$dollar.txt", "content4"),
            ("file@with@at.txt", "content5"),
            ("file#with#hash.txt", "content6"),
            ("file(with)parens.txt", "content7"),
            ("file[with]brackets.txt", "content8"),
            ("file{with}braces.txt", "content9"),
            ("file'with'quotes.txt", "content10"),
            ("file\"with\"doublequotes.txt", "content11"),
            ("file🔥with🔥emoji.txt", "content12"),
            ("文件名.txt", "content13"), // Chinese characters
            ("файл.txt", "content14"), // Cyrillic characters
        ];

        // Create all special files
        for (filename, content) in &special_files {
            fs::write(root_dir.path().join(filename), content).unwrap();
        }

        // Create checkpoint
        let checkpoint = titor.checkpoint(Some("Special characters test".to_string())).unwrap();
        assert_eq!(checkpoint.metadata.file_count, special_files.len());

        // Delete all files
        for (filename, _) in &special_files {
            fs::remove_file(root_dir.path().join(filename)).unwrap();
        }

        // Restore checkpoint
        let result = titor.restore(&checkpoint.id).unwrap();
        assert_eq!(result.files_restored, special_files.len());
        assert!(result.warnings.is_empty(), "Warnings during restore: {:?}", result.warnings);

        // Verify all files were restored correctly
        for (filename, expected_content) in &special_files {
            let path = root_dir.path().join(filename);
            assert!(path.exists(), "File {} was not restored", filename);
            let content = fs::read_to_string(&path).unwrap();
            assert_eq!(content, *expected_content, "Content mismatch for {}", filename);
        }
    }

    #[test]
    fn test_symlink_restoration() {
        use std::fs;
        let (mut titor, root_dir, _storage_dir) = create_test_titor();

        // Create a regular file and a symlink to it
        let target_path = root_dir.path().join("target.txt");
        let symlink_path = root_dir.path().join("symlink.txt");
        
        fs::write(&target_path, "target content").unwrap();
        utils::create_symlink(&PathBuf::from("target.txt"), &symlink_path).unwrap();
        
        // Verify symlink was created correctly
        assert!(symlink_path.exists(), "Symlink was not created");
        assert!(symlink_path.symlink_metadata().unwrap().file_type().is_symlink(), "Created file is not a symlink");
        
        // Create checkpoint
        let checkpoint = titor.checkpoint(Some("Symlink test".to_string())).unwrap();
        println!("Created checkpoint with {} files", checkpoint.metadata.file_count);
        
        // Load and inspect manifest
        let manifest = titor.storage.load_manifest(&checkpoint.id).unwrap();
        for entry in &manifest.files {
            println!("Manifest entry: path={:?}, is_symlink={}, symlink_target={:?}",
                     entry.path, entry.is_symlink, entry.symlink_target);
        }
        
        // Delete symlink and target
        fs::remove_file(&symlink_path).unwrap();
        fs::remove_file(&target_path).unwrap();
        
        // Restore checkpoint
        let result = titor.restore(&checkpoint.id).unwrap();
        println!("Restore result: {} files restored, warnings: {:?}", result.files_restored, result.warnings);
        assert!(result.warnings.is_empty(), "Warnings during restore: {:?}", result.warnings);
        
        // List what files exist after restore
        for entry in fs::read_dir(root_dir.path()).unwrap() {
            let entry = entry.unwrap();
            let metadata = entry.metadata();
            let is_symlink = if let Ok(m) = &metadata {
                m.file_type().is_symlink()
            } else {
                // If metadata fails, try symlink_metadata
                entry.path().symlink_metadata()
                    .map(|m| m.file_type().is_symlink())
                    .unwrap_or(false)
            };
            println!("After restore: {:?} (symlink: {})", 
                     entry.file_name(), 
                     is_symlink);
        }
        
        // Verify symlink was restored correctly
        assert!(symlink_path.exists() || symlink_path.symlink_metadata().is_ok(), 
                "Symlink was not restored");
        assert!(symlink_path.symlink_metadata().unwrap().file_type().is_symlink(), 
                "Restored file is not a symlink");
        
        // Verify symlink points to correct target
        let restored_target = utils::read_symlink(&symlink_path).unwrap();
        assert_eq!(restored_target, PathBuf::from("target.txt"));
    }

    #[test]
    fn test_compression_size_tracking() {
        let (mut titor, temp_dir, _storage_dir) = create_test_titor();
        
        // Create files with different compression characteristics
        // Highly compressible text file
        let repetitive_content = "This is a test. ".repeat(10000); // ~160KB
        fs::write(temp_dir.path().join("repetitive.txt"), &repetitive_content).unwrap();
        
        // Less compressible binary data
        let binary_content: Vec<u8> = (0..10000).map(|i| (i % 256) as u8).collect();
        fs::write(temp_dir.path().join("binary.dat"), &binary_content).unwrap();
        
        // Small file (below compression threshold)
        let small_content = "Small file";
        fs::write(temp_dir.path().join("small.txt"), &small_content).unwrap();
        
        // Create checkpoint
        let checkpoint = titor.checkpoint(Some("Compression test".to_string())).unwrap();
        
        // Verify that compression is happening
        assert!(checkpoint.metadata.compressed_size < checkpoint.metadata.total_size,
                "Compressed size ({}) should be less than total size ({})",
                checkpoint.metadata.compressed_size,
                checkpoint.metadata.total_size);
        
        // The repetitive text should compress significantly
        let compression_ratio = 1.0 - (checkpoint.metadata.compressed_size as f64 / checkpoint.metadata.total_size as f64);
        assert!(compression_ratio > 0.1, // At least 10% compression
                "Compression ratio {:.2}% is too low", compression_ratio * 100.0);
        
        println!("Compression test results:");
        println!("  Total size: {} bytes", checkpoint.metadata.total_size);
        println!("  Compressed size: {} bytes", checkpoint.metadata.compressed_size);
        println!("  Compression ratio: {:.2}%", compression_ratio * 100.0);
    }
    
    #[test]
    fn test_empty_directory_preservation() {
        let (mut titor, temp_dir, _storage_dir) = create_test_titor();

        // Create empty directories and directories with files
        fs::create_dir(temp_dir.path().join("empty_dir")).unwrap();
        fs::create_dir_all(temp_dir.path().join("nested/empty")).unwrap();
        fs::create_dir(temp_dir.path().join("dir_with_file")).unwrap();
        fs::write(temp_dir.path().join("dir_with_file/file.txt"), "content").unwrap();
        
        // Create checkpoint
        let checkpoint = titor.checkpoint(Some("Empty dirs test".to_string())).unwrap();
        
        // Delete all directories
        fs::remove_dir_all(temp_dir.path().join("empty_dir")).unwrap();
        fs::remove_dir_all(temp_dir.path().join("nested")).unwrap();
        fs::remove_dir_all(temp_dir.path().join("dir_with_file")).unwrap();
        
        // Restore checkpoint
        let result = titor.restore(&checkpoint.id).unwrap();
        assert!(result.warnings.is_empty(), "Warnings during restore: {:?}", result.warnings);
        
        // Verify empty directories were restored
        assert!(temp_dir.path().join("empty_dir").exists(), "Empty directory was not restored");
        assert!(temp_dir.path().join("nested/empty").exists(), "Nested empty directory was not restored");
        assert!(temp_dir.path().join("dir_with_file").exists(), "Directory with file was not restored");
        assert!(temp_dir.path().join("dir_with_file/file.txt").exists(), "File in directory was not restored");
    }
    
    #[test]
    fn test_line_diff_simple() {
        use crate::types::DiffOptions;
        
        let (mut titor, temp_dir, _storage_dir) = create_test_titor();
        
        // Create a file with initial content
        let file_path = temp_dir.path().join("test.txt");
        fs::write(&file_path, "line1\nline2\nline3\n").unwrap();
        let checkpoint1 = titor.checkpoint(Some("Initial".to_string())).unwrap();
        
        // Modify the file
        fs::write(&file_path, "line1\nline2 modified\nline3\nline4\n").unwrap();
        let checkpoint2 = titor.checkpoint(Some("Modified".to_string())).unwrap();
        
        // Get line diff
        let options = DiffOptions::default();
        let file_diff = titor.diff_file(
            &checkpoint1.id,
            &checkpoint2.id,
            Path::new("test.txt"),
            options
        ).unwrap();
        
        // Verify diff results
        assert!(!file_diff.is_binary);
        assert_eq!(file_diff.lines_added, 2); // "line2 modified" and "line4"
        assert_eq!(file_diff.lines_deleted, 1); // "line2"
        assert!(file_diff.hunks.len() > 0);
    }
    
    #[test]
    fn test_line_diff_binary_file() {
        use crate::types::DiffOptions;
        
        let (mut titor, temp_dir, _storage_dir) = create_test_titor();
        
        // Create a binary file
        let file_path = temp_dir.path().join("binary.dat");
        let binary_content: Vec<u8> = vec![0, 1, 2, 3, 255, 254, 253, 0];
        fs::write(&file_path, &binary_content).unwrap();
        let checkpoint1 = titor.checkpoint(Some("Binary v1".to_string())).unwrap();
        
        // Modify the binary file
        let modified_binary: Vec<u8> = vec![0, 1, 2, 3, 4, 5, 6, 7, 0];
        fs::write(&file_path, &modified_binary).unwrap();
        let checkpoint2 = titor.checkpoint(Some("Binary v2".to_string())).unwrap();
        
        // Get diff
        let options = DiffOptions::default();
        let file_diff = titor.diff_file(
            &checkpoint1.id,
            &checkpoint2.id,
            Path::new("binary.dat"),
            options
        ).unwrap();
        
        // Verify binary detection
        assert!(file_diff.is_binary);
        assert_eq!(file_diff.hunks.len(), 0); // No hunks for binary files
        assert_eq!(file_diff.lines_added, 0);
        assert_eq!(file_diff.lines_deleted, 0);
    }
    
    #[test]
    fn test_line_diff_new_file() {
        use crate::types::DiffOptions;
        
        let (mut titor, temp_dir, _storage_dir) = create_test_titor();
        
        // Create initial checkpoint without the file
        fs::write(temp_dir.path().join("other.txt"), "other content").unwrap();
        let checkpoint1 = titor.checkpoint(Some("Before".to_string())).unwrap();
        
        // Add new file
        fs::write(temp_dir.path().join("new.txt"), "new line 1\nnew line 2\n").unwrap();
        let checkpoint2 = titor.checkpoint(Some("After".to_string())).unwrap();
        
        // Try to diff a file that doesn't exist in first checkpoint
        let options = DiffOptions::default();
        let result = titor.diff_file(
            &checkpoint1.id,
            &checkpoint2.id,
            Path::new("new.txt"),
            options
        );
        
        // Should fail because file doesn't exist in first checkpoint
        assert!(result.is_err());
    }
    
    #[test]
    fn test_line_diff_context_lines() {
        use crate::types::{DiffOptions, LineChange};
        
        let (mut titor, temp_dir, _storage_dir) = create_test_titor();
        
        // Create a file with many lines
        let content = (1..=10).map(|i| format!("line{}", i)).collect::<Vec<_>>().join("\n");
        fs::write(temp_dir.path().join("context.txt"), &content).unwrap();
        let checkpoint1 = titor.checkpoint(Some("Original".to_string())).unwrap();
        
        // Modify one line in the middle
        let modified = content.replace("line5", "line5 modified");
        fs::write(temp_dir.path().join("context.txt"), &modified).unwrap();
        let checkpoint2 = titor.checkpoint(Some("Modified".to_string())).unwrap();
        
        // Get diff with custom context lines
        let mut options = DiffOptions::default();
        options.context_lines = 2;
        
        let file_diff = titor.diff_file(
            &checkpoint1.id,
            &checkpoint2.id,
            Path::new("context.txt"),
            options
        ).unwrap();
        
        // Verify we have context lines
        assert_eq!(file_diff.hunks.len(), 1);
        let hunk = &file_diff.hunks[0];
        
        // Count context lines
        let context_count = hunk.changes.iter()
            .filter(|c| matches!(c, LineChange::Context(_, _)))
            .count();
        
        // Should have at least 4 context lines (2 before, 2 after)
        assert!(context_count >= 4);
    }
    
    #[test]
    fn test_detailed_diff() {
        use crate::types::DiffOptions;
        
        let (mut titor, temp_dir, _storage_dir) = create_test_titor();
        
        // Create multiple files
        fs::write(temp_dir.path().join("file1.txt"), "content1").unwrap();
        fs::write(temp_dir.path().join("file2.txt"), "content2").unwrap();
        fs::write(temp_dir.path().join("file3.txt"), "content3").unwrap();
        let checkpoint1 = titor.checkpoint(Some("Initial".to_string())).unwrap();
        
        // Modify files
        fs::write(temp_dir.path().join("file1.txt"), "content1\nmodified").unwrap();
        fs::write(temp_dir.path().join("file2.txt"), "completely different").unwrap();
        fs::remove_file(temp_dir.path().join("file3.txt")).unwrap();
        fs::write(temp_dir.path().join("file4.txt"), "new file").unwrap();
        let checkpoint2 = titor.checkpoint(Some("Changes".to_string())).unwrap();
        
        // Get detailed diff
        let options = DiffOptions::default();
        let detailed = titor.diff_detailed(&checkpoint1.id, &checkpoint2.id, options).unwrap();
        
        // Verify basic diff info
        assert_eq!(detailed.basic_diff.added_files.len(), 1); // file4.txt
        assert_eq!(detailed.basic_diff.modified_files.len(), 2); // file1.txt, file2.txt
        assert_eq!(detailed.basic_diff.deleted_files.len(), 1); // file3.txt
        
        // Verify line-level diffs were computed for modified files
        assert_eq!(detailed.file_diffs.len(), 2); // Should have diffs for 2 modified files
        assert!(detailed.total_lines_added > 0);
        assert!(detailed.total_lines_deleted > 0);
    }
    
    #[test]
    fn test_line_diff_whitespace_ignore() {
        use crate::types::DiffOptions;
        
        let (mut titor, temp_dir, _storage_dir) = create_test_titor();
        
        // Create file with whitespace
        fs::write(temp_dir.path().join("whitespace.txt"), "line1\nline2  \nline3").unwrap();
        let checkpoint1 = titor.checkpoint(Some("Original".to_string())).unwrap();
        
        // Modify whitespace only
        fs::write(temp_dir.path().join("whitespace.txt"), "line1\nline2\nline3  ").unwrap();
        let checkpoint2 = titor.checkpoint(Some("Whitespace changed".to_string())).unwrap();
        
        // Diff without ignoring whitespace
        let options_no_ignore = DiffOptions {
            ignore_whitespace: false,
            ..Default::default()
        };
        let diff_with_ws = titor.diff_file(
            &checkpoint1.id,
            &checkpoint2.id,
            Path::new("whitespace.txt"),
            options_no_ignore
        ).unwrap();
        
        // Diff ignoring whitespace
        let options_ignore = DiffOptions {
            ignore_whitespace: true,
            ..Default::default()
        };
        let diff_without_ws = titor.diff_file(
            &checkpoint1.id,
            &checkpoint2.id,
            Path::new("whitespace.txt"),
            options_ignore
        ).unwrap();
        
        // When not ignoring whitespace, we should see changes
        assert!(diff_with_ws.lines_added > 0 || diff_with_ws.lines_deleted > 0);
        
        // When ignoring whitespace, changes should be minimal or none
        // (This depends on the exact implementation of whitespace handling)
        assert!(diff_without_ws.lines_added <= diff_with_ws.lines_added);
        assert!(diff_without_ws.lines_deleted <= diff_with_ws.lines_deleted);
    }
    
    #[test] 
    fn test_line_diff_large_file() {
        use crate::types::DiffOptions;
        
        let (mut titor, temp_dir, _storage_dir) = create_test_titor();
        
        // Create a large file
        let large_content = "Large line\n".repeat(10000);
        fs::write(temp_dir.path().join("large.txt"), &large_content).unwrap();
        let checkpoint1 = titor.checkpoint(Some("Large file".to_string())).unwrap();
        
        // Modify it slightly
        let modified = format!("First line modified\n{}", &large_content[11..]);
        fs::write(temp_dir.path().join("large.txt"), &modified).unwrap();
        let checkpoint2 = titor.checkpoint(Some("Large file modified".to_string())).unwrap();
        
        // Try to diff with size limit
        let small_options = DiffOptions {
            max_file_size: 100, // Very small limit
            ..Default::default()
        };
        
        let result = titor.diff_file(
            &checkpoint1.id,
            &checkpoint2.id,
            Path::new("large.txt"),
            small_options
        );
        
        // Should fail due to size limit
        assert!(result.is_err());
        
        // Try with larger limit
        let large_options = DiffOptions {
            max_file_size: 1024 * 1024 * 100, // 100MB
            ..Default::default()
        };
        let file_diff = titor.diff_file(
            &checkpoint1.id,
            &checkpoint2.id,
            Path::new("large.txt"),
            large_options
        ).unwrap();
        
        // Should succeed and show the change
        assert_eq!(file_diff.lines_added, 1);
        assert_eq!(file_diff.lines_deleted, 1);
    }
    
    #[test]
    fn test_line_diff_unicode() {
        use crate::types::DiffOptions;
        
        let (mut titor, temp_dir, _storage_dir) = create_test_titor();
        
        // Create file with unicode content
        let unicode_content = "Hello 世界\n🦀 Rust\nΓειά σου κόσμε\n";
        fs::write(temp_dir.path().join("unicode.txt"), unicode_content).unwrap();
        let checkpoint1 = titor.checkpoint(Some("Unicode v1".to_string())).unwrap();
        
        // Modify with more unicode
        let modified = "Hello 世界\n🦀 Rust 🔥\nΓειά σου κόσμε\n新しい行\n";
        fs::write(temp_dir.path().join("unicode.txt"), modified).unwrap();
        let checkpoint2 = titor.checkpoint(Some("Unicode v2".to_string())).unwrap();
        
        // Get diff
        let options = DiffOptions::default();
        let file_diff = titor.diff_file(
            &checkpoint1.id,
            &checkpoint2.id,
            Path::new("unicode.txt"),
            options
        ).unwrap();
        
        // Verify unicode handling
        assert!(!file_diff.is_binary);
        assert_eq!(file_diff.lines_added, 2); // Modified line + new line
        assert_eq!(file_diff.lines_deleted, 1); // Original emoji line
    }
    
    #[test]
    fn test_gitignore_creation() {
        let root_dir = TempDir::new().unwrap();
        let storage_dir = TempDir::new().unwrap();
        
        // Remove the directory created by TempDir
        std::fs::remove_dir_all(storage_dir.path()).ok();
        
        let gitignore_path = root_dir.path().join(".gitignore");
        assert!(!gitignore_path.exists(), ".gitignore should not exist initially");
        
        // Initialize Titor
        let _titor = Titor::init(
            root_dir.path().to_path_buf(),
            storage_dir.path().to_path_buf(),
        ).unwrap();
        
        // Check that .gitignore was created
        assert!(gitignore_path.exists(), ".gitignore should be created");
        
        // Read .gitignore content
        let content = fs::read_to_string(&gitignore_path).unwrap();
        assert!(content.contains(".titor"), ".gitignore should contain .titor");
    }
    
    #[test]
    fn test_gitignore_existing_file() {
        let root_dir = TempDir::new().unwrap();
        let storage_dir = TempDir::new().unwrap();
        
        // Remove the directory created by TempDir
        std::fs::remove_dir_all(storage_dir.path()).ok();
        
        // Create .gitignore with existing content
        let gitignore_path = root_dir.path().join(".gitignore");
        fs::write(&gitignore_path, "*.log\nnode_modules/\n").unwrap();
        
        // Initialize Titor
        let _titor = Titor::init(
            root_dir.path().to_path_buf(),
            storage_dir.path().to_path_buf(),
        ).unwrap();
        
        // Read .gitignore content
        let content = fs::read_to_string(&gitignore_path).unwrap();
        
        // Check that existing content is preserved
        assert!(content.contains("*.log"), "Existing content should be preserved");
        assert!(content.contains("node_modules/"), "Existing content should be preserved");
        
        // Check that .titor was added
        assert!(content.contains(".titor"), ".gitignore should contain .titor");
        
        // Verify the content structure
        let lines: Vec<&str> = content.lines().collect();
        assert!(lines.contains(&"*.log"));
        assert!(lines.contains(&"node_modules/"));
        assert!(lines.contains(&".titor"));
    }
    
    #[test]
    fn test_gitignore_already_contains_titor() {
        let root_dir = TempDir::new().unwrap();
        let storage_dir = TempDir::new().unwrap();
        
        // Remove the directory created by TempDir
        std::fs::remove_dir_all(storage_dir.path()).ok();
        
        // Create .gitignore with .titor already in it
        let gitignore_path = root_dir.path().join(".gitignore");
        fs::write(&gitignore_path, "*.log\n.titor\nnode_modules/\n").unwrap();
        let original_content = fs::read_to_string(&gitignore_path).unwrap();
        
        // Initialize Titor
        let _titor = Titor::init(
            root_dir.path().to_path_buf(),
            storage_dir.path().to_path_buf(),
        ).unwrap();
        
        // Read .gitignore content after init
        let new_content = fs::read_to_string(&gitignore_path).unwrap();
        
        // Content should remain unchanged
        assert_eq!(original_content, new_content, ".gitignore should not be modified if .titor already exists");
    }
    
    #[test]
    fn test_gitignore_on_open() {
        let root_dir = TempDir::new().unwrap();
        let storage_dir = TempDir::new().unwrap();
        
        // Remove the directory created by TempDir
        std::fs::remove_dir_all(storage_dir.path()).ok();
        
        // Initialize Titor first
        let _titor = Titor::init(
            root_dir.path().to_path_buf(),
            storage_dir.path().to_path_buf(),
        ).unwrap();
        
        // Remove .gitignore
        let gitignore_path = root_dir.path().join(".gitignore");
        fs::remove_file(&gitignore_path).unwrap();
        assert!(!gitignore_path.exists());
        
        // Open existing Titor storage
        let _titor2 = Titor::open(
            root_dir.path().to_path_buf(),
            storage_dir.path().to_path_buf(),
        ).unwrap();
        
        // Check that .gitignore was recreated
        assert!(gitignore_path.exists(), ".gitignore should be created on open");
        let content = fs::read_to_string(&gitignore_path).unwrap();
        assert!(content.contains(".titor"), ".gitignore should contain .titor");
    }
}