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

[PATCH 04/14] serve: stop using `the_repository`

From
Patrick Steinhardt <ps@pks.im>
Date
Dec 17, 2024, 06:43 UTC
Message-ID
<20241217-pks-use-the-repository-conversion-v1-4-0dba48bcc239@pks.im>
In-Reply-To
<20241217-pks-use-the-repository-conversion-v1-0-0dba48bcc239@pks.im>

Stop using `the_repository` in the "serve" subsystem by passing in a repository when advertising capabilities or serving requests.

Adjust callers accordingly by using `the_repository`. While there may be some callers that have a repository available in their context, this trivial conversion allows for easier verification and bubbles up the use of `the_repository` by one level.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 builtin/upload-pack.c    |  6 ++++--
 serve.c                  | 36 +++++++++++++++++-------------------
 serve.h                  |  6 ++++--
 t/helper/test-serve-v2.c |  7 +++++--
 4 files changed, 30 insertions(+), 25 deletions(-)
diff --git a/builtin/upload-pack.c b/builtin/upload-pack.c
index dd63d6eadfe0028b3a62d7d14f777c919f567cc8..c2bbc035ab0c91baf5d66cee8bb370ecf1640505 100644
--- a/builtin/upload-pack.c
+++ b/builtin/upload-pack.c
@@ -1,3 +1,5 @@
+#define USE_THE_REPOSITORY_VARIABLE
+
 #include "builtin.h"
 #include "exec-cmd.h"
 #include "gettext.h"
@@ -63,9 +65,9 @@ int cmd_upload_pack(int argc,
 	switch (determine_protocol_version_server()) {
 	case protocol_v2:
 		if (advertise_refs)
-			protocol_v2_advertise_capabilities();
+			protocol_v2_advertise_capabilities(the_repository);
 		else
-			protocol_v2_serve_loop(stateless_rpc);
+			protocol_v2_serve_loop(the_repository, stateless_rpc);
 		break;
 	case protocol_v1:
 		/*
diff --git a/serve.c b/serve.c
index c8694e375159ca0044cb045500954770e1e5cb93..f6dfe34a2bee6b24bedace2c272f8377dec9fb91 100644
--- a/serve.c
+++ b/serve.c
@@ -1,5 +1,3 @@
-#define USE_THE_REPOSITORY_VARIABLE
-
 #include "git-compat-util.h"
 #include "repository.h"
 #include "config.h"
@@ -159,7 +157,7 @@ static struct protocol_capability capabilities[] = {
 	},
 };
 
-void protocol_v2_advertise_capabilities(void)
+void protocol_v2_advertise_capabilities(struct repository *r)
 {
 	struct strbuf capability = STRBUF_INIT;
 	struct strbuf value = STRBUF_INIT;
@@ -170,7 +168,7 @@ void protocol_v2_advertise_capabilities(void)
 	for (size_t i = 0; i < ARRAY_SIZE(capabilities); i++) {
 		struct protocol_capability *c = &capabilities[i];
 
-		if (c->advertise(the_repository, &value)) {
+		if (c->advertise(r, &value)) {
 			strbuf_addstr(&capability, c->name);
 
 			if (value.len) {
@@ -214,20 +212,20 @@ static struct protocol_capability *get_capability(const char *key, const char **
 	return NULL;
 }
 
-static int receive_client_capability(const char *key)
+static int receive_client_capability(struct repository *r, const char *key)
 {
 	const char *value;
 	const struct protocol_capability *c = get_capability(key, &value);
 
-	if (!c || c->command || !c->advertise(the_repository, NULL))
+	if (!c || c->command || !c->advertise(r, NULL))
 		return 0;
 
 	if (c->receive)
-		c->receive(the_repository, value);
+		c->receive(r, value);
 	return 1;
 }
 
-static int parse_command(const char *key, struct protocol_capability **command)
+static int parse_command(struct repository *r, const char *key, struct protocol_capability **command)
 {
 	const char *out;
 
@@ -238,7 +236,7 @@ static int parse_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 || value)
+		if (!cmd || !cmd->advertise(r, NULL) || !cmd->command || value)
 			die("invalid command '%s'", out);
 
 		*command = cmd;
@@ -253,7 +251,7 @@ enum request_state {
 	PROCESS_REQUEST_DONE,
 };
 
-static int process_request(void)
+static int process_request(struct repository *r)
 {
 	enum request_state state = PROCESS_REQUEST_KEYS;
 	struct packet_reader reader;
@@ -278,8 +276,8 @@ static int process_request(void)
 		case PACKET_READ_EOF:
 			BUG("Should have already died when seeing EOF");
 		case PACKET_READ_NORMAL:
-			if (parse_command(reader.line, &command) ||
-			    receive_client_capability(reader.line))
+			if (parse_command(r, reader.line, &command) ||
+			    receive_client_capability(r, reader.line))
 				seen_capability_or_command = 1;
 			else
 				die("unknown capability '%s'", reader.line);
@@ -319,30 +317,30 @@ static int process_request(void)
 	if (!command)
 		die("no command requested");
 
-	if (client_hash_algo != hash_algo_by_ptr(the_repository->hash_algo))
+	if (client_hash_algo != hash_algo_by_ptr(r->hash_algo))
 		die("mismatched object format: server %s; client %s",
-		    the_repository->hash_algo->name,
+		    r->hash_algo->name,
 		    hash_algos[client_hash_algo].name);
 
-	command->command(the_repository, &reader);
+	command->command(r, &reader);
 
 	return 0;
 }
 
-void protocol_v2_serve_loop(int stateless_rpc)
+void protocol_v2_serve_loop(struct repository *r, int stateless_rpc)
 {
 	if (!stateless_rpc)
-		protocol_v2_advertise_capabilities();
+		protocol_v2_advertise_capabilities(r);
 
 	/*
 	 * If stateless-rpc was requested then exit after
 	 * a single request/response exchange
 	 */
 	if (stateless_rpc) {
-		process_request();
+		process_request(r);
 	} else {
 		for (;;)
-			if (process_request())
+			if (process_request(r))
 				break;
 	}
 }
diff --git a/serve.h b/serve.h
index f946cf904a242db5106625e280d7daa671348516..85bf73cfe53cb9cbb4b042c43a1f6a55338ad6ed 100644
--- a/serve.h
+++ b/serve.h
@@ -1,7 +1,9 @@
 #ifndef SERVE_H
 #define SERVE_H
 
-void protocol_v2_advertise_capabilities(void);
-void protocol_v2_serve_loop(int stateless_rpc);
+struct repository;
+
+void protocol_v2_advertise_capabilities(struct repository *r);
+void protocol_v2_serve_loop(struct repository *r, int stateless_rpc);
 
 #endif /* SERVE_H */
diff --git a/t/helper/test-serve-v2.c b/t/helper/test-serve-v2.c
index 054cbcf5d83946b225774dc9da6b0ec1d112e79d..63a200b8d46f68bfd69f63f844977cc8e382bb32 100644
--- a/t/helper/test-serve-v2.c
+++ b/t/helper/test-serve-v2.c
@@ -1,6 +1,9 @@
+#define USE_THE_REPOSITORY_VARIABLE
+
 #include "test-tool.h"
 #include "gettext.h"
 #include "parse-options.h"
+#include "repository.h"
 #include "serve.h"
 #include "setup.h"
 
@@ -28,9 +31,9 @@ int cmd__serve_v2(int argc, const char **argv)
 			     PARSE_OPT_KEEP_UNKNOWN_OPT);
 
 	if (advertise_capabilities)
