11 #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
31 #define DAV_LOCK "LOCK"
32 #define DAV_MKCOL "MKCOL"
33 #define DAV_MOVE "MOVE"
34 #define DAV_PROPFIND "PROPFIND"
36 #define DAV_UNLOCK "UNLOCK"
37 #define DAV_DELETE "DELETE"
40 #define DAV_PROP_LOCKWR (1u << 0)
41 #define DAV_PROP_LOCKEX (1u << 1)
42 #define DAV_LOCK_OK (1u << 2)
44 /* DAV XML properties */
45 #define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
46 #define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
47 #define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
48 #define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
49 #define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
50 #define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
51 #define DAV_PROPFIND_RESP ".multistatus.response"
52 #define DAV_PROPFIND_NAME ".multistatus.response.href"
53 #define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
55 /* DAV request body templates */
56 #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>"
57 #define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
58 #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>"
61 #define LOCK_REFRESH 30
63 /* bits #0-15 in revision.h */
65 #define LOCAL (1u<<16)
66 #define REMOTE (1u<<17)
67 #define FETCHING (1u<<18)
68 #define PUSHING (1u<<19)
70 /* We allow "recursive" symbolic refs. Only within reason, though */
75 static signed char remote_dir_exists[256];
77 static int push_verbosely;
78 static int push_all = MATCH_REFS_NONE;
82 static struct object_list *objects;
90 int can_update_info_refs;
92 struct packed_git *packs;
93 struct remote_lock *locks;
96 static struct repo *repo;
110 struct transfer_request
115 struct remote_lock *lock;
116 struct curl_slist *headers;
117 struct buffer buffer;
118 enum transfer_state state;
119 CURLcode curl_result;
120 char errorstr[CURL_ERROR_SIZE];
123 struct active_request_slot *slot;
124 struct transfer_request *next;
127 static struct transfer_request *request_queue_head;
134 void (*userFunc)(struct xml_ctx *ctx, int tag_closed);
143 char tmpfile_suffix[41];
147 struct remote_lock *next;
150 /* Flags that control remote_ls processing */
151 #define PROCESS_FILES (1u << 0)
152 #define PROCESS_DIRS (1u << 1)
153 #define RECURSIVE (1u << 2)
155 /* Flags that remote_ls passes to callback functions */
156 #define IS_DIR (1u << 0)
161 void (*userFunc)(struct remote_ls_ctx *ls);
166 struct remote_ls_ctx *parent;
169 /* get_dav_token_headers options */
170 enum dav_header_flag {
171 DAV_HEADER_IF = (1u << 0),
172 DAV_HEADER_LOCK = (1u << 1),
173 DAV_HEADER_TIMEOUT = (1u << 2)
176 static char *xml_entities(char *s)
178 struct strbuf buf = STRBUF_INIT;
180 size_t len = strcspn(s, "\"<>&");
181 strbuf_add(&buf, s, len);
185 strbuf_addstr(&buf, """);
188 strbuf_addstr(&buf, "<");
191 strbuf_addstr(&buf, ">");
194 strbuf_addstr(&buf, "&");
199 return strbuf_detach(&buf, NULL);
202 static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum dav_header_flag options)
204 struct strbuf buf = STRBUF_INIT;
205 struct curl_slist *dav_headers = NULL;
207 if (options & DAV_HEADER_IF) {
208 strbuf_addf(&buf, "If: (<%s>)", lock->token);
209 dav_headers = curl_slist_append(dav_headers, buf.buf);
212 if (options & DAV_HEADER_LOCK) {
213 strbuf_addf(&buf, "Lock-Token: <%s>", lock->token);
214 dav_headers = curl_slist_append(dav_headers, buf.buf);
217 if (options & DAV_HEADER_TIMEOUT) {
218 strbuf_addf(&buf, "Timeout: Second-%ld", lock->timeout);
219 dav_headers = curl_slist_append(dav_headers, buf.buf);
222 strbuf_release(&buf);
227 static void finish_request(struct transfer_request *request);
228 static void release_request(struct transfer_request *request);
230 static void process_response(void *callback_data)
232 struct transfer_request *request =
233 (struct transfer_request *)callback_data;
235 finish_request(request);
238 #ifdef USE_CURL_MULTI
240 static void start_fetch_loose(struct transfer_request *request)
242 struct active_request_slot *slot;
243 struct http_object_request *obj_req;
245 obj_req = new_http_object_request(repo->url, request->obj->sha1);
246 if (obj_req == NULL) {
247 request->state = ABORTED;
251 slot = obj_req->slot;
252 slot->callback_func = process_response;
253 slot->callback_data = request;
254 request->slot = slot;
255 request->userData = obj_req;
257 /* Try to get the request started, abort the request on error */
258 request->state = RUN_FETCH_LOOSE;
259 if (!start_active_slot(slot)) {
260 fprintf(stderr, "Unable to start GET request\n");
261 repo->can_update_info_refs = 0;
262 release_http_object_request(obj_req);
263 release_request(request);
267 static void start_mkcol(struct transfer_request *request)
269 char *hex = sha1_to_hex(request->obj->sha1);
270 struct active_request_slot *slot;
272 request->url = get_remote_object_url(repo->url, hex, 1);
274 slot = get_active_slot();
275 slot->callback_func = process_response;
276 slot->callback_data = request;
277 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
278 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
279 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
280 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
281 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
283 if (start_active_slot(slot)) {
284 request->slot = slot;
285 request->state = RUN_MKCOL;
287 request->state = ABORTED;
294 static void start_fetch_packed(struct transfer_request *request)
296 struct packed_git *target;
298 struct transfer_request *check_request = request_queue_head;
299 struct http_pack_request *preq;
301 target = find_sha1_pack(request->obj->sha1, repo->packs);
303 fprintf(stderr, "Unable to fetch %s, will not be able to update server info refs\n", sha1_to_hex(request->obj->sha1));
304 repo->can_update_info_refs = 0;
305 release_request(request);
309 fprintf(stderr, "Fetching pack %s\n", sha1_to_hex(target->sha1));
310 fprintf(stderr, " which contains %s\n", sha1_to_hex(request->obj->sha1));
312 preq = new_http_pack_request(target, repo->url);
314 release_http_pack_request(preq);
315 repo->can_update_info_refs = 0;
318 preq->lst = &repo->packs;
320 /* Make sure there isn't another open request for this pack */
321 while (check_request) {
322 if (check_request->state == RUN_FETCH_PACKED &&
323 !strcmp(check_request->url, preq->url)) {
324 release_http_pack_request(preq);
325 release_request(request);
328 check_request = check_request->next;
331 preq->slot->callback_func = process_response;
332 preq->slot->callback_data = request;
333 request->slot = preq->slot;
334 request->userData = preq;
336 /* Try to get the request started, abort the request on error */
337 request->state = RUN_FETCH_PACKED;
338 if (!start_active_slot(preq->slot)) {
339 fprintf(stderr, "Unable to start GET request\n");
340 release_http_pack_request(preq);
341 repo->can_update_info_refs = 0;
342 release_request(request);
346 static void start_put(struct transfer_request *request)
348 char *hex = sha1_to_hex(request->obj->sha1);
349 struct active_request_slot *slot;
350 struct strbuf buf = STRBUF_INIT;
351 enum object_type type;
359 unpacked = read_sha1_file(request->obj->sha1, &type, &len);
360 hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
363 memset(&stream, 0, sizeof(stream));
364 deflateInit(&stream, zlib_compression_level);
365 size = deflateBound(&stream, len + hdrlen);
366 strbuf_init(&request->buffer.buf, size);
367 request->buffer.posn = 0;
370 stream.next_out = (unsigned char *)request->buffer.buf.buf;
371 stream.avail_out = size;
374 stream.next_in = (void *)hdr;
375 stream.avail_in = hdrlen;
376 while (deflate(&stream, 0) == Z_OK)
379 /* Then the data itself.. */
380 stream.next_in = unpacked;
381 stream.avail_in = len;
382 while (deflate(&stream, Z_FINISH) == Z_OK)
387 request->buffer.buf.len = stream.total_out;
389 strbuf_addstr(&buf, "Destination: ");
390 append_remote_object_url(&buf, repo->url, hex, 0);
391 request->dest = strbuf_detach(&buf, NULL);
393 append_remote_object_url(&buf, repo->url, hex, 0);
394 strbuf_add(&buf, request->lock->tmpfile_suffix, 41);
395 request->url = strbuf_detach(&buf, NULL);
397 slot = get_active_slot();
398 slot->callback_func = process_response;
399 slot->callback_data = request;
400 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &request->buffer);
401 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, request->buffer.buf.len);
402 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
403 #ifndef NO_CURL_IOCTL
404 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
405 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &request->buffer);
407 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
408 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
409 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
410 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
411 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
412 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
414 if (start_active_slot(slot)) {
415 request->slot = slot;
416 request->state = RUN_PUT;
418 request->state = ABORTED;
424 static void start_move(struct transfer_request *request)
426 struct active_request_slot *slot;
427 struct curl_slist *dav_headers = NULL;
429 slot = get_active_slot();
430 slot->callback_func = process_response;
431 slot->callback_data = request;
432 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
433 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MOVE);
434 dav_headers = curl_slist_append(dav_headers, request->dest);
435 dav_headers = curl_slist_append(dav_headers, "Overwrite: T");
436 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
437 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
438 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
440 if (start_active_slot(slot)) {
441 request->slot = slot;
442 request->state = RUN_MOVE;
444 request->state = ABORTED;
450 static int refresh_lock(struct remote_lock *lock)
452 struct active_request_slot *slot;
453 struct slot_results results;
454 struct curl_slist *dav_headers;
457 lock->refreshing = 1;
459 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF | DAV_HEADER_TIMEOUT);
461 slot = get_active_slot();
462 slot->results = &results;
463 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
464 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
465 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
466 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
467 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
469 if (start_active_slot(slot)) {
470 run_active_slot(slot);
471 if (results.curl_result != CURLE_OK) {
472 fprintf(stderr, "LOCK HTTP error %ld\n",
475 lock->start_time = time(NULL);
480 lock->refreshing = 0;
481 curl_slist_free_all(dav_headers);
486 static void check_locks(void)
488 struct remote_lock *lock = repo->locks;
489 time_t current_time = time(NULL);
493 time_remaining = lock->start_time + lock->timeout -
495 if (!lock->refreshing && time_remaining < LOCK_REFRESH) {
496 if (!refresh_lock(lock)) {
498 "Unable to refresh lock for %s\n",
508 static void release_request(struct transfer_request *request)
510 struct transfer_request *entry = request_queue_head;
512 if (request == request_queue_head) {
513 request_queue_head = request->next;
515 while (entry->next != NULL && entry->next != request)
517 if (entry->next == request)
518 entry->next = entry->next->next;
525 static void finish_request(struct transfer_request *request)
527 struct http_pack_request *preq;
528 struct http_object_request *obj_req;
530 request->curl_result = request->slot->curl_result;
531 request->http_code = request->slot->http_code;
532 request->slot = NULL;
534 /* Keep locks active */
537 if (request->headers != NULL)
538 curl_slist_free_all(request->headers);
540 /* URL is reused for MOVE after PUT */
541 if (request->state != RUN_PUT) {
546 if (request->state == RUN_MKCOL) {
547 if (request->curl_result == CURLE_OK ||
548 request->http_code == 405) {
549 remote_dir_exists[request->obj->sha1[0]] = 1;
552 fprintf(stderr, "MKCOL %s failed, aborting (%d/%ld)\n",
553 sha1_to_hex(request->obj->sha1),
554 request->curl_result, request->http_code);
555 request->state = ABORTED;
558 } else if (request->state == RUN_PUT) {
559 if (request->curl_result == CURLE_OK) {
562 fprintf(stderr, "PUT %s failed, aborting (%d/%ld)\n",
563 sha1_to_hex(request->obj->sha1),
564 request->curl_result, request->http_code);
565 request->state = ABORTED;
568 } else if (request->state == RUN_MOVE) {
569 if (request->curl_result == CURLE_OK) {
571 fprintf(stderr, " sent %s\n",
572 sha1_to_hex(request->obj->sha1));
573 request->obj->flags |= REMOTE;
574 release_request(request);
576 fprintf(stderr, "MOVE %s failed, aborting (%d/%ld)\n",
577 sha1_to_hex(request->obj->sha1),
578 request->curl_result, request->http_code);
579 request->state = ABORTED;
582 } else if (request->state == RUN_FETCH_LOOSE) {
583 obj_req = (struct http_object_request *)request->userData;
585 if (finish_http_object_request(obj_req) == 0)
586 if (obj_req->rename == 0)
587 request->obj->flags |= (LOCAL | REMOTE);
589 /* Try fetching packed if necessary */
590 if (request->obj->flags & LOCAL) {
591 release_http_object_request(obj_req);
592 release_request(request);
594 start_fetch_packed(request);
596 } else if (request->state == RUN_FETCH_PACKED) {
598 if (request->curl_result != CURLE_OK) {
599 fprintf(stderr, "Unable to get pack file %s\n%s",
600 request->url, curl_errorstr);
602 preq = (struct http_pack_request *)request->userData;
605 if (finish_http_pack_request(preq) > 0)
607 release_http_pack_request(preq);
611 repo->can_update_info_refs = 0;
612 release_request(request);
616 #ifdef USE_CURL_MULTI
617 static int is_running_queue;
618 static int fill_active_slot(void *unused)
620 struct transfer_request *request;
622 if (aborted || !is_running_queue)
625 for (request = request_queue_head; request; request = request->next) {
626 if (request->state == NEED_FETCH) {
627 start_fetch_loose(request);
629 } else if (pushing && request->state == NEED_PUSH) {
630 if (remote_dir_exists[request->obj->sha1[0]] == 1) {
633 start_mkcol(request);
642 static void get_remote_object_list(unsigned char parent);
644 static void add_fetch_request(struct object *obj)
646 struct transfer_request *request;
651 * Don't fetch the object if it's known to exist locally
652 * or is already in the request queue
654 if (remote_dir_exists[obj->sha1[0]] == -1)
655 get_remote_object_list(obj->sha1[0]);
656 if (obj->flags & (LOCAL | FETCHING))
659 obj->flags |= FETCHING;
660 request = xmalloc(sizeof(*request));
663 request->lock = NULL;
664 request->headers = NULL;
665 request->state = NEED_FETCH;
666 request->next = request_queue_head;
667 request_queue_head = request;
669 #ifdef USE_CURL_MULTI
675 static int add_send_request(struct object *obj, struct remote_lock *lock)
677 struct transfer_request *request = request_queue_head;
678 struct packed_git *target;
680 /* Keep locks active */
684 * Don't push the object if it's known to exist on the remote
685 * or is already in the request queue
687 if (remote_dir_exists[obj->sha1[0]] == -1)
688 get_remote_object_list(obj->sha1[0]);
689 if (obj->flags & (REMOTE | PUSHING))
691 target = find_sha1_pack(obj->sha1, repo->packs);
693 obj->flags |= REMOTE;
697 obj->flags |= PUSHING;
698 request = xmalloc(sizeof(*request));
701 request->lock = lock;
702 request->headers = NULL;
703 request->state = NEED_PUSH;
704 request->next = request_queue_head;
705 request_queue_head = request;
707 #ifdef USE_CURL_MULTI
715 static int fetch_indices(void)
720 fprintf(stderr, "Getting pack list\n");
722 switch (http_get_info_packs(repo->url, &repo->packs)) {
724 case HTTP_MISSING_TARGET:
734 static void one_remote_object(const char *hex)
736 unsigned char sha1[20];
739 if (get_sha1_hex(hex, sha1) != 0)
742 obj = lookup_object(sha1);
744 obj = parse_object(sha1);
746 /* Ignore remote objects that don't exist locally */
750 obj->flags |= REMOTE;
751 if (!object_list_contains(objects, obj))
752 object_list_insert(obj, &objects);
755 static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed)
757 int *lock_flags = (int *)ctx->userData;
760 if (!strcmp(ctx->name, DAV_CTX_LOCKENTRY)) {
761 if ((*lock_flags & DAV_PROP_LOCKEX) &&
762 (*lock_flags & DAV_PROP_LOCKWR)) {
763 *lock_flags |= DAV_LOCK_OK;
765 *lock_flags &= DAV_LOCK_OK;
766 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_WRITE)) {
767 *lock_flags |= DAV_PROP_LOCKWR;
768 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_EXCLUSIVE)) {
769 *lock_flags |= DAV_PROP_LOCKEX;
774 static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
776 struct remote_lock *lock = (struct remote_lock *)ctx->userData;
778 unsigned char lock_token_sha1[20];
780 if (tag_closed && ctx->cdata) {
781 if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) {
782 lock->owner = xmalloc(strlen(ctx->cdata) + 1);
783 strcpy(lock->owner, ctx->cdata);
784 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
785 if (!prefixcmp(ctx->cdata, "Second-"))
787 strtol(ctx->cdata + 7, NULL, 10);
788 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
789 lock->token = xmalloc(strlen(ctx->cdata) + 1);
790 strcpy(lock->token, ctx->cdata);
792 git_SHA1_Init(&sha_ctx);
793 git_SHA1_Update(&sha_ctx, lock->token, strlen(lock->token));
794 git_SHA1_Final(lock_token_sha1, &sha_ctx);
796 lock->tmpfile_suffix[0] = '_';
797 memcpy(lock->tmpfile_suffix + 1, sha1_to_hex(lock_token_sha1), 40);
802 static void one_remote_ref(char *refname);
805 xml_start_tag(void *userData, const char *name, const char **atts)
807 struct xml_ctx *ctx = (struct xml_ctx *)userData;
808 const char *c = strchr(name, ':');
816 new_len = strlen(ctx->name) + strlen(c) + 2;
818 if (new_len > ctx->len) {
819 ctx->name = xrealloc(ctx->name, new_len);
822 strcat(ctx->name, ".");
823 strcat(ctx->name, c);
828 ctx->userFunc(ctx, 0);
832 xml_end_tag(void *userData, const char *name)
834 struct xml_ctx *ctx = (struct xml_ctx *)userData;
835 const char *c = strchr(name, ':');
838 ctx->userFunc(ctx, 1);
845 ep = ctx->name + strlen(ctx->name) - strlen(c) - 1;
850 xml_cdata(void *userData, const XML_Char *s, int len)
852 struct xml_ctx *ctx = (struct xml_ctx *)userData;
854 ctx->cdata = xmemdupz(s, len);
857 static struct remote_lock *lock_remote(const char *path, long timeout)
859 struct active_request_slot *slot;
860 struct slot_results results;
861 struct buffer out_buffer = { STRBUF_INIT, 0 };
862 struct strbuf in_buffer = STRBUF_INIT;
865 char timeout_header[25];
866 struct remote_lock *lock = NULL;
867 struct curl_slist *dav_headers = NULL;
871 url = xmalloc(strlen(repo->url) + strlen(path) + 1);
872 sprintf(url, "%s%s", repo->url, path);
874 /* Make sure leading directories exist for the remote ref */
875 ep = strchr(url + strlen(repo->url) + 1, '/');
877 char saved_character = ep[1];
879 slot = get_active_slot();
880 slot->results = &results;
881 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
882 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
883 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
884 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
885 if (start_active_slot(slot)) {
886 run_active_slot(slot);
887 if (results.curl_result != CURLE_OK &&
888 results.http_code != 405) {
890 "Unable to create branch path %s\n",
896 fprintf(stderr, "Unable to start MKCOL request\n");
900 ep[1] = saved_character;
901 ep = strchr(ep + 1, '/');
904 escaped = xml_entities(git_default_email);
905 strbuf_addf(&out_buffer.buf, LOCK_REQUEST, escaped);
908 sprintf(timeout_header, "Timeout: Second-%ld", timeout);
909 dav_headers = curl_slist_append(dav_headers, timeout_header);
910 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
912 slot = get_active_slot();
913 slot->results = &results;
914 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
915 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
916 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
917 #ifndef NO_CURL_IOCTL
918 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
919 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
921 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
922 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
923 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
924 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
925 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
926 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
928 lock = xcalloc(1, sizeof(*lock));
931 if (start_active_slot(slot)) {
932 run_active_slot(slot);
933 if (results.curl_result == CURLE_OK) {
934 XML_Parser parser = XML_ParserCreate(NULL);
935 enum XML_Status result;
936 ctx.name = xcalloc(10, 1);
939 ctx.userFunc = handle_new_lock_ctx;
941 XML_SetUserData(parser, &ctx);
942 XML_SetElementHandler(parser, xml_start_tag,
944 XML_SetCharacterDataHandler(parser, xml_cdata);
945 result = XML_Parse(parser, in_buffer.buf,
948 if (result != XML_STATUS_OK) {
949 fprintf(stderr, "XML error: %s\n",
951 XML_GetErrorCode(parser)));
954 XML_ParserFree(parser);
957 fprintf(stderr, "Unable to start LOCK request\n");
960 curl_slist_free_all(dav_headers);
961 strbuf_release(&out_buffer.buf);
962 strbuf_release(&in_buffer);
964 if (lock->token == NULL || lock->timeout <= 0) {
972 lock->start_time = time(NULL);
973 lock->next = repo->locks;
980 static int unlock_remote(struct remote_lock *lock)
982 struct active_request_slot *slot;
983 struct slot_results results;
984 struct remote_lock *prev = repo->locks;
985 struct curl_slist *dav_headers;
988 dav_headers = get_dav_token_headers(lock, DAV_HEADER_LOCK);
990 slot = get_active_slot();
991 slot->results = &results;
992 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
993 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
994 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_UNLOCK);
995 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
997 if (start_active_slot(slot)) {
998 run_active_slot(slot);
999 if (results.curl_result == CURLE_OK)
1002 fprintf(stderr, "UNLOCK HTTP error %ld\n",
1005 fprintf(stderr, "Unable to start UNLOCK request\n");
1008 curl_slist_free_all(dav_headers);
1010 if (repo->locks == lock) {
1011 repo->locks = lock->next;
1013 while (prev && prev->next != lock)
1016 prev->next = prev->next->next;
1027 static void remove_locks(void)
1029 struct remote_lock *lock = repo->locks;
1031 fprintf(stderr, "Removing remote locks...\n");
1033 unlock_remote(lock);
1038 static void remove_locks_on_signal(int signo)
1041 sigchain_pop(signo);
1045 static void remote_ls(const char *path, int flags,
1046 void (*userFunc)(struct remote_ls_ctx *ls),
1049 static void process_ls_object(struct remote_ls_ctx *ls)
1051 unsigned int *parent = (unsigned int *)ls->userData;
1052 char *path = ls->dentry_name;
1055 if (!strcmp(ls->path, ls->dentry_name) && (ls->flags & IS_DIR)) {
1056 remote_dir_exists[*parent] = 1;
1060 if (strlen(path) != 49)
1063 obj_hex = xmalloc(strlen(path));
1064 /* NB: path is not null-terminated, can not use strlcpy here */
1065 memcpy(obj_hex, path, 2);
1066 strcpy(obj_hex + 2, path + 3);
1067 one_remote_object(obj_hex);
1071 static void process_ls_ref(struct remote_ls_ctx *ls)
1073 if (!strcmp(ls->path, ls->dentry_name) && (ls->dentry_flags & IS_DIR)) {
1074 fprintf(stderr, " %s\n", ls->dentry_name);
1078 if (!(ls->dentry_flags & IS_DIR))
1079 one_remote_ref(ls->dentry_name);
1082 static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
1084 struct remote_ls_ctx *ls = (struct remote_ls_ctx *)ctx->userData;
1087 if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) {
1088 if (ls->dentry_flags & IS_DIR) {
1089 if (ls->flags & PROCESS_DIRS) {
1092 if (strcmp(ls->dentry_name, ls->path) &&
1093 ls->flags & RECURSIVE) {
1094 remote_ls(ls->dentry_name,
1099 } else if (ls->flags & PROCESS_FILES) {
1102 } else if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
1103 char *path = ctx->cdata;
1104 if (*ctx->cdata == 'h') {
1105 path = strstr(path, "//");
1107 path = strchr(path+2, '/');
1111 path += repo->path_len;
1112 ls->dentry_name = xstrdup(path);
1114 } else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
1115 ls->dentry_flags |= IS_DIR;
1117 } else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
1118 free(ls->dentry_name);
1119 ls->dentry_name = NULL;
1120 ls->dentry_flags = 0;
1125 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1126 * should _only_ heed the information from that file, instead of trying to
1127 * determine the refs from the remote file system (badly: it does not even
1128 * know about packed-refs).
1130 static void remote_ls(const char *path, int flags,
1131 void (*userFunc)(struct remote_ls_ctx *ls),
1134 char *url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1135 struct active_request_slot *slot;
1136 struct slot_results results;
1137 struct strbuf in_buffer = STRBUF_INIT;
1138 struct buffer out_buffer = { STRBUF_INIT, 0 };
1139 struct curl_slist *dav_headers = NULL;
1141 struct remote_ls_ctx ls;
1144 ls.path = xstrdup(path);
1145 ls.dentry_name = NULL;
1146 ls.dentry_flags = 0;
1147 ls.userData = userData;
1148 ls.userFunc = userFunc;
1150 sprintf(url, "%s%s", repo->url, path);
1152 strbuf_addf(&out_buffer.buf, PROPFIND_ALL_REQUEST);
1154 dav_headers = curl_slist_append(dav_headers, "Depth: 1");
1155 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1157 slot = get_active_slot();
1158 slot->results = &results;
1159 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1160 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1161 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1162 #ifndef NO_CURL_IOCTL
1163 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1164 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
1166 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1167 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1168 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1169 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1170 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1171 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1173 if (start_active_slot(slot)) {
1174 run_active_slot(slot);
1175 if (results.curl_result == CURLE_OK) {
1176 XML_Parser parser = XML_ParserCreate(NULL);
1177 enum XML_Status result;
1178 ctx.name = xcalloc(10, 1);
1181 ctx.userFunc = handle_remote_ls_ctx;
1183 XML_SetUserData(parser, &ctx);
1184 XML_SetElementHandler(parser, xml_start_tag,
1186 XML_SetCharacterDataHandler(parser, xml_cdata);
1187 result = XML_Parse(parser, in_buffer.buf,
1191 if (result != XML_STATUS_OK) {
1192 fprintf(stderr, "XML error: %s\n",
1194 XML_GetErrorCode(parser)));
1196 XML_ParserFree(parser);
1199 fprintf(stderr, "Unable to start PROPFIND request\n");
1204 strbuf_release(&out_buffer.buf);
1205 strbuf_release(&in_buffer);
1206 curl_slist_free_all(dav_headers);
1209 static void get_remote_object_list(unsigned char parent)
1211 char path[] = "objects/XX/";
1212 static const char hex[] = "0123456789abcdef";
1213 unsigned int val = parent;
1215 path[8] = hex[val >> 4];
1216 path[9] = hex[val & 0xf];
1217 remote_dir_exists[val] = 0;
1218 remote_ls(path, (PROCESS_FILES | PROCESS_DIRS),
1219 process_ls_object, &val);
1222 static int locking_available(void)
1224 struct active_request_slot *slot;
1225 struct slot_results results;
1226 struct strbuf in_buffer = STRBUF_INIT;
1227 struct buffer out_buffer = { STRBUF_INIT, 0 };
1228 struct curl_slist *dav_headers = NULL;
1233 escaped = xml_entities(repo->url);
1234 strbuf_addf(&out_buffer.buf, PROPFIND_SUPPORTEDLOCK_REQUEST, escaped);
1237 dav_headers = curl_slist_append(dav_headers, "Depth: 0");
1238 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1240 slot = get_active_slot();
1241 slot->results = &results;
1242 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1243 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1244 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1245 #ifndef NO_CURL_IOCTL
1246 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1247 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
1249 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1250 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1251 curl_easy_setopt(slot->curl, CURLOPT_URL, repo->url);
1252 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1253 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1254 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1256 if (start_active_slot(slot)) {
1257 run_active_slot(slot);
1258 if (results.curl_result == CURLE_OK) {
1259 XML_Parser parser = XML_ParserCreate(NULL);
1260 enum XML_Status result;
1261 ctx.name = xcalloc(10, 1);
1264 ctx.userFunc = handle_lockprop_ctx;
1265 ctx.userData = &lock_flags;
1266 XML_SetUserData(parser, &ctx);
1267 XML_SetElementHandler(parser, xml_start_tag,
1269 result = XML_Parse(parser, in_buffer.buf,
1273 if (result != XML_STATUS_OK) {
1274 fprintf(stderr, "XML error: %s\n",
1276 XML_GetErrorCode(parser)));
1279 XML_ParserFree(parser);
1281 error("no DAV locking support on %s",
1285 error("Cannot access URL %s, return code %d",
1286 repo->url, results.curl_result);
1290 error("Unable to start PROPFIND request on %s", repo->url);
1293 strbuf_release(&out_buffer.buf);
1294 strbuf_release(&in_buffer);
1295 curl_slist_free_all(dav_headers);
1300 static struct object_list **add_one_object(struct object *obj, struct object_list **p)
1302 struct object_list *entry = xmalloc(sizeof(struct object_list));
1306 return &entry->next;
1309 static struct object_list **process_blob(struct blob *blob,
1310 struct object_list **p,
1311 struct name_path *path,
1314 struct object *obj = &blob->object;
1316 obj->flags |= LOCAL;
1318 if (obj->flags & (UNINTERESTING | SEEN))
1322 return add_one_object(obj, p);
1325 static struct object_list **process_tree(struct tree *tree,
1326 struct object_list **p,
1327 struct name_path *path,
1330 struct object *obj = &tree->object;
1331 struct tree_desc desc;
1332 struct name_entry entry;
1333 struct name_path me;
1335 obj->flags |= LOCAL;
1337 if (obj->flags & (UNINTERESTING | SEEN))
1339 if (parse_tree(tree) < 0)
1340 die("bad tree object %s", sha1_to_hex(obj->sha1));
1343 name = xstrdup(name);
1344 p = add_one_object(obj, p);
1347 me.elem_len = strlen(name);
1349 init_tree_desc(&desc, tree->buffer, tree->size);
1351 while (tree_entry(&desc, &entry))
1352 switch (object_type(entry.mode)) {
1354 p = process_tree(lookup_tree(entry.sha1), p, &me, name);
1357 p = process_blob(lookup_blob(entry.sha1), p, &me, name);
1360 /* Subproject commit - not in this repository */
1365 tree->buffer = NULL;
1369 static int get_delta(struct rev_info *revs, struct remote_lock *lock)
1372 struct commit *commit;
1373 struct object_list **p = &objects;
1376 while ((commit = get_revision(revs)) != NULL) {
1377 p = process_tree(commit->tree, p, NULL, "");
1378 commit->object.flags |= LOCAL;
1379 if (!(commit->object.flags & UNINTERESTING))
1380 count += add_send_request(&commit->object, lock);
1383 for (i = 0; i < revs->pending.nr; i++) {
1384 struct object_array_entry *entry = revs->pending.objects + i;
1385 struct object *obj = entry->item;
1386 const char *name = entry->name;
1388 if (obj->flags & (UNINTERESTING | SEEN))
1390 if (obj->type == OBJ_TAG) {
1392 p = add_one_object(obj, p);
1395 if (obj->type == OBJ_TREE) {
1396 p = process_tree((struct tree *)obj, p, NULL, name);
1399 if (obj->type == OBJ_BLOB) {
1400 p = process_blob((struct blob *)obj, p, NULL, name);
1403 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
1407 if (!(objects->item->flags & UNINTERESTING))
1408 count += add_send_request(objects->item, lock);
1409 objects = objects->next;
1415 static int update_remote(unsigned char *sha1, struct remote_lock *lock)
1417 struct active_request_slot *slot;
1418 struct slot_results results;
1419 struct buffer out_buffer = { STRBUF_INIT, 0 };
1420 struct curl_slist *dav_headers;
1422 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1424 strbuf_addf(&out_buffer.buf, "%s\n", sha1_to_hex(sha1));
1426 slot = get_active_slot();
1427 slot->results = &results;
1428 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1429 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1430 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1431 #ifndef NO_CURL_IOCTL
1432 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1433 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
1435 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1436 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1437 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1438 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1439 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1440 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1442 if (start_active_slot(slot)) {
1443 run_active_slot(slot);
1444 strbuf_release(&out_buffer.buf);
1445 if (results.curl_result != CURLE_OK) {
1447 "PUT error: curl result=%d, HTTP code=%ld\n",
1448 results.curl_result, results.http_code);
1449 /* We should attempt recovery? */
1453 strbuf_release(&out_buffer.buf);
1454 fprintf(stderr, "Unable to start PUT request\n");
1461 static struct ref *remote_refs, **remote_tail;
1463 static void one_remote_ref(char *refname)
1468 ref = alloc_ref(refname);
1470 if (http_fetch_ref(repo->url, ref) != 0) {
1472 "Unable to fetch ref %s from %s\n",
1473 refname, repo->url);
1479 * Fetch a copy of the object if it doesn't exist locally - it
1480 * may be required for updating server info later.
1482 if (repo->can_update_info_refs && !has_sha1_file(ref->old_sha1)) {
1483 obj = lookup_unknown_object(ref->old_sha1);
1485 fprintf(stderr, " fetch %s for %s\n",
1486 sha1_to_hex(ref->old_sha1), refname);
1487 add_fetch_request(obj);
1492 remote_tail = &ref->next;
1495 static void get_dav_remote_heads(void)
1497 remote_tail = &remote_refs;
1498 remote_ls("refs/", (PROCESS_FILES | PROCESS_DIRS | RECURSIVE), process_ls_ref, NULL);
1501 static int is_zero_sha1(const unsigned char *sha1)
1505 for (i = 0; i < 20; i++) {
1512 static void add_remote_info_ref(struct remote_ls_ctx *ls)
1514 struct strbuf *buf = (struct strbuf *)ls->userData;
1520 ref = alloc_ref(ls->dentry_name);
1522 if (http_fetch_ref(repo->url, ref) != 0) {
1524 "Unable to fetch ref %s from %s\n",
1525 ls->dentry_name, repo->url);
1531 o = parse_object(ref->old_sha1);
1534 "Unable to parse object %s for remote ref %s\n",
1535 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1541 len = strlen(ls->dentry_name) + 42;
1542 ref_info = xcalloc(len + 1, 1);
1543 sprintf(ref_info, "%s %s\n",
1544 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1545 fwrite_buffer(ref_info, 1, len, buf);
1548 if (o->type == OBJ_TAG) {
1549 o = deref_tag(o, ls->dentry_name, 0);
1551 len = strlen(ls->dentry_name) + 45;
1552 ref_info = xcalloc(len + 1, 1);
1553 sprintf(ref_info, "%s %s^{}\n",
1554 sha1_to_hex(o->sha1), ls->dentry_name);
1555 fwrite_buffer(ref_info, 1, len, buf);
1562 static void update_remote_info_refs(struct remote_lock *lock)
1564 struct buffer buffer = { STRBUF_INIT, 0 };
1565 struct active_request_slot *slot;
1566 struct slot_results results;
1567 struct curl_slist *dav_headers;
1569 remote_ls("refs/", (PROCESS_FILES | RECURSIVE),
1570 add_remote_info_ref, &buffer.buf);
1572 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1574 slot = get_active_slot();
1575 slot->results = &results;
1576 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &buffer);
1577 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, buffer.buf.len);
1578 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1579 #ifndef NO_CURL_IOCTL
1580 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1581 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &buffer);
1583 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1584 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1585 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1586 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1587 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1588 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1590 if (start_active_slot(slot)) {
1591 run_active_slot(slot);
1592 if (results.curl_result != CURLE_OK) {
1594 "PUT error: curl result=%d, HTTP code=%ld\n",
1595 results.curl_result, results.http_code);
1599 strbuf_release(&buffer.buf);
1602 static int remote_exists(const char *path)
1604 char *url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1607 sprintf(url, "%s%s", repo->url, path);
1609 switch (http_get_strbuf(url, NULL, 0)) {
1613 case HTTP_MISSING_TARGET:
1617 http_error(url, HTTP_ERROR);
1625 static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
1628 struct strbuf buffer = STRBUF_INIT;
1630 url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1631 sprintf(url, "%s%s", repo->url, path);
1633 if (http_get_strbuf(url, &buffer, 0) != HTTP_OK)
1634 die("Couldn't get %s for remote symref\n%s", url,
1642 if (buffer.len == 0)
1645 /* If it's a symref, set the refname; otherwise try for a sha1 */
1646 if (!prefixcmp((char *)buffer.buf, "ref: ")) {
1647 *symref = xmemdupz((char *)buffer.buf + 5, buffer.len - 6);
1649 get_sha1_hex(buffer.buf, sha1);
1652 strbuf_release(&buffer);
1655 static int verify_merge_base(unsigned char *head_sha1, unsigned char *branch_sha1)
1657 struct commit *head = lookup_commit(head_sha1);
1658 struct commit *branch = lookup_commit(branch_sha1);
1659 struct commit_list *merge_bases = get_merge_bases(head, branch, 1);
1661 return (merge_bases && !merge_bases->next && merge_bases->item == branch);
1664 static int delete_remote_branch(char *pattern, int force)
1666 struct ref *refs = remote_refs;
1667 struct ref *remote_ref = NULL;
1668 unsigned char head_sha1[20];
1669 char *symref = NULL;
1671 int patlen = strlen(pattern);
1673 struct active_request_slot *slot;
1674 struct slot_results results;
1677 /* Find the remote branch(es) matching the specified branch name */
1678 for (match = 0; refs; refs = refs->next) {
1679 char *name = refs->name;
1680 int namelen = strlen(name);
1681 if (namelen < patlen ||
1682 memcmp(name + namelen - patlen, pattern, patlen))
1684 if (namelen != patlen && name[namelen - patlen - 1] != '/')
1690 return error("No remote branch matches %s", pattern);
1692 return error("More than one remote branch matches %s",
1696 * Remote HEAD must be a symref (not exactly foolproof; a remote
1697 * symlink to a symref will look like a symref)
1699 fetch_symref("HEAD", &symref, head_sha1);
1701 return error("Remote HEAD is not a symref");
1703 /* Remote branch must not be the remote HEAD */
1704 for (i=0; symref && i<MAXDEPTH; i++) {
1705 if (!strcmp(remote_ref->name, symref))
1706 return error("Remote branch %s is the current HEAD",
1708 fetch_symref(symref, &symref, head_sha1);
1711 /* Run extra sanity checks if delete is not forced */
1713 /* Remote HEAD must resolve to a known object */
1715 return error("Remote HEAD symrefs too deep");
1716 if (is_zero_sha1(head_sha1))
1717 return error("Unable to resolve remote HEAD");
1718 if (!has_sha1_file(head_sha1))
1719 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1));
1721 /* Remote branch must resolve to a known object */
1722 if (is_zero_sha1(remote_ref->old_sha1))
1723 return error("Unable to resolve remote branch %s",
1725 if (!has_sha1_file(remote_ref->old_sha1))
1726 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));
1728 /* Remote branch must be an ancestor of remote HEAD */
1729 if (!verify_merge_base(head_sha1, remote_ref->old_sha1)) {
1730 return error("The branch '%s' is not an ancestor "
1731 "of your current HEAD.\n"
1732 "If you are sure you want to delete it,"
1733 " run:\n\t'git http-push -D %s %s'",
1734 remote_ref->name, repo->url, pattern);
1738 /* Send delete request */
1739 fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name);
1742 url = xmalloc(strlen(repo->url) + strlen(remote_ref->name) + 1);
1743 sprintf(url, "%s%s", repo->url, remote_ref->name);
1744 slot = get_active_slot();
1745 slot->results = &results;
1746 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
1747 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1748 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1749 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_DELETE);
1750 if (start_active_slot(slot)) {
1751 run_active_slot(slot);
1753 if (results.curl_result != CURLE_OK)
1754 return error("DELETE request failed (%d/%ld)\n",
1755 results.curl_result, results.http_code);
1758 return error("Unable to start DELETE request");
1764 void run_request_queue(void)
1766 #ifdef USE_CURL_MULTI
1767 is_running_queue = 1;
1768 fill_active_slots();
1769 add_fill_function(NULL, fill_active_slot);
1772 finish_all_active_slots();
1773 #ifdef USE_CURL_MULTI
1774 fill_active_slots();
1776 } while (request_queue_head && !aborted);
1778 #ifdef USE_CURL_MULTI
1779 is_running_queue = 0;
1783 int main(int argc, char **argv)
1785 struct transfer_request *request;
1786 struct transfer_request *next_request;
1788 char **refspec = NULL;
1789 struct remote_lock *ref_lock = NULL;
1790 struct remote_lock *info_ref_lock = NULL;
1791 struct rev_info revs;
1792 int delete_branch = 0;
1793 int force_delete = 0;
1794 int objects_to_send;
1798 struct ref *ref, *local_refs;
1799 struct remote *remote;
1800 char *rewritten_url = NULL;
1802 git_extract_argv0_path(argv[0]);
1804 setup_git_directory();
1806 repo = xcalloc(sizeof(*repo), 1);
1809 for (i = 1; i < argc; i++, argv++) {
1813 if (!strcmp(arg, "--all")) {
1814 push_all = MATCH_REFS_ALL;
1817 if (!strcmp(arg, "--force")) {
1821 if (!strcmp(arg, "--dry-run")) {
1825 if (!strcmp(arg, "--verbose")) {
1827 http_is_verbose = 1;
1830 if (!strcmp(arg, "-d")) {
1834 if (!strcmp(arg, "-D")) {
1841 char *path = strstr(arg, "//");
1843 repo->path_len = strlen(arg);
1845 repo->path = strchr(path+2, '/');
1847 repo->path_len = strlen(repo->path);
1852 nr_refspec = argc - i;
1856 #ifndef USE_CURL_MULTI
1857 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
1861 usage(http_push_usage);
1863 if (delete_branch && nr_refspec != 1)
1864 die("You must specify only one branch name when deleting a remote branch");
1866 memset(remote_dir_exists, -1, 256);
1869 * Create a minimum remote by hand to give to http_init(),
1870 * primarily to allow it to look at the URL.
1872 remote = xcalloc(sizeof(*remote), 1);
1873 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
1874 remote->url[remote->url_nr++] = repo->url;
1877 if (repo->url && repo->url[strlen(repo->url)-1] != '/') {
1878 rewritten_url = xmalloc(strlen(repo->url)+2);
1879 strcpy(rewritten_url, repo->url);
1880 strcat(rewritten_url, "/");
1881 repo->path = rewritten_url + (repo->path - repo->url);
1883 repo->url = rewritten_url;
1886 #ifdef USE_CURL_MULTI
1887 is_running_queue = 0;
1890 /* Verify DAV compliance/lock support */
1891 if (!locking_available()) {
1896 sigchain_push_common(remove_locks_on_signal);
1898 /* Check whether the remote has server info files */
1899 repo->can_update_info_refs = 0;
1900 repo->has_info_refs = remote_exists("info/refs");
1901 repo->has_info_packs = remote_exists("objects/info/packs");
1902 if (repo->has_info_refs) {
1903 info_ref_lock = lock_remote("info/refs", LOCK_TIME);
1905 repo->can_update_info_refs = 1;
1907 error("cannot lock existing info/refs");
1912 if (repo->has_info_packs)
1915 /* Get a list of all local and remote heads to validate refspecs */
1916 local_refs = get_local_heads();
1917 fprintf(stderr, "Fetching remote heads...\n");
1918 get_dav_remote_heads();
1919 run_request_queue();
1921 /* Remove a remote branch if -d or -D was specified */
1922 if (delete_branch) {
1923 if (delete_remote_branch(refspec[0], force_delete) == -1)
1924 fprintf(stderr, "Unable to delete remote branch %s\n",
1931 remote_tail = &remote_refs;
1932 if (match_refs(local_refs, remote_refs, &remote_tail,
1933 nr_refspec, (const char **) refspec, push_all)) {
1938 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
1944 for (ref = remote_refs; ref; ref = ref->next) {
1945 char old_hex[60], *new_hex;
1946 const char *commit_argv[4];
1948 char *new_sha1_hex, *old_sha1_hex;
1953 if (is_zero_sha1(ref->peer_ref->new_sha1)) {
1954 if (delete_remote_branch(ref->name, 1) == -1) {
1955 error("Could not remove %s", ref->name);
1962 if (!hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
1963 if (push_verbosely || 1)
1964 fprintf(stderr, "'%s': up-to-date\n", ref->name);
1969 !is_zero_sha1(ref->old_sha1) &&
1971 if (!has_sha1_file(ref->old_sha1) ||
1972 !ref_newer(ref->peer_ref->new_sha1,
1975 * We do not have the remote ref, or
1976 * we know that the remote ref is not
1977 * an ancestor of what we are trying to
1978 * push. Either way this can be losing
1979 * commits at the remote end and likely
1980 * we were not up to date to begin with.
1982 error("remote '%s' is not an ancestor of\n"
1984 "Maybe you are not up-to-date and "
1985 "need to pull first?",
1987 ref->peer_ref->name);
1992 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
1994 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
1995 new_hex = sha1_to_hex(ref->new_sha1);
1997 fprintf(stderr, "updating '%s'", ref->name);
1998 if (strcmp(ref->name, ref->peer_ref->name))
1999 fprintf(stderr, " using '%s'", ref->peer_ref->name);
2000 fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex);
2004 /* Lock remote branch ref */
2005 ref_lock = lock_remote(ref->name, LOCK_TIME);
2006 if (ref_lock == NULL) {
2007 fprintf(stderr, "Unable to lock remote branch %s\n",
2013 /* Set up revision info for this refspec */
2015 new_sha1_hex = xstrdup(sha1_to_hex(ref->new_sha1));
2016 old_sha1_hex = NULL;
2017 commit_argv[1] = "--objects";
2018 commit_argv[2] = new_sha1_hex;
2019 if (!push_all && !is_zero_sha1(ref->old_sha1)) {
2020 old_sha1_hex = xmalloc(42);
2021 sprintf(old_sha1_hex, "^%s",
2022 sha1_to_hex(ref->old_sha1));
2023 commit_argv[3] = old_sha1_hex;
2026 init_revisions(&revs, setup_git_directory());
2027 setup_revisions(commit_argc, commit_argv, &revs, NULL);
2028 revs.edge_hint = 0; /* just in case */
2032 commit_argv[1] = NULL;
2035 /* Generate a list of objects that need to be pushed */
2037 if (prepare_revision_walk(&revs))
2038 die("revision walk setup failed");
2039 mark_edges_uninteresting(revs.commits, &revs, NULL);
2040 objects_to_send = get_delta(&revs, ref_lock);
2041 finish_all_active_slots();
2043 /* Push missing objects to remote, this would be a
2044 convenient time to pack them first if appropriate. */
2046 if (objects_to_send)
2047 fprintf(stderr, " sending %d objects\n",
2050 run_request_queue();
2052 /* Update the remote branch if all went well */
2053 if (aborted || !update_remote(ref->new_sha1, ref_lock))
2057 fprintf(stderr, " done\n");
2058 unlock_remote(ref_lock);
2062 /* Update remote server info if appropriate */
2063 if (repo->has_info_refs && new_refs) {
2064 if (info_ref_lock && repo->can_update_info_refs) {
2065 fprintf(stderr, "Updating remote server info\n");
2067 update_remote_info_refs(info_ref_lock);
2069 fprintf(stderr, "Unable to update server info\n");
2074 free(rewritten_url);
2076 unlock_remote(info_ref_lock);
2081 request = request_queue_head;
2082 while (request != NULL) {
2083 next_request = request->next;
2084 release_request(request);
2085 request = next_request;