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

[PATCH v3 05/12] serve.c: add call_{advertise,command}() indirection

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Jul 21, 2021, 23:40 UTC
Message-ID
<patch-05.12-99eeff6f890-20210721T233307Z-avarab@gmail.com>
In-Reply-To
<cover-00.12-00000000000-20210721T233307Z-avarab@gmail.com>

Instead of directly calling the callbacks defined in the "struct protocol_capability", let's call them via new "call_{advertise,command}()" functions. There's no use in this indirection now, but it'll be used in a subsequent commit to ensure that config is read before they're called.

See ed10cb952d3 (serve: introduce git-serve, 2018-03-15) for the introduction of the code being changed here.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 serve.c | 33 ++++++++++++++++++++++++---------
 1 file changed, 24 insertions(+), 9 deletions(-)
diff --git a/serve.c b/serve.c
index 49ea9fc8fd5..85cd3eab26e 100644
--- a/serve.c
+++ b/serve.c
@@ -42,6 +42,10 @@ static int session_id_advertise(struct repository *r, struct strbuf *value)
 	return 1;
 }
 
+typedef int (*advertise_fn_t)(struct repository *r, struct strbuf *value);
+typedef int (*command_fn_t)(struct repository *r, struct strvec *keys,
+			    struct packet_reader *request);
+
 struct protocol_capability {
 	/*
 	 * The name of the capability.  The server uses this name when
@@ -56,7 +60,7 @@ struct protocol_capability {
 	 * If a value is added to 'value', the server will advertise this
 	 * capability as "<name>=<value>" instead of "<name>".
 	 */
-	int (*advertise)(struct repository *r, struct strbuf *value);
+	advertise_fn_t advertise;
 
 	/*
 	 * Function called when a client requests the capability as a command.
@@ -67,9 +71,7 @@ struct protocol_capability {
 	 *
 	 * This field should be NULL for capabilities which are not commands.
 	 */
-	int (*command)(struct repository *r,
-		       struct strvec *keys,
-		       struct packet_reader *request);
+	command_fn_t command;
 };
 
 static struct protocol_capability capabilities[] = {
@@ -106,6 +108,19 @@ static struct protocol_capability capabilities[] = {
 	},
 };
 
