rev-list: allow -n<n> as shorthand for --max-count=<n>
[git.git] / http-fetch.c
index 4353173..92326f9 100644 (file)
@@ -267,7 +267,8 @@ static void process_object_response(void *callback_data)
        obj_req->state = COMPLETE;
 
        /* Use alternates if necessary */
-       if (obj_req->http_code == 404) {
+       if (obj_req->http_code == 404 ||
+           obj_req->curl_result == CURLE_FILE_COULDNT_READ_FILE) {
                fetch_alternates(alt->base);
                if (obj_req->repo->next != NULL) {
                        obj_req->repo =
@@ -374,6 +375,7 @@ static int fetch_index(struct alt_base *repo, unsigned char *sha1)
 
        FILE *indexfile;
        struct active_request_slot *slot;
+       static struct slot_results results;
 
        if (has_pack_index(sha1))
                return 0;
@@ -392,6 +394,7 @@ static int fetch_index(struct alt_base *repo, unsigned char *sha1)
                             filename);
 
        slot = get_active_slot();
+       slot->results = &results;
        curl_easy_setopt(slot->curl, CURLOPT_FILE, indexfile);
        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
        curl_easy_setopt(slot->curl, CURLOPT_URL, url);
@@ -413,7 +416,7 @@ static int fetch_index(struct alt_base *repo, unsigned char *sha1)
 
        if (start_active_slot(slot)) {
                run_active_slot(slot);
-               if (slot->curl_result != CURLE_OK) {
+               if (results.curl_result != CURLE_OK) {
                        fclose(indexfile);
                        return error("Unable to get pack index %s\n%s", url,
                                     curl_errorstr);
@@ -475,7 +478,8 @@ static void process_alternates_response(void *callback_data)
                        }
                }
        } else if (slot->curl_result != CURLE_OK) {
-               if (slot->http_code != 404) {
+               if (slot->http_code != 404 &&
+                   slot->curl_result != CURLE_FILE_COULDNT_READ_FILE) {
                        got_alternates = -1;
                        return;
                }
@@ -614,6 +618,7 @@ static int fetch_indices(struct alt_base *repo)
        int i = 0;
 
        struct active_request_slot *slot;
+       static struct slot_results results;
 
        if (repo->got_indices)
                return 0;
@@ -630,14 +635,16 @@ static int fetch_indices(struct alt_base *repo)
        sprintf(url, "%s/objects/info/packs", repo->base);
 
        slot = get_active_slot();
+       slot->results = &results;
        curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
        curl_easy_setopt(slot->curl, CURLOPT_URL, url);
        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
        if (start_active_slot(slot)) {
                run_active_slot(slot);
-               if (slot->curl_result != CURLE_OK) {
-                       if (slot->http_code == 404) {
+               if (results.curl_result != CURLE_OK) {
+                       if (results.http_code == 404 ||
+                           results.curl_result == CURLE_FILE_COULDNT_READ_FILE) {
                                repo->got_indices = 1;
                                free(buffer.buffer);
                                return 0;
@@ -658,7 +665,7 @@ static int fetch_indices(struct alt_base *repo)
                switch (data[i]) {
                case 'P':
                        i++;
-                       if (i + 52 < buffer.posn &&
+                       if (i + 52 <= buffer.posn &&
                            !strncmp(data + i, " pack-", 6) &&
                            !strncmp(data + i + 46, ".pack\n", 6)) {
                                get_sha1_hex(data + i + 6, sha1);
@@ -667,7 +674,7 @@ static int fetch_indices(struct alt_base *repo)
                                break;
                        }
                default:
-                       while (data[i] != '\n')
+                       while (i < buffer.posn && data[i] != '\n')
                                i++;
                }
                i++;
@@ -692,6 +699,7 @@ static int fetch_pack(struct alt_base *repo, unsigned char *sha1)
        struct curl_slist *range_header = NULL;
 
        struct active_request_slot *slot;
+       static struct slot_results results;
 
        if (fetch_indices(repo))
                return -1;
@@ -718,6 +726,7 @@ static int fetch_pack(struct alt_base *repo, unsigned char *sha1)
                             filename);
 
        slot = get_active_slot();
+       slot->results = &results;
        curl_easy_setopt(slot->curl, CURLOPT_FILE, packfile);
        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
        curl_easy_setopt(slot->curl, CURLOPT_URL, url);
@@ -739,7 +748,7 @@ static int fetch_pack(struct alt_base *repo, unsigned char *sha1)
 
        if (start_active_slot(slot)) {
                run_active_slot(slot);
-               if (slot->curl_result != CURLE_OK) {
+               if (results.curl_result != CURLE_OK) {
                        fclose(packfile);
                        return error("Unable to get pack file %s\n%s", url,
                                     curl_errorstr);
@@ -802,7 +811,8 @@ static int fetch_object(struct alt_base *repo, unsigned char *sha1)
                ret = error("Request for %s aborted", hex);
        } else if (obj_req->curl_result != CURLE_OK &&
                   obj_req->http_code != 416) {
-               if (obj_req->http_code == 404)
+               if (obj_req->http_code == 404 ||
+                   obj_req->curl_result == CURLE_FILE_COULDNT_READ_FILE)
                        ret = -1; /* Be silent, it is probably in a pack. */
                else
                        ret = error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",
@@ -890,6 +900,7 @@ int fetch_ref(char *ref, unsigned char *sha1)
         struct buffer buffer;
        char *base = alt->base;
        struct active_request_slot *slot;
+       static struct slot_results results;
         buffer.size = 41;
         buffer.posn = 0;
         buffer.buffer = hex;
@@ -897,13 +908,14 @@ int fetch_ref(char *ref, unsigned char *sha1)
         
        url = quote_ref_url(base, ref);
        slot = get_active_slot();
+       slot->results = &results;
        curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
        curl_easy_setopt(slot->curl, CURLOPT_URL, url);
        if (start_active_slot(slot)) {
                run_active_slot(slot);
-               if (slot->curl_result != CURLE_OK)
+               if (results.curl_result != CURLE_OK)
                        return error("Couldn't get %s for %s\n%s",
                                     url, ref, curl_errorstr);
        } else {
@@ -922,6 +934,8 @@ int main(int argc, char **argv)
        int arg = 1;
        int rc = 0;
 
+       setup_git_directory();
+
        while (arg < argc && argv[arg][0] == '-') {
                if (argv[arg][1] == 't') {
                        get_tree = 1;