cocci: remove 'unused.cocci'
[git.git] / protocol.c
blobbdb32e1eeb62473b9ae309b266c7f508a480c354
1 #include "cache.h"
2 #include "config.h"
3 #include "environment.h"
4 #include "protocol.h"
6 static enum protocol_version parse_protocol_version(const char *value)
8 if (!strcmp(value, "0"))
9 return protocol_v0;
10 else if (!strcmp(value, "1"))
11 return protocol_v1;
12 else if (!strcmp(value, "2"))
13 return protocol_v2;
14 else
15 return protocol_unknown_version;
18 enum protocol_version get_protocol_version_config(void)
20 const char *value;
21 const char *git_test_k = "GIT_TEST_PROTOCOL_VERSION";
22 const char *git_test_v;
24 if (!git_config_get_string_tmp("protocol.version", &value)) {
25 enum protocol_version version = parse_protocol_version(value);
27 if (version == protocol_unknown_version)
28 die("unknown value for config 'protocol.version': %s",
29 value);
31 return version;
34 git_test_v = getenv(git_test_k);
35 if (git_test_v && *git_test_v) {
36 enum protocol_version env = parse_protocol_version(git_test_v);
38 if (env == protocol_unknown_version)
39 die("unknown value for %s: %s", git_test_k, git_test_v);
40 return env;
43 return protocol_v2;
46 enum protocol_version determine_protocol_version_server(void)
48 const char *git_protocol = getenv(GIT_PROTOCOL_ENVIRONMENT);
49 enum protocol_version version = protocol_v0;
52 * Determine which protocol version the client has requested. Since
53 * multiple 'version' keys can be sent by the client, indicating that
54 * the client is okay to speak any of them, select the greatest version
55 * that the client has requested. This is due to the assumption that
56 * the most recent protocol version will be the most state-of-the-art.
58 if (git_protocol) {
59 struct string_list list = STRING_LIST_INIT_DUP;
60 const struct string_list_item *item;
61 string_list_split(&list, git_protocol, ':', -1);
63 for_each_string_list_item(item, &list) {
64 const char *value;
65 enum protocol_version v;
67 if (skip_prefix(item->string, "version=", &value)) {
68 v = parse_protocol_version(value);
69 if (v > version)
70 version = v;
74 string_list_clear(&list, 0);
77 trace2_data_intmax("transfer", NULL, "negotiated-version", version);
79 return version;
82 enum protocol_version determine_protocol_version_client(const char *server_response)
84 enum protocol_version version = protocol_v0;
86 if (skip_prefix(server_response, "version ", &server_response)) {
87 version = parse_protocol_version(server_response);
89 if (version == protocol_unknown_version)
90 die("server is speaking an unknown protocol");
91 if (version == protocol_v0)
92 die("protocol error: server explicitly said version 0");
95 return version;