Skip to content

Commit 019346e

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: #5757
1 parent 744c6bb commit 019346e

File tree

5 files changed

+853
-1
lines changed

5 files changed

+853
-1
lines changed

include/git2/sys/commit_graph.h

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
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.
6+
*/
7+
#ifndef INCLUDE_sys_git_commit_graph_h__
8+
#define INCLUDE_sys_git_commit_graph_h__
9+
10+
#include "git2/common.h"
11+
#include "git2/types.h"
12+
13+
/**
14+
* @file git2/commit_graph.h
15+
* @brief Git commit-graph routines
16+
* @defgroup git_commit_graph Git commit-graph routines
17+
* @ingroup Git
18+
* @{
19+
*/
20+
GIT_BEGIN_DECL
21+
22+
/**
23+
* Create a new writer for `commit-graph` files.
24+
*
25+
* @param out Location to store the writer pointer.
26+
* @param objects_info_dir The `objects/info` directory.
27+
* The `commit-graph` file will be written in this directory.
28+
* @return 0 or an error code
29+
*/
30+
GIT_EXTERN(int) git_commit_graph_writer_new(
31+
git_commit_graph_writer **out,
32+
const char *objects_info_dir);
33+
34+
/**
35+
* Free the commit-graph writer and its resources.
36+
*
37+
* @param w The writer to free. If NULL no action is taken.
38+
*/
39+
GIT_EXTERN(void) git_commit_graph_writer_free(git_commit_graph_writer *w);
40+
41+
/**
42+
* Add an `.idx` file (associated to a packfile) to the writer.
43+
*
44+
* @param w The writer.
45+
* @param repo The repository that owns the `.idx` file.
46+
* @param idx_path The path of an `.idx` file.
47+
* @return 0 or an error code
48+
*/
49+
GIT_EXTERN(int) git_commit_graph_writer_add_index_file(
50+
git_commit_graph_writer *w,
51+
git_repository *repo,
52+
const char *idx_path);
53+
54+
/**
55+
* Add a revwalk to the writer. This will add all the commits from the revwalk
56+
* to the commit-graph.
57+
*
58+
* @param w The writer.
59+
* @param walk The git_revwalk.
60+
* @return 0 or an error code
61+
*/
62+
GIT_EXTERN(int) git_commit_graph_writer_add_revwalk(
63+
git_commit_graph_writer *w,
64+
git_revwalk *walk);
65+
66+
67+
/**
68+
* The strategy to use when adding a new set of commits to a pre-existing
69+
* commit-graph chain.
70+
*/
71+
typedef enum {
72+
/**
73+
* Do not split commit-graph files. The other split strategy-related option
74+
* fields are ignored.
75+
*/
76+
GIT_COMMIT_GRAPH_SPLIT_STRATEGY_SINGLE_FILE = 0,
77+
} git_commit_graph_split_strategy_t;
78+
79+
/**
80+
* Options structure for
81+
* `git_commit_graph_writer_commit`/`git_commit_graph_writer_dump`.
82+
*
83+
* Initialize with `GIT_COMMIT_GRAPH_WRITER_OPTIONS_INIT`. Alternatively, you
84+
* can use `git_commit_graph_writer_options_init`.
85+
*/
86+
typedef struct {
87+
unsigned int version;
88+
89+
/**
90+
* The strategy to use when adding new commits to a pre-existing commit-graph
91+
* chain.
92+
*/
93+
git_commit_graph_split_strategy_t split_strategy;
94+
95+
/**
96+
* The number of commits in level N is less than X times the number of
97+
* commits in level N + 1.
98+
*/
99+
float size_multiple;
100+
101+
/**
102+
* The number of commits in level N + 1 is more than C commits.
103+
*/
104+
size_t max_commits;
105+
} git_commit_graph_writer_options;
106+
107+
#define GIT_COMMIT_GRAPH_WRITER_OPTIONS_VERSION 1
108+
#define GIT_COMMIT_GRAPH_WRITER_OPTIONS_INIT \
109+
{ \
110+
GIT_COMMIT_GRAPH_WRITER_OPTIONS_VERSION, \
111+
GIT_COMMIT_GRAPH_SPLIT_STRATEGY_SINGLE_FILE, 2.0f, 64000 \
112+
}
113+
114+
/**
115+
* Initialize git_commit_graph_writer_options structure
116+
*
117+
* Initializes a `git_commit_graph_writer_options` with default values. Equivalent to
118+
* creating an instance with `GIT_COMMIT_GRAPH_WRITER_OPTIONS_INIT`.
119+
*
120+
* @param opts The `git_commit_graph_writer_options` struct to initialize.
121+
* @param version The struct version; pass `GIT_COMMIT_GRAPH_WRITER_OPTIONS_VERSION`.
122+
* @return Zero on success; -1 on failure.
123+
*/
124+
GIT_EXTERN(int) git_commit_graph_writer_options_init(
125+
git_commit_graph_writer_options *opts,
126+
unsigned int version);
127+
128+
/**
129+
* Write a `commit-graph` file to a file.
130+
*
131+
* @param w The writer
132+
* @param opts Pointer to git_commit_graph_writer_options struct.
133+
* @return 0 or an error code
134+
*/
135+
GIT_EXTERN(int) git_commit_graph_writer_commit(
136+
git_commit_graph_writer *w,
137+
git_commit_graph_writer_options *opts);
138+
139+
/**
140+
* Dump the contents of the `commit-graph` to an in-memory buffer.
141+
*
142+
* @param buffer Buffer where to store the contents of the `commit-graph`.
143+
* @param w The writer.
144+
* @param opts Pointer to git_commit_graph_writer_options struct.
145+
* @return 0 or an error code
146+
*/
147+
GIT_EXTERN(int) git_commit_graph_writer_dump(
148+
git_buf *buffer,
149+
git_commit_graph_writer *w,
150+
git_commit_graph_writer_options *opts);
151+
152+
/** @} */
153+
GIT_END_DECL
154+
#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)