git.txt: fix list continuation
[git/raj.git] / http-push.c
blobd18346c0f5c9ce73f3fe9a1efc7b1b63e7380bb8
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;
81 static int helper_status;
83 static struct object_list *objects;
85 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 {
111 struct object *obj;
112 char *url;
113 char *dest;
114 struct remote_lock *lock;
115 struct curl_slist *headers;
116 struct buffer buffer;
117 enum transfer_state state;
118 CURLcode curl_result;
119 char errorstr[CURL_ERROR_SIZE];
120 long http_code;
121 void *userData;
122 struct active_request_slot *slot;
123 struct transfer_request *next;
126 static struct transfer_request *request_queue_head;
128 struct xml_ctx {
129 char *name;
130 int len;
131 char *cdata;
132 void (*userFunc)(struct xml_ctx *ctx, int tag_closed);
133 void *userData;
136 struct remote_lock {
137 char *url;
138 char *owner;
139 char *token;
140 char tmpfile_suffix[41];
141 time_t start_time;
142 long timeout;
143 int refreshing;
144 struct remote_lock *next;
147 /* Flags that control remote_ls processing */
148 #define PROCESS_FILES (1u << 0)
149 #define PROCESS_DIRS (1u << 1)
150 #define RECURSIVE (1u << 2)
152 /* Flags that remote_ls passes to callback functions */
153 #define IS_DIR (1u << 0)
155 struct remote_ls_ctx {
156 char *path;
157 void (*userFunc)(struct remote_ls_ctx *ls);
158 void *userData;
159 int flags;
160 char *dentry_name;
161 int dentry_flags;
162 struct remote_ls_ctx *parent;
165 /* get_dav_token_headers options */
166 enum dav_header_flag {
167 DAV_HEADER_IF = (1u << 0),
168 DAV_HEADER_LOCK = (1u << 1),
169 DAV_HEADER_TIMEOUT = (1u << 2)
172 static char *xml_entities(char *s)
174 struct strbuf buf = STRBUF_INIT;
175 while (*s) {
176 size_t len = strcspn(s, "\"<>&");
177 strbuf_add(&buf, s, len);
178 s += len;
179 switch (*s) {
180 case '"':
181 strbuf_addstr(&buf, "&quot;");
182 break;
183 case '<':
184 strbuf_addstr(&buf, "&lt;");
185 break;
186 case '>':
187 strbuf_addstr(&buf, "&gt;");
188 break;
189 case '&':
190 strbuf_addstr(&buf, "&amp;");
191 break;
192 case 0:
193 return strbuf_detach(&buf, NULL);
195 s++;
197 return strbuf_detach(&buf, NULL);
200 static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum dav_header_flag options)
202 struct strbuf buf = STRBUF_INIT;
203 struct curl_slist *dav_headers = NULL;
205 if (options & DAV_HEADER_IF) {
206 strbuf_addf(&buf, "If: (<%s>)", lock->token);
207 dav_headers = curl_slist_append(dav_headers, buf.buf);
208 strbuf_reset(&buf);
210 if (options & DAV_HEADER_LOCK) {
211 strbuf_addf(&buf, "Lock-Token: <%s>", lock->token);
212 dav_headers = curl_slist_append(dav_headers, buf.buf);
213 strbuf_reset(&buf);
215 if (options & DAV_HEADER_TIMEOUT) {
216 strbuf_addf(&buf, "Timeout: Second-%ld", lock->timeout);
217 dav_headers = curl_slist_append(dav_headers, buf.buf);
218 strbuf_reset(&buf);
220 strbuf_release(&buf);
222 return dav_headers;
225 static void finish_request(struct transfer_request *request);
226 static void release_request(struct transfer_request *request);
228 static void process_response(void *callback_data)
230 struct transfer_request *request =
231 (struct transfer_request *)callback_data;
233 finish_request(request);
236 #ifdef USE_CURL_MULTI
238 static void start_fetch_loose(struct transfer_request *request)
240 struct active_request_slot *slot;
241 struct http_object_request *obj_req;
243 obj_req = new_http_object_request(repo->url, request->obj->sha1);
244 if (obj_req == NULL) {
245 request->state = ABORTED;
246 return;
249 slot = obj_req->slot;
250 slot->callback_func = process_response;
251 slot->callback_data = request;
252 request->slot = slot;
253 request->userData = obj_req;
255 /* Try to get the request started, abort the request on error */
256 request->state = RUN_FETCH_LOOSE;
257 if (!start_active_slot(slot)) {
258 fprintf(stderr, "Unable to start GET request\n");
259 repo->can_update_info_refs = 0;
260 release_http_object_request(obj_req);
261 release_request(request);
265 static void start_mkcol(struct transfer_request *request)
267 char *hex = sha1_to_hex(request->obj->sha1);
268 struct active_request_slot *slot;
270 request->url = get_remote_object_url(repo->url, hex, 1);
272 slot = get_active_slot();
273 slot->callback_func = process_response;
274 slot->callback_data = request;
275 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
276 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
277 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
278 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
279 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
281 if (start_active_slot(slot)) {
282 request->slot = slot;
283 request->state = RUN_MKCOL;
284 } else {
285 request->state = ABORTED;
286 free(request->url);
287 request->url = NULL;
290 #endif
292 static void start_fetch_packed(struct transfer_request *request)
294 struct packed_git *target;
296 struct transfer_request *check_request = request_queue_head;
297 struct http_pack_request *preq;
299 target = find_sha1_pack(request->obj->sha1, repo->packs);
300 if (!target) {
301 fprintf(stderr, "Unable to fetch %s, will not be able to update server info refs\n", sha1_to_hex(request->obj->sha1));
302 repo->can_update_info_refs = 0;
303 release_request(request);
304 return;
307 fprintf(stderr, "Fetching pack %s\n", sha1_to_hex(target->sha1));
308 fprintf(stderr, " which contains %s\n", sha1_to_hex(request->obj->sha1));
310 preq = new_http_pack_request(target, repo->url);
311 if (preq == NULL) {
312 release_http_pack_request(preq);
313 repo->can_update_info_refs = 0;
314 return;
316 preq->lst = &repo->packs;
318 /* Make sure there isn't another open request for this pack */
319 while (check_request) {
320 if (check_request->state == RUN_FETCH_PACKED &&
321 !strcmp(check_request->url, preq->url)) {
322 release_http_pack_request(preq);
323 release_request(request);
324 return;
326 check_request = check_request->next;
329 preq->slot->callback_func = process_response;
330 preq->slot->callback_data = request;
331 request->slot = preq->slot;
332 request->userData = preq;
334 /* Try to get the request started, abort the request on error */
335 request->state = RUN_FETCH_PACKED;
336 if (!start_active_slot(preq->slot)) {
337 fprintf(stderr, "Unable to start GET request\n");
338 release_http_pack_request(preq);
339 repo->can_update_info_refs = 0;
340 release_request(request);
344 static void start_put(struct transfer_request *request)
346 char *hex = sha1_to_hex(request->obj->sha1);
347 struct active_request_slot *slot;
348 struct strbuf buf = STRBUF_INIT;
349 enum object_type type;
350 char hdr[50];
351 void *unpacked;
352 unsigned long len;
353 int hdrlen;
354 ssize_t size;
355 z_stream stream;
357 unpacked = read_sha1_file(request->obj->sha1, &type, &len);
358 hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
360 /* Set it up */
361 memset(&stream, 0, sizeof(stream));
362 deflateInit(&stream, zlib_compression_level);
363 size = deflateBound(&stream, len + hdrlen);
364 strbuf_init(&request->buffer.buf, size);
365 request->buffer.posn = 0;
367 /* Compress it */
368 stream.next_out = (unsigned char *)request->buffer.buf.buf;
369 stream.avail_out = size;
371 /* First header.. */
372 stream.next_in = (void *)hdr;
373 stream.avail_in = hdrlen;
374 while (deflate(&stream, 0) == Z_OK)
375 /* nothing */;
377 /* Then the data itself.. */
378 stream.next_in = unpacked;
379 stream.avail_in = len;
380 while (deflate(&stream, Z_FINISH) == Z_OK)
381 /* nothing */;
382 deflateEnd(&stream);
383 free(unpacked);
385 request->buffer.buf.len = stream.total_out;
387 strbuf_addstr(&buf, "Destination: ");
388 append_remote_object_url(&buf, repo->url, hex, 0);
389 request->dest = strbuf_detach(&buf, NULL);
391 append_remote_object_url(&buf, repo->url, hex, 0);
392 strbuf_add(&buf, request->lock->tmpfile_suffix, 41);
393 request->url = strbuf_detach(&buf, NULL);
395 slot = get_active_slot();
396 slot->callback_func = process_response;
397 slot->callback_data = request;
398 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &request->buffer);
399 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, request->buffer.buf.len);
400 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
401 #ifndef NO_CURL_IOCTL
402 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
403 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &request->buffer);
404 #endif
405 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
406 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
407 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
408 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
409 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
410 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
412 if (start_active_slot(slot)) {
413 request->slot = slot;
414 request->state = RUN_PUT;
415 } else {
416 request->state = ABORTED;
417 free(request->url);
418 request->url = NULL;
422 static void start_move(struct transfer_request *request)
424 struct active_request_slot *slot;
425 struct curl_slist *dav_headers = NULL;
427 slot = get_active_slot();
428 slot->callback_func = process_response;
429 slot->callback_data = request;
430 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
431 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MOVE);
432 dav_headers = curl_slist_append(dav_headers, request->dest);
433 dav_headers = curl_slist_append(dav_headers, "Overwrite: T");
434 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
435 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
436 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
438 if (start_active_slot(slot)) {
439 request->slot = slot;
440 request->state = RUN_MOVE;
441 } else {
442 request->state = ABORTED;
443 free(request->url);
444 request->url = NULL;
448 static int refresh_lock(struct remote_lock *lock)
450 struct active_request_slot *slot;
451 struct slot_results results;
452 struct curl_slist *dav_headers;
453 int rc = 0;
455 lock->refreshing = 1;
457 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF | DAV_HEADER_TIMEOUT);
459 slot = get_active_slot();
460 slot->results = &results;
461 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
462 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
463 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
464 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
465 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
467 if (start_active_slot(slot)) {
468 run_active_slot(slot);
469 if (results.curl_result != CURLE_OK) {
470 fprintf(stderr, "LOCK HTTP error %ld\n",
471 results.http_code);
472 } else {
473 lock->start_time = time(NULL);
474 rc = 1;
478 lock->refreshing = 0;
479 curl_slist_free_all(dav_headers);
481 return rc;
484 static void check_locks(void)
486 struct remote_lock *lock = repo->locks;
487 time_t current_time = time(NULL);
488 int time_remaining;
490 while (lock) {
491 time_remaining = lock->start_time + lock->timeout -
492 current_time;
493 if (!lock->refreshing && time_remaining < LOCK_REFRESH) {
494 if (!refresh_lock(lock)) {
495 fprintf(stderr,
496 "Unable to refresh lock for %s\n",
497 lock->url);
498 aborted = 1;
499 return;
502 lock = lock->next;
506 static void release_request(struct transfer_request *request)
508 struct transfer_request *entry = request_queue_head;
510 if (request == request_queue_head) {
511 request_queue_head = request->next;
512 } else {
513 while (entry->next != NULL && entry->next != request)
514 entry = entry->next;
515 if (entry->next == request)
516 entry->next = entry->next->next;
519 free(request->url);
520 free(request);
523 static void finish_request(struct transfer_request *request)
525 struct http_pack_request *preq;
526 struct http_object_request *obj_req;
528 request->curl_result = request->slot->curl_result;
529 request->http_code = request->slot->http_code;
530 request->slot = NULL;
532 /* Keep locks active */
533 check_locks();
535 if (request->headers != NULL)
536 curl_slist_free_all(request->headers);
538 /* URL is reused for MOVE after PUT */
539 if (request->state != RUN_PUT) {
540 free(request->url);
541 request->url = NULL;
544 if (request->state == RUN_MKCOL) {
545 if (request->curl_result == CURLE_OK ||
546 request->http_code == 405) {
547 remote_dir_exists[request->obj->sha1[0]] = 1;
548 start_put(request);
549 } else {
550 fprintf(stderr, "MKCOL %s failed, aborting (%d/%ld)\n",
551 sha1_to_hex(request->obj->sha1),
552 request->curl_result, request->http_code);
553 request->state = ABORTED;
554 aborted = 1;
556 } else if (request->state == RUN_PUT) {
557 if (request->curl_result == CURLE_OK) {
558 start_move(request);
559 } else {
560 fprintf(stderr, "PUT %s failed, aborting (%d/%ld)\n",
561 sha1_to_hex(request->obj->sha1),
562 request->curl_result, request->http_code);
563 request->state = ABORTED;
564 aborted = 1;
566 } else if (request->state == RUN_MOVE) {
567 if (request->curl_result == CURLE_OK) {
568 if (push_verbosely)
569 fprintf(stderr, " sent %s\n",
570 sha1_to_hex(request->obj->sha1));
571 request->obj->flags |= REMOTE;
572 release_request(request);
573 } else {
574 fprintf(stderr, "MOVE %s failed, aborting (%d/%ld)\n",
575 sha1_to_hex(request->obj->sha1),
576 request->curl_result, request->http_code);
577 request->state = ABORTED;
578 aborted = 1;
580 } else if (request->state == RUN_FETCH_LOOSE) {
581 obj_req = (struct http_object_request *)request->userData;
583 if (finish_http_object_request(obj_req) == 0)
584 if (obj_req->rename == 0)
585 request->obj->flags |= (LOCAL | REMOTE);
587 /* Try fetching packed if necessary */
588 if (request->obj->flags & LOCAL) {
589 release_http_object_request(obj_req);
590 release_request(request);
591 } else
592 start_fetch_packed(request);
594 } else if (request->state == RUN_FETCH_PACKED) {
595 int fail = 1;
596 if (request->curl_result != CURLE_OK) {
597 fprintf(stderr, "Unable to get pack file %s\n%s",
598 request->url, curl_errorstr);
599 } else {
600 preq = (struct http_pack_request *)request->userData;
602 if (preq) {
603 if (finish_http_pack_request(preq) == 0)
604 fail = 0;
605 release_http_pack_request(preq);
608 if (fail)
609 repo->can_update_info_refs = 0;
610 release_request(request);
614 #ifdef USE_CURL_MULTI
615 static int is_running_queue;
616 static int fill_active_slot(void *unused)
618 struct transfer_request *request;
620 if (aborted || !is_running_queue)
621 return 0;
623 for (request = request_queue_head; request; request = request->next) {
624 if (request->state == NEED_FETCH) {
625 start_fetch_loose(request);
626 return 1;
627 } else if (pushing && request->state == NEED_PUSH) {
628 if (remote_dir_exists[request->obj->sha1[0]] == 1) {
629 start_put(request);
630 } else {
631 start_mkcol(request);
633 return 1;
636 return 0;
638 #endif
640 static void get_remote_object_list(unsigned char parent);
642 static void add_fetch_request(struct object *obj)
644 struct transfer_request *request;
646 check_locks();
649 * Don't fetch the object if it's known to exist locally
650 * or is already in the request queue
652 if (remote_dir_exists[obj->sha1[0]] == -1)
653 get_remote_object_list(obj->sha1[0]);
654 if (obj->flags & (LOCAL | FETCHING))
655 return;
657 obj->flags |= FETCHING;
658 request = xmalloc(sizeof(*request));
659 request->obj = obj;
660 request->url = NULL;
661 request->lock = NULL;
662 request->headers = NULL;
663 request->state = NEED_FETCH;
664 request->next = request_queue_head;
665 request_queue_head = request;
667 #ifdef USE_CURL_MULTI
668 fill_active_slots();
669 step_active_slots();
670 #endif
673 static int add_send_request(struct object *obj, struct remote_lock *lock)
675 struct transfer_request *request = request_queue_head;
676 struct packed_git *target;
678 /* Keep locks active */
679 check_locks();
682 * Don't push the object if it's known to exist on the remote
683 * or is already in the request queue
685 if (remote_dir_exists[obj->sha1[0]] == -1)
686 get_remote_object_list(obj->sha1[0]);
687 if (obj->flags & (REMOTE | PUSHING))
688 return 0;
689 target = find_sha1_pack(obj->sha1, repo->packs);
690 if (target) {
691 obj->flags |= REMOTE;
692 return 0;
695 obj->flags |= PUSHING;
696 request = xmalloc(sizeof(*request));
697 request->obj = obj;
698 request->url = NULL;
699 request->lock = lock;
700 request->headers = NULL;
701 request->state = NEED_PUSH;
702 request->next = request_queue_head;
703 request_queue_head = request;
705 #ifdef USE_CURL_MULTI
706 fill_active_slots();
707 step_active_slots();
708 #endif
710 return 1;
713 static int fetch_indices(void)
715 int ret;
717 if (push_verbosely)
718 fprintf(stderr, "Getting pack list\n");
720 switch (http_get_info_packs(repo->url, &repo->packs)) {
721 case HTTP_OK:
722 case HTTP_MISSING_TARGET:
723 ret = 0;
724 break;
725 default:
726 ret = -1;
729 return ret;
732 static void one_remote_object(const char *hex)
734 unsigned char sha1[20];
735 struct object *obj;
737 if (get_sha1_hex(hex, sha1) != 0)
738 return;
740 obj = lookup_object(sha1);
741 if (!obj)
742 obj = parse_object(sha1);
744 /* Ignore remote objects that don't exist locally */
745 if (!obj)
746 return;
748 obj->flags |= REMOTE;
749 if (!object_list_contains(objects, obj))
750 object_list_insert(obj, &objects);
753 static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed)
755 int *lock_flags = (int *)ctx->userData;
757 if (tag_closed) {
758 if (!strcmp(ctx->name, DAV_CTX_LOCKENTRY)) {
759 if ((*lock_flags & DAV_PROP_LOCKEX) &&
760 (*lock_flags & DAV_PROP_LOCKWR)) {
761 *lock_flags |= DAV_LOCK_OK;
763 *lock_flags &= DAV_LOCK_OK;
764 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_WRITE)) {
765 *lock_flags |= DAV_PROP_LOCKWR;
766 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_EXCLUSIVE)) {
767 *lock_flags |= DAV_PROP_LOCKEX;
772 static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
774 struct remote_lock *lock = (struct remote_lock *)ctx->userData;
775 git_SHA_CTX sha_ctx;
776 unsigned char lock_token_sha1[20];
778 if (tag_closed && ctx->cdata) {
779 if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) {
780 lock->owner = xmalloc(strlen(ctx->cdata) + 1);
781 strcpy(lock->owner, ctx->cdata);
782 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
783 if (!prefixcmp(ctx->cdata, "Second-"))
784 lock->timeout =
785 strtol(ctx->cdata + 7, NULL, 10);
786 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
787 lock->token = xmalloc(strlen(ctx->cdata) + 1);
788 strcpy(lock->token, ctx->cdata);
790 git_SHA1_Init(&sha_ctx);
791 git_SHA1_Update(&sha_ctx, lock->token, strlen(lock->token));
792 git_SHA1_Final(lock_token_sha1, &sha_ctx);
794 lock->tmpfile_suffix[0] = '_';
795 memcpy(lock->tmpfile_suffix + 1, sha1_to_hex(lock_token_sha1), 40);
800 static void one_remote_ref(char *refname);
802 static void
803 xml_start_tag(void *userData, const char *name, const char **atts)
805 struct xml_ctx *ctx = (struct xml_ctx *)userData;
806 const char *c = strchr(name, ':');
807 int new_len;
809 if (c == NULL)
810 c = name;
811 else
812 c++;
814 new_len = strlen(ctx->name) + strlen(c) + 2;
816 if (new_len > ctx->len) {
817 ctx->name = xrealloc(ctx->name, new_len);
818 ctx->len = new_len;
820 strcat(ctx->name, ".");
821 strcat(ctx->name, c);
823 free(ctx->cdata);
824 ctx->cdata = NULL;
826 ctx->userFunc(ctx, 0);
829 static void
830 xml_end_tag(void *userData, const char *name)
832 struct xml_ctx *ctx = (struct xml_ctx *)userData;
833 const char *c = strchr(name, ':');
834 char *ep;
836 ctx->userFunc(ctx, 1);
838 if (c == NULL)
839 c = name;
840 else
841 c++;
843 ep = ctx->name + strlen(ctx->name) - strlen(c) - 1;
844 *ep = 0;
847 static void
848 xml_cdata(void *userData, const XML_Char *s, int len)
850 struct xml_ctx *ctx = (struct xml_ctx *)userData;
851 free(ctx->cdata);
852 ctx->cdata = xmemdupz(s, len);
855 static struct remote_lock *lock_remote(const char *path, long timeout)
857 struct active_request_slot *slot;
858 struct slot_results results;
859 struct buffer out_buffer = { STRBUF_INIT, 0 };
860 struct strbuf in_buffer = STRBUF_INIT;
861 char *url;
862 char *ep;
863 char timeout_header[25];
864 struct remote_lock *lock = NULL;
865 struct curl_slist *dav_headers = NULL;
866 struct xml_ctx ctx;
867 char *escaped;
869 url = xmalloc(strlen(repo->url) + strlen(path) + 1);
870 sprintf(url, "%s%s", repo->url, path);
872 /* Make sure leading directories exist for the remote ref */
873 ep = strchr(url + strlen(repo->url) + 1, '/');
874 while (ep) {
875 char saved_character = ep[1];
876 ep[1] = '\0';
877 slot = get_active_slot();
878 slot->results = &results;
879 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
880 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
881 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
882 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
883 if (start_active_slot(slot)) {
884 run_active_slot(slot);
885 if (results.curl_result != CURLE_OK &&
886 results.http_code != 405) {
887 fprintf(stderr,
888 "Unable to create branch path %s\n",
889 url);
890 free(url);
891 return NULL;
893 } else {
894 fprintf(stderr, "Unable to start MKCOL request\n");
895 free(url);
896 return NULL;
898 ep[1] = saved_character;
899 ep = strchr(ep + 1, '/');
902 escaped = xml_entities(git_default_email);
903 strbuf_addf(&out_buffer.buf, LOCK_REQUEST, escaped);
904 free(escaped);
906 sprintf(timeout_header, "Timeout: Second-%ld", timeout);
907 dav_headers = curl_slist_append(dav_headers, timeout_header);
908 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
910 slot = get_active_slot();
911 slot->results = &results;
912 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
913 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
914 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
915 #ifndef NO_CURL_IOCTL
916 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
917 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
918 #endif
919 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
920 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
921 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
922 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
923 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
924 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
926 lock = xcalloc(1, sizeof(*lock));
927 lock->timeout = -1;
929 if (start_active_slot(slot)) {
930 run_active_slot(slot);
931 if (results.curl_result == CURLE_OK) {
932 XML_Parser parser = XML_ParserCreate(NULL);
933 enum XML_Status result;
934 ctx.name = xcalloc(10, 1);
935 ctx.len = 0;
936 ctx.cdata = NULL;
937 ctx.userFunc = handle_new_lock_ctx;
938 ctx.userData = lock;
939 XML_SetUserData(parser, &ctx);
940 XML_SetElementHandler(parser, xml_start_tag,
941 xml_end_tag);
942 XML_SetCharacterDataHandler(parser, xml_cdata);
943 result = XML_Parse(parser, in_buffer.buf,
944 in_buffer.len, 1);
945 free(ctx.name);
946 if (result != XML_STATUS_OK) {
947 fprintf(stderr, "XML error: %s\n",
948 XML_ErrorString(
949 XML_GetErrorCode(parser)));
950 lock->timeout = -1;
952 XML_ParserFree(parser);
954 } else {
955 fprintf(stderr, "Unable to start LOCK request\n");
958 curl_slist_free_all(dav_headers);
959 strbuf_release(&out_buffer.buf);
960 strbuf_release(&in_buffer);
962 if (lock->token == NULL || lock->timeout <= 0) {
963 free(lock->token);
964 free(lock->owner);
965 free(url);
966 free(lock);
967 lock = NULL;
968 } else {
969 lock->url = url;
970 lock->start_time = time(NULL);
971 lock->next = repo->locks;
972 repo->locks = lock;
975 return lock;
978 static int unlock_remote(struct remote_lock *lock)
980 struct active_request_slot *slot;
981 struct slot_results results;
982 struct remote_lock *prev = repo->locks;
983 struct curl_slist *dav_headers;
984 int rc = 0;
986 dav_headers = get_dav_token_headers(lock, DAV_HEADER_LOCK);
988 slot = get_active_slot();
989 slot->results = &results;
990 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
991 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
992 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_UNLOCK);
993 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
995 if (start_active_slot(slot)) {
996 run_active_slot(slot);
997 if (results.curl_result == CURLE_OK)
998 rc = 1;
999 else
1000 fprintf(stderr, "UNLOCK HTTP error %ld\n",
1001 results.http_code);
1002 } else {
1003 fprintf(stderr, "Unable to start UNLOCK request\n");
1006 curl_slist_free_all(dav_headers);
1008 if (repo->locks == lock) {
1009 repo->locks = lock->next;
1010 } else {
1011 while (prev && prev->next != lock)
1012 prev = prev->next;
1013 if (prev)
1014 prev->next = prev->next->next;
1017 free(lock->owner);
1018 free(lock->url);
1019 free(lock->token);
1020 free(lock);
1022 return rc;
1025 static void remove_locks(void)
1027 struct remote_lock *lock = repo->locks;
1029 fprintf(stderr, "Removing remote locks...\n");
1030 while (lock) {
1031 struct remote_lock *next = lock->next;
1032 unlock_remote(lock);
1033 lock = next;
1037 static void remove_locks_on_signal(int signo)
1039 remove_locks();
1040 sigchain_pop(signo);
1041 raise(signo);
1044 static void remote_ls(const char *path, int flags,
1045 void (*userFunc)(struct remote_ls_ctx *ls),
1046 void *userData);
1048 static void process_ls_object(struct remote_ls_ctx *ls)
1050 unsigned int *parent = (unsigned int *)ls->userData;
1051 char *path = ls->dentry_name;
1052 char *obj_hex;
1054 if (!strcmp(ls->path, ls->dentry_name) && (ls->flags & IS_DIR)) {
1055 remote_dir_exists[*parent] = 1;
1056 return;
1059 if (strlen(path) != 49)
1060 return;
1061 path += 8;
1062 obj_hex = xmalloc(strlen(path));
1063 /* NB: path is not null-terminated, can not use strlcpy here */
1064 memcpy(obj_hex, path, 2);
1065 strcpy(obj_hex + 2, path + 3);
1066 one_remote_object(obj_hex);
1067 free(obj_hex);
1070 static void process_ls_ref(struct remote_ls_ctx *ls)
1072 if (!strcmp(ls->path, ls->dentry_name) && (ls->dentry_flags & IS_DIR)) {
1073 fprintf(stderr, " %s\n", ls->dentry_name);
1074 return;
1077 if (!(ls->dentry_flags & IS_DIR))
1078 one_remote_ref(ls->dentry_name);
1081 static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
1083 struct remote_ls_ctx *ls = (struct remote_ls_ctx *)ctx->userData;
1085 if (tag_closed) {
1086 if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) {
1087 if (ls->dentry_flags & IS_DIR) {
1089 /* ensure collection names end with slash */
1090 str_end_url_with_slash(ls->dentry_name, &ls->dentry_name);
1092 if (ls->flags & PROCESS_DIRS) {
1093 ls->userFunc(ls);
1095 if (strcmp(ls->dentry_name, ls->path) &&
1096 ls->flags & RECURSIVE) {
1097 remote_ls(ls->dentry_name,
1098 ls->flags,
1099 ls->userFunc,
1100 ls->userData);
1102 } else if (ls->flags & PROCESS_FILES) {
1103 ls->userFunc(ls);
1105 } else if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
1106 char *path = ctx->cdata;
1107 if (*ctx->cdata == 'h') {
1108 path = strstr(path, "//");
1109 if (path) {
1110 path = strchr(path+2, '/');
1113 if (path) {
1114 const char *url = repo->url;
1115 if (repo->path)
1116 url = repo->path;
1117 if (strncmp(path, url, repo->path_len))
1118 error("Parsed path '%s' does not match url: '%s'\n",
1119 path, url);
1120 else {
1121 path += repo->path_len;
1122 ls->dentry_name = xstrdup(path);
1125 } else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
1126 ls->dentry_flags |= IS_DIR;
1128 } else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
1129 free(ls->dentry_name);
1130 ls->dentry_name = NULL;
1131 ls->dentry_flags = 0;
1136 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1137 * should _only_ heed the information from that file, instead of trying to
1138 * determine the refs from the remote file system (badly: it does not even
1139 * know about packed-refs).
1141 static void remote_ls(const char *path, int flags,
1142 void (*userFunc)(struct remote_ls_ctx *ls),
1143 void *userData)
1145 char *url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1146 struct active_request_slot *slot;
1147 struct slot_results results;
1148 struct strbuf in_buffer = STRBUF_INIT;
1149 struct buffer out_buffer = { STRBUF_INIT, 0 };
1150 struct curl_slist *dav_headers = NULL;
1151 struct xml_ctx ctx;
1152 struct remote_ls_ctx ls;
1154 ls.flags = flags;
1155 ls.path = xstrdup(path);
1156 ls.dentry_name = NULL;
1157 ls.dentry_flags = 0;
1158 ls.userData = userData;
1159 ls.userFunc = userFunc;
1161 sprintf(url, "%s%s", repo->url, path);
1163 strbuf_addf(&out_buffer.buf, PROPFIND_ALL_REQUEST);
1165 dav_headers = curl_slist_append(dav_headers, "Depth: 1");
1166 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1168 slot = get_active_slot();
1169 slot->results = &results;
1170 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1171 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1172 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1173 #ifndef NO_CURL_IOCTL
1174 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1175 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
1176 #endif
1177 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1178 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1179 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1180 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1181 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1182 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1184 if (start_active_slot(slot)) {
1185 run_active_slot(slot);
1186 if (results.curl_result == CURLE_OK) {
1187 XML_Parser parser = XML_ParserCreate(NULL);
1188 enum XML_Status result;
1189 ctx.name = xcalloc(10, 1);
1190 ctx.len = 0;
1191 ctx.cdata = NULL;
1192 ctx.userFunc = handle_remote_ls_ctx;
1193 ctx.userData = &ls;
1194 XML_SetUserData(parser, &ctx);
1195 XML_SetElementHandler(parser, xml_start_tag,
1196 xml_end_tag);
1197 XML_SetCharacterDataHandler(parser, xml_cdata);
1198 result = XML_Parse(parser, in_buffer.buf,
1199 in_buffer.len, 1);
1200 free(ctx.name);
1202 if (result != XML_STATUS_OK) {
1203 fprintf(stderr, "XML error: %s\n",
1204 XML_ErrorString(
1205 XML_GetErrorCode(parser)));
1207 XML_ParserFree(parser);
1209 } else {
1210 fprintf(stderr, "Unable to start PROPFIND request\n");
1213 free(ls.path);
1214 free(url);
1215 strbuf_release(&out_buffer.buf);
1216 strbuf_release(&in_buffer);
1217 curl_slist_free_all(dav_headers);
1220 static void get_remote_object_list(unsigned char parent)
1222 char path[] = "objects/XX/";
1223 static const char hex[] = "0123456789abcdef";
1224 unsigned int val = parent;
1226 path[8] = hex[val >> 4];
1227 path[9] = hex[val & 0xf];
1228 remote_dir_exists[val] = 0;
1229 remote_ls(path, (PROCESS_FILES | PROCESS_DIRS),
1230 process_ls_object, &val);
1233 static int locking_available(void)
1235 struct active_request_slot *slot;
1236 struct slot_results results;
1237 struct strbuf in_buffer = STRBUF_INIT;
1238 struct buffer out_buffer = { STRBUF_INIT, 0 };
1239 struct curl_slist *dav_headers = NULL;
1240 struct xml_ctx ctx;
1241 int lock_flags = 0;
1242 char *escaped;
1244 escaped = xml_entities(repo->url);
1245 strbuf_addf(&out_buffer.buf, PROPFIND_SUPPORTEDLOCK_REQUEST, escaped);
1246 free(escaped);
1248 dav_headers = curl_slist_append(dav_headers, "Depth: 0");
1249 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1251 slot = get_active_slot();
1252 slot->results = &results;
1253 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1254 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1255 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1256 #ifndef NO_CURL_IOCTL
1257 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1258 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
1259 #endif
1260 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1261 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1262 curl_easy_setopt(slot->curl, CURLOPT_URL, repo->url);
1263 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1264 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1265 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1267 if (start_active_slot(slot)) {
1268 run_active_slot(slot);
1269 if (results.curl_result == CURLE_OK) {
1270 XML_Parser parser = XML_ParserCreate(NULL);
1271 enum XML_Status result;
1272 ctx.name = xcalloc(10, 1);
1273 ctx.len = 0;
1274 ctx.cdata = NULL;
1275 ctx.userFunc = handle_lockprop_ctx;
1276 ctx.userData = &lock_flags;
1277 XML_SetUserData(parser, &ctx);
1278 XML_SetElementHandler(parser, xml_start_tag,
1279 xml_end_tag);
1280 result = XML_Parse(parser, in_buffer.buf,
1281 in_buffer.len, 1);
1282 free(ctx.name);
1284 if (result != XML_STATUS_OK) {
1285 fprintf(stderr, "XML error: %s\n",
1286 XML_ErrorString(
1287 XML_GetErrorCode(parser)));
1288 lock_flags = 0;
1290 XML_ParserFree(parser);
1291 if (!lock_flags)
1292 error("no DAV locking support on %s",
1293 repo->url);
1295 } else {
1296 error("Cannot access URL %s, return code %d",
1297 repo->url, results.curl_result);
1298 lock_flags = 0;
1300 } else {
1301 error("Unable to start PROPFIND request on %s", repo->url);
1304 strbuf_release(&out_buffer.buf);
1305 strbuf_release(&in_buffer);
1306 curl_slist_free_all(dav_headers);
1308 return lock_flags;
1311 static struct object_list **add_one_object(struct object *obj, struct object_list **p)
1313 struct object_list *entry = xmalloc(sizeof(struct object_list));
1314 entry->item = obj;
1315 entry->next = *p;
1316 *p = entry;
1317 return &entry->next;
1320 static struct object_list **process_blob(struct blob *blob,
1321 struct object_list **p,
1322 struct name_path *path,
1323 const char *name)
1325 struct object *obj = &blob->object;
1327 obj->flags |= LOCAL;
1329 if (obj->flags & (UNINTERESTING | SEEN))
1330 return p;
1332 obj->flags |= SEEN;
1333 return add_one_object(obj, p);
1336 static struct object_list **process_tree(struct tree *tree,
1337 struct object_list **p,
1338 struct name_path *path,
1339 const char *name)
1341 struct object *obj = &tree->object;
1342 struct tree_desc desc;
1343 struct name_entry entry;
1344 struct name_path me;
1346 obj->flags |= LOCAL;
1348 if (obj->flags & (UNINTERESTING | SEEN))
1349 return p;
1350 if (parse_tree(tree) < 0)
1351 die("bad tree object %s", sha1_to_hex(obj->sha1));
1353 obj->flags |= SEEN;
1354 name = xstrdup(name);
1355 p = add_one_object(obj, p);
1356 me.up = path;
1357 me.elem = name;
1358 me.elem_len = strlen(name);
1360 init_tree_desc(&desc, tree->buffer, tree->size);
1362 while (tree_entry(&desc, &entry))
1363 switch (object_type(entry.mode)) {
1364 case OBJ_TREE:
1365 p = process_tree(lookup_tree(entry.sha1), p, &me, name);
1366 break;
1367 case OBJ_BLOB:
1368 p = process_blob(lookup_blob(entry.sha1), p, &me, name);
1369 break;
1370 default:
1371 /* Subproject commit - not in this repository */
1372 break;
1375 free(tree->buffer);
1376 tree->buffer = NULL;
1377 return p;
1380 static int get_delta(struct rev_info *revs, struct remote_lock *lock)
1382 int i;
1383 struct commit *commit;
1384 struct object_list **p = &objects;
1385 int count = 0;
1387 while ((commit = get_revision(revs)) != NULL) {
1388 p = process_tree(commit->tree, p, NULL, "");
1389 commit->object.flags |= LOCAL;
1390 if (!(commit->object.flags & UNINTERESTING))
1391 count += add_send_request(&commit->object, lock);
1394 for (i = 0; i < revs->pending.nr; i++) {
1395 struct object_array_entry *entry = revs->pending.objects + i;
1396 struct object *obj = entry->item;
1397 const char *name = entry->name;
1399 if (obj->flags & (UNINTERESTING | SEEN))
1400 continue;
1401 if (obj->type == OBJ_TAG) {
1402 obj->flags |= SEEN;
1403 p = add_one_object(obj, p);
1404 continue;
1406 if (obj->type == OBJ_TREE) {
1407 p = process_tree((struct tree *)obj, p, NULL, name);
1408 continue;
1410 if (obj->type == OBJ_BLOB) {
1411 p = process_blob((struct blob *)obj, p, NULL, name);
1412 continue;
1414 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
1417 while (objects) {
1418 if (!(objects->item->flags & UNINTERESTING))
1419 count += add_send_request(objects->item, lock);
1420 objects = objects->next;
1423 return count;
1426 static int update_remote(unsigned char *sha1, struct remote_lock *lock)
1428 struct active_request_slot *slot;
1429 struct slot_results results;
1430 struct buffer out_buffer = { STRBUF_INIT, 0 };
1431 struct curl_slist *dav_headers;
1433 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1435 strbuf_addf(&out_buffer.buf, "%s\n", sha1_to_hex(sha1));
1437 slot = get_active_slot();
1438 slot->results = &results;
1439 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1440 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1441 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1442 #ifndef NO_CURL_IOCTL
1443 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1444 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
1445 #endif
1446 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1447 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1448 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1449 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1450 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1451 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1453 if (start_active_slot(slot)) {
1454 run_active_slot(slot);
1455 strbuf_release(&out_buffer.buf);
1456 if (results.curl_result != CURLE_OK) {
1457 fprintf(stderr,
1458 "PUT error: curl result=%d, HTTP code=%ld\n",
1459 results.curl_result, results.http_code);
1460 /* We should attempt recovery? */
1461 return 0;
1463 } else {
1464 strbuf_release(&out_buffer.buf);
1465 fprintf(stderr, "Unable to start PUT request\n");
1466 return 0;
1469 return 1;
1472 static struct ref *remote_refs;
1474 static void one_remote_ref(char *refname)
1476 struct ref *ref;
1477 struct object *obj;
1479 ref = alloc_ref(refname);
1481 if (http_fetch_ref(repo->url, ref) != 0) {
1482 fprintf(stderr,
1483 "Unable to fetch ref %s from %s\n",
1484 refname, repo->url);
1485 free(ref);
1486 return;
1490 * Fetch a copy of the object if it doesn't exist locally - it
1491 * may be required for updating server info later.
1493 if (repo->can_update_info_refs && !has_sha1_file(ref->old_sha1)) {
1494 obj = lookup_unknown_object(ref->old_sha1);
1495 if (obj) {
1496 fprintf(stderr, " fetch %s for %s\n",
1497 sha1_to_hex(ref->old_sha1), refname);
1498 add_fetch_request(obj);
1502 ref->next = remote_refs;
1503 remote_refs = ref;
1506 static void get_dav_remote_heads(void)
1508 remote_ls("refs/", (PROCESS_FILES | PROCESS_DIRS | RECURSIVE), process_ls_ref, NULL);
1511 static void add_remote_info_ref(struct remote_ls_ctx *ls)
1513 struct strbuf *buf = (struct strbuf *)ls->userData;
1514 struct object *o;
1515 int len;
1516 char *ref_info;
1517 struct ref *ref;
1519 ref = alloc_ref(ls->dentry_name);
1521 if (http_fetch_ref(repo->url, ref) != 0) {
1522 fprintf(stderr,
1523 "Unable to fetch ref %s from %s\n",
1524 ls->dentry_name, repo->url);
1525 aborted = 1;
1526 free(ref);
1527 return;
1530 o = parse_object(ref->old_sha1);
1531 if (!o) {
1532 fprintf(stderr,
1533 "Unable to parse object %s for remote ref %s\n",
1534 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1535 aborted = 1;
1536 free(ref);
1537 return;
1540 len = strlen(ls->dentry_name) + 42;
1541 ref_info = xcalloc(len + 1, 1);
1542 sprintf(ref_info, "%s %s\n",
1543 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1544 fwrite_buffer(ref_info, 1, len, buf);
1545 free(ref_info);
1547 if (o->type == OBJ_TAG) {
1548 o = deref_tag(o, ls->dentry_name, 0);
1549 if (o) {
1550 len = strlen(ls->dentry_name) + 45;
1551 ref_info = xcalloc(len + 1, 1);
1552 sprintf(ref_info, "%s %s^{}\n",
1553 sha1_to_hex(o->sha1), ls->dentry_name);
1554 fwrite_buffer(ref_info, 1, len, buf);
1555 free(ref_info);
1558 free(ref);
1561 static void update_remote_info_refs(struct remote_lock *lock)
1563 struct buffer buffer = { STRBUF_INIT, 0 };
1564 struct active_request_slot *slot;
1565 struct slot_results results;
1566 struct curl_slist *dav_headers;
1568 remote_ls("refs/", (PROCESS_FILES | RECURSIVE),
1569 add_remote_info_ref, &buffer.buf);
1570 if (!aborted) {
1571 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1573 slot = get_active_slot();
1574 slot->results = &results;
1575 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &buffer);
1576 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, buffer.buf.len);
1577 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1578 #ifndef NO_CURL_IOCTL
1579 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1580 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &buffer);
1581 #endif
1582 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1583 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1584 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1585 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1586 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1587 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1589 if (start_active_slot(slot)) {
1590 run_active_slot(slot);
1591 if (results.curl_result != CURLE_OK) {
1592 fprintf(stderr,
1593 "PUT error: curl result=%d, HTTP code=%ld\n",
1594 results.curl_result, results.http_code);
1598 strbuf_release(&buffer.buf);
1601 static int remote_exists(const char *path)
1603 char *url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1604 int ret;
1606 sprintf(url, "%s%s", repo->url, path);
1608 switch (http_get_strbuf(url, NULL, 0)) {
1609 case HTTP_OK:
1610 ret = 1;
1611 break;
1612 case HTTP_MISSING_TARGET:
1613 ret = 0;
1614 break;
1615 case HTTP_ERROR:
1616 http_error(url, HTTP_ERROR);
1617 default:
1618 ret = -1;
1620 free(url);
1621 return ret;
1624 static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
1626 char *url;
1627 struct strbuf buffer = STRBUF_INIT;
1629 url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1630 sprintf(url, "%s%s", repo->url, path);
1632 if (http_get_strbuf(url, &buffer, 0) != HTTP_OK)
1633 die("Couldn't get %s for remote symref\n%s", url,
1634 curl_errorstr);
1635 free(url);
1637 free(*symref);
1638 *symref = NULL;
1639 hashclr(sha1);
1641 if (buffer.len == 0)
1642 return;
1644 /* If it's a symref, set the refname; otherwise try for a sha1 */
1645 if (!prefixcmp((char *)buffer.buf, "ref: ")) {
1646 *symref = xmemdupz((char *)buffer.buf + 5, buffer.len - 6);
1647 } else {
1648 get_sha1_hex(buffer.buf, sha1);
1651 strbuf_release(&buffer);
1654 static int verify_merge_base(unsigned char *head_sha1, unsigned char *branch_sha1)
1656 struct commit *head = lookup_commit(head_sha1);
1657 struct commit *branch = lookup_commit(branch_sha1);
1658 struct commit_list *merge_bases = get_merge_bases(head, branch, 1);
1660 return (merge_bases && !merge_bases->next && merge_bases->item == branch);
1663 static int delete_remote_branch(char *pattern, int force)
1665 struct ref *refs = remote_refs;
1666 struct ref *remote_ref = NULL;
1667 unsigned char head_sha1[20];
1668 char *symref = NULL;
1669 int match;
1670 int patlen = strlen(pattern);
1671 int i;
1672 struct active_request_slot *slot;
1673 struct slot_results results;
1674 char *url;
1676 /* Find the remote branch(es) matching the specified branch name */
1677 for (match = 0; refs; refs = refs->next) {
1678 char *name = refs->name;
1679 int namelen = strlen(name);
1680 if (namelen < patlen ||
1681 memcmp(name + namelen - patlen, pattern, patlen))
1682 continue;
1683 if (namelen != patlen && name[namelen - patlen - 1] != '/')
1684 continue;
1685 match++;
1686 remote_ref = refs;
1688 if (match == 0)
1689 return error("No remote branch matches %s", pattern);
1690 if (match != 1)
1691 return error("More than one remote branch matches %s",
1692 pattern);
1695 * Remote HEAD must be a symref (not exactly foolproof; a remote
1696 * symlink to a symref will look like a symref)
1698 fetch_symref("HEAD", &symref, head_sha1);
1699 if (!symref)
1700 return error("Remote HEAD is not a symref");
1702 /* Remote branch must not be the remote HEAD */
1703 for (i=0; symref && i<MAXDEPTH; i++) {
1704 if (!strcmp(remote_ref->name, symref))
1705 return error("Remote branch %s is the current HEAD",
1706 remote_ref->name);
1707 fetch_symref(symref, &symref, head_sha1);
1710 /* Run extra sanity checks if delete is not forced */
1711 if (!force) {
1712 /* Remote HEAD must resolve to a known object */
1713 if (symref)
1714 return error("Remote HEAD symrefs too deep");
1715 if (is_null_sha1(head_sha1))
1716 return error("Unable to resolve remote HEAD");
1717 if (!has_sha1_file(head_sha1))
1718 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1));
1720 /* Remote branch must resolve to a known object */
1721 if (is_null_sha1(remote_ref->old_sha1))
1722 return error("Unable to resolve remote branch %s",
1723 remote_ref->name);
1724 if (!has_sha1_file(remote_ref->old_sha1))
1725 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));
1727 /* Remote branch must be an ancestor of remote HEAD */
1728 if (!verify_merge_base(head_sha1, remote_ref->old_sha1)) {
1729 return error("The branch '%s' is not an ancestor "
1730 "of your current HEAD.\n"
1731 "If you are sure you want to delete it,"
1732 " run:\n\t'git http-push -D %s %s'",
1733 remote_ref->name, repo->url, pattern);
1737 /* Send delete request */
1738 fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name);
1739 if (dry_run)
1740 return 0;
1741 url = xmalloc(strlen(repo->url) + strlen(remote_ref->name) + 1);
1742 sprintf(url, "%s%s", repo->url, remote_ref->name);
1743 slot = get_active_slot();
1744 slot->results = &results;
1745 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
1746 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1747 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1748 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_DELETE);
1749 if (start_active_slot(slot)) {
1750 run_active_slot(slot);
1751 free(url);
1752 if (results.curl_result != CURLE_OK)
1753 return error("DELETE request failed (%d/%ld)\n",
1754 results.curl_result, results.http_code);
1755 } else {
1756 free(url);
1757 return error("Unable to start DELETE request");
1760 return 0;
1763 static void run_request_queue(void)
1765 #ifdef USE_CURL_MULTI
1766 is_running_queue = 1;
1767 fill_active_slots();
1768 add_fill_function(NULL, fill_active_slot);
1769 #endif
1770 do {
1771 finish_all_active_slots();
1772 #ifdef USE_CURL_MULTI
1773 fill_active_slots();
1774 #endif
1775 } while (request_queue_head && !aborted);
1777 #ifdef USE_CURL_MULTI
1778 is_running_queue = 0;
1779 #endif
1782 int main(int argc, char **argv)
1784 struct transfer_request *request;
1785 struct transfer_request *next_request;
1786 int nr_refspec = 0;
1787 char **refspec = NULL;
1788 struct remote_lock *ref_lock = NULL;
1789 struct remote_lock *info_ref_lock = NULL;
1790 struct rev_info revs;
1791 int delete_branch = 0;
1792 int force_delete = 0;
1793 int objects_to_send;
1794 int rc = 0;
1795 int i;
1796 int new_refs;
1797 struct ref *ref, *local_refs;
1798 struct remote *remote;
1800 git_extract_argv0_path(argv[0]);
1802 repo = xcalloc(sizeof(*repo), 1);
1804 argv++;
1805 for (i = 1; i < argc; i++, argv++) {
1806 char *arg = *argv;
1808 if (*arg == '-') {
1809 if (!strcmp(arg, "--all")) {
1810 push_all = MATCH_REFS_ALL;
1811 continue;
1813 if (!strcmp(arg, "--force")) {
1814 force_all = 1;
1815 continue;
1817 if (!strcmp(arg, "--dry-run")) {
1818 dry_run = 1;
1819 continue;
1821 if (!strcmp(arg, "--helper-status")) {
1822 helper_status = 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;
1839 if (!strcmp(arg, "-h"))
1840 usage(http_push_usage);
1842 if (!repo->url) {
1843 char *path = strstr(arg, "//");
1844 str_end_url_with_slash(arg, &repo->url);
1845 repo->path_len = strlen(repo->url);
1846 if (path) {
1847 repo->path = strchr(path+2, '/');
1848 if (repo->path)
1849 repo->path_len = strlen(repo->path);
1851 continue;
1853 refspec = argv;
1854 nr_refspec = argc - i;
1855 break;
1858 #ifndef USE_CURL_MULTI
1859 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
1860 #endif
1862 if (!repo->url)
1863 usage(http_push_usage);
1865 if (delete_branch && nr_refspec != 1)
1866 die("You must specify only one branch name when deleting a remote branch");
1868 setup_git_directory();
1870 memset(remote_dir_exists, -1, 256);
1873 * Create a minimum remote by hand to give to http_init(),
1874 * primarily to allow it to look at the URL.
1876 remote = xcalloc(sizeof(*remote), 1);
1877 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
1878 remote->url[remote->url_nr++] = repo->url;
1879 http_init(remote);
1881 #ifdef USE_CURL_MULTI
1882 is_running_queue = 0;
1883 #endif
1885 /* Verify DAV compliance/lock support */
1886 if (!locking_available()) {
1887 rc = 1;
1888 goto cleanup;
1891 sigchain_push_common(remove_locks_on_signal);
1893 /* Check whether the remote has server info files */
1894 repo->can_update_info_refs = 0;
1895 repo->has_info_refs = remote_exists("info/refs");
1896 repo->has_info_packs = remote_exists("objects/info/packs");
1897 if (repo->has_info_refs) {
1898 info_ref_lock = lock_remote("info/refs", LOCK_TIME);
1899 if (info_ref_lock)
1900 repo->can_update_info_refs = 1;
1901 else {
1902 error("cannot lock existing info/refs");
1903 rc = 1;
1904 goto cleanup;
1907 if (repo->has_info_packs)
1908 fetch_indices();
1910 /* Get a list of all local and remote heads to validate refspecs */
1911 local_refs = get_local_heads();
1912 fprintf(stderr, "Fetching remote heads...\n");
1913 get_dav_remote_heads();
1914 run_request_queue();
1916 /* Remove a remote branch if -d or -D was specified */
1917 if (delete_branch) {
1918 if (delete_remote_branch(refspec[0], force_delete) == -1) {
1919 fprintf(stderr, "Unable to delete remote branch %s\n",
1920 refspec[0]);
1921 if (helper_status)
1922 printf("error %s cannot remove\n", refspec[0]);
1924 goto cleanup;
1927 /* match them up */
1928 if (match_refs(local_refs, &remote_refs,
1929 nr_refspec, (const char **) refspec, push_all)) {
1930 rc = -1;
1931 goto cleanup;
1933 if (!remote_refs) {
1934 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
1935 if (helper_status)
1936 printf("error null no match\n");
1937 rc = 0;
1938 goto cleanup;
1941 new_refs = 0;
1942 for (ref = remote_refs; ref; ref = ref->next) {
1943 char old_hex[60], *new_hex;
1944 const char *commit_argv[5];
1945 int commit_argc;
1946 char *new_sha1_hex, *old_sha1_hex;
1948 if (!ref->peer_ref)
1949 continue;
1951 if (is_null_sha1(ref->peer_ref->new_sha1)) {
1952 if (delete_remote_branch(ref->name, 1) == -1) {
1953 error("Could not remove %s", ref->name);
1954 if (helper_status)
1955 printf("error %s cannot remove\n", ref->name);
1956 rc = -4;
1958 else if (helper_status)
1959 printf("ok %s\n", ref->name);
1960 new_refs++;
1961 continue;
1964 if (!hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
1965 if (push_verbosely)
1966 fprintf(stderr, "'%s': up-to-date\n", ref->name);
1967 if (helper_status)
1968 printf("ok %s up to date\n", ref->name);
1969 continue;
1972 if (!force_all &&
1973 !is_null_sha1(ref->old_sha1) &&
1974 !ref->force) {
1975 if (!has_sha1_file(ref->old_sha1) ||
1976 !ref_newer(ref->peer_ref->new_sha1,
1977 ref->old_sha1)) {
1979 * We do not have the remote ref, or
1980 * we know that the remote ref is not
1981 * an ancestor of what we are trying to
1982 * push. Either way this can be losing
1983 * commits at the remote end and likely
1984 * we were not up to date to begin with.
1986 error("remote '%s' is not an ancestor of\n"
1987 "local '%s'.\n"
1988 "Maybe you are not up-to-date and "
1989 "need to pull first?",
1990 ref->name,
1991 ref->peer_ref->name);
1992 if (helper_status)
1993 printf("error %s non-fast forward\n", ref->name);
1994 rc = -2;
1995 continue;
1998 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
1999 new_refs++;
2000 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
2001 new_hex = sha1_to_hex(ref->new_sha1);
2003 fprintf(stderr, "updating '%s'", ref->name);
2004 if (strcmp(ref->name, ref->peer_ref->name))
2005 fprintf(stderr, " using '%s'", ref->peer_ref->name);
2006 fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex);
2007 if (dry_run) {
2008 if (helper_status)
2009 printf("ok %s\n", ref->name);
2010 continue;
2013 /* Lock remote branch ref */
2014 ref_lock = lock_remote(ref->name, LOCK_TIME);
2015 if (ref_lock == NULL) {
2016 fprintf(stderr, "Unable to lock remote branch %s\n",
2017 ref->name);
2018 if (helper_status)
2019 printf("error %s lock error\n", ref->name);
2020 rc = 1;
2021 continue;
2024 /* Set up revision info for this refspec */
2025 commit_argc = 3;
2026 new_sha1_hex = xstrdup(sha1_to_hex(ref->new_sha1));
2027 old_sha1_hex = NULL;
2028 commit_argv[1] = "--objects";
2029 commit_argv[2] = new_sha1_hex;
2030 if (!push_all && !is_null_sha1(ref->old_sha1)) {
2031 old_sha1_hex = xmalloc(42);
2032 sprintf(old_sha1_hex, "^%s",
2033 sha1_to_hex(ref->old_sha1));
2034 commit_argv[3] = old_sha1_hex;
2035 commit_argc++;
2037 commit_argv[commit_argc] = NULL;
2038 init_revisions(&revs, setup_git_directory());
2039 setup_revisions(commit_argc, commit_argv, &revs, NULL);
2040 revs.edge_hint = 0; /* just in case */
2041 free(new_sha1_hex);
2042 if (old_sha1_hex) {
2043 free(old_sha1_hex);
2044 commit_argv[1] = NULL;
2047 /* Generate a list of objects that need to be pushed */
2048 pushing = 0;
2049 if (prepare_revision_walk(&revs))
2050 die("revision walk setup failed");
2051 mark_edges_uninteresting(revs.commits, &revs, NULL);
2052 objects_to_send = get_delta(&revs, ref_lock);
2053 finish_all_active_slots();
2055 /* Push missing objects to remote, this would be a
2056 convenient time to pack them first if appropriate. */
2057 pushing = 1;
2058 if (objects_to_send)
2059 fprintf(stderr, " sending %d objects\n",
2060 objects_to_send);
2062 run_request_queue();
2064 /* Update the remote branch if all went well */
2065 if (aborted || !update_remote(ref->new_sha1, ref_lock))
2066 rc = 1;
2068 if (!rc)
2069 fprintf(stderr, " done\n");
2070 if (helper_status)
2071 printf("%s %s\n", !rc ? "ok" : "error", ref->name);
2072 unlock_remote(ref_lock);
2073 check_locks();
2076 /* Update remote server info if appropriate */
2077 if (repo->has_info_refs && new_refs) {
2078 if (info_ref_lock && repo->can_update_info_refs) {
2079 fprintf(stderr, "Updating remote server info\n");
2080 if (!dry_run)
2081 update_remote_info_refs(info_ref_lock);
2082 } else {
2083 fprintf(stderr, "Unable to update server info\n");
2087 cleanup:
2088 if (info_ref_lock)
2089 unlock_remote(info_ref_lock);
2090 free(repo);
2092 http_cleanup();
2094 request = request_queue_head;
2095 while (request != NULL) {
2096 next_request = request->next;
2097 release_request(request);
2098 request = next_request;
2101 return rc;