git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[RFC 1/3] Alternates API: Untangle the interface

From
Michael Witten <mfwitten@gmail.com>
Date
Mar 30, 2011, 20:43 UTC
Message-ID
<5ed7886ec5e12c8e3a5f45d19625a4f5d2cdd38a.1301521243.git.mfwitten@gmail.com>
In-Reply-To
<cover.1301521243.git.mfwitten@gmail.com>
These 2 functions:
      cache.h:896:    foreach_alt_odb
  transport.h:170:    refs_from_alternate_cb
were unnecessarily coupled; the callback used by:
  refs_from_alternate_cb
was being passed to the higher-level:
  foreach_alt_odb

which necessitated some casting ugliness that has actually been invoking undefined behavior[0].

This commit decouples this relationship, resulting in a simpler (albeit slightly more verbose) usage. As a bonus, the undefined behavior is automatically resolved.

References ----------

[0] Variables of type pointer to void were being used to pass around values of type pointer to function; see:

  [RFC 1/2] Portability: Convert strictly between function pointers
  Message-ID: <3c6b883f-8860-4da2-b328-d912019a4145-mfwitten@gmail.com>
Signed-off-by: Michael Witten <mfwitten@gmail.com>
---
 builtin/fetch-pack.c   |    7 ++++++-
 builtin/receive-pack.c |    7 ++++++-
 cache.h                |    4 ++--
 sha1_file.c            |    4 ++--
 transport.c            |    3 +--
 transport.h            |    4 ++--
 6 files changed, 19 insertions(+), 10 deletions(-)
diff --git a/cache.h b/cache.h
index 9f06d21..71cfcef 100644
--- a/cache.h
+++ b/cache.h
@@ -892,8 +892,8 @@ extern struct alternate_object_database {
 } *alt_odb_list;
 extern void prepare_alt_odb(void);
 extern void add_to_alternates_file(const char *reference);
-typedef int alt_odb_fn(struct alternate_object_database *, void *);
+typedef int (*alt_odb_fn)(struct alternate_object_database *);
-extern void foreach_alt_odb(alt_odb_fn, void*);
+extern void foreach_alt_odb(alt_odb_fn);
 
 struct pack_window {
 	struct pack_window *next;
diff --git a/sha1_file.c b/sha1_file.c
index df0edba..e55a496 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -388,13 +388,13 @@ void add_to_alternates_file(const char *reference)
 		link_alt_odb_entries(alt, alt + strlen(alt), '\n', NULL, 0);
 }
 
-void foreach_alt_odb(alt_odb_fn fn, void *cb)
+void foreach_alt_odb(alt_odb_fn fn)
 {
 	struct alternate_object_database *ent;
 
 	prepare_alt_odb();
 	for (ent = alt_odb_list; ent; ent = ent->next)
-		if (fn(ent, cb))
+		if (fn(ent))
 			return;
 }
 
diff --git a/transport.h b/transport.h
index efb1968..72a692f 100644
--- a/transport.h
+++ b/transport.h
@@ -166,7 +166,7 @@ int transport_refs_pushed(struct ref *ref);
 void transport_print_push_status(const char *dest, struct ref *refs,
 		  int verbose, int porcelain, int *nonfastforward);
 
-typedef void alternate_ref_fn(const struct ref *, void *);
+typedef void (*alternate_ref_fn)(const struct ref *, void *);
-extern int refs_from_alternate_cb(struct alternate_object_database *e, void *cb);
+extern int refs_from_alternate_cb(struct alternate_object_database *, alternate_ref_fn);
 
 #endif
diff --git a/transport.c b/transport.c
index a02f79a..c61723f 100644
--- a/transport.c
+++ b/transport.c
@@ -1190,14 +1190,13 @@ literal_copy:
 	return xstrdup(url);
 }
 
-int refs_from_alternate_cb(struct alternate_object_database *e, void *cb)
+int refs_from_alternate_cb(struct alternate_object_database *e, alternate_ref_fn ref_fn)
 {
 	char *other;
 	size_t len;
 	struct remote *remote;
 	struct transport *transport;
 	const struct ref *extra;
-	alternate_ref_fn *ref_fn = cb;
 
 	e->name[-1] = '\0';
 	other = xstrdup(real_path(e->base));
diff --git a/builtin/fetch-pack.c b/builtin/fetch-pack.c
index 85aff02..62ebdb4 100644
--- a/builtin/fetch-pack.c
+++ b/builtin/fetch-pack.c
@@ -224,9 +224,14 @@ static void insert_one_alternate_ref(const struct ref *ref, void *unused)
 	rev_list_insert_ref(NULL, ref->old_sha1, 0, NULL);
 }
 
+static int alt_odb_callback(struct alternate_object_database *d)
+{
+	return refs_from_alternate_cb(d,insert_one_alternate_ref);
+}
+
 static void insert_alternate_refs(void)
 {
-	foreach_alt_odb(refs_from_alternate_cb, insert_one_alternate_ref);
+	foreach_alt_odb(alt_odb_callback);
 }
 
 #define INITIAL_FLUSH 16
diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index 27050e7..8ef6301 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -736,9 +736,14 @@ static void add_one_alternate_ref(const struct ref *ref, void *unused)
 	add_extra_ref(".have", ref->old_sha1, 0);
 }
 
+static int alt_odb_callback(struct alternate_object_database *d)
+{
+	return refs_from_alternate_cb(d,add_one_alternate_ref);
+}
+
 static void add_alternate_refs(void)
 {
-	foreach_alt_odb(refs_from_alternate_cb, add_one_alternate_ref);
+	foreach_alt_odb(alt_odb_callback);
 }
 
 int cmd_receive_pack(int argc, const char **argv, const char *prefix)
-- 
1.7.4.18.g68fe8
Previous: Junio C HamanoNext: Junio C Hamano
Message 6 of 12 in “[RFC 1/2] Portability: Convert strictly between function pointers”
  1. Michael WittenMar 29, 2011
  2. 2/2 Future Proofing: Pass around pointers to either functions or dataMichael Witten, Mar 29, 2011
  3. Erik Faye-LundMar 30, 2011
  4. Michael WittenMar 30, 2011
  5. Junio C HamanoMar 30, 2011
  6. 1/3 Alternates API: Untangle the interfaceMichael Witten, Mar 30, 2011
  7. Junio C HamanoMar 30, 2011
  8. 2/3 Alternates API: Improve namingMichael Witten, Mar 30, 2011
  9. Junio C HamanoMar 30, 2011
  10. 3/3 Alternates API: Remove unused parameterMichael Witten, Mar 30, 2011
  11. 0/3 Alternates APIMichael Witten, Mar 30, 2011
  12. Junio C HamanoMar 30, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.