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 if(!strcmp(value,"2")) 12return protocol_v2; 13else 14return protocol_unknown_version; 15} 16 17enum protocol_version get_protocol_version_config(void) 18{ 19const char*value; 20if(!git_config_get_string_const("protocol.version", &value)) { 21enum protocol_version version =parse_protocol_version(value); 22 23if(version == protocol_unknown_version) 24die("unknown value for config 'protocol.version':%s", 25 value); 26 27return version; 28} 29 30return protocol_v0; 31} 32 33enum protocol_version determine_protocol_version_server(void) 34{ 35const char*git_protocol =getenv(GIT_PROTOCOL_ENVIRONMENT); 36enum protocol_version version = protocol_v0; 37 38/* 39 * Determine which protocol version the client has requested. Since 40 * multiple 'version' keys can be sent by the client, indicating that 41 * the client is okay to speak any of them, select the greatest version 42 * that the client has requested. This is due to the assumption that 43 * the most recent protocol version will be the most state-of-the-art. 44 */ 45if(git_protocol) { 46struct string_list list = STRING_LIST_INIT_DUP; 47const struct string_list_item *item; 48string_list_split(&list, git_protocol,':', -1); 49 50for_each_string_list_item(item, &list) { 51const char*value; 52enum protocol_version v; 53 54if(skip_prefix(item->string,"version=", &value)) { 55 v =parse_protocol_version(value); 56if(v > version) 57 version = v; 58} 59} 60 61string_list_clear(&list,0); 62} 63 64return version; 65} 66 67enum protocol_version determine_protocol_version_client(const char*server_response) 68{ 69enum protocol_version version = protocol_v0; 70 71if(skip_prefix(server_response,"version ", &server_response)) { 72 version =parse_protocol_version(server_response); 73 74if(version == protocol_unknown_version) 75die("server is speaking an unknown protocol"); 76if(version == protocol_v0) 77die("protocol error: server explicitly said version 0"); 78} 79 80return version; 81}