clone-pack: new option --keep tells it not to explode the pack.
[git.git] / http-fetch.c
index b034a45..0aba891 100644 (file)
@@ -29,6 +29,7 @@ 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];
@@ -137,25 +138,6 @@ 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();
@@ -203,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);
 
@@ -294,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",
@@ -358,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. */
@@ -406,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));
@@ -489,7 +486,10 @@ 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;
@@ -529,15 +529,12 @@ void prefetch(unsigned char *sha1)
 #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;
@@ -565,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,
@@ -593,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)
@@ -627,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;
@@ -726,7 +718,6 @@ static int fetch_alternates(char *base)
                }
                i = posn + 1;
        }
-       got_alternates = 1;
        
        return ret;
 }
@@ -834,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,
@@ -862,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)
@@ -890,6 +881,11 @@ static int fetch_object(struct alt_base *repo, unsigned char *sha1)
        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) {
@@ -973,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;
@@ -985,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);
@@ -1034,22 +1069,11 @@ int main(int argc, char **argv)
                        arg++;
                } else if (!strcmp(argv[arg], "--recover")) {
                        get_recover = 1;
-#ifdef USE_CURL_MULTI
-               } else if (argv[arg][1] == 'r') {
-                       max_requests = atoi(argv[arg + 1]);
-                       if (max_requests < 1)
-                               max_requests = DEFAULT_MAX_REQUESTS;
-                       arg++;
-#endif
                }
                arg++;
        }
        if (argc < arg + 2) {
-#ifdef USE_CURL_MULTI
-               usage("git-http-fetch [-c] [-t] [-a] [-d] [-v] [-r concurrent-request-limit] [--recover] [-w ref] commit-id url");
-#else
                usage("git-http-fetch [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
-#endif
                return 1;
        }
        commit_id = argv[arg];
@@ -1058,12 +1082,19 @@ 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:");
 
@@ -1103,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);