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 packed_git *target;
732 while (request != NULL && memcmp(request->sha1, sha1, 20))
733 request = request->next;
737 target = find_sha1_pack(sha1, remote->packs);
741 request = xmalloc(sizeof(*request));
742 memcpy(request->sha1, sha1, 20);
744 request->lock_token = lock_token;
745 request->headers = NULL;
746 request->state = NEED_CHECK;
747 request->next = request_queue_head;
748 request_queue_head = request;
749 #ifdef USE_CURL_MULTI
750 process_request_queue();
751 process_curl_messages();
755 static int fetch_index(unsigned char *sha1)
757 char *hex = sha1_to_hex(sha1);
760 char tmpfile[PATH_MAX];
762 char range[RANGE_HEADER_SIZE];
763 struct curl_slist *range_header = NULL;
766 struct active_request_slot *slot;
768 /* Don't use the index if the pack isn't there */
769 url = xmalloc(strlen(remote->url) + 65);
770 sprintf(url, "%s/objects/pack/pack-%s.pack", remote->url, hex);
771 slot = get_active_slot();
772 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
773 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
774 if (start_active_slot(slot)) {
775 run_active_slot(slot);
776 if (slot->curl_result != CURLE_OK) {
778 return error("Unable to verify pack %s is available",
782 return error("Unable to start request");
785 if (has_pack_index(sha1))
789 fprintf(stderr, "Getting index for pack %s\n", hex);
791 sprintf(url, "%s/objects/pack/pack-%s.idx", remote->url, hex);
793 filename = sha1_pack_index_name(sha1);
794 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
795 indexfile = fopen(tmpfile, "a");
797 return error("Unable to open local file %s for pack index",
800 slot = get_active_slot();
801 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
802 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
803 curl_easy_setopt(slot->curl, CURLOPT_FILE, indexfile);
804 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
805 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
806 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
807 slot->local = indexfile;
809 /* If there is data present from a previous transfer attempt,
810 resume where it left off */
811 prev_posn = ftell(indexfile);
815 "Resuming fetch of index for pack %s at byte %ld\n",
817 sprintf(range, "Range: bytes=%ld-", prev_posn);
818 range_header = curl_slist_append(range_header, range);
819 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
822 if (start_active_slot(slot)) {
823 run_active_slot(slot);
824 if (slot->curl_result != CURLE_OK) {
827 return error("Unable to get pack index %s\n%s", url,
832 return error("Unable to start request");
838 return move_temp_to_file(tmpfile, filename);
841 static int setup_index(unsigned char *sha1)
843 struct packed_git *new_pack;
845 if (fetch_index(sha1))
848 new_pack = parse_pack_index(sha1);
849 new_pack->next = remote->packs;
850 remote->packs = new_pack;
854 static int fetch_indices()
856 unsigned char sha1[20];
858 struct buffer buffer;
862 struct active_request_slot *slot;
864 data = xmalloc(4096);
865 memset(data, 0, 4096);
868 buffer.buffer = data;
871 fprintf(stderr, "Getting pack list\n");
873 url = xmalloc(strlen(remote->url) + 21);
874 sprintf(url, "%s/objects/info/packs", remote->url);
876 slot = get_active_slot();
877 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
878 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
879 fwrite_buffer_dynamic);
880 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
881 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
882 if (start_active_slot(slot)) {
883 run_active_slot(slot);
884 if (slot->curl_result != CURLE_OK) {
887 if (slot->http_code == 404)
890 return error("%s", curl_errorstr);
895 return error("Unable to start request");
899 data = buffer.buffer;
900 while (i < buffer.posn) {
904 if (i + 52 < buffer.posn &&
905 !strncmp(data + i, " pack-", 6) &&
906 !strncmp(data + i + 46, ".pack\n", 6)) {
907 get_sha1_hex(data + i + 6, sha1);
913 while (data[i] != '\n')
923 static inline int needs_quote(int ch)
926 case '/': case '-': case '.':
927 case 'A'...'Z': case 'a'...'z': case '0'...'9':
934 static inline int hex(int v)
936 if (v < 10) return '0' + v;
937 else return 'A' + v - 10;
940 static char *quote_ref_url(const char *base, const char *ref)
944 int len, baselen, ch;
946 baselen = strlen(base);
947 len = baselen + 12; /* "refs/heads/" + NUL */
948 for (cp = ref; (ch = *cp) != 0; cp++, len++)
950 len += 2; /* extra two hex plus replacement % */
952 memcpy(qref, base, baselen);
953 memcpy(qref + baselen, "refs/heads/", 11);
954 for (cp = ref, dp = qref + baselen + 11; (ch = *cp) != 0; cp++) {
955 if (needs_quote(ch)) {
957 *dp++ = hex((ch >> 4) & 0xF);
958 *dp++ = hex(ch & 0xF);
968 int fetch_ref(char *ref, unsigned char *sha1)
972 struct buffer buffer;
973 char *base = remote->url;
974 struct active_request_slot *slot;
980 url = quote_ref_url(base, ref);
981 slot = get_active_slot();
982 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
983 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
984 fwrite_buffer_dynamic);
985 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
986 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
987 if (start_active_slot(slot)) {
988 run_active_slot(slot);
989 if (slot->curl_result != CURLE_OK)
990 return error("Couldn't get %s for %s\n%s",
991 url, ref, curl_errorstr);
993 return error("Unable to start request");
997 get_sha1_hex(hex, sha1);
1002 start_lockprop_element(void *userData, const char *name, const char **atts)
1004 struct lockprop *prop = (struct lockprop *)userData;
1006 if (prop->lock_type && !strcmp(name, "D:write")) {
1007 if (prop->lock_exclusive) {
1008 prop->lock_exclusive_write = 1;
1010 } else if (prop->lock_scope && !strcmp(name, "D:exclusive")) {
1011 prop->lock_exclusive = 1;
1012 } else if (prop->lock_entry) {
1013 if (!strcmp(name, "D:lockscope")) {
1014 prop->lock_scope = 1;
1015 } else if (!strcmp(name, "D:locktype")) {
1016 prop->lock_type = 1;
1018 } else if (prop->supported_lock) {
1019 if (!strcmp(name, "D:lockentry")) {
1020 prop->lock_entry = 1;
1022 } else if (!strcmp(name, "D:supportedlock")) {
1023 prop->supported_lock = 1;
1028 end_lockprop_element(void *userData, const char *name)
1030 struct lockprop *prop = (struct lockprop *)userData;
1032 if (!strcmp(name, "D:lockentry")) {
1033 prop->lock_entry = 0;
1034 prop->lock_scope = 0;
1035 prop->lock_type = 0;
1036 prop->lock_exclusive = 0;
1037 } else if (!strcmp(name, "D:supportedlock")) {
1038 prop->supported_lock = 0;
1042 size_t process_lock_header( void *ptr, size_t size, size_t nmemb, void *stream)
1044 size_t header_size = size*nmemb;
1048 if (!strncmp(ptr, "Lock-Token: <opaquelocktoken:", 29)) {
1050 for (end = ptr + header_size;
1051 *(end - 1) == '\r' || *(end - 1) == '\n' || *(end - 1) == '>';
1054 lock_token = xmalloc(end - start + 1);
1055 memcpy(lock_token, start, end - start);
1056 lock_token[end - start] = 0;
1063 char *lock_remote(char *file, int timeout)
1065 struct active_request_slot *slot;
1066 struct buffer out_buffer;
1069 char timeout_header[25];
1070 struct curl_slist *dav_headers = NULL;
1072 if (lock_token != NULL)
1075 out_buffer.size = strlen(LOCK_REQUEST) + strlen(git_default_email) - 2;
1076 out_data = xmalloc(out_buffer.size + 1);
1077 snprintf(out_data, out_buffer.size + 1, LOCK_REQUEST, git_default_email);
1078 out_buffer.posn = 0;
1079 out_buffer.buffer = out_data;
1081 sprintf(timeout_header, "Timeout: Second-%d", timeout);
1082 url = xmalloc(strlen(remote->url) + strlen(file) + 1);
1083 sprintf(url, "%s%s", remote->url, file);
1084 dav_headers = curl_slist_append(dav_headers, timeout_header);
1085 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1087 slot = get_active_slot();
1088 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1089 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size);
1090 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1091 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1092 curl_easy_setopt(slot->curl, CURLOPT_HEADERFUNCTION,
1093 process_lock_header);
1094 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1095 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1096 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
1097 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1099 if (start_active_slot(slot)) {
1100 run_active_slot(slot);
1102 if (slot->curl_result != CURLE_OK) {
1103 fprintf(stderr, "Got HTTP error %ld\n", slot->http_code);
1108 fprintf(stderr, "Unable to start request\n");
1111 return strdup(lock_token);
1114 int unlock_remote(char *file, char *lock_token)
1116 struct active_request_slot *slot;
1118 char *lock_token_header;
1119 struct curl_slist *dav_headers = NULL;
1122 if (lock_token == NULL) {
1123 fprintf(stderr, "Unable to unlock, no lock token");
1127 lock_token_header = xmalloc(strlen(lock_token) + 31);
1128 sprintf(lock_token_header, "Lock-Token: <opaquelocktoken:%s>",
1130 url = xmalloc(strlen(remote->url) + strlen(file) + 1);
1131 sprintf(url, "%s%s", remote->url, file);
1132 dav_headers = curl_slist_append(dav_headers, lock_token_header);
1134 slot = get_active_slot();
1135 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1136 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1137 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_UNLOCK);
1138 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1140 if (start_active_slot(slot)) {
1141 run_active_slot(slot);
1142 if (slot->curl_result == CURLE_OK)
1145 fprintf(stderr, "Got HTTP error %ld\n",
1148 fprintf(stderr, "Unable to start request\n");
1151 curl_slist_free_all(dav_headers);
1152 free(lock_token_header);
1160 struct active_request_slot *slot;
1161 struct buffer in_buffer;
1162 struct buffer out_buffer;
1165 XML_Parser parser = XML_ParserCreate(NULL);
1166 enum XML_Status result;
1167 struct lockprop supported_lock;
1168 struct curl_slist *dav_headers = NULL;
1170 out_buffer.size = strlen(PROPFIND_REQUEST) + strlen(remote->url) - 2;
1171 out_data = xmalloc(out_buffer.size + 1);
1172 snprintf(out_data, out_buffer.size + 1, PROPFIND_REQUEST, remote->url);
1173 out_buffer.posn = 0;
1174 out_buffer.buffer = out_data;
1176 in_buffer.size = 4096;
1177 in_data = xmalloc(in_buffer.size);
1179 in_buffer.buffer = in_data;
1181 dav_headers = curl_slist_append(dav_headers, "Depth: 0");
1182 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1184 slot = get_active_slot();
1185 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1186 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size);
1187 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1188 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1189 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
1190 fwrite_buffer_dynamic);
1191 curl_easy_setopt(slot->curl, CURLOPT_URL, remote->url);
1192 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1193 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1194 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1196 if (start_active_slot(slot)) {
1197 run_active_slot(slot);
1199 if (slot->curl_result != CURLE_OK) {
1200 free(in_buffer.buffer);
1204 XML_SetUserData(parser, &supported_lock);
1205 XML_SetElementHandler(parser, start_lockprop_element,
1206 end_lockprop_element);
1207 result = XML_Parse(parser, in_buffer.buffer, in_buffer.posn, 1);
1208 free(in_buffer.buffer);
1209 if (result != XML_STATUS_OK)
1210 return error("%s", XML_ErrorString(
1211 XML_GetErrorCode(parser)));
1214 free(in_buffer.buffer);
1215 return error("Unable to start request");
1218 if (supported_lock.lock_exclusive_write)
1224 int is_ancestor(unsigned char *sha1, struct commit *commit)
1226 struct commit_list *parents;
1228 if (parse_commit(commit))
1230 parents = commit->parents;
1231 for (; parents; parents = parents->next) {
1232 if (!memcmp(sha1, parents->item->object.sha1, 20)) {
1234 } else if (parents->item->object.type == commit_type) {
1237 (struct commit *)&parents->item->object
1245 void get_delta(unsigned char *sha1, struct object *obj, char *lock_token)
1247 struct commit *commit;
1248 struct commit_list *parents;
1250 struct tree_entry_list *entry;
1252 if (sha1 && !memcmp(sha1, obj->sha1, 20))
1258 if (obj->type == commit_type) {
1260 fprintf(stderr, "walk %s\n", sha1_to_hex(obj->sha1));
1261 add_request(obj->sha1, lock_token);
1262 commit = (struct commit *)obj;
1263 if (parse_commit(commit)) {
1264 fprintf(stderr, "Error parsing commit %s\n",
1265 sha1_to_hex(obj->sha1));
1269 parents = commit->parents;
1270 for (; parents; parents = parents->next)
1272 memcmp(sha1, parents->item->object.sha1, 20))
1273 get_delta(sha1, &parents->item->object,
1275 get_delta(sha1, &commit->tree->object, lock_token);
1276 } else if (obj->type == tree_type) {
1278 fprintf(stderr, "walk %s\n", sha1_to_hex(obj->sha1));
1279 add_request(obj->sha1, lock_token);
1280 tree = (struct tree *)obj;
1281 if (parse_tree(tree)) {
1282 fprintf(stderr, "Error parsing tree %s\n",
1283 sha1_to_hex(obj->sha1));
1287 entry = tree->entries;
1288 tree->entries = NULL;
1290 struct tree_entry_list *next = entry->next;
1291 get_delta(sha1, entry->item.any, lock_token);
1296 } else if (obj->type == blob_type || obj->type == tag_type) {
1297 add_request(obj->sha1, lock_token);
1301 int update_remote(char *remote_path, unsigned char *sha1, char *lock_token)
1303 struct active_request_slot *slot;
1307 struct buffer out_buffer;
1308 struct curl_slist *dav_headers = NULL;
1311 url = xmalloc(strlen(remote->url) + strlen(remote_path) + 1);
1312 sprintf(url, "%s%s", remote->url, remote_path);
1314 if_header = xmalloc(strlen(lock_token) + 25);
1315 sprintf(if_header, "If: (<opaquelocktoken:%s>)", lock_token);
1316 dav_headers = curl_slist_append(dav_headers, if_header);
1318 out_buffer.size = 41;
1319 out_data = xmalloc(out_buffer.size + 1);
1320 i = snprintf(out_data, out_buffer.size + 1, "%s\n", sha1_to_hex(sha1));
1321 if (i != out_buffer.size) {
1322 fprintf(stderr, "Unable to initialize PUT request body\n");
1325 out_buffer.posn = 0;
1326 out_buffer.buffer = out_data;
1328 slot = get_active_slot();
1329 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1330 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size);
1331 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1332 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1333 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1334 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1335 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1336 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1337 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1339 if (start_active_slot(slot)) {
1340 run_active_slot(slot);
1344 if (slot->curl_result != CURLE_OK) {
1346 "PUT error: curl result=%d, HTTP code=%ld\n",
1347 slot->curl_result, slot->http_code);
1348 /* We should attempt recovery? */
1355 fprintf(stderr, "Unable to start PUT request\n");
1362 int main(int argc, char **argv)
1364 struct active_request_slot *slot;
1365 struct active_request_slot *next_slot;
1366 struct transfer_request *request;
1367 struct transfer_request *next_request;
1369 char **refspec = NULL;
1370 int do_remote_update;
1374 unsigned char local_sha1[20];
1375 struct object *local_object = NULL;
1376 char *remote_ref = NULL;
1377 unsigned char remote_sha1[20];
1378 char *remote_lock = NULL;
1379 char *remote_path = NULL;
1380 char *low_speed_limit;
1381 char *low_speed_time;
1387 remote = xmalloc(sizeof(*remote));
1389 remote->packs = NULL;
1392 for (i = 1; i < argc; i++, argv++) {
1396 if (!strcmp(arg, "--complete")) {
1400 if (!strcmp(arg, "--force")) {
1404 if (!strcmp(arg, "--verbose")) {
1408 usage(http_push_usage);
1415 nr_refspec = argc - i;
1419 curl_global_init(CURL_GLOBAL_ALL);
1421 #ifdef USE_CURL_MULTI
1423 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
1424 if (http_max_requests != NULL)
1425 max_requests = atoi(http_max_requests);
1428 curlm = curl_multi_init();
1429 if (curlm == NULL) {
1430 fprintf(stderr, "Error creating curl multi handle.\n");
1435 if (getenv("GIT_SSL_NO_VERIFY"))
1436 curl_ssl_verify = 0;
1438 ssl_cert = getenv("GIT_SSL_CERT");
1439 #if LIBCURL_VERSION_NUM >= 0x070902
1440 ssl_key = getenv("GIT_SSL_KEY");
1442 #if LIBCURL_VERSION_NUM >= 0x070908
1443 ssl_capath = getenv("GIT_SSL_CAPATH");
1445 ssl_cainfo = getenv("GIT_SSL_CAINFO");
1447 low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
1448 if (low_speed_limit != NULL)
1449 curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
1450 low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
1451 if (low_speed_time != NULL)
1452 curl_low_speed_time = strtol(low_speed_time, NULL, 10);
1454 git_config(http_options);
1456 if (curl_ssl_verify == -1)
1457 curl_ssl_verify = 1;
1459 #ifdef USE_CURL_MULTI
1460 if (max_requests < 1)
1461 max_requests = DEFAULT_MAX_REQUESTS;
1464 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
1465 default_headers = curl_slist_append(default_headers, "Range:");
1466 default_headers = curl_slist_append(default_headers, "Destination:");
1467 default_headers = curl_slist_append(default_headers, "If:");
1468 default_headers = curl_slist_append(default_headers,
1469 "Pragma: no-cache");
1471 #ifndef NO_CURL_EASY_DUPHANDLE
1472 curl_default = get_curl_handle();
1475 /* Verify DAV compliance/lock support */
1476 if (check_locking() != 0) {
1477 fprintf(stderr, "Error: no DAV locking support on remote repo %s\n", remote->url);
1482 /* Process each refspec */
1483 for (i = 0; i < nr_refspec; i++) {
1486 do_remote_update = 0;
1488 local_ref = refspec[i];
1489 if (*local_ref == '+') {
1493 ep = strchr(local_ref, ':');
1495 remote_ref = ep + 1;
1499 remote_ref = local_ref;
1501 /* Lock remote branch ref */
1504 remote_path = xmalloc(strlen(remote_ref) + 12);
1505 sprintf(remote_path, "refs/heads/%s", remote_ref);
1506 remote_lock = lock_remote(remote_path, 3600);
1507 if (remote_lock == NULL) {
1508 fprintf(stderr, "Unable to lock remote branch %s\n",
1514 /* Resolve local and remote refs */
1515 if (fetch_ref(remote_ref, remote_sha1) != 0) {
1517 "Remote branch %s does not exist on %s\n",
1518 remote_ref, remote->url);
1521 if (get_sha1(local_ref, local_sha1) != 0) {
1522 fprintf(stderr, "Error resolving local branch %s\n",
1528 /* Find relationship between local and remote */
1529 local_object = parse_object(local_sha1);
1530 if (!local_object) {
1531 fprintf(stderr, "Unable to parse local object %s\n",
1532 sha1_to_hex(local_sha1));
1535 } else if (new_branch) {
1536 do_remote_update = 1;
1538 if (!memcmp(local_sha1, remote_sha1, 20)) {
1540 "* %s: same as branch '%s' of %s\n",
1541 local_ref, remote_ref, remote->url);
1542 } else if (is_ancestor(remote_sha1,
1543 (struct commit *)local_object)) {
1545 "Remote %s will fast-forward to local %s\n",
1546 remote_ref, local_ref);
1547 do_remote_update = 1;
1548 } else if (force_all || force_this) {
1550 "* %s on %s does not fast forward to local branch '%s', overwriting\n",
1551 remote_ref, remote->url, local_ref);
1552 do_remote_update = 1;
1555 "* %s on %s does not fast forward to local branch '%s'\n",
1556 remote_ref, remote->url, local_ref);
1562 /* Generate and check list of required objects */
1564 if (do_remote_update || push_all)
1566 get_delta(push_all ? NULL : remote_sha1,
1567 local_object, remote_lock);
1568 process_waiting_requests();
1570 /* Push missing objects to remote, this would be a
1571 convenient time to pack them first if appropriate. */
1573 process_request_queue();
1574 process_waiting_requests();
1576 /* Update the remote branch if all went well */
1577 if (do_remote_update) {
1578 if (!aborted && update_remote(remote_path,
1581 fprintf(stderr, "%s remote branch %s\n",
1582 new_branch ? "Created" : "Updated",
1586 "Unable to %s remote branch %s\n",
1587 new_branch ? "create" : "update",
1595 unlock_remote(remote_path, remote_lock);
1603 curl_slist_free_all(no_pragma_header);
1604 curl_slist_free_all(default_headers);
1606 slot = active_queue_head;
1607 while (slot != NULL) {
1608 next_slot = slot->next;
1609 if (slot->curl != NULL)
1610 curl_easy_cleanup(slot->curl);
1615 request = request_queue_head;
1616 while (request != NULL) {
1617 next_request = request->next;
1618 release_request(request);
1619 request = next_request;
1622 #ifndef NO_CURL_EASY_DUPHANDLE
1623 curl_easy_cleanup(curl_default);
1625 #ifdef USE_CURL_MULTI
1626 curl_multi_cleanup(curlm);
1628 curl_global_cleanup();