+static int call_advertise(struct protocol_capability *command,
+			  struct repository *r, struct strbuf *value)
+{
+	return command->advertise(r, value);
+}
+
+static int call_command(struct protocol_capability *command,
+			struct repository *r, struct strvec *keys,
+			struct packet_reader *request)
+{
+	return command->command(r, keys, request);
+}
+
 static void advertise_capabilities(void)
 {
 	struct strbuf capability = STRBUF_INIT;
@@ -115,7 +130,7 @@ static void advertise_capabilities(void)
 	for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
 		struct protocol_capability *c = &capabilities[i];
 
-		if (c->advertise(the_repository, &value)) {
+		if (call_advertise(c, the_repository, &value)) {
 			strbuf_addstr(&capability, c->name);
 
 			if (value.len) {
@@ -155,9 +170,9 @@ static struct protocol_capability *get_capability(const char *key)
 
 static int is_valid_capability(const char *key)
 {
-	const struct protocol_capability *c = get_capability(key);
+	struct protocol_capability *c = get_capability(key);
 
-	return c && c->advertise(the_repository, NULL);
+	return c && call_advertise(c, the_repository, NULL);
 }
 
 static int is_command(const char *key, struct protocol_capability **command)
@@ -170,7 +185,7 @@ static int is_command(const char *key, struct protocol_capability **command)
 		if (*command)
 			die("command '%s' requested after already requesting command '%s'",
 			    out, (*command)->name);
-		if (!cmd || !cmd->advertise(the_repository, NULL) || !cmd->command)
+		if (!cmd || !call_advertise(cmd, the_repository, NULL) || !cmd->command)
 			die("invalid command '%s'", out);
 
 		*command = cmd;
@@ -294,7 +309,7 @@ static int process_request(void)
 	if (has_capability(&keys, "session-id", &client_sid))
 		trace2_data_string("transfer", NULL, "client-sid", client_sid);
 
-	command->command(the_repository, &keys, &reader);
+	call_command(command, the_repository, &keys, &reader);
 
 	strvec_clear(&keys);
 	return 0;
-- 
2.32.0.955.ge7c5360f7e7
Previous: Ævar Arnfjörð BjarmasonNext: Ævar Arnfjörð Bjarmason
Message 36 of 58 in “serve: add "configure" callback”
  1. 0/5 serve: add "configure" callbackÆvar Arnfjörð Bjarmason, Jun 16, 2021
  2. 1/5 serve: mark has_capability() as staticÆvar Arnfjörð Bjarmason, Jun 16, 2021
  3. Eric SunshineJun 16, 2021
  4. Junio C HamanoJun 17, 2021
  5. 2/5 transport: rename "fetch" in transport_vtable to "fetch_refs"Ævar Arnfjörð Bjarmason, Jun 16, 2021
  6. 3/5 transport: use designated initializersÆvar Arnfjörð Bjarmason, Jun 16, 2021
  7. 5/5 serve: add support for a git_config() callbackÆvar Arnfjörð Bjarmason, Jun 16, 2021
  8. Jeff KingJun 16, 2021
  9. 4/5 serve: use designated initializersÆvar Arnfjörð Bjarmason, Jun 16, 2021
  10. Jeff KingJun 16, 2021
  11. Junio C HamanoJun 17, 2021
  12. 0/8 serve: add "startup_config" callbackÆvar Arnfjörð Bjarmason, Jun 28, 2021
  13. 1/8 serve: mark has_capability() as staticÆvar Arnfjörð Bjarmason, Jun 28, 2021
  14. 3/8 transport: use designated initializersÆvar Arnfjörð Bjarmason, Jun 28, 2021
  15. 2/8 transport: rename "fetch" in transport_vtable to "fetch_refs"Ævar Arnfjörð Bjarmason, Jun 28, 2021
  16. 5/8 serve.c: add call_{advertise,command}() indirectionÆvar Arnfjörð Bjarmason, Jun 28, 2021
  17. 4/8 serve: use designated initializersÆvar Arnfjörð Bjarmason, Jun 28, 2021
  18. 7/8 serve: add support for a "startup" git_config() callbackÆvar Arnfjörð Bjarmason, Jun 28, 2021
  19. Jeff KingJul 1, 2021
  20. Jeff KingJul 1, 2021
  21. Ævar Arnfjörð BjarmasonJul 2, 2021
  22. Jeff KingJul 2, 2021
  23. Han-Wen NienhuysJul 5, 2021
  24. Han-Wen NienhuysJul 5, 2021
  25. 6/8 serve.c: add trace2 regions for advertise & commandÆvar Arnfjörð Bjarmason, Jun 28, 2021
  26. Jeff KingJul 1, 2021
  27. Ævar Arnfjörð BjarmasonJul 2, 2021
  28. Han-Wen NienhuysJul 5, 2021
  29. 8/8 upload-pack.c: convert to new serve.c "startup" config cbÆvar Arnfjörð Bjarmason, Jun 28, 2021
  30. Han-Wen NienhuysJul 5, 2021
  31. 00/12 serve.[ch]: general API cleanupÆvar Arnfjörð Bjarmason, Jul 21, 2021
  32. 01/12 serve: mark has_capability() as staticÆvar Arnfjörð Bjarmason, Jul 21, 2021
  33. 02/12 transport: rename "fetch" in transport_vtable to "fetch_refs"Ævar Arnfjörð Bjarmason, Jul 21, 2021
  34. 03/12 transport: use designated initializersÆvar Arnfjörð Bjarmason, Jul 21, 2021
  35. 04/12 serve: use designated initializersÆvar Arnfjörð Bjarmason, Jul 21, 2021
  36. 05/12 serve.c: add call_{advertise,command}() indirectionÆvar Arnfjörð Bjarmason, Jul 21, 2021
  37. 06/12 serve: add support for a "startup" git_config() callbackÆvar Arnfjörð Bjarmason, Jul 21, 2021
  38. 07/12 serve.c: move version line to advertise_capabilities()Ævar Arnfjörð Bjarmason, Jul 21, 2021
  39. 08/12 serve.[ch]: remove "serve_options", split up --advertise-refs codeÆvar Arnfjörð Bjarmason, Jul 21, 2021
  40. 10/12 upload-pack: document and rename --advertise-refsÆvar Arnfjörð Bjarmason, Jul 21, 2021
  41. 09/12 {upload,receive}-pack tests: add --advertise-refs testsÆvar Arnfjörð Bjarmason, Jul 21, 2021
  42. fixup! {upload,receive}-pack tests: add --advertise-refs testsCarlo Marcelo Arenas Belón, Aug 3, 2021
  43. 11/12 upload-pack.c: convert to new serve.c "startup" config cbÆvar Arnfjörð Bjarmason, Jul 21, 2021
  44. 12/12 serve.[ch]: don't pass "struct strvec *keys" to commandsÆvar Arnfjörð Bjarmason, Jul 21, 2021
  45. Josh SteadmonAug 2, 2021
  46. 00/10 serve.[ch]: general API cleanup + --advertise-refs cleanupÆvar Arnfjörð Bjarmason, Aug 5, 2021
  47. 01/10 serve: mark has_capability() as staticÆvar Arnfjörð Bjarmason, Aug 5, 2021
  48. 02/10 transport: rename "fetch" in transport_vtable to "fetch_refs"Ævar Arnfjörð Bjarmason, Aug 5, 2021
  49. 03/10 transport: use designated initializersÆvar Arnfjörð Bjarmason, Aug 5, 2021
  50. 04/10 serve: use designated initializersÆvar Arnfjörð Bjarmason, Aug 5, 2021
  51. 05/10 serve.[ch]: don't pass "struct strvec *keys" to commandsÆvar Arnfjörð Bjarmason, Aug 5, 2021
  52. 06/10 serve: move transfer.advertiseSID check into session_id_advertise()Ævar Arnfjörð Bjarmason, Aug 5, 2021
  53. 07/10 serve.c: move version line to advertise_capabilities()Ævar Arnfjörð Bjarmason, Aug 5, 2021
  54. 08/10 {upload,receive}-pack tests: add --advertise-refs testsÆvar Arnfjörð Bjarmason, Aug 5, 2021
  55. 09/10 serve.[ch]: remove "serve_options", split up --advertise-refs codeÆvar Arnfjörð Bjarmason, Aug 5, 2021
  56. Derrick StoleeAug 24, 2021
  57. 10/10 upload-pack: document and rename --advertise-refsÆvar Arnfjörð Bjarmason, Aug 5, 2021
  58. Derrick StoleeAug 24, 2021

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.