submodule: fix NULL correctness in renamed broken submodules
[gitweb.git] / transport-helper.c
index c948d5215c22fbbb3e174e219017935741e9b2d5..1f8ff7e9424e5d780578a6ab00623f814d8f836a 100644 (file)
@@ -11,6 +11,9 @@
 #include "sigchain.h"
 #include "argv-array.h"
 #include "refs.h"
+#include "refspec.h"
+#include "transport-internal.h"
+#include "protocol.h"
 
 static int debug;
 
@@ -25,6 +28,7 @@ struct helper_data {
                option : 1,
                push : 1,
                connect : 1,
+               stateless_connect : 1,
                signed_tags : 1,
                check_connectivity : 1,
                no_disconnect_req : 1,
@@ -32,8 +36,7 @@ struct helper_data {
        char *export_marks;
        char *import_marks;
        /* These go from remote name (as in "list") to private name */
-       struct refspec *refspecs;
-       int refspec_nr;
+       struct refspec rs;
        /* Transport options for fetch-pack/send-pack (should one of
         * those be invoked).
         */
@@ -48,7 +51,7 @@ static void sendline(struct helper_data *helper, struct strbuf *buffer)
                die_errno("Full write to remote helper failed");
 }
 
-static int recvline_fh(FILE *helper, struct strbuf *buffer, const char *name)
+static int recvline_fh(FILE *helper, struct strbuf *buffer)
 {
        strbuf_reset(buffer);
        if (debug)
@@ -66,7 +69,7 @@ static int recvline_fh(FILE *helper, struct strbuf *buffer, const char *name)
 
 static int recvline(struct helper_data *helper, struct strbuf *buffer)
 {
-       return recvline_fh(helper->out, buffer, helper->name);
+       return recvline_fh(helper->out, buffer);
 }
 
 static void write_constant(int fd, const char *str)
@@ -103,9 +106,6 @@ static struct child_process *get_helper(struct transport *transport)
        struct helper_data *data = transport->data;
        struct strbuf buf = STRBUF_INIT;
        struct child_process *helper;
-       const char **refspecs = NULL;
-       int refspec_nr = 0;
-       int refspec_alloc = 0;
        int duped;
        int code;
 
@@ -135,6 +135,7 @@ static struct child_process *get_helper(struct transport *transport)
 
        data->helper = helper;
        data->no_disconnect_req = 0;
+       refspec_init(&data->rs, REFSPEC_FETCH);
 
        /*
         * Open the output as FILE* so strbuf_getline_*() family of
@@ -180,13 +181,12 @@ static struct child_process *get_helper(struct transport *transport)
                        data->export = 1;
                else if (!strcmp(capname, "check-connectivity"))
                        data->check_connectivity = 1;
-               else if (!data->refspecs && skip_prefix(capname, "refspec ", &arg)) {
-                       ALLOC_GROW(refspecs,
-                                  refspec_nr + 1,
-                                  refspec_alloc);
-                       refspecs[refspec_nr++] = xstrdup(arg);
+               else if (skip_prefix(capname, "refspec ", &arg)) {
+                       refspec_append(&data->rs, arg);
                } else if (!strcmp(capname, "connect")) {
                        data->connect = 1;
+               } else if (!strcmp(capname, "stateless-connect")) {
+                       data->stateless_connect = 1;
                } else if (!strcmp(capname, "signed-tags")) {
                        data->signed_tags = 1;
                } else if (skip_prefix(capname, "export-marks ", &arg)) {
@@ -201,14 +201,7 @@ static struct child_process *get_helper(struct transport *transport)
                            capname);
                }
        }
-       if (refspecs) {
-               int i;
-               data->refspec_nr = refspec_nr;
-               data->refspecs = parse_fetch_refspec(refspec_nr, refspecs);
-               for (i = 0; i < refspec_nr; i++)
-                       free((char *)refspecs[i]);
-               free(refspecs);
-       } else if (data->import || data->bidi_import || data->export) {
+       if (!data->rs.nr && (data->import || data->bidi_import || data->export)) {
                warning("This remote helper should implement refspec capability.");
        }
        strbuf_release(&buf);
@@ -372,8 +365,7 @@ static int release_helper(struct transport *transport)
 {
        int res = 0;
        struct helper_data *data = transport->data;
-       free_refspec(data->refspec_nr, data->refspecs);
-       data->refspecs = NULL;
+       refspec_clear(&data->rs);
        res = disconnect_helper(transport);
        free(transport->data);
        return res;
@@ -530,12 +522,12 @@ static int fetch_with_import(struct transport *transport,
                if (posn->status & REF_STATUS_UPTODATE)
                        continue;
                name = posn->symref ? posn->symref : posn->name;
-               if (data->refspecs)
-                       private = apply_refspecs(data->refspecs, data->refspec_nr, name);
+               if (data->rs.nr)
+                       private = apply_refspecs(&data->rs, name);
                else
                        private = xstrdup(name);
                if (private) {
-                       if (read_ref(private, posn->old_oid.hash) < 0)
+                       if (read_ref(private, &posn->old_oid) < 0)
                                die("Could not read ref %s", private);
                        free(private);
                }
@@ -544,14 +536,13 @@ static int fetch_with_import(struct transport *transport,
        return 0;
 }
 
-static int process_connect_service(struct transport *transport,
-                                  const char *name, const char *exec)
+static int run_connect(struct transport *transport, struct strbuf *cmdbuf)
 {
        struct helper_data *data = transport->data;
-       struct strbuf cmdbuf = STRBUF_INIT;
-       struct child_process *helper;
-       int r, duped, ret = 0;
+       int ret = 0;
+       int duped;
        FILE *input;
+       struct child_process *helper;
 
        helper = get_helper(transport);
 
@@ -567,44 +558,61 @@ static int process_connect_service(struct transport *transport,
        input = xfdopen(duped, "r");
        setvbuf(input, NULL, _IONBF, 0);
 
+       sendline(data, cmdbuf);
+       if (recvline_fh(input, cmdbuf))
+               exit(128);
+
+       if (!strcmp(cmdbuf->buf, "")) {
+               data->no_disconnect_req = 1;
+               if (debug)
+                       fprintf(stderr, "Debug: Smart transport connection "
+                               "ready.\n");
+               ret = 1;
+       } else if (!strcmp(cmdbuf->buf, "fallback")) {
+               if (debug)
+                       fprintf(stderr, "Debug: Falling back to dumb "
+                               "transport.\n");
+       } else {
+               die("Unknown response to connect: %s",
+                       cmdbuf->buf);
+       }
+
+       fclose(input);
+       return ret;
+}
+
+static int process_connect_service(struct transport *transport,
+                                  const char *name, const char *exec)
+{
+       struct helper_data *data = transport->data;
+       struct strbuf cmdbuf = STRBUF_INIT;
+       int ret = 0;
+
        /*
         * Handle --upload-pack and friends. This is fire and forget...
         * just warn if it fails.
         */
        if (strcmp(name, exec)) {
-               r = set_helper_option(transport, "servpath", exec);
+               int r = set_helper_option(transport, "servpath", exec);
                if (r > 0)
                        warning("Setting remote service path not supported by protocol.");
                else if (r < 0)
                        warning("Invalid remote service path.");
        }
 
-       if (data->connect)
+       if (data->connect) {
                strbuf_addf(&cmdbuf, "connect %s\n", name);
-       else
-               goto exit;
-
-       sendline(data, &cmdbuf);
-       if (recvline_fh(input, &cmdbuf, name))
-               exit(128);
-
-       if (!strcmp(cmdbuf.buf, "")) {
-               data->no_disconnect_req = 1;
-               if (debug)
-                       fprintf(stderr, "Debug: Smart transport connection "
-                               "ready.\n");
-               ret = 1;
-       } else if (!strcmp(cmdbuf.buf, "fallback")) {
-               if (debug)
-                       fprintf(stderr, "Debug: Falling back to dumb "
-                               "transport.\n");
-       } else
-               die("Unknown response to connect: %s",
-                       cmdbuf.buf);
+               ret = run_connect(transport, &cmdbuf);
+       } else if (data->stateless_connect &&
+                  (get_protocol_version_config() == protocol_v2) &&
+                  !strcmp("git-upload-pack", name)) {
+               strbuf_addf(&cmdbuf, "stateless-connect %s\n", name);
+               ret = run_connect(transport, &cmdbuf);
+               if (ret)
+                       transport->stateless_rpc = 1;
+       }
 
-exit:
        strbuf_release(&cmdbuf);
-       fclose(input);
        return ret;
 }
 
@@ -650,7 +658,7 @@ static int fetch(struct transport *transport,
 
        if (process_connect(transport, 0)) {
                do_take_over(transport);
-               return transport->fetch(transport, nr_heads, to_fetch);
+               return transport->vtable->fetch(transport, nr_heads, to_fetch);
        }
 
        count = 0;
@@ -671,6 +679,11 @@ static int fetch(struct transport *transport,
        if (data->transport_options.update_shallow)
                set_helper_option(transport, "update-shallow", "true");
 
+       if (data->transport_options.filter_options.choice)
+               set_helper_option(
+                       transport, "filter",
+                       data->transport_options.filter_options.filter_spec);
+
        if (data->fetch)
                return fetch_with_fetch(transport, nr_heads, to_fetch);
 
@@ -788,14 +801,15 @@ static int push_update_refs_status(struct helper_data *data,
                if (push_update_ref_status(&buf, &ref, remote_refs))
                        continue;
 
-               if (flags & TRANSPORT_PUSH_DRY_RUN || !data->refspecs || data->no_private_update)
+               if (flags & TRANSPORT_PUSH_DRY_RUN || !data->rs.nr || data->no_private_update)
                        continue;
 
                /* propagate back the update to the remote namespace */
-               private = apply_refspecs(data->refspecs, data->refspec_nr, ref->name);
+               private = apply_refspecs(&data->rs, ref->name);
                if (!private)
                        continue;
-               update_ref("update by helper", private, ref->new_oid.hash, NULL, 0, 0);
+               update_ref("update by helper", private, &ref->new_oid, NULL,
+                          0, 0);
                free(private);
        }
        strbuf_release(&buf);
@@ -881,7 +895,8 @@ static int push_refs_with_push(struct transport *transport,
                        struct strbuf cas = STRBUF_INIT;
                        strbuf_addf(&cas, "%s:%s",
                                    ref->name, oid_to_hex(&ref->old_oid_expect));
-                       string_list_append(&cas_options, strbuf_detach(&cas, NULL));
+                       string_list_append_nodup(&cas_options,
+                                                strbuf_detach(&cas, NULL));
                }
        }
        if (buf.len == 0) {
@@ -896,6 +911,7 @@ static int push_refs_with_push(struct transport *transport,
        strbuf_addch(&buf, '\n');
        sendline(data, &buf);
        strbuf_release(&buf);
+       string_list_clear(&cas_options, 0);
 
        return push_update_refs_status(data, remote_refs, flags);
 }
@@ -909,7 +925,7 @@ static int push_refs_with_export(struct transport *transport,
        struct string_list revlist_args = STRING_LIST_INIT_DUP;
        struct strbuf buf = STRBUF_INIT;
 
-       if (!data->refspecs)
+       if (!data->rs.nr)
                die("remote-helper doesn't support push; refspec needed");
 
        set_common_push_options(transport, data->name, flags);
@@ -926,10 +942,11 @@ static int push_refs_with_export(struct transport *transport,
                char *private;
                struct object_id oid;
 
-               private = apply_refspecs(data->refspecs, data->refspec_nr, ref->name);
+               private = apply_refspecs(&data->rs, ref->name);
                if (private && !get_oid(private, &oid)) {
                        strbuf_addf(&buf, "^%s", private);
-                       string_list_append(&revlist_args, strbuf_detach(&buf, NULL));
+                       string_list_append_nodup(&revlist_args,
+                                                strbuf_detach(&buf, NULL));
                        oidcpy(&ref->old_oid, &oid);
                }
                free(private);
@@ -941,10 +958,9 @@ static int push_refs_with_export(struct transport *transport,
                                        int flag;
 
                                        /* Follow symbolic refs (mainly for HEAD). */
-                                       name = resolve_ref_unsafe(
-                                                ref->peer_ref->name,
-                                                RESOLVE_REF_READING,
-                                                oid.hash, &flag);
+                                       name = resolve_ref_unsafe(ref->peer_ref->name,
+                                                                 RESOLVE_REF_READING,
+                                                                 &oid, &flag);
                                        if (!name || !(flag & REF_ISSYMREF))
                                                name = ref->peer_ref->name;
 
@@ -987,7 +1003,7 @@ static int push_refs(struct transport *transport,
 
        if (process_connect(transport, 1)) {
                do_take_over(transport);
-               return transport->push_refs(transport, remote_refs, flags);
+               return transport->vtable->push_refs(transport, remote_refs, flags);
        }
 
        if (!remote_refs) {
@@ -1022,7 +1038,8 @@ static int has_attribute(const char *attrs, const char *attr) {
        }
 }
 
-static struct ref *get_refs_list(struct transport *transport, int for_push)
+static struct ref *get_refs_list(struct transport *transport, int for_push,
+                                const struct argv_array *ref_prefixes)
 {
        struct helper_data *data = transport->data;
        struct child_process *helper;
@@ -1035,7 +1052,7 @@ static struct ref *get_refs_list(struct transport *transport, int for_push)
 
        if (process_connect(transport, for_push)) {
                do_take_over(transport);
-               return transport->get_refs_list(transport, for_push);
+               return transport->vtable->get_refs_list(transport, for_push, ref_prefixes);
        }
 
        if (data->push && for_push)
@@ -1066,8 +1083,7 @@ static struct ref *get_refs_list(struct transport *transport, int for_push)
                if (eon) {
                        if (has_attribute(eon + 1, "unchanged")) {
                                (*tail)->status |= REF_STATUS_UPTODATE;
-                               if (read_ref((*tail)->name,
-                                            (*tail)->old_oid.hash) < 0)
+                               if (read_ref((*tail)->name, &(*tail)->old_oid) < 0)
                                        die(_("Could not read ref %s"),
                                            (*tail)->name);
                        }
@@ -1084,6 +1100,15 @@ static struct ref *get_refs_list(struct transport *transport, int for_push)
        return ret;
 }
 
+static struct transport_vtable vtable = {
+       set_helper_option,
+       get_refs_list,
+       fetch,
+       push_refs,
+       connect_helper,
+       release_helper
+};
+
 int transport_helper_init(struct transport *transport, const char *name)
 {
        struct helper_data *data = xcalloc(1, sizeof(*data));
@@ -1095,12 +1120,7 @@ int transport_helper_init(struct transport *transport, const char *name)
                debug = 1;
 
        transport->data = data;
-       transport->set_option = set_helper_option;
-       transport->get_refs_list = get_refs_list;
-       transport->fetch = fetch;
-       transport->push_refs = push_refs;
-       transport->disconnect = release_helper;
-       transport->connect = connect_helper;
+       transport->vtable = &vtable;
        transport->smart_options = &(data->transport_options);
        return 0;
 }