]> git.ipfire.org Git - thirdparty/git.git/blobdiff - connect.c
remote.h: replace struct extra_have_objects with struct sha1_array
[thirdparty/git.git] / connect.c
index a0783d4867c5e6a9496e11ed043f666a402c5db9..48eec414f7e25ffc704b8a394077a3b60c2a1b61 100644 (file)
--- a/connect.c
+++ b/connect.c
@@ -5,9 +5,13 @@
 #include "refs.h"
 #include "run-command.h"
 #include "remote.h"
+#include "connect.h"
 #include "url.h"
+#include "string-list.h"
+#include "sha1-array.h"
 
 static char *server_capabilities;
+static const char *parse_feature_value(const char *, const char *, int *);
 
 static int check_ref(const char *name, int len, unsigned int flags)
 {
@@ -42,13 +46,6 @@ int check_ref_type(const struct ref *ref, int flags)
        return check_ref(ref->name, strlen(ref->name), flags);
 }
 
-static void add_extra_have(struct extra_have_objects *extra, unsigned char *sha1)
-{
-       ALLOC_GROW(extra->array, extra->nr + 1, extra->alloc);
-       hashcpy(&(extra->array[extra->nr][0]), sha1);
-       extra->nr++;
-}
-
 static void die_initial_contact(int got_at_least_one_head)
 {
        if (got_at_least_one_head)
@@ -59,13 +56,69 @@ static void die_initial_contact(int got_at_least_one_head)
                    "and the repository exists.");
 }
 
+static void parse_one_symref_info(struct string_list *symref, const char *val, int len)
+{
+       char *sym, *target;
+       struct string_list_item *item;
+
+       if (!len)
+               return; /* just "symref" */
+       /* e.g. "symref=HEAD:refs/heads/master" */
+       sym = xmalloc(len + 1);
+       memcpy(sym, val, len);
+       sym[len] = '\0';
+       target = strchr(sym, ':');
+       if (!target)
+               /* just "symref=something" */
+               goto reject;
+       *(target++) = '\0';
+       if (check_refname_format(sym, REFNAME_ALLOW_ONELEVEL) ||
+           check_refname_format(target, REFNAME_ALLOW_ONELEVEL))
+               /* "symref=bogus:pair */
+               goto reject;
+       item = string_list_append(symref, sym);
+       item->util = target;
+       return;
+reject:
+       free(sym);
+       return;
+}
+
+static void annotate_refs_with_symref_info(struct ref *ref)
+{
+       struct string_list symref = STRING_LIST_INIT_DUP;
+       const char *feature_list = server_capabilities;
+
+       while (feature_list) {
+               int len;
+               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;
+       }
+       sort_string_list(&symref);
+
+       for (; ref; ref = ref->next) {
+               struct string_list_item *item;
+               item = string_list_lookup(&symref, ref->name);
+               if (!item)
+                       continue;
+               ref->symref = xstrdup((char *)item->util);
+       }
+       string_list_clear(&symref, 0);
+}
+
 /*
  * Read all the refs from the other end
  */
 struct ref **get_remote_heads(int in, char *src_buf, size_t src_len,
                              struct ref **list, unsigned int flags,
-                             struct extra_have_objects *extra_have)
+                             struct sha1_array *extra_have)
 {
+       struct ref **orig_list = list;
        int got_at_least_one_head = 0;
 
        *list = NULL;
@@ -101,7 +154,7 @@ struct ref **get_remote_heads(int in, char *src_buf, size_t src_len,
 
                if (extra_have &&
                    name_len == 5 && !memcmp(".have", name, 5)) {
-                       add_extra_have(extra_have, old_sha1);
+                       sha1_array_append(extra_have, old_sha1);
                        continue;
                }
 
@@ -113,10 +166,13 @@ struct ref **get_remote_heads(int in, char *src_buf, size_t src_len,
                list = &ref->next;
                got_at_least_one_head = 1;
        }
+
+       annotate_refs_with_symref_info(*orig_list);
+
        return list;
 }
 
-const char *parse_feature_value(const char *feature_list, const char *feature, int *lenp)
+static const char *parse_feature_value(const char *feature_list, const char *feature, int *lenp)
 {
        int len;
 
@@ -551,7 +607,7 @@ struct child_process *git_connect(int fd[2], const char *url_orig,
        path = strchr(end, c);
        if (path && !has_dos_drive_prefix(end)) {
                if (c == ':') {
-                       if (path < strchrnul(host, '/')) {
+                       if (host != url || path < strchrnul(host, '/')) {
                                protocol = PROTO_SSH;
                                *path++ = '\0';
                        } else /* '/' in the host part, assume local path */