git-fetch --tags: deal with tags with spaces in them.
[gitweb.git] / http-fetch.c
index 0b01877b5c1b564b1fe65b7a960a73829a6479d6..0aba8917548b3db9a43ce9802caa1c90176c70c1 100644 (file)
@@ -6,7 +6,10 @@
 #include <curl/curl.h>
 #include <curl/easy.h>
 
+#if LIBCURL_VERSION_NUM >= 0x070908
+#define USE_CURL_MULTI
 #define DEFAULT_MAX_REQUESTS 5
+#endif
 
 #if LIBCURL_VERSION_NUM < 0x070704
 #define curl_global_cleanup() do { /* nothing */ } while(0)
 #define PREV_BUF_SIZE 4096
 #define RANGE_HEADER_SIZE 30
 
-static int max_requests = DEFAULT_MAX_REQUESTS;
 static int active_requests = 0;
 static int data_received;
 
+#ifdef USE_CURL_MULTI
+static int max_requests = DEFAULT_MAX_REQUESTS;
 static CURLM *curlm;
+#endif
 static CURL *curl_default;
+static struct curl_slist *pragma_header;
 static struct curl_slist *no_pragma_header;
 static struct curl_slist *no_range_header;
 static char curl_errorstr[CURL_ERROR_SIZE];
@@ -132,32 +138,17 @@ static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
        return size;
 }
 
-int relink_or_rename(char *old, char *new) {
-       int ret;
-
-       ret = link(old, new);
-       if (ret < 0) {
-               /* Same Coda hack as in write_sha1_file(sha1_file.c) */
-               ret = errno;
-               if (ret == EXDEV && !rename(old, new))
-                       return 0;
-       }
-       unlink(old);
-       if (ret) {
-               if (ret != EEXIST)
-                       return ret;
-       }
-
-       return 0;
-}
-
+#ifdef USE_CURL_MULTI
 void process_curl_messages();
 void process_request_queue();
+#endif
 
 struct active_request_slot *get_active_slot()
 {
        struct active_request_slot *slot = active_queue_head;
        struct active_request_slot *newslot;
+
+#ifdef USE_CURL_MULTI
        int num_transfers;
 
        /* Wait for a slot to open up if the queue is full */
@@ -167,6 +158,7 @@ struct active_request_slot *get_active_slot()
                        process_curl_messages();
                }
        }
+#endif
 
        while (slot != NULL && slot->in_use) {
                slot = slot->next;
@@ -193,7 +185,7 @@ struct active_request_slot *get_active_slot()
        slot->in_use = 1;
        slot->done = 0;
        slot->local = NULL;
-       curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
+       curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_range_header);
        curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
 
@@ -202,6 +194,7 @@ struct active_request_slot *get_active_slot()
 
 int start_active_slot(struct active_request_slot *slot)
 {
+#ifdef USE_CURL_MULTI
        CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
 
        if (curlm_result != CURLM_OK &&
@@ -210,12 +203,13 @@ int start_active_slot(struct active_request_slot *slot)
                slot->in_use = 0;
                return 0;
        }
-
+#endif
        return 1;
 }
 
 void run_active_slot(struct active_request_slot *slot)
 {
+#ifdef USE_CURL_MULTI
        int num_transfers;
        long last_pos = 0;
        long current_pos;
@@ -255,6 +249,10 @@ void run_active_slot(struct active_request_slot *slot)
                               &excfds, &select_timeout);
                }
        }
+#else
+       slot->curl_result = curl_easy_perform(slot->curl);
+       active_requests--;
+#endif
 }
 
 void start_request(struct transfer_request *request)
@@ -278,6 +276,20 @@ void start_request(struct transfer_request *request)
 
        request->local = open(request->tmpfile,
                              O_WRONLY | O_CREAT | O_EXCL, 0666);
+       /* This could have failed due to the "lazy directory creation";
+        * try to mkdir the last path component.
+        */
+       if (request->local < 0 && errno == ENOENT) {
+               char *dir = strrchr(request->tmpfile, '/');
+               if (dir) {
+                       *dir = 0;
+                       mkdir(request->tmpfile, 0777);
+                       *dir = '/';
+               }
+               request->local = open(request->tmpfile,
+                                     O_WRONLY | O_CREAT | O_EXCL, 0666);
+       }
+
        if (request->local < 0) {
                request->state = ABORTED;
                error("Couldn't create temporary file %s for %s: %s\n",
@@ -342,6 +354,7 @@ void start_request(struct transfer_request *request)
        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
        curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
        curl_easy_setopt(slot->curl, CURLOPT_URL, url);
+       curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
 
        /* If we have successfully processed data from a previous fetch
           attempt, only fetch the data we don't already have. */
@@ -356,7 +369,7 @@ void start_request(struct transfer_request *request)
                                 CURLOPT_HTTPHEADER, range_header);
        }
 
