Port to 12 other Platforms.
[gitweb.git] / http-push.c
index c96e43da163fac00b245f2fe5736494185e40aba..665712a85de3fb4bba3a879753b60ece64a4243a 100644 (file)
@@ -9,6 +9,7 @@
 #include "revision.h"
 #include "exec_cmd.h"
 #include "remote.h"
+#include "list-objects.h"
 
 #include <expat.h>
 
@@ -75,7 +76,6 @@ static int aborted;
 static signed char remote_dir_exists[256];
 
 static struct curl_slist *no_pragma_header;
-static struct curl_slist *default_headers;
 
 static int push_verbosely;
 static int push_all = MATCH_REFS_NONE;
@@ -495,10 +495,11 @@ static void start_put(struct transfer_request *request)
        memset(&stream, 0, sizeof(stream));
        deflateInit(&stream, zlib_compression_level);
        size = deflateBound(&stream, len + hdrlen);
-       request->buffer.buffer = xmalloc(size);
+       strbuf_init(&request->buffer.buf, size);
+       request->buffer.posn = 0;
 
        /* Compress it */
-       stream.next_out = request->buffer.buffer;
+       stream.next_out = (unsigned char *)request->buffer.buf.buf;
        stream.avail_out = size;
 
        /* First header.. */
@@ -515,8 +516,7 @@ static void start_put(struct transfer_request *request)
        deflateEnd(&stream);
        free(unpacked);
 
-       request->buffer.size = stream.total_out;
-       request->buffer.posn = 0;
+       request->buffer.buf.len = stream.total_out;
 
        request->url = xmalloc(strlen(remote->url) +
                               strlen(request->lock->token) + 51);
@@ -538,7 +538,7 @@ static void start_put(struct transfer_request *request)
        slot->callback_func = process_response;
        slot->callback_data = request;
        curl_easy_setopt(slot->curl, CURLOPT_INFILE, &request->buffer);
-       curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, request->buffer.size);
+       curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, request->buffer.buf.len);
        curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
        curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
@@ -665,8 +665,7 @@ static void release_request(struct transfer_request *request)
                close(request->local_fileno);
        if (request->local_stream)
                fclose(request->local_stream);
-       if (request->url != NULL)
-               free(request->url);
+       free(request->url);
        free(request);
 }
 
