Code

i18n: add infrastructure for translating Git with gettext
[git.git] / http-push.c
index ff41a0e183dd3a449f5136c6d1488a31b4a8960b..f856299254fb7d2cf120c423fdb25748357ef926 100644 (file)
@@ -82,8 +82,7 @@ static int helper_status;
 
 static struct object_list *objects;
 
-struct repo
-{
+struct repo {
        char *url;
        char *path;
        int path_len;
@@ -108,8 +107,7 @@ enum transfer_state {
        COMPLETE
 };
 
-struct transfer_request
-{
+struct transfer_request {
        struct object *obj;
        char *url;
        char *dest;
@@ -127,8 +125,7 @@ struct transfer_request
 
 static struct transfer_request *request_queue_head;
 
-struct xml_ctx
-{
+struct xml_ctx {
        char *name;
        int len;
        char *cdata;
@@ -136,8 +133,7 @@ struct xml_ctx
        void *userData;
 };
 
-struct remote_lock
-{
+struct remote_lock {
        char *url;
        char *owner;
        char *token;
@@ -156,8 +152,7 @@ struct remote_lock
 /* Flags that remote_ls passes to callback functions */
 #define IS_DIR (1u << 0)
 
-struct remote_ls_ctx
-{
+struct remote_ls_ctx {
        char *path;
        void (*userFunc)(struct remote_ls_ctx *ls);
        void *userData;
@@ -174,7 +169,7 @@ enum dav_header_flag {
        DAV_HEADER_TIMEOUT = (1u << 2)
 };
 
-static char *xml_entities(char *s)
+static char *xml_entities(const char *s)
 {
        struct strbuf buf = STRBUF_INIT;
        while (*s) {
@@ -202,6 +197,34 @@ static char *xml_entities(char *s)
        return strbuf_detach(&buf, NULL);
 }
 
+static void curl_setup_http_get(CURL *curl, const char *url,
+               const char *custom_req)
+{
+       curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
+       curl_easy_setopt(curl, CURLOPT_URL, url);
+       curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, custom_req);
+       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_null);
+}
+
+static void curl_setup_http(CURL *curl, const char *url,
+               const char *custom_req, struct buffer *buffer,
+               curl_write_callback write_fn)
+{
+       curl_easy_setopt(curl, CURLOPT_PUT, 1);
+       curl_easy_setopt(curl, CURLOPT_URL, url);
+       curl_easy_setopt(curl, CURLOPT_INFILE, buffer);
+       curl_easy_setopt(curl, CURLOPT_INFILESIZE, buffer->buf.len);
+       curl_easy_setopt(curl, CURLOPT_READFUNCTION, fread_buffer);
+#ifndef NO_CURL_IOCTL
+       curl_easy_setopt(curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
+       curl_easy_setopt(curl, CURLOPT_IOCTLDATA, &buffer);
+#endif
+       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_fn);
+       curl_easy_setopt(curl, CURLOPT_NOBODY, 0);
+       curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, custom_req);
+       curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
+}
+
 static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum dav_header_flag options)
 {
        struct strbuf buf = STRBUF_INIT;
@@ -277,11 +300,8 @@ static void start_mkcol(struct transfer_request *request)
        slot = get_active_slot();
        slot->callback_func = process_response;
        slot->callback_data = request;
-       curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
-       curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
+       curl_setup_http_get(slot->curl, request->url, DAV_MKCOL);
        curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
-       curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
-       curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
 
        if (start_active_slot(slot)) {
                request->slot = slot;
@@ -357,15 +377,15 @@ static void start_put(struct transfer_request *request)
        unsigned long len;
        int hdrlen;
        ssize_t size;
-       z_stream stream;
+       git_zstream stream;
 
        unpacked = read_sha1_file(request->obj->sha1, &type, &len);
        hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
 
        /* Set it up */
        memset(&stream, 0, sizeof(stream));
-       deflateInit(&stream, zlib_compression_level);
-       size = deflateBound(&stream, len + hdrlen);
+       git_deflate_init(&stream, zlib_compression_level);
+       size = git_deflate_bound(&stream, len + hdrlen);
        strbuf_init(&request->buffer.buf, size);
        request->buffer.posn = 0;
 
@@ -376,15 +396,15 @@ static void start_put(struct transfer_request *request)
        /* First header.. */
        stream.next_in = (void *)hdr;
        stream.avail_in = hdrlen;
-       while (deflate(&stream, 0) == Z_OK)
-               /* nothing */;
+       while (git_deflate(&stream, 0) == Z_OK)
+               ; /* nothing */
 
        /* Then the data itself.. */
        stream.next_in = unpacked;
        stream.avail_in = len;
-       while (deflate(&stream, Z_FINISH) == Z_OK)
-               /* nothing */;
-       deflateEnd(&stream);
+       while (git_deflate(&stream, Z_FINISH) == Z_OK)
+               ; /* nothing */
+       git_deflate_end(&stream);
        free(unpacked);
 
        request->buffer.buf.len = stream.total_out;
@@ -400,19 +420,8 @@ static void start_put(struct transfer_request *request)
        slot = get_active_slot();
        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.buf.len);
-       curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
-#ifndef NO_CURL_IOCTL
-       curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
-       curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &request->buffer);
-#endif
-       curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
-       curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
-       curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
-       curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
-       curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
-       curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
+       curl_setup_http(slot->curl, request->url, DAV_PUT,
+                       &request->buffer, fwrite_null);
 
        if (start_active_slot(slot)) {
                request->slot = slot;
@@ -432,13 +441,10 @@ static void start_move(struct transfer_request *request)
        slot = get_active_slot();
        slot->callback_func = process_response;
        slot->callback_data = request;
-       curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
-       curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MOVE);
+       curl_setup_http_get(slot->curl, request->url, DAV_MOVE);
        dav_headers = curl_slist_append(dav_headers, request->dest);
        dav_headers = curl_slist_append(dav_headers, "Overwrite: T");
        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
-       curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
-       curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
 
        if (start_active_slot(slot)) {
                request->slot = slot;
@@ -463,10 +469,7 @@ static int refresh_lock(struct remote_lock *lock)
 
        slot = get_active_slot();
        slot->results = &results;
-       curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
-       curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
-       curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
-       curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
+       curl_setup_http_get(slot->curl, lock->url, DAV_LOCK);
        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
 
        if (start_active_slot(slot)) {
@@ -802,7 +805,7 @@ static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
        }
 }
 
-static void one_remote_ref(char *refname);
+static void one_remote_ref(const char *refname);
 
 static void
 xml_start_tag(void *userData, const char *name, const char **atts)
@@ -881,10 +884,7 @@ static struct remote_lock *lock_remote(const char *path, long timeout)
                ep[1] = '\0';
                slot = get_active_slot();
                slot->results = &results;
-               curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
-               curl_easy_setopt(slot->curl, CURLOPT_URL, url);
-               curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
-               curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
+               curl_setup_http_get(slot->curl, url, DAV_MKCOL);
                if (start_active_slot(slot)) {
                        run_active_slot(slot);
                        if (results.curl_result != CURLE_OK &&
@@ -914,19 +914,9 @@ 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.buf.len);
-       curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
-#ifndef NO_CURL_IOCTL
-       curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
-       curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
-#endif
-       curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
-       curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
-       curl_easy_setopt(slot->curl, CURLOPT_URL, url);
-       curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
-       curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
+       curl_setup_http(slot->curl, url, DAV_LOCK, &out_buffer, fwrite_buffer);
        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
+       curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
 
        lock = xcalloc(1, sizeof(*lock));
        lock->timeout = -1;
@@ -992,9 +982,7 @@ static int unlock_remote(struct remote_lock *lock)
 
        slot = get_active_slot();
        slot->results = &results;
-       curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
-       curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
-       curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_UNLOCK);
+       curl_setup_http_get(slot->curl, lock->url, DAV_UNLOCK);
        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
 
        if (start_active_slot(slot)) {
@@ -1172,19 +1160,10 @@ 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.buf.len);
-       curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
-#ifndef NO_CURL_IOCTL
-       curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
-       curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
-#endif
-       curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
-       curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
-       curl_easy_setopt(slot->curl, CURLOPT_URL, url);
-       curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
-       curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
+       curl_setup_http(slot->curl, url, DAV_PROPFIND,
+                       &out_buffer, fwrite_buffer);
        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
+       curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
 
        if (start_active_slot(slot)) {
                run_active_slot(slot);
@@ -1255,19 +1234,10 @@ 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.buf.len);
-       curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
-#ifndef NO_CURL_IOCTL
-       curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
-       curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
-#endif
-       curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
-       curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
-       curl_easy_setopt(slot->curl, CURLOPT_URL, repo->url);
-       curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
-       curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
+       curl_setup_http(slot->curl, repo->url, DAV_PROPFIND,
+                       &out_buffer, fwrite_buffer);
        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
+       curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
 
        if (start_active_slot(slot)) {
                run_active_slot(slot);
@@ -1441,19 +1411,9 @@ static int update_remote(unsigned char *sha1, struct remote_lock *lock)
 
        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.buf.len);
-       curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
-#ifndef NO_CURL_IOCTL
-       curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
-       curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
-#endif
-       curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
-       curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
+       curl_setup_http(slot->curl, lock->url, DAV_PUT,
+                       &out_buffer, fwrite_null);
        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
-       curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
-       curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
-       curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
 
        if (start_active_slot(slot)) {
                run_active_slot(slot);
@@ -1476,7 +1436,7 @@ static int update_remote(unsigned char *sha1, struct remote_lock *lock)
 
 static struct ref *remote_refs;
 
-static void one_remote_ref(char *refname)
+static void one_remote_ref(const char *refname)
 {
        struct ref *ref;
        struct object *obj;
@@ -1577,19 +1537,9 @@ 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.buf.len);
-               curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
-#ifndef NO_CURL_IOCTL
-               curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
-               curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &buffer);
-#endif
-               curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
-               curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
+               curl_setup_http(slot->curl, lock->url, DAV_PUT,
+                               &buffer, fwrite_null);
                curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
-               curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
-               curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
-               curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
 
                if (start_active_slot(slot)) {
                        run_active_slot(slot);
@@ -1656,16 +1606,16 @@ static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
        strbuf_release(&buffer);
 }
 
-static int verify_merge_base(unsigned char *head_sha1, unsigned char *branch_sha1)
+static int verify_merge_base(unsigned char *head_sha1, struct ref *remote)
 {
-       struct commit *head = lookup_commit(head_sha1);
-       struct commit *branch = lookup_commit(branch_sha1);
+       struct commit *head = lookup_commit_or_die(head_sha1, "HEAD");
+       struct commit *branch = lookup_commit_or_die(remote->old_sha1, remote->name);
        struct commit_list *merge_bases = get_merge_bases(head, branch, 1);
 
        return (merge_bases && !merge_bases->next && merge_bases->item == branch);
 }
 
-static int delete_remote_branch(char *pattern, int force)
+static int delete_remote_branch(const char *pattern, int force)
 {
        struct ref *refs = remote_refs;
        struct ref *remote_ref = NULL;
@@ -1705,7 +1655,7 @@ static int delete_remote_branch(char *pattern, int force)
                return error("Remote HEAD is not a symref");
 
        /* Remote branch must not be the remote HEAD */
-       for (i=0; symref && i<MAXDEPTH; i++) {
+       for (i = 0; symref && i < MAXDEPTH; i++) {
                if (!strcmp(remote_ref->name, symref))
                        return error("Remote branch %s is the current HEAD",
                                     remote_ref->name);
@@ -1730,7 +1680,7 @@ static int delete_remote_branch(char *pattern, int force)
                        return error("Remote branch %s resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", remote_ref->name, sha1_to_hex(remote_ref->old_sha1));
 
                /* Remote branch must be an ancestor of remote HEAD */
-               if (!verify_merge_base(head_sha1, remote_ref->old_sha1)) {
+               if (!verify_merge_base(head_sha1, remote_ref)) {
                        return error("The branch '%s' is not an ancestor "
                                     "of your current HEAD.\n"
                                     "If you are sure you want to delete it,"
@@ -1747,10 +1697,7 @@ static int delete_remote_branch(char *pattern, int force)
        sprintf(url, "%s%s", repo->url, remote_ref->name);
        slot = get_active_slot();
        slot->results = &results;
-       curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
-       curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
-       curl_easy_setopt(slot->curl, CURLOPT_URL, url);
-       curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_DELETE);
+       curl_setup_http_get(slot->curl, url, DAV_DELETE);
        if (start_active_slot(slot)) {
                run_active_slot(slot);
                free(url);
@@ -1800,7 +1747,8 @@ int main(int argc, char **argv)
        int i;
        int new_refs;
        struct ref *ref, *local_refs;
-       struct remote *remote;
+
+       git_setup_gettext();
 
        git_extract_argv0_path(argv[0]);
 
@@ -1874,14 +1822,7 @@ int main(int argc, char **argv)
 
        memset(remote_dir_exists, -1, 256);
 
-       /*
-        * Create a minimum remote by hand to give to http_init(),
-        * primarily to allow it to look at the URL.
-        */
-       remote = xcalloc(sizeof(*remote), 1);
-       ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
-       remote->url[remote->url_nr++] = repo->url;
-       http_init(remote);
+       http_init(NULL, repo->url);
 
 #ifdef USE_CURL_MULTI
        is_running_queue = 0;
@@ -1930,8 +1871,8 @@ int main(int argc, char **argv)
        }
 
        /* match them up */
-       if (match_refs(local_refs, &remote_refs,
-                      nr_refspec, (const char **) refspec, push_all)) {
+       if (match_push_refs(local_refs, &remote_refs,
+                           nr_refspec, (const char **) refspec, push_all)) {
                rc = -1;
                goto cleanup;
        }