blob: c53f7df5be4abae7a4f5792f42e4c4ecac006e9a [file] [log] [blame]
Brandon Williams373d70e2017-10-16 10:55:24 -07001#include "cache.h"
2#include "config.h"
3#include "protocol.h"
4
5static enum protocol_version parse_protocol_version(const char *value)
6{
7 if (!strcmp(value, "0"))
8 return protocol_v0;
9 else if (!strcmp(value, "1"))
10 return protocol_v1;
Brandon Williams8f6982b2018-03-14 11:31:47 -070011 else if (!strcmp(value, "2"))
12 return protocol_v2;
Brandon Williams373d70e2017-10-16 10:55:24 -070013 else
14 return protocol_unknown_version;
15}
16
17enum protocol_version get_protocol_version_config(void)
18{
19 const char *value;
Jonathan Tan8cbeba02019-02-25 13:54:06 -080020 const char *git_test_k = "GIT_TEST_PROTOCOL_VERSION";
Jonathan Nieder33166f32019-12-23 17:02:28 -080021 const char *git_test_v;
Jonathan Tan8cbeba02019-02-25 13:54:06 -080022
Jeff Kingf1de9812020-08-14 12:17:36 -040023 if (!git_config_get_string_tmp("protocol.version", &value)) {
Brandon Williams373d70e2017-10-16 10:55:24 -070024 enum protocol_version version = parse_protocol_version(value);
25
26 if (version == protocol_unknown_version)
27 die("unknown value for config 'protocol.version': %s",
28 value);
29
Jonathan Nieder33166f32019-12-23 17:02:28 -080030 return version;
Brandon Williams373d70e2017-10-16 10:55:24 -070031 }
32
Jonathan Nieder33166f32019-12-23 17:02:28 -080033 git_test_v = getenv(git_test_k);
Jonathan Tan8cbeba02019-02-25 13:54:06 -080034 if (git_test_v && *git_test_v) {
35 enum protocol_version env = parse_protocol_version(git_test_v);
36
37 if (env == protocol_unknown_version)
38 die("unknown value for %s: %s", git_test_k, git_test_v);
Jonathan Nieder33166f32019-12-23 17:02:28 -080039 return env;
Jonathan Tan8cbeba02019-02-25 13:54:06 -080040 }
41
Jeff Kingeb049752020-09-25 14:34:36 -040042 return protocol_v2;
Brandon Williams373d70e2017-10-16 10:55:24 -070043}
44
45enum protocol_version determine_protocol_version_server(void)
46{
47 const char *git_protocol = getenv(GIT_PROTOCOL_ENVIRONMENT);
48 enum protocol_version version = protocol_v0;
49
50 /*
51 * Determine which protocol version the client has requested. Since
52 * multiple 'version' keys can be sent by the client, indicating that
53 * the client is okay to speak any of them, select the greatest version
54 * that the client has requested. This is due to the assumption that
55 * the most recent protocol version will be the most state-of-the-art.
56 */
57 if (git_protocol) {
58 struct string_list list = STRING_LIST_INIT_DUP;
59 const struct string_list_item *item;
60 string_list_split(&list, git_protocol, ':', -1);
61
62 for_each_string_list_item(item, &list) {
63 const char *value;
64 enum protocol_version v;
65
66 if (skip_prefix(item->string, "version=", &value)) {
67 v = parse_protocol_version(value);
68 if (v > version)
69 version = v;
70 }
71 }
72
73 string_list_clear(&list, 0);
74 }
75
Josh Steadmon626beeb2021-08-10 10:20:39 -070076 trace2_data_intmax("transfer", NULL, "negotiated-version", version);
77
Brandon Williams373d70e2017-10-16 10:55:24 -070078 return version;
79}
80
81enum protocol_version determine_protocol_version_client(const char *server_response)
82{
83 enum protocol_version version = protocol_v0;
84
85 if (skip_prefix(server_response, "version ", &server_response)) {
86 version = parse_protocol_version(server_response);
87
88 if (version == protocol_unknown_version)
89 die("server is speaking an unknown protocol");
90 if (version == protocol_v0)
91 die("protocol error: server explicitly said version 0");
92 }
93
94 return version;
95}