@@ -1008,18 +1007,13 @@ static int fetch_indices(void)
 {
        unsigned char sha1[20];
        char *url;
-       struct buffer buffer;
+       struct strbuf buffer = STRBUF_INIT;
        char *data;
        int i = 0;
 
        struct active_request_slot *slot;
        struct slot_results results;
 
-       data = xcalloc(1, 4096);
-       buffer.size = 4096;
-       buffer.posn = 0;
-       buffer.buffer = data;
-
        if (push_verbosely)
                fprintf(stderr, "Getting pack list\n");
 
@@ -1035,7 +1029,7 @@ static int fetch_indices(void)
        if (start_active_slot(slot)) {
                run_active_slot(slot);
                if (results.curl_result != CURLE_OK) {
-                       free(buffer.buffer);
+                       strbuf_release(&buffer);
                        free(url);
                        if (results.http_code == 404)
                                return 0;
@@ -1043,18 +1037,18 @@ static int fetch_indices(void)
                                return error("%s", curl_errorstr);
                }
        } else {
-               free(buffer.buffer);
+               strbuf_release(&buffer);
                free(url);
                return error("Unable to start request");
        }
        free(url);
 
-       data = buffer.buffer;
-       while (i < buffer.posn) {
+       data = buffer.buf;
+       while (i < buffer.len) {
                switch (data[i]) {
                case 'P':
                        i++;
-                       if (i + 52 < buffer.posn &&
+                       if (i + 52 < buffer.len &&
                            !prefixcmp(data + i, " pack-") &&
                            !prefixcmp(data + i + 46, ".pack\n")) {
                                get_sha1_hex(data + i + 6, sha1);
@@ -1069,92 +1063,10 @@ static int fetch_indices(void)
                i++;
        }
 
-       free(buffer.buffer);
+       strbuf_release(&buffer);
        return 0;
 }
 
-static inline int needs_quote(int ch)
-{
-       if (((ch >= 'A') && (ch <= 'Z'))
-                       || ((ch >= 'a') && (ch <= 'z'))
-                       || ((ch >= '0') && (ch <= '9'))
-                       || (ch == '/')
-                       || (ch == '-')
-                       || (ch == '.'))
-               return 0;
-       return 1;
-}
-
-static inline int hex(int v)
-{
-       if (v < 10) return '0' + v;
-       else return 'A' + v - 10;
-}
-
-static char *quote_ref_url(const char *base, const char *ref)
-{
-       const char *cp;
-       char *dp, *qref;
-       int len, baselen, ch;
-
-       baselen = strlen(base);
-       len = baselen + 1;
-       for (cp = ref; (ch = *cp) != 0; cp++, len++)
-               if (needs_quote(ch))
-                       len += 2; /* extra two hex plus replacement % */
-       qref = xmalloc(len);
-       memcpy(qref, base, baselen);
-       for (cp = ref, dp = qref + baselen; (ch = *cp) != 0; cp++) {
-               if (needs_quote(ch)) {
-                       *dp++ = '%';
-                       *dp++ = hex((ch >> 4) & 0xF);
-                       *dp++ = hex(ch & 0xF);
-               }
-               else
-                       *dp++ = ch;
-       }
-       *dp = 0;
-
-       return qref;
-}
-
-int fetch_ref(char *ref, unsigned char *sha1)
-{
-        char *url;
-        char hex[42];
-        struct buffer buffer;
-       char *base = remote->url;
-       struct active_request_slot *slot;
-       struct slot_results results;
-        buffer.size = 41;
-        buffer.posn = 0;
-        buffer.buffer = hex;
-        hex[41] = '\0';
-
-       url = quote_ref_url(base, ref);
-       slot = get_active_slot();
-       slot->results = &results;
-       curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
-       curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
-       curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
-       curl_easy_setopt(slot->curl, CURLOPT_URL, url);
-       if (start_active_slot(slot)) {
-               run_active_slot(slot);
-               free(url);
-               if (results.curl_result != CURLE_OK)
-                       return error("Couldn't get %s for %s\n%s",
-                                    url, ref, curl_errorstr);
-       } else {
-               free(url);
-               return error("Unable to start request");
-       }
-
-       if (buffer.posn != 41)
-               return 1;
-        hex[40] = '\0';
-       return get_sha1_hex(hex, sha1);
-}
-
 static void one_remote_object(const char *hex)
 {
        unsigned char sha1[20];
@@ -1275,10 +1187,8 @@ static struct remote_lock *lock_remote(const char *path, long timeout)
 {
        struct active_request_slot *slot;
        struct slot_results results;
-       struct buffer out_buffer;
-       struct buffer in_buffer;
-       char *out_data;
-       char *in_data;
+       struct buffer out_buffer = { STRBUF_INIT, 0 };
+       struct strbuf in_buffer = STRBUF_INIT;
        char *url;
        char *ep;
        char timeout_header[25];
@@ -1318,16 +1228,7 @@ static struct remote_lock *lock_remote(const char *path, long timeout)
                ep = strchr(ep + 1, '/');
        }
 
-       out_buffer.size = strlen(LOCK_REQUEST) + strlen(git_default_email) - 2;
-       out_data = xmalloc(out_buffer.size + 1);
-       snprintf(out_data, out_buffer.size + 1, LOCK_REQUEST, git_default_email);
-       out_buffer.posn = 0;
-       out_buffer.buffer = out_data;
-
-       in_buffer.size = 4096;
-       in_data = xmalloc(in_buffer.size);
-       in_buffer.posn = 0;
-       in_buffer.buffer = in_data;
+       strbuf_addf(&out_buffer.buf, LOCK_REQUEST, git_default_email);
 
        sprintf(timeout_header, "Timeout: Second-%ld", timeout);
        dav_headers = curl_slist_append(dav_headers, timeout_header);
@@ -1336,7 +1237,7 @@ static struct remote_lock *lock_remote(const char *path, long timeout)
        slot = get_active_slot();
        slot->results = &results;
        curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
-       curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size);
+       curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
        curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
        curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
@@ -1362,8 +1263,8 @@ static struct remote_lock *lock_remote(const char *path, long timeout)
                        XML_SetElementHandler(parser, xml_start_tag,
                                              xml_end_tag);
                        XML_SetCharacterDataHandler(parser, xml_cdata);
-                       result = XML_Parse(parser, in_buffer.buffer,
-                                          in_buffer.posn, 1);
+                       result = XML_Parse(parser, in_buffer.buf,
+                                          in_buffer.len, 1);
                        free(ctx.name);
                        if (result != XML_STATUS_OK) {
                                fprintf(stderr, "XML error: %s\n",
@@ -1378,14 +1279,12 @@ static struct remote_lock *lock_remote(const char *path, long timeout)
        }
 
        curl_slist_free_all(dav_headers);
-       free(out_data);
-       free(in_data);
+       strbuf_release(&out_buffer.buf);
+       strbuf_release(&in_buffer);
 
        if (lock->token == NULL || lock->timeout <= 0) {
-               if (lock->token != NULL)
-                       free(lock->token);
-               if (lock->owner != NULL)
-                       free(lock->owner);
+               free(lock->token);
+               free(lock->owner);
                free(url);
                free(lock);
                lock = NULL;
@@ -1443,8 +1342,7 @@ static int unlock_remote(struct remote_lock *lock)
                        prev->next = prev->next->next;
        }
 
-       if (lock->owner != NULL)
-               free(lock->owner);
+       free(lock->owner);
        free(lock->url);
        free(lock->token);
        free(lock);
@@ -1452,6 +1350,24 @@ static int unlock_remote(struct remote_lock *lock)
        return rc;
 }
 
+static void remove_locks(void)
+{
+       struct remote_lock *lock = remote->locks;
+
+       fprintf(stderr, "Removing remote locks...\n");
+       while (lock) {
+               unlock_remote(lock);
+               lock = lock->next;
+       }
+}
+
+static void remove_locks_on_signal(int signo)
+{
+       remove_locks();
+       signal(signo, SIG_DFL);
+       raise(signo);
+}
+
 static void remote_ls(const char *path, int flags,
                      void (*userFunc)(struct remote_ls_ctx *ls),
                      void *userData);
@@ -1530,10 +1446,8 @@ static void remote_ls(const char *path, int flags,
        char *url = xmalloc(strlen(remote->url) + strlen(path) + 1);
        struct active_request_slot *slot;
        struct slot_results results;
-       struct buffer in_buffer;
-       struct buffer out_buffer;
-       char *in_data;
-       char *out_data;
+       struct strbuf in_buffer = STRBUF_INIT;
+       struct buffer out_buffer = { STRBUF_INIT, 0 };
        struct curl_slist *dav_headers = NULL;
        struct xml_ctx ctx;
        struct remote_ls_ctx ls;
@@ -1547,16 +1461,7 @@ static void remote_ls(const char *path, int flags,
 
        sprintf(url, "%s%s", remote->url, path);
 
-       out_buffer.size = strlen(PROPFIND_ALL_REQUEST);
-       out_data = xmalloc(out_buffer.size + 1);
-       snprintf(out_data, out_buffer.size + 1, PROPFIND_ALL_REQUEST);
-       out_buffer.posn = 0;
-       out_buffer.buffer = out_data;
-
-       in_buffer.size = 4096;
-       in_data = xmalloc(in_buffer.size);
-       in_buffer.posn = 0;
-       in_buffer.buffer = in_data;
+       strbuf_addf(&out_buffer.buf, PROPFIND_ALL_REQUEST);
 
        dav_headers = curl_slist_append(dav_headers, "Depth: 1");
        dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
@@ -1564,7 +1469,7 @@ static void remote_ls(const char *path, int flags,
        slot = get_active_slot();
        slot->results = &results;
        curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
-       curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size);
+       curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
        curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
        curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
@@ -1587,8 +1492,8 @@ static void remote_ls(const char *path, int flags,
                        XML_SetElementHandler(parser, xml_start_tag,
                                              xml_end_tag);
                        XML_SetCharacterDataHandler(parser, xml_cdata);
-                       result = XML_Parse(parser, in_buffer.buffer,
-                                          in_buffer.posn, 1);
+                       result = XML_Parse(parser, in_buffer.buf,
+                                          in_buffer.len, 1);
                        free(ctx.name);
 
                        if (result != XML_STATUS_OK) {
@@ -1604,8 +1509,8 @@ static void remote_ls(const char *path, int flags,
 
        free(ls.path);
        free(url);
-       free(out_data);
-       free(in_buffer.buffer);
+       strbuf_release(&out_buffer.buf);
+       strbuf_release(&in_buffer);
        curl_slist_free_all(dav_headers);
 }
 
@@ -1626,27 +1531,13 @@ static int locking_available(void)
 {
        struct active_request_slot *slot;
        struct slot_results results;
-       struct buffer in_buffer;
-       struct buffer out_buffer;
-       char *in_data;
-       char *out_data;
+       struct strbuf in_buffer = STRBUF_INIT;
+       struct buffer out_buffer = { STRBUF_INIT, 0 };
        struct curl_slist *dav_headers = NULL;
        struct xml_ctx ctx;
        int lock_flags = 0;
 
-       out_buffer.size =
-               strlen(PROPFIND_SUPPORTEDLOCK_REQUEST) +
-               strlen(remote->url) - 2;
-       out_data = xmalloc(out_buffer.size + 1);
-       snprintf(out_data, out_buffer.size + 1,
-                PROPFIND_SUPPORTEDLOCK_REQUEST, remote->url);
-       out_buffer.posn = 0;
-       out_buffer.buffer = out_data;
-
-       in_buffer.size = 4096;
-       in_data = xmalloc(in_buffer.size);
-       in_buffer.posn = 0;
-       in_buffer.buffer = in_data;
+       strbuf_addf(&out_buffer.buf, PROPFIND_SUPPORTEDLOCK_REQUEST, remote->url);
 
        dav_headers = curl_slist_append(dav_headers, "Depth: 0");
        dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
@@ -1654,7 +1545,7 @@ static int locking_available(void)
        slot = get_active_slot();
        slot->results = &results;
        curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
-       curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size);
+       curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
        curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
        curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
@@ -1676,8 +1567,8 @@ static int locking_available(void)
                        XML_SetUserData(parser, &ctx);
                        XML_SetElementHandler(parser, xml_start_tag,
                                              xml_end_tag);
-                       result = XML_Parse(parser, in_buffer.buffer,
-                                          in_buffer.posn, 1);
+                       result = XML_Parse(parser, in_buffer.buf,
+                                          in_buffer.len, 1);
                        free(ctx.name);
 
                        if (result != XML_STATUS_OK) {
@@ -1687,13 +1578,21 @@ static int locking_available(void)
                                lock_flags = 0;
                        }
                        XML_ParserFree(parser);
+                       if (!lock_flags)
+                               error("Error: no DAV locking support on %s",
+                                     remote->url);
+
+               } else {
+                       error("Cannot access URL %s, return code %d",
+                             remote->url, results.curl_result);
+                       lock_flags = 0;
                }
        } else {
-               fprintf(stderr, "Unable to start PROPFIND request\n");
+               error("Unable to start PROPFIND request on %s", remote->url);
        }
 
-       free(out_data);
-       free(in_buffer.buffer);
+       strbuf_release(&out_buffer.buf);
+       strbuf_release(&in_buffer);
        curl_slist_free_all(dav_headers);
 
        return lock_flags;
@@ -1750,12 +1649,19 @@ static struct object_list **process_tree(struct tree *tree,
 
        init_tree_desc(&desc, tree->buffer, tree->size);
 
-       while (tree_entry(&desc, &entry)) {
-               if (S_ISDIR(entry.mode))
+       while (tree_entry(&desc, &entry))
+               switch (object_type(entry.mode)) {
+               case OBJ_TREE:
                        p = process_tree(lookup_tree(entry.sha1), p, &me, name);
-               else
+                       break;
+               case OBJ_BLOB:
                        p = process_blob(lookup_blob(entry.sha1), p, &me, name);
-       }
+                       break;
+               default:
+                       /* Subproject commit - not in this repository */
+                       break;
+               }
+
        free(tree->buffer);
        tree->buffer = NULL;
        return p;
@@ -1811,30 +1717,20 @@ static int update_remote(unsigned char *sha1, struct remote_lock *lock)
 {
        struct active_request_slot *slot;
        struct slot_results results;
-       char *out_data;
        char *if_header;
-       struct buffer out_buffer;
+       struct buffer out_buffer = { STRBUF_INIT, 0 };
        struct curl_slist *dav_headers = NULL;
-       int i;
 
        if_header = xmalloc(strlen(lock->token) + 25);
        sprintf(if_header, "If: (<opaquelocktoken:%s>)", lock->token);
        dav_headers = curl_slist_append(dav_headers, if_header);
 
-       out_buffer.size = 41;
-       out_data = xmalloc(out_buffer.size + 1);
-       i = snprintf(out_data, out_buffer.size + 1, "%s\n", sha1_to_hex(sha1));
-       if (i != out_buffer.size) {
-               fprintf(stderr, "Unable to initialize PUT request body\n");
-               return 0;
-       }
-       out_buffer.posn = 0;
-       out_buffer.buffer = out_data;
+       strbuf_addf(&out_buffer.buf, "%s\n", sha1_to_hex(sha1));
 
        slot = get_active_slot();
        slot->results = &results;
        curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
-       curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size);
+       curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
        curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
        curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
@@ -1845,7 +1741,7 @@ static int update_remote(unsigned char *sha1, struct remote_lock *lock)
 
        if (start_active_slot(slot)) {
                run_active_slot(slot);
-               free(out_data);
+               strbuf_release(&out_buffer.buf);
                free(if_header);
                if (results.curl_result != CURLE_OK) {
                        fprintf(stderr,
@@ -1855,7 +1751,7 @@ static int update_remote(unsigned char *sha1, struct remote_lock *lock)
                        return 0;
                }
        } else {
-               free(out_data);
+               strbuf_release(&out_buffer.buf);
                free(if_header);
                fprintf(stderr, "Unable to start PUT request\n");
                return 0;
@@ -1882,14 +1778,15 @@ static int one_local_ref(const char *refname, const unsigned char *sha1, int fla
 static void one_remote_ref(char *refname)
 {
        struct ref *ref;
-       unsigned char remote_sha1[20];
        struct object *obj;
-       int len = strlen(refname) + 1;
 
-       if (fetch_ref(refname, remote_sha1) != 0) {
+       ref = alloc_ref_from_str(refname);
+
+       if (http_fetch_ref(remote->url, ref) != 0) {
                fprintf(stderr,
                        "Unable to fetch ref %s from %s\n",
                        refname, remote->url);
+               free(ref);
                return;
        }
 
@@ -1897,18 +1794,15 @@ static void one_remote_ref(char *refname)
         * Fetch a copy of the object if it doesn't exist locally - it
         * may be required for updating server info later.
         */
-       if (remote->can_update_info_refs && !has_sha1_file(remote_sha1)) {
-               obj = lookup_unknown_object(remote_sha1);
+       if (remote->can_update_info_refs && !has_sha1_file(ref->old_sha1)) {
+               obj = lookup_unknown_object(ref->old_sha1);
                if (obj) {
                        fprintf(stderr, "  fetch %s for %s\n",
-                               sha1_to_hex(remote_sha1), refname);
+                               sha1_to_hex(ref->old_sha1), refname);
                        add_fetch_request(obj);
                }
        }
 
-       ref = xcalloc(1, sizeof(*ref) + len);
-       hashcpy(ref->old_sha1, remote_sha1);
-       memcpy(ref->name, refname, len);
        *remote_tail = ref;
        remote_tail = &ref->next;
 }
@@ -1985,60 +1879,39 @@ static int ref_newer(const unsigned char *new_sha1,
        return found;
 }
 
-static void mark_edge_parents_uninteresting(struct commit *commit)
-{
-       struct commit_list *parents;
-
-       for (parents = commit->parents; parents; parents = parents->next) {
-               struct commit *parent = parents->item;
-               if (!(parent->object.flags & UNINTERESTING))
-                       continue;
-               mark_tree_uninteresting(parent->tree);
-       }
-}
-
-static void mark_edges_uninteresting(struct commit_list *list)
-{
-       for ( ; list; list = list->next) {
-               struct commit *commit = list->item;
-
-               if (commit->object.flags & UNINTERESTING) {
-                       mark_tree_uninteresting(commit->tree);
-                       continue;
-               }
-               mark_edge_parents_uninteresting(commit);
-       }
-}
-
 static void add_remote_info_ref(struct remote_ls_ctx *ls)
 {
-       struct buffer *buf = (struct buffer *)ls->userData;
-       unsigned char remote_sha1[20];
+       struct strbuf *buf = (struct strbuf *)ls->userData;
        struct object *o;
        int len;
        char *ref_info;
+       struct ref *ref;
 
-       if (fetch_ref(ls->dentry_name, remote_sha1) != 0) {
+       ref = alloc_ref_from_str(ls->dentry_name);
+
+       if (http_fetch_ref(remote->url, ref) != 0) {
                fprintf(stderr,
                        "Unable to fetch ref %s from %s\n",
                        ls->dentry_name, remote->url);
                aborted = 1;
+               free(ref);
                return;
        }
 
-       o = parse_object(remote_sha1);
+       o = parse_object(ref->old_sha1);
        if (!o) {
                fprintf(stderr,
                        "Unable to parse object %s for remote ref %s\n",
-                       sha1_to_hex(remote_sha1), ls->dentry_name);
+                       sha1_to_hex(ref->old_sha1), ls->dentry_name);
                aborted = 1;
+               free(ref);
                return;
        }
 
        len = strlen(ls->dentry_name) + 42;
        ref_info = xcalloc(len + 1, 1);
        sprintf(ref_info, "%s   %s\n",
-               sha1_to_hex(remote_sha1), ls->dentry_name);
+               sha1_to_hex(ref->old_sha1), ls->dentry_name);
        fwrite_buffer(ref_info, 1, len, buf);
        free(ref_info);
 
@@ -2053,21 +1926,19 @@ static void add_remote_info_ref(struct remote_ls_ctx *ls)
                        free(ref_info);
                }
        }
+       free(ref);
 }
 
 static void update_remote_info_refs(struct remote_lock *lock)
 {
-       struct buffer buffer;
+       struct buffer buffer = { STRBUF_INIT, 0 };
        struct active_request_slot *slot;
        struct slot_results results;
        char *if_header;
        struct curl_slist *dav_headers = NULL;
 
-       buffer.buffer = xcalloc(1, 4096);
-       buffer.size = 4096;
-       buffer.posn = 0;
        remote_ls("refs/", (PROCESS_FILES | RECURSIVE),
-                 add_remote_info_ref, &buffer);
+                 add_remote_info_ref, &buffer.buf);
        if (!aborted) {
                if_header = xmalloc(strlen(lock->token) + 25);
                sprintf(if_header, "If: (<opaquelocktoken:%s>)", lock->token);
@@ -2076,7 +1947,7 @@ static void update_remote_info_refs(struct remote_lock *lock)
                slot = get_active_slot();
                slot->results = &results;
                curl_easy_setopt(slot->curl, CURLOPT_INFILE, &buffer);
-               curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, buffer.posn);
+               curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, buffer.buf.len);
                curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
                curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
                curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
@@ -2085,8 +1956,6 @@ static void update_remote_info_refs(struct remote_lock *lock)
                curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
                curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
 
-               buffer.posn = 0;
-
                if (start_active_slot(slot)) {
                        run_active_slot(slot);
                        if (results.curl_result != CURLE_OK) {
@@ -2097,7 +1966,7 @@ static void update_remote_info_refs(struct remote_lock *lock)
                }
                free(if_header);
        }
-       free(buffer.buffer);
+       strbuf_release(&buffer.buf);
 }
 
 static int remote_exists(const char *path)
@@ -2105,45 +1974,41 @@ static int remote_exists(const char *path)
        char *url = xmalloc(strlen(remote->url) + strlen(path) + 1);
        struct active_request_slot *slot;
        struct slot_results results;
+       int ret = -1;
 
        sprintf(url, "%s%s", remote->url, path);
 
-        slot = get_active_slot();
+       slot = get_active_slot();
        slot->results = &results;
-        curl_easy_setopt(slot->curl, CURLOPT_URL, url);
-        curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
+       curl_easy_setopt(slot->curl, CURLOPT_URL, url);
+       curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
 
-        if (start_active_slot(slot)) {
+       if (start_active_slot(slot)) {
                run_active_slot(slot);
-               free(url);
                if (results.http_code == 404)
-                       return 0;
+                       ret = 0;
                else if (results.curl_result == CURLE_OK)
-                       return 1;
+                       ret = 1;
                else
                        fprintf(stderr, "HEAD HTTP error %ld\n", results.http_code);
        } else {
-               free(url);
                fprintf(stderr, "Unable to start HEAD request\n");
        }
 
-       return -1;
+       free(url);
+       return ret;
 }
 
 static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
 {
        char *url;
-       struct buffer buffer;
+       struct strbuf buffer = STRBUF_INIT;
        struct active_request_slot *slot;
        struct slot_results results;
 
        url = xmalloc(strlen(remote->url) + strlen(path) + 1);
        sprintf(url, "%s%s", remote->url, path);
 
-       buffer.size = 4096;
-       buffer.posn = 0;
-       buffer.buffer = xmalloc(buffer.size);
-
        slot = get_active_slot();
        slot->results = &results;
        curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
@@ -2161,22 +2026,21 @@ static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
        }
        free(url);
 
-       if (*symref != NULL)
-               free(*symref);
+       free(*symref);
        *symref = NULL;
        hashclr(sha1);
 
-       if (buffer.posn == 0)
+       if (buffer.len == 0)
                return;
 
        /* If it's a symref, set the refname; otherwise try for a sha1 */
-       if (!prefixcmp((char *)buffer.buffer, "ref: ")) {
-               *symref = xmemdupz((char *)buffer.buffer + 5, buffer.posn - 6);
+       if (!prefixcmp((char *)buffer.buf, "ref: ")) {
+               *symref = xmemdupz((char *)buffer.buf + 5, buffer.len - 6);
        } else {
-               get_sha1_hex(buffer.buffer, sha1);
+               get_sha1_hex(buffer.buf, sha1);
        }
 
-       free(buffer.buffer);
+       strbuf_release(&buffer);
 }
 
 static int verify_merge_base(unsigned char *head_sha1, unsigned char *branch_sha1)
@@ -2264,6 +2128,8 @@ static int delete_remote_branch(char *pattern, int force)
 
        /* Send delete request */
        fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name);
+       if (dry_run)
+               return 0;
        url = xmalloc(strlen(remote->url) + strlen(remote_ref->name) + 1);
        sprintf(url, "%s%s", remote->url, remote_ref->name);
        slot = get_active_slot();
@@ -2302,6 +2168,7 @@ int main(int argc, char **argv)
        int i;
        int new_refs;
        struct ref *ref;
+       char *rewritten_url = NULL;
 
        setup_git_directory();
 
@@ -2353,6 +2220,10 @@ int main(int argc, char **argv)
                break;
        }
 
+#ifndef USE_CURL_MULTI
+       die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
+#endif
+
        if (!remote->url)
                usage(http_push_usage);
 
@@ -2361,22 +2232,29 @@ int main(int argc, char **argv)
 
        memset(remote_dir_exists, -1, 256);
 
-       http_init();
+       http_init(NULL);
 
        no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
-       default_headers = curl_slist_append(default_headers, "Range:");
-       default_headers = curl_slist_append(default_headers, "Destination:");
-       default_headers = curl_slist_append(default_headers, "If:");
-       default_headers = curl_slist_append(default_headers,
-                                           "Pragma: no-cache");
+
+       if (remote->url && remote->url[strlen(remote->url)-1] != '/') {
+               rewritten_url = malloc(strlen(remote->url)+2);
+               strcpy(rewritten_url, remote->url);
+               strcat(rewritten_url, "/");
+               remote->url = rewritten_url;
+               ++remote->path_len;
+       }
 
        /* Verify DAV compliance/lock support */
        if (!locking_available()) {
-               fprintf(stderr, "Error: no DAV locking support on remote repo %s\n", remote->url);
                rc = 1;
                goto cleanup;
        }
 
+       signal(SIGINT, remove_locks_on_signal);
+       signal(SIGHUP, remove_locks_on_signal);
+       signal(SIGQUIT, remove_locks_on_signal);
+       signal(SIGTERM, remove_locks_on_signal);
+
        /* Check whether the remote has server info files */
        remote->can_update_info_refs = 0;
        remote->has_info_refs = remote_exists("info/refs");
@@ -2385,6 +2263,11 @@ int main(int argc, char **argv)
                info_ref_lock = lock_remote("info/refs", LOCK_TIME);
                if (info_ref_lock)
                        remote->can_update_info_refs = 1;
+               else {
+                       fprintf(stderr, "Error: cannot lock existing info/refs\n");
+                       rc = 1;
+                       goto cleanup;
+               }
        }
        if (remote->has_info_packs)
                fetch_indices();
@@ -2406,11 +2289,14 @@ int main(int argc, char **argv)
        if (!remote_tail)
                remote_tail = &remote_refs;
        if (match_refs(local_refs, remote_refs, &remote_tail,
-                      nr_refspec, (const char **) refspec, push_all))
-               return -1;
+                      nr_refspec, (const char **) refspec, push_all)) {
+               rc = -1;
+               goto cleanup;
+       }
        if (!remote_refs) {
                fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
-               return 0;
+               rc = 0;
+               goto cleanup;
        }
 
        new_refs = 0;
@@ -2422,6 +2308,16 @@ int main(int argc, char **argv)
 
                if (!ref->peer_ref)
                        continue;
+
+               if (is_zero_sha1(ref->peer_ref->new_sha1)) {
+                       if (delete_remote_branch(ref->name, 1) == -1) {
+                               error("Could not remove %s", ref->name);
+                               rc = -4;
+                       }
+                       new_refs++;
+                       continue;
+               }
+
                if (!hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
                        if (push_verbosely || 1)
                                fprintf(stderr, "'%s': up-to-date\n", ref->name);
@@ -2453,11 +2349,6 @@ int main(int argc, char **argv)
                        }
                }
                hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
