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;
18 static char curl_errorstr[CURL_ERROR_SIZE];
20 static char *initial_base;
26 struct packed_git *packs;
27 struct alt_base *next;
30 struct alt_base *alt = NULL;
33 static z_stream stream;
38 static int curl_ssl_verify;
47 static size_t fwrite_buffer(void *ptr, size_t eltsize, size_t nmemb,
48 struct buffer *buffer)
50 size_t size = eltsize * nmemb;
51 if (size > buffer->size - buffer->posn)
52 size = buffer->size - buffer->posn;
53 memcpy(buffer->buffer + buffer->posn, ptr, size);
58 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
61 unsigned char expn[4096];
62 size_t size = eltsize * nmemb;
65 ssize_t retval = write(local, ptr + posn, size - posn);
69 } while (posn < size);
71 stream.avail_in = size;
74 stream.next_out = expn;
75 stream.avail_out = sizeof(expn);
76 zret = inflate(&stream, Z_SYNC_FLUSH);
77 SHA1_Update(&c, expn, sizeof(expn) - stream.avail_out);
78 } while (stream.avail_in && zret == Z_OK);
82 void prefetch(unsigned char *sha1)
86 static int got_alternates = 0;
88 static int fetch_index(struct alt_base *repo, unsigned char *sha1)
95 if (has_pack_index(sha1))
99 fprintf(stderr, "Getting index for pack %s\n",
102 url = xmalloc(strlen(repo->base) + 64);
103 sprintf(url, "%s/objects/pack/pack-%s.idx",
104 repo->base, sha1_to_hex(sha1));
106 filename = sha1_pack_index_name(sha1);
107 indexfile = fopen(filename, "w");
109 return error("Unable to open local file %s for pack index",
112 curl_easy_setopt(curl, CURLOPT_FILE, indexfile);
113 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
114 curl_easy_setopt(curl, CURLOPT_URL, url);
115 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, no_pragma_header);
116 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr);
118 if (curl_easy_perform(curl)) {
120 return error("Unable to get pack index %s\n%s", url,
128 static int setup_index(struct alt_base *repo, unsigned char *sha1)
130 struct packed_git *new_pack;
131 if (has_pack_file(sha1))
132 return 0; // don't list this as something we can get
134 if (fetch_index(repo, sha1))
137 new_pack = parse_pack_index(sha1);
138 new_pack->next = repo->packs;
139 repo->packs = new_pack;
143 static int fetch_alternates(char *base)
146 struct buffer buffer;
150 int http_specific = 1;
153 data = xmalloc(4096);
156 buffer.buffer = data;
159 fprintf(stderr, "Getting alternates list\n");
161 url = xmalloc(strlen(base) + 31);
162 sprintf(url, "%s/objects/info/http-alternates", base);
164 curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
165 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
166 curl_easy_setopt(curl, CURLOPT_URL, url);
168 if (curl_easy_perform(curl) || !buffer.posn) {
171 sprintf(url, "%s/objects/info/alternates", base);
173 curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
174 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
175 curl_easy_setopt(curl, CURLOPT_URL, url);
177 if (curl_easy_perform(curl)) {
182 data[buffer.posn] = '\0';
184 while (i < buffer.posn) {
186 while (posn < buffer.posn && data[posn] != '\n')
188 if (data[posn] == '\n') {
191 struct alt_base *newalt;
193 if (data[i] == '/') {
194 serverlen = strchr(base + 8, '/') - base;
196 } else if (!memcmp(data + i, "../", 3)) {
198 serverlen = strlen(base);
199 while (i + 2 < posn &&
200 !memcmp(data + i, "../", 3)) {
203 } while (serverlen &&
204 base[serverlen - 1] != '/');
207 // If the server got removed, give up.
208 okay = strchr(base, ':') - base + 3 <
210 } else if (http_specific) {
211 char *colon = strchr(data + i, ':');
212 char *slash = strchr(data + i, '/');
213 if (colon && slash && colon < data + posn &&
214 slash < data + posn && colon < slash) {
218 // skip 'objects' at end
220 target = xmalloc(serverlen + posn - i - 6);
221 strncpy(target, base, serverlen);
222 strncpy(target + serverlen, data + i,
224 target[serverlen + posn - i - 7] = '\0';
227 "Also look at %s\n", target);
228 newalt = xmalloc(sizeof(*newalt));
230 newalt->base = target;
231 newalt->got_indices = 0;
232 newalt->packs = NULL;
244 static int fetch_indices(struct alt_base *repo)
246 unsigned char sha1[20];
248 struct buffer buffer;
252 if (repo->got_indices)
255 data = xmalloc(4096);
258 buffer.buffer = data;
261 fprintf(stderr, "Getting pack list\n");
263 url = xmalloc(strlen(repo->base) + 21);
264 sprintf(url, "%s/objects/info/packs", repo->base);
266 curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
267 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
268 curl_easy_setopt(curl, CURLOPT_URL, url);
269 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, NULL);
270 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr);
272 if (curl_easy_perform(curl))
273 return error("%s", curl_errorstr);
275 while (i < buffer.posn) {
279 if (i + 52 < buffer.posn &&
280 !strncmp(data + i, " pack-", 6) &&
281 !strncmp(data + i + 46, ".pack\n", 6)) {
282 get_sha1_hex(data + i + 6, sha1);
283 setup_index(repo, sha1);
288 while (data[i] != '\n')
294 repo->got_indices = 1;
298 static int fetch_pack(struct alt_base *repo, unsigned char *sha1)
301 struct packed_git *target;
302 struct packed_git **lst;
306 if (fetch_indices(repo))
308 target = find_sha1_pack(sha1, repo->packs);
313 fprintf(stderr, "Getting pack %s\n",
314 sha1_to_hex(target->sha1));
315 fprintf(stderr, " which contains %s\n",
319 url = xmalloc(strlen(repo->base) + 65);
320 sprintf(url, "%s/objects/pack/pack-%s.pack",
321 repo->base, sha1_to_hex(target->sha1));
323 filename = sha1_pack_name(target->sha1);
324 packfile = fopen(filename, "w");
326 return error("Unable to open local file %s for pack",
329 curl_easy_setopt(curl, CURLOPT_FILE, packfile);
330 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
331 curl_easy_setopt(curl, CURLOPT_URL, url);
332 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, no_pragma_header);
333 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr);
335 if (curl_easy_perform(curl)) {
337 return error("Unable to get pack file %s\n%s", url,
344 while (*lst != target)
345 lst = &((*lst)->next);
348 install_packed_git(target);
353 int fetch_object(struct alt_base *repo, unsigned char *sha1)
355 char *hex = sha1_to_hex(sha1);
356 char *filename = sha1_file_name(sha1);
357 unsigned char real_sha1[20];
358 char tmpfile[PATH_MAX];
363 snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX",
364 get_object_directory());
366 local = mkstemp(tmpfile);
368 return error("Couldn't create temporary file %s for %s: %s\n",
369 tmpfile, filename, strerror(errno));
371 memset(&stream, 0, sizeof(stream));
373 inflateInit(&stream);
377 curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
378 curl_easy_setopt(curl, CURLOPT_FILE, NULL);
379 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
380 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, no_pragma_header);
381 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr);
383 url = xmalloc(strlen(repo->base) + 50);
384 strcpy(url, repo->base);
385 posn = url + strlen(repo->base);
386 strcpy(posn, "objects/");
388 memcpy(posn, hex, 2);
391 strcpy(posn, hex + 2);
393 curl_easy_setopt(curl, CURLOPT_URL, url);
395 if (curl_easy_perform(curl)) {
397 return error("%s", curl_errorstr);
403 SHA1_Final(real_sha1, &c);
404 if (zret != Z_STREAM_END) {
406 return error("File %s (%s) corrupt\n", hex, url);
408 if (memcmp(sha1, real_sha1, 20)) {
410 return error("File %s has bad hash\n", hex);
412 ret = link(tmpfile, filename);
414 /* Same Coda hack as in write_sha1_file(sha1_file.c) */
416 if (ret == EXDEV && !rename(tmpfile, filename))
422 return error("unable to write sha1 filename %s: %s",
423 filename, strerror(ret));
426 pull_say("got %s\n", hex);
430 int fetch(unsigned char *sha1)
432 struct alt_base *altbase = alt;
434 if (!fetch_object(altbase, sha1))
436 if (!fetch_pack(altbase, sha1))
438 if (fetch_alternates(altbase->base) > 0) {
442 altbase = altbase->next;
444 return error("Unable to find %s under %s\n", sha1_to_hex(sha1),
448 int fetch_ref(char *ref, unsigned char *sha1)
452 struct buffer buffer;
453 char *base = initial_base;
459 curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
460 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
461 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, NULL);
462 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr);
464 url = xmalloc(strlen(base) + 6 + strlen(ref));
466 posn = url + strlen(base);
467 strcpy(posn, "refs/");
471 curl_easy_setopt(curl, CURLOPT_URL, url);
473 if (curl_easy_perform(curl))
474 return error("Couldn't get %s for %s\n%s",
475 url, ref, curl_errorstr);
478 get_sha1_hex(hex, sha1);
482 int main(int argc, char **argv)
488 while (arg < argc && argv[arg][0] == '-') {
489 if (argv[arg][1] == 't') {
491 } else if (argv[arg][1] == 'c') {
493 } else if (argv[arg][1] == 'a') {
497 } else if (argv[arg][1] == 'v') {
499 } else if (argv[arg][1] == 'w') {
500 write_ref = argv[arg + 1];
502 } else if (!strcmp(argv[arg], "--recover")) {
507 if (argc < arg + 2) {
508 usage("git-http-fetch [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
511 commit_id = argv[arg];
514 curl_global_init(CURL_GLOBAL_ALL);
516 curl = curl_easy_init();
517 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
519 curl_ssl_verify = getenv("GIT_SSL_NO_VERIFY") ? 0 : 1;
520 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify);
521 #if LIBCURL_VERSION_NUM >= 0x070907
522 curl_easy_setopt(curl, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
525 alt = xmalloc(sizeof(*alt));
527 alt->got_indices = 0;
535 curl_slist_free_all(no_pragma_header);
536 curl_global_cleanup();