Skip to content

Commit 114a6a8

Browse files
peffgitster
authored andcommitted
refactor refs_from_alternate_cb to allow passing extra data
The foreach_alt_odb function triggers a callback for each alternate object db we have, with room for a single void pointer as data. Currently, we always call refs_from_alternate_cb as the callback function, and then pass another callback (to receive each ref individually) as the void pointer. This has two problems: 1. C technically forbids stuffing a function pointer into a "void *". In practice, this probably doesn't matter on any architectures git runs on, but it never hurts to follow the letter of the law. 2. There is no room for an extra data pointer. Indeed, the alternate_ref_fn that refs_from_alternate_cb calls takes a void* for data, but we always pass it NULL. Instead, let's properly stuff our function pointer into a data struct, which also leaves room for an extra caller-supplied data pointer. And to keep things simple for existing callers, let's make a for_each_alternate_ref function that takes care of creating the extra struct. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ea1ab4b commit 114a6a8

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

builtin/fetch-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ static void insert_one_alternate_ref(const struct ref *ref, void *unused)
226226

227227
static void insert_alternate_refs(void)
228228
{
229-
foreach_alt_odb(refs_from_alternate_cb, insert_one_alternate_ref);
229+
for_each_alternate_ref(insert_one_alternate_ref, NULL);
230230
}
231231

232232
#define INITIAL_FLUSH 16

builtin/receive-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ static void add_one_alternate_ref(const struct ref *ref, void *unused)
738738

739739
static void add_alternate_refs(void)
740740
{
741-
foreach_alt_odb(refs_from_alternate_cb, add_one_alternate_ref);
741+
for_each_alternate_ref(add_one_alternate_ref, NULL);
742742
}
743743

744744
int cmd_receive_pack(int argc, const char **argv, const char *prefix)

transport.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,14 +1190,20 @@ char *transport_anonymize_url(const char *url)
11901190
return xstrdup(url);
11911191
}
11921192

1193-
int refs_from_alternate_cb(struct alternate_object_database *e, void *cb)
1193+
struct alternate_refs_data {
1194+
alternate_ref_fn *fn;
1195+
void *data;
1196+
};
1197+
1198+
static int refs_from_alternate_cb(struct alternate_object_database *e,
1199+
void *data)
11941200
{
11951201
char *other;
11961202
size_t len;
11971203
struct remote *remote;
11981204
struct transport *transport;
11991205
const struct ref *extra;
1200-
alternate_ref_fn *ref_fn = cb;
1206+
struct alternate_refs_data *cb = data;
12011207

12021208
e->name[-1] = '\0';
12031209
other = xstrdup(real_path(e->base));
@@ -1218,8 +1224,16 @@ int refs_from_alternate_cb(struct alternate_object_database *e, void *cb)
12181224
for (extra = transport_get_remote_refs(transport);
12191225
extra;
12201226
extra = extra->next)
1221-
ref_fn(extra, NULL);
1227+
cb->fn(extra, cb->data);
12221228
transport_disconnect(transport);
12231229
free(other);
12241230
return 0;
12251231
}
1232+
1233+
void for_each_alternate_ref(alternate_ref_fn fn, void *data)
1234+
{
1235+
struct alternate_refs_data cb;
1236+
cb.fn = fn;
1237+
cb.data = data;
1238+
foreach_alt_odb(refs_from_alternate_cb, &cb);
1239+
}

transport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,6 @@ void transport_print_push_status(const char *dest, struct ref *refs,
167167
int verbose, int porcelain, int *nonfastforward);
168168

169169
typedef void alternate_ref_fn(const struct ref *, void *);
170-
extern int refs_from_alternate_cb(struct alternate_object_database *e, void *cb);
170+
extern void for_each_alternate_ref(alternate_ref_fn, void *);
171171

172172
#endif

0 commit comments

Comments
 (0)