History

all docs for git-commitgraph::file

Sebastian Thiel committed 5 years ago

Commit 8b262011 parent 23bba1c6 files · +54 −20Browse files

Showing the changes to git-commitgraph (5 of 6 files). Show all

Unified · Split

5 files
  1. git-commitgraph/src/file/access.rs +9 −0
  2. git-commitgraph/src/file/commit.rs +11 −12
  3. git-commitgraph/src/file/init.rs +3 −0
  4. git-commitgraph/src/file/mod.rs +9 −4
  5. git-commitgraph/src/file/verify.rs +18 −1
git-commitgraph/src/file/access.rs+9−0
@@ -9,6 +9,7 @@ use std::{ · 8 unmodified lines
99
1010 /// Access
1111 impl File {
12+ /// The amount of base graphs that this file depends on.
1213 pub fn base_graph_count(&self) -> u8 {
1314 self.base_graph_count
1415 }
@@ -24,6 +25,9 @@ impl File { · 9 unmodified lines
2425 Commit::new(self, pos)
2526 }
2627
28+ /// The kind of hash used in this File.
29+ ///
30+ /// Note that it is always conforming to the hash used in the owning repository.
2731 pub fn hash_kind(&self) -> HashKind {
2832 HashKind::Sha1
2933 }
@@ -46,6 +50,7 @@ impl File { · 16 unmodified lines
4650 borrowed::Id::try_from(&self.data[start..start + SHA1_SIZE]).expect("20 bytes SHA1 to be alright")
4751 }
4852
53+ /// Return an iterator over all object hashes stored in the base graph.
4954 pub fn iter_base_graph_ids(&self) -> impl Iterator<Item = borrowed::Id<'_>> {
5055 let start = self.base_graphs_list_offset.unwrap_or(0);
5156 let base_graphs_list = &self.data[start..start + (SHA1_SIZE * usize::from(self.base_graph_count))];
@@ -54,14 +59,17 @@ impl File { · 2 unmodified lines
5459 .map(|bytes| borrowed::Id::try_from(bytes).expect("20 bytes SHA1 to be alright"))
5560 }
5661
62+ /// return an iterator over all commits in this file.
5763 pub fn iter_commits(&self) -> impl Iterator<Item = Commit<'_>> {
5864 (0..self.num_commits()).map(move |i| self.commit_at(file::Position(i)))
5965 }
6066
67+ /// Return an iterator over all object hashes stored in this file.
6168 pub fn iter_ids(&self) -> impl Iterator<Item = borrowed::Id<'_>> {
6269 (0..self.num_commits()).map(move |i| self.id_at(file::Position(i)))
6370 }
6471
72+ /// Translate the given object hash to its position within this file, if present.
6573 // copied from git-odb/src/pack/index/access.rs
6674 pub fn lookup(&self, id: borrowed::Id<'_>) -> Option<file::Position> {
6775 let first_byte = usize::from(id.first_byte());
@@ -94,6 +102,7 @@ impl File { · 26 unmodified lines
94102 self.fan[255]
95103 }
96104
105+ /// Returns the path to this file.
97106 pub fn path(&self) -> &Path {
98107 &self.path
99108 }
git-commitgraph/src/file/commit.rs+11−12
@@ -11,7 +11,9 @@ use std::{ · 10 unmodified lines
1111 slice::Chunks,
1212 };
1313
14+/// The error used in the [`file::commit`][self] module.
1415 #[derive(thiserror::Error, Debug)]
16+#[allow(missing_docs)]
1517 pub enum Error {
1618 #[error("commit {0}'s extra edges overflows the commit-graph file's extra edges list")]
1719 ExtraEdgesListOverflow(owned::Id),
@@ -28,6 +30,7 @@ pub enum Error { · 10 unmodified lines
2830 const NO_PARENT: u32 = 0x7000_0000;
2931 const EXTENDED_EDGES_MASK: u32 = 0x8000_0000;
3032
33+/// A commit as stored in a [`File`].
3134 pub struct Commit<'a> {
3235 file: &'a File,
3336 pos: file::Position,
@@ -68,6 +71,7 @@ impl<'a> Commit<'a> { · 34 unmodified lines
6871 self.generation
6972 }
7073
74+ /// Returns an iterator over the parent positions for lookup in the owning [Graph][crate::Graph].
7175 pub fn iter_parents(&'a self) -> impl Iterator<Item = Result<graph::Position, Error>> + 'a {
7276 // I didn't find a combinator approach that a) was as strict as ParentIterator, b) supported
7377 // fuse-after-first-error behavior, and b) was significantly shorter or more understandable
@@ -78,29 +82,23 @@ impl<'a> Commit<'a> { · 4 unmodified lines
7882 }
7983 }
8084
81− // Allow the return value to outlive this Commit object, as it only needs to be bound by the
82− // lifetime of the parent file.
83− pub fn id<'b>(&'b self) -> borrowed::Id<'a>
84− where
85− 'a: 'b,
86− {
85+ /// Returns the hash of this commit.
86+ pub fn id(&self) -> borrowed::Id<'a> {
8787 self.file.id_at(self.pos)
8888 }
8989
90+ /// Returns the first parent of this commit.
9091 pub fn parent1(&self) -> Result<Option<graph::Position>, Error> {
9192 self.iter_parents().next().transpose()
9293 }
9394
95+ /// Returns the position at which this commit is stored in the parent [File].
9496 pub fn position(&self) -> file::Position {
9597 self.pos
9698 }
9799
98− // Allow the return value to outlive this Commit object, as it only needs to be bound by the
99− // lifetime of the parent file.
100− pub fn root_tree_id<'b>(&'b self) -> borrowed::Id<'a>
101− where
102− 'a: 'b,
103− {
100+ /// Return the hash of the tree this commit points to.
101+ pub fn root_tree_id(&self) -> borrowed::Id<'a> {
104102 self.root_tree_id
105103 }
106104 }
@@ -128,6 +126,7 @@ impl<'a> PartialEq for Commit<'a> { · 21 unmodified lines
128126 }
129127 }
130128
129+/// An iterator over parents of a [`Commit`].
131130 pub struct ParentIterator<'a> {
132131 commit_data: &'a Commit<'a>,
133132 state: ParentIteratorState<'a>,
git-commitgraph/src/file/init.rs+3−0
@@ -11,7 +11,9 @@ use std::{ · 10 unmodified lines
1111
1212 type ChunkId = [u8; 4];
1313
14+/// The error used in [`File::at()`].
1415 #[derive(thiserror::Error, Debug)]
16+#[allow(missing_docs)]
1517 pub enum Error {
1618 #[error("Commit-graph {:?} chunk contains {from_chunk} base graphs, but commit-graph file header claims {from_header} base graphs", BASE_GRAPHS_LIST_CHUNK_ID.as_bstr())]
1719 BaseGraphMismatch { from_header: u8, from_chunk: u32 },
@@ -63,6 +65,7 @@ const OID_LOOKUP_CHUNK_ID: ChunkId = *b"OIDL"; · 45 unmodified lines
6365 const SENTINEL_CHUNK_ID: ChunkId = [0u8; 4];
6466
6567 impl File {
68+ /// Try to parse the commit graph file at `path`.
6669 pub fn at(path: impl AsRef<Path>) -> Result<File, Error> {
6770 Self::try_from(path.as_ref())
6871 }
git-commitgraph/src/file/mod.rs+9−4
@@ -1,12 +1,17 @@
11 //! Operations on a single commit-graph file.
2+
23 mod access;
4+
5+///
36 pub mod commit;
7+pub use commit::Commit;
8+
49 mod init;
10+///
511 pub mod verify;
612
713 pub use init::Error;
814
9−pub use commit::Commit;
1015 use filebuffer::FileBuffer;
1116 use git_hash::SIZE_OF_SHA1_DIGEST as SHA1_SIZE;
1217 use std::{
@@ -22,7 +27,7 @@ const SIGNATURE: &[u8] = b"CGPH"; · 9 unmodified lines
2227 /// A single commit-graph file.
2328 ///
2429 /// All operations on a `File` are local to that graph file. Since a commit graph can span multiple
25−/// files, all interesting graph operations belong on `Graph`.
30+/// files, all interesting graph operations belong on [`Graph`][crate::Graph].
2631 pub struct File {
2732 base_graph_count: u8,
2833 base_graphs_list_offset: Option<usize>,
@@ -39,8 +44,8 @@ pub struct File { · 10 unmodified lines
3944 /// Commits within a graph file are sorted in lexicographical order by OID; a commit's lexigraphical position
4045 /// is its position in this ordering. If a commit graph spans multiple files, each file's commits
4146 /// start at lexigraphical position 0, so it is unique across a single file but is not unique across
42−/// the whole commit graph. Each commit also has a graph position (`graph::Position`), which is unique
43−/// across the whole commit graph. In order to avoid accidentally mixing lexigraphical positions with graph
47+/// the whole commit graph. Each commit also has a graph position ([`graph::Position`][crate::graph::Position]),
48+/// which is unique /// across the whole commit graph. In order to avoid accidentally mixing lexigraphical positions with graph
4449 /// positions, distinct types are used for each.
4550 #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
4651 pub struct Position(pub u32);
git-commitgraph/src/file/verify.rs+18−1
@@ -12,7 +12,9 @@ use std::{ · 11 unmodified lines
1212 path::Path,
1313 };
1414
15+/// The error used in [`File::traverse()`].
1516 #[derive(thiserror::Error, Debug)]
17+#[allow(missing_docs)]
1618 pub enum Error<E: std::error::Error + 'static> {
1719 #[error(transparent)]
1820 Commit(#[from] file::commit::Error),
@@ -36,21 +38,32 @@ pub enum Error<E: std::error::Error + 'static> { · 17 unmodified lines
3638 RootTreeId { id: owned::Id, root_tree_id: owned::Id },
3739 }
3840
41+/// The positive result of [`File::traverse()`] providing some statistical information.
3942 #[derive(Clone, Debug, Eq, PartialEq)]
4043 #[cfg_attr(feature = "serde1", derive(serde::Deserialize, serde::Serialize))]
4144 pub struct Outcome {
45+ /// The highest encountered [`file::Commit`] generation number.
4246 pub max_generation: u32,
43− pub max_parents: u32,
47+ /// The smallest encountered [`file::Commit`] generation number.
4448 pub min_generation: u32,
49+ /// The highest amount parents in a single [`file::Commit`].
50+ pub max_parents: u32,
51+ /// The total amount of [`commits`][file::Commit] seen in the iteration.
4552 pub num_commits: u32,
53+ /// A mapping of `parent-count -> amount of [commits][file::Commit] with that count`.
4654 pub parent_counts: HashMap<u32, u32>,
4755 }
4856
57+/// Verification
4958 impl File {
59+ /// Returns the trailing checksum over the entire content of this file.
5060 pub fn checksum(&self) -> borrowed::Id<'_> {
5161 borrowed::Id::try_from(&self.data[self.data.len() - SHA1_SIZE..]).expect("file to be large enough for a hash")
5262 }
5363
64+ /// Traverse all [commits][file::Commit] stored in this file and call `processor(commit) -> Result<(), Error>` on it.
65+ ///
66+ /// If the `processor` fails, the iteration will be stopped and the entire call results in the respective error.
5467 pub fn traverse<'a, E, Processor>(&'a self, mut processor: Processor) -> Result<Outcome, Error<E>>
5568 where
5669 E: std::error::Error + 'static,
@@ -118,6 +131,10 @@ impl File { · 61 unmodified lines
118131 Ok(stats)
119132 }
120133
134+ /// Assure the [`checksum`][File::checksum()] matches the actual checksum over all content of this file, excluding the trailing
135+ /// checksum itself.
136+ ///
137+ /// Return the actual checksum on success or `(actual checksum, expected checksum)` if there is a mismatch.
121138 pub fn verify_checksum(&self) -> Result<owned::Id, (owned::Id, owned::Id)> {
122139 // Even though we could use git_features::hash::bytes_of_file(…), this would require using our own
123140 // Error type to support io::Error and Mismatch. As we only gain progress, there probably isn't much value