obsidian-backups 0.1.3

A Git-based backup library for Rust applications. Originally designed for the Obsidian Minecraft Server Panel, but generic enough to be used in any project requiring file backup management.
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
//! `BackupManager` is responsible for managing backup operations within a Git-based
//! storage mechanism. It provides functionality to initialize a backup repository,
//! create new backups, list existing backups, restore backups, and export backups
//! as compressed archives.
//!
//! # Examples
//!
//! ```rust
//! use obsidian_backups::BackupManager;
//!
//! let store_dir = "./backup_store";
//! let working_dir = "./my_data";
//! let backup_manager = BackupManager::new(store_dir, working_dir)
//!     .expect("Failed to initialize BackupManager");
//! ```
//!
//! # Fields
//!
//! * `repository` - The Git repository used for managing backups.
use crate::data::backup_item::BackupItem;
use crate::data::modified_file::ModifiedFile;
use crate::log_stub::*;
use anyhow::{Result, anyhow};
use git2::{Oid, Repository, RepositoryInitOptions};
use ignore::gitignore::{Gitignore, GitignoreBuilder};
#[cfg(feature = "zip")]
use sevenz_rust2::{ArchiveWriter, encoder_options};
use std::fs;
use std::path::Path;

/// `BackupManager` is a struct responsible for managing backup operations.
///
/// This struct serves as a core component for creating, storing, and retrieving backups
/// in the system. It encapsulates the `Repository` where backup data is managed,
/// providing an interface to interact with the underlying repository for backup-related tasks.
///
/// # Fields
/// - `repository`: The repository where backup data is stored and managed.
///
/// # Example
/// ```rust
/// use obsidian_backups::BackupManager;
///
/// let backup_manager = BackupManager::new("./backup_store", "./my_data")
///     .expect("Failed to create BackupManager");
/// ```
pub struct BackupManager {
    repository: Repository,
    ignore_matcher: Option<Gitignore>,
}

impl BackupManager {
    /// Helper function to check if a path should be excluded from backups using ignore patterns in `exclude.obak`
    fn should_exclude(&self, path: &Path, is_dir: bool) -> bool {
        // Always skip the Git metadata directory and common junk files
        if let Some(name) = path.file_name().and_then(|n| n.to_str())
            && (name == ".git"
                || matches!(
                    name,
                    ".DS_Store"
                        | "Thumbs.db"
                        | "desktop.ini"
                        | ".Spotlight-V100"
                        | ".Trashes"
                        | "ehthumbs.db"
                        | "ehthumbs_vista.db"
                        | "$RECYCLE.BIN"
                )
                || name.starts_with("~$")
                || name.ends_with(".tmp")
                || name.ends_with(".swp")
                || name.ends_with("~")
                || name == "__pycache__")
        {
            return true;
        }

        if let Some(matcher) = &self.ignore_matcher {
            let m = matcher.matched(path, is_dir);
            if m.is_ignore() {
                return true;
            }
        }
        false
    }

    /// Helper function to recursively add files from a directory to the git index
    #[allow(clippy::only_used_in_recursion)]
    fn add_directory_to_index(
        &self,
        index: &mut git2::Index,
        dir_path: &Path,
        base_path: &Path,
    ) -> Result<()> {
        for entry in fs::read_dir(dir_path)? {
            let entry = entry?;
            let path = entry.path();

            let file_type = entry.file_type()?;

            // Skip excluded files and directories
            if self.should_exclude(&path, file_type.is_dir()) {
                debug!("Skipping excluded path: {:?}", path);
                continue;
            }

            if file_type.is_dir() {
                // Recursively add subdirectory
                self.add_directory_to_index(index, &path, base_path)?;
            } else if file_type.is_file() {
                // Calculate relative path from base_path
                let relative_path = path.strip_prefix(base_path)?;
                debug!("Adding file to index: {:?}", relative_path);
                index.add_path(relative_path)?;
            }
        }
        Ok(())
    }

    /// Creates a new instance of `BackupManager`.
    ///
    /// This function initializes a `BackupManager` by setting up a new Git repository
    /// in the specified `store_directory` with the specified `working_directory` as
    /// the working directory for the repository.
    ///
    /// # Arguments
    ///
    /// * `store_directory` - A reference to a path where the repository data will be stored.
    /// * `working_directory` - A reference to a path that will serve as the working directory for the repository.
    ///
    /// Both arguments accept types that can be converted into a `PathBuf`.
    ///
    /// # Returns
    ///
    /// Returns `Ok(Self)` with the initialized `BackupManager` if successful, or an error
    /// if the Git repository initialization fails.
    ///
    /// # Logging
    ///
    /// * Logs an informational message when starting and successfully completing the initialization process.
    /// * Logs debug messages showing the resolved paths and steps during initialization.
    ///
    /// # Errors
    ///
    /// Returns an error if repository initialization fails. This typically occurs
    /// due to invalid paths, insufficient permissions, or issues with the Git backend.
    ///
    /// # Example
    ///
    /// ```
    /// use obsidian_backup_system::BackupManager;
    ///
    /// let manager = BackupManager::new("./backup_store", "./my_data")
    ///     .expect("Failed to initialize BackupManager");
    /// ```
    ///
    /// Note: Ensure that the provided paths are valid and writable for the process.
    pub fn new(
        store_directory: impl AsRef<Path>,
        working_directory: impl AsRef<Path>,
    ) -> Result<Self> {
        info!("Initializing BackupManager");

        // Convert to absolute paths to avoid path resolution issues
        let store_directory = if store_directory.as_ref().is_absolute() {
            store_directory.as_ref().to_path_buf()
        } else {
            std::env::current_dir()?.join(store_directory.as_ref())
        };

        let working_directory = if working_directory.as_ref().is_absolute() {
            working_directory.as_ref().to_path_buf()
        } else {
            std::env::current_dir()?.join(working_directory.as_ref())
        };

        debug!("Store directory (absolute): {:?}", store_directory);
        debug!("Working directory (absolute): {:?}", working_directory);

        let mut opts = RepositoryInitOptions::new();
        opts.workdir_path(&working_directory);
        opts.no_dotgit_dir(true);

        debug!("Initializing git repository with options");
        let repository = Repository::init_opts(&store_directory, &opts)?;

        info!("BackupManager initialized successfully");
        Ok(Self {
            repository,
            ignore_matcher: None,
        })
    }

    /// Sets up a `.gitignore`-style ignore file for the repository using the provided file path.
    /// This function configures an ignore matcher to exclude specified paths or patterns.
    ///
    /// # Arguments
    /// * `ignore_file` - A path-like object referencing the ignore file to process. The file should follow `.gitignore` syntax.
    ///
    /// # Returns
    /// * `Result<()>` - Returns `Ok(())` if the ignore matcher is successfully built and configured.
    ///                  Returns an error if the ignore matcher cannot be built or if the ignore file causes an issue.
    ///
    /// # Behavior
    /// 1. Locates the working directory of the repository. Defaults to `./` if the repository has no working directory.
    /// 2. Initializes a `GitignoreBuilder` using the repository's working directory.
    /// 3. Checks whether the provided ignore file exists:
    ///    - If the file exists, attempts to add it to the builder. Logs a warning if there's an issue while adding the file.
    /// 4. Attempts to construct the ignore matcher from the builder:
    ///    - If successful, stores the ignore matcher in `self.ignore_matcher`.
    ///    - If unsuccessful, logs an error message and returns an error.
    ///
    /// # Errors
    /// * Returns an error if:
    ///   - The ignore file could not be properly parsed or added.
    ///   - The ignore matcher fails to build successfully.
    ///
    /// # Logging
    /// - Logs a warning message if the function fails to add the ignore file to the builder.
    /// - Logs an error message if the function fails to build the ignore matcher.
    ///
    /// # Example Usage
    /// ```rust
    /// use std::path::Path;
    /// use obsidian_backup_system::BackupManager;
    ///
    /// let mut backup_manager = BackupManager::new("./backup_store", "./my_data")?;
    /// backup_manager.setup_ignore_file(".my_ignore_file")?;
    /// ```
    pub fn setup_ignore_file(&mut self, ignore_file: impl AsRef<Path>) -> Result<()> {
        let working_directory = self.repository.workdir().unwrap_or(Path::new("./"));
        let mut builder = GitignoreBuilder::new(working_directory);

        let ignore_file = ignore_file.as_ref();

        if ignore_file.exists()
            && let Some(e) = builder.add(ignore_file)
        {
            warn!("Failed to add ignore file {ignore_file:?}: {e}");
        }
        match builder.build() {
            Ok(ignore_matcher) => {
                self.ignore_matcher = Some(ignore_matcher);
                Ok(())
            }
            Err(e) => {
                error!("Failed to build ignore matcher: {e}");
                Err(anyhow!("Failed to build ignore matcher: {e}"))
            }
        }
    }

