liboxen 0.49.1

Oxen is a fast, unstructured data version control, to help version large machine learning datasets written in Rust.
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
use std::cmp::Ordering;
use std::collections::{BinaryHeap, HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::str;

use glob::Pattern;
use rocksdb::{DBWithThreadMode, MultiThreaded, SingleThreaded};
use time::OffsetDateTime;

use crate::config::UserConfig;
use crate::constants::COMMIT_COUNT_DIR;
use crate::core::db::key_val::{opts, str_val_db};
use crate::core::db::merkle_node::MerkleNodeDB;
use crate::core::refs::with_ref_manager;
use crate::core::v_latest::index::CommitMerkleTree;
use crate::error::OxenError;
use crate::model::merkle_tree::node::commit_node::CommitNodeOpts;
use crate::model::merkle_tree::node::dir_node::DirNodeOpts;
use crate::model::merkle_tree::node::{CommitNode, DirNode, EMerkleTreeNode};
use crate::model::{Commit, LocalRepository, MerkleHash, User};
use crate::opts::PaginateOpts;
use crate::repositories::commits::commit_writer;
use crate::view::{PaginatedCommits, StatusMessage};
use crate::{repositories, util};

/// Configuration for commit traversal operations
struct CommitTraversalConfig<'a> {
    /// Repository to traverse
    repo: &'a LocalRepository,
    /// Starting commit for traversal
    head_commit: Commit,
    /// Optional base commit to stop at (exclusive)
    stop_at_base: Option<&'a Commit>,
    /// Set of visited commit IDs to avoid cycles
    visited: &'a mut HashSet<String>,
    /// Number of commits to skip (for pagination)
    skip: usize,
    /// Maximum number of commits to collect (for pagination)
    limit: usize,
    /// Optional cache database for count lookups
    cache_db: Option<&'a DBWithThreadMode<MultiThreaded>>,
    /// Known total count for early exit optimization
    known_total_count: Option<usize>,
}

pub fn commit(repo: &LocalRepository, message: impl AsRef<str>) -> Result<Commit, OxenError> {
    commit_writer::commit(repo, message)
}

pub fn commit_with_user(
    repo: &LocalRepository,
    message: impl AsRef<str>,
    user: &User,
) -> Result<Commit, OxenError> {
    commit_writer::commit_with_cfg(
        repo,
        message,
        &UserConfig {
            name: user.name.clone(),
            email: user.email.clone(),
            editor: None,
        },
        None,
        &commit_writer::default_commit_progress_bar(),
    )
}

pub async fn commit_allow_empty(
    repo: &LocalRepository,
    message: impl AsRef<str>,
) -> Result<Commit, OxenError> {
    let message = message.as_ref();

    // Check if there are staged changes
    let status = crate::core::v_latest::status::status(repo).await?;
    let has_changes = !status.staged_files.is_empty() || !status.staged_dirs.is_empty();

    if has_changes {
        // If there are changes, commit normally
        commit_writer::commit(repo, message)
    } else {
        // No changes, create an empty commit
        let cfg = crate::config::UserConfig::get()?;
        let branch = repositories::branches::current_branch(repo)?
            .ok_or_else(|| OxenError::basic_str("No current branch found"))?;

        let head_commit = head_commit(repo)?;

        // Create a new commit with the same tree as parent
        let timestamp = OffsetDateTime::now_utc();
        let new_commit_data = crate::model::NewCommit {
            parent_ids: vec![head_commit.id.clone()],
            message: message.to_string(),
            author: cfg.name.clone(),
            email: cfg.email.clone(),
            timestamp,
        };

        // Compute the commit hash
        let commit_hash = commit_writer::compute_commit_id(&new_commit_data)?;

        let new_commit = Commit::from_new_and_id(&new_commit_data, commit_hash.to_string());

        // Use the existing create_empty_commit function
        let result = create_empty_commit(repo, &branch.name, &new_commit)?;

        println!("🐂 commit {result} (empty)");

        Ok(result)
    }
}

pub fn get_commit_or_head<S: AsRef<str> + Clone>(
    repo: &LocalRepository,
    commit_id_or_branch_name: Option<S>,
) -> Result<Commit, OxenError> {
    match commit_id_or_branch_name {
        Some(ref_name) => {
            log::debug!("get_commit_or_head: ref_name: {:?}", ref_name.as_ref());
            get_commit_by_ref(repo, ref_name)
        }
        None => {
            log::debug!("get_commit_or_head: calling head_commit");
            head_commit(repo)
        }
    }
}

