Skip to main content

gix_traverse/commit/
mod.rs

1//! Provide multiple traversal implementations with different performance envelopes.
2//!
3//! Use [`Simple`] for fast walks that maintain minimal state, or [`Topo`] for a more elaborate traversal.
4use gix_hash::ObjectId;
5use gix_object::FindExt;
6use gix_revwalk::{PriorityQueue, graph::IdMap};
7use smallvec::SmallVec;
8
9/// A fast iterator over the ancestors of one or more starting commits.
10pub struct Simple<Find, Predicate> {
11    objects: Find,
12    cache: Option<gix_commitgraph::Graph>,
13    predicate: Predicate,
14    state: simple::State,
15    parents: Parents,
16    sorting: simple::Sorting,
17}
18
19/// Simple ancestors traversal, without the need to keep track of graph-state.
20pub mod simple;
21
22/// A commit walker that walks in topographical order, like `git rev-list
23/// --topo-order` or `--date-order` depending on the chosen [`topo::Sorting`].
24///
25/// Instantiate with [`topo::Builder`].
26pub struct Topo<Find, Predicate> {
27    commit_graph: Option<gix_commitgraph::Graph>,
28    find: Find,
29    predicate: Predicate,
30    indegrees: IdMap<i32>,
31    states: IdMap<topo::WalkFlags>,
32    explore_queue: PriorityQueue<topo::iter::GenAndCommitTime, ObjectId>,
33    indegree_queue: PriorityQueue<topo::iter::GenAndCommitTime, ObjectId>,
34    topo_queue: topo::iter::Queue,
35    parents: Parents,
36    min_gen: u32,
37    buf: Vec<u8>,
38}
39
40pub mod topo;
41
42/// Specify how to handle commit parents during traversal.
43#[derive(Default, Copy, Clone)]
44pub enum Parents {
45    /// Traverse all parents, useful for traversing the entire ancestry.
46    #[default]
47    All,
48    /// Only traverse along the first parent, which commonly ignores all branches.
49    First,
50}
51
52/// The collection of parent ids we saw as part of the iteration.
53///
54/// Note that this list is truncated if [`Parents::First`] was used.
55pub type ParentIds = SmallVec<[gix_hash::ObjectId; 1]>;
56
57/// Information about a commit that we obtained naturally as part of the iteration.
58#[derive(Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
59pub struct Info {
60    /// The id of the commit.
61    pub id: gix_hash::ObjectId,
62    /// All parent ids we have encountered. Note that these will be at most one if [`Parents::First`] is enabled.
63    pub parent_ids: ParentIds,
64    /// The generation number if this commit was read from a commit-graph.
65    pub generation: Option<gix_revwalk::graph::Generation>,
66    /// The time at which the commit was created. It will only be `Some(_)` if the chosen traversal was
67    /// taking dates into consideration.
68    pub commit_time: Option<gix_date::SecondsSinceUnixEpoch>,
69}
70
71/// Information about a commit that can be obtained either from a [`gix_object::CommitRefIter`] or
72/// a [`gix_commitgraph::file::Commit`].
73#[derive(Clone, Copy)]
74pub enum Either<'buf, 'cache> {
75    /// See [`gix_object::CommitRefIter`].
76    CommitRefIter(gix_object::CommitRefIter<'buf>),
77    /// See [`gix_commitgraph::file::Commit`].
78    CachedCommit(gix_commitgraph::file::Commit<'cache>),
79}
80
81impl Either<'_, '_> {
82    /// Get a commit’s `tree_id` by either getting it from a [`gix_commitgraph::Graph`], if
83    /// present, or a [`gix_object::CommitRefIter`] otherwise.
84    pub fn tree_id(self) -> Result<ObjectId, gix_object::decode::Error> {
85        match self {
86            Self::CommitRefIter(mut commit_ref_iter) => commit_ref_iter.tree_id(),
87            Self::CachedCommit(commit) => Ok(commit.root_tree_id().into()),
88        }
89    }
90
91    /// Get a committer timestamp by either getting it from a [`gix_commitgraph::Graph`], if
92    /// present, or a [`gix_object::CommitRefIter`] otherwise.
93    pub fn commit_time(self) -> Result<gix_date::SecondsSinceUnixEpoch, gix_object::decode::Error> {
94        match self {
95            Self::CommitRefIter(commit_ref_iter) => commit_ref_iter.committer().map(|c| c.seconds()),
96            Self::CachedCommit(commit) => Ok(commit.committer_timestamp() as gix_date::SecondsSinceUnixEpoch),
97        }
98    }
99}
100
101/// Find information about a commit by either getting it from a [`gix_commitgraph::Graph`], if
102/// present, or a [`gix_object::CommitRefIter`] otherwise.
103pub fn find<'cache, 'buf, Find>(
104    cache: Option<&'cache gix_commitgraph::Graph>,
105    objects: Find,
106    id: &gix_hash::oid,
107    buf: &'buf mut Vec<u8>,
108) -> Result<Either<'buf, 'cache>, gix_object::find::existing_iter::Error>
109where
110    Find: gix_object::Find,
111{
112    match cache.and_then(|cache| cache.commit_by_id(id).map(Either::CachedCommit)) {
113        Some(c) => Ok(c),
114        None => objects.find_commit_iter(id, buf).map(Either::CommitRefIter),
115    }
116}