-		protocol_v2_advertise_capabilities();
+		protocol_v2_advertise_capabilities(the_repository);
 	else
-		protocol_v2_serve_loop(stateless_rpc);
+		protocol_v2_serve_loop(the_repository, stateless_rpc);
 
 	return 0;
 }
-- 
2.48.0.rc0.184.g0fc57dec57.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 10 of 26 in “Stop using `the_repository` in some trivial cases”
  1. 00/14 Stop using `the_repository` in some trivial casesPatrick Steinhardt, Dec 17, 2024
  2. 01/14 progress: stop using `the_repository`Patrick Steinhardt, Dec 17, 2024
  3. Karthik NayakDec 31, 2024
  4. Toon ClaesJan 6, 2025
  5. Patrick SteinhardtJan 7, 2025
  6. 02/14 pager: stop using `the_repository`Patrick Steinhardt, Dec 17, 2024
  7. shejialuoDec 17, 2024
  8. Karthik NayakDec 31, 2024
  9. 03/14 trace: stop using `the_repository`Patrick Steinhardt, Dec 17, 2024
  10. 04/14 serve: stop using `the_repository`Patrick Steinhardt, Dec 17, 2024
  11. 05/14 send-pack: stop using `the_repository`Patrick Steinhardt, Dec 17, 2024
  12. 06/14 server-info: stop using `the_repository`Patrick Steinhardt, Dec 17, 2024
  13. shejialuoDec 17, 2024
  14. 07/14 diagnose: stop using `the_repository`Patrick Steinhardt, Dec 17, 2024
  15. 08/14 mailinfo: stop using `the_repository`Patrick Steinhardt, Dec 17, 2024
  16. 09/14 credential: stop using `the_repository`Patrick Steinhardt, Dec 17, 2024
  17. 10/14 resolve-undo: stop using `the_repository`Patrick Steinhardt, Dec 17, 2024
  18. 11/14 tmp-objdir: stop using `the_repository`Patrick Steinhardt, Dec 17, 2024
  19. 12/14 add-interactive: stop using `the_repository`Patrick Steinhardt, Dec 17, 2024
  20. 13/14 graph: stop using `the_repository`Patrick Steinhardt, Dec 17, 2024
  21. 14/14 match-trees: stop using `the_repository`Patrick Steinhardt, Dec 17, 2024
  22. shejialuoDec 17, 2024
  23. Patrick SteinhardtDec 27, 2024
  24. Karthik NayakJan 7, 2025
  25. Toon ClaesJan 7, 2025
  26. Junio C HamanoJan 7, 2025

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.