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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed May 19, 2011 at 17:33 UTC 114a6a889f5d21c26800c395a97cdd400073a9e8
4 files changed +20 -6
builtin/fetch-pack.c
+1 -1
@@ -226,7 +226,7 @@ static void insert_one_alternate_ref(const struct ref *ref, void *unused)
226
227 static void insert_alternate_refs(void)
228 {
229 - foreach_alt_odb(refs_from_alternate_cb, insert_one_alternate_ref);
229 + for_each_alternate_ref(insert_one_alternate_ref, NULL);
230 }
231
232 #define INITIAL_FLUSH 16
builtin/receive-pack.c
+1 -1
@@ -738,7 +738,7 @@ static void add_one_alternate_ref(const struct ref *ref, void *unused)
738
739 static void add_alternate_refs(void)
740 {
741 - foreach_alt_odb(refs_from_alternate_cb, add_one_alternate_ref);
741 + for_each_alternate_ref(add_one_alternate_ref, NULL);
742 }
743
744 int cmd_receive_pack(int argc, const char **argv, const char *prefix)
transport.c
+17 -3
@@ -1190,14 +1190,20 @@ literal_copy:
1190 return xstrdup(url);
1191 }
1192
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)
1200 {
1201 char *other;
1202 size_t len;
1203 struct remote *remote;
1204 struct transport *transport;
1205 const struct ref *extra;
1200 - alternate_ref_fn *ref_fn = cb;
1206 + struct alternate_refs_data *cb = data;
1207
1208 e->name[-1] = '\0';
1209 other = xstrdup(real_path(e->base));
@@ -1218,8 +1224,16 @@ int refs_from_alternate_cb(struct alternate_object_database *e, void *cb)
1224 for (extra = transport_get_remote_refs(transport);
1225 extra;
1226 extra = extra->next)
1221 - ref_fn(extra, NULL);
1227 + cb->fn(extra, cb->data);
1228 transport_disconnect(transport);
1229 free(other);
1230 return 0;
1231 }
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
+1 -1
@@ -167,6 +167,6 @@ void transport_print_push_status(const char *dest, struct ref *refs,
167 int verbose, int porcelain, int *nonfastforward);
168
169 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 *);
171
172 #endif