From 06708ce18066b40e05fba22491542d95b43b0f22 Mon Sep 17 00:00:00 2001 From: Xing Xin Date: Tue, 8 Oct 2024 03:38:15 +0000 Subject: [PATCH 1/5] transport: introduce parse_transport_option() method Add the `parse_transport_option()` method to parse the `push.pushOption` configuration. This method will also be used in the next commit to handle the new `remote..serverOption` configuration for setting server options in Git protocol v2. Signed-off-by: Xing Xin Reviewed-by: Patrick Steinhardt Signed-off-by: Junio C Hamano --- builtin/push.c | 9 +-------- transport.c | 12 ++++++++++++ transport.h | 4 ++++ 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/builtin/push.c b/builtin/push.c index 59d4485603..51c609f208 100644 --- a/builtin/push.c +++ b/builtin/push.c @@ -519,14 +519,7 @@ static int git_push_config(const char *k, const char *v, RECURSE_SUBMODULES_ON_DEMAND : RECURSE_SUBMODULES_OFF; recurse_submodules = val; } else if (!strcmp(k, "push.pushoption")) { - if (!v) - return config_error_nonbool(k); - else - if (!*v) - string_list_clear(&push_options_config, 0); - else - string_list_append(&push_options_config, v); - return 0; + return parse_transport_option(k, v, &push_options_config); } else if (!strcmp(k, "color.push")) { push_use_color = git_config_colorbool(k, v); return 0; diff --git a/transport.c b/transport.c index 1098bbd60e..19b7e4cffd 100644 --- a/transport.c +++ b/transport.c @@ -1108,6 +1108,18 @@ int is_transport_allowed(const char *type, int from_user) BUG("invalid protocol_allow_config type"); } +int parse_transport_option(const char *var, const char *value, + struct string_list *transport_options) +{ + if (!value) + return config_error_nonbool(var); + if (!*value) + string_list_clear(transport_options, 0); + else + string_list_append(transport_options, value); + return 0; +} + void transport_check_allowed(const char *type) { if (!is_transport_allowed(type, -1)) diff --git a/transport.h b/transport.h index 6393cd9823..44100fa9b7 100644 --- a/transport.h +++ b/transport.h @@ -342,4 +342,8 @@ void transport_print_push_status(const char *dest, struct ref *refs, /* common method used by transport-helper.c and send-pack.c */ void reject_atomic_push(struct ref *refs, int mirror_mode); +/* common method to parse push-option or server-option from config */ +int parse_transport_option(const char *var, const char *value, + struct string_list *transport_options); + #endif From 72da5cfb1c81a1835a1f6bad5f19451baf9ad03c Mon Sep 17 00:00:00 2001 From: Xing Xin Date: Tue, 8 Oct 2024 03:38:16 +0000 Subject: [PATCH 2/5] remote: introduce remote..serverOption configuration Currently, server options for Git protocol v2 can only be specified via the command line option "--server-option" or "-o", which is inconvenient when users want to specify a list of default options to send. Therefore, we are introducing a new configuration to hold a list of default server options, akin to the `push.pushOption` configuration for push options. Initially, I named the new configuration `fetch.serverOption` to align with `push.pushOption`. However, after discussing with Patrick, it was renamed to `remote..serverOption` as suggested, because: 1. Server options are designed to be server-specific, making it more logical to use a per-remote configuration. 2. Using "fetch." prefixed configurations in git-clone or git-ls-remote seems out of place and inconsistent in design. The parsing logic for `remote..serverOption` also relies on `transport.c:parse_transport_option`, similar to `push.pushOption`, and they follow the same priority design: 1. Server options set in lower-priority configuration files (e.g., /etc/gitconfig or $HOME/.gitconfig) can be overridden or unset in more specific repository configurations using an empty string. 2. Command-line specified server options take precedence over those from the configuration. Server options from configuration are stored to the corresponding `remote.h:remote` as a new field `server_options`. The field will be utilized in the subsequent commit to help initialize the `server_options` of `transport.h:transport`. And documentation have been updated accordingly. Helped-by: Patrick Steinhardt Helped-by: Junio C Hamano Reported-by: Liu Zhongbo Signed-off-by: Xing Xin Reviewed-by: Patrick Steinhardt Signed-off-by: Junio C Hamano --- Documentation/config/remote.txt | 10 ++++++++++ remote.c | 6 ++++++ remote.h | 3 +++ 3 files changed, 19 insertions(+) diff --git a/Documentation/config/remote.txt b/Documentation/config/remote.txt index 71d1fee835..6d8b7d6c63 100644 --- a/Documentation/config/remote.txt +++ b/Documentation/config/remote.txt @@ -96,3 +96,13 @@ remote..partialclonefilter:: Changing or clearing this value will only affect fetches for new commits. To fetch associated objects for commits already present in the local object database, use the `--refetch` option of linkgit:git-fetch[1]. + +remote..serverOption:: + The default set of server options used when fetching from this remote. + These server options can be overridden by the `--server-option=` command + line arguments. ++ +This is a multi-valued variable, and an empty value can be used in a higher +priority configuration file (e.g. `.git/config` in a repository) to clear +the values inherited from a lower priority configuration files (e.g. +`$HOME/.gitconfig`). diff --git a/remote.c b/remote.c index e291e8ff5c..1777d45086 100644 --- a/remote.c +++ b/remote.c @@ -24,6 +24,7 @@ #include "advice.h" #include "connect.h" #include "parse-options.h" +#include "transport.h" enum map_direction { FROM_SRC, FROM_DST }; @@ -143,6 +144,7 @@ static struct remote *make_remote(struct remote_state *remote_state, ret->name = xstrndup(name, len); refspec_init(&ret->push, REFSPEC_PUSH); refspec_init(&ret->fetch, REFSPEC_FETCH); + string_list_init_dup(&ret->server_options); ALLOC_GROW(remote_state->remotes, remote_state->remotes_nr + 1, remote_state->remotes_alloc); @@ -166,6 +168,7 @@ static void remote_clear(struct remote *remote) free((char *)remote->uploadpack); FREE_AND_NULL(remote->http_proxy); FREE_AND_NULL(remote->http_proxy_authmethod); + string_list_clear(&remote->server_options, 0); } static void add_merge(struct branch *branch, const char *name) @@ -508,6 +511,9 @@ static int handle_config(const char *key, const char *value, } else if (!strcmp(subkey, "vcs")) { FREE_AND_NULL(remote->foreign_vcs); return git_config_string(&remote->foreign_vcs, key, value); + } else if (!strcmp(subkey, "serveroption")) { + return parse_transport_option(key, value, + &remote->server_options); } return 0; } diff --git a/remote.h b/remote.h index ad4513f639..a694681620 100644 --- a/remote.h +++ b/remote.h @@ -4,6 +4,7 @@ #include "hash.h" #include "hashmap.h" #include "refspec.h" +#include "string-list.h" #include "strvec.h" struct option; @@ -104,6 +105,8 @@ struct remote { /* The method used for authenticating against `http_proxy`. */ char *http_proxy_authmethod; + + struct string_list server_options; }; /** From 094f78a16a6c150ff125afd9c3f2773b47384c06 Mon Sep 17 00:00:00 2001 From: Xing Xin Date: Tue, 8 Oct 2024 03:38:17 +0000 Subject: [PATCH 3/5] transport.c::handshake: make use of server options from remote Utilize the `server_options` from the corresponding remote during the handshake in `transport.c` when Git protocol v2 is detected. This helps initialize the `server_options` in `transport.h:transport` if no server options are set for the transport (typically via `--server-option` or `-o`). While another potential place to incorporate server options from the remote is in `transport.c:transport_get`, setting server options for a transport using a protocol other than v2 could lead to unexpected errors (see `transport.c:die_if_server_options`). Relevant tests and documentation have been updated accordingly. Signed-off-by: Xing Xin Reviewed-by: Patrick Steinhardt Signed-off-by: Junio C Hamano --- Documentation/fetch-options.txt | 3 + Documentation/git-clone.txt | 3 + Documentation/git-ls-remote.txt | 3 + t/t5702-protocol-v2.sh | 123 ++++++++++++++++++++++++++++++++ transport.c | 3 + 5 files changed, 135 insertions(+) diff --git a/Documentation/fetch-options.txt b/Documentation/fetch-options.txt index 80838fe37e..9dc7ac8dbd 100644 --- a/Documentation/fetch-options.txt +++ b/Documentation/fetch-options.txt @@ -305,6 +305,9 @@ endif::git-pull[] unknown ones, is server-specific. When multiple `--server-option=