fn get_commit_by_ref<S: AsRef<str> + Clone>(
    repo: &LocalRepository,
    ref_name: S,
) -> Result<Commit, OxenError> {
    get_by_id(repo, ref_name.clone())?
        .or_else(|| get_commit_by_branch(repo, ref_name.as_ref()))
        .ok_or_else(|| OxenError::basic_str("Commit not found"))
}

fn get_commit_by_branch(repo: &LocalRepository, branch_name: &str) -> Option<Commit> {
    repositories::branches::get_by_name(repo, branch_name)
        .ok()
        .and_then(|branch| get_by_id(repo, &branch.commit_id).ok().flatten())
}

pub fn latest_commit(repo: &LocalRepository) -> Result<Commit, OxenError> {
    let branches = with_ref_manager(repo, |manager| manager.list_branches())?;
    let mut latest_commit: Option<Commit> = None;
    for branch in branches {
        let commit = get_by_id(repo, &branch.commit_id)?;
        if let Some(commit) = commit
            && (latest_commit.is_none()
                || commit.timestamp < latest_commit.as_ref().unwrap().timestamp)
        {
            latest_commit = Some(commit);
        }
    }
    latest_commit.ok_or(OxenError::NoCommitsFound)
}

fn head_commit_id(repo: &LocalRepository) -> Result<MerkleHash, OxenError> {
    let commit_id = with_ref_manager(repo, |manager| manager.head_commit_id())?;
    match commit_id {
        Some(commit_id) => Ok(commit_id.parse()?),
        None => Err(OxenError::HeadNotFound),
    }
}

pub fn head_commit_maybe(repo: &LocalRepository) -> Result<Option<Commit>, OxenError> {
    let commit_id = with_ref_manager(repo, |manager| manager.head_commit_id())?;
    match commit_id {
        Some(commit_id) => {
            let commit_id = commit_id.parse()?;
            get_by_hash(repo, &commit_id)
        }
        None => Ok(None),
    }
}

pub fn head_commit(repo: &LocalRepository) -> Result<Commit, OxenError> {
    let head_commit_id = head_commit_id(repo)?;
    log::debug!("head_commit: head_commit_id: {head_commit_id:?}");

    let node = repositories::tree::get_node_by_id(repo, &head_commit_id)?.ok_or_else(|| {
        OxenError::basic_str(format!(
            "Merkle tree node not found for head commit: '{head_commit_id}'"
        ))
    })?;
    let commit = node.commit()?;
    Ok(commit.to_commit())
}

/// Get the root commit of the repository or None
pub fn root_commit_maybe(repo: &LocalRepository) -> Result<Option<Commit>, OxenError> {
    // Try to get a branch ref and follow it to the root
    // We only need to look at one ref as all branches will have the same root
    let branches = with_ref_manager(repo, |manager| manager.list_branches())?;

    if let Some(branch) = branches.first()
        && let Some(commit) = get_by_id(repo, &branch.commit_id)?
    {
        let mut seen = HashSet::new();
        let root_commit = root_commit_recursive(repo, commit.id.parse()?, &mut seen)?;
        return Ok(Some(root_commit));
    }
    log::debug!("root_commit_maybe: no root commit found");
    Ok(None)
}

fn root_commit_recursive(
    repo: &LocalRepository,
    commit_id: MerkleHash,
    seen: &mut HashSet<String>,
) -> Result<Commit, OxenError> {
    let mut current_id = commit_id;

    loop {
        // Check if we've already seen this commit
        if !seen.insert(current_id.to_string()) {
            return Err(OxenError::basic_str("Cycle detected in commit history"));
        }

        if let Some(commit) = get_by_hash(repo, &current_id)? {
            if commit.parent_ids.is_empty() {
                return Ok(commit);
            }

            // Only need to check the first parent, as all paths lead to the root
            if let Some(parent_id) = commit.parent_ids.first() {
                current_id = parent_id.parse()?;
                continue;
            }
        }

        return Err(OxenError::basic_str("No root commit found"));
    }
}

pub fn get_by_id(
    repo: &LocalRepository,
    commit_id_str: impl AsRef<str>,
) -> Result<Option<Commit>, OxenError> {
    let commit_id_str = commit_id_str.as_ref();
    let Ok(commit_id) = commit_id_str.parse() else {
        // log::debug!(
        //     "get_by_id could not create commit_id from [{}]",
        //     commit_id_str
        // );
        return Ok(None);
    };
    get_by_hash(repo, &commit_id)
}

