i18n: tag: mark parseopt strings for translation
[git/jnareb-git.git] / http-push.c
bloba832ca77a31245e8ef9c71adcc0d7110fcda1e5c
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(const 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 void curl_setup_http_get(CURL *curl, const char *url,
201 const char *custom_req)
203 curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
204 curl_easy_setopt(curl, CURLOPT_URL, url);
205 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, custom_req);
206 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_null);
209 static void curl_setup_http(CURL *curl, const char *url,
210 const char *custom_req, struct buffer *buffer,
211 curl_write_callback write_fn)
213 curl_easy_setopt(curl, CURLOPT_PUT, 1);
214 curl_easy_setopt(curl, CURLOPT_URL, url);
215 curl_easy_setopt(curl, CURLOPT_INFILE, buffer);
216 curl_easy_setopt(curl, CURLOPT_INFILESIZE, buffer->buf.len);
217 curl_easy_setopt(curl, CURLOPT_READFUNCTION, fread_buffer);
218 #ifndef NO_CURL_IOCTL
219 curl_easy_setopt(curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
220 curl_easy_setopt(curl, CURLOPT_IOCTLDATA, &buffer);
221 #endif
222 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_fn);
223 curl_easy_setopt(curl, CURLOPT_NOBODY, 0);
224 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, custom_req);
225 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
228 static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum dav_header_flag options)
230 struct strbuf buf = STRBUF_INIT;
231 struct curl_slist *dav_headers = NULL;
233 if (options & DAV_HEADER_IF) {
234 strbuf_addf(&buf, "If: (<%s>)", lock->token);
235 dav_headers = curl_slist_append(dav_headers, buf.buf);
236 strbuf_reset(&buf);
238 if (options & DAV_HEADER_LOCK) {
239 strbuf_addf(&buf, "Lock-Token: <%s>", lock->token);
240 dav_headers = curl_slist_append(dav_headers, buf.buf);
241 strbuf_reset(&buf);
243 if (options & DAV_HEADER_TIMEOUT) {
244 strbuf_addf(&buf, "Timeout: Second-%ld", lock->timeout);
245 dav_headers = curl_slist_append(dav_headers, buf.buf);
246 strbuf_reset(&buf);
248 strbuf_release(&buf);
250 return dav_headers;
253 static void finish_request(struct transfer_request *request);
254 static void release_request(struct transfer_request *request);
256 static void process_response(void *callback_data)
258 struct transfer_request *request =
259 (struct transfer_request *)callback_data;
261 finish_request(request);
264 #ifdef USE_CURL_MULTI
266 static void start_fetch_loose(struct transfer_request *request)
268 struct active_request_slot *slot;
269 struct http_object_request *obj_req;
271 obj_req = new_http_object_request(repo->url, request->obj->sha1);
272 if (obj_req == NULL) {
273 request->state = ABORTED;
274 return;
277 slot = obj_req->slot;
278 slot->callback_func = process_response;
279 slot->callback_data = request;
280 request->slot = slot;
281 request->userData = obj_req;
283 /* Try to get the request started, abort the request on error */
284 request->state = RUN_FETCH_LOOSE;
285 if (!start_active_slot(slot)) {
286 fprintf(stderr, "Unable to start GET request\n");
287 repo->can_update_info_refs = 0;
288 release_http_object_request(obj_req);
289 release_request(request);
293 static void start_mkcol(struct transfer_request *request)
295 char *hex = sha1_to_hex(request->obj->sha1);
296 struct active_request_slot *slot;
298 request->url = get_remote_object_url(repo->url, hex, 1);
300 slot = get_active_slot();
301 slot->callback_func = process_response;
302 slot->callback_data = request;
303 curl_setup_http_get(slot->curl, request->url, DAV_MKCOL);
304 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
306 if (start_active_slot(slot)) {
307 request->slot = slot;
308 request->state = RUN_MKCOL;
309 } else {
310 request->state = ABORTED;
311 free(request->url);
312 request->url = NULL;
315 #endif
317 static void start_fetch_packed(struct transfer_request *request)
319 struct packed_git *target;
321 struct transfer_request *check_request = request_queue_head;
322 struct http_pack_request *preq;
324 target = find_sha1_pack(request->obj->sha1, repo->packs);
325 if (!target) {
326 fprintf(stderr, "Unable to fetch %s, will not be able to update server info refs\n", sha1_to_hex(request->obj->sha1));
327 repo->can_update_info_refs = 0;
328 release_request(request);
329 return;
332 fprintf(stderr, "Fetching pack %s\n", sha1_to_hex(target->sha1));
333 fprintf(stderr, " which contains %s\n", sha1_to_hex(request->obj->sha1));
335 preq = new_http_pack_request(target, repo->url);
336 if (preq == NULL) {
337 release_http_pack_request(preq);
338 repo->can_update_info_refs = 0;
339 return;
341 preq->lst = &repo->packs;
343 /* Make sure there isn't another open request for this pack */
344 while (check_request) {
345 if (check_request->state == RUN_FETCH_PACKED &&
346 !strcmp(check_request->url, preq->url)) {
347 release_http_pack_request(preq);
348 release_request(request);
349 return;
351 check_request = check_request->next;
354 preq->slot->callback_func = process_response;
355 preq->slot->callback_data = request;
356 request->slot = preq->slot;
357 request->userData = preq;
359 /* Try to get the request started, abort the request on error */
360 request->state = RUN_FETCH_PACKED;
361 if (!start_active_slot(preq->slot)) {
362 fprintf(stderr, "Unable to start GET request\n");
363 release_http_pack_request(preq);
364 repo->can_update_info_refs = 0;
365 release_request(request);
369 static void start_put(struct transfer_request *request)
371 char *hex = sha1_to_hex(request->obj->sha1);
372 struct active_request_slot *slot;
373 struct strbuf buf = STRBUF_INIT;
374 enum object_type type;
375 char hdr[50];
376 void *unpacked;
377 unsigned long len;
378 int hdrlen;
379 ssize_t size;
380 git_zstream stream;
382 unpacked = read_sha1_file(request->obj->sha1, &type, &len);
383 hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
385 /* Set it up */
386 memset(&stream, 0, sizeof(stream));
387 git_deflate_init(&stream, zlib_compression_level);
388 size = git_deflate_bound(&stream, len + hdrlen);
389 strbuf_init(&request->buffer.buf, size);
390 request->buffer.posn = 0;
392 /* Compress it */
393 stream.next_out = (unsigned char *)request->buffer.buf.buf;
394 stream.avail_out = size;
396 /* First header.. */
397 stream.next_in = (void *)hdr;
398 stream.avail_in = hdrlen;
399 while (git_deflate(&stream, 0) == Z_OK)
400 ; /* nothing */
402 /* Then the data itself.. */
403 stream.next_in = unpacked;
404 stream.avail_in = len;
405 while (git_deflate(&stream, Z_FINISH) == Z_OK)
406 ; /* nothing */
407 git_deflate_end(&stream);
408 free(unpacked);
410 request->buffer.buf.len = stream.total_out;
412 strbuf_addstr(&buf, "Destination: ");
413 append_remote_object_url(&buf, repo->url, hex, 0);
414 request->dest = strbuf_detach(&buf, NULL);
416 append_remote_object_url(&buf, repo->url, hex, 0);
417 strbuf_add(&buf, request->lock->tmpfile_suffix, 41);
418 request->url = strbuf_detach(&buf, NULL);
420 slot = get_active_slot();
421 slot->callback_func = process_response;
422 slot->callback_data = request;
423 curl_setup_http(slot->curl, request->url, DAV_PUT,
424 &request->buffer, fwrite_null);
426 if (start_active_slot(slot)) {
427 request->slot = slot;
428 request->state = RUN_PUT;
429 } else {
430 request->state = ABORTED;
431 free(request->url);
432 request->url = NULL;
436 static void start_move(struct transfer_request *request)
438 struct active_request_slot *slot;
439 struct curl_slist *dav_headers = NULL;
441 slot = get_active_slot();
442 slot->callback_func = process_response;
443 slot->callback_data = request;
444 curl_setup_http_get(slot->curl, request->url, DAV_MOVE);
445 dav_headers = curl_slist_append(dav_headers, request->dest);
446 dav_headers = curl_slist_append(dav_headers, "Overwrite: T");
447 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
449 if (start_active_slot(slot)) {
450 request->slot = slot;
451 request->state = RUN_MOVE;
452 } else {
453 request->state = ABORTED;
454 free(request->url);
455 request->url = NULL;
459 static int refresh_lock(struct remote_lock *lock)
461 struct active_request_slot *slot;
462 struct slot_results results;
463 struct curl_slist *dav_headers;
464 int rc = 0;
466 lock->refreshing = 1;
468 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF | DAV_HEADER_TIMEOUT);
470 slot = get_active_slot();
471 slot->results = &results;
472 curl_setup_http_get(slot->curl, lock->url, DAV_LOCK);
473 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
475 if (start_active_slot(slot)) {
476 run_active_slot(slot);
477 if (results.curl_result != CURLE_OK) {
478 fprintf(stderr, "LOCK HTTP error %ld\n",
479 results.http_code);
480 } else {
481 lock->start_time = time(NULL);
482 rc = 1;
486 lock->refreshing = 0;
487 curl_slist_free_all(dav_headers);
489 return rc;
492 static void check_locks(void)
494 struct remote_lock *lock = repo->locks;
495 time_t current_time = time(NULL);
496 int time_remaining;
498 while (lock) {
499 time_remaining = lock->start_time + lock->timeout -
500 current_time;
501 if (!lock->refreshing && time_remaining < LOCK_REFRESH) {
502 if (!refresh_lock(lock)) {
503 fprintf(stderr,
504 "Unable to refresh lock for %s\n",
505 lock->url);
506 aborted = 1;
507 return;
510 lock = lock->next;
514 static void release_request(struct transfer_request *request)
516 struct transfer_request *entry = request_queue_head;
518 if (request == request_queue_head) {
519 request_queue_head = request->next;
520 } else {
521 while (entry->next != NULL && entry->next != request)
522 entry = entry->next;
523 if (entry->next == request)
524 entry->next = entry->next->next;
527 free(request->url);
528 free(request);
531 static void finish_request(struct transfer_request *request)
533 struct http_pack_request *preq;
534 struct http_object_request *obj_req;
536 request->curl_result = request->slot->curl_result;
537 request->http_code = request->slot->http_code;
538 request->slot = NULL;
540 /* Keep locks active */
541 check_locks();
543 if (request->headers != NULL)
544 curl_slist_free_all(request->headers);
546 /* URL is reused for MOVE after PUT */
547 if (request->state != RUN_PUT) {
548 free(request->url);
549 request->url = NULL;
552 if (request->state == RUN_MKCOL) {
553 if (request->curl_result == CURLE_OK ||
554 request->http_code == 405) {
555 remote_dir_exists[request->obj->sha1[0]] = 1;
556 start_put(request);
557 } else {
558 fprintf(stderr, "MKCOL %s failed, aborting (%d/%ld)\n",
559 sha1_to_hex(request->obj->sha1),
560 request->curl_result, request->http_code);
561 request->state = ABORTED;
562 aborted = 1;
564 } else if (request->state == RUN_PUT) {
565 if (request->curl_result == CURLE_OK) {
566 start_move(request);
567 } else {
568 fprintf(stderr, "PUT %s failed, aborting (%d/%ld)\n",
569 sha1_to_hex(request->obj->sha1),
570 request->curl_result, request->http_code);
571 request->state = ABORTED;
572 aborted = 1;
574 } else if (request->state == RUN_MOVE) {
575 if (request->curl_result == CURLE_OK) {
576 if (push_verbosely)
577 fprintf(stderr, " sent %s\n",
578 sha1_to_hex(request->obj->sha1));
579 request->obj->flags |= REMOTE;
580 release_request(request);
581 } else {
582 fprintf(stderr, "MOVE %s failed, aborting (%d/%ld)\n",
583 sha1_to_hex(request->obj->sha1),
584 request->curl_result, request->http_code);
585 request->state = ABORTED;
586 aborted = 1;
588 } else if (request->state == RUN_FETCH_LOOSE) {
589 obj_req = (struct http_object_request *)request->userData;
591 if (finish_http_object_request(obj_req) == 0)
592 if (obj_req->rename == 0)
593 request->obj->flags |= (LOCAL | REMOTE);
595 /* Try fetching packed if necessary */
596 if (request->obj->flags & LOCAL) {
597 release_http_object_request(obj_req);
598 release_request(request);
599 } else
600 start_fetch_packed(request);
602 } else if (request->state == RUN_FETCH_PACKED) {
603 int fail = 1;
604 if (request->curl_result != CURLE_OK) {
605 fprintf(stderr, "Unable to get pack file %s\n%s",
606 request->url, curl_errorstr);
607 } else {
608 preq = (struct http_pack_request *)request->userData;
610 if (preq) {
611 if (finish_http_pack_request(preq) == 0)
612 fail = 0;
613 release_http_pack_request(preq);
616 if (fail)
617 repo->can_update_info_refs = 0;
618 release_request(request);
622 #ifdef USE_CURL_MULTI
623 static int is_running_queue;
624 static int fill_active_slot(void *unused)
626 struct transfer_request *request;
628 if (aborted || !is_running_queue)
629 return 0;
631 for (request = request_queue_head; request; request = request->next) {
632 if (request->state == NEED_FETCH) {
633 start_fetch_loose(request);
634 return 1;
635 } else if (pushing && request->state == NEED_PUSH) {
636 if (remote_dir_exists[request->obj->sha1[0]] == 1) {
637 start_put(request);
638 } else {
639 start_mkcol(request);
641 return 1;
644 return 0;
646 #endif
648 static void get_remote_object_list(unsigned char parent);
650 static void add_fetch_request(struct object *obj)
652 struct transfer_request *request;
654 check_locks();
657 * Don't fetch the object if it's known to exist locally
658 * or is already in the request queue
660 if (remote_dir_exists[obj->sha1[0]] == -1)
661 get_remote_object_list(obj->sha1[0]);
662 if (obj->flags & (LOCAL | FETCHING))
663 return;
665 obj->flags |= FETCHING;
666 request = xmalloc(sizeof(*request));
667 request->obj = obj;
668 request->url = NULL;
669 request->lock = NULL;
670 request->headers = NULL;
671 request->state = NEED_FETCH;
672 request->next = request_queue_head;
673 request_queue_head = request;
675 #ifdef USE_CURL_MULTI
676 fill_active_slots();
677 step_active_slots();
678 #endif
681 static int add_send_request(struct object *obj, struct remote_lock *lock)
683 struct transfer_request *request = request_queue_head;
684 struct packed_git *target;
686 /* Keep locks active */
687 check_locks();
690 * Don't push the object if it's known to exist on the remote
691 * or is already in the request queue
693 if (remote_dir_exists[obj->sha1[0]] == -1)
694 get_remote_object_list(obj->sha1[0]);
695 if (obj->flags & (REMOTE | PUSHING))
696 return 0;
697 target = find_sha1_pack(obj->sha1, repo->packs);
698 if (target) {
699 obj->flags |= REMOTE;
700 return 0;
703 obj->flags |= PUSHING;
704 request = xmalloc(sizeof(*request));
705 request->obj = obj;
706 request->url = NULL;
707 request->lock = lock;
708 request->headers = NULL;
709 request->state = NEED_PUSH;
710 request->next = request_queue_head;
711 request_queue_head = request;
713 #ifdef USE_CURL_MULTI
714 fill_active_slots();
715 step_active_slots();
716 #endif
718 return 1;
721 static int fetch_indices(void)
723 int ret;
725 if (push_verbosely)
726 fprintf(stderr, "Getting pack list\n");
728 switch (http_get_info_packs(repo->url, &repo->packs)) {
729 case HTTP_OK:
730 case HTTP_MISSING_TARGET:
731 ret = 0;
732 break;
733 default:
734 ret = -1;
737 return ret;
740 static void one_remote_object(const char *hex)
742 unsigned char sha1[20];
743 struct object *obj;
745 if (get_sha1_hex(hex, sha1) != 0)
746 return;
748 obj = lookup_object(sha1);
749 if (!obj)
750 obj = parse_object(sha1);
752 /* Ignore remote objects that don't exist locally */
753 if (!obj)
754 return;
756 obj->flags |= REMOTE;
757 if (!object_list_contains(objects, obj))
758 object_list_insert(obj, &objects);
761 static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed)
763 int *lock_flags = (int *)ctx->userData;
765 if (tag_closed) {
766 if (!strcmp(ctx->name, DAV_CTX_LOCKENTRY)) {
767 if ((*lock_flags & DAV_PROP_LOCKEX) &&
768 (*lock_flags & DAV_PROP_LOCKWR)) {
769 *lock_flags |= DAV_LOCK_OK;
771 *lock_flags &= DAV_LOCK_OK;
772 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_WRITE)) {
773 *lock_flags |= DAV_PROP_LOCKWR;
774 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_EXCLUSIVE)) {
775 *lock_flags |= DAV_PROP_LOCKEX;
780 static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
782 struct remote_lock *lock = (struct remote_lock *)ctx->userData;
783 git_SHA_CTX sha_ctx;
784 unsigned char lock_token_sha1[20];
786 if (tag_closed && ctx->cdata) {
787 if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) {
788 lock->owner = xmalloc(strlen(ctx->cdata) + 1);
789 strcpy(lock->owner, ctx->cdata);
790 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
791 if (!prefixcmp(ctx->cdata, "Second-"))
792 lock->timeout =
793 strtol(ctx->cdata + 7, NULL, 10);
794 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
795 lock->token = xmalloc(strlen(ctx->cdata) + 1);
796 strcpy(lock->token, ctx->cdata);
798 git_SHA1_Init(&sha_ctx);
799 git_SHA1_Update(&sha_ctx, lock->token, strlen(lock->token));
800 git_SHA1_Final(lock_token_sha1, &sha_ctx);
802 lock->tmpfile_suffix[0] = '_';
803 memcpy(lock->tmpfile_suffix + 1, sha1_to_hex(lock_token_sha1), 40);
808 static void one_remote_ref(const char *refname);
810 static void
811 xml_start_tag(void *userData, const char *name, const char **atts)
813 struct xml_ctx *ctx = (struct xml_ctx *)userData;
814 const char *c = strchr(name, ':');
815 int new_len;
817 if (c == NULL)
818 c = name;
819 else
820 c++;
822 new_len = strlen(ctx->name) + strlen(c) + 2;
824 if (new_len > ctx->len) {
825 ctx->name = xrealloc(ctx->name, new_len);
826 ctx->len = new_len;
828 strcat(ctx->name, ".");
829 strcat(ctx->name, c);
831 free(ctx->cdata);
832 ctx->cdata = NULL;
834 ctx->userFunc(ctx, 0);
837 static void
838 xml_end_tag(void *userData, const char *name)
840 struct xml_ctx *ctx = (struct xml_ctx *)userData;
841 const char *c = strchr(name, ':');
842 char *ep;
844 ctx->userFunc(ctx, 1);
846 if (c == NULL)
847 c = name;
848 else
849 c++;
851 ep = ctx->name + strlen(ctx->name) - strlen(c) - 1;
852 *ep = 0;
855 static void
856 xml_cdata(void *userData, const XML_Char *s, int len)
858 struct xml_ctx *ctx = (struct xml_ctx *)userData;
859 free(ctx->cdata);
860 ctx->cdata = xmemdupz(s, len);
863 static struct remote_lock *lock_remote(const char *path, long timeout)
865 struct active_request_slot *slot;
866 struct slot_results results;
867 struct buffer out_buffer = { STRBUF_INIT, 0 };
868 struct strbuf in_buffer = STRBUF_INIT;
869 char *url;
870 char *ep;
871 char timeout_header[25];
872 struct remote_lock *lock = NULL;
873 struct curl_slist *dav_headers = NULL;
874 struct xml_ctx ctx;
875 char *escaped;
877 url = xmalloc(strlen(repo->url) + strlen(path) + 1);
878 sprintf(url, "%s%s", repo->url, path);
880 /* Make sure leading directories exist for the remote ref */
881 ep = strchr(url + strlen(repo->url) + 1, '/');
882 while (ep) {
883 char saved_character = ep[1];
884 ep[1] = '\0';
885 slot = get_active_slot();
886 slot->results = &results;
887 curl_setup_http_get(slot->curl, url, DAV_MKCOL);
888 if (start_active_slot(slot)) {
889 run_active_slot(slot);
890 if (results.curl_result != CURLE_OK &&
891 results.http_code != 405) {
892 fprintf(stderr,
893 "Unable to create branch path %s\n",
894 url);
895 free(url);
896 return NULL;
898 } else {
899 fprintf(stderr, "Unable to start MKCOL request\n");
900 free(url);
901 return NULL;
903 ep[1] = saved_character;
904 ep = strchr(ep + 1, '/');
907 escaped = xml_entities(ident_default_email());
908 strbuf_addf(&out_buffer.buf, LOCK_REQUEST, escaped);
909 free(escaped);
911 sprintf(timeout_header, "Timeout: Second-%ld", timeout);
912 dav_headers = curl_slist_append(dav_headers, timeout_header);
913 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
915 slot = get_active_slot();
916 slot->results = &results;
917 curl_setup_http(slot->curl, url, DAV_LOCK, &out_buffer, fwrite_buffer);
918 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
919 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
921 lock = xcalloc(1, sizeof(*lock));
922 lock->timeout = -1;
924 if (start_active_slot(slot)) {
925 run_active_slot(slot);
926 if (results.curl_result == CURLE_OK) {
927 XML_Parser parser = XML_ParserCreate(NULL);
928 enum XML_Status result;
929 ctx.name = xcalloc(10, 1);
930 ctx.len = 0;
931 ctx.cdata = NULL;
932 ctx.userFunc = handle_new_lock_ctx;
933 ctx.userData = lock;
934 XML_SetUserData(parser, &ctx);
935 XML_SetElementHandler(parser, xml_start_tag,
936 xml_end_tag);
937 XML_SetCharacterDataHandler(parser, xml_cdata);
938 result = XML_Parse(parser, in_buffer.buf,
939 in_buffer.len, 1);
940 free(ctx.name);
941 if (result != XML_STATUS_OK) {
942 fprintf(stderr, "XML error: %s\n",
943 XML_ErrorString(
944 XML_GetErrorCode(parser)));
945 lock->timeout = -1;
947 XML_ParserFree(parser);
949 } else {
950 fprintf(stderr, "Unable to start LOCK request\n");
953 curl_slist_free_all(dav_headers);
954 strbuf_release(&out_buffer.buf);
955 strbuf_release(&in_buffer);
957 if (lock->token == NULL || lock->timeout <= 0) {
958 free(lock->token);
959 free(lock->owner);
960 free(url);
961 free(lock);
962 lock = NULL;
963 } else {
964 lock->url = url;
965 lock->start_time = time(NULL);
966 lock->next = repo->locks;
967 repo->locks = lock;
970 return lock;
973 static int unlock_remote(struct remote_lock *lock)
975 struct active_request_slot *slot;
976 struct slot_results results;
977 struct remote_lock *prev = repo->locks;
978 struct curl_slist *dav_headers;
979 int rc = 0;
981 dav_headers = get_dav_token_headers(lock, DAV_HEADER_LOCK);
983 slot = get_active_slot();
984 slot->results = &results;
985 curl_setup_http_get(slot->curl, lock->url, DAV_UNLOCK);
986 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
988 if (start_active_slot(slot)) {
989 run_active_slot(slot);
990 if (results.curl_result == CURLE_OK)
991 rc = 1;
992 else
993 fprintf(stderr, "UNLOCK HTTP error %ld\n",
994 results.http_code);
995 } else {
996 fprintf(stderr, "Unable to start UNLOCK request\n");
999 curl_slist_free_all(dav_headers);
1001 if (repo->locks == lock) {
1002 repo->locks = lock->next;
1003 } else {
1004 while (prev && prev->next != lock)
1005 prev = prev->next;
1006 if (prev)
1007 prev->next = prev->next->next;
1010 free(lock->owner);
1011 free(lock->url);
1012 free(lock->token);
1013 free(lock);
1015 return rc;
1018 static void remove_locks(void)
1020 struct remote_lock *lock = repo->locks;
1022 fprintf(stderr, "Removing remote locks...\n");
1023 while (lock) {
1024 struct remote_lock *next = lock->next;
1025 unlock_remote(lock);
1026 lock = next;
1030 static void remove_locks_on_signal(int signo)
1032 remove_locks();
1033 sigchain_pop(signo);
1034 raise(signo);
1037 static void remote_ls(const char *path, int flags,
1038 void (*userFunc)(struct remote_ls_ctx *ls),
1039 void *userData);
1041 static void process_ls_object(struct remote_ls_ctx *ls)
1043 unsigned int *parent = (unsigned int *)ls->userData;
1044 char *path = ls->dentry_name;
1045 char *obj_hex;
1047 if (!strcmp(ls->path, ls->dentry_name) && (ls->flags & IS_DIR)) {
1048 remote_dir_exists[*parent] = 1;
1049 return;
1052 if (strlen(path) != 49)
1053 return;
1054 path += 8;
1055 obj_hex = xmalloc(strlen(path));
1056 /* NB: path is not null-terminated, can not use strlcpy here */
1057 memcpy(obj_hex, path, 2);
1058 strcpy(obj_hex + 2, path + 3);
1059 one_remote_object(obj_hex);
1060 free(obj_hex);
1063 static void process_ls_ref(struct remote_ls_ctx *ls)
1065 if (!strcmp(ls->path, ls->dentry_name) && (ls->dentry_flags & IS_DIR)) {
1066 fprintf(stderr, " %s\n", ls->dentry_name);
1067 return;
1070 if (!(ls->dentry_flags & IS_DIR))
1071 one_remote_ref(ls->dentry_name);
1074 static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
1076 struct remote_ls_ctx *ls = (struct remote_ls_ctx *)ctx->userData;
1078 if (tag_closed) {
1079 if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) {
1080 if (ls->dentry_flags & IS_DIR) {
1082 /* ensure collection names end with slash */
1083 str_end_url_with_slash(ls->dentry_name, &ls->dentry_name);
1085 if (ls->flags & PROCESS_DIRS) {
1086 ls->userFunc(ls);
1088 if (strcmp(ls->dentry_name, ls->path) &&
1089 ls->flags & RECURSIVE) {
1090 remote_ls(ls->dentry_name,
1091 ls->flags,
1092 ls->userFunc,
1093 ls->userData);
1095 } else if (ls->flags & PROCESS_FILES) {
1096 ls->userFunc(ls);
1098 } else if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
1099 char *path = ctx->cdata;
1100 if (*ctx->cdata == 'h') {
1101 path = strstr(path, "//");
1102 if (path) {
1103 path = strchr(path+2, '/');
1106 if (path) {
1107 const char *url = repo->url;
1108 if (repo->path)
1109 url = repo->path;
1110 if (strncmp(path, url, repo->path_len))
1111 error("Parsed path '%s' does not match url: '%s'",
1112 path, url);
1113 else {
1114 path += repo->path_len;
1115 ls->dentry_name = xstrdup(path);
1118 } else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
1119 ls->dentry_flags |= IS_DIR;
1121 } else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
1122 free(ls->dentry_name);
1123 ls->dentry_name = NULL;
1124 ls->dentry_flags = 0;
1129 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1130 * should _only_ heed the information from that file, instead of trying to
1131 * determine the refs from the remote file system (badly: it does not even
1132 * know about packed-refs).
1134 static void remote_ls(const char *path, int flags,
1135 void (*userFunc)(struct remote_ls_ctx *ls),
1136 void *userData)
1138 char *url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1139 struct active_request_slot *slot;
1140 struct slot_results results;
1141 struct strbuf in_buffer = STRBUF_INIT;
1142 struct buffer out_buffer = { STRBUF_INIT, 0 };
1143 struct curl_slist *dav_headers = NULL;
1144 struct xml_ctx ctx;
1145 struct remote_ls_ctx ls;
1147 ls.flags = flags;
1148 ls.path = xstrdup(path);
1149 ls.dentry_name = NULL;
1150 ls.dentry_flags = 0;
1151 ls.userData = userData;
1152 ls.userFunc = userFunc;
1154 sprintf(url, "%s%s", repo->url, path);
1156 strbuf_addf(&out_buffer.buf, PROPFIND_ALL_REQUEST);
1158 dav_headers = curl_slist_append(dav_headers, "Depth: 1");
1159 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1161 slot = get_active_slot();
1162 slot->results = &results;
1163 curl_setup_http(slot->curl, url, DAV_PROPFIND,
1164 &out_buffer, fwrite_buffer);
1165 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1166 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1168 if (start_active_slot(slot)) {
1169 run_active_slot(slot);
1170 if (results.curl_result == CURLE_OK) {
1171 XML_Parser parser = XML_ParserCreate(NULL);
1172 enum XML_Status result;
1173 ctx.name = xcalloc(10, 1);
1174 ctx.len = 0;
1175 ctx.cdata = NULL;
1176 ctx.userFunc = handle_remote_ls_ctx;
1177 ctx.userData = &ls;
1178 XML_SetUserData(parser, &ctx);
1179 XML_SetElementHandler(parser, xml_start_tag,
1180 xml_end_tag);
1181 XML_SetCharacterDataHandler(parser, xml_cdata);
1182 result = XML_Parse(parser, in_buffer.buf,
1183 in_buffer.len, 1);
1184 free(ctx.name);
1186 if (result != XML_STATUS_OK) {
1187 fprintf(stderr, "XML error: %s\n",
1188 XML_ErrorString(
1189 XML_GetErrorCode(parser)));
1191 XML_ParserFree(parser);
1193 } else {
1194 fprintf(stderr, "Unable to start PROPFIND request\n");
1197 free(ls.path);
1198 free(url);
1199 strbuf_release(&out_buffer.buf);
1200 strbuf_release(&in_buffer);
1201 curl_slist_free_all(dav_headers);
1204 static void get_remote_object_list(unsigned char parent)
1206 char path[] = "objects/XX/";
1207 static const char hex[] = "0123456789abcdef";
1208 unsigned int val = parent;
1210 path[8] = hex[val >> 4];
1211 path[9] = hex[val & 0xf];
1212 remote_dir_exists[val] = 0;
1213 remote_ls(path, (PROCESS_FILES | PROCESS_DIRS),
1214 process_ls_object, &val);
1217 static int locking_available(void)
1219 struct active_request_slot *slot;
1220 struct slot_results results;
1221 struct strbuf in_buffer = STRBUF_INIT;
1222 struct buffer out_buffer = { STRBUF_INIT, 0 };
1223 struct curl_slist *dav_headers = NULL;
1224 struct xml_ctx ctx;
1225 int lock_flags = 0;
1226 char *escaped;
1228 escaped = xml_entities(repo->url);
1229 strbuf_addf(&out_buffer.buf, PROPFIND_SUPPORTEDLOCK_REQUEST, escaped);
1230 free(escaped);
1232 dav_headers = curl_slist_append(dav_headers, "Depth: 0");
1233 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1235 slot = get_active_slot();
1236 slot->results = &results;
1237 curl_setup_http(slot->curl, repo->url, DAV_PROPFIND,
1238 &out_buffer, fwrite_buffer);
1239 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1240 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1242 if (start_active_slot(slot)) {
1243 run_active_slot(slot);
1244 if (results.curl_result == CURLE_OK) {
1245 XML_Parser parser = XML_ParserCreate(NULL);
1246 enum XML_Status result;
1247 ctx.name = xcalloc(10, 1);
1248 ctx.len = 0;
1249 ctx.cdata = NULL;
1250 ctx.userFunc = handle_lockprop_ctx;
1251 ctx.userData = &lock_flags;
1252 XML_SetUserData(parser, &ctx);
1253 XML_SetElementHandler(parser, xml_start_tag,
1254 xml_end_tag);
1255 result = XML_Parse(parser, in_buffer.buf,
1256 in_buffer.len, 1);
1257 free(ctx.name);
1259 if (result != XML_STATUS_OK) {
1260 fprintf(stderr, "XML error: %s\n",
1261 XML_ErrorString(
1262 XML_GetErrorCode(parser)));
1263 lock_flags = 0;
1265 XML_ParserFree(parser);
1266 if (!lock_flags)
1267 error("no DAV locking support on %s",
1268 repo->url);
1270 } else {
1271 error("Cannot access URL %s, return code %d",
1272 repo->url, results.curl_result);
1273 lock_flags = 0;
1275 } else {
1276 error("Unable to start PROPFIND request on %s", repo->url);
1279 strbuf_release(&out_buffer.buf);
1280 strbuf_release(&in_buffer);
1281 curl_slist_free_all(dav_headers);
1283 return lock_flags;
1286 static struct object_list **add_one_object(struct object *obj, struct object_list **p)
1288 struct object_list *entry = xmalloc(sizeof(struct object_list));
1289 entry->item = obj;
1290 entry->next = *p;
1291 *p = entry;
1292 return &entry->next;
1295 static struct object_list **process_blob(struct blob *blob,
1296 struct object_list **p,
1297 struct name_path *path,
1298 const char *name)
1300 struct object *obj = &blob->object;
1302 obj->flags |= LOCAL;
1304 if (obj->flags & (UNINTERESTING | SEEN))
1305 return p;
1307 obj->flags |= SEEN;
1308 return add_one_object(obj, p);
1311 static struct object_list **process_tree(struct tree *tree,
1312 struct object_list **p,
1313 struct name_path *path,
1314 const char *name)
1316 struct object *obj = &tree->object;
1317 struct tree_desc desc;
1318 struct name_entry entry;
1319 struct name_path me;
1321 obj->flags |= LOCAL;
1323 if (obj->flags & (UNINTERESTING | SEEN))
1324 return p;
1325 if (parse_tree(tree) < 0)
1326 die("bad tree object %s", sha1_to_hex(obj->sha1));
1328 obj->flags |= SEEN;
1329 name = xstrdup(name);
1330 p = add_one_object(obj, p);
1331 me.up = path;
1332 me.elem = name;
1333 me.elem_len = strlen(name);
1335 init_tree_desc(&desc, tree->buffer, tree->size);
1337 while (tree_entry(&desc, &entry))
1338 switch (object_type(entry.mode)) {
1339 case OBJ_TREE:
1340 p = process_tree(lookup_tree(entry.sha1), p, &me, name);
1341 break;
1342 case OBJ_BLOB:
1343 p = process_blob(lookup_blob(entry.sha1), p, &me, name);
1344 break;
1345 default:
1346 /* Subproject commit - not in this repository */
1347 break;
1350 free(tree->buffer);
1351 tree->buffer = NULL;
1352 return p;
1355 static int get_delta(struct rev_info *revs, struct remote_lock *lock)
1357 int i;
1358 struct commit *commit;
1359 struct object_list **p = &objects;
1360 int count = 0;
1362 while ((commit = get_revision(revs)) != NULL) {
1363 p = process_tree(commit->tree, p, NULL, "");
1364 commit->object.flags |= LOCAL;
1365 if (!(commit->object.flags & UNINTERESTING))
1366 count += add_send_request(&commit->object, lock);
1369 for (i = 0; i < revs->pending.nr; i++) {
1370 struct object_array_entry *entry = revs->pending.objects + i;
1371 struct object *obj = entry->item;
1372 const char *name = entry->name;
1374 if (obj->flags & (UNINTERESTING | SEEN))
1375 continue;
1376 if (obj->type == OBJ_TAG) {
1377 obj->flags |= SEEN;
1378 p = add_one_object(obj, p);
1379 continue;
1381 if (obj->type == OBJ_TREE) {
1382 p = process_tree((struct tree *)obj, p, NULL, name);
1383 continue;
1385 if (obj->type == OBJ_BLOB) {
1386 p = process_blob((struct blob *)obj, p, NULL, name);
1387 continue;
1389 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
1392 while (objects) {
1393 if (!(objects->item->flags & UNINTERESTING))
1394 count += add_send_request(objects->item, lock);
1395 objects = objects->next;
1398 return count;
1401 static int update_remote(unsigned char *sha1, struct remote_lock *lock)
1403 struct active_request_slot *slot;
1404 struct slot_results results;
1405 struct buffer out_buffer = { STRBUF_INIT, 0 };
1406 struct curl_slist *dav_headers;
1408 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1410 strbuf_addf(&out_buffer.buf, "%s\n", sha1_to_hex(sha1));
1412 slot = get_active_slot();
1413 slot->results = &results;
1414 curl_setup_http(slot->curl, lock->url, DAV_PUT,
1415 &out_buffer, fwrite_null);
1416 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1418 if (start_active_slot(slot)) {
1419 run_active_slot(slot);
1420 strbuf_release(&out_buffer.buf);
1421 if (results.curl_result != CURLE_OK) {
1422 fprintf(stderr,
1423 "PUT error: curl result=%d, HTTP code=%ld\n",
1424 results.curl_result, results.http_code);
1425 /* We should attempt recovery? */
1426 return 0;
1428 } else {
1429 strbuf_release(&out_buffer.buf);
1430 fprintf(stderr, "Unable to start PUT request\n");
1431 return 0;
1434 return 1;
1437 static struct ref *remote_refs;
1439 static void one_remote_ref(const char *refname)
1441 struct ref *ref;
1442 struct object *obj;
1444 ref = alloc_ref(refname);
1446 if (http_fetch_ref(repo->url, ref) != 0) {
1447 fprintf(stderr,
1448 "Unable to fetch ref %s from %s\n",
1449 refname, repo->url);
1450 free(ref);
1451 return;
1455 * Fetch a copy of the object if it doesn't exist locally - it
1456 * may be required for updating server info later.
1458 if (repo->can_update_info_refs && !has_sha1_file(ref->old_sha1)) {
1459 obj = lookup_unknown_object(ref->old_sha1);
1460 if (obj) {
1461 fprintf(stderr, " fetch %s for %s\n",
1462 sha1_to_hex(ref->old_sha1), refname);
1463 add_fetch_request(obj);
1467 ref->next = remote_refs;
1468 remote_refs = ref;
1471 static void get_dav_remote_heads(void)
1473 remote_ls("refs/", (PROCESS_FILES | PROCESS_DIRS | RECURSIVE), process_ls_ref, NULL);
1476 static void add_remote_info_ref(struct remote_ls_ctx *ls)
1478 struct strbuf *buf = (struct strbuf *)ls->userData;
1479 struct object *o;
1480 int len;
1481 char *ref_info;
1482 struct ref *ref;
1484 ref = alloc_ref(ls->dentry_name);
1486 if (http_fetch_ref(repo->url, ref) != 0) {
1487 fprintf(stderr,
1488 "Unable to fetch ref %s from %s\n",
1489 ls->dentry_name, repo->url);
1490 aborted = 1;
1491 free(ref);
1492 return;
1495 o = parse_object(ref->old_sha1);
1496 if (!o) {
1497 fprintf(stderr,
1498 "Unable to parse object %s for remote ref %s\n",
1499 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1500 aborted = 1;
1501 free(ref);
1502 return;
1505 len = strlen(ls->dentry_name) + 42;
1506 ref_info = xcalloc(len + 1, 1);
1507 sprintf(ref_info, "%s %s\n",
1508 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1509 fwrite_buffer(ref_info, 1, len, buf);
1510 free(ref_info);
1512 if (o->type == OBJ_TAG) {
1513 o = deref_tag(o, ls->dentry_name, 0);
1514 if (o) {
1515 len = strlen(ls->dentry_name) + 45;
1516 ref_info = xcalloc(len + 1, 1);
1517 sprintf(ref_info, "%s %s^{}\n",
1518 sha1_to_hex(o->sha1), ls->dentry_name);
1519 fwrite_buffer(ref_info, 1, len, buf);
1520 free(ref_info);
1523 free(ref);
1526 static void update_remote_info_refs(struct remote_lock *lock)
1528 struct buffer buffer = { STRBUF_INIT, 0 };
1529 struct active_request_slot *slot;
1530 struct slot_results results;
1531 struct curl_slist *dav_headers;
1533 remote_ls("refs/", (PROCESS_FILES | RECURSIVE),
1534 add_remote_info_ref, &buffer.buf);
1535 if (!aborted) {
1536 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1538 slot = get_active_slot();
1539 slot->results = &results;
1540 curl_setup_http(slot->curl, lock->url, DAV_PUT,
1541 &buffer, fwrite_null);
1542 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1544 if (start_active_slot(slot)) {
1545 run_active_slot(slot);
1546 if (results.curl_result != CURLE_OK) {
1547 fprintf(stderr,
1548 "PUT error: curl result=%d, HTTP code=%ld\n",
1549 results.curl_result, results.http_code);
1553 strbuf_release(&buffer.buf);
1556 static int remote_exists(const char *path)
1558 char *url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1559 int ret;
1561 sprintf(url, "%s%s", repo->url, path);
1563 switch (http_get_strbuf(url, NULL, 0)) {
1564 case HTTP_OK:
1565 ret = 1;
1566 break;
1567 case HTTP_MISSING_TARGET:
1568 ret = 0;
1569 break;
1570 case HTTP_ERROR:
1571 http_error(url, HTTP_ERROR);
1572 default:
1573 ret = -1;
1575 free(url);
1576 return ret;
1579 static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
1581 char *url;
1582 struct strbuf buffer = STRBUF_INIT;
1584 url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1585 sprintf(url, "%s%s", repo->url, path);
1587 if (http_get_strbuf(url, &buffer, 0) != HTTP_OK)
1588 die("Couldn't get %s for remote symref\n%s", url,
1589 curl_errorstr);
1590 free(url);
1592 free(*symref);
1593 *symref = NULL;
1594 hashclr(sha1);
1596 if (buffer.len == 0)
1597 return;
1599 /* If it's a symref, set the refname; otherwise try for a sha1 */
1600 if (!prefixcmp((char *)buffer.buf, "ref: ")) {
1601 *symref = xmemdupz((char *)buffer.buf + 5, buffer.len - 6);
1602 } else {
1603 get_sha1_hex(buffer.buf, sha1);
1606 strbuf_release(&buffer);
1609 static int verify_merge_base(unsigned char *head_sha1, struct ref *remote)
1611 struct commit *head = lookup_commit_or_die(head_sha1, "HEAD");
1612 struct commit *branch = lookup_commit_or_die(remote->old_sha1, remote->name);
1613 struct commit_list *merge_bases = get_merge_bases(head, branch, 1);
1615 return (merge_bases && !merge_bases->next && merge_bases->item == branch);
1618 static int delete_remote_branch(const char *pattern, int force)
1620 struct ref *refs = remote_refs;
1621 struct ref *remote_ref = NULL;
1622 unsigned char head_sha1[20];
1623 char *symref = NULL;
1624 int match;
1625 int patlen = strlen(pattern);
1626 int i;
1627 struct active_request_slot *slot;
1628 struct slot_results results;
1629 char *url;
1631 /* Find the remote branch(es) matching the specified branch name */
1632 for (match = 0; refs; refs = refs->next) {
1633 char *name = refs->name;
1634 int namelen = strlen(name);
1635 if (namelen < patlen ||
1636 memcmp(name + namelen - patlen, pattern, patlen))
1637 continue;
1638 if (namelen != patlen && name[namelen - patlen - 1] != '/')
1639 continue;
1640 match++;
1641 remote_ref = refs;
1643 if (match == 0)
1644 return error("No remote branch matches %s", pattern);
1645 if (match != 1)
1646 return error("More than one remote branch matches %s",
1647 pattern);
1650 * Remote HEAD must be a symref (not exactly foolproof; a remote
1651 * symlink to a symref will look like a symref)
1653 fetch_symref("HEAD", &symref, head_sha1);
1654 if (!symref)
1655 return error("Remote HEAD is not a symref");
1657 /* Remote branch must not be the remote HEAD */
1658 for (i = 0; symref && i < MAXDEPTH; i++) {
1659 if (!strcmp(remote_ref->name, symref))
1660 return error("Remote branch %s is the current HEAD",
1661 remote_ref->name);
1662 fetch_symref(symref, &symref, head_sha1);
1665 /* Run extra sanity checks if delete is not forced */
1666 if (!force) {
1667 /* Remote HEAD must resolve to a known object */
1668 if (symref)
1669 return error("Remote HEAD symrefs too deep");
1670 if (is_null_sha1(head_sha1))
1671 return error("Unable to resolve remote HEAD");
1672 if (!has_sha1_file(head_sha1))
1673 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1));
1675 /* Remote branch must resolve to a known object */
1676 if (is_null_sha1(remote_ref->old_sha1))
1677 return error("Unable to resolve remote branch %s",
1678 remote_ref->name);
1679 if (!has_sha1_file(remote_ref->old_sha1))
1680 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));
1682 /* Remote branch must be an ancestor of remote HEAD */
1683 if (!verify_merge_base(head_sha1, remote_ref)) {
1684 return error("The branch '%s' is not an ancestor "
1685 "of your current HEAD.\n"
1686 "If you are sure you want to delete it,"
1687 " run:\n\t'git http-push -D %s %s'",
1688 remote_ref->name, repo->url, pattern);
1692 /* Send delete request */
1693 fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name);
1694 if (dry_run)
1695 return 0;
1696 url = xmalloc(strlen(repo->url) + strlen(remote_ref->name) + 1);
1697 sprintf(url, "%s%s", repo->url, remote_ref->name);
1698 slot = get_active_slot();
1699 slot->results = &results;
1700 curl_setup_http_get(slot->curl, url, DAV_DELETE);
1701 if (start_active_slot(slot)) {
1702 run_active_slot(slot);
1703 free(url);
1704 if (results.curl_result != CURLE_OK)
1705 return error("DELETE request failed (%d/%ld)",
1706 results.curl_result, results.http_code);
1707 } else {
1708 free(url);
1709 return error("Unable to start DELETE request");
1712 return 0;
1715 static void run_request_queue(void)
1717 #ifdef USE_CURL_MULTI
1718 is_running_queue = 1;
1719 fill_active_slots();
1720 add_fill_function(NULL, fill_active_slot);
1721 #endif
1722 do {
1723 finish_all_active_slots();
1724 #ifdef USE_CURL_MULTI
1725 fill_active_slots();
1726 #endif
1727 } while (request_queue_head && !aborted);
1729 #ifdef USE_CURL_MULTI
1730 is_running_queue = 0;
1731 #endif
1734 int main(int argc, char **argv)
1736 struct transfer_request *request;
1737 struct transfer_request *next_request;
1738 int nr_refspec = 0;
1739 char **refspec = NULL;
1740 struct remote_lock *ref_lock = NULL;
1741 struct remote_lock *info_ref_lock = NULL;
1742 struct rev_info revs;
1743 int delete_branch = 0;
1744 int force_delete = 0;
1745 int objects_to_send;
1746 int rc = 0;
1747 int i;
1748 int new_refs;
1749 struct ref *ref, *local_refs;
1751 git_setup_gettext();
1753 git_extract_argv0_path(argv[0]);
1755 repo = xcalloc(sizeof(*repo), 1);
1757 argv++;
1758 for (i = 1; i < argc; i++, argv++) {
1759 char *arg = *argv;
1761 if (*arg == '-') {
1762 if (!strcmp(arg, "--all")) {
1763 push_all = MATCH_REFS_ALL;
1764 continue;
1766 if (!strcmp(arg, "--force")) {
1767 force_all = 1;
1768 continue;
1770 if (!strcmp(arg, "--dry-run")) {
1771 dry_run = 1;
1772 continue;
1774 if (!strcmp(arg, "--helper-status")) {
1775 helper_status = 1;
1776 continue;
1778 if (!strcmp(arg, "--verbose")) {
1779 push_verbosely = 1;
1780 http_is_verbose = 1;
1781 continue;
1783 if (!strcmp(arg, "-d")) {
1784 delete_branch = 1;
1785 continue;
1787 if (!strcmp(arg, "-D")) {
1788 delete_branch = 1;
1789 force_delete = 1;
1790 continue;
1792 if (!strcmp(arg, "-h"))
1793 usage(http_push_usage);
1795 if (!repo->url) {
1796 char *path = strstr(arg, "//");
1797 str_end_url_with_slash(arg, &repo->url);
1798 repo->path_len = strlen(repo->url);
1799 if (path) {
1800 repo->path = strchr(path+2, '/');
1801 if (repo->path)
1802 repo->path_len = strlen(repo->path);
1804 continue;
1806 refspec = argv;
1807 nr_refspec = argc - i;
1808 break;
1811 #ifndef USE_CURL_MULTI
1812 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
1813 #endif
1815 if (!repo->url)
1816 usage(http_push_usage);
1818 if (delete_branch && nr_refspec != 1)
1819 die("You must specify only one branch name when deleting a remote branch");
1821 setup_git_directory();
1823 memset(remote_dir_exists, -1, 256);
1825 http_init(NULL, repo->url, 1);
1827 #ifdef USE_CURL_MULTI
1828 is_running_queue = 0;
1829 #endif
1831 /* Verify DAV compliance/lock support */
1832 if (!locking_available()) {
1833 rc = 1;
1834 goto cleanup;
1837 sigchain_push_common(remove_locks_on_signal);
1839 /* Check whether the remote has server info files */
1840 repo->can_update_info_refs = 0;
1841 repo->has_info_refs = remote_exists("info/refs");
1842 repo->has_info_packs = remote_exists("objects/info/packs");
1843 if (repo->has_info_refs) {
1844 info_ref_lock = lock_remote("info/refs", LOCK_TIME);
1845 if (info_ref_lock)
1846 repo->can_update_info_refs = 1;
1847 else {
1848 error("cannot lock existing info/refs");
1849 rc = 1;
1850 goto cleanup;
1853 if (repo->has_info_packs)
1854 fetch_indices();
1856 /* Get a list of all local and remote heads to validate refspecs */
1857 local_refs = get_local_heads();
1858 fprintf(stderr, "Fetching remote heads...\n");
1859 get_dav_remote_heads();
1860 run_request_queue();
1862 /* Remove a remote branch if -d or -D was specified */
1863 if (delete_branch) {
1864 if (delete_remote_branch(refspec[0], force_delete) == -1) {
1865 fprintf(stderr, "Unable to delete remote branch %s\n",
1866 refspec[0]);
1867 if (helper_status)
1868 printf("error %s cannot remove\n", refspec[0]);
1870 goto cleanup;
1873 /* match them up */
1874 if (match_push_refs(local_refs, &remote_refs,
1875 nr_refspec, (const char **) refspec, push_all)) {
1876 rc = -1;
1877 goto cleanup;
1879 if (!remote_refs) {
1880 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
1881 if (helper_status)
1882 printf("error null no match\n");
1883 rc = 0;
1884 goto cleanup;
1887 new_refs = 0;
1888 for (ref = remote_refs; ref; ref = ref->next) {
1889 char old_hex[60], *new_hex;
1890 const char *commit_argv[5];
1891 int commit_argc;
1892 char *new_sha1_hex, *old_sha1_hex;
1894 if (!ref->peer_ref)
1895 continue;
1897 if (is_null_sha1(ref->peer_ref->new_sha1)) {
1898 if (delete_remote_branch(ref->name, 1) == -1) {
1899 error("Could not remove %s", ref->name);
1900 if (helper_status)
1901 printf("error %s cannot remove\n", ref->name);
1902 rc = -4;
1904 else if (helper_status)
1905 printf("ok %s\n", ref->name);
1906 new_refs++;
1907 continue;
1910 if (!hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
1911 if (push_verbosely)
1912 fprintf(stderr, "'%s': up-to-date\n", ref->name);
1913 if (helper_status)
1914 printf("ok %s up to date\n", ref->name);
1915 continue;
1918 if (!force_all &&
1919 !is_null_sha1(ref->old_sha1) &&
1920 !ref->force) {
1921 if (!has_sha1_file(ref->old_sha1) ||
1922 !ref_newer(ref->peer_ref->new_sha1,
1923 ref->old_sha1)) {
1925 * We do not have the remote ref, or
1926 * we know that the remote ref is not
1927 * an ancestor of what we are trying to
1928 * push. Either way this can be losing
1929 * commits at the remote end and likely
1930 * we were not up to date to begin with.
1932 error("remote '%s' is not an ancestor of\n"
1933 "local '%s'.\n"
1934 "Maybe you are not up-to-date and "
1935 "need to pull first?",
1936 ref->name,
1937 ref->peer_ref->name);
1938 if (helper_status)
1939 printf("error %s non-fast forward\n", ref->name);
1940 rc = -2;
1941 continue;
1944 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
1945 new_refs++;
1946 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
1947 new_hex = sha1_to_hex(ref->new_sha1);
1949 fprintf(stderr, "updating '%s'", ref->name);
1950 if (strcmp(ref->name, ref->peer_ref->name))
1951 fprintf(stderr, " using '%s'", ref->peer_ref->name);
1952 fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex);
1953 if (dry_run) {
1954 if (helper_status)
1955 printf("ok %s\n", ref->name);
1956 continue;
1959 /* Lock remote branch ref */
1960 ref_lock = lock_remote(ref->name, LOCK_TIME);
1961 if (ref_lock == NULL) {
1962 fprintf(stderr, "Unable to lock remote branch %s\n",
1963 ref->name);
1964 if (helper_status)
1965 printf("error %s lock error\n", ref->name);
1966 rc = 1;
1967 continue;
1970 /* Set up revision info for this refspec */
1971 commit_argc = 3;
1972 new_sha1_hex = xstrdup(sha1_to_hex(ref->new_sha1));
1973 old_sha1_hex = NULL;
1974 commit_argv[1] = "--objects";
1975 commit_argv[2] = new_sha1_hex;
1976 if (!push_all && !is_null_sha1(ref->old_sha1)) {
1977 old_sha1_hex = xmalloc(42);
1978 sprintf(old_sha1_hex, "^%s",
1979 sha1_to_hex(ref->old_sha1));
1980 commit_argv[3] = old_sha1_hex;
1981 commit_argc++;
1983 commit_argv[commit_argc] = NULL;
1984 init_revisions(&revs, setup_git_directory());
1985 setup_revisions(commit_argc, commit_argv, &revs, NULL);
1986 revs.edge_hint = 0; /* just in case */
1987 free(new_sha1_hex);
1988 if (old_sha1_hex) {
1989 free(old_sha1_hex);
1990 commit_argv[1] = NULL;
1993 /* Generate a list of objects that need to be pushed */
1994 pushing = 0;
1995 if (prepare_revision_walk(&revs))
1996 die("revision walk setup failed");
1997 mark_edges_uninteresting(revs.commits, &revs, NULL);
1998 objects_to_send = get_delta(&revs, ref_lock);
1999 finish_all_active_slots();
2001 /* Push missing objects to remote, this would be a
2002 convenient time to pack them first if appropriate. */
2003 pushing = 1;
2004 if (objects_to_send)
2005 fprintf(stderr, " sending %d objects\n",
2006 objects_to_send);
2008 run_request_queue();
2010 /* Update the remote branch if all went well */
2011 if (aborted || !update_remote(ref->new_sha1, ref_lock))
2012 rc = 1;
2014 if (!rc)
2015 fprintf(stderr, " done\n");
2016 if (helper_status)
2017 printf("%s %s\n", !rc ? "ok" : "error", ref->name);
2018 unlock_remote(ref_lock);
2019 check_locks();
2022 /* Update remote server info if appropriate */
2023 if (repo->has_info_refs && new_refs) {
2024 if (info_ref_lock && repo->can_update_info_refs) {
2025 fprintf(stderr, "Updating remote server info\n");
2026 if (!dry_run)
2027 update_remote_info_refs(info_ref_lock);
2028 } else {
2029 fprintf(stderr, "Unable to update server info\n");
2033 cleanup:
2034 if (info_ref_lock)
2035 unlock_remote(info_ref_lock);
2036 free(repo);
2038 http_cleanup();
2040 request = request_queue_head;
2041 while (request != NULL) {
2042 next_request = request->next;
2043 release_request(request);
2044 request = next_request;
2047 return rc;