12 #include "list-objects.h"
16 static const char http_push_usage[] =
17 "git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
24 #define XML_STATUS_OK 1
25 #define XML_STATUS_ERROR 0
28 #define PREV_BUF_SIZE 4096
29 #define RANGE_HEADER_SIZE 30
32 #define DAV_LOCK "LOCK"
33 #define DAV_MKCOL "MKCOL"
34 #define DAV_MOVE "MOVE"
35 #define DAV_PROPFIND "PROPFIND"
37 #define DAV_UNLOCK "UNLOCK"
38 #define DAV_DELETE "DELETE"
41 #define DAV_PROP_LOCKWR (1u << 0)
42 #define DAV_PROP_LOCKEX (1u << 1)
43 #define DAV_LOCK_OK (1u << 2)
45 /* DAV XML properties */
46 #define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
47 #define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
48 #define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
49 #define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
50 #define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
51 #define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
52 #define DAV_PROPFIND_RESP ".multistatus.response"
53 #define DAV_PROPFIND_NAME ".multistatus.response.href"
54 #define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
56 /* DAV request body templates */
57 #define PROPFIND_SUPPORTEDLOCK_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>"
58 #define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
59 #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>"
62 #define LOCK_REFRESH 30
64 /* bits #0-15 in revision.h */
66 #define LOCAL (1u<<16)
67 #define REMOTE (1u<<17)
68 #define FETCHING (1u<<18)
69 #define PUSHING (1u<<19)
71 /* We allow "recursive" symbolic refs. Only within reason, though */
76 static signed char remote_dir_exists[256];
78 static struct curl_slist *no_pragma_header;
80 static int push_verbosely;
81 static int push_all = MATCH_REFS_NONE;
85 static struct object_list *objects;
93 int can_update_info_refs;
95 struct packed_git *packs;
96 struct remote_lock *locks;
99 static struct repo *remote;
101 enum transfer_state {
113 struct transfer_request
118 struct remote_lock *lock;
119 struct curl_slist *headers;
120 struct buffer buffer;
121 char filename[PATH_MAX];
122 char tmpfile[PATH_MAX];
125 enum transfer_state state;
126 CURLcode curl_result;
127 char errorstr[CURL_ERROR_SIZE];
129 unsigned char real_sha1[20];
135 struct active_request_slot *slot;
136 struct transfer_request *next;
139 static struct transfer_request *request_queue_head;
146 void (*userFunc)(struct xml_ctx *ctx, int tag_closed);
158 struct remote_lock *next;
161 /* Flags that control remote_ls processing */
162 #define PROCESS_FILES (1u << 0)
163 #define PROCESS_DIRS (1u << 1)
164 #define RECURSIVE (1u << 2)
166 /* Flags that remote_ls passes to callback functions */
167 #define IS_DIR (1u << 0)
172 void (*userFunc)(struct remote_ls_ctx *ls);
177 struct remote_ls_ctx *parent;
180 /* get_dav_token_headers options */
181 enum dav_header_flag {
182 DAV_HEADER_IF = (1u << 0),
183 DAV_HEADER_LOCK = (1u << 1),
184 DAV_HEADER_TIMEOUT = (1u << 2)
187 static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum dav_header_flag options)
189 struct strbuf buf = STRBUF_INIT;
190 struct curl_slist *dav_headers = NULL;
192 if (options & DAV_HEADER_IF) {
193 strbuf_addf(&buf, "If: (<%s>)", lock->token);
194 dav_headers = curl_slist_append(dav_headers, buf.buf);
197 if (options & DAV_HEADER_LOCK) {
198 strbuf_addf(&buf, "Lock-Token: <%s>", lock->token);
199 dav_headers = curl_slist_append(dav_headers, buf.buf);
202 if (options & DAV_HEADER_TIMEOUT) {
203 strbuf_addf(&buf, "Timeout: Second-%ld", lock->timeout);
204 dav_headers = curl_slist_append(dav_headers, buf.buf);
207 strbuf_release(&buf);
212 static void append_remote_object_url(struct strbuf *buf, const char *url,
214 int only_two_digit_prefix)
216 strbuf_addf(buf, "%sobjects/%.*s/", url, 2, hex);
217 if (!only_two_digit_prefix)
218 strbuf_addf(buf, "%s", hex+2);
221 static void finish_request(struct transfer_request *request);
222 static void release_request(struct transfer_request *request);
224 static void process_response(void *callback_data)
226 struct transfer_request *request =
227 (struct transfer_request *)callback_data;
229 finish_request(request);
232 #ifdef USE_CURL_MULTI
234 static char *get_remote_object_url(const char *url, const char *hex,
235 int only_two_digit_prefix)
237 struct strbuf buf = STRBUF_INIT;
238 append_remote_object_url(&buf, url, hex, only_two_digit_prefix);
239 return strbuf_detach(&buf, NULL);
242 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
245 unsigned char expn[4096];
246 size_t size = eltsize * nmemb;
248 struct transfer_request *request = (struct transfer_request *)data;
250 ssize_t retval = xwrite(request->local_fileno,
251 (char *) ptr + posn, size - posn);
255 } while (posn < size);
257 request->stream.avail_in = size;
258 request->stream.next_in = ptr;
260 request->stream.next_out = expn;
261 request->stream.avail_out = sizeof(expn);
262 request->zret = git_inflate(&request->stream, Z_SYNC_FLUSH);
263 git_SHA1_Update(&request->c, expn,
264 sizeof(expn) - request->stream.avail_out);
265 } while (request->stream.avail_in && request->zret == Z_OK);
270 static void start_fetch_loose(struct transfer_request *request)
272 char *hex = sha1_to_hex(request->obj->sha1);
274 char prevfile[PATH_MAX];
277 unsigned char prev_buf[PREV_BUF_SIZE];
278 ssize_t prev_read = 0;
280 char range[RANGE_HEADER_SIZE];
281 struct curl_slist *range_header = NULL;
282 struct active_request_slot *slot;
284 filename = sha1_file_name(request->obj->sha1);
285 snprintf(request->filename, sizeof(request->filename), "%s", filename);
286 snprintf(request->tmpfile, sizeof(request->tmpfile),
287 "%s.temp", filename);
289 snprintf(prevfile, sizeof(prevfile), "%s.prev", request->filename);
291 rename(request->tmpfile, prevfile);
292 unlink(request->tmpfile);
294 if (request->local_fileno != -1)
295 error("fd leakage in start: %d", request->local_fileno);
296 request->local_fileno = open(request->tmpfile,
297 O_WRONLY | O_CREAT | O_EXCL, 0666);
298 /* This could have failed due to the "lazy directory creation";
299 * try to mkdir the last path component.
301 if (request->local_fileno < 0 && errno == ENOENT) {
302 char *dir = strrchr(request->tmpfile, '/');
305 mkdir(request->tmpfile, 0777);
308 request->local_fileno = open(request->tmpfile,
309 O_WRONLY | O_CREAT | O_EXCL, 0666);
312 if (request->local_fileno < 0) {
313 request->state = ABORTED;
314 error("Couldn't create temporary file %s for %s: %s",
315 request->tmpfile, request->filename, strerror(errno));
319 memset(&request->stream, 0, sizeof(request->stream));
321 git_inflate_init(&request->stream);
323 git_SHA1_Init(&request->c);
325 url = get_remote_object_url(remote->url, hex, 0);
326 request->url = xstrdup(url);
328 /* If a previous temp file is present, process what was already
330 prevlocal = open(prevfile, O_RDONLY);
331 if (prevlocal != -1) {
333 prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE);
335 if (fwrite_sha1_file(prev_buf,
338 request) == prev_read) {
339 prev_posn += prev_read;
344 } while (prev_read > 0);
349 /* Reset inflate/SHA1 if there was an error reading the previous temp
350 file; also rewind to the beginning of the local file. */
351 if (prev_read == -1) {
352 memset(&request->stream, 0, sizeof(request->stream));
353 git_inflate_init(&request->stream);
354 git_SHA1_Init(&request->c);
357 lseek(request->local_fileno, 0, SEEK_SET);
358 ftruncate(request->local_fileno, 0);
362 slot = get_active_slot();
363 slot->callback_func = process_response;
364 slot->callback_data = request;
365 request->slot = slot;
367 curl_easy_setopt(slot->curl, CURLOPT_FILE, request);
368 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
369 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
370 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
371 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
373 /* If we have successfully processed data from a previous fetch
374 attempt, only fetch the data we don't already have. */
378 "Resuming fetch of object %s at byte %ld\n",
380 sprintf(range, "Range: bytes=%ld-", prev_posn);
381 range_header = curl_slist_append(range_header, range);
382 curl_easy_setopt(slot->curl,
383 CURLOPT_HTTPHEADER, range_header);
386 /* Try to get the request started, abort the request on error */
387 request->state = RUN_FETCH_LOOSE;
388 if (!start_active_slot(slot)) {
389 fprintf(stderr, "Unable to start GET request\n");
390 remote->can_update_info_refs = 0;
391 release_request(request);
395 static void start_mkcol(struct transfer_request *request)
397 char *hex = sha1_to_hex(request->obj->sha1);
398 struct active_request_slot *slot;
400 request->url = get_remote_object_url(remote->url, hex, 1);
402 slot = get_active_slot();
403 slot->callback_func = process_response;
404 slot->callback_data = request;
405 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
406 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
407 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
408 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
409 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
411 if (start_active_slot(slot)) {
412 request->slot = slot;
413 request->state = RUN_MKCOL;
415 request->state = ABORTED;
422 static void start_fetch_packed(struct transfer_request *request)
425 struct packed_git *target;
429 char range[RANGE_HEADER_SIZE];
430 struct curl_slist *range_header = NULL;
432 struct transfer_request *check_request = request_queue_head;
433 struct active_request_slot *slot;
435 target = find_sha1_pack(request->obj->sha1, remote->packs);
437 fprintf(stderr, "Unable to fetch %s, will not be able to update server info refs\n", sha1_to_hex(request->obj->sha1));
438 remote->can_update_info_refs = 0;
439 release_request(request);
443 fprintf(stderr, "Fetching pack %s\n", sha1_to_hex(target->sha1));
444 fprintf(stderr, " which contains %s\n", sha1_to_hex(request->obj->sha1));
446 filename = sha1_pack_name(target->sha1);
447 snprintf(request->filename, sizeof(request->filename), "%s", filename);
448 snprintf(request->tmpfile, sizeof(request->tmpfile),
449 "%s.temp", filename);
451 url = xmalloc(strlen(remote->url) + 64);
452 sprintf(url, "%sobjects/pack/pack-%s.pack",
453 remote->url, sha1_to_hex(target->sha1));
455 /* Make sure there isn't another open request for this pack */
456 while (check_request) {
457 if (check_request->state == RUN_FETCH_PACKED &&
458 !strcmp(check_request->url, url)) {
460 release_request(request);
463 check_request = check_request->next;
466 packfile = fopen(request->tmpfile, "a");
468 fprintf(stderr, "Unable to open local file %s for pack",
470 remote->can_update_info_refs = 0;
475 slot = get_active_slot();
476 slot->callback_func = process_response;
477 slot->callback_data = request;
478 request->slot = slot;
479 request->local_stream = packfile;
480 request->userData = target;
483 curl_easy_setopt(slot->curl, CURLOPT_FILE, packfile);
484 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
485 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
486 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
487 slot->local = packfile;
489 /* If there is data present from a previous transfer attempt,
490 resume where it left off */
491 prev_posn = ftell(packfile);
495 "Resuming fetch of pack %s at byte %ld\n",
496 sha1_to_hex(target->sha1), prev_posn);
497 sprintf(range, "Range: bytes=%ld-", prev_posn);
498 range_header = curl_slist_append(range_header, range);
499 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
502 /* Try to get the request started, abort the request on error */
503 request->state = RUN_FETCH_PACKED;
504 if (!start_active_slot(slot)) {
505 fprintf(stderr, "Unable to start GET request\n");
506 remote->can_update_info_refs = 0;
507 release_request(request);
511 static void start_put(struct transfer_request *request)
513 char *hex = sha1_to_hex(request->obj->sha1);
514 struct active_request_slot *slot;
515 struct strbuf buf = STRBUF_INIT;
516 enum object_type type;
524 unpacked = read_sha1_file(request->obj->sha1, &type, &len);
525 hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
528 memset(&stream, 0, sizeof(stream));
529 deflateInit(&stream, zlib_compression_level);
530 size = deflateBound(&stream, len + hdrlen);
531 strbuf_init(&request->buffer.buf, size);
532 request->buffer.posn = 0;
535 stream.next_out = (unsigned char *)request->buffer.buf.buf;
536 stream.avail_out = size;
539 stream.next_in = (void *)hdr;
540 stream.avail_in = hdrlen;
541 while (deflate(&stream, 0) == Z_OK)
544 /* Then the data itself.. */
545 stream.next_in = unpacked;
546 stream.avail_in = len;
547 while (deflate(&stream, Z_FINISH) == Z_OK)
552 request->buffer.buf.len = stream.total_out;
554 strbuf_addstr(&buf, "Destination: ");
555 append_remote_object_url(&buf, remote->url, hex, 0);
556 request->dest = strbuf_detach(&buf, NULL);
558 append_remote_object_url(&buf, remote->url, hex, 0);
559 strbuf_addstr(&buf, "_");
560 strbuf_addstr(&buf, request->lock->token);
561 request->url = strbuf_detach(&buf, NULL);
563 slot = get_active_slot();
564 slot->callback_func = process_response;
565 slot->callback_data = request;
566 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &request->buffer);
567 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, request->buffer.buf.len);
568 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
569 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
570 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
571 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
572 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
573 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
574 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
576 if (start_active_slot(slot)) {
577 request->slot = slot;
578 request->state = RUN_PUT;
580 request->state = ABORTED;
586 static void start_move(struct transfer_request *request)
588 struct active_request_slot *slot;
589 struct curl_slist *dav_headers = NULL;
591 slot = get_active_slot();
592 slot->callback_func = process_response;
593 slot->callback_data = request;
594 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
595 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MOVE);
596 dav_headers = curl_slist_append(dav_headers, request->dest);
597 dav_headers = curl_slist_append(dav_headers, "Overwrite: T");
598 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
599 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
600 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
602 if (start_active_slot(slot)) {
603 request->slot = slot;
604 request->state = RUN_MOVE;
606 request->state = ABORTED;
612 static int refresh_lock(struct remote_lock *lock)
614 struct active_request_slot *slot;
615 struct slot_results results;
616 struct curl_slist *dav_headers;
619 lock->refreshing = 1;
621 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF | DAV_HEADER_TIMEOUT);
623 slot = get_active_slot();
624 slot->results = &results;
625 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
626 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
627 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
628 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
629 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
631 if (start_active_slot(slot)) {
632 run_active_slot(slot);
633 if (results.curl_result != CURLE_OK) {
634 fprintf(stderr, "LOCK HTTP error %ld\n",
637 lock->start_time = time(NULL);
642 lock->refreshing = 0;
643 curl_slist_free_all(dav_headers);
648 static void check_locks(void)
650 struct remote_lock *lock = remote->locks;
651 time_t current_time = time(NULL);
655 time_remaining = lock->start_time + lock->timeout -
657 if (!lock->refreshing && time_remaining < LOCK_REFRESH) {
658 if (!refresh_lock(lock)) {
660 "Unable to refresh lock for %s\n",
670 static void release_request(struct transfer_request *request)
672 struct transfer_request *entry = request_queue_head;
674 if (request == request_queue_head) {
675 request_queue_head = request->next;
677 while (entry->next != NULL && entry->next != request)
679 if (entry->next == request)
680 entry->next = entry->next->next;
683 if (request->local_fileno != -1)
684 close(request->local_fileno);
685 if (request->local_stream)
686 fclose(request->local_stream);
691 static void finish_request(struct transfer_request *request)
694 struct packed_git *target;
695 struct packed_git **lst;
697 request->curl_result = request->slot->curl_result;
698 request->http_code = request->slot->http_code;
699 request->slot = NULL;
701 /* Keep locks active */
704 if (request->headers != NULL)
705 curl_slist_free_all(request->headers);
707 /* URL is reused for MOVE after PUT */
708 if (request->state != RUN_PUT) {
713 if (request->state == RUN_MKCOL) {
714 if (request->curl_result == CURLE_OK ||
715 request->http_code == 405) {
716 remote_dir_exists[request->obj->sha1[0]] = 1;
719 fprintf(stderr, "MKCOL %s failed, aborting (%d/%ld)\n",
720 sha1_to_hex(request->obj->sha1),
721 request->curl_result, request->http_code);
722 request->state = ABORTED;
725 } else if (request->state == RUN_PUT) {
726 if (request->curl_result == CURLE_OK) {
729 fprintf(stderr, "PUT %s failed, aborting (%d/%ld)\n",
730 sha1_to_hex(request->obj->sha1),
731 request->curl_result, request->http_code);
732 request->state = ABORTED;
735 } else if (request->state == RUN_MOVE) {
736 if (request->curl_result == CURLE_OK) {
738 fprintf(stderr, " sent %s\n",
739 sha1_to_hex(request->obj->sha1));
740 request->obj->flags |= REMOTE;
741 release_request(request);
743 fprintf(stderr, "MOVE %s failed, aborting (%d/%ld)\n",
744 sha1_to_hex(request->obj->sha1),
745 request->curl_result, request->http_code);
746 request->state = ABORTED;
749 } else if (request->state == RUN_FETCH_LOOSE) {
750 fchmod(request->local_fileno, 0444);
751 close(request->local_fileno); request->local_fileno = -1;
753 if (request->curl_result != CURLE_OK &&
754 request->http_code != 416) {
755 if (stat(request->tmpfile, &st) == 0) {
757 unlink(request->tmpfile);
760 if (request->http_code == 416)
761 fprintf(stderr, "Warning: requested range invalid; we may already have all the data.\n");
763 git_inflate_end(&request->stream);
764 git_SHA1_Final(request->real_sha1, &request->c);
765 if (request->zret != Z_STREAM_END) {
766 unlink(request->tmpfile);
767 } else if (hashcmp(request->obj->sha1, request->real_sha1)) {
768 unlink(request->tmpfile);
774 if (request->rename == 0) {
775 request->obj->flags |= (LOCAL | REMOTE);
780 /* Try fetching packed if necessary */
781 if (request->obj->flags & LOCAL)
782 release_request(request);
784 start_fetch_packed(request);
786 } else if (request->state == RUN_FETCH_PACKED) {
787 if (request->curl_result != CURLE_OK) {
788 fprintf(stderr, "Unable to get pack file %s\n%s",
789 request->url, curl_errorstr);
790 remote->can_update_info_refs = 0;
792 off_t pack_size = ftell(request->local_stream);
794 fclose(request->local_stream);
795 request->local_stream = NULL;
796 if (!move_temp_to_file(request->tmpfile,
797 request->filename)) {
798 target = (struct packed_git *)request->userData;
799 target->pack_size = pack_size;
800 lst = &remote->packs;
801 while (*lst != target)
802 lst = &((*lst)->next);
805 if (!verify_pack(target))
806 install_packed_git(target);
808 remote->can_update_info_refs = 0;
811 release_request(request);
815 #ifdef USE_CURL_MULTI
816 static int fill_active_slot(void *unused)
818 struct transfer_request *request = request_queue_head;
823 for (request = request_queue_head; request; request = request->next) {
824 if (request->state == NEED_FETCH) {
825 start_fetch_loose(request);
827 } else if (pushing && request->state == NEED_PUSH) {
828 if (remote_dir_exists[request->obj->sha1[0]] == 1) {
831 start_mkcol(request);
840 static void get_remote_object_list(unsigned char parent);
842 static void add_fetch_request(struct object *obj)
844 struct transfer_request *request;
849 * Don't fetch the object if it's known to exist locally
850 * or is already in the request queue
852 if (remote_dir_exists[obj->sha1[0]] == -1)
853 get_remote_object_list(obj->sha1[0]);
854 if (obj->flags & (LOCAL | FETCHING))
857 obj->flags |= FETCHING;
858 request = xmalloc(sizeof(*request));
861 request->lock = NULL;
862 request->headers = NULL;
863 request->local_fileno = -1;
864 request->local_stream = NULL;
865 request->state = NEED_FETCH;
866 request->next = request_queue_head;
867 request_queue_head = request;
869 #ifdef USE_CURL_MULTI
875 static int add_send_request(struct object *obj, struct remote_lock *lock)
877 struct transfer_request *request = request_queue_head;
878 struct packed_git *target;
880 /* Keep locks active */
884 * Don't push the object if it's known to exist on the remote
885 * or is already in the request queue
887 if (remote_dir_exists[obj->sha1[0]] == -1)
888 get_remote_object_list(obj->sha1[0]);
889 if (obj->flags & (REMOTE | PUSHING))
891 target = find_sha1_pack(obj->sha1, remote->packs);
893 obj->flags |= REMOTE;
897 obj->flags |= PUSHING;
898 request = xmalloc(sizeof(*request));
901 request->lock = lock;
902 request->headers = NULL;
903 request->local_fileno = -1;
904 request->local_stream = NULL;
905 request->state = NEED_PUSH;
906 request->next = request_queue_head;
907 request_queue_head = request;
909 #ifdef USE_CURL_MULTI
917 static int fetch_index(unsigned char *sha1)
919 char *hex = sha1_to_hex(sha1);
922 char tmpfile[PATH_MAX];
924 char range[RANGE_HEADER_SIZE];
925 struct curl_slist *range_header = NULL;
928 struct active_request_slot *slot;
929 struct slot_results results;
931 /* Don't use the index if the pack isn't there */
932 url = xmalloc(strlen(remote->url) + 64);
933 sprintf(url, "%sobjects/pack/pack-%s.pack", remote->url, hex);
934 slot = get_active_slot();
935 slot->results = &results;
936 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
937 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
938 if (start_active_slot(slot)) {
939 run_active_slot(slot);
940 if (results.curl_result != CURLE_OK) {
942 return error("Unable to verify pack %s is available",
947 return error("Unable to start request");
950 if (has_pack_index(sha1)) {
956 fprintf(stderr, "Getting index for pack %s\n", hex);
958 sprintf(url, "%sobjects/pack/pack-%s.idx", remote->url, hex);
960 filename = sha1_pack_index_name(sha1);
961 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
962 indexfile = fopen(tmpfile, "a");
965 return error("Unable to open local file %s for pack index",
969 slot = get_active_slot();
970 slot->results = &results;
971 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
972 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
973 curl_easy_setopt(slot->curl, CURLOPT_FILE, indexfile);
974 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
975 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
976 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
977 slot->local = indexfile;
979 /* If there is data present from a previous transfer attempt,
980 resume where it left off */
981 prev_posn = ftell(indexfile);
985 "Resuming fetch of index for pack %s at byte %ld\n",
987 sprintf(range, "Range: bytes=%ld-", prev_posn);
988 range_header = curl_slist_append(range_header, range);
989 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
992 if (start_active_slot(slot)) {
993 run_active_slot(slot);
994 if (results.curl_result != CURLE_OK) {
997 return error("Unable to get pack index %s\n%s", url,
1003 return error("Unable to start request");
1009 return move_temp_to_file(tmpfile, filename);
1012 static int setup_index(unsigned char *sha1)
1014 struct packed_git *new_pack;
1016 if (fetch_index(sha1))
1019 new_pack = parse_pack_index(sha1);
1020 new_pack->next = remote->packs;
1021 remote->packs = new_pack;
1025 static int fetch_indices(void)
1027 unsigned char sha1[20];
1029 struct strbuf buffer = STRBUF_INIT;
1033 struct active_request_slot *slot;
1034 struct slot_results results;
1037 fprintf(stderr, "Getting pack list\n");
1039 url = xmalloc(strlen(remote->url) + 20);
1040 sprintf(url, "%sobjects/info/packs", remote->url);
1042 slot = get_active_slot();
1043 slot->results = &results;
1044 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
1045 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1046 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1047 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
1048 if (start_active_slot(slot)) {
1049 run_active_slot(slot);
1050 if (results.curl_result != CURLE_OK) {
1051 strbuf_release(&buffer);
1053 if (results.http_code == 404)
1056 return error("%s", curl_errorstr);
1059 strbuf_release(&buffer);
1061 return error("Unable to start request");
1066 while (i < buffer.len) {
1070 if (i + 52 < buffer.len &&
1071 !prefixcmp(data + i, " pack-") &&
1072 !prefixcmp(data + i + 46, ".pack\n")) {
1073 get_sha1_hex(data + i + 6, sha1);
1079 while (data[i] != '\n')
1085 strbuf_release(&buffer);
1089 static void one_remote_object(const char *hex)
1091 unsigned char sha1[20];
1094 if (get_sha1_hex(hex, sha1) != 0)
1097 obj = lookup_object(sha1);
1099 obj = parse_object(sha1);
1101 /* Ignore remote objects that don't exist locally */
1105 obj->flags |= REMOTE;
1106 if (!object_list_contains(objects, obj))
1107 object_list_insert(obj, &objects);
1110 static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed)
1112 int *lock_flags = (int *)ctx->userData;
1115 if (!strcmp(ctx->name, DAV_CTX_LOCKENTRY)) {
1116 if ((*lock_flags & DAV_PROP_LOCKEX) &&
1117 (*lock_flags & DAV_PROP_LOCKWR)) {
1118 *lock_flags |= DAV_LOCK_OK;
1120 *lock_flags &= DAV_LOCK_OK;
1121 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_WRITE)) {
1122 *lock_flags |= DAV_PROP_LOCKWR;
1123 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_EXCLUSIVE)) {
1124 *lock_flags |= DAV_PROP_LOCKEX;
1129 static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
1131 struct remote_lock *lock = (struct remote_lock *)ctx->userData;
1133 if (tag_closed && ctx->cdata) {
1134 if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) {
1135 lock->owner = xmalloc(strlen(ctx->cdata) + 1);
1136 strcpy(lock->owner, ctx->cdata);
1137 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
1138 if (!prefixcmp(ctx->cdata, "Second-"))
1140 strtol(ctx->cdata + 7, NULL, 10);
1141 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
1142 lock->token = xmalloc(strlen(ctx->cdata) + 1);
1143 strcpy(lock->token, ctx->cdata);
1148 static void one_remote_ref(char *refname);
1151 xml_start_tag(void *userData, const char *name, const char **atts)
1153 struct xml_ctx *ctx = (struct xml_ctx *)userData;
1154 const char *c = strchr(name, ':');
1162 new_len = strlen(ctx->name) + strlen(c) + 2;
1164 if (new_len > ctx->len) {
1165 ctx->name = xrealloc(ctx->name, new_len);
1168 strcat(ctx->name, ".");
1169 strcat(ctx->name, c);
1174 ctx->userFunc(ctx, 0);
1178 xml_end_tag(void *userData, const char *name)
1180 struct xml_ctx *ctx = (struct xml_ctx *)userData;
1181 const char *c = strchr(name, ':');
1184 ctx->userFunc(ctx, 1);
1191 ep = ctx->name + strlen(ctx->name) - strlen(c) - 1;
1196 xml_cdata(void *userData, const XML_Char *s, int len)
1198 struct xml_ctx *ctx = (struct xml_ctx *)userData;
1200 ctx->cdata = xmemdupz(s, len);
1203 static struct remote_lock *lock_remote(const char *path, long timeout)
1205 struct active_request_slot *slot;
1206 struct slot_results results;
1207 struct buffer out_buffer = { STRBUF_INIT, 0 };
1208 struct strbuf in_buffer = STRBUF_INIT;
1211 char timeout_header[25];
1212 struct remote_lock *lock = NULL;
1213 struct curl_slist *dav_headers = NULL;
1216 url = xmalloc(strlen(remote->url) + strlen(path) + 1);
1217 sprintf(url, "%s%s", remote->url, path);
1219 /* Make sure leading directories exist for the remote ref */
1220 ep = strchr(url + strlen(remote->url) + 1, '/');
1222 char saved_character = ep[1];
1224 slot = get_active_slot();
1225 slot->results = &results;
1226 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
1227 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1228 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
1229 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1230 if (start_active_slot(slot)) {
1231 run_active_slot(slot);
1232 if (results.curl_result != CURLE_OK &&
1233 results.http_code != 405) {
1235 "Unable to create branch path %s\n",
1241 fprintf(stderr, "Unable to start MKCOL request\n");
1245 ep[1] = saved_character;
1246 ep = strchr(ep + 1, '/');
1249 strbuf_addf(&out_buffer.buf, LOCK_REQUEST, git_default_email);
1251 sprintf(timeout_header, "Timeout: Second-%ld", timeout);
1252 dav_headers = curl_slist_append(dav_headers, timeout_header);
1253 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1255 slot = get_active_slot();
1256 slot->results = &results;
1257 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1258 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1259 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1260 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1261 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1262 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1263 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1264 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
1265 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1267 lock = xcalloc(1, sizeof(*lock));
1270 if (start_active_slot(slot)) {
1271 run_active_slot(slot);
1272 if (results.curl_result == CURLE_OK) {
1273 XML_Parser parser = XML_ParserCreate(NULL);
1274 enum XML_Status result;
1275 ctx.name = xcalloc(10, 1);
1278 ctx.userFunc = handle_new_lock_ctx;
1279 ctx.userData = lock;
1280 XML_SetUserData(parser, &ctx);
1281 XML_SetElementHandler(parser, xml_start_tag,
1283 XML_SetCharacterDataHandler(parser, xml_cdata);
1284 result = XML_Parse(parser, in_buffer.buf,
1287 if (result != XML_STATUS_OK) {
1288 fprintf(stderr, "XML error: %s\n",
1290 XML_GetErrorCode(parser)));
1293 XML_ParserFree(parser);
1296 fprintf(stderr, "Unable to start LOCK request\n");
1299 curl_slist_free_all(dav_headers);
1300 strbuf_release(&out_buffer.buf);
1301 strbuf_release(&in_buffer);
1303 if (lock->token == NULL || lock->timeout <= 0) {
1311 lock->start_time = time(NULL);
1312 lock->next = remote->locks;
1313 remote->locks = lock;
1319 static int unlock_remote(struct remote_lock *lock)
1321 struct active_request_slot *slot;
1322 struct slot_results results;
1323 struct remote_lock *prev = remote->locks;
1324 struct curl_slist *dav_headers;
1327 dav_headers = get_dav_token_headers(lock, DAV_HEADER_LOCK);
1329 slot = get_active_slot();
1330 slot->results = &results;
1331 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1332 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1333 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_UNLOCK);
1334 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1336 if (start_active_slot(slot)) {
1337 run_active_slot(slot);
1338 if (results.curl_result == CURLE_OK)
1341 fprintf(stderr, "UNLOCK HTTP error %ld\n",
1344 fprintf(stderr, "Unable to start UNLOCK request\n");
1347 curl_slist_free_all(dav_headers);
1349 if (remote->locks == lock) {
1350 remote->locks = lock->next;
1352 while (prev && prev->next != lock)
1355 prev->next = prev->next->next;
1366 static void remove_locks(void)
1368 struct remote_lock *lock = remote->locks;
1370 fprintf(stderr, "Removing remote locks...\n");
1372 unlock_remote(lock);
1377 static void remove_locks_on_signal(int signo)
1380 signal(signo, SIG_DFL);
1384 static void remote_ls(const char *path, int flags,
1385 void (*userFunc)(struct remote_ls_ctx *ls),
1388 static void process_ls_object(struct remote_ls_ctx *ls)
1390 unsigned int *parent = (unsigned int *)ls->userData;
1391 char *path = ls->dentry_name;
1394 if (!strcmp(ls->path, ls->dentry_name) && (ls->flags & IS_DIR)) {
1395 remote_dir_exists[*parent] = 1;
1399 if (strlen(path) != 49)
1402 obj_hex = xmalloc(strlen(path));
1403 /* NB: path is not null-terminated, can not use strlcpy here */
1404 memcpy(obj_hex, path, 2);
1405 strcpy(obj_hex + 2, path + 3);
1406 one_remote_object(obj_hex);
1410 static void process_ls_ref(struct remote_ls_ctx *ls)
1412 if (!strcmp(ls->path, ls->dentry_name) && (ls->dentry_flags & IS_DIR)) {
1413 fprintf(stderr, " %s\n", ls->dentry_name);
1417 if (!(ls->dentry_flags & IS_DIR))
1418 one_remote_ref(ls->dentry_name);
1421 static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
1423 struct remote_ls_ctx *ls = (struct remote_ls_ctx *)ctx->userData;
1426 if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) {
1427 if (ls->dentry_flags & IS_DIR) {
1428 if (ls->flags & PROCESS_DIRS) {
1431 if (strcmp(ls->dentry_name, ls->path) &&
1432 ls->flags & RECURSIVE) {
1433 remote_ls(ls->dentry_name,
1438 } else if (ls->flags & PROCESS_FILES) {
1441 } else if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
1442 char *path = ctx->cdata;
1443 if (*ctx->cdata == 'h') {
1444 path = strstr(path, "//");
1446 path = strchr(path+2, '/');
1450 path += remote->path_len;
1451 ls->dentry_name = xstrdup(path);
1453 } else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
1454 ls->dentry_flags |= IS_DIR;
1456 } else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
1457 free(ls->dentry_name);
1458 ls->dentry_name = NULL;
1459 ls->dentry_flags = 0;
1464 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1465 * should _only_ heed the information from that file, instead of trying to
1466 * determine the refs from the remote file system (badly: it does not even
1467 * know about packed-refs).
1469 static void remote_ls(const char *path, int flags,
1470 void (*userFunc)(struct remote_ls_ctx *ls),
1473 char *url = xmalloc(strlen(remote->url) + strlen(path) + 1);
1474 struct active_request_slot *slot;
1475 struct slot_results results;
1476 struct strbuf in_buffer = STRBUF_INIT;
1477 struct buffer out_buffer = { STRBUF_INIT, 0 };
1478 struct curl_slist *dav_headers = NULL;
1480 struct remote_ls_ctx ls;
1483 ls.path = xstrdup(path);
1484 ls.dentry_name = NULL;
1485 ls.dentry_flags = 0;
1486 ls.userData = userData;
1487 ls.userFunc = userFunc;
1489 sprintf(url, "%s%s", remote->url, path);
1491 strbuf_addf(&out_buffer.buf, PROPFIND_ALL_REQUEST);
1493 dav_headers = curl_slist_append(dav_headers, "Depth: 1");
1494 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1496 slot = get_active_slot();
1497 slot->results = &results;
1498 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1499 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1500 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1501 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1502 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1503 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1504 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1505 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1506 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1508 if (start_active_slot(slot)) {
1509 run_active_slot(slot);
1510 if (results.curl_result == CURLE_OK) {
1511 XML_Parser parser = XML_ParserCreate(NULL);
1512 enum XML_Status result;
1513 ctx.name = xcalloc(10, 1);
1516 ctx.userFunc = handle_remote_ls_ctx;
1518 XML_SetUserData(parser, &ctx);
1519 XML_SetElementHandler(parser, xml_start_tag,
1521 XML_SetCharacterDataHandler(parser, xml_cdata);
1522 result = XML_Parse(parser, in_buffer.buf,
1526 if (result != XML_STATUS_OK) {
1527 fprintf(stderr, "XML error: %s\n",
1529 XML_GetErrorCode(parser)));
1531 XML_ParserFree(parser);
1534 fprintf(stderr, "Unable to start PROPFIND request\n");
1539 strbuf_release(&out_buffer.buf);
1540 strbuf_release(&in_buffer);
1541 curl_slist_free_all(dav_headers);
1544 static void get_remote_object_list(unsigned char parent)
1546 char path[] = "objects/XX/";
1547 static const char hex[] = "0123456789abcdef";
1548 unsigned int val = parent;
1550 path[8] = hex[val >> 4];
1551 path[9] = hex[val & 0xf];
1552 remote_dir_exists[val] = 0;
1553 remote_ls(path, (PROCESS_FILES | PROCESS_DIRS),
1554 process_ls_object, &val);
1557 static int locking_available(void)
1559 struct active_request_slot *slot;
1560 struct slot_results results;
1561 struct strbuf in_buffer = STRBUF_INIT;
1562 struct buffer out_buffer = { STRBUF_INIT, 0 };
1563 struct curl_slist *dav_headers = NULL;
1567 strbuf_addf(&out_buffer.buf, PROPFIND_SUPPORTEDLOCK_REQUEST, remote->url);
1569 dav_headers = curl_slist_append(dav_headers, "Depth: 0");
1570 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1572 slot = get_active_slot();
1573 slot->results = &results;
1574 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1575 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1576 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1577 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1578 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1579 curl_easy_setopt(slot->curl, CURLOPT_URL, remote->url);
1580 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1581 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1582 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1584 if (start_active_slot(slot)) {
1585 run_active_slot(slot);
1586 if (results.curl_result == CURLE_OK) {
1587 XML_Parser parser = XML_ParserCreate(NULL);
1588 enum XML_Status result;
1589 ctx.name = xcalloc(10, 1);
1592 ctx.userFunc = handle_lockprop_ctx;
1593 ctx.userData = &lock_flags;
1594 XML_SetUserData(parser, &ctx);
1595 XML_SetElementHandler(parser, xml_start_tag,
1597 result = XML_Parse(parser, in_buffer.buf,
1601 if (result != XML_STATUS_OK) {
1602 fprintf(stderr, "XML error: %s\n",
1604 XML_GetErrorCode(parser)));
1607 XML_ParserFree(parser);
1609 error("Error: no DAV locking support on %s",
1613 error("Cannot access URL %s, return code %d",
1614 remote->url, results.curl_result);
1618 error("Unable to start PROPFIND request on %s", remote->url);
1621 strbuf_release(&out_buffer.buf);
1622 strbuf_release(&in_buffer);
1623 curl_slist_free_all(dav_headers);
1628 static struct object_list **add_one_object(struct object *obj, struct object_list **p)
1630 struct object_list *entry = xmalloc(sizeof(struct object_list));
1634 return &entry->next;
1637 static struct object_list **process_blob(struct blob *blob,
1638 struct object_list **p,
1639 struct name_path *path,
1642 struct object *obj = &blob->object;
1644 obj->flags |= LOCAL;
1646 if (obj->flags & (UNINTERESTING | SEEN))
1650 return add_one_object(obj, p);
1653 static struct object_list **process_tree(struct tree *tree,
1654 struct object_list **p,
1655 struct name_path *path,
1658 struct object *obj = &tree->object;
1659 struct tree_desc desc;
1660 struct name_entry entry;
1661 struct name_path me;
1663 obj->flags |= LOCAL;
1665 if (obj->flags & (UNINTERESTING | SEEN))
1667 if (parse_tree(tree) < 0)
1668 die("bad tree object %s", sha1_to_hex(obj->sha1));
1671 name = xstrdup(name);
1672 p = add_one_object(obj, p);
1675 me.elem_len = strlen(name);
1677 init_tree_desc(&desc, tree->buffer, tree->size);
1679 while (tree_entry(&desc, &entry))
1680 switch (object_type(entry.mode)) {
1682 p = process_tree(lookup_tree(entry.sha1), p, &me, name);
1685 p = process_blob(lookup_blob(entry.sha1), p, &me, name);
1688 /* Subproject commit - not in this repository */
1693 tree->buffer = NULL;
1697 static int get_delta(struct rev_info *revs, struct remote_lock *lock)
1700 struct commit *commit;
1701 struct object_list **p = &objects;
1704 while ((commit = get_revision(revs)) != NULL) {
1705 p = process_tree(commit->tree, p, NULL, "");
1706 commit->object.flags |= LOCAL;
1707 if (!(commit->object.flags & UNINTERESTING))
1708 count += add_send_request(&commit->object, lock);
1711 for (i = 0; i < revs->pending.nr; i++) {
1712 struct object_array_entry *entry = revs->pending.objects + i;
1713 struct object *obj = entry->item;
1714 const char *name = entry->name;
1716 if (obj->flags & (UNINTERESTING | SEEN))
1718 if (obj->type == OBJ_TAG) {
1720 p = add_one_object(obj, p);
1723 if (obj->type == OBJ_TREE) {
1724 p = process_tree((struct tree *)obj, p, NULL, name);
1727 if (obj->type == OBJ_BLOB) {
1728 p = process_blob((struct blob *)obj, p, NULL, name);
1731 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
1735 if (!(objects->item->flags & UNINTERESTING))
1736 count += add_send_request(objects->item, lock);
1737 objects = objects->next;
1743 static int update_remote(unsigned char *sha1, struct remote_lock *lock)
1745 struct active_request_slot *slot;
1746 struct slot_results results;
1747 struct buffer out_buffer = { STRBUF_INIT, 0 };
1748 struct curl_slist *dav_headers;
1750 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1752 strbuf_addf(&out_buffer.buf, "%s\n", sha1_to_hex(sha1));
1754 slot = get_active_slot();
1755 slot->results = &results;
1756 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1757 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1758 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1759 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1760 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1761 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1762 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1763 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1764 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1766 if (start_active_slot(slot)) {
1767 run_active_slot(slot);
1768 strbuf_release(&out_buffer.buf);
1769 if (results.curl_result != CURLE_OK) {
1771 "PUT error: curl result=%d, HTTP code=%ld\n",
1772 results.curl_result, results.http_code);
1773 /* We should attempt recovery? */
1777 strbuf_release(&out_buffer.buf);
1778 fprintf(stderr, "Unable to start PUT request\n");
1785 static struct ref *local_refs, **local_tail;
1786 static struct ref *remote_refs, **remote_tail;
1788 static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
1791 int len = strlen(refname) + 1;
1792 ref = xcalloc(1, sizeof(*ref) + len);
1793 hashcpy(ref->new_sha1, sha1);
1794 memcpy(ref->name, refname, len);
1796 local_tail = &ref->next;
1800 static void one_remote_ref(char *refname)
1805 ref = alloc_ref(refname);
1807 if (http_fetch_ref(remote->url, ref) != 0) {
1809 "Unable to fetch ref %s from %s\n",
1810 refname, remote->url);
1816 * Fetch a copy of the object if it doesn't exist locally - it
1817 * may be required for updating server info later.
1819 if (remote->can_update_info_refs && !has_sha1_file(ref->old_sha1)) {
1820 obj = lookup_unknown_object(ref->old_sha1);
1822 fprintf(stderr, " fetch %s for %s\n",
1823 sha1_to_hex(ref->old_sha1), refname);
1824 add_fetch_request(obj);
1829 remote_tail = &ref->next;
1832 static void get_local_heads(void)
1834 local_tail = &local_refs;
1835 for_each_ref(one_local_ref, NULL);
1838 static void get_dav_remote_heads(void)
1840 remote_tail = &remote_refs;
1841 remote_ls("refs/", (PROCESS_FILES | PROCESS_DIRS | RECURSIVE), process_ls_ref, NULL);
1844 static int is_zero_sha1(const unsigned char *sha1)
1848 for (i = 0; i < 20; i++) {
1855 static void unmark_and_free(struct commit_list *list, unsigned int mark)
1858 struct commit_list *temp = list;
1859 temp->item->object.flags &= ~mark;
1865 static int ref_newer(const unsigned char *new_sha1,
1866 const unsigned char *old_sha1)
1869 struct commit *old, *new;
1870 struct commit_list *list, *used;
1873 /* Both new and old must be commit-ish and new is descendant of
1874 * old. Otherwise we require --force.
1876 o = deref_tag(parse_object(old_sha1), NULL, 0);
1877 if (!o || o->type != OBJ_COMMIT)
1879 old = (struct commit *) o;
1881 o = deref_tag(parse_object(new_sha1), NULL, 0);
1882 if (!o || o->type != OBJ_COMMIT)
1884 new = (struct commit *) o;
1886 if (parse_commit(new) < 0)
1890 commit_list_insert(new, &list);
1892 new = pop_most_recent_commit(&list, TMP_MARK);
1893 commit_list_insert(new, &used);
1899 unmark_and_free(list, TMP_MARK);
1900 unmark_and_free(used, TMP_MARK);
1904 static void add_remote_info_ref(struct remote_ls_ctx *ls)
1906 struct strbuf *buf = (struct strbuf *)ls->userData;
1912 ref = alloc_ref(ls->dentry_name);
1914 if (http_fetch_ref(remote->url, ref) != 0) {
1916 "Unable to fetch ref %s from %s\n",
1917 ls->dentry_name, remote->url);
1923 o = parse_object(ref->old_sha1);
1926 "Unable to parse object %s for remote ref %s\n",
1927 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1933 len = strlen(ls->dentry_name) + 42;
1934 ref_info = xcalloc(len + 1, 1);
1935 sprintf(ref_info, "%s %s\n",
1936 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1937 fwrite_buffer(ref_info, 1, len, buf);
1940 if (o->type == OBJ_TAG) {
1941 o = deref_tag(o, ls->dentry_name, 0);
1943 len = strlen(ls->dentry_name) + 45;
1944 ref_info = xcalloc(len + 1, 1);
1945 sprintf(ref_info, "%s %s^{}\n",
1946 sha1_to_hex(o->sha1), ls->dentry_name);
1947 fwrite_buffer(ref_info, 1, len, buf);
1954 static void update_remote_info_refs(struct remote_lock *lock)
1956 struct buffer buffer = { STRBUF_INIT, 0 };
1957 struct active_request_slot *slot;
1958 struct slot_results results;
1959 struct curl_slist *dav_headers;
1961 remote_ls("refs/", (PROCESS_FILES | RECURSIVE),
1962 add_remote_info_ref, &buffer.buf);
1964 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1966 slot = get_active_slot();
1967 slot->results = &results;
1968 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &buffer);
1969 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, buffer.buf.len);
1970 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1971 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1972 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1973 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1974 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1975 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1976 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1978 if (start_active_slot(slot)) {
1979 run_active_slot(slot);
1980 if (results.curl_result != CURLE_OK) {
1982 "PUT error: curl result=%d, HTTP code=%ld\n",
1983 results.curl_result, results.http_code);
1987 strbuf_release(&buffer.buf);
1990 static int remote_exists(const char *path)
1992 char *url = xmalloc(strlen(remote->url) + strlen(path) + 1);
1993 struct active_request_slot *slot;
1994 struct slot_results results;
1997 sprintf(url, "%s%s", remote->url, path);
1999 slot = get_active_slot();
2000 slot->results = &results;
2001 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2002 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
2004 if (start_active_slot(slot)) {
2005 run_active_slot(slot);
2006 if (results.http_code == 404)
2008 else if (results.curl_result == CURLE_OK)
2011 fprintf(stderr, "HEAD HTTP error %ld\n", results.http_code);
2013 fprintf(stderr, "Unable to start HEAD request\n");
2020 static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
2023 struct strbuf buffer = STRBUF_INIT;
2024 struct active_request_slot *slot;
2025 struct slot_results results;
2027 url = xmalloc(strlen(remote->url) + strlen(path) + 1);
2028 sprintf(url, "%s%s", remote->url, path);
2030 slot = get_active_slot();
2031 slot->results = &results;
2032 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
2033 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
2034 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
2035 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2036 if (start_active_slot(slot)) {
2037 run_active_slot(slot);
2038 if (results.curl_result != CURLE_OK) {
2039 die("Couldn't get %s for remote symref\n%s",
2040 url, curl_errorstr);
2043 die("Unable to start remote symref request");
2051 if (buffer.len == 0)
2054 /* If it's a symref, set the refname; otherwise try for a sha1 */
2055 if (!prefixcmp((char *)buffer.buf, "ref: ")) {
2056 *symref = xmemdupz((char *)buffer.buf + 5, buffer.len - 6);
2058 get_sha1_hex(buffer.buf, sha1);
2061 strbuf_release(&buffer);
2064 static int verify_merge_base(unsigned char *head_sha1, unsigned char *branch_sha1)
2066 struct commit *head = lookup_commit(head_sha1);
2067 struct commit *branch = lookup_commit(branch_sha1);
2068 struct commit_list *merge_bases = get_merge_bases(head, branch, 1);
2070 return (merge_bases && !merge_bases->next && merge_bases->item == branch);
2073 static int delete_remote_branch(char *pattern, int force)
2075 struct ref *refs = remote_refs;
2076 struct ref *remote_ref = NULL;
2077 unsigned char head_sha1[20];
2078 char *symref = NULL;
2080 int patlen = strlen(pattern);
2082 struct active_request_slot *slot;
2083 struct slot_results results;
2086 /* Find the remote branch(es) matching the specified branch name */
2087 for (match = 0; refs; refs = refs->next) {
2088 char *name = refs->name;
2089 int namelen = strlen(name);
2090 if (namelen < patlen ||
2091 memcmp(name + namelen - patlen, pattern, patlen))
2093 if (namelen != patlen && name[namelen - patlen - 1] != '/')
2099 return error("No remote branch matches %s", pattern);
2101 return error("More than one remote branch matches %s",
2105 * Remote HEAD must be a symref (not exactly foolproof; a remote
2106 * symlink to a symref will look like a symref)
2108 fetch_symref("HEAD", &symref, head_sha1);
2110 return error("Remote HEAD is not a symref");
2112 /* Remote branch must not be the remote HEAD */
2113 for (i=0; symref && i<MAXDEPTH; i++) {
2114 if (!strcmp(remote_ref->name, symref))
2115 return error("Remote branch %s is the current HEAD",
2117 fetch_symref(symref, &symref, head_sha1);
2120 /* Run extra sanity checks if delete is not forced */
2122 /* Remote HEAD must resolve to a known object */
2124 return error("Remote HEAD symrefs too deep");
2125 if (is_zero_sha1(head_sha1))
2126 return error("Unable to resolve remote HEAD");
2127 if (!has_sha1_file(head_sha1))
2128 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1));
2130 /* Remote branch must resolve to a known object */
2131 if (is_zero_sha1(remote_ref->old_sha1))
2132 return error("Unable to resolve remote branch %s",
2134 if (!has_sha1_file(remote_ref->old_sha1))
2135 return error("Remote branch %s resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", remote_ref->name, sha1_to_hex(remote_ref->old_sha1));
2137 /* Remote branch must be an ancestor of remote HEAD */
2138 if (!verify_merge_base(head_sha1, remote_ref->old_sha1)) {
2139 return error("The branch '%s' is not an ancestor "
2140 "of your current HEAD.\n"
2141 "If you are sure you want to delete it,"
2142 " run:\n\t'git http-push -D %s %s'",
2143 remote_ref->name, remote->url, pattern);
2147 /* Send delete request */
2148 fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name);
2151 url = xmalloc(strlen(remote->url) + strlen(remote_ref->name) + 1);
2152 sprintf(url, "%s%s", remote->url, remote_ref->name);
2153 slot = get_active_slot();
2154 slot->results = &results;
2155 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
2156 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
2157 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2158 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_DELETE);
2159 if (start_active_slot(slot)) {
2160 run_active_slot(slot);
2162 if (results.curl_result != CURLE_OK)
2163 return error("DELETE request failed (%d/%ld)\n",
2164 results.curl_result, results.http_code);
2167 return error("Unable to start DELETE request");
2173 int main(int argc, char **argv)
2175 struct transfer_request *request;
2176 struct transfer_request *next_request;
2178 char **refspec = NULL;
2179 struct remote_lock *ref_lock = NULL;
2180 struct remote_lock *info_ref_lock = NULL;
2181 struct rev_info revs;
2182 int delete_branch = 0;
2183 int force_delete = 0;
2184 int objects_to_send;
2189 char *rewritten_url = NULL;
2191 setup_git_directory();
2193 remote = xcalloc(sizeof(*remote), 1);
2196 for (i = 1; i < argc; i++, argv++) {
2200 if (!strcmp(arg, "--all")) {
2201 push_all = MATCH_REFS_ALL;
2204 if (!strcmp(arg, "--force")) {
2208 if (!strcmp(arg, "--dry-run")) {
2212 if (!strcmp(arg, "--verbose")) {
2216 if (!strcmp(arg, "-d")) {
2220 if (!strcmp(arg, "-D")) {
2227 char *path = strstr(arg, "//");
2229 remote->path_len = strlen(arg);
2231 remote->path = strchr(path+2, '/');
2233 remote->path_len = strlen(remote->path);
2238 nr_refspec = argc - i;
2242 #ifndef USE_CURL_MULTI
2243 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
2247 usage(http_push_usage);
2249 if (delete_branch && nr_refspec != 1)
2250 die("You must specify only one branch name when deleting a remote branch");
2252 memset(remote_dir_exists, -1, 256);
2256 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
2258 if (remote->url && remote->url[strlen(remote->url)-1] != '/') {
2259 rewritten_url = xmalloc(strlen(remote->url)+2);
2260 strcpy(rewritten_url, remote->url);
2261 strcat(rewritten_url, "/");
2262 remote->path = rewritten_url + (remote->path - remote->url);
2264 remote->url = rewritten_url;
2267 /* Verify DAV compliance/lock support */
2268 if (!locking_available()) {
2273 signal(SIGINT, remove_locks_on_signal);
2274 signal(SIGHUP, remove_locks_on_signal);
2275 signal(SIGQUIT, remove_locks_on_signal);
2276 signal(SIGTERM, remove_locks_on_signal);
2278 /* Check whether the remote has server info files */
2279 remote->can_update_info_refs = 0;
2280 remote->has_info_refs = remote_exists("info/refs");
2281 remote->has_info_packs = remote_exists("objects/info/packs");
2282 if (remote->has_info_refs) {
2283 info_ref_lock = lock_remote("info/refs", LOCK_TIME);
2285 remote->can_update_info_refs = 1;
2287 fprintf(stderr, "Error: cannot lock existing info/refs\n");
2292 if (remote->has_info_packs)
2295 /* Get a list of all local and remote heads to validate refspecs */
2297 fprintf(stderr, "Fetching remote heads...\n");
2298 get_dav_remote_heads();
2300 /* Remove a remote branch if -d or -D was specified */
2301 if (delete_branch) {
2302 if (delete_remote_branch(refspec[0], force_delete) == -1)
2303 fprintf(stderr, "Unable to delete remote branch %s\n",
2310 remote_tail = &remote_refs;
2311 if (match_refs(local_refs, remote_refs, &remote_tail,
2312 nr_refspec, (const char **) refspec, push_all)) {
2317 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
2323 for (ref = remote_refs; ref; ref = ref->next) {
2324 char old_hex[60], *new_hex;
2325 const char *commit_argv[4];
2327 char *new_sha1_hex, *old_sha1_hex;
2332 if (is_zero_sha1(ref->peer_ref->new_sha1)) {
2333 if (delete_remote_branch(ref->name, 1) == -1) {
2334 error("Could not remove %s", ref->name);
2341 if (!hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
2342 if (push_verbosely || 1)
2343 fprintf(stderr, "'%s': up-to-date\n", ref->name);
2348 !is_zero_sha1(ref->old_sha1) &&
2350 if (!has_sha1_file(ref->old_sha1) ||
2351 !ref_newer(ref->peer_ref->new_sha1,
2354 * We do not have the remote ref, or
2355 * we know that the remote ref is not
2356 * an ancestor of what we are trying to
2357 * push. Either way this can be losing
2358 * commits at the remote end and likely
2359 * we were not up to date to begin with.
2361 error("remote '%s' is not an ancestor of\n"
2363 "Maybe you are not up-to-date and "
2364 "need to pull first?",
2366 ref->peer_ref->name);
2371 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
2373 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
2374 new_hex = sha1_to_hex(ref->new_sha1);
2376 fprintf(stderr, "updating '%s'", ref->name);
2377 if (strcmp(ref->name, ref->peer_ref->name))
2378 fprintf(stderr, " using '%s'", ref->peer_ref->name);
2379 fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex);
2383 /* Lock remote branch ref */
2384 ref_lock = lock_remote(ref->name, LOCK_TIME);
2385 if (ref_lock == NULL) {
2386 fprintf(stderr, "Unable to lock remote branch %s\n",
2392 /* Set up revision info for this refspec */
2394 new_sha1_hex = xstrdup(sha1_to_hex(ref->new_sha1));
2395 old_sha1_hex = NULL;
2396 commit_argv[1] = "--objects";
2397 commit_argv[2] = new_sha1_hex;
2398 if (!push_all && !is_zero_sha1(ref->old_sha1)) {
2399 old_sha1_hex = xmalloc(42);
2400 sprintf(old_sha1_hex, "^%s",
2401 sha1_to_hex(ref->old_sha1));
2402 commit_argv[3] = old_sha1_hex;
2405 init_revisions(&revs, setup_git_directory());
2406 setup_revisions(commit_argc, commit_argv, &revs, NULL);
2407 revs.edge_hint = 0; /* just in case */
2411 commit_argv[1] = NULL;
2414 /* Generate a list of objects that need to be pushed */
2416 if (prepare_revision_walk(&revs))
2417 die("revision walk setup failed");
2418 mark_edges_uninteresting(revs.commits, &revs, NULL);
2419 objects_to_send = get_delta(&revs, ref_lock);
2420 finish_all_active_slots();
2422 /* Push missing objects to remote, this would be a
2423 convenient time to pack them first if appropriate. */
2425 if (objects_to_send)
2426 fprintf(stderr, " sending %d objects\n",
2428 #ifdef USE_CURL_MULTI
2429 fill_active_slots();
2430 add_fill_function(NULL, fill_active_slot);
2433 finish_all_active_slots();
2434 #ifdef USE_CURL_MULTI
2435 fill_active_slots();
2437 } while (request_queue_head && !aborted);
2439 /* Update the remote branch if all went well */
2440 if (aborted || !update_remote(ref->new_sha1, ref_lock))
2444 fprintf(stderr, " done\n");
2445 unlock_remote(ref_lock);
2449 /* Update remote server info if appropriate */
2450 if (remote->has_info_refs && new_refs) {
2451 if (info_ref_lock && remote->can_update_info_refs) {
2452 fprintf(stderr, "Updating remote server info\n");
2454 update_remote_info_refs(info_ref_lock);
2456 fprintf(stderr, "Unable to update server info\n");
2461 free(rewritten_url);
2463 unlock_remote(info_ref_lock);
2466 curl_slist_free_all(no_pragma_header);
2470 request = request_queue_head;
2471 while (request != NULL) {
2472 next_request = request->next;
2473 release_request(request);
2474 request = next_request;