pub fn get_by_hash(repo: &LocalRepository, hash: &MerkleHash) -> Result<Option<Commit>, OxenError> {
    let Some(node) = repositories::tree::get_node_by_id(repo, hash)? else {
        return Ok(None);
    };
    let commit = node.commit()?;
    Ok(Some(commit.to_commit()))
}

pub fn create_empty_commit(
    repo: &LocalRepository,
    branch_name: impl AsRef<str>,
    new_commit: &Commit,
) -> Result<Commit, OxenError> {
    let branch_name = branch_name.as_ref();
    let Some(existing_commit) = repositories::revisions::get(repo, branch_name)? else {
        return Err(OxenError::RevisionNotFound(branch_name.into()));
    };
    let existing_commit_id = existing_commit.id.parse()?;
    let existing_node =
        repositories::tree::get_node_by_id_with_children(repo, &existing_commit_id)?.ok_or_else(
            || {
                OxenError::basic_str(format!(
                    "Merkle tree node not found for commit: '{}'",
                    existing_commit.id
                ))
            },
        )?;
    let timestamp = OffsetDateTime::now_utc();
    let commit_node = CommitNode::new(
        repo,
        CommitNodeOpts {
            hash: new_commit.id.parse()?,
            parent_ids: vec![existing_commit_id],
            email: new_commit.email.clone(),
            author: new_commit.author.clone(),
            message: new_commit.message.clone(),
            timestamp,
        },
    )?;

    let parent_id = Some(existing_node.hash);
    let mut commit_db = MerkleNodeDB::open_read_write(&repo.path, &commit_node, parent_id)?;
    // There should always be one child, the root directory
    let dir_node = existing_node.children.first().unwrap().dir()?;
    commit_db.add_child(&dir_node)?;

    // Copy the dir hashes db to the new commit
    repositories::tree::cp_dir_hashes_to(repo, &existing_commit_id, commit_node.hash())?;

    // Update the ref
    with_ref_manager(repo, |manager| {
        manager.set_branch_commit_id(branch_name, commit_node.hash().to_string())
    })?;

    Ok(commit_node.to_commit())
}

/// Create an initial empty commit for an empty repository.
/// This creates the first commit with an empty tree and sets up the branch.
/// Returns an error if the repository already has commits.
pub fn create_initial_commit(
    repo: &LocalRepository,
    branch_name: impl AsRef<str>,
    user: &User,
    message: impl AsRef<str>,
) -> Result<Commit, OxenError> {
    let branch_name = branch_name.as_ref();
    let message = message.as_ref();

    // Ensure the repository is actually empty
    if head_commit_maybe(repo)?.is_some() {
        return Err(OxenError::basic_str(
            "Cannot create initial commit: repository already has commits",
        ));
    }

    let timestamp = OffsetDateTime::now_utc();

    // Create commit data with no parents
    let new_commit = crate::model::NewCommit {
        parent_ids: vec![],
        message: message.to_string(),
        author: user.name.clone(),
        email: user.email.clone(),
        timestamp,
    };

    // Compute the commit hash
    let commit_id = commit_writer::compute_commit_id(&new_commit)?;

    // Create the commit node
    let commit_node = CommitNode::new(
        repo,
        CommitNodeOpts {
            hash: commit_id,
            parent_ids: vec![],
            email: user.email.clone(),
            author: user.name.clone(),
            message: message.to_string(),
            timestamp,
        },
    )?;

    // Create an empty root directory node
    let empty_dir_hash = MerkleHash::new(0); // Empty hash for empty directory
    let dir_node = DirNode::new(
        repo,
        DirNodeOpts {
            name: String::new(), // Root directory has empty name
            hash: empty_dir_hash,
            num_entries: 0,
            num_bytes: 0,
            last_commit_id: commit_id,
            last_modified_seconds: timestamp.unix_timestamp(),
            last_modified_nanoseconds: timestamp.nanosecond(),
            data_type_counts: HashMap::new(),
            data_type_sizes: HashMap::new(),
        },
    )?;

    // Open the commit database and add the root directory
    let mut commit_db = MerkleNodeDB::open_read_write(&repo.path, &commit_node, None)?;
    commit_db.add_child(&dir_node)?;

    // Initialize the dir_hash_db with the root directory hash
    let commit_id_string = commit_id.to_string();
    let dir_hash_db_path =
        CommitMerkleTree::dir_hash_db_path_from_commit_id(repo, &commit_id_string);
    let dir_hash_db: DBWithThreadMode<SingleThreaded> =
        DBWithThreadMode::open(&opts::default(), dunce::simplified(&dir_hash_db_path))?;
    str_val_db::put(&dir_hash_db, "", &dir_node.hash().to_string())?;

    // Create the branch pointing to this commit
    with_ref_manager(repo, |manager| {
        manager.create_branch(branch_name, commit_id.to_string())
    })?;

    // Set HEAD to the new branch
    with_ref_manager(repo, |manager| {
        manager.set_head(branch_name);
        Ok(())
    })?;

    Ok(commit_node.to_commit())
}

