From 83862c83748b03d473a58d983916bec0bc385b62 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Mon, 17 Feb 2020 21:28:13 +0000 Subject: 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 --- include/git2/sys/commit_graph.h | 132 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 131 insertions(+), 1 deletion(-) (limited to 'include/git2/sys') diff --git a/include/git2/sys/commit_graph.h b/include/git2/sys/commit_graph.h index 038c9b739..01cf8d1bf 100644 --- a/include/git2/sys/commit_graph.h +++ b/include/git2/sys/commit_graph.h @@ -40,6 +40,136 @@ GIT_EXTERN(int) git_commit_graph_open(git_commit_graph **cgraph_out, const char */ GIT_EXTERN(void) git_commit_graph_free(git_commit_graph *cgraph); -GIT_END_DECL +/** + * Create a new writer for `commit-graph` files. + * + * @param out Location to store the writer pointer. + * @param objects_info_dir The `objects/info` directory. + * The `commit-graph` file will be written in this directory. + * @return 0 or an error code + */ +GIT_EXTERN(int) git_commit_graph_writer_new( + git_commit_graph_writer **out, + const char *objects_info_dir); + +/** + * Free the commit-graph writer and its resources. + * + * @param w The writer to free. If NULL no action is taken. + */ +GIT_EXTERN(void) git_commit_graph_writer_free(git_commit_graph_writer *w); + +/** + * Add an `.idx` file (associated to a packfile) to the writer. + * + * @param w The writer. + * @param repo The repository that owns the `.idx` file. + * @param idx_path The path of an `.idx` file. + * @return 0 or an error code + */ +GIT_EXTERN(int) git_commit_graph_writer_add_index_file( + git_commit_graph_writer *w, + git_repository *repo, + const char *idx_path); + +/** + * Add a revwalk to the writer. This will add all the commits from the revwalk + * to the commit-graph. + * + * @param w The writer. + * @param walk The git_revwalk. + * @return 0 or an error code + */ +GIT_EXTERN(int) git_commit_graph_writer_add_revwalk( + git_commit_graph_writer *w, + git_revwalk *walk); + +/** + * The strategy to use when adding a new set of commits to a pre-existing + * commit-graph chain. + */ +typedef enum { + /** + * Do not split commit-graph files. The other split strategy-related option + * fields are ignored. + */ + GIT_COMMIT_GRAPH_SPLIT_STRATEGY_SINGLE_FILE = 0, +} git_commit_graph_split_strategy_t; + +/** + * Options structure for + * `git_commit_graph_writer_commit`/`git_commit_graph_writer_dump`. + * + * Initialize with `GIT_COMMIT_GRAPH_WRITER_OPTIONS_INIT`. Alternatively, you + * can use `git_commit_graph_writer_options_init`. + */ +typedef struct { + unsigned int version; + + /** + * The strategy to use when adding new commits to a pre-existing commit-graph + * chain. + */ + git_commit_graph_split_strategy_t split_strategy; + + /** + * The number of commits in level N is less than X times the number of + * commits in level N + 1. + */ + float size_multiple; + + /** + * The number of commits in level N + 1 is more than C commits. + */ + size_t max_commits; +} git_commit_graph_writer_options; + +#define GIT_COMMIT_GRAPH_WRITER_OPTIONS_VERSION 1 +#define GIT_COMMIT_GRAPH_WRITER_OPTIONS_INIT \ + { \ + GIT_COMMIT_GRAPH_WRITER_OPTIONS_VERSION, \ + GIT_COMMIT_GRAPH_SPLIT_STRATEGY_SINGLE_FILE, 2.0f, 64000 \ + } + +/** + * Initialize git_commit_graph_writer_options structure + * + * Initializes a `git_commit_graph_writer_options` with default values. Equivalent to + * creating an instance with `GIT_COMMIT_GRAPH_WRITER_OPTIONS_INIT`. + * + * @param opts The `git_commit_graph_writer_options` struct to initialize. + * @param version The struct version; pass `GIT_COMMIT_GRAPH_WRITER_OPTIONS_VERSION`. + * @return Zero on success; -1 on failure. + */ +GIT_EXTERN(int) git_commit_graph_writer_options_init( + git_commit_graph_writer_options *opts, + unsigned int version); + +/** + * Write a `commit-graph` file to a file. + * + * @param w The writer + * @param opts Pointer to git_commit_graph_writer_options struct. + * @return 0 or an error code + */ +GIT_EXTERN(int) git_commit_graph_writer_commit( + git_commit_graph_writer *w, + git_commit_graph_writer_options *opts); + +/** + * Dump the contents of the `commit-graph` to an in-memory buffer. + * + * @param buffer Buffer where to store the contents of the `commit-graph`. + * @param w The writer. + * @param opts Pointer to git_commit_graph_writer_options struct. + * @return 0 or an error code + */ +GIT_EXTERN(int) git_commit_graph_writer_dump( + git_buf *buffer, + git_commit_graph_writer *w, + git_commit_graph_writer_options *opts); + +/** @} */ +GIT_END_DECL #endif -- cgit v1.2.1 From 63f08e4258122d6f6ea1f04ec8c08779bf300b6c Mon Sep 17 00:00:00 2001 From: lhchavez Date: Thu, 26 Aug 2021 05:29:34 -0700 Subject: Make the defaultable fields defaultable Also, add `git_commit_graph_writer_options_init`! --- include/git2/sys/commit_graph.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'include/git2/sys') diff --git a/include/git2/sys/commit_graph.h b/include/git2/sys/commit_graph.h index 01cf8d1bf..67d95469c 100644 --- a/include/git2/sys/commit_graph.h +++ b/include/git2/sys/commit_graph.h @@ -115,12 +115,13 @@ typedef struct { /** * The number of commits in level N is less than X times the number of - * commits in level N + 1. + * commits in level N + 1. Default is 2. */ float size_multiple; /** * The number of commits in level N + 1 is more than C commits. + * Default is 64000. */ size_t max_commits; } git_commit_graph_writer_options; @@ -129,7 +130,6 @@ typedef struct { #define GIT_COMMIT_GRAPH_WRITER_OPTIONS_INIT \ { \ GIT_COMMIT_GRAPH_WRITER_OPTIONS_VERSION, \ - GIT_COMMIT_GRAPH_SPLIT_STRATEGY_SINGLE_FILE, 2.0f, 64000 \ } /** -- cgit v1.2.1 From 34fa6311423f55827184c6338226dde4ee46d4e5 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Mon, 30 Aug 2021 17:55:13 -0400 Subject: commit graph: formatting fixes --- include/git2/sys/commit_graph.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'include/git2/sys') diff --git a/include/git2/sys/commit_graph.h b/include/git2/sys/commit_graph.h index 67d95469c..f6c0fc4b5 100644 --- a/include/git2/sys/commit_graph.h +++ b/include/git2/sys/commit_graph.h @@ -127,9 +127,8 @@ typedef struct { } git_commit_graph_writer_options; #define GIT_COMMIT_GRAPH_WRITER_OPTIONS_VERSION 1 -#define GIT_COMMIT_GRAPH_WRITER_OPTIONS_INIT \ - { \ - GIT_COMMIT_GRAPH_WRITER_OPTIONS_VERSION, \ +#define GIT_COMMIT_GRAPH_WRITER_OPTIONS_INIT { \ + GIT_COMMIT_GRAPH_WRITER_OPTIONS_VERSION \ } /** -- cgit v1.2.1