Skip to content

Commit 8a7fe96

Browse files
committed
commit-graph: Add a way to write commit-graph files
This change adds the git_commit_graph_writer_* functions to allow to write and create `commit-graph` files from `.idx`/`.pack` files or `git_revwalk`s. Part of: libgit2#5757
1 parent 033048d commit 8a7fe96

File tree

5 files changed

+828
-2
lines changed

5 files changed

+828
-2
lines changed

include/git2/sys/commit_graph.h

Lines changed: 131 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,136 @@ GIT_EXTERN(int) git_commit_graph_open(git_commit_graph **cgraph_out, const char
4040
*/
4141
GIT_EXTERN(void) git_commit_graph_free(git_commit_graph *cgraph);
4242

43-
GIT_END_DECL
43+
/**
44+
* Create a new writer for `commit-graph` files.
45+
*
46+
* @param out Location to store the writer pointer.
47+
* @param objects_info_dir The `objects/info` directory.
48+
* The `commit-graph` file will be written in this directory.
49+
* @return 0 or an error code
50+
*/
51+
GIT_EXTERN(int) git_commit_graph_writer_new(
52+
git_commit_graph_writer **out,
53+
const char *objects_info_dir);
54+
55+
/**
56+
* Free the commit-graph writer and its resources.
57+
*
58+
* @param w The writer to free. If NULL no action is taken.
59+
*/
60+
GIT_EXTERN(void) git_commit_graph_writer_free(git_commit_graph_writer *w);
61+
62+
/**
63+
* Add an `.idx` file (associated to a packfile) to the writer.
64+
*
65+
* @param w The writer.
66+
* @param repo The repository that owns the `.idx` file.
67+
* @param idx_path The path of an `.idx` file.
68+
* @return 0 or an error code
69+
*/
70+
GIT_EXTERN(int) git_commit_graph_writer_add_index_file(
71+
git_commit_graph_writer *w,
72+
git_repository *repo,
73+
const char *idx_path);
74+
75+
/**
76+
* Add a revwalk to the writer. This will add all the commits from the revwalk
77+
* to the commit-graph.
78+
*
79+
* @param w The writer.
80+
* @param walk The git_revwalk.
81+
* @return 0 or an error code
82+
*/
83+
GIT_EXTERN(int) git_commit_graph_writer_add_revwalk(
84+
git_commit_graph_writer *w,
85+
git_revwalk *walk);
86+
4487

88+
/**
89+
* The strategy to use when adding a new set of commits to a pre-existing
90+
* commit-graph chain.
91+
*/
92+
typedef enum {
93+
/**
94+
* Do not split commit-graph files. The other split strategy-related option
95+
* fields are ignored.
96+
*/
97+
GIT_COMMIT_GRAPH_SPLIT_STRATEGY_SINGLE_FILE = 0,
98+
} git_commit_graph_split_strategy_t;
99+
100+
/**
101+
* Options structure for
102+
* `git_commit_graph_writer_commit`/`git_commit_graph_writer_dump`.
103+
*
104+
* Initialize with `GIT_COMMIT_GRAPH_WRITER_OPTIONS_INIT`. Alternatively, you
105+
* can use `git_commit_graph_writer_options_init`.
106+
*/
107+
typedef struct {
108+
unsigned int version;
109+
110+
/**
111+
* The strategy to use when adding new commits to a pre-existing commit-graph
112+
* chain.
113+
*/
114+
git_commit_graph_split_strategy_t split_strategy;
115+
116+
/**
117+
* The number of commits in level N is less than X times the number of
118+
* commits in level N + 1.
119+
*/
120+
float size_multiple;
121+
122+
/**
123+
* The number of commits in level N + 1 is more than C commits.
124+
*/
125+
size_t max_commits;
126+
} git_commit_graph_writer_options;
127+
128+
#define GIT_COMMIT_GRAPH_WRITER_OPTIONS_VERSION 1
129+
#define GIT_COMMIT_GRAPH_WRITER_OPTIONS_INIT \
130+
{ \
131+
GIT_COMMIT_GRAPH_WRITER_OPTIONS_VERSION, \
132+
GIT_COMMIT_GRAPH_SPLIT_STRATEGY_SINGLE_FILE, 2.0f, 64000 \
133+
}
134+
135+
/**
136+
* Initialize git_commit_graph_writer_options structure
137+
*
138+
* Initializes a `git_commit_graph_writer_options` with default values. Equivalent to
139+
* creating an instance with `GIT_COMMIT_GRAPH_WRITER_OPTIONS_INIT`.
140+
*
141+
* @param opts The `git_commit_graph_writer_options` struct to initialize.
142+
* @param version The struct version; pass `GIT_COMMIT_GRAPH_WRITER_OPTIONS_VERSION`.
143+
* @return Zero on success; -1 on failure.
144+
*/
145+
GIT_EXTERN(int) git_commit_graph_writer_options_init(
146+
git_commit_graph_writer_options *opts,
147+
unsigned int version);
148+
149+
/**
150+
* Write a `commit-graph` file to a file.
151+
*
152+
* @param w The writer
153+
* @param opts Pointer to git_commit_graph_writer_options struct.
154+
* @return 0 or an error code
155+
*/
156+
GIT_EXTERN(int) git_commit_graph_writer_commit(
157+
git_commit_graph_writer *w,
158+
git_commit_graph_writer_options *opts);
159+
160+
/**
161+
* Dump the contents of the `commit-graph` to an in-memory buffer.
162+
*
163+
* @param buffer Buffer where to store the contents of the `commit-graph`.
164+
* @param w The writer.
165+
* @param opts Pointer to git_commit_graph_writer_options struct.
166+
* @return 0 or an error code
167+
*/
168+
GIT_EXTERN(int) git_commit_graph_writer_dump(
169+
git_buf *buffer,
170+
git_commit_graph_writer *w,
171+
git_commit_graph_writer_options *opts);
172+
173+
/** @} */
174+
GIT_END_DECL
45175
#endif

include/git2/types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ typedef struct git_odb_stream git_odb_stream;
9696
/** A stream to write a packfile to the ODB */
9797
typedef struct git_odb_writepack git_odb_writepack;
9898

99+
/** a writer for commit-graph files. */
100+
typedef struct git_commit_graph_writer git_commit_graph_writer;
101+
99102
/** An open refs database handle. */
100103
typedef struct git_refdb git_refdb;
101104

0 commit comments

Comments
 (0)