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 static void finish_request(struct transfer_request *request);
181 static void release_request(struct transfer_request *request);
183 static void process_response(void *callback_data)
185 struct transfer_request *request =
186 (struct transfer_request *)callback_data;
188 finish_request(request);
191 #ifdef USE_CURL_MULTI
192 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
195 unsigned char expn[4096];
196 size_t size = eltsize * nmemb;
198 struct transfer_request *request = (struct transfer_request *)data;
200 ssize_t retval = xwrite(request->local_fileno,
201 (char *) ptr + posn, size - posn);
205 } while (posn < size);
207 request->stream.avail_in = size;
208 request->stream.next_in = ptr;
210 request->stream.next_out = expn;
211 request->stream.avail_out = sizeof(expn);
212 request->zret = inflate(&request->stream, Z_SYNC_FLUSH);
213 git_SHA1_Update(&request->c, expn,
214 sizeof(expn) - request->stream.avail_out);
215 } while (request->stream.avail_in && request->zret == Z_OK);
220 static void start_fetch_loose(struct transfer_request *request)
222 char *hex = sha1_to_hex(request->obj->sha1);
224 char prevfile[PATH_MAX];
228 unsigned char prev_buf[PREV_BUF_SIZE];
229 ssize_t prev_read = 0;
231 char range[RANGE_HEADER_SIZE];
232 struct curl_slist *range_header = NULL;
233 struct active_request_slot *slot;
235 filename = sha1_file_name(request->obj->sha1);
236 snprintf(request->filename, sizeof(request->filename), "%s", filename);
237 snprintf(request->tmpfile, sizeof(request->tmpfile),
238 "%s.temp", filename);
240 snprintf(prevfile, sizeof(prevfile), "%s.prev", request->filename);
242 rename(request->tmpfile, prevfile);
243 unlink(request->tmpfile);
245 if (request->local_fileno != -1)
246 error("fd leakage in start: %d", request->local_fileno);
247 request->local_fileno = open(request->tmpfile,
248 O_WRONLY | O_CREAT | O_EXCL, 0666);
249 /* This could have failed due to the "lazy directory creation";
250 * try to mkdir the last path component.
252 if (request->local_fileno < 0 && errno == ENOENT) {
253 char *dir = strrchr(request->tmpfile, '/');
256 mkdir(request->tmpfile, 0777);
259 request->local_fileno = open(request->tmpfile,
260 O_WRONLY | O_CREAT | O_EXCL, 0666);
263 if (request->local_fileno < 0) {
264 request->state = ABORTED;
265 error("Couldn't create temporary file %s for %s: %s",
266 request->tmpfile, request->filename, strerror(errno));
270 memset(&request->stream, 0, sizeof(request->stream));
272 inflateInit(&request->stream);
274 git_SHA1_Init(&request->c);
276 url = xmalloc(strlen(remote->url) + 50);
277 request->url = xmalloc(strlen(remote->url) + 50);
278 strcpy(url, remote->url);
279 posn = url + strlen(remote->url);
280 strcpy(posn, "objects/");
282 memcpy(posn, hex, 2);
285 strcpy(posn, hex + 2);
286 strcpy(request->url, url);
288 /* If a previous temp file is present, process what was already
290 prevlocal = open(prevfile, O_RDONLY);
291 if (prevlocal != -1) {
293 prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE);
295 if (fwrite_sha1_file(prev_buf,
298 request) == prev_read) {
299 prev_posn += prev_read;
304 } while (prev_read > 0);
309 /* Reset inflate/SHA1 if there was an error reading the previous temp
310 file; also rewind to the beginning of the local file. */
311 if (prev_read == -1) {
312 memset(&request->stream, 0, sizeof(request->stream));
313 inflateInit(&request->stream);
314 git_SHA1_Init(&request->c);
317 lseek(request->local_fileno, 0, SEEK_SET);
318 ftruncate(request->local_fileno, 0);
322 slot = get_active_slot();
323 slot->callback_func = process_response;
324 slot->callback_data = request;
325 request->slot = slot;
327 curl_easy_setopt(slot->curl, CURLOPT_FILE, request);
328 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
329 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
330 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
331 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
333 /* If we have successfully processed data from a previous fetch
334 attempt, only fetch the data we don't already have. */
338 "Resuming fetch of object %s at byte %ld\n",
340 sprintf(range, "Range: bytes=%ld-", prev_posn);
341 range_header = curl_slist_append(range_header, range);
342 curl_easy_setopt(slot->curl,
343 CURLOPT_HTTPHEADER, range_header);
346 /* Try to get the request started, abort the request on error */
347 request->state = RUN_FETCH_LOOSE;
348 if (!start_active_slot(slot)) {
349 fprintf(stderr, "Unable to start GET request\n");
350 remote->can_update_info_refs = 0;
351 release_request(request);
355 static void start_mkcol(struct transfer_request *request)
357 char *hex = sha1_to_hex(request->obj->sha1);
358 struct active_request_slot *slot;
361 request->url = xmalloc(strlen(remote->url) + 13);
362 strcpy(request->url, remote->url);
363 posn = request->url + strlen(remote->url);
364 strcpy(posn, "objects/");
366 memcpy(posn, hex, 2);
370 slot = get_active_slot();
371 slot->callback_func = process_response;
372 slot->callback_data = request;
373 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
374 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
375 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
376 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
377 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
379 if (start_active_slot(slot)) {
380 request->slot = slot;
381 request->state = RUN_MKCOL;
383 request->state = ABORTED;
390 static void start_fetch_packed(struct transfer_request *request)
393 struct packed_git *target;
397 char range[RANGE_HEADER_SIZE];
398 struct curl_slist *range_header = NULL;
400 struct transfer_request *check_request = request_queue_head;
401 struct active_request_slot *slot;
403 target = find_sha1_pack(request->obj->sha1, remote->packs);
405 fprintf(stderr, "Unable to fetch %s, will not be able to update server info refs\n", sha1_to_hex(request->obj->sha1));
406 remote->can_update_info_refs = 0;
407 release_request(request);
411 fprintf(stderr, "Fetching pack %s\n", sha1_to_hex(target->sha1));
412 fprintf(stderr, " which contains %s\n", sha1_to_hex(request->obj->sha1));
414 filename = sha1_pack_name(target->sha1);
415 snprintf(request->filename, sizeof(request->filename), "%s", filename);
416 snprintf(request->tmpfile, sizeof(request->tmpfile),
417 "%s.temp", filename);
419 url = xmalloc(strlen(remote->url) + 64);
420 sprintf(url, "%sobjects/pack/pack-%s.pack",
421 remote->url, sha1_to_hex(target->sha1));
423 /* Make sure there isn't another open request for this pack */
424 while (check_request) {
425 if (check_request->state == RUN_FETCH_PACKED &&
426 !strcmp(check_request->url, url)) {
428 release_request(request);
431 check_request = check_request->next;
434 packfile = fopen(request->tmpfile, "a");
436 fprintf(stderr, "Unable to open local file %s for pack",
438 remote->can_update_info_refs = 0;
443 slot = get_active_slot();
444 slot->callback_func = process_response;
445 slot->callback_data = request;
446 request->slot = slot;
447 request->local_stream = packfile;
448 request->userData = target;
451 curl_easy_setopt(slot->curl, CURLOPT_FILE, packfile);
452 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
453 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
454 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
455 slot->local = packfile;
457 /* If there is data present from a previous transfer attempt,
458 resume where it left off */
459 prev_posn = ftell(packfile);
463 "Resuming fetch of pack %s at byte %ld\n",
464 sha1_to_hex(target->sha1), prev_posn);
465 sprintf(range, "Range: bytes=%ld-", prev_posn);
466 range_header = curl_slist_append(range_header, range);
467 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
470 /* Try to get the request started, abort the request on error */
471 request->state = RUN_FETCH_PACKED;
472 if (!start_active_slot(slot)) {
473 fprintf(stderr, "Unable to start GET request\n");
474 remote->can_update_info_refs = 0;
475 release_request(request);
479 static void start_put(struct transfer_request *request)
481 char *hex = sha1_to_hex(request->obj->sha1);
482 struct active_request_slot *slot;
484 enum object_type type;
492 unpacked = read_sha1_file(request->obj->sha1, &type, &len);
493 hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
496 memset(&stream, 0, sizeof(stream));
497 deflateInit(&stream, zlib_compression_level);
498 size = deflateBound(&stream, len + hdrlen);
499 strbuf_init(&request->buffer.buf, size);
500 request->buffer.posn = 0;
503 stream.next_out = (unsigned char *)request->buffer.buf.buf;
504 stream.avail_out = size;
507 stream.next_in = (void *)hdr;
508 stream.avail_in = hdrlen;
509 while (deflate(&stream, 0) == Z_OK)
512 /* Then the data itself.. */
513 stream.next_in = unpacked;
514 stream.avail_in = len;
515 while (deflate(&stream, Z_FINISH) == Z_OK)
520 request->buffer.buf.len = stream.total_out;
522 request->url = xmalloc(strlen(remote->url) +
523 strlen(request->lock->token) + 51);
524 strcpy(request->url, remote->url);
525 posn = request->url + strlen(remote->url);
526 strcpy(posn, "objects/");
528 memcpy(posn, hex, 2);
531 strcpy(posn, hex + 2);
532 request->dest = xmalloc(strlen(request->url) + 14);
533 sprintf(request->dest, "Destination: %s", request->url);
536 strcpy(posn, request->lock->token);
538 slot = get_active_slot();
539 slot->callback_func = process_response;
540 slot->callback_data = request;
541 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &request->buffer);
542 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, request->buffer.buf.len);
543 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
544 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
545 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
546 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
547 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
548 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
549 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
551 if (start_active_slot(slot)) {
552 request->slot = slot;
553 request->state = RUN_PUT;
555 request->state = ABORTED;
561 static void start_move(struct transfer_request *request)
563 struct active_request_slot *slot;
564 struct curl_slist *dav_headers = NULL;
566 slot = get_active_slot();
567 slot->callback_func = process_response;
568 slot->callback_data = request;
569 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
570 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MOVE);
571 dav_headers = curl_slist_append(dav_headers, request->dest);
572 dav_headers = curl_slist_append(dav_headers, "Overwrite: T");
573 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
574 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
575 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
577 if (start_active_slot(slot)) {
578 request->slot = slot;
579 request->state = RUN_MOVE;
581 request->state = ABORTED;
587 static int refresh_lock(struct remote_lock *lock)
589 struct active_request_slot *slot;
590 struct slot_results results;
592 char timeout_header[25];
593 struct curl_slist *dav_headers = NULL;
596 lock->refreshing = 1;
598 if_header = xmalloc(strlen(lock->token) + 25);
599 sprintf(if_header, "If: (<%s>)", lock->token);
600 sprintf(timeout_header, "Timeout: Second-%ld", lock->timeout);
601 dav_headers = curl_slist_append(dav_headers, if_header);
602 dav_headers = curl_slist_append(dav_headers, timeout_header);
604 slot = get_active_slot();
605 slot->results = &results;
606 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
607 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
608 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
609 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
610 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
612 if (start_active_slot(slot)) {
613 run_active_slot(slot);
614 if (results.curl_result != CURLE_OK) {
615 fprintf(stderr, "LOCK HTTP error %ld\n",
618 lock->start_time = time(NULL);
623 lock->refreshing = 0;
624 curl_slist_free_all(dav_headers);
630 static void check_locks(void)
632 struct remote_lock *lock = remote->locks;
633 time_t current_time = time(NULL);
637 time_remaining = lock->start_time + lock->timeout -
639 if (!lock->refreshing && time_remaining < LOCK_REFRESH) {
640 if (!refresh_lock(lock)) {
642 "Unable to refresh lock for %s\n",
652 static void release_request(struct transfer_request *request)
654 struct transfer_request *entry = request_queue_head;
656 if (request == request_queue_head) {
657 request_queue_head = request->next;
659 while (entry->next != NULL && entry->next != request)
661 if (entry->next == request)
662 entry->next = entry->next->next;
665 if (request->local_fileno != -1)
666 close(request->local_fileno);
667 if (request->local_stream)
668 fclose(request->local_stream);
673 static void finish_request(struct transfer_request *request)
676 struct packed_git *target;
677 struct packed_git **lst;
679 request->curl_result = request->slot->curl_result;
680 request->http_code = request->slot->http_code;
681 request->slot = NULL;
683 /* Keep locks active */
686 if (request->headers != NULL)
687 curl_slist_free_all(request->headers);
689 /* URL is reused for MOVE after PUT */
690 if (request->state != RUN_PUT) {
695 if (request->state == RUN_MKCOL) {
696 if (request->curl_result == CURLE_OK ||
697 request->http_code == 405) {
698 remote_dir_exists[request->obj->sha1[0]] = 1;
701 fprintf(stderr, "MKCOL %s failed, aborting (%d/%ld)\n",
702 sha1_to_hex(request->obj->sha1),
703 request->curl_result, request->http_code);
704 request->state = ABORTED;
707 } else if (request->state == RUN_PUT) {
708 if (request->curl_result == CURLE_OK) {
711 fprintf(stderr, "PUT %s failed, aborting (%d/%ld)\n",
712 sha1_to_hex(request->obj->sha1),
713 request->curl_result, request->http_code);
714 request->state = ABORTED;
717 } else if (request->state == RUN_MOVE) {
718 if (request->curl_result == CURLE_OK) {
720 fprintf(stderr, " sent %s\n",
721 sha1_to_hex(request->obj->sha1));
722 request->obj->flags |= REMOTE;
723 release_request(request);
725 fprintf(stderr, "MOVE %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_FETCH_LOOSE) {
732 fchmod(request->local_fileno, 0444);
733 close(request->local_fileno); request->local_fileno = -1;
735 if (request->curl_result != CURLE_OK &&
736 request->http_code != 416) {
737 if (stat(request->tmpfile, &st) == 0) {
739 unlink(request->tmpfile);
742 if (request->http_code == 416)
743 fprintf(stderr, "Warning: requested range invalid; we may already have all the data.\n");
745 inflateEnd(&request->stream);
746 git_SHA1_Final(request->real_sha1, &request->c);
747 if (request->zret != Z_STREAM_END) {
748 unlink(request->tmpfile);
749 } else if (hashcmp(request->obj->sha1, request->real_sha1)) {
750 unlink(request->tmpfile);
756 if (request->rename == 0) {
757 request->obj->flags |= (LOCAL | REMOTE);
762 /* Try fetching packed if necessary */
763 if (request->obj->flags & LOCAL)
764 release_request(request);
766 start_fetch_packed(request);
768 } else if (request->state == RUN_FETCH_PACKED) {
769 if (request->curl_result != CURLE_OK) {
770 fprintf(stderr, "Unable to get pack file %s\n%s",
771 request->url, curl_errorstr);
772 remote->can_update_info_refs = 0;
774 off_t pack_size = ftell(request->local_stream);
776 fclose(request->local_stream);
777 request->local_stream = NULL;
778 if (!move_temp_to_file(request->tmpfile,
779 request->filename)) {
780 target = (struct packed_git *)request->userData;
781 target->pack_size = pack_size;
782 lst = &remote->packs;
783 while (*lst != target)
784 lst = &((*lst)->next);
787 if (!verify_pack(target))
788 install_packed_git(target);
790 remote->can_update_info_refs = 0;
793 release_request(request);
797 #ifdef USE_CURL_MULTI
798 static int fill_active_slot(void *unused)
800 struct transfer_request *request = request_queue_head;
805 for (request = request_queue_head; request; request = request->next) {
806 if (request->state == NEED_FETCH) {
807 start_fetch_loose(request);
809 } else if (pushing && request->state == NEED_PUSH) {
810 if (remote_dir_exists[request->obj->sha1[0]] == 1) {
813 start_mkcol(request);
822 static void get_remote_object_list(unsigned char parent);
824 static void add_fetch_request(struct object *obj)
826 struct transfer_request *request;
831 * Don't fetch the object if it's known to exist locally
832 * or is already in the request queue
834 if (remote_dir_exists[obj->sha1[0]] == -1)
835 get_remote_object_list(obj->sha1[0]);
836 if (obj->flags & (LOCAL | FETCHING))
839 obj->flags |= FETCHING;
840 request = xmalloc(sizeof(*request));
843 request->lock = NULL;
844 request->headers = NULL;
845 request->local_fileno = -1;
846 request->local_stream = NULL;
847 request->state = NEED_FETCH;
848 request->next = request_queue_head;
849 request_queue_head = request;
851 #ifdef USE_CURL_MULTI
857 static int add_send_request(struct object *obj, struct remote_lock *lock)
859 struct transfer_request *request = request_queue_head;
860 struct packed_git *target;
862 /* Keep locks active */
866 * Don't push the object if it's known to exist on the remote
867 * or is already in the request queue
869 if (remote_dir_exists[obj->sha1[0]] == -1)
870 get_remote_object_list(obj->sha1[0]);
871 if (obj->flags & (REMOTE | PUSHING))
873 target = find_sha1_pack(obj->sha1, remote->packs);
875 obj->flags |= REMOTE;
879 obj->flags |= PUSHING;
880 request = xmalloc(sizeof(*request));
883 request->lock = lock;
884 request->headers = NULL;
885 request->local_fileno = -1;
886 request->local_stream = NULL;
887 request->state = NEED_PUSH;
888 request->next = request_queue_head;
889 request_queue_head = request;
891 #ifdef USE_CURL_MULTI
899 static int fetch_index(unsigned char *sha1)
901 char *hex = sha1_to_hex(sha1);
904 char tmpfile[PATH_MAX];
906 char range[RANGE_HEADER_SIZE];
907 struct curl_slist *range_header = NULL;
910 struct active_request_slot *slot;
911 struct slot_results results;
913 /* Don't use the index if the pack isn't there */
914 url = xmalloc(strlen(remote->url) + 64);
915 sprintf(url, "%sobjects/pack/pack-%s.pack", remote->url, hex);
916 slot = get_active_slot();
917 slot->results = &results;
918 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
919 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
920 if (start_active_slot(slot)) {
921 run_active_slot(slot);
922 if (results.curl_result != CURLE_OK) {
924 return error("Unable to verify pack %s is available",
929 return error("Unable to start request");
932 if (has_pack_index(sha1)) {
938 fprintf(stderr, "Getting index for pack %s\n", hex);
940 sprintf(url, "%sobjects/pack/pack-%s.idx", remote->url, hex);
942 filename = sha1_pack_index_name(sha1);
943 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
944 indexfile = fopen(tmpfile, "a");
947 return error("Unable to open local file %s for pack index",
951 slot = get_active_slot();
952 slot->results = &results;
953 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
954 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
955 curl_easy_setopt(slot->curl, CURLOPT_FILE, indexfile);
956 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
957 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
958 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
959 slot->local = indexfile;
961 /* If there is data present from a previous transfer attempt,
962 resume where it left off */
963 prev_posn = ftell(indexfile);
967 "Resuming fetch of index for pack %s at byte %ld\n",
969 sprintf(range, "Range: bytes=%ld-", prev_posn);
970 range_header = curl_slist_append(range_header, range);
971 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
974 if (start_active_slot(slot)) {
975 run_active_slot(slot);
976 if (results.curl_result != CURLE_OK) {
979 return error("Unable to get pack index %s\n%s", url,
985 return error("Unable to start request");
991 return move_temp_to_file(tmpfile, filename);
994 static int setup_index(unsigned char *sha1)
996 struct packed_git *new_pack;
998 if (fetch_index(sha1))
1001 new_pack = parse_pack_index(sha1);
1002 new_pack->next = remote->packs;
1003 remote->packs = new_pack;
1007 static int fetch_indices(void)
1009 unsigned char sha1[20];
1011 struct strbuf buffer = STRBUF_INIT;
1015 struct active_request_slot *slot;
1016 struct slot_results results;
1019 fprintf(stderr, "Getting pack list\n");
1021 url = xmalloc(strlen(remote->url) + 20);
1022 sprintf(url, "%sobjects/info/packs", remote->url);
1024 slot = get_active_slot();
1025 slot->results = &results;
1026 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
1027 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1028 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1029 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
1030 if (start_active_slot(slot)) {
1031 run_active_slot(slot);
1032 if (results.curl_result != CURLE_OK) {
1033 strbuf_release(&buffer);
1035 if (results.http_code == 404)
1038 return error("%s", curl_errorstr);
1041 strbuf_release(&buffer);
1043 return error("Unable to start request");
1048 while (i < buffer.len) {
1052 if (i + 52 < buffer.len &&
1053 !prefixcmp(data + i, " pack-") &&
1054 !prefixcmp(data + i + 46, ".pack\n")) {
1055 get_sha1_hex(data + i + 6, sha1);
1061 while (data[i] != '\n')
1067 strbuf_release(&buffer);
1071 static void one_remote_object(const char *hex)
1073 unsigned char sha1[20];
1076 if (get_sha1_hex(hex, sha1) != 0)
1079 obj = lookup_object(sha1);
1081 obj = parse_object(sha1);
1083 /* Ignore remote objects that don't exist locally */
1087 obj->flags |= REMOTE;
1088 if (!object_list_contains(objects, obj))
1089 object_list_insert(obj, &objects);
1092 static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed)
1094 int *lock_flags = (int *)ctx->userData;
1097 if (!strcmp(ctx->name, DAV_CTX_LOCKENTRY)) {
1098 if ((*lock_flags & DAV_PROP_LOCKEX) &&
1099 (*lock_flags & DAV_PROP_LOCKWR)) {
1100 *lock_flags |= DAV_LOCK_OK;
1102 *lock_flags &= DAV_LOCK_OK;
1103 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_WRITE)) {
1104 *lock_flags |= DAV_PROP_LOCKWR;
1105 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_EXCLUSIVE)) {
1106 *lock_flags |= DAV_PROP_LOCKEX;
1111 static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
1113 struct remote_lock *lock = (struct remote_lock *)ctx->userData;
1115 if (tag_closed && ctx->cdata) {
1116 if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) {
1117 lock->owner = xmalloc(strlen(ctx->cdata) + 1);
1118 strcpy(lock->owner, ctx->cdata);
1119 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
1120 if (!prefixcmp(ctx->cdata, "Second-"))
1122 strtol(ctx->cdata + 7, NULL, 10);
1123 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
1124 lock->token = xmalloc(strlen(ctx->cdata) + 1);
1125 strcpy(lock->token, ctx->cdata);
1130 static void one_remote_ref(char *refname);
1133 xml_start_tag(void *userData, const char *name, const char **atts)
1135 struct xml_ctx *ctx = (struct xml_ctx *)userData;
1136 const char *c = strchr(name, ':');
1144 new_len = strlen(ctx->name) + strlen(c) + 2;
1146 if (new_len > ctx->len) {
1147 ctx->name = xrealloc(ctx->name, new_len);
1150 strcat(ctx->name, ".");
1151 strcat(ctx->name, c);
1156 ctx->userFunc(ctx, 0);
1160 xml_end_tag(void *userData, const char *name)
1162 struct xml_ctx *ctx = (struct xml_ctx *)userData;
1163 const char *c = strchr(name, ':');
1166 ctx->userFunc(ctx, 1);
1173 ep = ctx->name + strlen(ctx->name) - strlen(c) - 1;
1178 xml_cdata(void *userData, const XML_Char *s, int len)
1180 struct xml_ctx *ctx = (struct xml_ctx *)userData;
1182 ctx->cdata = xmemdupz(s, len);
1185 static struct remote_lock *lock_remote(const char *path, long timeout)
1187 struct active_request_slot *slot;
1188 struct slot_results results;
1189 struct buffer out_buffer = { STRBUF_INIT, 0 };
1190 struct strbuf in_buffer = STRBUF_INIT;
1193 char timeout_header[25];
1194 struct remote_lock *lock = NULL;
1195 struct curl_slist *dav_headers = NULL;
1198 url = xmalloc(strlen(remote->url) + strlen(path) + 1);
1199 sprintf(url, "%s%s", remote->url, path);
1201 /* Make sure leading directories exist for the remote ref */
1202 ep = strchr(url + strlen(remote->url) + 1, '/');
1205 slot = get_active_slot();
1206 slot->results = &results;
1207 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
1208 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1209 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
1210 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1211 if (start_active_slot(slot)) {
1212 run_active_slot(slot);
1213 if (results.curl_result != CURLE_OK &&
1214 results.http_code != 405) {
1216 "Unable to create branch path %s\n",
1222 fprintf(stderr, "Unable to start MKCOL request\n");
1227 ep = strchr(ep + 1, '/');
1230 strbuf_addf(&out_buffer.buf, LOCK_REQUEST, git_default_email);
1232 sprintf(timeout_header, "Timeout: Second-%ld", timeout);
1233 dav_headers = curl_slist_append(dav_headers, timeout_header);
1234 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1236 slot = get_active_slot();
1237 slot->results = &results;
1238 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1239 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1240 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1241 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1242 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1243 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1244 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1245 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
1246 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1248 lock = xcalloc(1, sizeof(*lock));
1251 if (start_active_slot(slot)) {
1252 run_active_slot(slot);
1253 if (results.curl_result == CURLE_OK) {
1254 XML_Parser parser = XML_ParserCreate(NULL);
1255 enum XML_Status result;
1256 ctx.name = xcalloc(10, 1);
1259 ctx.userFunc = handle_new_lock_ctx;
1260 ctx.userData = lock;
1261 XML_SetUserData(parser, &ctx);
1262 XML_SetElementHandler(parser, xml_start_tag,
1264 XML_SetCharacterDataHandler(parser, xml_cdata);
1265 result = XML_Parse(parser, in_buffer.buf,
1268 if (result != XML_STATUS_OK) {
1269 fprintf(stderr, "XML error: %s\n",
1271 XML_GetErrorCode(parser)));
1274 XML_ParserFree(parser);
1277 fprintf(stderr, "Unable to start LOCK request\n");
1280 curl_slist_free_all(dav_headers);
1281 strbuf_release(&out_buffer.buf);
1282 strbuf_release(&in_buffer);
1284 if (lock->token == NULL || lock->timeout <= 0) {
1292 lock->start_time = time(NULL);
1293 lock->next = remote->locks;
1294 remote->locks = lock;
1300 static int unlock_remote(struct remote_lock *lock)
1302 struct active_request_slot *slot;
1303 struct slot_results results;
1304 struct remote_lock *prev = remote->locks;
1305 char *lock_token_header;
1306 struct curl_slist *dav_headers = NULL;
1309 lock_token_header = xmalloc(strlen(lock->token) + 31);
1310 sprintf(lock_token_header, "Lock-Token: <%s>",
1312 dav_headers = curl_slist_append(dav_headers, lock_token_header);
1314 slot = get_active_slot();
1315 slot->results = &results;
1316 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1317 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1318 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_UNLOCK);
1319 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1321 if (start_active_slot(slot)) {
1322 run_active_slot(slot);
1323 if (results.curl_result == CURLE_OK)
1326 fprintf(stderr, "UNLOCK HTTP error %ld\n",
1329 fprintf(stderr, "Unable to start UNLOCK request\n");
1332 curl_slist_free_all(dav_headers);
1333 free(lock_token_header);
1335 if (remote->locks == lock) {
1336 remote->locks = lock->next;
1338 while (prev && prev->next != lock)
1341 prev->next = prev->next->next;
1352 static void remove_locks(void)
1354 struct remote_lock *lock = remote->locks;
1356 fprintf(stderr, "Removing remote locks...\n");
1358 unlock_remote(lock);
1363 static void remove_locks_on_signal(int signo)
1366 signal(signo, SIG_DFL);
1370 static void remote_ls(const char *path, int flags,
1371 void (*userFunc)(struct remote_ls_ctx *ls),
1374 static void process_ls_object(struct remote_ls_ctx *ls)
1376 unsigned int *parent = (unsigned int *)ls->userData;
1377 char *path = ls->dentry_name;
1380 if (!strcmp(ls->path, ls->dentry_name) && (ls->flags & IS_DIR)) {
1381 remote_dir_exists[*parent] = 1;
1385 if (strlen(path) != 49)
1388 obj_hex = xmalloc(strlen(path));
1389 /* NB: path is not null-terminated, can not use strlcpy here */
1390 memcpy(obj_hex, path, 2);
1391 strcpy(obj_hex + 2, path + 3);
1392 one_remote_object(obj_hex);
1396 static void process_ls_ref(struct remote_ls_ctx *ls)
1398 if (!strcmp(ls->path, ls->dentry_name) && (ls->dentry_flags & IS_DIR)) {
1399 fprintf(stderr, " %s\n", ls->dentry_name);
1403 if (!(ls->dentry_flags & IS_DIR))
1404 one_remote_ref(ls->dentry_name);
1407 static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
1409 struct remote_ls_ctx *ls = (struct remote_ls_ctx *)ctx->userData;
1412 if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) {
1413 if (ls->dentry_flags & IS_DIR) {
1414 if (ls->flags & PROCESS_DIRS) {
1417 if (strcmp(ls->dentry_name, ls->path) &&
1418 ls->flags & RECURSIVE) {
1419 remote_ls(ls->dentry_name,
1424 } else if (ls->flags & PROCESS_FILES) {
1427 } else if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
1428 char *path = ctx->cdata;
1429 if (*ctx->cdata == 'h') {
1430 path = strstr(path, "//");
1432 path = strchr(path+2, '/');
1436 path += remote->path_len;
1438 ls->dentry_name = xmalloc(strlen(path) -
1439 remote->path_len + 1);
1440 strcpy(ls->dentry_name, path + remote->path_len);
1441 } else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
1442 ls->dentry_flags |= IS_DIR;
1444 } else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
1445 free(ls->dentry_name);
1446 ls->dentry_name = NULL;
1447 ls->dentry_flags = 0;
1451 static void remote_ls(const char *path, int flags,
1452 void (*userFunc)(struct remote_ls_ctx *ls),
1455 char *url = xmalloc(strlen(remote->url) + strlen(path) + 1);
1456 struct active_request_slot *slot;
1457 struct slot_results results;
1458 struct strbuf in_buffer = STRBUF_INIT;
1459 struct buffer out_buffer = { STRBUF_INIT, 0 };
1460 struct curl_slist *dav_headers = NULL;
1462 struct remote_ls_ctx ls;
1465 ls.path = xstrdup(path);
1466 ls.dentry_name = NULL;
1467 ls.dentry_flags = 0;
1468 ls.userData = userData;
1469 ls.userFunc = userFunc;
1471 sprintf(url, "%s%s", remote->url, path);
1473 strbuf_addf(&out_buffer.buf, PROPFIND_ALL_REQUEST);
1475 dav_headers = curl_slist_append(dav_headers, "Depth: 1");
1476 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1478 slot = get_active_slot();
1479 slot->results = &results;
1480 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1481 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1482 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1483 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1484 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1485 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1486 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1487 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1488 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1490 if (start_active_slot(slot)) {
1491 run_active_slot(slot);
1492 if (results.curl_result == CURLE_OK) {
1493 XML_Parser parser = XML_ParserCreate(NULL);
1494 enum XML_Status result;
1495 ctx.name = xcalloc(10, 1);
1498 ctx.userFunc = handle_remote_ls_ctx;
1500 XML_SetUserData(parser, &ctx);
1501 XML_SetElementHandler(parser, xml_start_tag,
1503 XML_SetCharacterDataHandler(parser, xml_cdata);
1504 result = XML_Parse(parser, in_buffer.buf,
1508 if (result != XML_STATUS_OK) {
1509 fprintf(stderr, "XML error: %s\n",
1511 XML_GetErrorCode(parser)));
1513 XML_ParserFree(parser);
1516 fprintf(stderr, "Unable to start PROPFIND request\n");
1521 strbuf_release(&out_buffer.buf);
1522 strbuf_release(&in_buffer);
1523 curl_slist_free_all(dav_headers);
1526 static void get_remote_object_list(unsigned char parent)
1528 char path[] = "objects/XX/";
1529 static const char hex[] = "0123456789abcdef";
1530 unsigned int val = parent;
1532 path[8] = hex[val >> 4];
1533 path[9] = hex[val & 0xf];
1534 remote_dir_exists[val] = 0;
1535 remote_ls(path, (PROCESS_FILES | PROCESS_DIRS),
1536 process_ls_object, &val);
1539 static int locking_available(void)
1541 struct active_request_slot *slot;
1542 struct slot_results results;
1543 struct strbuf in_buffer = STRBUF_INIT;
1544 struct buffer out_buffer = { STRBUF_INIT, 0 };
1545 struct curl_slist *dav_headers = NULL;
1549 strbuf_addf(&out_buffer.buf, PROPFIND_SUPPORTEDLOCK_REQUEST, remote->url);
1551 dav_headers = curl_slist_append(dav_headers, "Depth: 0");
1552 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1554 slot = get_active_slot();
1555 slot->results = &results;
1556 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1557 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1558 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1559 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1560 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1561 curl_easy_setopt(slot->curl, CURLOPT_URL, remote->url);
1562 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1563 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1564 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1566 if (start_active_slot(slot)) {
1567 run_active_slot(slot);
1568 if (results.curl_result == CURLE_OK) {
1569 XML_Parser parser = XML_ParserCreate(NULL);
1570 enum XML_Status result;
1571 ctx.name = xcalloc(10, 1);
1574 ctx.userFunc = handle_lockprop_ctx;
1575 ctx.userData = &lock_flags;
1576 XML_SetUserData(parser, &ctx);
1577 XML_SetElementHandler(parser, xml_start_tag,
1579 result = XML_Parse(parser, in_buffer.buf,
1583 if (result != XML_STATUS_OK) {
1584 fprintf(stderr, "XML error: %s\n",
1586 XML_GetErrorCode(parser)));
1589 XML_ParserFree(parser);
1591 error("Error: no DAV locking support on %s",
1595 error("Cannot access URL %s, return code %d",
1596 remote->url, results.curl_result);
1600 error("Unable to start PROPFIND request on %s", remote->url);
1603 strbuf_release(&out_buffer.buf);
1604 strbuf_release(&in_buffer);
1605 curl_slist_free_all(dav_headers);
1610 static struct object_list **add_one_object(struct object *obj, struct object_list **p)
1612 struct object_list *entry = xmalloc(sizeof(struct object_list));
1616 return &entry->next;
1619 static struct object_list **process_blob(struct blob *blob,
1620 struct object_list **p,
1621 struct name_path *path,
1624 struct object *obj = &blob->object;
1626 obj->flags |= LOCAL;
1628 if (obj->flags & (UNINTERESTING | SEEN))
1632 return add_one_object(obj, p);
1635 static struct object_list **process_tree(struct tree *tree,
1636 struct object_list **p,
1637 struct name_path *path,
1640 struct object *obj = &tree->object;
1641 struct tree_desc desc;
1642 struct name_entry entry;
1643 struct name_path me;
1645 obj->flags |= LOCAL;
1647 if (obj->flags & (UNINTERESTING | SEEN))
1649 if (parse_tree(tree) < 0)
1650 die("bad tree object %s", sha1_to_hex(obj->sha1));
1653 name = xstrdup(name);
1654 p = add_one_object(obj, p);
1657 me.elem_len = strlen(name);
1659 init_tree_desc(&desc, tree->buffer, tree->size);
1661 while (tree_entry(&desc, &entry))
1662 switch (object_type(entry.mode)) {
1664 p = process_tree(lookup_tree(entry.sha1), p, &me, name);
1667 p = process_blob(lookup_blob(entry.sha1), p, &me, name);
1670 /* Subproject commit - not in this repository */
1675 tree->buffer = NULL;
1679 static int get_delta(struct rev_info *revs, struct remote_lock *lock)
1682 struct commit *commit;
1683 struct object_list **p = &objects;
1686 while ((commit = get_revision(revs)) != NULL) {
1687 p = process_tree(commit->tree, p, NULL, "");
1688 commit->object.flags |= LOCAL;
1689 if (!(commit->object.flags & UNINTERESTING))
1690 count += add_send_request(&commit->object, lock);
1693 for (i = 0; i < revs->pending.nr; i++) {
1694 struct object_array_entry *entry = revs->pending.objects + i;
1695 struct object *obj = entry->item;
1696 const char *name = entry->name;
1698 if (obj->flags & (UNINTERESTING | SEEN))
1700 if (obj->type == OBJ_TAG) {
1702 p = add_one_object(obj, p);
1705 if (obj->type == OBJ_TREE) {
1706 p = process_tree((struct tree *)obj, p, NULL, name);
1709 if (obj->type == OBJ_BLOB) {
1710 p = process_blob((struct blob *)obj, p, NULL, name);
1713 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
1717 if (!(objects->item->flags & UNINTERESTING))
1718 count += add_send_request(objects->item, lock);
1719 objects = objects->next;
1725 static int update_remote(unsigned char *sha1, struct remote_lock *lock)
1727 struct active_request_slot *slot;
1728 struct slot_results results;
1730 struct buffer out_buffer = { STRBUF_INIT, 0 };
1731 struct curl_slist *dav_headers = NULL;
1733 if_header = xmalloc(strlen(lock->token) + 25);
1734 sprintf(if_header, "If: (<%s>)", lock->token);
1735 dav_headers = curl_slist_append(dav_headers, if_header);
1737 strbuf_addf(&out_buffer.buf, "%s\n", sha1_to_hex(sha1));
1739 slot = get_active_slot();
1740 slot->results = &results;
1741 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1742 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1743 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1744 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1745 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1746 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1747 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1748 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1749 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1751 if (start_active_slot(slot)) {
1752 run_active_slot(slot);
1753 strbuf_release(&out_buffer.buf);
1755 if (results.curl_result != CURLE_OK) {
1757 "PUT error: curl result=%d, HTTP code=%ld\n",
1758 results.curl_result, results.http_code);
1759 /* We should attempt recovery? */
1763 strbuf_release(&out_buffer.buf);
1765 fprintf(stderr, "Unable to start PUT request\n");
1772 static struct ref *local_refs, **local_tail;
1773 static struct ref *remote_refs, **remote_tail;
1775 static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
1778 int len = strlen(refname) + 1;
1779 ref = xcalloc(1, sizeof(*ref) + len);
1780 hashcpy(ref->new_sha1, sha1);
1781 memcpy(ref->name, refname, len);
1783 local_tail = &ref->next;
1787 static void one_remote_ref(char *refname)
1792 ref = alloc_ref(refname);
1794 if (http_fetch_ref(remote->url, ref) != 0) {
1796 "Unable to fetch ref %s from %s\n",
1797 refname, remote->url);
1803 * Fetch a copy of the object if it doesn't exist locally - it
1804 * may be required for updating server info later.
1806 if (remote->can_update_info_refs && !has_sha1_file(ref->old_sha1)) {
1807 obj = lookup_unknown_object(ref->old_sha1);
1809 fprintf(stderr, " fetch %s for %s\n",
1810 sha1_to_hex(ref->old_sha1), refname);
1811 add_fetch_request(obj);
1816 remote_tail = &ref->next;
1819 static void get_local_heads(void)
1821 local_tail = &local_refs;
1822 for_each_ref(one_local_ref, NULL);
1825 static void get_dav_remote_heads(void)
1827 remote_tail = &remote_refs;
1828 remote_ls("refs/", (PROCESS_FILES | PROCESS_DIRS | RECURSIVE), process_ls_ref, NULL);
1831 static int is_zero_sha1(const unsigned char *sha1)
1835 for (i = 0; i < 20; i++) {
1842 static void unmark_and_free(struct commit_list *list, unsigned int mark)
1845 struct commit_list *temp = list;
1846 temp->item->object.flags &= ~mark;
1852 static int ref_newer(const unsigned char *new_sha1,
1853 const unsigned char *old_sha1)
1856 struct commit *old, *new;
1857 struct commit_list *list, *used;
1860 /* Both new and old must be commit-ish and new is descendant of
1861 * old. Otherwise we require --force.
1863 o = deref_tag(parse_object(old_sha1), NULL, 0);
1864 if (!o || o->type != OBJ_COMMIT)
1866 old = (struct commit *) o;
1868 o = deref_tag(parse_object(new_sha1), NULL, 0);
1869 if (!o || o->type != OBJ_COMMIT)
1871 new = (struct commit *) o;
1873 if (parse_commit(new) < 0)
1877 commit_list_insert(new, &list);
1879 new = pop_most_recent_commit(&list, TMP_MARK);
1880 commit_list_insert(new, &used);
1886 unmark_and_free(list, TMP_MARK);
1887 unmark_and_free(used, TMP_MARK);
1891 static void add_remote_info_ref(struct remote_ls_ctx *ls)
1893 struct strbuf *buf = (struct strbuf *)ls->userData;
1899 ref = alloc_ref(ls->dentry_name);
1901 if (http_fetch_ref(remote->url, ref) != 0) {
1903 "Unable to fetch ref %s from %s\n",
1904 ls->dentry_name, remote->url);
1910 o = parse_object(ref->old_sha1);
1913 "Unable to parse object %s for remote ref %s\n",
1914 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1920 len = strlen(ls->dentry_name) + 42;
1921 ref_info = xcalloc(len + 1, 1);
1922 sprintf(ref_info, "%s %s\n",
1923 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1924 fwrite_buffer(ref_info, 1, len, buf);
1927 if (o->type == OBJ_TAG) {
1928 o = deref_tag(o, ls->dentry_name, 0);
1930 len = strlen(ls->dentry_name) + 45;
1931 ref_info = xcalloc(len + 1, 1);
1932 sprintf(ref_info, "%s %s^{}\n",
1933 sha1_to_hex(o->sha1), ls->dentry_name);
1934 fwrite_buffer(ref_info, 1, len, buf);
1941 static void update_remote_info_refs(struct remote_lock *lock)
1943 struct buffer buffer = { STRBUF_INIT, 0 };
1944 struct active_request_slot *slot;
1945 struct slot_results results;
1947 struct curl_slist *dav_headers = NULL;
1949 remote_ls("refs/", (PROCESS_FILES | RECURSIVE),
1950 add_remote_info_ref, &buffer.buf);
1952 if_header = xmalloc(strlen(lock->token) + 25);
1953 sprintf(if_header, "If: (<%s>)", lock->token);
1954 dav_headers = curl_slist_append(dav_headers, if_header);
1956 slot = get_active_slot();
1957 slot->results = &results;
1958 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &buffer);
1959 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, buffer.buf.len);
1960 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1961 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1962 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1963 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1964 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1965 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1966 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1968 if (start_active_slot(slot)) {
1969 run_active_slot(slot);
1970 if (results.curl_result != CURLE_OK) {
1972 "PUT error: curl result=%d, HTTP code=%ld\n",
1973 results.curl_result, results.http_code);
1978 strbuf_release(&buffer.buf);
1981 static int remote_exists(const char *path)
1983 char *url = xmalloc(strlen(remote->url) + strlen(path) + 1);
1984 struct active_request_slot *slot;
1985 struct slot_results results;
1988 sprintf(url, "%s%s", remote->url, path);
1990 slot = get_active_slot();
1991 slot->results = &results;
1992 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1993 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
1995 if (start_active_slot(slot)) {
1996 run_active_slot(slot);
1997 if (results.http_code == 404)
1999 else if (results.curl_result == CURLE_OK)
2002 fprintf(stderr, "HEAD HTTP error %ld\n", results.http_code);
2004 fprintf(stderr, "Unable to start HEAD request\n");
2011 static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
2014 struct strbuf buffer = STRBUF_INIT;
2015 struct active_request_slot *slot;
2016 struct slot_results results;
2018 url = xmalloc(strlen(remote->url) + strlen(path) + 1);
2019 sprintf(url, "%s%s", remote->url, path);
2021 slot = get_active_slot();
2022 slot->results = &results;
2023 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
2024 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
2025 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
2026 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2027 if (start_active_slot(slot)) {
2028 run_active_slot(slot);
2029 if (results.curl_result != CURLE_OK) {
2030 die("Couldn't get %s for remote symref\n%s",
2031 url, curl_errorstr);
2034 die("Unable to start remote symref request");
2042 if (buffer.len == 0)
2045 /* If it's a symref, set the refname; otherwise try for a sha1 */
2046 if (!prefixcmp((char *)buffer.buf, "ref: ")) {
2047 *symref = xmemdupz((char *)buffer.buf + 5, buffer.len - 6);
2049 get_sha1_hex(buffer.buf, sha1);
2052 strbuf_release(&buffer);
2055 static int verify_merge_base(unsigned char *head_sha1, unsigned char *branch_sha1)
2057 struct commit *head = lookup_commit(head_sha1);
2058 struct commit *branch = lookup_commit(branch_sha1);
2059 struct commit_list *merge_bases = get_merge_bases(head, branch, 1);
2061 return (merge_bases && !merge_bases->next && merge_bases->item == branch);
2064 static int delete_remote_branch(char *pattern, int force)
2066 struct ref *refs = remote_refs;
2067 struct ref *remote_ref = NULL;
2068 unsigned char head_sha1[20];
2069 char *symref = NULL;
2071 int patlen = strlen(pattern);
2073 struct active_request_slot *slot;
2074 struct slot_results results;
2077 /* Find the remote branch(es) matching the specified branch name */
2078 for (match = 0; refs; refs = refs->next) {
2079 char *name = refs->name;
2080 int namelen = strlen(name);
2081 if (namelen < patlen ||
2082 memcmp(name + namelen - patlen, pattern, patlen))
2084 if (namelen != patlen && name[namelen - patlen - 1] != '/')
2090 return error("No remote branch matches %s", pattern);
2092 return error("More than one remote branch matches %s",
2096 * Remote HEAD must be a symref (not exactly foolproof; a remote
2097 * symlink to a symref will look like a symref)
2099 fetch_symref("HEAD", &symref, head_sha1);
2101 return error("Remote HEAD is not a symref");
2103 /* Remote branch must not be the remote HEAD */
2104 for (i=0; symref && i<MAXDEPTH; i++) {
2105 if (!strcmp(remote_ref->name, symref))
2106 return error("Remote branch %s is the current HEAD",
2108 fetch_symref(symref, &symref, head_sha1);
2111 /* Run extra sanity checks if delete is not forced */
2113 /* Remote HEAD must resolve to a known object */
2115 return error("Remote HEAD symrefs too deep");
2116 if (is_zero_sha1(head_sha1))
2117 return error("Unable to resolve remote HEAD");
2118 if (!has_sha1_file(head_sha1))
2119 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1));
2121 /* Remote branch must resolve to a known object */
2122 if (is_zero_sha1(remote_ref->old_sha1))
2123 return error("Unable to resolve remote branch %s",
2125 if (!has_sha1_file(remote_ref->old_sha1))
2126 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));
2128 /* Remote branch must be an ancestor of remote HEAD */
2129 if (!verify_merge_base(head_sha1, remote_ref->old_sha1)) {
2130 return error("The branch '%s' is not an ancestor "
2131 "of your current HEAD.\n"
2132 "If you are sure you want to delete it,"
2133 " run:\n\t'git http-push -D %s %s'",
2134 remote_ref->name, remote->url, pattern);
2138 /* Send delete request */
2139 fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name);
2142 url = xmalloc(strlen(remote->url) + strlen(remote_ref->name) + 1);
2143 sprintf(url, "%s%s", remote->url, remote_ref->name);
2144 slot = get_active_slot();
2145 slot->results = &results;
2146 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
2147 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
2148 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2149 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_DELETE);
2150 if (start_active_slot(slot)) {
2151 run_active_slot(slot);
2153 if (results.curl_result != CURLE_OK)
2154 return error("DELETE request failed (%d/%ld)\n",
2155 results.curl_result, results.http_code);
2158 return error("Unable to start DELETE request");
2164 int main(int argc, char **argv)
2166 struct transfer_request *request;
2167 struct transfer_request *next_request;
2169 char **refspec = NULL;
2170 struct remote_lock *ref_lock = NULL;
2171 struct remote_lock *info_ref_lock = NULL;
2172 struct rev_info revs;
2173 int delete_branch = 0;
2174 int force_delete = 0;
2175 int objects_to_send;
2180 char *rewritten_url = NULL;
2182 setup_git_directory();
2184 remote = xcalloc(sizeof(*remote), 1);
2187 for (i = 1; i < argc; i++, argv++) {
2191 if (!strcmp(arg, "--all")) {
2192 push_all = MATCH_REFS_ALL;
2195 if (!strcmp(arg, "--force")) {
2199 if (!strcmp(arg, "--dry-run")) {
2203 if (!strcmp(arg, "--verbose")) {
2207 if (!strcmp(arg, "-d")) {
2211 if (!strcmp(arg, "-D")) {
2218 char *path = strstr(arg, "//");
2220 remote->path_len = strlen(arg);
2222 remote->path = strchr(path+2, '/');
2224 remote->path_len = strlen(remote->path);
2229 nr_refspec = argc - i;
2233 #ifndef USE_CURL_MULTI
2234 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
2238 usage(http_push_usage);
2240 if (delete_branch && nr_refspec != 1)
2241 die("You must specify only one branch name when deleting a remote branch");
2243 memset(remote_dir_exists, -1, 256);
2247 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
2249 if (remote->url && remote->url[strlen(remote->url)-1] != '/') {
2250 rewritten_url = xmalloc(strlen(remote->url)+2);
2251 strcpy(rewritten_url, remote->url);
2252 strcat(rewritten_url, "/");
2253 remote->path = rewritten_url + (remote->path - remote->url);
2255 remote->url = rewritten_url;
2258 /* Verify DAV compliance/lock support */
2259 if (!locking_available()) {
2264 signal(SIGINT, remove_locks_on_signal);
2265 signal(SIGHUP, remove_locks_on_signal);
2266 signal(SIGQUIT, remove_locks_on_signal);
2267 signal(SIGTERM, remove_locks_on_signal);
2269 /* Check whether the remote has server info files */
2270 remote->can_update_info_refs = 0;
2271 remote->has_info_refs = remote_exists("info/refs");
2272 remote->has_info_packs = remote_exists("objects/info/packs");
2273 if (remote->has_info_refs) {
2274 info_ref_lock = lock_remote("info/refs", LOCK_TIME);
2276 remote->can_update_info_refs = 1;
2278 fprintf(stderr, "Error: cannot lock existing info/refs\n");
2283 if (remote->has_info_packs)
2286 /* Get a list of all local and remote heads to validate refspecs */
2288 fprintf(stderr, "Fetching remote heads...\n");
2289 get_dav_remote_heads();
2291 /* Remove a remote branch if -d or -D was specified */
2292 if (delete_branch) {
2293 if (delete_remote_branch(refspec[0], force_delete) == -1)
2294 fprintf(stderr, "Unable to delete remote branch %s\n",
2301 remote_tail = &remote_refs;
2302 if (match_refs(local_refs, remote_refs, &remote_tail,
2303 nr_refspec, (const char **) refspec, push_all)) {
2308 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
2314 for (ref = remote_refs; ref; ref = ref->next) {
2315 char old_hex[60], *new_hex;
2316 const char *commit_argv[4];
2318 char *new_sha1_hex, *old_sha1_hex;
2323 if (is_zero_sha1(ref->peer_ref->new_sha1)) {
2324 if (delete_remote_branch(ref->name, 1) == -1) {
2325 error("Could not remove %s", ref->name);
2332 if (!hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
2333 if (push_verbosely || 1)
2334 fprintf(stderr, "'%s': up-to-date\n", ref->name);
2339 !is_zero_sha1(ref->old_sha1) &&
2341 if (!has_sha1_file(ref->old_sha1) ||
2342 !ref_newer(ref->peer_ref->new_sha1,
2345 * We do not have the remote ref, or
2346 * we know that the remote ref is not
2347 * an ancestor of what we are trying to
2348 * push. Either way this can be losing
2349 * commits at the remote end and likely
2350 * we were not up to date to begin with.
2352 error("remote '%s' is not an ancestor of\n"
2354 "Maybe you are not up-to-date and "
2355 "need to pull first?",
2357 ref->peer_ref->name);
2362 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
2364 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
2365 new_hex = sha1_to_hex(ref->new_sha1);
2367 fprintf(stderr, "updating '%s'", ref->name);
2368 if (strcmp(ref->name, ref->peer_ref->name))
2369 fprintf(stderr, " using '%s'", ref->peer_ref->name);
2370 fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex);
2374 /* Lock remote branch ref */
2375 ref_lock = lock_remote(ref->name, LOCK_TIME);
2376 if (ref_lock == NULL) {
2377 fprintf(stderr, "Unable to lock remote branch %s\n",
2383 /* Set up revision info for this refspec */
2385 new_sha1_hex = xstrdup(sha1_to_hex(ref->new_sha1));
2386 old_sha1_hex = NULL;
2387 commit_argv[1] = "--objects";
2388 commit_argv[2] = new_sha1_hex;
2389 if (!push_all && !is_zero_sha1(ref->old_sha1)) {
2390 old_sha1_hex = xmalloc(42);
2391 sprintf(old_sha1_hex, "^%s",
2392 sha1_to_hex(ref->old_sha1));
2393 commit_argv[3] = old_sha1_hex;
2396 init_revisions(&revs, setup_git_directory());
2397 setup_revisions(commit_argc, commit_argv, &revs, NULL);
2398 revs.edge_hint = 0; /* just in case */
2402 commit_argv[1] = NULL;
2405 /* Generate a list of objects that need to be pushed */
2407 if (prepare_revision_walk(&revs))
2408 die("revision walk setup failed");
2409 mark_edges_uninteresting(revs.commits, &revs, NULL);
2410 objects_to_send = get_delta(&revs, ref_lock);
2411 finish_all_active_slots();
2413 /* Push missing objects to remote, this would be a
2414 convenient time to pack them first if appropriate. */
2416 if (objects_to_send)
2417 fprintf(stderr, " sending %d objects\n",
2419 #ifdef USE_CURL_MULTI
2420 fill_active_slots();
2421 add_fill_function(NULL, fill_active_slot);
2424 finish_all_active_slots();
2425 #ifdef USE_CURL_MULTI
2426 fill_active_slots();
2428 } while (request_queue_head && !aborted);
2430 /* Update the remote branch if all went well */
2431 if (aborted || !update_remote(ref->new_sha1, ref_lock))
2435 fprintf(stderr, " done\n");
2436 unlock_remote(ref_lock);
2440 /* Update remote server info if appropriate */
2441 if (remote->has_info_refs && new_refs) {
2442 if (info_ref_lock && remote->can_update_info_refs) {
2443 fprintf(stderr, "Updating remote server info\n");
2445 update_remote_info_refs(info_ref_lock);
2447 fprintf(stderr, "Unable to update server info\n");
2452 free(rewritten_url);
2454 unlock_remote(info_ref_lock);
2457 curl_slist_free_all(no_pragma_header);
2461 request = request_queue_head;
2462 while (request != NULL) {
2463 next_request = request->next;
2464 release_request(request);
2465 request = next_request;