Bugfix: stop if directory already exists
[gitweb.git] / http-push.c
index 89fda42efb678d2f9f9e802454fe561b4452a167..8866189332568a317183f6f895014519515f67e7 100644 (file)
@@ -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 */
@@ -47,6 +56,7 @@ 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;
@@ -585,7 +595,7 @@ static void start_move(struct transfer_request *request)
        }
 }
 
-int refresh_lock(struct active_lock *lock)
+static int refresh_lock(struct active_lock *lock)
 {
        struct active_request_slot *slot;
        char *if_header;
@@ -650,6 +660,7 @@ static void finish_request(struct transfer_request *request)
                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",
@@ -661,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",
@@ -714,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;
@@ -728,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);
@@ -753,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;
@@ -767,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;
@@ -783,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;
 
@@ -796,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;
@@ -923,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;
@@ -1173,7 +1189,7 @@ end_lockprop_element(void *userData, const char *name)
        }
 }
 
-struct active_lock *lock_remote(char *file, long timeout)
+static struct active_lock *lock_remote(char *file, long timeout)
 {
        struct active_request_slot *slot;
        struct buffer out_buffer;
@@ -1230,7 +1246,7 @@ struct active_lock *lock_remote(char *file, long 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;
@@ -1302,7 +1318,7 @@ struct active_lock *lock_remote(char *file, long timeout)
        return new_lock;
 }
 
-int unlock_remote(struct active_lock *lock)
+static int unlock_remote(struct active_lock *lock)
 {
        struct active_request_slot *slot;
        char *lock_token_header;
@@ -1343,7 +1359,7 @@ int unlock_remote(struct active_lock *lock)
        return rc;
 }
 
-int check_locking()
+static int check_locking(void)
 {
        struct active_request_slot *slot;
        struct buffer in_buffer;
@@ -1409,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;
 
@@ -1430,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;
@@ -1487,7 +1503,7 @@ void get_delta(unsigned char *sha1, struct object *obj,
        }
 }
 
-int update_remote(unsigned char *sha1, struct active_lock *lock)
+static int update_remote(unsigned char *sha1, struct active_lock *lock)
 {
        struct active_request_slot *slot;
        char *out_data;
@@ -1599,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