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];
59 static int push_verbosely = 0;
60 static int push_all = 0;
61 static int force_all = 0;
73 struct packed_git *packs;
76 static struct repo *remote = NULL;
89 struct transfer_request
91 unsigned char sha1[20];
94 struct active_lock *lock;
95 struct curl_slist *headers;
97 char filename[PATH_MAX];
98 char tmpfile[PATH_MAX];
99 enum transfer_state state;
100 CURLcode curl_result;
101 char errorstr[CURL_ERROR_SIZE];
103 unsigned char real_sha1[20];
108 struct active_request_slot *slot;
109 struct transfer_request *next;
112 struct active_request_slot
118 CURLcode curl_result;
120 struct active_request_slot *next;
123 static struct transfer_request *request_queue_head = NULL;
124 static struct active_request_slot *active_queue_head = NULL;
126 static int curl_ssl_verify = -1;
127 static char *ssl_cert = NULL;
128 #if LIBCURL_VERSION_NUM >= 0x070902
129 static char *ssl_key = NULL;
131 #if LIBCURL_VERSION_NUM >= 0x070908
132 static char *ssl_capath = NULL;
134 static char *ssl_cainfo = NULL;
135 static long curl_low_speed_limit = -1;
136 static long curl_low_speed_time = -1;
145 int ctx_locktoken_href;
159 int lock_exclusive_write;
162 static int http_options(const char *var, const char *value)
164 if (!strcmp("http.sslverify", var)) {
165 if (curl_ssl_verify == -1) {
166 curl_ssl_verify = git_config_bool(var, value);
171 if (!strcmp("http.sslcert", var)) {
172 if (ssl_cert == NULL) {
173 ssl_cert = xmalloc(strlen(value)+1);
174 strcpy(ssl_cert, value);
178 #if LIBCURL_VERSION_NUM >= 0x070902
179 if (!strcmp("http.sslkey", var)) {
180 if (ssl_key == NULL) {
181 ssl_key = xmalloc(strlen(value)+1);
182 strcpy(ssl_key, value);
187 #if LIBCURL_VERSION_NUM >= 0x070908
188 if (!strcmp("http.sslcapath", var)) {
189 if (ssl_capath == NULL) {
190 ssl_capath = xmalloc(strlen(value)+1);
191 strcpy(ssl_capath, value);
196 if (!strcmp("http.sslcainfo", var)) {
197 if (ssl_cainfo == NULL) {
198 ssl_cainfo = xmalloc(strlen(value)+1);
199 strcpy(ssl_cainfo, value);
204 #ifdef USE_CURL_MULTI
205 if (!strcmp("http.maxrequests", var)) {
206 if (max_requests == -1)
207 max_requests = git_config_int(var, value);
212 if (!strcmp("http.lowspeedlimit", var)) {
213 if (curl_low_speed_limit == -1)
214 curl_low_speed_limit = (long)git_config_int(var, value);
217 if (!strcmp("http.lowspeedtime", var)) {
218 if (curl_low_speed_time == -1)
219 curl_low_speed_time = (long)git_config_int(var, value);
223 /* Fall back on the default ones */
224 return git_default_config(var, value);
227 static size_t fread_buffer(void *ptr, size_t eltsize, size_t nmemb,
228 struct buffer *buffer)
230 size_t size = eltsize * nmemb;
231 if (size > buffer->size - buffer->posn)
232 size = buffer->size - buffer->posn;
233 memcpy(ptr, buffer->buffer + buffer->posn, size);
234 buffer->posn += size;
238 static size_t fwrite_buffer_dynamic(const void *ptr, size_t eltsize,
239 size_t nmemb, struct buffer *buffer)
241 size_t size = eltsize * nmemb;
242 if (size > buffer->size - buffer->posn) {
243 buffer->size = buffer->size * 3 / 2;
244 if (buffer->size < buffer->posn + size)
245 buffer->size = buffer->posn + size;
246 buffer->buffer = xrealloc(buffer->buffer, buffer->size);
248 memcpy(buffer->buffer + buffer->posn, ptr, size);
249 buffer->posn += size;
254 static size_t fwrite_null(const void *ptr, size_t eltsize,
255 size_t nmemb, struct buffer *buffer)
258 return eltsize * nmemb;
261 #ifdef USE_CURL_MULTI
262 static void process_curl_messages(void);
263 static void process_request_queue(void);
266 static CURL* get_curl_handle(void)
268 CURL* result = curl_easy_init();
270 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify);
271 #if LIBCURL_VERSION_NUM >= 0x070907
272 curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
275 if (ssl_cert != NULL)
276 curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
277 #if LIBCURL_VERSION_NUM >= 0x070902
279 curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
281 #if LIBCURL_VERSION_NUM >= 0x070908
282 if (ssl_capath != NULL)
283 curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
285 if (ssl_cainfo != NULL)
286 curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
287 curl_easy_setopt(result, CURLOPT_FAILONERROR, 1);
289 if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
290 curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
291 curl_low_speed_limit);
292 curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
293 curl_low_speed_time);
299 static struct active_request_slot *get_active_slot(void)
301 struct active_request_slot *slot = active_queue_head;
302 struct active_request_slot *newslot;
304 #ifdef USE_CURL_MULTI
307 /* Wait for a slot to open up if the queue is full */
308 while (active_requests >= max_requests) {
309 curl_multi_perform(curlm, &num_transfers);
310 if (num_transfers < active_requests) {
311 process_curl_messages();
316 while (slot != NULL && slot->in_use) {
320 newslot = xmalloc(sizeof(*newslot));
321 newslot->curl = NULL;
323 newslot->next = NULL;
325 slot = active_queue_head;
327 active_queue_head = newslot;
329 while (slot->next != NULL) {
332 slot->next = newslot;
337 if (slot->curl == NULL) {
338 #ifdef NO_CURL_EASY_DUPHANDLE
339 slot->curl = get_curl_handle();
341 slot->curl = curl_easy_duphandle(curl_default);
349 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, default_headers);
350 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
355 static int start_active_slot(struct active_request_slot *slot)
357 #ifdef USE_CURL_MULTI
358 CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
360 if (curlm_result != CURLM_OK &&
361 curlm_result != CURLM_CALL_MULTI_PERFORM) {
370 static void run_active_slot(struct active_request_slot *slot)
372 #ifdef USE_CURL_MULTI
380 struct timeval select_timeout;
381 CURLMcode curlm_result;
383 while (!slot->done) {
386 curlm_result = curl_multi_perform(curlm,
388 } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
389 if (num_transfers < active_requests) {
390 process_curl_messages();
391 process_request_queue();
394 if (!data_received && slot->local != NULL) {
395 current_pos = ftell(slot->local);
396 if (current_pos > last_pos)
398 last_pos = current_pos;
401 if (!slot->done && !data_received) {
406 select_timeout.tv_sec = 0;
407 select_timeout.tv_usec = 50000;
408 select(max_fd, &readfds, &writefds,
409 &excfds, &select_timeout);
413 slot->curl_result = curl_easy_perform(slot->curl);
418 static void start_check(struct transfer_request *request)
420 char *hex = sha1_to_hex(request->sha1);
421 struct active_request_slot *slot;
424 request->url = xmalloc(strlen(remote->url) + 55);
425 strcpy(request->url, remote->url);
426 posn = request->url + strlen(remote->url);
427 strcpy(posn, "objects/");
429 memcpy(posn, hex, 2);
432 strcpy(posn, hex + 2);
434 slot = get_active_slot();
435 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
436 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
437 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
439 if (start_active_slot(slot)) {
440 request->slot = slot;
441 request->state = RUN_HEAD;
443 request->state = ABORTED;
448 static void start_mkcol(struct transfer_request *request)
450 char *hex = sha1_to_hex(request->sha1);
451 struct active_request_slot *slot;
454 request->url = xmalloc(strlen(remote->url) + 13);
455 strcpy(request->url, remote->url);
456 posn = request->url + strlen(remote->url);
457 strcpy(posn, "objects/");
459 memcpy(posn, hex, 2);
463 slot = get_active_slot();
464 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
465 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
466 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
467 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
468 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
470 if (start_active_slot(slot)) {
471 request->slot = slot;
472 request->state = RUN_MKCOL;
474 request->state = ABORTED;
479 static void start_put(struct transfer_request *request)
481 char *hex = sha1_to_hex(request->sha1);
482 struct active_request_slot *slot;
492 unpacked = read_sha1_file(request->sha1, type, &len);
493 hdrlen = sprintf(hdr, "%s %lu", type, len) + 1;
496 memset(&stream, 0, sizeof(stream));
497 deflateInit(&stream, Z_BEST_COMPRESSION);
498 size = deflateBound(&stream, len + hdrlen);
499 request->buffer.buffer = xmalloc(size);
502 stream.next_out = request->buffer.buffer;
503 stream.avail_out = size;
506 stream.next_in = (void *)hdr;
507 stream.avail_in = hdrlen;
508 while (deflate(&stream, 0) == Z_OK)
511 /* Then the data itself.. */
512 stream.next_in = unpacked;
513 stream.avail_in = len;
514 while (deflate(&stream, Z_FINISH) == Z_OK)
519 request->buffer.size = stream.total_out;
520 request->buffer.posn = 0;
522 if (request->url != NULL)
524 request->url = xmalloc(strlen(remote->url) +
525 strlen(request->lock->token) + 51);
526 strcpy(request->url, remote->url);
527 posn = request->url + strlen(remote->url);
528 strcpy(posn, "objects/");
530 memcpy(posn, hex, 2);
533 strcpy(posn, hex + 2);
534 request->dest = xmalloc(strlen(request->url) + 14);
535 sprintf(request->dest, "Destination: %s", request->url);
538 strcpy(posn, request->lock->token);
540 slot = get_active_slot();
541 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &request->buffer);
542 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, request->buffer.size);
543 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
544 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
545 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
546 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
547 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
548 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
549 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
551 if (start_active_slot(slot)) {
552 request->slot = slot;
553 request->state = RUN_PUT;
555 request->state = ABORTED;
560 static void start_move(struct transfer_request *request)
562 struct active_request_slot *slot;
563 struct curl_slist *dav_headers = NULL;
565 slot = get_active_slot();
566 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
567 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MOVE);
568 dav_headers = curl_slist_append(dav_headers, request->dest);
569 dav_headers = curl_slist_append(dav_headers, "Overwrite: T");
570 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
571 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
572 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
574 if (start_active_slot(slot)) {
575 request->slot = slot;
576 request->state = RUN_MOVE;
578 request->state = ABORTED;
583 static void finish_request(struct transfer_request *request)
585 request->curl_result = request->slot->curl_result;
586 request->http_code = request->slot->http_code;
587 request->slot = NULL;
588 if (request->headers != NULL)
589 curl_slist_free_all(request->headers);
590 if (request->state == RUN_HEAD) {
591 if (request->http_code == 404) {
592 request->state = NEED_PUSH;
593 } else if (request->curl_result == CURLE_OK) {
594 request->state = COMPLETE;
596 fprintf(stderr, "HEAD %s failed, aborting (%d/%ld)\n",
597 sha1_to_hex(request->sha1),
598 request->curl_result, request->http_code);
599 request->state = ABORTED;
602 } else if (request->state == RUN_MKCOL) {
603 if (request->curl_result == CURLE_OK ||
604 request->http_code == 405) {
607 fprintf(stderr, "MKCOL %s failed, aborting (%d/%ld)\n",
608 sha1_to_hex(request->sha1),
609 request->curl_result, request->http_code);
610 request->state = ABORTED;
613 } else if (request->state == RUN_PUT) {
614 if (request->curl_result == CURLE_OK) {
617 fprintf(stderr, "PUT %s failed, aborting (%d/%ld)\n",
618 sha1_to_hex(request->sha1),
619 request->curl_result, request->http_code);
620 request->state = ABORTED;
623 } else if (request->state == RUN_MOVE) {
624 if (request->curl_result == CURLE_OK) {
628 sha1_to_hex(request->sha1));
629 request->state = COMPLETE;
631 fprintf(stderr, "MOVE %s failed, aborting (%d/%ld)\n",
632 sha1_to_hex(request->sha1),
633 request->curl_result, request->http_code);
634 request->state = ABORTED;
640 static void release_request(struct transfer_request *request)
642 struct transfer_request *entry = request_queue_head;
644 if (request == request_queue_head) {
645 request_queue_head = request->next;
647 while (entry->next != NULL && entry->next != request)
649 if (entry->next == request)
650 entry->next = entry->next->next;
657 #ifdef USE_CURL_MULTI
658 void process_curl_messages(void)
661 struct active_request_slot *slot;
662 struct transfer_request *request = NULL;
663 CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
665 while (curl_message != NULL) {
666 if (curl_message->msg == CURLMSG_DONE) {
667 slot = active_queue_head;
668 while (slot != NULL &&
669 slot->curl != curl_message->easy_handle)
672 curl_multi_remove_handle(curlm, slot->curl);
676 slot->curl_result = curl_message->data.result;
677 curl_easy_getinfo(slot->curl,
680 request = request_queue_head;
681 while (request != NULL &&
682 request->slot != slot)
683 request = request->next;
685 finish_request(request);
687 fprintf(stderr, "Received DONE message for unknown request!\n");
690 fprintf(stderr, "Unknown CURL message received: %d\n",
691 (int)curl_message->msg);
693 curl_message = curl_multi_info_read(curlm, &num_messages);
697 void process_request_queue(void)
699 struct transfer_request *request = request_queue_head;
700 struct active_request_slot *slot = active_queue_head;
706 while (active_requests < max_requests && request != NULL) {
707 if (!pushing && request->state == NEED_CHECK) {
708 start_check(request);
709 curl_multi_perform(curlm, &num_transfers);
710 } else if (pushing && request->state == NEED_PUSH) {
711 start_mkcol(request);
712 curl_multi_perform(curlm, &num_transfers);
714 request = request->next;
717 while (slot != NULL) {
718 if (!slot->in_use && slot->curl != NULL) {
719 curl_easy_cleanup(slot->curl);
727 void process_waiting_requests(void)
729 struct active_request_slot *slot = active_queue_head;
733 run_active_slot(slot);
734 slot = active_queue_head;
740 void add_request(unsigned char *sha1, struct active_lock *lock)
742 struct transfer_request *request = request_queue_head;
743 struct packed_git *target;
745 while (request != NULL && memcmp(request->sha1, sha1, 20))
746 request = request->next;
750 target = find_sha1_pack(sha1, remote->packs);
754 request = xmalloc(sizeof(*request));
755 memcpy(request->sha1, sha1, 20);
757 request->lock = lock;
758 request->headers = NULL;
759 request->state = NEED_CHECK;
760 request->next = request_queue_head;
761 request_queue_head = request;
762 #ifdef USE_CURL_MULTI
763 process_request_queue();
764 process_curl_messages();
768 static int fetch_index(unsigned char *sha1)
770 char *hex = sha1_to_hex(sha1);
773 char tmpfile[PATH_MAX];
775 char range[RANGE_HEADER_SIZE];
776 struct curl_slist *range_header = NULL;
779 struct active_request_slot *slot;
781 /* Don't use the index if the pack isn't there */
782 url = xmalloc(strlen(remote->url) + 65);
783 sprintf(url, "%s/objects/pack/pack-%s.pack", remote->url, hex);
784 slot = get_active_slot();
785 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
786 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
787 if (start_active_slot(slot)) {
788 run_active_slot(slot);
789 if (slot->curl_result != CURLE_OK) {
791 return error("Unable to verify pack %s is available",
795 return error("Unable to start request");
798 if (has_pack_index(sha1))
802 fprintf(stderr, "Getting index for pack %s\n", hex);
804 sprintf(url, "%s/objects/pack/pack-%s.idx", remote->url, hex);
806 filename = sha1_pack_index_name(sha1);
807 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
808 indexfile = fopen(tmpfile, "a");
810 return error("Unable to open local file %s for pack index",
813 slot = get_active_slot();
814 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
815 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
816 curl_easy_setopt(slot->curl, CURLOPT_FILE, indexfile);
817 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
818 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
819 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
820 slot->local = indexfile;
822 /* If there is data present from a previous transfer attempt,
823 resume where it left off */
824 prev_posn = ftell(indexfile);
828 "Resuming fetch of index for pack %s at byte %ld\n",
830 sprintf(range, "Range: bytes=%ld-", prev_posn);
831 range_header = curl_slist_append(range_header, range);
832 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
835 if (start_active_slot(slot)) {
836 run_active_slot(slot);
837 if (slot->curl_result != CURLE_OK) {
840 return error("Unable to get pack index %s\n%s", url,
845 return error("Unable to start request");
851 return move_temp_to_file(tmpfile, filename);
854 static int setup_index(unsigned char *sha1)
856 struct packed_git *new_pack;
858 if (fetch_index(sha1))
861 new_pack = parse_pack_index(sha1);
862 new_pack->next = remote->packs;
863 remote->packs = new_pack;
867 static int fetch_indices()
869 unsigned char sha1[20];
871 struct buffer buffer;
875 struct active_request_slot *slot;
877 data = xmalloc(4096);
878 memset(data, 0, 4096);
881 buffer.buffer = data;
884 fprintf(stderr, "Getting pack list\n");
886 url = xmalloc(strlen(remote->url) + 21);
887 sprintf(url, "%s/objects/info/packs", remote->url);
889 slot = get_active_slot();
890 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
891 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
892 fwrite_buffer_dynamic);
893 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
894 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
895 if (start_active_slot(slot)) {
896 run_active_slot(slot);
897 if (slot->curl_result != CURLE_OK) {
900 if (slot->http_code == 404)
903 return error("%s", curl_errorstr);
908 return error("Unable to start request");
912 data = buffer.buffer;
913 while (i < buffer.posn) {
917 if (i + 52 < buffer.posn &&
918 !strncmp(data + i, " pack-", 6) &&
919 !strncmp(data + i + 46, ".pack\n", 6)) {
920 get_sha1_hex(data + i + 6, sha1);
926 while (data[i] != '\n')
936 static inline int needs_quote(int ch)
939 case '/': case '-': case '.':
940 case 'A'...'Z': case 'a'...'z': case '0'...'9':
947 static inline int hex(int v)
949 if (v < 10) return '0' + v;
950 else return 'A' + v - 10;
953 static char *quote_ref_url(const char *base, const char *ref)
957 int len, baselen, ch;
959 baselen = strlen(base);
960 len = baselen + 12; /* "refs/heads/" + NUL */
961 for (cp = ref; (ch = *cp) != 0; cp++, len++)
963 len += 2; /* extra two hex plus replacement % */
965 memcpy(qref, base, baselen);
966 memcpy(qref + baselen, "refs/heads/", 11);
967 for (cp = ref, dp = qref + baselen + 11; (ch = *cp) != 0; cp++) {
968 if (needs_quote(ch)) {
970 *dp++ = hex((ch >> 4) & 0xF);
971 *dp++ = hex(ch & 0xF);
981 int fetch_ref(char *ref, unsigned char *sha1)
985 struct buffer buffer;
986 char *base = remote->url;
987 struct active_request_slot *slot;
993 url = quote_ref_url(base, ref);
994 slot = get_active_slot();
995 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
996 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
997 fwrite_buffer_dynamic);
998 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
999 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1000 if (start_active_slot(slot)) {
1001 run_active_slot(slot);
1002 if (slot->curl_result != CURLE_OK)
1003 return error("Couldn't get %s for %s\n%s",
1004 url, ref, curl_errorstr);
1006 return error("Unable to start request");
1010 get_sha1_hex(hex, sha1);
1015 start_activelock_element(void *userData, const char *name, const char **atts)
1017 struct active_lock *lock = (struct active_lock *)userData;
1019 if (lock->ctx_activelock && !strcmp(name, "D:timeout"))
1020 lock->ctx_timeout = 1;
1021 else if (lock->ctx_owner && strstr(name, "href"))
1022 lock->ctx_owner_href = 1;
1023 else if (lock->ctx_activelock && strstr(name, "owner"))
1024 lock->ctx_owner = 1;
1025 else if (lock->ctx_locktoken && !strcmp(name, "D:href"))
1026 lock->ctx_locktoken_href = 1;
1027 else if (lock->ctx_activelock && !strcmp(name, "D:locktoken"))
1028 lock->ctx_locktoken = 1;
1029 else if (!strcmp(name, "D:activelock"))
1030 lock->ctx_activelock = 1;
1034 end_activelock_element(void *userData, const char *name)
1036 struct active_lock *lock = (struct active_lock *)userData;
1038 if (lock->ctx_timeout && !strcmp(name, "D:timeout")) {
1039 lock->ctx_timeout = 0;
1040 } else if (lock->ctx_owner_href && strstr(name, "href")) {
1041 lock->ctx_owner_href = 0;
1042 } else if (lock->ctx_owner && strstr(name, "owner")) {
1043 lock->ctx_owner = 0;
1044 } else if (lock->ctx_locktoken_href && !strcmp(name, "D:href")) {
1045 lock->ctx_locktoken_href = 0;
1046 } else if (lock->ctx_locktoken && !strcmp(name, "D:locktoken")) {
1047 lock->ctx_locktoken = 0;
1048 } else if (lock->ctx_activelock && !strcmp(name, "D:activelock")) {
1049 lock->ctx_activelock = 0;
1054 activelock_cdata(void *userData, const XML_Char *s, int len)
1056 struct active_lock *lock = (struct active_lock *)userData;
1057 char *this = malloc(len+1);
1058 strncpy(this, s, len);
1060 if (lock->ctx_owner_href) {
1061 lock->owner = malloc(len+1);
1062 strcpy(lock->owner, this);
1063 } else if (lock->ctx_locktoken_href) {
1064 if (!strncmp(this, "opaquelocktoken:", 16)) {
1065 lock->token = malloc(len-15);
1066 strcpy(lock->token, this+16);
1068 } else if (lock->ctx_timeout) {
1069 if (!strncmp(this, "Second-", 7))
1070 lock->timeout = strtol(this+7, NULL, 10);
1077 start_lockprop_element(void *userData, const char *name, const char **atts)
1079 struct lockprop *prop = (struct lockprop *)userData;
1081 if (prop->lock_type && !strcmp(name, "D:write")) {
1082 if (prop->lock_exclusive) {
1083 prop->lock_exclusive_write = 1;
1085 } else if (prop->lock_scope && !strcmp(name, "D:exclusive")) {
1086 prop->lock_exclusive = 1;
1087 } else if (prop->lock_entry) {
1088 if (!strcmp(name, "D:lockscope")) {
1089 prop->lock_scope = 1;
1090 } else if (!strcmp(name, "D:locktype")) {
1091 prop->lock_type = 1;
1093 } else if (prop->supported_lock) {
1094 if (!strcmp(name, "D:lockentry")) {
1095 prop->lock_entry = 1;
1097 } else if (!strcmp(name, "D:supportedlock")) {
1098 prop->supported_lock = 1;
1103 end_lockprop_element(void *userData, const char *name)
1105 struct lockprop *prop = (struct lockprop *)userData;
1107 if (!strcmp(name, "D:lockentry")) {
1108 prop->lock_entry = 0;
1109 prop->lock_scope = 0;
1110 prop->lock_type = 0;
1111 prop->lock_exclusive = 0;
1112 } else if (!strcmp(name, "D:supportedlock")) {
1113 prop->supported_lock = 0;
1117 struct active_lock *lock_remote(char *file, int timeout)
1119 struct active_request_slot *slot;
1120 struct buffer out_buffer;
1121 struct buffer in_buffer;
1126 char timeout_header[25];
1127 struct active_lock *new_lock;
1128 XML_Parser parser = XML_ParserCreate(NULL);
1129 enum XML_Status result;
1130 struct curl_slist *dav_headers = NULL;
1132 url = xmalloc(strlen(remote->url) + strlen(file) + 1);
1133 sprintf(url, "%s%s", remote->url, file);
1135 /* Make sure leading directories exist for the remote ref */
1136 ep = strchr(url + strlen(remote->url) + 11, '/');
1139 slot = get_active_slot();
1140 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
1141 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1142 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
1143 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1144 if (start_active_slot(slot)) {
1145 run_active_slot(slot);
1146 if (slot->curl_result != CURLE_OK &&
1147 slot->http_code != 405) {
1149 "Unable to create branch path %s\n",
1155 fprintf(stderr, "Unable to start request\n");
1160 ep = strchr(ep + 1, '/');
1163 out_buffer.size = strlen(LOCK_REQUEST) + strlen(git_default_email) - 2;
1164 out_data = xmalloc(out_buffer.size + 1);
1165 snprintf(out_data, out_buffer.size + 1, LOCK_REQUEST, git_default_email);
1166 out_buffer.posn = 0;
1167 out_buffer.buffer = out_data;
1169 in_buffer.size = 4096;
1170 in_data = xmalloc(in_buffer.size);
1172 in_buffer.buffer = in_data;
1174 new_lock = xmalloc(sizeof(*new_lock));
1175 new_lock->owner = NULL;
1176 new_lock->token = NULL;
1177 new_lock->timeout = -1;
1179 sprintf(timeout_header, "Timeout: Second-%d", timeout);
1180 dav_headers = curl_slist_append(dav_headers, timeout_header);
1181 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1183 slot = get_active_slot();
1184 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1185 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size);
1186 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1187 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1188 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
1189 fwrite_buffer_dynamic);
1190 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1191 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1192 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
1193 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1195 if (start_active_slot(slot)) {
1196 run_active_slot(slot);
1197 if (slot->curl_result != CURLE_OK) {
1198 fprintf(stderr, "Got HTTP error %ld\n", slot->http_code);
1210 fprintf(stderr, "Unable to start request\n");
1217 XML_SetUserData(parser, new_lock);
1218 XML_SetElementHandler(parser, start_activelock_element,
1219 end_activelock_element);
1220 XML_SetCharacterDataHandler(parser, activelock_cdata);
1221 result = XML_Parse(parser, in_buffer.buffer, in_buffer.posn, 1);
1223 if (result != XML_STATUS_OK) {
1224 fprintf(stderr, "%s", XML_ErrorString(
1225 XML_GetErrorCode(parser)));
1230 if (new_lock->token == NULL || new_lock->timeout <= 0) {
1231 if (new_lock->token != NULL)
1232 free(new_lock->token);
1233 if (new_lock->owner != NULL)
1234 free(new_lock->owner);
1239 new_lock->start_time = time(NULL);
1243 int unlock_remote(char *file, struct active_lock *lock)
1245 struct active_request_slot *slot;
1247 char *lock_token_header;
1248 struct curl_slist *dav_headers = NULL;
1251 lock_token_header = xmalloc(strlen(lock->token) + 31);
1252 sprintf(lock_token_header, "Lock-Token: <opaquelocktoken:%s>",
1254 url = xmalloc(strlen(remote->url) + strlen(file) + 1);
1255 sprintf(url, "%s%s", remote->url, file);
1256 dav_headers = curl_slist_append(dav_headers, lock_token_header);
1258 slot = get_active_slot();
1259 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1260 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1261 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_UNLOCK);
1262 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1264 if (start_active_slot(slot)) {
1265 run_active_slot(slot);
1266 if (slot->curl_result == CURLE_OK)
1269 fprintf(stderr, "Got HTTP error %ld\n",
1272 fprintf(stderr, "Unable to start request\n");
1275 curl_slist_free_all(dav_headers);
1276 free(lock_token_header);
1284 struct active_request_slot *slot;
1285 struct buffer in_buffer;
1286 struct buffer out_buffer;
1289 XML_Parser parser = XML_ParserCreate(NULL);
1290 enum XML_Status result;
1291 struct lockprop supported_lock;
1292 struct curl_slist *dav_headers = NULL;
1294 out_buffer.size = strlen(PROPFIND_REQUEST) + strlen(remote->url) - 2;
1295 out_data = xmalloc(out_buffer.size + 1);
1296 snprintf(out_data, out_buffer.size + 1, PROPFIND_REQUEST, remote->url);
1297 out_buffer.posn = 0;
1298 out_buffer.buffer = out_data;
1300 in_buffer.size = 4096;
1301 in_data = xmalloc(in_buffer.size);
1303 in_buffer.buffer = in_data;
1305 dav_headers = curl_slist_append(dav_headers, "Depth: 0");
1306 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1308 slot = get_active_slot();
1309 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1310 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size);
1311 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1312 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1313 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
1314 fwrite_buffer_dynamic);
1315 curl_easy_setopt(slot->curl, CURLOPT_URL, remote->url);
1316 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1317 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1318 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1320 if (start_active_slot(slot)) {
1321 run_active_slot(slot);
1323 if (slot->curl_result != CURLE_OK) {
1324 free(in_buffer.buffer);
1328 XML_SetUserData(parser, &supported_lock);
1329 XML_SetElementHandler(parser, start_lockprop_element,
1330 end_lockprop_element);
1331 result = XML_Parse(parser, in_buffer.buffer, in_buffer.posn, 1);
1332 free(in_buffer.buffer);
1333 if (result != XML_STATUS_OK)
1334 return error("%s", XML_ErrorString(
1335 XML_GetErrorCode(parser)));
1338 free(in_buffer.buffer);
1339 return error("Unable to start request");
1342 if (supported_lock.lock_exclusive_write)
1348 int is_ancestor(unsigned char *sha1, struct commit *commit)
1350 struct commit_list *parents;
1352 if (parse_commit(commit))
1354 parents = commit->parents;
1355 for (; parents; parents = parents->next) {
1356 if (!memcmp(sha1, parents->item->object.sha1, 20)) {
1358 } else if (parents->item->object.type == commit_type) {
1361 (struct commit *)&parents->item->object
1369 void get_delta(unsigned char *sha1, struct object *obj,
1370 struct active_lock *lock)
1372 struct commit *commit;
1373 struct commit_list *parents;
1375 struct tree_entry_list *entry;
1377 if (sha1 && !memcmp(sha1, obj->sha1, 20))
1383 if (obj->type == commit_type) {
1385 fprintf(stderr, "walk %s\n", sha1_to_hex(obj->sha1));
1386 add_request(obj->sha1, lock);
1387 commit = (struct commit *)obj;
1388 if (parse_commit(commit)) {
1389 fprintf(stderr, "Error parsing commit %s\n",
1390 sha1_to_hex(obj->sha1));
1394 parents = commit->parents;
1395 for (; parents; parents = parents->next)
1397 memcmp(sha1, parents->item->object.sha1, 20))
1398 get_delta(sha1, &parents->item->object,
1400 get_delta(sha1, &commit->tree->object, lock);
1401 } else if (obj->type == tree_type) {
1403 fprintf(stderr, "walk %s\n", sha1_to_hex(obj->sha1));
1404 add_request(obj->sha1, lock);
1405 tree = (struct tree *)obj;
1406 if (parse_tree(tree)) {
1407 fprintf(stderr, "Error parsing tree %s\n",
1408 sha1_to_hex(obj->sha1));
1412 entry = tree->entries;
1413 tree->entries = NULL;
1415 struct tree_entry_list *next = entry->next;
1416 get_delta(sha1, entry->item.any, lock);
1421 } else if (obj->type == blob_type || obj->type == tag_type) {
1422 add_request(obj->sha1, lock);
1426 int update_remote(char *remote_path, unsigned char *sha1,
1427 struct active_lock *lock)
1429 struct active_request_slot *slot;
1433 struct buffer out_buffer;
1434 struct curl_slist *dav_headers = NULL;
1437 url = xmalloc(strlen(remote->url) + strlen(remote_path) + 1);
1438 sprintf(url, "%s%s", remote->url, remote_path);
1440 if_header = xmalloc(strlen(lock->token) + 25);
1441 sprintf(if_header, "If: (<opaquelocktoken:%s>)", lock->token);
1442 dav_headers = curl_slist_append(dav_headers, if_header);
1444 out_buffer.size = 41;
1445 out_data = xmalloc(out_buffer.size + 1);
1446 i = snprintf(out_data, out_buffer.size + 1, "%s\n", sha1_to_hex(sha1));
1447 if (i != out_buffer.size) {
1448 fprintf(stderr, "Unable to initialize PUT request body\n");
1451 out_buffer.posn = 0;
1452 out_buffer.buffer = out_data;
1454 slot = get_active_slot();
1455 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1456 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size);
1457 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1458 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1459 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1460 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1461 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1462 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1463 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1465 if (start_active_slot(slot)) {
1466 run_active_slot(slot);
1470 if (slot->curl_result != CURLE_OK) {
1472 "PUT error: curl result=%d, HTTP code=%ld\n",
1473 slot->curl_result, slot->http_code);
1474 /* We should attempt recovery? */
1481 fprintf(stderr, "Unable to start PUT request\n");
1488 int main(int argc, char **argv)
1490 struct active_request_slot *slot;
1491 struct active_request_slot *next_slot;
1492 struct transfer_request *request;
1493 struct transfer_request *next_request;
1495 char **refspec = NULL;
1496 int do_remote_update;
1500 unsigned char local_sha1[20];
1501 struct object *local_object = NULL;
1502 char *remote_ref = NULL;
1503 unsigned char remote_sha1[20];
1504 struct active_lock *remote_lock;
1505 char *remote_path = NULL;
1506 char *low_speed_limit;
1507 char *low_speed_time;
1513 remote = xmalloc(sizeof(*remote));
1515 remote->packs = NULL;
1518 for (i = 1; i < argc; i++, argv++) {
1522 if (!strcmp(arg, "--complete")) {
1526 if (!strcmp(arg, "--force")) {
1530 if (!strcmp(arg, "--verbose")) {
1534 usage(http_push_usage);
1541 nr_refspec = argc - i;
1545 curl_global_init(CURL_GLOBAL_ALL);
1547 #ifdef USE_CURL_MULTI
1549 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
1550 if (http_max_requests != NULL)
1551 max_requests = atoi(http_max_requests);
1554 curlm = curl_multi_init();
1555 if (curlm == NULL) {
1556 fprintf(stderr, "Error creating curl multi handle.\n");
1561 if (getenv("GIT_SSL_NO_VERIFY"))
1562 curl_ssl_verify = 0;
1564 ssl_cert = getenv("GIT_SSL_CERT");
1565 #if LIBCURL_VERSION_NUM >= 0x070902
1566 ssl_key = getenv("GIT_SSL_KEY");
1568 #if LIBCURL_VERSION_NUM >= 0x070908
1569 ssl_capath = getenv("GIT_SSL_CAPATH");
1571 ssl_cainfo = getenv("GIT_SSL_CAINFO");
1573 low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
1574 if (low_speed_limit != NULL)
1575 curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
1576 low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
1577 if (low_speed_time != NULL)
1578 curl_low_speed_time = strtol(low_speed_time, NULL, 10);
1580 git_config(http_options);
1582 if (curl_ssl_verify == -1)
1583 curl_ssl_verify = 1;
1585 #ifdef USE_CURL_MULTI
1586 if (max_requests < 1)
1587 max_requests = DEFAULT_MAX_REQUESTS;
1590 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
1591 default_headers = curl_slist_append(default_headers, "Range:");
1592 default_headers = curl_slist_append(default_headers, "Destination:");
1593 default_headers = curl_slist_append(default_headers, "If:");
1594 default_headers = curl_slist_append(default_headers,
1595 "Pragma: no-cache");
1597 #ifndef NO_CURL_EASY_DUPHANDLE
1598 curl_default = get_curl_handle();
1601 /* Verify DAV compliance/lock support */
1602 if (check_locking() != 0) {
1603 fprintf(stderr, "Error: no DAV locking support on remote repo %s\n", remote->url);
1608 /* Process each refspec */
1609 for (i = 0; i < nr_refspec; i++) {
1612 do_remote_update = 0;
1614 local_ref = refspec[i];
1615 if (*local_ref == '+') {
1619 ep = strchr(local_ref, ':');
1621 remote_ref = ep + 1;
1625 remote_ref = local_ref;
1627 /* Lock remote branch ref */
1630 remote_path = xmalloc(strlen(remote_ref) + 12);
1631 sprintf(remote_path, "refs/heads/%s", remote_ref);
1632 remote_lock = lock_remote(remote_path, 3600);
1633 if (remote_lock == NULL) {
1634 fprintf(stderr, "Unable to lock remote branch %s\n",
1640 /* Resolve local and remote refs */
1641 if (fetch_ref(remote_ref, remote_sha1) != 0) {
1643 "Remote branch %s does not exist on %s\n",
1644 remote_ref, remote->url);
1647 if (get_sha1(local_ref, local_sha1) != 0) {
1648 fprintf(stderr, "Error resolving local branch %s\n",
1654 /* Find relationship between local and remote */
1655 local_object = parse_object(local_sha1);
1656 if (!local_object) {
1657 fprintf(stderr, "Unable to parse local object %s\n",
1658 sha1_to_hex(local_sha1));
1661 } else if (new_branch) {
1662 do_remote_update = 1;
1664 if (!memcmp(local_sha1, remote_sha1, 20)) {
1666 "* %s: same as branch '%s' of %s\n",
1667 local_ref, remote_ref, remote->url);
1668 } else if (is_ancestor(remote_sha1,
1669 (struct commit *)local_object)) {
1671 "Remote %s will fast-forward to local %s\n",
1672 remote_ref, local_ref);
1673 do_remote_update = 1;
1674 } else if (force_all || force_this) {
1676 "* %s on %s does not fast forward to local branch '%s', overwriting\n",
1677 remote_ref, remote->url, local_ref);
1678 do_remote_update = 1;
1681 "* %s on %s does not fast forward to local branch '%s'\n",
1682 remote_ref, remote->url, local_ref);
1688 /* Generate and check list of required objects */
1690 if (do_remote_update || push_all)
1692 get_delta(push_all ? NULL : remote_sha1,
1693 local_object, remote_lock);
1694 process_waiting_requests();
1696 /* Push missing objects to remote, this would be a
1697 convenient time to pack them first if appropriate. */
1699 process_request_queue();
1700 process_waiting_requests();
1702 /* Update the remote branch if all went well */
1703 if (do_remote_update) {
1704 if (!aborted && update_remote(remote_path,
1707 fprintf(stderr, "%s remote branch %s\n",
1708 new_branch ? "Created" : "Updated",
1712 "Unable to %s remote branch %s\n",
1713 new_branch ? "create" : "update",
1721 unlock_remote(remote_path, remote_lock);
1723 if (remote_lock->owner != NULL)
1724 free(remote_lock->owner);
1725 free(remote_lock->token);
1732 curl_slist_free_all(no_pragma_header);
1733 curl_slist_free_all(default_headers);
1735 slot = active_queue_head;
1736 while (slot != NULL) {
1737 next_slot = slot->next;
1738 if (slot->curl != NULL)
1739 curl_easy_cleanup(slot->curl);
1744 request = request_queue_head;
1745 while (request != NULL) {
1746 next_request = request->next;
1747 release_request(request);
1748 request = next_request;
1751 #ifndef NO_CURL_EASY_DUPHANDLE
1752 curl_easy_cleanup(curl_default);
1754 #ifdef USE_CURL_MULTI
1755 curl_multi_cleanup(curlm);
1757 curl_global_cleanup();