Skip to content

Commit cfd575f

Browse files
pks-tgitster
authored andcommitted
odb: introduce struct odb_for_each_object_options
The `odb_for_each_object()` function only accepts a bitset of flags. In a subsequent commit we'll want to change object iteration to also support iterating over only those objects that have a specific prefix. While we could of course add the prefix to the function signature, or alternatively introduce a new function, both of these options don't really seem to be that sensible. Instead, introduce a new `struct odb_for_each_object_options` that can be passed to a new `odb_for_each_object_ext()` function. Splice through the options structure into the respective object database sources. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent fe446b0 commit cfd575f

11 files changed

Lines changed: 71 additions & 34 deletions

File tree

builtin/cat-file.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,9 @@ static void batch_each_object(struct batch_options *opt,
848848
.callback = callback,
849849
.payload = _payload,
850850
};
851+
struct odb_for_each_object_options opts = {
852+
.flags = flags,
853+
};
851854
struct bitmap_index *bitmap = NULL;
852855
struct odb_source *source;
853856

@@ -860,7 +863,7 @@ static void batch_each_object(struct batch_options *opt,
860863
odb_prepare_alternates(the_repository->objects);
861864
for (source = the_repository->objects->sources; source; source = source->next) {
862865
int ret = odb_source_loose_for_each_object(source, NULL, batch_one_object_oi,
863-
&payload, flags);
866+
&payload, &opts);
864867
if (ret)
865868
break;
866869
}
@@ -884,7 +887,7 @@ static void batch_each_object(struct batch_options *opt,
884887
for (source = the_repository->objects->sources; source; source = source->next) {
885888
struct odb_source_files *files = odb_source_files_downcast(source);
886889
int ret = packfile_store_for_each_object(files->packed, &oi,
887-
batch_one_object_oi, &payload, flags);
890+
batch_one_object_oi, &payload, &opts);
888891
if (ret)
889892
break;
890893
}

builtin/pack-objects.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4344,6 +4344,12 @@ static void add_objects_in_unpacked_packs(void)
43444344
{
43454345
struct odb_source *source;
43464346
time_t mtime;
4347+
struct odb_for_each_object_options opts = {
4348+
.flags = ODB_FOR_EACH_OBJECT_PACK_ORDER |
4349+
ODB_FOR_EACH_OBJECT_LOCAL_ONLY |
4350+
ODB_FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS |
4351+
ODB_FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS,
4352+
};
43474353
struct object_info oi = {
43484354
.mtimep = &mtime,
43494355
};
@@ -4356,11 +4362,7 @@ static void add_objects_in_unpacked_packs(void)
43564362
continue;
43574363

43584364
if (packfile_store_for_each_object(files->packed, &oi,
4359-
add_object_in_unpacked_pack, NULL,
4360-
ODB_FOR_EACH_OBJECT_PACK_ORDER |
4361-
ODB_FOR_EACH_OBJECT_LOCAL_ONLY |
4362-
ODB_FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS |
4363-
ODB_FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS))
4365+
add_object_in_unpacked_pack, NULL, &opts))
43644366
die(_("cannot open pack index"));
43654367
}
43664368
}

commit-graph.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1969,6 +1969,9 @@ static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
19691969
{
19701970
struct odb_source *source;
19711971
enum object_type type;
1972+
struct odb_for_each_object_options opts = {
1973+
.flags = ODB_FOR_EACH_OBJECT_PACK_ORDER,
1974+
};
19721975
struct object_info oi = {
19731976
.typep = &type,
19741977
};
@@ -1983,7 +1986,7 @@ static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
19831986
for (source = ctx->r->objects->sources; source; source = source->next) {
19841987
struct odb_source_files *files = odb_source_files_downcast(source);
19851988
packfile_store_for_each_object(files->packed, &oi, add_packed_commits_oi,
1986-
ctx, ODB_FOR_EACH_OBJECT_PACK_ORDER);
1989+
ctx, &opts);
19871990
}
19881991

19891992
if (ctx->progress_done < ctx->approx_nr_objects)