    /// Lists all backup items available in the repository.
    ///
    /// The method traverses the commit history of the repository, collects metadata
    /// for each commit, and returns a list of items representing the backup points. Each
    /// item includes the commit ID, timestamp, and commit message.
    ///
    /// # Process
    /// - Logs an informational message indicating the start of the operation.
    /// - Initializes a revision walk over the repository to retrieve commit objects.
    /// - Iterates through each commit, retrieves its metadata, and constructs a `BackupItem` instance.
    /// - Each created `BackupItem` is logged at the trace level, and the total count is logged at the end.
    ///
    /// # Returns
    /// A `Result` containing a vector of `BackupItem` instances if the operation succeeds, or an error
    /// if any repository operation fails.
    ///
    /// # Errors
    /// Returns an error if:
    /// - The revision walk initialization fails.
    /// - Retrieving an individual commit in the history fails.
    /// - Any other repository-related operation encounters an error.
    ///
    /// # Logging
    /// - Logs informational messages about the start and result of the operation.
    /// - Logs debug messages about processing individual commits.
    /// - Logs trace messages with details of each created `BackupItem`.
    ///
    /// # Example
    /// ```
    /// use obsidian_backup_system::BackupManager;
    ///
    /// let manager = BackupManager::new("./backup_store", "./my_data")
    ///     .expect("Failed to initialize BackupManager");
    ///
    /// match manager.list() {
    ///     Ok(backup_items) => {
    ///         for item in backup_items {
    ///             println!("Backup ID: {}, Timestamp: {}, Description: {}",
    ///                      item.id, item.timestamp, item.description);
    ///         }
    ///     },
    ///     Err(e) => eprintln!("Error listing backup items: {}", e),
    /// }
    /// ```
    ///
    /// # Note
    /// The method assumes that commit messages are UTF-8 encoded. If a commit has
    /// no message, an empty string is used as the description.
    ///
    /// # Dependencies
    /// - Requires the repository to be properly initialized and accessible.
    /// - Relies on the `BackupItem` struct to hold commit metadata.
    pub fn list(&self) -> Result<Vec<BackupItem>> {
        info!("Listing backup items");
        debug!("Starting revision walk");
        let mut items = Vec::new();
        let ids = self.list_ids()?;
        debug!("Found {} commit IDs", ids.len());

        for commit_id in ids {
            debug!("Processing commit: {}", commit_id);
            let oid = match Oid::from_str(&commit_id) {
                Ok(oid) => oid,
                Err(e) => {
                    warn!("Skipping invalid commit id {}: {}", commit_id, e);
                    continue;
                }
            };
            match self.repository.find_commit(oid) {
                Ok(commit) => {
                    let item = BackupItem {
                        id: commit_id,
                        timestamp: chrono::DateTime::from_timestamp_secs(commit.time().seconds())
                            .unwrap_or(chrono::DateTime::<chrono::Utc>::MIN_UTC),
                        description: commit
                            .message()
                            .unwrap_or("No description was provided")
                            .to_string(),
                    };
                    trace!(
                        "Created backup item: id={}, timestamp={}, description={:?}",
                        item.id, item.timestamp, item.description
                    );
                    items.push(item);
                }
                Err(e) => {
                    warn!("Skipping missing or unreadable commit {}: {}", commit_id, e);
                    continue;
                }
            }
        }

        info!("Found {} backup items", items.len());
        Ok(items)
    }

    fn list_ids(&self) -> Result<Vec<String>> {
        let mut rev_walk = self.repository.revwalk()?;
        // Try HEAD first; if it fails, fall back to any available reference target.
        let mut pushed = false;
        if let Ok(head) = self.repository.head()
            && let Some(oid) = head.target()
            && rev_walk.push(oid).is_ok()
        {
            pushed = true;
        }
        if !pushed && let Ok(refs) = self.repository.references() {
            for r in refs {
                if let Ok(r) = r
                    && let Some(oid) = r.target()
                    && rev_walk.push(oid).is_ok()
                {
                    pushed = true;
                    break;
                }
            }
        }
        if !pushed {
            // No references to walk; return empty list rather than erroring
            return Ok(Vec::new());
        }

        let mut ids = Vec::new();
        for oid in rev_walk.flatten() {
            ids.push(oid.to_string());
        }
        Ok(ids)
    }

    /// Creates a backup by committing the current state of the repository.
    ///
    /// This method stages all changes, creates a commit with the given description, and returns the ID
    /// of the newly created commit. If no description is provided, a default description of "No description
    /// provided" is used. It also ensures proper handling for creating an initial commit if the repository
    /// does not have an existing HEAD.
    ///
    /// # Arguments
    ///
    /// * `description` - An optional string containing a description for the backup commit.
    ///
    /// # Returns
    ///
    /// Returns a `Result<String>` which contains:
    /// * On success: The ID of the newly created commit as a string.
    /// * On failure: An error indicating the cause of the failure.
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// * There is an issue accessing or writing the repository index.
    /// * There is an issue creating a new tree or finding the tree object in the repository.
    /// * The repository signature (user name and email) is invalid or not set.
    /// * The commit operation fails due to any Git-related error.
    ///
    /// # Logging
    ///
    /// This method emits the following log messages:
    /// * `info` logs for the overall operation (`Creating backup`, `Backup created successfully`).
    /// * `debug` logs for intermediate steps, such as getting the index, adding files, writing the tree, finding
    ///   parents, creating signatures, and creating the commit.
    ///
    /// # Example
    ///
    /// ```rust
    /// use obsidian_backup_system::BackupManager;
    ///
    /// let manager = BackupManager::new("./backup_store", "./my_data")
    ///     .expect("Failed to initialize BackupManager");
    ///
    /// let description = Some("Backup before deployment".to_string());
    /// match manager.backup(description) {
    ///     Ok(commit_id) => println!("Backup created with ID: {}", commit_id),
    ///     Err(e) => eprintln!("Failed to create backup: {}", e),
    /// }
    /// ```
    ///
    /// # Notes
    ///
    /// * This method assumes that the caller has already initialized the repository (`self.repository`) and has
    ///   proper permissions to write to it.
    /// * If no HEAD exists (e.g., for an empty repository), it creates an initial commit without parent commits.
    pub fn backup(&self, description: Option<String>) -> Result<String> {
        info!("Creating backup with description: {:?}", description);

        debug!("Getting repository index");
        let mut index = self.repository.index()?;

        // Get the working directory
        let workdir = self
            .repository
            .workdir()
            .ok_or_else(|| anyhow::anyhow!("Repository has no working directory"))?;

        debug!("Working directory: {:?}", workdir);

        // Clear the index first to handle deleted files
        debug!("Clearing index");
        index.clear()?;

        debug!("Adding all files from working directory to index");
        self.add_directory_to_index(&mut index, workdir, workdir)?;

        debug!("Writing index");
        index.write()?;

        debug!("Creating tree from index");
        let tree_id = index.write_tree()?;
        debug!("Tree created with ID: {}", tree_id);

        let tree = self.repository.find_tree(tree_id)?;
        let head = self.repository.head();

        // Create and own the parent_commit outside the if scope
        let parent_commit = if let Ok(head) = head {
            debug!("Found existing HEAD, using as parent commit");
            Some(head.peel_to_commit()?)
        } else {
            debug!("No existing HEAD found, creating initial commit");
            None
        };

        // Build the parent's vector using references to the owned commit
        let parents = match &parent_commit {
            Some(commit) => {
                debug!("Using parent commit: {}", commit.id());
                vec![commit]
            }
            None => {
                debug!("No parent commits");
                vec![]
            }
        };

        debug!("Getting repository signature");
        let sig = self.repository.signature()?;
        debug!(
            "Signature: {} <{}>",
            sig.name().unwrap_or("unknown"),
            sig.email().unwrap_or("unknown")
        );

        debug!("Creating commit");
        let commit_id = self.repository.commit(
            Some("HEAD"),
            &sig,
            &sig,
            description
                .unwrap_or("No description provided".to_string())
                .as_ref(),
            &tree,
            &parents,
        )?;

        info!("Backup created successfully with ID: {}", commit_id);
        Ok(commit_id.to_string())
    }

