Merge http://www.kernel.org/pub/scm/gitk/gitk
[gitweb.git] / http-push.c
index 85cd595802ca565f380a9851efd5a3551c959db0..8866189332568a317183f6f895014519515f67e7 100644 (file)
@@ -7,7 +7,7 @@
 
 #include <curl/curl.h>
 #include <curl/easy.h>
-#include "expat.h"
+#include <expat.h>
 
 static const char http_push_usage[] =
 "git-http-push [--complete] [--force] [--verbose] <url> <ref> [<ref>...]\n";
@@ -28,6 +28,15 @@ static const char http_push_usage[] =
 #define NO_CURL_EASY_DUPHANDLE
 #endif
 
+#ifndef XML_STATUS_OK
+enum XML_Status {
+  XML_STATUS_OK = 1,
+  XML_STATUS_ERROR = 0
+};
+#define XML_STATUS_OK    1
+#define XML_STATUS_ERROR 0
+#endif
+
 #define RANGE_HEADER_SIZE 30
 
 /* DAV method names and request body templates */
@@ -40,10 +49,14 @@ static const char http_push_usage[] =
 #define PROPFIND_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:prop xmlns:R=\"%s\">\n<D:supportedlock/>\n</D:prop>\n</D:propfind>"
 #define LOCK_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:lockinfo xmlns:D=\"DAV:\">\n<D:lockscope><D:exclusive/></D:lockscope>\n<D:locktype><D:write/></D:locktype>\n<D:owner>\n<D:href>mailto:%s</D:href>\n</D:owner>\n</D:lockinfo>"
 
+#define LOCK_TIME 600
+#define LOCK_REFRESH 30
+
 static int active_requests = 0;
 static int data_received;
 static int pushing = 0;
 static int aborted = 0;
+static char remote_dir_exists[256];
 
 #ifdef USE_CURL_MULTI
 static int max_requests = -1;
@@ -143,10 +156,12 @@ struct active_lock
        int ctx_timeout;
        int ctx_locktoken;
        int ctx_locktoken_href;
+       char *url;
        char *owner;
+       char *token;
        time_t start_time;
        long timeout;
-       char *token;
+       int refreshing;
 };
 
 struct lockprop
@@ -580,17 +595,72 @@ static void start_move(struct transfer_request *request)
        }
 }
 
