fetch tests: test --prune and refspec interaction
[alt-git.git] / protocol.c
blob43012b7eb6e18bc48d93e6b96a3f3f57fe8ef79f
1 #include "cache.h"
2 #include "config.h"
3 #include "protocol.h"
5 static enum protocol_version parse_protocol_version(const char *value)
7 if (!strcmp(value, "0"))
8 return protocol_v0;
9 else if (!strcmp(value, "1"))
10 return protocol_v1;
11 else
12 return protocol_unknown_version;
15 enum protocol_version get_protocol_version_config(void)
17 const char *value;
18 if (!git_config_get_string_const("protocol.version", &value)) {
19 enum protocol_version version = parse_protocol_version(value);
21 if (version == protocol_unknown_version)
22 die("unknown value for config 'protocol.version': %s",
23 value);
25 return version;
28 return protocol_v0;
31 enum protocol_version determine_protocol_version_server(void)
33 const char *git_protocol = getenv(GIT_PROTOCOL_ENVIRONMENT);
34 enum protocol_version version = protocol_v0;
37 * Determine which protocol version the client has requested. Since
38 * multiple 'version' keys can be sent by the client, indicating that
39 * the client is okay to speak any of them, select the greatest version
40 * that the client has requested. This is due to the assumption that
41 * the most recent protocol version will be the most state-of-the-art.
43 if (git_protocol) {
44 struct string_list list = STRING_LIST_INIT_DUP;
45 const struct string_list_item *item;
46 string_list_split(&list, git_protocol, ':', -1);
48 for_each_string_list_item(item, &list) {
49 const char *value;
50 enum protocol_version v;
52 if (skip_prefix(item->string, "version=", &value)) {
53 v = parse_protocol_version(value);
54 if (v > version)
55 version = v;
59 string_list_clear(&list, 0);
62 return version;
65 enum protocol_version determine_protocol_version_client(const char *server_response)
67 enum protocol_version version = protocol_v0;
69 if (skip_prefix(server_response, "version ", &server_response)) {
70 version = parse_protocol_version(server_response);
72 if (version == protocol_unknown_version)
73 die("server is speaking an unknown protocol");
74 if (version == protocol_v0)
75 die("protocol error: server explicitly said version 0");
78 return version;