gix_commitgraph/
access.rs1use crate::{File, Graph, Position, file, file::Commit};
2
3impl Graph {
5 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 pub fn object_hash(&self) -> gix_hash::Kind {
18 self.files.first().object_hash()
19 }
20
21 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 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 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 pub fn corrected_commit_dates_available(&self) -> bool {
51 self.corrected_commit_dates_available
52 }
53
54 pub fn iter_commits(&self) -> impl Iterator<Item = Commit<'_>> {
56 self.files.iter().flat_map(File::iter_commits)
57 }
58
59 pub fn iter_ids(&self) -> impl Iterator<Item = &gix_hash::oid> {
61 self.files.iter().flat_map(File::iter_ids)
62 }
63
64 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 pub fn num_commits(&self) -> u32 {
71 self.files.iter().map(File::num_commits).sum()
72 }
73}
74
75impl 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}