    /// Restores a backup by its ID and checks out the associated commit.
    ///
    /// # Arguments
    ///
    /// * `backup_id` - A reference to a string that uniquely identifies the backup.
    ///                 This ID is parsed as a git object ID.
    ///
    /// # Returns
    ///
    /// * `Result<()>` - Returns `Ok(())` if the backup was successfully restored,
    ///                  or an error if the operation fails at any stage.
    ///
    /// # Process
    ///
    /// 1. The backup ID is parsed as a git object ID (OID).
    /// 2. The associated git commit is retrieved using the OID.
    /// 3. The commit's tree is accessed, and its contents are checked out in the repository.
    /// 4. If the repository is configured with a working directory:
    ///    * The contents of the current working directory are removed.
    ///    * A new working directory is created.
    ///    * HEAD is checked out into the working directory.
    /// 5. Logs are generated at various points to provide insights into the restoration process.
    ///
    /// # Logs
    ///
    /// * **Info** logs are used to indicate the start and successful completion of the restore operation.
    /// * **Debug** logs provide detailed information about each step of the process, such as parsing the backup ID,
    ///   working with git objects, and modifying the working directory.
    /// * **Warning** logs occur if no working directory is configured for the repository.
    ///
    /// # Errors
    ///
    /// Returns an error if any of the following occurs:
    ///
    /// * The backup ID cannot be parsed as a valid git OID.
    /// * The associated commit cannot be found in the repository.
    /// * The commit's tree cannot be accessed.
    /// * Checking out the tree in the repository fails.
    /// * File system operations, such as removing or creating the working directory, encounter errors.
    ///
    /// # Example Usage
    ///
    /// ```no_run
    /// use obsidian_backup_system::BackupManager;
    ///
    /// let manager = BackupManager::new("./backup_store", "./my_data")
    ///     .expect("Failed to initialize BackupManager");
    ///
    /// let backup_id = "abcdef1234567890";
    /// if let Err(err) = manager.restore(backup_id) {
    ///     eprintln!("Failed to restore backup: {}", err);
    /// } else {
    ///     println!("Backup restored successfully!");
    /// }
    /// ```
    pub fn restore(&self, backup_id: impl AsRef<str>) -> Result<()> {
        let backup_id = backup_id.as_ref();
        info!("Restoring backup with ID: {}", backup_id);

        debug!("Parsing backup ID as git OID");
        let oid = Oid::from_str(backup_id)?;

        debug!("Finding commit for OID: {}", oid);
        let commit = self.repository.find_commit(oid)?;

        debug!("Getting tree from commit");
        let tree = commit.tree()?;
        debug!("Tree ID: {}", tree.id());

        if let Some(ref workdir) = self.repository.workdir() {
            debug!("Working directory found: {:?}", workdir);

            // Use safer restore approach with temporary directory
            let temp_dir = workdir
                .parent()
                .ok_or_else(|| {
                    anyhow::anyhow!("Cannot determine parent directory for working directory")
                })?
                .join(format!(
                    "{}_restore_tmp",
                    workdir
                        .file_name()
                        .and_then(|n| n.to_str())
                        .unwrap_or("workdir")
                ));

            debug!("Using temporary directory: {:?}", temp_dir);

            // Clean up temp directory if it exists from a previous failed restore
            if temp_dir.exists() {
                debug!("Cleaning up existing temporary directory");
                fs::remove_dir_all(&temp_dir)?;
            }

            // Create temp directory
            debug!("Creating temporary directory");
            fs::create_dir_all(&temp_dir)?;

            // Checkout to temp location
            debug!("Checking out tree to temporary directory");
            let mut checkout_opts = git2::build::CheckoutBuilder::new();
            checkout_opts.target_dir(&temp_dir);
            checkout_opts.force();
            checkout_opts.remove_untracked(true);
            self.repository
                .checkout_tree(tree.as_object(), Some(&mut checkout_opts))?;

            // At this point, the checkout succeeded. Now perform the swap.
            debug!("Checkout successful, swapping directories");

            // Create a backup of the old working directory
            let backup_dir = workdir
                .parent()
                .ok_or_else(|| {
                    anyhow::anyhow!("Cannot determine parent directory for working directory")
                })?
                .join(format!(
                    "{}_old_backup",
                    workdir
                        .file_name()
                        .and_then(|n| n.to_str())
                        .unwrap_or("workdir")
                ));

            // Clean up old backup if it exists
            if backup_dir.exists() {
                debug!("Cleaning up old backup directory");
                fs::remove_dir_all(&backup_dir)?;
            }

            // Move current workdir to backup location
            debug!("Moving current working directory to backup location");
            fs::rename(workdir, &backup_dir)?;

            // Move temp directory to workdir location
            debug!("Moving temporary directory to working directory location");
            match fs::rename(&temp_dir, workdir) {
                Ok(_) => {
                    debug!("Restore completed successfully, cleaning up old backup");
                    // Only remove the old backup if the restore succeeded
                    let _ = fs::remove_dir_all(&backup_dir);
                }
                Err(e) => {
                    // If rename fails, try to restore the original
                    error!("Failed to move temp directory: {}", e);
                    debug!("Attempting to restore original working directory");
                    if let Err(_restore_err) = fs::rename(&backup_dir, workdir) {
                        error!("Failed to restore original directory: {}", _restore_err);
                        return Err(anyhow::anyhow!(
                            "Restore failed and could not recover original directory. Original backed up at: {:?}",
                            backup_dir
                        ));
                    }
                    return Err(anyhow::anyhow!("Failed to complete restore: {}", e));
                }
            }
        } else {
            warn!("No working directory configured for repository");
            // For bare repositories, just update HEAD
            debug!("Checking out tree in bare repository");
            self.repository.checkout_tree(tree.as_object(), None)?;
        }

        info!("Backup restored successfully");
        Ok(())
    }