/// List commits on the current branch from HEAD
pub fn list(repo: &LocalRepository) -> Result<Vec<Commit>, OxenError> {
    if let Some(commit) = head_commit_maybe(repo)? {
        let (results, _) = list_recursive_paginated(repo, commit, 0, usize::MAX, None, None)?;
        Ok(results)
    } else {
        Ok(vec![])
    }
}

fn list_forward_paginated(
    repo: &LocalRepository,
    head_commit: Commit,
    skip: usize,
    limit: usize,
) -> Result<Vec<Commit>, OxenError> {
    let mut results = Vec::new();
    let mut current = Some(head_commit);
    let mut count = 0;
    let end_idx = skip + limit;

    while let Some(commit) = current {
        if count >= skip && count < end_idx {
            results.push(commit.clone());
        }
        count += 1;

        if count >= end_idx {
            break;
        }

        current = if let Some(parent_id) = commit.parent_ids.first() {
            let parent_id: MerkleHash = parent_id.parse()?;
            get_by_hash(repo, &parent_id)?
        } else {
            None
        };
    }

    Ok(results)
}

pub fn list_recursive_paginated(
    repo: &LocalRepository,
    head_commit: Commit,
    skip: usize,
    limit: usize,
    stop_at_base: Option<&Commit>,
    known_total_count: Option<usize>,
) -> Result<(Vec<Commit>, usize), OxenError> {
    let mut results = vec![];
    let mut visited = HashSet::new();

    let config = CommitTraversalConfig {
        repo,
        head_commit,
        stop_at_base,
        visited: &mut visited,
        skip,
        limit,
        cache_db: None,
        known_total_count,
    };

    let total_count = traverse_commits(config, Some(&mut results))?;
    Ok((results, total_count))
}

/// Mark all ancestors of `commit` as visited and return the number of
/// newly-visited ancestors.  The commit itself is assumed to already be in the
/// visited set (the caller inserts it), so we start from its parents.
fn mark_ancestors_visited(
    repo: &LocalRepository,
    commit: &Commit,
    visited: &mut HashSet<String>,
) -> Result<usize, OxenError> {
    let mut newly_visited: usize = 0;
    let mut stack: Vec<Commit> = Vec::new();

    // Seed the stack with the commit's parents (not the commit itself,
    // which is already in `visited`).
    for parent_id in &commit.parent_ids {
        let parent_id: MerkleHash = parent_id.parse()?;
        if let Some(parent) = get_by_hash(repo, &parent_id)?
            && !visited.contains(&parent.id)
        {
            stack.push(parent);
        }
    }

    while let Some(current) = stack.pop() {
        if visited.contains(&current.id) {
            continue;
        }
        visited.insert(current.id.clone());
        newly_visited += 1;

        for parent_id in current.parent_ids.clone() {
            let parent_id: MerkleHash = parent_id.parse()?;
            if let Some(parent) = get_by_hash(repo, &parent_id)?
                && !visited.contains(&parent.id)
            {
                stack.push(parent);
            }
        }
    }

    Ok(newly_visited)
}

/// Wrapper to order commits by timestamp descending in a BinaryHeap (max-heap).
/// Ties are broken by commit id for deterministic ordering.
struct TimestampedCommit(Commit);

impl PartialEq for TimestampedCommit {
    fn eq(&self, other: &Self) -> bool {
        self.0.timestamp == other.0.timestamp && self.0.id == other.0.id
    }
}

impl Eq for TimestampedCommit {}

