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

[PATCH 4/4] builtin/ls-remote.c: recognize fetch.serverOption configuration

From
XGXing Xin via GitGitGadget <gitgitgadget@gmail.com>
Date
Sep 2, 2024, 12:13 UTC
Message-ID
<8962031f9997c4030cf32652908fc431f3bf9976.1725279236.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1776.git.git.1725279236.gitgitgadget@gmail.com>
From: Xing Xin <xingxin.xx@bytedance.com>

Teach git-ls-remote to recognize the `fetch.serverOption` configuration as a default list of server options to send for Git protocol v2, if server options are not explicitly set via the command line.

Tests and documentation have been updated accordingly.
Signed-off-by: Xing Xin <xingxin.xx@bytedance.com>
---
 Documentation/git-ls-remote.txt |  3 +++
 builtin/ls-remote.c             | 32 ++++++++++++++++++++++++++++----
 t/t5702-protocol-v2.sh          | 29 ++++++++++++++++++++++++-----
 3 files changed, 55 insertions(+), 9 deletions(-)
diff --git a/Documentation/git-ls-remote.txt b/Documentation/git-ls-remote.txt
index 76c86c3ce4f..fc9930193f8 100644
--- a/Documentation/git-ls-remote.txt
+++ b/Documentation/git-ls-remote.txt
@@ -81,6 +81,9 @@ OPTIONS
 	character.
 	When multiple `--server-option=<option>` are given, they are all
 	sent to the other side in the order listed on the command line.
+	When no `--server-option=<option>` is given from the command
+	line, the values of configuration variable `fetch.serverOption`
+	are used instead.
 
 <repository>::
 	The "remote" repository to query.  This parameter can be
diff --git a/builtin/ls-remote.c b/builtin/ls-remote.c
index 0a491595ca8..87afd69828c 100644
--- a/builtin/ls-remote.c
+++ b/builtin/ls-remote.c
@@ -1,4 +1,5 @@
 #include "builtin.h"
+#include "config.h"
 #include "gettext.h"
 #include "hex.h"
 #include "transport.h"
@@ -8,6 +9,8 @@
 #include "parse-options.h"
 #include "wildmatch.h"
 
+static struct string_list config_server_options = STRING_LIST_INIT_DUP;
+
 static const char * const ls_remote_usage[] = {
 	N_("git ls-remote [--branches] [--tags] [--refs] [--upload-pack=<exec>]\n"
 	   "              [-q | --quiet] [--exit-code] [--get-url] [--sort=<key>]\n"
@@ -37,6 +40,18 @@ static int tail_match(const struct strvec *pattern, const char *path)
 	return 0;
 }
 
+static int git_ls_remote_config(const char *k, const char *v,
+			    const struct config_context *ctx, void *cb)
+{
+	if (!strcmp(k, "fetch.serveroption")) {
+		if (!v)
+			return config_error_nonbool(k);
+		parse_transport_option(v, &config_server_options);
+		return 0;
+	}
+	return git_default_config(k, v, ctx, cb);
+}
+
 int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 {
 	const char *dest = NULL;
@@ -49,8 +64,9 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 	struct strvec pattern = STRVEC_INIT;
 	struct transport_ls_refs_options transport_options =
 		TRANSPORT_LS_REFS_OPTIONS_INIT;
+	struct string_list option_server_options = STRING_LIST_INIT_DUP;
+	struct string_list *server_options = NULL;
 	int i;
-	struct string_list server_options = STRING_LIST_INIT_DUP;
 
 	struct remote *remote;
 	struct transport *transport;
@@ -80,16 +96,23 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 			      2, PARSE_OPT_NOCOMPLETE),
 		OPT_BOOL(0, "symref", &show_symref_target,
 			 N_("show underlying ref in addition to the object pointed by it")),
-		OPT_STRING_LIST('o', "server-option", &server_options, N_("server-specific"), N_("option to transmit")),
+		OPT_STRING_LIST('o', "server-option", &option_server_options,
+				N_("server-specific"),
+				N_("option to transmit")),
 		OPT_END()
 	};
 
+	git_config(git_ls_remote_config, NULL);
+
 	memset(&ref_array, 0, sizeof(ref_array));
 
 	argc = parse_options(argc, argv, prefix, options, ls_remote_usage,
 			     PARSE_OPT_STOP_AT_NON_OPTION);
 	dest = argv[0];
 
