1#include"cache.h" 2#include"config.h" 3#include"protocol.h" 4 5static enum protocol_version parse_protocol_version(const char*value) 6{ 7if(!strcmp(value,"0")) 8return protocol_v0; 9else if(!strcmp(value,"1")) 10return protocol_v1; 11else 12return protocol_unknown_version; 13} 14 15enum protocol_version get_protocol_version_config(void) 16{ 17const char*value; 18if(!git_config_get_string_const("protocol.version", &value)) { 19enum protocol_version version =parse_protocol_version(value); 20 21if(version == protocol_unknown_version) 22die("unknown value for config 'protocol.version':%s", 23 value); 24 25return version; 26} 27 28return protocol_v0; 29} 30 31enum protocol_version determine_protocol_version_server(void) 32{ 33const char*git_protocol =getenv(GIT_PROTOCOL_ENVIRONMENT); 34enum protocol_version version = protocol_v0; 35 36/* 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. 42 */ 43if(git_protocol) { 44struct string_list list = STRING_LIST_INIT_DUP; 45const struct string_list_item *item; 46string_list_split(&list, git_protocol,':', -1); 47 48for_each_string_list_item(item, &list) { 49const char*value; 50enum protocol_version v; 51 52if(skip_prefix(item->string,"version=", &value)) { 53 v =parse_protocol_version(value); 54if(v > version) 55 version = v; 56} 57} 58 59string_list_clear(&list,0); 60} 61 62return version; 63} 64 65enum protocol_version determine_protocol_version_client(const char*server_response) 66{ 67enum protocol_version version = protocol_v0; 68 69if(skip_prefix(server_response,"version ", &server_response)) { 70 version =parse_protocol_version(server_response); 71 72if(version == protocol_unknown_version) 73die("server is speaking an unknown protocol"); 74if(version == protocol_v0) 75die("protocol error: server explicitly said version 0"); 76} 77 78return version; 79}