-       /* Try to add to multi handle, abort the request on error */
+       /* Try to get the request started, abort the request on error */
        if (!start_active_slot(slot)) {
                request->state = ABORTED;
                close(request->local);
@@ -390,7 +403,7 @@ void finish_request(struct transfer_request *request)
                return;
        }
        request->rename =
-               relink_or_rename(request->tmpfile, request->filename);
+               move_temp_to_file(request->tmpfile, request->filename);
 
        if (request->rename == 0)
                pull_say("got %s\n", sha1_to_hex(request->sha1));
@@ -413,6 +426,7 @@ void release_request(struct transfer_request *request)
        free(request);
 }
 
+#ifdef USE_CURL_MULTI
 void process_curl_messages()
 {
        int num_messages;
@@ -472,12 +486,16 @@ void process_request_queue()
 
        while (active_requests < max_requests && request != NULL) {
                if (request->state == WAITING) {
-                       start_request(request);
+                       if (has_sha1_file(request->sha1))
+                               release_request(request);
+                       else
+                               start_request(request);
                        curl_multi_perform(curlm, &num_transfers);
                }
                request = request->next;
        }
 }
+#endif
 
 void prefetch(unsigned char *sha1)
 {
@@ -505,19 +523,18 @@ void prefetch(unsigned char *sha1)
                }
                tail->next = newreq;
        }
+#ifdef USE_CURL_MULTI
        process_request_queue();
        process_curl_messages();
+#endif
 }
 
-static int got_alternates = 0;
-
 static int fetch_index(struct alt_base *repo, unsigned char *sha1)
 {
        char *hex = sha1_to_hex(sha1);
        char *filename;
        char *url;
        char tmpfile[PATH_MAX];
-       int ret;
        long prev_posn = 0;
        char range[RANGE_HEADER_SIZE];
        struct curl_slist *range_header = NULL;
@@ -545,6 +562,7 @@ static int fetch_index(struct alt_base *repo, unsigned char *sha1)
        curl_easy_setopt(slot->curl, CURLOPT_FILE, indexfile);
        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
        curl_easy_setopt(slot->curl, CURLOPT_URL, url);
+       curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
        slot->local = indexfile;
 
        /* If there is data present from a previous transfer attempt,
@@ -573,12 +591,7 @@ static int fetch_index(struct alt_base *repo, unsigned char *sha1)
 
        fclose(indexfile);
 
-       ret = relink_or_rename(tmpfile, filename);
-       if (ret)
-               return error("unable to write index filename %s: %s",
-                            filename, strerror(ret));
-
-       return 0;
+       return move_temp_to_file(tmpfile, filename);
 }
 
 static int setup_index(struct alt_base *repo, unsigned char *sha1)
@@ -607,8 +620,7 @@ static int fetch_alternates(char *base)
        struct alt_base *tail = alt;
 
        struct active_request_slot *slot;
-       if (got_alternates)
-               return 0;
+
        data = xmalloc(4096);
        buffer.size = 4095;
        buffer.posn = 0;
@@ -706,7 +718,6 @@ static int fetch_alternates(char *base)
                }
                i = posn + 1;
        }
-       got_alternates = 1;
        
        return ret;
 }
@@ -814,6 +825,7 @@ static int fetch_pack(struct alt_base *repo, unsigned char *sha1)
        curl_easy_setopt(slot->curl, CURLOPT_FILE, packfile);
        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
        curl_easy_setopt(slot->curl, CURLOPT_URL, url);
+       curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
        slot->local = packfile;
 
        /* If there is data present from a previous transfer attempt,
@@ -842,10 +854,9 @@ static int fetch_pack(struct alt_base *repo, unsigned char *sha1)
 
        fclose(packfile);
 
-       ret = relink_or_rename(tmpfile, filename);
+       ret = move_temp_to_file(tmpfile, filename);
        if (ret)
-               return error("unable to write pack filename %s: %s",
-                            filename, strerror(ret));
+               return ret;
 
        lst = &repo->packs;
        while (*lst != target)
@@ -864,13 +875,19 @@ static int fetch_object(struct alt_base *repo, unsigned char *sha1)
        char *hex = sha1_to_hex(sha1);
        int ret;
        struct transfer_request *request = request_queue_head;
-       int num_transfers;
 
        while (request != NULL && memcmp(request->sha1, sha1, 20))
                request = request->next;
        if (request == NULL)
                return error("Couldn't find request for %s in the queue", hex);
 
+       if (has_sha1_file(request->sha1)) {
+               release_request(request);
+               return 0;
+       }
+
+#ifdef USE_CURL_MULTI
+       int num_transfers;
        while (request->state == WAITING) {
                curl_multi_perform(curlm, &num_transfers);
                if (num_transfers < active_requests) {
@@ -878,9 +895,30 @@ static int fetch_object(struct alt_base *repo, unsigned char *sha1)
                        process_request_queue();
                }
        }
+#else
+       start_request(request);
+#endif
 
-       if (request->state == ACTIVE)
+       while (request->state == ACTIVE) {
                run_active_slot(request->slot);
+#ifndef USE_CURL_MULTI
+               request->curl_result = request->slot->curl_result;
+               curl_easy_getinfo(request->slot->curl,
+                                 CURLINFO_HTTP_CODE,
+                                 &request->http_code);
+               request->slot = NULL;
+
+               /* Use alternates if necessary */
+               if (request->http_code == 404 &&
+                   request->repo->next != NULL) {
+                       request->repo = request->repo->next;
+                       start_request(request);
+               } else {
+                       finish_request(request);
+                       request->state = COMPLETE;
+               }
+#endif
+       }
 
        if (request->state == ABORTED) {
                release_request(request);
@@ -931,9 +969,54 @@ int fetch(unsigned char *sha1)
                     alt->base);
 }
 
