Skip to main content

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))]
16#![deny(missing_docs, unsafe_code)]
17
18use std::path::Path;
19
20use gix_error::ExnMessageResult;
21
22/// A single commit-graph file.
23///
24/// 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`].
26pub struct File {
27    base_graph_count: u8,
28    base_graphs_list_offset: Option<usize>,
29    commit_data_offset: usize,
30    data: memmap2::Mmap,
31    extra_edges_list_range: Option<std::ops::Range<usize>>,
32    fan: [u32; file::FAN_LEN],
33    oid_lookup_offset: usize,
34    path: std::path::PathBuf,
35    hash_len: usize,
36    object_hash: gix_hash::Kind,
37}
38
39/// A complete commit graph.
40///
41/// The data in the commit graph may come from a monolithic `objects/info/commit-graph` file, or it
42/// may come from one or more `objects/info/commit-graphs/graph-*.graph` files. These files are
43/// generated via `git commit-graph write ...` commands.
44pub struct Graph {
45    files: nonempty::NonEmpty<File>,
46}
47
48/// Instantiate a commit graph from an `.git/objects/info` directory, or one of the various commit-graph files.
49pub fn at(path: impl AsRef<Path>) -> ExnMessageResult<Graph> {
50    Graph::at(path.as_ref())
51}
52
53mod access;
54pub mod file;
55///
56pub mod init;
57pub mod verify;
58
59/// The number of generations that are considered 'infinite' commit history.
60pub const GENERATION_NUMBER_INFINITY: u32 = 0xffff_ffff;
61/// The largest valid generation number.
62///
63/// If a commit's real generation number is larger than this, the commit graph will cap the value to
64/// this number.
65/// The largest distinct generation number is `GENERATION_NUMBER_MAX - 1`.
66pub const GENERATION_NUMBER_MAX: u32 = 0x3fff_ffff;
67
68/// The maximum number of commits that can be stored in a commit graph.
69pub const MAX_COMMITS: u32 = (1 << 30) + (1 << 29) + (1 << 28) - 1;
70
71/// A generalized position for use in [`Graph`].
72#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
73pub struct Position(pub u32);
74
75impl std::fmt::Display for Position {
76    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77        self.0.fmt(f)
78    }
79}