2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
8 #ifndef INCLUDE_commit_graph_h__
9 #define INCLUDE_commit_graph_h__
13 #include "git2/types.h"
14 #include "git2/sys/commit_graph.h"
22 * A commit-graph file.
24 * This file contains metadata about commits, particularly the generation
25 * number for each one. This can help speed up graph operations without
26 * requiring a full graph traversal.
28 * Support for this feature was added in git 2.19.
30 typedef struct git_commit_graph_file
{
33 /* The OID Fanout table. */
34 const uint32_t *oid_fanout
;
35 /* The total number of commits in the graph. */
38 /* The OID Lookup table. */
39 unsigned char *oid_lookup
;
42 * The Commit Data table. Each entry contains the OID of the commit followed
43 * by two 8-byte fields in network byte order:
44 * - The indices of the first two parents (32 bits each).
45 * - The generation number (first 30 bits) and commit time in seconds since
46 * UNIX epoch (34 bits).
48 const unsigned char *commit_data
;
51 * The Extra Edge List table. Each 4-byte entry is a network byte order index
52 * of one of the i-th (i > 0) parents of commits in the `commit_data` table,
53 * when the commit has more than 2 parents.
55 const unsigned char *extra_edge_list
;
56 /* The number of entries in the Extra Edge List table. Each entry is 4 bytes wide. */
57 size_t num_extra_edge_list
;
59 /* The trailer of the file. Contains the SHA1-checksum of the whole file. */
60 unsigned char checksum
[GIT_HASH_SHA1_SIZE
];
61 } git_commit_graph_file
;
64 * An entry in the commit-graph file. Provides a subset of the information that
65 * can be obtained from the commit header.
67 typedef struct git_commit_graph_entry
{
68 /* The generation number of the commit within the graph */
71 /* Time in seconds from UNIX epoch. */
72 git_time_t commit_time
;
74 /* The number of parents of the commit. */
78 * The indices of the parent commits within the Commit Data table. The value
79 * of `GIT_COMMIT_GRAPH_MISSING_PARENT` indicates that no parent is in that
82 size_t parent_indices
[2];
84 /* The index within the Extra Edge List of any parent after the first two. */
85 size_t extra_parents_index
;
87 /* The SHA-1 hash of the root tree of the commit. */
90 /* The SHA-1 hash of the requested commit. */
92 } git_commit_graph_entry
;
94 /* A wrapper for git_commit_graph_file to enable lazy loading in the ODB. */
95 struct git_commit_graph
{
96 /* The path to the commit-graph file. Something like ".git/objects/info/commit-graph". */
99 /* The underlying commit-graph file. */
100 git_commit_graph_file
*file
;
102 /* Whether the commit-graph file was already checked for validity. */
106 /** Create a new commit-graph, optionally opening the underlying file. */
107 int git_commit_graph_new(git_commit_graph
**cgraph_out
, const char *objects_dir
, bool open_file
);
109 /** Open and validate a commit-graph file. */
110 int git_commit_graph_file_open(git_commit_graph_file
**file_out
, const char *path
);
113 * Attempt to get the git_commit_graph's commit-graph file. This object is
114 * still owned by the git_commit_graph. If the repository does not contain a commit graph,
115 * it will return GIT_ENOTFOUND.
117 * This function is not thread-safe.
119 int git_commit_graph_get_file(git_commit_graph_file
**file_out
, git_commit_graph
*cgraph
);
121 /* Marks the commit-graph file as needing a refresh. */
122 void git_commit_graph_refresh(git_commit_graph
*cgraph
);
125 * A writer for `commit-graph` files.
127 struct git_commit_graph_writer
{
129 * The path of the `objects/info` directory where the `commit-graph` will be
132 git_str objects_info_dir
;
134 /* The list of packed commits. */
138 int git_commit_graph__writer_dump(
140 git_commit_graph_writer
*w
,
141 git_commit_graph_writer_options
*opts
);
144 * Returns whether the git_commit_graph_file needs to be reloaded since the
145 * contents of the commit-graph file have changed on disk.
147 bool git_commit_graph_file_needs_refresh(
148 const git_commit_graph_file
*file
, const char *path
);
150 int git_commit_graph_entry_find(
151 git_commit_graph_entry
*e
,
152 const git_commit_graph_file
*file
,
153 const git_oid
*short_oid
,
155 int git_commit_graph_entry_parent(
156 git_commit_graph_entry
*parent
,
157 const git_commit_graph_file
*file
,
158 const git_commit_graph_entry
*entry
,
160 int git_commit_graph_file_close(git_commit_graph_file
*cgraph
);
161 void git_commit_graph_file_free(git_commit_graph_file
*cgraph
);
163 /* This is exposed for use in the fuzzers. */
164 int git_commit_graph_file_parse(
165 git_commit_graph_file
*file
,
166 const unsigned char *data
,