12 static const char http_push_usage
[] =
13 "git-http-push [--complete] [--force] [--verbose] <url> <ref> [<ref>...]\n";
15 #if LIBCURL_VERSION_NUM >= 0x070908
16 #define USE_CURL_MULTI
17 #define DEFAULT_MAX_REQUESTS 5
20 #if LIBCURL_VERSION_NUM < 0x070704
21 #define curl_global_cleanup() do { /* nothing */ } while(0)
23 #if LIBCURL_VERSION_NUM < 0x070800
24 #define curl_global_init(a) do { /* nothing */ } while(0)
27 #if LIBCURL_VERSION_NUM < 0x070c04
28 #define NO_CURL_EASY_DUPHANDLE
31 #define RANGE_HEADER_SIZE 30
33 /* DAV method names and request body templates */
34 #define DAV_LOCK "LOCK"
35 #define DAV_MKCOL "MKCOL"
36 #define DAV_MOVE "MOVE"
37 #define DAV_PROPFIND "PROPFIND"
39 #define DAV_UNLOCK "UNLOCK"
40 #define PROPFIND_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:prop xmlns:R=\"%s\">\n<D:supportedlock/>\n</D:prop>\n</D:propfind>"
41 #define LOCK_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:lockinfo xmlns:D=\"DAV:\">\n<D:lockscope><D:exclusive/></D:lockscope>\n<D:locktype><D:write/></D:locktype>\n<D:owner>\n<D:href>mailto:%s</D:href>\n</D:owner>\n</D:lockinfo>"
44 #define LOCK_REFRESH 30
46 static int active_requests
= 0;
47 static int data_received
;
48 static int pushing
= 0;
49 static int aborted
= 0;
50 static char remote_dir_exists
[256];
53 static int max_requests
= -1;
56 #ifndef NO_CURL_EASY_DUPHANDLE
57 static CURL
*curl_default
;
59 static struct curl_slist
*no_pragma_header
;
60 static struct curl_slist
*default_headers
;
61 static char curl_errorstr
[CURL_ERROR_SIZE
];
63 static int push_verbosely
= 0;
64 static int push_all
= 0;
65 static int force_all
= 0;
77 struct packed_git
*packs
;
80 static struct repo
*remote
= NULL
;
93 struct transfer_request
95 unsigned char sha1
[20];
98 struct active_lock
*lock
;
99 struct curl_slist
*headers
;
100 struct buffer buffer
;
101 char filename
[PATH_MAX
];
102 char tmpfile
[PATH_MAX
];
103 enum transfer_state state
;
104 CURLcode curl_result
;
105 char errorstr
[CURL_ERROR_SIZE
];
107 unsigned char real_sha1
[20];
112 struct active_request_slot
*slot
;
113 struct transfer_request
*next
;
116 struct active_request_slot
122 CURLcode curl_result
;
124 struct active_request_slot
*next
;
127 static struct transfer_request
*request_queue_head
= NULL
;
128 static struct active_request_slot
*active_queue_head
= NULL
;
130 static int curl_ssl_verify
= -1;
131 static char *ssl_cert
= NULL
;
132 #if LIBCURL_VERSION_NUM >= 0x070902
133 static char *ssl_key
= NULL
;
135 #if LIBCURL_VERSION_NUM >= 0x070908
136 static char *ssl_capath
= NULL
;
138 static char *ssl_cainfo
= NULL
;
139 static long curl_low_speed_limit
= -1;
140 static long curl_low_speed_time
= -1;
149 int ctx_locktoken_href
;
165 int lock_exclusive_write
;
168 static int http_options(const char *var
, const char *value
)
170 if (!strcmp("http.sslverify", var
)) {
171 if (curl_ssl_verify
== -1) {
172 curl_ssl_verify
= git_config_bool(var
, value
);
177 if (!strcmp("http.sslcert", var
)) {
178 if (ssl_cert
== NULL
) {
179 ssl_cert
= xmalloc(strlen(value
)+1);
180 strcpy(ssl_cert
, value
);
184 #if LIBCURL_VERSION_NUM >= 0x070902
185 if (!strcmp("http.sslkey", var
)) {
186 if (ssl_key
== NULL
) {
187 ssl_key
= xmalloc(strlen(value
)+1);
188 strcpy(ssl_key
, value
);
193 #if LIBCURL_VERSION_NUM >= 0x070908
194 if (!strcmp("http.sslcapath", var
)) {
195 if (ssl_capath
== NULL
) {
196 ssl_capath
= xmalloc(strlen(value
)+1);
197 strcpy(ssl_capath
, value
);
202 if (!strcmp("http.sslcainfo", var
)) {
203 if (ssl_cainfo
== NULL
) {
204 ssl_cainfo
= xmalloc(strlen(value
)+1);
205 strcpy(ssl_cainfo
, value
);
210 #ifdef USE_CURL_MULTI
211 if (!strcmp("http.maxrequests", var
)) {
212 if (max_requests
== -1)
213 max_requests
= git_config_int(var
, value
);
218 if (!strcmp("http.lowspeedlimit", var
)) {
219 if (curl_low_speed_limit
== -1)
220 curl_low_speed_limit
= (long)git_config_int(var
, value
);
223 if (!strcmp("http.lowspeedtime", var
)) {
224 if (curl_low_speed_time
== -1)
225 curl_low_speed_time
= (long)git_config_int(var
, value
);
229 /* Fall back on the default ones */
230 return git_default_config(var
, value
);
233 static size_t fread_buffer(void *ptr
, size_t eltsize
, size_t nmemb
,
234 struct buffer
*buffer
)
236 size_t size
= eltsize
* nmemb
;
237 if (size
> buffer
->size
- buffer
->posn
)
238 size
= buffer
->size
- buffer
->posn
;
239 memcpy(ptr
, buffer
->buffer
+ buffer
->posn
, size
);
240 buffer
->posn
+= size
;
244 static size_t fwrite_buffer_dynamic(const void *ptr
, size_t eltsize
,
245 size_t nmemb
, struct buffer
*buffer
)
247 size_t size
= eltsize
* nmemb
;
248 if (size
> buffer
->size
- buffer
->posn
) {
249 buffer
->size
= buffer
->size
* 3 / 2;
250 if (buffer
->size
< buffer
->posn
+ size
)
251 buffer
->size
= buffer
->posn
+ size
;
252 buffer
->buffer
= xrealloc(buffer
->buffer
, buffer
->size
);
254 memcpy(buffer
->buffer
+ buffer
->posn
, ptr
, size
);
255 buffer
->posn
+= size
;
260 static size_t fwrite_null(const void *ptr
, size_t eltsize
,
261 size_t nmemb
, struct buffer
*buffer
)
264 return eltsize
* nmemb
;
267 #ifdef USE_CURL_MULTI
268 static void process_curl_messages(void);
269 static void process_request_queue(void);
272 static CURL
* get_curl_handle(void)
274 CURL
* result
= curl_easy_init();
276 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, curl_ssl_verify
);
277 #if LIBCURL_VERSION_NUM >= 0x070907
278 curl_easy_setopt(result
, CURLOPT_NETRC
, CURL_NETRC_OPTIONAL
);
281 if (ssl_cert
!= NULL
)
282 curl_easy_setopt(result
, CURLOPT_SSLCERT
, ssl_cert
);
283 #if LIBCURL_VERSION_NUM >= 0x070902
285 curl_easy_setopt(result
, CURLOPT_SSLKEY
, ssl_key
);
287 #if LIBCURL_VERSION_NUM >= 0x070908
288 if (ssl_capath
!= NULL
)
289 curl_easy_setopt(result
, CURLOPT_CAPATH
, ssl_capath
);
291 if (ssl_cainfo
!= NULL
)
292 curl_easy_setopt(result
, CURLOPT_CAINFO
, ssl_cainfo
);
293 curl_easy_setopt(result
, CURLOPT_FAILONERROR
, 1);
295 if (curl_low_speed_limit
> 0 && curl_low_speed_time
> 0) {
296 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_LIMIT
,
297 curl_low_speed_limit
);
298 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_TIME
,
299 curl_low_speed_time
);
305 static struct active_request_slot
*get_active_slot(void)
307 struct active_request_slot
*slot
= active_queue_head
;
308 struct active_request_slot
*newslot
;
310 #ifdef USE_CURL_MULTI
313 /* Wait for a slot to open up if the queue is full */
314 while (active_requests
>= max_requests
) {
315 curl_multi_perform(curlm
, &num_transfers
);
316 if (num_transfers
< active_requests
) {
317 process_curl_messages();
322 while (slot
!= NULL
&& slot
->in_use
) {
326 newslot
= xmalloc(sizeof(*newslot
));
327 newslot
->curl
= NULL
;
329 newslot
->next
= NULL
;
331 slot
= active_queue_head
;
333 active_queue_head
= newslot
;
335 while (slot
->next
!= NULL
) {
338 slot
->next
= newslot
;
343 if (slot
->curl
== NULL
) {
344 #ifdef NO_CURL_EASY_DUPHANDLE
345 slot
->curl
= get_curl_handle();
347 slot
->curl
= curl_easy_duphandle(curl_default
);
355 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, default_headers
);
356 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
361 static int start_active_slot(struct active_request_slot
*slot
)
363 #ifdef USE_CURL_MULTI
364 CURLMcode curlm_result
= curl_multi_add_handle(curlm
, slot
->curl
);
366 if (curlm_result
!= CURLM_OK
&&
367 curlm_result
!= CURLM_CALL_MULTI_PERFORM
) {
376 static void run_active_slot(struct active_request_slot
*slot
)
378 #ifdef USE_CURL_MULTI
386 struct timeval select_timeout
;
387 CURLMcode curlm_result
;
389 while (!slot
->done
) {
392 curlm_result
= curl_multi_perform(curlm
,
394 } while (curlm_result
== CURLM_CALL_MULTI_PERFORM
);
395 if (num_transfers
< active_requests
) {
396 process_curl_messages();
397 process_request_queue();
400 if (!data_received
&& slot
->local
!= NULL
) {
401 current_pos
= ftell(slot
->local
);
402 if (current_pos
> last_pos
)
404 last_pos
= current_pos
;
407 if (!slot
->done
&& !data_received
) {
412 select_timeout
.tv_sec
= 0;
413 select_timeout
.tv_usec
= 50000;
414 select(max_fd
, &readfds
, &writefds
,
415 &excfds
, &select_timeout
);
419 slot
->curl_result
= curl_easy_perform(slot
->curl
);
424 static void start_check(struct transfer_request
*request
)
426 char *hex
= sha1_to_hex(request
->sha1
);
427 struct active_request_slot
*slot
;
430 request
->url
= xmalloc(strlen(remote
->url
) + 55);
431 strcpy(request
->url
, remote
->url
);
432 posn
= request
->url
+ strlen(remote
->url
);
433 strcpy(posn
, "objects/");
435 memcpy(posn
, hex
, 2);
438 strcpy(posn
, hex
+ 2);
440 slot
= get_active_slot();
441 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
442 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
443 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
445 if (start_active_slot(slot
)) {
446 request
->slot
= slot
;
447 request
->state
= RUN_HEAD
;
449 request
->state
= ABORTED
;
454 static void start_mkcol(struct transfer_request
*request
)
456 char *hex
= sha1_to_hex(request
->sha1
);
457 struct active_request_slot
*slot
;
460 request
->url
= xmalloc(strlen(remote
->url
) + 13);
461 strcpy(request
->url
, remote
->url
);
462 posn
= request
->url
+ strlen(remote
->url
);
463 strcpy(posn
, "objects/");
465 memcpy(posn
, hex
, 2);
469 slot
= get_active_slot();
470 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1); /* undo PUT setup */
471 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
472 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
473 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MKCOL
);
474 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
476 if (start_active_slot(slot
)) {
477 request
->slot
= slot
;
478 request
->state
= RUN_MKCOL
;
480 request
->state
= ABORTED
;
485 static void start_put(struct transfer_request
*request
)
487 char *hex
= sha1_to_hex(request
->sha1
);
488 struct active_request_slot
*slot
;
498 unpacked
= read_sha1_file(request
->sha1
, type
, &len
);
499 hdrlen
= sprintf(hdr
, "%s %lu", type
, len
) + 1;
502 memset(&stream
, 0, sizeof(stream
));
503 deflateInit(&stream
, Z_BEST_COMPRESSION
);
504 size
= deflateBound(&stream
, len
+ hdrlen
);
505 request
->buffer
.buffer
= xmalloc(size
);
508 stream
.next_out
= request
->buffer
.buffer
;
509 stream
.avail_out
= size
;
512 stream
.next_in
= (void *)hdr
;
513 stream
.avail_in
= hdrlen
;
514 while (deflate(&stream
, 0) == Z_OK
)
517 /* Then the data itself.. */
518 stream
.next_in
= unpacked
;
519 stream
.avail_in
= len
;
520 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
525 request
->buffer
.size
= stream
.total_out
;
526 request
->buffer
.posn
= 0;
528 if (request
->url
!= NULL
)
530 request
->url
= xmalloc(strlen(remote
->url
) +
531 strlen(request
->lock
->token
) + 51);
532 strcpy(request
->url
, remote
->url
);
533 posn
= request
->url
+ strlen(remote
->url
);
534 strcpy(posn
, "objects/");
536 memcpy(posn
, hex
, 2);
539 strcpy(posn
, hex
+ 2);
540 request
->dest
= xmalloc(strlen(request
->url
) + 14);
541 sprintf(request
->dest
, "Destination: %s", request
->url
);
544 strcpy(posn
, request
->lock
->token
);
546 slot
= get_active_slot();
547 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &request
->buffer
);
548 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, request
->buffer
.size
);
549 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
550 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
551 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
552 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
553 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
554 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
555 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
557 if (start_active_slot(slot
)) {
558 request
->slot
= slot
;
559 request
->state
= RUN_PUT
;
561 request
->state
= ABORTED
;
566 static void start_move(struct transfer_request
*request
)
568 struct active_request_slot
*slot
;
569 struct curl_slist
*dav_headers
= NULL
;
571 slot
= get_active_slot();
572 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1); /* undo PUT setup */
573 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MOVE
);
574 dav_headers
= curl_slist_append(dav_headers
, request
->dest
);
575 dav_headers
= curl_slist_append(dav_headers
, "Overwrite: T");
576 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
577 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
578 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
580 if (start_active_slot(slot
)) {
581 request
->slot
= slot
;
582 request
->state
= RUN_MOVE
;
584 request
->state
= ABORTED
;
589 int refresh_lock(struct active_lock
*lock
)
591 struct active_request_slot
*slot
;
593 char timeout_header
[25];
594 struct curl_slist
*dav_headers
= NULL
;
597 lock
->refreshing
= 1;
599 if_header
= xmalloc(strlen(lock
->token
) + 25);
600 sprintf(if_header
, "If: (<opaquelocktoken:%s>)", lock
->token
);
601 sprintf(timeout_header
, "Timeout: Second-%ld", lock
->timeout
);
602 dav_headers
= curl_slist_append(dav_headers
, if_header
);
603 dav_headers
= curl_slist_append(dav_headers
, timeout_header
);
605 slot
= get_active_slot();
606 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
607 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
608 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
609 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_LOCK
);
610 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
612 if (start_active_slot(slot
)) {
613 run_active_slot(slot
);
614 if (slot
->curl_result
!= CURLE_OK
) {
615 fprintf(stderr
, "Got HTTP error %ld\n", slot
->http_code
);
617 lock
->start_time
= time(NULL
);
622 lock
->refreshing
= 0;
623 curl_slist_free_all(dav_headers
);
629 static void finish_request(struct transfer_request
*request
)
631 time_t current_time
= time(NULL
);
634 request
->curl_result
= request
->slot
->curl_result
;
635 request
->http_code
= request
->slot
->http_code
;
636 request
->slot
= NULL
;
638 /* Refresh the lock if it is close to timing out */
639 time_remaining
= request
->lock
->start_time
+ request
->lock
->timeout
641 if (time_remaining
< LOCK_REFRESH
&& !request
->lock
->refreshing
) {
642 if (!refresh_lock(request
->lock
)) {
643 fprintf(stderr
, "Unable to refresh remote lock\n");
648 if (request
->headers
!= NULL
)
649 curl_slist_free_all(request
->headers
);
650 if (request
->state
== RUN_HEAD
) {
651 if (request
->http_code
== 404) {
652 request
->state
= NEED_PUSH
;
653 } else if (request
->curl_result
== CURLE_OK
) {
654 remote_dir_exists
[request
->sha1
[0]] = 1;
655 request
->state
= COMPLETE
;
657 fprintf(stderr
, "HEAD %s failed, aborting (%d/%ld)\n",
658 sha1_to_hex(request
->sha1
),
659 request
->curl_result
, request
->http_code
);
660 request
->state
= ABORTED
;
663 } else if (request
->state
== RUN_MKCOL
) {
664 if (request
->curl_result
== CURLE_OK
||
665 request
->http_code
== 405) {
666 remote_dir_exists
[request
->sha1
[0]] = 1;
669 fprintf(stderr
, "MKCOL %s failed, aborting (%d/%ld)\n",
670 sha1_to_hex(request
->sha1
),
671 request
->curl_result
, request
->http_code
);
672 request
->state
= ABORTED
;
675 } else if (request
->state
== RUN_PUT
) {
676 if (request
->curl_result
== CURLE_OK
) {
679 fprintf(stderr
, "PUT %s failed, aborting (%d/%ld)\n",
680 sha1_to_hex(request
->sha1
),
681 request
->curl_result
, request
->http_code
);
682 request
->state
= ABORTED
;
685 } else if (request
->state
== RUN_MOVE
) {
686 if (request
->curl_result
== CURLE_OK
) {
690 sha1_to_hex(request
->sha1
));
691 request
->state
= COMPLETE
;
693 fprintf(stderr
, "MOVE %s failed, aborting (%d/%ld)\n",
694 sha1_to_hex(request
->sha1
),
695 request
->curl_result
, request
->http_code
);
696 request
->state
= ABORTED
;
702 static void release_request(struct transfer_request
*request
)
704 struct transfer_request
*entry
= request_queue_head
;
706 if (request
== request_queue_head
) {
707 request_queue_head
= request
->next
;
709 while (entry
->next
!= NULL
&& entry
->next
!= request
)
711 if (entry
->next
== request
)
712 entry
->next
= entry
->next
->next
;
719 #ifdef USE_CURL_MULTI
720 void process_curl_messages(void)
723 struct active_request_slot
*slot
;
724 struct transfer_request
*request
= NULL
;
725 CURLMsg
*curl_message
= curl_multi_info_read(curlm
, &num_messages
);
727 while (curl_message
!= NULL
) {
728 if (curl_message
->msg
== CURLMSG_DONE
) {
729 slot
= active_queue_head
;
730 while (slot
!= NULL
&&
731 slot
->curl
!= curl_message
->easy_handle
)
734 curl_multi_remove_handle(curlm
, slot
->curl
);
738 slot
->curl_result
= curl_message
->data
.result
;
739 curl_easy_getinfo(slot
->curl
,
742 request
= request_queue_head
;
743 while (request
!= NULL
&&
744 request
->slot
!= slot
)
745 request
= request
->next
;
747 finish_request(request
);
749 fprintf(stderr
, "Received DONE message for unknown request!\n");
752 fprintf(stderr
, "Unknown CURL message received: %d\n",
753 (int)curl_message
->msg
);
755 curl_message
= curl_multi_info_read(curlm
, &num_messages
);
759 void process_request_queue(void)
761 struct transfer_request
*request
= request_queue_head
;
762 struct active_request_slot
*slot
= active_queue_head
;
768 while (active_requests
< max_requests
&& request
!= NULL
) {
769 if (!pushing
&& request
->state
== NEED_CHECK
) {
770 start_check(request
);
771 curl_multi_perform(curlm
, &num_transfers
);
772 } else if (pushing
&& request
->state
== NEED_PUSH
) {
773 if (remote_dir_exists
[request
->sha1
[0]])
776 start_mkcol(request
);
777 curl_multi_perform(curlm
, &num_transfers
);
779 request
= request
->next
;
782 while (slot
!= NULL
) {
783 if (!slot
->in_use
&& slot
->curl
!= NULL
) {
784 curl_easy_cleanup(slot
->curl
);
792 void process_waiting_requests(void)
794 struct active_request_slot
*slot
= active_queue_head
;
798 run_active_slot(slot
);
799 slot
= active_queue_head
;
805 void add_request(unsigned char *sha1
, struct active_lock
*lock
)
807 struct transfer_request
*request
= request_queue_head
;
808 struct packed_git
*target
;
810 while (request
!= NULL
&& memcmp(request
->sha1
, sha1
, 20))
811 request
= request
->next
;
815 target
= find_sha1_pack(sha1
, remote
->packs
);
819 request
= xmalloc(sizeof(*request
));
820 memcpy(request
->sha1
, sha1
, 20);
822 request
->lock
= lock
;
823 request
->headers
= NULL
;
824 request
->state
= NEED_CHECK
;
825 request
->next
= request_queue_head
;
826 request_queue_head
= request
;
827 #ifdef USE_CURL_MULTI
828 process_request_queue();
829 process_curl_messages();
833 static int fetch_index(unsigned char *sha1
)
835 char *hex
= sha1_to_hex(sha1
);
838 char tmpfile
[PATH_MAX
];
840 char range
[RANGE_HEADER_SIZE
];
841 struct curl_slist
*range_header
= NULL
;
844 struct active_request_slot
*slot
;
846 /* Don't use the index if the pack isn't there */
847 url
= xmalloc(strlen(remote
->url
) + 65);
848 sprintf(url
, "%s/objects/pack/pack-%s.pack", remote
->url
, hex
);
849 slot
= get_active_slot();
850 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
851 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
852 if (start_active_slot(slot
)) {
853 run_active_slot(slot
);
854 if (slot
->curl_result
!= CURLE_OK
) {
856 return error("Unable to verify pack %s is available",
860 return error("Unable to start request");
863 if (has_pack_index(sha1
))
867 fprintf(stderr
, "Getting index for pack %s\n", hex
);
869 sprintf(url
, "%s/objects/pack/pack-%s.idx", remote
->url
, hex
);
871 filename
= sha1_pack_index_name(sha1
);
872 snprintf(tmpfile
, sizeof(tmpfile
), "%s.temp", filename
);
873 indexfile
= fopen(tmpfile
, "a");
875 return error("Unable to open local file %s for pack index",
878 slot
= get_active_slot();
879 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
880 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
881 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, indexfile
);
882 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
883 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
884 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
885 slot
->local
= indexfile
;
887 /* If there is data present from a previous transfer attempt,
888 resume where it left off */
889 prev_posn
= ftell(indexfile
);
893 "Resuming fetch of index for pack %s at byte %ld\n",
895 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
896 range_header
= curl_slist_append(range_header
, range
);
897 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, range_header
);
900 if (start_active_slot(slot
)) {
901 run_active_slot(slot
);
902 if (slot
->curl_result
!= CURLE_OK
) {
905 return error("Unable to get pack index %s\n%s", url
,
910 return error("Unable to start request");
916 return move_temp_to_file(tmpfile
, filename
);
919 static int setup_index(unsigned char *sha1
)
921 struct packed_git
*new_pack
;
923 if (fetch_index(sha1
))
926 new_pack
= parse_pack_index(sha1
);
927 new_pack
->next
= remote
->packs
;
928 remote
->packs
= new_pack
;
932 static int fetch_indices()
934 unsigned char sha1
[20];
936 struct buffer buffer
;
940 struct active_request_slot
*slot
;
942 data
= xmalloc(4096);
943 memset(data
, 0, 4096);
946 buffer
.buffer
= data
;
949 fprintf(stderr
, "Getting pack list\n");
951 url
= xmalloc(strlen(remote
->url
) + 21);
952 sprintf(url
, "%s/objects/info/packs", remote
->url
);
954 slot
= get_active_slot();
955 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
956 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
957 fwrite_buffer_dynamic
);
958 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
959 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
960 if (start_active_slot(slot
)) {
961 run_active_slot(slot
);
962 if (slot
->curl_result
!= CURLE_OK
) {
965 if (slot
->http_code
== 404)
968 return error("%s", curl_errorstr
);
973 return error("Unable to start request");
977 data
= buffer
.buffer
;
978 while (i
< buffer
.posn
) {
982 if (i
+ 52 < buffer
.posn
&&
983 !strncmp(data
+ i
, " pack-", 6) &&
984 !strncmp(data
+ i
+ 46, ".pack\n", 6)) {
985 get_sha1_hex(data
+ i
+ 6, sha1
);
991 while (data
[i
] != '\n')
1001 static inline int needs_quote(int ch
)
1004 case '/': case '-': case '.':
1005 case 'A'...'Z': case 'a'...'z': case '0'...'9':
1012 static inline int hex(int v
)
1014 if (v
< 10) return '0' + v
;
1015 else return 'A' + v
- 10;
1018 static char *quote_ref_url(const char *base
, const char *ref
)
1022 int len
, baselen
, ch
;
1024 baselen
= strlen(base
);
1025 len
= baselen
+ 12; /* "refs/heads/" + NUL */
1026 for (cp
= ref
; (ch
= *cp
) != 0; cp
++, len
++)
1027 if (needs_quote(ch
))
1028 len
+= 2; /* extra two hex plus replacement % */
1029 qref
= xmalloc(len
);
1030 memcpy(qref
, base
, baselen
);
1031 memcpy(qref
+ baselen
, "refs/heads/", 11);
1032 for (cp
= ref
, dp
= qref
+ baselen
+ 11; (ch
= *cp
) != 0; cp
++) {
1033 if (needs_quote(ch
)) {
1035 *dp
++ = hex((ch
>> 4) & 0xF);
1036 *dp
++ = hex(ch
& 0xF);
1046 int fetch_ref(char *ref
, unsigned char *sha1
)
1050 struct buffer buffer
;
1051 char *base
= remote
->url
;
1052 struct active_request_slot
*slot
;
1055 buffer
.buffer
= hex
;
1058 url
= quote_ref_url(base
, ref
);
1059 slot
= get_active_slot();
1060 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
1061 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
1062 fwrite_buffer_dynamic
);
1063 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
1064 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1065 if (start_active_slot(slot
)) {
1066 run_active_slot(slot
);
1067 if (slot
->curl_result
!= CURLE_OK
)
1068 return error("Couldn't get %s for %s\n%s",
1069 url
, ref
, curl_errorstr
);
1071 return error("Unable to start request");
1075 get_sha1_hex(hex
, sha1
);
1080 start_activelock_element(void *userData
, const char *name
, const char **atts
)
1082 struct active_lock
*lock
= (struct active_lock
*)userData
;
1084 if (lock
->ctx_activelock
&& !strcmp(name
, "D:timeout"))
1085 lock
->ctx_timeout
= 1;
1086 else if (lock
->ctx_owner
&& strstr(name
, "href"))
1087 lock
->ctx_owner_href
= 1;
1088 else if (lock
->ctx_activelock
&& strstr(name
, "owner"))
1089 lock
->ctx_owner
= 1;
1090 else if (lock
->ctx_locktoken
&& !strcmp(name
, "D:href"))
1091 lock
->ctx_locktoken_href
= 1;
1092 else if (lock
->ctx_activelock
&& !strcmp(name
, "D:locktoken"))
1093 lock
->ctx_locktoken
= 1;
1094 else if (!strcmp(name
, "D:activelock"))
1095 lock
->ctx_activelock
= 1;
1099 end_activelock_element(void *userData
, const char *name
)
1101 struct active_lock
*lock
= (struct active_lock
*)userData
;
1103 if (lock
->ctx_timeout
&& !strcmp(name
, "D:timeout")) {
1104 lock
->ctx_timeout
= 0;
1105 } else if (lock
->ctx_owner_href
&& strstr(name
, "href")) {
1106 lock
->ctx_owner_href
= 0;
1107 } else if (lock
->ctx_owner
&& strstr(name
, "owner")) {
1108 lock
->ctx_owner
= 0;
1109 } else if (lock
->ctx_locktoken_href
&& !strcmp(name
, "D:href")) {
1110 lock
->ctx_locktoken_href
= 0;
1111 } else if (lock
->ctx_locktoken
&& !strcmp(name
, "D:locktoken")) {
1112 lock
->ctx_locktoken
= 0;
1113 } else if (lock
->ctx_activelock
&& !strcmp(name
, "D:activelock")) {
1114 lock
->ctx_activelock
= 0;
1119 activelock_cdata(void *userData
, const XML_Char
*s
, int len
)
1121 struct active_lock
*lock
= (struct active_lock
*)userData
;
1122 char *this = malloc(len
+1);
1123 strncpy(this, s
, len
);
1125 if (lock
->ctx_owner_href
) {
1126 lock
->owner
= malloc(len
+1);
1127 strcpy(lock
->owner
, this);
1128 } else if (lock
->ctx_locktoken_href
) {
1129 if (!strncmp(this, "opaquelocktoken:", 16)) {
1130 lock
->token
= malloc(len
-15);
1131 strcpy(lock
->token
, this+16);
1133 } else if (lock
->ctx_timeout
) {
1134 if (!strncmp(this, "Second-", 7))
1135 lock
->timeout
= strtol(this+7, NULL
, 10);
1142 start_lockprop_element(void *userData
, const char *name
, const char **atts
)
1144 struct lockprop
*prop
= (struct lockprop
*)userData
;
1146 if (prop
->lock_type
&& !strcmp(name
, "D:write")) {
1147 if (prop
->lock_exclusive
) {
1148 prop
->lock_exclusive_write
= 1;
1150 } else if (prop
->lock_scope
&& !strcmp(name
, "D:exclusive")) {
1151 prop
->lock_exclusive
= 1;
1152 } else if (prop
->lock_entry
) {
1153 if (!strcmp(name
, "D:lockscope")) {
1154 prop
->lock_scope
= 1;
1155 } else if (!strcmp(name
, "D:locktype")) {
1156 prop
->lock_type
= 1;
1158 } else if (prop
->supported_lock
) {
1159 if (!strcmp(name
, "D:lockentry")) {
1160 prop
->lock_entry
= 1;
1162 } else if (!strcmp(name
, "D:supportedlock")) {
1163 prop
->supported_lock
= 1;
1168 end_lockprop_element(void *userData
, const char *name
)
1170 struct lockprop
*prop
= (struct lockprop
*)userData
;
1172 if (!strcmp(name
, "D:lockentry")) {
1173 prop
->lock_entry
= 0;
1174 prop
->lock_scope
= 0;
1175 prop
->lock_type
= 0;
1176 prop
->lock_exclusive
= 0;
1177 } else if (!strcmp(name
, "D:supportedlock")) {
1178 prop
->supported_lock
= 0;
1182 struct active_lock
*lock_remote(char *file
, long timeout
)
1184 struct active_request_slot
*slot
;
1185 struct buffer out_buffer
;
1186 struct buffer in_buffer
;
1191 char timeout_header
[25];
1192 struct active_lock
*new_lock
;
1193 XML_Parser parser
= XML_ParserCreate(NULL
);
1194 enum XML_Status result
;
1195 struct curl_slist
*dav_headers
= NULL
;
1197 url
= xmalloc(strlen(remote
->url
) + strlen(file
) + 1);
1198 sprintf(url
, "%s%s", remote
->url
, file
);
1200 /* Make sure leading directories exist for the remote ref */
1201 ep
= strchr(url
+ strlen(remote
->url
) + 11, '/');
1204 slot
= get_active_slot();
1205 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
1206 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1207 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MKCOL
);
1208 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1209 if (start_active_slot(slot
)) {
1210 run_active_slot(slot
);
1211 if (slot
->curl_result
!= CURLE_OK
&&
1212 slot
->http_code
!= 405) {
1214 "Unable to create branch path %s\n",
1220 fprintf(stderr
, "Unable to start request\n");
1225 ep
= strchr(ep
+ 1, '/');
1228 out_buffer
.size
= strlen(LOCK_REQUEST
) + strlen(git_default_email
) - 2;
1229 out_data
= xmalloc(out_buffer
.size
+ 1);
1230 snprintf(out_data
, out_buffer
.size
+ 1, LOCK_REQUEST
, git_default_email
);
1231 out_buffer
.posn
= 0;
1232 out_buffer
.buffer
= out_data
;
1234 in_buffer
.size
= 4096;
1235 in_data
= xmalloc(in_buffer
.size
);
1237 in_buffer
.buffer
= in_data
;
1239 new_lock
= xmalloc(sizeof(*new_lock
));
1240 new_lock
->owner
= NULL
;
1241 new_lock
->token
= NULL
;
1242 new_lock
->timeout
= -1;
1243 new_lock
->refreshing
= 0;
1245 sprintf(timeout_header
, "Timeout: Second-%ld", timeout
);
1246 dav_headers
= curl_slist_append(dav_headers
, timeout_header
);
1247 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1249 slot
= get_active_slot();
1250 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1251 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.size
);
1252 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1253 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1254 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
1255 fwrite_buffer_dynamic
);
1256 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1257 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1258 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_LOCK
);
1259 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1261 if (start_active_slot(slot
)) {
1262 run_active_slot(slot
);
1263 if (slot
->curl_result
!= CURLE_OK
) {
1264 fprintf(stderr
, "Got HTTP error %ld\n", slot
->http_code
);
1276 fprintf(stderr
, "Unable to start request\n");
1282 XML_SetUserData(parser
, new_lock
);
1283 XML_SetElementHandler(parser
, start_activelock_element
,
1284 end_activelock_element
);
1285 XML_SetCharacterDataHandler(parser
, activelock_cdata
);
1286 result
= XML_Parse(parser
, in_buffer
.buffer
, in_buffer
.posn
, 1);
1288 if (result
!= XML_STATUS_OK
) {
1289 fprintf(stderr
, "%s", XML_ErrorString(
1290 XML_GetErrorCode(parser
)));
1296 if (new_lock
->token
== NULL
|| new_lock
->timeout
<= 0) {
1297 if (new_lock
->token
!= NULL
)
1298 free(new_lock
->token
);
1299 if (new_lock
->owner
!= NULL
)
1300 free(new_lock
->owner
);
1306 new_lock
->url
= url
;
1307 new_lock
->start_time
= time(NULL
);
1311 int unlock_remote(struct active_lock
*lock
)
1313 struct active_request_slot
*slot
;
1314 char *lock_token_header
;
1315 struct curl_slist
*dav_headers
= NULL
;
1318 lock_token_header
= xmalloc(strlen(lock
->token
) + 31);
1319 sprintf(lock_token_header
, "Lock-Token: <opaquelocktoken:%s>",
1321 dav_headers
= curl_slist_append(dav_headers
, lock_token_header
);
1323 slot
= get_active_slot();
1324 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1325 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
1326 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_UNLOCK
);
1327 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1329 if (start_active_slot(slot
)) {
1330 run_active_slot(slot
);
1331 if (slot
->curl_result
== CURLE_OK
)
1334 fprintf(stderr
, "Got HTTP error %ld\n",
1337 fprintf(stderr
, "Unable to start request\n");
1340 curl_slist_free_all(dav_headers
);
1341 free(lock_token_header
);
1343 if (lock
->owner
!= NULL
)
1354 struct active_request_slot
*slot
;
1355 struct buffer in_buffer
;
1356 struct buffer out_buffer
;
1359 XML_Parser parser
= XML_ParserCreate(NULL
);
1360 enum XML_Status result
;
1361 struct lockprop supported_lock
;
1362 struct curl_slist
*dav_headers
= NULL
;
1364 out_buffer
.size
= strlen(PROPFIND_REQUEST
) + strlen(remote
->url
) - 2;
1365 out_data
= xmalloc(out_buffer
.size
+ 1);
1366 snprintf(out_data
, out_buffer
.size
+ 1, PROPFIND_REQUEST
, remote
->url
);
1367 out_buffer
.posn
= 0;
1368 out_buffer
.buffer
= out_data
;
1370 in_buffer
.size
= 4096;
1371 in_data
= xmalloc(in_buffer
.size
);
1373 in_buffer
.buffer
= in_data
;
1375 dav_headers
= curl_slist_append(dav_headers
, "Depth: 0");
1376 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1378 slot
= get_active_slot();
1379 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1380 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.size
);
1381 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1382 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1383 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
1384 fwrite_buffer_dynamic
);
1385 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, remote
->url
);
1386 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1387 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PROPFIND
);
1388 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1390 if (start_active_slot(slot
)) {
1391 run_active_slot(slot
);
1393 if (slot
->curl_result
!= CURLE_OK
) {
1394 free(in_buffer
.buffer
);
1398 XML_SetUserData(parser
, &supported_lock
);
1399 XML_SetElementHandler(parser
, start_lockprop_element
,
1400 end_lockprop_element
);
1401 result
= XML_Parse(parser
, in_buffer
.buffer
, in_buffer
.posn
, 1);
1402 free(in_buffer
.buffer
);
1403 if (result
!= XML_STATUS_OK
)
1404 return error("%s", XML_ErrorString(
1405 XML_GetErrorCode(parser
)));
1408 free(in_buffer
.buffer
);
1409 return error("Unable to start request");
1412 if (supported_lock
.lock_exclusive_write
)
1418 int is_ancestor(unsigned char *sha1
, struct commit
*commit
)
1420 struct commit_list
*parents
;
1422 if (parse_commit(commit
))
1424 parents
= commit
->parents
;
1425 for (; parents
; parents
= parents
->next
) {
1426 if (!memcmp(sha1
, parents
->item
->object
.sha1
, 20)) {
1428 } else if (parents
->item
->object
.type
== commit_type
) {
1431 (struct commit
*)&parents
->item
->object
1439 void get_delta(unsigned char *sha1
, struct object
*obj
,
1440 struct active_lock
*lock
)
1442 struct commit
*commit
;
1443 struct commit_list
*parents
;
1445 struct tree_entry_list
*entry
;
1447 if (sha1
&& !memcmp(sha1
, obj
->sha1
, 20))
1453 if (obj
->type
== commit_type
) {
1455 fprintf(stderr
, "walk %s\n", sha1_to_hex(obj
->sha1
));
1456 add_request(obj
->sha1
, lock
);
1457 commit
= (struct commit
*)obj
;
1458 if (parse_commit(commit
)) {
1459 fprintf(stderr
, "Error parsing commit %s\n",
1460 sha1_to_hex(obj
->sha1
));
1464 parents
= commit
->parents
;
1465 for (; parents
; parents
= parents
->next
)
1467 memcmp(sha1
, parents
->item
->object
.sha1
, 20))
1468 get_delta(sha1
, &parents
->item
->object
,
1470 get_delta(sha1
, &commit
->tree
->object
, lock
);
1471 } else if (obj
->type
== tree_type
) {
1473 fprintf(stderr
, "walk %s\n", sha1_to_hex(obj
->sha1
));
1474 add_request(obj
->sha1
, lock
);
1475 tree
= (struct tree
*)obj
;
1476 if (parse_tree(tree
)) {
1477 fprintf(stderr
, "Error parsing tree %s\n",
1478 sha1_to_hex(obj
->sha1
));
1482 entry
= tree
->entries
;
1483 tree
->entries
= NULL
;
1485 struct tree_entry_list
*next
= entry
->next
;
1486 get_delta(sha1
, entry
->item
.any
, lock
);
1491 } else if (obj
->type
== blob_type
|| obj
->type
== tag_type
) {
1492 add_request(obj
->sha1
, lock
);
1496 int update_remote(unsigned char *sha1
, struct active_lock
*lock
)
1498 struct active_request_slot
*slot
;
1501 struct buffer out_buffer
;
1502 struct curl_slist
*dav_headers
= NULL
;
1505 if_header
= xmalloc(strlen(lock
->token
) + 25);
1506 sprintf(if_header
, "If: (<opaquelocktoken:%s>)", lock
->token
);
1507 dav_headers
= curl_slist_append(dav_headers
, if_header
);
1509 out_buffer
.size
= 41;
1510 out_data
= xmalloc(out_buffer
.size
+ 1);
1511 i
= snprintf(out_data
, out_buffer
.size
+ 1, "%s\n", sha1_to_hex(sha1
));
1512 if (i
!= out_buffer
.size
) {
1513 fprintf(stderr
, "Unable to initialize PUT request body\n");
1516 out_buffer
.posn
= 0;
1517 out_buffer
.buffer
= out_data
;
1519 slot
= get_active_slot();
1520 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1521 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.size
);
1522 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1523 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1524 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
1525 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1526 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1527 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
1528 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
1530 if (start_active_slot(slot
)) {
1531 run_active_slot(slot
);
1534 if (slot
->curl_result
!= CURLE_OK
) {
1536 "PUT error: curl result=%d, HTTP code=%ld\n",
1537 slot
->curl_result
, slot
->http_code
);
1538 /* We should attempt recovery? */
1544 fprintf(stderr
, "Unable to start PUT request\n");
1551 int main(int argc
, char **argv
)
1553 struct active_request_slot
*slot
;
1554 struct active_request_slot
*next_slot
;
1555 struct transfer_request
*request
;
1556 struct transfer_request
*next_request
;
1558 char **refspec
= NULL
;
1559 int do_remote_update
;
1563 unsigned char local_sha1
[20];
1564 struct object
*local_object
= NULL
;
1565 char *remote_ref
= NULL
;
1566 unsigned char remote_sha1
[20];
1567 struct active_lock
*remote_lock
;
1568 char *remote_path
= NULL
;
1569 char *low_speed_limit
;
1570 char *low_speed_time
;
1576 remote
= xmalloc(sizeof(*remote
));
1578 remote
->packs
= NULL
;
1581 for (i
= 1; i
< argc
; i
++, argv
++) {
1585 if (!strcmp(arg
, "--complete")) {
1589 if (!strcmp(arg
, "--force")) {
1593 if (!strcmp(arg
, "--verbose")) {
1597 usage(http_push_usage
);
1604 nr_refspec
= argc
- i
;
1608 memset(remote_dir_exists
, 0, 256);
1610 curl_global_init(CURL_GLOBAL_ALL
);
1612 #ifdef USE_CURL_MULTI
1614 char *http_max_requests
= getenv("GIT_HTTP_MAX_REQUESTS");
1615 if (http_max_requests
!= NULL
)
1616 max_requests
= atoi(http_max_requests
);
1619 curlm
= curl_multi_init();
1620 if (curlm
== NULL
) {
1621 fprintf(stderr
, "Error creating curl multi handle.\n");
1626 if (getenv("GIT_SSL_NO_VERIFY"))
1627 curl_ssl_verify
= 0;
1629 ssl_cert
= getenv("GIT_SSL_CERT");
1630 #if LIBCURL_VERSION_NUM >= 0x070902
1631 ssl_key
= getenv("GIT_SSL_KEY");
1633 #if LIBCURL_VERSION_NUM >= 0x070908
1634 ssl_capath
= getenv("GIT_SSL_CAPATH");
1636 ssl_cainfo
= getenv("GIT_SSL_CAINFO");
1638 low_speed_limit
= getenv("GIT_HTTP_LOW_SPEED_LIMIT");
1639 if (low_speed_limit
!= NULL
)
1640 curl_low_speed_limit
= strtol(low_speed_limit
, NULL
, 10);
1641 low_speed_time
= getenv("GIT_HTTP_LOW_SPEED_TIME");
1642 if (low_speed_time
!= NULL
)
1643 curl_low_speed_time
= strtol(low_speed_time
, NULL
, 10);
1645 git_config(http_options
);
1647 if (curl_ssl_verify
== -1)
1648 curl_ssl_verify
= 1;
1650 #ifdef USE_CURL_MULTI
1651 if (max_requests
< 1)
1652 max_requests
= DEFAULT_MAX_REQUESTS
;
1655 no_pragma_header
= curl_slist_append(no_pragma_header
, "Pragma:");
1656 default_headers
= curl_slist_append(default_headers
, "Range:");
1657 default_headers
= curl_slist_append(default_headers
, "Destination:");
1658 default_headers
= curl_slist_append(default_headers
, "If:");
1659 default_headers
= curl_slist_append(default_headers
,
1660 "Pragma: no-cache");
1662 #ifndef NO_CURL_EASY_DUPHANDLE
1663 curl_default
= get_curl_handle();
1666 /* Verify DAV compliance/lock support */
1667 if (check_locking() != 0) {
1668 fprintf(stderr
, "Error: no DAV locking support on remote repo %s\n", remote
->url
);
1673 /* Process each refspec */
1674 for (i
= 0; i
< nr_refspec
; i
++) {
1677 do_remote_update
= 0;
1679 local_ref
= refspec
[i
];
1680 if (*local_ref
== '+') {
1684 ep
= strchr(local_ref
, ':');
1686 remote_ref
= ep
+ 1;
1690 remote_ref
= local_ref
;
1692 /* Lock remote branch ref */
1695 remote_path
= xmalloc(strlen(remote_ref
) + 12);
1696 sprintf(remote_path
, "refs/heads/%s", remote_ref
);
1697 remote_lock
= lock_remote(remote_path
, LOCK_TIME
);
1698 if (remote_lock
== NULL
) {
1699 fprintf(stderr
, "Unable to lock remote branch %s\n",
1705 /* Resolve local and remote refs */
1706 if (fetch_ref(remote_ref
, remote_sha1
) != 0) {
1708 "Remote branch %s does not exist on %s\n",
1709 remote_ref
, remote
->url
);
1712 if (get_sha1(local_ref
, local_sha1
) != 0) {
1713 fprintf(stderr
, "Error resolving local branch %s\n",
1719 /* Find relationship between local and remote */
1720 local_object
= parse_object(local_sha1
);
1721 if (!local_object
) {
1722 fprintf(stderr
, "Unable to parse local object %s\n",
1723 sha1_to_hex(local_sha1
));
1726 } else if (new_branch
) {
1727 do_remote_update
= 1;
1729 if (!memcmp(local_sha1
, remote_sha1
, 20)) {
1731 "* %s: same as branch '%s' of %s\n",
1732 local_ref
, remote_ref
, remote
->url
);
1733 } else if (is_ancestor(remote_sha1
,
1734 (struct commit
*)local_object
)) {
1736 "Remote %s will fast-forward to local %s\n",
1737 remote_ref
, local_ref
);
1738 do_remote_update
= 1;
1739 } else if (force_all
|| force_this
) {
1741 "* %s on %s does not fast forward to local branch '%s', overwriting\n",
1742 remote_ref
, remote
->url
, local_ref
);
1743 do_remote_update
= 1;
1746 "* %s on %s does not fast forward to local branch '%s'\n",
1747 remote_ref
, remote
->url
, local_ref
);
1753 /* Generate and check list of required objects */
1755 if (do_remote_update
|| push_all
)
1757 get_delta(push_all
? NULL
: remote_sha1
,
1758 local_object
, remote_lock
);
1759 process_waiting_requests();
1761 /* Push missing objects to remote, this would be a
1762 convenient time to pack them first if appropriate. */
1764 process_request_queue();
1765 process_waiting_requests();
1767 /* Update the remote branch if all went well */
1768 if (do_remote_update
) {
1769 if (!aborted
&& update_remote(local_sha1
,
1771 fprintf(stderr
, "%s remote branch %s\n",
1772 new_branch
? "Created" : "Updated",
1776 "Unable to %s remote branch %s\n",
1777 new_branch
? "create" : "update",
1785 unlock_remote(remote_lock
);
1792 curl_slist_free_all(no_pragma_header
);
1793 curl_slist_free_all(default_headers
);
1795 slot
= active_queue_head
;
1796 while (slot
!= NULL
) {
1797 next_slot
= slot
->next
;
1798 if (slot
->curl
!= NULL
)
1799 curl_easy_cleanup(slot
->curl
);
1804 request
= request_queue_head
;
1805 while (request
!= NULL
) {
1806 next_request
= request
->next
;
1807 release_request(request
);
1808 request
= next_request
;
1811 #ifndef NO_CURL_EASY_DUPHANDLE
1812 curl_easy_cleanup(curl_default
);
1814 #ifdef USE_CURL_MULTI
1815 curl_multi_cleanup(curlm
);
1817 curl_global_cleanup();