connect: in ref advertisement, shallows are last
Currently, get_remote_heads() parses the ref advertisement in one loop, allowing refs and shallow lines to intersperse, despite this not being allowed by the specification. Refactor get_remote_heads() to use two loops instead, enforcing that refs come first, and then shallows. This also makes it easier to teach get_remote_heads() to interpret other lines in the ref advertisement, which will be done in a subsequent patch. As part of this change, this patch interprets capabilities only on the first line in the ref advertisement, printing a warning message when encountering capabilities on other lines. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>maint
							parent
							
								
									7451fcdc0d
								
							
						
					
					
						commit
						0cd83283df
					
				
							
								
								
									
										189
									
								
								connect.c
								
								
								
								
							
							
						
						
									
										189
									
								
								connect.c
								
								
								
								
							|  | @ -11,6 +11,7 @@ | ||||||
| #include "string-list.h" | #include "string-list.h" | ||||||
| #include "sha1-array.h" | #include "sha1-array.h" | ||||||
| #include "transport.h" | #include "transport.h" | ||||||
|  | #include "strbuf.h" | ||||||
|  |  | ||||||
| static char *server_capabilities; | static char *server_capabilities; | ||||||
| static const char *parse_feature_value(const char *, const char *, int *); | static const char *parse_feature_value(const char *, const char *, int *); | ||||||
|  | @ -107,6 +108,104 @@ static void annotate_refs_with_symref_info(struct ref *ref) | ||||||
| 	string_list_clear(&symref, 0); | 	string_list_clear(&symref, 0); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | /* | ||||||
|  |  * Read one line of a server's ref advertisement into packet_buffer. | ||||||
|  |  */ | ||||||
|  | static int read_remote_ref(int in, char **src_buf, size_t *src_len, | ||||||
|  | 			   int *responded) | ||||||
|  | { | ||||||
|  | 	int len = packet_read(in, src_buf, src_len, | ||||||
|  | 			      packet_buffer, sizeof(packet_buffer), | ||||||
|  | 			      PACKET_READ_GENTLE_ON_EOF | | ||||||
|  | 			      PACKET_READ_CHOMP_NEWLINE); | ||||||
|  | 	const char *arg; | ||||||
|  | 	if (len < 0) | ||||||
|  | 		die_initial_contact(*responded); | ||||||
|  | 	if (len > 4 && skip_prefix(packet_buffer, "ERR ", &arg)) | ||||||
|  | 		die("remote error: %s", arg); | ||||||
|  |  | ||||||
|  | 	*responded = 1; | ||||||
|  |  | ||||||
|  | 	return len; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | #define EXPECTING_FIRST_REF 0 | ||||||
|  | #define EXPECTING_REF 1 | ||||||
|  | #define EXPECTING_SHALLOW 2 | ||||||
|  |  | ||||||
|  | static void process_capabilities(int *len) | ||||||
|  | { | ||||||
|  | 	int nul_location = strlen(packet_buffer); | ||||||
|  | 	if (nul_location == *len) | ||||||
|  | 		return; | ||||||
|  | 	server_capabilities = xstrdup(packet_buffer + nul_location + 1); | ||||||
|  | 	*len = nul_location; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | static int process_dummy_ref(void) | ||||||
|  | { | ||||||
|  | 	struct object_id oid; | ||||||
|  | 	const char *name; | ||||||
|  |  | ||||||
|  | 	if (parse_oid_hex(packet_buffer, &oid, &name)) | ||||||
|  | 		return 0; | ||||||
|  | 	if (*name != ' ') | ||||||
|  | 		return 0; | ||||||
|  | 	name++; | ||||||
|  |  | ||||||
|  | 	return !oidcmp(&null_oid, &oid) && !strcmp(name, "capabilities^{}"); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | static void check_no_capabilities(int len) | ||||||
|  | { | ||||||
|  | 	if (strlen(packet_buffer) != len) | ||||||
|  | 		warning("Ignoring capabilities after first line '%s'", | ||||||
|  | 			packet_buffer + strlen(packet_buffer)); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | static int process_ref(int len, struct ref ***list, unsigned int flags, | ||||||
|  | 		       struct oid_array *extra_have) | ||||||
|  | { | ||||||
|  | 	struct object_id old_oid; | ||||||
|  | 	const char *name; | ||||||
|  |  | ||||||
|  | 	if (parse_oid_hex(packet_buffer, &old_oid, &name)) | ||||||
|  | 		return 0; | ||||||
|  | 	if (*name != ' ') | ||||||
|  | 		return 0; | ||||||
|  | 	name++; | ||||||
|  |  | ||||||
|  | 	if (extra_have && !strcmp(name, ".have")) { | ||||||
|  | 		oid_array_append(extra_have, &old_oid); | ||||||
|  | 	} else if (!strcmp(name, "capabilities^{}")) { | ||||||
|  | 		die("protocol error: unexpected capabilities^{}"); | ||||||
|  | 	} else if (check_ref(name, flags)) { | ||||||
|  | 		struct ref *ref = alloc_ref(name); | ||||||
|  | 		oidcpy(&ref->old_oid, &old_oid); | ||||||
|  | 		**list = ref; | ||||||
|  | 		*list = &ref->next; | ||||||
|  | 	} | ||||||
|  | 	check_no_capabilities(len); | ||||||
|  | 	return 1; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | static int process_shallow(int len, struct oid_array *shallow_points) | ||||||
|  | { | ||||||
|  | 	const char *arg; | ||||||
|  | 	struct object_id old_oid; | ||||||
|  |  | ||||||
|  | 	if (!skip_prefix(packet_buffer, "shallow ", &arg)) | ||||||
|  | 		return 0; | ||||||
|  |  | ||||||
|  | 	if (get_oid_hex(arg, &old_oid)) | ||||||
|  | 		die("protocol error: expected shallow sha-1, got '%s'", arg); | ||||||
|  | 	if (!shallow_points) | ||||||
|  | 		die("repository on the other end cannot be shallow"); | ||||||
|  | 	oid_array_append(shallow_points, &old_oid); | ||||||
|  | 	check_no_capabilities(len); | ||||||
|  | 	return 1; | ||||||
|  | } | ||||||
|  |  | ||||||
| /* | /* | ||||||
|  * Read all the refs from the other end |  * Read all the refs from the other end | ||||||
|  */ |  */ | ||||||
|  | @ -123,76 +222,34 @@ struct ref **get_remote_heads(int in, char *src_buf, size_t src_len, | ||||||
| 	 * willing to talk to us.  A hang-up before seeing any | 	 * willing to talk to us.  A hang-up before seeing any | ||||||
| 	 * response does not necessarily mean an ACL problem, though. | 	 * response does not necessarily mean an ACL problem, though. | ||||||
| 	 */ | 	 */ | ||||||
| 	int saw_response; | 	int responded = 0; | ||||||
| 	int got_dummy_ref_with_capabilities_declaration = 0; | 	int len; | ||||||
|  | 	int state = EXPECTING_FIRST_REF; | ||||||
|  |  | ||||||
| 	*list = NULL; | 	*list = NULL; | ||||||
| 	for (saw_response = 0; ; saw_response = 1) { |  | ||||||
| 		struct ref *ref; |  | ||||||
| 		struct object_id old_oid; |  | ||||||
| 		char *name; |  | ||||||
| 		int len, name_len; |  | ||||||
| 		char *buffer = packet_buffer; |  | ||||||
| 		const char *arg; |  | ||||||
|  |  | ||||||
| 		len = packet_read(in, &src_buf, &src_len, | 	while ((len = read_remote_ref(in, &src_buf, &src_len, &responded))) { | ||||||
| 				  packet_buffer, sizeof(packet_buffer), | 		switch (state) { | ||||||
| 				  PACKET_READ_GENTLE_ON_EOF | | 		case EXPECTING_FIRST_REF: | ||||||
| 				  PACKET_READ_CHOMP_NEWLINE); | 			process_capabilities(&len); | ||||||
| 		if (len < 0) | 			if (process_dummy_ref()) { | ||||||
| 			die_initial_contact(saw_response); | 				state = EXPECTING_SHALLOW; | ||||||
|  | 				break; | ||||||
| 		if (!len) | 			} | ||||||
| 			break; | 			state = EXPECTING_REF; | ||||||
|  | 			/* fallthrough */ | ||||||
| 		if (len > 4 && skip_prefix(buffer, "ERR ", &arg)) | 		case EXPECTING_REF: | ||||||
| 			die("remote error: %s", arg); | 			if (process_ref(len, &list, flags, extra_have)) | ||||||
|  | 				break; | ||||||
| 		if (len == GIT_SHA1_HEXSZ + strlen("shallow ") && | 			state = EXPECTING_SHALLOW; | ||||||
| 			skip_prefix(buffer, "shallow ", &arg)) { | 			/* fallthrough */ | ||||||
| 			if (get_oid_hex(arg, &old_oid)) | 		case EXPECTING_SHALLOW: | ||||||
| 				die("protocol error: expected shallow sha-1, got '%s'", arg); | 			if (process_shallow(len, shallow_points)) | ||||||
| 			if (!shallow_points) | 				break; | ||||||
| 				die("repository on the other end cannot be shallow"); | 			die("protocol error: unexpected '%s'", packet_buffer); | ||||||
| 			oid_array_append(shallow_points, &old_oid); | 		default: | ||||||
| 			continue; | 			die("unexpected state %d", state); | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		if (len < GIT_SHA1_HEXSZ + 2 || get_oid_hex(buffer, &old_oid) || |  | ||||||
| 			buffer[GIT_SHA1_HEXSZ] != ' ') |  | ||||||
| 			die("protocol error: expected sha/ref, got '%s'", buffer); |  | ||||||
| 		name = buffer + GIT_SHA1_HEXSZ + 1; |  | ||||||
|  |  | ||||||
| 		name_len = strlen(name); |  | ||||||
| 		if (len != name_len + GIT_SHA1_HEXSZ + 1) { |  | ||||||
| 			free(server_capabilities); |  | ||||||
| 			server_capabilities = xstrdup(name + name_len + 1); |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		if (extra_have && !strcmp(name, ".have")) { |  | ||||||
| 			oid_array_append(extra_have, &old_oid); |  | ||||||
| 			continue; |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		if (!strcmp(name, "capabilities^{}")) { |  | ||||||
| 			if (saw_response) |  | ||||||
| 				die("protocol error: unexpected capabilities^{}"); |  | ||||||
| 			if (got_dummy_ref_with_capabilities_declaration) |  | ||||||
| 				die("protocol error: multiple capabilities^{}"); |  | ||||||
| 			got_dummy_ref_with_capabilities_declaration = 1; |  | ||||||
| 			continue; |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		if (!check_ref(name, flags)) |  | ||||||
| 			continue; |  | ||||||
|  |  | ||||||
| 		if (got_dummy_ref_with_capabilities_declaration) |  | ||||||
| 			die("protocol error: unexpected ref after capabilities^{}"); |  | ||||||
|  |  | ||||||
| 		ref = alloc_ref(buffer + GIT_SHA1_HEXSZ + 1); |  | ||||||
| 		oidcpy(&ref->old_oid, &old_oid); |  | ||||||
| 		*list = ref; |  | ||||||
| 		list = &ref->next; |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	annotate_refs_with_symref_info(*orig_list); | 	annotate_refs_with_symref_info(*orig_list); | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	 Jonathan Tan
						Jonathan Tan