+static inline int needs_quote(int ch)
+{
+       switch (ch) {
+       case '/': case '-': case '.':
+       case 'A'...'Z': case 'a'...'z': case '0'...'9':
+               return 0;
+       default:
+               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 + 6; /* "refs/" + NUL */
+       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);
+       memcpy(qref + baselen, "refs/", 5);
+       for (cp = ref, dp = qref + baselen + 5; (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, *posn;
+        char *url;
         char hex[42];
         struct buffer buffer;
        char *base = alt->base;
@@ -943,13 +1026,7 @@ int fetch_ref(char *ref, unsigned char *sha1)
         buffer.buffer = hex;
         hex[41] = '\0';
         
-        url = xmalloc(strlen(base) + 6 + strlen(ref));
-        strcpy(url, base);
-        posn = url + strlen(base);
-        strcpy(posn, "refs/");
-        posn += 5;
-        strcpy(posn, ref);
-
+       url = quote_ref_url(base, ref);
        slot = get_active_slot();
        curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
@@ -992,11 +1069,6 @@ int main(int argc, char **argv)
                        arg++;
                } else if (!strcmp(argv[arg], "--recover")) {
                        get_recover = 1;
-               } else if (argv[arg][1] == 'r') {
-                       max_requests = atoi(argv[arg + 1]);
-                       if (max_requests < 1)
-                               max_requests = DEFAULT_MAX_REQUESTS;
-                       arg++;
                }
                arg++;
        }
@@ -1009,11 +1081,20 @@ int main(int argc, char **argv)
 
        curl_global_init(CURL_GLOBAL_ALL);
 
+#ifdef USE_CURL_MULTI
+       char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
+       if (http_max_requests != NULL)
+               max_requests = atoi(http_max_requests);
+       if (max_requests < 1)
+               max_requests = DEFAULT_MAX_REQUESTS;
+
        curlm = curl_multi_init();
        if (curlm == NULL) {
                fprintf(stderr, "Error creating curl multi handle.\n");
                return 1;
        }
+#endif
+       pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
        no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
        no_range_header = curl_slist_append(no_range_header, "Range:");
 
@@ -1053,6 +1134,7 @@ int main(int argc, char **argv)
        if (pull(commit_id))
                return 1;
 
+       curl_slist_free_all(pragma_header);
        curl_slist_free_all(no_pragma_header);
        curl_slist_free_all(no_range_header);
        curl_easy_cleanup(curl_default);
@@ -1061,7 +1143,9 @@ int main(int argc, char **argv)
                curl_easy_cleanup(slot->curl);
                slot = slot->next;
        }
+#ifdef USE_CURL_MULTI
        curl_multi_cleanup(curlm);
+#endif
        curl_global_cleanup();
        return 0;
 }