request-pull: optionally show a patch as well
[tgit.git] / http-push.c
blob00e83dcec1d973b069d4c75105aed96634b00994
1 #include "cache.h"
2 #include "commit.h"
3 #include "tag.h"
4 #include "blob.h"
5 #include "http.h"
6 #include "refs.h"
7 #include "diff.h"
8 #include "revision.h"
9 #include "exec_cmd.h"
10 #include "remote.h"
11 #include "list-objects.h"
12 #include "sigchain.h"
14 #include <expat.h>
16 static const char http_push_usage[] =
17 "git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
19 #ifndef XML_STATUS_OK
20 enum XML_Status {
21 XML_STATUS_OK = 1,
22 XML_STATUS_ERROR = 0
24 #define XML_STATUS_OK 1
25 #define XML_STATUS_ERROR 0
26 #endif
28 #define PREV_BUF_SIZE 4096
30 /* DAV methods */
31 #define DAV_LOCK "LOCK"
32 #define DAV_MKCOL "MKCOL"
33 #define DAV_MOVE "MOVE"
34 #define DAV_PROPFIND "PROPFIND"
35 #define DAV_PUT "PUT"
36 #define DAV_UNLOCK "UNLOCK"
37 #define DAV_DELETE "DELETE"
39 /* DAV lock flags */
40 #define DAV_PROP_LOCKWR (1u << 0)
41 #define DAV_PROP_LOCKEX (1u << 1)
42 #define DAV_LOCK_OK (1u << 2)
44 /* DAV XML properties */
45 #define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
46 #define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
47 #define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
48 #define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
49 #define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
50 #define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
51 #define DAV_PROPFIND_RESP ".multistatus.response"
52 #define DAV_PROPFIND_NAME ".multistatus.response.href"
53 #define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
55 /* DAV request body templates */
56 #define PROPFIND_SUPPORTEDLOCK_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:prop xmlns:R=\"%s\">\n<D:supportedlock/>\n</D:prop>\n</D:propfind>"
57 #define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
58 #define LOCK_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:lockinfo xmlns:D=\"DAV:\">\n<D:lockscope><D:exclusive/></D:lockscope>\n<D:locktype><D:write/></D:locktype>\n<D:owner>\n<D:href>mailto:%s</D:href>\n</D:owner>\n</D:lockinfo>"
60 #define LOCK_TIME 600
61 #define LOCK_REFRESH 30
63 /* bits #0-15 in revision.h */
65 #define LOCAL (1u<<16)
66 #define REMOTE (1u<<17)
67 #define FETCHING (1u<<18)
68 #define PUSHING (1u<<19)
70 /* We allow "recursive" symbolic refs. Only within reason, though */
71 #define MAXDEPTH 5
73 static int pushing;
74 static int aborted;
75 static signed char remote_dir_exists[256];
77 static int push_verbosely;
78 static int push_all = MATCH_REFS_NONE;
79 static int force_all;
80 static int dry_run;
82 static struct object_list *objects;
84 struct repo
86 char *url;
87 char *path;
88 int path_len;
89 int has_info_refs;
90 int can_update_info_refs;
91 int has_info_packs;
92 struct packed_git *packs;
93 struct remote_lock *locks;
96 static struct repo *repo;
98 enum transfer_state {
99 NEED_FETCH,
100 RUN_FETCH_LOOSE,
101 RUN_FETCH_PACKED,
102 NEED_PUSH,
103 RUN_MKCOL,
104 RUN_PUT,
105 RUN_MOVE,
106 ABORTED,
107 COMPLETE,
110 struct transfer_request
112 struct object *obj;
113 char *url;
114 char *dest;
115 struct remote_lock *lock;
116 struct curl_slist *headers;
117 struct buffer buffer;
118 enum transfer_state state;
119 CURLcode curl_result;
120 char errorstr[CURL_ERROR_SIZE];
121 long http_code;
122 void *userData;
123 struct active_request_slot *slot;
124 struct transfer_request *next;
127 static struct transfer_request *request_queue_head;
129 struct xml_ctx
131 char *name;
132 int len;
133 char *cdata;
134 void (*userFunc)(struct xml_ctx *ctx, int tag_closed);
135 void *userData;
138 struct remote_lock
140 char *url;
141 char *owner;
142 char *token;
143 char tmpfile_suffix[41];
144 time_t start_time;
145 long timeout;
146 int refreshing;
147 struct remote_lock *next;
150 /* Flags that control remote_ls processing */
151 #define PROCESS_FILES (1u << 0)
152 #define PROCESS_DIRS (1u << 1)
153 #define RECURSIVE (1u << 2)
155 /* Flags that remote_ls passes to callback functions */
156 #define IS_DIR (1u << 0)
158 struct remote_ls_ctx
160 char *path;
161 void (*userFunc)(struct remote_ls_ctx *ls);
162 void *userData;
163 int flags;
164 char *dentry_name;
165 int dentry_flags;
166 struct remote_ls_ctx *parent;
169 /* get_dav_token_headers options */
170 enum dav_header_flag {
171 DAV_HEADER_IF = (1u << 0),
172 DAV_HEADER_LOCK = (1u << 1),
173 DAV_HEADER_TIMEOUT = (1u << 2)
176 static char *xml_entities(char *s)
178 struct strbuf buf = STRBUF_INIT;
179 while (*s) {
180 size_t len = strcspn(s, "\"<>&");
181 strbuf_add(&buf, s, len);
182 s += len;
183 switch (*s) {
184 case '"':
185 strbuf_addstr(&buf, "&quot;");
186 break;
187 case '<':
188 strbuf_addstr(&buf, "&lt;");
189 break;
190 case '>':
191 strbuf_addstr(&buf, "&gt;");
192 break;
193 case '&':
194 strbuf_addstr(&buf, "&amp;");
195 break;
196 case 0:
197 return strbuf_detach(&buf, NULL);
199 s++;
201 return strbuf_detach(&buf, NULL);
204 static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum dav_header_flag options)
206 struct strbuf buf = STRBUF_INIT;
207 struct curl_slist *dav_headers = NULL;
209 if (options & DAV_HEADER_IF) {
210 strbuf_addf(&buf, "If: (<%s>)", lock->token);
211 dav_headers = curl_slist_append(dav_headers, buf.buf);
212 strbuf_reset(&buf);
214 if (options & DAV_HEADER_LOCK) {
215 strbuf_addf(&buf, "Lock-Token: <%s>", lock->token);
216 dav_headers = curl_slist_append(dav_headers, buf.buf);
217 strbuf_reset(&buf);
219 if (options & DAV_HEADER_TIMEOUT) {
220 strbuf_addf(&buf, "Timeout: Second-%ld", lock->timeout);
221 dav_headers = curl_slist_append(dav_headers, buf.buf);
222 strbuf_reset(&buf);
224 strbuf_release(&buf);
226 return dav_headers;
229 static void finish_request(struct transfer_request *request);
230 static void release_request(struct transfer_request *request);
232 static void process_response(void *callback_data)
234 struct transfer_request *request =
235 (struct transfer_request *)callback_data;
237 finish_request(request);
240 #ifdef USE_CURL_MULTI
242 static void start_fetch_loose(struct transfer_request *request)
244 struct active_request_slot *slot;
245 struct http_object_request *obj_req;
247 obj_req = new_http_object_request(repo->url, request->obj->sha1);
248 if (obj_req == NULL) {
249 request->state = ABORTED;
250 return;
253 slot = obj_req->slot;
254 slot->callback_func = process_response;
255 slot->callback_data = request;
256 request->slot = slot;
257 request->userData = obj_req;
259 /* Try to get the request started, abort the request on error */
260 request->state = RUN_FETCH_LOOSE;
261 if (!start_active_slot(slot)) {
262 fprintf(stderr, "Unable to start GET request\n");
263 repo->can_update_info_refs = 0;
264 release_http_object_request(obj_req);
265 release_request(request);
269 static void start_mkcol(struct transfer_request *request)
271 char *hex = sha1_to_hex(request->obj->sha1);
272 struct active_request_slot *slot;
274 request->url = get_remote_object_url(repo->url, hex, 1);
276 slot = get_active_slot();
277 slot->callback_func = process_response;
278 slot->callback_data = request;
279 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
280 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
281 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
282 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
283 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
285 if (start_active_slot(slot)) {
286 request->slot = slot;
287 request->state = RUN_MKCOL;
288 } else {
289 request->state = ABORTED;
290 free(request->url);
291 request->url = NULL;
294 #endif
296 static void start_fetch_packed(struct transfer_request *request)
298 struct packed_git *target;
300 struct transfer_request *check_request = request_queue_head;
301 struct http_pack_request *preq;
303 target = find_sha1_pack(request->obj->sha1, repo->packs);
304 if (!target) {
305 fprintf(stderr, "Unable to fetch %s, will not be able to update server info refs\n", sha1_to_hex(request->obj->sha1));
306 repo->can_update_info_refs = 0;
307 release_request(request);
308 return;
311 fprintf(stderr, "Fetching pack %s\n", sha1_to_hex(target->sha1));
312 fprintf(stderr, " which contains %s\n", sha1_to_hex(request->obj->sha1));
314 preq = new_http_pack_request(target, repo->url);
315 if (preq == NULL) {
316 release_http_pack_request(preq);
317 repo->can_update_info_refs = 0;
318 return;
320 preq->lst = &repo->packs;
322 /* Make sure there isn't another open request for this pack */
323 while (check_request) {
324 if (check_request->state == RUN_FETCH_PACKED &&
325 !strcmp(check_request->url, preq->url)) {
326 release_http_pack_request(preq);
327 release_request(request);
328 return;
330 check_request = check_request->next;
333 preq->slot->callback_func = process_response;
334 preq->slot->callback_data = request;
335 request->slot = preq->slot;
336 request->userData = preq;
338 /* Try to get the request started, abort the request on error */
339 request->state = RUN_FETCH_PACKED;
340 if (!start_active_slot(preq->slot)) {
341 fprintf(stderr, "Unable to start GET request\n");
342 release_http_pack_request(preq);
343 repo->can_update_info_refs = 0;
344 release_request(request);
348 static void start_put(struct transfer_request *request)
350 char *hex = sha1_to_hex(request->obj->sha1);
351 struct active_request_slot *slot;
352 struct strbuf buf = STRBUF_INIT;
353 enum object_type type;
354 char hdr[50];
355 void *unpacked;
356 unsigned long len;
357 int hdrlen;
358 ssize_t size;
359 z_stream stream;
361 unpacked = read_sha1_file(request->obj->sha1, &type, &len);
362 hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
364 /* Set it up */
365 memset(&stream, 0, sizeof(stream));
366 deflateInit(&stream, zlib_compression_level);
367 size = deflateBound(&stream, len + hdrlen);
368 strbuf_init(&request->buffer.buf, size);
369 request->buffer.posn = 0;
371 /* Compress it */
372 stream.next_out = (unsigned char *)request->buffer.buf.buf;
373 stream.avail_out = size;
375 /* First header.. */
376 stream.next_in = (void *)hdr;
377 stream.avail_in = hdrlen;
378 while (deflate(&stream, 0) == Z_OK)
379 /* nothing */;
381 /* Then the data itself.. */
382 stream.next_in = unpacked;
383 stream.avail_in = len;
384 while (deflate(&stream, Z_FINISH) == Z_OK)
385 /* nothing */;
386 deflateEnd(&stream);
387 free(unpacked);
389 request->buffer.buf.len = stream.total_out;
391 strbuf_addstr(&buf, "Destination: ");
392 append_remote_object_url(&buf, repo->url, hex, 0);
393 request->dest = strbuf_detach(&buf, NULL);
395 append_remote_object_url(&buf, repo->url, hex, 0);
396 strbuf_add(&buf, request->lock->tmpfile_suffix, 41);
397 request->url = strbuf_detach(&buf, NULL);
399 slot = get_active_slot();
400 slot->callback_func = process_response;
401 slot->callback_data = request;
402 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &request->buffer);
403 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, request->buffer.buf.len);
404 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
405 #ifndef NO_CURL_IOCTL
406 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
407 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &request->buffer);
408 #endif
409 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
410 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
411 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
412 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
413 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
414 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
416 if (start_active_slot(slot)) {
417 request->slot = slot;
418 request->state = RUN_PUT;
419 } else {
420 request->state = ABORTED;
421 free(request->url);
422 request->url = NULL;
426 static void start_move(struct transfer_request *request)
428 struct active_request_slot *slot;
429 struct curl_slist *dav_headers = NULL;
431 slot = get_active_slot();
432 slot->callback_func = process_response;
433 slot->callback_data = request;
434 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
435 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MOVE);
436 dav_headers = curl_slist_append(dav_headers, request->dest);
437 dav_headers = curl_slist_append(dav_headers, "Overwrite: T");
438 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
439 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
440 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
442 if (start_active_slot(slot)) {
443 request->slot = slot;
444 request->state = RUN_MOVE;
445 } else {
446 request->state = ABORTED;
447 free(request->url);
448 request->url = NULL;
452 static int refresh_lock(struct remote_lock *lock)
454 struct active_request_slot *slot;
455 struct slot_results results;
456 struct curl_slist *dav_headers;
457 int rc = 0;
459 lock->refreshing = 1;
461 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF | DAV_HEADER_TIMEOUT);
463 slot = get_active_slot();
464 slot->results = &results;
465 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
466 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
467 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
468 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
469 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
471 if (start_active_slot(slot)) {
472 run_active_slot(slot);
473 if (results.curl_result != CURLE_OK) {
474 fprintf(stderr, "LOCK HTTP error %ld\n",
475 results.http_code);
476 } else {
477 lock->start_time = time(NULL);
478 rc = 1;
482 lock->refreshing = 0;
483 curl_slist_free_all(dav_headers);
485 return rc;
488 static void check_locks(void)
490 struct remote_lock *lock = repo->locks;
491 time_t current_time = time(NULL);
492 int time_remaining;
494 while (lock) {
495 time_remaining = lock->start_time + lock->timeout -
496 current_time;
497 if (!lock->refreshing && time_remaining < LOCK_REFRESH) {
498 if (!refresh_lock(lock)) {
499 fprintf(stderr,
500 "Unable to refresh lock for %s\n",
501 lock->url);
502 aborted = 1;
503 return;
506 lock = lock->next;
510 static void release_request(struct transfer_request *request)
512 struct transfer_request *entry = request_queue_head;
514 if (request == request_queue_head) {
515 request_queue_head = request->next;
516 } else {
517 while (entry->next != NULL && entry->next != request)
518 entry = entry->next;
519 if (entry->next == request)
520 entry->next = entry->next->next;
523 free(request->url);
524 free(request);
527 static void finish_request(struct transfer_request *request)
529 struct http_pack_request *preq;
530 struct http_object_request *obj_req;
532 request->curl_result = request->slot->curl_result;
533 request->http_code = request->slot->http_code;
534 request->slot = NULL;
536 /* Keep locks active */
537 check_locks();
539 if (request->headers != NULL)
540 curl_slist_free_all(request->headers);
542 /* URL is reused for MOVE after PUT */
543 if (request->state != RUN_PUT) {
544 free(request->url);
545 request->url = NULL;
548 if (request->state == RUN_MKCOL) {
549 if (request->curl_result == CURLE_OK ||
550 request->http_code == 405) {
551 remote_dir_exists[request->obj->sha1[0]] = 1;
552 start_put(request);
553 } else {
554 fprintf(stderr, "MKCOL %s failed, aborting (%d/%ld)\n",
555 sha1_to_hex(request->obj->sha1),
556 request->curl_result, request->http_code);
557 request->state = ABORTED;
558 aborted = 1;
560 } else if (request->state == RUN_PUT) {
561 if (request->curl_result == CURLE_OK) {
562 start_move(request);
563 } else {
564 fprintf(stderr, "PUT %s failed, aborting (%d/%ld)\n",
565 sha1_to_hex(request->obj->sha1),
566 request->curl_result, request->http_code);
567 request->state = ABORTED;
568 aborted = 1;
570 } else if (request->state == RUN_MOVE) {
571 if (request->curl_result == CURLE_OK) {
572 if (push_verbosely)
573 fprintf(stderr, " sent %s\n",
574 sha1_to_hex(request->obj->sha1));
575 request->obj->flags |= REMOTE;
576 release_request(request);
577 } else {
578 fprintf(stderr, "MOVE %s failed, aborting (%d/%ld)\n",
579 sha1_to_hex(request->obj->sha1),
580 request->curl_result, request->http_code);
581 request->state = ABORTED;
582 aborted = 1;
584 } else if (request->state == RUN_FETCH_LOOSE) {
585 obj_req = (struct http_object_request *)request->userData;
587 if (finish_http_object_request(obj_req) == 0)
588 if (obj_req->rename == 0)
589 request->obj->flags |= (LOCAL | REMOTE);
591 /* Try fetching packed if necessary */
592 if (request->obj->flags & LOCAL) {
593 release_http_object_request(obj_req);
594 release_request(request);
595 } else
596 start_fetch_packed(request);
598 } else if (request->state == RUN_FETCH_PACKED) {
599 int fail = 1;
600 if (request->curl_result != CURLE_OK) {
601 fprintf(stderr, "Unable to get pack file %s\n%s",
602 request->url, curl_errorstr);
603 } else {
604 preq = (struct http_pack_request *)request->userData;
606 if (preq) {
607 if (finish_http_pack_request(preq) > 0)
608 fail = 0;
609 release_http_pack_request(preq);
612 if (fail)
613 repo->can_update_info_refs = 0;
614 release_request(request);
618 #ifdef USE_CURL_MULTI
619 static int is_running_queue;
620 static int fill_active_slot(void *unused)
622 struct transfer_request *request;
624 if (aborted || !is_running_queue)
625 return 0;
627 for (request = request_queue_head; request; request = request->next) {
628 if (request->state == NEED_FETCH) {
629 start_fetch_loose(request);
630 return 1;
631 } else if (pushing && request->state == NEED_PUSH) {
632 if (remote_dir_exists[request->obj->sha1[0]] == 1) {
633 start_put(request);
634 } else {
635 start_mkcol(request);
637 return 1;
640 return 0;
642 #endif
644 static void get_remote_object_list(unsigned char parent);
646 static void add_fetch_request(struct object *obj)
648 struct transfer_request *request;
650 check_locks();
653 * Don't fetch the object if it's known to exist locally
654 * or is already in the request queue
656 if (remote_dir_exists[obj->sha1[0]] == -1)
657 get_remote_object_list(obj->sha1[0]);
658 if (obj->flags & (LOCAL | FETCHING))
659 return;
661 obj->flags |= FETCHING;
662 request = xmalloc(sizeof(*request));
663 request->obj = obj;
664 request->url = NULL;
665 request->lock = NULL;
666 request->headers = NULL;
667 request->state = NEED_FETCH;
668 request->next = request_queue_head;
669 request_queue_head = request;
671 #ifdef USE_CURL_MULTI
672 fill_active_slots();
673 step_active_slots();
674 #endif
677 static int add_send_request(struct object *obj, struct remote_lock *lock)
679 struct transfer_request *request = request_queue_head;
680 struct packed_git *target;
682 /* Keep locks active */
683 check_locks();
686 * Don't push the object if it's known to exist on the remote
687 * or is already in the request queue
689 if (remote_dir_exists[obj->sha1[0]] == -1)
690 get_remote_object_list(obj->sha1[0]);
691 if (obj->flags & (REMOTE | PUSHING))
692 return 0;
693 target = find_sha1_pack(obj->sha1, repo->packs);
694 if (target) {
695 obj->flags |= REMOTE;
696 return 0;
699 obj->flags |= PUSHING;
700 request = xmalloc(sizeof(*request));
701 request->obj = obj;
702 request->url = NULL;
703 request->lock = lock;
704 request->headers = NULL;
705 request->state = NEED_PUSH;
706 request->next = request_queue_head;
707 request_queue_head = request;
709 #ifdef USE_CURL_MULTI
710 fill_active_slots();
711 step_active_slots();
712 #endif
714 return 1;
717 static int fetch_indices(void)
719 int ret;
721 if (push_verbosely)
722 fprintf(stderr, "Getting pack list\n");
724 switch (http_get_info_packs(repo->url, &repo->packs)) {
725 case HTTP_OK:
726 case HTTP_MISSING_TARGET:
727 ret = 0;
728 break;
729 default:
730 ret = -1;
733 return ret;
736 static void one_remote_object(const char *hex)
738 unsigned char sha1[20];
739 struct object *obj;
741 if (get_sha1_hex(hex, sha1) != 0)
742 return;
744 obj = lookup_object(sha1);
745 if (!obj)
746 obj = parse_object(sha1);
748 /* Ignore remote objects that don't exist locally */
749 if (!obj)
750 return;
752 obj->flags |= REMOTE;
753 if (!object_list_contains(objects, obj))
754 object_list_insert(obj, &objects);
757 static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed)
759 int *lock_flags = (int *)ctx->userData;
761 if (tag_closed) {
762 if (!strcmp(ctx->name, DAV_CTX_LOCKENTRY)) {
763 if ((*lock_flags & DAV_PROP_LOCKEX) &&
764 (*lock_flags & DAV_PROP_LOCKWR)) {
765 *lock_flags |= DAV_LOCK_OK;
767 *lock_flags &= DAV_LOCK_OK;
768 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_WRITE)) {
769 *lock_flags |= DAV_PROP_LOCKWR;
770 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_EXCLUSIVE)) {
771 *lock_flags |= DAV_PROP_LOCKEX;
776 static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
778 struct remote_lock *lock = (struct remote_lock *)ctx->userData;
779 git_SHA_CTX sha_ctx;
780 unsigned char lock_token_sha1[20];
782 if (tag_closed && ctx->cdata) {
783 if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) {
784 lock->owner = xmalloc(strlen(ctx->cdata) + 1);
785 strcpy(lock->owner, ctx->cdata);
786 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
787 if (!prefixcmp(ctx->cdata, "Second-"))
788 lock->timeout =
789 strtol(ctx->cdata + 7, NULL, 10);
790 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
791 lock->token = xmalloc(strlen(ctx->cdata) + 1);
792 strcpy(lock->token, ctx->cdata);
794 git_SHA1_Init(&sha_ctx);
795 git_SHA1_Update(&sha_ctx, lock->token, strlen(lock->token));
796 git_SHA1_Final(lock_token_sha1, &sha_ctx);
798 lock->tmpfile_suffix[0] = '_';
799 memcpy(lock->tmpfile_suffix + 1, sha1_to_hex(lock_token_sha1), 40);
804 static void one_remote_ref(char *refname);
806 static void
807 xml_start_tag(void *userData, const char *name, const char **atts)
809 struct xml_ctx *ctx = (struct xml_ctx *)userData;
810 const char *c = strchr(name, ':');
811 int new_len;
813 if (c == NULL)
814 c = name;
815 else
816 c++;
818 new_len = strlen(ctx->name) + strlen(c) + 2;
820 if (new_len > ctx->len) {
821 ctx->name = xrealloc(ctx->name, new_len);
822 ctx->len = new_len;
824 strcat(ctx->name, ".");
825 strcat(ctx->name, c);
827 free(ctx->cdata);
828 ctx->cdata = NULL;
830 ctx->userFunc(ctx, 0);
833 static void
834 xml_end_tag(void *userData, const char *name)
836 struct xml_ctx *ctx = (struct xml_ctx *)userData;
837 const char *c = strchr(name, ':');
838 char *ep;
840 ctx->userFunc(ctx, 1);
842 if (c == NULL)
843 c = name;
844 else
845 c++;
847 ep = ctx->name + strlen(ctx->name) - strlen(c) - 1;
848 *ep = 0;
851 static void
852 xml_cdata(void *userData, const XML_Char *s, int len)
854 struct xml_ctx *ctx = (struct xml_ctx *)userData;
855 free(ctx->cdata);
856 ctx->cdata = xmemdupz(s, len);
859 static struct remote_lock *lock_remote(const char *path, long timeout)
861 struct active_request_slot *slot;
862 struct slot_results results;
863 struct buffer out_buffer = { STRBUF_INIT, 0 };
864 struct strbuf in_buffer = STRBUF_INIT;
865 char *url;
866 char *ep;
867 char timeout_header[25];
868 struct remote_lock *lock = NULL;
869 struct curl_slist *dav_headers = NULL;
870 struct xml_ctx ctx;
871 char *escaped;
873 url = xmalloc(strlen(repo->url) + strlen(path) + 1);
874 sprintf(url, "%s%s", repo->url, path);
876 /* Make sure leading directories exist for the remote ref */
877 ep = strchr(url + strlen(repo->url) + 1, '/');
878 while (ep) {
879 char saved_character = ep[1];
880 ep[1] = '\0';
881 slot = get_active_slot();
882 slot->results = &results;
883 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
884 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
885 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
886 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
887 if (start_active_slot(slot)) {
888 run_active_slot(slot);
889 if (results.curl_result != CURLE_OK &&
890 results.http_code != 405) {
891 fprintf(stderr,
892 "Unable to create branch path %s\n",
893 url);
894 free(url);
895 return NULL;
897 } else {
898 fprintf(stderr, "Unable to start MKCOL request\n");
899 free(url);
900 return NULL;
902 ep[1] = saved_character;
903 ep = strchr(ep + 1, '/');
906 escaped = xml_entities(git_default_email);
907 strbuf_addf(&out_buffer.buf, LOCK_REQUEST, escaped);
908 free(escaped);
910 sprintf(timeout_header, "Timeout: Second-%ld", timeout);
911 dav_headers = curl_slist_append(dav_headers, timeout_header);
912 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
914 slot = get_active_slot();
915 slot->results = &results;
916 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
917 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
918 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
919 #ifndef NO_CURL_IOCTL
920 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
921 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
922 #endif
923 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
924 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
925 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
926 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
927 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
928 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
930 lock = xcalloc(1, sizeof(*lock));
931 lock->timeout = -1;
933 if (start_active_slot(slot)) {
934 run_active_slot(slot);
935 if (results.curl_result == CURLE_OK) {
936 XML_Parser parser = XML_ParserCreate(NULL);
937 enum XML_Status result;
938 ctx.name = xcalloc(10, 1);
939 ctx.len = 0;
940 ctx.cdata = NULL;
941 ctx.userFunc = handle_new_lock_ctx;
942 ctx.userData = lock;
943 XML_SetUserData(parser, &ctx);
944 XML_SetElementHandler(parser, xml_start_tag,
945 xml_end_tag);
946 XML_SetCharacterDataHandler(parser, xml_cdata);
947 result = XML_Parse(parser, in_buffer.buf,
948 in_buffer.len, 1);
949 free(ctx.name);
950 if (result != XML_STATUS_OK) {
951 fprintf(stderr, "XML error: %s\n",
952 XML_ErrorString(
953 XML_GetErrorCode(parser)));
954 lock->timeout = -1;
956 XML_ParserFree(parser);
958 } else {
959 fprintf(stderr, "Unable to start LOCK request\n");
962 curl_slist_free_all(dav_headers);
963 strbuf_release(&out_buffer.buf);
964 strbuf_release(&in_buffer);
966 if (lock->token == NULL || lock->timeout <= 0) {
967 free(lock->token);
968 free(lock->owner);
969 free(url);
970 free(lock);
971 lock = NULL;
972 } else {
973 lock->url = url;
974 lock->start_time = time(NULL);
975 lock->next = repo->locks;
976 repo->locks = lock;
979 return lock;
982 static int unlock_remote(struct remote_lock *lock)
984 struct active_request_slot *slot;
985 struct slot_results results;
986 struct remote_lock *prev = repo->locks;
987 struct curl_slist *dav_headers;
988 int rc = 0;
990 dav_headers = get_dav_token_headers(lock, DAV_HEADER_LOCK);
992 slot = get_active_slot();
993 slot->results = &results;
994 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
995 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
996 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_UNLOCK);
997 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
999 if (start_active_slot(slot)) {
1000 run_active_slot(slot);
1001 if (results.curl_result == CURLE_OK)
1002 rc = 1;
1003 else
1004 fprintf(stderr, "UNLOCK HTTP error %ld\n",
1005 results.http_code);
1006 } else {
1007 fprintf(stderr, "Unable to start UNLOCK request\n");
1010 curl_slist_free_all(dav_headers);
1012 if (repo->locks == lock) {
1013 repo->locks = lock->next;
1014 } else {
1015 while (prev && prev->next != lock)
1016 prev = prev->next;
1017 if (prev)
1018 prev->next = prev->next->next;
1021 free(lock->owner);
1022 free(lock->url);
1023 free(lock->token);
1024 free(lock);
1026 return rc;
1029 static void remove_locks(void)
1031 struct remote_lock *lock = repo->locks;
1033 fprintf(stderr, "Removing remote locks...\n");
1034 while (lock) {
1035 struct remote_lock *next = lock->next;
1036 unlock_remote(lock);
1037 lock = next;
1041 static void remove_locks_on_signal(int signo)
1043 remove_locks();
1044 sigchain_pop(signo);
1045 raise(signo);
1048 static void remote_ls(const char *path, int flags,
1049 void (*userFunc)(struct remote_ls_ctx *ls),
1050 void *userData);
1052 static void process_ls_object(struct remote_ls_ctx *ls)
1054 unsigned int *parent = (unsigned int *)ls->userData;
1055 char *path = ls->dentry_name;
1056 char *obj_hex;
1058 if (!strcmp(ls->path, ls->dentry_name) && (ls->flags & IS_DIR)) {
1059 remote_dir_exists[*parent] = 1;
1060 return;
1063 if (strlen(path) != 49)
1064 return;
1065 path += 8;
1066 obj_hex = xmalloc(strlen(path));
1067 /* NB: path is not null-terminated, can not use strlcpy here */
1068 memcpy(obj_hex, path, 2);
1069 strcpy(obj_hex + 2, path + 3);
1070 one_remote_object(obj_hex);
1071 free(obj_hex);
1074 static void process_ls_ref(struct remote_ls_ctx *ls)
1076 if (!strcmp(ls->path, ls->dentry_name) && (ls->dentry_flags & IS_DIR)) {
1077 fprintf(stderr, " %s\n", ls->dentry_name);
1078 return;
1081 if (!(ls->dentry_flags & IS_DIR))
1082 one_remote_ref(ls->dentry_name);
1085 static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
1087 struct remote_ls_ctx *ls = (struct remote_ls_ctx *)ctx->userData;
1089 if (tag_closed) {
1090 if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) {
1091 if (ls->dentry_flags & IS_DIR) {
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 path += repo->path_len;
1115 ls->dentry_name = xstrdup(path);
1117 } else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
1118 ls->dentry_flags |= IS_DIR;
1120 } else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
1121 free(ls->dentry_name);
1122 ls->dentry_name = NULL;
1123 ls->dentry_flags = 0;
1128 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1129 * should _only_ heed the information from that file, instead of trying to
1130 * determine the refs from the remote file system (badly: it does not even
1131 * know about packed-refs).
1133 static void remote_ls(const char *path, int flags,
1134 void (*userFunc)(struct remote_ls_ctx *ls),
1135 void *userData)
1137 char *url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1138 struct active_request_slot *slot;
1139 struct slot_results results;
1140 struct strbuf in_buffer = STRBUF_INIT;
1141 struct buffer out_buffer = { STRBUF_INIT, 0 };
1142 struct curl_slist *dav_headers = NULL;
1143 struct xml_ctx ctx;
1144 struct remote_ls_ctx ls;
1146 ls.flags = flags;
1147 ls.path = xstrdup(path);
1148 ls.dentry_name = NULL;
1149 ls.dentry_flags = 0;
1150 ls.userData = userData;
1151 ls.userFunc = userFunc;
1153 sprintf(url, "%s%s", repo->url, path);
1155 strbuf_addf(&out_buffer.buf, PROPFIND_ALL_REQUEST);
1157 dav_headers = curl_slist_append(dav_headers, "Depth: 1");
1158 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1160 slot = get_active_slot();
1161 slot->results = &results;
1162 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1163 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1164 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1165 #ifndef NO_CURL_IOCTL
1166 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1167 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
1168 #endif
1169 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1170 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1171 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1172 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1173 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1174 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1176 if (start_active_slot(slot)) {
1177 run_active_slot(slot);
1178 if (results.curl_result == CURLE_OK) {
1179 XML_Parser parser = XML_ParserCreate(NULL);
1180 enum XML_Status result;
1181 ctx.name = xcalloc(10, 1);
1182 ctx.len = 0;
1183 ctx.cdata = NULL;
1184 ctx.userFunc = handle_remote_ls_ctx;
1185 ctx.userData = &ls;
1186 XML_SetUserData(parser, &ctx);
1187 XML_SetElementHandler(parser, xml_start_tag,
1188 xml_end_tag);
1189 XML_SetCharacterDataHandler(parser, xml_cdata);
1190 result = XML_Parse(parser, in_buffer.buf,
1191 in_buffer.len, 1);
1192 free(ctx.name);
1194 if (result != XML_STATUS_OK) {
1195 fprintf(stderr, "XML error: %s\n",
1196 XML_ErrorString(
1197 XML_GetErrorCode(parser)));
1199 XML_ParserFree(parser);
1201 } else {
1202 fprintf(stderr, "Unable to start PROPFIND request\n");
1205 free(ls.path);
1206 free(url);
1207 strbuf_release(&out_buffer.buf);
1208 strbuf_release(&in_buffer);
1209 curl_slist_free_all(dav_headers);
1212 static void get_remote_object_list(unsigned char parent)
1214 char path[] = "objects/XX/";
1215 static const char hex[] = "0123456789abcdef";
1216 unsigned int val = parent;
1218 path[8] = hex[val >> 4];
1219 path[9] = hex[val & 0xf];
1220 remote_dir_exists[val] = 0;
1221 remote_ls(path, (PROCESS_FILES | PROCESS_DIRS),
1222 process_ls_object, &val);
1225 static int locking_available(void)
1227 struct active_request_slot *slot;
1228 struct slot_results results;
1229 struct strbuf in_buffer = STRBUF_INIT;
1230 struct buffer out_buffer = { STRBUF_INIT, 0 };
1231 struct curl_slist *dav_headers = NULL;
1232 struct xml_ctx ctx;
1233 int lock_flags = 0;
1234 char *escaped;
1236 escaped = xml_entities(repo->url);
1237 strbuf_addf(&out_buffer.buf, PROPFIND_SUPPORTEDLOCK_REQUEST, escaped);
1238 free(escaped);
1240 dav_headers = curl_slist_append(dav_headers, "Depth: 0");
1241 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1243 slot = get_active_slot();
1244 slot->results = &results;
1245 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1246 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1247 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1248 #ifndef NO_CURL_IOCTL
1249 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1250 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
1251 #endif
1252 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1253 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1254 curl_easy_setopt(slot->curl, CURLOPT_URL, repo->url);
1255 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1256 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1257 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1259 if (start_active_slot(slot)) {
1260 run_active_slot(slot);
1261 if (results.curl_result == CURLE_OK) {
1262 XML_Parser parser = XML_ParserCreate(NULL);
1263 enum XML_Status result;
1264 ctx.name = xcalloc(10, 1);
1265 ctx.len = 0;
1266 ctx.cdata = NULL;
1267 ctx.userFunc = handle_lockprop_ctx;
1268 ctx.userData = &lock_flags;
1269 XML_SetUserData(parser, &ctx);
1270 XML_SetElementHandler(parser, xml_start_tag,
1271 xml_end_tag);
1272 result = XML_Parse(parser, in_buffer.buf,
1273 in_buffer.len, 1);
1274 free(ctx.name);
1276 if (result != XML_STATUS_OK) {
1277 fprintf(stderr, "XML error: %s\n",
1278 XML_ErrorString(
1279 XML_GetErrorCode(parser)));
1280 lock_flags = 0;
1282 XML_ParserFree(parser);
1283 if (!lock_flags)
1284 error("no DAV locking support on %s",
1285 repo->url);
1287 } else {
1288 error("Cannot access URL %s, return code %d",
1289 repo->url, results.curl_result);
1290 lock_flags = 0;
1292 } else {
1293 error("Unable to start PROPFIND request on %s", repo->url);
1296 strbuf_release(&out_buffer.buf);
1297 strbuf_release(&in_buffer);
1298 curl_slist_free_all(dav_headers);
1300 return lock_flags;
1303 static struct object_list **add_one_object(struct object *obj, struct object_list **p)
1305 struct object_list *entry = xmalloc(sizeof(struct object_list));
1306 entry->item = obj;
1307 entry->next = *p;
1308 *p = entry;
1309 return &entry->next;
1312 static struct object_list **process_blob(struct blob *blob,
1313 struct object_list **p,
1314 struct name_path *path,
1315 const char *name)
1317 struct object *obj = &blob->object;
1319 obj->flags |= LOCAL;
1321 if (obj->flags & (UNINTERESTING | SEEN))
1322 return p;
1324 obj->flags |= SEEN;
1325 return add_one_object(obj, p);
1328 static struct object_list **process_tree(struct tree *tree,
1329 struct object_list **p,
1330 struct name_path *path,
1331 const char *name)
1333 struct object *obj = &tree->object;
1334 struct tree_desc desc;
1335 struct name_entry entry;
1336 struct name_path me;
1338 obj->flags |= LOCAL;
1340 if (obj->flags & (UNINTERESTING | SEEN))
1341 return p;
1342 if (parse_tree(tree) < 0)
1343 die("bad tree object %s", sha1_to_hex(obj->sha1));
1345 obj->flags |= SEEN;
1346 name = xstrdup(name);
1347 p = add_one_object(obj, p);
1348 me.up = path;
1349 me.elem = name;
1350 me.elem_len = strlen(name);
1352 init_tree_desc(&desc, tree->buffer, tree->size);
1354 while (tree_entry(&desc, &entry))
1355 switch (object_type(entry.mode)) {
1356 case OBJ_TREE:
1357 p = process_tree(lookup_tree(entry.sha1), p, &me, name);
1358 break;
1359 case OBJ_BLOB:
1360 p = process_blob(lookup_blob(entry.sha1), p, &me, name);
1361 break;
1362 default:
1363 /* Subproject commit - not in this repository */
1364 break;
1367 free(tree->buffer);
1368 tree->buffer = NULL;
1369 return p;
1372 static int get_delta(struct rev_info *revs, struct remote_lock *lock)
1374 int i;
1375 struct commit *commit;
1376 struct object_list **p = &objects;
1377 int count = 0;
1379 while ((commit = get_revision(revs)) != NULL) {
1380 p = process_tree(commit->tree, p, NULL, "");
1381 commit->object.flags |= LOCAL;
1382 if (!(commit->object.flags & UNINTERESTING))
1383 count += add_send_request(&commit->object, lock);
1386 for (i = 0; i < revs->pending.nr; i++) {
1387 struct object_array_entry *entry = revs->pending.objects + i;
1388 struct object *obj = entry->item;
1389 const char *name = entry->name;
1391 if (obj->flags & (UNINTERESTING | SEEN))
1392 continue;
1393 if (obj->type == OBJ_TAG) {
1394 obj->flags |= SEEN;
1395 p = add_one_object(obj, p);
1396 continue;
1398 if (obj->type == OBJ_TREE) {
1399 p = process_tree((struct tree *)obj, p, NULL, name);
1400 continue;
1402 if (obj->type == OBJ_BLOB) {
1403 p = process_blob((struct blob *)obj, p, NULL, name);
1404 continue;
1406 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
1409 while (objects) {
1410 if (!(objects->item->flags & UNINTERESTING))
1411 count += add_send_request(objects->item, lock);
1412 objects = objects->next;
1415 return count;
1418 static int update_remote(unsigned char *sha1, struct remote_lock *lock)
1420 struct active_request_slot *slot;
1421 struct slot_results results;
1422 struct buffer out_buffer = { STRBUF_INIT, 0 };
1423 struct curl_slist *dav_headers;
1425 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1427 strbuf_addf(&out_buffer.buf, "%s\n", sha1_to_hex(sha1));
1429 slot = get_active_slot();
1430 slot->results = &results;
1431 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1432 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1433 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1434 #ifndef NO_CURL_IOCTL
1435 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1436 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
1437 #endif
1438 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1439 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1440 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1441 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1442 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1443 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1445 if (start_active_slot(slot)) {
1446 run_active_slot(slot);
1447 strbuf_release(&out_buffer.buf);
1448 if (results.curl_result != CURLE_OK) {
1449 fprintf(stderr,
1450 "PUT error: curl result=%d, HTTP code=%ld\n",
1451 results.curl_result, results.http_code);
1452 /* We should attempt recovery? */
1453 return 0;
1455 } else {
1456 strbuf_release(&out_buffer.buf);
1457 fprintf(stderr, "Unable to start PUT request\n");
1458 return 0;
1461 return 1;
1464 static struct ref *remote_refs;
1466 static void one_remote_ref(char *refname)
1468 struct ref *ref;
1469 struct object *obj;
1471 ref = alloc_ref(refname);
1473 if (http_fetch_ref(repo->url, ref) != 0) {
1474 fprintf(stderr,
1475 "Unable to fetch ref %s from %s\n",
1476 refname, repo->url);
1477 free(ref);
1478 return;
1482 * Fetch a copy of the object if it doesn't exist locally - it
1483 * may be required for updating server info later.
1485 if (repo->can_update_info_refs && !has_sha1_file(ref->old_sha1)) {
1486 obj = lookup_unknown_object(ref->old_sha1);
1487 if (obj) {
1488 fprintf(stderr, " fetch %s for %s\n",
1489 sha1_to_hex(ref->old_sha1), refname);
1490 add_fetch_request(obj);
1494 ref->next = remote_refs;
1495 remote_refs = ref;
1498 static void get_dav_remote_heads(void)
1500 remote_ls("refs/", (PROCESS_FILES | PROCESS_DIRS | RECURSIVE), process_ls_ref, NULL);
1503 static void add_remote_info_ref(struct remote_ls_ctx *ls)
1505 struct strbuf *buf = (struct strbuf *)ls->userData;
1506 struct object *o;
1507 int len;
1508 char *ref_info;
1509 struct ref *ref;
1511 ref = alloc_ref(ls->dentry_name);
1513 if (http_fetch_ref(repo->url, ref) != 0) {
1514 fprintf(stderr,
1515 "Unable to fetch ref %s from %s\n",
1516 ls->dentry_name, repo->url);
1517 aborted = 1;
1518 free(ref);
1519 return;
1522 o = parse_object(ref->old_sha1);
1523 if (!o) {
1524 fprintf(stderr,
1525 "Unable to parse object %s for remote ref %s\n",
1526 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1527 aborted = 1;
1528 free(ref);
1529 return;
1532 len = strlen(ls->dentry_name) + 42;
1533 ref_info = xcalloc(len + 1, 1);
1534 sprintf(ref_info, "%s %s\n",
1535 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1536 fwrite_buffer(ref_info, 1, len, buf);
1537 free(ref_info);
1539 if (o->type == OBJ_TAG) {
1540 o = deref_tag(o, ls->dentry_name, 0);
1541 if (o) {
1542 len = strlen(ls->dentry_name) + 45;
1543 ref_info = xcalloc(len + 1, 1);
1544 sprintf(ref_info, "%s %s^{}\n",
1545 sha1_to_hex(o->sha1), ls->dentry_name);
1546 fwrite_buffer(ref_info, 1, len, buf);
1547 free(ref_info);
1550 free(ref);
1553 static void update_remote_info_refs(struct remote_lock *lock)
1555 struct buffer buffer = { STRBUF_INIT, 0 };
1556 struct active_request_slot *slot;
1557 struct slot_results results;
1558 struct curl_slist *dav_headers;
1560 remote_ls("refs/", (PROCESS_FILES | RECURSIVE),
1561 add_remote_info_ref, &buffer.buf);
1562 if (!aborted) {
1563 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1565 slot = get_active_slot();
1566 slot->results = &results;
1567 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &buffer);
1568 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, buffer.buf.len);
1569 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1570 #ifndef NO_CURL_IOCTL
1571 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1572 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &buffer);
1573 #endif
1574 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1575 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1576 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1577 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1578 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1579 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1581 if (start_active_slot(slot)) {
1582 run_active_slot(slot);
1583 if (results.curl_result != CURLE_OK) {
1584 fprintf(stderr,
1585 "PUT error: curl result=%d, HTTP code=%ld\n",
1586 results.curl_result, results.http_code);
1590 strbuf_release(&buffer.buf);
1593 static int remote_exists(const char *path)
1595 char *url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1596 int ret;
1598 sprintf(url, "%s%s", repo->url, path);
1600 switch (http_get_strbuf(url, NULL, 0)) {
1601 case HTTP_OK:
1602 ret = 1;
1603 break;
1604 case HTTP_MISSING_TARGET:
1605 ret = 0;
1606 break;
1607 case HTTP_ERROR:
1608 http_error(url, HTTP_ERROR);
1609 default:
1610 ret = -1;
1612 free(url);
1613 return ret;
1616 static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
1618 char *url;
1619 struct strbuf buffer = STRBUF_INIT;
1621 url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1622 sprintf(url, "%s%s", repo->url, path);
1624 if (http_get_strbuf(url, &buffer, 0) != HTTP_OK)
1625 die("Couldn't get %s for remote symref\n%s", url,
1626 curl_errorstr);
1627 free(url);
1629 free(*symref);
1630 *symref = NULL;
1631 hashclr(sha1);
1633 if (buffer.len == 0)
1634 return;
1636 /* If it's a symref, set the refname; otherwise try for a sha1 */
1637 if (!prefixcmp((char *)buffer.buf, "ref: ")) {
1638 *symref = xmemdupz((char *)buffer.buf + 5, buffer.len - 6);
1639 } else {
1640 get_sha1_hex(buffer.buf, sha1);
1643 strbuf_release(&buffer);
1646 static int verify_merge_base(unsigned char *head_sha1, unsigned char *branch_sha1)
1648 struct commit *head = lookup_commit(head_sha1);
1649 struct commit *branch = lookup_commit(branch_sha1);
1650 struct commit_list *merge_bases = get_merge_bases(head, branch, 1);
1652 return (merge_bases && !merge_bases->next && merge_bases->item == branch);
1655 static int delete_remote_branch(char *pattern, int force)
1657 struct ref *refs = remote_refs;
1658 struct ref *remote_ref = NULL;
1659 unsigned char head_sha1[20];
1660 char *symref = NULL;
1661 int match;
1662 int patlen = strlen(pattern);
1663 int i;
1664 struct active_request_slot *slot;
1665 struct slot_results results;
1666 char *url;
1668 /* Find the remote branch(es) matching the specified branch name */
1669 for (match = 0; refs; refs = refs->next) {
1670 char *name = refs->name;
1671 int namelen = strlen(name);
1672 if (namelen < patlen ||
1673 memcmp(name + namelen - patlen, pattern, patlen))
1674 continue;
1675 if (namelen != patlen && name[namelen - patlen - 1] != '/')
1676 continue;
1677 match++;
1678 remote_ref = refs;
1680 if (match == 0)
1681 return error("No remote branch matches %s", pattern);
1682 if (match != 1)
1683 return error("More than one remote branch matches %s",
1684 pattern);
1687 * Remote HEAD must be a symref (not exactly foolproof; a remote
1688 * symlink to a symref will look like a symref)
1690 fetch_symref("HEAD", &symref, head_sha1);
1691 if (!symref)
1692 return error("Remote HEAD is not a symref");
1694 /* Remote branch must not be the remote HEAD */
1695 for (i=0; symref && i<MAXDEPTH; i++) {
1696 if (!strcmp(remote_ref->name, symref))
1697 return error("Remote branch %s is the current HEAD",
1698 remote_ref->name);
1699 fetch_symref(symref, &symref, head_sha1);
1702 /* Run extra sanity checks if delete is not forced */
1703 if (!force) {
1704 /* Remote HEAD must resolve to a known object */
1705 if (symref)
1706 return error("Remote HEAD symrefs too deep");
1707 if (is_null_sha1(head_sha1))
1708 return error("Unable to resolve remote HEAD");
1709 if (!has_sha1_file(head_sha1))
1710 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1));
1712 /* Remote branch must resolve to a known object */
1713 if (is_null_sha1(remote_ref->old_sha1))
1714 return error("Unable to resolve remote branch %s",
1715 remote_ref->name);
1716 if (!has_sha1_file(remote_ref->old_sha1))
1717 return error("Remote branch %s resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", remote_ref->name, sha1_to_hex(remote_ref->old_sha1));
1719 /* Remote branch must be an ancestor of remote HEAD */
1720 if (!verify_merge_base(head_sha1, remote_ref->old_sha1)) {
1721 return error("The branch '%s' is not an ancestor "
1722 "of your current HEAD.\n"
1723 "If you are sure you want to delete it,"
1724 " run:\n\t'git http-push -D %s %s'",
1725 remote_ref->name, repo->url, pattern);
1729 /* Send delete request */
1730 fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name);
1731 if (dry_run)
1732 return 0;
1733 url = xmalloc(strlen(repo->url) + strlen(remote_ref->name) + 1);
1734 sprintf(url, "%s%s", repo->url, remote_ref->name);
1735 slot = get_active_slot();
1736 slot->results = &results;
1737 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
1738 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1739 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1740 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_DELETE);
1741 if (start_active_slot(slot)) {
1742 run_active_slot(slot);
1743 free(url);
1744 if (results.curl_result != CURLE_OK)
1745 return error("DELETE request failed (%d/%ld)\n",
1746 results.curl_result, results.http_code);
1747 } else {
1748 free(url);
1749 return error("Unable to start DELETE request");
1752 return 0;
1755 static void run_request_queue(void)
1757 #ifdef USE_CURL_MULTI
1758 is_running_queue = 1;
1759 fill_active_slots();
1760 add_fill_function(NULL, fill_active_slot);
1761 #endif
1762 do {
1763 finish_all_active_slots();
1764 #ifdef USE_CURL_MULTI
1765 fill_active_slots();
1766 #endif
1767 } while (request_queue_head && !aborted);
1769 #ifdef USE_CURL_MULTI
1770 is_running_queue = 0;
1771 #endif
1774 int main(int argc, char **argv)
1776 struct transfer_request *request;
1777 struct transfer_request *next_request;
1778 int nr_refspec = 0;
1779 char **refspec = NULL;
1780 struct remote_lock *ref_lock = NULL;
1781 struct remote_lock *info_ref_lock = NULL;
1782 struct rev_info revs;
1783 int delete_branch = 0;
1784 int force_delete = 0;
1785 int objects_to_send;
1786 int rc = 0;
1787 int i;
1788 int new_refs;
1789 struct ref *ref, *local_refs;
1790 struct remote *remote;
1791 char *rewritten_url = NULL;
1793 git_extract_argv0_path(argv[0]);
1795 setup_git_directory();
1797 repo = xcalloc(sizeof(*repo), 1);
1799 argv++;
1800 for (i = 1; i < argc; i++, argv++) {
1801 char *arg = *argv;
1803 if (*arg == '-') {
1804 if (!strcmp(arg, "--all")) {
1805 push_all = MATCH_REFS_ALL;
1806 continue;
1808 if (!strcmp(arg, "--force")) {
1809 force_all = 1;
1810 continue;
1812 if (!strcmp(arg, "--dry-run")) {
1813 dry_run = 1;
1814 continue;
1816 if (!strcmp(arg, "--verbose")) {
1817 push_verbosely = 1;
1818 http_is_verbose = 1;
1819 continue;
1821 if (!strcmp(arg, "-d")) {
1822 delete_branch = 1;
1823 continue;
1825 if (!strcmp(arg, "-D")) {
1826 delete_branch = 1;
1827 force_delete = 1;
1828 continue;
1831 if (!repo->url) {
1832 char *path = strstr(arg, "//");
1833 repo->url = arg;
1834 repo->path_len = strlen(arg);
1835 if (path) {
1836 repo->path = strchr(path+2, '/');
1837 if (repo->path)
1838 repo->path_len = strlen(repo->path);
1840 continue;
1842 refspec = argv;
1843 nr_refspec = argc - i;
1844 break;
1847 #ifndef USE_CURL_MULTI
1848 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
1849 #endif
1851 if (!repo->url)
1852 usage(http_push_usage);
1854 if (delete_branch && nr_refspec != 1)
1855 die("You must specify only one branch name when deleting a remote branch");
1857 memset(remote_dir_exists, -1, 256);
1860 * Create a minimum remote by hand to give to http_init(),
1861 * primarily to allow it to look at the URL.
1863 remote = xcalloc(sizeof(*remote), 1);
1864 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
1865 remote->url[remote->url_nr++] = repo->url;
1866 http_init(remote);
1868 if (repo->url && repo->url[strlen(repo->url)-1] != '/') {
1869 rewritten_url = xmalloc(strlen(repo->url)+2);
1870 strcpy(rewritten_url, repo->url);
1871 strcat(rewritten_url, "/");
1872 repo->path = rewritten_url + (repo->path - repo->url);
1873 repo->path_len++;
1874 repo->url = rewritten_url;
1877 #ifdef USE_CURL_MULTI
1878 is_running_queue = 0;
1879 #endif
1881 /* Verify DAV compliance/lock support */
1882 if (!locking_available()) {
1883 rc = 1;
1884 goto cleanup;
1887 sigchain_push_common(remove_locks_on_signal);
1889 /* Check whether the remote has server info files */
1890 repo->can_update_info_refs = 0;
1891 repo->has_info_refs = remote_exists("info/refs");
1892 repo->has_info_packs = remote_exists("objects/info/packs");
1893 if (repo->has_info_refs) {
1894 info_ref_lock = lock_remote("info/refs", LOCK_TIME);
1895 if (info_ref_lock)
1896 repo->can_update_info_refs = 1;
1897 else {
1898 error("cannot lock existing info/refs");
1899 rc = 1;
1900 goto cleanup;
1903 if (repo->has_info_packs)
1904 fetch_indices();
1906 /* Get a list of all local and remote heads to validate refspecs */
1907 local_refs = get_local_heads();
1908 fprintf(stderr, "Fetching remote heads...\n");
1909 get_dav_remote_heads();
1910 run_request_queue();
1912 /* Remove a remote branch if -d or -D was specified */
1913 if (delete_branch) {
1914 if (delete_remote_branch(refspec[0], force_delete) == -1)
1915 fprintf(stderr, "Unable to delete remote branch %s\n",
1916 refspec[0]);
1917 goto cleanup;
1920 /* match them up */
1921 if (match_refs(local_refs, &remote_refs,
1922 nr_refspec, (const char **) refspec, push_all)) {
1923 rc = -1;
1924 goto cleanup;
1926 if (!remote_refs) {
1927 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
1928 rc = 0;
1929 goto cleanup;
1932 new_refs = 0;
1933 for (ref = remote_refs; ref; ref = ref->next) {
1934 char old_hex[60], *new_hex;
1935 const char *commit_argv[5];
1936 int commit_argc;
1937 char *new_sha1_hex, *old_sha1_hex;
1939 if (!ref->peer_ref)
1940 continue;
1942 if (is_null_sha1(ref->peer_ref->new_sha1)) {
1943 if (delete_remote_branch(ref->name, 1) == -1) {
1944 error("Could not remove %s", ref->name);
1945 rc = -4;
1947 new_refs++;
1948 continue;
1951 if (!hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
1952 if (push_verbosely || 1)
1953 fprintf(stderr, "'%s': up-to-date\n", ref->name);
1954 continue;
1957 if (!force_all &&
1958 !is_null_sha1(ref->old_sha1) &&
1959 !ref->force) {
1960 if (!has_sha1_file(ref->old_sha1) ||
1961 !ref_newer(ref->peer_ref->new_sha1,
1962 ref->old_sha1)) {
1964 * We do not have the remote ref, or
1965 * we know that the remote ref is not
1966 * an ancestor of what we are trying to
1967 * push. Either way this can be losing
1968 * commits at the remote end and likely
1969 * we were not up to date to begin with.
1971 error("remote '%s' is not an ancestor of\n"
1972 "local '%s'.\n"
1973 "Maybe you are not up-to-date and "
1974 "need to pull first?",
1975 ref->name,
1976 ref->peer_ref->name);
1977 rc = -2;
1978 continue;
1981 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
1982 new_refs++;
1983 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
1984 new_hex = sha1_to_hex(ref->new_sha1);
1986 fprintf(stderr, "updating '%s'", ref->name);
1987 if (strcmp(ref->name, ref->peer_ref->name))
1988 fprintf(stderr, " using '%s'", ref->peer_ref->name);
1989 fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex);
1990 if (dry_run)
1991 continue;
1993 /* Lock remote branch ref */
1994 ref_lock = lock_remote(ref->name, LOCK_TIME);
1995 if (ref_lock == NULL) {
1996 fprintf(stderr, "Unable to lock remote branch %s\n",
1997 ref->name);
1998 rc = 1;
1999 continue;
2002 /* Set up revision info for this refspec */
2003 commit_argc = 3;
2004 new_sha1_hex = xstrdup(sha1_to_hex(ref->new_sha1));
2005 old_sha1_hex = NULL;
2006 commit_argv[1] = "--objects";
2007 commit_argv[2] = new_sha1_hex;
2008 if (!push_all && !is_null_sha1(ref->old_sha1)) {
2009 old_sha1_hex = xmalloc(42);
2010 sprintf(old_sha1_hex, "^%s",
2011 sha1_to_hex(ref->old_sha1));
2012 commit_argv[3] = old_sha1_hex;
2013 commit_argc++;
2015 commit_argv[commit_argc] = NULL;
2016 init_revisions(&revs, setup_git_directory());
2017 setup_revisions(commit_argc, commit_argv, &revs, NULL);
2018 revs.edge_hint = 0; /* just in case */
2019 free(new_sha1_hex);
2020 if (old_sha1_hex) {
2021 free(old_sha1_hex);
2022 commit_argv[1] = NULL;
2025 /* Generate a list of objects that need to be pushed */
2026 pushing = 0;
2027 if (prepare_revision_walk(&revs))
2028 die("revision walk setup failed");
2029 mark_edges_uninteresting(revs.commits, &revs, NULL);
2030 objects_to_send = get_delta(&revs, ref_lock);
2031 finish_all_active_slots();
2033 /* Push missing objects to remote, this would be a
2034 convenient time to pack them first if appropriate. */
2035 pushing = 1;
2036 if (objects_to_send)
2037 fprintf(stderr, " sending %d objects\n",
2038 objects_to_send);
2040 run_request_queue();
2042 /* Update the remote branch if all went well */
2043 if (aborted || !update_remote(ref->new_sha1, ref_lock))
2044 rc = 1;
2046 if (!rc)
2047 fprintf(stderr, " done\n");
2048 unlock_remote(ref_lock);
2049 check_locks();
2052 /* Update remote server info if appropriate */
2053 if (repo->has_info_refs && new_refs) {
2054 if (info_ref_lock && repo->can_update_info_refs) {
2055 fprintf(stderr, "Updating remote server info\n");
2056 if (!dry_run)
2057 update_remote_info_refs(info_ref_lock);
2058 } else {
2059 fprintf(stderr, "Unable to update server info\n");
2063 cleanup:
2064 free(rewritten_url);
2065 if (info_ref_lock)
2066 unlock_remote(info_ref_lock);
2067 free(repo);
2069 http_cleanup();
2071 request = request_queue_head;
2072 while (request != NULL) {
2073 next_request = request->next;
2074 release_request(request);
2075 request = next_request;
2078 return rc;