git-rebase: Add --stat and --no-stat for producing diffstat on rebase
[gitweb.git] / http-walker.c
index 8dbf9cc369c59d36930c6cf67fda8ed279d50922..0dbad3c888c6c9441af4d9550fd147ecb5b1aaf3 100644 (file)
@@ -36,7 +36,7 @@ struct object_request
        char errorstr[CURL_ERROR_SIZE];
        long http_code;
        unsigned char real_sha1[20];
-       SHA_CTX c;
+       git_SHA_CTX c;
        z_stream stream;
        int zret;
        int rename;
@@ -82,8 +82,8 @@ static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
        do {
                obj_req->stream.next_out = expn;
                obj_req->stream.avail_out = sizeof(expn);
-               obj_req->zret = inflate(&obj_req->stream, Z_SYNC_FLUSH);
-               SHA1_Update(&obj_req->c, expn,
+               obj_req->zret = git_inflate(&obj_req->stream, Z_SYNC_FLUSH);
+               git_SHA1_Update(&obj_req->c, expn,
                            sizeof(expn) - obj_req->stream.avail_out);
        } while (obj_req->stream.avail_in && obj_req->zret == Z_OK);
        data_received++;
@@ -142,9 +142,9 @@ static void start_object_request(struct walker *walker,
 
        memset(&obj_req->stream, 0, sizeof(obj_req->stream));
 
-       inflateInit(&obj_req->stream);
+       git_inflate_init(&obj_req->stream);
 
-       SHA1_Init(&obj_req->c);
+       git_SHA1_Init(&obj_req->c);
 
        url = xmalloc(strlen(obj_req->repo->base) + 51);
        obj_req->url = xmalloc(strlen(obj_req->repo->base) + 51);
@@ -183,8 +183,8 @@ static void start_object_request(struct walker *walker,
           file; also rewind to the beginning of the local file. */
        if (prev_read == -1) {
                memset(&obj_req->stream, 0, sizeof(obj_req->stream));
-               inflateInit(&obj_req->stream);
-               SHA1_Init(&obj_req->c);
+               git_inflate_init(&obj_req->stream);
+               git_SHA1_Init(&obj_req->c);
                if (prev_posn>0) {
                        prev_posn = 0;
                        lseek(obj_req->local, 0, SEEK_SET);
@@ -243,8 +243,8 @@ static void finish_object_request(struct object_request *obj_req)
                return;
        }
 
-       inflateEnd(&obj_req->stream);
-       SHA1_Final(obj_req->real_sha1, &obj_req->c);
+       git_inflate_end(&obj_req->stream);
+       git_SHA1_Final(obj_req->real_sha1, &obj_req->c);
        if (obj_req->zret != Z_STREAM_END) {
                unlink(obj_req->tmpfile);
                return;
@@ -442,6 +442,8 @@ static int setup_index(struct walker *walker, struct alt_base *repo, unsigned ch
                return -1;
 
        new_pack = parse_pack_index(sha1);
+       if (!new_pack)
+               return -1; /* parse_pack_index() already issued error message */
        new_pack->next = repo->packs;
        repo->packs = new_pack;
        return 0;
@@ -644,6 +646,7 @@ static int fetch_indices(struct walker *walker, struct alt_base *repo)
        struct strbuf buffer = STRBUF_INIT;
        char *data;
        int i = 0;
+       int ret = 0;
 
        struct active_request_slot *slot;
        struct slot_results results;
@@ -666,19 +669,19 @@ static int fetch_indices(struct walker *walker, struct alt_base *repo)
        if (start_active_slot(slot)) {
                run_active_slot(slot);
                if (results.curl_result != CURLE_OK) {
-                       strbuf_release(&buffer);
                        if (missing_target(&results)) {
                                repo->got_indices = 1;
-                               return 0;
+                               goto cleanup;
                        } else {
                                repo->got_indices = 0;
-                               return error("%s", curl_errorstr);
+                               ret = error("%s", curl_errorstr);
+                               goto cleanup;
                        }
                }
        } else {
                repo->got_indices = 0;
-               strbuf_release(&buffer);
-               return error("Unable to start request");
+               ret = error("Unable to start request");
+               goto cleanup;
        }
 
        data = buffer.buf;
@@ -701,9 +704,11 @@ static int fetch_indices(struct walker *walker, struct alt_base *repo)
                i++;
        }
 
-       strbuf_release(&buffer);
        repo->got_indices = 1;
-       return 0;
+cleanup:
+       strbuf_release(&buffer);
+       free(url);
+       return ret;
 }
 
 static int fetch_pack(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
@@ -792,7 +797,7 @@ static int fetch_pack(struct walker *walker, struct alt_base *repo, unsigned cha
                lst = &((*lst)->next);
        *lst = (*lst)->next;
 
-       if (verify_pack(target, 0))
+       if (verify_pack(target))
                return -1;
        install_packed_git(target);
 
@@ -885,81 +890,10 @@ static int fetch(struct walker *walker, unsigned char *sha1)
                     data->alt->base);
 }
 
-static inline int needs_quote(int ch)
-{
-       if (((ch >= 'A') && (ch <= 'Z'))
-                       || ((ch >= 'a') && (ch <= 'z'))
-                       || ((ch >= '0') && (ch <= '9'))
-                       || (ch == '/')
-                       || (ch == '-')
-                       || (ch == '.'))
-               return 0;
-       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 + 7; /* "/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/", 6);
-       for (cp = ref, dp = qref + baselen + 6; (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;
-}
-
-static int fetch_ref(struct walker *walker, char *ref, unsigned char *sha1)
+static int fetch_ref(struct walker *walker, struct ref *ref)
 {
-       char *url;
-       struct strbuf buffer = STRBUF_INIT;
        struct walker_data *data = walker->data;
-       const char *base = data->alt->base;
-       struct active_request_slot *slot;
-       struct slot_results results;
-
-       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 (results.curl_result != CURLE_OK)
-                       return error("Couldn't get %s for %s\n%s",
-                                    url, ref, curl_errorstr);
-       } else {
-               return error("Unable to start request");
-       }
-
-       strbuf_rtrim(&buffer);
-       if (buffer.len != 40)
-               return 1;
-       return get_sha1_hex(buffer.buf, sha1);
+       return http_fetch_ref(data->alt->base, ref);
 }
 
 static void cleanup(struct walker *walker)
@@ -970,13 +904,13 @@ static void cleanup(struct walker *walker)
        curl_slist_free_all(data->no_pragma_header);
 }
 
-struct walker *get_http_walker(const char *url)
+struct walker *get_http_walker(const char *url, struct remote *remote)
 {
        char *s;
        struct walker_data *data = xmalloc(sizeof(struct walker_data));
        struct walker *walker = xmalloc(sizeof(struct walker));
 
-       http_init();
+       http_init(remote);
 
        data->no_pragma_header = curl_slist_append(NULL, "Pragma:");