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)
16 #define PREV_BUF_SIZE 4096
17 #define RANGE_HEADER_SIZE 30
20 static struct curl_slist
*no_pragma_header
;
21 static struct curl_slist
*no_range_header
;
22 static char curl_errorstr
[CURL_ERROR_SIZE
];
24 static char *initial_base
;
30 struct packed_git
*packs
;
31 struct alt_base
*next
;
34 static struct alt_base
*alt
= NULL
;
37 static z_stream stream
;
42 static int curl_ssl_verify
;
43 static char *ssl_cert
;
45 static char *ssl_capath
;
46 static char *ssl_cainfo
;
55 static size_t fwrite_buffer(void *ptr
, size_t eltsize
, size_t nmemb
,
56 struct buffer
*buffer
)
58 size_t size
= eltsize
* nmemb
;
59 if (size
> buffer
->size
- buffer
->posn
)
60 size
= buffer
->size
- buffer
->posn
;
61 memcpy(buffer
->buffer
+ buffer
->posn
, ptr
, size
);
66 static size_t fwrite_sha1_file(void *ptr
, size_t eltsize
, size_t nmemb
,
69 unsigned char expn
[4096];
70 size_t size
= eltsize
* nmemb
;
73 ssize_t retval
= write(local
, ptr
+ posn
, size
- posn
);
77 } while (posn
< size
);
79 stream
.avail_in
= size
;
82 stream
.next_out
= expn
;
83 stream
.avail_out
= sizeof(expn
);
84 zret
= inflate(&stream
, Z_SYNC_FLUSH
);
85 SHA1_Update(&c
, expn
, sizeof(expn
) - stream
.avail_out
);
86 } while (stream
.avail_in
&& zret
== Z_OK
);
90 void prefetch(unsigned char *sha1
)
94 int relink_or_rename(char *old
, char *new) {
99 /* Same Coda hack as in write_sha1_file(sha1_file.c) */
101 if (ret
== EXDEV
&& !rename(old
, new))
113 static int got_alternates
= 0;
115 static int fetch_index(struct alt_base
*repo
, unsigned char *sha1
)
119 char tmpfile
[PATH_MAX
];
122 char range
[RANGE_HEADER_SIZE
];
123 struct curl_slist
*range_header
= NULL
;
124 CURLcode curl_result
;
128 if (has_pack_index(sha1
))
132 fprintf(stderr
, "Getting index for pack %s\n",
135 url
= xmalloc(strlen(repo
->base
) + 64);
136 sprintf(url
, "%s/objects/pack/pack-%s.idx",
137 repo
->base
, sha1_to_hex(sha1
));
139 filename
= sha1_pack_index_name(sha1
);
140 snprintf(tmpfile
, sizeof(tmpfile
), "%s.temp", filename
);
141 indexfile
= fopen(tmpfile
, "a");
143 return error("Unable to open local file %s for pack index",
146 curl_easy_setopt(curl
, CURLOPT_FILE
, indexfile
);
147 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
148 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
149 curl_easy_setopt(curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
150 curl_easy_setopt(curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
152 /* If there is data present from a previous transfer attempt,
153 resume where it left off */
154 prev_posn
= ftell(indexfile
);
158 "Resuming fetch of index for pack %s at byte %ld\n",
159 sha1_to_hex(sha1
), prev_posn
);
160 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
161 range_header
= curl_slist_append(range_header
, range
);
162 curl_easy_setopt(curl
, CURLOPT_HTTPHEADER
, range_header
);
165 /* Clear out the Range: header after performing the request, so
166 other curl requests don't inherit inappropriate header data */
167 curl_result
= curl_easy_perform(curl
);
168 curl_easy_setopt(curl
, CURLOPT_HTTPHEADER
, no_range_header
);
169 if (curl_result
!= 0) {
171 return error("Unable to get pack index %s\n%s", url
,
177 ret
= relink_or_rename(tmpfile
, filename
);
179 return error("unable to write index filename %s: %s",
180 filename
, strerror(ret
));
185 static int setup_index(struct alt_base
*repo
, unsigned char *sha1
)
187 struct packed_git
*new_pack
;
188 if (has_pack_file(sha1
))
189 return 0; // don't list this as something we can get
191 if (fetch_index(repo
, sha1
))
194 new_pack
= parse_pack_index(sha1
);
195 new_pack
->next
= repo
->packs
;
196 repo
->packs
= new_pack
;
200 static int fetch_alternates(char *base
)
203 struct buffer buffer
;
207 int http_specific
= 1;
210 data
= xmalloc(4096);
213 buffer
.buffer
= data
;
216 fprintf(stderr
, "Getting alternates list\n");
218 url
= xmalloc(strlen(base
) + 31);
219 sprintf(url
, "%s/objects/info/http-alternates", base
);
221 curl_easy_setopt(curl
, CURLOPT_FILE
, &buffer
);
222 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
223 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
225 if (curl_easy_perform(curl
) || !buffer
.posn
) {
228 sprintf(url
, "%s/objects/info/alternates", base
);
230 curl_easy_setopt(curl
, CURLOPT_FILE
, &buffer
);
231 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
232 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
234 if (curl_easy_perform(curl
)) {
239 data
[buffer
.posn
] = '\0';
241 while (i
< buffer
.posn
) {
243 while (posn
< buffer
.posn
&& data
[posn
] != '\n')
245 if (data
[posn
] == '\n') {
248 struct alt_base
*newalt
;
250 if (data
[i
] == '/') {
251 serverlen
= strchr(base
+ 8, '/') - base
;
253 } else if (!memcmp(data
+ i
, "../", 3)) {
255 serverlen
= strlen(base
);
256 while (i
+ 2 < posn
&&
257 !memcmp(data
+ i
, "../", 3)) {
260 } while (serverlen
&&
261 base
[serverlen
- 1] != '/');
264 // If the server got removed, give up.
265 okay
= strchr(base
, ':') - base
+ 3 <
267 } else if (http_specific
) {
268 char *colon
= strchr(data
+ i
, ':');
269 char *slash
= strchr(data
+ i
, '/');
270 if (colon
&& slash
&& colon
< data
+ posn
&&
271 slash
< data
+ posn
&& colon
< slash
) {
275 // skip 'objects' at end
277 target
= xmalloc(serverlen
+ posn
- i
- 6);
278 strncpy(target
, base
, serverlen
);
279 strncpy(target
+ serverlen
, data
+ i
,
281 target
[serverlen
+ posn
- i
- 7] = '\0';
284 "Also look at %s\n", target
);
285 newalt
= xmalloc(sizeof(*newalt
));
287 newalt
->base
= target
;
288 newalt
->got_indices
= 0;
289 newalt
->packs
= NULL
;
301 static int fetch_indices(struct alt_base
*repo
)
303 unsigned char sha1
[20];
305 struct buffer buffer
;
309 if (repo
->got_indices
)
312 data
= xmalloc(4096);
315 buffer
.buffer
= data
;
318 fprintf(stderr
, "Getting pack list\n");
320 url
= xmalloc(strlen(repo
->base
) + 21);
321 sprintf(url
, "%s/objects/info/packs", repo
->base
);
323 curl_easy_setopt(curl
, CURLOPT_FILE
, &buffer
);
324 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
325 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
326 curl_easy_setopt(curl
, CURLOPT_HTTPHEADER
, NULL
);
327 curl_easy_setopt(curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
329 if (curl_easy_perform(curl
))
330 return error("%s", curl_errorstr
);
332 while (i
< buffer
.posn
) {
336 if (i
+ 52 < buffer
.posn
&&
337 !strncmp(data
+ i
, " pack-", 6) &&
338 !strncmp(data
+ i
+ 46, ".pack\n", 6)) {
339 get_sha1_hex(data
+ i
+ 6, sha1
);
340 setup_index(repo
, sha1
);
345 while (data
[i
] != '\n')
351 repo
->got_indices
= 1;
355 static int fetch_pack(struct alt_base
*repo
, unsigned char *sha1
)
358 struct packed_git
*target
;
359 struct packed_git
**lst
;
362 char tmpfile
[PATH_MAX
];
365 char range
[RANGE_HEADER_SIZE
];
366 struct curl_slist
*range_header
= NULL
;
367 CURLcode curl_result
;
369 if (fetch_indices(repo
))
371 target
= find_sha1_pack(sha1
, repo
->packs
);
376 fprintf(stderr
, "Getting pack %s\n",
377 sha1_to_hex(target
->sha1
));
378 fprintf(stderr
, " which contains %s\n",
382 url
= xmalloc(strlen(repo
->base
) + 65);
383 sprintf(url
, "%s/objects/pack/pack-%s.pack",
384 repo
->base
, sha1_to_hex(target
->sha1
));
386 filename
= sha1_pack_name(target
->sha1
);
387 snprintf(tmpfile
, sizeof(tmpfile
), "%s.temp", filename
);
388 packfile
= fopen(tmpfile
, "a");
390 return error("Unable to open local file %s for pack",
393 curl_easy_setopt(curl
, CURLOPT_FILE
, packfile
);
394 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
395 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
396 curl_easy_setopt(curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
397 curl_easy_setopt(curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
399 /* If there is data present from a previous transfer attempt,
400 resume where it left off */
401 prev_posn
= ftell(packfile
);
405 "Resuming fetch of pack %s at byte %ld\n",
406 sha1_to_hex(target
->sha1
), prev_posn
);
407 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
408 range_header
= curl_slist_append(range_header
, range
);
409 curl_easy_setopt(curl
, CURLOPT_HTTPHEADER
, range_header
);
412 /* Clear out the Range: header after performing the request, so
413 other curl requests don't inherit inappropriate header data */
414 curl_result
= curl_easy_perform(curl
);
415 curl_easy_setopt(curl
, CURLOPT_HTTPHEADER
, no_range_header
);
416 if (curl_result
!= 0) {
418 return error("Unable to get pack file %s\n%s", url
,
424 ret
= relink_or_rename(tmpfile
, filename
);
426 return error("unable to write pack filename %s: %s",
427 filename
, strerror(ret
));
430 while (*lst
!= target
)
431 lst
= &((*lst
)->next
);
434 if (verify_pack(target
, 0))
436 install_packed_git(target
);
441 static int fetch_object(struct alt_base
*repo
, unsigned char *sha1
)
443 char *hex
= sha1_to_hex(sha1
);
444 char *filename
= sha1_file_name(sha1
);
445 unsigned char real_sha1
[20];
446 char tmpfile
[PATH_MAX
];
447 char prevfile
[PATH_MAX
];
452 unsigned char prev_buf
[PREV_BUF_SIZE
];
453 ssize_t prev_read
= 0;
455 char range
[RANGE_HEADER_SIZE
];
456 struct curl_slist
*range_header
= NULL
;
457 CURLcode curl_result
;
459 snprintf(tmpfile
, sizeof(tmpfile
), "%s.temp", filename
);
460 snprintf(prevfile
, sizeof(prevfile
), "%s.prev", filename
);
462 if (unlink(prevfile
) && (errno
!= ENOENT
))
463 return error("Failed to unlink %s (%s)",
464 prevfile
, strerror(errno
));
465 if (rename(tmpfile
, prevfile
) && (errno
!= ENOENT
))
466 return error("Failed to rename %s to %s (%s)",
467 tmpfile
, prevfile
, strerror(errno
));
469 local
= open(tmpfile
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
471 /* Note: if another instance starts now, it will turn our new
472 tmpfile into its prevfile. */
475 return error("Couldn't create temporary file %s for %s: %s\n",
476 tmpfile
, filename
, strerror(errno
));
478 memset(&stream
, 0, sizeof(stream
));
480 inflateInit(&stream
);
484 curl_easy_setopt(curl
, CURLOPT_FAILONERROR
, 1);
485 curl_easy_setopt(curl
, CURLOPT_FILE
, NULL
);
486 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, fwrite_sha1_file
);
487 curl_easy_setopt(curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
488 curl_easy_setopt(curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
490 url
= xmalloc(strlen(repo
->base
) + 50);
491 strcpy(url
, repo
->base
);
492 posn
= url
+ strlen(repo
->base
);
493 strcpy(posn
, "objects/");
495 memcpy(posn
, hex
, 2);
498 strcpy(posn
, hex
+ 2);
500 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
502 /* If a previous temp file is present, process what was already
504 prevlocal
= open(prevfile
, O_RDONLY
);
505 if (prevlocal
!= -1) {
507 prev_read
= read(prevlocal
, prev_buf
, PREV_BUF_SIZE
);
509 if (fwrite_sha1_file(prev_buf
,
512 NULL
) == prev_read
) {
513 prev_posn
+= prev_read
;
518 } while (prev_read
> 0);
523 /* Reset inflate/SHA1 if there was an error reading the previous temp
524 file; also rewind to the beginning of the local file. */
525 if (prev_read
== -1) {
526 memset(&stream
, 0, sizeof(stream
));
527 inflateInit(&stream
);
531 lseek(local
, SEEK_SET
, 0);
536 /* If we have successfully processed data from a previous fetch
537 attempt, only fetch the data we don't already have. */
541 "Resuming fetch of object %s at byte %ld\n",
543 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
544 range_header
= curl_slist_append(range_header
, range
);
545 curl_easy_setopt(curl
, CURLOPT_HTTPHEADER
, range_header
);
548 /* Clear out the Range: header after performing the request, so
549 other curl requests don't inherit inappropriate header data */
550 curl_result
= curl_easy_perform(curl
);
551 curl_easy_setopt(curl
, CURLOPT_HTTPHEADER
, no_range_header
);
552 if (curl_result
!= 0) {
554 return error("%s", curl_errorstr
);
560 SHA1_Final(real_sha1
, &c
);
561 if (zret
!= Z_STREAM_END
) {
563 return error("File %s (%s) corrupt\n", hex
, url
);
565 if (memcmp(sha1
, real_sha1
, 20)) {
567 return error("File %s has bad hash\n", hex
);
569 ret
= relink_or_rename(tmpfile
, filename
);
571 return error("unable to write sha1 filename %s: %s",
572 filename
, strerror(ret
));
574 pull_say("got %s\n", hex
);
578 int fetch(unsigned char *sha1
)
580 struct alt_base
*altbase
= alt
;
582 if (!fetch_object(altbase
, sha1
))
584 if (!fetch_pack(altbase
, sha1
))
586 if (fetch_alternates(altbase
->base
) > 0) {
590 altbase
= altbase
->next
;
592 return error("Unable to find %s under %s\n", sha1_to_hex(sha1
),
596 int fetch_ref(char *ref
, unsigned char *sha1
)
600 struct buffer buffer
;
601 char *base
= initial_base
;
607 curl_easy_setopt(curl
, CURLOPT_FILE
, &buffer
);
608 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
609 curl_easy_setopt(curl
, CURLOPT_HTTPHEADER
, NULL
);
610 curl_easy_setopt(curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
612 url
= xmalloc(strlen(base
) + 6 + strlen(ref
));
614 posn
= url
+ strlen(base
);
615 strcpy(posn
, "refs/");
619 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
621 if (curl_easy_perform(curl
))
622 return error("Couldn't get %s for %s\n%s",
623 url
, ref
, curl_errorstr
);
626 get_sha1_hex(hex
, sha1
);
630 int main(int argc
, char **argv
)
636 while (arg
< argc
&& argv
[arg
][0] == '-') {
637 if (argv
[arg
][1] == 't') {
639 } else if (argv
[arg
][1] == 'c') {
641 } else if (argv
[arg
][1] == 'a') {
645 } else if (argv
[arg
][1] == 'v') {
647 } else if (argv
[arg
][1] == 'w') {
648 write_ref
= argv
[arg
+ 1];
650 } else if (!strcmp(argv
[arg
], "--recover")) {
655 if (argc
< arg
+ 2) {
656 usage("git-http-fetch [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
659 commit_id
= argv
[arg
];
662 curl_global_init(CURL_GLOBAL_ALL
);
664 curl
= curl_easy_init();
665 no_pragma_header
= curl_slist_append(no_pragma_header
, "Pragma:");
666 no_range_header
= curl_slist_append(no_range_header
, "Range:");
668 curl_ssl_verify
= getenv("GIT_SSL_NO_VERIFY") ? 0 : 1;
669 curl_easy_setopt(curl
, CURLOPT_SSL_VERIFYPEER
, curl_ssl_verify
);
670 #if LIBCURL_VERSION_NUM >= 0x070907
671 curl_easy_setopt(curl
, CURLOPT_NETRC
, CURL_NETRC_OPTIONAL
);
674 if ((ssl_cert
= getenv("GIT_SSL_CERT")) != NULL
) {
675 curl_easy_setopt(curl
, CURLOPT_SSLCERT
, ssl_cert
);
677 #if LIBCURL_VERSION_NUM >= 0x070902
678 if ((ssl_key
= getenv("GIT_SSL_KEY")) != NULL
) {
679 curl_easy_setopt(curl
, CURLOPT_SSLKEY
, ssl_key
);
682 #if LIBCURL_VERSION_NUM >= 0x070908
683 if ((ssl_capath
= getenv("GIT_SSL_CAPATH")) != NULL
) {
684 curl_easy_setopt(curl
, CURLOPT_CAPATH
, ssl_capath
);
687 if ((ssl_cainfo
= getenv("GIT_SSL_CAINFO")) != NULL
) {
688 curl_easy_setopt(curl
, CURLOPT_CAINFO
, ssl_cainfo
);
691 alt
= xmalloc(sizeof(*alt
));
693 alt
->got_indices
= 0;
701 curl_slist_free_all(no_pragma_header
);
702 curl_global_cleanup();