1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
5 #include "environment.h"
9 static enum protocol_version
parse_protocol_version(const char *value
)
11 if (!strcmp(value
, "0"))
13 else if (!strcmp(value
, "1"))
15 else if (!strcmp(value
, "2"))
18 return protocol_unknown_version
;
21 enum protocol_version
get_protocol_version_config(void)
24 const char *git_test_k
= "GIT_TEST_PROTOCOL_VERSION";
25 const char *git_test_v
;
27 if (!git_config_get_string_tmp("protocol.version", &value
)) {
28 enum protocol_version version
= parse_protocol_version(value
);
30 if (version
== protocol_unknown_version
)
31 die("unknown value for config 'protocol.version': %s",
37 git_test_v
= getenv(git_test_k
);
38 if (git_test_v
&& *git_test_v
) {
39 enum protocol_version env
= parse_protocol_version(git_test_v
);
41 if (env
== protocol_unknown_version
)
42 die("unknown value for %s: %s", git_test_k
, git_test_v
);
49 enum protocol_version
determine_protocol_version_server(void)
51 const char *git_protocol
= getenv(GIT_PROTOCOL_ENVIRONMENT
);
52 enum protocol_version version
= protocol_v0
;
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.
62 struct string_list list
= STRING_LIST_INIT_DUP
;
63 const struct string_list_item
*item
;
64 string_list_split(&list
, git_protocol
, ':', -1);
66 for_each_string_list_item(item
, &list
) {
68 enum protocol_version v
;
70 if (skip_prefix(item
->string
, "version=", &value
)) {
71 v
= parse_protocol_version(value
);
77 string_list_clear(&list
, 0);
80 trace2_data_intmax("transfer", NULL
, "negotiated-version", version
);
85 enum protocol_version
determine_protocol_version_client(const char *server_response
)
87 enum protocol_version version
= protocol_v0
;
89 if (skip_prefix(server_response
, "version ", &server_response
)) {
90 version
= parse_protocol_version(server_response
);
92 if (version
== protocol_unknown_version
)
93 die("server is speaking an unknown protocol");
94 if (version
== protocol_v0
)
95 die("protocol error: server explicitly said version 0");