http*: add helper methods for fetching objects (loose)
[git/dscho.git] / http-push.c
blob3439dcc5b89c6c4b056823634c43298a1ff3edc0
1 #include "cache.h"
2 #include "commit.h"
3 #include "tag.h"
4 #include "blob.h"
5 #include "http.h"
6 #include "refs.h"
7 #include "diff.h"
8 #include "revision.h"
9 #include "exec_cmd.h"
10 #include "remote.h"
11 #include "list-objects.h"
12 #include "sigchain.h"
14 #include <expat.h>
16 static const char http_push_usage[] =
17 "git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
19 #ifndef XML_STATUS_OK
20 enum XML_Status {
21 XML_STATUS_OK = 1,
22 XML_STATUS_ERROR = 0
24 #define XML_STATUS_OK 1
25 #define XML_STATUS_ERROR 0
26 #endif
28 #define PREV_BUF_SIZE 4096
30 /* DAV methods */
31 #define DAV_LOCK "LOCK"
32 #define DAV_MKCOL "MKCOL"
33 #define DAV_MOVE "MOVE"
34 #define DAV_PROPFIND "PROPFIND"
35 #define DAV_PUT "PUT"
36 #define DAV_UNLOCK "UNLOCK"
37 #define DAV_DELETE "DELETE"
39 /* DAV lock flags */
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>"
60 #define LOCK_TIME 600
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 */
71 #define MAXDEPTH 5
73 static int pushing;
74 static int aborted;
75 static signed char remote_dir_exists[256];
77 static int push_verbosely;
78 static int push_all = MATCH_REFS_NONE;
79 static int force_all;
80 static int dry_run;
82 static struct object_list *objects;
84 struct repo
86 char *url;
87 char *path;
88 int path_len;
89 int has_info_refs;
90 int can_update_info_refs;
91 int has_info_packs;
92 struct packed_git *packs;
93 struct remote_lock *locks;
96 static struct repo *repo;
98 enum transfer_state {
99 NEED_FETCH,
100 RUN_FETCH_LOOSE,
101 RUN_FETCH_PACKED,
102 NEED_PUSH,
103 RUN_MKCOL,
104 RUN_PUT,
105 RUN_MOVE,
106 ABORTED,
107 COMPLETE,
110 struct transfer_request
112 struct object *obj;
113 char *url;
114 char *dest;
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];
121 long http_code;
122 void *userData;
123 struct active_request_slot *slot;
124 struct transfer_request *next;
127 static struct transfer_request *request_queue_head;
129 struct xml_ctx
131 char *name;
132 int len;
133 char *cdata;
134 void (*userFunc)(struct xml_ctx *ctx, int tag_closed);
135 void *userData;
138 struct remote_lock
140 char *url;
141 char *owner;
142 char *token;
143 char tmpfile_suffix[41];
144 time_t start_time;
145 long timeout;
146 int refreshing;
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)
158 struct remote_ls_ctx
160 char *path;
161 void (*userFunc)(struct remote_ls_ctx *ls);
162 void *userData;
163 int flags;
164 char *dentry_name;
165 int dentry_flags;
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;
179 while (*s) {
180 size_t len = strcspn(s, "\"<>&");
181 strbuf_add(&buf, s, len);
182 s += len;
183 switch (*s) {
184 case '"':
185 strbuf_addstr(&buf, "&quot;");
186 break;
187 case '<':
188 strbuf_addstr(&buf, "&lt;");
189 break;
190 case '>':
191 strbuf_addstr(&buf, "&gt;");
192 break;
193 case '&':
194 strbuf_addstr(&buf, "&amp;");
195 break;
197 s++;
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);
210 strbuf_reset(&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);
215 strbuf_reset(&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);
220 strbuf_reset(&buf);
222 strbuf_release(&buf);
224 return dav_headers;
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;
248 return;
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;
286 } else {
287 request->state = ABORTED;
288 free(request->url);
289 request->url = NULL;
292 #endif
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);
302 if (!target) {
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);
306 return;
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);
313 if (preq == NULL) {
314 release_http_pack_request(preq);
315 repo->can_update_info_refs = 0;
316 return;
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);
326 return;
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;
352 char hdr[50];
353 void *unpacked;
354 unsigned long len;
355 int hdrlen;
356 ssize_t size;
357 z_stream stream;
359 unpacked = read_sha1_file(request->obj->sha1, &type, &len);
360 hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
362 /* Set it up */
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;
369 /* Compress it */
370 stream.next_out = (unsigned char *)request->buffer.buf.buf;
371 stream.avail_out = size;
373 /* First header.. */
374 stream.next_in = (void *)hdr;
375 stream.avail_in = hdrlen;
376 while (deflate(&stream, 0) == Z_OK)
377 /* nothing */;
379 /* Then the data itself.. */
380 stream.next_in = unpacked;
381 stream.avail_in = len;
382 while (deflate(&stream, Z_FINISH) == Z_OK)
383 /* nothing */;
384 deflateEnd(&stream);
385 free(unpacked);
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);
406 #endif
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;
417 } else {
418 request->state = ABORTED;
419 free(request->url);
420 request->url = NULL;
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;
443 } else {
444 request->state = ABORTED;
445 free(request->url);
446 request->url = NULL;
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;
455 int rc = 0;
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",
473 results.http_code);
474 } else {
475 lock->start_time = time(NULL);
476 rc = 1;
480 lock->refreshing = 0;
481 curl_slist_free_all(dav_headers);
483 return rc;
486 static void check_locks(void)
488 struct remote_lock *lock = repo->locks;
489 time_t current_time = time(NULL);
490 int time_remaining;
492 while (lock) {
493 time_remaining = lock->start_time + lock->timeout -
494 current_time;
495 if (!lock->refreshing && time_remaining < LOCK_REFRESH) {
496 if (!refresh_lock(lock)) {
497 fprintf(stderr,
498 "Unable to refresh lock for %s\n",
499 lock->url);
500 aborted = 1;
501 return;
504 lock = lock->next;
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;
514 } else {
515 while (entry->next != NULL && entry->next != request)
516 entry = entry->next;
517 if (entry->next == request)
518 entry->next = entry->next->next;
521 free(request->url);
522 free(request);
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 */
535 check_locks();
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) {
542 free(request->url);
543 request->url = NULL;
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;
550 start_put(request);
551 } else {
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;
556 aborted = 1;
558 } else if (request->state == RUN_PUT) {
559 if (request->curl_result == CURLE_OK) {
560 start_move(request);
561 } else {
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;
566 aborted = 1;
568 } else if (request->state == RUN_MOVE) {
569 if (request->curl_result == CURLE_OK) {
570 if (push_verbosely)
571 fprintf(stderr, " sent %s\n",
572 sha1_to_hex(request->obj->sha1));
573 request->obj->flags |= REMOTE;
574 release_request(request);
575 } else {
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;
580 aborted = 1;
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);
593 } else
594 start_fetch_packed(request);
596 } else if (request->state == RUN_FETCH_PACKED) {
597 int fail = 1;
598 if (request->curl_result != CURLE_OK) {
599 fprintf(stderr, "Unable to get pack file %s\n%s",
600 request->url, curl_errorstr);
601 } else {
602 preq = (struct http_pack_request *)request->userData;
604 if (preq) {
605 if (finish_http_pack_request(preq) > 0)
606 fail = 0;
607 release_http_pack_request(preq);
610 if (fail)
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)
623 return 0;
625 for (request = request_queue_head; request; request = request->next) {
626 if (request->state == NEED_FETCH) {
627 start_fetch_loose(request);
628 return 1;
629 } else if (pushing && request->state == NEED_PUSH) {
630 if (remote_dir_exists[request->obj->sha1[0]] == 1) {
631 start_put(request);
632 } else {
633 start_mkcol(request);
635 return 1;
638 return 0;
640 #endif
642 static void get_remote_object_list(unsigned char parent);
644 static void add_fetch_request(struct object *obj)
646 struct transfer_request *request;
648 check_locks();
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))
657 return;
659 obj->flags |= FETCHING;
660 request = xmalloc(sizeof(*request));
661 request->obj = obj;
662 request->url = NULL;
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
670 fill_active_slots();
671 step_active_slots();
672 #endif
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 */
681 check_locks();
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))
690 return 0;
691 target = find_sha1_pack(obj->sha1, repo->packs);
692 if (target) {
693 obj->flags |= REMOTE;
694 return 0;
697 obj->flags |= PUSHING;
698 request = xmalloc(sizeof(*request));
699 request->obj = obj;
700 request->url = NULL;
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
708 fill_active_slots();
709 step_active_slots();
710 #endif
712 return 1;
715 static int fetch_indices(void)
717 int ret;
719 if (push_verbosely)
720 fprintf(stderr, "Getting pack list\n");
722 switch (http_get_info_packs(repo->url, &repo->packs)) {
723 case HTTP_OK:
724 case HTTP_MISSING_TARGET:
725 ret = 0;
726 break;
727 default:
728 ret = -1;
731 return ret;
734 static void one_remote_object(const char *hex)
736 unsigned char sha1[20];
737 struct object *obj;
739 if (get_sha1_hex(hex, sha1) != 0)
740 return;
742 obj = lookup_object(sha1);
743 if (!obj)
744 obj = parse_object(sha1);
746 /* Ignore remote objects that don't exist locally */
747 if (!obj)
748 return;
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;
759 if (tag_closed) {
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;
777 git_SHA_CTX sha_ctx;
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-"))
786 lock->timeout =
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);
804 static void
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, ':');
809 int new_len;
811 if (c == NULL)
812 c = name;
813 else
814 c++;
816 new_len = strlen(ctx->name) + strlen(c) + 2;
818 if (new_len > ctx->len) {
819 ctx->name = xrealloc(ctx->name, new_len);
820 ctx->len = new_len;
822 strcat(ctx->name, ".");
823 strcat(ctx->name, c);
825 free(ctx->cdata);
826 ctx->cdata = NULL;
828 ctx->userFunc(ctx, 0);
831 static void
832 xml_end_tag(void *userData, const char *name)
834 struct xml_ctx *ctx = (struct xml_ctx *)userData;
835 const char *c = strchr(name, ':');
836 char *ep;
838 ctx->userFunc(ctx, 1);
840 if (c == NULL)
841 c = name;
842 else
843 c++;
845 ep = ctx->name + strlen(ctx->name) - strlen(c) - 1;
846 *ep = 0;
849 static void
850 xml_cdata(void *userData, const XML_Char *s, int len)
852 struct xml_ctx *ctx = (struct xml_ctx *)userData;
853 free(ctx->cdata);
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;
863 char *url;
864 char *ep;
865 char timeout_header[25];
866 struct remote_lock *lock = NULL;
867 struct curl_slist *dav_headers = NULL;
868 struct xml_ctx ctx;
869 char *escaped;
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, '/');
876 while (ep) {
877 char saved_character = ep[1];
878 ep[1] = '\0';
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) {
889 fprintf(stderr,
890 "Unable to create branch path %s\n",
891 url);
892 free(url);
893 return NULL;
895 } else {
896 fprintf(stderr, "Unable to start MKCOL request\n");
897 free(url);
898 return NULL;
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);
906 free(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);
920 #endif
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));
929 lock->timeout = -1;
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);
937 ctx.len = 0;
938 ctx.cdata = NULL;
939 ctx.userFunc = handle_new_lock_ctx;
940 ctx.userData = lock;
941 XML_SetUserData(parser, &ctx);
942 XML_SetElementHandler(parser, xml_start_tag,
943 xml_end_tag);
944 XML_SetCharacterDataHandler(parser, xml_cdata);
945 result = XML_Parse(parser, in_buffer.buf,
946 in_buffer.len, 1);
947 free(ctx.name);
948 if (result != XML_STATUS_OK) {
949 fprintf(stderr, "XML error: %s\n",
950 XML_ErrorString(
951 XML_GetErrorCode(parser)));
952 lock->timeout = -1;
954 XML_ParserFree(parser);
956 } else {
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) {
965 free(lock->token);
966 free(lock->owner);
967 free(url);
968 free(lock);
969 lock = NULL;
970 } else {
971 lock->url = url;
972 lock->start_time = time(NULL);
973 lock->next = repo->locks;
974 repo->locks = lock;
977 return lock;
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;
986 int rc = 0;
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)
1000 rc = 1;
1001 else
1002 fprintf(stderr, "UNLOCK HTTP error %ld\n",
1003 results.http_code);
1004 } else {
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;
1012 } else {
1013 while (prev && prev->next != lock)
1014 prev = prev->next;
1015 if (prev)
1016 prev->next = prev->next->next;
1019 free(lock->owner);
1020 free(lock->url);
1021 free(lock->token);
1022 free(lock);
1024 return rc;
1027 static void remove_locks(void)
1029 struct remote_lock *lock = repo->locks;
1031 fprintf(stderr, "Removing remote locks...\n");
1032 while (lock) {
1033 unlock_remote(lock);
1034 lock = lock->next;
1038 static void remove_locks_on_signal(int signo)
1040 remove_locks();
1041 sigchain_pop(signo);
1042 raise(signo);
1045 static void remote_ls(const char *path, int flags,
1046 void (*userFunc)(struct remote_ls_ctx *ls),
1047 void *userData);
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;
1053 char *obj_hex;
1055 if (!strcmp(ls->path, ls->dentry_name) && (ls->flags & IS_DIR)) {
1056 remote_dir_exists[*parent] = 1;
1057 return;
1060 if (strlen(path) != 49)
1061 return;
1062 path += 8;
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);
1068 free(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);
1075 return;
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;
1086 if (tag_closed) {
1087 if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) {
1088 if (ls->dentry_flags & IS_DIR) {
1089 if (ls->flags & PROCESS_DIRS) {
1090 ls->userFunc(ls);
1092 if (strcmp(ls->dentry_name, ls->path) &&
1093 ls->flags & RECURSIVE) {
1094 remote_ls(ls->dentry_name,
1095 ls->flags,
1096 ls->userFunc,
1097 ls->userData);
1099 } else if (ls->flags & PROCESS_FILES) {
1100 ls->userFunc(ls);
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, "//");
1106 if (path) {
1107 path = strchr(path+2, '/');
1110 if (path) {
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),
1132 void *userData)
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;
1140 struct xml_ctx ctx;
1141 struct remote_ls_ctx ls;
1143 ls.flags = flags;
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);
1165 #endif
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);
1179 ctx.len = 0;
1180 ctx.cdata = NULL;
1181 ctx.userFunc = handle_remote_ls_ctx;
1182 ctx.userData = &ls;
1183 XML_SetUserData(parser, &ctx);
1184 XML_SetElementHandler(parser, xml_start_tag,
1185 xml_end_tag);
1186 XML_SetCharacterDataHandler(parser, xml_cdata);
1187 result = XML_Parse(parser, in_buffer.buf,
1188 in_buffer.len, 1);
1189 free(ctx.name);
1191 if (result != XML_STATUS_OK) {
1192 fprintf(stderr, "XML error: %s\n",
1193 XML_ErrorString(
1194 XML_GetErrorCode(parser)));
1196 XML_ParserFree(parser);
1198 } else {
1199 fprintf(stderr, "Unable to start PROPFIND request\n");
1202 free(ls.path);
1203 free(url);
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;
1229 struct xml_ctx ctx;
1230 int lock_flags = 0;
1231 char *escaped;
1233 escaped = xml_entities(repo->url);
1234 strbuf_addf(&out_buffer.buf, PROPFIND_SUPPORTEDLOCK_REQUEST, escaped);
1235 free(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);
1248 #endif
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);
1262 ctx.len = 0;
1263 ctx.cdata = NULL;
1264 ctx.userFunc = handle_lockprop_ctx;
1265 ctx.userData = &lock_flags;
1266 XML_SetUserData(parser, &ctx);
1267 XML_SetElementHandler(parser, xml_start_tag,
1268 xml_end_tag);
1269 result = XML_Parse(parser, in_buffer.buf,
1270 in_buffer.len, 1);
1271 free(ctx.name);
1273 if (result != XML_STATUS_OK) {
1274 fprintf(stderr, "XML error: %s\n",
1275 XML_ErrorString(
1276 XML_GetErrorCode(parser)));
1277 lock_flags = 0;
1279 XML_ParserFree(parser);
1280 if (!lock_flags)
1281 error("no DAV locking support on %s",
1282 repo->url);
1284 } else {
1285 error("Cannot access URL %s, return code %d",
1286 repo->url, results.curl_result);
1287 lock_flags = 0;
1289 } else {
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);
1297 return lock_flags;
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));
1303 entry->item = obj;
1304 entry->next = *p;
1305 *p = entry;
1306 return &entry->next;
1309 static struct object_list **process_blob(struct blob *blob,
1310 struct object_list **p,
1311 struct name_path *path,
1312 const char *name)
1314 struct object *obj = &blob->object;
1316 obj->flags |= LOCAL;
1318 if (obj->flags & (UNINTERESTING | SEEN))
1319 return p;
1321 obj->flags |= 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,
1328 const char *name)
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))
1338 return p;
1339 if (parse_tree(tree) < 0)
1340 die("bad tree object %s", sha1_to_hex(obj->sha1));
1342 obj->flags |= SEEN;
1343 name = xstrdup(name);
1344 p = add_one_object(obj, p);
1345 me.up = path;
1346 me.elem = name;
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)) {
1353 case OBJ_TREE:
1354 p = process_tree(lookup_tree(entry.sha1), p, &me, name);
1355 break;
1356 case OBJ_BLOB:
1357 p = process_blob(lookup_blob(entry.sha1), p, &me, name);
1358 break;
1359 default:
1360 /* Subproject commit - not in this repository */
1361 break;
1364 free(tree->buffer);
1365 tree->buffer = NULL;
1366 return p;
1369 static int get_delta(struct rev_info *revs, struct remote_lock *lock)
1371 int i;
1372 struct commit *commit;
1373 struct object_list **p = &objects;
1374 int count = 0;
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))
1389 continue;
1390 if (obj->type == OBJ_TAG) {
1391 obj->flags |= SEEN;
1392 p = add_one_object(obj, p);
1393 continue;
1395 if (obj->type == OBJ_TREE) {
1396 p = process_tree((struct tree *)obj, p, NULL, name);
1397 continue;
1399 if (obj->type == OBJ_BLOB) {
1400 p = process_blob((struct blob *)obj, p, NULL, name);
1401 continue;
1403 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
1406 while (objects) {
1407 if (!(objects->item->flags & UNINTERESTING))
1408 count += add_send_request(objects->item, lock);
1409 objects = objects->next;
1412 return count;
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);
1434 #endif
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) {
1446 fprintf(stderr,
1447 "PUT error: curl result=%d, HTTP code=%ld\n",
1448 results.curl_result, results.http_code);
1449 /* We should attempt recovery? */
1450 return 0;
1452 } else {
1453 strbuf_release(&out_buffer.buf);
1454 fprintf(stderr, "Unable to start PUT request\n");
1455 return 0;
1458 return 1;
1461 static struct ref *remote_refs, **remote_tail;
1463 static void one_remote_ref(char *refname)
1465 struct ref *ref;
1466 struct object *obj;
1468 ref = alloc_ref(refname);
1470 if (http_fetch_ref(repo->url, ref) != 0) {
1471 fprintf(stderr,
1472 "Unable to fetch ref %s from %s\n",
1473 refname, repo->url);
1474 free(ref);
1475 return;
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);
1484 if (obj) {
1485 fprintf(stderr, " fetch %s for %s\n",
1486 sha1_to_hex(ref->old_sha1), refname);
1487 add_fetch_request(obj);
1491 *remote_tail = ref;
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)
1503 int i;
1505 for (i = 0; i < 20; i++) {
1506 if (*sha1++)
1507 return 0;
1509 return 1;
1512 static void add_remote_info_ref(struct remote_ls_ctx *ls)
1514 struct strbuf *buf = (struct strbuf *)ls->userData;
1515 struct object *o;
1516 int len;
1517 char *ref_info;
1518 struct ref *ref;
1520 ref = alloc_ref(ls->dentry_name);
1522 if (http_fetch_ref(repo->url, ref) != 0) {
1523 fprintf(stderr,
1524 "Unable to fetch ref %s from %s\n",
1525 ls->dentry_name, repo->url);
1526 aborted = 1;
1527 free(ref);
1528 return;
1531 o = parse_object(ref->old_sha1);
1532 if (!o) {
1533 fprintf(stderr,
1534 "Unable to parse object %s for remote ref %s\n",
1535 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1536 aborted = 1;
1537 free(ref);
1538 return;
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);
1546 free(ref_info);
1548 if (o->type == OBJ_TAG) {
1549 o = deref_tag(o, ls->dentry_name, 0);
1550 if (o) {
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);
1556 free(ref_info);
1559 free(ref);
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);
1571 if (!aborted) {
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);
1582 #endif
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) {
1593 fprintf(stderr,
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);
1605 int ret;
1607 sprintf(url, "%s%s", repo->url, path);
1609 switch (http_get_strbuf(url, NULL, 0)) {
1610 case HTTP_OK:
1611 ret = 1;
1612 break;
1613 case HTTP_MISSING_TARGET:
1614 ret = 0;
1615 break;
1616 case HTTP_ERROR:
1617 http_error(url, HTTP_ERROR);
1618 default:
1619 ret = -1;
1621 free(url);
1622 return ret;
1625 static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
1627 char *url;
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,
1635 curl_errorstr);
1636 free(url);
1638 free(*symref);
1639 *symref = NULL;
1640 hashclr(sha1);
1642 if (buffer.len == 0)
1643 return;
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);
1648 } else {
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;
1670 int match;
1671 int patlen = strlen(pattern);
1672 int i;
1673 struct active_request_slot *slot;
1674 struct slot_results results;
1675 char *url;
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))
1683 continue;
1684 if (namelen != patlen && name[namelen - patlen - 1] != '/')
1685 continue;
1686 match++;
1687 remote_ref = refs;
1689 if (match == 0)
1690 return error("No remote branch matches %s", pattern);
1691 if (match != 1)
1692 return error("More than one remote branch matches %s",
1693 pattern);
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);
1700 if (!symref)
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",
1707 remote_ref->name);
1708 fetch_symref(symref, &symref, head_sha1);
1711 /* Run extra sanity checks if delete is not forced */
1712 if (!force) {
1713 /* Remote HEAD must resolve to a known object */
1714 if (symref)
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",
1724 remote_ref->name);
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);
1740 if (dry_run)
1741 return 0;
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);
1752 free(url);
1753 if (results.curl_result != CURLE_OK)
1754 return error("DELETE request failed (%d/%ld)\n",
1755 results.curl_result, results.http_code);
1756 } else {
1757 free(url);
1758 return error("Unable to start DELETE request");
1761 return 0;
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);
1770 #endif
1771 do {
1772 finish_all_active_slots();
1773 #ifdef USE_CURL_MULTI
1774 fill_active_slots();
1775 #endif
1776 } while (request_queue_head && !aborted);
1778 #ifdef USE_CURL_MULTI
1779 is_running_queue = 0;
1780 #endif
1783 int main(int argc, char **argv)
1785 struct transfer_request *request;
1786 struct transfer_request *next_request;
1787 int nr_refspec = 0;
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;
1795 int rc = 0;
1796 int i;
1797 int new_refs;
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);
1808 argv++;
1809 for (i = 1; i < argc; i++, argv++) {
1810 char *arg = *argv;
1812 if (*arg == '-') {
1813 if (!strcmp(arg, "--all")) {
1814 push_all = MATCH_REFS_ALL;
1815 continue;
1817 if (!strcmp(arg, "--force")) {
1818 force_all = 1;
1819 continue;
1821 if (!strcmp(arg, "--dry-run")) {
1822 dry_run = 1;
1823 continue;
1825 if (!strcmp(arg, "--verbose")) {
1826 push_verbosely = 1;
1827 http_is_verbose = 1;
1828 continue;
1830 if (!strcmp(arg, "-d")) {
1831 delete_branch = 1;
1832 continue;
1834 if (!strcmp(arg, "-D")) {
1835 delete_branch = 1;
1836 force_delete = 1;
1837 continue;
1840 if (!repo->url) {
1841 char *path = strstr(arg, "//");
1842 repo->url = arg;
1843 repo->path_len = strlen(arg);
1844 if (path) {
1845 repo->path = strchr(path+2, '/');
1846 if (repo->path)
1847 repo->path_len = strlen(repo->path);
1849 continue;
1851 refspec = argv;
1852 nr_refspec = argc - i;
1853 break;
1856 #ifndef USE_CURL_MULTI
1857 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
1858 #endif
1860 if (!repo->url)
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;
1875 http_init(remote);
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);
1882 repo->path_len++;
1883 repo->url = rewritten_url;
1886 #ifdef USE_CURL_MULTI
1887 is_running_queue = 0;
1888 #endif
1890 /* Verify DAV compliance/lock support */
1891 if (!locking_available()) {
1892 rc = 1;
1893 goto cleanup;
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);
1904 if (info_ref_lock)
1905 repo->can_update_info_refs = 1;
1906 else {
1907 error("cannot lock existing info/refs");
1908 rc = 1;
1909 goto cleanup;
1912 if (repo->has_info_packs)
1913 fetch_indices();
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",
1925 refspec[0]);
1926 goto cleanup;
1929 /* match them up */
1930 if (!remote_tail)
1931 remote_tail = &remote_refs;
1932 if (match_refs(local_refs, remote_refs, &remote_tail,
1933 nr_refspec, (const char **) refspec, push_all)) {
1934 rc = -1;
1935 goto cleanup;
1937 if (!remote_refs) {
1938 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
1939 rc = 0;
1940 goto cleanup;
1943 new_refs = 0;
1944 for (ref = remote_refs; ref; ref = ref->next) {
1945 char old_hex[60], *new_hex;
1946 const char *commit_argv[4];
1947 int commit_argc;
1948 char *new_sha1_hex, *old_sha1_hex;
1950 if (!ref->peer_ref)
1951 continue;
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);
1956 rc = -4;
1958 new_refs++;
1959 continue;
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);
1965 continue;
1968 if (!force_all &&
1969 !is_zero_sha1(ref->old_sha1) &&
1970 !ref->force) {
1971 if (!has_sha1_file(ref->old_sha1) ||
1972 !ref_newer(ref->peer_ref->new_sha1,
1973 ref->old_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"
1983 "local '%s'.\n"
1984 "Maybe you are not up-to-date and "
1985 "need to pull first?",
1986 ref->name,
1987 ref->peer_ref->name);
1988 rc = -2;
1989 continue;
1992 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
1993 new_refs++;
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);
2001 if (dry_run)
2002 continue;
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",
2008 ref->name);
2009 rc = 1;
2010 continue;
2013 /* Set up revision info for this refspec */
2014 commit_argc = 3;
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;
2024 commit_argc++;
2026 init_revisions(&revs, setup_git_directory());
2027 setup_revisions(commit_argc, commit_argv, &revs, NULL);
2028 revs.edge_hint = 0; /* just in case */
2029 free(new_sha1_hex);
2030 if (old_sha1_hex) {
2031 free(old_sha1_hex);
2032 commit_argv[1] = NULL;
2035 /* Generate a list of objects that need to be pushed */
2036 pushing = 0;
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. */
2045 pushing = 1;
2046 if (objects_to_send)
2047 fprintf(stderr, " sending %d objects\n",
2048 objects_to_send);
2050 run_request_queue();
2052 /* Update the remote branch if all went well */
2053 if (aborted || !update_remote(ref->new_sha1, ref_lock))
2054 rc = 1;
2056 if (!rc)
2057 fprintf(stderr, " done\n");
2058 unlock_remote(ref_lock);
2059 check_locks();
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");
2066 if (!dry_run)
2067 update_remote_info_refs(info_ref_lock);
2068 } else {
2069 fprintf(stderr, "Unable to update server info\n");
2073 cleanup:
2074 free(rewritten_url);
2075 if (info_ref_lock)
2076 unlock_remote(info_ref_lock);
2077 free(repo);
2079 http_cleanup();
2081 request = request_queue_head;
2082 while (request != NULL) {
2083 next_request = request->next;
2084 release_request(request);
2085 request = next_request;
2088 return rc;