git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Stefan Beller <sbeller@google.com>
To: git@vger.kernel.org
Cc: pclouds@gmail.com, gitster@pobox.com, peff@peff.net,
	Stefan Beller <sbeller@google.com>
Subject: [RFCv2 03/16] connect: rewrite feature parsing to work on string_list
Date: Mon,  1 Jun 2015 17:02:05 -0700	[thread overview]
Message-ID: <1433203338-27493-4-git-send-email-sbeller@google.com> (raw)
In-Reply-To: <1433203338-27493-1-git-send-email-sbeller@google.com>

Signed-off-by: Stefan Beller <sbeller@google.com>
---
 builtin/receive-pack.c | 13 +++++----
 connect.c              | 79 ++++++++++++++++++++++----------------------------
 connect.h              |  2 +-
 upload-pack.c          | 11 +++++--
 4 files changed, 51 insertions(+), 54 deletions(-)

diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index d2ec52b..518ce85 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -1391,16 +1391,19 @@ static struct command *read_head_info(struct sha1_array *shallow)
 
 		linelen = strlen(line);
 		if (linelen < len) {
-			const char *feature_list = line + linelen + 1;
-			if (parse_feature_request(feature_list, "report-status"))
+			struct string_list feature_list = STRING_LIST_INIT_DUP;
+			from_space_separated_string(&feature_list, line + linelen + 1);
+			if (parse_feature_request(&feature_list, "report-status"))
 				report_status = 1;
-			if (parse_feature_request(feature_list, "side-band-64k"))
+			if (parse_feature_request(&feature_list, "side-band-64k"))
 				use_sideband = LARGE_PACKET_MAX;
-			if (parse_feature_request(feature_list, "quiet"))
+			if (parse_feature_request(&feature_list, "quiet"))
 				quiet = 1;
 			if (advertise_atomic_push
-			    && parse_feature_request(feature_list, "atomic"))
+			    && parse_feature_request(&feature_list, "atomic"))
 				use_atomic = 1;
