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>"
43 static int active_requests
= 0;
44 static int data_received
;
45 static int pushing
= 0;
46 static int aborted
= 0;
49 static int max_requests
= -1;
52 #ifndef NO_CURL_EASY_DUPHANDLE
53 static CURL
*curl_default
;
55 static struct curl_slist
*no_pragma_header
;
56 static struct curl_slist
*default_headers
;
57 static char curl_errorstr
[CURL_ERROR_SIZE
];
58 static char *lock_token
= NULL
;
60 static int push_verbosely
= 0;
61 static int push_all
= 0;
62 static int force_all
= 0;
74 struct packed_git
*packs
;
77 static struct repo
*remote
= NULL
;
90 struct transfer_request
92 unsigned char sha1
[20];
96 struct curl_slist
*headers
;
98 char filename
[PATH_MAX
];
99 char tmpfile
[PATH_MAX
];
100 enum transfer_state state
;
101 CURLcode curl_result
;
102 char errorstr
[CURL_ERROR_SIZE
];
104 unsigned char real_sha1
[20];
109 struct active_request_slot
*slot
;
110 struct transfer_request
*next
;
113 struct active_request_slot
119 CURLcode curl_result
;
121 struct active_request_slot
*next
;
124 static struct transfer_request
*request_queue_head
= NULL
;
125 static struct active_request_slot
*active_queue_head
= NULL
;
127 static int curl_ssl_verify
= -1;
128 static char *ssl_cert
= NULL
;
129 #if LIBCURL_VERSION_NUM >= 0x070902
130 static char *ssl_key
= NULL
;
132 #if LIBCURL_VERSION_NUM >= 0x070908
133 static char *ssl_capath
= NULL
;
135 static char *ssl_cainfo
= NULL
;
136 static long curl_low_speed_limit
= -1;
137 static long curl_low_speed_time
= -1;
146 int lock_exclusive_write
;
149 static int http_options(const char *var
, const char *value
)
151 if (!strcmp("http.sslverify", var
)) {
152 if (curl_ssl_verify
== -1) {
153 curl_ssl_verify
= git_config_bool(var
, value
);
158 if (!strcmp("http.sslcert", var
)) {
159 if (ssl_cert
== NULL
) {
160 ssl_cert
= xmalloc(strlen(value
)+1);
161 strcpy(ssl_cert
, value
);
165 #if LIBCURL_VERSION_NUM >= 0x070902
166 if (!strcmp("http.sslkey", var
)) {
167 if (ssl_key
== NULL
) {
168 ssl_key
= xmalloc(strlen(value
)+1);
169 strcpy(ssl_key
, value
);
174 #if LIBCURL_VERSION_NUM >= 0x070908
175 if (!strcmp("http.sslcapath", var
)) {
176 if (ssl_capath
== NULL
) {
177 ssl_capath
= xmalloc(strlen(value
)+1);
178 strcpy(ssl_capath
, value
);
183 if (!strcmp("http.sslcainfo", var
)) {
184 if (ssl_cainfo
== NULL
) {
185 ssl_cainfo
= xmalloc(strlen(value
)+1);
186 strcpy(ssl_cainfo
, value
);
191 #ifdef USE_CURL_MULTI
192 if (!strcmp("http.maxrequests", var
)) {
193 if (max_requests
== -1)
194 max_requests
= git_config_int(var
, value
);
199 if (!strcmp("http.lowspeedlimit", var
)) {
200 if (curl_low_speed_limit
== -1)
201 curl_low_speed_limit
= (long)git_config_int(var
, value
);
204 if (!strcmp("http.lowspeedtime", var
)) {
205 if (curl_low_speed_time
== -1)
206 curl_low_speed_time
= (long)git_config_int(var
, value
);
210 /* Fall back on the default ones */
211 return git_default_config(var
, value
);
214 static size_t fread_buffer(void *ptr
, size_t eltsize
, size_t nmemb
,
215 struct buffer
*buffer
)
217 size_t size
= eltsize
* nmemb
;
218 if (size
> buffer
->size
- buffer
->posn
)
219 size
= buffer
->size
- buffer
->posn
;
220 memcpy(ptr
, buffer
->buffer
+ buffer
->posn
, size
);
221 buffer
->posn
+= size
;
225 static size_t fwrite_buffer_dynamic(const void *ptr
, size_t eltsize
,
226 size_t nmemb
, struct buffer
*buffer
)
228 size_t size
= eltsize
* nmemb
;
229 if (size
> buffer
->size
- buffer
->posn
) {
230 buffer
->size
= buffer
->size
* 3 / 2;
231 if (buffer
->size
< buffer
->posn
+ size
)
232 buffer
->size
= buffer
->posn
+ size
;
233 buffer
->buffer
= xrealloc(buffer
->buffer
, buffer
->size
);
235 memcpy(buffer
->buffer
+ buffer
->posn
, ptr
, size
);
236 buffer
->posn
+= size
;
241 static size_t fwrite_null(const void *ptr
, size_t eltsize
,
242 size_t nmemb
, struct buffer
*buffer
)
245 return eltsize
* nmemb
;
248 #ifdef USE_CURL_MULTI
249 static void process_curl_messages(void);
250 static void process_request_queue(void);
253 static CURL
* get_curl_handle(void)
255 CURL
* result
= curl_easy_init();
257 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, curl_ssl_verify
);
258 #if LIBCURL_VERSION_NUM >= 0x070907
259 curl_easy_setopt(result
, CURLOPT_NETRC
, CURL_NETRC_OPTIONAL
);
262 if (ssl_cert
!= NULL
)
263 curl_easy_setopt(result
, CURLOPT_SSLCERT
, ssl_cert
);
264 #if LIBCURL_VERSION_NUM >= 0x070902
266 curl_easy_setopt(result
, CURLOPT_SSLKEY
, ssl_key
);
268 #if LIBCURL_VERSION_NUM >= 0x070908
269 if (ssl_capath
!= NULL
)
270 curl_easy_setopt(result
, CURLOPT_CAPATH
, ssl_capath
);
272 if (ssl_cainfo
!= NULL
)
273 curl_easy_setopt(result
, CURLOPT_CAINFO
, ssl_cainfo
);
274 curl_easy_setopt(result
, CURLOPT_FAILONERROR
, 1);
276 if (curl_low_speed_limit
> 0 && curl_low_speed_time
> 0) {
277 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_LIMIT
,
278 curl_low_speed_limit
);
279 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_TIME
,
280 curl_low_speed_time
);
286 static struct active_request_slot
*get_active_slot(void)
288 struct active_request_slot
*slot
= active_queue_head
;
289 struct active_request_slot
*newslot
;
291 #ifdef USE_CURL_MULTI
294 /* Wait for a slot to open up if the queue is full */
295 while (active_requests
>= max_requests
) {
296 curl_multi_perform(curlm
, &num_transfers
);
297 if (num_transfers
< active_requests
) {
298 process_curl_messages();
303 while (slot
!= NULL
&& slot
->in_use
) {
307 newslot
= xmalloc(sizeof(*newslot
));
308 newslot
->curl
= NULL
;
310 newslot
->next
= NULL
;
312 slot
= active_queue_head
;
314 active_queue_head
= newslot
;
316 while (slot
->next
!= NULL
) {
319 slot
->next
= newslot
;
324 if (slot
->curl
== NULL
) {
325 #ifdef NO_CURL_EASY_DUPHANDLE
326 slot
->curl
= get_curl_handle();
328 slot
->curl
= curl_easy_duphandle(curl_default
);
336 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, default_headers
);
337 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
342 static int start_active_slot(struct active_request_slot
*slot
)
344 #ifdef USE_CURL_MULTI
345 CURLMcode curlm_result
= curl_multi_add_handle(curlm
, slot
->curl
);
347 if (curlm_result
!= CURLM_OK
&&
348 curlm_result
!= CURLM_CALL_MULTI_PERFORM
) {
357 static void run_active_slot(struct active_request_slot
*slot
)
359 #ifdef USE_CURL_MULTI
367 struct timeval select_timeout
;
368 CURLMcode curlm_result
;
370 while (!slot
->done
) {
373 curlm_result
= curl_multi_perform(curlm
,
375 } while (curlm_result
== CURLM_CALL_MULTI_PERFORM
);
376 if (num_transfers
< active_requests
) {
377 process_curl_messages();
378 process_request_queue();
381 if (!data_received
&& slot
->local
!= NULL
) {
382 current_pos
= ftell(slot
->local
);
383 if (current_pos
> last_pos
)
385 last_pos
= current_pos
;
388 if (!slot
->done
&& !data_received
) {
393 select_timeout
.tv_sec
= 0;
394 select_timeout
.tv_usec
= 50000;
395 select(max_fd
, &readfds
, &writefds
,
396 &excfds
, &select_timeout
);
400 slot
->curl_result
= curl_easy_perform(slot
->curl
);
405 static void start_check(struct transfer_request
*request
)
407 char *hex
= sha1_to_hex(request
->sha1
);
408 struct active_request_slot
*slot
;
411 request
->url
= xmalloc(strlen(remote
->url
) + 55);
412 strcpy(request
->url
, remote
->url
);
413 posn
= request
->url
+ strlen(remote
->url
);
414 strcpy(posn
, "objects/");
416 memcpy(posn
, hex
, 2);
419 strcpy(posn
, hex
+ 2);
421 slot
= get_active_slot();
422 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
423 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
424 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
426 if (start_active_slot(slot
)) {
427 request
->slot
= slot
;
428 request
->state
= RUN_HEAD
;
430 request
->state
= ABORTED
;
435 static void start_mkcol(struct transfer_request
*request
)
437 char *hex
= sha1_to_hex(request
->sha1
);
438 struct active_request_slot
*slot
;
441 request
->url
= xmalloc(strlen(remote
->url
) + 13);
442 strcpy(request
->url
, remote
->url
);
443 posn
= request
->url
+ strlen(remote
->url
);
444 strcpy(posn
, "objects/");
446 memcpy(posn
, hex
, 2);
450 slot
= get_active_slot();
451 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1); /* undo PUT setup */
452 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
453 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
454 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MKCOL
);
455 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
457 if (start_active_slot(slot
)) {
458 request
->slot
= slot
;
459 request
->state
= RUN_MKCOL
;
461 request
->state
= ABORTED
;
466 static void start_put(struct transfer_request
*request
)
468 char *hex
= sha1_to_hex(request
->sha1
);
469 struct active_request_slot
*slot
;
479 unpacked
= read_sha1_file(request
->sha1
, type
, &len
);
480 hdrlen
= sprintf(hdr
, "%s %lu", type
, len
) + 1;
483 memset(&stream
, 0, sizeof(stream
));
484 deflateInit(&stream
, Z_BEST_COMPRESSION
);
485 size
= deflateBound(&stream
, len
+ hdrlen
);
486 request
->buffer
.buffer
= xmalloc(size
);
489 stream
.next_out
= request
->buffer
.buffer
;
490 stream
.avail_out
= size
;
493 stream
.next_in
= (void *)hdr
;
494 stream
.avail_in
= hdrlen
;
495 while (deflate(&stream
, 0) == Z_OK
)
498 /* Then the data itself.. */
499 stream
.next_in
= unpacked
;
500 stream
.avail_in
= len
;
501 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
506 request
->buffer
.size
= stream
.total_out
;
507 request
->buffer
.posn
= 0;
509 if (request
->url
!= NULL
)
511 request
->url
= xmalloc(strlen(remote
->url
) +
512 strlen(request
->lock_token
) + 51);
513 strcpy(request
->url
, remote
->url
);
514 posn
= request
->url
+ strlen(remote
->url
);
515 strcpy(posn
, "objects/");
517 memcpy(posn
, hex
, 2);
520 strcpy(posn
, hex
+ 2);
521 request
->dest
= xmalloc(strlen(request
->url
) + 14);
522 sprintf(request
->dest
, "Destination: %s", request
->url
);
525 strcpy(posn
, request
->lock_token
);
527 slot
= get_active_slot();
528 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &request
->buffer
);
529 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, request
->buffer
.size
);
530 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
531 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
532 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
533 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
534 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
535 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
536 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
538 if (start_active_slot(slot
)) {
539 request
->slot
= slot
;
540 request
->state
= RUN_PUT
;
542 request
->state
= ABORTED
;
547 static void start_move(struct transfer_request
*request
)
549 struct active_request_slot
*slot
;
550 struct curl_slist
*dav_headers
= NULL
;
552 slot
= get_active_slot();
553 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1); /* undo PUT setup */
554 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MOVE
);
555 dav_headers
= curl_slist_append(dav_headers
, request
->dest
);
556 dav_headers
= curl_slist_append(dav_headers
, "Overwrite: T");
557 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
558 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
559 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
561 if (start_active_slot(slot
)) {
562 request
->slot
= slot
;
563 request
->state
= RUN_MOVE
;
565 request
->state
= ABORTED
;
570 static void finish_request(struct transfer_request
*request
)
572 request
->curl_result
= request
->slot
->curl_result
;
573 request
->http_code
= request
->slot
->http_code
;
574 request
->slot
= NULL
;
575 if (request
->headers
!= NULL
)
576 curl_slist_free_all(request
->headers
);
577 if (request
->state
== RUN_HEAD
) {
578 if (request
->http_code
== 404) {
579 request
->state
= NEED_PUSH
;
580 } else if (request
->curl_result
== CURLE_OK
) {
581 request
->state
= COMPLETE
;
583 fprintf(stderr
, "HEAD %s failed, aborting (%d/%ld)\n",
584 sha1_to_hex(request
->sha1
),
585 request
->curl_result
, request
->http_code
);
586 request
->state
= ABORTED
;
589 } else if (request
->state
== RUN_MKCOL
) {
590 if (request
->curl_result
== CURLE_OK
||
591 request
->http_code
== 405) {
594 fprintf(stderr
, "MKCOL %s failed, aborting (%d/%ld)\n",
595 sha1_to_hex(request
->sha1
),
596 request
->curl_result
, request
->http_code
);
597 request
->state
= ABORTED
;
600 } else if (request
->state
== RUN_PUT
) {
601 if (request
->curl_result
== CURLE_OK
) {
604 fprintf(stderr
, "PUT %s failed, aborting (%d/%ld)\n",
605 sha1_to_hex(request
->sha1
),
606 request
->curl_result
, request
->http_code
);
607 request
->state
= ABORTED
;
610 } else if (request
->state
== RUN_MOVE
) {
611 if (request
->curl_result
== CURLE_OK
) {
615 sha1_to_hex(request
->sha1
));
616 request
->state
= COMPLETE
;
618 fprintf(stderr
, "MOVE %s failed, aborting (%d/%ld)\n",
619 sha1_to_hex(request
->sha1
),
620 request
->curl_result
, request
->http_code
);
621 request
->state
= ABORTED
;
627 static void release_request(struct transfer_request
*request
)
629 struct transfer_request
*entry
= request_queue_head
;
631 if (request
== request_queue_head
) {
632 request_queue_head
= request
->next
;
634 while (entry
->next
!= NULL
&& entry
->next
!= request
)
636 if (entry
->next
== request
)
637 entry
->next
= entry
->next
->next
;
644 #ifdef USE_CURL_MULTI
645 void process_curl_messages(void)
648 struct active_request_slot
*slot
;
649 struct transfer_request
*request
= NULL
;
650 CURLMsg
*curl_message
= curl_multi_info_read(curlm
, &num_messages
);
652 while (curl_message
!= NULL
) {
653 if (curl_message
->msg
== CURLMSG_DONE
) {
654 slot
= active_queue_head
;
655 while (slot
!= NULL
&&
656 slot
->curl
!= curl_message
->easy_handle
)
659 curl_multi_remove_handle(curlm
, slot
->curl
);
663 slot
->curl_result
= curl_message
->data
.result
;
664 curl_easy_getinfo(slot
->curl
,
667 request
= request_queue_head
;
668 while (request
!= NULL
&&
669 request
->slot
!= slot
)
670 request
= request
->next
;
672 finish_request(request
);
674 fprintf(stderr
, "Received DONE message for unknown request!\n");
677 fprintf(stderr
, "Unknown CURL message received: %d\n",
678 (int)curl_message
->msg
);
680 curl_message
= curl_multi_info_read(curlm
, &num_messages
);
684 void process_request_queue(void)
686 struct transfer_request
*request
= request_queue_head
;
687 struct active_request_slot
*slot
= active_queue_head
;
693 while (active_requests
< max_requests
&& request
!= NULL
) {
694 if (!pushing
&& request
->state
== NEED_CHECK
) {
695 start_check(request
);
696 curl_multi_perform(curlm
, &num_transfers
);
697 } else if (pushing
&& request
->state
== NEED_PUSH
) {
698 start_mkcol(request
);
699 curl_multi_perform(curlm
, &num_transfers
);
701 request
= request
->next
;
704 while (slot
!= NULL
) {
705 if (!slot
->in_use
&& slot
->curl
!= NULL
) {
706 curl_easy_cleanup(slot
->curl
);
714 void process_waiting_requests(void)
716 struct active_request_slot
*slot
= active_queue_head
;
720 run_active_slot(slot
);
721 slot
= active_queue_head
;
727 void add_request(unsigned char *sha1
, char *lock_token
)
729 struct transfer_request
*request
= request_queue_head
;
730 struct transfer_request
*tail
;
731 struct packed_git
*target
;
733 while (request
!= NULL
&& memcmp(request
->sha1
, sha1
, 20))
734 request
= request
->next
;
738 target
= find_sha1_pack(sha1
, remote
->packs
);
742 request
= xmalloc(sizeof(*request
));
743 memcpy(request
->sha1
, sha1
, 20);
745 request
->lock_token
= lock_token
;
746 request
->headers
= NULL
;
747 request
->state
= NEED_CHECK
;
748 request
->next
= NULL
;
750 if (request_queue_head
== NULL
) {
751 request_queue_head
= request
;
753 tail
= request_queue_head
;
754 while (tail
->next
!= NULL
) {
757 tail
->next
= request
;
759 #ifdef USE_CURL_MULTI
760 process_request_queue();
761 process_curl_messages();
765 static int fetch_index(unsigned char *sha1
)
767 char *hex
= sha1_to_hex(sha1
);
770 char tmpfile
[PATH_MAX
];
772 char range
[RANGE_HEADER_SIZE
];
773 struct curl_slist
*range_header
= NULL
;
776 struct active_request_slot
*slot
;
778 if (has_pack_index(sha1
))
782 fprintf(stderr
, "Getting index for pack %s\n", hex
);
784 url
= xmalloc(strlen(remote
->url
) + 64);
785 sprintf(url
, "%s/objects/pack/pack-%s.idx", remote
->url
, hex
);
787 filename
= sha1_pack_index_name(sha1
);
788 snprintf(tmpfile
, sizeof(tmpfile
), "%s.temp", filename
);
789 indexfile
= fopen(tmpfile
, "a");
791 return error("Unable to open local file %s for pack index",
794 slot
= get_active_slot();
795 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, indexfile
);
796 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
797 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
798 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
799 slot
->local
= indexfile
;
801 /* If there is data present from a previous transfer attempt,
802 resume where it left off */
803 prev_posn
= ftell(indexfile
);
807 "Resuming fetch of index for pack %s at byte %ld\n",
809 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
810 range_header
= curl_slist_append(range_header
, range
);
811 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, range_header
);
814 if (start_active_slot(slot
)) {
815 run_active_slot(slot
);
816 if (slot
->curl_result
!= CURLE_OK
) {
819 return error("Unable to get pack index %s\n%s", url
,
824 return error("Unable to start request");
830 return move_temp_to_file(tmpfile
, filename
);
833 static int setup_index(unsigned char *sha1
)
835 struct packed_git
*new_pack
;
836 if (has_pack_file(sha1
))
837 return 0; // don't list this as something we can get
839 if (fetch_index(sha1
))
842 new_pack
= parse_pack_index(sha1
);
843 new_pack
->next
= remote
->packs
;
844 remote
->packs
= new_pack
;
848 static int fetch_indices()
850 unsigned char sha1
[20];
852 struct buffer buffer
;
856 struct active_request_slot
*slot
;
858 data
= xmalloc(4096);
859 memset(data
, 0, 4096);
862 buffer
.buffer
= data
;
865 fprintf(stderr
, "Getting pack list\n");
867 url
= xmalloc(strlen(remote
->url
) + 21);
868 sprintf(url
, "%s/objects/info/packs", remote
->url
);
870 slot
= get_active_slot();
871 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
872 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
873 fwrite_buffer_dynamic
);
874 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
875 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
876 if (start_active_slot(slot
)) {
877 run_active_slot(slot
);
878 if (slot
->curl_result
!= CURLE_OK
) {
881 if (slot
->http_code
== 404)
884 return error("%s", curl_errorstr
);
889 return error("Unable to start request");
893 data
= buffer
.buffer
;
894 while (i
< buffer
.posn
) {
898 if (i
+ 52 < buffer
.posn
&&
899 !strncmp(data
+ i
, " pack-", 6) &&
900 !strncmp(data
+ i
+ 46, ".pack\n", 6)) {
901 get_sha1_hex(data
+ i
+ 6, sha1
);
907 while (data
[i
] != '\n')
917 static inline int needs_quote(int ch
)
920 case '/': case '-': case '.':
921 case 'A'...'Z': case 'a'...'z': case '0'...'9':
928 static inline int hex(int v
)
930 if (v
< 10) return '0' + v
;
931 else return 'A' + v
- 10;
934 static char *quote_ref_url(const char *base
, const char *ref
)
938 int len
, baselen
, ch
;
940 baselen
= strlen(base
);
941 len
= baselen
+ 12; /* "refs/heads/" + NUL */
942 for (cp
= ref
; (ch
= *cp
) != 0; cp
++, len
++)
944 len
+= 2; /* extra two hex plus replacement % */
946 memcpy(qref
, base
, baselen
);
947 memcpy(qref
+ baselen
, "refs/heads/", 11);
948 for (cp
= ref
, dp
= qref
+ baselen
+ 11; (ch
= *cp
) != 0; cp
++) {
949 if (needs_quote(ch
)) {
951 *dp
++ = hex((ch
>> 4) & 0xF);
952 *dp
++ = hex(ch
& 0xF);
962 int fetch_ref(char *ref
, unsigned char *sha1
)
966 struct buffer buffer
;
967 char *base
= remote
->url
;
968 struct active_request_slot
*slot
;
974 url
= quote_ref_url(base
, ref
);
975 slot
= get_active_slot();
976 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
977 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
978 fwrite_buffer_dynamic
);
979 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
980 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
981 if (start_active_slot(slot
)) {
982 run_active_slot(slot
);
983 if (slot
->curl_result
!= CURLE_OK
)
984 return error("Couldn't get %s for %s\n%s",
985 url
, ref
, curl_errorstr
);
987 return error("Unable to start request");
991 get_sha1_hex(hex
, sha1
);
996 start_lockprop_element(void *userData
, const char *name
, const char **atts
)
998 struct lockprop
*prop
= (struct lockprop
*)userData
;
1000 if (prop
->lock_type
&& !strcmp(name
, "D:write")) {
1001 if (prop
->lock_exclusive
) {
1002 prop
->lock_exclusive_write
= 1;
1004 } else if (prop
->lock_scope
&& !strcmp(name
, "D:exclusive")) {
1005 prop
->lock_exclusive
= 1;
1006 } else if (prop
->lock_entry
) {
1007 if (!strcmp(name
, "D:lockscope")) {
1008 prop
->lock_scope
= 1;
1009 } else if (!strcmp(name
, "D:locktype")) {
1010 prop
->lock_type
= 1;
1012 } else if (prop
->supported_lock
) {
1013 if (!strcmp(name
, "D:lockentry")) {
1014 prop
->lock_entry
= 1;
1016 } else if (!strcmp(name
, "D:supportedlock")) {
1017 prop
->supported_lock
= 1;
1022 end_lockprop_element(void *userData
, const char *name
)
1024 struct lockprop
*prop
= (struct lockprop
*)userData
;
1026 if (!strcmp(name
, "D:lockentry")) {
1027 prop
->lock_entry
= 0;
1028 prop
->lock_scope
= 0;
1029 prop
->lock_type
= 0;
1030 prop
->lock_exclusive
= 0;
1031 } else if (!strcmp(name
, "D:supportedlock")) {
1032 prop
->supported_lock
= 0;
1036 size_t process_lock_header( void *ptr
, size_t size
, size_t nmemb
, void *stream
)
1038 size_t header_size
= size
*nmemb
;
1042 if (!strncmp(ptr
, "Lock-Token: <opaquelocktoken:", 29)) {
1044 for (end
= ptr
+ header_size
;
1045 *(end
- 1) == '\r' || *(end
- 1) == '\n' || *(end
- 1) == '>';
1048 lock_token
= xmalloc(end
- start
+ 1);
1049 memcpy(lock_token
, start
, end
- start
);
1050 lock_token
[end
- start
] = 0;
1057 char *lock_remote(char *file
, int timeout
)
1059 struct active_request_slot
*slot
;
1060 struct buffer out_buffer
;
1063 char timeout_header
[25];
1064 struct curl_slist
*dav_headers
= NULL
;
1066 if (lock_token
!= NULL
)
1069 out_buffer
.size
= strlen(LOCK_REQUEST
) + strlen(git_default_email
) - 2;
1070 out_data
= xmalloc(out_buffer
.size
+ 1);
1071 snprintf(out_data
, out_buffer
.size
+ 1, LOCK_REQUEST
, git_default_email
);
1072 out_buffer
.posn
= 0;
1073 out_buffer
.buffer
= out_data
;
1075 sprintf(timeout_header
, "Timeout: Second-%d", timeout
);
1076 url
= xmalloc(strlen(remote
->url
) + strlen(file
) + 1);
1077 sprintf(url
, "%s%s", remote
->url
, file
);
1078 dav_headers
= curl_slist_append(dav_headers
, timeout_header
);
1079 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1081 slot
= get_active_slot();
1082 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1083 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.size
);
1084 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1085 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1086 curl_easy_setopt(slot
->curl
, CURLOPT_HEADERFUNCTION
,
1087 process_lock_header
);
1088 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1089 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1090 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_LOCK
);
1091 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1093 if (start_active_slot(slot
)) {
1094 run_active_slot(slot
);
1096 if (slot
->curl_result
!= CURLE_OK
) {
1097 fprintf(stderr
, "Got HTTP error %ld\n", slot
->http_code
);
1102 fprintf(stderr
, "Unable to start request\n");
1105 return strdup(lock_token
);
1108 int unlock_remote(char *file
, char *lock_token
)
1110 struct active_request_slot
*slot
;
1112 char *lock_token_header
;
1113 struct curl_slist
*dav_headers
= NULL
;
1116 if (lock_token
== NULL
) {
1117 fprintf(stderr
, "Unable to unlock, no lock token");
1121 lock_token_header
= xmalloc(strlen(lock_token
) + 31);
1122 sprintf(lock_token_header
, "Lock-Token: <opaquelocktoken:%s>",
1124 url
= xmalloc(strlen(remote
->url
) + strlen(file
) + 1);
1125 sprintf(url
, "%s%s", remote
->url
, file
);
1126 dav_headers
= curl_slist_append(dav_headers
, lock_token_header
);
1128 slot
= get_active_slot();
1129 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1130 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1131 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_UNLOCK
);
1132 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1134 if (start_active_slot(slot
)) {
1135 run_active_slot(slot
);
1136 if (slot
->curl_result
== CURLE_OK
)
1139 fprintf(stderr
, "Got HTTP error %ld\n",
1142 fprintf(stderr
, "Unable to start request\n");
1145 curl_slist_free_all(dav_headers
);
1146 free(lock_token_header
);
1154 struct active_request_slot
*slot
;
1155 struct buffer in_buffer
;
1156 struct buffer out_buffer
;
1159 XML_Parser parser
= XML_ParserCreate(NULL
);
1160 enum XML_Status result
;
1161 struct lockprop supported_lock
;
1162 struct curl_slist
*dav_headers
= NULL
;
1164 out_buffer
.size
= strlen(PROPFIND_REQUEST
) + strlen(remote
->url
) - 2;
1165 out_data
= xmalloc(out_buffer
.size
+ 1);
1166 snprintf(out_data
, out_buffer
.size
+ 1, PROPFIND_REQUEST
, remote
->url
);
1167 out_buffer
.posn
= 0;
1168 out_buffer
.buffer
= out_data
;
1170 in_buffer
.size
= 4096;
1171 in_data
= xmalloc(in_buffer
.size
);
1173 in_buffer
.buffer
= in_data
;
1175 dav_headers
= curl_slist_append(dav_headers
, "Depth: 0");
1176 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1178 slot
= get_active_slot();
1179 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1180 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.size
);
1181 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1182 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1183 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
1184 fwrite_buffer_dynamic
);
1185 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, remote
->url
);
1186 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1187 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PROPFIND
);
1188 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1190 if (start_active_slot(slot
)) {
1191 run_active_slot(slot
);
1193 if (slot
->curl_result
!= CURLE_OK
) {
1194 free(in_buffer
.buffer
);
1198 XML_SetUserData(parser
, &supported_lock
);
1199 XML_SetElementHandler(parser
, start_lockprop_element
,
1200 end_lockprop_element
);
1201 result
= XML_Parse(parser
, in_buffer
.buffer
, in_buffer
.posn
, 1);
1202 free(in_buffer
.buffer
);
1203 if (result
!= XML_STATUS_OK
)
1204 return error("%s", XML_ErrorString(
1205 XML_GetErrorCode(parser
)));
1208 free(in_buffer
.buffer
);
1209 return error("Unable to start request");
1212 if (supported_lock
.lock_exclusive_write
)
1218 int is_ancestor(unsigned char *sha1
, struct commit
*commit
)
1220 struct commit_list
*parents
;
1222 if (parse_commit(commit
))
1224 parents
= commit
->parents
;
1225 for (; parents
; parents
= parents
->next
) {
1226 if (!memcmp(sha1
, parents
->item
->object
.sha1
, 20)) {
1228 } else if (parents
->item
->object
.type
== commit_type
) {
1231 (struct commit
*)&parents
->item
->object
1239 void get_delta(unsigned char *sha1
, struct object
*obj
, char *lock_token
)
1241 struct commit
*commit
;
1242 struct commit_list
*parents
;
1244 struct tree_entry_list
*entry
;
1246 if (sha1
&& !memcmp(sha1
, obj
->sha1
, 20))
1252 if (obj
->type
== commit_type
) {
1254 fprintf(stderr
, "walk %s\n", sha1_to_hex(obj
->sha1
));
1255 add_request(obj
->sha1
, lock_token
);
1256 commit
= (struct commit
*)obj
;
1257 if (parse_commit(commit
)) {
1258 fprintf(stderr
, "Error parsing commit %s\n",
1259 sha1_to_hex(obj
->sha1
));
1263 parents
= commit
->parents
;
1264 for (; parents
; parents
= parents
->next
)
1266 memcmp(sha1
, parents
->item
->object
.sha1
, 20))
1267 get_delta(sha1
, &parents
->item
->object
,
1269 get_delta(sha1
, &commit
->tree
->object
, lock_token
);
1270 } else if (obj
->type
== tree_type
) {
1272 fprintf(stderr
, "walk %s\n", sha1_to_hex(obj
->sha1
));
1273 add_request(obj
->sha1
, lock_token
);
1274 tree
= (struct tree
*)obj
;
1275 if (parse_tree(tree
)) {
1276 fprintf(stderr
, "Error parsing tree %s\n",
1277 sha1_to_hex(obj
->sha1
));
1281 entry
= tree
->entries
;
1282 tree
->entries
= NULL
;
1284 struct tree_entry_list
*next
= entry
->next
;
1285 get_delta(sha1
, entry
->item
.any
, lock_token
);
1290 } else if (obj
->type
== blob_type
|| obj
->type
== tag_type
) {
1291 add_request(obj
->sha1
, lock_token
);
1295 int update_remote(char *remote_path
, unsigned char *sha1
, char *lock_token
)
1297 struct active_request_slot
*slot
;
1301 struct buffer out_buffer
;
1302 struct curl_slist
*dav_headers
= NULL
;
1305 url
= xmalloc(strlen(remote
->url
) + strlen(remote_path
) + 1);
1306 sprintf(url
, "%s%s", remote
->url
, remote_path
);
1308 if_header
= xmalloc(strlen(lock_token
) + 25);
1309 sprintf(if_header
, "If: (<opaquelocktoken:%s>)", lock_token
);
1310 dav_headers
= curl_slist_append(dav_headers
, if_header
);
1312 out_buffer
.size
= 41;
1313 out_data
= xmalloc(out_buffer
.size
+ 1);
1314 i
= snprintf(out_data
, out_buffer
.size
+ 1, "%s\n", sha1_to_hex(sha1
));
1315 if (i
!= out_buffer
.size
) {
1316 fprintf(stderr
, "Unable to initialize PUT request body\n");
1319 out_buffer
.posn
= 0;
1320 out_buffer
.buffer
= out_data
;
1322 slot
= get_active_slot();
1323 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1324 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.size
);
1325 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1326 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1327 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
1328 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1329 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1330 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
1331 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1333 if (start_active_slot(slot
)) {
1334 run_active_slot(slot
);
1338 if (slot
->curl_result
!= CURLE_OK
) {
1340 "PUT error: curl result=%d, HTTP code=%ld\n",
1341 slot
->curl_result
, slot
->http_code
);
1342 /* We should attempt recovery? */
1349 fprintf(stderr
, "Unable to start PUT request\n");
1356 int main(int argc
, char **argv
)
1358 struct active_request_slot
*slot
;
1359 struct active_request_slot
*next_slot
;
1360 struct transfer_request
*request
;
1361 struct transfer_request
*next_request
;
1363 char **refspec
= NULL
;
1364 int do_remote_update
;
1368 unsigned char local_sha1
[20];
1369 struct object
*local_object
= NULL
;
1370 char *remote_ref
= NULL
;
1371 unsigned char remote_sha1
[20];
1372 char *remote_lock
= NULL
;
1373 char *remote_path
= NULL
;
1374 char *low_speed_limit
;
1375 char *low_speed_time
;
1381 remote
= xmalloc(sizeof(*remote
));
1383 remote
->packs
= NULL
;
1386 for (i
= 1; i
< argc
; i
++, argv
++) {
1390 if (!strcmp(arg
, "--complete")) {
1394 if (!strcmp(arg
, "--force")) {
1398 if (!strcmp(arg
, "--verbose")) {
1402 usage(http_push_usage
);
1409 nr_refspec
= argc
- i
;
1413 curl_global_init(CURL_GLOBAL_ALL
);
1415 #ifdef USE_CURL_MULTI
1417 char *http_max_requests
= getenv("GIT_HTTP_MAX_REQUESTS");
1418 if (http_max_requests
!= NULL
)
1419 max_requests
= atoi(http_max_requests
);
1422 curlm
= curl_multi_init();
1423 if (curlm
== NULL
) {
1424 fprintf(stderr
, "Error creating curl multi handle.\n");
1429 if (getenv("GIT_SSL_NO_VERIFY"))
1430 curl_ssl_verify
= 0;
1432 ssl_cert
= getenv("GIT_SSL_CERT");
1433 #if LIBCURL_VERSION_NUM >= 0x070902
1434 ssl_key
= getenv("GIT_SSL_KEY");
1436 #if LIBCURL_VERSION_NUM >= 0x070908
1437 ssl_capath
= getenv("GIT_SSL_CAPATH");
1439 ssl_cainfo
= getenv("GIT_SSL_CAINFO");
1441 low_speed_limit
= getenv("GIT_HTTP_LOW_SPEED_LIMIT");
1442 if (low_speed_limit
!= NULL
)
1443 curl_low_speed_limit
= strtol(low_speed_limit
, NULL
, 10);
1444 low_speed_time
= getenv("GIT_HTTP_LOW_SPEED_TIME");
1445 if (low_speed_time
!= NULL
)
1446 curl_low_speed_time
= strtol(low_speed_time
, NULL
, 10);
1448 git_config(http_options
);
1450 if (curl_ssl_verify
== -1)
1451 curl_ssl_verify
= 1;
1453 #ifdef USE_CURL_MULTI
1454 if (max_requests
< 1)
1455 max_requests
= DEFAULT_MAX_REQUESTS
;
1458 no_pragma_header
= curl_slist_append(no_pragma_header
, "Pragma:");
1459 default_headers
= curl_slist_append(default_headers
, "Range:");
1460 default_headers
= curl_slist_append(default_headers
, "Destination:");
1461 default_headers
= curl_slist_append(default_headers
, "If:");
1462 default_headers
= curl_slist_append(default_headers
,
1463 "Pragma: no-cache");
1465 #ifndef NO_CURL_EASY_DUPHANDLE
1466 curl_default
= get_curl_handle();
1469 /* Verify DAV compliance/lock support */
1470 if (check_locking() != 0) {
1471 fprintf(stderr
, "Error: no DAV locking support on remote repo %s\n", remote
->url
);
1476 /* Process each refspec */
1477 for (i
= 0; i
< nr_refspec
; i
++) {
1480 do_remote_update
= 0;
1482 local_ref
= refspec
[i
];
1483 if (*local_ref
== '+') {
1487 ep
= strchr(local_ref
, ':');
1489 remote_ref
= ep
+ 1;
1493 remote_ref
= local_ref
;
1495 /* Lock remote branch ref */
1498 remote_path
= xmalloc(strlen(remote_ref
) + 12);
1499 sprintf(remote_path
, "refs/heads/%s", remote_ref
);
1500 remote_lock
= lock_remote(remote_path
, 3600);
1501 if (remote_lock
== NULL
) {
1502 fprintf(stderr
, "Unable to lock remote branch %s\n",
1508 /* Resolve local and remote refs */
1509 if (fetch_ref(remote_ref
, remote_sha1
) != 0) {
1511 "Remote branch %s does not exist on %s\n",
1512 remote_ref
, remote
->url
);
1515 if (get_sha1(local_ref
, local_sha1
) != 0) {
1516 fprintf(stderr
, "Error resolving local branch %s\n",
1522 /* Find relationship between local and remote */
1523 local_object
= parse_object(local_sha1
);
1524 if (!local_object
) {
1525 fprintf(stderr
, "Unable to parse local object %s\n",
1526 sha1_to_hex(local_sha1
));
1529 } else if (new_branch
) {
1530 do_remote_update
= 1;
1532 if (!memcmp(local_sha1
, remote_sha1
, 20)) {
1534 "* %s: same as branch '%s' of %s\n",
1535 local_ref
, remote_ref
, remote
->url
);
1536 } else if (is_ancestor(remote_sha1
,
1537 (struct commit
*)local_object
)) {
1539 "Remote %s will fast-forward to local %s\n",
1540 remote_ref
, local_ref
);
1541 do_remote_update
= 1;
1542 } else if (force_all
|| force_this
) {
1544 "* %s on %s does not fast forward to local branch '%s', overwriting\n",
1545 remote_ref
, remote
->url
, local_ref
);
1546 do_remote_update
= 1;
1549 "* %s on %s does not fast forward to local branch '%s'\n",
1550 remote_ref
, remote
->url
, local_ref
);
1556 /* Generate and check list of required objects */
1558 if (do_remote_update
|| push_all
)
1560 get_delta(push_all
? NULL
: remote_sha1
,
1561 local_object
, remote_lock
);
1562 process_waiting_requests();
1564 /* Push missing objects to remote, this would be a
1565 convenient time to pack them first if appropriate. */
1567 process_request_queue();
1568 process_waiting_requests();
1570 /* Update the remote branch if all went well */
1571 if (do_remote_update
) {
1572 if (!aborted
&& update_remote(remote_path
,
1575 fprintf(stderr
, "%s remote branch %s\n",
1576 new_branch
? "Created" : "Updated",
1580 "Unable to %s remote branch %s\n",
1581 new_branch
? "create" : "update",
1589 unlock_remote(remote_path
, remote_lock
);
1597 curl_slist_free_all(no_pragma_header
);
1598 curl_slist_free_all(default_headers
);
1600 slot
= active_queue_head
;
1601 while (slot
!= NULL
) {
1602 next_slot
= slot
->next
;
1603 if (slot
->curl
!= NULL
)
1604 curl_easy_cleanup(slot
->curl
);
1609 request
= request_queue_head
;
1610 while (request
!= NULL
) {
1611 next_request
= request
->next
;
1612 release_request(request
);
1614 request
= next_request
;
1617 #ifndef NO_CURL_EASY_DUPHANDLE
1618 curl_easy_cleanup(curl_default
);
1620 #ifdef USE_CURL_MULTI
1621 curl_multi_cleanup(curlm
);
1623 curl_global_cleanup();