gix_commitgraph/lib.rs
1//! Read, verify, and traverse git commit graphs.
2//!
3//! A [commit graph][Graph] is an index of commits in the git commit history.
4//! The [Graph] stores commit data in a way that accelerates lookups considerably compared to
5//! traversing the git history by usual means.
6//!
7//! As generating the full commit graph from scratch can take some time, git may write new commits
8//! to separate [files][File] instead of overwriting the original file.
9//! Eventually, git will merge these files together as the number of files grows.
10//! ## Feature Flags
11#![cfg_attr(
12 all(doc, feature = "document-features"),
13 doc = ::document_features::document_features!()
14)]
15#![cfg_attr(all(doc, feature = "document-features"), feature(doc_cfg, doc_auto_cfg))]
16#![deny(missing_docs, rust_2018_idioms, unsafe_code)]
17
18use std::path::Path;
19
20/// A single commit-graph file.
21///
22/// All operations on a `File` are local to that graph file. Since a commit graph can span multiple
23/// files, all interesting graph operations belong on [`Graph`].
24pub struct File {
25 base_graph_count: u8,
26 base_graphs_list_offset: Option<usize>,
27 commit_data_offset: usize,
28 data: memmap2::Mmap,
29 extra_edges_list_range: Option<std::ops::Range<usize>>,
30 fan: [u32; file::FAN_LEN],
31 oid_lookup_offset: usize,
32 path: std::path::PathBuf,
33 hash_len: usize,
34 object_hash: gix_hash::Kind,
35}
36
37/// A complete commit graph.
38///
39/// The data in the commit graph may come from a monolithic `objects/info/commit-graph` file, or it
40/// may come from one or more `objects/info/commit-graphs/graph-*.graph` files. These files are
41/// generated via `git commit-graph write ...` commands.
42pub struct Graph {
43 files: Vec<File>,
44}
45
46/// Instantiate a commit graph from an `.git/objects/info` directory, or one of the various commit-graph files.
47pub fn at(path: impl AsRef<Path>) -> Result<Graph, init::Error> {
48 Graph::at(path.as_ref())
49}
50
51mod access;
52pub mod file;
53///
54pub mod init;
55pub mod verify;
56
57/// The number of generations that are considered 'infinite' commit history.
58pub const GENERATION_NUMBER_INFINITY: u32 = 0xffff_ffff;
59/// The largest valid generation number.
60///
61/// If a commit's real generation number is larger than this, the commit graph will cap the value to
62/// this number.
63/// The largest distinct generation number is `GENERATION_NUMBER_MAX - 1`.
64pub const GENERATION_NUMBER_MAX: u32 = 0x3fff_ffff;
65
66/// The maximum number of commits that can be stored in a commit graph.
67pub const MAX_COMMITS: u32 = (1 << 30) + (1 << 29) + (1 << 28) - 1;
68
69/// A generalized position for use in [`Graph`].
70#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
71pub struct Position(pub u32);
72
73impl std::fmt::Display for Position {
74 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
75 self.0.fmt(f)
76 }
77}