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
36 #define XML_STATUS_OK 1
37 #define XML_STATUS_ERROR 0
40 #define RANGE_HEADER_SIZE 30
42 /* DAV method names and request body templates */
43 #define DAV_LOCK "LOCK"
44 #define DAV_MKCOL "MKCOL"
45 #define DAV_MOVE "MOVE"
46 #define DAV_PROPFIND "PROPFIND"
48 #define DAV_UNLOCK "UNLOCK"
49 #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>"
50 #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>"
53 #define LOCK_REFRESH 30
55 static int active_requests
= 0;
56 static int data_received
;
57 static int pushing
= 0;
58 static int aborted
= 0;
59 static char remote_dir_exists
[256];
62 static int max_requests
= -1;
65 #ifndef NO_CURL_EASY_DUPHANDLE
66 static CURL
*curl_default
;
68 static struct curl_slist
*no_pragma_header
;
69 static struct curl_slist
*default_headers
;
70 static char curl_errorstr
[CURL_ERROR_SIZE
];
72 static int push_verbosely
= 0;
73 static int push_all
= 0;
74 static int force_all
= 0;
86 struct packed_git
*packs
;
89 static struct repo
*remote
= NULL
;
102 struct transfer_request
104 unsigned char sha1
[20];
107 struct active_lock
*lock
;
108 struct curl_slist
*headers
;
109 struct buffer buffer
;
110 char filename
[PATH_MAX
];
111 char tmpfile
[PATH_MAX
];
112 enum transfer_state state
;
113 CURLcode curl_result
;
114 char errorstr
[CURL_ERROR_SIZE
];
116 unsigned char real_sha1
[20];
121 struct active_request_slot
*slot
;
122 struct transfer_request
*next
;
125 struct active_request_slot
131 CURLcode curl_result
;
133 struct active_request_slot
*next
;
136 static struct transfer_request
*request_queue_head
= NULL
;
137 static struct active_request_slot
*active_queue_head
= NULL
;
139 static int curl_ssl_verify
= -1;
140 static char *ssl_cert
= NULL
;
141 #if LIBCURL_VERSION_NUM >= 0x070902
142 static char *ssl_key
= NULL
;
144 #if LIBCURL_VERSION_NUM >= 0x070908
145 static char *ssl_capath
= NULL
;
147 static char *ssl_cainfo
= NULL
;
148 static long curl_low_speed_limit
= -1;
149 static long curl_low_speed_time
= -1;
158 int ctx_locktoken_href
;
174 int lock_exclusive_write
;
177 static int http_options(const char *var
, const char *value
)
179 if (!strcmp("http.sslverify", var
)) {
180 if (curl_ssl_verify
== -1) {
181 curl_ssl_verify
= git_config_bool(var
, value
);
186 if (!strcmp("http.sslcert", var
)) {
187 if (ssl_cert
== NULL
) {
188 ssl_cert
= xmalloc(strlen(value
)+1);
189 strcpy(ssl_cert
, value
);
193 #if LIBCURL_VERSION_NUM >= 0x070902
194 if (!strcmp("http.sslkey", var
)) {
195 if (ssl_key
== NULL
) {
196 ssl_key
= xmalloc(strlen(value
)+1);
197 strcpy(ssl_key
, value
);
202 #if LIBCURL_VERSION_NUM >= 0x070908
203 if (!strcmp("http.sslcapath", var
)) {
204 if (ssl_capath
== NULL
) {
205 ssl_capath
= xmalloc(strlen(value
)+1);
206 strcpy(ssl_capath
, value
);
211 if (!strcmp("http.sslcainfo", var
)) {
212 if (ssl_cainfo
== NULL
) {
213 ssl_cainfo
= xmalloc(strlen(value
)+1);
214 strcpy(ssl_cainfo
, value
);
219 #ifdef USE_CURL_MULTI
220 if (!strcmp("http.maxrequests", var
)) {
221 if (max_requests
== -1)
222 max_requests
= git_config_int(var
, value
);
227 if (!strcmp("http.lowspeedlimit", var
)) {
228 if (curl_low_speed_limit
== -1)
229 curl_low_speed_limit
= (long)git_config_int(var
, value
);
232 if (!strcmp("http.lowspeedtime", var
)) {
233 if (curl_low_speed_time
== -1)
234 curl_low_speed_time
= (long)git_config_int(var
, value
);
238 /* Fall back on the default ones */
239 return git_default_config(var
, value
);
242 static size_t fread_buffer(void *ptr
, size_t eltsize
, size_t nmemb
,
243 struct buffer
*buffer
)
245 size_t size
= eltsize
* nmemb
;
246 if (size
> buffer
->size
- buffer
->posn
)
247 size
= buffer
->size
- buffer
->posn
;
248 memcpy(ptr
, buffer
->buffer
+ buffer
->posn
, size
);
249 buffer
->posn
+= size
;
253 static size_t fwrite_buffer_dynamic(const void *ptr
, size_t eltsize
,
254 size_t nmemb
, struct buffer
*buffer
)
256 size_t size
= eltsize
* nmemb
;
257 if (size
> buffer
->size
- buffer
->posn
) {
258 buffer
->size
= buffer
->size
* 3 / 2;
259 if (buffer
->size
< buffer
->posn
+ size
)
260 buffer
->size
= buffer
->posn
+ size
;
261 buffer
->buffer
= xrealloc(buffer
->buffer
, buffer
->size
);
263 memcpy(buffer
->buffer
+ buffer
->posn
, ptr
, size
);
264 buffer
->posn
+= size
;
269 static size_t fwrite_null(const void *ptr
, size_t eltsize
,
270 size_t nmemb
, struct buffer
*buffer
)
273 return eltsize
* nmemb
;
276 #ifdef USE_CURL_MULTI
277 static void process_curl_messages(void);
278 static void process_request_queue(void);
281 static CURL
* get_curl_handle(void)
283 CURL
* result
= curl_easy_init();
285 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, curl_ssl_verify
);
286 #if LIBCURL_VERSION_NUM >= 0x070907
287 curl_easy_setopt(result
, CURLOPT_NETRC
, CURL_NETRC_OPTIONAL
);
290 if (ssl_cert
!= NULL
)
291 curl_easy_setopt(result
, CURLOPT_SSLCERT
, ssl_cert
);
292 #if LIBCURL_VERSION_NUM >= 0x070902
294 curl_easy_setopt(result
, CURLOPT_SSLKEY
, ssl_key
);
296 #if LIBCURL_VERSION_NUM >= 0x070908
297 if (ssl_capath
!= NULL
)
298 curl_easy_setopt(result
, CURLOPT_CAPATH
, ssl_capath
);
300 if (ssl_cainfo
!= NULL
)
301 curl_easy_setopt(result
, CURLOPT_CAINFO
, ssl_cainfo
);
302 curl_easy_setopt(result
, CURLOPT_FAILONERROR
, 1);
304 if (curl_low_speed_limit
> 0 && curl_low_speed_time
> 0) {
305 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_LIMIT
,
306 curl_low_speed_limit
);
307 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_TIME
,
308 curl_low_speed_time
);
314 static struct active_request_slot
*get_active_slot(void)
316 struct active_request_slot
*slot
= active_queue_head
;
317 struct active_request_slot
*newslot
;
319 #ifdef USE_CURL_MULTI
322 /* Wait for a slot to open up if the queue is full */
323 while (active_requests
>= max_requests
) {
324 curl_multi_perform(curlm
, &num_transfers
);
325 if (num_transfers
< active_requests
) {
326 process_curl_messages();
331 while (slot
!= NULL
&& slot
->in_use
) {
335 newslot
= xmalloc(sizeof(*newslot
));
336 newslot
->curl
= NULL
;
338 newslot
->next
= NULL
;
340 slot
= active_queue_head
;
342 active_queue_head
= newslot
;
344 while (slot
->next
!= NULL
) {
347 slot
->next
= newslot
;
352 if (slot
->curl
== NULL
) {
353 #ifdef NO_CURL_EASY_DUPHANDLE
354 slot
->curl
= get_curl_handle();
356 slot
->curl
= curl_easy_duphandle(curl_default
);
364 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, default_headers
);
365 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
370 static int start_active_slot(struct active_request_slot
*slot
)
372 #ifdef USE_CURL_MULTI
373 CURLMcode curlm_result
= curl_multi_add_handle(curlm
, slot
->curl
);
375 if (curlm_result
!= CURLM_OK
&&
376 curlm_result
!= CURLM_CALL_MULTI_PERFORM
) {
385 static void run_active_slot(struct active_request_slot
*slot
)
387 #ifdef USE_CURL_MULTI
395 struct timeval select_timeout
;
396 CURLMcode curlm_result
;
398 while (!slot
->done
) {
401 curlm_result
= curl_multi_perform(curlm
,
403 } while (curlm_result
== CURLM_CALL_MULTI_PERFORM
);
404 if (num_transfers
< active_requests
) {
405 process_curl_messages();
406 process_request_queue();
409 if (!data_received
&& slot
->local
!= NULL
) {
410 current_pos
= ftell(slot
->local
);
411 if (current_pos
> last_pos
)
413 last_pos
= current_pos
;
416 if (!slot
->done
&& !data_received
) {
421 select_timeout
.tv_sec
= 0;
422 select_timeout
.tv_usec
= 50000;
423 select(max_fd
, &readfds
, &writefds
,
424 &excfds
, &select_timeout
);
428 slot
->curl_result
= curl_easy_perform(slot
->curl
);
433 static void start_check(struct transfer_request
*request
)
435 char *hex
= sha1_to_hex(request
->sha1
);
436 struct active_request_slot
*slot
;
439 request
->url
= xmalloc(strlen(remote
->url
) + 55);
440 strcpy(request
->url
, remote
->url
);
441 posn
= request
->url
+ strlen(remote
->url
);
442 strcpy(posn
, "objects/");
444 memcpy(posn
, hex
, 2);
447 strcpy(posn
, hex
+ 2);
449 slot
= get_active_slot();
450 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
451 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
452 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
454 if (start_active_slot(slot
)) {
455 request
->slot
= slot
;
456 request
->state
= RUN_HEAD
;
458 request
->state
= ABORTED
;
463 static void start_mkcol(struct transfer_request
*request
)
465 char *hex
= sha1_to_hex(request
->sha1
);
466 struct active_request_slot
*slot
;
469 request
->url
= xmalloc(strlen(remote
->url
) + 13);
470 strcpy(request
->url
, remote
->url
);
471 posn
= request
->url
+ strlen(remote
->url
);
472 strcpy(posn
, "objects/");
474 memcpy(posn
, hex
, 2);
478 slot
= get_active_slot();
479 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1); /* undo PUT setup */
480 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
481 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
482 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MKCOL
);
483 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
485 if (start_active_slot(slot
)) {
486 request
->slot
= slot
;
487 request
->state
= RUN_MKCOL
;
489 request
->state
= ABORTED
;
494 static void start_put(struct transfer_request
*request
)
496 char *hex
= sha1_to_hex(request
->sha1
);
497 struct active_request_slot
*slot
;
507 unpacked
= read_sha1_file(request
->sha1
, type
, &len
);
508 hdrlen
= sprintf(hdr
, "%s %lu", type
, len
) + 1;
511 memset(&stream
, 0, sizeof(stream
));
512 deflateInit(&stream
, Z_BEST_COMPRESSION
);
513 size
= deflateBound(&stream
, len
+ hdrlen
);
514 request
->buffer
.buffer
= xmalloc(size
);
517 stream
.next_out
= request
->buffer
.buffer
;
518 stream
.avail_out
= size
;
521 stream
.next_in
= (void *)hdr
;
522 stream
.avail_in
= hdrlen
;
523 while (deflate(&stream
, 0) == Z_OK
)
526 /* Then the data itself.. */
527 stream
.next_in
= unpacked
;
528 stream
.avail_in
= len
;
529 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
534 request
->buffer
.size
= stream
.total_out
;
535 request
->buffer
.posn
= 0;
537 if (request
->url
!= NULL
)
539 request
->url
= xmalloc(strlen(remote
->url
) +
540 strlen(request
->lock
->token
) + 51);
541 strcpy(request
->url
, remote
->url
);
542 posn
= request
->url
+ strlen(remote
->url
);
543 strcpy(posn
, "objects/");
545 memcpy(posn
, hex
, 2);
548 strcpy(posn
, hex
+ 2);
549 request
->dest
= xmalloc(strlen(request
->url
) + 14);
550 sprintf(request
->dest
, "Destination: %s", request
->url
);
553 strcpy(posn
, request
->lock
->token
);
555 slot
= get_active_slot();
556 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &request
->buffer
);
557 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, request
->buffer
.size
);
558 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
559 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
560 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
561 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
562 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
563 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
564 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
566 if (start_active_slot(slot
)) {
567 request
->slot
= slot
;
568 request
->state
= RUN_PUT
;
570 request
->state
= ABORTED
;
575 static void start_move(struct transfer_request
*request
)
577 struct active_request_slot
*slot
;
578 struct curl_slist
*dav_headers
= NULL
;
580 slot
= get_active_slot();
581 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1); /* undo PUT setup */
582 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MOVE
);
583 dav_headers
= curl_slist_append(dav_headers
, request
->dest
);
584 dav_headers
= curl_slist_append(dav_headers
, "Overwrite: T");
585 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
586 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
587 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
589 if (start_active_slot(slot
)) {
590 request
->slot
= slot
;
591 request
->state
= RUN_MOVE
;
593 request
->state
= ABORTED
;
598 static int refresh_lock(struct active_lock
*lock
)
600 struct active_request_slot
*slot
;
602 char timeout_header
[25];
603 struct curl_slist
*dav_headers
= NULL
;
606 lock
->refreshing
= 1;
608 if_header
= xmalloc(strlen(lock
->token
) + 25);
609 sprintf(if_header
, "If: (<opaquelocktoken:%s>)", lock
->token
);
610 sprintf(timeout_header
, "Timeout: Second-%ld", lock
->timeout
);
611 dav_headers
= curl_slist_append(dav_headers
, if_header
);
612 dav_headers
= curl_slist_append(dav_headers
, timeout_header
);
614 slot
= get_active_slot();
615 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
616 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
617 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
618 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_LOCK
);
619 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
621 if (start_active_slot(slot
)) {
622 run_active_slot(slot
);
623 if (slot
->curl_result
!= CURLE_OK
) {
624 fprintf(stderr
, "Got HTTP error %ld\n", slot
->http_code
);
626 lock
->start_time
= time(NULL
);
631 lock
->refreshing
= 0;
632 curl_slist_free_all(dav_headers
);
638 static void finish_request(struct transfer_request
*request
)
640 time_t current_time
= time(NULL
);
643 request
->curl_result
= request
->slot
->curl_result
;
644 request
->http_code
= request
->slot
->http_code
;
645 request
->slot
= NULL
;
647 /* Refresh the lock if it is close to timing out */
648 time_remaining
= request
->lock
->start_time
+ request
->lock
->timeout
650 if (time_remaining
< LOCK_REFRESH
&& !request
->lock
->refreshing
) {
651 if (!refresh_lock(request
->lock
)) {
652 fprintf(stderr
, "Unable to refresh remote lock\n");
657 if (request
->headers
!= NULL
)
658 curl_slist_free_all(request
->headers
);
659 if (request
->state
== RUN_HEAD
) {
660 if (request
->http_code
== 404) {
661 request
->state
= NEED_PUSH
;
662 } else if (request
->curl_result
== CURLE_OK
) {
663 remote_dir_exists
[request
->sha1
[0]] = 1;
664 request
->state
= COMPLETE
;
666 fprintf(stderr
, "HEAD %s failed, aborting (%d/%ld)\n",
667 sha1_to_hex(request
->sha1
),
668 request
->curl_result
, request
->http_code
);
669 request
->state
= ABORTED
;
672 } else if (request
->state
== RUN_MKCOL
) {
673 if (request
->curl_result
== CURLE_OK
||
674 request
->http_code
== 405) {
675 remote_dir_exists
[request
->sha1
[0]] = 1;
678 fprintf(stderr
, "MKCOL %s failed, aborting (%d/%ld)\n",
679 sha1_to_hex(request
->sha1
),
680 request
->curl_result
, request
->http_code
);
681 request
->state
= ABORTED
;
684 } else if (request
->state
== RUN_PUT
) {
685 if (request
->curl_result
== CURLE_OK
) {
688 fprintf(stderr
, "PUT %s failed, aborting (%d/%ld)\n",
689 sha1_to_hex(request
->sha1
),
690 request
->curl_result
, request
->http_code
);
691 request
->state
= ABORTED
;
694 } else if (request
->state
== RUN_MOVE
) {
695 if (request
->curl_result
== CURLE_OK
) {
699 sha1_to_hex(request
->sha1
));
700 request
->state
= COMPLETE
;
702 fprintf(stderr
, "MOVE %s failed, aborting (%d/%ld)\n",
703 sha1_to_hex(request
->sha1
),
704 request
->curl_result
, request
->http_code
);
705 request
->state
= ABORTED
;
711 static void release_request(struct transfer_request
*request
)
713 struct transfer_request
*entry
= request_queue_head
;
715 if (request
== request_queue_head
) {
716 request_queue_head
= request
->next
;
718 while (entry
->next
!= NULL
&& entry
->next
!= request
)
720 if (entry
->next
== request
)
721 entry
->next
= entry
->next
->next
;
728 #ifdef USE_CURL_MULTI
729 static void process_curl_messages(void)
732 struct active_request_slot
*slot
;
733 struct transfer_request
*request
= NULL
;
734 CURLMsg
*curl_message
= curl_multi_info_read(curlm
, &num_messages
);
736 while (curl_message
!= NULL
) {
737 if (curl_message
->msg
== CURLMSG_DONE
) {
738 slot
= active_queue_head
;
739 while (slot
!= NULL
&&
740 slot
->curl
!= curl_message
->easy_handle
)
743 int curl_result
= curl_message
->data
.result
;
744 curl_multi_remove_handle(curlm
, slot
->curl
);
748 slot
->curl_result
= curl_result
;
749 curl_easy_getinfo(slot
->curl
,
752 request
= request_queue_head
;
753 while (request
!= NULL
&&
754 request
->slot
!= slot
)
755 request
= request
->next
;
757 finish_request(request
);
759 fprintf(stderr
, "Received DONE message for unknown request!\n");
762 fprintf(stderr
, "Unknown CURL message received: %d\n",
763 (int)curl_message
->msg
);
765 curl_message
= curl_multi_info_read(curlm
, &num_messages
);
769 static void process_request_queue(void)
771 struct transfer_request
*request
= request_queue_head
;
772 struct active_request_slot
*slot
= active_queue_head
;
778 while (active_requests
< max_requests
&& request
!= NULL
) {
779 if (!pushing
&& request
->state
== NEED_CHECK
) {
780 start_check(request
);
781 curl_multi_perform(curlm
, &num_transfers
);
782 } else if (pushing
&& request
->state
== NEED_PUSH
) {
783 if (remote_dir_exists
[request
->sha1
[0]])
786 start_mkcol(request
);
787 curl_multi_perform(curlm
, &num_transfers
);
789 request
= request
->next
;
792 while (slot
!= NULL
) {
793 if (!slot
->in_use
&& slot
->curl
!= NULL
) {
794 curl_easy_cleanup(slot
->curl
);
802 static void process_waiting_requests(void)
804 struct active_request_slot
*slot
= active_queue_head
;
808 run_active_slot(slot
);
809 slot
= active_queue_head
;
815 static void add_request(unsigned char *sha1
, struct active_lock
*lock
)
817 struct transfer_request
*request
= request_queue_head
;
818 struct packed_git
*target
;
820 while (request
!= NULL
&& memcmp(request
->sha1
, sha1
, 20))
821 request
= request
->next
;
825 target
= find_sha1_pack(sha1
, remote
->packs
);
829 request
= xmalloc(sizeof(*request
));
830 memcpy(request
->sha1
, sha1
, 20);
832 request
->lock
= lock
;
833 request
->headers
= NULL
;
834 request
->state
= NEED_CHECK
;
835 request
->next
= request_queue_head
;
836 request_queue_head
= request
;
837 #ifdef USE_CURL_MULTI
838 process_request_queue();
839 process_curl_messages();
843 static int fetch_index(unsigned char *sha1
)
845 char *hex
= sha1_to_hex(sha1
);
848 char tmpfile
[PATH_MAX
];
850 char range
[RANGE_HEADER_SIZE
];
851 struct curl_slist
*range_header
= NULL
;
854 struct active_request_slot
*slot
;
856 /* Don't use the index if the pack isn't there */
857 url
= xmalloc(strlen(remote
->url
) + 65);
858 sprintf(url
, "%s/objects/pack/pack-%s.pack", remote
->url
, hex
);
859 slot
= get_active_slot();
860 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
861 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
862 if (start_active_slot(slot
)) {
863 run_active_slot(slot
);
864 if (slot
->curl_result
!= CURLE_OK
) {
866 return error("Unable to verify pack %s is available",
870 return error("Unable to start request");
873 if (has_pack_index(sha1
))
877 fprintf(stderr
, "Getting index for pack %s\n", hex
);
879 sprintf(url
, "%s/objects/pack/pack-%s.idx", remote
->url
, hex
);
881 filename
= sha1_pack_index_name(sha1
);
882 snprintf(tmpfile
, sizeof(tmpfile
), "%s.temp", filename
);
883 indexfile
= fopen(tmpfile
, "a");
885 return error("Unable to open local file %s for pack index",
888 slot
= get_active_slot();
889 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
890 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
891 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, indexfile
);
892 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
893 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
894 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
895 slot
->local
= indexfile
;
897 /* If there is data present from a previous transfer attempt,
898 resume where it left off */
899 prev_posn
= ftell(indexfile
);
903 "Resuming fetch of index for pack %s at byte %ld\n",
905 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
906 range_header
= curl_slist_append(range_header
, range
);
907 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, range_header
);
910 if (start_active_slot(slot
)) {
911 run_active_slot(slot
);
912 if (slot
->curl_result
!= CURLE_OK
) {
915 return error("Unable to get pack index %s\n%s", url
,
920 return error("Unable to start request");
926 return move_temp_to_file(tmpfile
, filename
);
929 static int setup_index(unsigned char *sha1
)
931 struct packed_git
*new_pack
;
933 if (fetch_index(sha1
))
936 new_pack
= parse_pack_index(sha1
);
937 new_pack
->next
= remote
->packs
;
938 remote
->packs
= new_pack
;
942 static int fetch_indices(void)
944 unsigned char sha1
[20];
946 struct buffer buffer
;
950 struct active_request_slot
*slot
;
952 data
= xmalloc(4096);
953 memset(data
, 0, 4096);
956 buffer
.buffer
= data
;
959 fprintf(stderr
, "Getting pack list\n");
961 url
= xmalloc(strlen(remote
->url
) + 21);
962 sprintf(url
, "%s/objects/info/packs", remote
->url
);
964 slot
= get_active_slot();
965 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
966 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
967 fwrite_buffer_dynamic
);
968 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
969 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
970 if (start_active_slot(slot
)) {
971 run_active_slot(slot
);
972 if (slot
->curl_result
!= CURLE_OK
) {
975 if (slot
->http_code
== 404)
978 return error("%s", curl_errorstr
);
983 return error("Unable to start request");
987 data
= buffer
.buffer
;
988 while (i
< buffer
.posn
) {
992 if (i
+ 52 < buffer
.posn
&&
993 !strncmp(data
+ i
, " pack-", 6) &&
994 !strncmp(data
+ i
+ 46, ".pack\n", 6)) {
995 get_sha1_hex(data
+ i
+ 6, sha1
);
1001 while (data
[i
] != '\n')
1007 free(buffer
.buffer
);
1011 static inline int needs_quote(int ch
)
1014 case '/': case '-': case '.':
1015 case 'A'...'Z': case 'a'...'z': case '0'...'9':
1022 static inline int hex(int v
)
1024 if (v
< 10) return '0' + v
;
1025 else return 'A' + v
- 10;
1028 static char *quote_ref_url(const char *base
, const char *ref
)
1032 int len
, baselen
, ch
;
1034 baselen
= strlen(base
);
1035 len
= baselen
+ 12; /* "refs/heads/" + NUL */
1036 for (cp
= ref
; (ch
= *cp
) != 0; cp
++, len
++)
1037 if (needs_quote(ch
))
1038 len
+= 2; /* extra two hex plus replacement % */
1039 qref
= xmalloc(len
);
1040 memcpy(qref
, base
, baselen
);
1041 memcpy(qref
+ baselen
, "refs/heads/", 11);
1042 for (cp
= ref
, dp
= qref
+ baselen
+ 11; (ch
= *cp
) != 0; cp
++) {
1043 if (needs_quote(ch
)) {
1045 *dp
++ = hex((ch
>> 4) & 0xF);
1046 *dp
++ = hex(ch
& 0xF);
1056 int fetch_ref(char *ref
, unsigned char *sha1
)
1060 struct buffer buffer
;
1061 char *base
= remote
->url
;
1062 struct active_request_slot
*slot
;
1065 buffer
.buffer
= hex
;
1068 url
= quote_ref_url(base
, ref
);
1069 slot
= get_active_slot();
1070 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
1071 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
1072 fwrite_buffer_dynamic
);
1073 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
1074 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1075 if (start_active_slot(slot
)) {
1076 run_active_slot(slot
);
1077 if (slot
->curl_result
!= CURLE_OK
)
1078 return error("Couldn't get %s for %s\n%s",
1079 url
, ref
, curl_errorstr
);
1081 return error("Unable to start request");
1085 get_sha1_hex(hex
, sha1
);
1090 start_activelock_element(void *userData
, const char *name
, const char **atts
)
1092 struct active_lock
*lock
= (struct active_lock
*)userData
;
1094 if (lock
->ctx_activelock
&& !strcmp(name
, "D:timeout"))
1095 lock
->ctx_timeout
= 1;
1096 else if (lock
->ctx_owner
&& strstr(name
, "href"))
1097 lock
->ctx_owner_href
= 1;
1098 else if (lock
->ctx_activelock
&& strstr(name
, "owner"))
1099 lock
->ctx_owner
= 1;
1100 else if (lock
->ctx_locktoken
&& !strcmp(name
, "D:href"))
1101 lock
->ctx_locktoken_href
= 1;
1102 else if (lock
->ctx_activelock
&& !strcmp(name
, "D:locktoken"))
1103 lock
->ctx_locktoken
= 1;
1104 else if (!strcmp(name
, "D:activelock"))
1105 lock
->ctx_activelock
= 1;
1109 end_activelock_element(void *userData
, const char *name
)
1111 struct active_lock
*lock
= (struct active_lock
*)userData
;
1113 if (lock
->ctx_timeout
&& !strcmp(name
, "D:timeout")) {
1114 lock
->ctx_timeout
= 0;
1115 } else if (lock
->ctx_owner_href
&& strstr(name
, "href")) {
1116 lock
->ctx_owner_href
= 0;
1117 } else if (lock
->ctx_owner
&& strstr(name
, "owner")) {
1118 lock
->ctx_owner
= 0;
1119 } else if (lock
->ctx_locktoken_href
&& !strcmp(name
, "D:href")) {
1120 lock
->ctx_locktoken_href
= 0;
1121 } else if (lock
->ctx_locktoken
&& !strcmp(name
, "D:locktoken")) {
1122 lock
->ctx_locktoken
= 0;
1123 } else if (lock
->ctx_activelock
&& !strcmp(name
, "D:activelock")) {
1124 lock
->ctx_activelock
= 0;
1129 activelock_cdata(void *userData
, const XML_Char
*s
, int len
)
1131 struct active_lock
*lock
= (struct active_lock
*)userData
;
1132 char *this = malloc(len
+1);
1133 strncpy(this, s
, len
);
1135 if (lock
->ctx_owner_href
) {
1136 lock
->owner
= malloc(len
+1);
1137 strcpy(lock
->owner
, this);
1138 } else if (lock
->ctx_locktoken_href
) {
1139 if (!strncmp(this, "opaquelocktoken:", 16)) {
1140 lock
->token
= malloc(len
-15);
1141 strcpy(lock
->token
, this+16);
1143 } else if (lock
->ctx_timeout
) {
1144 if (!strncmp(this, "Second-", 7))
1145 lock
->timeout
= strtol(this+7, NULL
, 10);
1152 start_lockprop_element(void *userData
, const char *name
, const char **atts
)
1154 struct lockprop
*prop
= (struct lockprop
*)userData
;
1156 if (prop
->lock_type
&& !strcmp(name
, "D:write")) {
1157 if (prop
->lock_exclusive
) {
1158 prop
->lock_exclusive_write
= 1;
1160 } else if (prop
->lock_scope
&& !strcmp(name
, "D:exclusive")) {
1161 prop
->lock_exclusive
= 1;
1162 } else if (prop
->lock_entry
) {
1163 if (!strcmp(name
, "D:lockscope")) {
1164 prop
->lock_scope
= 1;
1165 } else if (!strcmp(name
, "D:locktype")) {
1166 prop
->lock_type
= 1;
1168 } else if (prop
->supported_lock
) {
1169 if (!strcmp(name
, "D:lockentry")) {
1170 prop
->lock_entry
= 1;
1172 } else if (!strcmp(name
, "D:supportedlock")) {
1173 prop
->supported_lock
= 1;
1178 end_lockprop_element(void *userData
, const char *name
)
1180 struct lockprop
*prop
= (struct lockprop
*)userData
;
1182 if (!strcmp(name
, "D:lockentry")) {
1183 prop
->lock_entry
= 0;
1184 prop
->lock_scope
= 0;
1185 prop
->lock_type
= 0;
1186 prop
->lock_exclusive
= 0;
1187 } else if (!strcmp(name
, "D:supportedlock")) {
1188 prop
->supported_lock
= 0;
1192 static struct active_lock
*lock_remote(char *file
, long timeout
)
1194 struct active_request_slot
*slot
;
1195 struct buffer out_buffer
;
1196 struct buffer in_buffer
;
1201 char timeout_header
[25];
1202 struct active_lock
*new_lock
;
1203 XML_Parser parser
= XML_ParserCreate(NULL
);
1204 enum XML_Status result
;
1205 struct curl_slist
*dav_headers
= NULL
;
1207 url
= xmalloc(strlen(remote
->url
) + strlen(file
) + 1);
1208 sprintf(url
, "%s%s", remote
->url
, file
);
1210 /* Make sure leading directories exist for the remote ref */
1211 ep
= strchr(url
+ strlen(remote
->url
) + 11, '/');
1214 slot
= get_active_slot();
1215 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
1216 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1217 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MKCOL
);
1218 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1219 if (start_active_slot(slot
)) {
1220 run_active_slot(slot
);
1221 if (slot
->curl_result
!= CURLE_OK
&&
1222 slot
->http_code
!= 405) {
1224 "Unable to create branch path %s\n",
1230 fprintf(stderr
, "Unable to start request\n");
1235 ep
= strchr(ep
+ 1, '/');
1238 out_buffer
.size
= strlen(LOCK_REQUEST
) + strlen(git_default_email
) - 2;
1239 out_data
= xmalloc(out_buffer
.size
+ 1);
1240 snprintf(out_data
, out_buffer
.size
+ 1, LOCK_REQUEST
, git_default_email
);
1241 out_buffer
.posn
= 0;
1242 out_buffer
.buffer
= out_data
;
1244 in_buffer
.size
= 4096;
1245 in_data
= xmalloc(in_buffer
.size
);
1247 in_buffer
.buffer
= in_data
;
1249 new_lock
= xcalloc(1, sizeof(*new_lock
));
1250 new_lock
->owner
= NULL
;
1251 new_lock
->token
= NULL
;
1252 new_lock
->timeout
= -1;
1253 new_lock
->refreshing
= 0;
1255 sprintf(timeout_header
, "Timeout: Second-%ld", timeout
);
1256 dav_headers
= curl_slist_append(dav_headers
, timeout_header
);
1257 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1259 slot
= get_active_slot();
1260 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1261 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.size
);
1262 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1263 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1264 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
1265 fwrite_buffer_dynamic
);
1266 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1267 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1268 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_LOCK
);
1269 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1271 if (start_active_slot(slot
)) {
1272 run_active_slot(slot
);
1273 if (slot
->curl_result
!= CURLE_OK
) {
1274 fprintf(stderr
, "Got HTTP error %ld\n", slot
->http_code
);
1286 fprintf(stderr
, "Unable to start request\n");
1292 XML_SetUserData(parser
, new_lock
);
1293 XML_SetElementHandler(parser
, start_activelock_element
,
1294 end_activelock_element
);
1295 XML_SetCharacterDataHandler(parser
, activelock_cdata
);
1296 result
= XML_Parse(parser
, in_buffer
.buffer
, in_buffer
.posn
, 1);
1298 if (result
!= XML_STATUS_OK
) {
1299 fprintf(stderr
, "%s", XML_ErrorString(
1300 XML_GetErrorCode(parser
)));
1306 if (new_lock
->token
== NULL
|| new_lock
->timeout
<= 0) {
1307 if (new_lock
->token
!= NULL
)
1308 free(new_lock
->token
);
1309 if (new_lock
->owner
!= NULL
)
1310 free(new_lock
->owner
);
1316 new_lock
->url
= url
;
1317 new_lock
->start_time
= time(NULL
);
1321 static int unlock_remote(struct active_lock
*lock
)
1323 struct active_request_slot
*slot
;
1324 char *lock_token_header
;
1325 struct curl_slist
*dav_headers
= NULL
;
1328 lock_token_header
= xmalloc(strlen(lock
->token
) + 31);
1329 sprintf(lock_token_header
, "Lock-Token: <opaquelocktoken:%s>",
1331 dav_headers
= curl_slist_append(dav_headers
, lock_token_header
);
1333 slot
= get_active_slot();
1334 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1335 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
1336 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_UNLOCK
);
1337 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1339 if (start_active_slot(slot
)) {
1340 run_active_slot(slot
);
1341 if (slot
->curl_result
== CURLE_OK
)
1344 fprintf(stderr
, "Got HTTP error %ld\n",
1347 fprintf(stderr
, "Unable to start request\n");
1350 curl_slist_free_all(dav_headers
);
1351 free(lock_token_header
);
1353 if (lock
->owner
!= NULL
)
1362 static int check_locking(void)
1364 struct active_request_slot
*slot
;
1365 struct buffer in_buffer
;
1366 struct buffer out_buffer
;
1369 XML_Parser parser
= XML_ParserCreate(NULL
);
1370 enum XML_Status result
;
1371 struct lockprop supported_lock
;
1372 struct curl_slist
*dav_headers
= NULL
;
1374 out_buffer
.size
= strlen(PROPFIND_REQUEST
) + strlen(remote
->url
) - 2;
1375 out_data
= xmalloc(out_buffer
.size
+ 1);
1376 snprintf(out_data
, out_buffer
.size
+ 1, PROPFIND_REQUEST
, remote
->url
);
1377 out_buffer
.posn
= 0;
1378 out_buffer
.buffer
= out_data
;
1380 in_buffer
.size
= 4096;
1381 in_data
= xmalloc(in_buffer
.size
);
1383 in_buffer
.buffer
= in_data
;
1385 dav_headers
= curl_slist_append(dav_headers
, "Depth: 0");
1386 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1388 slot
= get_active_slot();
1389 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1390 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.size
);
1391 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1392 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1393 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
1394 fwrite_buffer_dynamic
);
1395 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, remote
->url
);
1396 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1397 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PROPFIND
);
1398 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1400 if (start_active_slot(slot
)) {
1401 run_active_slot(slot
);
1403 if (slot
->curl_result
!= CURLE_OK
) {
1404 free(in_buffer
.buffer
);
1408 XML_SetUserData(parser
, &supported_lock
);
1409 XML_SetElementHandler(parser
, start_lockprop_element
,
1410 end_lockprop_element
);
1411 result
= XML_Parse(parser
, in_buffer
.buffer
, in_buffer
.posn
, 1);
1412 free(in_buffer
.buffer
);
1413 if (result
!= XML_STATUS_OK
)
1414 return error("%s", XML_ErrorString(
1415 XML_GetErrorCode(parser
)));
1418 free(in_buffer
.buffer
);
1419 return error("Unable to start request");
1422 if (supported_lock
.lock_exclusive_write
)
1428 static int is_ancestor(unsigned char *sha1
, struct commit
*commit
)
1430 struct commit_list
*parents
;
1432 if (parse_commit(commit
))
1434 parents
= commit
->parents
;
1435 for (; parents
; parents
= parents
->next
) {
1436 if (!memcmp(sha1
, parents
->item
->object
.sha1
, 20)) {
1438 } else if (parents
->item
->object
.type
== commit_type
) {
1441 (struct commit
*)&parents
->item
->object
1449 static void get_delta(unsigned char *sha1
, struct object
*obj
,
1450 struct active_lock
*lock
)
1452 struct commit
*commit
;
1453 struct commit_list
*parents
;
1455 struct tree_entry_list
*entry
;
1457 if (sha1
&& !memcmp(sha1
, obj
->sha1
, 20))
1463 if (obj
->type
== commit_type
) {
1465 fprintf(stderr
, "walk %s\n", sha1_to_hex(obj
->sha1
));
1466 add_request(obj
->sha1
, lock
);
1467 commit
= (struct commit
*)obj
;
1468 if (parse_commit(commit
)) {
1469 fprintf(stderr
, "Error parsing commit %s\n",
1470 sha1_to_hex(obj
->sha1
));
1474 parents
= commit
->parents
;
1475 for (; parents
; parents
= parents
->next
)
1477 memcmp(sha1
, parents
->item
->object
.sha1
, 20))
1478 get_delta(sha1
, &parents
->item
->object
,
1480 get_delta(sha1
, &commit
->tree
->object
, lock
);
1481 } else if (obj
->type
== tree_type
) {
1483 fprintf(stderr
, "walk %s\n", sha1_to_hex(obj
->sha1
));
1484 add_request(obj
->sha1
, lock
);
1485 tree
= (struct tree
*)obj
;
1486 if (parse_tree(tree
)) {
1487 fprintf(stderr
, "Error parsing tree %s\n",
1488 sha1_to_hex(obj
->sha1
));
1492 entry
= tree
->entries
;
1493 tree
->entries
= NULL
;
1495 struct tree_entry_list
*next
= entry
->next
;
1496 get_delta(sha1
, entry
->item
.any
, lock
);
1501 } else if (obj
->type
== blob_type
|| obj
->type
== tag_type
) {
1502 add_request(obj
->sha1
, lock
);
1506 static int update_remote(unsigned char *sha1
, struct active_lock
*lock
)
1508 struct active_request_slot
*slot
;
1511 struct buffer out_buffer
;
1512 struct curl_slist
*dav_headers
= NULL
;
1515 if_header
= xmalloc(strlen(lock
->token
) + 25);
1516 sprintf(if_header
, "If: (<opaquelocktoken:%s>)", lock
->token
);
1517 dav_headers
= curl_slist_append(dav_headers
, if_header
);
1519 out_buffer
.size
= 41;
1520 out_data
= xmalloc(out_buffer
.size
+ 1);
1521 i
= snprintf(out_data
, out_buffer
.size
+ 1, "%s\n", sha1_to_hex(sha1
));
1522 if (i
!= out_buffer
.size
) {
1523 fprintf(stderr
, "Unable to initialize PUT request body\n");
1526 out_buffer
.posn
= 0;
1527 out_buffer
.buffer
= out_data
;
1529 slot
= get_active_slot();
1530 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1531 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.size
);
1532 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1533 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1534 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
1535 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1536 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1537 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
1538 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
1540 if (start_active_slot(slot
)) {
1541 run_active_slot(slot
);
1544 if (slot
->curl_result
!= CURLE_OK
) {
1546 "PUT error: curl result=%d, HTTP code=%ld\n",
1547 slot
->curl_result
, slot
->http_code
);
1548 /* We should attempt recovery? */
1554 fprintf(stderr
, "Unable to start PUT request\n");
1561 int main(int argc
, char **argv
)
1563 struct active_request_slot
*slot
;
1564 struct active_request_slot
*next_slot
;
1565 struct transfer_request
*request
;
1566 struct transfer_request
*next_request
;
1568 char **refspec
= NULL
;
1569 int do_remote_update
;
1573 unsigned char local_sha1
[20];
1574 struct object
*local_object
= NULL
;
1575 char *remote_ref
= NULL
;
1576 unsigned char remote_sha1
[20];
1577 struct active_lock
*remote_lock
;
1578 char *remote_path
= NULL
;
1579 char *low_speed_limit
;
1580 char *low_speed_time
;
1586 remote
= xmalloc(sizeof(*remote
));
1588 remote
->packs
= NULL
;
1591 for (i
= 1; i
< argc
; i
++, argv
++) {
1595 if (!strcmp(arg
, "--complete")) {
1599 if (!strcmp(arg
, "--force")) {
1603 if (!strcmp(arg
, "--verbose")) {
1607 usage(http_push_usage
);
1614 nr_refspec
= argc
- i
;
1618 memset(remote_dir_exists
, 0, 256);
1620 curl_global_init(CURL_GLOBAL_ALL
);
1622 #ifdef USE_CURL_MULTI
1624 char *http_max_requests
= getenv("GIT_HTTP_MAX_REQUESTS");
1625 if (http_max_requests
!= NULL
)
1626 max_requests
= atoi(http_max_requests
);
1629 curlm
= curl_multi_init();
1630 if (curlm
== NULL
) {
1631 fprintf(stderr
, "Error creating curl multi handle.\n");
1636 if (getenv("GIT_SSL_NO_VERIFY"))
1637 curl_ssl_verify
= 0;
1639 ssl_cert
= getenv("GIT_SSL_CERT");
1640 #if LIBCURL_VERSION_NUM >= 0x070902
1641 ssl_key
= getenv("GIT_SSL_KEY");
1643 #if LIBCURL_VERSION_NUM >= 0x070908
1644 ssl_capath
= getenv("GIT_SSL_CAPATH");
1646 ssl_cainfo
= getenv("GIT_SSL_CAINFO");
1648 low_speed_limit
= getenv("GIT_HTTP_LOW_SPEED_LIMIT");
1649 if (low_speed_limit
!= NULL
)
1650 curl_low_speed_limit
= strtol(low_speed_limit
, NULL
, 10);
1651 low_speed_time
= getenv("GIT_HTTP_LOW_SPEED_TIME");
1652 if (low_speed_time
!= NULL
)
1653 curl_low_speed_time
= strtol(low_speed_time
, NULL
, 10);
1655 git_config(http_options
);
1657 if (curl_ssl_verify
== -1)
1658 curl_ssl_verify
= 1;
1660 #ifdef USE_CURL_MULTI
1661 if (max_requests
< 1)
1662 max_requests
= DEFAULT_MAX_REQUESTS
;
1665 no_pragma_header
= curl_slist_append(no_pragma_header
, "Pragma:");
1666 default_headers
= curl_slist_append(default_headers
, "Range:");
1667 default_headers
= curl_slist_append(default_headers
, "Destination:");
1668 default_headers
= curl_slist_append(default_headers
, "If:");
1669 default_headers
= curl_slist_append(default_headers
,
1670 "Pragma: no-cache");
1672 #ifndef NO_CURL_EASY_DUPHANDLE
1673 curl_default
= get_curl_handle();
1676 /* Verify DAV compliance/lock support */
1677 if (check_locking() != 0) {
1678 fprintf(stderr
, "Error: no DAV locking support on remote repo %s\n", remote
->url
);
1683 /* Process each refspec */
1684 for (i
= 0; i
< nr_refspec
; i
++) {
1687 do_remote_update
= 0;
1689 local_ref
= refspec
[i
];
1690 if (*local_ref
== '+') {
1694 ep
= strchr(local_ref
, ':');
1696 remote_ref
= ep
+ 1;
1700 remote_ref
= local_ref
;
1702 /* Lock remote branch ref */
1705 remote_path
= xmalloc(strlen(remote_ref
) + 12);
1706 sprintf(remote_path
, "refs/heads/%s", remote_ref
);
1707 remote_lock
= lock_remote(remote_path
, LOCK_TIME
);
1708 if (remote_lock
== NULL
) {
1709 fprintf(stderr
, "Unable to lock remote branch %s\n",
1715 /* Resolve local and remote refs */
1716 if (fetch_ref(remote_ref
, remote_sha1
) != 0) {
1718 "Remote branch %s does not exist on %s\n",
1719 remote_ref
, remote
->url
);
1722 if (get_sha1(local_ref
, local_sha1
) != 0) {
1723 fprintf(stderr
, "Error resolving local branch %s\n",
1729 /* Find relationship between local and remote */
1730 local_object
= parse_object(local_sha1
);
1731 if (!local_object
) {
1732 fprintf(stderr
, "Unable to parse local object %s\n",
1733 sha1_to_hex(local_sha1
));
1736 } else if (new_branch
) {
1737 do_remote_update
= 1;
1739 if (!memcmp(local_sha1
, remote_sha1
, 20)) {
1741 "* %s: same as branch '%s' of %s\n",
1742 local_ref
, remote_ref
, remote
->url
);
1743 } else if (is_ancestor(remote_sha1
,
1744 (struct commit
*)local_object
)) {
1746 "Remote %s will fast-forward to local %s\n",
1747 remote_ref
, local_ref
);
1748 do_remote_update
= 1;
1749 } else if (force_all
|| force_this
) {
1751 "* %s on %s does not fast forward to local branch '%s', overwriting\n",
1752 remote_ref
, remote
->url
, local_ref
);
1753 do_remote_update
= 1;
1756 "* %s on %s does not fast forward to local branch '%s'\n",
1757 remote_ref
, remote
->url
, local_ref
);
1763 /* Generate and check list of required objects */
1765 if (do_remote_update
|| push_all
)
1767 get_delta(push_all
? NULL
: remote_sha1
,
1768 local_object
, remote_lock
);
1769 process_waiting_requests();
1771 /* Push missing objects to remote, this would be a
1772 convenient time to pack them first if appropriate. */
1774 process_request_queue();
1775 process_waiting_requests();
1777 /* Update the remote branch if all went well */
1778 if (do_remote_update
) {
1779 if (!aborted
&& update_remote(local_sha1
,
1781 fprintf(stderr
, "%s remote branch %s\n",
1782 new_branch
? "Created" : "Updated",
1786 "Unable to %s remote branch %s\n",
1787 new_branch
? "create" : "update",
1795 unlock_remote(remote_lock
);
1802 curl_slist_free_all(no_pragma_header
);
1803 curl_slist_free_all(default_headers
);
1805 slot
= active_queue_head
;
1806 while (slot
!= NULL
) {
1807 next_slot
= slot
->next
;
1808 if (slot
->curl
!= NULL
)
1809 curl_easy_cleanup(slot
->curl
);
1814 request
= request_queue_head
;
1815 while (request
!= NULL
) {
1816 next_request
= request
->next
;
1817 release_request(request
);
1818 request
= next_request
;
1821 #ifndef NO_CURL_EASY_DUPHANDLE
1822 curl_easy_cleanup(curl_default
);
1824 #ifdef USE_CURL_MULTI
1825 curl_multi_cleanup(curlm
);
1827 curl_global_cleanup();