+	server_options = option_server_options.nr ?
+			 &option_server_options : &config_server_options;
+
 	/*
 	 * TODO: This is buggy, but required for transport helpers. When a
 	 * transport helper advertises a "refspec", then we'd add that to a
@@ -130,8 +153,8 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 	transport = transport_get(remote, NULL);
 	if (uploadpack)
 		transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
-	if (server_options.nr)
-		transport->server_options = &server_options;
+	if (server_options && server_options->nr)
+		transport->server_options = server_options;
 
 	ref = transport_get_remote_refs(transport, &transport_options);
 	if (ref) {
@@ -169,5 +192,6 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 	transport_ls_refs_options_release(&transport_options);
 
 	strvec_clear(&pattern);
+	string_list_clear(&option_server_options, 0);
 	return status;
 }
diff --git a/t/t5702-protocol-v2.sh b/t/t5702-protocol-v2.sh
index 3bf31fb570d..da6241afa22 100755
--- a/t/t5702-protocol-v2.sh
+++ b/t/t5702-protocol-v2.sh
@@ -173,16 +173,35 @@ test_expect_success 'ref advertisement is filtered with ls-remote using protocol
 test_expect_success 'server-options are sent when using ls-remote' '
 	test_when_finished "rm -f log" &&
 
-	GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
-		ls-remote -o hello -o world "file://$(pwd)/file_parent" main >actual &&
-
 	cat >expect <<-EOF &&
 	$(git -C file_parent rev-parse refs/heads/main)$(printf "\t")refs/heads/main
 	EOF
 
+	# Specify server options from command line
+	GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
+		ls-remote -o hello -o world "file://$(pwd)/file_parent" main >actual &&
+	test_cmp expect actual &&
+	test_grep "server-option=hello" log &&
+	test_grep "server-option=world" log &&
+	rm -f log &&
+
+	# Specify server options from fetch.serverOption config
+	GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
+		-c fetch.serverOption=hello -c fetch.serverOption=world \
+		ls-remote "file://$(pwd)/file_parent" main >actual &&
+	test_cmp expect actual &&
+	test_grep "server-option=hello" log &&
+	test_grep "server-option=world" log &&
+	rm -f log &&
+
+	# Cmdline server options take a higher priority
+	GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
+		-c fetch.serverOption=hello -c fetch.serverOption=world \
+		ls-remote -o foo=bar "file://$(pwd)/file_parent" main >actual &&
 	test_cmp expect actual &&
-	grep "server-option=hello" log &&
-	grep "server-option=world" log
+	test_grep ! "server-option=hello" log &&
+	test_grep ! "server-option=world" log &&
+	test_grep "server-option=foo=bar" log
 '
 
 test_expect_success 'warn if using server-option with ls-remote with legacy protocol' '
-- 
gitgitgadget
Previous: Junio C HamanoNext: Patrick Steinhardt
Message 16 of 39 in “Support server option from configuration”
  1. 0/4 Support server option from configurationblanet via GitGitGadget, Sep 2, 2024
  2. 1/4 transport: add parse_transport_option() methodXing Xin via GitGitGadget, Sep 2, 2024
  3. 2/4 builtin/fetch.c: add fetch.serverOption configurationXing Xin via GitGitGadget, Sep 2, 2024
  4. Patrick SteinhardtSep 3, 2024
  5. 3/4 builtin/clone.c: recognize fetch.serverOption configurationXing Xin via GitGitGadget, Sep 2, 2024
  6. Patrick SteinhardtSep 3, 2024
  7. Xing XinSep 4, 2024
  8. Patrick SteinhardtSep 5, 2024
  9. Xing XinSep 5, 2024
  10. Patrick SteinhardtSep 5, 2024
  11. Junio C HamanoSep 5, 2024
  12. Xing XinSep 9, 2024
  13. Patrick SteinhardtSep 9, 2024
  14. Xing XinSep 23, 2024
  15. Junio C HamanoSep 9, 2024
  16. 4/4 builtin/ls-remote.c: recognize fetch.serverOption configurationXing Xin via GitGitGadget, Sep 2, 2024
  17. Patrick SteinhardtSep 3, 2024
  18. Patrick SteinhardtSep 3, 2024
  19. 0/5 Support server option from configurationblanet via GitGitGadget, Sep 23, 2024
  20. 1/5 transport: introduce parse_transport_option() methodXing Xin via GitGitGadget, Sep 23, 2024
  21. 2/5 remote: introduce remote.<name>.serverOption configurationXing Xin via GitGitGadget, Sep 23, 2024
  22. Patrick SteinhardtOct 7, 2024
  23. Xing XinOct 8, 2024
  24. 3/5 transport.c::handshake: make use of server options from remoteXing Xin via GitGitGadget, Sep 23, 2024
  25. 4/5 fetch: respect --server-option when fetching multiple remotesXing Xin via GitGitGadget, Sep 23, 2024
  26. Patrick SteinhardtOct 7, 2024
  27. 5/5 ls-remote: leakfix for not clearing server_optionsXing Xin via GitGitGadget, Sep 23, 2024
  28. Patrick SteinhardtOct 7, 2024
  29. Patrick SteinhardtOct 7, 2024
  30. Xing XinOct 8, 2024
  31. 0/5 Support server option from configurationblanet via GitGitGadget, Oct 8, 2024
  32. 1/5 transport: introduce parse_transport_option() methodXing Xin via GitGitGadget, Oct 8, 2024
  33. 2/5 remote: introduce remote.<name>.serverOption configurationXing Xin via GitGitGadget, Oct 8, 2024
  34. 3/5 transport.c::handshake: make use of server options from remoteXing Xin via GitGitGadget, Oct 8, 2024
  35. 4/5 fetch: respect --server-option when fetching multiple remotesXing Xin via GitGitGadget, Oct 8, 2024
  36. Junio C HamanoOct 8, 2024
  37. 5/5 ls-remote: leakfix for not clearing server_optionsXing Xin via GitGitGadget, Oct 8, 2024
  38. Patrick SteinhardtOct 8, 2024
  39. Junio C HamanoOct 8, 2024

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.