+static int refresh_lock(struct active_lock *lock)
+{
+       struct active_request_slot *slot;
+       char *if_header;
+       char timeout_header[25];
+       struct curl_slist *dav_headers = NULL;
+       int rc = 0;
+
+       lock->refreshing = 1;
+
+       if_header = xmalloc(strlen(lock->token) + 25);
+       sprintf(if_header, "If: (<opaquelocktoken:%s>)", lock->token);
+       sprintf(timeout_header, "Timeout: Second-%ld", lock->timeout);
+       dav_headers = curl_slist_append(dav_headers, if_header);
+       dav_headers = curl_slist_append(dav_headers, timeout_header);
+
+       slot = get_active_slot();
+       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_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
+
+       if (start_active_slot(slot)) {
+               run_active_slot(slot);
+               if (slot->curl_result != CURLE_OK) {
+                       fprintf(stderr, "Got HTTP error %ld\n", slot->http_code);
+               } else {
+                       lock->start_time = time(NULL);
+                       rc = 1;
+               }
+       }
+
+       lock->refreshing = 0;
+       curl_slist_free_all(dav_headers);
+       free(if_header);
+
+       return rc;
+}
+
 static void finish_request(struct transfer_request *request)
 {
+       time_t current_time = time(NULL);
+       int time_remaining;
+
        request->curl_result =  request->slot->curl_result;
        request->http_code = request->slot->http_code;
        request->slot = NULL;
+
+       /* Refresh the lock if it is close to timing out */
+       time_remaining = request->lock->start_time + request->lock->timeout
+               - current_time;
+       if (time_remaining < LOCK_REFRESH && !request->lock->refreshing) {
+               if (!refresh_lock(request->lock)) {
+                       fprintf(stderr, "Unable to refresh remote lock\n");
+                       aborted = 1;
+               }
+       }
+
        if (request->headers != NULL)
                curl_slist_free_all(request->headers);
        if (request->state == RUN_HEAD) {
                if (request->http_code == 404) {
                        request->state = NEED_PUSH;
                } else if (request->curl_result == CURLE_OK) {
+                       remote_dir_exists[request->sha1[0]] = 1;
                        request->state = COMPLETE;
                } else {
                        fprintf(stderr, "HEAD %s failed, aborting (%d/%ld)\n",
@@ -602,6 +672,7 @@ static void finish_request(struct transfer_request *request)
        } else if (request->state == RUN_MKCOL) {
                if (request->curl_result == CURLE_OK ||
                    request->http_code == 405) {
+                       remote_dir_exists[request->sha1[0]] = 1;
                        start_put(request);
                } else {
                        fprintf(stderr, "MKCOL %s failed, aborting (%d/%ld)\n",
@@ -655,7 +726,7 @@ static void release_request(struct transfer_request *request)
 }
 
 #ifdef USE_CURL_MULTI
-void process_curl_messages(void)
+static void process_curl_messages(void)
 {
        int num_messages;
        struct active_request_slot *slot;
@@ -669,11 +740,12 @@ void process_curl_messages(void)
                               slot->curl != curl_message->easy_handle)
                                slot = slot->next;
                        if (slot != NULL) {
+                               int curl_result = curl_message->data.result;
                                curl_multi_remove_handle(curlm, slot->curl);
                                active_requests--;
                                slot->done = 1;
                                slot->in_use = 0;
-                               slot->curl_result = curl_message->data.result;
+                               slot->curl_result = curl_result;
                                curl_easy_getinfo(slot->curl,
                                                  CURLINFO_HTTP_CODE,
                                                  &slot->http_code);
@@ -694,7 +766,7 @@ void process_curl_messages(void)
        }
 }
 
-void process_request_queue(void)
+static void process_request_queue(void)
 {
        struct transfer_request *request = request_queue_head;
        struct active_request_slot *slot = active_queue_head;
@@ -708,7 +780,10 @@ void process_request_queue(void)
                        start_check(request);
                        curl_multi_perform(curlm, &num_transfers);
                } else if (pushing && request->state == NEED_PUSH) {
-                       start_mkcol(request);
+                       if (remote_dir_exists[request->sha1[0]])
+                               start_put(request);
+                       else
+                               start_mkcol(request);
                        curl_multi_perform(curlm, &num_transfers);
                }
                request = request->next;
@@ -724,7 +799,7 @@ void process_request_queue(void)
 }
 #endif
 
-void process_waiting_requests(void)
+static void process_waiting_requests(void)
 {
        struct active_request_slot *slot = active_queue_head;
 
@@ -737,7 +812,7 @@ void process_waiting_requests(void)
                }
 }
 
-void add_request(unsigned char *sha1, struct active_lock *lock)
+static void add_request(unsigned char *sha1, struct active_lock *lock)
 {
        struct transfer_request *request = request_queue_head;
        struct packed_git *target;
@@ -864,7 +939,7 @@ static int setup_index(unsigned char *sha1)
        return 0;
 }
 
-static int fetch_indices()
+static int fetch_indices(void)
 {
        unsigned char sha1[20];
        char *url;
@@ -1114,7 +1189,7 @@ end_lockprop_element(void *userData, const char *name)
        }
 }
 
-struct active_lock *lock_remote(char *file, int timeout)
+static struct active_lock *lock_remote(char *file, long timeout)
 {
        struct active_request_slot *slot;
        struct buffer out_buffer;
@@ -1171,12 +1246,13 @@ struct active_lock *lock_remote(char *file, int timeout)
        in_buffer.posn = 0;
        in_buffer.buffer = in_data;
 
-       new_lock = xmalloc(sizeof(*new_lock));
+       new_lock = xcalloc(1, sizeof(*new_lock));
        new_lock->owner = NULL;
        new_lock->token = NULL;
        new_lock->timeout = -1;
+       new_lock->refreshing = 0;
 
-       sprintf(timeout_header, "Timeout: Second-%d", timeout);
+       sprintf(timeout_header, "Timeout: Second-%ld", timeout);
        dav_headers = curl_slist_append(dav_headers, timeout_header);
        dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
 
@@ -1211,7 +1287,6 @@ struct active_lock *lock_remote(char *file, int timeout)
                return NULL;
        }
 
-       free(url);
        free(out_data);
 
        XML_SetUserData(parser, new_lock);
@@ -1223,6 +1298,7 @@ struct active_lock *lock_remote(char *file, int timeout)
        if (result != XML_STATUS_OK) {
                fprintf(stderr, "%s", XML_ErrorString(
                                XML_GetErrorCode(parser)));
+               free(url);
                free(new_lock);
                return NULL;
        }
@@ -1232,18 +1308,19 @@ struct active_lock *lock_remote(char *file, int timeout)
                        free(new_lock->token);
                if (new_lock->owner != NULL)
                        free(new_lock->owner);
+               free(url);
                free(new_lock);
                return NULL;
        }
 
+       new_lock->url = url;
        new_lock->start_time = time(NULL);
        return new_lock;
 }
 
