Skip to content

Commit c16556a

Browse files
committed
indexer: introduce options struct to git_indexer_new
We strive to keep an options structure to many functions to be able to extend options in the future without breaking the API. `git_indexer_new` doesn't have one right now, but we want to be able to add an option for enabling strict packfile verification. Add a new `git_indexer_options` structure and adjust callers to use that.
1 parent a616fb1 commit c16556a

File tree

7 files changed

+62
-21
lines changed

7 files changed

+62
-21
lines changed

examples/network/index-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ int index_pack(git_repository *repo, int argc, char **argv)
4646
return EXIT_FAILURE;
4747
}
4848

49-
if (git_indexer_new(&idx, ".", 0, NULL, NULL, NULL) < 0) {
49+
if (git_indexer_new(&idx, ".", 0, NULL, NULL) < 0) {
5050
puts("bad idx");
5151
return -1;
5252
}

include/git2/indexer.h

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,30 @@ GIT_BEGIN_DECL
1515

1616
typedef struct git_indexer git_indexer;
1717

18+
typedef struct git_indexer_options {
19+
unsigned int version;
20+
21+
/** progress_cb function to call with progress information */
22+
git_transfer_progress_cb progress_cb;
23+
/** progress_cb_payload payload for the progress callback */
24+
void *progress_cb_payload;
25+
} git_indexer_options;
26+
27+
#define GIT_INDEXER_OPTIONS_VERSION 1
28+
#define GIT_INDEXER_OPTIONS_INIT { GIT_INDEXER_OPTIONS_VERSION }
29+
30+
/**
31+
* Initializes a `git_indexer_options` with default values. Equivalent to
32+
* creating an instance with GIT_INDEXER_OPTIONS_INIT.
33+
*
34+
* @param opts the `git_indexer_options` struct to initialize.
35+
* @param version Version of struct; pass `GIT_INDEXER_OPTIONS_VERSION`
36+
* @return Zero on success; -1 on failure.
37+
*/
38+
GIT_EXTERN(int) git_indexer_init_options(
39+
git_indexer_options *opts,
40+
unsigned int version);
41+
1842
/**
1943
* Create a new indexer instance
2044
*
@@ -24,16 +48,15 @@ typedef struct git_indexer git_indexer;
2448
* @param odb object database from which to read base objects when
2549
* fixing thin packs. Pass NULL if no thin pack is expected (an error
2650
* will be returned if there are bases missing)
27-
* @param progress_cb function to call with progress information
28-
* @param progress_cb_payload payload for the progress callback
51+
* @param opts Optional structure containing additional options. See
52+
* `git_indexer_options` above.
2953
*/
3054
GIT_EXTERN(int) git_indexer_new(
3155
git_indexer **out,
3256
const char *path,
3357
unsigned int mode,
3458
git_odb *odb,
35-
git_transfer_progress_cb progress_cb,
36-
void *progress_cb_payload);
59+
git_indexer_options *opts);
3760

