Skip to content

Commit 3eba918

Browse files
committed
odb: add git_odb_options
Users will need to be able to specify the object id type for the given object database; add a new `git_odb_options` with that option.
1 parent 8444b6d commit 3eba918

File tree

16 files changed

+61
-25
lines changed

16 files changed

+61
-25
lines changed

fuzzers/packfile_fuzzer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ int LLVMFuzzerInitialize(int *argc, char ***argv)
3636
fprintf(stderr, "Failed to limit maximum pack object count\n");
3737
abort();
3838
}
39-
if (git_odb_new(&odb) < 0) {
39+
if (git_odb_new(&odb, NULL) < 0) {
4040
fprintf(stderr, "Failed to create the odb\n");
4141
abort();
4242
}

include/git2/odb.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,19 @@ typedef enum {
3838
*/
3939
typedef int GIT_CALLBACK(git_odb_foreach_cb)(const git_oid *id, void *payload);
4040

41+
/** Options for configuring a loose object backend. */
42+
typedef struct {
43+
unsigned int version; /**< version for the struct */
44+
} git_odb_options;
45+
46+
/* The current version of the diff options structure */
47+
#define GIT_ODB_OPTIONS_VERSION 1
48+
49+
/* Stack initializer for odb options. Alternatively use
50+
* `git_odb_options_init` programmatic initialization.
51+
*/
52+
#define GIT_ODB_OPTIONS_INIT { GIT_ODB_OPTIONS_VERSION }
53+
4154
/**
4255
* Create a new object database with no backends.
4356
*
@@ -46,9 +59,10 @@ typedef int GIT_CALLBACK(git_odb_foreach_cb)(const git_oid *id, void *payload);
4659
*
4760
* @param out location to store the database pointer, if opened.
4861
* Set to NULL if the open failed.
62+
* @param opts the options for this object database or NULL for defaults
4963
* @return 0 or an error code
5064
*/
51-
GIT_EXTERN(int) git_odb_new(git_odb **out);
65+
GIT_EXTERN(int) git_odb_new(git_odb **out, const git_odb_options *opts);
5266

5367
/**
5468
* Create a new object database and automatically add
@@ -64,9 +78,13 @@ GIT_EXTERN(int) git_odb_new(git_odb **out);
6478
* @param out location to store the database pointer, if opened.
6579
* Set to NULL if the open failed.
6680
* @param objects_dir path of the backends' "objects" directory.
81+
* @param opts the options for this object database or NULL for defaults
6782
* @return 0 or an error code
6883
*/
69-
GIT_EXTERN(int) git_odb_open(git_odb **out, const char *objects_dir);
84+
GIT_EXTERN(int) git_odb_open(
85+
git_odb **out,
86+
const char *objects_dir,
87+
const git_odb_options *opts);
7088

7189
/**
7290
* Add an on-disk alternate to an existing Object DB.

src/libgit2/odb.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -475,11 +475,25 @@ static int backend_sort_cmp(const void *a, const void *b)
475475
return (backend_b->priority - backend_a->priority);
476476
}
477477

478-
int git_odb_new(git_odb **out)
478+
static void normalize_options(
479+
git_odb_options *opts,
480+
const git_odb_options *given_opts)
481+
{
482+
git_odb_options init = GIT_ODB_OPTIONS_INIT;
483+
484+
if (given_opts)
485+
memcpy(opts, given_opts, sizeof(git_odb_options));
486+
else
487+
memcpy(opts, &init, sizeof(git_odb_options));
488+
}
489+
490+
int git_odb_new(git_odb **out, const git_odb_options *opts)
479491
{
480492
git_odb *db = git__calloc(1, sizeof(*db));
481493
GIT_ERROR_CHECK_ALLOC(db);
482494

495+
normalize_options(&db->options, opts);
496+
483497
if (git_mutex_init(&db->lock) < 0) {
484498
git__free(db);
485499
return -1;
@@ -740,7 +754,10 @@ int git_odb_set_commit_graph(git_odb *odb, git_commit_graph *cgraph)
740754
return error;
741755
}
742756

743-
int git_odb_open(git_odb **out, const char *objects_dir)
757+
int git_odb_open(
758+
git_odb **out,
759+
const char *objects_dir,
760+
const git_odb_options *opts)
744761
{
745762
git_odb *db;
746763

@@ -749,7 +766,7 @@ int git_odb_open(git_odb **out, const char *objects_dir)
749766

750767
*out = NULL;
751768

752-
if (git_odb_new(&db) < 0)
769+
if (git_odb_new(&db, opts) < 0)
753770
return -1;
754771

755772
if (git_odb__add_default_backends(db, objects_dir, 0, 0) < 0) {

src/libgit2/odb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ struct git_odb_object {
4646
struct git_odb {
4747
git_refcount rc;
4848
git_mutex lock; /* protects backends */
49+
git_odb_options options;
4950
git_vector backends;
5051
git_cache own_cache;
5152
git_commit_graph *cgraph;

src/libgit2/repository.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ static int _git_repository_open_ext_from_env(
790790
else if (error < 0)
791791
goto error;
792792
else {
793-
error = git_odb_open(&odb, git_str_cstr(&object_dir_buf));
793+
error = git_odb_open(&odb, git_str_cstr(&object_dir_buf), NULL);
794794
if (error < 0)
795795
goto error;
796796
}
@@ -1217,7 +1217,7 @@ int git_repository_odb__weakptr(git_odb **out, git_repository *repo)
12171217

12181218
if ((error = git_repository__item_path(&odb_path, repo,
12191219
GIT_REPOSITORY_ITEM_OBJECTS)) < 0 ||
1220-
(error = git_odb_new(&odb)) < 0)
1220+
(error = git_odb_new(&odb, NULL)) < 0)
12211221
return error;
12221222

12231223
GIT_REFCOUNT_OWN(odb, repo);

tests/libgit2/object/raw/write.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void test_body(object_data *d, git_rawobj *o)
6666
git_rawobj tmp;
6767

6868
make_odb_dir();
69-
cl_git_pass(git_odb_open(&db, odb_dir));
69+
cl_git_pass(git_odb_open(&db, odb_dir, NULL));
7070
cl_git_pass(git_oid_fromstr(&id1, d->id, GIT_OID_SHA1));
7171

7272
streaming_write(&id2, db, o);

tests/libgit2/odb/backend/mempack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void test_odb_backend_mempack__initialize(void)
1313
git_odb_backend *backend;
1414

1515
cl_git_pass(git_mempack_new(&backend));
16-
cl_git_pass(git_odb_new(&_odb));
16+
cl_git_pass(git_odb_new(&_odb, NULL));
1717
cl_git_pass(git_odb_add_backend(_odb, backend, 10));
1818
cl_git_pass(git_repository_wrap_odb(&_repo, _odb));
1919
}

tests/libgit2/odb/backend/nobackend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ void test_odb_backend_nobackend__initialize(void)
1212

1313
cl_git_pass(git_repository_new(&_repo));
1414
cl_git_pass(git_config_new(&config));
15-
cl_git_pass(git_odb_new(&odb));
15+
cl_git_pass(git_odb_new(&odb, NULL));
1616
cl_git_pass(git_refdb_new(&refdb, _repo));
1717

1818
git_repository_set_config(_repo, config);

tests/libgit2/odb/foreach.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void test_odb_foreach__one_pack(void)
5151
git_odb_backend *backend = NULL;
5252
int nobj = 0;
5353

54-
cl_git_pass(git_odb_new(&_odb));
54+
cl_git_pass(git_odb_new(&_odb, NULL));
5555
cl_git_pass(git_odb_backend_one_pack(&backend, cl_fixture("testrepo.git/objects/pack/pack-a81e489679b7d3418f9ab594bda8ceb37dd4c695.idx")));
5656
cl_git_pass(git_odb_add_backend(_odb, backend, 1));
5757
_repo = NULL;

tests/libgit2/odb/loose.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static void test_read_object(object_data *data)
4141

4242
write_object_files(data);
4343

44-
cl_git_pass(git_odb_open(&odb, "test-objects"));
44+
cl_git_pass(git_odb_open(&odb, "test-objects", NULL));
4545
cl_git_pass(git_oid_fromstr(&id, data->id, GIT_OID_SHA1));
4646
cl_git_pass(git_odb_read(&obj, odb, &id));
4747

@@ -64,7 +64,7 @@ static void test_read_header(object_data *data)
6464

6565
write_object_files(data);
6666

67-
cl_git_pass(git_odb_open(&odb, "test-objects"));
67+
cl_git_pass(git_odb_open(&odb, "test-objects", NULL));
6868
cl_git_pass(git_oid_fromstr(&id, data->id, GIT_OID_SHA1));
6969
cl_git_pass(git_odb_read_header(&len, &type, odb, &id));
7070

@@ -86,7 +86,7 @@ static void test_readstream_object(object_data *data, size_t blocksize)
8686

8787
write_object_files(data);
8888

89-
cl_git_pass(git_odb_open(&odb, "test-objects"));
89+
cl_git_pass(git_odb_open(&odb, "test-objects", NULL));
9090
cl_git_pass(git_oid_fromstr(&id, data->id, GIT_OID_SHA1));
9191
cl_git_pass(git_odb_open_rstream(&stream, &tmp.len, &tmp.type, odb, &id));
9292

@@ -130,7 +130,7 @@ void test_odb_loose__exists(void)
130130
git_odb *odb;
131131

132132
write_object_files(&one);
133-
cl_git_pass(git_odb_open(&odb, "test-objects"));
133+
cl_git_pass(git_odb_open(&odb, "test-objects", NULL));
134134

135135
cl_git_pass(git_oid_fromstr(&id, one.id, GIT_OID_SHA1));
136136
cl_assert(git_odb_exists(odb, &id));
@@ -209,7 +209,7 @@ static void test_write_object_permission(
209209
mask = p_umask(0);
210210
p_umask(mask);
211211

212-
cl_git_pass(git_odb_new(&odb));
212+
cl_git_pass(git_odb_new(&odb, NULL));
213213
cl_git_pass(git_odb_backend_loose(&backend, "test-objects", -1, 0, dir_mode, file_mode));
214214
cl_git_pass(git_odb_add_backend(odb, backend, 1));
215215
cl_git_pass(git_odb_write(&oid, odb, "Test data\n", 10, GIT_OBJECT_BLOB));
@@ -244,7 +244,7 @@ static void write_object_to_loose_odb(int fsync)
244244
git_odb_backend *backend;
245245
git_oid oid;
246246

247-
cl_git_pass(git_odb_new(&odb));
247+
cl_git_pass(git_odb_new(&odb, NULL));
248248
cl_git_pass(git_odb_backend_loose(&backend, "test-objects", -1, fsync, 0777, 0666));
249249
cl_git_pass(git_odb_add_backend(odb, backend, 1));
250250
cl_git_pass(git_odb_write(&oid, odb, "Test data\n", 10, GIT_OBJECT_BLOB));

0 commit comments

Comments
 (0)