    /// Exports a backup identified by its ID into a compressed archive.
    ///
    /// This function retrieves a backup commit from the Git repository using the provided `backup_id`,
    /// packages its content into a compressed archive, and writes the result to the specified `output_path`.
    ///
    /// # Parameters
    ///
    /// * `backup_id` - A string-like identifier of the backup to export. This must correspond to a valid Git object ID (OID) in the repository.
    /// * `output_path` - The destination path for the created archive. This must be a valid filesystem path.
    /// * `level` - Compression level (0-9, clamped to this range). The value determines the trade-off between compression size and speed.
    ///
    /// # Returns
    ///
    /// * `Result<()>` - Returns `Ok(())` if the archive is successfully created, or an error if any step in the process fails.
    ///
    /// # Errors
    ///
    /// This function can fail for several reasons, including (but not limited to):
    ///
    /// 1. The provided `backup_id` is not a valid Git OID.
    /// 2. The backup commit or its associated tree cannot be found within the repository.
    /// 3. Issues encountered while creating the archive writer or writing to the output path.
    /// 4. Any errors arising from compression settings or file operations during the archive creation process.
    ///
    /// # Logging
    ///
    /// - Logs the progress of the backup export process at `info` and `debug` levels.
    /// - Logs errors if any step in the process fails.
    ///
    /// # Example
    ///
    /// ```rust
    /// use obsidian_backup_system::BackupManager;
    ///
    /// let manager = BackupManager::new("./backup_store", "./my_data")
    ///     .expect("Failed to initialize BackupManager");
    ///
    /// let last_backup = manager
    ///     .last()
    ///     .expect("Failed to get last backup")
    ///     .expect("No backups found");
    ///
    /// manager.export(&last_backup.id, "backup.7z", 5)
    ///     .expect("Failed to export backup");
    /// ```
    ///
    /// In this example, the specified backup ID is packed into a `.7z` archive
    /// with medium compression level (5) and saved to the given output path.
    #[cfg(feature = "zip")]
    pub fn export(
        &self,
        backup_id: impl AsRef<str>,
        output_path: impl AsRef<Path>,
        level: u8,
    ) -> Result<()> {
        // Validate and clamp compression level to 0-9 range
        let level = level.clamp(0, 9);

        let mut writer = ArchiveWriter::create(output_path)?;
        writer.set_content_methods(vec![
            encoder_options::Lzma2Options::from_level(level as u32).into(),
        ]);

        let backup_id = backup_id.as_ref();
        info!("Exporting backup with ID: {} to archive", backup_id);
        let oid = Oid::from_str(backup_id)?;
        let commit = self.repository.find_commit(oid)?;
        let tree = commit.tree()?;

        // Walk the tree recursively and add files to the archive
        self.add_tree_to_archive(&mut writer, &tree, "")?;

        debug!("Finalizing archive");
        writer.finish()?;

        info!("Archive created successfully");
        Ok(())
    }

    /// Exports a backup identified by its ID into a compressed archive stream.
    ///
    /// This function retrieves a backup commit from the Git repository using the provided `backup_id`,
    /// packages its content into a compressed archive, and writes the result to the provided writer stream.
    /// This is useful for scenarios where you want to stream the archive directly to an in-memory buffer,
    /// or any other seekable destination without creating an intermediate file.
    ///
    /// # Parameters
    ///
    /// * `backup_id` - A string-like identifier of the backup to export. This must correspond to a valid Git object ID (OID) in the repository.
    /// * `writer` - A writer implementing both `Write` and `Seek` where the archive will be written to. The 7z format requires seeking to write headers and metadata.
    /// * `level` - Compression level (0-9, clamped to this range). The value determines the trade-off between compression size and speed.
    ///
    /// # Returns
    ///
    /// * `Result<()>` - Returns `Ok(())` if the archive is successfully created and written to the stream, or an error if any step in the process fails.
    ///
    /// # Errors
    ///
    /// This function can fail for several reasons, including (but not limited to):
    ///
    /// 1. The provided `backup_id` is not a valid Git OID.
    /// 2. The backup commit or its associated tree cannot be found within the repository.
    /// 3. Issues encountered while creating the archive writer or writing to the output stream.
    /// 4. Any errors arising from compression settings or file operations during the archive creation process.
    ///
    /// # Logging
    ///
    /// - Logs the progress of the backup export process at `info` and `debug` levels.
    /// - Logs errors if any step in the process fails.
    ///
    /// # Example
    ///
    /// ```rust
    /// use obsidian_backup_system::BackupManager;
    /// use std::io::Cursor;
    ///
    /// let manager = BackupManager::new("./backup_store", "./my_data")
    ///     .expect("Failed to initialize BackupManager");
    ///
    /// let last_backup = manager
    ///     .last()
    ///     .expect("Failed to get last backup")
    ///     .expect("No backups found");
    ///
    /// // Export to an in-memory buffer
    /// let mut buffer = Cursor::new(Vec::new());
    /// manager.export_to_stream(&last_backup.id, &mut buffer, 5)
    ///     .expect("Failed to export backup to stream");
    ///
    /// let archive_bytes = buffer.into_inner();
    /// println!("Archive size: {} bytes", archive_bytes.len());
    /// ```
    ///
    /// In this example, the specified backup ID is packed into a `.7z` archive
    /// with medium compression level (5) and written to the provided stream.
    #[cfg(feature = "zip")]
    pub fn export_to_stream<W: std::io::Write + std::io::Seek>(
        &self,
        backup_id: impl AsRef<str>,
        writer: W,
        level: u8,
    ) -> Result<()> {
        // Validate and clamp compression level to 0-9 range
        let level = level.clamp(0, 9);

        let mut archive_writer = ArchiveWriter::new(writer)?;
        archive_writer.set_content_methods(vec![
            encoder_options::Lzma2Options::from_level(level as u32).into(),
        ]);

        let backup_id = backup_id.as_ref();
        info!("Exporting backup with ID: {} to stream", backup_id);
        let oid = Oid::from_str(backup_id)?;
        let commit = self.repository.find_commit(oid)?;
        let tree = commit.tree()?;

        // Walk the tree recursively and add files to the archive
        self.add_tree_to_archive(&mut archive_writer, &tree, "")?;

        debug!("Finalizing archive stream");
        archive_writer.finish()?;

        info!("Archive stream created successfully");
        Ok(())
    }

    /// Computes the list of files that were modified (added, updated, or deleted)
    /// in the specified backup/commit within the repository.
    ///
    /// # Arguments
    ///
    /// * `backup_id` - A string-like identifier for the backup or commit to compute
    ///                 the modified files against its parent commit. The function
    ///                 expects this to be in the format of a valid Git object ID.
    ///
    /// # Returns
    ///
    /// A `Result` containing:
    /// * `Ok(Vec<ModifiedFile>)` - A vector of `ModifiedFile` objects, each representing
    ///                             a file that was added, updated, or deleted. Each `ModifiedFile`
    ///                             includes:
    ///   - `path`: The path of the file.
    ///   - `content_before`: The file's content before modification (if applicable, `Some` if the file existed, otherwise `None`).
    ///   - `content_after`: The file's content after modification (if applicable, `Some` if the file exists, otherwise `None` for deletions).
    /// * `Err(git2::Error)` - In case of any error during Git repository or commit/tree operations.
    ///
    /// # Details
    ///
    /// * The function computes the difference between the specified commit/tree and its
    ///   immediate parent (if available). If there is no parent commit (e.g., for the first commit),
    ///   only the newly added files will appear in the output list.
    /// * For each file in the current tree:
    ///     - If a corresponding file exists in the parent tree, the function checks for modifications.
    ///     - If the file does not exist in the parent tree, it is marked as newly added.
    /// * For files that existed in the parent tree but are absent in the current tree,
    ///   the function marks them as deleted.
    ///
    /// # Errors
    ///
    /// This function can return an `Err` in the following situations:
    /// * If the provided `backup_id` is not a valid Git commit or tree object ID.
    /// * If the repository cannot find the commit or tree corresponding to `backup_id`.
    /// * If there are errors while retrieving or processing blobs within the trees.
    ///
    /// # Example
    ///
    /// ```rust
    /// use obsidian_backup_system::BackupManager;
    ///
    /// let manager = BackupManager::new("./backup_store", "./my_data")
    ///     .expect("Failed to initialize BackupManager");
    ///
    /// let backup_id = "abcd1234";
    /// let modified_files = manager.diff(backup_id)
    ///     .expect("Failed to get diff");
    ///
    /// for file in modified_files {
    ///     println!("Path: {}", file.path);
    ///     match (&file.content_before, &file.content_after) {
    ///         (Some(before), Some(after)) => {
    ///             println!("File was modified. Before size: {}, After size: {}", before.len(), after.len());
    ///         }
    ///         (None, Some(after)) => {
    ///             println!("File was added. Size: {}", after.len());
    ///         }
    ///         (Some(before), None) => {
    ///             println!("File was deleted. Previous size: {}", before.len());
    ///         }
    ///         _ => {}
    ///     }
    /// }
    /// ```
    ///
    /// # Structs Used
    ///
    /// * `ModifiedFile`: A struct representing a modified file, with the following fields:
    ///     - `path`: The file's path as a `String`.
    ///     - `content_before`: An optional `Vec<u8>` containing the file's content in the parent revision (if it existed).
    ///     - `content_after`: An optional `Vec<u8>` containing the file's content in the current revision (if it exists).
    ///
    /// # Note
    ///
    /// * This function assumes text or binary files are stored as blobs in the Git repository.
    /// * Files that are not blobs (e.g., submodules or symlinks) are ignored.
    pub fn diff(&self, backup_id: impl AsRef<str>) -> Result<Vec<ModifiedFile>> {
        let backup_id = backup_id.as_ref();
        let mut files = Vec::new();
        let oid = Oid::from_str(backup_id)?;
        let commit = self.repository.find_commit(oid)?;
        let tree = commit.tree()?;

        // Get the parent commit tree (if exists) to compare against
        let parent_tree = if commit.parent_count() > 0 {
            Some(commit.parent(0)?.tree()?)
        } else {
            None
        };

        // Recursively diff trees
        self.diff_trees_recursive(&tree, parent_tree.as_ref(), "", &mut files)?;

        Ok(files)
    }