3861
/**
3962
* Add data to the indexer

src/indexer.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,24 +113,34 @@ static int objects_cmp(const void *a, const void *b)
113113
return git_oid__cmp(&entrya->oid, &entryb->oid);
114114
}
115115

116+
int git_indexer_init_options(git_indexer_options *opts, unsigned int version)
117+
{
118+
GIT_INIT_STRUCTURE_FROM_TEMPLATE(
119+
opts, version, git_indexer_options, GIT_INDEXER_OPTIONS_INIT);
120+
return 0;
121+
}
122+
116123
int git_indexer_new(
117124
git_indexer **out,
118125
const char *prefix,
119126
unsigned int mode,
120127
git_odb *odb,
121-
git_transfer_progress_cb progress_cb,
122-
void *progress_payload)
128+
git_indexer_options *in_opts)
123129
{
130+
git_indexer_options opts = GIT_INDEXER_OPTIONS_INIT;
124131
git_indexer *idx;
125132
git_buf path = GIT_BUF_INIT, tmp_path = GIT_BUF_INIT;
126133
static const char suff[] = "/pack";
127134
int error, fd = -1;
128135

136+
if (in_opts)
137+
memcpy(&opts, in_opts, sizeof(opts));
138+
129139
idx = git__calloc(1, sizeof(git_indexer));
130140
GITERR_CHECK_ALLOC(idx);
131141
idx->odb = odb;
132-
idx->progress_cb = progress_cb;
133-
idx->progress_payload = progress_payload;
142+
idx->progress_cb = opts.progress_cb;
143+
idx->progress_payload = opts.progress_cb_payload;
134144
idx->mode = mode ? mode : GIT_PACK_FILE_MODE;
135145
git_hash_ctx_init(&idx->hash_ctx);
136146
git_hash_ctx_init(&idx->trailer);

src/odb_pack.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,20 +519,24 @@ static int pack_backend__writepack(struct git_odb_writepack **out,
519519
git_transfer_progress_cb progress_cb,
520520
void *progress_payload)
521521
{
522+
git_indexer_options opts;
522523
struct pack_backend *backend;
523524
struct pack_writepack *writepack;
524525

525526
assert(out && _backend);
526527

527528
*out = NULL;
528529

530+
opts.progress_cb = progress_cb;
531+
opts.progress_cb_payload = progress_payload;
532+
529533
backend = (struct pack_backend *)_backend;
530534

531535
writepack = git__calloc(1, sizeof(struct pack_writepack));
532536
GITERR_CHECK_ALLOC(writepack);
533537

534538
if (git_indexer_new(&writepack->indexer,
535-
backend->pack_folder, 0, odb, progress_cb, progress_payload) < 0) {
539+
backend->pack_folder, 0, odb, &opts) < 0) {
536540
git__free(writepack);
537541
return -1;
538542
}

src/pack-objects.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1388,15 +1388,19 @@ int git_packbuilder_write(
13881388
git_transfer_progress_cb progress_cb,
13891389
void *progress_cb_payload)
13901390
{
1391+
git_indexer_options opts = GIT_INDEXER_OPTIONS_INIT;
13911392
git_indexer *indexer;
13921393
git_transfer_progress stats;
13931394
struct pack_write_context ctx;
13941395
int t;
13951396

13961397
PREPARE_PACK;
13971398

1399+
opts.progress_cb = progress_cb;
1400+
opts.progress_cb_payload = progress_cb_payload;
1401+
13981402
if (git_indexer_new(
1399-
&indexer, path, mode, pb->odb, progress_cb, progress_cb_payload) < 0)
1403+
&indexer, path, mode, pb->odb, &opts) < 0)
14001404
return -1;
14011405

14021406
if (!git_repository__cvar(&t, pb->repo, GIT_CVAR_FSYNCOBJECTFILES) && t)

tests/pack/indexer.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void test_pack_indexer__out_of_order(void)
8282
git_indexer *idx = 0;
8383
git_transfer_progress stats = { 0 };
8484

85-
cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL, NULL));
85+
cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL));
8686
cl_git_pass(git_indexer_append(
8787
idx, out_of_order_pack, out_of_order_pack_len, &stats));
8888
cl_git_pass(git_indexer_commit(idx, &stats));
@@ -99,7 +99,7 @@ void test_pack_indexer__missing_trailer(void)
9999
git_indexer *idx = 0;
100100
git_transfer_progress stats = { 0 };
101101

102-
cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL, NULL));
102+
cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL));
103103
cl_git_pass(git_indexer_append(
104104
idx, missing_trailer_pack, missing_trailer_pack_len, &stats));
105105
cl_git_fail(git_indexer_commit(idx, &stats));
@@ -115,7 +115,7 @@ void test_pack_indexer__leaky(void)
115115
git_indexer *idx = 0;
116116
git_transfer_progress stats = { 0 };
117117

118-
cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL, NULL));
118+
cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL));
119119
cl_git_pass(git_indexer_append(
120120
idx, leaky_pack, leaky_pack_len, &stats));
121121
cl_git_fail(git_indexer_commit(idx, &stats));
@@ -142,7 +142,7 @@ void test_pack_indexer__fix_thin(void)
142142
git_oid_fromstr(&should_id, "e68fe8129b546b101aee9510c5328e7f21ca1d18");
143143
cl_assert_equal_oid(&should_id, &id);
144144

145-
cl_git_pass(git_indexer_new(&idx, ".", 0, odb, NULL, NULL));
145+
cl_git_pass(git_indexer_new(&idx, ".", 0, odb, NULL));
146146
cl_git_pass(git_indexer_append(idx, thin_pack, thin_pack_len, &stats));
147147
cl_git_pass(git_indexer_commit(idx, &stats));
148148

@@ -175,7 +175,7 @@ void test_pack_indexer__fix_thin(void)
175175

176176
cl_git_pass(p_stat(name, &st));
177177

178-
cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL, NULL));
178+
cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL));
179179
read = p_read(fd, buffer, sizeof(buffer));
180180
cl_assert(read != -1);
181181
p_close(fd);
@@ -208,7 +208,7 @@ void test_pack_indexer__corrupt_length(void)
208208
git_oid_fromstr(&should_id, "e68fe8129b546b101aee9510c5328e7f21ca1d18");
209209
cl_assert_equal_oid(&should_id, &id);
210210

211-
cl_git_pass(git_indexer_new(&idx, ".", 0, odb, NULL, NULL));
211+
cl_git_pass(git_indexer_new(&idx, ".", 0, odb, NULL));
212212
cl_git_pass(git_indexer_append(
213213
idx, corrupt_thin_pack, corrupt_thin_pack_len, &stats));
214214
cl_git_fail(git_indexer_commit(idx, &stats));
@@ -252,7 +252,7 @@ void test_pack_indexer__no_tmp_files(void)
252252
git_buf_dispose(&path);
253253
cl_assert(git_buf_len(&first_tmp_file) == 0);
254254

255-
cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL, NULL));
255+
cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL));
256256
git_indexer_free(idx);
257257

258258
cl_git_pass(git_buf_sets(&path, clar_sandbox_path()));

tests/pack/packbuilder.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ void test_pack_packbuilder__create_pack(void)
100100

101101
seed_packbuilder();
102102

103-
cl_git_pass(git_indexer_new(&_indexer, ".", 0, NULL, NULL, NULL));
103+
cl_git_pass(git_indexer_new(&_indexer, ".", 0, NULL, NULL));
104104
cl_git_pass(git_packbuilder_foreach(_packbuilder, feed_indexer, &stats));
105105
cl_git_pass(git_indexer_commit(_indexer, &stats));
106106

@@ -237,7 +237,7 @@ void test_pack_packbuilder__foreach(void)
237237
git_indexer *idx;
238238

239239
seed_packbuilder();
240-
cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL, NULL));
240+
cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL));
241241
cl_git_pass(git_packbuilder_foreach(_packbuilder, foreach_cb, idx));
242242
cl_git_pass(git_indexer_commit(idx, &_stats));
243243
git_indexer_free(idx);
@@ -255,7 +255,7 @@ void test_pack_packbuilder__foreach_with_cancel(void)
255255
git_indexer *idx;
256256

257257
seed_packbuilder();
258-
cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL, NULL));
258+
cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL));
259259
cl_git_fail_with(
260260
git_packbuilder_foreach(_packbuilder, foreach_cancel_cb, idx), -1111);
261261
git_indexer_free(idx);

0 commit comments

Comments
 (0)