From 9aa5faa38b1be87c8d59d661581271276c05171b Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Mon, 16 Dec 2024 16:58:50 +0000 Subject: [PATCH] indexer: move oid_type into the opts structure Object ID type should be an option within the options structure; move it there. --- examples/index-pack.c | 2 +- fuzzers/packfile_fuzzer.c | 2 +- include/git2/indexer.h | 6 ++++-- src/cli/cmd_index_pack.c | 4 +++- src/libgit2/indexer.c | 8 +++++--- src/libgit2/odb_pack.c | 2 +- src/libgit2/pack-objects.c | 3 ++- tests/libgit2/pack/indexer.c | 19 ++++++++++--------- tests/libgit2/pack/packbuilder.c | 6 +++--- 9 files changed, 30 insertions(+), 22 deletions(-) diff --git a/examples/index-pack.c b/examples/index-pack.c index 0f8234c75..e9f32b8b2 100644 --- a/examples/index-pack.c +++ b/examples/index-pack.c @@ -29,7 +29,7 @@ int lg2_index_pack(git_repository *repo, int argc, char **argv) } #ifdef GIT_EXPERIMENTAL_SHA256 - error = git_indexer_new(&idx, ".", git_repository_oid_type(repo), NULL); + error = git_indexer_new(&idx, ".", NULL); #else error = git_indexer_new(&idx, ".", 0, NULL, NULL); #endif diff --git a/fuzzers/packfile_fuzzer.c b/fuzzers/packfile_fuzzer.c index aeba9575c..bcbdd8bc4 100644 --- a/fuzzers/packfile_fuzzer.c +++ b/fuzzers/packfile_fuzzer.c @@ -84,7 +84,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) } #ifdef GIT_EXPERIMENTAL_SHA256 - error = git_indexer_new(&indexer, ".", GIT_OID_SHA1, NULL); + error = git_indexer_new(&indexer, ".", NULL); #else error = git_indexer_new(&indexer, ".", 0, odb, NULL); #endif diff --git a/include/git2/indexer.h b/include/git2/indexer.h index d47ce2c18..9aaedc3c4 100644 --- a/include/git2/indexer.h +++ b/include/git2/indexer.h @@ -77,6 +77,9 @@ typedef struct git_indexer_options { /** permissions to use creating packfile or 0 for defaults */ unsigned int mode; + /** the type of object ids in the packfile or 0 for SHA1 */ + git_oid_t oid_type; + /** * object database from which to read base objects when * fixing thin packs. This can be NULL if there are no thin @@ -120,13 +123,12 @@ GIT_EXTERN(int) git_indexer_options_init( * * @param out where to store the indexer instance * @param path to the directory where the packfile should be stored - * @param oid_type the oid type to use for objects + * @param opts the options to create the indexer with * @return 0 or an error code. */ GIT_EXTERN(int) git_indexer_new( git_indexer **out, const char *path, - git_oid_t oid_type, git_indexer_options *opts); #else /** diff --git a/src/cli/cmd_index_pack.c b/src/cli/cmd_index_pack.c index b8e9749de..6a67990b4 100644 --- a/src/cli/cmd_index_pack.c +++ b/src/cli/cmd_index_pack.c @@ -78,7 +78,9 @@ int cmd_index_pack(int argc, char **argv) } #ifdef GIT_EXPERIMENTAL_SHA256 - ret = git_indexer_new(&idx, ".", GIT_OID_SHA1, &idx_opts); + idx_opts.oid_type = GIT_OID_SHA1; + + ret = git_indexer_new(&idx, ".", &idx_opts); #else ret = git_indexer_new(&idx, ".", 0, NULL, &idx_opts); #endif diff --git a/src/libgit2/indexer.c b/src/libgit2/indexer.c index 32a37e067..e62daacfa 100644 --- a/src/libgit2/indexer.c +++ b/src/libgit2/indexer.c @@ -171,9 +171,12 @@ static int indexer_new( if (in_opts) memcpy(&opts, in_opts, sizeof(opts)); + if (oid_type) + GIT_ASSERT_ARG(git_oid_type_is_valid(oid_type)); + idx = git__calloc(1, sizeof(git_indexer)); GIT_ERROR_CHECK_ALLOC(idx); - idx->oid_type = oid_type; + idx->oid_type = oid_type ? oid_type : GIT_OID_DEFAULT; idx->odb = odb; idx->progress_cb = opts.progress_cb; idx->progress_payload = opts.progress_cb_payload; @@ -233,13 +236,12 @@ cleanup: int git_indexer_new( git_indexer **out, const char *prefix, - git_oid_t oid_type, git_indexer_options *opts) { return indexer_new( out, prefix, - oid_type, + opts ? opts->oid_type : 0, opts ? opts->mode : 0, opts ? opts->odb : NULL, opts); diff --git a/src/libgit2/odb_pack.c b/src/libgit2/odb_pack.c index 96d3a2be4..430a54a3b 100644 --- a/src/libgit2/odb_pack.c +++ b/src/libgit2/odb_pack.c @@ -743,10 +743,10 @@ static int pack_backend__writepack(struct git_odb_writepack **out, #ifdef GIT_EXPERIMENTAL_SHA256 opts.odb = odb; + opts.oid_type = backend->opts.oid_type; error = git_indexer_new(&writepack->indexer, backend->pack_folder, - backend->opts.oid_type, &opts); #else error = git_indexer_new(&writepack->indexer, diff --git a/src/libgit2/pack-objects.c b/src/libgit2/pack-objects.c index 298e70aa6..ced98e8c8 100644 --- a/src/libgit2/pack-objects.c +++ b/src/libgit2/pack-objects.c @@ -1442,8 +1442,9 @@ int git_packbuilder_write( #ifdef GIT_EXPERIMENTAL_SHA256 opts.mode = mode; opts.odb = pb->odb; + opts.oid_type = GIT_OID_SHA1; - error = git_indexer_new(&indexer, path, GIT_OID_SHA1, &opts); + error = git_indexer_new(&indexer, path, &opts); #else error = git_indexer_new(&indexer, path, mode, pb->odb, &opts); #endif diff --git a/tests/libgit2/pack/indexer.c b/tests/libgit2/pack/indexer.c index 9722decaf..023eb5da7 100644 --- a/tests/libgit2/pack/indexer.c +++ b/tests/libgit2/pack/indexer.c @@ -101,7 +101,7 @@ void test_pack_indexer__out_of_order(void) git_indexer_progress stats = { 0 }; #ifdef GIT_EXPERIMENTAL_SHA256 - cl_git_pass(git_indexer_new(&idx, ".", GIT_OID_SHA1, NULL)); + cl_git_pass(git_indexer_new(&idx, ".", NULL)); #else cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL)); #endif @@ -123,7 +123,7 @@ void test_pack_indexer__missing_trailer(void) git_indexer_progress stats = { 0 }; #ifdef GIT_EXPERIMENTAL_SHA256 - cl_git_pass(git_indexer_new(&idx, ".", GIT_OID_SHA1, NULL)); + cl_git_pass(git_indexer_new(&idx, ".", NULL)); #else cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL)); #endif @@ -144,7 +144,7 @@ void test_pack_indexer__leaky(void) git_indexer_progress stats = { 0 }; #ifdef GIT_EXPERIMENTAL_SHA256 - cl_git_pass(git_indexer_new(&idx, ".", GIT_OID_SHA1, NULL)); + cl_git_pass(git_indexer_new(&idx, ".", NULL)); #else cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL)); #endif @@ -178,7 +178,7 @@ void test_pack_indexer__fix_thin(void) #ifdef GIT_EXPERIMENTAL_SHA256 opts.odb = odb; - cl_git_pass(git_indexer_new(&idx, ".", GIT_OID_SHA1, &opts)); + cl_git_pass(git_indexer_new(&idx, ".", &opts)); #else cl_git_pass(git_indexer_new(&idx, ".", 0, odb, &opts)); #endif @@ -215,7 +215,7 @@ void test_pack_indexer__fix_thin(void) cl_git_pass(p_stat(name, &st)); #ifdef GIT_EXPERIMENTAL_SHA256 - cl_git_pass(git_indexer_new(&idx, ".", GIT_OID_SHA1, NULL)); + cl_git_pass(git_indexer_new(&idx, ".", NULL)); #else cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL)); #endif @@ -255,7 +255,8 @@ void test_pack_indexer__corrupt_length(void) #ifdef GIT_EXPERIMENTAL_SHA256 opts.odb = odb; - cl_git_pass(git_indexer_new(&idx, ".", GIT_OID_SHA1, &opts)); + opts.oid_type = GIT_OID_SHA1; + cl_git_pass(git_indexer_new(&idx, ".", &opts)); #else cl_git_pass(git_indexer_new(&idx, ".", 0, odb, &opts)); #endif @@ -281,7 +282,7 @@ void test_pack_indexer__incomplete_pack_fails_with_strict(void) opts.verify = 1; #ifdef GIT_EXPERIMENTAL_SHA256 - cl_git_pass(git_indexer_new(&idx, ".", GIT_OID_SHA1, &opts)); + cl_git_pass(git_indexer_new(&idx, ".", &opts)); #else cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, &opts)); #endif @@ -306,7 +307,7 @@ void test_pack_indexer__out_of_order_with_connectivity_checks(void) opts.verify = 1; #ifdef GIT_EXPERIMENTAL_SHA256 - cl_git_pass(git_indexer_new(&idx, ".", GIT_OID_SHA1, &opts)); + cl_git_pass(git_indexer_new(&idx, ".", &opts)); #else cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, &opts)); #endif @@ -354,7 +355,7 @@ void test_pack_indexer__no_tmp_files(void) cl_assert(git_str_len(&first_tmp_file) == 0); #ifdef GIT_EXPERIMENTAL_SHA256 - cl_git_pass(git_indexer_new(&idx, ".", GIT_OID_SHA1, NULL)); + cl_git_pass(git_indexer_new(&idx, ".", NULL)); #else cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL)); #endif diff --git a/tests/libgit2/pack/packbuilder.c b/tests/libgit2/pack/packbuilder.c index 05dea29d1..2c598b6ee 100644 --- a/tests/libgit2/pack/packbuilder.c +++ b/tests/libgit2/pack/packbuilder.c @@ -102,7 +102,7 @@ void test_pack_packbuilder__create_pack(void) seed_packbuilder(); #ifdef GIT_EXPERIMENTAL_SHA256 - cl_git_pass(git_indexer_new(&_indexer, ".", GIT_OID_SHA1, NULL)); + cl_git_pass(git_indexer_new(&_indexer, ".", NULL)); #else cl_git_pass(git_indexer_new(&_indexer, ".", 0, NULL, NULL)); #endif @@ -261,7 +261,7 @@ void test_pack_packbuilder__foreach(void) seed_packbuilder(); #ifdef GIT_EXPERIMENTAL_SHA256 - cl_git_pass(git_indexer_new(&idx, ".", GIT_OID_SHA1, NULL)); + cl_git_pass(git_indexer_new(&idx, ".", NULL)); #else cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL)); #endif @@ -285,7 +285,7 @@ void test_pack_packbuilder__foreach_with_cancel(void) seed_packbuilder(); #ifdef GIT_EXPERIMENTAL_SHA256 - cl_git_pass(git_indexer_new(&idx, ".", GIT_OID_SHA1, NULL)); + cl_git_pass(git_indexer_new(&idx, ".", NULL)); #else cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL)); #endif