+
+			string_list_clear(&feature_list, 1);
 		}
 
 		if (!strcmp(line, "push-cert")) {
diff --git a/connect.c b/connect.c
index c0144d8..4295ba1 100644
--- a/connect.c
+++ b/connect.c
@@ -10,8 +10,8 @@
 #include "string-list.h"
 #include "sha1-array.h"
 
-static char *server_capabilities;
-static const char *parse_feature_value(const char *, const char *, int *);
+struct string_list server_capabilities = STRING_LIST_INIT_DUP;
+static const char *parse_feature_value(struct string_list *, const char *, int *);
 
 static int check_ref(const char *name, unsigned int flags)
 {
@@ -80,18 +80,18 @@ reject:
 
 static void annotate_refs_with_symref_info(struct ref *ref)
 {
+	struct string_list_item *item;
 	struct string_list symref = STRING_LIST_INIT_DUP;
-	const char *feature_list = server_capabilities;
 
-	while (feature_list) {
-		int len;
+	for_each_string_list_item(item, &server_capabilities) {
 		const char *val;
 
-		val = parse_feature_value(feature_list, "symref", &len);
-		if (!val)
-			break;
-		parse_one_symref_info(&symref, val, len);
-		feature_list = val + 1;
+		if (skip_prefix(item->string, "symref", &val)) {
+			if (!val)
+				continue;
+			val++; /* skip the = */
+			parse_one_symref_info(&symref, val, strlen(val));
+		}
 	}
 	string_list_sort(&symref);
 
@@ -153,8 +153,8 @@ struct ref **get_remote_heads(int in, char *src_buf, size_t src_len,
 
 		name_len = strlen(name);
 		if (len != name_len + 41) {
-			free(server_capabilities);
-			server_capabilities = xstrdup(name + name_len + 1);
+			string_list_clear(&server_capabilities, 1);
+			from_space_separated_string(&server_capabilities, name + name_len + 1);
 		}
 
 		if (extra_have && !strcmp(name, ".have")) {
@@ -176,51 +176,40 @@ struct ref **get_remote_heads(int in, char *src_buf, size_t src_len,
 	return list;
 }
 
-static const char *parse_feature_value(const char *feature_list, const char *feature, int *lenp)
+static const char *parse_feature_value(struct string_list *feature_list, const char *feature, int *lenp)
 {
-	int len;
-
-	if (!feature_list)
-		return NULL;
-
-	len = strlen(feature);
-	while (*feature_list) {
-		const char *found = strstr(feature_list, feature);
-		if (!found)
-			return NULL;
-		if (feature_list == found || isspace(found[-1])) {
-			const char *value = found + len;
-			/* feature with no value (e.g., "thin-pack") */
-			if (!*value || isspace(*value)) {
-				if (lenp)
-					*lenp = 0;
-				return value;
-			}
-			/* feature with a value (e.g., "agent=git/1.2.3") */
-			else if (*value == '=') {
-				value++;
-				if (lenp)
-					*lenp = strcspn(value, " \t\n");
-				return value;
-			}
-			/*
-			 * otherwise we matched a substring of another feature;
-			 * keep looking
-			 */
+	const char *value;
+	struct string_list_item *item;
+
+	for_each_string_list_item(item, feature_list) {
+		if (!skip_prefix(item->string, feature, &value))
+			continue;
+
+		/* feature with no value (e.g., "thin-pack") */
+		if (!*value) {
+			if (lenp)
+				*lenp = 0;
+			return value;
+		}
+		/* feature with a value (e.g., "agent=git/1.2.3") */
+		else if (*value == '=') {
+			value++;
+			if (lenp)
+				*lenp = strlen(value);
+			return value;
 		}
-		feature_list = found + 1;
 	}
 	return NULL;
 }
 
-int parse_feature_request(const char *feature_list, const char *feature)
+int parse_feature_request(struct string_list *feature_list, const char *feature)
 {
 	return !!parse_feature_value(feature_list, feature, NULL);
 }
 
 const char *server_feature_value(const char *feature, int *len)
 {
-	return parse_feature_value(server_capabilities, feature, len);
+	return parse_feature_value(&server_capabilities, feature, len);
 }
 
 int server_supports(const char *feature)
diff --git a/connect.h b/connect.h
index c41a685..47492e5 100644
--- a/connect.h
+++ b/connect.h
@@ -7,7 +7,7 @@ extern struct child_process *git_connect(int fd[2], const char *url, const char
 extern int finish_connect(struct child_process *conn);
 extern int git_connection_is_socket(struct child_process *conn);
 extern int server_supports(const char *feature);
-extern int parse_feature_request(const char *features, const char *feature);
+extern int parse_feature_request(struct string_list *, const char *feature);
 extern const char *server_feature_value(const char *feature, int *len_ret);
 extern int url_is_local_not_ssh(const char *url);
 
diff --git a/upload-pack.c b/upload-pack.c
index 5449ff7..2493964 100644
--- a/upload-pack.c
+++ b/upload-pack.c
@@ -531,7 +531,7 @@ error:
 	}
 }
 
-static void parse_features(const char *features)
+static void parse_features(struct string_list *features)
 {
 	if (parse_feature_request(features, "multi_ack_detailed"))
 		multi_ack = 2;
@@ -563,7 +563,10 @@ static void receive_needs(void)
 	for (;;) {
 		struct object *o;
 		unsigned char sha1_buf[20];
-		char *line = packet_read_line(0, NULL);
+		int pkt_len;
+		struct string_list list = STRING_LIST_INIT_DUP;
+		char *line = packet_read_line(0, &pkt_len);
+
 		reset_timeout();
 		if (!line)
 			break;
@@ -596,7 +599,9 @@ static void receive_needs(void)
 			die("git upload-pack: protocol error, "
 			    "expected to get sha, not '%s'", line);
 
-		parse_features(line + 45);
+		from_space_separated_string(&list, xstrdup(line + 45));
+		parse_features(&list);
+		string_list_clear(&list, 1);
 
 		o = parse_object(sha1_buf);
 		if (!o)
-- 
2.4.1.345.gab207b6.dirty

  parent reply	other threads:[~2015-06-02  0:02 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-02  0:02 [RFCv2 00/16] Protocol version 2 Stefan Beller
2015-06-02  0:02 ` [RFCv2 01/16] stringlist: add from_space_separated_string Stefan Beller
2015-06-02  9:42   ` Duy Nguyen
2015-06-02 15:10     ` Eric Sunshine
2015-06-02 17:54       ` Stefan Beller
2015-06-02  0:02 ` [RFCv2 02/16] upload-pack: make client capability parsing code a separate function Stefan Beller
2015-06-02  0:02 ` Stefan Beller [this message]
2015-06-02 18:48   ` [RFCv2 03/16] connect: rewrite feature parsing to work on string_list Junio C Hamano
2015-06-02  0:02 ` [RFCv2 04/16] upload-pack-2: Implement the version 2 of upload-pack Stefan Beller
2015-06-02 18:59   ` Junio C Hamano
2015-06-02 23:08     ` Stefan Beller
2015-06-02  0:02 ` [RFCv2 05/16] remote.h: Change get_remote_heads return to void Stefan Beller
2015-06-02 21:17   ` Junio C Hamano
2015-06-02 21:25     ` Stefan Beller
2015-06-02 21:41       ` Junio C Hamano
2015-06-02  0:02 ` [RFCv2 06/16] remote.h: add new struct for options Stefan Beller
2015-06-02 21:18   ` Junio C Hamano
2015-06-02 21:40     ` Stefan Beller
2015-06-02 21:43       ` Junio C Hamano
2015-06-02  0:02 ` [RFCv2 07/16] transport: add infrastructure to support a protocol version number Stefan Beller
2015-06-02  0:02 ` [RFCv2 08/16] transport: select transport version via command line or config Stefan Beller
2015-06-02  0:02 ` [RFCv2 09/16] remote.h: add get_remote_capabilities, request_capabilities Stefan Beller
2015-06-02 21:24   ` Junio C Hamano
2015-06-02  0:02 ` [RFCv2 10/16] transport: connect_setup appends protocol version number Stefan Beller
2015-06-02  9:58   ` Duy Nguyen
2015-06-02 18:04     ` Stefan Beller
2015-06-02 21:37   ` Junio C Hamano
2015-06-02 22:09     ` Stefan Beller
2015-06-02 22:27       ` Junio C Hamano
2015-06-02  0:02 ` [RFCv2 11/16] remote: have preselect_capabilities Stefan Beller
2015-06-02 21:45   ` Junio C Hamano
2015-06-02  0:02 ` [RFCv2 12/16] transport: get_refs_via_connect exchanges capabilities before refs Stefan Beller
2015-06-02 21:55   ` Junio C Hamano
2015-06-02 22:19     ` Stefan Beller
2015-06-02  0:02 ` [RFCv2 13/16] fetch-pack: use the configured transport protocol Stefan Beller
2015-06-02  9:55   ` Duy Nguyen
2015-06-02 10:02   ` Duy Nguyen
2015-06-02 11:32     ` Ilari Liusvaara
2015-06-02  0:02 ` [RFCv2 14/16] t5544: add a test case for the new protocol Stefan Beller
2015-06-03  0:16   ` Eric Sunshine
2015-06-02  0:02 ` [RFCv2 15/16] Documentation/technical/pack-protocol: Mention http as possible protocol Stefan Beller
2015-06-02 21:57   ` Junio C Hamano
2015-06-02 22:00     ` Junio C Hamano
2015-06-02  0:02 ` [RFCv2 16/16] Document protocol version 2 Stefan Beller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1433203338-27493-4-git-send-email-sbeller@google.com \
    --to=sbeller@google.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=pclouds@gmail.com \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).