    /// Helper method to recursively diff two trees
    fn diff_trees_recursive(
        &self,
        tree: &git2::Tree,
        parent_tree: Option<&git2::Tree>,
        path_prefix: &str,
        files: &mut Vec<ModifiedFile>,
    ) -> Result<()> {
        // Check files in current tree (for added/modified files)
        for entry in tree.iter() {
            let name = entry.name().unwrap_or("");
            let full_path = if path_prefix.is_empty() {
                name.to_string()
            } else {
                format!("{}/{}", path_prefix, name)
            };

            match entry.kind() {
                Some(git2::ObjectType::Blob) => {
                    // It's a file
                    let blob = self.repository.find_blob(entry.id())?;
                    let content_after = blob.content().to_vec();

                    // Try to get the content before from parent commit
                    let content_before = if let Some(parent_tree) = parent_tree {
                        parent_tree
                            .get_name(name)
                            .and_then(|parent_entry| {
                                if let Some(git2::ObjectType::Blob) = parent_entry.kind() {
                                    self.repository.find_blob(parent_entry.id()).ok()
                                } else {
                                    None
                                }
                            })
                            .map(|parent_blob| parent_blob.content().to_vec())
                    } else {
                        None
                    };

                    // Only add if file was added or modified
                    if let Some(before_content) = content_before {
                        // File existed before - check if it was modified
                        if before_content != content_after {
                            files.push(ModifiedFile {
                                path: full_path,
                                content_before: Some(before_content),
                                content_after: Some(content_after),
                            });
                        }
                        // If content is the same, don't add to results
                    } else {
                        // File was added
                        files.push(ModifiedFile {
                            path: full_path,
                            content_before: None,
                            content_after: Some(content_after),
                        });
                    }
                }
                Some(git2::ObjectType::Tree) => {
                    // It's a directory, recurse into it
                    let subtree = self.repository.find_tree(entry.id())?;
                    let parent_subtree =
                        parent_tree.and_then(|pt| pt.get_name(name)).and_then(|e| {
                            if let Some(git2::ObjectType::Tree) = e.kind() {
                                self.repository.find_tree(e.id()).ok()
                            } else {
                                None
                            }
                        });
                    self.diff_trees_recursive(
                        &subtree,
                        parent_subtree.as_ref(),
                        &full_path,
                        files,
                    )?;
                }
                _ => {
                    // Skip other object types
                }
            }
        }

        // Check for files/directories that were deleted (existed in parent but not in current)
        if let Some(parent_tree) = parent_tree {
            for parent_entry in parent_tree.iter() {
                let name = parent_entry.name().unwrap_or("");
                let full_path = if path_prefix.is_empty() {
                    name.to_string()
                } else {
                    format!("{}/{}", path_prefix, name)
                };

                // If this entry doesn't exist in the current tree, it was deleted
                if tree.get_name(name).is_none() {
                    match parent_entry.kind() {
                        Some(git2::ObjectType::Blob) => {
                            // File was deleted
                            let parent_blob = self.repository.find_blob(parent_entry.id())?;
                            let content_before = parent_blob.content().to_vec();

                            files.push(ModifiedFile {
                                path: full_path,
                                content_before: Some(content_before),
                                content_after: None,
                            });
                        }
                        Some(git2::ObjectType::Tree) => {
                            // Directory was deleted - recursively add all files as deleted
                            let parent_subtree = self.repository.find_tree(parent_entry.id())?;
                            self.diff_trees_recursive(
                                &parent_subtree,
                                Some(&parent_subtree),
                                &full_path,
                                &mut Vec::new(),
                            )?;
                            // Mark all files in the deleted directory
                            self.mark_tree_as_deleted(&parent_subtree, &full_path, files)?;
                        }
                        _ => {}
                    }
                }
            }
        }

        Ok(())
    }

    /// Helper method to mark all files in a tree as deleted
    fn mark_tree_as_deleted(
        &self,
        tree: &git2::Tree,
        path_prefix: &str,
        files: &mut Vec<ModifiedFile>,
    ) -> Result<()> {
        for entry in tree.iter() {
            let name = entry.name().unwrap_or("");
            let full_path = if path_prefix.is_empty() {
                name.to_string()
            } else {
                format!("{}/{}", path_prefix, name)
            };

            match entry.kind() {
                Some(git2::ObjectType::Blob) => {
                    let blob = self.repository.find_blob(entry.id())?;
                    files.push(ModifiedFile {
                        path: full_path,
                        content_before: Some(blob.content().to_vec()),
                        content_after: None,
                    });
                }
                Some(git2::ObjectType::Tree) => {
                    let subtree = self.repository.find_tree(entry.id())?;
                    self.mark_tree_as_deleted(&subtree, &full_path, files)?;
                }
                _ => {}
            }
        }
        Ok(())
    }
    pub fn last(&self) -> Result<Option<BackupItem>> {
        // Check if HEAD exists first
        if self.repository.head().is_err() {
            return Ok(None); // No backups yet
        }

        let mut rev_walk = self.repository.revwalk()?;
        rev_walk.push_head()?;
        if let Some(oid) = rev_walk.next() {
            let oid = oid?;
            let commit = self.repository.find_commit(oid)?;
            let item = BackupItem {
                id: oid.to_string(),
                timestamp: chrono::DateTime::from_timestamp_secs(commit.time().seconds())
                    .unwrap_or(chrono::DateTime::<chrono::Utc>::MIN_UTC),
                description: commit
                    .message()
                    .unwrap_or("No description was provided")
                    .to_string(),
            };
            Ok(Some(item))
        } else {
            Ok(None)
        }
    }

