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 *remote;
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);
159 struct remote_lock *next;
162 /* Flags that control remote_ls processing */
163 #define PROCESS_FILES (1u << 0)
164 #define PROCESS_DIRS (1u << 1)
165 #define RECURSIVE (1u << 2)
167 /* Flags that remote_ls passes to callback functions */
168 #define IS_DIR (1u << 0)
173 void (*userFunc)(struct remote_ls_ctx *ls);
178 struct remote_ls_ctx *parent;
181 /* get_dav_token_headers options */
182 enum dav_header_flag {
183 DAV_HEADER_IF = (1u << 0),
184 DAV_HEADER_LOCK = (1u << 1),
185 DAV_HEADER_TIMEOUT = (1u << 2)
188 static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum dav_header_flag options)
190 struct strbuf buf = STRBUF_INIT;
191 struct curl_slist *dav_headers = NULL;
193 if (options & DAV_HEADER_IF) {
194 strbuf_addf(&buf, "If: (<%s>)", lock->token);
195 dav_headers = curl_slist_append(dav_headers, buf.buf);
198 if (options & DAV_HEADER_LOCK) {
199 strbuf_addf(&buf, "Lock-Token: <%s>", lock->token);
200 dav_headers = curl_slist_append(dav_headers, buf.buf);
203 if (options & DAV_HEADER_TIMEOUT) {
204 strbuf_addf(&buf, "Timeout: Second-%ld", lock->timeout);
205 dav_headers = curl_slist_append(dav_headers, buf.buf);
208 strbuf_release(&buf);
213 static void append_remote_object_url(struct strbuf *buf, const char *url,
215 int only_two_digit_prefix)
217 strbuf_addf(buf, "%sobjects/%.*s/", url, 2, hex);
218 if (!only_two_digit_prefix)
219 strbuf_addf(buf, "%s", hex+2);
222 static void finish_request(struct transfer_request *request);
223 static void release_request(struct transfer_request *request);
225 static void process_response(void *callback_data)
227 struct transfer_request *request =
228 (struct transfer_request *)callback_data;
230 finish_request(request);
233 #ifdef USE_CURL_MULTI
235 static char *get_remote_object_url(const char *url, const char *hex,
236 int only_two_digit_prefix)
238 struct strbuf buf = STRBUF_INIT;
239 append_remote_object_url(&buf, url, hex, only_two_digit_prefix);
240 return strbuf_detach(&buf, NULL);
243 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
246 unsigned char expn[4096];
247 size_t size = eltsize * nmemb;
249 struct transfer_request *request = (struct transfer_request *)data;
251 ssize_t retval = xwrite(request->local_fileno,
252 (char *) ptr + posn, size - posn);
256 } while (posn < size);
258 request->stream.avail_in = size;
259 request->stream.next_in = ptr;
261 request->stream.next_out = expn;
262 request->stream.avail_out = sizeof(expn);
263 request->zret = git_inflate(&request->stream, Z_SYNC_FLUSH);
264 git_SHA1_Update(&request->c, expn,
265 sizeof(expn) - request->stream.avail_out);
266 } while (request->stream.avail_in && request->zret == Z_OK);
271 static void start_fetch_loose(struct transfer_request *request)
273 char *hex = sha1_to_hex(request->obj->sha1);
275 char prevfile[PATH_MAX];
278 unsigned char prev_buf[PREV_BUF_SIZE];
279 ssize_t prev_read = 0;
281 char range[RANGE_HEADER_SIZE];
282 struct curl_slist *range_header = NULL;
283 struct active_request_slot *slot;
285 filename = sha1_file_name(request->obj->sha1);
286 snprintf(request->filename, sizeof(request->filename), "%s", filename);
287 snprintf(request->tmpfile, sizeof(request->tmpfile),
288 "%s.temp", filename);
290 snprintf(prevfile, sizeof(prevfile), "%s.prev", request->filename);
292 rename(request->tmpfile, prevfile);
293 unlink(request->tmpfile);
295 if (request->local_fileno != -1)
296 error("fd leakage in start: %d", request->local_fileno);
297 request->local_fileno = open(request->tmpfile,
298 O_WRONLY | O_CREAT | O_EXCL, 0666);
299 /* This could have failed due to the "lazy directory creation";
300 * try to mkdir the last path component.
302 if (request->local_fileno < 0 && errno == ENOENT) {
303 char *dir = strrchr(request->tmpfile, '/');
306 mkdir(request->tmpfile, 0777);
309 request->local_fileno = open(request->tmpfile,
310 O_WRONLY | O_CREAT | O_EXCL, 0666);
313 if (request->local_fileno < 0) {
314 request->state = ABORTED;
315 error("Couldn't create temporary file %s for %s: %s",
316 request->tmpfile, request->filename, strerror(errno));
320 memset(&request->stream, 0, sizeof(request->stream));
322 git_inflate_init(&request->stream);
324 git_SHA1_Init(&request->c);
326 url = get_remote_object_url(remote->url, hex, 0);
327 request->url = xstrdup(url);
329 /* If a previous temp file is present, process what was already
331 prevlocal = open(prevfile, O_RDONLY);
332 if (prevlocal != -1) {
334 prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE);
336 if (fwrite_sha1_file(prev_buf,
339 request) == prev_read) {
340 prev_posn += prev_read;
345 } while (prev_read > 0);
350 /* Reset inflate/SHA1 if there was an error reading the previous temp
351 file; also rewind to the beginning of the local file. */
352 if (prev_read == -1) {
353 memset(&request->stream, 0, sizeof(request->stream));
354 git_inflate_init(&request->stream);
355 git_SHA1_Init(&request->c);
358 lseek(request->local_fileno, 0, SEEK_SET);
359 ftruncate(request->local_fileno, 0);
363 slot = get_active_slot();
364 slot->callback_func = process_response;
365 slot->callback_data = request;
366 request->slot = slot;
368 curl_easy_setopt(slot->curl, CURLOPT_FILE, request);
369 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
370 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
371 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
372 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
374 /* If we have successfully processed data from a previous fetch
375 attempt, only fetch the data we don't already have. */
379 "Resuming fetch of object %s at byte %ld\n",
381 sprintf(range, "Range: bytes=%ld-", prev_posn);
382 range_header = curl_slist_append(range_header, range);
383 curl_easy_setopt(slot->curl,
384 CURLOPT_HTTPHEADER, range_header);
387 /* Try to get the request started, abort the request on error */
388 request->state = RUN_FETCH_LOOSE;
389 if (!start_active_slot(slot)) {
390 fprintf(stderr, "Unable to start GET request\n");
391 remote->can_update_info_refs = 0;
392 release_request(request);
396 static void start_mkcol(struct transfer_request *request)
398 char *hex = sha1_to_hex(request->obj->sha1);
399 struct active_request_slot *slot;
401 request->url = get_remote_object_url(remote->url, hex, 1);
403 slot = get_active_slot();
404 slot->callback_func = process_response;
405 slot->callback_data = request;
406 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
407 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
408 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
409 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
410 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
412 if (start_active_slot(slot)) {
413 request->slot = slot;
414 request->state = RUN_MKCOL;
416 request->state = ABORTED;
423 static void start_fetch_packed(struct transfer_request *request)
426 struct packed_git *target;
430 char range[RANGE_HEADER_SIZE];
431 struct curl_slist *range_header = NULL;
433 struct transfer_request *check_request = request_queue_head;
434 struct active_request_slot *slot;
436 target = find_sha1_pack(request->obj->sha1, remote->packs);
438 fprintf(stderr, "Unable to fetch %s, will not be able to update server info refs\n", sha1_to_hex(request->obj->sha1));
439 remote->can_update_info_refs = 0;
440 release_request(request);
444 fprintf(stderr, "Fetching pack %s\n", sha1_to_hex(target->sha1));
445 fprintf(stderr, " which contains %s\n", sha1_to_hex(request->obj->sha1));
447 filename = sha1_pack_name(target->sha1);
448 snprintf(request->filename, sizeof(request->filename), "%s", filename);
449 snprintf(request->tmpfile, sizeof(request->tmpfile),
450 "%s.temp", filename);
452 url = xmalloc(strlen(remote->url) + 64);
453 sprintf(url, "%sobjects/pack/pack-%s.pack",
454 remote->url, sha1_to_hex(target->sha1));
456 /* Make sure there isn't another open request for this pack */
457 while (check_request) {
458 if (check_request->state == RUN_FETCH_PACKED &&
459 !strcmp(check_request->url, url)) {
461 release_request(request);
464 check_request = check_request->next;
467 packfile = fopen(request->tmpfile, "a");
469 fprintf(stderr, "Unable to open local file %s for pack",
471 remote->can_update_info_refs = 0;
476 slot = get_active_slot();
477 slot->callback_func = process_response;
478 slot->callback_data = request;
479 request->slot = slot;
480 request->local_stream = packfile;
481 request->userData = target;
484 curl_easy_setopt(slot->curl, CURLOPT_FILE, packfile);
485 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
486 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
487 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
488 slot->local = packfile;
490 /* If there is data present from a previous transfer attempt,
491 resume where it left off */
492 prev_posn = ftell(packfile);
496 "Resuming fetch of pack %s at byte %ld\n",
497 sha1_to_hex(target->sha1), prev_posn);
498 sprintf(range, "Range: bytes=%ld-", prev_posn);
499 range_header = curl_slist_append(range_header, range);
500 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
503 /* Try to get the request started, abort the request on error */
504 request->state = RUN_FETCH_PACKED;
505 if (!start_active_slot(slot)) {
506 fprintf(stderr, "Unable to start GET request\n");
507 remote->can_update_info_refs = 0;
508 release_request(request);
512 static void start_put(struct transfer_request *request)
514 char *hex = sha1_to_hex(request->obj->sha1);
515 struct active_request_slot *slot;
516 struct strbuf buf = STRBUF_INIT;
517 enum object_type type;
525 unpacked = read_sha1_file(request->obj->sha1, &type, &len);
526 hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
529 memset(&stream, 0, sizeof(stream));
530 deflateInit(&stream, zlib_compression_level);
531 size = deflateBound(&stream, len + hdrlen);
532 strbuf_init(&request->buffer.buf, size);
533 request->buffer.posn = 0;
536 stream.next_out = (unsigned char *)request->buffer.buf.buf;
537 stream.avail_out = size;
540 stream.next_in = (void *)hdr;
541 stream.avail_in = hdrlen;
542 while (deflate(&stream, 0) == Z_OK)
545 /* Then the data itself.. */
546 stream.next_in = unpacked;
547 stream.avail_in = len;
548 while (deflate(&stream, Z_FINISH) == Z_OK)
553 request->buffer.buf.len = stream.total_out;
555 strbuf_addstr(&buf, "Destination: ");
556 append_remote_object_url(&buf, remote->url, hex, 0);
557 request->dest = strbuf_detach(&buf, NULL);
559 append_remote_object_url(&buf, remote->url, hex, 0);
560 strbuf_addstr(&buf, "_");
561 strbuf_addstr(&buf, request->lock->token);
562 request->url = strbuf_detach(&buf, NULL);
564 slot = get_active_slot();
565 slot->callback_func = process_response;
566 slot->callback_data = request;
567 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &request->buffer);
568 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, request->buffer.buf.len);
569 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
570 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
571 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
572 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
573 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
574 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
575 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
577 if (start_active_slot(slot)) {
578 request->slot = slot;
579 request->state = RUN_PUT;
581 request->state = ABORTED;
587 static void start_move(struct transfer_request *request)
589 struct active_request_slot *slot;
590 struct curl_slist *dav_headers = NULL;
592 slot = get_active_slot();
593 slot->callback_func = process_response;
594 slot->callback_data = request;
595 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
596 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MOVE);
597 dav_headers = curl_slist_append(dav_headers, request->dest);
598 dav_headers = curl_slist_append(dav_headers, "Overwrite: T");
599 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
600 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
601 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
603 if (start_active_slot(slot)) {
604 request->slot = slot;
605 request->state = RUN_MOVE;
607 request->state = ABORTED;
613 static int refresh_lock(struct remote_lock *lock)
615 struct active_request_slot *slot;
616 struct slot_results results;
617 struct curl_slist *dav_headers;
620 lock->refreshing = 1;
622 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF | DAV_HEADER_TIMEOUT);
624 slot = get_active_slot();
625 slot->results = &results;
626 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
627 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
628 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
629 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
630 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
632 if (start_active_slot(slot)) {
633 run_active_slot(slot);
634 if (results.curl_result != CURLE_OK) {
635 fprintf(stderr, "LOCK HTTP error %ld\n",
638 lock->start_time = time(NULL);
643 lock->refreshing = 0;
644 curl_slist_free_all(dav_headers);
649 static void check_locks(void)
651 struct remote_lock *lock = remote->locks;
652 time_t current_time = time(NULL);
656 time_remaining = lock->start_time + lock->timeout -
658 if (!lock->refreshing && time_remaining < LOCK_REFRESH) {
659 if (!refresh_lock(lock)) {
661 "Unable to refresh lock for %s\n",
671 static void release_request(struct transfer_request *request)
673 struct transfer_request *entry = request_queue_head;
675 if (request == request_queue_head) {
676 request_queue_head = request->next;
678 while (entry->next != NULL && entry->next != request)
680 if (entry->next == request)
681 entry->next = entry->next->next;
684 if (request->local_fileno != -1)
685 close(request->local_fileno);
686 if (request->local_stream)
687 fclose(request->local_stream);
692 static void finish_request(struct transfer_request *request)
695 struct packed_git *target;
696 struct packed_git **lst;
698 request->curl_result = request->slot->curl_result;
699 request->http_code = request->slot->http_code;
700 request->slot = NULL;
702 /* Keep locks active */
705 if (request->headers != NULL)
706 curl_slist_free_all(request->headers);
708 /* URL is reused for MOVE after PUT */
709 if (request->state != RUN_PUT) {
714 if (request->state == RUN_MKCOL) {
715 if (request->curl_result == CURLE_OK ||
716 request->http_code == 405) {
717 remote_dir_exists[request->obj->sha1[0]] = 1;
720 fprintf(stderr, "MKCOL %s failed, aborting (%d/%ld)\n",
721 sha1_to_hex(request->obj->sha1),
722 request->curl_result, request->http_code);
723 request->state = ABORTED;
726 } else if (request->state == RUN_PUT) {
727 if (request->curl_result == CURLE_OK) {
730 fprintf(stderr, "PUT %s failed, aborting (%d/%ld)\n",
731 sha1_to_hex(request->obj->sha1),
732 request->curl_result, request->http_code);
733 request->state = ABORTED;
736 } else if (request->state == RUN_MOVE) {
737 if (request->curl_result == CURLE_OK) {
739 fprintf(stderr, " sent %s\n",
740 sha1_to_hex(request->obj->sha1));
741 request->obj->flags |= REMOTE;
742 release_request(request);
744 fprintf(stderr, "MOVE %s failed, aborting (%d/%ld)\n",
745 sha1_to_hex(request->obj->sha1),
746 request->curl_result, request->http_code);
747 request->state = ABORTED;
750 } else if (request->state == RUN_FETCH_LOOSE) {
751 fchmod(request->local_fileno, 0444);
752 close(request->local_fileno); request->local_fileno = -1;
754 if (request->curl_result != CURLE_OK &&
755 request->http_code != 416) {
756 if (stat(request->tmpfile, &st) == 0) {
758 unlink(request->tmpfile);
761 if (request->http_code == 416)
762 fprintf(stderr, "Warning: requested range invalid; we may already have all the data.\n");
764 git_inflate_end(&request->stream);
765 git_SHA1_Final(request->real_sha1, &request->c);
766 if (request->zret != Z_STREAM_END) {
767 unlink(request->tmpfile);
768 } else if (hashcmp(request->obj->sha1, request->real_sha1)) {
769 unlink(request->tmpfile);
775 if (request->rename == 0) {
776 request->obj->flags |= (LOCAL | REMOTE);
781 /* Try fetching packed if necessary */
782 if (request->obj->flags & LOCAL)
783 release_request(request);
785 start_fetch_packed(request);
787 } else if (request->state == RUN_FETCH_PACKED) {
788 if (request->curl_result != CURLE_OK) {
789 fprintf(stderr, "Unable to get pack file %s\n%s",
790 request->url, curl_errorstr);
791 remote->can_update_info_refs = 0;
793 off_t pack_size = ftell(request->local_stream);
795 fclose(request->local_stream);
796 request->local_stream = NULL;
797 if (!move_temp_to_file(request->tmpfile,
798 request->filename)) {
799 target = (struct packed_git *)request->userData;
800 target->pack_size = pack_size;
801 lst = &remote->packs;
802 while (*lst != target)
803 lst = &((*lst)->next);
806 if (!verify_pack(target))
807 install_packed_git(target);
809 remote->can_update_info_refs = 0;
812 release_request(request);
816 #ifdef USE_CURL_MULTI
817 static int fill_active_slot(void *unused)
819 struct transfer_request *request = request_queue_head;
824 for (request = request_queue_head; request; request = request->next) {
825 if (request->state == NEED_FETCH) {
826 start_fetch_loose(request);
828 } else if (pushing && request->state == NEED_PUSH) {
829 if (remote_dir_exists[request->obj->sha1[0]] == 1) {
832 start_mkcol(request);
841 static void get_remote_object_list(unsigned char parent);
843 static void add_fetch_request(struct object *obj)
845 struct transfer_request *request;
850 * Don't fetch the object if it's known to exist locally
851 * or is already in the request queue
853 if (remote_dir_exists[obj->sha1[0]] == -1)
854 get_remote_object_list(obj->sha1[0]);
855 if (obj->flags & (LOCAL | FETCHING))
858 obj->flags |= FETCHING;
859 request = xmalloc(sizeof(*request));
862 request->lock = NULL;
863 request->headers = NULL;
864 request->local_fileno = -1;
865 request->local_stream = NULL;
866 request->state = NEED_FETCH;
867 request->next = request_queue_head;
868 request_queue_head = request;
870 #ifdef USE_CURL_MULTI
876 static int add_send_request(struct object *obj, struct remote_lock *lock)
878 struct transfer_request *request = request_queue_head;
879 struct packed_git *target;
881 /* Keep locks active */
885 * Don't push the object if it's known to exist on the remote
886 * or is already in the request queue
888 if (remote_dir_exists[obj->sha1[0]] == -1)
889 get_remote_object_list(obj->sha1[0]);
890 if (obj->flags & (REMOTE | PUSHING))
892 target = find_sha1_pack(obj->sha1, remote->packs);
894 obj->flags |= REMOTE;
898 obj->flags |= PUSHING;
899 request = xmalloc(sizeof(*request));
902 request->lock = lock;
903 request->headers = NULL;
904 request->local_fileno = -1;
905 request->local_stream = NULL;
906 request->state = NEED_PUSH;
907 request->next = request_queue_head;
908 request_queue_head = request;
910 #ifdef USE_CURL_MULTI
918 static int fetch_index(unsigned char *sha1)
920 char *hex = sha1_to_hex(sha1);
923 char tmpfile[PATH_MAX];
925 char range[RANGE_HEADER_SIZE];
926 struct curl_slist *range_header = NULL;
929 struct active_request_slot *slot;
930 struct slot_results results;
932 /* Don't use the index if the pack isn't there */
933 url = xmalloc(strlen(remote->url) + 64);
934 sprintf(url, "%sobjects/pack/pack-%s.pack", remote->url, hex);
935 slot = get_active_slot();
936 slot->results = &results;
937 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
938 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
939 if (start_active_slot(slot)) {
940 run_active_slot(slot);
941 if (results.curl_result != CURLE_OK) {
943 return error("Unable to verify pack %s is available",
948 return error("Unable to start request");
951 if (has_pack_index(sha1)) {
957 fprintf(stderr, "Getting index for pack %s\n", hex);
959 sprintf(url, "%sobjects/pack/pack-%s.idx", remote->url, hex);
961 filename = sha1_pack_index_name(sha1);
962 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
963 indexfile = fopen(tmpfile, "a");
966 return error("Unable to open local file %s for pack index",
970 slot = get_active_slot();
971 slot->results = &results;
972 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
973 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
974 curl_easy_setopt(slot->curl, CURLOPT_FILE, indexfile);
975 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
976 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
977 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
978 slot->local = indexfile;
980 /* If there is data present from a previous transfer attempt,
981 resume where it left off */
982 prev_posn = ftell(indexfile);
986 "Resuming fetch of index for pack %s at byte %ld\n",
988 sprintf(range, "Range: bytes=%ld-", prev_posn);
989 range_header = curl_slist_append(range_header, range);
990 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
993 if (start_active_slot(slot)) {
994 run_active_slot(slot);
995 if (results.curl_result != CURLE_OK) {
998 return error("Unable to get pack index %s\n%s", url,
1004 return error("Unable to start request");
1010 return move_temp_to_file(tmpfile, filename);
1013 static int setup_index(unsigned char *sha1)
1015 struct packed_git *new_pack;
1017 if (fetch_index(sha1))
1020 new_pack = parse_pack_index(sha1);
1021 new_pack->next = remote->packs;
1022 remote->packs = new_pack;
1026 static int fetch_indices(void)
1028 unsigned char sha1[20];
1030 struct strbuf buffer = STRBUF_INIT;
1034 struct active_request_slot *slot;
1035 struct slot_results results;
1038 fprintf(stderr, "Getting pack list\n");
1040 url = xmalloc(strlen(remote->url) + 20);
1041 sprintf(url, "%sobjects/info/packs", remote->url);
1043 slot = get_active_slot();
1044 slot->results = &results;
1045 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
1046 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1047 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1048 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
1049 if (start_active_slot(slot)) {
1050 run_active_slot(slot);
1051 if (results.curl_result != CURLE_OK) {
1052 strbuf_release(&buffer);
1054 if (results.http_code == 404)
1057 return error("%s", curl_errorstr);
1060 strbuf_release(&buffer);
1062 return error("Unable to start request");
1067 while (i < buffer.len) {
1071 if (i + 52 < buffer.len &&
1072 !prefixcmp(data + i, " pack-") &&
1073 !prefixcmp(data + i + 46, ".pack\n")) {
1074 get_sha1_hex(data + i + 6, sha1);
1080 while (data[i] != '\n')
1086 strbuf_release(&buffer);
1090 static void one_remote_object(const char *hex)
1092 unsigned char sha1[20];
1095 if (get_sha1_hex(hex, sha1) != 0)
1098 obj = lookup_object(sha1);
1100 obj = parse_object(sha1);
1102 /* Ignore remote objects that don't exist locally */
1106 obj->flags |= REMOTE;
1107 if (!object_list_contains(objects, obj))
1108 object_list_insert(obj, &objects);
1111 static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed)
1113 int *lock_flags = (int *)ctx->userData;
1116 if (!strcmp(ctx->name, DAV_CTX_LOCKENTRY)) {
1117 if ((*lock_flags & DAV_PROP_LOCKEX) &&
1118 (*lock_flags & DAV_PROP_LOCKWR)) {
1119 *lock_flags |= DAV_LOCK_OK;
1121 *lock_flags &= DAV_LOCK_OK;
1122 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_WRITE)) {
1123 *lock_flags |= DAV_PROP_LOCKWR;
1124 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_EXCLUSIVE)) {
1125 *lock_flags |= DAV_PROP_LOCKEX;
1130 static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
1132 struct remote_lock *lock = (struct remote_lock *)ctx->userData;
1134 if (tag_closed && ctx->cdata) {
1135 if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) {
1136 lock->owner = xmalloc(strlen(ctx->cdata) + 1);
1137 strcpy(lock->owner, ctx->cdata);
1138 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
1139 if (!prefixcmp(ctx->cdata, "Second-"))
1141 strtol(ctx->cdata + 7, NULL, 10);
1142 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
1143 lock->token = xmalloc(strlen(ctx->cdata) + 1);
1144 strcpy(lock->token, ctx->cdata);
1149 static void one_remote_ref(char *refname);
1152 xml_start_tag(void *userData, const char *name, const char **atts)
1154 struct xml_ctx *ctx = (struct xml_ctx *)userData;
1155 const char *c = strchr(name, ':');
1163 new_len = strlen(ctx->name) + strlen(c) + 2;
1165 if (new_len > ctx->len) {
1166 ctx->name = xrealloc(ctx->name, new_len);
1169 strcat(ctx->name, ".");
1170 strcat(ctx->name, c);
1175 ctx->userFunc(ctx, 0);
1179 xml_end_tag(void *userData, const char *name)
1181 struct xml_ctx *ctx = (struct xml_ctx *)userData;
1182 const char *c = strchr(name, ':');
1185 ctx->userFunc(ctx, 1);
1192 ep = ctx->name + strlen(ctx->name) - strlen(c) - 1;
1197 xml_cdata(void *userData, const XML_Char *s, int len)
1199 struct xml_ctx *ctx = (struct xml_ctx *)userData;
1201 ctx->cdata = xmemdupz(s, len);
1204 static struct remote_lock *lock_remote(const char *path, long timeout)
1206 struct active_request_slot *slot;
1207 struct slot_results results;
1208 struct buffer out_buffer = { STRBUF_INIT, 0 };
1209 struct strbuf in_buffer = STRBUF_INIT;
1212 char timeout_header[25];
1213 struct remote_lock *lock = NULL;
1214 struct curl_slist *dav_headers = NULL;
1217 url = xmalloc(strlen(remote->url) + strlen(path) + 1);
1218 sprintf(url, "%s%s", remote->url, path);
1220 /* Make sure leading directories exist for the remote ref */
1221 ep = strchr(url + strlen(remote->url) + 1, '/');
1223 char saved_character = ep[1];
1225 slot = get_active_slot();
1226 slot->results = &results;
1227 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
1228 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1229 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
1230 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1231 if (start_active_slot(slot)) {
1232 run_active_slot(slot);
1233 if (results.curl_result != CURLE_OK &&
1234 results.http_code != 405) {
1236 "Unable to create branch path %s\n",
1242 fprintf(stderr, "Unable to start MKCOL request\n");
1246 ep[1] = saved_character;
1247 ep = strchr(ep + 1, '/');
1250 strbuf_addf(&out_buffer.buf, LOCK_REQUEST, git_default_email);
1252 sprintf(timeout_header, "Timeout: Second-%ld", timeout);
1253 dav_headers = curl_slist_append(dav_headers, timeout_header);
1254 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1256 slot = get_active_slot();
1257 slot->results = &results;
1258 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1259 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1260 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1261 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1262 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1263 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1264 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1265 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
1266 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1268 lock = xcalloc(1, sizeof(*lock));
1271 if (start_active_slot(slot)) {
1272 run_active_slot(slot);
1273 if (results.curl_result == CURLE_OK) {
1274 XML_Parser parser = XML_ParserCreate(NULL);
1275 enum XML_Status result;
1276 ctx.name = xcalloc(10, 1);
1279 ctx.userFunc = handle_new_lock_ctx;
1280 ctx.userData = lock;
1281 XML_SetUserData(parser, &ctx);
1282 XML_SetElementHandler(parser, xml_start_tag,
1284 XML_SetCharacterDataHandler(parser, xml_cdata);
1285 result = XML_Parse(parser, in_buffer.buf,
1288 if (result != XML_STATUS_OK) {
1289 fprintf(stderr, "XML error: %s\n",
1291 XML_GetErrorCode(parser)));
1294 XML_ParserFree(parser);
1297 fprintf(stderr, "Unable to start LOCK request\n");
1300 curl_slist_free_all(dav_headers);
1301 strbuf_release(&out_buffer.buf);
1302 strbuf_release(&in_buffer);
1304 if (lock->token == NULL || lock->timeout <= 0) {
1312 lock->start_time = time(NULL);
1313 lock->next = remote->locks;
1314 remote->locks = lock;
1320 static int unlock_remote(struct remote_lock *lock)
1322 struct active_request_slot *slot;
1323 struct slot_results results;
1324 struct remote_lock *prev = remote->locks;
1325 struct curl_slist *dav_headers;
1328 dav_headers = get_dav_token_headers(lock, DAV_HEADER_LOCK);
1330 slot = get_active_slot();
1331 slot->results = &results;
1332 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1333 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1334 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_UNLOCK);
1335 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1337 if (start_active_slot(slot)) {
1338 run_active_slot(slot);
1339 if (results.curl_result == CURLE_OK)
1342 fprintf(stderr, "UNLOCK HTTP error %ld\n",
1345 fprintf(stderr, "Unable to start UNLOCK request\n");
1348 curl_slist_free_all(dav_headers);
1350 if (remote->locks == lock) {
1351 remote->locks = lock->next;
1353 while (prev && prev->next != lock)
1356 prev->next = prev->next->next;
1367 static void remove_locks(void)
1369 struct remote_lock *lock = remote->locks;
1371 fprintf(stderr, "Removing remote locks...\n");
1373 unlock_remote(lock);
1378 static void remove_locks_on_signal(int signo)
1381 sigchain_pop(signo);
1385 static void remote_ls(const char *path, int flags,
1386 void (*userFunc)(struct remote_ls_ctx *ls),
1389 static void process_ls_object(struct remote_ls_ctx *ls)
1391 unsigned int *parent = (unsigned int *)ls->userData;
1392 char *path = ls->dentry_name;
1395 if (!strcmp(ls->path, ls->dentry_name) && (ls->flags & IS_DIR)) {
1396 remote_dir_exists[*parent] = 1;
1400 if (strlen(path) != 49)
1403 obj_hex = xmalloc(strlen(path));
1404 /* NB: path is not null-terminated, can not use strlcpy here */
1405 memcpy(obj_hex, path, 2);
1406 strcpy(obj_hex + 2, path + 3);
1407 one_remote_object(obj_hex);
1411 static void process_ls_ref(struct remote_ls_ctx *ls)
1413 if (!strcmp(ls->path, ls->dentry_name) && (ls->dentry_flags & IS_DIR)) {
1414 fprintf(stderr, " %s\n", ls->dentry_name);
1418 if (!(ls->dentry_flags & IS_DIR))
1419 one_remote_ref(ls->dentry_name);
1422 static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
1424 struct remote_ls_ctx *ls = (struct remote_ls_ctx *)ctx->userData;
1427 if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) {
1428 if (ls->dentry_flags & IS_DIR) {
1429 if (ls->flags & PROCESS_DIRS) {
1432 if (strcmp(ls->dentry_name, ls->path) &&
1433 ls->flags & RECURSIVE) {
1434 remote_ls(ls->dentry_name,
1439 } else if (ls->flags & PROCESS_FILES) {
1442 } else if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
1443 char *path = ctx->cdata;
1444 if (*ctx->cdata == 'h') {
1445 path = strstr(path, "//");
1447 path = strchr(path+2, '/');
1451 path += remote->path_len;
1452 ls->dentry_name = xstrdup(path);
1454 } else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
1455 ls->dentry_flags |= IS_DIR;
1457 } else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
1458 free(ls->dentry_name);
1459 ls->dentry_name = NULL;
1460 ls->dentry_flags = 0;
1465 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1466 * should _only_ heed the information from that file, instead of trying to
1467 * determine the refs from the remote file system (badly: it does not even
1468 * know about packed-refs).
1470 static void remote_ls(const char *path, int flags,
1471 void (*userFunc)(struct remote_ls_ctx *ls),
1474 char *url = xmalloc(strlen(remote->url) + strlen(path) + 1);
1475 struct active_request_slot *slot;
1476 struct slot_results results;
1477 struct strbuf in_buffer = STRBUF_INIT;
1478 struct buffer out_buffer = { STRBUF_INIT, 0 };
1479 struct curl_slist *dav_headers = NULL;
1481 struct remote_ls_ctx ls;
1484 ls.path = xstrdup(path);
1485 ls.dentry_name = NULL;
1486 ls.dentry_flags = 0;
1487 ls.userData = userData;
1488 ls.userFunc = userFunc;
1490 sprintf(url, "%s%s", remote->url, path);
1492 strbuf_addf(&out_buffer.buf, PROPFIND_ALL_REQUEST);
1494 dav_headers = curl_slist_append(dav_headers, "Depth: 1");
1495 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1497 slot = get_active_slot();
1498 slot->results = &results;
1499 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1500 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1501 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1502 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1503 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1504 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1505 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1506 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1507 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1509 if (start_active_slot(slot)) {
1510 run_active_slot(slot);
1511 if (results.curl_result == CURLE_OK) {
1512 XML_Parser parser = XML_ParserCreate(NULL);
1513 enum XML_Status result;
1514 ctx.name = xcalloc(10, 1);
1517 ctx.userFunc = handle_remote_ls_ctx;
1519 XML_SetUserData(parser, &ctx);
1520 XML_SetElementHandler(parser, xml_start_tag,
1522 XML_SetCharacterDataHandler(parser, xml_cdata);
1523 result = XML_Parse(parser, in_buffer.buf,
1527 if (result != XML_STATUS_OK) {
1528 fprintf(stderr, "XML error: %s\n",
1530 XML_GetErrorCode(parser)));
1532 XML_ParserFree(parser);
1535 fprintf(stderr, "Unable to start PROPFIND request\n");
1540 strbuf_release(&out_buffer.buf);
1541 strbuf_release(&in_buffer);
1542 curl_slist_free_all(dav_headers);
1545 static void get_remote_object_list(unsigned char parent)
1547 char path[] = "objects/XX/";
1548 static const char hex[] = "0123456789abcdef";
1549 unsigned int val = parent;
1551 path[8] = hex[val >> 4];
1552 path[9] = hex[val & 0xf];
1553 remote_dir_exists[val] = 0;
1554 remote_ls(path, (PROCESS_FILES | PROCESS_DIRS),
1555 process_ls_object, &val);
1558 static int locking_available(void)
1560 struct active_request_slot *slot;
1561 struct slot_results results;
1562 struct strbuf in_buffer = STRBUF_INIT;
1563 struct buffer out_buffer = { STRBUF_INIT, 0 };
1564 struct curl_slist *dav_headers = NULL;
1568 strbuf_addf(&out_buffer.buf, PROPFIND_SUPPORTEDLOCK_REQUEST, remote->url);
1570 dav_headers = curl_slist_append(dav_headers, "Depth: 0");
1571 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1573 slot = get_active_slot();
1574 slot->results = &results;
1575 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1576 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1577 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1578 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1579 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1580 curl_easy_setopt(slot->curl, CURLOPT_URL, remote->url);
1581 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1582 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1583 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1585 if (start_active_slot(slot)) {
1586 run_active_slot(slot);
1587 if (results.curl_result == CURLE_OK) {
1588 XML_Parser parser = XML_ParserCreate(NULL);
1589 enum XML_Status result;
1590 ctx.name = xcalloc(10, 1);
1593 ctx.userFunc = handle_lockprop_ctx;
1594 ctx.userData = &lock_flags;
1595 XML_SetUserData(parser, &ctx);
1596 XML_SetElementHandler(parser, xml_start_tag,
1598 result = XML_Parse(parser, in_buffer.buf,
1602 if (result != XML_STATUS_OK) {
1603 fprintf(stderr, "XML error: %s\n",
1605 XML_GetErrorCode(parser)));
1608 XML_ParserFree(parser);
1610 error("Error: no DAV locking support on %s",
1614 error("Cannot access URL %s, return code %d",
1615 remote->url, results.curl_result);
1619 error("Unable to start PROPFIND request on %s", remote->url);
1622 strbuf_release(&out_buffer.buf);
1623 strbuf_release(&in_buffer);
1624 curl_slist_free_all(dav_headers);
1629 static struct object_list **add_one_object(struct object *obj, struct object_list **p)
1631 struct object_list *entry = xmalloc(sizeof(struct object_list));
1635 return &entry->next;
1638 static struct object_list **process_blob(struct blob *blob,
1639 struct object_list **p,
1640 struct name_path *path,
1643 struct object *obj = &blob->object;
1645 obj->flags |= LOCAL;
1647 if (obj->flags & (UNINTERESTING | SEEN))
1651 return add_one_object(obj, p);
1654 static struct object_list **process_tree(struct tree *tree,
1655 struct object_list **p,
1656 struct name_path *path,
1659 struct object *obj = &tree->object;
1660 struct tree_desc desc;
1661 struct name_entry entry;
1662 struct name_path me;
1664 obj->flags |= LOCAL;
1666 if (obj->flags & (UNINTERESTING | SEEN))
1668 if (parse_tree(tree) < 0)
1669 die("bad tree object %s", sha1_to_hex(obj->sha1));
1672 name = xstrdup(name);
1673 p = add_one_object(obj, p);
1676 me.elem_len = strlen(name);
1678 init_tree_desc(&desc, tree->buffer, tree->size);
1680 while (tree_entry(&desc, &entry))
1681 switch (object_type(entry.mode)) {
1683 p = process_tree(lookup_tree(entry.sha1), p, &me, name);
1686 p = process_blob(lookup_blob(entry.sha1), p, &me, name);
1689 /* Subproject commit - not in this repository */
1694 tree->buffer = NULL;
1698 static int get_delta(struct rev_info *revs, struct remote_lock *lock)
1701 struct commit *commit;
1702 struct object_list **p = &objects;
1705 while ((commit = get_revision(revs)) != NULL) {
1706 p = process_tree(commit->tree, p, NULL, "");
1707 commit->object.flags |= LOCAL;
1708 if (!(commit->object.flags & UNINTERESTING))
1709 count += add_send_request(&commit->object, lock);
1712 for (i = 0; i < revs->pending.nr; i++) {
1713 struct object_array_entry *entry = revs->pending.objects + i;
1714 struct object *obj = entry->item;
1715 const char *name = entry->name;
1717 if (obj->flags & (UNINTERESTING | SEEN))
1719 if (obj->type == OBJ_TAG) {
1721 p = add_one_object(obj, p);
1724 if (obj->type == OBJ_TREE) {
1725 p = process_tree((struct tree *)obj, p, NULL, name);
1728 if (obj->type == OBJ_BLOB) {
1729 p = process_blob((struct blob *)obj, p, NULL, name);
1732 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
1736 if (!(objects->item->flags & UNINTERESTING))
1737 count += add_send_request(objects->item, lock);
1738 objects = objects->next;
1744 static int update_remote(unsigned char *sha1, struct remote_lock *lock)
1746 struct active_request_slot *slot;
1747 struct slot_results results;
1748 struct buffer out_buffer = { STRBUF_INIT, 0 };
1749 struct curl_slist *dav_headers;
1751 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1753 strbuf_addf(&out_buffer.buf, "%s\n", sha1_to_hex(sha1));
1755 slot = get_active_slot();
1756 slot->results = &results;
1757 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1758 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1759 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1760 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1761 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1762 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1763 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1764 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1765 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1767 if (start_active_slot(slot)) {
1768 run_active_slot(slot);
1769 strbuf_release(&out_buffer.buf);
1770 if (results.curl_result != CURLE_OK) {
1772 "PUT error: curl result=%d, HTTP code=%ld\n",
1773 results.curl_result, results.http_code);
1774 /* We should attempt recovery? */
1778 strbuf_release(&out_buffer.buf);
1779 fprintf(stderr, "Unable to start PUT request\n");
1786 static struct ref *local_refs, **local_tail;
1787 static struct ref *remote_refs, **remote_tail;
1789 static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
1792 int len = strlen(refname) + 1;
1793 ref = xcalloc(1, sizeof(*ref) + len);
1794 hashcpy(ref->new_sha1, sha1);
1795 memcpy(ref->name, refname, len);
1797 local_tail = &ref->next;
1801 static void one_remote_ref(char *refname)
1806 ref = alloc_ref(refname);
1808 if (http_fetch_ref(remote->url, ref) != 0) {
1810 "Unable to fetch ref %s from %s\n",
1811 refname, remote->url);
1817 * Fetch a copy of the object if it doesn't exist locally - it
1818 * may be required for updating server info later.
1820 if (remote->can_update_info_refs && !has_sha1_file(ref->old_sha1)) {
1821 obj = lookup_unknown_object(ref->old_sha1);
1823 fprintf(stderr, " fetch %s for %s\n",
1824 sha1_to_hex(ref->old_sha1), refname);
1825 add_fetch_request(obj);
1830 remote_tail = &ref->next;
1833 static void get_local_heads(void)
1835 local_tail = &local_refs;
1836 for_each_ref(one_local_ref, NULL);
1839 static void get_dav_remote_heads(void)
1841 remote_tail = &remote_refs;
1842 remote_ls("refs/", (PROCESS_FILES | PROCESS_DIRS | RECURSIVE), process_ls_ref, NULL);
1845 static int is_zero_sha1(const unsigned char *sha1)
1849 for (i = 0; i < 20; i++) {
1856 static void unmark_and_free(struct commit_list *list, unsigned int mark)
1859 struct commit_list *temp = list;
1860 temp->item->object.flags &= ~mark;
1866 static int ref_newer(const unsigned char *new_sha1,
1867 const unsigned char *old_sha1)
1870 struct commit *old, *new;
1871 struct commit_list *list, *used;
1874 /* Both new and old must be commit-ish and new is descendant of
1875 * old. Otherwise we require --force.
1877 o = deref_tag(parse_object(old_sha1), NULL, 0);
1878 if (!o || o->type != OBJ_COMMIT)
1880 old = (struct commit *) o;
1882 o = deref_tag(parse_object(new_sha1), NULL, 0);
1883 if (!o || o->type != OBJ_COMMIT)
1885 new = (struct commit *) o;
1887 if (parse_commit(new) < 0)
1891 commit_list_insert(new, &list);
1893 new = pop_most_recent_commit(&list, TMP_MARK);
1894 commit_list_insert(new, &used);
1900 unmark_and_free(list, TMP_MARK);
1901 unmark_and_free(used, TMP_MARK);
1905 static void add_remote_info_ref(struct remote_ls_ctx *ls)
1907 struct strbuf *buf = (struct strbuf *)ls->userData;
1913 ref = alloc_ref(ls->dentry_name);
1915 if (http_fetch_ref(remote->url, ref) != 0) {
1917 "Unable to fetch ref %s from %s\n",
1918 ls->dentry_name, remote->url);
1924 o = parse_object(ref->old_sha1);
1927 "Unable to parse object %s for remote ref %s\n",
1928 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1934 len = strlen(ls->dentry_name) + 42;
1935 ref_info = xcalloc(len + 1, 1);
1936 sprintf(ref_info, "%s %s\n",
1937 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1938 fwrite_buffer(ref_info, 1, len, buf);
1941 if (o->type == OBJ_TAG) {
1942 o = deref_tag(o, ls->dentry_name, 0);
1944 len = strlen(ls->dentry_name) + 45;
1945 ref_info = xcalloc(len + 1, 1);
1946 sprintf(ref_info, "%s %s^{}\n",
1947 sha1_to_hex(o->sha1), ls->dentry_name);
1948 fwrite_buffer(ref_info, 1, len, buf);
1955 static void update_remote_info_refs(struct remote_lock *lock)
1957 struct buffer buffer = { STRBUF_INIT, 0 };
1958 struct active_request_slot *slot;
1959 struct slot_results results;
1960 struct curl_slist *dav_headers;
1962 remote_ls("refs/", (PROCESS_FILES | RECURSIVE),
1963 add_remote_info_ref, &buffer.buf);
1965 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1967 slot = get_active_slot();
1968 slot->results = &results;
1969 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &buffer);
1970 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, buffer.buf.len);
1971 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1972 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1973 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1974 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1975 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1976 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1977 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1979 if (start_active_slot(slot)) {
1980 run_active_slot(slot);
1981 if (results.curl_result != CURLE_OK) {
1983 "PUT error: curl result=%d, HTTP code=%ld\n",
1984 results.curl_result, results.http_code);
1988 strbuf_release(&buffer.buf);
1991 static int remote_exists(const char *path)
1993 char *url = xmalloc(strlen(remote->url) + strlen(path) + 1);
1994 struct active_request_slot *slot;
1995 struct slot_results results;
1998 sprintf(url, "%s%s", remote->url, path);
2000 slot = get_active_slot();
2001 slot->results = &results;
2002 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2003 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
2005 if (start_active_slot(slot)) {
2006 run_active_slot(slot);
2007 if (results.http_code == 404)
2009 else if (results.curl_result == CURLE_OK)
2012 fprintf(stderr, "HEAD HTTP error %ld\n", results.http_code);
2014 fprintf(stderr, "Unable to start HEAD request\n");
2021 static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
2024 struct strbuf buffer = STRBUF_INIT;
2025 struct active_request_slot *slot;
2026 struct slot_results results;
2028 url = xmalloc(strlen(remote->url) + strlen(path) + 1);
2029 sprintf(url, "%s%s", remote->url, path);
2031 slot = get_active_slot();
2032 slot->results = &results;
2033 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
2034 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
2035 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
2036 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2037 if (start_active_slot(slot)) {
2038 run_active_slot(slot);
2039 if (results.curl_result != CURLE_OK) {
2040 die("Couldn't get %s for remote symref\n%s",
2041 url, curl_errorstr);
2044 die("Unable to start remote symref request");
2052 if (buffer.len == 0)
2055 /* If it's a symref, set the refname; otherwise try for a sha1 */
2056 if (!prefixcmp((char *)buffer.buf, "ref: ")) {
2057 *symref = xmemdupz((char *)buffer.buf + 5, buffer.len - 6);
2059 get_sha1_hex(buffer.buf, sha1);
2062 strbuf_release(&buffer);
2065 static int verify_merge_base(unsigned char *head_sha1, unsigned char *branch_sha1)
2067 struct commit *head = lookup_commit(head_sha1);
2068 struct commit *branch = lookup_commit(branch_sha1);
2069 struct commit_list *merge_bases = get_merge_bases(head, branch, 1);
2071 return (merge_bases && !merge_bases->next && merge_bases->item == branch);
2074 static int delete_remote_branch(char *pattern, int force)
2076 struct ref *refs = remote_refs;
2077 struct ref *remote_ref = NULL;
2078 unsigned char head_sha1[20];
2079 char *symref = NULL;
2081 int patlen = strlen(pattern);
2083 struct active_request_slot *slot;
2084 struct slot_results results;
2087 /* Find the remote branch(es) matching the specified branch name */
2088 for (match = 0; refs; refs = refs->next) {
2089 char *name = refs->name;
2090 int namelen = strlen(name);
2091 if (namelen < patlen ||
2092 memcmp(name + namelen - patlen, pattern, patlen))
2094 if (namelen != patlen && name[namelen - patlen - 1] != '/')
2100 return error("No remote branch matches %s", pattern);
2102 return error("More than one remote branch matches %s",
2106 * Remote HEAD must be a symref (not exactly foolproof; a remote
2107 * symlink to a symref will look like a symref)
2109 fetch_symref("HEAD", &symref, head_sha1);
2111 return error("Remote HEAD is not a symref");
2113 /* Remote branch must not be the remote HEAD */
2114 for (i=0; symref && i<MAXDEPTH; i++) {
2115 if (!strcmp(remote_ref->name, symref))
2116 return error("Remote branch %s is the current HEAD",
2118 fetch_symref(symref, &symref, head_sha1);
2121 /* Run extra sanity checks if delete is not forced */
2123 /* Remote HEAD must resolve to a known object */
2125 return error("Remote HEAD symrefs too deep");
2126 if (is_zero_sha1(head_sha1))
2127 return error("Unable to resolve remote HEAD");
2128 if (!has_sha1_file(head_sha1))
2129 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1));
2131 /* Remote branch must resolve to a known object */
2132 if (is_zero_sha1(remote_ref->old_sha1))
2133 return error("Unable to resolve remote branch %s",
2135 if (!has_sha1_file(remote_ref->old_sha1))
2136 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));
2138 /* Remote branch must be an ancestor of remote HEAD */
2139 if (!verify_merge_base(head_sha1, remote_ref->old_sha1)) {
2140 return error("The branch '%s' is not an ancestor "
2141 "of your current HEAD.\n"
2142 "If you are sure you want to delete it,"
2143 " run:\n\t'git http-push -D %s %s'",
2144 remote_ref->name, remote->url, pattern);
2148 /* Send delete request */
2149 fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name);
2152 url = xmalloc(strlen(remote->url) + strlen(remote_ref->name) + 1);
2153 sprintf(url, "%s%s", remote->url, remote_ref->name);
2154 slot = get_active_slot();
2155 slot->results = &results;
2156 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
2157 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
2158 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2159 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_DELETE);
2160 if (start_active_slot(slot)) {
2161 run_active_slot(slot);
2163 if (results.curl_result != CURLE_OK)
2164 return error("DELETE request failed (%d/%ld)\n",
2165 results.curl_result, results.http_code);
2168 return error("Unable to start DELETE request");
2174 int main(int argc, char **argv)
2176 struct transfer_request *request;
2177 struct transfer_request *next_request;
2179 char **refspec = NULL;
2180 struct remote_lock *ref_lock = NULL;
2181 struct remote_lock *info_ref_lock = NULL;
2182 struct rev_info revs;
2183 int delete_branch = 0;
2184 int force_delete = 0;
2185 int objects_to_send;
2190 char *rewritten_url = NULL;
2192 git_extract_argv0_path(argv[0]);
2194 setup_git_directory();
2196 remote = xcalloc(sizeof(*remote), 1);
2199 for (i = 1; i < argc; i++, argv++) {
2203 if (!strcmp(arg, "--all")) {
2204 push_all = MATCH_REFS_ALL;
2207 if (!strcmp(arg, "--force")) {
2211 if (!strcmp(arg, "--dry-run")) {
2215 if (!strcmp(arg, "--verbose")) {
2219 if (!strcmp(arg, "-d")) {
2223 if (!strcmp(arg, "-D")) {
2230 char *path = strstr(arg, "//");
2232 remote->path_len = strlen(arg);
2234 remote->path = strchr(path+2, '/');
2236 remote->path_len = strlen(remote->path);
2241 nr_refspec = argc - i;
2245 #ifndef USE_CURL_MULTI
2246 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
2250 usage(http_push_usage);
2252 if (delete_branch && nr_refspec != 1)
2253 die("You must specify only one branch name when deleting a remote branch");
2255 memset(remote_dir_exists, -1, 256);
2259 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
2261 if (remote->url && remote->url[strlen(remote->url)-1] != '/') {
2262 rewritten_url = xmalloc(strlen(remote->url)+2);
2263 strcpy(rewritten_url, remote->url);
2264 strcat(rewritten_url, "/");
2265 remote->path = rewritten_url + (remote->path - remote->url);
2267 remote->url = rewritten_url;
2270 /* Verify DAV compliance/lock support */
2271 if (!locking_available()) {
2276 sigchain_push_common(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;