9 #if LIBCURL_VERSION_NUM < 0x070704
10 #define curl_global_cleanup() do { /* nothing */ } while(0)
12 #if LIBCURL_VERSION_NUM < 0x070800
13 #define curl_global_init(a) do { /* nothing */ } while(0)
17 static struct curl_slist *no_pragma_header;
19 static char *initial_base;
25 struct packed_git *packs;
26 struct alt_base *next;
29 struct alt_base *alt = NULL;
32 static z_stream stream;
37 static int curl_ssl_verify;
46 static size_t fwrite_buffer(void *ptr, size_t eltsize, size_t nmemb,
47 struct buffer *buffer)
49 size_t size = eltsize * nmemb;
50 if (size > buffer->size - buffer->posn)
51 size = buffer->size - buffer->posn;
52 memcpy(buffer->buffer + buffer->posn, ptr, size);
57 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
60 unsigned char expn[4096];
61 size_t size = eltsize * nmemb;
64 ssize_t retval = write(local, ptr + posn, size - posn);
68 } while (posn < size);
70 stream.avail_in = size;
73 stream.next_out = expn;
74 stream.avail_out = sizeof(expn);
75 zret = inflate(&stream, Z_SYNC_FLUSH);
76 SHA1_Update(&c, expn, sizeof(expn) - stream.avail_out);
77 } while (stream.avail_in && zret == Z_OK);
81 void prefetch(unsigned char *sha1)
85 static int got_alternates = 0;
87 static int fetch_index(struct alt_base *repo, unsigned char *sha1)
94 if (has_pack_index(sha1))
98 fprintf(stderr, "Getting index for pack %s\n",
101 url = xmalloc(strlen(repo->base) + 64);
102 sprintf(url, "%s/objects/pack/pack-%s.idx",
103 repo->base, sha1_to_hex(sha1));
105 filename = sha1_pack_index_name(sha1);
106 indexfile = fopen(filename, "w");
108 return error("Unable to open local file %s for pack index",
111 curl_easy_setopt(curl, CURLOPT_FILE, indexfile);
112 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
113 curl_easy_setopt(curl, CURLOPT_URL, url);
114 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, no_pragma_header);
116 if (curl_easy_perform(curl)) {
118 return error("Unable to get pack index %s", url);
125 static int setup_index(struct alt_base *repo, unsigned char *sha1)
127 struct packed_git *new_pack;
128 if (has_pack_file(sha1))
129 return 0; // don't list this as something we can get
131 if (fetch_index(repo, sha1))
134 new_pack = parse_pack_index(sha1);
135 new_pack->next = repo->packs;
136 repo->packs = new_pack;
140 static int fetch_alternates(char *base)
143 struct buffer buffer;
147 int http_specific = 1;
150 data = xmalloc(4096);
153 buffer.buffer = data;
156 fprintf(stderr, "Getting alternates list\n");
158 url = xmalloc(strlen(base) + 31);
159 sprintf(url, "%s/objects/info/http-alternates", base);
161 curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
162 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
163 curl_easy_setopt(curl, CURLOPT_URL, url);
165 if (curl_easy_perform(curl) || !buffer.posn) {
168 sprintf(url, "%s/objects/info/alternates", base);
170 curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
171 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
172 curl_easy_setopt(curl, CURLOPT_URL, url);
174 if (curl_easy_perform(curl)) {
179 data[buffer.posn] = '\0';
181 while (i < buffer.posn) {
183 while (posn < buffer.posn && data[posn] != '\n')
185 if (data[posn] == '\n') {
188 struct alt_base *newalt;
190 if (data[i] == '/') {
191 serverlen = strchr(base + 8, '/') - base;
193 } else if (!memcmp(data + i, "../", 3)) {
195 serverlen = strlen(base);
196 while (i + 2 < posn &&
197 !memcmp(data + i, "../", 3)) {
200 } while (serverlen &&
201 base[serverlen - 1] != '/');
204 // If the server got removed, give up.
205 okay = strchr(base, ':') - base + 3 <
207 } else if (http_specific) {
208 char *colon = strchr(data + i, ':');
209 char *slash = strchr(data + i, '/');
210 if (colon && slash && colon < data + posn &&
211 slash < data + posn && colon < slash) {
215 // skip 'objects' at end
217 target = xmalloc(serverlen + posn - i - 6);
218 strncpy(target, base, serverlen);
219 strncpy(target + serverlen, data + i,
221 target[serverlen + posn - i - 7] = '\0';
224 "Also look at %s\n", target);
225 newalt = xmalloc(sizeof(*newalt));
227 newalt->base = target;
228 newalt->got_indices = 0;
229 newalt->packs = NULL;
241 static int fetch_indices(struct alt_base *repo)
243 unsigned char sha1[20];
245 struct buffer buffer;
249 if (repo->got_indices)
252 data = xmalloc(4096);
255 buffer.buffer = data;
258 fprintf(stderr, "Getting pack list\n");
260 url = xmalloc(strlen(repo->base) + 21);
261 sprintf(url, "%s/objects/info/packs", repo->base);
263 curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
264 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
265 curl_easy_setopt(curl, CURLOPT_URL, url);
266 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, NULL);
268 if (curl_easy_perform(curl)) {
272 while (i < buffer.posn) {
276 if (i + 52 < buffer.posn &&
277 !strncmp(data + i, " pack-", 6) &&
278 !strncmp(data + i + 46, ".pack\n", 6)) {
279 get_sha1_hex(data + i + 6, sha1);
280 setup_index(repo, sha1);
285 while (data[i] != '\n')
291 repo->got_indices = 1;
295 static int fetch_pack(struct alt_base *repo, unsigned char *sha1)
298 struct packed_git *target;
299 struct packed_git **lst;
303 if (fetch_indices(repo))
305 target = find_sha1_pack(sha1, repo->packs);
310 fprintf(stderr, "Getting pack %s\n",
311 sha1_to_hex(target->sha1));
312 fprintf(stderr, " which contains %s\n",
316 url = xmalloc(strlen(repo->base) + 65);
317 sprintf(url, "%s/objects/pack/pack-%s.pack",
318 repo->base, sha1_to_hex(target->sha1));
320 filename = sha1_pack_name(target->sha1);
321 packfile = fopen(filename, "w");
323 return error("Unable to open local file %s for pack",
326 curl_easy_setopt(curl, CURLOPT_FILE, packfile);
327 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
328 curl_easy_setopt(curl, CURLOPT_URL, url);
329 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, no_pragma_header);
331 if (curl_easy_perform(curl)) {
333 return error("Unable to get pack file %s", url);
339 while (*lst != target)
340 lst = &((*lst)->next);
343 install_packed_git(target);
348 int fetch_object(struct alt_base *repo, unsigned char *sha1)
350 char *hex = sha1_to_hex(sha1);
351 char *filename = sha1_file_name(sha1);
352 unsigned char real_sha1[20];
353 char tmpfile[PATH_MAX];
358 snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX",
359 get_object_directory());
361 local = mkstemp(tmpfile);
363 return error("Couldn't create temporary file %s for %s: %s\n",
364 tmpfile, filename, strerror(errno));
366 memset(&stream, 0, sizeof(stream));
368 inflateInit(&stream);
372 curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
373 curl_easy_setopt(curl, CURLOPT_FILE, NULL);
374 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
375 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, no_pragma_header);
377 url = xmalloc(strlen(repo->base) + 50);
378 strcpy(url, repo->base);
379 posn = url + strlen(repo->base);
380 strcpy(posn, "objects/");
382 memcpy(posn, hex, 2);
385 strcpy(posn, hex + 2);
387 curl_easy_setopt(curl, CURLOPT_URL, url);
389 if (curl_easy_perform(curl)) {
397 SHA1_Final(real_sha1, &c);
398 if (zret != Z_STREAM_END) {
400 return error("File %s (%s) corrupt\n", hex, url);
402 if (memcmp(sha1, real_sha1, 20)) {
404 return error("File %s has bad hash\n", hex);
406 ret = link(tmpfile, filename);
408 /* Same Coda hack as in write_sha1_file(sha1_file.c) */
410 if (ret == EXDEV && !rename(tmpfile, filename))
416 return error("unable to write sha1 filename %s: %s",
417 filename, strerror(ret));
420 pull_say("got %s\n", hex);
424 int fetch(unsigned char *sha1)
426 struct alt_base *altbase = alt;
428 if (!fetch_object(altbase, sha1))
430 if (!fetch_pack(altbase, sha1))
432 if (fetch_alternates(altbase->base) > 0) {
436 altbase = altbase->next;
438 return error("Unable to find %s under %s\n", sha1_to_hex(sha1),
442 int fetch_ref(char *ref, unsigned char *sha1)
446 struct buffer buffer;
447 char *base = initial_base;
453 curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
454 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
455 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, NULL);
457 url = xmalloc(strlen(base) + 6 + strlen(ref));
459 posn = url + strlen(base);
460 strcpy(posn, "refs/");
464 curl_easy_setopt(curl, CURLOPT_URL, url);
466 if (curl_easy_perform(curl))
467 return error("Couldn't get %s for %s\n", url, ref);
470 get_sha1_hex(hex, sha1);
474 int main(int argc, char **argv)
480 while (arg < argc && argv[arg][0] == '-') {
481 if (argv[arg][1] == 't') {
483 } else if (argv[arg][1] == 'c') {
485 } else if (argv[arg][1] == 'a') {
489 } else if (argv[arg][1] == 'v') {
491 } else if (argv[arg][1] == 'w') {
492 write_ref = argv[arg + 1];
497 if (argc < arg + 2) {
498 usage("git-http-fetch [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
501 commit_id = argv[arg];
504 curl_global_init(CURL_GLOBAL_ALL);
506 curl = curl_easy_init();
507 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
509 curl_ssl_verify = getenv("GIT_SSL_NO_VERIFY") ? 0 : 1;
510 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify);
511 #if LIBCURL_VERSION_NUM >= 0x070907
512 curl_easy_setopt(curl, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
515 alt = xmalloc(sizeof(*alt));
517 alt->got_indices = 0;
525 curl_slist_free_all(no_pragma_header);
526 curl_global_cleanup();