Commit | Line | Data |
---|---|---|
373d70ef BW |
1 | #include "cache.h" |
2 | #include "config.h" | |
3 | #include "protocol.h" | |
4 | ||
5 | static 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; | |
8f6982b4 BW |
11 | else if (!strcmp(value, "2")) |
12 | return protocol_v2; | |
373d70ef BW |
13 | else |
14 | return protocol_unknown_version; | |
15 | } | |
16 | ||
17 | enum protocol_version get_protocol_version_config(void) | |
18 | { | |
19 | const char *value; | |
3697caf4 | 20 | int val; |
8cbeba06 | 21 | const char *git_test_k = "GIT_TEST_PROTOCOL_VERSION"; |
33166f3a | 22 | const char *git_test_v; |
8cbeba06 | 23 | |
373d70ef BW |
24 | if (!git_config_get_string_const("protocol.version", &value)) { |
25 | enum protocol_version version = parse_protocol_version(value); | |
26 | ||
27 | if (version == protocol_unknown_version) | |
28 | die("unknown value for config 'protocol.version': %s", | |
29 | value); | |
30 | ||
33166f3a | 31 | return version; |
8cbeba06 JT |
32 | } |
33 | ||
3697caf4 JN |
34 | if (!git_config_get_bool("feature.experimental", &val) && val) |
35 | return protocol_v2; | |
36 | ||
33166f3a | 37 | git_test_v = getenv(git_test_k); |
8cbeba06 JT |
38 | if (git_test_v && *git_test_v) { |
39 | enum protocol_version env = parse_protocol_version(git_test_v); | |
40 | ||
41 | if (env == protocol_unknown_version) | |
42 | die("unknown value for %s: %s", git_test_k, git_test_v); | |
33166f3a | 43 | return env; |
373d70ef BW |
44 | } |
45 | ||
11c7f2a3 | 46 | return protocol_v0; |
373d70ef BW |
47 | } |
48 | ||
49 | enum protocol_version determine_protocol_version_server(void) | |
50 | { | |
51 | const char *git_protocol = getenv(GIT_PROTOCOL_ENVIRONMENT); | |
52 | enum protocol_version version = protocol_v0; | |
53 | ||
54 | /* | |
55 | * Determine which protocol version the client has requested. Since | |
56 | * multiple 'version' keys can be sent by the client, indicating that | |
57 | * the client is okay to speak any of them, select the greatest version | |
58 | * that the client has requested. This is due to the assumption that | |
59 | * the most recent protocol version will be the most state-of-the-art. | |
60 | */ | |
61 | if (git_protocol) { | |
62 | struct string_list list = STRING_LIST_INIT_DUP; | |
63 | const struct string_list_item *item; | |
64 | string_list_split(&list, git_protocol, ':', -1); | |
65 | ||
66 | for_each_string_list_item(item, &list) { | |
67 | const char *value; | |
68 | enum protocol_version v; | |
69 | ||
70 | if (skip_prefix(item->string, "version=", &value)) { | |
71 | v = parse_protocol_version(value); | |
72 | if (v > version) | |
73 | version = v; | |
74 | } | |
75 | } | |
76 | ||
77 | string_list_clear(&list, 0); | |
78 | } | |
79 | ||
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 | } |