Re: [PATCH v5] connect: in ref advertisement, shallows are last

2017-09-26 Thread Brandon Williams
On 09/26, Jonathan Tan wrote:
> 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 
> ---
> Changes in v5:
>  - print warning when encountering capabilities on other lines instead
>of ignoring them (also updated commit message)
>  - explicitly disallow refs of name "capabilities^{}" (except when it is
>the only ref)
> ---
>  connect.c | 183 
> +++---
>  1 file changed, 117 insertions(+), 66 deletions(-)
> 
> diff --git a/connect.c b/connect.c
> index df56c0cbf..df65a3fc4 100644
> --- a/connect.c
> +++ b/connect.c
> @@ -11,6 +11,7 @@
>  #include "string-list.h"
>  #include "sha1-array.h"
>  #include "transport.h"
> +#include "strbuf.h"
>  
>  static char *server_capabilities;
>  static const char *parse_feature_value(const char *, const char *, int *);
> @@ -107,6 +108,98 @@ static void annotate_refs_with_symref_info(struct ref 
> *ref)
>   string_list_clear(, 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 ", ))
> + 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)
> +{
> + static char *template;
> + if (!template)
> + template = xstrfmt("%040d capabilities^{}", 0);

My only complaint is still here, I don't like the notion of hardcoding
the 0's.  Its much more future proof and less error prone to call
parse_oid_hex and require that it matches null_oid.

> + return !strcmp(packet_buffer, template);
> +}
> +
> +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, _oid, ))
> + return 0;
> + if (*name != ' ')
> + return 0;
> + name++;
> +
> + if (extra_have && !strcmp(name, ".have")) {
> + oid_array_append(extra_have, _oid);
> + } else if (!strcmp(name, "capabilities^{}")) {
> + die("protocol error: unexpected capabilities^{}");
> + } else if (check_ref(name, flags)) {
> + struct ref *ref = alloc_ref(name);
> + oidcpy(>old_oid, _oid);
> + **list = ref;
> + *list = >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 ", ))
> + return 0;
> +
> + if (get_oid_hex(arg, _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, _oid);
> + check_no_capabilities(len);
> + return 1;
> +}
> +
>  /*
>   * Read all the refs from the other end
>   */
> @@ -123,76 +216,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
>* response does not necessarily mean an ACL problem, though.
>*/
> - 

[PATCH v5] connect: in ref advertisement, shallows are last

2017-09-26 Thread Jonathan Tan
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 
---
Changes in v5:
 - print warning when encountering capabilities on other lines instead
   of ignoring them (also updated commit message)
 - explicitly disallow refs of name "capabilities^{}" (except when it is
   the only ref)
---
 connect.c | 183 +++---
 1 file changed, 117 insertions(+), 66 deletions(-)

diff --git a/connect.c b/connect.c
index df56c0cbf..df65a3fc4 100644
--- a/connect.c
+++ b/connect.c
@@ -11,6 +11,7 @@
 #include "string-list.h"
 #include "sha1-array.h"
 #include "transport.h"
+#include "strbuf.h"
 
 static char *server_capabilities;
 static const char *parse_feature_value(const char *, const char *, int *);
@@ -107,6 +108,98 @@ static void annotate_refs_with_symref_info(struct ref *ref)
string_list_clear(, 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 ", ))
+   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)
+{
+   static char *template;
+   if (!template)
+   template = xstrfmt("%040d capabilities^{}", 0);
+   return !strcmp(packet_buffer, template);
+}
+
+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, _oid, ))
+   return 0;
+   if (*name != ' ')
+   return 0;
+   name++;
+
+   if (extra_have && !strcmp(name, ".have")) {
+   oid_array_append(extra_have, _oid);
+   } else if (!strcmp(name, "capabilities^{}")) {
+   die("protocol error: unexpected capabilities^{}");
+   } else if (check_ref(name, flags)) {
+   struct ref *ref = alloc_ref(name);
+   oidcpy(>old_oid, _oid);
+   **list = ref;
+   *list = >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 ", ))
+   return 0;
+
+   if (get_oid_hex(arg, _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, _oid);
+   check_no_capabilities(len);
+   return 1;
+}
+
 /*
  * Read all the refs from the other end
  */
@@ -123,76 +216,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
 * response does not necessarily mean an ACL problem, though.
 */
-   int saw_response;
-   int got_dummy_ref_with_capabilities_declaration = 0;
+   int responded = 0;
+   int len;
+   int state = EXPECTING_FIRST_REF;
 
*list = NULL;
-   for (saw_response = 0; ; saw_response = 1) {
-   struct ref *ref;
-   struct object_id old_oid;
-   char *name;
-   int len, name_len;
-