    #[cfg(feature = "zip")]
    fn add_tree_to_archive<W: std::io::Write + std::io::Seek>(
        &self,
        writer: &mut ArchiveWriter<W>,
        tree: &git2::Tree,
        path_prefix: &str,
    ) -> Result<()> {
        for entry in tree.iter() {
            let name = entry.name().unwrap_or("");
            let full_path = if path_prefix.is_empty() {
                name.to_string()
            } else {
                format!("{}/{}", path_prefix, name)
            };

            match entry.kind() {
                Some(git2::ObjectType::Blob) => {
                    // It's a file
                    debug!("Adding file to archive: {}", full_path);
                    let blob = self.repository.find_blob(entry.id())?;
                    let content = blob.content();

                    writer.push_archive_entry(
                        sevenz_rust2::ArchiveEntry::new_file(&full_path),
                        Some(content),
                    )?;
                }
                Some(git2::ObjectType::Tree) => {
                    // It's a directory, recurse into it
                    debug!("Entering directory: {}", full_path);
                    let subtree = self.repository.find_tree(entry.id())?;
                    self.add_tree_to_archive(writer, &subtree, &full_path)?;
                }
                _ => {
                    // Skip other object types (commits, tags, etc.)
                    debug!("Skipping object type: {:?} for {}", entry.kind(), full_path);
                }
            }
        }
        Ok(())
    }

    pub fn purge_backups_over_count(&self, count: usize) -> Result<()> {
        info!("Purging backups over count: {}", count);

        // Get all commit IDs
        let ids = self.list_ids()?;

        if ids.len() <= count {
            info!(
                "Number of backups ({}) is within limit ({})",
                ids.len(),
                count
            );
            return Ok(());
        }

        // Keep the most recent 'count' commits
        let commits_to_keep = &ids[..count];
        let oldest_commit_to_keep = &ids[count - 1];

        debug!("Keeping {} most recent commits", count);
        debug!("Oldest commit to keep: {}", oldest_commit_to_keep);

        // Get the tree of the oldest commit we want to keep
        let oldest_oid = Oid::from_str(oldest_commit_to_keep)?;
        let oldest_commit = self.repository.find_commit(oldest_oid)?;
        let oldest_tree = oldest_commit.tree()?;

        // Create a new initial commit with this tree
        let sig = self.repository.signature()?;
        let new_base_oid = self.repository.commit(
            None, // Don't update any reference yet
            &sig,
            &sig,
            &format!(
                "Consolidated backup prior to {}",
                oldest_commit.time().seconds()
            ),
            &oldest_tree,
            &[], // No parents - this becomes the new root
        )?;

        debug!("Created new base commit: {}", new_base_oid);

        // Now we need to rewrite the remaining commits to use this new base
        self.rewrite_commit_chain(&commits_to_keep[..commits_to_keep.len() - 1], new_base_oid)?;

        // Force garbage collection to remove unreferenced objects
        self.cleanup_orphaned_commits()?;

        info!("Successfully purged {} old backups", ids.len() - count);
        Ok(())
    }

    pub fn purge_backups_older_than(&self, period: chrono::Duration) -> Result<()> {
        info!("Purging backups older than {:?}", period);

        let now = chrono::Utc::now();
        let cutoff_time = now - period;
        let cutoff_timestamp = cutoff_time.timestamp();

        debug!("Cutoff timestamp: {}", cutoff_timestamp);

        // Get all commits
        let ids = self.list_ids()?;
        let mut commits_to_keep = Vec::new();
        let mut oldest_commit_to_delete = None;

        for commit_id in &ids {
            let oid = Oid::from_str(commit_id)?;
            let commit = self.repository.find_commit(oid)?;
            let commit_time = commit.time().seconds();

            if commit_time >= cutoff_timestamp {
                commits_to_keep.push(commit_id.clone());
            } else {
                // Track the oldest commit we're deleting (youngest of the ones to delete)
                if oldest_commit_to_delete.is_none() {
                    oldest_commit_to_delete = Some((commit_id.clone(), commit));
                }
            }
        }

        if commits_to_keep.len() == ids.len() {
            info!("No backups to purge");
            return Ok(());
        }

        if commits_to_keep.is_empty() {
            return Err(anyhow::anyhow!("Cannot purge all backups"));
        }

        // Create a consolidated base commit from the oldest commit to keep
        let oldest_to_keep = &commits_to_keep[commits_to_keep.len() - 1];
        let oldest_oid = Oid::from_str(oldest_to_keep)?;
        let oldest_commit = self.repository.find_commit(oldest_oid)?;
        let oldest_tree = oldest_commit.tree()?;

        let sig = self.repository.signature()?;
        let new_base_oid = self.repository.commit(
            None,
            &sig,
            &sig,
            &format!(
                "Consolidated backup prior to {}",
                chrono::DateTime::from_timestamp_secs(oldest_commit.time().seconds()).unwrap()
            ),
            &oldest_tree,
            &[],
        )?;

        debug!("Created new base commit: {}", new_base_oid);

        // Rewrite remaining commits
        if commits_to_keep.len() > 1 {
            self.rewrite_commit_chain(&commits_to_keep[..commits_to_keep.len() - 1], new_base_oid)?;
        } else {
            // Only one commit to keep, just update HEAD to the new base
            self.repository.reference(
                "refs/heads/master",
                new_base_oid,
                true,
                "Purged old backups",
            )?;
            self.repository.set_head("refs/heads/master")?;
        }

        self.cleanup_orphaned_commits()?;

        info!("Successfully purged backups older than {:?}", period);
        Ok(())
    }

    pub fn purge_backups_over_size(&self, size: usize) -> Result<()> {
        info!(
            "Purging backups to reduce repository size below {} bytes",
            size
        );

        // Get current repository size
        let repo_path = self.repository.path();
        let current_size = self.calculate_repo_size(repo_path)?;

        debug!("Current repository size: {} bytes", current_size);

        if current_size <= size {
            info!("Repository size is within limit");
            return Ok(());
        }

        // Strategy: Remove oldest commits one by one until size is acceptable
        let ids = self.list_ids()?;

        if ids.len() <= 1 {
            return Err(anyhow::anyhow!(
                "Cannot reduce size further without removing all backups"
            ));
        }

        // Binary search for the right number of commits to keep
        let mut keep_count = ids.len();

        while keep_count > 1 {
            keep_count /= 2;

            // Estimate if this would be enough by checking
            // We'll need to actually try purging to get accurate size
            debug!("Trying to keep {} commits", keep_count);

            // For now, just use purge_backups_over_count approach
            // In production, you might want a more sophisticated size estimation
            self.purge_backups_over_count(keep_count)?;

            let new_size = self.calculate_repo_size(repo_path)?;
            debug!("New repository size: {} bytes", new_size);

            if new_size <= size {
                info!("Successfully reduced repository size to {} bytes", new_size);
                return Ok(());
            }
        }

        Err(anyhow::anyhow!(
            "Could not reduce repository size below {} bytes",
            size
        ))
    }

    /// Helper function to rewrite a chain of commits with a new parent
    fn rewrite_commit_chain(&self, commit_ids: &[String], new_parent_oid: Oid) -> Result<()> {
        debug!("Rewriting commit chain with {} commits", commit_ids.len());

        let mut current_parent = new_parent_oid;
        let mut new_head = None;

        // Iterate through commits from oldest to newest (reverse order)
        for commit_id in commit_ids.iter().rev() {
            let old_oid = Oid::from_str(commit_id)?;
            let old_commit = self.repository.find_commit(old_oid)?;

            debug!("Rewriting commit: {}", commit_id);

            let parent_commit = self.repository.find_commit(current_parent)?;

            // Create new commit with same tree but new parent
            let new_oid = self.repository.commit(
                None,
                &old_commit.author(),
                &old_commit.committer(),
                old_commit.message().unwrap_or("No description provided"),
                &old_commit.tree()?,
                &[&parent_commit],
            )?;

            debug!("Created new commit: {} (was: {})", new_oid, old_oid);

            current_parent = new_oid;
            new_head = Some(new_oid);
        }

        // Update HEAD to point to the new chain
        if let Some(head_oid) = new_head {
            debug!("Updating HEAD to: {}", head_oid);
            self.repository.reference(
                "refs/heads/master",
                head_oid,
                true,
                "Restructured commit history",
            )?;
            self.repository.set_head("refs/heads/master")?;
        }

        Ok(())
    }

