Showing the changes to git-commitgraph/src (3 of 4 files). Show all
Unified · Split
| @@ -6,16 +6,25 @@ use git_object::borrowed; · 5 unmodified lines | ||
| 6 | 6 | |
| 7 | 7 | /// Access |
| 8 | 8 | impl Graph { |
| 9 | + /// Returns the commit at the given position `pos`. | |
| 10 | + /// | |
| 11 | + /// # Panics | |
| 12 | + /// If `pos` is greater or equal to [`num_commits()`][Graph::num_commits()]. | |
| 9 | 13 | pub fn commit_at(&self, pos: graph::Position) -> Commit<'_> { |
| 10 | 14 | let r = self.lookup_by_pos(pos); |
| 11 | 15 | r.file.commit_at(r.pos) |
| 12 | 16 | } |
| 13 | 17 | |
| 18 | + /// Returns the commit matching the given `id`. | |
| 14 | 19 | pub fn commit_by_id(&self, id: borrowed::Id<'_>) -> Option<Commit<'_>> { |
| 15 | 20 | let r = self.lookup_by_id(id)?; |
| 16 | 21 | Some(r.file.commit_at(r.file_pos)) |
| 17 | 22 | } |
| 18 | 23 | |
| 24 | + /// Returns the `hash` at the given position `pos`. | |
| 25 | + /// | |
| 26 | + /// # Panics | |
| 27 | + /// If `pos` is greater or equal to [`num_commits()`][Graph::num_commits()]. | |
| 19 | 28 | pub fn id_at(&self, pos: graph::Position) -> borrowed::Id<'_> { |
| 20 | 29 | let r = self.lookup_by_pos(pos); |
| 21 | 30 | r.file.id_at(r.pos) |
| @@ -31,10 +40,12 @@ impl Graph { · 9 unmodified lines | ||
| 31 | 40 | self.files.iter().flat_map(|file| file.iter_ids()) |
| 32 | 41 | } |
| 33 | 42 | |
| 43 | + /// Translate the given `id` to its position in the file. | |
| 34 | 44 | pub fn lookup(&self, id: borrowed::Id<'_>) -> Option<graph::Position> { |
| 35 | 45 | Some(self.lookup_by_id(id)?.graph_pos) |
| 36 | 46 | } |
| 37 | 47 | |
| 48 | + /// Returns the amount of commits stored in this file. | |
| 38 | 49 | pub fn num_commits(&self) -> u32 { |
| 39 | 50 | self.files.iter().map(|f| f.num_commits()).sum() |
| 40 | 51 | } |
| @@ -9,7 +9,9 @@ use std::{ · 8 unmodified lines | ||
| 9 | 9 | path::{Path, PathBuf}, |
| 10 | 10 | }; |
| 11 | 11 | |
| 12 | +/// The error used in the [`graph`][crate::graph] module. | |
| 12 | 13 | #[derive(thiserror::Error, Debug)] |
| 14 | +#[allow(missing_docs)] | |
| 13 | 15 | pub enum Error { |
| 14 | 16 | #[error("{}", .path.display())] |
| 15 | 17 | File { |
| @@ -41,10 +43,12 @@ pub enum Error { · 25 unmodified lines | ||
| 41 | 43 | |
| 42 | 44 | /// Instantiate a `Graph` from various sources. |
| 43 | 45 | impl Graph { |
| 46 | + /// Instantiate a commit graph from `path` which may be a directory containing graph files or the graph file itself. | |
| 44 | 47 | pub fn at(path: impl AsRef<Path>) -> Result<Self, Error> { |
| 45 | 48 | Self::try_from(path.as_ref()) |
| 46 | 49 | } |
| 47 | 50 | |
| 51 | + /// Instantiate a commit graph from the directory containing all of its files. | |
| 48 | 52 | pub fn from_commit_graphs_dir(path: impl AsRef<Path>) -> Result<Self, Error> { |
| 49 | 53 | let commit_graphs_dir = path.as_ref(); |
| 50 | 54 | let chain_file_path = commit_graphs_dir.join("commit-graph-chain"); |
| @@ -67,6 +71,8 @@ impl Graph { · 16 unmodified lines | ||
| 67 | 71 | Self::new(files) |
| 68 | 72 | } |
| 69 | 73 | |
| 74 | + /// Instantiate a commit graph from a `.git/objects/info/commit-graph` or | |
| 75 | + /// `.git/objects/info/commit-graphs/graph-*.graph` file. | |
| 70 | 76 | pub fn from_file(path: impl AsRef<Path>) -> Result<Self, Error> { |
| 71 | 77 | let path = path.as_ref(); |
| 72 | 78 | let file = File::at(path).map_err(|e| Error::File { |
| @@ -76,11 +82,13 @@ impl Graph { · 3 unmodified lines | ||
| 76 | 82 | Self::new(vec![file]) |
| 77 | 83 | } |
| 78 | 84 | |
| 85 | + /// Instantiate a commit graph from an `.git/objects/info` directory. | |
| 79 | 86 | pub fn from_info_dir(info_dir: impl AsRef<Path>) -> Result<Self, Error> { |
| 80 | 87 | Self::from_file(info_dir.as_ref().join("commit-graph")) |
| 81 | 88 | .or_else(|_| Self::from_commit_graphs_dir(info_dir.as_ref().join("commit-graphs"))) |
| 82 | 89 | } |
| 83 | 90 | |
| 91 | + /// Create a new commit graph from a list of `files`. | |
| 84 | 92 | pub fn new(files: Vec<File>) -> Result<Self, Error> { |
| 85 | 93 | let num_commits: u64 = files.iter().map(|f| u64::from(f.num_commits())).sum(); |
| 86 | 94 | if num_commits > u64::from(MAX_COMMITS) { |
| @@ -10,7 +10,9 @@ use std::{ · 9 unmodified lines | ||
| 10 | 10 | path::PathBuf, |
| 11 | 11 | }; |
| 12 | 12 | |
| 13 | +/// The error used in [`verify_integrity()`][Graph::verify_integrity]. | |
| 13 | 14 | #[derive(thiserror::Error, Debug)] |
| 15 | +#[allow(missing_docs)] | |
| 14 | 16 | pub enum Error<E: std::error::Error + 'static> { |
| 15 | 17 | #[error("'{}' should have {expected} base graphs, but claims {actual} base graphs", .path.display())] |
| 16 | 18 | BaseGraphCount { actual: u8, expected: u8, path: PathBuf }, |