-               if (is_zero_sha1(ref->new_sha1)) {
-                       error("cannot happen anymore");
-                       rc = -3;
-                       continue;
-               }
                new_refs++;
                strcpy(old_hex, sha1_to_hex(ref->old_sha1));
                new_hex = sha1_to_hex(ref->new_sha1);
@@ -2493,6 +2384,7 @@ int main(int argc, char **argv)
                }
                init_revisions(&revs, setup_git_directory());
                setup_revisions(commit_argc, commit_argv, &revs, NULL);
+               revs.edge_hint = 0; /* just in case */
                free(new_sha1_hex);
                if (old_sha1_hex) {
                        free(old_sha1_hex);
@@ -2501,8 +2393,9 @@ int main(int argc, char **argv)
 
                /* Generate a list of objects that need to be pushed */
                pushing = 0;
-               prepare_revision_walk(&revs);
-               mark_edges_uninteresting(revs.commits);
+               if (prepare_revision_walk(&revs))
+                       die("revision walk setup failed");
+               mark_edges_uninteresting(revs.commits, &revs, NULL);
                objects_to_send = get_delta(&revs, ref_lock);
                finish_all_active_slots();
 
@@ -2516,15 +2409,17 @@ int main(int argc, char **argv)
                fill_active_slots();
                add_fill_function(NULL, fill_active_slot);
 #endif
-               finish_all_active_slots();
+               do {
+                       finish_all_active_slots();
+#ifdef USE_CURL_MULTI
+                       fill_active_slots();
+#endif
+               } while (request_queue_head && !aborted);
 
                /* Update the remote branch if all went well */
-               if (aborted || !update_remote(ref->new_sha1, ref_lock)) {
+               if (aborted || !update_remote(ref->new_sha1, ref_lock))
                        rc = 1;
-                       goto unlock;
-               }
 
-       unlock:
                if (!rc)
                        fprintf(stderr, "    done\n");
                unlock_remote(ref_lock);
@@ -2541,14 +2436,14 @@ int main(int argc, char **argv)
                        fprintf(stderr, "Unable to update server info\n");
                }
        }
-       if (info_ref_lock)
-               unlock_remote(info_ref_lock);
 
  cleanup:
+       free(rewritten_url);
+       if (info_ref_lock)
+               unlock_remote(info_ref_lock);
        free(remote);
 
        curl_slist_free_all(no_pragma_header);
-       curl_slist_free_all(default_headers);
 
        http_cleanup();