    /// Clean up orphaned commits and run garbage collection
    ///
    /// This implements a standalone garbage collection mechanism that:
    /// 1. Expires reflog entries
    /// 2. Identifies all reachable objects from refs
    /// 3. Removes unreachable loose objects
    /// 4. Packs remaining loose objects into packfiles
    fn cleanup_orphaned_commits(&self) -> Result<()> {
        info!("Starting comprehensive garbage collection");

        // Step 1: Expire reflog entries immediately
        debug!("Expiring reflog entries");
        self.expire_reflogs()?;

        // Step 2: Collect all reachable objects
        debug!("Identifying reachable objects");
        let reachable_oids = self.find_reachable_objects()?;
        info!("Found {} reachable objects", reachable_oids.len());

        // Step 3: Remove unreachable loose objects
        debug!("Pruning unreachable objects");
        let _pruned_count = self.prune_unreachable_objects(&reachable_oids)?;
        info!("Pruned {} unreachable objects", _pruned_count);

        // Step 4: Pack loose objects
        debug!("Packing loose objects");
        let _packed_count = self.pack_loose_objects()?;
        info!("Packed {} loose objects", _packed_count);

        // Step 5: Pack references
        debug!("Packing references");
        self.pack_references()?;

        info!("Garbage collection completed successfully");
        Ok(())
    }

    /// Expire all reflog entries
    fn expire_reflogs(&self) -> Result<()> {
        let reflog_refs = vec!["HEAD", "refs/heads/master"];

        for ref_name in reflog_refs {
            if let Ok(mut reflog) = self.repository.reflog(ref_name) {
                // Clear all reflog entries
                while !reflog.is_empty() {
                    reflog.remove(0, false)?;
                }
                reflog.write()?;
            }
        }

        Ok(())
    }

    /// Find all objects reachable from current refs
    fn find_reachable_objects(&self) -> Result<std::collections::HashSet<Oid>> {
        use std::collections::{HashSet, VecDeque};

        let mut reachable = HashSet::new();
        let mut to_visit = VecDeque::new();

        // Start from all references
        for reference in self.repository.references()? {
            let reference = reference?;
            if let Some(oid) = reference.target() {
                to_visit.push_back(oid);
                reachable.insert(oid);
            }
        }

        // Also include HEAD if it exists
        if let Ok(head) = self.repository.head()
            && let Some(oid) = head.target()
        {
            to_visit.push_back(oid);
            reachable.insert(oid);
        }

        // Traverse the object graph
        while let Some(oid) = to_visit.pop_front() {
            // Try to read the object and find its dependencies
            if let Ok(obj) = self.repository.find_object(oid, None) {
                match obj.kind() {
                    Some(git2::ObjectType::Commit) => {
                        if let Some(commit) = obj.as_commit() {
                            // Add the tree
                            let tree_oid = commit.tree_id();
                            if reachable.insert(tree_oid) {
                                to_visit.push_back(tree_oid);
                            }

                            // Add all parents
                            for parent in commit.parents() {
                                let parent_oid = parent.id();
                                if reachable.insert(parent_oid) {
                                    to_visit.push_back(parent_oid);
                                }
                            }
                        }
                    }
                    Some(git2::ObjectType::Tree) => {
                        if let Some(tree) = obj.as_tree() {
                            for entry in tree.iter() {
                                let entry_oid = entry.id();
                                if reachable.insert(entry_oid) {
                                    to_visit.push_back(entry_oid);
                                }
                            }
                        }
                    }
                    Some(git2::ObjectType::Tag) => {
                        if let Some(tag) = obj.as_tag() {
                            let target_id = tag.target_id();
                            if reachable.insert(target_id) {
                                to_visit.push_back(target_id);
                            }
                        }
                    }
                    _ => {
                        // Blobs have no dependencies
                    }
                }
            }
        }

        Ok(reachable)
    }

    /// Remove unreachable loose objects from the object database
    fn prune_unreachable_objects(
        &self,
        reachable_oids: &std::collections::HashSet<Oid>,
    ) -> Result<usize> {
        let objects_dir = self.repository.path().join("objects");
        let mut pruned_count = 0;

        // Iterate through loose object directories (00-ff)
        for i in 0..256 {
            let dir_name = format!("{:02x}", i);
            let dir_path = objects_dir.join(&dir_name);

            if !dir_path.exists() {
                continue;
            }

            for entry in fs::read_dir(&dir_path)? {
                let entry = entry?;
                let file_name = entry.file_name();
                let file_name_str = file_name.to_string_lossy();

                // Skip pack and idx files
                if file_name_str == "pack" || file_name_str == "info" {
                    continue;
                }

                // Construct the full OID from directory and filename
                let oid_str = format!("{}{}", dir_name, file_name_str);

                if let Ok(oid) = Oid::from_str(&oid_str) {
                    // If this object is not reachable, delete it
                    if !reachable_oids.contains(&oid) {
                        let file_path = entry.path();
                        debug!("Pruning unreachable object: {}", oid);
                        fs::remove_file(&file_path)?;
                        pruned_count += 1;

                        // Remove directory if it's now empty
                        if let Ok(mut entries) = fs::read_dir(&dir_path)
                            && entries.next().is_none()
                        {
                            let _ = fs::remove_dir(&dir_path);
                        }
                    }
                }
            }
        }

        Ok(pruned_count)
    }

    /// Pack loose objects into packfiles
    fn pack_loose_objects(&self) -> Result<usize> {
        let objects_dir = self.repository.path().join("objects");
        let mut loose_oids = Vec::new();

        // Collect all loose object OIDs
        for i in 0..256 {
            let dir_name = format!("{:02x}", i);
            let dir_path = objects_dir.join(&dir_name);

            if !dir_path.exists() {
                continue;
            }

            for entry in fs::read_dir(&dir_path)? {
                let entry = entry?;
                let file_name = entry.file_name();
                let file_name_str = file_name.to_string_lossy();

                // Skip pack and idx files
                if file_name_str == "pack" || file_name_str == "info" {
                    continue;
                }

                // Construct the full OID
                let oid_str = format!("{}{}", dir_name, file_name_str);
                if let Ok(oid) = Oid::from_str(&oid_str) {
                    loose_oids.push(oid);
                }
            }
        }

        let loose_count = loose_oids.len();

        if loose_oids.is_empty() {
            debug!("No loose objects to pack");
            return Ok(0);
        }

        debug!("Packing {} loose objects", loose_count);

        // Create a packbuilder
        let mut packbuilder = self.repository.packbuilder()?;

        // Add all loose objects to the pack
        for oid in &loose_oids {
            if let Err(_e) = packbuilder.insert_object(*oid, None) {
                debug!("Failed to insert object {} into pack: {}", oid, _e);
                // Continue with other objects
            }
        }

        // Write the packfile
        let pack_dir = objects_dir.join("pack");
        fs::create_dir_all(&pack_dir)?;

        // Generate a unique pack name based on timestamp
        let timestamp = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)?
            .as_secs();
        let pack_path = pack_dir.join(format!("pack-{:x}.pack", timestamp));

        debug!("Writing packfile to: {:?}", pack_path);
        let mut buf = git2::Buf::new();
        packbuilder.write_buf(&mut buf)?;
        fs::write::<&std::path::PathBuf, &[u8]>(&pack_path, buf.as_ref())?;

        // After successful packing, remove the loose objects
        for oid in &loose_oids {
            let oid_str = oid.to_string();
            let dir_name = &oid_str[..2];
            let file_name = &oid_str[2..];
            let file_path = objects_dir.join(dir_name).join(file_name);

            if file_path.exists() {
                let _ = fs::remove_file(&file_path);
            }
        }