impl PartialOrd for TimestampedCommit {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for TimestampedCommit {
    fn cmp(&self, other: &Self) -> Ordering {
        self.0
            .timestamp
            .cmp(&other.0.timestamp)
            .then_with(|| self.0.id.cmp(&other.0.id))
    }
}

fn traverse_commits(
    config: CommitTraversalConfig,
    mut results: Option<&mut Vec<Commit>>,
) -> Result<usize, OxenError> {
    let mut count = 0;
    let mut heap = BinaryHeap::new();
    heap.push(TimestampedCommit(config.head_commit));
    let end_idx = config.skip + config.limit;
    let can_early_exit = config.known_total_count.is_some();
    let collect_results = results.is_some();

    while let Some(TimestampedCommit(commit)) = heap.pop() {
        if config.visited.contains(&commit.id) {
            continue;
        }

        config.visited.insert(commit.id.clone());

        // Check for base case
        if let Some(base) = config.stop_at_base
            && commit.id == base.id
        {
            if count >= config.skip
                && count < end_idx
                && let Some(ref mut res) = results
            {
                res.push(commit);
            }
            count += 1;
            continue;
        }

        // Check cache — use 1 (this commit) + newly-visited ancestors so that
        // shared ancestors between merge parents are not double-counted.
        if let Some(db) = config.cache_db
            && let Some(_cached_count) = get_cached_count(db, &commit.id)?
        {
            let newly_visited = mark_ancestors_visited(config.repo, &commit, config.visited)?;
            log::debug!(
                "Cache hit for commit {}: cached={}, newly_visited={}",
                &commit.id[..8],
                _cached_count,
                newly_visited
            );
            count += 1 + newly_visited;
            continue;
        }

        // Process commit (globally newest-first via max-heap)
        if count >= config.skip
            && count < end_idx
            && let Some(ref mut res) = results
        {
            res.push(commit.clone());
        }
        count += 1;

        if can_early_exit && collect_results && count >= end_idx {
            log::debug!(
                "Early exit: collected {} commits (skip={}, limit={})",
                results.as_ref().map(|r| r.len()).unwrap_or(0),
                config.skip,
                config.limit
            );
            break;
        }

        // Add parents to the heap
        for parent_id in commit.parent_ids.clone() {
            let parent_id = parent_id.parse()?;
            if let Some(c) = get_by_hash(config.repo, &parent_id)?
                && !config.visited.contains(&c.id)
            {
                heap.push(TimestampedCommit(c));
            }
        }
    }

    Ok(config.known_total_count.unwrap_or(count))
}

/// List commits for the repository in no particular order
pub fn list_all(repo: &LocalRepository) -> Result<HashSet<Commit>, OxenError> {
    let branches = with_ref_manager(repo, |manager| manager.list_branches())?;
    let mut commits = HashSet::new();
    for branch in branches {
        let commit = get_by_id(repo, &branch.commit_id)?;
        if let Some(commit) = commit {
            list_all_recursive(repo, commit, &mut commits)?;
        }
    }
    Ok(commits)
}

fn list_all_recursive(
    repo: &LocalRepository,
    commit: Commit,
    commits: &mut HashSet<Commit>,
) -> Result<(), OxenError> {
    // Create a temporary Vec to collect results, then add to HashSet
    let mut visited_ids = HashSet::new();
    let mut results = Vec::new();

    let config = CommitTraversalConfig {
        repo,
        head_commit: commit,
        stop_at_base: None,
        visited: &mut visited_ids,
        skip: 0,
        limit: usize::MAX,
        cache_db: None,
        known_total_count: None,
    };

    traverse_commits(config, Some(&mut results))?;
    commits.extend(results);
    Ok(())
}

/// Get commit history given a revision (branch name or commit id)
pub fn list_from(
    repo: &LocalRepository,
    revision: impl AsRef<str>,
) -> Result<Vec<Commit>, OxenError> {
    let (commits, _, _) = list_from_paginated_impl(repo, revision, 0, usize::MAX)?;
    Ok(commits)
}

pub fn list_from_paginated_impl(
    repo: &LocalRepository,
    revision: impl AsRef<str>,
    skip: usize,
    limit: usize,
) -> Result<(Vec<Commit>, usize, bool), OxenError> {
    let _perf = crate::perf_guard!("core::commits::list_from_paginated_impl");

    let revision = revision.as_ref();
    if revision.contains("..") {
        let _perf_between = crate::perf_guard!("core::commits::list_between_range");
        let split: Vec<&str> = revision.split("..").collect();
        let base = split[0];
        let head = split[1];
        let base_commit = repositories::commits::get_by_id(repo, base)?
            .ok_or_else(|| OxenError::RevisionNotFound(base.into()))?;
        let head_commit = repositories::commits::get_by_id(repo, head)?
            .ok_or_else(|| OxenError::RevisionNotFound(head.into()))?;

        let (commits, total_count) =
            list_recursive_paginated(repo, head_commit, skip, limit, Some(&base_commit), None)?;
        return Ok((commits, total_count, false));
    }

    let _perf_get = crate::perf_guard!("core::commits::get_revision");
    let commit = repositories::revisions::get(repo, revision)?;
    drop(_perf_get);

    if let Some(commit) = commit {
        let _perf_count = crate::perf_guard!("core::commits::get_cached_count");
        let (total_count, cached) = count_from(repo, &commit.id)?;
        drop(_perf_count);

        log::info!(
            "list_from_paginated_impl: total_count={total_count}, cached={cached}, skip={skip}, limit={limit}"
        );

        if skip + limit <= 10 {
            let _perf_fast = crate::perf_guard!("core::commits::list_forward_paginated");
            let commits = list_forward_paginated(repo, commit, skip, limit)?;
            drop(_perf_fast);
            return Ok((commits, total_count, cached));
        }

        let _perf_recursive = crate::perf_guard!("core::commits::list_recursive_paginated");
        let (commits, _) =
            list_recursive_paginated(repo, commit, skip, limit, None, Some(total_count))?;
        drop(_perf_recursive);

        return Ok((commits, total_count, cached));
    }

    Ok((vec![], 0, false))
}

/// Get commit history given a revision (branch name or commit id)
pub fn list_from_with_depth(
    repo: &LocalRepository,
    revision: impl AsRef<str>,
) -> Result<HashMap<Commit, usize>, OxenError> {
    let mut results = HashMap::new();
    let commit = repositories::revisions::get(repo, revision)?;
    if let Some(commit) = commit {
        list_recursive_with_depth(repo, commit, &mut results, 0)?;
    }
    Ok(results)
}

fn list_recursive_with_depth(
    repo: &LocalRepository,
    commit: Commit,
    results: &mut HashMap<Commit, usize>,
    depth: usize,
) -> Result<(), OxenError> {
    let mut stack = vec![(commit, depth)];

    while let Some((current_commit, current_depth)) = stack.pop() {
        // Check if we've already visited this commit at a shallower or equal depth
        if let Some(&existing_depth) = results.get(&current_commit)
            && existing_depth <= current_depth
        {
            // We've already processed this commit, skip it
            continue;
        }

        // Insert or update with the current (shallower) depth
        results.insert(current_commit.clone(), current_depth);

        for parent_id in current_commit.parent_ids {
            let parent_id = parent_id.parse()?;
            if let Some(parent_commit) = get_by_hash(repo, &parent_id)? {
                stack.push((parent_commit, current_depth + 1));
            }
        }
    }
    Ok(())
}

fn open_commit_count_db(
    repo: &LocalRepository,
) -> Result<DBWithThreadMode<MultiThreaded>, OxenError> {
    let db_path = util::fs::oxen_hidden_dir(&repo.path).join(COMMIT_COUNT_DIR);
    util::fs::create_dir_all(&db_path)?;
    let opts = crate::core::db::key_val::opts::default();
    Ok(DBWithThreadMode::open(&opts, dunce::simplified(&db_path))?)
}

fn get_cached_count(
    db: &DBWithThreadMode<MultiThreaded>,
    commit_id: &str,
) -> Result<Option<usize>, OxenError> {
    str_val_db::get(db, commit_id)
}

fn cache_count(
    db: &DBWithThreadMode<MultiThreaded>,
    commit_id: &str,
    count: usize,
) -> Result<(), OxenError> {
    str_val_db::put(db, commit_id, &count)
}

pub fn count_from(
    repo: &LocalRepository,
    revision: impl AsRef<str>,
) -> Result<(usize, bool), OxenError> {
    let revision = revision.as_ref();

    let commit = repositories::revisions::get(repo, revision)?
        .ok_or_else(|| OxenError::RevisionNotFound(revision.into()))?;

    let db = open_commit_count_db(repo)?;

    if let Some(cached_count) = get_cached_count(&db, &commit.id)? {
        return Ok((cached_count, true));
    }

    let config = CommitTraversalConfig {
        repo,
        head_commit: commit.clone(),
        stop_at_base: None,
        visited: &mut HashSet::new(),
        skip: 0,
        limit: usize::MAX,
        cache_db: Some(&db),
        known_total_count: None,
    };
    let count = traverse_commits(config, None)?;

    cache_count(&db, &commit.id, count)?;

    Ok((count, false))
}

/// List the history between two commits
pub fn list_between(
    repo: &LocalRepository,
    base: &Commit,
    head: &Commit,
) -> Result<Vec<Commit>, OxenError> {
    log::debug!("list_between()\nbase: {base}\nhead: {head}");
    let (results, _) =
        list_recursive_paginated(repo, head.clone(), 0, usize::MAX, Some(base), None)?;
    Ok(results)
}

/// Retrieve entries with filepaths matching a provided glob pattern
pub fn search_entries(
    repo: &LocalRepository,
    commit: &Commit,
    pattern: impl AsRef<str>,
) -> Result<HashSet<PathBuf>, OxenError> {
    let pattern = pattern.as_ref();
    let pattern = Pattern::new(pattern)?;

    let mut results = HashSet::new();
    let tree = repositories::tree::get_root_with_children(repo, commit)?
        .ok_or_else(|| OxenError::basic_str("Root not found"))?;
    let (files, _) = repositories::tree::list_files_and_dirs(&tree)?;
    for file in files {
        let path = file.dir.join(file.file_node.name());
        if pattern.matches_path(&path) {
            results.insert(path);
        }
    }
    Ok(results)
}

/// List commits by path (directory or file) recursively
pub fn list_by_path_recursive(
    repo: &LocalRepository,
    path: &Path,
    commit: &Commit,
    commits: &mut Vec<Commit>,
) -> Result<(), OxenError> {
    let mut visited = HashSet::new();
    list_by_path_recursive_impl(repo, path, commit, commits, &mut visited)
}

fn list_by_path_recursive_impl(
    repo: &LocalRepository,
    path: &Path,
    commit: &Commit,
    commits: &mut Vec<Commit>,
    visited: &mut HashSet<String>,
) -> Result<(), OxenError> {
    let mut stack = vec![commit.clone()];

    while let Some(current_commit) = stack.pop() {
        if !visited.insert(current_commit.id.clone()) {
            continue;
        }

        let Some(node) = repositories::tree::get_node_by_path(repo, &current_commit, path)? else {
            continue;
        };

        let current_node_hash = node.hash;
        let last_commit_id = node.latest_commit_id()?;

        // Check if current_commit modified the file by comparing the node hash
        // with each parent's.
        let file_modified = current_commit.parent_ids.iter().try_fold(
            // No parents means the file was added in this commit.
            current_commit.parent_ids.is_empty(),
            |modified, parent_id| -> Result<bool, OxenError> {
                if modified {
                    return Ok(true);
                }
                let parent_hash = match repositories::revisions::get(repo, parent_id.clone())? {
                    Some(pc) => {
                        repositories::tree::get_node_by_path(repo, &pc, path)?.map(|n| n.hash)
                    }
                    None => None,
                };
                Ok(parent_hash != Some(current_node_hash))
            },
        )?;

        if file_modified {
            // This commit modified the file — add it and explore parents.
            commits.push(current_commit.clone());
            push_unvisited_parents(repo, &current_commit, visited, &mut stack)?;
        } else {
            // File not modified here. Use last_commit_id to jump ahead to the
            // next commit that did, skipping intermediate unmodified commits.
            match repositories::revisions::get(repo, last_commit_id.to_string())? {
                Some(jump_commit) if !visited.contains(&jump_commit.id) => {
                    stack.push(jump_commit);
                }
                _ => push_unvisited_parents(repo, &current_commit, visited, &mut stack)?,
            }
        }
    }

    Ok(())
}

fn push_unvisited_parents(
    repo: &LocalRepository,
    commit: &Commit,
    visited: &HashSet<String>,
    stack: &mut Vec<Commit>,
) -> Result<(), OxenError> {
    for parent_id in &commit.parent_ids {
        if let Some(parent) = repositories::revisions::get(repo, parent_id.clone())?
            && !visited.contains(&parent.id)
        {
            stack.push(parent);
        }
    }
    Ok(())
}

/// Get paginated list of commits by path (directory or file)
pub fn list_by_path_from_paginated(
    repo: &LocalRepository,
    commit: &Commit,
    path: &Path,
    pagination: PaginateOpts,
) -> Result<PaginatedCommits, OxenError> {
    let _perf = crate::perf_guard!("core::commits::list_by_path_from_paginated");

    // Check if the path is a directory or file
    let _perf_node = crate::perf_guard!("core::commits::get_node_by_path");
    let node = repositories::tree::get_node_by_path(repo, commit, path)?.ok_or_else(|| {
        OxenError::basic_str(format!("Merkle tree node not found for path: {path:?}"))
    })?;
    let last_commit_id = match &node.node {
        EMerkleTreeNode::File(file_node) => file_node.last_commit_id(),
        EMerkleTreeNode::Directory(dir_node) => dir_node.last_commit_id(),
        _ => {
            return Err(OxenError::basic_str(format!(
                "Merkle tree node not found for path: {path:?}"
            )));
        }
    };
    let last_commit_id = last_commit_id.to_string();
    drop(_perf_node);

    let _perf_recursive = crate::perf_guard!("core::commits::list_by_path_recursive");
    let mut commits: Vec<Commit> = Vec::new();
    list_by_path_recursive(repo, path, commit, &mut commits)?;
    log::info!(
        "list_by_path_from_paginated {} got {} commits before pagination",
        last_commit_id,
        commits.len()
    );
    drop(_perf_recursive);

    let _perf_paginate = crate::perf_guard!("core::commits::paginate_path_commits");
    let (commits, pagination) = util::paginate(commits, pagination.page_num, pagination.page_size);
    drop(_perf_paginate);

    Ok(PaginatedCommits {
        status: StatusMessage::resource_found(),
        commits,
        pagination,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::repositories;
    use crate::test;

    #[tokio::test]
    async fn test_pagination_order_with_more_than_10_commits() -> Result<(), OxenError> {
        test::run_empty_local_repo_test_async(|repo| async move {
            // Create 15 commits to trigger the slow path (skip + limit > 10)
            let mut commit_ids = Vec::new();

            for i in 0..15 {
                let filename = format!("file_{i}.txt");
                let file_path = repo.path.join(&filename);
                test::write_txt_file_to_path(&file_path, format!("Content {i}"))?;

                repositories::add(&repo, &file_path).await?;
                let commit = repositories::commit(&repo, &format!("Commit {i}"))?;
                commit_ids.push(commit.id.clone());
            }

            // Commits should be ordered newest-first (C14, C13, C12, ...)
            // Test: skip=9, limit=2 (total 11 > 10) to trigger the slow path
            // This should return [C5, C4] (skip 9 newest, then take 2)
            let (paginated_commits, _total, _cached) =
                list_from_paginated_impl(&repo, "main", 9, 2)?;

            assert_eq!(
                paginated_commits.len(),
                2,
                "Should return exactly 2 commits"
            );

            // Expected: skip 9 newest (C14 down to C6), then take [C5, C4]
            let expected_first = &commit_ids[5]; // C5 (0-indexed)
            let expected_second = &commit_ids[4]; // C4

            println!("Total commits: {}", commit_ids.len());
            println!("Expected first: {expected_first} (C5 - Commit 5)");
            println!("Expected second: {expected_second} (C4 - Commit 4)");
            println!(
                "Actual first: {} ({})",
                paginated_commits[0].id, paginated_commits[0].message
            );
            println!(
                "Actual second: {} ({})",
                paginated_commits[1].id, paginated_commits[1].message
            );

            assert_eq!(
                &paginated_commits[0].id, expected_first,
                "First result should be C5 (Commit 5), but got {}",
                paginated_commits[0].message
            );
            assert_eq!(
                &paginated_commits[1].id, expected_second,
                "Second result should be C4 (Commit 4), but got {}",
                paginated_commits[1].message
            );

            Ok(())
        })
        .await
    }

    #[tokio::test]
    async fn test_pagination_with_forward_path() -> Result<(), OxenError> {
        test::run_empty_local_repo_test_async(|repo| async move {
            // Create exactly 10 commits - this should use forward pagination (fast path)
            let mut commit_ids = Vec::new();

            for i in 0..10 {
                let filename = format!("file_{i}.txt");
                let file_path = repo.path.join(&filename);
                test::write_txt_file_to_path(&file_path, format!("Content {i}"))?;

                repositories::add(&repo, &file_path).await?;
                let commit = repositories::commit(&repo, &format!("Commit {i}"))?;
                commit_ids.push(commit.id.clone());
            }

            // With skip=1, limit=2, and total commits <= 10, should use forward path
            let (paginated_commits, _total, _cached) =
                list_from_paginated_impl(&repo, "main", 1, 2)?;

            assert_eq!(
                paginated_commits.len(),
                2,
                "Should return exactly 2 commits"
            );

            // Forward path should work correctly: skip C9, return [C8, C7]
            let expected_first = &commit_ids[8]; // C8
            let expected_second = &commit_ids[7]; // C7

            println!("Forward path test:");
            println!("Expected first: {expected_first} (C8)");
            println!("Expected second: {expected_second} (C7)");
            println!(
                "Actual first: {} ({})",
                paginated_commits[0].id, paginated_commits[0].message
            );
            println!(
                "Actual second: {} ({})",
                paginated_commits[1].id, paginated_commits[1].message
            );

            assert_eq!(
                &paginated_commits[0].id, expected_first,
                "First result should be C8, got {}",
                paginated_commits[0].message
            );
            assert_eq!(
                &paginated_commits[1].id, expected_second,
                "Second result should be C7, got {}",
                paginated_commits[1].message
            );

            Ok(())
        })
        .await
    }
}