12 #include "list-objects.h"
16 static const char http_push_usage[] =
17 "git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
24 #define XML_STATUS_OK 1
25 #define XML_STATUS_ERROR 0
28 #define PREV_BUF_SIZE 4096
29 #define RANGE_HEADER_SIZE 30
32 #define DAV_LOCK "LOCK"
33 #define DAV_MKCOL "MKCOL"
34 #define DAV_MOVE "MOVE"
35 #define DAV_PROPFIND "PROPFIND"
37 #define DAV_UNLOCK "UNLOCK"
38 #define DAV_DELETE "DELETE"
41 #define DAV_PROP_LOCKWR (1u << 0)
42 #define DAV_PROP_LOCKEX (1u << 1)
43 #define DAV_LOCK_OK (1u << 2)
45 /* DAV XML properties */
46 #define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
47 #define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
48 #define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
49 #define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
50 #define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
51 #define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
52 #define DAV_PROPFIND_RESP ".multistatus.response"
53 #define DAV_PROPFIND_NAME ".multistatus.response.href"
54 #define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
56 /* DAV request body templates */
57 #define PROPFIND_SUPPORTEDLOCK_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:prop xmlns:R=\"%s\">\n<D:supportedlock/>\n</D:prop>\n</D:propfind>"
58 #define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
59 #define LOCK_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:lockinfo xmlns:D=\"DAV:\">\n<D:lockscope><D:exclusive/></D:lockscope>\n<D:locktype><D:write/></D:locktype>\n<D:owner>\n<D:href>mailto:%s</D:href>\n</D:owner>\n</D:lockinfo>"
62 #define LOCK_REFRESH 30
64 /* bits #0-15 in revision.h */
66 #define LOCAL (1u<<16)
67 #define REMOTE (1u<<17)
68 #define FETCHING (1u<<18)
69 #define PUSHING (1u<<19)
71 /* We allow "recursive" symbolic refs. Only within reason, though */
76 static signed char remote_dir_exists[256];
78 static struct curl_slist *no_pragma_header;
80 static int push_verbosely;
81 static int push_all = MATCH_REFS_NONE;
85 static struct object_list *objects;
93 int can_update_info_refs;
95 struct packed_git *packs;
96 struct remote_lock *locks;
99 static struct repo *remote;
101 enum transfer_state {
113 struct transfer_request
118 struct remote_lock *lock;
119 struct curl_slist *headers;
120 struct buffer buffer;
121 char filename[PATH_MAX];
122 char tmpfile[PATH_MAX];
125 enum transfer_state state;
126 CURLcode curl_result;
127 char errorstr[CURL_ERROR_SIZE];
129 unsigned char real_sha1[20];
135 struct active_request_slot *slot;
136 struct transfer_request *next;
139 static struct transfer_request *request_queue_head;
146 void (*userFunc)(struct xml_ctx *ctx, int tag_closed);
158 struct remote_lock *next;
161 /* Flags that control remote_ls processing */
162 #define PROCESS_FILES (1u << 0)
163 #define PROCESS_DIRS (1u << 1)
164 #define RECURSIVE (1u << 2)
166 /* Flags that remote_ls passes to callback functions */
167 #define IS_DIR (1u << 0)
172 void (*userFunc)(struct remote_ls_ctx *ls);
177 struct remote_ls_ctx *parent;
180 /* get_dav_token_headers options */
181 enum dav_header_flag {
182 DAV_HEADER_IF = (1u << 0),
183 DAV_HEADER_LOCK = (1u << 1),
184 DAV_HEADER_TIMEOUT = (1u << 2)
187 static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum dav_header_flag options) {
188 struct strbuf buf = STRBUF_INIT;
189 struct curl_slist *dav_headers = NULL;
191 if(options & DAV_HEADER_IF) {
192 strbuf_addf(&buf, "If: (<%s>)", lock->token);
193 dav_headers = curl_slist_append(dav_headers, buf.buf);
196 if(options & DAV_HEADER_LOCK) {
197 strbuf_addf(&buf, "Lock-Token: <%s>", lock->token);
198 dav_headers = curl_slist_append(dav_headers, buf.buf);
201 if(options & DAV_HEADER_TIMEOUT) {
202 strbuf_addf(&buf, "Timeout: Second-%ld", lock->timeout);
203 dav_headers = curl_slist_append(dav_headers, buf.buf);
206 strbuf_release(&buf);
211 static void finish_request(struct transfer_request *request);
212 static void release_request(struct transfer_request *request);
214 static void process_response(void *callback_data)
216 struct transfer_request *request =
217 (struct transfer_request *)callback_data;
219 finish_request(request);
222 #ifdef USE_CURL_MULTI
223 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
226 unsigned char expn[4096];
227 size_t size = eltsize * nmemb;
229 struct transfer_request *request = (struct transfer_request *)data;
231 ssize_t retval = xwrite(request->local_fileno,
232 (char *) ptr + posn, size - posn);
236 } while (posn < size);
238 request->stream.avail_in = size;
239 request->stream.next_in = ptr;
241 request->stream.next_out = expn;
242 request->stream.avail_out = sizeof(expn);
243 request->zret = git_inflate(&request->stream, Z_SYNC_FLUSH);
244 git_SHA1_Update(&request->c, expn,
245 sizeof(expn) - request->stream.avail_out);
246 } while (request->stream.avail_in && request->zret == Z_OK);
251 static void start_fetch_loose(struct transfer_request *request)
253 char *hex = sha1_to_hex(request->obj->sha1);
255 char prevfile[PATH_MAX];
259 unsigned char prev_buf[PREV_BUF_SIZE];
260 ssize_t prev_read = 0;
262 char range[RANGE_HEADER_SIZE];
263 struct curl_slist *range_header = NULL;
264 struct active_request_slot *slot;
266 filename = sha1_file_name(request->obj->sha1);
267 snprintf(request->filename, sizeof(request->filename), "%s", filename);
268 snprintf(request->tmpfile, sizeof(request->tmpfile),
269 "%s.temp", filename);
271 snprintf(prevfile, sizeof(prevfile), "%s.prev", request->filename);
273 rename(request->tmpfile, prevfile);
274 unlink(request->tmpfile);
276 if (request->local_fileno != -1)
277 error("fd leakage in start: %d", request->local_fileno);
278 request->local_fileno = open(request->tmpfile,
279 O_WRONLY | O_CREAT | O_EXCL, 0666);
280 /* This could have failed due to the "lazy directory creation";
281 * try to mkdir the last path component.
283 if (request->local_fileno < 0 && errno == ENOENT) {
284 char *dir = strrchr(request->tmpfile, '/');
287 mkdir(request->tmpfile, 0777);
290 request->local_fileno = open(request->tmpfile,
291 O_WRONLY | O_CREAT | O_EXCL, 0666);
294 if (request->local_fileno < 0) {
295 request->state = ABORTED;
296 error("Couldn't create temporary file %s for %s: %s",
297 request->tmpfile, request->filename, strerror(errno));
301 memset(&request->stream, 0, sizeof(request->stream));
303 git_inflate_init(&request->stream);
305 git_SHA1_Init(&request->c);
307 url = xmalloc(strlen(remote->url) + 50);
308 request->url = xmalloc(strlen(remote->url) + 50);
309 strcpy(url, remote->url);
310 posn = url + strlen(remote->url);
311 strcpy(posn, "objects/");
313 memcpy(posn, hex, 2);
316 strcpy(posn, hex + 2);
317 strcpy(request->url, url);
319 /* If a previous temp file is present, process what was already
321 prevlocal = open(prevfile, O_RDONLY);
322 if (prevlocal != -1) {
324 prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE);
326 if (fwrite_sha1_file(prev_buf,
329 request) == prev_read) {
330 prev_posn += prev_read;
335 } while (prev_read > 0);
340 /* Reset inflate/SHA1 if there was an error reading the previous temp
341 file; also rewind to the beginning of the local file. */
342 if (prev_read == -1) {
343 memset(&request->stream, 0, sizeof(request->stream));
344 git_inflate_init(&request->stream);
345 git_SHA1_Init(&request->c);
348 lseek(request->local_fileno, 0, SEEK_SET);
349 ftruncate(request->local_fileno, 0);
353 slot = get_active_slot();
354 slot->callback_func = process_response;
355 slot->callback_data = request;
356 request->slot = slot;
358 curl_easy_setopt(slot->curl, CURLOPT_FILE, request);
359 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
360 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
361 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
362 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
364 /* If we have successfully processed data from a previous fetch
365 attempt, only fetch the data we don't already have. */
369 "Resuming fetch of object %s at byte %ld\n",
371 sprintf(range, "Range: bytes=%ld-", prev_posn);
372 range_header = curl_slist_append(range_header, range);
373 curl_easy_setopt(slot->curl,
374 CURLOPT_HTTPHEADER, range_header);
377 /* Try to get the request started, abort the request on error */
378 request->state = RUN_FETCH_LOOSE;
379 if (!start_active_slot(slot)) {
380 fprintf(stderr, "Unable to start GET request\n");
381 remote->can_update_info_refs = 0;
382 release_request(request);
386 static void start_mkcol(struct transfer_request *request)
388 char *hex = sha1_to_hex(request->obj->sha1);
389 struct active_request_slot *slot;
392 request->url = xmalloc(strlen(remote->url) + 13);
393 strcpy(request->url, remote->url);
394 posn = request->url + strlen(remote->url);
395 strcpy(posn, "objects/");
397 memcpy(posn, hex, 2);
401 slot = get_active_slot();
402 slot->callback_func = process_response;
403 slot->callback_data = request;
404 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
405 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
406 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
407 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
408 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
410 if (start_active_slot(slot)) {
411 request->slot = slot;
412 request->state = RUN_MKCOL;
414 request->state = ABORTED;
421 static void start_fetch_packed(struct transfer_request *request)
424 struct packed_git *target;
428 char range[RANGE_HEADER_SIZE];
429 struct curl_slist *range_header = NULL;
431 struct transfer_request *check_request = request_queue_head;
432 struct active_request_slot *slot;
434 target = find_sha1_pack(request->obj->sha1, remote->packs);
436 fprintf(stderr, "Unable to fetch %s, will not be able to update server info refs\n", sha1_to_hex(request->obj->sha1));
437 remote->can_update_info_refs = 0;
438 release_request(request);
442 fprintf(stderr, "Fetching pack %s\n", sha1_to_hex(target->sha1));
443 fprintf(stderr, " which contains %s\n", sha1_to_hex(request->obj->sha1));
445 filename = sha1_pack_name(target->sha1);
446 snprintf(request->filename, sizeof(request->filename), "%s", filename);
447 snprintf(request->tmpfile, sizeof(request->tmpfile),
448 "%s.temp", filename);
450 url = xmalloc(strlen(remote->url) + 64);
451 sprintf(url, "%sobjects/pack/pack-%s.pack",
452 remote->url, sha1_to_hex(target->sha1));
454 /* Make sure there isn't another open request for this pack */
455 while (check_request) {
456 if (check_request->state == RUN_FETCH_PACKED &&
457 !strcmp(check_request->url, url)) {
459 release_request(request);
462 check_request = check_request->next;
465 packfile = fopen(request->tmpfile, "a");
467 fprintf(stderr, "Unable to open local file %s for pack",
469 remote->can_update_info_refs = 0;
474 slot = get_active_slot();
475 slot->callback_func = process_response;
476 slot->callback_data = request;
477 request->slot = slot;
478 request->local_stream = packfile;
479 request->userData = target;
482 curl_easy_setopt(slot->curl, CURLOPT_FILE, packfile);
483 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
484 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
485 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
486 slot->local = packfile;
488 /* If there is data present from a previous transfer attempt,
489 resume where it left off */
490 prev_posn = ftell(packfile);
494 "Resuming fetch of pack %s at byte %ld\n",
495 sha1_to_hex(target->sha1), prev_posn);
496 sprintf(range, "Range: bytes=%ld-", prev_posn);
497 range_header = curl_slist_append(range_header, range);
498 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
501 /* Try to get the request started, abort the request on error */
502 request->state = RUN_FETCH_PACKED;
503 if (!start_active_slot(slot)) {
504 fprintf(stderr, "Unable to start GET request\n");
505 remote->can_update_info_refs = 0;
506 release_request(request);
510 static void start_put(struct transfer_request *request)
512 char *hex = sha1_to_hex(request->obj->sha1);
513 struct active_request_slot *slot;
515 enum object_type type;
523 unpacked = read_sha1_file(request->obj->sha1, &type, &len);
524 hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
527 memset(&stream, 0, sizeof(stream));
528 deflateInit(&stream, zlib_compression_level);
529 size = deflateBound(&stream, len + hdrlen);
530 strbuf_init(&request->buffer.buf, size);
531 request->buffer.posn = 0;
534 stream.next_out = (unsigned char *)request->buffer.buf.buf;
535 stream.avail_out = size;
538 stream.next_in = (void *)hdr;
539 stream.avail_in = hdrlen;
540 while (deflate(&stream, 0) == Z_OK)
543 /* Then the data itself.. */
544 stream.next_in = unpacked;
545 stream.avail_in = len;
546 while (deflate(&stream, Z_FINISH) == Z_OK)
551 request->buffer.buf.len = stream.total_out;
553 request->url = xmalloc(strlen(remote->url) +
554 strlen(request->lock->token) + 51);
555 strcpy(request->url, remote->url);
556 posn = request->url + strlen(remote->url);
557 strcpy(posn, "objects/");
559 memcpy(posn, hex, 2);
562 strcpy(posn, hex + 2);
563 request->dest = xmalloc(strlen(request->url) + 14);
564 sprintf(request->dest, "Destination: %s", request->url);
567 strcpy(posn, request->lock->token);
569 slot = get_active_slot();
570 slot->callback_func = process_response;
571 slot->callback_data = request;
572 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &request->buffer);
573 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, request->buffer.buf.len);
574 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
575 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
576 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
577 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
578 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
579 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
580 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
582 if (start_active_slot(slot)) {
583 request->slot = slot;
584 request->state = RUN_PUT;
586 request->state = ABORTED;
592 static void start_move(struct transfer_request *request)
594 struct active_request_slot *slot;
595 struct curl_slist *dav_headers = NULL;
597 slot = get_active_slot();
598 slot->callback_func = process_response;
599 slot->callback_data = request;
600 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
601 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MOVE);
602 dav_headers = curl_slist_append(dav_headers, request->dest);
603 dav_headers = curl_slist_append(dav_headers, "Overwrite: T");
604 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
605 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
606 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
608 if (start_active_slot(slot)) {
609 request->slot = slot;
610 request->state = RUN_MOVE;
612 request->state = ABORTED;
618 static int refresh_lock(struct remote_lock *lock)
620 struct active_request_slot *slot;
621 struct slot_results results;
622 struct curl_slist *dav_headers;
625 lock->refreshing = 1;
627 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF | DAV_HEADER_TIMEOUT);
629 slot = get_active_slot();
630 slot->results = &results;
631 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
632 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
633 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
634 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
635 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
637 if (start_active_slot(slot)) {
638 run_active_slot(slot);
639 if (results.curl_result != CURLE_OK) {
640 fprintf(stderr, "LOCK HTTP error %ld\n",
643 lock->start_time = time(NULL);
648 lock->refreshing = 0;
649 curl_slist_free_all(dav_headers);
654 static void check_locks(void)
656 struct remote_lock *lock = remote->locks;
657 time_t current_time = time(NULL);
661 time_remaining = lock->start_time + lock->timeout -
663 if (!lock->refreshing && time_remaining < LOCK_REFRESH) {
664 if (!refresh_lock(lock)) {
666 "Unable to refresh lock for %s\n",
676 static void release_request(struct transfer_request *request)
678 struct transfer_request *entry = request_queue_head;
680 if (request == request_queue_head) {
681 request_queue_head = request->next;
683 while (entry->next != NULL && entry->next != request)
685 if (entry->next == request)
686 entry->next = entry->next->next;
689 if (request->local_fileno != -1)
690 close(request->local_fileno);
691 if (request->local_stream)
692 fclose(request->local_stream);
697 static void finish_request(struct transfer_request *request)
700 struct packed_git *target;
701 struct packed_git **lst;
703 request->curl_result = request->slot->curl_result;
704 request->http_code = request->slot->http_code;
705 request->slot = NULL;
707 /* Keep locks active */
710 if (request->headers != NULL)
711 curl_slist_free_all(request->headers);
713 /* URL is reused for MOVE after PUT */
714 if (request->state != RUN_PUT) {
719 if (request->state == RUN_MKCOL) {
720 if (request->curl_result == CURLE_OK ||
721 request->http_code == 405) {
722 remote_dir_exists[request->obj->sha1[0]] = 1;
725 fprintf(stderr, "MKCOL %s failed, aborting (%d/%ld)\n",
726 sha1_to_hex(request->obj->sha1),
727 request->curl_result, request->http_code);
728 request->state = ABORTED;
731 } else if (request->state == RUN_PUT) {
732 if (request->curl_result == CURLE_OK) {
735 fprintf(stderr, "PUT %s failed, aborting (%d/%ld)\n",
736 sha1_to_hex(request->obj->sha1),
737 request->curl_result, request->http_code);
738 request->state = ABORTED;
741 } else if (request->state == RUN_MOVE) {
742 if (request->curl_result == CURLE_OK) {
744 fprintf(stderr, " sent %s\n",
745 sha1_to_hex(request->obj->sha1));
746 request->obj->flags |= REMOTE;
747 release_request(request);
749 fprintf(stderr, "MOVE %s failed, aborting (%d/%ld)\n",
750 sha1_to_hex(request->obj->sha1),
751 request->curl_result, request->http_code);
752 request->state = ABORTED;
755 } else if (request->state == RUN_FETCH_LOOSE) {
756 fchmod(request->local_fileno, 0444);
757 close(request->local_fileno); request->local_fileno = -1;
759 if (request->curl_result != CURLE_OK &&
760 request->http_code != 416) {
761 if (stat(request->tmpfile, &st) == 0) {
763 unlink(request->tmpfile);
766 if (request->http_code == 416)
767 fprintf(stderr, "Warning: requested range invalid; we may already have all the data.\n");
769 git_inflate_end(&request->stream);
770 git_SHA1_Final(request->real_sha1, &request->c);
771 if (request->zret != Z_STREAM_END) {
772 unlink(request->tmpfile);
773 } else if (hashcmp(request->obj->sha1, request->real_sha1)) {
774 unlink(request->tmpfile);
780 if (request->rename == 0) {
781 request->obj->flags |= (LOCAL | REMOTE);
786 /* Try fetching packed if necessary */
787 if (request->obj->flags & LOCAL)
788 release_request(request);
790 start_fetch_packed(request);
792 } else if (request->state == RUN_FETCH_PACKED) {
793 if (request->curl_result != CURLE_OK) {
794 fprintf(stderr, "Unable to get pack file %s\n%s",
795 request->url, curl_errorstr);
796 remote->can_update_info_refs = 0;
798 off_t pack_size = ftell(request->local_stream);
800 fclose(request->local_stream);
801 request->local_stream = NULL;
802 if (!move_temp_to_file(request->tmpfile,
803 request->filename)) {
804 target = (struct packed_git *)request->userData;
805 target->pack_size = pack_size;
806 lst = &remote->packs;
807 while (*lst != target)
808 lst = &((*lst)->next);
811 if (!verify_pack(target))
812 install_packed_git(target);
814 remote->can_update_info_refs = 0;
817 release_request(request);
821 #ifdef USE_CURL_MULTI
822 static int fill_active_slot(void *unused)
824 struct transfer_request *request = request_queue_head;
829 for (request = request_queue_head; request; request = request->next) {
830 if (request->state == NEED_FETCH) {
831 start_fetch_loose(request);
833 } else if (pushing && request->state == NEED_PUSH) {
834 if (remote_dir_exists[request->obj->sha1[0]] == 1) {
837 start_mkcol(request);
846 static void get_remote_object_list(unsigned char parent);
848 static void add_fetch_request(struct object *obj)
850 struct transfer_request *request;
855 * Don't fetch the object if it's known to exist locally
856 * or is already in the request queue
858 if (remote_dir_exists[obj->sha1[0]] == -1)
859 get_remote_object_list(obj->sha1[0]);
860 if (obj->flags & (LOCAL | FETCHING))
863 obj->flags |= FETCHING;
864 request = xmalloc(sizeof(*request));
867 request->lock = NULL;
868 request->headers = NULL;
869 request->local_fileno = -1;
870 request->local_stream = NULL;
871 request->state = NEED_FETCH;
872 request->next = request_queue_head;
873 request_queue_head = request;
875 #ifdef USE_CURL_MULTI
881 static int add_send_request(struct object *obj, struct remote_lock *lock)
883 struct transfer_request *request = request_queue_head;
884 struct packed_git *target;
886 /* Keep locks active */
890 * Don't push the object if it's known to exist on the remote
891 * or is already in the request queue
893 if (remote_dir_exists[obj->sha1[0]] == -1)
894 get_remote_object_list(obj->sha1[0]);
895 if (obj->flags & (REMOTE | PUSHING))
897 target = find_sha1_pack(obj->sha1, remote->packs);
899 obj->flags |= REMOTE;
903 obj->flags |= PUSHING;
904 request = xmalloc(sizeof(*request));
907 request->lock = lock;
908 request->headers = NULL;
909 request->local_fileno = -1;
910 request->local_stream = NULL;
911 request->state = NEED_PUSH;
912 request->next = request_queue_head;
913 request_queue_head = request;
915 #ifdef USE_CURL_MULTI
923 static int fetch_index(unsigned char *sha1)
925 char *hex = sha1_to_hex(sha1);
928 char tmpfile[PATH_MAX];
930 char range[RANGE_HEADER_SIZE];
931 struct curl_slist *range_header = NULL;
934 struct active_request_slot *slot;
935 struct slot_results results;
937 /* Don't use the index if the pack isn't there */
938 url = xmalloc(strlen(remote->url) + 64);
939 sprintf(url, "%sobjects/pack/pack-%s.pack", remote->url, hex);
940 slot = get_active_slot();
941 slot->results = &results;
942 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
943 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
944 if (start_active_slot(slot)) {
945 run_active_slot(slot);
946 if (results.curl_result != CURLE_OK) {
948 return error("Unable to verify pack %s is available",
953 return error("Unable to start request");
956 if (has_pack_index(sha1)) {
962 fprintf(stderr, "Getting index for pack %s\n", hex);
964 sprintf(url, "%sobjects/pack/pack-%s.idx", remote->url, hex);
966 filename = sha1_pack_index_name(sha1);
967 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
968 indexfile = fopen(tmpfile, "a");
971 return error("Unable to open local file %s for pack index",
975 slot = get_active_slot();
976 slot->results = &results;
977 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
978 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
979 curl_easy_setopt(slot->curl, CURLOPT_FILE, indexfile);
980 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
981 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
982 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
983 slot->local = indexfile;
985 /* If there is data present from a previous transfer attempt,
986 resume where it left off */
987 prev_posn = ftell(indexfile);
991 "Resuming fetch of index for pack %s at byte %ld\n",
993 sprintf(range, "Range: bytes=%ld-", prev_posn);
994 range_header = curl_slist_append(range_header, range);
995 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
998 if (start_active_slot(slot)) {
999 run_active_slot(slot);
1000 if (results.curl_result != CURLE_OK) {
1003 return error("Unable to get pack index %s\n%s", url,
1009 return error("Unable to start request");
1015 return move_temp_to_file(tmpfile, filename);
1018 static int setup_index(unsigned char *sha1)
1020 struct packed_git *new_pack;
1022 if (fetch_index(sha1))
1025 new_pack = parse_pack_index(sha1);
1026 new_pack->next = remote->packs;
1027 remote->packs = new_pack;
1031 static int fetch_indices(void)
1033 unsigned char sha1[20];
1035 struct strbuf buffer = STRBUF_INIT;
1039 struct active_request_slot *slot;
1040 struct slot_results results;
1043 fprintf(stderr, "Getting pack list\n");
1045 url = xmalloc(strlen(remote->url) + 20);
1046 sprintf(url, "%sobjects/info/packs", remote->url);
1048 slot = get_active_slot();
1049 slot->results = &results;
1050 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
1051 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1052 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1053 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
1054 if (start_active_slot(slot)) {
1055 run_active_slot(slot);
1056 if (results.curl_result != CURLE_OK) {
1057 strbuf_release(&buffer);
1059 if (results.http_code == 404)
1062 return error("%s", curl_errorstr);
1065 strbuf_release(&buffer);
1067 return error("Unable to start request");
1072 while (i < buffer.len) {
1076 if (i + 52 < buffer.len &&
1077 !prefixcmp(data + i, " pack-") &&
1078 !prefixcmp(data + i + 46, ".pack\n")) {
1079 get_sha1_hex(data + i + 6, sha1);
1085 while (data[i] != '\n')
1091 strbuf_release(&buffer);
1095 static void one_remote_object(const char *hex)
1097 unsigned char sha1[20];
1100 if (get_sha1_hex(hex, sha1) != 0)
1103 obj = lookup_object(sha1);
1105 obj = parse_object(sha1);
1107 /* Ignore remote objects that don't exist locally */
1111 obj->flags |= REMOTE;
1112 if (!object_list_contains(objects, obj))
1113 object_list_insert(obj, &objects);
1116 static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed)
1118 int *lock_flags = (int *)ctx->userData;
1121 if (!strcmp(ctx->name, DAV_CTX_LOCKENTRY)) {
1122 if ((*lock_flags & DAV_PROP_LOCKEX) &&
1123 (*lock_flags & DAV_PROP_LOCKWR)) {
1124 *lock_flags |= DAV_LOCK_OK;
1126 *lock_flags &= DAV_LOCK_OK;
1127 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_WRITE)) {
1128 *lock_flags |= DAV_PROP_LOCKWR;
1129 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_EXCLUSIVE)) {
1130 *lock_flags |= DAV_PROP_LOCKEX;
1135 static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
1137 struct remote_lock *lock = (struct remote_lock *)ctx->userData;
1139 if (tag_closed && ctx->cdata) {
1140 if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) {
1141 lock->owner = xmalloc(strlen(ctx->cdata) + 1);
1142 strcpy(lock->owner, ctx->cdata);
1143 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
1144 if (!prefixcmp(ctx->cdata, "Second-"))
1146 strtol(ctx->cdata + 7, NULL, 10);
1147 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
1148 lock->token = xmalloc(strlen(ctx->cdata) + 1);
1149 strcpy(lock->token, ctx->cdata);
1154 static void one_remote_ref(char *refname);
1157 xml_start_tag(void *userData, const char *name, const char **atts)
1159 struct xml_ctx *ctx = (struct xml_ctx *)userData;
1160 const char *c = strchr(name, ':');
1168 new_len = strlen(ctx->name) + strlen(c) + 2;
1170 if (new_len > ctx->len) {
1171 ctx->name = xrealloc(ctx->name, new_len);
1174 strcat(ctx->name, ".");
1175 strcat(ctx->name, c);
1180 ctx->userFunc(ctx, 0);
1184 xml_end_tag(void *userData, const char *name)
1186 struct xml_ctx *ctx = (struct xml_ctx *)userData;
1187 const char *c = strchr(name, ':');
1190 ctx->userFunc(ctx, 1);
1197 ep = ctx->name + strlen(ctx->name) - strlen(c) - 1;
1202 xml_cdata(void *userData, const XML_Char *s, int len)
1204 struct xml_ctx *ctx = (struct xml_ctx *)userData;
1206 ctx->cdata = xmemdupz(s, len);
1209 static struct remote_lock *lock_remote(const char *path, long timeout)
1211 struct active_request_slot *slot;
1212 struct slot_results results;
1213 struct buffer out_buffer = { STRBUF_INIT, 0 };
1214 struct strbuf in_buffer = STRBUF_INIT;
1217 char timeout_header[25];
1218 struct remote_lock *lock = NULL;
1219 struct curl_slist *dav_headers = NULL;
1222 url = xmalloc(strlen(remote->url) + strlen(path) + 1);
1223 sprintf(url, "%s%s", remote->url, path);
1225 /* Make sure leading directories exist for the remote ref */
1226 ep = strchr(url + strlen(remote->url) + 1, '/');
1228 char saved_character = ep[1];
1230 slot = get_active_slot();
1231 slot->results = &results;
1232 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
1233 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1234 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
1235 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1236 if (start_active_slot(slot)) {
1237 run_active_slot(slot);
1238 if (results.curl_result != CURLE_OK &&
1239 results.http_code != 405) {
1241 "Unable to create branch path %s\n",
1247 fprintf(stderr, "Unable to start MKCOL request\n");
1251 ep[1] = saved_character;
1252 ep = strchr(ep + 1, '/');
1255 strbuf_addf(&out_buffer.buf, LOCK_REQUEST, git_default_email);
1257 sprintf(timeout_header, "Timeout: Second-%ld", timeout);
1258 dav_headers = curl_slist_append(dav_headers, timeout_header);
1259 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1261 slot = get_active_slot();
1262 slot->results = &results;
1263 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1264 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1265 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1266 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1267 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1268 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1269 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1270 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
1271 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1273 lock = xcalloc(1, sizeof(*lock));
1276 if (start_active_slot(slot)) {
1277 run_active_slot(slot);
1278 if (results.curl_result == CURLE_OK) {
1279 XML_Parser parser = XML_ParserCreate(NULL);
1280 enum XML_Status result;
1281 ctx.name = xcalloc(10, 1);
1284 ctx.userFunc = handle_new_lock_ctx;
1285 ctx.userData = lock;
1286 XML_SetUserData(parser, &ctx);
1287 XML_SetElementHandler(parser, xml_start_tag,
1289 XML_SetCharacterDataHandler(parser, xml_cdata);
1290 result = XML_Parse(parser, in_buffer.buf,
1293 if (result != XML_STATUS_OK) {
1294 fprintf(stderr, "XML error: %s\n",
1296 XML_GetErrorCode(parser)));
1299 XML_ParserFree(parser);
1302 fprintf(stderr, "Unable to start LOCK request\n");
1305 curl_slist_free_all(dav_headers);
1306 strbuf_release(&out_buffer.buf);
1307 strbuf_release(&in_buffer);
1309 if (lock->token == NULL || lock->timeout <= 0) {
1317 lock->start_time = time(NULL);
1318 lock->next = remote->locks;
1319 remote->locks = lock;
1325 static int unlock_remote(struct remote_lock *lock)
1327 struct active_request_slot *slot;
1328 struct slot_results results;
1329 struct remote_lock *prev = remote->locks;
1330 struct curl_slist *dav_headers;
1333 dav_headers = get_dav_token_headers(lock, DAV_HEADER_LOCK);
1335 slot = get_active_slot();
1336 slot->results = &results;
1337 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1338 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1339 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_UNLOCK);
1340 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1342 if (start_active_slot(slot)) {
1343 run_active_slot(slot);
1344 if (results.curl_result == CURLE_OK)
1347 fprintf(stderr, "UNLOCK HTTP error %ld\n",
1350 fprintf(stderr, "Unable to start UNLOCK request\n");
1353 curl_slist_free_all(dav_headers);
1355 if (remote->locks == lock) {
1356 remote->locks = lock->next;
1358 while (prev && prev->next != lock)
1361 prev->next = prev->next->next;
1372 static void remove_locks(void)
1374 struct remote_lock *lock = remote->locks;
1376 fprintf(stderr, "Removing remote locks...\n");
1378 unlock_remote(lock);
1383 static void remove_locks_on_signal(int signo)
1386 signal(signo, SIG_DFL);
1390 static void remote_ls(const char *path, int flags,
1391 void (*userFunc)(struct remote_ls_ctx *ls),
1394 static void process_ls_object(struct remote_ls_ctx *ls)
1396 unsigned int *parent = (unsigned int *)ls->userData;
1397 char *path = ls->dentry_name;
1400 if (!strcmp(ls->path, ls->dentry_name) && (ls->flags & IS_DIR)) {
1401 remote_dir_exists[*parent] = 1;
1405 if (strlen(path) != 49)
1408 obj_hex = xmalloc(strlen(path));
1409 /* NB: path is not null-terminated, can not use strlcpy here */
1410 memcpy(obj_hex, path, 2);
1411 strcpy(obj_hex + 2, path + 3);
1412 one_remote_object(obj_hex);
1416 static void process_ls_ref(struct remote_ls_ctx *ls)
1418 if (!strcmp(ls->path, ls->dentry_name) && (ls->dentry_flags & IS_DIR)) {
1419 fprintf(stderr, " %s\n", ls->dentry_name);
1423 if (!(ls->dentry_flags & IS_DIR))
1424 one_remote_ref(ls->dentry_name);
1427 static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
1429 struct remote_ls_ctx *ls = (struct remote_ls_ctx *)ctx->userData;
1432 if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) {
1433 if (ls->dentry_flags & IS_DIR) {
1434 if (ls->flags & PROCESS_DIRS) {
1437 if (strcmp(ls->dentry_name, ls->path) &&
1438 ls->flags & RECURSIVE) {
1439 remote_ls(ls->dentry_name,
1444 } else if (ls->flags & PROCESS_FILES) {
1447 } else if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
1448 char *path = ctx->cdata;
1449 if (*ctx->cdata == 'h') {
1450 path = strstr(path, "//");
1452 path = strchr(path+2, '/');
1456 path += remote->path_len;
1457 ls->dentry_name = xstrdup(path);
1459 } else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
1460 ls->dentry_flags |= IS_DIR;
1462 } else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
1463 free(ls->dentry_name);
1464 ls->dentry_name = NULL;
1465 ls->dentry_flags = 0;
1470 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1471 * should _only_ heed the information from that file, instead of trying to
1472 * determine the refs from the remote file system (badly: it does not even
1473 * know about packed-refs).
1475 static void remote_ls(const char *path, int flags,
1476 void (*userFunc)(struct remote_ls_ctx *ls),
1479 char *url = xmalloc(strlen(remote->url) + strlen(path) + 1);
1480 struct active_request_slot *slot;
1481 struct slot_results results;
1482 struct strbuf in_buffer = STRBUF_INIT;
1483 struct buffer out_buffer = { STRBUF_INIT, 0 };
1484 struct curl_slist *dav_headers = NULL;
1486 struct remote_ls_ctx ls;
1489 ls.path = xstrdup(path);
1490 ls.dentry_name = NULL;
1491 ls.dentry_flags = 0;
1492 ls.userData = userData;
1493 ls.userFunc = userFunc;
1495 sprintf(url, "%s%s", remote->url, path);
1497 strbuf_addf(&out_buffer.buf, PROPFIND_ALL_REQUEST);
1499 dav_headers = curl_slist_append(dav_headers, "Depth: 1");
1500 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1502 slot = get_active_slot();
1503 slot->results = &results;
1504 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1505 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1506 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1507 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1508 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1509 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1510 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1511 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1512 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1514 if (start_active_slot(slot)) {
1515 run_active_slot(slot);
1516 if (results.curl_result == CURLE_OK) {
1517 XML_Parser parser = XML_ParserCreate(NULL);
1518 enum XML_Status result;
1519 ctx.name = xcalloc(10, 1);
1522 ctx.userFunc = handle_remote_ls_ctx;
1524 XML_SetUserData(parser, &ctx);
1525 XML_SetElementHandler(parser, xml_start_tag,
1527 XML_SetCharacterDataHandler(parser, xml_cdata);
1528 result = XML_Parse(parser, in_buffer.buf,
1532 if (result != XML_STATUS_OK) {
1533 fprintf(stderr, "XML error: %s\n",
1535 XML_GetErrorCode(parser)));
1537 XML_ParserFree(parser);
1540 fprintf(stderr, "Unable to start PROPFIND request\n");
1545 strbuf_release(&out_buffer.buf);
1546 strbuf_release(&in_buffer);
1547 curl_slist_free_all(dav_headers);
1550 static void get_remote_object_list(unsigned char parent)
1552 char path[] = "objects/XX/";
1553 static const char hex[] = "0123456789abcdef";
1554 unsigned int val = parent;
1556 path[8] = hex[val >> 4];
1557 path[9] = hex[val & 0xf];
1558 remote_dir_exists[val] = 0;
1559 remote_ls(path, (PROCESS_FILES | PROCESS_DIRS),
1560 process_ls_object, &val);
1563 static int locking_available(void)
1565 struct active_request_slot *slot;
1566 struct slot_results results;
1567 struct strbuf in_buffer = STRBUF_INIT;
1568 struct buffer out_buffer = { STRBUF_INIT, 0 };
1569 struct curl_slist *dav_headers = NULL;
1573 strbuf_addf(&out_buffer.buf, PROPFIND_SUPPORTEDLOCK_REQUEST, remote->url);
1575 dav_headers = curl_slist_append(dav_headers, "Depth: 0");
1576 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1578 slot = get_active_slot();
1579 slot->results = &results;
1580 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1581 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1582 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1583 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1584 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1585 curl_easy_setopt(slot->curl, CURLOPT_URL, remote->url);
1586 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1587 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1588 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1590 if (start_active_slot(slot)) {
1591 run_active_slot(slot);
1592 if (results.curl_result == CURLE_OK) {
1593 XML_Parser parser = XML_ParserCreate(NULL);
1594 enum XML_Status result;
1595 ctx.name = xcalloc(10, 1);
1598 ctx.userFunc = handle_lockprop_ctx;
1599 ctx.userData = &lock_flags;
1600 XML_SetUserData(parser, &ctx);
1601 XML_SetElementHandler(parser, xml_start_tag,
1603 result = XML_Parse(parser, in_buffer.buf,
1607 if (result != XML_STATUS_OK) {
1608 fprintf(stderr, "XML error: %s\n",
1610 XML_GetErrorCode(parser)));
1613 XML_ParserFree(parser);
1615 error("Error: no DAV locking support on %s",
1619 error("Cannot access URL %s, return code %d",
1620 remote->url, results.curl_result);
1624 error("Unable to start PROPFIND request on %s", remote->url);
1627 strbuf_release(&out_buffer.buf);
1628 strbuf_release(&in_buffer);
1629 curl_slist_free_all(dav_headers);
1634 static struct object_list **add_one_object(struct object *obj, struct object_list **p)
1636 struct object_list *entry = xmalloc(sizeof(struct object_list));
1640 return &entry->next;
1643 static struct object_list **process_blob(struct blob *blob,
1644 struct object_list **p,
1645 struct name_path *path,
1648 struct object *obj = &blob->object;
1650 obj->flags |= LOCAL;
1652 if (obj->flags & (UNINTERESTING | SEEN))
1656 return add_one_object(obj, p);
1659 static struct object_list **process_tree(struct tree *tree,
1660 struct object_list **p,
1661 struct name_path *path,
1664 struct object *obj = &tree->object;
1665 struct tree_desc desc;
1666 struct name_entry entry;
1667 struct name_path me;
1669 obj->flags |= LOCAL;
1671 if (obj->flags & (UNINTERESTING | SEEN))
1673 if (parse_tree(tree) < 0)
1674 die("bad tree object %s", sha1_to_hex(obj->sha1));
1677 name = xstrdup(name);
1678 p = add_one_object(obj, p);
1681 me.elem_len = strlen(name);
1683 init_tree_desc(&desc, tree->buffer, tree->size);
1685 while (tree_entry(&desc, &entry))
1686 switch (object_type(entry.mode)) {
1688 p = process_tree(lookup_tree(entry.sha1), p, &me, name);
1691 p = process_blob(lookup_blob(entry.sha1), p, &me, name);
1694 /* Subproject commit - not in this repository */
1699 tree->buffer = NULL;
1703 static int get_delta(struct rev_info *revs, struct remote_lock *lock)
1706 struct commit *commit;
1707 struct object_list **p = &objects;
1710 while ((commit = get_revision(revs)) != NULL) {
1711 p = process_tree(commit->tree, p, NULL, "");
1712 commit->object.flags |= LOCAL;
1713 if (!(commit->object.flags & UNINTERESTING))
1714 count += add_send_request(&commit->object, lock);
1717 for (i = 0; i < revs->pending.nr; i++) {
1718 struct object_array_entry *entry = revs->pending.objects + i;
1719 struct object *obj = entry->item;
1720 const char *name = entry->name;
1722 if (obj->flags & (UNINTERESTING | SEEN))
1724 if (obj->type == OBJ_TAG) {
1726 p = add_one_object(obj, p);
1729 if (obj->type == OBJ_TREE) {
1730 p = process_tree((struct tree *)obj, p, NULL, name);
1733 if (obj->type == OBJ_BLOB) {
1734 p = process_blob((struct blob *)obj, p, NULL, name);
1737 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
1741 if (!(objects->item->flags & UNINTERESTING))
1742 count += add_send_request(objects->item, lock);
1743 objects = objects->next;
1749 static int update_remote(unsigned char *sha1, struct remote_lock *lock)
1751 struct active_request_slot *slot;
1752 struct slot_results results;
1753 struct buffer out_buffer = { STRBUF_INIT, 0 };
1754 struct curl_slist *dav_headers;
1756 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1758 strbuf_addf(&out_buffer.buf, "%s\n", sha1_to_hex(sha1));
1760 slot = get_active_slot();
1761 slot->results = &results;
1762 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1763 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1764 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1765 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1766 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1767 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1768 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1769 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1770 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1772 if (start_active_slot(slot)) {
1773 run_active_slot(slot);
1774 strbuf_release(&out_buffer.buf);
1775 if (results.curl_result != CURLE_OK) {
1777 "PUT error: curl result=%d, HTTP code=%ld\n",
1778 results.curl_result, results.http_code);
1779 /* We should attempt recovery? */
1783 strbuf_release(&out_buffer.buf);
1784 fprintf(stderr, "Unable to start PUT request\n");
1791 static struct ref *local_refs, **local_tail;
1792 static struct ref *remote_refs, **remote_tail;
1794 static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
1797 int len = strlen(refname) + 1;
1798 ref = xcalloc(1, sizeof(*ref) + len);
1799 hashcpy(ref->new_sha1, sha1);
1800 memcpy(ref->name, refname, len);
1802 local_tail = &ref->next;
1806 static void one_remote_ref(char *refname)
1811 ref = alloc_ref(refname);
1813 if (http_fetch_ref(remote->url, ref) != 0) {
1815 "Unable to fetch ref %s from %s\n",
1816 refname, remote->url);
1822 * Fetch a copy of the object if it doesn't exist locally - it
1823 * may be required for updating server info later.
1825 if (remote->can_update_info_refs && !has_sha1_file(ref->old_sha1)) {
1826 obj = lookup_unknown_object(ref->old_sha1);
1828 fprintf(stderr, " fetch %s for %s\n",
1829 sha1_to_hex(ref->old_sha1), refname);
1830 add_fetch_request(obj);
1835 remote_tail = &ref->next;
1838 static void get_local_heads(void)
1840 local_tail = &local_refs;
1841 for_each_ref(one_local_ref, NULL);
1844 static void get_dav_remote_heads(void)
1846 remote_tail = &remote_refs;
1847 remote_ls("refs/", (PROCESS_FILES | PROCESS_DIRS | RECURSIVE), process_ls_ref, NULL);
1850 static int is_zero_sha1(const unsigned char *sha1)
1854 for (i = 0; i < 20; i++) {
1861 static void unmark_and_free(struct commit_list *list, unsigned int mark)
1864 struct commit_list *temp = list;
1865 temp->item->object.flags &= ~mark;
1871 static int ref_newer(const unsigned char *new_sha1,
1872 const unsigned char *old_sha1)
1875 struct commit *old, *new;
1876 struct commit_list *list, *used;
1879 /* Both new and old must be commit-ish and new is descendant of
1880 * old. Otherwise we require --force.
1882 o = deref_tag(parse_object(old_sha1), NULL, 0);
1883 if (!o || o->type != OBJ_COMMIT)
1885 old = (struct commit *) o;
1887 o = deref_tag(parse_object(new_sha1), NULL, 0);
1888 if (!o || o->type != OBJ_COMMIT)
1890 new = (struct commit *) o;
1892 if (parse_commit(new) < 0)
1896 commit_list_insert(new, &list);
1898 new = pop_most_recent_commit(&list, TMP_MARK);
1899 commit_list_insert(new, &used);
1905 unmark_and_free(list, TMP_MARK);
1906 unmark_and_free(used, TMP_MARK);
1910 static void add_remote_info_ref(struct remote_ls_ctx *ls)
1912 struct strbuf *buf = (struct strbuf *)ls->userData;
1918 ref = alloc_ref(ls->dentry_name);
1920 if (http_fetch_ref(remote->url, ref) != 0) {
1922 "Unable to fetch ref %s from %s\n",
1923 ls->dentry_name, remote->url);
1929 o = parse_object(ref->old_sha1);
1932 "Unable to parse object %s for remote ref %s\n",
1933 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1939 len = strlen(ls->dentry_name) + 42;
1940 ref_info = xcalloc(len + 1, 1);
1941 sprintf(ref_info, "%s %s\n",
1942 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1943 fwrite_buffer(ref_info, 1, len, buf);
1946 if (o->type == OBJ_TAG) {
1947 o = deref_tag(o, ls->dentry_name, 0);
1949 len = strlen(ls->dentry_name) + 45;
1950 ref_info = xcalloc(len + 1, 1);
1951 sprintf(ref_info, "%s %s^{}\n",
1952 sha1_to_hex(o->sha1), ls->dentry_name);
1953 fwrite_buffer(ref_info, 1, len, buf);
1960 static void update_remote_info_refs(struct remote_lock *lock)
1962 struct buffer buffer = { STRBUF_INIT, 0 };
1963 struct active_request_slot *slot;
1964 struct slot_results results;
1965 struct curl_slist *dav_headers;
1967 remote_ls("refs/", (PROCESS_FILES | RECURSIVE),
1968 add_remote_info_ref, &buffer.buf);
1970 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1972 slot = get_active_slot();
1973 slot->results = &results;
1974 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &buffer);
1975 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, buffer.buf.len);
1976 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1977 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1978 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1979 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1980 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1981 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1982 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1984 if (start_active_slot(slot)) {
1985 run_active_slot(slot);
1986 if (results.curl_result != CURLE_OK) {
1988 "PUT error: curl result=%d, HTTP code=%ld\n",
1989 results.curl_result, results.http_code);
1993 strbuf_release(&buffer.buf);
1996 static int remote_exists(const char *path)
1998 char *url = xmalloc(strlen(remote->url) + strlen(path) + 1);
1999 struct active_request_slot *slot;
2000 struct slot_results results;
2003 sprintf(url, "%s%s", remote->url, path);
2005 slot = get_active_slot();
2006 slot->results = &results;
2007 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2008 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
2010 if (start_active_slot(slot)) {
2011 run_active_slot(slot);
2012 if (results.http_code == 404)
2014 else if (results.curl_result == CURLE_OK)
2017 fprintf(stderr, "HEAD HTTP error %ld\n", results.http_code);
2019 fprintf(stderr, "Unable to start HEAD request\n");
2026 static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
2029 struct strbuf buffer = STRBUF_INIT;
2030 struct active_request_slot *slot;
2031 struct slot_results results;
2033 url = xmalloc(strlen(remote->url) + strlen(path) + 1);
2034 sprintf(url, "%s%s", remote->url, path);
2036 slot = get_active_slot();
2037 slot->results = &results;
2038 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
2039 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
2040 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
2041 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2042 if (start_active_slot(slot)) {
2043 run_active_slot(slot);
2044 if (results.curl_result != CURLE_OK) {
2045 die("Couldn't get %s for remote symref\n%s",
2046 url, curl_errorstr);
2049 die("Unable to start remote symref request");
2057 if (buffer.len == 0)
2060 /* If it's a symref, set the refname; otherwise try for a sha1 */
2061 if (!prefixcmp((char *)buffer.buf, "ref: ")) {
2062 *symref = xmemdupz((char *)buffer.buf + 5, buffer.len - 6);
2064 get_sha1_hex(buffer.buf, sha1);
2067 strbuf_release(&buffer);
2070 static int verify_merge_base(unsigned char *head_sha1, unsigned char *branch_sha1)
2072 struct commit *head = lookup_commit(head_sha1);
2073 struct commit *branch = lookup_commit(branch_sha1);
2074 struct commit_list *merge_bases = get_merge_bases(head, branch, 1);
2076 return (merge_bases && !merge_bases->next && merge_bases->item == branch);
2079 static int delete_remote_branch(char *pattern, int force)
2081 struct ref *refs = remote_refs;
2082 struct ref *remote_ref = NULL;
2083 unsigned char head_sha1[20];
2084 char *symref = NULL;
2086 int patlen = strlen(pattern);
2088 struct active_request_slot *slot;
2089 struct slot_results results;
2092 /* Find the remote branch(es) matching the specified branch name */
2093 for (match = 0; refs; refs = refs->next) {
2094 char *name = refs->name;
2095 int namelen = strlen(name);
2096 if (namelen < patlen ||
2097 memcmp(name + namelen - patlen, pattern, patlen))
2099 if (namelen != patlen && name[namelen - patlen - 1] != '/')
2105 return error("No remote branch matches %s", pattern);
2107 return error("More than one remote branch matches %s",
2111 * Remote HEAD must be a symref (not exactly foolproof; a remote
2112 * symlink to a symref will look like a symref)
2114 fetch_symref("HEAD", &symref, head_sha1);
2116 return error("Remote HEAD is not a symref");
2118 /* Remote branch must not be the remote HEAD */
2119 for (i=0; symref && i<MAXDEPTH; i++) {
2120 if (!strcmp(remote_ref->name, symref))
2121 return error("Remote branch %s is the current HEAD",
2123 fetch_symref(symref, &symref, head_sha1);
2126 /* Run extra sanity checks if delete is not forced */
2128 /* Remote HEAD must resolve to a known object */
2130 return error("Remote HEAD symrefs too deep");
2131 if (is_zero_sha1(head_sha1))
2132 return error("Unable to resolve remote HEAD");
2133 if (!has_sha1_file(head_sha1))
2134 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1));
2136 /* Remote branch must resolve to a known object */
2137 if (is_zero_sha1(remote_ref->old_sha1))
2138 return error("Unable to resolve remote branch %s",
2140 if (!has_sha1_file(remote_ref->old_sha1))
2141 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));
2143 /* Remote branch must be an ancestor of remote HEAD */
2144 if (!verify_merge_base(head_sha1, remote_ref->old_sha1)) {
2145 return error("The branch '%s' is not an ancestor "
2146 "of your current HEAD.\n"
2147 "If you are sure you want to delete it,"
2148 " run:\n\t'git http-push -D %s %s'",
2149 remote_ref->name, remote->url, pattern);
2153 /* Send delete request */
2154 fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name);
2157 url = xmalloc(strlen(remote->url) + strlen(remote_ref->name) + 1);
2158 sprintf(url, "%s%s", remote->url, remote_ref->name);
2159 slot = get_active_slot();
2160 slot->results = &results;
2161 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
2162 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
2163 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2164 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_DELETE);
2165 if (start_active_slot(slot)) {
2166 run_active_slot(slot);
2168 if (results.curl_result != CURLE_OK)
2169 return error("DELETE request failed (%d/%ld)\n",
2170 results.curl_result, results.http_code);
2173 return error("Unable to start DELETE request");
2179 int main(int argc, char **argv)
2181 struct transfer_request *request;
2182 struct transfer_request *next_request;
2184 char **refspec = NULL;
2185 struct remote_lock *ref_lock = NULL;
2186 struct remote_lock *info_ref_lock = NULL;
2187 struct rev_info revs;
2188 int delete_branch = 0;
2189 int force_delete = 0;
2190 int objects_to_send;
2195 char *rewritten_url = NULL;
2197 setup_git_directory();
2199 remote = xcalloc(sizeof(*remote), 1);
2202 for (i = 1; i < argc; i++, argv++) {
2206 if (!strcmp(arg, "--all")) {
2207 push_all = MATCH_REFS_ALL;
2210 if (!strcmp(arg, "--force")) {
2214 if (!strcmp(arg, "--dry-run")) {
2218 if (!strcmp(arg, "--verbose")) {
2222 if (!strcmp(arg, "-d")) {
2226 if (!strcmp(arg, "-D")) {
2233 char *path = strstr(arg, "//");
2235 remote->path_len = strlen(arg);
2237 remote->path = strchr(path+2, '/');
2239 remote->path_len = strlen(remote->path);
2244 nr_refspec = argc - i;
2248 #ifndef USE_CURL_MULTI
2249 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
2253 usage(http_push_usage);
2255 if (delete_branch && nr_refspec != 1)
2256 die("You must specify only one branch name when deleting a remote branch");
2258 memset(remote_dir_exists, -1, 256);
2262 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
2264 if (remote->url && remote->url[strlen(remote->url)-1] != '/') {
2265 rewritten_url = xmalloc(strlen(remote->url)+2);
2266 strcpy(rewritten_url, remote->url);
2267 strcat(rewritten_url, "/");
2268 remote->path = rewritten_url + (remote->path - remote->url);
2270 remote->url = rewritten_url;
2273 /* Verify DAV compliance/lock support */
2274 if (!locking_available()) {
2279 signal(SIGINT, remove_locks_on_signal);
2280 signal(SIGHUP, remove_locks_on_signal);
2281 signal(SIGQUIT, remove_locks_on_signal);
2282 signal(SIGTERM, remove_locks_on_signal);
2284 /* Check whether the remote has server info files */
2285 remote->can_update_info_refs = 0;
2286 remote->has_info_refs = remote_exists("info/refs");
2287 remote->has_info_packs = remote_exists("objects/info/packs");
2288 if (remote->has_info_refs) {
2289 info_ref_lock = lock_remote("info/refs", LOCK_TIME);
2291 remote->can_update_info_refs = 1;
2293 fprintf(stderr, "Error: cannot lock existing info/refs\n");
2298 if (remote->has_info_packs)
2301 /* Get a list of all local and remote heads to validate refspecs */
2303 fprintf(stderr, "Fetching remote heads...\n");
2304 get_dav_remote_heads();
2306 /* Remove a remote branch if -d or -D was specified */
2307 if (delete_branch) {
2308 if (delete_remote_branch(refspec[0], force_delete) == -1)
2309 fprintf(stderr, "Unable to delete remote branch %s\n",
2316 remote_tail = &remote_refs;
2317 if (match_refs(local_refs, remote_refs, &remote_tail,
2318 nr_refspec, (const char **) refspec, push_all)) {
2323 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
2329 for (ref = remote_refs; ref; ref = ref->next) {
2330 char old_hex[60], *new_hex;
2331 const char *commit_argv[4];
2333 char *new_sha1_hex, *old_sha1_hex;
2338 if (is_zero_sha1(ref->peer_ref->new_sha1)) {
2339 if (delete_remote_branch(ref->name, 1) == -1) {
2340 error("Could not remove %s", ref->name);
2347 if (!hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
2348 if (push_verbosely || 1)
2349 fprintf(stderr, "'%s': up-to-date\n", ref->name);
2354 !is_zero_sha1(ref->old_sha1) &&
2356 if (!has_sha1_file(ref->old_sha1) ||
2357 !ref_newer(ref->peer_ref->new_sha1,
2360 * We do not have the remote ref, or
2361 * we know that the remote ref is not
2362 * an ancestor of what we are trying to
2363 * push. Either way this can be losing
2364 * commits at the remote end and likely
2365 * we were not up to date to begin with.
2367 error("remote '%s' is not an ancestor of\n"
2369 "Maybe you are not up-to-date and "
2370 "need to pull first?",
2372 ref->peer_ref->name);
2377 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
2379 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
2380 new_hex = sha1_to_hex(ref->new_sha1);
2382 fprintf(stderr, "updating '%s'", ref->name);
2383 if (strcmp(ref->name, ref->peer_ref->name))
2384 fprintf(stderr, " using '%s'", ref->peer_ref->name);
2385 fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex);
2389 /* Lock remote branch ref */
2390 ref_lock = lock_remote(ref->name, LOCK_TIME);
2391 if (ref_lock == NULL) {
2392 fprintf(stderr, "Unable to lock remote branch %s\n",
2398 /* Set up revision info for this refspec */
2400 new_sha1_hex = xstrdup(sha1_to_hex(ref->new_sha1));
2401 old_sha1_hex = NULL;
2402 commit_argv[1] = "--objects";
2403 commit_argv[2] = new_sha1_hex;
2404 if (!push_all && !is_zero_sha1(ref->old_sha1)) {
2405 old_sha1_hex = xmalloc(42);
2406 sprintf(old_sha1_hex, "^%s",
2407 sha1_to_hex(ref->old_sha1));
2408 commit_argv[3] = old_sha1_hex;
2411 init_revisions(&revs, setup_git_directory());
2412 setup_revisions(commit_argc, commit_argv, &revs, NULL);
2413 revs.edge_hint = 0; /* just in case */
2417 commit_argv[1] = NULL;
2420 /* Generate a list of objects that need to be pushed */
2422 if (prepare_revision_walk(&revs))
2423 die("revision walk setup failed");
2424 mark_edges_uninteresting(revs.commits, &revs, NULL);
2425 objects_to_send = get_delta(&revs, ref_lock);
2426 finish_all_active_slots();
2428 /* Push missing objects to remote, this would be a
2429 convenient time to pack them first if appropriate. */
2431 if (objects_to_send)
2432 fprintf(stderr, " sending %d objects\n",
2434 #ifdef USE_CURL_MULTI
2435 fill_active_slots();
2436 add_fill_function(NULL, fill_active_slot);
2439 finish_all_active_slots();
2440 #ifdef USE_CURL_MULTI
2441 fill_active_slots();
2443 } while (request_queue_head && !aborted);
2445 /* Update the remote branch if all went well */
2446 if (aborted || !update_remote(ref->new_sha1, ref_lock))
2450 fprintf(stderr, " done\n");
2451 unlock_remote(ref_lock);
2455 /* Update remote server info if appropriate */
2456 if (remote->has_info_refs && new_refs) {
2457 if (info_ref_lock && remote->can_update_info_refs) {
2458 fprintf(stderr, "Updating remote server info\n");
2460 update_remote_info_refs(info_ref_lock);
2462 fprintf(stderr, "Unable to update server info\n");
2467 free(rewritten_url);
2469 unlock_remote(info_ref_lock);
2472 curl_slist_free_all(no_pragma_header);
2476 request = request_queue_head;
2477 while (request != NULL) {
2478 next_request = request->next;
2479 release_request(request);
2480 request = next_request;