Skip to main content

gix_commitgraph/
access.rs

1use crate::{File, Graph, Position, file, file::Commit};
2
3/// Access
4impl Graph {
5    /// Returns the commit at the given position `pos`.
6    ///
7    /// # Panics
8    /// If `pos` is greater or equal to [`num_commits()`][Graph::num_commits()].
9    pub fn commit_at(&self, pos: Position) -> Commit<'_> {
10        let r = self.lookup_by_pos(pos);
11        r.file.commit_at(r.pos)
12    }
13
14    /// The kind of hash used in this `Graph`.
15    ///
16    /// Note that it is always conforming to the hash used in the owning repository.
17    pub fn object_hash(&self) -> gix_hash::Kind {
18        self.files.first().object_hash()
19    }
20
21    /// Returns the commit matching the given `id`.
22    pub fn commit_by_id(&self, id: impl AsRef<gix_hash::oid>) -> Option<Commit<'_>> {
23        let r = self.lookup_by_id(id.as_ref())?;
24        Some(r.file.commit_at(r.file_pos))
25    }
26
27    /// Returns the `hash` at the given position `pos`.
28    ///
29    /// # Panics
30    /// If `pos` is greater or equal to [`num_commits()`][Graph::num_commits()].
31    pub fn id_at(&self, pos: Position) -> &gix_hash::oid {
32        let r = self.lookup_by_pos(pos);
33        r.file.id_at(r.pos)
34    }
35
36    /// Return whether all graph layers have computed topological generation numbers.
37    ///
38    /// This checks the first commit in each nonempty file, relying on Git's invariant that a file
39    /// contains either all zero (uncomputed) or all nonzero generations. It does not validate that
40    /// invariant or rule out capped generations at [`crate::GENERATION_NUMBER_MAX`].
41    pub fn generation_numbers_available(&self) -> bool {
42        self.files
43            .iter()
44            .all(|file| file.iter_commits().next().is_none_or(|commit| commit.generation() != 0))
45    }
46
47    /// Return whether every layer contains corrected commit dates (generation numbers version 2).
48    ///
49    /// A mixed chain must treat corrected dates as unavailable throughout, as the two measures are not comparable.
50    pub fn corrected_commit_dates_available(&self) -> bool {
51        self.corrected_commit_dates_available
52    }
53
54    /// Iterate over commits in unsorted order.
55    pub fn iter_commits(&self) -> impl Iterator<Item = Commit<'_>> {
56        self.files.iter().flat_map(File::iter_commits)
57    }
58
59    /// Iterate over commit IDs in unsorted order.
60    pub fn iter_ids(&self) -> impl Iterator<Item = &gix_hash::oid> {
61        self.files.iter().flat_map(File::iter_ids)
62    }
63
64    /// Translate the given `id` to its position in the file.
65    pub fn lookup(&self, id: impl AsRef<gix_hash::oid>) -> Option<Position> {
66        Some(self.lookup_by_id(id.as_ref())?.graph_pos)
67    }
68
69    /// Returns the number of commits stored in this file.
70    pub fn num_commits(&self) -> u32 {
71        self.files.iter().map(File::num_commits).sum()
72    }
73}
74
75/// Access fundamentals
76impl Graph {
77    fn lookup_by_id(&self, id: &gix_hash::oid) -> Option<LookupByIdResult<'_>> {
78        let mut current_file_start = 0;
79        for file in &self.files {
80            if let Some(lex_pos) = file.lookup(id) {
81                return Some(LookupByIdResult {
82                    file,
83                    file_pos: lex_pos,
84                    graph_pos: Position(current_file_start + lex_pos.0),
85                });
86            }
87            current_file_start += file.num_commits();
88        }
89        None
90    }
91
92    fn lookup_by_pos(&self, pos: Position) -> LookupByPositionResult<'_> {
93        let mut remaining = pos.0;
94        for (file_index, file) in self.files.iter().enumerate() {
95            match remaining.checked_sub(file.num_commits()) {
96                Some(v) => remaining = v,
97                None => {
98                    return LookupByPositionResult {
99                        file,
100                        _file_index: file_index,
101                        pos: file::Position(remaining),
102                    };
103                }
104            }
105        }
106        panic!("graph position too large: {}", pos.0);
107    }
108}
109
110#[derive(Clone)]
111struct LookupByIdResult<'a> {
112    pub file: &'a File,
113    pub graph_pos: Position,
114    pub file_pos: file::Position,
115}
116
117#[derive(Clone)]
118struct LookupByPositionResult<'a> {
119    pub file: &'a File,
120    pub _file_index: usize,
121    pub pos: file::Position,
122}