        // Clean up empty directories
        for i in 0..256 {
            let dir_name = format!("{:02x}", i);
            let dir_path = objects_dir.join(&dir_name);

            if dir_path.exists()
                && let Ok(mut entries) = fs::read_dir(&dir_path)
                && entries.next().is_none()
            {
                let _ = fs::remove_dir(&dir_path);
            }
        }

        Ok(loose_count)
    }

    /// Pack references into packed-refs file
    fn pack_references(&self) -> Result<()> {
        // Get all references
        let mut refs_to_pack = Vec::new();

        for reference in self.repository.references()? {
            let reference = reference?;
            let name = reference.name().unwrap_or("");

            // Only pack refs under refs/ (not HEAD or other special refs)
            if name.starts_with("refs/")
                && let Some(target) = reference.target()
            {
                refs_to_pack.push((name.to_string(), target));
            }
        }

        if refs_to_pack.is_empty() {
            debug!("No references to pack");
            return Ok(());
        }

        // Write packed-refs file
        let packed_refs_path = self.repository.path().join("packed-refs");
        let mut content = String::from("# pack-refs with: peeled fully-peeled sorted\n");

        for (name, oid) in &refs_to_pack {
            content.push_str(&format!("{} {}\n", oid, name));
        }

        fs::write(&packed_refs_path, content)?;

        // Remove individual ref files
        for (name, _) in &refs_to_pack {
            let ref_path = self.repository.path().join(name);
            if ref_path.exists() {
                let _ = fs::remove_file(&ref_path);
            }
        }

        debug!("Packed {} references", refs_to_pack.len());
        Ok(())
    }

    /// Calculate the total size of the repository
    fn calculate_repo_size(&self, repo_path: &Path) -> Result<usize> {
        let mut total_size = 0;

        fn visit_dirs(dir: &Path, total: &mut usize) -> Result<()> {
            if dir.is_dir() {
                for entry in fs::read_dir(dir)? {
                    let entry = entry?;
                    let path = entry.path();
                    if path.is_dir() {
                        visit_dirs(&path, total)?;
                    } else {
                        *total += fs::metadata(&path)?.len() as usize;
                    }
                }
            }
            Ok(())
        }

        visit_dirs(repo_path, &mut total_size)?;
        Ok(total_size)
    }

    /// Exports a backup identified by its ID into a compressed ZIP archive stream (async).
    ///
    /// This function retrieves a backup commit from the Git repository using the provided `backup_id`,
    /// packages its content into a compressed ZIP archive, and streams the result to the provided async writer.
    /// This is designed for use with async I/O systems like Tokio, enabling efficient streaming of large
    /// backups without loading them entirely into memory.
    ///
    /// # Parameters
    ///
    /// * `backup_id` - A string-like identifier of the backup to export. This must correspond to a valid Git object ID (OID) in the repository.
    /// * `writer` - An async writer implementing `AsyncWrite` where the ZIP archive will be streamed to.
    /// * `level` - Compression level (0-9, clamped to this range). The value determines the trade-off between compression size and speed.
    ///
    /// # Returns
    ///
    /// * `Result<()>` - Returns `Ok(())` if the archive is successfully created and streamed, or an error if any step in the process fails.
    ///
    /// # Errors
    ///
    /// This function can fail for several reasons, including (but not limited to):
    ///
    /// 1. The provided `backup_id` is not a valid Git OID.
    /// 2. The backup commit or its associated tree cannot be found within the repository.
    /// 3. Issues encountered while creating the archive writer or writing to the output stream.
    /// 4. Any errors arising from compression settings or file operations during the archive creation process.
    ///
    /// # Logging
    ///
    /// - Logs the progress of the backup export process at `info` and `debug` levels.
    /// - Logs errors if any step in the process fails.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use obsidian_backups::BackupManager;
    /// use tokio::fs::File;
    ///
    /// let manager = BackupManager::new("./backup_store", "./my_data")
    ///     .expect("Failed to initialize BackupManager");
    ///
    /// let last_backup = manager
    ///     .last()
    ///     .expect("Failed to get last backup")
    ///     .expect("No backups found");
    ///
    /// // Export to an async file
    /// let mut file = File::create("backup.zip").await.expect("Failed to create file");
    /// manager.export_to_stream_async(&last_backup.id, &mut file, 6).await
    ///     .expect("Failed to export backup to stream");
    /// ```
    ///
    /// In this example, the specified backup ID is packed into a ZIP archive
    /// with compression level 6 and streamed to the provided async writer.
    #[cfg(feature = "async-stream")]
    pub async fn export_to_stream_async<W: tokio::io::AsyncWrite + Unpin + Send>(
        &self,
        backup_id: impl AsRef<str>,
        writer: W,
        level: u8,
    ) -> Result<()> {
        use archflow::compress::tokio::archive::ZipArchive;

        // Validate and clamp compression level to 0-9 range
        let level = level.clamp(0, 9);

        let backup_id = backup_id.as_ref();
        info!("Exporting backup with ID: {} to async stream", backup_id);

        let oid = git2::Oid::from_str(backup_id)?;
        let commit = self.repository.find_commit(oid)?;
        let tree = commit.tree()?;

        // Create ZIP archive with streaming support
        let mut archive = ZipArchive::new_streamable(writer);

        // Set compression method
        let compression_type: archflow::compression::CompressionMethod = match level {
            0 => archflow::compression::CompressionMethod::Store(),
            _ => archflow::compression::CompressionMethod::Deflate(),
        };

        // Walk the tree recursively and add files to the archive
        self.add_tree_to_zip_archive_async(&mut archive, &tree, "", compression_type)
            .await?;

        debug!("Finalizing archive stream");
        archive.finalize().await.map_err(|e| anyhow!("Failed to finalize archive: {}", e))?;

        info!("Archive stream created successfully");
        Ok(())
    }

    /// Helper method to recursively add files from a git tree to a ZIP archive (async)
    #[cfg(feature = "async-stream")]
    async fn add_tree_to_zip_archive_async<W: tokio::io::AsyncWrite + Unpin + Send>(
        &self,
        archive: &mut archflow::compress::tokio::archive::ZipArchive<'_, W>,
        tree: &git2::Tree<'_>,
        path_prefix: &str,
        compress_method: archflow::compression::CompressionMethod,
    ) -> Result<()> {
        use archflow::compress::FileOptions;

        for entry in tree.iter() {
            let name = entry.name().unwrap_or("");
            let full_path = if path_prefix.is_empty() {
                name.to_string()
            } else {
                format!("{}/{}", path_prefix, name)
            };

            match entry.kind() {
                Some(git2::ObjectType::Blob) => {
                    // It's a file
                    debug!("Adding file to archive: {}", full_path);
                    let blob = self.repository.find_blob(entry.id())?;
                    let content = blob.content();

                    // Create file options with compression method
                    let options = FileOptions::default().compression_method(compress_method);

                    // Create a cursor for the content
                    let mut cursor = std::io::Cursor::new(content);

                    // Append file to the archive
                    archive
                        .append(&full_path, &options, &mut cursor)
                        .await
                        .map_err(|e| anyhow!("Failed to append file to archive: {}", e))?;
                }
                Some(git2::ObjectType::Tree) => {
                    // It's a directory, recurse into it
                    debug!("Entering directory: {}", full_path);
                    let subtree = self.repository.find_tree(entry.id())?;
                    Box::pin(self.add_tree_to_zip_archive_async(
                        archive,
                        &subtree,
                        &full_path,
                        compress_method,
                    ))
                    .await?;
                }
                _ => {
                    // Skip other object types (commits, tags, etc.)
                    debug!("Skipping object type: {:?} for {}", entry.kind(), full_path);
                }
            }
        }
        Ok(())
    }
}