Merge branch 'ab/i18n' into ab/test-1
[git/dscho.git] / http-push.c
blobfc2c5f7f652d25c0b26184250f54bc6ccb7c8dee
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"
13 #include "gettext.h"
15 #include <expat.h>
17 static const char http_push_usage[] =
18 "git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
20 #ifndef XML_STATUS_OK
21 enum XML_Status {
22 XML_STATUS_OK = 1,
23 XML_STATUS_ERROR = 0
25 #define XML_STATUS_OK 1
26 #define XML_STATUS_ERROR 0
27 #endif
29 #define PREV_BUF_SIZE 4096
31 /* DAV methods */
32 #define DAV_LOCK "LOCK"
33 #define DAV_MKCOL "MKCOL"
34 #define DAV_MOVE "MOVE"
35 #define DAV_PROPFIND "PROPFIND"
36 #define DAV_PUT "PUT"
37 #define DAV_UNLOCK "UNLOCK"
38 #define DAV_DELETE "DELETE"
40 /* DAV lock flags */
41 #define DAV_PROP_LOCKWR (1u << 0)
42 #define DAV_PROP_LOCKEX (1u << 1)
43 #define DAV_LOCK_OK (1u << 2)
45 /* DAV XML properties */
46 #define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
47 #define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
48 #define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
49 #define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
50 #define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
51 #define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
52 #define DAV_PROPFIND_RESP ".multistatus.response"
53 #define DAV_PROPFIND_NAME ".multistatus.response.href"
54 #define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
56 /* DAV request body templates */
57 #define PROPFIND_SUPPORTEDLOCK_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:prop xmlns:R=\"%s\">\n<D:supportedlock/>\n</D:prop>\n</D:propfind>"
58 #define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
59 #define LOCK_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:lockinfo xmlns:D=\"DAV:\">\n<D:lockscope><D:exclusive/></D:lockscope>\n<D:locktype><D:write/></D:locktype>\n<D:owner>\n<D:href>mailto:%s</D:href>\n</D:owner>\n</D:lockinfo>"
61 #define LOCK_TIME 600
62 #define LOCK_REFRESH 30
64 /* bits #0-15 in revision.h */
66 #define LOCAL (1u<<16)
67 #define REMOTE (1u<<17)
68 #define FETCHING (1u<<18)
69 #define PUSHING (1u<<19)
71 /* We allow "recursive" symbolic refs. Only within reason, though */
72 #define MAXDEPTH 5
74 static int pushing;
75 static int aborted;
76 static signed char remote_dir_exists[256];
78 static int push_verbosely;
79 static int push_all = MATCH_REFS_NONE;
80 static int force_all;
81 static int dry_run;
82 static int helper_status;
84 static struct object_list *objects;
86 struct repo
88 char *url;
89 char *path;
90 int path_len;
91 int has_info_refs;
92 int can_update_info_refs;
93 int has_info_packs;
94 struct packed_git *packs;
95 struct remote_lock *locks;
98 static struct repo *repo;
100 enum transfer_state {
101 NEED_FETCH,
102 RUN_FETCH_LOOSE,
103 RUN_FETCH_PACKED,
104 NEED_PUSH,
105 RUN_MKCOL,
106 RUN_PUT,
107 RUN_MOVE,
108 ABORTED,
109 COMPLETE
112 struct transfer_request
114 struct object *obj;
115 char *url;
116 char *dest;
117 struct remote_lock *lock;
118 struct curl_slist *headers;
119 struct buffer buffer;
120 enum transfer_state state;
121 CURLcode curl_result;
122 char errorstr[CURL_ERROR_SIZE];
123 long http_code;
124 void *userData;
125 struct active_request_slot *slot;
126 struct transfer_request *next;
129 static struct transfer_request *request_queue_head;
131 struct xml_ctx
133 char *name;
134 int len;
135 char *cdata;
136 void (*userFunc)(struct xml_ctx *ctx, int tag_closed);
137 void *userData;
140 struct remote_lock
142 char *url;
143 char *owner;
144 char *token;
145 char tmpfile_suffix[41];
146 time_t start_time;
147 long timeout;
148 int refreshing;
149 struct remote_lock *next;
152 /* Flags that control remote_ls processing */
153 #define PROCESS_FILES (1u << 0)
154 #define PROCESS_DIRS (1u << 1)
155 #define RECURSIVE (1u << 2)
157 /* Flags that remote_ls passes to callback functions */
158 #define IS_DIR (1u << 0)
160 struct remote_ls_ctx
162 char *path;
163 void (*userFunc)(struct remote_ls_ctx *ls);
164 void *userData;
165 int flags;
166 char *dentry_name;
167 int dentry_flags;
168 struct remote_ls_ctx *parent;
171 /* get_dav_token_headers options */
172 enum dav_header_flag {
173 DAV_HEADER_IF = (1u << 0),
174 DAV_HEADER_LOCK = (1u << 1),
175 DAV_HEADER_TIMEOUT = (1u << 2)
178 static char *xml_entities(char *s)
180 struct strbuf buf = STRBUF_INIT;
181 while (*s) {
182 size_t len = strcspn(s, "\"<>&");
183 strbuf_add(&buf, s, len);
184 s += len;
185 switch (*s) {
186 case '"':
187 strbuf_addstr(&buf, "&quot;");
188 break;
189 case '<':
190 strbuf_addstr(&buf, "&lt;");
191 break;
192 case '>':
193 strbuf_addstr(&buf, "&gt;");
194 break;
195 case '&':
196 strbuf_addstr(&buf, "&amp;");
197 break;
198 case 0:
199 return strbuf_detach(&buf, NULL);
201 s++;
203 return strbuf_detach(&buf, NULL);
206 static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum dav_header_flag options)
208 struct strbuf buf = STRBUF_INIT;
209 struct curl_slist *dav_headers = NULL;
211 if (options & DAV_HEADER_IF) {
212 strbuf_addf(&buf, "If: (<%s>)", lock->token);
213 dav_headers = curl_slist_append(dav_headers, buf.buf);
214 strbuf_reset(&buf);
216 if (options & DAV_HEADER_LOCK) {
217 strbuf_addf(&buf, "Lock-Token: <%s>", lock->token);
218 dav_headers = curl_slist_append(dav_headers, buf.buf);
219 strbuf_reset(&buf);
221 if (options & DAV_HEADER_TIMEOUT) {
222 strbuf_addf(&buf, "Timeout: Second-%ld", lock->timeout);
223 dav_headers = curl_slist_append(dav_headers, buf.buf);
224 strbuf_reset(&buf);
226 strbuf_release(&buf);
228 return dav_headers;
231 static void finish_request(struct transfer_request *request);
232 static void release_request(struct transfer_request *request);
234 static void process_response(void *callback_data)
236 struct transfer_request *request =
237 (struct transfer_request *)callback_data;
239 finish_request(request);
242 #ifdef USE_CURL_MULTI
244 static void start_fetch_loose(struct transfer_request *request)
246 struct active_request_slot *slot;
247 struct http_object_request *obj_req;
249 obj_req = new_http_object_request(repo->url, request->obj->sha1);
250 if (obj_req == NULL) {
251 request->state = ABORTED;
252 return;
255 slot = obj_req->slot;
256 slot->callback_func = process_response;
257 slot->callback_data = request;
258 request->slot = slot;
259 request->userData = obj_req;
261 /* Try to get the request started, abort the request on error */
262 request->state = RUN_FETCH_LOOSE;
263 if (!start_active_slot(slot)) {
264 fprintf(stderr, "Unable to start GET request\n");
265 repo->can_update_info_refs = 0;
266 release_http_object_request(obj_req);
267 release_request(request);
271 static void start_mkcol(struct transfer_request *request)
273 char *hex = sha1_to_hex(request->obj->sha1);
274 struct active_request_slot *slot;
276 request->url = get_remote_object_url(repo->url, hex, 1);
278 slot = get_active_slot();
279 slot->callback_func = process_response;
280 slot->callback_data = request;
281 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
282 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
283 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
284 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
285 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
287 if (start_active_slot(slot)) {
288 request->slot = slot;
289 request->state = RUN_MKCOL;
290 } else {
291 request->state = ABORTED;
292 free(request->url);
293 request->url = NULL;
296 #endif
298 static void start_fetch_packed(struct transfer_request *request)
300 struct packed_git *target;
302 struct transfer_request *check_request = request_queue_head;
303 struct http_pack_request *preq;
305 target = find_sha1_pack(request->obj->sha1, repo->packs);
306 if (!target) {
307 fprintf(stderr, "Unable to fetch %s, will not be able to update server info refs\n", sha1_to_hex(request->obj->sha1));
308 repo->can_update_info_refs = 0;
309 release_request(request);
310 return;
313 fprintf(stderr, "Fetching pack %s\n", sha1_to_hex(target->sha1));
314 fprintf(stderr, " which contains %s\n", sha1_to_hex(request->obj->sha1));
316 preq = new_http_pack_request(target, repo->url);
317 if (preq == NULL) {
318 release_http_pack_request(preq);
319 repo->can_update_info_refs = 0;
320 return;
322 preq->lst = &repo->packs;
324 /* Make sure there isn't another open request for this pack */
325 while (check_request) {
326 if (check_request->state == RUN_FETCH_PACKED &&
327 !strcmp(check_request->url, preq->url)) {
328 release_http_pack_request(preq);
329 release_request(request);
330 return;
332 check_request = check_request->next;
335 preq->slot->callback_func = process_response;
336 preq->slot->callback_data = request;
337 request->slot = preq->slot;
338 request->userData = preq;
340 /* Try to get the request started, abort the request on error */
341 request->state = RUN_FETCH_PACKED;
342 if (!start_active_slot(preq->slot)) {
343 fprintf(stderr, "Unable to start GET request\n");
344 release_http_pack_request(preq);
345 repo->can_update_info_refs = 0;
346 release_request(request);
350 static void start_put(struct transfer_request *request)
352 char *hex = sha1_to_hex(request->obj->sha1);
353 struct active_request_slot *slot;
354 struct strbuf buf = STRBUF_INIT;
355 enum object_type type;
356 char hdr[50];
357 void *unpacked;
358 unsigned long len;
359 int hdrlen;
360 ssize_t size;
361 z_stream stream;
363 unpacked = read_sha1_file(request->obj->sha1, &type, &len);
364 hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
366 /* Set it up */
367 memset(&stream, 0, sizeof(stream));
368 deflateInit(&stream, zlib_compression_level);
369 size = deflateBound(&stream, len + hdrlen);
370 strbuf_init(&request->buffer.buf, size);
371 request->buffer.posn = 0;
373 /* Compress it */
374 stream.next_out = (unsigned char *)request->buffer.buf.buf;
375 stream.avail_out = size;
377 /* First header.. */
378 stream.next_in = (void *)hdr;
379 stream.avail_in = hdrlen;
380 while (deflate(&stream, 0) == Z_OK)
381 /* nothing */;
383 /* Then the data itself.. */
384 stream.next_in = unpacked;
385 stream.avail_in = len;
386 while (deflate(&stream, Z_FINISH) == Z_OK)
387 /* nothing */;
388 deflateEnd(&stream);
389 free(unpacked);
391 request->buffer.buf.len = stream.total_out;
393 strbuf_addstr(&buf, "Destination: ");
394 append_remote_object_url(&buf, repo->url, hex, 0);
395 request->dest = strbuf_detach(&buf, NULL);
397 append_remote_object_url(&buf, repo->url, hex, 0);
398 strbuf_add(&buf, request->lock->tmpfile_suffix, 41);
399 request->url = strbuf_detach(&buf, NULL);
401 slot = get_active_slot();
402 slot->callback_func = process_response;
403 slot->callback_data = request;
404 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &request->buffer);
405 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, request->buffer.buf.len);
406 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
407 #ifndef NO_CURL_IOCTL
408 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
409 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &request->buffer);
410 #endif
411 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
412 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
413 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
414 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
415 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
416 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
418 if (start_active_slot(slot)) {
419 request->slot = slot;
420 request->state = RUN_PUT;
421 } else {
422 request->state = ABORTED;
423 free(request->url);
424 request->url = NULL;
428 static void start_move(struct transfer_request *request)
430 struct active_request_slot *slot;
431 struct curl_slist *dav_headers = NULL;
433 slot = get_active_slot();
434 slot->callback_func = process_response;
435 slot->callback_data = request;
436 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
437 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MOVE);
438 dav_headers = curl_slist_append(dav_headers, request->dest);
439 dav_headers = curl_slist_append(dav_headers, "Overwrite: T");
440 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
441 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
442 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
444 if (start_active_slot(slot)) {
445 request->slot = slot;
446 request->state = RUN_MOVE;
447 } else {
448 request->state = ABORTED;
449 free(request->url);
450 request->url = NULL;
454 static int refresh_lock(struct remote_lock *lock)
456 struct active_request_slot *slot;
457 struct slot_results results;
458 struct curl_slist *dav_headers;
459 int rc = 0;
461 lock->refreshing = 1;
463 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF | DAV_HEADER_TIMEOUT);
465 slot = get_active_slot();
466 slot->results = &results;
467 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
468 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
469 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
470 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
471 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
473 if (start_active_slot(slot)) {
474 run_active_slot(slot);
475 if (results.curl_result != CURLE_OK) {
476 fprintf(stderr, "LOCK HTTP error %ld\n",
477 results.http_code);
478 } else {
479 lock->start_time = time(NULL);
480 rc = 1;
484 lock->refreshing = 0;
485 curl_slist_free_all(dav_headers);
487 return rc;
490 static void check_locks(void)
492 struct remote_lock *lock = repo->locks;
493 time_t current_time = time(NULL);
494 int time_remaining;
496 while (lock) {
497 time_remaining = lock->start_time + lock->timeout -
498 current_time;
499 if (!lock->refreshing && time_remaining < LOCK_REFRESH) {
500 if (!refresh_lock(lock)) {
501 fprintf(stderr,
502 "Unable to refresh lock for %s\n",
503 lock->url);
504 aborted = 1;
505 return;
508 lock = lock->next;
512 static void release_request(struct transfer_request *request)
514 struct transfer_request *entry = request_queue_head;
516 if (request == request_queue_head) {
517 request_queue_head = request->next;
518 } else {
519 while (entry->next != NULL && entry->next != request)
520 entry = entry->next;
521 if (entry->next == request)
522 entry->next = entry->next->next;
525 free(request->url);
526 free(request);
529 static void finish_request(struct transfer_request *request)
531 struct http_pack_request *preq;
532 struct http_object_request *obj_req;
534 request->curl_result = request->slot->curl_result;
535 request->http_code = request->slot->http_code;
536 request->slot = NULL;
538 /* Keep locks active */
539 check_locks();
541 if (request->headers != NULL)
542 curl_slist_free_all(request->headers);
544 /* URL is reused for MOVE after PUT */
545 if (request->state != RUN_PUT) {
546 free(request->url);
547 request->url = NULL;
550 if (request->state == RUN_MKCOL) {
551 if (request->curl_result == CURLE_OK ||
552 request->http_code == 405) {
553 remote_dir_exists[request->obj->sha1[0]] = 1;
554 start_put(request);
555 } else {
556 fprintf(stderr, "MKCOL %s failed, aborting (%d/%ld)\n",
557 sha1_to_hex(request->obj->sha1),
558 request->curl_result, request->http_code);
559 request->state = ABORTED;
560 aborted = 1;
562 } else if (request->state == RUN_PUT) {
563 if (request->curl_result == CURLE_OK) {
564 start_move(request);
565 } else {
566 fprintf(stderr, "PUT %s failed, aborting (%d/%ld)\n",
567 sha1_to_hex(request->obj->sha1),
568 request->curl_result, request->http_code);
569 request->state = ABORTED;
570 aborted = 1;
572 } else if (request->state == RUN_MOVE) {
573 if (request->curl_result == CURLE_OK) {
574 if (push_verbosely)
575 fprintf(stderr, " sent %s\n",
576 sha1_to_hex(request->obj->sha1));
577 request->obj->flags |= REMOTE;
578 release_request(request);
579 } else {
580 fprintf(stderr, "MOVE %s failed, aborting (%d/%ld)\n",
581 sha1_to_hex(request->obj->sha1),
582 request->curl_result, request->http_code);
583 request->state = ABORTED;
584 aborted = 1;
586 } else if (request->state == RUN_FETCH_LOOSE) {
587 obj_req = (struct http_object_request *)request->userData;
589 if (finish_http_object_request(obj_req) == 0)
590 if (obj_req->rename == 0)
591 request->obj->flags |= (LOCAL | REMOTE);
593 /* Try fetching packed if necessary */
594 if (request->obj->flags & LOCAL) {
595 release_http_object_request(obj_req);
596 release_request(request);
597 } else
598 start_fetch_packed(request);
600 } else if (request->state == RUN_FETCH_PACKED) {
601 int fail = 1;
602 if (request->curl_result != CURLE_OK) {
603 fprintf(stderr, "Unable to get pack file %s\n%s",
604 request->url, curl_errorstr);
605 } else {
606 preq = (struct http_pack_request *)request->userData;
608 if (preq) {
609 if (finish_http_pack_request(preq) == 0)
610 fail = 0;
611 release_http_pack_request(preq);
614 if (fail)
615 repo->can_update_info_refs = 0;
616 release_request(request);
620 #ifdef USE_CURL_MULTI
621 static int is_running_queue;
622 static int fill_active_slot(void *unused)
624 struct transfer_request *request;
626 if (aborted || !is_running_queue)
627 return 0;
629 for (request = request_queue_head; request; request = request->next) {
630 if (request->state == NEED_FETCH) {
631 start_fetch_loose(request);
632 return 1;
633 } else if (pushing && request->state == NEED_PUSH) {
634 if (remote_dir_exists[request->obj->sha1[0]] == 1) {
635 start_put(request);
636 } else {
637 start_mkcol(request);
639 return 1;
642 return 0;
644 #endif
646 static void get_remote_object_list(unsigned char parent);
648 static void add_fetch_request(struct object *obj)
650 struct transfer_request *request;
652 check_locks();
655 * Don't fetch the object if it's known to exist locally
656 * or is already in the request queue
658 if (remote_dir_exists[obj->sha1[0]] == -1)
659 get_remote_object_list(obj->sha1[0]);
660 if (obj->flags & (LOCAL | FETCHING))
661 return;
663 obj->flags |= FETCHING;
664 request = xmalloc(sizeof(*request));
665 request->obj = obj;
666 request->url = NULL;
667 request->lock = NULL;
668 request->headers = NULL;
669 request->state = NEED_FETCH;
670 request->next = request_queue_head;
671 request_queue_head = request;
673 #ifdef USE_CURL_MULTI
674 fill_active_slots();
675 step_active_slots();
676 #endif
679 static int add_send_request(struct object *obj, struct remote_lock *lock)
681 struct transfer_request *request = request_queue_head;
682 struct packed_git *target;
684 /* Keep locks active */
685 check_locks();
688 * Don't push the object if it's known to exist on the remote
689 * or is already in the request queue
691 if (remote_dir_exists[obj->sha1[0]] == -1)
692 get_remote_object_list(obj->sha1[0]);
693 if (obj->flags & (REMOTE | PUSHING))
694 return 0;
695 target = find_sha1_pack(obj->sha1, repo->packs);
696 if (target) {
697 obj->flags |= REMOTE;
698 return 0;
701 obj->flags |= PUSHING;
702 request = xmalloc(sizeof(*request));
703 request->obj = obj;
704 request->url = NULL;
705 request->lock = lock;
706 request->headers = NULL;
707 request->state = NEED_PUSH;
708 request->next = request_queue_head;
709 request_queue_head = request;
711 #ifdef USE_CURL_MULTI
712 fill_active_slots();
713 step_active_slots();
714 #endif
716 return 1;
719 static int fetch_indices(void)
721 int ret;
723 if (push_verbosely)
724 fprintf(stderr, "Getting pack list\n");
726 switch (http_get_info_packs(repo->url, &repo->packs)) {
727 case HTTP_OK:
728 case HTTP_MISSING_TARGET:
729 ret = 0;
730 break;
731 default:
732 ret = -1;
735 return ret;
738 static void one_remote_object(const char *hex)
740 unsigned char sha1[20];
741 struct object *obj;
743 if (get_sha1_hex(hex, sha1) != 0)
744 return;
746 obj = lookup_object(sha1);
747 if (!obj)
748 obj = parse_object(sha1);
750 /* Ignore remote objects that don't exist locally */
751 if (!obj)
752 return;
754 obj->flags |= REMOTE;
755 if (!object_list_contains(objects, obj))
756 object_list_insert(obj, &objects);
759 static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed)
761 int *lock_flags = (int *)ctx->userData;
763 if (tag_closed) {
764 if (!strcmp(ctx->name, DAV_CTX_LOCKENTRY)) {
765 if ((*lock_flags & DAV_PROP_LOCKEX) &&
766 (*lock_flags & DAV_PROP_LOCKWR)) {
767 *lock_flags |= DAV_LOCK_OK;
769 *lock_flags &= DAV_LOCK_OK;
770 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_WRITE)) {
771 *lock_flags |= DAV_PROP_LOCKWR;
772 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_EXCLUSIVE)) {
773 *lock_flags |= DAV_PROP_LOCKEX;
778 static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
780 struct remote_lock *lock = (struct remote_lock *)ctx->userData;
781 git_SHA_CTX sha_ctx;
782 unsigned char lock_token_sha1[20];
784 if (tag_closed && ctx->cdata) {
785 if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) {
786 lock->owner = xmalloc(strlen(ctx->cdata) + 1);
787 strcpy(lock->owner, ctx->cdata);
788 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
789 if (!prefixcmp(ctx->cdata, "Second-"))
790 lock->timeout =
791 strtol(ctx->cdata + 7, NULL, 10);
792 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
793 lock->token = xmalloc(strlen(ctx->cdata) + 1);
794 strcpy(lock->token, ctx->cdata);
796 git_SHA1_Init(&sha_ctx);
797 git_SHA1_Update(&sha_ctx, lock->token, strlen(lock->token));
798 git_SHA1_Final(lock_token_sha1, &sha_ctx);
800 lock->tmpfile_suffix[0] = '_';
801 memcpy(lock->tmpfile_suffix + 1, sha1_to_hex(lock_token_sha1), 40);
806 static void one_remote_ref(char *refname);
808 static void
809 xml_start_tag(void *userData, const char *name, const char **atts)
811 struct xml_ctx *ctx = (struct xml_ctx *)userData;
812 const char *c = strchr(name, ':');
813 int new_len;
815 if (c == NULL)
816 c = name;
817 else
818 c++;
820 new_len = strlen(ctx->name) + strlen(c) + 2;
822 if (new_len > ctx->len) {
823 ctx->name = xrealloc(ctx->name, new_len);
824 ctx->len = new_len;
826 strcat(ctx->name, ".");
827 strcat(ctx->name, c);
829 free(ctx->cdata);
830 ctx->cdata = NULL;
832 ctx->userFunc(ctx, 0);
835 static void
836 xml_end_tag(void *userData, const char *name)
838 struct xml_ctx *ctx = (struct xml_ctx *)userData;
839 const char *c = strchr(name, ':');
840 char *ep;
842 ctx->userFunc(ctx, 1);
844 if (c == NULL)
845 c = name;
846 else
847 c++;
849 ep = ctx->name + strlen(ctx->name) - strlen(c) - 1;
850 *ep = 0;
853 static void
854 xml_cdata(void *userData, const XML_Char *s, int len)
856 struct xml_ctx *ctx = (struct xml_ctx *)userData;
857 free(ctx->cdata);
858 ctx->cdata = xmemdupz(s, len);
861 static struct remote_lock *lock_remote(const char *path, long timeout)
863 struct active_request_slot *slot;
864 struct slot_results results;
865 struct buffer out_buffer = { STRBUF_INIT, 0 };
866 struct strbuf in_buffer = STRBUF_INIT;
867 char *url;
868 char *ep;
869 char timeout_header[25];
870 struct remote_lock *lock = NULL;
871 struct curl_slist *dav_headers = NULL;
872 struct xml_ctx ctx;
873 char *escaped;
875 url = xmalloc(strlen(repo->url) + strlen(path) + 1);
876 sprintf(url, "%s%s", repo->url, path);
878 /* Make sure leading directories exist for the remote ref */
879 ep = strchr(url + strlen(repo->url) + 1, '/');
880 while (ep) {
881 char saved_character = ep[1];
882 ep[1] = '\0';
883 slot = get_active_slot();
884 slot->results = &results;
885 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
886 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
887 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
888 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
889 if (start_active_slot(slot)) {
890 run_active_slot(slot);
891 if (results.curl_result != CURLE_OK &&
892 results.http_code != 405) {
893 fprintf(stderr,
894 "Unable to create branch path %s\n",
895 url);
896 free(url);
897 return NULL;
899 } else {
900 fprintf(stderr, "Unable to start MKCOL request\n");
901 free(url);
902 return NULL;
904 ep[1] = saved_character;
905 ep = strchr(ep + 1, '/');
908 escaped = xml_entities(git_default_email);
909 strbuf_addf(&out_buffer.buf, LOCK_REQUEST, escaped);
910 free(escaped);
912 sprintf(timeout_header, "Timeout: Second-%ld", timeout);
913 dav_headers = curl_slist_append(dav_headers, timeout_header);
914 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
916 slot = get_active_slot();
917 slot->results = &results;
918 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
919 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
920 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
921 #ifndef NO_CURL_IOCTL
922 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
923 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
924 #endif
925 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
926 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
927 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
928 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
929 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
930 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
932 lock = xcalloc(1, sizeof(*lock));
933 lock->timeout = -1;
935 if (start_active_slot(slot)) {
936 run_active_slot(slot);
937 if (results.curl_result == CURLE_OK) {
938 XML_Parser parser = XML_ParserCreate(NULL);
939 enum XML_Status result;
940 ctx.name = xcalloc(10, 1);
941 ctx.len = 0;
942 ctx.cdata = NULL;
943 ctx.userFunc = handle_new_lock_ctx;
944 ctx.userData = lock;
945 XML_SetUserData(parser, &ctx);
946 XML_SetElementHandler(parser, xml_start_tag,
947 xml_end_tag);
948 XML_SetCharacterDataHandler(parser, xml_cdata);
949 result = XML_Parse(parser, in_buffer.buf,
950 in_buffer.len, 1);
951 free(ctx.name);
952 if (result != XML_STATUS_OK) {
953 fprintf(stderr, "XML error: %s\n",
954 XML_ErrorString(
955 XML_GetErrorCode(parser)));
956 lock->timeout = -1;
958 XML_ParserFree(parser);
960 } else {
961 fprintf(stderr, "Unable to start LOCK request\n");
964 curl_slist_free_all(dav_headers);
965 strbuf_release(&out_buffer.buf);
966 strbuf_release(&in_buffer);
968 if (lock->token == NULL || lock->timeout <= 0) {
969 free(lock->token);
970 free(lock->owner);
971 free(url);
972 free(lock);
973 lock = NULL;
974 } else {
975 lock->url = url;
976 lock->start_time = time(NULL);
977 lock->next = repo->locks;
978 repo->locks = lock;
981 return lock;
984 static int unlock_remote(struct remote_lock *lock)
986 struct active_request_slot *slot;
987 struct slot_results results;
988 struct remote_lock *prev = repo->locks;
989 struct curl_slist *dav_headers;
990 int rc = 0;
992 dav_headers = get_dav_token_headers(lock, DAV_HEADER_LOCK);
994 slot = get_active_slot();
995 slot->results = &results;
996 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
997 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
998 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_UNLOCK);
999 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1001 if (start_active_slot(slot)) {
1002 run_active_slot(slot);
1003 if (results.curl_result == CURLE_OK)
1004 rc = 1;
1005 else
1006 fprintf(stderr, "UNLOCK HTTP error %ld\n",
1007 results.http_code);
1008 } else {
1009 fprintf(stderr, "Unable to start UNLOCK request\n");
1012 curl_slist_free_all(dav_headers);
1014 if (repo->locks == lock) {
1015 repo->locks = lock->next;
1016 } else {
1017 while (prev && prev->next != lock)
1018 prev = prev->next;
1019 if (prev)
1020 prev->next = prev->next->next;
1023 free(lock->owner);
1024 free(lock->url);
1025 free(lock->token);
1026 free(lock);
1028 return rc;
1031 static void remove_locks(void)
1033 struct remote_lock *lock = repo->locks;
1035 fprintf(stderr, "Removing remote locks...\n");
1036 while (lock) {
1037 struct remote_lock *next = lock->next;
1038 unlock_remote(lock);
1039 lock = next;
1043 static void remove_locks_on_signal(int signo)
1045 remove_locks();
1046 sigchain_pop(signo);
1047 raise(signo);
1050 static void remote_ls(const char *path, int flags,
1051 void (*userFunc)(struct remote_ls_ctx *ls),
1052 void *userData);
1054 static void process_ls_object(struct remote_ls_ctx *ls)
1056 unsigned int *parent = (unsigned int *)ls->userData;
1057 char *path = ls->dentry_name;
1058 char *obj_hex;
1060 if (!strcmp(ls->path, ls->dentry_name) && (ls->flags & IS_DIR)) {
1061 remote_dir_exists[*parent] = 1;
1062 return;
1065 if (strlen(path) != 49)
1066 return;
1067 path += 8;
1068 obj_hex = xmalloc(strlen(path));
1069 /* NB: path is not null-terminated, can not use strlcpy here */
1070 memcpy(obj_hex, path, 2);
1071 strcpy(obj_hex + 2, path + 3);
1072 one_remote_object(obj_hex);
1073 free(obj_hex);
1076 static void process_ls_ref(struct remote_ls_ctx *ls)
1078 if (!strcmp(ls->path, ls->dentry_name) && (ls->dentry_flags & IS_DIR)) {
1079 fprintf(stderr, " %s\n", ls->dentry_name);
1080 return;
1083 if (!(ls->dentry_flags & IS_DIR))
1084 one_remote_ref(ls->dentry_name);
1087 static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
1089 struct remote_ls_ctx *ls = (struct remote_ls_ctx *)ctx->userData;
1091 if (tag_closed) {
1092 if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) {
1093 if (ls->dentry_flags & IS_DIR) {
1094 if (ls->flags & PROCESS_DIRS) {
1095 ls->userFunc(ls);
1097 if (strcmp(ls->dentry_name, ls->path) &&
1098 ls->flags & RECURSIVE) {
1099 remote_ls(ls->dentry_name,
1100 ls->flags,
1101 ls->userFunc,
1102 ls->userData);
1104 } else if (ls->flags & PROCESS_FILES) {
1105 ls->userFunc(ls);
1107 } else if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
1108 char *path = ctx->cdata;
1109 if (*ctx->cdata == 'h') {
1110 path = strstr(path, "//");
1111 if (path) {
1112 path = strchr(path+2, '/');
1115 if (path) {
1116 path += repo->path_len;
1117 ls->dentry_name = xstrdup(path);
1119 } else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
1120 ls->dentry_flags |= IS_DIR;
1122 } else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
1123 free(ls->dentry_name);
1124 ls->dentry_name = NULL;
1125 ls->dentry_flags = 0;
1130 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1131 * should _only_ heed the information from that file, instead of trying to
1132 * determine the refs from the remote file system (badly: it does not even
1133 * know about packed-refs).
1135 static void remote_ls(const char *path, int flags,
1136 void (*userFunc)(struct remote_ls_ctx *ls),
1137 void *userData)
1139 char *url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1140 struct active_request_slot *slot;
1141 struct slot_results results;
1142 struct strbuf in_buffer = STRBUF_INIT;
1143 struct buffer out_buffer = { STRBUF_INIT, 0 };
1144 struct curl_slist *dav_headers = NULL;
1145 struct xml_ctx ctx;
1146 struct remote_ls_ctx ls;
1148 ls.flags = flags;
1149 ls.path = xstrdup(path);
1150 ls.dentry_name = NULL;
1151 ls.dentry_flags = 0;
1152 ls.userData = userData;
1153 ls.userFunc = userFunc;
1155 sprintf(url, "%s%s", repo->url, path);
1157 strbuf_addf(&out_buffer.buf, PROPFIND_ALL_REQUEST);
1159 dav_headers = curl_slist_append(dav_headers, "Depth: 1");
1160 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1162 slot = get_active_slot();
1163 slot->results = &results;
1164 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1165 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1166 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1167 #ifndef NO_CURL_IOCTL
1168 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1169 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
1170 #endif
1171 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1172 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1173 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1174 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1175 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1176 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1178 if (start_active_slot(slot)) {
1179 run_active_slot(slot);
1180 if (results.curl_result == CURLE_OK) {
1181 XML_Parser parser = XML_ParserCreate(NULL);
1182 enum XML_Status result;
1183 ctx.name = xcalloc(10, 1);
1184 ctx.len = 0;
1185 ctx.cdata = NULL;
1186 ctx.userFunc = handle_remote_ls_ctx;
1187 ctx.userData = &ls;
1188 XML_SetUserData(parser, &ctx);
1189 XML_SetElementHandler(parser, xml_start_tag,
1190 xml_end_tag);
1191 XML_SetCharacterDataHandler(parser, xml_cdata);
1192 result = XML_Parse(parser, in_buffer.buf,
1193 in_buffer.len, 1);
1194 free(ctx.name);
1196 if (result != XML_STATUS_OK) {
1197 fprintf(stderr, "XML error: %s\n",
1198 XML_ErrorString(
1199 XML_GetErrorCode(parser)));
1201 XML_ParserFree(parser);
1203 } else {
1204 fprintf(stderr, "Unable to start PROPFIND request\n");
1207 free(ls.path);
1208 free(url);
1209 strbuf_release(&out_buffer.buf);
1210 strbuf_release(&in_buffer);
1211 curl_slist_free_all(dav_headers);
1214 static void get_remote_object_list(unsigned char parent)
1216 char path[] = "objects/XX/";
1217 static const char hex[] = "0123456789abcdef";
1218 unsigned int val = parent;
1220 path[8] = hex[val >> 4];
1221 path[9] = hex[val & 0xf];
1222 remote_dir_exists[val] = 0;
1223 remote_ls(path, (PROCESS_FILES | PROCESS_DIRS),
1224 process_ls_object, &val);
1227 static int locking_available(void)
1229 struct active_request_slot *slot;
1230 struct slot_results results;
1231 struct strbuf in_buffer = STRBUF_INIT;
1232 struct buffer out_buffer = { STRBUF_INIT, 0 };
1233 struct curl_slist *dav_headers = NULL;
1234 struct xml_ctx ctx;
1235 int lock_flags = 0;
1236 char *escaped;
1238 escaped = xml_entities(repo->url);
1239 strbuf_addf(&out_buffer.buf, PROPFIND_SUPPORTEDLOCK_REQUEST, escaped);
1240 free(escaped);
1242 dav_headers = curl_slist_append(dav_headers, "Depth: 0");
1243 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1245 slot = get_active_slot();
1246 slot->results = &results;
1247 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1248 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1249 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1250 #ifndef NO_CURL_IOCTL
1251 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1252 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
1253 #endif
1254 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1255 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1256 curl_easy_setopt(slot->curl, CURLOPT_URL, repo->url);
1257 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1258 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1259 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1261 if (start_active_slot(slot)) {
1262 run_active_slot(slot);
1263 if (results.curl_result == CURLE_OK) {
1264 XML_Parser parser = XML_ParserCreate(NULL);
1265 enum XML_Status result;
1266 ctx.name = xcalloc(10, 1);
1267 ctx.len = 0;
1268 ctx.cdata = NULL;
1269 ctx.userFunc = handle_lockprop_ctx;
1270 ctx.userData = &lock_flags;
1271 XML_SetUserData(parser, &ctx);
1272 XML_SetElementHandler(parser, xml_start_tag,
1273 xml_end_tag);
1274 result = XML_Parse(parser, in_buffer.buf,
1275 in_buffer.len, 1);
1276 free(ctx.name);
1278 if (result != XML_STATUS_OK) {
1279 fprintf(stderr, "XML error: %s\n",
1280 XML_ErrorString(
1281 XML_GetErrorCode(parser)));
1282 lock_flags = 0;
1284 XML_ParserFree(parser);
1285 if (!lock_flags)
1286 error("no DAV locking support on %s",
1287 repo->url);
1289 } else {
1290 error("Cannot access URL %s, return code %d",
1291 repo->url, results.curl_result);
1292 lock_flags = 0;
1294 } else {
1295 error("Unable to start PROPFIND request on %s", repo->url);
1298 strbuf_release(&out_buffer.buf);
1299 strbuf_release(&in_buffer);
1300 curl_slist_free_all(dav_headers);
1302 return lock_flags;
1305 static struct object_list **add_one_object(struct object *obj, struct object_list **p)
1307 struct object_list *entry = xmalloc(sizeof(struct object_list));
1308 entry->item = obj;
1309 entry->next = *p;
1310 *p = entry;
1311 return &entry->next;
1314 static struct object_list **process_blob(struct blob *blob,
1315 struct object_list **p,
1316 struct name_path *path,
1317 const char *name)
1319 struct object *obj = &blob->object;
1321 obj->flags |= LOCAL;
1323 if (obj->flags & (UNINTERESTING | SEEN))
1324 return p;
1326 obj->flags |= SEEN;
1327 return add_one_object(obj, p);
1330 static struct object_list **process_tree(struct tree *tree,
1331 struct object_list **p,
1332 struct name_path *path,
1333 const char *name)
1335 struct object *obj = &tree->object;
1336 struct tree_desc desc;
1337 struct name_entry entry;
1338 struct name_path me;
1340 obj->flags |= LOCAL;
1342 if (obj->flags & (UNINTERESTING | SEEN))
1343 return p;
1344 if (parse_tree(tree) < 0)
1345 die("bad tree object %s", sha1_to_hex(obj->sha1));
1347 obj->flags |= SEEN;
1348 name = xstrdup(name);
1349 p = add_one_object(obj, p);
1350 me.up = path;
1351 me.elem = name;
1352 me.elem_len = strlen(name);
1354 init_tree_desc(&desc, tree->buffer, tree->size);
1356 while (tree_entry(&desc, &entry))
1357 switch (object_type(entry.mode)) {
1358 case OBJ_TREE:
1359 p = process_tree(lookup_tree(entry.sha1), p, &me, name);
1360 break;
1361 case OBJ_BLOB:
1362 p = process_blob(lookup_blob(entry.sha1), p, &me, name);
1363 break;
1364 default:
1365 /* Subproject commit - not in this repository */
1366 break;
1369 free(tree->buffer);
1370 tree->buffer = NULL;
1371 return p;
1374 static int get_delta(struct rev_info *revs, struct remote_lock *lock)
1376 int i;
1377 struct commit *commit;
1378 struct object_list **p = &objects;
1379 int count = 0;
1381 while ((commit = get_revision(revs)) != NULL) {
1382 p = process_tree(commit->tree, p, NULL, "");
1383 commit->object.flags |= LOCAL;
1384 if (!(commit->object.flags & UNINTERESTING))
1385 count += add_send_request(&commit->object, lock);
1388 for (i = 0; i < revs->pending.nr; i++) {
1389 struct object_array_entry *entry = revs->pending.objects + i;
1390 struct object *obj = entry->item;
1391 const char *name = entry->name;
1393 if (obj->flags & (UNINTERESTING | SEEN))
1394 continue;
1395 if (obj->type == OBJ_TAG) {
1396 obj->flags |= SEEN;
1397 p = add_one_object(obj, p);
1398 continue;
1400 if (obj->type == OBJ_TREE) {
1401 p = process_tree((struct tree *)obj, p, NULL, name);
1402 continue;
1404 if (obj->type == OBJ_BLOB) {
1405 p = process_blob((struct blob *)obj, p, NULL, name);
1406 continue;
1408 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
1411 while (objects) {
1412 if (!(objects->item->flags & UNINTERESTING))
1413 count += add_send_request(objects->item, lock);
1414 objects = objects->next;
1417 return count;
1420 static int update_remote(unsigned char *sha1, struct remote_lock *lock)
1422 struct active_request_slot *slot;
1423 struct slot_results results;
1424 struct buffer out_buffer = { STRBUF_INIT, 0 };
1425 struct curl_slist *dav_headers;
1427 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1429 strbuf_addf(&out_buffer.buf, "%s\n", sha1_to_hex(sha1));
1431 slot = get_active_slot();
1432 slot->results = &results;
1433 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1434 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1435 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1436 #ifndef NO_CURL_IOCTL
1437 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1438 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
1439 #endif
1440 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1441 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1442 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1443 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1444 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1445 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1447 if (start_active_slot(slot)) {
1448 run_active_slot(slot);
1449 strbuf_release(&out_buffer.buf);
1450 if (results.curl_result != CURLE_OK) {
1451 fprintf(stderr,
1452 "PUT error: curl result=%d, HTTP code=%ld\n",
1453 results.curl_result, results.http_code);
1454 /* We should attempt recovery? */
1455 return 0;
1457 } else {
1458 strbuf_release(&out_buffer.buf);
1459 fprintf(stderr, "Unable to start PUT request\n");
1460 return 0;
1463 return 1;
1466 static struct ref *remote_refs;
1468 static void one_remote_ref(char *refname)
1470 struct ref *ref;
1471 struct object *obj;
1473 ref = alloc_ref(refname);
1475 if (http_fetch_ref(repo->url, ref) != 0) {
1476 fprintf(stderr,
1477 "Unable to fetch ref %s from %s\n",
1478 refname, repo->url);
1479 free(ref);
1480 return;
1484 * Fetch a copy of the object if it doesn't exist locally - it
1485 * may be required for updating server info later.
1487 if (repo->can_update_info_refs && !has_sha1_file(ref->old_sha1)) {
1488 obj = lookup_unknown_object(ref->old_sha1);
1489 if (obj) {
1490 fprintf(stderr, " fetch %s for %s\n",
1491 sha1_to_hex(ref->old_sha1), refname);
1492 add_fetch_request(obj);
1496 ref->next = remote_refs;
1497 remote_refs = ref;
1500 static void get_dav_remote_heads(void)
1502 remote_ls("refs/", (PROCESS_FILES | PROCESS_DIRS | RECURSIVE), process_ls_ref, NULL);
1505 static void add_remote_info_ref(struct remote_ls_ctx *ls)
1507 struct strbuf *buf = (struct strbuf *)ls->userData;
1508 struct object *o;
1509 int len;
1510 char *ref_info;
1511 struct ref *ref;
1513 ref = alloc_ref(ls->dentry_name);
1515 if (http_fetch_ref(repo->url, ref) != 0) {
1516 fprintf(stderr,
1517 "Unable to fetch ref %s from %s\n",
1518 ls->dentry_name, repo->url);
1519 aborted = 1;
1520 free(ref);
1521 return;
1524 o = parse_object(ref->old_sha1);
1525 if (!o) {
1526 fprintf(stderr,
1527 "Unable to parse object %s for remote ref %s\n",
1528 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1529 aborted = 1;
1530 free(ref);
1531 return;
1534 len = strlen(ls->dentry_name) + 42;
1535 ref_info = xcalloc(len + 1, 1);
1536 sprintf(ref_info, "%s %s\n",
1537 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1538 fwrite_buffer(ref_info, 1, len, buf);
1539 free(ref_info);
1541 if (o->type == OBJ_TAG) {
1542 o = deref_tag(o, ls->dentry_name, 0);
1543 if (o) {
1544 len = strlen(ls->dentry_name) + 45;
1545 ref_info = xcalloc(len + 1, 1);
1546 sprintf(ref_info, "%s %s^{}\n",
1547 sha1_to_hex(o->sha1), ls->dentry_name);
1548 fwrite_buffer(ref_info, 1, len, buf);
1549 free(ref_info);
1552 free(ref);
1555 static void update_remote_info_refs(struct remote_lock *lock)
1557 struct buffer buffer = { STRBUF_INIT, 0 };
1558 struct active_request_slot *slot;
1559 struct slot_results results;
1560 struct curl_slist *dav_headers;
1562 remote_ls("refs/", (PROCESS_FILES | RECURSIVE),
1563 add_remote_info_ref, &buffer.buf);
1564 if (!aborted) {
1565 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1567 slot = get_active_slot();
1568 slot->results = &results;
1569 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &buffer);
1570 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, buffer.buf.len);
1571 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1572 #ifndef NO_CURL_IOCTL
1573 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1574 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &buffer);
1575 #endif
1576 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1577 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1578 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1579 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1580 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1581 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1583 if (start_active_slot(slot)) {
1584 run_active_slot(slot);
1585 if (results.curl_result != CURLE_OK) {
1586 fprintf(stderr,
1587 "PUT error: curl result=%d, HTTP code=%ld\n",
1588 results.curl_result, results.http_code);
1592 strbuf_release(&buffer.buf);
1595 static int remote_exists(const char *path)
1597 char *url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1598 int ret;
1600 sprintf(url, "%s%s", repo->url, path);
1602 switch (http_get_strbuf(url, NULL, 0)) {
1603 case HTTP_OK:
1604 ret = 1;
1605 break;
1606 case HTTP_MISSING_TARGET:
1607 ret = 0;
1608 break;
1609 case HTTP_ERROR:
1610 http_error(url, HTTP_ERROR);
1611 default:
1612 ret = -1;
1614 free(url);
1615 return ret;
1618 static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
1620 char *url;
1621 struct strbuf buffer = STRBUF_INIT;
1623 url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1624 sprintf(url, "%s%s", repo->url, path);
1626 if (http_get_strbuf(url, &buffer, 0) != HTTP_OK)
1627 die("Couldn't get %s for remote symref\n%s", url,
1628 curl_errorstr);
1629 free(url);
1631 free(*symref);
1632 *symref = NULL;
1633 hashclr(sha1);
1635 if (buffer.len == 0)
1636 return;
1638 /* If it's a symref, set the refname; otherwise try for a sha1 */
1639 if (!prefixcmp((char *)buffer.buf, "ref: ")) {
1640 *symref = xmemdupz((char *)buffer.buf + 5, buffer.len - 6);
1641 } else {
1642 get_sha1_hex(buffer.buf, sha1);
1645 strbuf_release(&buffer);
1648 static int verify_merge_base(unsigned char *head_sha1, unsigned char *branch_sha1)
1650 struct commit *head = lookup_commit(head_sha1);
1651 struct commit *branch = lookup_commit(branch_sha1);
1652 struct commit_list *merge_bases = get_merge_bases(head, branch, 1);
1654 return (merge_bases && !merge_bases->next && merge_bases->item == branch);
1657 static int delete_remote_branch(char *pattern, int force)
1659 struct ref *refs = remote_refs;
1660 struct ref *remote_ref = NULL;
1661 unsigned char head_sha1[20];
1662 char *symref = NULL;
1663 int match;
1664 int patlen = strlen(pattern);
1665 int i;
1666 struct active_request_slot *slot;
1667 struct slot_results results;
1668 char *url;
1670 /* Find the remote branch(es) matching the specified branch name */
1671 for (match = 0; refs; refs = refs->next) {
1672 char *name = refs->name;
1673 int namelen = strlen(name);
1674 if (namelen < patlen ||
1675 memcmp(name + namelen - patlen, pattern, patlen))
1676 continue;
1677 if (namelen != patlen && name[namelen - patlen - 1] != '/')
1678 continue;
1679 match++;
1680 remote_ref = refs;
1682 if (match == 0)
1683 return error("No remote branch matches %s", pattern);
1684 if (match != 1)
1685 return error("More than one remote branch matches %s",
1686 pattern);
1689 * Remote HEAD must be a symref (not exactly foolproof; a remote
1690 * symlink to a symref will look like a symref)
1692 fetch_symref("HEAD", &symref, head_sha1);
1693 if (!symref)
1694 return error("Remote HEAD is not a symref");
1696 /* Remote branch must not be the remote HEAD */
1697 for (i=0; symref && i<MAXDEPTH; i++) {
1698 if (!strcmp(remote_ref->name, symref))
1699 return error("Remote branch %s is the current HEAD",
1700 remote_ref->name);
1701 fetch_symref(symref, &symref, head_sha1);
1704 /* Run extra sanity checks if delete is not forced */
1705 if (!force) {
1706 /* Remote HEAD must resolve to a known object */
1707 if (symref)
1708 return error("Remote HEAD symrefs too deep");
1709 if (is_null_sha1(head_sha1))
1710 return error("Unable to resolve remote HEAD");
1711 if (!has_sha1_file(head_sha1))
1712 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1));
1714 /* Remote branch must resolve to a known object */
1715 if (is_null_sha1(remote_ref->old_sha1))
1716 return error("Unable to resolve remote branch %s",
1717 remote_ref->name);
1718 if (!has_sha1_file(remote_ref->old_sha1))
1719 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));
1721 /* Remote branch must be an ancestor of remote HEAD */
1722 if (!verify_merge_base(head_sha1, remote_ref->old_sha1)) {
1723 return error("The branch '%s' is not an ancestor "
1724 "of your current HEAD.\n"
1725 "If you are sure you want to delete it,"
1726 " run:\n\t'git http-push -D %s %s'",
1727 remote_ref->name, repo->url, pattern);
1731 /* Send delete request */
1732 fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name);
1733 if (dry_run)
1734 return 0;
1735 url = xmalloc(strlen(repo->url) + strlen(remote_ref->name) + 1);
1736 sprintf(url, "%s%s", repo->url, remote_ref->name);
1737 slot = get_active_slot();
1738 slot->results = &results;
1739 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
1740 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1741 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1742 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_DELETE);
1743 if (start_active_slot(slot)) {
1744 run_active_slot(slot);
1745 free(url);
1746 if (results.curl_result != CURLE_OK)
1747 return error("DELETE request failed (%d/%ld)\n",
1748 results.curl_result, results.http_code);
1749 } else {
1750 free(url);
1751 return error("Unable to start DELETE request");
1754 return 0;
1757 static void run_request_queue(void)
1759 #ifdef USE_CURL_MULTI
1760 is_running_queue = 1;
1761 fill_active_slots();
1762 add_fill_function(NULL, fill_active_slot);
1763 #endif
1764 do {
1765 finish_all_active_slots();
1766 #ifdef USE_CURL_MULTI
1767 fill_active_slots();
1768 #endif
1769 } while (request_queue_head && !aborted);
1771 #ifdef USE_CURL_MULTI
1772 is_running_queue = 0;
1773 #endif
1776 int main(int argc, char **argv)
1778 struct transfer_request *request;
1779 struct transfer_request *next_request;
1780 int nr_refspec = 0;
1781 char **refspec = NULL;
1782 struct remote_lock *ref_lock = NULL;
1783 struct remote_lock *info_ref_lock = NULL;
1784 struct rev_info revs;
1785 int delete_branch = 0;
1786 int force_delete = 0;
1787 int objects_to_send;
1788 int rc = 0;
1789 int i;
1790 int new_refs;
1791 struct ref *ref, *local_refs;
1792 struct remote *remote;
1793 char *rewritten_url = NULL;
1795 git_setup_gettext();
1797 git_extract_argv0_path(argv[0]);
1799 repo = xcalloc(sizeof(*repo), 1);
1801 argv++;
1802 for (i = 1; i < argc; i++, argv++) {
1803 char *arg = *argv;
1805 if (*arg == '-') {
1806 if (!strcmp(arg, "--all")) {
1807 push_all = MATCH_REFS_ALL;
1808 continue;
1810 if (!strcmp(arg, "--force")) {
1811 force_all = 1;
1812 continue;
1814 if (!strcmp(arg, "--dry-run")) {
1815 dry_run = 1;
1816 continue;
1818 if (!strcmp(arg, "--helper-status")) {
1819 helper_status = 1;
1820 continue;
1822 if (!strcmp(arg, "--verbose")) {
1823 push_verbosely = 1;
1824 http_is_verbose = 1;
1825 continue;
1827 if (!strcmp(arg, "-d")) {
1828 delete_branch = 1;
1829 continue;
1831 if (!strcmp(arg, "-D")) {
1832 delete_branch = 1;
1833 force_delete = 1;
1834 continue;
1836 if (!strcmp(arg, "-h"))
1837 usage(http_push_usage);
1839 if (!repo->url) {
1840 char *path = strstr(arg, "//");
1841 repo->url = arg;
1842 repo->path_len = strlen(arg);
1843 if (path) {
1844 repo->path = strchr(path+2, '/');
1845 if (repo->path)
1846 repo->path_len = strlen(repo->path);
1848 continue;
1850 refspec = argv;
1851 nr_refspec = argc - i;
1852 break;
1855 #ifndef USE_CURL_MULTI
1856 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
1857 #endif
1859 if (!repo->url)
1860 usage(http_push_usage);
1862 if (delete_branch && nr_refspec != 1)
1863 die("You must specify only one branch name when deleting a remote branch");
1865 setup_git_directory();
1867 memset(remote_dir_exists, -1, 256);
1870 * Create a minimum remote by hand to give to http_init(),
1871 * primarily to allow it to look at the URL.
1873 remote = xcalloc(sizeof(*remote), 1);
1874 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
1875 remote->url[remote->url_nr++] = repo->url;
1876 http_init(remote);
1878 if (repo->url && repo->url[strlen(repo->url)-1] != '/') {
1879 rewritten_url = xmalloc(strlen(repo->url)+2);
1880 strcpy(rewritten_url, repo->url);
1881 strcat(rewritten_url, "/");
1882 repo->path = rewritten_url + (repo->path - repo->url);
1883 repo->path_len++;
1884 repo->url = rewritten_url;
1887 #ifdef USE_CURL_MULTI
1888 is_running_queue = 0;
1889 #endif
1891 /* Verify DAV compliance/lock support */
1892 if (!locking_available()) {
1893 rc = 1;
1894 goto cleanup;
1897 sigchain_push_common(remove_locks_on_signal);
1899 /* Check whether the remote has server info files */
1900 repo->can_update_info_refs = 0;
1901 repo->has_info_refs = remote_exists("info/refs");
1902 repo->has_info_packs = remote_exists("objects/info/packs");
1903 if (repo->has_info_refs) {
1904 info_ref_lock = lock_remote("info/refs", LOCK_TIME);
1905 if (info_ref_lock)
1906 repo->can_update_info_refs = 1;
1907 else {
1908 error("cannot lock existing info/refs");
1909 rc = 1;
1910 goto cleanup;
1913 if (repo->has_info_packs)
1914 fetch_indices();
1916 /* Get a list of all local and remote heads to validate refspecs */
1917 local_refs = get_local_heads();
1918 fprintf(stderr, "Fetching remote heads...\n");
1919 get_dav_remote_heads();
1920 run_request_queue();
1922 /* Remove a remote branch if -d or -D was specified */
1923 if (delete_branch) {
1924 if (delete_remote_branch(refspec[0], force_delete) == -1) {
1925 fprintf(stderr, "Unable to delete remote branch %s\n",
1926 refspec[0]);
1927 if (helper_status)
1928 printf("error %s cannot remove\n", refspec[0]);
1930 goto cleanup;
1933 /* match them up */
1934 if (match_refs(local_refs, &remote_refs,
1935 nr_refspec, (const char **) refspec, push_all)) {
1936 rc = -1;
1937 goto cleanup;
1939 if (!remote_refs) {
1940 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
1941 if (helper_status)
1942 printf("error null no match\n");
1943 rc = 0;
1944 goto cleanup;
1947 new_refs = 0;
1948 for (ref = remote_refs; ref; ref = ref->next) {
1949 char old_hex[60], *new_hex;
1950 const char *commit_argv[5];
1951 int commit_argc;
1952 char *new_sha1_hex, *old_sha1_hex;
1954 if (!ref->peer_ref)
1955 continue;
1957 if (is_null_sha1(ref->peer_ref->new_sha1)) {
1958 if (delete_remote_branch(ref->name, 1) == -1) {
1959 error("Could not remove %s", ref->name);
1960 if (helper_status)
1961 printf("error %s cannot remove\n", ref->name);
1962 rc = -4;
1964 else if (helper_status)
1965 printf("ok %s\n", ref->name);
1966 new_refs++;
1967 continue;
1970 if (!hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
1971 if (push_verbosely)
1972 fprintf(stderr, "'%s': up-to-date\n", ref->name);
1973 if (helper_status)
1974 printf("ok %s up to date\n", ref->name);
1975 continue;
1978 if (!force_all &&
1979 !is_null_sha1(ref->old_sha1) &&
1980 !ref->force) {
1981 if (!has_sha1_file(ref->old_sha1) ||
1982 !ref_newer(ref->peer_ref->new_sha1,
1983 ref->old_sha1)) {
1985 * We do not have the remote ref, or
1986 * we know that the remote ref is not
1987 * an ancestor of what we are trying to
1988 * push. Either way this can be losing
1989 * commits at the remote end and likely
1990 * we were not up to date to begin with.
1992 error("remote '%s' is not an ancestor of\n"
1993 "local '%s'.\n"
1994 "Maybe you are not up-to-date and "
1995 "need to pull first?",
1996 ref->name,
1997 ref->peer_ref->name);
1998 if (helper_status)
1999 printf("error %s non-fast forward\n", ref->name);
2000 rc = -2;
2001 continue;
2004 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
2005 new_refs++;
2006 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
2007 new_hex = sha1_to_hex(ref->new_sha1);
2009 fprintf(stderr, "updating '%s'", ref->name);
2010 if (strcmp(ref->name, ref->peer_ref->name))
2011 fprintf(stderr, " using '%s'", ref->peer_ref->name);
2012 fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex);
2013 if (dry_run) {
2014 if (helper_status)
2015 printf("ok %s\n", ref->name);
2016 continue;
2019 /* Lock remote branch ref */
2020 ref_lock = lock_remote(ref->name, LOCK_TIME);
2021 if (ref_lock == NULL) {
2022 fprintf(stderr, "Unable to lock remote branch %s\n",
2023 ref->name);
2024 if (helper_status)
2025 printf("error %s lock error\n", ref->name);
2026 rc = 1;
2027 continue;
2030 /* Set up revision info for this refspec */
2031 commit_argc = 3;
2032 new_sha1_hex = xstrdup(sha1_to_hex(ref->new_sha1));
2033 old_sha1_hex = NULL;
2034 commit_argv[1] = "--objects";
2035 commit_argv[2] = new_sha1_hex;
2036 if (!push_all && !is_null_sha1(ref->old_sha1)) {
2037 old_sha1_hex = xmalloc(42);
2038 sprintf(old_sha1_hex, "^%s",
2039 sha1_to_hex(ref->old_sha1));
2040 commit_argv[3] = old_sha1_hex;
2041 commit_argc++;
2043 commit_argv[commit_argc] = NULL;
2044 init_revisions(&revs, setup_git_directory());
2045 setup_revisions(commit_argc, commit_argv, &revs, NULL);
2046 revs.edge_hint = 0; /* just in case */
2047 free(new_sha1_hex);
2048 if (old_sha1_hex) {
2049 free(old_sha1_hex);
2050 commit_argv[1] = NULL;
2053 /* Generate a list of objects that need to be pushed */
2054 pushing = 0;
2055 if (prepare_revision_walk(&revs))
2056 die("revision walk setup failed");
2057 mark_edges_uninteresting(revs.commits, &revs, NULL);
2058 objects_to_send = get_delta(&revs, ref_lock);
2059 finish_all_active_slots();
2061 /* Push missing objects to remote, this would be a
2062 convenient time to pack them first if appropriate. */
2063 pushing = 1;
2064 if (objects_to_send)
2065 fprintf(stderr, " sending %d objects\n",
2066 objects_to_send);
2068 run_request_queue();
2070 /* Update the remote branch if all went well */
2071 if (aborted || !update_remote(ref->new_sha1, ref_lock))
2072 rc = 1;
2074 if (!rc)
2075 fprintf(stderr, " done\n");
2076 if (helper_status)
2077 printf("%s %s\n", !rc ? "ok" : "error", ref->name);
2078 unlock_remote(ref_lock);
2079 check_locks();
2082 /* Update remote server info if appropriate */
2083 if (repo->has_info_refs && new_refs) {
2084 if (info_ref_lock && repo->can_update_info_refs) {
2085 fprintf(stderr, "Updating remote server info\n");
2086 if (!dry_run)
2087 update_remote_info_refs(info_ref_lock);
2088 } else {
2089 fprintf(stderr, "Unable to update server info\n");
2093 cleanup:
2094 free(rewritten_url);
2095 if (info_ref_lock)
2096 unlock_remote(info_ref_lock);
2097 free(repo);
2099 http_cleanup();
2101 request = request_queue_head;
2102 while (request != NULL) {
2103 next_request = request->next;
2104 release_request(request);
2105 request = next_request;
2108 return rc;