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, "&");
197 return strbuf_detach(&buf, NULL);
201 return strbuf_detach(&buf, NULL);
204 static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum dav_header_flag options)
206 struct strbuf buf = STRBUF_INIT;
207 struct curl_slist *dav_headers = NULL;
209 if (options & DAV_HEADER_IF) {
210 strbuf_addf(&buf, "If: (<%s>)", lock->token);
211 dav_headers = curl_slist_append(dav_headers, buf.buf);
214 if (options & DAV_HEADER_LOCK) {
215 strbuf_addf(&buf, "Lock-Token: <%s>", lock->token);
216 dav_headers = curl_slist_append(dav_headers, buf.buf);
219 if (options & DAV_HEADER_TIMEOUT) {
220 strbuf_addf(&buf, "Timeout: Second-%ld", lock->timeout);
221 dav_headers = curl_slist_append(dav_headers, buf.buf);
224 strbuf_release(&buf);
229 static void finish_request(struct transfer_request *request);
230 static void release_request(struct transfer_request *request);
232 static void process_response(void *callback_data)
234 struct transfer_request *request =
235 (struct transfer_request *)callback_data;
237 finish_request(request);
240 #ifdef USE_CURL_MULTI
242 static void start_fetch_loose(struct transfer_request *request)
244 struct active_request_slot *slot;
245 struct http_object_request *obj_req;
247 obj_req = new_http_object_request(repo->url, request->obj->sha1);
248 if (obj_req == NULL) {
249 request->state = ABORTED;
253 slot = obj_req->slot;
254 slot->callback_func = process_response;
255 slot->callback_data = request;
256 request->slot = slot;
257 request->userData = obj_req;
259 /* Try to get the request started, abort the request on error */
260 request->state = RUN_FETCH_LOOSE;
261 if (!start_active_slot(slot)) {
262 fprintf(stderr, "Unable to start GET request\n");
263 repo->can_update_info_refs = 0;
264 release_http_object_request(obj_req);
265 release_request(request);
269 static void start_mkcol(struct transfer_request *request)
271 char *hex = sha1_to_hex(request->obj->sha1);
272 struct active_request_slot *slot;
274 request->url = get_remote_object_url(repo->url, hex, 1);
276 slot = get_active_slot();
277 slot->callback_func = process_response;
278 slot->callback_data = request;
279 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
280 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
281 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
282 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
283 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
285 if (start_active_slot(slot)) {
286 request->slot = slot;
287 request->state = RUN_MKCOL;
289 request->state = ABORTED;
296 static void start_fetch_packed(struct transfer_request *request)
298 struct packed_git *target;
300 struct transfer_request *check_request = request_queue_head;
301 struct http_pack_request *preq;
303 target = find_sha1_pack(request->obj->sha1, repo->packs);
305 fprintf(stderr, "Unable to fetch %s, will not be able to update server info refs\n", sha1_to_hex(request->obj->sha1));
306 repo->can_update_info_refs = 0;
307 release_request(request);
311 fprintf(stderr, "Fetching pack %s\n", sha1_to_hex(target->sha1));
312 fprintf(stderr, " which contains %s\n", sha1_to_hex(request->obj->sha1));
314 preq = new_http_pack_request(target, repo->url);
316 release_http_pack_request(preq);
317 repo->can_update_info_refs = 0;
320 preq->lst = &repo->packs;
322 /* Make sure there isn't another open request for this pack */
323 while (check_request) {
324 if (check_request->state == RUN_FETCH_PACKED &&
325 !strcmp(check_request->url, preq->url)) {
326 release_http_pack_request(preq);
327 release_request(request);
330 check_request = check_request->next;
333 preq->slot->callback_func = process_response;
334 preq->slot->callback_data = request;
335 request->slot = preq->slot;
336 request->userData = preq;
338 /* Try to get the request started, abort the request on error */
339 request->state = RUN_FETCH_PACKED;
340 if (!start_active_slot(preq->slot)) {
341 fprintf(stderr, "Unable to start GET request\n");
342 release_http_pack_request(preq);
343 repo->can_update_info_refs = 0;
344 release_request(request);
348 static void start_put(struct transfer_request *request)
350 char *hex = sha1_to_hex(request->obj->sha1);
351 struct active_request_slot *slot;
352 struct strbuf buf = STRBUF_INIT;
353 enum object_type type;
361 unpacked = read_sha1_file(request->obj->sha1, &type, &len);
362 hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
365 memset(&stream, 0, sizeof(stream));
366 deflateInit(&stream, zlib_compression_level);
367 size = deflateBound(&stream, len + hdrlen);
368 strbuf_init(&request->buffer.buf, size);
369 request->buffer.posn = 0;
372 stream.next_out = (unsigned char *)request->buffer.buf.buf;
373 stream.avail_out = size;
376 stream.next_in = (void *)hdr;
377 stream.avail_in = hdrlen;
378 while (deflate(&stream, 0) == Z_OK)
381 /* Then the data itself.. */
382 stream.next_in = unpacked;
383 stream.avail_in = len;
384 while (deflate(&stream, Z_FINISH) == Z_OK)
389 request->buffer.buf.len = stream.total_out;
391 strbuf_addstr(&buf, "Destination: ");
392 append_remote_object_url(&buf, repo->url, hex, 0);
393 request->dest = strbuf_detach(&buf, NULL);
395 append_remote_object_url(&buf, repo->url, hex, 0);
396 strbuf_add(&buf, request->lock->tmpfile_suffix, 41);
397 request->url = strbuf_detach(&buf, NULL);
399 slot = get_active_slot();
400 slot->callback_func = process_response;
401 slot->callback_data = request;
402 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &request->buffer);
403 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, request->buffer.buf.len);
404 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
405 #ifndef NO_CURL_IOCTL
406 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
407 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &request->buffer);
409 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
410 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
411 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
412 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
413 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
414 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
416 if (start_active_slot(slot)) {
417 request->slot = slot;
418 request->state = RUN_PUT;
420 request->state = ABORTED;
426 static void start_move(struct transfer_request *request)
428 struct active_request_slot *slot;
429 struct curl_slist *dav_headers = NULL;
431 slot = get_active_slot();
432 slot->callback_func = process_response;
433 slot->callback_data = request;
434 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
435 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MOVE);
436 dav_headers = curl_slist_append(dav_headers, request->dest);
437 dav_headers = curl_slist_append(dav_headers, "Overwrite: T");
438 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
439 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
440 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
442 if (start_active_slot(slot)) {
443 request->slot = slot;
444 request->state = RUN_MOVE;
446 request->state = ABORTED;
452 static int refresh_lock(struct remote_lock *lock)
454 struct active_request_slot *slot;
455 struct slot_results results;
456 struct curl_slist *dav_headers;
459 lock->refreshing = 1;
461 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF | DAV_HEADER_TIMEOUT);
463 slot = get_active_slot();
464 slot->results = &results;
465 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
466 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
467 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
468 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
469 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
471 if (start_active_slot(slot)) {
472 run_active_slot(slot);
473 if (results.curl_result != CURLE_OK) {
474 fprintf(stderr, "LOCK HTTP error %ld\n",
477 lock->start_time = time(NULL);
482 lock->refreshing = 0;
483 curl_slist_free_all(dav_headers);
488 static void check_locks(void)
490 struct remote_lock *lock = repo->locks;
491 time_t current_time = time(NULL);
495 time_remaining = lock->start_time + lock->timeout -
497 if (!lock->refreshing && time_remaining < LOCK_REFRESH) {
498 if (!refresh_lock(lock)) {
500 "Unable to refresh lock for %s\n",
510 static void release_request(struct transfer_request *request)
512 struct transfer_request *entry = request_queue_head;
514 if (request == request_queue_head) {
515 request_queue_head = request->next;
517 while (entry->next != NULL && entry->next != request)
519 if (entry->next == request)
520 entry->next = entry->next->next;
527 static void finish_request(struct transfer_request *request)
529 struct http_pack_request *preq;
530 struct http_object_request *obj_req;
532 request->curl_result = request->slot->curl_result;
533 request->http_code = request->slot->http_code;
534 request->slot = NULL;
536 /* Keep locks active */
539 if (request->headers != NULL)
540 curl_slist_free_all(request->headers);
542 /* URL is reused for MOVE after PUT */
543 if (request->state != RUN_PUT) {
548 if (request->state == RUN_MKCOL) {
549 if (request->curl_result == CURLE_OK ||
550 request->http_code == 405) {
551 remote_dir_exists[request->obj->sha1[0]] = 1;
554 fprintf(stderr, "MKCOL %s failed, aborting (%d/%ld)\n",
555 sha1_to_hex(request->obj->sha1),
556 request->curl_result, request->http_code);
557 request->state = ABORTED;
560 } else if (request->state == RUN_PUT) {
561 if (request->curl_result == CURLE_OK) {
564 fprintf(stderr, "PUT %s failed, aborting (%d/%ld)\n",
565 sha1_to_hex(request->obj->sha1),
566 request->curl_result, request->http_code);
567 request->state = ABORTED;
570 } else if (request->state == RUN_MOVE) {
571 if (request->curl_result == CURLE_OK) {
573 fprintf(stderr, " sent %s\n",
574 sha1_to_hex(request->obj->sha1));
575 request->obj->flags |= REMOTE;
576 release_request(request);
578 fprintf(stderr, "MOVE %s failed, aborting (%d/%ld)\n",
579 sha1_to_hex(request->obj->sha1),
580 request->curl_result, request->http_code);
581 request->state = ABORTED;
584 } else if (request->state == RUN_FETCH_LOOSE) {
585 obj_req = (struct http_object_request *)request->userData;
587 if (finish_http_object_request(obj_req) == 0)
588 if (obj_req->rename == 0)
589 request->obj->flags |= (LOCAL | REMOTE);
591 /* Try fetching packed if necessary */
592 if (request->obj->flags & LOCAL) {
593 release_http_object_request(obj_req);
594 release_request(request);
596 start_fetch_packed(request);
598 } else if (request->state == RUN_FETCH_PACKED) {
600 if (request->curl_result != CURLE_OK) {
601 fprintf(stderr, "Unable to get pack file %s\n%s",
602 request->url, curl_errorstr);
604 preq = (struct http_pack_request *)request->userData;
607 if (finish_http_pack_request(preq) > 0)
609 release_http_pack_request(preq);
613 repo->can_update_info_refs = 0;
614 release_request(request);
618 #ifdef USE_CURL_MULTI
619 static int is_running_queue;
620 static int fill_active_slot(void *unused)
622 struct transfer_request *request;
624 if (aborted || !is_running_queue)
627 for (request = request_queue_head; request; request = request->next) {
628 if (request->state == NEED_FETCH) {
629 start_fetch_loose(request);
631 } else if (pushing && request->state == NEED_PUSH) {
632 if (remote_dir_exists[request->obj->sha1[0]] == 1) {
635 start_mkcol(request);
644 static void get_remote_object_list(unsigned char parent);
646 static void add_fetch_request(struct object *obj)
648 struct transfer_request *request;
653 * Don't fetch the object if it's known to exist locally
654 * or is already in the request queue
656 if (remote_dir_exists[obj->sha1[0]] == -1)
657 get_remote_object_list(obj->sha1[0]);
658 if (obj->flags & (LOCAL | FETCHING))
661 obj->flags |= FETCHING;
662 request = xmalloc(sizeof(*request));
665 request->lock = NULL;
666 request->headers = NULL;
667 request->state = NEED_FETCH;
668 request->next = request_queue_head;
669 request_queue_head = request;
671 #ifdef USE_CURL_MULTI
677 static int add_send_request(struct object *obj, struct remote_lock *lock)
679 struct transfer_request *request = request_queue_head;
680 struct packed_git *target;
682 /* Keep locks active */
686 * Don't push the object if it's known to exist on the remote
687 * or is already in the request queue
689 if (remote_dir_exists[obj->sha1[0]] == -1)
690 get_remote_object_list(obj->sha1[0]);
691 if (obj->flags & (REMOTE | PUSHING))
693 target = find_sha1_pack(obj->sha1, repo->packs);
695 obj->flags |= REMOTE;
699 obj->flags |= PUSHING;
700 request = xmalloc(sizeof(*request));
703 request->lock = lock;
704 request->headers = NULL;
705 request->state = NEED_PUSH;
706 request->next = request_queue_head;
707 request_queue_head = request;
709 #ifdef USE_CURL_MULTI
717 static int fetch_indices(void)
722 fprintf(stderr, "Getting pack list\n");
724 switch (http_get_info_packs(repo->url, &repo->packs)) {
726 case HTTP_MISSING_TARGET:
736 static void one_remote_object(const char *hex)
738 unsigned char sha1[20];
741 if (get_sha1_hex(hex, sha1) != 0)
744 obj = lookup_object(sha1);
746 obj = parse_object(sha1);
748 /* Ignore remote objects that don't exist locally */
752 obj->flags |= REMOTE;
753 if (!object_list_contains(objects, obj))
754 object_list_insert(obj, &objects);
757 static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed)
759 int *lock_flags = (int *)ctx->userData;
762 if (!strcmp(ctx->name, DAV_CTX_LOCKENTRY)) {
763 if ((*lock_flags & DAV_PROP_LOCKEX) &&
764 (*lock_flags & DAV_PROP_LOCKWR)) {
765 *lock_flags |= DAV_LOCK_OK;
767 *lock_flags &= DAV_LOCK_OK;
768 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_WRITE)) {
769 *lock_flags |= DAV_PROP_LOCKWR;
770 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_EXCLUSIVE)) {
771 *lock_flags |= DAV_PROP_LOCKEX;
776 static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
778 struct remote_lock *lock = (struct remote_lock *)ctx->userData;
780 unsigned char lock_token_sha1[20];
782 if (tag_closed && ctx->cdata) {
783 if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) {
784 lock->owner = xmalloc(strlen(ctx->cdata) + 1);
785 strcpy(lock->owner, ctx->cdata);
786 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
787 if (!prefixcmp(ctx->cdata, "Second-"))
789 strtol(ctx->cdata + 7, NULL, 10);
790 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
791 lock->token = xmalloc(strlen(ctx->cdata) + 1);
792 strcpy(lock->token, ctx->cdata);
794 git_SHA1_Init(&sha_ctx);
795 git_SHA1_Update(&sha_ctx, lock->token, strlen(lock->token));
796 git_SHA1_Final(lock_token_sha1, &sha_ctx);
798 lock->tmpfile_suffix[0] = '_';
799 memcpy(lock->tmpfile_suffix + 1, sha1_to_hex(lock_token_sha1), 40);
804 static void one_remote_ref(char *refname);
807 xml_start_tag(void *userData, const char *name, const char **atts)
809 struct xml_ctx *ctx = (struct xml_ctx *)userData;
810 const char *c = strchr(name, ':');
818 new_len = strlen(ctx->name) + strlen(c) + 2;
820 if (new_len > ctx->len) {
821 ctx->name = xrealloc(ctx->name, new_len);
824 strcat(ctx->name, ".");
825 strcat(ctx->name, c);
830 ctx->userFunc(ctx, 0);
834 xml_end_tag(void *userData, const char *name)
836 struct xml_ctx *ctx = (struct xml_ctx *)userData;
837 const char *c = strchr(name, ':');
840 ctx->userFunc(ctx, 1);
847 ep = ctx->name + strlen(ctx->name) - strlen(c) - 1;
852 xml_cdata(void *userData, const XML_Char *s, int len)
854 struct xml_ctx *ctx = (struct xml_ctx *)userData;
856 ctx->cdata = xmemdupz(s, len);
859 static struct remote_lock *lock_remote(const char *path, long timeout)
861 struct active_request_slot *slot;
862 struct slot_results results;
863 struct buffer out_buffer = { STRBUF_INIT, 0 };
864 struct strbuf in_buffer = STRBUF_INIT;
867 char timeout_header[25];
868 struct remote_lock *lock = NULL;
869 struct curl_slist *dav_headers = NULL;
873 url = xmalloc(strlen(repo->url) + strlen(path) + 1);
874 sprintf(url, "%s%s", repo->url, path);
876 /* Make sure leading directories exist for the remote ref */
877 ep = strchr(url + strlen(repo->url) + 1, '/');
879 char saved_character = ep[1];
881 slot = get_active_slot();
882 slot->results = &results;
883 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
884 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
885 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
886 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
887 if (start_active_slot(slot)) {
888 run_active_slot(slot);
889 if (results.curl_result != CURLE_OK &&
890 results.http_code != 405) {
892 "Unable to create branch path %s\n",
898 fprintf(stderr, "Unable to start MKCOL request\n");
902 ep[1] = saved_character;
903 ep = strchr(ep + 1, '/');
906 escaped = xml_entities(git_default_email);
907 strbuf_addf(&out_buffer.buf, LOCK_REQUEST, escaped);
910 sprintf(timeout_header, "Timeout: Second-%ld", timeout);
911 dav_headers = curl_slist_append(dav_headers, timeout_header);
912 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
914 slot = get_active_slot();
915 slot->results = &results;
916 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
917 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
918 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
919 #ifndef NO_CURL_IOCTL
920 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
921 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
923 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
924 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
925 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
926 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
927 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
928 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
930 lock = xcalloc(1, sizeof(*lock));
933 if (start_active_slot(slot)) {
934 run_active_slot(slot);
935 if (results.curl_result == CURLE_OK) {
936 XML_Parser parser = XML_ParserCreate(NULL);
937 enum XML_Status result;
938 ctx.name = xcalloc(10, 1);
941 ctx.userFunc = handle_new_lock_ctx;
943 XML_SetUserData(parser, &ctx);
944 XML_SetElementHandler(parser, xml_start_tag,
946 XML_SetCharacterDataHandler(parser, xml_cdata);
947 result = XML_Parse(parser, in_buffer.buf,
950 if (result != XML_STATUS_OK) {
951 fprintf(stderr, "XML error: %s\n",
953 XML_GetErrorCode(parser)));
956 XML_ParserFree(parser);
959 fprintf(stderr, "Unable to start LOCK request\n");
962 curl_slist_free_all(dav_headers);
963 strbuf_release(&out_buffer.buf);
964 strbuf_release(&in_buffer);
966 if (lock->token == NULL || lock->timeout <= 0) {
974 lock->start_time = time(NULL);
975 lock->next = repo->locks;
982 static int unlock_remote(struct remote_lock *lock)
984 struct active_request_slot *slot;
985 struct slot_results results;
986 struct remote_lock *prev = repo->locks;
987 struct curl_slist *dav_headers;
990 dav_headers = get_dav_token_headers(lock, DAV_HEADER_LOCK);
992 slot = get_active_slot();
993 slot->results = &results;
994 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
995 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
996 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_UNLOCK);
997 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
999 if (start_active_slot(slot)) {
1000 run_active_slot(slot);
1001 if (results.curl_result == CURLE_OK)
1004 fprintf(stderr, "UNLOCK HTTP error %ld\n",
1007 fprintf(stderr, "Unable to start UNLOCK request\n");
1010 curl_slist_free_all(dav_headers);
1012 if (repo->locks == lock) {
1013 repo->locks = lock->next;
1015 while (prev && prev->next != lock)
1018 prev->next = prev->next->next;
1029 static void remove_locks(void)
1031 struct remote_lock *lock = repo->locks;
1033 fprintf(stderr, "Removing remote locks...\n");
1035 struct remote_lock *next = lock->next;
1036 unlock_remote(lock);
1041 static void remove_locks_on_signal(int signo)
1044 sigchain_pop(signo);
1048 static void remote_ls(const char *path, int flags,
1049 void (*userFunc)(struct remote_ls_ctx *ls),
1052 static void process_ls_object(struct remote_ls_ctx *ls)
1054 unsigned int *parent = (unsigned int *)ls->userData;
1055 char *path = ls->dentry_name;
1058 if (!strcmp(ls->path, ls->dentry_name) && (ls->flags & IS_DIR)) {
1059 remote_dir_exists[*parent] = 1;
1063 if (strlen(path) != 49)
1066 obj_hex = xmalloc(strlen(path));
1067 /* NB: path is not null-terminated, can not use strlcpy here */
1068 memcpy(obj_hex, path, 2);
1069 strcpy(obj_hex + 2, path + 3);
1070 one_remote_object(obj_hex);
1074 static void process_ls_ref(struct remote_ls_ctx *ls)
1076 if (!strcmp(ls->path, ls->dentry_name) && (ls->dentry_flags & IS_DIR)) {
1077 fprintf(stderr, " %s\n", ls->dentry_name);
1081 if (!(ls->dentry_flags & IS_DIR))
1082 one_remote_ref(ls->dentry_name);
1085 static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
1087 struct remote_ls_ctx *ls = (struct remote_ls_ctx *)ctx->userData;
1090 if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) {
1091 if (ls->dentry_flags & IS_DIR) {
1092 if (ls->flags & PROCESS_DIRS) {
1095 if (strcmp(ls->dentry_name, ls->path) &&
1096 ls->flags & RECURSIVE) {
1097 remote_ls(ls->dentry_name,
1102 } else if (ls->flags & PROCESS_FILES) {
1105 } else if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
1106 char *path = ctx->cdata;
1107 if (*ctx->cdata == 'h') {
1108 path = strstr(path, "//");
1110 path = strchr(path+2, '/');
1114 path += repo->path_len;
1115 ls->dentry_name = xstrdup(path);
1117 } else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
1118 ls->dentry_flags |= IS_DIR;
1120 } else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
1121 free(ls->dentry_name);
1122 ls->dentry_name = NULL;
1123 ls->dentry_flags = 0;
1128 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1129 * should _only_ heed the information from that file, instead of trying to
1130 * determine the refs from the remote file system (badly: it does not even
1131 * know about packed-refs).
1133 static void remote_ls(const char *path, int flags,
1134 void (*userFunc)(struct remote_ls_ctx *ls),
1137 char *url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1138 struct active_request_slot *slot;
1139 struct slot_results results;
1140 struct strbuf in_buffer = STRBUF_INIT;
1141 struct buffer out_buffer = { STRBUF_INIT, 0 };
1142 struct curl_slist *dav_headers = NULL;
1144 struct remote_ls_ctx ls;
1147 ls.path = xstrdup(path);
1148 ls.dentry_name = NULL;
1149 ls.dentry_flags = 0;
1150 ls.userData = userData;
1151 ls.userFunc = userFunc;
1153 sprintf(url, "%s%s", repo->url, path);
1155 strbuf_addf(&out_buffer.buf, PROPFIND_ALL_REQUEST);
1157 dav_headers = curl_slist_append(dav_headers, "Depth: 1");
1158 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1160 slot = get_active_slot();
1161 slot->results = &results;
1162 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1163 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1164 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1165 #ifndef NO_CURL_IOCTL
1166 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1167 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
1169 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1170 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1171 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1172 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1173 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1174 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1176 if (start_active_slot(slot)) {
1177 run_active_slot(slot);
1178 if (results.curl_result == CURLE_OK) {
1179 XML_Parser parser = XML_ParserCreate(NULL);
1180 enum XML_Status result;
1181 ctx.name = xcalloc(10, 1);
1184 ctx.userFunc = handle_remote_ls_ctx;
1186 XML_SetUserData(parser, &ctx);
1187 XML_SetElementHandler(parser, xml_start_tag,
1189 XML_SetCharacterDataHandler(parser, xml_cdata);
1190 result = XML_Parse(parser, in_buffer.buf,
1194 if (result != XML_STATUS_OK) {
1195 fprintf(stderr, "XML error: %s\n",
1197 XML_GetErrorCode(parser)));
1199 XML_ParserFree(parser);
1202 fprintf(stderr, "Unable to start PROPFIND request\n");
1207 strbuf_release(&out_buffer.buf);
1208 strbuf_release(&in_buffer);
1209 curl_slist_free_all(dav_headers);
1212 static void get_remote_object_list(unsigned char parent)
1214 char path[] = "objects/XX/";
1215 static const char hex[] = "0123456789abcdef";
1216 unsigned int val = parent;
1218 path[8] = hex[val >> 4];
1219 path[9] = hex[val & 0xf];
1220 remote_dir_exists[val] = 0;
1221 remote_ls(path, (PROCESS_FILES | PROCESS_DIRS),
1222 process_ls_object, &val);
1225 static int locking_available(void)
1227 struct active_request_slot *slot;
1228 struct slot_results results;
1229 struct strbuf in_buffer = STRBUF_INIT;
1230 struct buffer out_buffer = { STRBUF_INIT, 0 };
1231 struct curl_slist *dav_headers = NULL;
1236 escaped = xml_entities(repo->url);
1237 strbuf_addf(&out_buffer.buf, PROPFIND_SUPPORTEDLOCK_REQUEST, escaped);
1240 dav_headers = curl_slist_append(dav_headers, "Depth: 0");
1241 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1243 slot = get_active_slot();
1244 slot->results = &results;
1245 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1246 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1247 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1248 #ifndef NO_CURL_IOCTL
1249 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1250 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
1252 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1253 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1254 curl_easy_setopt(slot->curl, CURLOPT_URL, repo->url);
1255 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1256 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1257 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1259 if (start_active_slot(slot)) {
1260 run_active_slot(slot);
1261 if (results.curl_result == CURLE_OK) {
1262 XML_Parser parser = XML_ParserCreate(NULL);
1263 enum XML_Status result;
1264 ctx.name = xcalloc(10, 1);
1267 ctx.userFunc = handle_lockprop_ctx;
1268 ctx.userData = &lock_flags;
1269 XML_SetUserData(parser, &ctx);
1270 XML_SetElementHandler(parser, xml_start_tag,
1272 result = XML_Parse(parser, in_buffer.buf,
1276 if (result != XML_STATUS_OK) {
1277 fprintf(stderr, "XML error: %s\n",
1279 XML_GetErrorCode(parser)));
1282 XML_ParserFree(parser);
1284 error("no DAV locking support on %s",
1288 error("Cannot access URL %s, return code %d",
1289 repo->url, results.curl_result);
1293 error("Unable to start PROPFIND request on %s", repo->url);
1296 strbuf_release(&out_buffer.buf);
1297 strbuf_release(&in_buffer);
1298 curl_slist_free_all(dav_headers);
1303 static struct object_list **add_one_object(struct object *obj, struct object_list **p)
1305 struct object_list *entry = xmalloc(sizeof(struct object_list));
1309 return &entry->next;
1312 static struct object_list **process_blob(struct blob *blob,
1313 struct object_list **p,
1314 struct name_path *path,
1317 struct object *obj = &blob->object;
1319 obj->flags |= LOCAL;
1321 if (obj->flags & (UNINTERESTING | SEEN))
1325 return add_one_object(obj, p);
1328 static struct object_list **process_tree(struct tree *tree,
1329 struct object_list **p,
1330 struct name_path *path,
1333 struct object *obj = &tree->object;
1334 struct tree_desc desc;
1335 struct name_entry entry;
1336 struct name_path me;
1338 obj->flags |= LOCAL;
1340 if (obj->flags & (UNINTERESTING | SEEN))
1342 if (parse_tree(tree) < 0)
1343 die("bad tree object %s", sha1_to_hex(obj->sha1));
1346 name = xstrdup(name);
1347 p = add_one_object(obj, p);
1350 me.elem_len = strlen(name);
1352 init_tree_desc(&desc, tree->buffer, tree->size);
1354 while (tree_entry(&desc, &entry))
1355 switch (object_type(entry.mode)) {
1357 p = process_tree(lookup_tree(entry.sha1), p, &me, name);
1360 p = process_blob(lookup_blob(entry.sha1), p, &me, name);
1363 /* Subproject commit - not in this repository */
1368 tree->buffer = NULL;
1372 static int get_delta(struct rev_info *revs, struct remote_lock *lock)
1375 struct commit *commit;
1376 struct object_list **p = &objects;
1379 while ((commit = get_revision(revs)) != NULL) {
1380 p = process_tree(commit->tree, p, NULL, "");
1381 commit->object.flags |= LOCAL;
1382 if (!(commit->object.flags & UNINTERESTING))
1383 count += add_send_request(&commit->object, lock);
1386 for (i = 0; i < revs->pending.nr; i++) {
1387 struct object_array_entry *entry = revs->pending.objects + i;
1388 struct object *obj = entry->item;
1389 const char *name = entry->name;
1391 if (obj->flags & (UNINTERESTING | SEEN))
1393 if (obj->type == OBJ_TAG) {
1395 p = add_one_object(obj, p);
1398 if (obj->type == OBJ_TREE) {
1399 p = process_tree((struct tree *)obj, p, NULL, name);
1402 if (obj->type == OBJ_BLOB) {
1403 p = process_blob((struct blob *)obj, p, NULL, name);
1406 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
1410 if (!(objects->item->flags & UNINTERESTING))
1411 count += add_send_request(objects->item, lock);
1412 objects = objects->next;
1418 static int update_remote(unsigned char *sha1, struct remote_lock *lock)
1420 struct active_request_slot *slot;
1421 struct slot_results results;
1422 struct buffer out_buffer = { STRBUF_INIT, 0 };
1423 struct curl_slist *dav_headers;
1425 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1427 strbuf_addf(&out_buffer.buf, "%s\n", sha1_to_hex(sha1));
1429 slot = get_active_slot();
1430 slot->results = &results;
1431 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1432 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1433 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1434 #ifndef NO_CURL_IOCTL
1435 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1436 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
1438 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1439 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1440 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1441 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1442 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1443 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1445 if (start_active_slot(slot)) {
1446 run_active_slot(slot);
1447 strbuf_release(&out_buffer.buf);
1448 if (results.curl_result != CURLE_OK) {
1450 "PUT error: curl result=%d, HTTP code=%ld\n",
1451 results.curl_result, results.http_code);
1452 /* We should attempt recovery? */
1456 strbuf_release(&out_buffer.buf);
1457 fprintf(stderr, "Unable to start PUT request\n");
1464 static struct ref *remote_refs;
1466 static void one_remote_ref(char *refname)
1471 ref = alloc_ref(refname);
1473 if (http_fetch_ref(repo->url, ref) != 0) {
1475 "Unable to fetch ref %s from %s\n",
1476 refname, repo->url);
1482 * Fetch a copy of the object if it doesn't exist locally - it
1483 * may be required for updating server info later.
1485 if (repo->can_update_info_refs && !has_sha1_file(ref->old_sha1)) {
1486 obj = lookup_unknown_object(ref->old_sha1);
1488 fprintf(stderr, " fetch %s for %s\n",
1489 sha1_to_hex(ref->old_sha1), refname);
1490 add_fetch_request(obj);
1494 ref->next = remote_refs;
1498 static void get_dav_remote_heads(void)
1500 remote_ls("refs/", (PROCESS_FILES | PROCESS_DIRS | RECURSIVE), process_ls_ref, NULL);
1503 static void add_remote_info_ref(struct remote_ls_ctx *ls)
1505 struct strbuf *buf = (struct strbuf *)ls->userData;
1511 ref = alloc_ref(ls->dentry_name);
1513 if (http_fetch_ref(repo->url, ref) != 0) {
1515 "Unable to fetch ref %s from %s\n",
1516 ls->dentry_name, repo->url);
1522 o = parse_object(ref->old_sha1);
1525 "Unable to parse object %s for remote ref %s\n",
1526 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1532 len = strlen(ls->dentry_name) + 42;
1533 ref_info = xcalloc(len + 1, 1);
1534 sprintf(ref_info, "%s %s\n",
1535 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1536 fwrite_buffer(ref_info, 1, len, buf);
1539 if (o->type == OBJ_TAG) {
1540 o = deref_tag(o, ls->dentry_name, 0);
1542 len = strlen(ls->dentry_name) + 45;
1543 ref_info = xcalloc(len + 1, 1);
1544 sprintf(ref_info, "%s %s^{}\n",
1545 sha1_to_hex(o->sha1), ls->dentry_name);
1546 fwrite_buffer(ref_info, 1, len, buf);
1553 static void update_remote_info_refs(struct remote_lock *lock)
1555 struct buffer buffer = { STRBUF_INIT, 0 };
1556 struct active_request_slot *slot;
1557 struct slot_results results;
1558 struct curl_slist *dav_headers;
1560 remote_ls("refs/", (PROCESS_FILES | RECURSIVE),
1561 add_remote_info_ref, &buffer.buf);
1563 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1565 slot = get_active_slot();
1566 slot->results = &results;
1567 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &buffer);
1568 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, buffer.buf.len);
1569 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1570 #ifndef NO_CURL_IOCTL
1571 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1572 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &buffer);
1574 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1575 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1576 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1577 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1578 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1579 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1581 if (start_active_slot(slot)) {
1582 run_active_slot(slot);
1583 if (results.curl_result != CURLE_OK) {
1585 "PUT error: curl result=%d, HTTP code=%ld\n",
1586 results.curl_result, results.http_code);
1590 strbuf_release(&buffer.buf);
1593 static int remote_exists(const char *path)
1595 char *url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1598 sprintf(url, "%s%s", repo->url, path);
1600 switch (http_get_strbuf(url, NULL, 0)) {
1604 case HTTP_MISSING_TARGET:
1608 http_error(url, HTTP_ERROR);
1616 static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
1619 struct strbuf buffer = STRBUF_INIT;
1621 url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1622 sprintf(url, "%s%s", repo->url, path);
1624 if (http_get_strbuf(url, &buffer, 0) != HTTP_OK)
1625 die("Couldn't get %s for remote symref\n%s", url,
1633 if (buffer.len == 0)
1636 /* If it's a symref, set the refname; otherwise try for a sha1 */
1637 if (!prefixcmp((char *)buffer.buf, "ref: ")) {
1638 *symref = xmemdupz((char *)buffer.buf + 5, buffer.len - 6);
1640 get_sha1_hex(buffer.buf, sha1);
1643 strbuf_release(&buffer);
1646 static int verify_merge_base(unsigned char *head_sha1, unsigned char *branch_sha1)
1648 struct commit *head = lookup_commit(head_sha1);
1649 struct commit *branch = lookup_commit(branch_sha1);
1650 struct commit_list *merge_bases = get_merge_bases(head, branch, 1);
1652 return (merge_bases && !merge_bases->next && merge_bases->item == branch);
1655 static int delete_remote_branch(char *pattern, int force)
1657 struct ref *refs = remote_refs;
1658 struct ref *remote_ref = NULL;
1659 unsigned char head_sha1[20];
1660 char *symref = NULL;
1662 int patlen = strlen(pattern);
1664 struct active_request_slot *slot;
1665 struct slot_results results;
1668 /* Find the remote branch(es) matching the specified branch name */
1669 for (match = 0; refs; refs = refs->next) {
1670 char *name = refs->name;
1671 int namelen = strlen(name);
1672 if (namelen < patlen ||
1673 memcmp(name + namelen - patlen, pattern, patlen))
1675 if (namelen != patlen && name[namelen - patlen - 1] != '/')
1681 return error("No remote branch matches %s", pattern);
1683 return error("More than one remote branch matches %s",
1687 * Remote HEAD must be a symref (not exactly foolproof; a remote
1688 * symlink to a symref will look like a symref)
1690 fetch_symref("HEAD", &symref, head_sha1);
1692 return error("Remote HEAD is not a symref");
1694 /* Remote branch must not be the remote HEAD */
1695 for (i=0; symref && i<MAXDEPTH; i++) {
1696 if (!strcmp(remote_ref->name, symref))
1697 return error("Remote branch %s is the current HEAD",
1699 fetch_symref(symref, &symref, head_sha1);
1702 /* Run extra sanity checks if delete is not forced */
1704 /* Remote HEAD must resolve to a known object */
1706 return error("Remote HEAD symrefs too deep");
1707 if (is_null_sha1(head_sha1))
1708 return error("Unable to resolve remote HEAD");
1709 if (!has_sha1_file(head_sha1))
1710 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1));
1712 /* Remote branch must resolve to a known object */
1713 if (is_null_sha1(remote_ref->old_sha1))
1714 return error("Unable to resolve remote branch %s",
1716 if (!has_sha1_file(remote_ref->old_sha1))
1717 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));
1719 /* Remote branch must be an ancestor of remote HEAD */
1720 if (!verify_merge_base(head_sha1, remote_ref->old_sha1)) {
1721 return error("The branch '%s' is not an ancestor "
1722 "of your current HEAD.\n"
1723 "If you are sure you want to delete it,"
1724 " run:\n\t'git http-push -D %s %s'",
1725 remote_ref->name, repo->url, pattern);
1729 /* Send delete request */
1730 fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name);
1733 url = xmalloc(strlen(repo->url) + strlen(remote_ref->name) + 1);
1734 sprintf(url, "%s%s", repo->url, remote_ref->name);
1735 slot = get_active_slot();
1736 slot->results = &results;
1737 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
1738 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1739 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1740 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_DELETE);
1741 if (start_active_slot(slot)) {
1742 run_active_slot(slot);
1744 if (results.curl_result != CURLE_OK)
1745 return error("DELETE request failed (%d/%ld)\n",
1746 results.curl_result, results.http_code);
1749 return error("Unable to start DELETE request");
1755 static void run_request_queue(void)
1757 #ifdef USE_CURL_MULTI
1758 is_running_queue = 1;
1759 fill_active_slots();
1760 add_fill_function(NULL, fill_active_slot);
1763 finish_all_active_slots();
1764 #ifdef USE_CURL_MULTI
1765 fill_active_slots();
1767 } while (request_queue_head && !aborted);
1769 #ifdef USE_CURL_MULTI
1770 is_running_queue = 0;
1774 int main(int argc, char **argv)
1776 struct transfer_request *request;
1777 struct transfer_request *next_request;
1779 char **refspec = NULL;
1780 struct remote_lock *ref_lock = NULL;
1781 struct remote_lock *info_ref_lock = NULL;
1782 struct rev_info revs;
1783 int delete_branch = 0;
1784 int force_delete = 0;
1785 int objects_to_send;
1789 struct ref *ref, *local_refs;
1790 struct remote *remote;
1791 char *rewritten_url = NULL;
1793 git_extract_argv0_path(argv[0]);
1795 setup_git_directory();
1797 repo = xcalloc(sizeof(*repo), 1);
1800 for (i = 1; i < argc; i++, argv++) {
1804 if (!strcmp(arg, "--all")) {
1805 push_all = MATCH_REFS_ALL;
1808 if (!strcmp(arg, "--force")) {
1812 if (!strcmp(arg, "--dry-run")) {
1816 if (!strcmp(arg, "--verbose")) {
1818 http_is_verbose = 1;
1821 if (!strcmp(arg, "-d")) {
1825 if (!strcmp(arg, "-D")) {
1832 char *path = strstr(arg, "//");
1834 repo->path_len = strlen(arg);
1836 repo->path = strchr(path+2, '/');
1838 repo->path_len = strlen(repo->path);
1843 nr_refspec = argc - i;
1847 #ifndef USE_CURL_MULTI
1848 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
1852 usage(http_push_usage);
1854 if (delete_branch && nr_refspec != 1)
1855 die("You must specify only one branch name when deleting a remote branch");
1857 memset(remote_dir_exists, -1, 256);
1860 * Create a minimum remote by hand to give to http_init(),
1861 * primarily to allow it to look at the URL.
1863 remote = xcalloc(sizeof(*remote), 1);
1864 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
1865 remote->url[remote->url_nr++] = repo->url;
1868 if (repo->url && repo->url[strlen(repo->url)-1] != '/') {
1869 rewritten_url = xmalloc(strlen(repo->url)+2);
1870 strcpy(rewritten_url, repo->url);
1871 strcat(rewritten_url, "/");
1872 repo->path = rewritten_url + (repo->path - repo->url);
1874 repo->url = rewritten_url;
1877 #ifdef USE_CURL_MULTI
1878 is_running_queue = 0;
1881 /* Verify DAV compliance/lock support */
1882 if (!locking_available()) {
1887 sigchain_push_common(remove_locks_on_signal);
1889 /* Check whether the remote has server info files */
1890 repo->can_update_info_refs = 0;
1891 repo->has_info_refs = remote_exists("info/refs");
1892 repo->has_info_packs = remote_exists("objects/info/packs");
1893 if (repo->has_info_refs) {
1894 info_ref_lock = lock_remote("info/refs", LOCK_TIME);
1896 repo->can_update_info_refs = 1;
1898 error("cannot lock existing info/refs");
1903 if (repo->has_info_packs)
1906 /* Get a list of all local and remote heads to validate refspecs */
1907 local_refs = get_local_heads();
1908 fprintf(stderr, "Fetching remote heads...\n");
1909 get_dav_remote_heads();
1910 run_request_queue();
1912 /* Remove a remote branch if -d or -D was specified */
1913 if (delete_branch) {
1914 if (delete_remote_branch(refspec[0], force_delete) == -1)
1915 fprintf(stderr, "Unable to delete remote branch %s\n",
1921 if (match_refs(local_refs, &remote_refs,
1922 nr_refspec, (const char **) refspec, push_all)) {
1927 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
1933 for (ref = remote_refs; ref; ref = ref->next) {
1934 char old_hex[60], *new_hex;
1935 const char *commit_argv[5];
1937 char *new_sha1_hex, *old_sha1_hex;
1942 if (is_null_sha1(ref->peer_ref->new_sha1)) {
1943 if (delete_remote_branch(ref->name, 1) == -1) {
1944 error("Could not remove %s", ref->name);
1951 if (!hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
1952 if (push_verbosely || 1)
1953 fprintf(stderr, "'%s': up-to-date\n", ref->name);
1958 !is_null_sha1(ref->old_sha1) &&
1960 if (!has_sha1_file(ref->old_sha1) ||
1961 !ref_newer(ref->peer_ref->new_sha1,
1964 * We do not have the remote ref, or
1965 * we know that the remote ref is not
1966 * an ancestor of what we are trying to
1967 * push. Either way this can be losing
1968 * commits at the remote end and likely
1969 * we were not up to date to begin with.
1971 error("remote '%s' is not an ancestor of\n"
1973 "Maybe you are not up-to-date and "
1974 "need to pull first?",
1976 ref->peer_ref->name);
1981 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
1983 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
1984 new_hex = sha1_to_hex(ref->new_sha1);
1986 fprintf(stderr, "updating '%s'", ref->name);
1987 if (strcmp(ref->name, ref->peer_ref->name))
1988 fprintf(stderr, " using '%s'", ref->peer_ref->name);
1989 fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex);
1993 /* Lock remote branch ref */
1994 ref_lock = lock_remote(ref->name, LOCK_TIME);
1995 if (ref_lock == NULL) {
1996 fprintf(stderr, "Unable to lock remote branch %s\n",
2002 /* Set up revision info for this refspec */
2004 new_sha1_hex = xstrdup(sha1_to_hex(ref->new_sha1));
2005 old_sha1_hex = NULL;
2006 commit_argv[1] = "--objects";
2007 commit_argv[2] = new_sha1_hex;
2008 if (!push_all && !is_null_sha1(ref->old_sha1)) {
2009 old_sha1_hex = xmalloc(42);
2010 sprintf(old_sha1_hex, "^%s",
2011 sha1_to_hex(ref->old_sha1));
2012 commit_argv[3] = old_sha1_hex;
2015 commit_argv[commit_argc] = NULL;
2016 init_revisions(&revs, setup_git_directory());
2017 setup_revisions(commit_argc, commit_argv, &revs, NULL);
2018 revs.edge_hint = 0; /* just in case */
2022 commit_argv[1] = NULL;
2025 /* Generate a list of objects that need to be pushed */
2027 if (prepare_revision_walk(&revs))
2028 die("revision walk setup failed");
2029 mark_edges_uninteresting(revs.commits, &revs, NULL);
2030 objects_to_send = get_delta(&revs, ref_lock);
2031 finish_all_active_slots();
2033 /* Push missing objects to remote, this would be a
2034 convenient time to pack them first if appropriate. */
2036 if (objects_to_send)
2037 fprintf(stderr, " sending %d objects\n",
2040 run_request_queue();
2042 /* Update the remote branch if all went well */
2043 if (aborted || !update_remote(ref->new_sha1, ref_lock))
2047 fprintf(stderr, " done\n");
2048 unlock_remote(ref_lock);
2052 /* Update remote server info if appropriate */
2053 if (repo->has_info_refs && new_refs) {
2054 if (info_ref_lock && repo->can_update_info_refs) {
2055 fprintf(stderr, "Updating remote server info\n");
2057 update_remote_info_refs(info_ref_lock);
2059 fprintf(stderr, "Unable to update server info\n");
2064 free(rewritten_url);
2066 unlock_remote(info_ref_lock);
2071 request = request_queue_head;
2072 while (request != NULL) {
2073 next_request = request->next;
2074 release_request(request);
2075 request = next_request;