Elijah Newren | 5579f44 | 2023-04-11 00:41:48 -0700 | [diff] [blame] | 1 | #include "git-compat-util.h" |
Brandon Williams | 373d70e | 2017-10-16 10:55:24 -0700 | [diff] [blame] | 2 | #include "config.h" |
Elijah Newren | 32a8f51 | 2023-03-21 06:26:03 +0000 | [diff] [blame] | 3 | #include "environment.h" |
Brandon Williams | 373d70e | 2017-10-16 10:55:24 -0700 | [diff] [blame] | 4 | #include "protocol.h" |
Elijah Newren | 74ea5c9 | 2023-04-11 03:00:38 +0000 | [diff] [blame] | 5 | #include "trace2.h" |
Brandon Williams | 373d70e | 2017-10-16 10:55:24 -0700 | [diff] [blame] | 6 | |
| 7 | static enum protocol_version parse_protocol_version(const char *value) |
| 8 | { |
| 9 | if (!strcmp(value, "0")) |
| 10 | return protocol_v0; |
| 11 | else if (!strcmp(value, "1")) |
| 12 | return protocol_v1; |
Brandon Williams | 8f6982b | 2018-03-14 11:31:47 -0700 | [diff] [blame] | 13 | else if (!strcmp(value, "2")) |
| 14 | return protocol_v2; |
Brandon Williams | 373d70e | 2017-10-16 10:55:24 -0700 | [diff] [blame] | 15 | else |
| 16 | return protocol_unknown_version; |
| 17 | } |
| 18 | |
| 19 | enum protocol_version get_protocol_version_config(void) |
| 20 | { |
| 21 | const char *value; |
Jonathan Tan | 8cbeba0 | 2019-02-25 13:54:06 -0800 | [diff] [blame] | 22 | const char *git_test_k = "GIT_TEST_PROTOCOL_VERSION"; |
Jonathan Nieder | 33166f3 | 2019-12-23 17:02:28 -0800 | [diff] [blame] | 23 | const char *git_test_v; |
Jonathan Tan | 8cbeba0 | 2019-02-25 13:54:06 -0800 | [diff] [blame] | 24 | |
Jeff King | f1de981 | 2020-08-14 12:17:36 -0400 | [diff] [blame] | 25 | if (!git_config_get_string_tmp("protocol.version", &value)) { |
Brandon Williams | 373d70e | 2017-10-16 10:55:24 -0700 | [diff] [blame] | 26 | enum protocol_version version = parse_protocol_version(value); |
| 27 | |
| 28 | if (version == protocol_unknown_version) |
| 29 | die("unknown value for config 'protocol.version': %s", |
| 30 | value); |
| 31 | |
Jonathan Nieder | 33166f3 | 2019-12-23 17:02:28 -0800 | [diff] [blame] | 32 | return version; |
Brandon Williams | 373d70e | 2017-10-16 10:55:24 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Jonathan Nieder | 33166f3 | 2019-12-23 17:02:28 -0800 | [diff] [blame] | 35 | git_test_v = getenv(git_test_k); |
Jonathan Tan | 8cbeba0 | 2019-02-25 13:54:06 -0800 | [diff] [blame] | 36 | if (git_test_v && *git_test_v) { |
| 37 | enum protocol_version env = parse_protocol_version(git_test_v); |
| 38 | |
| 39 | if (env == protocol_unknown_version) |
| 40 | die("unknown value for %s: %s", git_test_k, git_test_v); |
Jonathan Nieder | 33166f3 | 2019-12-23 17:02:28 -0800 | [diff] [blame] | 41 | return env; |
Jonathan Tan | 8cbeba0 | 2019-02-25 13:54:06 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Jeff King | eb04975 | 2020-09-25 14:34:36 -0400 | [diff] [blame] | 44 | return protocol_v2; |
Brandon Williams | 373d70e | 2017-10-16 10:55:24 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | enum protocol_version determine_protocol_version_server(void) |
| 48 | { |
| 49 | const char *git_protocol = getenv(GIT_PROTOCOL_ENVIRONMENT); |
| 50 | enum protocol_version version = protocol_v0; |
| 51 | |
| 52 | /* |
| 53 | * Determine which protocol version the client has requested. Since |
| 54 | * multiple 'version' keys can be sent by the client, indicating that |
| 55 | * the client is okay to speak any of them, select the greatest version |
| 56 | * that the client has requested. This is due to the assumption that |
| 57 | * the most recent protocol version will be the most state-of-the-art. |
| 58 | */ |
| 59 | if (git_protocol) { |
| 60 | struct string_list list = STRING_LIST_INIT_DUP; |
| 61 | const struct string_list_item *item; |
| 62 | string_list_split(&list, git_protocol, ':', -1); |
| 63 | |
| 64 | for_each_string_list_item(item, &list) { |
| 65 | const char *value; |
| 66 | enum protocol_version v; |
| 67 | |
| 68 | if (skip_prefix(item->string, "version=", &value)) { |
| 69 | v = parse_protocol_version(value); |
| 70 | if (v > version) |
| 71 | version = v; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | string_list_clear(&list, 0); |
| 76 | } |
| 77 | |
Josh Steadmon | 626beeb | 2021-08-10 10:20:39 -0700 | [diff] [blame] | 78 | trace2_data_intmax("transfer", NULL, "negotiated-version", version); |
| 79 | |
Brandon Williams | 373d70e | 2017-10-16 10:55:24 -0700 | [diff] [blame] | 80 | return version; |
| 81 | } |
| 82 | |
| 83 | enum protocol_version determine_protocol_version_client(const char *server_response) |
| 84 | { |
| 85 | enum protocol_version version = protocol_v0; |
| 86 | |
| 87 | if (skip_prefix(server_response, "version ", &server_response)) { |
| 88 | version = parse_protocol_version(server_response); |
| 89 | |
| 90 | if (version == protocol_unknown_version) |
| 91 | die("server is speaking an unknown protocol"); |
| 92 | if (version == protocol_v0) |
| 93 | die("protocol error: server explicitly said version 0"); |
| 94 | } |
| 95 | |
| 96 | return version; |
| 97 | } |