Add method for transport to call when parsing primeclone output from
stdin. Suppress stderr when using git_connect with ssh, unless output
is verbose.

Signed-off-by: Kevin Wern <kevin.m.w...@gmail.com>
---
 connect.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 connect.h | 10 ++++++----
 2 files changed, 53 insertions(+), 4 deletions(-)

diff --git a/connect.c b/connect.c
index 0478631..284de53 100644
--- a/connect.c
+++ b/connect.c
@@ -804,6 +804,10 @@ struct child_process *git_connect(int fd[2], const char 
*url,
                }
                argv_array_push(&conn->args, cmd.buf);
 
+               if (flags & CONNECT_SUPPRESS_ERRORS) {
+                       conn->no_stderr = 1;
+               }
+
                if (start_command(conn))
                        die("unable to fork");
 
@@ -831,3 +835,46 @@ int finish_connect(struct child_process *conn)
        free(conn);
        return code;
 }
+
+const struct alt_resource *const get_alt_res_connect(int fd, int flags)
+{
+       struct alt_resource *res = NULL;
+       const char *line;
+       char *url = NULL, *filetype = NULL;
+       char *error_string = NULL;
+
+       while (line = packet_read_line_gentle(fd, NULL)) {
+               const char *space = strchr(line, ' ');
+
+               // We will eventually support multiple resources, so always
+               // parse the whole message
+               if ((filetype && url) || error_string) {
+                       continue;
+               }
+               if (skip_prefix(line, "ERR ", &line) || !space ||
+                               strchr(space + 1, ' ')) {
+                       error_string = xstrdup(line);
+                       continue;
+               }
+               filetype = xstrndup(line, (space - line));
+               url = xstrdup(space + 1);
+       }
+
+       if (filetype && url && !error_string){
+               res = xcalloc(1, sizeof(*res));
+               res->filetype = filetype;
+               res->url = url;
+       }
+       else {
+               if (!(flags & CONNECT_SUPPRESS_ERRORS)) {
+                       if (error_string)
+                               fprintf(stderr, "prime clone protocol error: "
+                                       "got '%s'\n", error_string);
+                       else
+                               fprintf(stderr, "did not get required "
+                                       "components for alternate resource\n");
+               }
+       }
+
+       return res;
+}
diff --git a/connect.h b/connect.h
index 01f14cd..966c0eb 100644
--- a/connect.h
+++ b/connect.h
@@ -1,10 +1,11 @@
 #ifndef CONNECT_H
 #define CONNECT_H
 
-#define CONNECT_VERBOSE       (1u << 0)
-#define CONNECT_DIAG_URL      (1u << 1)
-#define CONNECT_IPV4          (1u << 2)
-#define CONNECT_IPV6          (1u << 3)
+#define CONNECT_VERBOSE         (1u << 0)
+#define CONNECT_DIAG_URL        (1u << 1)
+#define CONNECT_IPV4            (1u << 2)
+#define CONNECT_IPV6            (1u << 3)
+#define CONNECT_SUPPRESS_ERRORS (1u << 4)
 extern struct child_process *git_connect(int fd[2], const char *url, const 
char *prog, int flags);
 extern int finish_connect(struct child_process *conn);
 extern int git_connection_is_socket(struct child_process *conn);
@@ -12,5 +13,6 @@ extern int server_supports(const char *feature);
 extern int parse_feature_request(const char *features, 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);
+const struct alt_resource *const get_alt_res_connect(int fd, int flags);
 
 #endif
-- 
2.7.4

Reply via email to