object-file.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,7 +1849,7 @@ int odb_source_loose_for_each_object(struct odb_source *source,
18491849
const struct object_info *request,
18501850
odb_for_each_object_cb cb,
18511851
void *cb_data,
1852-
unsigned flags)
1852+
const struct odb_for_each_object_options *opts)
18531853
{
18541854
struct for_each_object_wrapper_data data = {
18551855
.source = source,
@@ -1859,9 +1859,9 @@ int odb_source_loose_for_each_object(struct odb_source *source,
18591859
};
18601860

18611861
/* There are no loose promisor objects, so we can return immediately. */
1862-
if ((flags & ODB_FOR_EACH_OBJECT_PROMISOR_ONLY))
1862+
if ((opts->flags & ODB_FOR_EACH_OBJECT_PROMISOR_ONLY))
18631863
return 0;
1864-
if ((flags & ODB_FOR_EACH_OBJECT_LOCAL_ONLY) && !source->local)
1864+
if ((opts->flags & ODB_FOR_EACH_OBJECT_LOCAL_ONLY) && !source->local)
18651865
return 0;
18661866

18671867
return for_each_loose_file_in_source(source, for_each_object_wrapper_cb,
@@ -1914,9 +1914,10 @@ int odb_source_loose_count_objects(struct odb_source *source,
19141914
*out = count * 256;
19151915
ret = 0;
19161916
} else {
1917+
struct odb_for_each_object_options opts = { 0 };
19171918
*out = 0;
19181919
ret = odb_source_loose_for_each_object(source, NULL, count_loose_object,
1919-
out, NULL);
1920+
out, &opts);
19201921
}
19211922

19221923
out:

object-file.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ int odb_source_loose_for_each_object(struct odb_source *source,
137137
const struct object_info *request,
138138
odb_for_each_object_cb cb,
139139
void *cb_data,
140-
unsigned flags);
140+
const struct odb_for_each_object_options *opts);
141141

142142
/*
143143
* Count the number of loose objects in this source.

odb.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -896,27 +896,39 @@ int odb_freshen_object(struct object_database *odb,
896896
return 0;
897897
}
898898

899-
int odb_for_each_object(struct object_database *odb,
900-
const struct object_info *request,
901-
odb_for_each_object_cb cb,
902-
void *cb_data,
903-
unsigned flags)
899+
int odb_for_each_object_ext(struct object_database *odb,
900+
const struct object_info *request,
901+
odb_for_each_object_cb cb,
902+
void *cb_data,
903+
const struct odb_for_each_object_options *opts)
904904
{
905905
int ret;
906906

907907
odb_prepare_alternates(odb);
908908
for (struct odb_source *source = odb->sources; source; source = source->next) {
909-
if (flags & ODB_FOR_EACH_OBJECT_LOCAL_ONLY && !source->local)
909+
if (opts->flags & ODB_FOR_EACH_OBJECT_LOCAL_ONLY && !source->local)
910910
continue;
911911

912-
ret = odb_source_for_each_object(source, request, cb, cb_data, flags);
912+
ret = odb_source_for_each_object(source, request, cb, cb_data, opts);
913913
if (ret)
914914
return ret;
915915
}
916916

917917
return 0;
918918
}
919919

920+
int odb_for_each_object(struct object_database *odb,
921+
const struct object_info *request,
922+
odb_for_each_object_cb cb,
923+
void *cb_data,
924+
unsigned flags)
925+
{
926+
struct odb_for_each_object_options opts = {
927+
.flags = flags,
928+
};
929+
return odb_for_each_object_ext(odb, request, cb, cb_data, &opts);
930+
}
931+
920932
int odb_count_objects(struct object_database *odb,
921933
enum odb_count_objects_flags flags,
922934
unsigned long *out)

odb.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,15 @@ typedef int (*odb_for_each_object_cb)(const struct object_id *oid,
481481
struct object_info *oi,
482482
void *cb_data);
483483

484+
/*
485+
* Options that can be passed to `odb_for_each_object()` and its
486+
* backend-specific implementations.
487+
*/
488+
struct odb_for_each_object_options {
489+
/* A bitfield of `odb_for_each_object_flags`. */
490+
enum odb_for_each_object_flags flags;
491+
};
492+
484493
/*
485494
* Iterate through all objects contained in the object database. Note that
486495
* objects may be iterated over multiple times in case they are either stored
@@ -495,6 +504,13 @@ typedef int (*odb_for_each_object_cb)(const struct object_id *oid,
495504
* Returns 0 on success, a negative error code in case a failure occurred, or
496505
* an arbitrary non-zero error code returned by the callback itself.
497506
*/
507+
int odb_for_each_object_ext(struct object_database *odb,
508+
const struct object_info *request,
509+
odb_for_each_object_cb cb,
510+
void *cb_data,
511+
const struct odb_for_each_object_options *opts);
512+
513+
/* Same as `odb_for_each_object_ext()` with `opts.flags` set to the given flags. */
498514
int odb_for_each_object(struct object_database *odb,
499515
const struct object_info *request,
500516
odb_for_each_object_cb cb,

odb/source-files.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,18 @@ static int odb_source_files_for_each_object(struct odb_source *source,
7575
const struct object_info *request,
7676
odb_for_each_object_cb cb,
7777
void *cb_data,
78-
unsigned flags)
78+
const struct odb_for_each_object_options *opts)
7979
{
8080
struct odb_source_files *files = odb_source_files_downcast(source);
8181
int ret;
8282

83-
if (!(flags & ODB_FOR_EACH_OBJECT_PROMISOR_ONLY)) {
84-
ret = odb_source_loose_for_each_object(source, request, cb, cb_data, flags);
83+
if (!(opts->flags & ODB_FOR_EACH_OBJECT_PROMISOR_ONLY)) {
84+
ret = odb_source_loose_for_each_object(source, request, cb, cb_data, opts);
8585
if (ret)
8686
return ret;
8787
}
8888

89-
ret = packfile_store_for_each_object(files->packed, request, cb, cb_data, flags);
89+
ret = packfile_store_for_each_object(files->packed, request, cb, cb_data, opts);
9090
if (ret)
9191
return ret;
9292

odb/source.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ struct odb_source {
140140
const struct object_info *request,
141141
odb_for_each_object_cb cb,
142142
void *cb_data,
143-
unsigned flags);
143+
const struct odb_for_each_object_options *opts);
144144

145145
/*
146146
* This callback is expected to count objects in the given object
@@ -343,9 +343,9 @@ static inline int odb_source_for_each_object(struct odb_source *source,
343343
const struct object_info *request,
344344
odb_for_each_object_cb cb,
345345
void *cb_data,
346-
unsigned flags)
346+
const struct odb_for_each_object_options *opts)
347347
{
348-
return source->for_each_object(source, request, cb, cb_data, flags);
348+
return source->for_each_object(source, request, cb, cb_data, opts);
349349
}
350350

351351
/*

packfile.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2375,7 +2375,7 @@ int packfile_store_for_each_object(struct packfile_store *store,
23752375
const struct object_info *request,
23762376
odb_for_each_object_cb cb,
23772377
void *cb_data,
2378-
unsigned flags)
2378+
const struct odb_for_each_object_options *opts)
23792379
{
23802380
struct packfile_store_for_each_object_wrapper_data data = {
23812381
.store = store,
@@ -2391,15 +2391,15 @@ int packfile_store_for_each_object(struct packfile_store *store,
23912391
for (e = packfile_store_get_packs(store); e; e = e->next) {
23922392
struct packed_git *p = e->pack;
23932393

2394-
if ((flags & ODB_FOR_EACH_OBJECT_LOCAL_ONLY) && !p->pack_local)
2394+
if ((opts->flags & ODB_FOR_EACH_OBJECT_LOCAL_ONLY) && !p->pack_local)
23952395
continue;
2396-
if ((flags & ODB_FOR_EACH_OBJECT_PROMISOR_ONLY) &&
2396+
if ((opts->flags & ODB_FOR_EACH_OBJECT_PROMISOR_ONLY) &&
23972397
!p->pack_promisor)
23982398
continue;
2399-
if ((flags & ODB_FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS) &&
2399+
if ((opts->flags & ODB_FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS) &&
24002400
p->pack_keep_in_core)
24012401
continue;
2402-
if ((flags & ODB_FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS) &&
2402+
if ((opts->flags & ODB_FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS) &&
24032403
p->pack_keep)
24042404
continue;
24052405
if (open_pack_index(p)) {
@@ -2408,7 +2408,7 @@ int packfile_store_for_each_object(struct packfile_store *store,
24082408
}
24092409

24102410
ret = for_each_object_in_pack(p, packfile_store_for_each_object_wrapper,
2411-
&data, flags);
2411+
&data, opts->flags);
24122412
if (ret)
24132413
goto out;
24142414
}

0 commit comments

Comments
 (0)