-int unlock_remote(char *file, struct active_lock *lock)
+static int unlock_remote(struct active_lock *lock)
 {
        struct active_request_slot *slot;
-       char *url;
        char *lock_token_header;
        struct curl_slist *dav_headers = NULL;
        int rc = 0;
@@ -1251,13 +1328,11 @@ int unlock_remote(char *file, struct active_lock *lock)
        lock_token_header = xmalloc(strlen(lock->token) + 31);
        sprintf(lock_token_header, "Lock-Token: <opaquelocktoken:%s>",
                lock->token);
-       url = xmalloc(strlen(remote->url) + strlen(file) + 1);
-       sprintf(url, "%s%s", remote->url, file);
        dav_headers = curl_slist_append(dav_headers, lock_token_header);
 
        slot = get_active_slot();
        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
-       curl_easy_setopt(slot->curl, CURLOPT_URL, url);
+       curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
        curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_UNLOCK);
        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
 
@@ -1274,12 +1349,17 @@ int unlock_remote(char *file, struct active_lock *lock)
 
        curl_slist_free_all(dav_headers);
        free(lock_token_header);
-       free(url);
+
+       if (lock->owner != NULL)
+               free(lock->owner);
+       free(lock->url);
+       free(lock->token);
+       free(lock);
 
        return rc;
 }
 
-int check_locking()
+static int check_locking(void)
 {
        struct active_request_slot *slot;
        struct buffer in_buffer;
@@ -1345,7 +1425,7 @@ int check_locking()
                return 1;
 }
 
-int is_ancestor(unsigned char *sha1, struct commit *commit)
+static int is_ancestor(unsigned char *sha1, struct commit *commit)
 {
        struct commit_list *parents;
 
@@ -1366,8 +1446,8 @@ int is_ancestor(unsigned char *sha1, struct commit *commit)
        return 0;
 }
 
-void get_delta(unsigned char *sha1, struct object *obj,
-              struct active_lock *lock)
+static void get_delta(unsigned char *sha1, struct object *obj,
+                     struct active_lock *lock)
 {
        struct commit *commit;
        struct commit_list *parents;
@@ -1423,20 +1503,15 @@ void get_delta(unsigned char *sha1, struct object *obj,
        }
 }
 
-int update_remote(char *remote_path, unsigned char *sha1,
-                 struct active_lock *lock)
+static int update_remote(unsigned char *sha1, struct active_lock *lock)
 {
        struct active_request_slot *slot;
-       char *url;
        char *out_data;
        char *if_header;
        struct buffer out_buffer;
        struct curl_slist *dav_headers = NULL;
        int i;
 
-       url = xmalloc(strlen(remote->url) + strlen(remote_path) + 1);
-       sprintf(url, "%s%s", remote->url, remote_path);
-
        if_header = xmalloc(strlen(lock->token) + 25);
        sprintf(if_header, "If: (<opaquelocktoken:%s>)", lock->token);
        dav_headers = curl_slist_append(dav_headers, if_header);
@@ -1460,13 +1535,12 @@ int update_remote(char *remote_path, unsigned char *sha1,
        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, url);
+       curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
 
        if (start_active_slot(slot)) {
                run_active_slot(slot);
                free(out_data);
                free(if_header);
-               free(url);
                if (slot->curl_result != CURLE_OK) {
                        fprintf(stderr,
                                "PUT error: curl result=%d, HTTP code=%ld\n",
@@ -1477,7 +1551,6 @@ int update_remote(char *remote_path, unsigned char *sha1,
        } else {
                free(out_data);
                free(if_header);
-               free(url);
                fprintf(stderr, "Unable to start PUT request\n");
                return 0;
        }
@@ -1542,6 +1615,8 @@ int main(int argc, char **argv)
                break;
        }
 
+       memset(remote_dir_exists, 0, 256);
+
        curl_global_init(CURL_GLOBAL_ALL);
 
 #ifdef USE_CURL_MULTI
@@ -1629,7 +1704,7 @@ int main(int argc, char **argv)
                        free(remote_path);
                remote_path = xmalloc(strlen(remote_ref) + 12);
                sprintf(remote_path, "refs/heads/%s", remote_ref);
-               remote_lock = lock_remote(remote_path, 3600);
+               remote_lock = lock_remote(remote_path, LOCK_TIME);
                if (remote_lock == NULL) {
                        fprintf(stderr, "Unable to lock remote branch %s\n",
                                remote_ref);
@@ -1701,8 +1776,7 @@ int main(int argc, char **argv)
 
                /* Update the remote branch if all went well */
                if (do_remote_update) {
-                       if (!aborted && update_remote(remote_path,
-                                                     local_sha1,
+                       if (!aborted && update_remote(local_sha1,
                                                      remote_lock)) {
                                fprintf(stderr, "%s remote branch %s\n",
                                        new_branch ? "Created" : "Updated",
@@ -1718,12 +1792,8 @@ int main(int argc, char **argv)
                }
 
        unlock:
-               unlock_remote(remote_path, remote_lock);
+               unlock_remote(remote_lock);
                free(remote_path);
-               if (remote_lock->owner != NULL)
-                       free(remote_lock->owner);
-               free(remote_lock->token);
-               free(remote_lock);
        }
 
  cleanup: