12 #include "list-objects.h"
17 static const char http_push_usage[] =
18 "git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
25 #define XML_STATUS_OK 1
26 #define XML_STATUS_ERROR 0
29 #define PREV_BUF_SIZE 4096
30 #define RANGE_HEADER_SIZE 30
33 #define DAV_LOCK "LOCK"
34 #define DAV_MKCOL "MKCOL"
35 #define DAV_MOVE "MOVE"
36 #define DAV_PROPFIND "PROPFIND"
38 #define DAV_UNLOCK "UNLOCK"
39 #define DAV_DELETE "DELETE"
42 #define DAV_PROP_LOCKWR (1u << 0)
43 #define DAV_PROP_LOCKEX (1u << 1)
44 #define DAV_LOCK_OK (1u << 2)
46 /* DAV XML properties */
47 #define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
48 #define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
49 #define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
50 #define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
51 #define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
52 #define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
53 #define DAV_PROPFIND_RESP ".multistatus.response"
54 #define DAV_PROPFIND_NAME ".multistatus.response.href"
55 #define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
57 /* DAV request body templates */
58 #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>"
59 #define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
60 #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>"
63 #define LOCK_REFRESH 30
65 /* bits #0-15 in revision.h */
67 #define LOCAL (1u<<16)
68 #define REMOTE (1u<<17)
69 #define FETCHING (1u<<18)
70 #define PUSHING (1u<<19)
72 /* We allow "recursive" symbolic refs. Only within reason, though */
77 static signed char remote_dir_exists[256];
79 static struct curl_slist *no_pragma_header;
81 static int push_verbosely;
82 static int push_all = MATCH_REFS_NONE;
86 static struct object_list *objects;
94 int can_update_info_refs;
96 struct packed_git *packs;
97 struct remote_lock *locks;
100 static struct repo *repo;
102 enum transfer_state {
114 struct transfer_request
119 struct remote_lock *lock;
120 struct curl_slist *headers;
121 struct buffer buffer;
122 char filename[PATH_MAX];
123 char tmpfile[PATH_MAX];
126 enum transfer_state state;
127 CURLcode curl_result;
128 char errorstr[CURL_ERROR_SIZE];
130 unsigned char real_sha1[20];
136 struct active_request_slot *slot;
137 struct transfer_request *next;
140 static struct transfer_request *request_queue_head;
147 void (*userFunc)(struct xml_ctx *ctx, int tag_closed);
156 char tmpfile_suffix[41];
160 struct remote_lock *next;
163 /* Flags that control remote_ls processing */
164 #define PROCESS_FILES (1u << 0)
165 #define PROCESS_DIRS (1u << 1)
166 #define RECURSIVE (1u << 2)
168 /* Flags that remote_ls passes to callback functions */
169 #define IS_DIR (1u << 0)
174 void (*userFunc)(struct remote_ls_ctx *ls);
179 struct remote_ls_ctx *parent;
182 /* get_dav_token_headers options */
183 enum dav_header_flag {
184 DAV_HEADER_IF = (1u << 0),
185 DAV_HEADER_LOCK = (1u << 1),
186 DAV_HEADER_TIMEOUT = (1u << 2)
189 static char *xml_entities(char *s)
191 struct strbuf buf = STRBUF_INIT;
193 size_t len = strcspn(s, "\"<>&");
194 strbuf_add(&buf, s, len);
198 strbuf_addstr(&buf, """);
201 strbuf_addstr(&buf, "<");
204 strbuf_addstr(&buf, ">");
207 strbuf_addstr(&buf, "&");
212 return strbuf_detach(&buf, NULL);
215 static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum dav_header_flag options)
217 struct strbuf buf = STRBUF_INIT;
218 struct curl_slist *dav_headers = NULL;
220 if (options & DAV_HEADER_IF) {
221 strbuf_addf(&buf, "If: (<%s>)", lock->token);
222 dav_headers = curl_slist_append(dav_headers, buf.buf);
225 if (options & DAV_HEADER_LOCK) {
226 strbuf_addf(&buf, "Lock-Token: <%s>", lock->token);
227 dav_headers = curl_slist_append(dav_headers, buf.buf);
230 if (options & DAV_HEADER_TIMEOUT) {
231 strbuf_addf(&buf, "Timeout: Second-%ld", lock->timeout);
232 dav_headers = curl_slist_append(dav_headers, buf.buf);
235 strbuf_release(&buf);
240 static void append_remote_object_url(struct strbuf *buf, const char *url,
242 int only_two_digit_prefix)
244 strbuf_addf(buf, "%sobjects/%.*s/", url, 2, hex);
245 if (!only_two_digit_prefix)
246 strbuf_addf(buf, "%s", hex+2);
249 static void finish_request(struct transfer_request *request);
250 static void release_request(struct transfer_request *request);
252 static void process_response(void *callback_data)
254 struct transfer_request *request =
255 (struct transfer_request *)callback_data;
257 finish_request(request);
260 #ifdef USE_CURL_MULTI
262 static char *get_remote_object_url(const char *url, const char *hex,
263 int only_two_digit_prefix)
265 struct strbuf buf = STRBUF_INIT;
266 append_remote_object_url(&buf, url, hex, only_two_digit_prefix);
267 return strbuf_detach(&buf, NULL);
270 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
273 unsigned char expn[4096];
274 size_t size = eltsize * nmemb;
276 struct transfer_request *request = (struct transfer_request *)data;
278 ssize_t retval = xwrite(request->local_fileno,
279 (char *) ptr + posn, size - posn);
283 } while (posn < size);
285 request->stream.avail_in = size;
286 request->stream.next_in = ptr;
288 request->stream.next_out = expn;
289 request->stream.avail_out = sizeof(expn);
290 request->zret = git_inflate(&request->stream, Z_SYNC_FLUSH);
291 git_SHA1_Update(&request->c, expn,
292 sizeof(expn) - request->stream.avail_out);
293 } while (request->stream.avail_in && request->zret == Z_OK);
298 static void start_fetch_loose(struct transfer_request *request)
300 char *hex = sha1_to_hex(request->obj->sha1);
302 char prevfile[PATH_MAX];
305 unsigned char prev_buf[PREV_BUF_SIZE];
306 ssize_t prev_read = 0;
308 char range[RANGE_HEADER_SIZE];
309 struct curl_slist *range_header = NULL;
310 struct active_request_slot *slot;
312 filename = sha1_file_name(request->obj->sha1);
313 snprintf(request->filename, sizeof(request->filename), "%s", filename);
314 snprintf(request->tmpfile, sizeof(request->tmpfile),
315 "%s.temp", filename);
317 snprintf(prevfile, sizeof(prevfile), "%s.prev", request->filename);
318 unlink_or_warn(prevfile);
319 rename(request->tmpfile, prevfile);
320 unlink_or_warn(request->tmpfile);
322 if (request->local_fileno != -1)
323 error("fd leakage in start: %d", request->local_fileno);
324 request->local_fileno = open(request->tmpfile,
325 O_WRONLY | O_CREAT | O_EXCL, 0666);
327 * This could have failed due to the "lazy directory creation";
328 * try to mkdir the last path component.
330 if (request->local_fileno < 0 && errno == ENOENT) {
331 char *dir = strrchr(request->tmpfile, '/');
334 mkdir(request->tmpfile, 0777);
337 request->local_fileno = open(request->tmpfile,
338 O_WRONLY | O_CREAT | O_EXCL, 0666);
341 if (request->local_fileno < 0) {
342 request->state = ABORTED;
343 error("Couldn't create temporary file %s for %s: %s",
344 request->tmpfile, request->filename, strerror(errno));
348 memset(&request->stream, 0, sizeof(request->stream));
350 git_inflate_init(&request->stream);
352 git_SHA1_Init(&request->c);
354 url = get_remote_object_url(repo->url, hex, 0);
355 request->url = xstrdup(url);
358 * If a previous temp file is present, process what was already
361 prevlocal = open(prevfile, O_RDONLY);
362 if (prevlocal != -1) {
364 prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE);
366 if (fwrite_sha1_file(prev_buf,
369 request) == prev_read)
370 prev_posn += prev_read;
374 } while (prev_read > 0);
377 unlink_or_warn(prevfile);
380 * Reset inflate/SHA1 if there was an error reading the previous temp
381 * file; also rewind to the beginning of the local file.
383 if (prev_read == -1) {
384 memset(&request->stream, 0, sizeof(request->stream));
385 git_inflate_init(&request->stream);
386 git_SHA1_Init(&request->c);
389 lseek(request->local_fileno, 0, SEEK_SET);
390 ftruncate(request->local_fileno, 0);
394 slot = get_active_slot();
395 slot->callback_func = process_response;
396 slot->callback_data = request;
397 request->slot = slot;
399 curl_easy_setopt(slot->curl, CURLOPT_FILE, request);
400 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
401 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
402 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
403 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
406 * If we have successfully processed data from a previous fetch
407 * attempt, only fetch the data we don't already have.
412 "Resuming fetch of object %s at byte %ld\n",
414 sprintf(range, "Range: bytes=%ld-", prev_posn);
415 range_header = curl_slist_append(range_header, range);
416 curl_easy_setopt(slot->curl,
417 CURLOPT_HTTPHEADER, range_header);
420 /* Try to get the request started, abort the request on error */
421 request->state = RUN_FETCH_LOOSE;
422 if (!start_active_slot(slot)) {
423 fprintf(stderr, "Unable to start GET request\n");
424 repo->can_update_info_refs = 0;
425 release_request(request);
429 static void start_mkcol(struct transfer_request *request)
431 char *hex = sha1_to_hex(request->obj->sha1);
432 struct active_request_slot *slot;
434 request->url = get_remote_object_url(repo->url, hex, 1);
436 slot = get_active_slot();
437 slot->callback_func = process_response;
438 slot->callback_data = request;
439 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
440 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
441 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
442 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
443 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
445 if (start_active_slot(slot)) {
446 request->slot = slot;
447 request->state = RUN_MKCOL;
449 request->state = ABORTED;
456 static void start_fetch_packed(struct transfer_request *request)
459 struct packed_git *target;
463 char range[RANGE_HEADER_SIZE];
464 struct curl_slist *range_header = NULL;
466 struct transfer_request *check_request = request_queue_head;
467 struct active_request_slot *slot;
469 target = find_sha1_pack(request->obj->sha1, repo->packs);
471 fprintf(stderr, "Unable to fetch %s, will not be able to update server info refs\n", sha1_to_hex(request->obj->sha1));
472 repo->can_update_info_refs = 0;
473 release_request(request);
477 fprintf(stderr, "Fetching pack %s\n", sha1_to_hex(target->sha1));
478 fprintf(stderr, " which contains %s\n", sha1_to_hex(request->obj->sha1));
480 filename = sha1_pack_name(target->sha1);
481 snprintf(request->filename, sizeof(request->filename), "%s", filename);
482 snprintf(request->tmpfile, sizeof(request->tmpfile),
483 "%s.temp", filename);
485 url = xmalloc(strlen(repo->url) + 64);
486 sprintf(url, "%sobjects/pack/pack-%s.pack",
487 repo->url, sha1_to_hex(target->sha1));
489 /* Make sure there isn't another open request for this pack */
490 while (check_request) {
491 if (check_request->state == RUN_FETCH_PACKED &&
492 !strcmp(check_request->url, url)) {
494 release_request(request);
497 check_request = check_request->next;
500 packfile = fopen(request->tmpfile, "a");
502 fprintf(stderr, "Unable to open local file %s for pack",
504 repo->can_update_info_refs = 0;
509 slot = get_active_slot();
510 slot->callback_func = process_response;
511 slot->callback_data = request;
512 request->slot = slot;
513 request->local_stream = packfile;
514 request->userData = target;
517 curl_easy_setopt(slot->curl, CURLOPT_FILE, packfile);
518 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
519 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
520 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
521 slot->local = packfile;
524 * If there is data present from a previous transfer attempt,
525 * resume where it left off
527 prev_posn = ftell(packfile);
531 "Resuming fetch of pack %s at byte %ld\n",
532 sha1_to_hex(target->sha1), prev_posn);
533 sprintf(range, "Range: bytes=%ld-", prev_posn);
534 range_header = curl_slist_append(range_header, range);
535 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
538 /* Try to get the request started, abort the request on error */
539 request->state = RUN_FETCH_PACKED;
540 if (!start_active_slot(slot)) {
541 fprintf(stderr, "Unable to start GET request\n");
542 repo->can_update_info_refs = 0;
543 release_request(request);
547 static void start_put(struct transfer_request *request)
549 char *hex = sha1_to_hex(request->obj->sha1);
550 struct active_request_slot *slot;
551 struct strbuf buf = STRBUF_INIT;
552 enum object_type type;
560 unpacked = read_sha1_file(request->obj->sha1, &type, &len);
561 hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
564 memset(&stream, 0, sizeof(stream));
565 deflateInit(&stream, zlib_compression_level);
566 size = deflateBound(&stream, len + hdrlen);
567 strbuf_init(&request->buffer.buf, size);
568 request->buffer.posn = 0;
571 stream.next_out = (unsigned char *)request->buffer.buf.buf;
572 stream.avail_out = size;
575 stream.next_in = (void *)hdr;
576 stream.avail_in = hdrlen;
577 while (deflate(&stream, 0) == Z_OK)
580 /* Then the data itself.. */
581 stream.next_in = unpacked;
582 stream.avail_in = len;
583 while (deflate(&stream, Z_FINISH) == Z_OK)
588 request->buffer.buf.len = stream.total_out;
590 strbuf_addstr(&buf, "Destination: ");
591 append_remote_object_url(&buf, repo->url, hex, 0);
592 request->dest = strbuf_detach(&buf, NULL);
594 append_remote_object_url(&buf, repo->url, hex, 0);
595 strbuf_add(&buf, request->lock->tmpfile_suffix, 41);
596 request->url = strbuf_detach(&buf, NULL);
598 slot = get_active_slot();
599 slot->callback_func = process_response;
600 slot->callback_data = request;
601 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &request->buffer);
602 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, request->buffer.buf.len);
603 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
604 #ifndef NO_CURL_IOCTL
605 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
606 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &request->buffer);
608 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
609 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
610 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
611 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
612 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
613 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
615 if (start_active_slot(slot)) {
616 request->slot = slot;
617 request->state = RUN_PUT;
619 request->state = ABORTED;
625 static void start_move(struct transfer_request *request)
627 struct active_request_slot *slot;
628 struct curl_slist *dav_headers = NULL;
630 slot = get_active_slot();
631 slot->callback_func = process_response;
632 slot->callback_data = request;
633 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
634 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MOVE);
635 dav_headers = curl_slist_append(dav_headers, request->dest);
636 dav_headers = curl_slist_append(dav_headers, "Overwrite: T");
637 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
638 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
639 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
641 if (start_active_slot(slot)) {
642 request->slot = slot;
643 request->state = RUN_MOVE;
645 request->state = ABORTED;
651 static int refresh_lock(struct remote_lock *lock)
653 struct active_request_slot *slot;
654 struct slot_results results;
655 struct curl_slist *dav_headers;
658 lock->refreshing = 1;
660 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF | DAV_HEADER_TIMEOUT);
662 slot = get_active_slot();
663 slot->results = &results;
664 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
665 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
666 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
667 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
668 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
670 if (start_active_slot(slot)) {
671 run_active_slot(slot);
672 if (results.curl_result != CURLE_OK) {
673 fprintf(stderr, "LOCK HTTP error %ld\n",
676 lock->start_time = time(NULL);
681 lock->refreshing = 0;
682 curl_slist_free_all(dav_headers);
687 static void check_locks(void)
689 struct remote_lock *lock = repo->locks;
690 time_t current_time = time(NULL);
694 time_remaining = lock->start_time + lock->timeout -
696 if (!lock->refreshing && time_remaining < LOCK_REFRESH) {
697 if (!refresh_lock(lock)) {
699 "Unable to refresh lock for %s\n",
709 static void release_request(struct transfer_request *request)
711 struct transfer_request *entry = request_queue_head;
713 if (request == request_queue_head) {
714 request_queue_head = request->next;
716 while (entry->next != NULL && entry->next != request)
718 if (entry->next == request)
719 entry->next = entry->next->next;
722 if (request->local_fileno != -1)
723 close(request->local_fileno);
724 if (request->local_stream)
725 fclose(request->local_stream);
730 static void finish_request(struct transfer_request *request)
733 struct packed_git *target;
734 struct packed_git **lst;
735 struct active_request_slot *slot;
737 request->curl_result = request->slot->curl_result;
738 request->http_code = request->slot->http_code;
739 slot = request->slot;
740 request->slot = NULL;
742 /* Keep locks active */
745 if (request->headers != NULL)
746 curl_slist_free_all(request->headers);
748 /* URL is reused for MOVE after PUT */
749 if (request->state != RUN_PUT) {
754 if (request->state == RUN_MKCOL) {
755 if (request->curl_result == CURLE_OK ||
756 request->http_code == 405) {
757 remote_dir_exists[request->obj->sha1[0]] = 1;
760 fprintf(stderr, "MKCOL %s failed, aborting (%d/%ld)\n",
761 sha1_to_hex(request->obj->sha1),
762 request->curl_result, request->http_code);
763 request->state = ABORTED;
766 } else if (request->state == RUN_PUT) {
767 if (request->curl_result == CURLE_OK) {
770 fprintf(stderr, "PUT %s failed, aborting (%d/%ld)\n",
771 sha1_to_hex(request->obj->sha1),
772 request->curl_result, request->http_code);
773 request->state = ABORTED;
776 } else if (request->state == RUN_MOVE) {
777 if (request->curl_result == CURLE_OK) {
779 fprintf(stderr, " sent %s\n",
780 sha1_to_hex(request->obj->sha1));
781 request->obj->flags |= REMOTE;
782 release_request(request);
784 fprintf(stderr, "MOVE %s failed, aborting (%d/%ld)\n",
785 sha1_to_hex(request->obj->sha1),
786 request->curl_result, request->http_code);
787 request->state = ABORTED;
790 } else if (request->state == RUN_FETCH_LOOSE) {
791 close(request->local_fileno);
792 request->local_fileno = -1;
794 if (request->curl_result != CURLE_OK &&
795 request->http_code != 416) {
796 if (stat(request->tmpfile, &st) == 0) {
798 unlink_or_warn(request->tmpfile);
801 if (request->http_code == 416)
802 warning("requested range invalid; we may already have all the data.");
804 git_inflate_end(&request->stream);
805 git_SHA1_Final(request->real_sha1, &request->c);
806 if (request->zret != Z_STREAM_END) {
807 unlink_or_warn(request->tmpfile);
808 } else if (hashcmp(request->obj->sha1, request->real_sha1)) {
809 unlink_or_warn(request->tmpfile);
815 if (request->rename == 0)
816 request->obj->flags |= (LOCAL | REMOTE);
820 /* Try fetching packed if necessary */
821 if (request->obj->flags & LOCAL)
822 release_request(request);
824 start_fetch_packed(request);
826 } else if (request->state == RUN_FETCH_PACKED) {
827 if (request->curl_result != CURLE_OK) {
828 fprintf(stderr, "Unable to get pack file %s\n%s",
829 request->url, curl_errorstr);
830 repo->can_update_info_refs = 0;
832 off_t pack_size = ftell(request->local_stream);
834 fclose(request->local_stream);
835 request->local_stream = NULL;
837 if (!move_temp_to_file(request->tmpfile,
838 request->filename)) {
839 target = (struct packed_git *)request->userData;
840 target->pack_size = pack_size;
842 while (*lst != target)
843 lst = &((*lst)->next);
846 if (!verify_pack(target))
847 install_packed_git(target);
849 repo->can_update_info_refs = 0;
852 release_request(request);
856 #ifdef USE_CURL_MULTI
857 static int is_running_queue;
858 static int fill_active_slot(void *unused)
860 struct transfer_request *request;
862 if (aborted || !is_running_queue)
865 for (request = request_queue_head; request; request = request->next) {
866 if (request->state == NEED_FETCH) {
867 start_fetch_loose(request);
869 } else if (pushing && request->state == NEED_PUSH) {
870 if (remote_dir_exists[request->obj->sha1[0]] == 1) {
873 start_mkcol(request);
882 static void get_remote_object_list(unsigned char parent);
884 static void add_fetch_request(struct object *obj)
886 struct transfer_request *request;
891 * Don't fetch the object if it's known to exist locally
892 * or is already in the request queue
894 if (remote_dir_exists[obj->sha1[0]] == -1)
895 get_remote_object_list(obj->sha1[0]);
896 if (obj->flags & (LOCAL | FETCHING))
899 obj->flags |= FETCHING;
900 request = xmalloc(sizeof(*request));
903 request->lock = NULL;
904 request->headers = NULL;
905 request->local_fileno = -1;
906 request->local_stream = NULL;
907 request->state = NEED_FETCH;
908 request->next = request_queue_head;
909 request_queue_head = request;
911 #ifdef USE_CURL_MULTI
917 static int add_send_request(struct object *obj, struct remote_lock *lock)
919 struct transfer_request *request = request_queue_head;
920 struct packed_git *target;
922 /* Keep locks active */
926 * Don't push the object if it's known to exist on the remote
927 * or is already in the request queue
929 if (remote_dir_exists[obj->sha1[0]] == -1)
930 get_remote_object_list(obj->sha1[0]);
931 if (obj->flags & (REMOTE | PUSHING))
933 target = find_sha1_pack(obj->sha1, repo->packs);
935 obj->flags |= REMOTE;
939 obj->flags |= PUSHING;
940 request = xmalloc(sizeof(*request));
943 request->lock = lock;
944 request->headers = NULL;
945 request->local_fileno = -1;
946 request->local_stream = NULL;
947 request->state = NEED_PUSH;
948 request->next = request_queue_head;
949 request_queue_head = request;
951 #ifdef USE_CURL_MULTI
959 static int fetch_index(unsigned char *sha1)
962 char *hex = xstrdup(sha1_to_hex(sha1));
965 char tmpfile[PATH_MAX];
967 char range[RANGE_HEADER_SIZE];
968 struct curl_slist *range_header = NULL;
971 struct active_request_slot *slot;
972 struct slot_results results;
974 /* Don't use the index if the pack isn't there */
975 url = xmalloc(strlen(repo->url) + 64);
976 sprintf(url, "%sobjects/pack/pack-%s.pack", repo->url, hex);
977 slot = get_active_slot();
978 slot->results = &results;
979 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
980 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
981 if (start_active_slot(slot)) {
982 run_active_slot(slot);
983 if (results.curl_result != CURLE_OK) {
984 ret = error("Unable to verify pack %s is available",
989 ret = error("Unable to start request");
993 if (has_pack_index(sha1)) {
999 fprintf(stderr, "Getting index for pack %s\n", hex);
1001 sprintf(url, "%sobjects/pack/pack-%s.idx", repo->url, hex);
1003 filename = sha1_pack_index_name(sha1);
1004 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
1005 indexfile = fopen(tmpfile, "a");
1007 ret = error("Unable to open local file %s for pack index",
1012 slot = get_active_slot();
1013 slot->results = &results;
1014 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
1015 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
1016 curl_easy_setopt(slot->curl, CURLOPT_FILE, indexfile);
1017 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
1018 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1019 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
1020 slot->local = indexfile;
1023 * If there is data present from a previous transfer attempt,
1024 * resume where it left off
1026 prev_posn = ftell(indexfile);
1030 "Resuming fetch of index for pack %s at byte %ld\n",
1032 sprintf(range, "Range: bytes=%ld-", prev_posn);
1033 range_header = curl_slist_append(range_header, range);
1034 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
1037 if (start_active_slot(slot)) {
1038 run_active_slot(slot);
1039 if (results.curl_result != CURLE_OK) {
1040 ret = error("Unable to get pack index %s\n%s", url,
1045 ret = error("Unable to start request");
1049 ret = move_temp_to_file(tmpfile, filename);
1060 static int setup_index(unsigned char *sha1)
1062 struct packed_git *new_pack;
1064 if (fetch_index(sha1))
1067 new_pack = parse_pack_index(sha1);
1069 return -1; /* parse_pack_index() already issued error message */
1070 new_pack->next = repo->packs;
1071 repo->packs = new_pack;
1075 static int fetch_indices(void)
1077 unsigned char sha1[20];
1079 struct strbuf buffer = STRBUF_INIT;
1083 struct active_request_slot *slot;
1084 struct slot_results results;
1087 fprintf(stderr, "Getting pack list\n");
1089 url = xmalloc(strlen(repo->url) + 20);
1090 sprintf(url, "%sobjects/info/packs", repo->url);
1092 slot = get_active_slot();
1093 slot->results = &results;
1094 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
1095 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1096 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1097 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
1098 if (start_active_slot(slot)) {
1099 run_active_slot(slot);
1100 if (results.curl_result != CURLE_OK) {
1101 strbuf_release(&buffer);
1103 if (results.http_code == 404)
1106 return error("%s", curl_errorstr);
1109 strbuf_release(&buffer);
1111 return error("Unable to start request");
1116 while (i < buffer.len) {
1120 if (i + 52 < buffer.len &&
1121 !prefixcmp(data + i, " pack-") &&
1122 !prefixcmp(data + i + 46, ".pack\n")) {
1123 get_sha1_hex(data + i + 6, sha1);
1129 while (data[i] != '\n')
1135 strbuf_release(&buffer);
1139 static void one_remote_object(const char *hex)
1141 unsigned char sha1[20];
1144 if (get_sha1_hex(hex, sha1) != 0)
1147 obj = lookup_object(sha1);
1149 obj = parse_object(sha1);
1151 /* Ignore remote objects that don't exist locally */
1155 obj->flags |= REMOTE;
1156 if (!object_list_contains(objects, obj))
1157 object_list_insert(obj, &objects);
1160 static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed)
1162 int *lock_flags = (int *)ctx->userData;
1165 if (!strcmp(ctx->name, DAV_CTX_LOCKENTRY)) {
1166 if ((*lock_flags & DAV_PROP_LOCKEX) &&
1167 (*lock_flags & DAV_PROP_LOCKWR)) {
1168 *lock_flags |= DAV_LOCK_OK;
1170 *lock_flags &= DAV_LOCK_OK;
1171 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_WRITE)) {
1172 *lock_flags |= DAV_PROP_LOCKWR;
1173 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_EXCLUSIVE)) {
1174 *lock_flags |= DAV_PROP_LOCKEX;
1179 static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
1181 struct remote_lock *lock = (struct remote_lock *)ctx->userData;
1182 git_SHA_CTX sha_ctx;
1183 unsigned char lock_token_sha1[20];
1185 if (tag_closed && ctx->cdata) {
1186 if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) {
1187 lock->owner = xmalloc(strlen(ctx->cdata) + 1);
1188 strcpy(lock->owner, ctx->cdata);
1189 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
1190 if (!prefixcmp(ctx->cdata, "Second-"))
1192 strtol(ctx->cdata + 7, NULL, 10);
1193 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
1194 lock->token = xmalloc(strlen(ctx->cdata) + 1);
1195 strcpy(lock->token, ctx->cdata);
1197 git_SHA1_Init(&sha_ctx);
1198 git_SHA1_Update(&sha_ctx, lock->token, strlen(lock->token));
1199 git_SHA1_Final(lock_token_sha1, &sha_ctx);
1201 lock->tmpfile_suffix[0] = '_';
1202 memcpy(lock->tmpfile_suffix + 1, sha1_to_hex(lock_token_sha1), 40);
1207 static void one_remote_ref(char *refname);
1210 xml_start_tag(void *userData, const char *name, const char **atts)
1212 struct xml_ctx *ctx = (struct xml_ctx *)userData;
1213 const char *c = strchr(name, ':');
1221 new_len = strlen(ctx->name) + strlen(c) + 2;
1223 if (new_len > ctx->len) {
1224 ctx->name = xrealloc(ctx->name, new_len);
1227 strcat(ctx->name, ".");
1228 strcat(ctx->name, c);
1233 ctx->userFunc(ctx, 0);
1237 xml_end_tag(void *userData, const char *name)
1239 struct xml_ctx *ctx = (struct xml_ctx *)userData;
1240 const char *c = strchr(name, ':');
1243 ctx->userFunc(ctx, 1);
1250 ep = ctx->name + strlen(ctx->name) - strlen(c) - 1;
1255 xml_cdata(void *userData, const XML_Char *s, int len)
1257 struct xml_ctx *ctx = (struct xml_ctx *)userData;
1259 ctx->cdata = xmemdupz(s, len);
1262 static struct remote_lock *lock_remote(const char *path, long timeout)
1264 struct active_request_slot *slot;
1265 struct slot_results results;
1266 struct buffer out_buffer = { STRBUF_INIT, 0 };
1267 struct strbuf in_buffer = STRBUF_INIT;
1270 char timeout_header[25];
1271 struct remote_lock *lock = NULL;
1272 struct curl_slist *dav_headers = NULL;
1276 url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1277 sprintf(url, "%s%s", repo->url, path);
1279 /* Make sure leading directories exist for the remote ref */
1280 ep = strchr(url + strlen(repo->url) + 1, '/');
1282 char saved_character = ep[1];
1284 slot = get_active_slot();
1285 slot->results = &results;
1286 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
1287 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1288 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
1289 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1290 if (start_active_slot(slot)) {
1291 run_active_slot(slot);
1292 if (results.curl_result != CURLE_OK &&
1293 results.http_code != 405) {
1295 "Unable to create branch path %s\n",
1301 fprintf(stderr, "Unable to start MKCOL request\n");
1305 ep[1] = saved_character;
1306 ep = strchr(ep + 1, '/');
1309 escaped = xml_entities(git_default_email);
1310 strbuf_addf(&out_buffer.buf, LOCK_REQUEST, escaped);
1313 sprintf(timeout_header, "Timeout: Second-%ld", timeout);
1314 dav_headers = curl_slist_append(dav_headers, timeout_header);
1315 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1317 slot = get_active_slot();
1318 slot->results = &results;
1319 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1320 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1321 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1322 #ifndef NO_CURL_IOCTL
1323 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1324 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
1326 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1327 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1328 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1329 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1330 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
1331 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1333 lock = xcalloc(1, sizeof(*lock));
1336 if (start_active_slot(slot)) {
1337 run_active_slot(slot);
1338 if (results.curl_result == CURLE_OK) {
1339 XML_Parser parser = XML_ParserCreate(NULL);
1340 enum XML_Status result;
1341 ctx.name = xcalloc(10, 1);
1344 ctx.userFunc = handle_new_lock_ctx;
1345 ctx.userData = lock;
1346 XML_SetUserData(parser, &ctx);
1347 XML_SetElementHandler(parser, xml_start_tag,
1349 XML_SetCharacterDataHandler(parser, xml_cdata);
1350 result = XML_Parse(parser, in_buffer.buf,
1353 if (result != XML_STATUS_OK) {
1354 fprintf(stderr, "XML error: %s\n",
1356 XML_GetErrorCode(parser)));
1359 XML_ParserFree(parser);
1362 fprintf(stderr, "Unable to start LOCK request\n");
1365 curl_slist_free_all(dav_headers);
1366 strbuf_release(&out_buffer.buf);
1367 strbuf_release(&in_buffer);
1369 if (lock->token == NULL || lock->timeout <= 0) {
1377 lock->start_time = time(NULL);
1378 lock->next = repo->locks;
1385 static int unlock_remote(struct remote_lock *lock)
1387 struct active_request_slot *slot;
1388 struct slot_results results;
1389 struct remote_lock *prev = repo->locks;
1390 struct curl_slist *dav_headers;
1393 dav_headers = get_dav_token_headers(lock, DAV_HEADER_LOCK);
1395 slot = get_active_slot();
1396 slot->results = &results;
1397 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1398 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1399 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_UNLOCK);
1400 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1402 if (start_active_slot(slot)) {
1403 run_active_slot(slot);
1404 if (results.curl_result == CURLE_OK)
1407 fprintf(stderr, "UNLOCK HTTP error %ld\n",
1410 fprintf(stderr, "Unable to start UNLOCK request\n");
1413 curl_slist_free_all(dav_headers);
1415 if (repo->locks == lock) {
1416 repo->locks = lock->next;
1418 while (prev && prev->next != lock)
1421 prev->next = prev->next->next;
1432 static void remove_locks(void)
1434 struct remote_lock *lock = repo->locks;
1436 fprintf(stderr, "Removing remote locks...\n");
1438 unlock_remote(lock);
1443 static void remove_locks_on_signal(int signo)
1446 sigchain_pop(signo);
1450 static void remote_ls(const char *path, int flags,
1451 void (*userFunc)(struct remote_ls_ctx *ls),
1454 static void process_ls_object(struct remote_ls_ctx *ls)
1456 unsigned int *parent = (unsigned int *)ls->userData;
1457 char *path = ls->dentry_name;
1460 if (!strcmp(ls->path, ls->dentry_name) && (ls->flags & IS_DIR)) {
1461 remote_dir_exists[*parent] = 1;
1465 if (strlen(path) != 49)
1468 obj_hex = xmalloc(strlen(path));
1469 /* NB: path is not null-terminated, can not use strlcpy here */
1470 memcpy(obj_hex, path, 2);
1471 strcpy(obj_hex + 2, path + 3);
1472 one_remote_object(obj_hex);
1476 static void process_ls_ref(struct remote_ls_ctx *ls)
1478 if (!strcmp(ls->path, ls->dentry_name) && (ls->dentry_flags & IS_DIR)) {
1479 fprintf(stderr, " %s\n", ls->dentry_name);
1483 if (!(ls->dentry_flags & IS_DIR))
1484 one_remote_ref(ls->dentry_name);
1487 static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
1489 struct remote_ls_ctx *ls = (struct remote_ls_ctx *)ctx->userData;
1492 if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) {
1493 if (ls->dentry_flags & IS_DIR) {
1494 if (ls->flags & PROCESS_DIRS) {
1497 if (strcmp(ls->dentry_name, ls->path) &&
1498 ls->flags & RECURSIVE) {
1499 remote_ls(ls->dentry_name,
1504 } else if (ls->flags & PROCESS_FILES) {
1507 } else if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
1508 char *path = ctx->cdata;
1509 if (*ctx->cdata == 'h') {
1510 path = strstr(path, "//");
1512 path = strchr(path+2, '/');
1516 path += repo->path_len;
1517 ls->dentry_name = xstrdup(path);
1519 } else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
1520 ls->dentry_flags |= IS_DIR;
1522 } else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
1523 free(ls->dentry_name);
1524 ls->dentry_name = NULL;
1525 ls->dentry_flags = 0;
1530 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1531 * should _only_ heed the information from that file, instead of trying to
1532 * determine the refs from the remote file system (badly: it does not even
1533 * know about packed-refs).
1535 static void remote_ls(const char *path, int flags,
1536 void (*userFunc)(struct remote_ls_ctx *ls),
1539 char *url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1540 struct active_request_slot *slot;
1541 struct slot_results results;
1542 struct strbuf in_buffer = STRBUF_INIT;
1543 struct buffer out_buffer = { STRBUF_INIT, 0 };
1544 struct curl_slist *dav_headers = NULL;
1546 struct remote_ls_ctx ls;
1549 ls.path = xstrdup(path);
1550 ls.dentry_name = NULL;
1551 ls.dentry_flags = 0;
1552 ls.userData = userData;
1553 ls.userFunc = userFunc;
1555 sprintf(url, "%s%s", repo->url, path);
1557 strbuf_addf(&out_buffer.buf, PROPFIND_ALL_REQUEST);
1559 dav_headers = curl_slist_append(dav_headers, "Depth: 1");
1560 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1562 slot = get_active_slot();
1563 slot->results = &results;
1564 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1565 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1566 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1567 #ifndef NO_CURL_IOCTL
1568 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1569 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
1571 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1572 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1573 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1574 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1575 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1576 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1578 if (start_active_slot(slot)) {
1579 run_active_slot(slot);
1580 if (results.curl_result == CURLE_OK) {
1581 XML_Parser parser = XML_ParserCreate(NULL);
1582 enum XML_Status result;
1583 ctx.name = xcalloc(10, 1);
1586 ctx.userFunc = handle_remote_ls_ctx;
1588 XML_SetUserData(parser, &ctx);
1589 XML_SetElementHandler(parser, xml_start_tag,
1591 XML_SetCharacterDataHandler(parser, xml_cdata);
1592 result = XML_Parse(parser, in_buffer.buf,
1596 if (result != XML_STATUS_OK) {
1597 fprintf(stderr, "XML error: %s\n",
1599 XML_GetErrorCode(parser)));
1601 XML_ParserFree(parser);
1604 fprintf(stderr, "Unable to start PROPFIND request\n");
1609 strbuf_release(&out_buffer.buf);
1610 strbuf_release(&in_buffer);
1611 curl_slist_free_all(dav_headers);
1614 static void get_remote_object_list(unsigned char parent)
1616 char path[] = "objects/XX/";
1617 static const char hex[] = "0123456789abcdef";
1618 unsigned int val = parent;
1620 path[8] = hex[val >> 4];
1621 path[9] = hex[val & 0xf];
1622 remote_dir_exists[val] = 0;
1623 remote_ls(path, (PROCESS_FILES | PROCESS_DIRS),
1624 process_ls_object, &val);
1627 static int locking_available(void)
1629 struct active_request_slot *slot;
1630 struct slot_results results;
1631 struct strbuf in_buffer = STRBUF_INIT;
1632 struct buffer out_buffer = { STRBUF_INIT, 0 };
1633 struct curl_slist *dav_headers = NULL;
1638 escaped = xml_entities(repo->url);
1639 strbuf_addf(&out_buffer.buf, PROPFIND_SUPPORTEDLOCK_REQUEST, escaped);
1642 dav_headers = curl_slist_append(dav_headers, "Depth: 0");
1643 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1645 slot = get_active_slot();
1646 slot->results = &results;
1647 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1648 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1649 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1650 #ifndef NO_CURL_IOCTL
1651 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1652 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
1654 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1655 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1656 curl_easy_setopt(slot->curl, CURLOPT_URL, repo->url);
1657 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1658 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1659 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1661 if (start_active_slot(slot)) {
1662 run_active_slot(slot);
1663 if (results.curl_result == CURLE_OK) {
1664 XML_Parser parser = XML_ParserCreate(NULL);
1665 enum XML_Status result;
1666 ctx.name = xcalloc(10, 1);
1669 ctx.userFunc = handle_lockprop_ctx;
1670 ctx.userData = &lock_flags;
1671 XML_SetUserData(parser, &ctx);
1672 XML_SetElementHandler(parser, xml_start_tag,
1674 result = XML_Parse(parser, in_buffer.buf,
1678 if (result != XML_STATUS_OK) {
1679 fprintf(stderr, "XML error: %s\n",
1681 XML_GetErrorCode(parser)));
1684 XML_ParserFree(parser);
1686 error("no DAV locking support on %s",
1690 error("Cannot access URL %s, return code %d",
1691 repo->url, results.curl_result);
1695 error("Unable to start PROPFIND request on %s", repo->url);
1698 strbuf_release(&out_buffer.buf);
1699 strbuf_release(&in_buffer);
1700 curl_slist_free_all(dav_headers);
1705 static struct object_list **add_one_object(struct object *obj, struct object_list **p)
1707 struct object_list *entry = xmalloc(sizeof(struct object_list));
1711 return &entry->next;
1714 static struct object_list **process_blob(struct blob *blob,
1715 struct object_list **p,
1716 struct name_path *path,
1719 struct object *obj = &blob->object;
1721 obj->flags |= LOCAL;
1723 if (obj->flags & (UNINTERESTING | SEEN))
1727 return add_one_object(obj, p);
1730 static struct object_list **process_tree(struct tree *tree,
1731 struct object_list **p,
1732 struct name_path *path,
1735 struct object *obj = &tree->object;
1736 struct tree_desc desc;
1737 struct name_entry entry;
1738 struct name_path me;
1740 obj->flags |= LOCAL;
1742 if (obj->flags & (UNINTERESTING | SEEN))
1744 if (parse_tree(tree) < 0)
1745 die("bad tree object %s", sha1_to_hex(obj->sha1));
1748 name = xstrdup(name);
1749 p = add_one_object(obj, p);
1752 me.elem_len = strlen(name);
1754 init_tree_desc(&desc, tree->buffer, tree->size);
1756 while (tree_entry(&desc, &entry))
1757 switch (object_type(entry.mode)) {
1759 p = process_tree(lookup_tree(entry.sha1), p, &me, name);
1762 p = process_blob(lookup_blob(entry.sha1), p, &me, name);
1765 /* Subproject commit - not in this repository */
1770 tree->buffer = NULL;
1774 static int get_delta(struct rev_info *revs, struct remote_lock *lock)
1777 struct commit *commit;
1778 struct object_list **p = &objects;
1781 while ((commit = get_revision(revs)) != NULL) {
1782 p = process_tree(commit->tree, p, NULL, "");
1783 commit->object.flags |= LOCAL;
1784 if (!(commit->object.flags & UNINTERESTING))
1785 count += add_send_request(&commit->object, lock);
1788 for (i = 0; i < revs->pending.nr; i++) {
1789 struct object_array_entry *entry = revs->pending.objects + i;
1790 struct object *obj = entry->item;
1791 const char *name = entry->name;
1793 if (obj->flags & (UNINTERESTING | SEEN))
1795 if (obj->type == OBJ_TAG) {
1797 p = add_one_object(obj, p);
1800 if (obj->type == OBJ_TREE) {
1801 p = process_tree((struct tree *)obj, p, NULL, name);
1804 if (obj->type == OBJ_BLOB) {
1805 p = process_blob((struct blob *)obj, p, NULL, name);
1808 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
1812 if (!(objects->item->flags & UNINTERESTING))
1813 count += add_send_request(objects->item, lock);
1814 objects = objects->next;
1820 static int update_remote(unsigned char *sha1, struct remote_lock *lock)
1822 struct active_request_slot *slot;
1823 struct slot_results results;
1824 struct buffer out_buffer = { STRBUF_INIT, 0 };
1825 struct curl_slist *dav_headers;
1827 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1829 strbuf_addf(&out_buffer.buf, "%s\n", sha1_to_hex(sha1));
1831 slot = get_active_slot();
1832 slot->results = &results;
1833 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1834 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1835 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1836 #ifndef NO_CURL_IOCTL
1837 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1838 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
1840 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1841 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1842 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1843 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1844 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1845 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1847 if (start_active_slot(slot)) {
1848 run_active_slot(slot);
1849 strbuf_release(&out_buffer.buf);
1850 if (results.curl_result != CURLE_OK) {
1852 "PUT error: curl result=%d, HTTP code=%ld\n",
1853 results.curl_result, results.http_code);
1854 /* We should attempt recovery? */
1858 strbuf_release(&out_buffer.buf);
1859 fprintf(stderr, "Unable to start PUT request\n");
1866 static struct ref *remote_refs, **remote_tail;
1868 static void one_remote_ref(char *refname)
1873 ref = alloc_ref(refname);
1875 if (http_fetch_ref(repo->url, ref) != 0) {
1877 "Unable to fetch ref %s from %s\n",
1878 refname, repo->url);
1884 * Fetch a copy of the object if it doesn't exist locally - it
1885 * may be required for updating server info later.
1887 if (repo->can_update_info_refs && !has_sha1_file(ref->old_sha1)) {
1888 obj = lookup_unknown_object(ref->old_sha1);
1890 fprintf(stderr, " fetch %s for %s\n",
1891 sha1_to_hex(ref->old_sha1), refname);
1892 add_fetch_request(obj);
1897 remote_tail = &ref->next;
1900 static void get_dav_remote_heads(void)
1902 remote_tail = &remote_refs;
1903 remote_ls("refs/", (PROCESS_FILES | PROCESS_DIRS | RECURSIVE), process_ls_ref, NULL);
1906 static int is_zero_sha1(const unsigned char *sha1)
1910 for (i = 0; i < 20; i++) {
1917 static void add_remote_info_ref(struct remote_ls_ctx *ls)
1919 struct strbuf *buf = (struct strbuf *)ls->userData;
1925 ref = alloc_ref(ls->dentry_name);
1927 if (http_fetch_ref(repo->url, ref) != 0) {
1929 "Unable to fetch ref %s from %s\n",
1930 ls->dentry_name, repo->url);
1936 o = parse_object(ref->old_sha1);
1939 "Unable to parse object %s for remote ref %s\n",
1940 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1946 len = strlen(ls->dentry_name) + 42;
1947 ref_info = xcalloc(len + 1, 1);
1948 sprintf(ref_info, "%s %s\n",
1949 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1950 fwrite_buffer(ref_info, 1, len, buf);
1953 if (o->type == OBJ_TAG) {
1954 o = deref_tag(o, ls->dentry_name, 0);
1956 len = strlen(ls->dentry_name) + 45;
1957 ref_info = xcalloc(len + 1, 1);
1958 sprintf(ref_info, "%s %s^{}\n",
1959 sha1_to_hex(o->sha1), ls->dentry_name);
1960 fwrite_buffer(ref_info, 1, len, buf);
1967 static void update_remote_info_refs(struct remote_lock *lock)
1969 struct buffer buffer = { STRBUF_INIT, 0 };
1970 struct active_request_slot *slot;
1971 struct slot_results results;
1972 struct curl_slist *dav_headers;
1974 remote_ls("refs/", (PROCESS_FILES | RECURSIVE),
1975 add_remote_info_ref, &buffer.buf);
1977 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1979 slot = get_active_slot();
1980 slot->results = &results;
1981 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &buffer);
1982 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, buffer.buf.len);
1983 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1984 #ifndef NO_CURL_IOCTL
1985 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1986 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &buffer);
1988 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1989 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1990 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1991 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1992 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1993 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1995 if (start_active_slot(slot)) {
1996 run_active_slot(slot);
1997 if (results.curl_result != CURLE_OK) {
1999 "PUT error: curl result=%d, HTTP code=%ld\n",
2000 results.curl_result, results.http_code);
2004 strbuf_release(&buffer.buf);
2007 static int remote_exists(const char *path)
2009 char *url = xmalloc(strlen(repo->url) + strlen(path) + 1);
2010 struct active_request_slot *slot;
2011 struct slot_results results;
2014 sprintf(url, "%s%s", repo->url, path);
2016 slot = get_active_slot();
2017 slot->results = &results;
2018 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2019 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
2021 if (start_active_slot(slot)) {
2022 run_active_slot(slot);
2023 if (results.http_code == 404)
2025 else if (results.curl_result == CURLE_OK)
2028 fprintf(stderr, "HEAD HTTP error %ld\n", results.http_code);
2030 fprintf(stderr, "Unable to start HEAD request\n");
2037 static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
2040 struct strbuf buffer = STRBUF_INIT;
2041 struct active_request_slot *slot;
2042 struct slot_results results;
2044 url = xmalloc(strlen(repo->url) + strlen(path) + 1);
2045 sprintf(url, "%s%s", repo->url, path);
2047 slot = get_active_slot();
2048 slot->results = &results;
2049 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
2050 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
2051 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
2052 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2053 if (start_active_slot(slot)) {
2054 run_active_slot(slot);
2055 if (results.curl_result != CURLE_OK) {
2056 die("Couldn't get %s for remote symref\n%s",
2057 url, curl_errorstr);
2060 die("Unable to start remote symref request");
2068 if (buffer.len == 0)
2071 /* If it's a symref, set the refname; otherwise try for a sha1 */
2072 if (!prefixcmp((char *)buffer.buf, "ref: ")) {
2073 *symref = xmemdupz((char *)buffer.buf + 5, buffer.len - 6);
2075 get_sha1_hex(buffer.buf, sha1);
2078 strbuf_release(&buffer);
2081 static int verify_merge_base(unsigned char *head_sha1, unsigned char *branch_sha1)
2083 struct commit *head = lookup_commit(head_sha1);
2084 struct commit *branch = lookup_commit(branch_sha1);
2085 struct commit_list *merge_bases = get_merge_bases(head, branch, 1);
2087 return (merge_bases && !merge_bases->next && merge_bases->item == branch);
2090 static int delete_remote_branch(char *pattern, int force)
2092 struct ref *refs = remote_refs;
2093 struct ref *remote_ref = NULL;
2094 unsigned char head_sha1[20];
2095 char *symref = NULL;
2097 int patlen = strlen(pattern);
2099 struct active_request_slot *slot;
2100 struct slot_results results;
2103 /* Find the remote branch(es) matching the specified branch name */
2104 for (match = 0; refs; refs = refs->next) {
2105 char *name = refs->name;
2106 int namelen = strlen(name);
2107 if (namelen < patlen ||
2108 memcmp(name + namelen - patlen, pattern, patlen))
2110 if (namelen != patlen && name[namelen - patlen - 1] != '/')
2116 return error("No remote branch matches %s", pattern);
2118 return error("More than one remote branch matches %s",
2122 * Remote HEAD must be a symref (not exactly foolproof; a remote
2123 * symlink to a symref will look like a symref)
2125 fetch_symref("HEAD", &symref, head_sha1);
2127 return error("Remote HEAD is not a symref");
2129 /* Remote branch must not be the remote HEAD */
2130 for (i=0; symref && i<MAXDEPTH; i++) {
2131 if (!strcmp(remote_ref->name, symref))
2132 return error("Remote branch %s is the current HEAD",
2134 fetch_symref(symref, &symref, head_sha1);
2137 /* Run extra sanity checks if delete is not forced */
2139 /* Remote HEAD must resolve to a known object */
2141 return error("Remote HEAD symrefs too deep");
2142 if (is_zero_sha1(head_sha1))
2143 return error("Unable to resolve remote HEAD");
2144 if (!has_sha1_file(head_sha1))
2145 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1));
2147 /* Remote branch must resolve to a known object */
2148 if (is_zero_sha1(remote_ref->old_sha1))
2149 return error("Unable to resolve remote branch %s",
2151 if (!has_sha1_file(remote_ref->old_sha1))
2152 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));
2154 /* Remote branch must be an ancestor of remote HEAD */
2155 if (!verify_merge_base(head_sha1, remote_ref->old_sha1)) {
2156 return error("The branch '%s' is not an ancestor "
2157 "of your current HEAD.\n"
2158 "If you are sure you want to delete it,"
2159 " run:\n\t'git http-push -D %s %s'",
2160 remote_ref->name, repo->url, pattern);
2164 /* Send delete request */
2165 fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name);
2168 url = xmalloc(strlen(repo->url) + strlen(remote_ref->name) + 1);
2169 sprintf(url, "%s%s", repo->url, remote_ref->name);
2170 slot = get_active_slot();
2171 slot->results = &results;
2172 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
2173 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
2174 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2175 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_DELETE);
2176 if (start_active_slot(slot)) {
2177 run_active_slot(slot);
2179 if (results.curl_result != CURLE_OK)
2180 return error("DELETE request failed (%d/%ld)\n",
2181 results.curl_result, results.http_code);
2184 return error("Unable to start DELETE request");
2190 void run_request_queue(void)
2192 #ifdef USE_CURL_MULTI
2193 is_running_queue = 1;
2194 fill_active_slots();
2195 add_fill_function(NULL, fill_active_slot);
2198 finish_all_active_slots();
2199 #ifdef USE_CURL_MULTI
2200 fill_active_slots();
2202 } while (request_queue_head && !aborted);
2204 #ifdef USE_CURL_MULTI
2205 is_running_queue = 0;
2209 int main(int argc, char **argv)
2211 struct transfer_request *request;
2212 struct transfer_request *next_request;
2214 char **refspec = NULL;
2215 struct remote_lock *ref_lock = NULL;
2216 struct remote_lock *info_ref_lock = NULL;
2217 struct rev_info revs;
2218 int delete_branch = 0;
2219 int force_delete = 0;
2220 int objects_to_send;
2224 struct ref *ref, *local_refs;
2225 struct remote *remote;
2226 char *rewritten_url = NULL;
2228 git_extract_argv0_path(argv[0]);
2230 setup_git_directory();
2232 repo = xcalloc(sizeof(*repo), 1);
2235 for (i = 1; i < argc; i++, argv++) {
2239 if (!strcmp(arg, "--all")) {
2240 push_all = MATCH_REFS_ALL;
2243 if (!strcmp(arg, "--force")) {
2247 if (!strcmp(arg, "--dry-run")) {
2251 if (!strcmp(arg, "--verbose")) {
2255 if (!strcmp(arg, "-d")) {
2259 if (!strcmp(arg, "-D")) {
2266 char *path = strstr(arg, "//");
2268 repo->path_len = strlen(arg);
2270 repo->path = strchr(path+2, '/');
2272 repo->path_len = strlen(repo->path);
2277 nr_refspec = argc - i;
2281 #ifndef USE_CURL_MULTI
2282 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
2286 usage(http_push_usage);
2288 if (delete_branch && nr_refspec != 1)
2289 die("You must specify only one branch name when deleting a remote branch");
2291 memset(remote_dir_exists, -1, 256);
2294 * Create a minimum remote by hand to give to http_init(),
2295 * primarily to allow it to look at the URL.
2297 remote = xcalloc(sizeof(*remote), 1);
2298 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
2299 remote->url[remote->url_nr++] = repo->url;
2302 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
2304 if (repo->url && repo->url[strlen(repo->url)-1] != '/') {
2305 rewritten_url = xmalloc(strlen(repo->url)+2);
2306 strcpy(rewritten_url, repo->url);
2307 strcat(rewritten_url, "/");
2308 repo->path = rewritten_url + (repo->path - repo->url);
2310 repo->url = rewritten_url;
2313 #ifdef USE_CURL_MULTI
2314 is_running_queue = 0;
2317 /* Verify DAV compliance/lock support */
2318 if (!locking_available()) {
2323 sigchain_push_common(remove_locks_on_signal);
2325 /* Check whether the remote has server info files */
2326 repo->can_update_info_refs = 0;
2327 repo->has_info_refs = remote_exists("info/refs");
2328 repo->has_info_packs = remote_exists("objects/info/packs");
2329 if (repo->has_info_refs) {
2330 info_ref_lock = lock_remote("info/refs", LOCK_TIME);
2332 repo->can_update_info_refs = 1;
2334 error("cannot lock existing info/refs");
2339 if (repo->has_info_packs)
2342 /* Get a list of all local and remote heads to validate refspecs */
2343 local_refs = get_local_heads();
2344 fprintf(stderr, "Fetching remote heads...\n");
2345 get_dav_remote_heads();
2346 run_request_queue();
2348 /* Remove a remote branch if -d or -D was specified */
2349 if (delete_branch) {
2350 if (delete_remote_branch(refspec[0], force_delete) == -1)
2351 fprintf(stderr, "Unable to delete remote branch %s\n",
2358 remote_tail = &remote_refs;
2359 if (match_refs(local_refs, remote_refs, &remote_tail,
2360 nr_refspec, (const char **) refspec, push_all)) {
2365 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
2371 for (ref = remote_refs; ref; ref = ref->next) {
2372 char old_hex[60], *new_hex;
2373 const char *commit_argv[4];
2375 char *new_sha1_hex, *old_sha1_hex;
2380 if (is_zero_sha1(ref->peer_ref->new_sha1)) {
2381 if (delete_remote_branch(ref->name, 1) == -1) {
2382 error("Could not remove %s", ref->name);
2389 if (!hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
2390 if (push_verbosely || 1)
2391 fprintf(stderr, "'%s': up-to-date\n", ref->name);
2396 !is_zero_sha1(ref->old_sha1) &&
2398 if (!has_sha1_file(ref->old_sha1) ||
2399 !ref_newer(ref->peer_ref->new_sha1,
2402 * We do not have the remote ref, or
2403 * we know that the remote ref is not
2404 * an ancestor of what we are trying to
2405 * push. Either way this can be losing
2406 * commits at the remote end and likely
2407 * we were not up to date to begin with.
2409 error("remote '%s' is not an ancestor of\n"
2411 "Maybe you are not up-to-date and "
2412 "need to pull first?",
2414 ref->peer_ref->name);
2419 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
2421 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
2422 new_hex = sha1_to_hex(ref->new_sha1);
2424 fprintf(stderr, "updating '%s'", ref->name);
2425 if (strcmp(ref->name, ref->peer_ref->name))
2426 fprintf(stderr, " using '%s'", ref->peer_ref->name);
2427 fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex);
2431 /* Lock remote branch ref */
2432 ref_lock = lock_remote(ref->name, LOCK_TIME);
2433 if (ref_lock == NULL) {
2434 fprintf(stderr, "Unable to lock remote branch %s\n",
2440 /* Set up revision info for this refspec */
2442 new_sha1_hex = xstrdup(sha1_to_hex(ref->new_sha1));
2443 old_sha1_hex = NULL;
2444 commit_argv[1] = "--objects";
2445 commit_argv[2] = new_sha1_hex;
2446 if (!push_all && !is_zero_sha1(ref->old_sha1)) {
2447 old_sha1_hex = xmalloc(42);
2448 sprintf(old_sha1_hex, "^%s",
2449 sha1_to_hex(ref->old_sha1));
2450 commit_argv[3] = old_sha1_hex;
2453 init_revisions(&revs, setup_git_directory());
2454 setup_revisions(commit_argc, commit_argv, &revs, NULL);
2455 revs.edge_hint = 0; /* just in case */
2459 commit_argv[1] = NULL;
2462 /* Generate a list of objects that need to be pushed */
2464 if (prepare_revision_walk(&revs))
2465 die("revision walk setup failed");
2466 mark_edges_uninteresting(revs.commits, &revs, NULL);
2467 objects_to_send = get_delta(&revs, ref_lock);
2468 finish_all_active_slots();
2470 /* Push missing objects to remote, this would be a
2471 convenient time to pack them first if appropriate. */
2473 if (objects_to_send)
2474 fprintf(stderr, " sending %d objects\n",
2477 run_request_queue();
2479 /* Update the remote branch if all went well */
2480 if (aborted || !update_remote(ref->new_sha1, ref_lock))
2484 fprintf(stderr, " done\n");
2485 unlock_remote(ref_lock);
2489 /* Update remote server info if appropriate */
2490 if (repo->has_info_refs && new_refs) {
2491 if (info_ref_lock && repo->can_update_info_refs) {
2492 fprintf(stderr, "Updating remote server info\n");
2494 update_remote_info_refs(info_ref_lock);
2496 fprintf(stderr, "Unable to update server info\n");
2501 free(rewritten_url);
2503 unlock_remote(info_ref_lock);
2506 curl_slist_free_all(no_pragma_header);
2510 request = request_queue_head;
2511 while (request != NULL) {
2512 next_request = request->next;
2513 release_request(request);
2514 request = next_request;