http.c: new functions for the http API
[git/dscho.git] / http-push.c
blobf18da2a26334e6ea00b61cba572d25e4aa92e9c3
1 #include "cache.h"
2 #include "commit.h"
3 #include "pack.h"
4 #include "tag.h"
5 #include "blob.h"
6 #include "http.h"
7 #include "refs.h"
8 #include "diff.h"
9 #include "revision.h"
10 #include "exec_cmd.h"
11 #include "remote.h"
12 #include "list-objects.h"
13 #include "sigchain.h"
15 #include <expat.h>
17 static const char http_push_usage[] =
18 "git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
20 #ifndef XML_STATUS_OK
21 enum XML_Status {
22 XML_STATUS_OK = 1,
23 XML_STATUS_ERROR = 0
25 #define XML_STATUS_OK 1
26 #define XML_STATUS_ERROR 0
27 #endif
29 #define PREV_BUF_SIZE 4096
31 /* DAV methods */
32 #define DAV_LOCK "LOCK"
33 #define DAV_MKCOL "MKCOL"
34 #define DAV_MOVE "MOVE"
35 #define DAV_PROPFIND "PROPFIND"
36 #define DAV_PUT "PUT"
37 #define DAV_UNLOCK "UNLOCK"
38 #define DAV_DELETE "DELETE"
40 /* DAV lock flags */
41 #define DAV_PROP_LOCKWR (1u << 0)
42 #define DAV_PROP_LOCKEX (1u << 1)
43 #define DAV_LOCK_OK (1u << 2)
45 /* DAV XML properties */
46 #define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
47 #define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
48 #define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
49 #define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
50 #define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
51 #define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
52 #define DAV_PROPFIND_RESP ".multistatus.response"
53 #define DAV_PROPFIND_NAME ".multistatus.response.href"
54 #define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
56 /* DAV request body templates */
57 #define PROPFIND_SUPPORTEDLOCK_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:prop xmlns:R=\"%s\">\n<D:supportedlock/>\n</D:prop>\n</D:propfind>"
58 #define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
59 #define LOCK_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:lockinfo xmlns:D=\"DAV:\">\n<D:lockscope><D:exclusive/></D:lockscope>\n<D:locktype><D:write/></D:locktype>\n<D:owner>\n<D:href>mailto:%s</D:href>\n</D:owner>\n</D:lockinfo>"
61 #define LOCK_TIME 600
62 #define LOCK_REFRESH 30
64 /* bits #0-15 in revision.h */
66 #define LOCAL (1u<<16)
67 #define REMOTE (1u<<17)
68 #define FETCHING (1u<<18)
69 #define PUSHING (1u<<19)
71 /* We allow "recursive" symbolic refs. Only within reason, though */
72 #define MAXDEPTH 5
74 static int pushing;
75 static int aborted;
76 static signed char remote_dir_exists[256];
78 static int push_verbosely;
79 static int push_all = MATCH_REFS_NONE;
80 static int force_all;
81 static int dry_run;
83 static struct object_list *objects;
85 struct repo
87 char *url;
88 char *path;
89 int path_len;
90 int has_info_refs;
91 int can_update_info_refs;
92 int has_info_packs;
93 struct packed_git *packs;
94 struct remote_lock *locks;
97 static struct repo *repo;
99 enum transfer_state {
100 NEED_FETCH,
101 RUN_FETCH_LOOSE,
102 RUN_FETCH_PACKED,
103 NEED_PUSH,
104 RUN_MKCOL,
105 RUN_PUT,
106 RUN_MOVE,
107 ABORTED,
108 COMPLETE,
111 struct transfer_request
113 struct object *obj;
114 char *url;
115 char *dest;
116 struct remote_lock *lock;
117 struct curl_slist *headers;
118 struct buffer buffer;
119 char filename[PATH_MAX];
120 char tmpfile[PATH_MAX];
121 int local_fileno;
122 FILE *local_stream;
123 enum transfer_state state;
124 CURLcode curl_result;
125 char errorstr[CURL_ERROR_SIZE];
126 long http_code;
127 unsigned char real_sha1[20];
128 git_SHA_CTX c;
129 z_stream stream;
130 int zret;
131 int rename;
132 void *userData;
133 struct active_request_slot *slot;
134 struct transfer_request *next;
137 static struct transfer_request *request_queue_head;
139 struct xml_ctx
141 char *name;
142 int len;
143 char *cdata;
144 void (*userFunc)(struct xml_ctx *ctx, int tag_closed);
145 void *userData;
148 struct remote_lock
150 char *url;
151 char *owner;
152 char *token;
153 char tmpfile_suffix[41];
154 time_t start_time;
155 long timeout;
156 int refreshing;
157 struct remote_lock *next;
160 /* Flags that control remote_ls processing */
161 #define PROCESS_FILES (1u << 0)
162 #define PROCESS_DIRS (1u << 1)
163 #define RECURSIVE (1u << 2)
165 /* Flags that remote_ls passes to callback functions */
166 #define IS_DIR (1u << 0)
168 struct remote_ls_ctx
170 char *path;
171 void (*userFunc)(struct remote_ls_ctx *ls);
172 void *userData;
173 int flags;
174 char *dentry_name;
175 int dentry_flags;
176 struct remote_ls_ctx *parent;
179 /* get_dav_token_headers options */
180 enum dav_header_flag {
181 DAV_HEADER_IF = (1u << 0),
182 DAV_HEADER_LOCK = (1u << 1),
183 DAV_HEADER_TIMEOUT = (1u << 2)
186 static char *xml_entities(char *s)
188 struct strbuf buf = STRBUF_INIT;
189 while (*s) {
190 size_t len = strcspn(s, "\"<>&");
191 strbuf_add(&buf, s, len);
192 s += len;
193 switch (*s) {
194 case '"':
195 strbuf_addstr(&buf, "&quot;");
196 break;
197 case '<':
198 strbuf_addstr(&buf, "&lt;");
199 break;
200 case '>':
201 strbuf_addstr(&buf, "&gt;");
202 break;
203 case '&':
204 strbuf_addstr(&buf, "&amp;");
205 break;
207 s++;
209 return strbuf_detach(&buf, NULL);
212 static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum dav_header_flag options)
214 struct strbuf buf = STRBUF_INIT;
215 struct curl_slist *dav_headers = NULL;
217 if (options & DAV_HEADER_IF) {
218 strbuf_addf(&buf, "If: (<%s>)", lock->token);
219 dav_headers = curl_slist_append(dav_headers, buf.buf);
220 strbuf_reset(&buf);
222 if (options & DAV_HEADER_LOCK) {
223 strbuf_addf(&buf, "Lock-Token: <%s>", lock->token);
224 dav_headers = curl_slist_append(dav_headers, buf.buf);
225 strbuf_reset(&buf);
227 if (options & DAV_HEADER_TIMEOUT) {
228 strbuf_addf(&buf, "Timeout: Second-%ld", lock->timeout);
229 dav_headers = curl_slist_append(dav_headers, buf.buf);
230 strbuf_reset(&buf);
232 strbuf_release(&buf);
234 return dav_headers;
237 static void append_remote_object_url(struct strbuf *buf, const char *url,
238 const char *hex,
239 int only_two_digit_prefix)
241 strbuf_addf(buf, "%sobjects/%.*s/", url, 2, hex);
242 if (!only_two_digit_prefix)
243 strbuf_addf(buf, "%s", hex+2);
246 static void finish_request(struct transfer_request *request);
247 static void release_request(struct transfer_request *request);
249 static void process_response(void *callback_data)
251 struct transfer_request *request =
252 (struct transfer_request *)callback_data;
254 finish_request(request);
257 #ifdef USE_CURL_MULTI
259 static char *get_remote_object_url(const char *url, const char *hex,
260 int only_two_digit_prefix)
262 struct strbuf buf = STRBUF_INIT;
263 append_remote_object_url(&buf, url, hex, only_two_digit_prefix);
264 return strbuf_detach(&buf, NULL);
267 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
268 void *data)
270 unsigned char expn[4096];
271 size_t size = eltsize * nmemb;
272 int posn = 0;
273 struct transfer_request *request = (struct transfer_request *)data;
274 do {
275 ssize_t retval = xwrite(request->local_fileno,
276 (char *) ptr + posn, size - posn);
277 if (retval < 0)
278 return posn;
279 posn += retval;
280 } while (posn < size);
282 request->stream.avail_in = size;
283 request->stream.next_in = ptr;
284 do {
285 request->stream.next_out = expn;
286 request->stream.avail_out = sizeof(expn);
287 request->zret = git_inflate(&request->stream, Z_SYNC_FLUSH);
288 git_SHA1_Update(&request->c, expn,
289 sizeof(expn) - request->stream.avail_out);
290 } while (request->stream.avail_in && request->zret == Z_OK);
291 data_received++;
292 return size;
295 static void start_fetch_loose(struct transfer_request *request)
297 char *hex = sha1_to_hex(request->obj->sha1);
298 char *filename;
299 char prevfile[PATH_MAX];
300 char *url;
301 int prevlocal;
302 unsigned char prev_buf[PREV_BUF_SIZE];
303 ssize_t prev_read = 0;
304 long prev_posn = 0;
305 char range[RANGE_HEADER_SIZE];
306 struct curl_slist *range_header = NULL;
307 struct active_request_slot *slot;
309 filename = sha1_file_name(request->obj->sha1);
310 snprintf(request->filename, sizeof(request->filename), "%s", filename);
311 snprintf(request->tmpfile, sizeof(request->tmpfile),
312 "%s.temp", filename);
314 snprintf(prevfile, sizeof(prevfile), "%s.prev", request->filename);
315 unlink_or_warn(prevfile);
316 rename(request->tmpfile, prevfile);
317 unlink_or_warn(request->tmpfile);
319 if (request->local_fileno != -1)
320 error("fd leakage in start: %d", request->local_fileno);
321 request->local_fileno = open(request->tmpfile,
322 O_WRONLY | O_CREAT | O_EXCL, 0666);
324 * This could have failed due to the "lazy directory creation";
325 * try to mkdir the last path component.
327 if (request->local_fileno < 0 && errno == ENOENT) {
328 char *dir = strrchr(request->tmpfile, '/');
329 if (dir) {
330 *dir = 0;
331 mkdir(request->tmpfile, 0777);
332 *dir = '/';
334 request->local_fileno = open(request->tmpfile,
335 O_WRONLY | O_CREAT | O_EXCL, 0666);
338 if (request->local_fileno < 0) {
339 request->state = ABORTED;
340 error("Couldn't create temporary file %s for %s: %s",
341 request->tmpfile, request->filename, strerror(errno));
342 return;
345 memset(&request->stream, 0, sizeof(request->stream));
347 git_inflate_init(&request->stream);
349 git_SHA1_Init(&request->c);
351 url = get_remote_object_url(repo->url, hex, 0);
352 request->url = xstrdup(url);
355 * If a previous temp file is present, process what was already
356 * fetched.
358 prevlocal = open(prevfile, O_RDONLY);
359 if (prevlocal != -1) {
360 do {
361 prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE);
362 if (prev_read>0) {
363 if (fwrite_sha1_file(prev_buf,
365 prev_read,
366 request) == prev_read)
367 prev_posn += prev_read;
368 else
369 prev_read = -1;
371 } while (prev_read > 0);
372 close(prevlocal);
374 unlink_or_warn(prevfile);
377 * Reset inflate/SHA1 if there was an error reading the previous temp
378 * file; also rewind to the beginning of the local file.
380 if (prev_read == -1) {
381 memset(&request->stream, 0, sizeof(request->stream));
382 git_inflate_init(&request->stream);
383 git_SHA1_Init(&request->c);
384 if (prev_posn>0) {
385 prev_posn = 0;
386 lseek(request->local_fileno, 0, SEEK_SET);
387 ftruncate(request->local_fileno, 0);
391 slot = get_active_slot();
392 slot->callback_func = process_response;
393 slot->callback_data = request;
394 request->slot = slot;
396 curl_easy_setopt(slot->curl, CURLOPT_FILE, request);
397 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
398 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
399 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
400 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
403 * If we have successfully processed data from a previous fetch
404 * attempt, only fetch the data we don't already have.
406 if (prev_posn>0) {
407 if (push_verbosely)
408 fprintf(stderr,
409 "Resuming fetch of object %s at byte %ld\n",
410 hex, prev_posn);
411 sprintf(range, "Range: bytes=%ld-", prev_posn);
412 range_header = curl_slist_append(range_header, range);
413 curl_easy_setopt(slot->curl,
414 CURLOPT_HTTPHEADER, range_header);
417 /* Try to get the request started, abort the request on error */
418 request->state = RUN_FETCH_LOOSE;
419 if (!start_active_slot(slot)) {
420 fprintf(stderr, "Unable to start GET request\n");
421 repo->can_update_info_refs = 0;
422 release_request(request);
426 static void start_mkcol(struct transfer_request *request)
428 char *hex = sha1_to_hex(request->obj->sha1);
429 struct active_request_slot *slot;
431 request->url = get_remote_object_url(repo->url, hex, 1);
433 slot = get_active_slot();
434 slot->callback_func = process_response;
435 slot->callback_data = request;
436 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
437 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
438 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
439 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
440 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
442 if (start_active_slot(slot)) {
443 request->slot = slot;
444 request->state = RUN_MKCOL;
445 } else {
446 request->state = ABORTED;
447 free(request->url);
448 request->url = NULL;
451 #endif
453 static void start_fetch_packed(struct transfer_request *request)
455 char *url;
456 struct packed_git *target;
457 FILE *packfile;
458 char *filename;
459 long prev_posn = 0;
460 char range[RANGE_HEADER_SIZE];
461 struct curl_slist *range_header = NULL;
463 struct transfer_request *check_request = request_queue_head;
464 struct active_request_slot *slot;
466 target = find_sha1_pack(request->obj->sha1, repo->packs);
467 if (!target) {
468 fprintf(stderr, "Unable to fetch %s, will not be able to update server info refs\n", sha1_to_hex(request->obj->sha1));
469 repo->can_update_info_refs = 0;
470 release_request(request);
471 return;
474 fprintf(stderr, "Fetching pack %s\n", sha1_to_hex(target->sha1));
475 fprintf(stderr, " which contains %s\n", sha1_to_hex(request->obj->sha1));
477 filename = sha1_pack_name(target->sha1);
478 snprintf(request->filename, sizeof(request->filename), "%s", filename);
479 snprintf(request->tmpfile, sizeof(request->tmpfile),
480 "%s.temp", filename);
482 url = xmalloc(strlen(repo->url) + 64);
483 sprintf(url, "%sobjects/pack/pack-%s.pack",
484 repo->url, sha1_to_hex(target->sha1));
486 /* Make sure there isn't another open request for this pack */
487 while (check_request) {
488 if (check_request->state == RUN_FETCH_PACKED &&
489 !strcmp(check_request->url, url)) {
490 free(url);
491 release_request(request);
492 return;
494 check_request = check_request->next;
497 packfile = fopen(request->tmpfile, "a");
498 if (!packfile) {
499 fprintf(stderr, "Unable to open local file %s for pack",
500 request->tmpfile);
501 repo->can_update_info_refs = 0;
502 free(url);
503 return;
506 slot = get_active_slot();
507 slot->callback_func = process_response;
508 slot->callback_data = request;
509 request->slot = slot;
510 request->local_stream = packfile;
511 request->userData = target;
513 request->url = url;
514 curl_easy_setopt(slot->curl, CURLOPT_FILE, packfile);
515 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
516 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
517 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
518 slot->local = packfile;
521 * If there is data present from a previous transfer attempt,
522 * resume where it left off
524 prev_posn = ftell(packfile);
525 if (prev_posn>0) {
526 if (push_verbosely)
527 fprintf(stderr,
528 "Resuming fetch of pack %s at byte %ld\n",
529 sha1_to_hex(target->sha1), prev_posn);
530 sprintf(range, "Range: bytes=%ld-", prev_posn);
531 range_header = curl_slist_append(range_header, range);
532 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
535 /* Try to get the request started, abort the request on error */
536 request->state = RUN_FETCH_PACKED;
537 if (!start_active_slot(slot)) {
538 fprintf(stderr, "Unable to start GET request\n");
539 repo->can_update_info_refs = 0;
540 release_request(request);
544 static void start_put(struct transfer_request *request)
546 char *hex = sha1_to_hex(request->obj->sha1);
547 struct active_request_slot *slot;
548 struct strbuf buf = STRBUF_INIT;
549 enum object_type type;
550 char hdr[50];
551 void *unpacked;
552 unsigned long len;
553 int hdrlen;
554 ssize_t size;
555 z_stream stream;
557 unpacked = read_sha1_file(request->obj->sha1, &type, &len);
558 hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
560 /* Set it up */
561 memset(&stream, 0, sizeof(stream));
562 deflateInit(&stream, zlib_compression_level);
563 size = deflateBound(&stream, len + hdrlen);
564 strbuf_init(&request->buffer.buf, size);
565 request->buffer.posn = 0;
567 /* Compress it */
568 stream.next_out = (unsigned char *)request->buffer.buf.buf;
569 stream.avail_out = size;
571 /* First header.. */
572 stream.next_in = (void *)hdr;
573 stream.avail_in = hdrlen;
574 while (deflate(&stream, 0) == Z_OK)
575 /* nothing */;
577 /* Then the data itself.. */
578 stream.next_in = unpacked;
579 stream.avail_in = len;
580 while (deflate(&stream, Z_FINISH) == Z_OK)
581 /* nothing */;
582 deflateEnd(&stream);
583 free(unpacked);
585 request->buffer.buf.len = stream.total_out;
587 strbuf_addstr(&buf, "Destination: ");
588 append_remote_object_url(&buf, repo->url, hex, 0);
589 request->dest = strbuf_detach(&buf, NULL);
591 append_remote_object_url(&buf, repo->url, hex, 0);
592 strbuf_add(&buf, request->lock->tmpfile_suffix, 41);
593 request->url = strbuf_detach(&buf, NULL);
595 slot = get_active_slot();
596 slot->callback_func = process_response;
597 slot->callback_data = request;
598 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &request->buffer);
599 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, request->buffer.buf.len);
600 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
601 #ifndef NO_CURL_IOCTL
602 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
603 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &request->buffer);
604 #endif
605 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
606 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
607 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
608 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
609 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
610 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
612 if (start_active_slot(slot)) {
613 request->slot = slot;
614 request->state = RUN_PUT;
615 } else {
616 request->state = ABORTED;
617 free(request->url);
618 request->url = NULL;
622 static void start_move(struct transfer_request *request)
624 struct active_request_slot *slot;
625 struct curl_slist *dav_headers = NULL;
627 slot = get_active_slot();
628 slot->callback_func = process_response;
629 slot->callback_data = request;
630 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
631 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MOVE);
632 dav_headers = curl_slist_append(dav_headers, request->dest);
633 dav_headers = curl_slist_append(dav_headers, "Overwrite: T");
634 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
635 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
636 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
638 if (start_active_slot(slot)) {
639 request->slot = slot;
640 request->state = RUN_MOVE;
641 } else {
642 request->state = ABORTED;
643 free(request->url);
644 request->url = NULL;
648 static int refresh_lock(struct remote_lock *lock)
650 struct active_request_slot *slot;
651 struct slot_results results;
652 struct curl_slist *dav_headers;
653 int rc = 0;
655 lock->refreshing = 1;
657 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF | DAV_HEADER_TIMEOUT);
659 slot = get_active_slot();
660 slot->results = &results;
661 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
662 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
663 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
664 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
665 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
667 if (start_active_slot(slot)) {
668 run_active_slot(slot);
669 if (results.curl_result != CURLE_OK) {
670 fprintf(stderr, "LOCK HTTP error %ld\n",
671 results.http_code);
672 } else {
673 lock->start_time = time(NULL);
674 rc = 1;
678 lock->refreshing = 0;
679 curl_slist_free_all(dav_headers);
681 return rc;
684 static void check_locks(void)
686 struct remote_lock *lock = repo->locks;
687 time_t current_time = time(NULL);
688 int time_remaining;
690 while (lock) {
691 time_remaining = lock->start_time + lock->timeout -
692 current_time;
693 if (!lock->refreshing && time_remaining < LOCK_REFRESH) {
694 if (!refresh_lock(lock)) {
695 fprintf(stderr,
696 "Unable to refresh lock for %s\n",
697 lock->url);
698 aborted = 1;
699 return;
702 lock = lock->next;
706 static void release_request(struct transfer_request *request)
708 struct transfer_request *entry = request_queue_head;
710 if (request == request_queue_head) {
711 request_queue_head = request->next;
712 } else {
713 while (entry->next != NULL && entry->next != request)
714 entry = entry->next;
715 if (entry->next == request)
716 entry->next = entry->next->next;
719 if (request->local_fileno != -1)
720 close(request->local_fileno);
721 if (request->local_stream)
722 fclose(request->local_stream);
723 free(request->url);
724 free(request);
727 static void finish_request(struct transfer_request *request)
729 struct stat st;
730 struct packed_git *target;
731 struct packed_git **lst;
732 struct active_request_slot *slot;
734 request->curl_result = request->slot->curl_result;
735 request->http_code = request->slot->http_code;
736 slot = request->slot;
737 request->slot = NULL;
739 /* Keep locks active */
740 check_locks();
742 if (request->headers != NULL)
743 curl_slist_free_all(request->headers);
745 /* URL is reused for MOVE after PUT */
746 if (request->state != RUN_PUT) {
747 free(request->url);
748 request->url = NULL;
751 if (request->state == RUN_MKCOL) {
752 if (request->curl_result == CURLE_OK ||
753 request->http_code == 405) {
754 remote_dir_exists[request->obj->sha1[0]] = 1;
755 start_put(request);
756 } else {
757 fprintf(stderr, "MKCOL %s failed, aborting (%d/%ld)\n",
758 sha1_to_hex(request->obj->sha1),
759 request->curl_result, request->http_code);
760 request->state = ABORTED;
761 aborted = 1;
763 } else if (request->state == RUN_PUT) {
764 if (request->curl_result == CURLE_OK) {
765 start_move(request);
766 } else {
767 fprintf(stderr, "PUT %s failed, aborting (%d/%ld)\n",
768 sha1_to_hex(request->obj->sha1),
769 request->curl_result, request->http_code);
770 request->state = ABORTED;
771 aborted = 1;
773 } else if (request->state == RUN_MOVE) {
774 if (request->curl_result == CURLE_OK) {
775 if (push_verbosely)
776 fprintf(stderr, " sent %s\n",
777 sha1_to_hex(request->obj->sha1));
778 request->obj->flags |= REMOTE;
779 release_request(request);
780 } else {
781 fprintf(stderr, "MOVE %s failed, aborting (%d/%ld)\n",
782 sha1_to_hex(request->obj->sha1),
783 request->curl_result, request->http_code);
784 request->state = ABORTED;
785 aborted = 1;
787 } else if (request->state == RUN_FETCH_LOOSE) {
788 close(request->local_fileno);
789 request->local_fileno = -1;
791 if (request->curl_result != CURLE_OK &&
792 request->http_code != 416) {
793 if (stat(request->tmpfile, &st) == 0) {
794 if (st.st_size == 0)
795 unlink_or_warn(request->tmpfile);
797 } else {
798 if (request->http_code == 416)
799 warning("requested range invalid; we may already have all the data.");
801 git_inflate_end(&request->stream);
802 git_SHA1_Final(request->real_sha1, &request->c);
803 if (request->zret != Z_STREAM_END) {
804 unlink_or_warn(request->tmpfile);
805 } else if (hashcmp(request->obj->sha1, request->real_sha1)) {
806 unlink_or_warn(request->tmpfile);
807 } else {
808 request->rename =
809 move_temp_to_file(
810 request->tmpfile,
811 request->filename);
812 if (request->rename == 0)
813 request->obj->flags |= (LOCAL | REMOTE);
817 /* Try fetching packed if necessary */
818 if (request->obj->flags & LOCAL)
819 release_request(request);
820 else
821 start_fetch_packed(request);
823 } else if (request->state == RUN_FETCH_PACKED) {
824 if (request->curl_result != CURLE_OK) {
825 fprintf(stderr, "Unable to get pack file %s\n%s",
826 request->url, curl_errorstr);
827 repo->can_update_info_refs = 0;
828 } else {
829 off_t pack_size = ftell(request->local_stream);
831 fclose(request->local_stream);
832 request->local_stream = NULL;
833 slot->local = NULL;
834 if (!move_temp_to_file(request->tmpfile,
835 request->filename)) {
836 target = (struct packed_git *)request->userData;
837 target->pack_size = pack_size;
838 lst = &repo->packs;
839 while (*lst != target)
840 lst = &((*lst)->next);
841 *lst = (*lst)->next;
843 if (!verify_pack(target))
844 install_packed_git(target);
845 else
846 repo->can_update_info_refs = 0;
849 release_request(request);
853 #ifdef USE_CURL_MULTI
854 static int is_running_queue;
855 static int fill_active_slot(void *unused)
857 struct transfer_request *request;
859 if (aborted || !is_running_queue)
860 return 0;
862 for (request = request_queue_head; request; request = request->next) {
863 if (request->state == NEED_FETCH) {
864 start_fetch_loose(request);
865 return 1;
866 } else if (pushing && request->state == NEED_PUSH) {
867 if (remote_dir_exists[request->obj->sha1[0]] == 1) {
868 start_put(request);
869 } else {
870 start_mkcol(request);
872 return 1;
875 return 0;
877 #endif
879 static void get_remote_object_list(unsigned char parent);
881 static void add_fetch_request(struct object *obj)
883 struct transfer_request *request;
885 check_locks();
888 * Don't fetch the object if it's known to exist locally
889 * or is already in the request queue
891 if (remote_dir_exists[obj->sha1[0]] == -1)
892 get_remote_object_list(obj->sha1[0]);
893 if (obj->flags & (LOCAL | FETCHING))
894 return;
896 obj->flags |= FETCHING;
897 request = xmalloc(sizeof(*request));
898 request->obj = obj;
899 request->url = NULL;
900 request->lock = NULL;
901 request->headers = NULL;
902 request->local_fileno = -1;
903 request->local_stream = NULL;
904 request->state = NEED_FETCH;
905 request->next = request_queue_head;
906 request_queue_head = request;
908 #ifdef USE_CURL_MULTI
909 fill_active_slots();
910 step_active_slots();
911 #endif
914 static int add_send_request(struct object *obj, struct remote_lock *lock)
916 struct transfer_request *request = request_queue_head;
917 struct packed_git *target;
919 /* Keep locks active */
920 check_locks();
923 * Don't push the object if it's known to exist on the remote
924 * or is already in the request queue
926 if (remote_dir_exists[obj->sha1[0]] == -1)
927 get_remote_object_list(obj->sha1[0]);
928 if (obj->flags & (REMOTE | PUSHING))
929 return 0;
930 target = find_sha1_pack(obj->sha1, repo->packs);
931 if (target) {
932 obj->flags |= REMOTE;
933 return 0;
936 obj->flags |= PUSHING;
937 request = xmalloc(sizeof(*request));
938 request->obj = obj;
939 request->url = NULL;
940 request->lock = lock;
941 request->headers = NULL;
942 request->local_fileno = -1;
943 request->local_stream = NULL;
944 request->state = NEED_PUSH;
945 request->next = request_queue_head;
946 request_queue_head = request;
948 #ifdef USE_CURL_MULTI
949 fill_active_slots();
950 step_active_slots();
951 #endif
953 return 1;
956 static int fetch_index(unsigned char *sha1)
958 int ret = 0;
959 char *hex = xstrdup(sha1_to_hex(sha1));
960 char *filename;
961 char *url;
962 char tmpfile[PATH_MAX];
963 long prev_posn = 0;
964 char range[RANGE_HEADER_SIZE];
965 struct curl_slist *range_header = NULL;
967 FILE *indexfile;
968 struct active_request_slot *slot;
969 struct slot_results results;
971 /* Don't use the index if the pack isn't there */
972 url = xmalloc(strlen(repo->url) + 64);
973 sprintf(url, "%sobjects/pack/pack-%s.pack", repo->url, hex);
974 slot = get_active_slot();
975 slot->results = &results;
976 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
977 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
978 if (start_active_slot(slot)) {
979 run_active_slot(slot);
980 if (results.curl_result != CURLE_OK) {
981 ret = error("Unable to verify pack %s is available",
982 hex);
983 goto cleanup_pack;
985 } else {
986 ret = error("Unable to start request");
987 goto cleanup_pack;
990 if (has_pack_index(sha1)) {
991 ret = 0;
992 goto cleanup_pack;
995 if (push_verbosely)
996 fprintf(stderr, "Getting index for pack %s\n", hex);
998 sprintf(url, "%sobjects/pack/pack-%s.idx", repo->url, hex);
1000 filename = sha1_pack_index_name(sha1);
1001 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
1002 indexfile = fopen(tmpfile, "a");
1003 if (!indexfile) {
1004 ret = error("Unable to open local file %s for pack index",
1005 tmpfile);
1006 goto cleanup_pack;
1009 slot = get_active_slot();
1010 slot->results = &results;
1011 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
1012 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
1013 curl_easy_setopt(slot->curl, CURLOPT_FILE, indexfile);
1014 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
1015 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1016 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
1017 slot->local = indexfile;
1020 * If there is data present from a previous transfer attempt,
1021 * resume where it left off
1023 prev_posn = ftell(indexfile);
1024 if (prev_posn>0) {
1025 if (push_verbosely)
1026 fprintf(stderr,
1027 "Resuming fetch of index for pack %s at byte %ld\n",
1028 hex, prev_posn);
1029 sprintf(range, "Range: bytes=%ld-", prev_posn);
1030 range_header = curl_slist_append(range_header, range);
1031 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
1034 if (start_active_slot(slot)) {
1035 run_active_slot(slot);
1036 if (results.curl_result != CURLE_OK) {
1037 ret = error("Unable to get pack index %s\n%s", url,
1038 curl_errorstr);
1039 goto cleanup_index;
1041 } else {
1042 ret = error("Unable to start request");
1043 goto cleanup_index;
1046 ret = move_temp_to_file(tmpfile, filename);
1048 cleanup_index:
1049 fclose(indexfile);
1050 slot->local = NULL;
1051 cleanup_pack:
1052 free(url);
1053 free(hex);
1054 return ret;
1057 static int setup_index(unsigned char *sha1)
1059 struct packed_git *new_pack;
1061 if (fetch_index(sha1))
1062 return -1;
1064 new_pack = parse_pack_index(sha1);
1065 if (!new_pack)
1066 return -1; /* parse_pack_index() already issued error message */
1067 new_pack->next = repo->packs;
1068 repo->packs = new_pack;
1069 return 0;
1072 static int fetch_indices(void)
1074 unsigned char sha1[20];
1075 char *url;
1076 struct strbuf buffer = STRBUF_INIT;
1077 char *data;
1078 int i = 0;
1080 struct active_request_slot *slot;
1081 struct slot_results results;
1083 if (push_verbosely)
1084 fprintf(stderr, "Getting pack list\n");
1086 url = xmalloc(strlen(repo->url) + 20);
1087 sprintf(url, "%sobjects/info/packs", repo->url);
1089 slot = get_active_slot();
1090 slot->results = &results;
1091 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
1092 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1093 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1094 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
1095 if (start_active_slot(slot)) {
1096 run_active_slot(slot);
1097 if (results.curl_result != CURLE_OK) {
1098 strbuf_release(&buffer);
1099 free(url);
1100 if (results.http_code == 404)
1101 return 0;
1102 else
1103 return error("%s", curl_errorstr);
1105 } else {
1106 strbuf_release(&buffer);
1107 free(url);
1108 return error("Unable to start request");
1110 free(url);
1112 data = buffer.buf;
1113 while (i < buffer.len) {
1114 switch (data[i]) {
1115 case 'P':
1116 i++;
1117 if (i + 52 < buffer.len &&
1118 !prefixcmp(data + i, " pack-") &&
1119 !prefixcmp(data + i + 46, ".pack\n")) {
1120 get_sha1_hex(data + i + 6, sha1);
1121 setup_index(sha1);
1122 i += 51;
1123 break;
1125 default:
1126 while (data[i] != '\n')
1127 i++;
1129 i++;
1132 strbuf_release(&buffer);
1133 return 0;
1136 static void one_remote_object(const char *hex)
1138 unsigned char sha1[20];
1139 struct object *obj;
1141 if (get_sha1_hex(hex, sha1) != 0)
1142 return;
1144 obj = lookup_object(sha1);
1145 if (!obj)
1146 obj = parse_object(sha1);
1148 /* Ignore remote objects that don't exist locally */
1149 if (!obj)
1150 return;
1152 obj->flags |= REMOTE;
1153 if (!object_list_contains(objects, obj))
1154 object_list_insert(obj, &objects);
1157 static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed)
1159 int *lock_flags = (int *)ctx->userData;
1161 if (tag_closed) {
1162 if (!strcmp(ctx->name, DAV_CTX_LOCKENTRY)) {
1163 if ((*lock_flags & DAV_PROP_LOCKEX) &&
1164 (*lock_flags & DAV_PROP_LOCKWR)) {
1165 *lock_flags |= DAV_LOCK_OK;
1167 *lock_flags &= DAV_LOCK_OK;
1168 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_WRITE)) {
1169 *lock_flags |= DAV_PROP_LOCKWR;
1170 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_EXCLUSIVE)) {
1171 *lock_flags |= DAV_PROP_LOCKEX;
1176 static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
1178 struct remote_lock *lock = (struct remote_lock *)ctx->userData;
1179 git_SHA_CTX sha_ctx;
1180 unsigned char lock_token_sha1[20];
1182 if (tag_closed && ctx->cdata) {
1183 if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) {
1184 lock->owner = xmalloc(strlen(ctx->cdata) + 1);
1185 strcpy(lock->owner, ctx->cdata);
1186 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
1187 if (!prefixcmp(ctx->cdata, "Second-"))
1188 lock->timeout =
1189 strtol(ctx->cdata + 7, NULL, 10);
1190 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
1191 lock->token = xmalloc(strlen(ctx->cdata) + 1);
1192 strcpy(lock->token, ctx->cdata);
1194 git_SHA1_Init(&sha_ctx);
1195 git_SHA1_Update(&sha_ctx, lock->token, strlen(lock->token));
1196 git_SHA1_Final(lock_token_sha1, &sha_ctx);
1198 lock->tmpfile_suffix[0] = '_';
1199 memcpy(lock->tmpfile_suffix + 1, sha1_to_hex(lock_token_sha1), 40);
1204 static void one_remote_ref(char *refname);
1206 static void
1207 xml_start_tag(void *userData, const char *name, const char **atts)
1209 struct xml_ctx *ctx = (struct xml_ctx *)userData;
1210 const char *c = strchr(name, ':');
1211 int new_len;
1213 if (c == NULL)
1214 c = name;
1215 else
1216 c++;
1218 new_len = strlen(ctx->name) + strlen(c) + 2;
1220 if (new_len > ctx->len) {
1221 ctx->name = xrealloc(ctx->name, new_len);
1222 ctx->len = new_len;
1224 strcat(ctx->name, ".");
1225 strcat(ctx->name, c);
1227 free(ctx->cdata);
1228 ctx->cdata = NULL;
1230 ctx->userFunc(ctx, 0);
1233 static void
1234 xml_end_tag(void *userData, const char *name)
1236 struct xml_ctx *ctx = (struct xml_ctx *)userData;
1237 const char *c = strchr(name, ':');
1238 char *ep;
1240 ctx->userFunc(ctx, 1);
1242 if (c == NULL)
1243 c = name;
1244 else
1245 c++;
1247 ep = ctx->name + strlen(ctx->name) - strlen(c) - 1;
1248 *ep = 0;
1251 static void
1252 xml_cdata(void *userData, const XML_Char *s, int len)
1254 struct xml_ctx *ctx = (struct xml_ctx *)userData;
1255 free(ctx->cdata);
1256 ctx->cdata = xmemdupz(s, len);
1259 static struct remote_lock *lock_remote(const char *path, long timeout)
1261 struct active_request_slot *slot;
1262 struct slot_results results;
1263 struct buffer out_buffer = { STRBUF_INIT, 0 };
1264 struct strbuf in_buffer = STRBUF_INIT;
1265 char *url;
1266 char *ep;
1267 char timeout_header[25];
1268 struct remote_lock *lock = NULL;
1269 struct curl_slist *dav_headers = NULL;
1270 struct xml_ctx ctx;
1271 char *escaped;
1273 url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1274 sprintf(url, "%s%s", repo->url, path);
1276 /* Make sure leading directories exist for the remote ref */
1277 ep = strchr(url + strlen(repo->url) + 1, '/');
1278 while (ep) {
1279 char saved_character = ep[1];
1280 ep[1] = '\0';
1281 slot = get_active_slot();
1282 slot->results = &results;
1283 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
1284 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1285 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
1286 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1287 if (start_active_slot(slot)) {
1288 run_active_slot(slot);
1289 if (results.curl_result != CURLE_OK &&
1290 results.http_code != 405) {
1291 fprintf(stderr,
1292 "Unable to create branch path %s\n",
1293 url);
1294 free(url);
1295 return NULL;
1297 } else {
1298 fprintf(stderr, "Unable to start MKCOL request\n");
1299 free(url);
1300 return NULL;
1302 ep[1] = saved_character;
1303 ep = strchr(ep + 1, '/');
1306 escaped = xml_entities(git_default_email);
1307 strbuf_addf(&out_buffer.buf, LOCK_REQUEST, escaped);
1308 free(escaped);
1310 sprintf(timeout_header, "Timeout: Second-%ld", timeout);
1311 dav_headers = curl_slist_append(dav_headers, timeout_header);
1312 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1314 slot = get_active_slot();
1315 slot->results = &results;
1316 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1317 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1318 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1319 #ifndef NO_CURL_IOCTL
1320 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1321 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
1322 #endif
1323 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1324 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1325 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1326 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1327 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
1328 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1330 lock = xcalloc(1, sizeof(*lock));
1331 lock->timeout = -1;
1333 if (start_active_slot(slot)) {
1334 run_active_slot(slot);
1335 if (results.curl_result == CURLE_OK) {
1336 XML_Parser parser = XML_ParserCreate(NULL);
1337 enum XML_Status result;
1338 ctx.name = xcalloc(10, 1);
1339 ctx.len = 0;
1340 ctx.cdata = NULL;
1341 ctx.userFunc = handle_new_lock_ctx;
1342 ctx.userData = lock;
1343 XML_SetUserData(parser, &ctx);
1344 XML_SetElementHandler(parser, xml_start_tag,
1345 xml_end_tag);
1346 XML_SetCharacterDataHandler(parser, xml_cdata);
1347 result = XML_Parse(parser, in_buffer.buf,
1348 in_buffer.len, 1);
1349 free(ctx.name);
1350 if (result != XML_STATUS_OK) {
1351 fprintf(stderr, "XML error: %s\n",
1352 XML_ErrorString(
1353 XML_GetErrorCode(parser)));
1354 lock->timeout = -1;
1356 XML_ParserFree(parser);
1358 } else {
1359 fprintf(stderr, "Unable to start LOCK request\n");
1362 curl_slist_free_all(dav_headers);
1363 strbuf_release(&out_buffer.buf);
1364 strbuf_release(&in_buffer);
1366 if (lock->token == NULL || lock->timeout <= 0) {
1367 free(lock->token);
1368 free(lock->owner);
1369 free(url);
1370 free(lock);
1371 lock = NULL;
1372 } else {
1373 lock->url = url;
1374 lock->start_time = time(NULL);
1375 lock->next = repo->locks;
1376 repo->locks = lock;
1379 return lock;
1382 static int unlock_remote(struct remote_lock *lock)
1384 struct active_request_slot *slot;
1385 struct slot_results results;
1386 struct remote_lock *prev = repo->locks;
1387 struct curl_slist *dav_headers;
1388 int rc = 0;
1390 dav_headers = get_dav_token_headers(lock, DAV_HEADER_LOCK);
1392 slot = get_active_slot();
1393 slot->results = &results;
1394 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1395 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1396 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_UNLOCK);
1397 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1399 if (start_active_slot(slot)) {
1400 run_active_slot(slot);
1401 if (results.curl_result == CURLE_OK)
1402 rc = 1;
1403 else
1404 fprintf(stderr, "UNLOCK HTTP error %ld\n",
1405 results.http_code);
1406 } else {
1407 fprintf(stderr, "Unable to start UNLOCK request\n");
1410 curl_slist_free_all(dav_headers);
1412 if (repo->locks == lock) {
1413 repo->locks = lock->next;
1414 } else {
1415 while (prev && prev->next != lock)
1416 prev = prev->next;
1417 if (prev)
1418 prev->next = prev->next->next;
1421 free(lock->owner);
1422 free(lock->url);
1423 free(lock->token);
1424 free(lock);
1426 return rc;
1429 static void remove_locks(void)
1431 struct remote_lock *lock = repo->locks;
1433 fprintf(stderr, "Removing remote locks...\n");
1434 while (lock) {
1435 unlock_remote(lock);
1436 lock = lock->next;
1440 static void remove_locks_on_signal(int signo)
1442 remove_locks();
1443 sigchain_pop(signo);
1444 raise(signo);
1447 static void remote_ls(const char *path, int flags,
1448 void (*userFunc)(struct remote_ls_ctx *ls),
1449 void *userData);
1451 static void process_ls_object(struct remote_ls_ctx *ls)
1453 unsigned int *parent = (unsigned int *)ls->userData;
1454 char *path = ls->dentry_name;
1455 char *obj_hex;
1457 if (!strcmp(ls->path, ls->dentry_name) && (ls->flags & IS_DIR)) {
1458 remote_dir_exists[*parent] = 1;
1459 return;
1462 if (strlen(path) != 49)
1463 return;
1464 path += 8;
1465 obj_hex = xmalloc(strlen(path));
1466 /* NB: path is not null-terminated, can not use strlcpy here */
1467 memcpy(obj_hex, path, 2);
1468 strcpy(obj_hex + 2, path + 3);
1469 one_remote_object(obj_hex);
1470 free(obj_hex);
1473 static void process_ls_ref(struct remote_ls_ctx *ls)
1475 if (!strcmp(ls->path, ls->dentry_name) && (ls->dentry_flags & IS_DIR)) {
1476 fprintf(stderr, " %s\n", ls->dentry_name);
1477 return;
1480 if (!(ls->dentry_flags & IS_DIR))
1481 one_remote_ref(ls->dentry_name);
1484 static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
1486 struct remote_ls_ctx *ls = (struct remote_ls_ctx *)ctx->userData;
1488 if (tag_closed) {
1489 if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) {
1490 if (ls->dentry_flags & IS_DIR) {
1491 if (ls->flags & PROCESS_DIRS) {
1492 ls->userFunc(ls);
1494 if (strcmp(ls->dentry_name, ls->path) &&
1495 ls->flags & RECURSIVE) {
1496 remote_ls(ls->dentry_name,
1497 ls->flags,
1498 ls->userFunc,
1499 ls->userData);
1501 } else if (ls->flags & PROCESS_FILES) {
1502 ls->userFunc(ls);
1504 } else if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
1505 char *path = ctx->cdata;
1506 if (*ctx->cdata == 'h') {
1507 path = strstr(path, "//");
1508 if (path) {
1509 path = strchr(path+2, '/');
1512 if (path) {
1513 path += repo->path_len;
1514 ls->dentry_name = xstrdup(path);
1516 } else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
1517 ls->dentry_flags |= IS_DIR;
1519 } else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
1520 free(ls->dentry_name);
1521 ls->dentry_name = NULL;
1522 ls->dentry_flags = 0;
1527 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1528 * should _only_ heed the information from that file, instead of trying to
1529 * determine the refs from the remote file system (badly: it does not even
1530 * know about packed-refs).
1532 static void remote_ls(const char *path, int flags,
1533 void (*userFunc)(struct remote_ls_ctx *ls),
1534 void *userData)
1536 char *url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1537 struct active_request_slot *slot;
1538 struct slot_results results;
1539 struct strbuf in_buffer = STRBUF_INIT;
1540 struct buffer out_buffer = { STRBUF_INIT, 0 };
1541 struct curl_slist *dav_headers = NULL;
1542 struct xml_ctx ctx;
1543 struct remote_ls_ctx ls;
1545 ls.flags = flags;
1546 ls.path = xstrdup(path);
1547 ls.dentry_name = NULL;
1548 ls.dentry_flags = 0;
1549 ls.userData = userData;
1550 ls.userFunc = userFunc;
1552 sprintf(url, "%s%s", repo->url, path);
1554 strbuf_addf(&out_buffer.buf, PROPFIND_ALL_REQUEST);
1556 dav_headers = curl_slist_append(dav_headers, "Depth: 1");
1557 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1559 slot = get_active_slot();
1560 slot->results = &results;
1561 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1562 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1563 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1564 #ifndef NO_CURL_IOCTL
1565 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1566 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
1567 #endif
1568 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1569 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1570 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1571 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1572 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1573 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1575 if (start_active_slot(slot)) {
1576 run_active_slot(slot);
1577 if (results.curl_result == CURLE_OK) {
1578 XML_Parser parser = XML_ParserCreate(NULL);
1579 enum XML_Status result;
1580 ctx.name = xcalloc(10, 1);
1581 ctx.len = 0;
1582 ctx.cdata = NULL;
1583 ctx.userFunc = handle_remote_ls_ctx;
1584 ctx.userData = &ls;
1585 XML_SetUserData(parser, &ctx);
1586 XML_SetElementHandler(parser, xml_start_tag,
1587 xml_end_tag);
1588 XML_SetCharacterDataHandler(parser, xml_cdata);
1589 result = XML_Parse(parser, in_buffer.buf,
1590 in_buffer.len, 1);
1591 free(ctx.name);
1593 if (result != XML_STATUS_OK) {
1594 fprintf(stderr, "XML error: %s\n",
1595 XML_ErrorString(
1596 XML_GetErrorCode(parser)));
1598 XML_ParserFree(parser);
1600 } else {
1601 fprintf(stderr, "Unable to start PROPFIND request\n");
1604 free(ls.path);
1605 free(url);
1606 strbuf_release(&out_buffer.buf);
1607 strbuf_release(&in_buffer);
1608 curl_slist_free_all(dav_headers);
1611 static void get_remote_object_list(unsigned char parent)
1613 char path[] = "objects/XX/";
1614 static const char hex[] = "0123456789abcdef";
1615 unsigned int val = parent;
1617 path[8] = hex[val >> 4];
1618 path[9] = hex[val & 0xf];
1619 remote_dir_exists[val] = 0;
1620 remote_ls(path, (PROCESS_FILES | PROCESS_DIRS),
1621 process_ls_object, &val);
1624 static int locking_available(void)
1626 struct active_request_slot *slot;
1627 struct slot_results results;
1628 struct strbuf in_buffer = STRBUF_INIT;
1629 struct buffer out_buffer = { STRBUF_INIT, 0 };
1630 struct curl_slist *dav_headers = NULL;
1631 struct xml_ctx ctx;
1632 int lock_flags = 0;
1633 char *escaped;
1635 escaped = xml_entities(repo->url);
1636 strbuf_addf(&out_buffer.buf, PROPFIND_SUPPORTEDLOCK_REQUEST, escaped);
1637 free(escaped);
1639 dav_headers = curl_slist_append(dav_headers, "Depth: 0");
1640 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1642 slot = get_active_slot();
1643 slot->results = &results;
1644 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1645 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1646 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1647 #ifndef NO_CURL_IOCTL
1648 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1649 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
1650 #endif
1651 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1652 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1653 curl_easy_setopt(slot->curl, CURLOPT_URL, repo->url);
1654 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1655 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1656 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1658 if (start_active_slot(slot)) {
1659 run_active_slot(slot);
1660 if (results.curl_result == CURLE_OK) {
1661 XML_Parser parser = XML_ParserCreate(NULL);
1662 enum XML_Status result;
1663 ctx.name = xcalloc(10, 1);
1664 ctx.len = 0;
1665 ctx.cdata = NULL;
1666 ctx.userFunc = handle_lockprop_ctx;
1667 ctx.userData = &lock_flags;
1668 XML_SetUserData(parser, &ctx);
1669 XML_SetElementHandler(parser, xml_start_tag,
1670 xml_end_tag);
1671 result = XML_Parse(parser, in_buffer.buf,
1672 in_buffer.len, 1);
1673 free(ctx.name);
1675 if (result != XML_STATUS_OK) {
1676 fprintf(stderr, "XML error: %s\n",
1677 XML_ErrorString(
1678 XML_GetErrorCode(parser)));
1679 lock_flags = 0;
1681 XML_ParserFree(parser);
1682 if (!lock_flags)
1683 error("no DAV locking support on %s",
1684 repo->url);
1686 } else {
1687 error("Cannot access URL %s, return code %d",
1688 repo->url, results.curl_result);
1689 lock_flags = 0;
1691 } else {
1692 error("Unable to start PROPFIND request on %s", repo->url);
1695 strbuf_release(&out_buffer.buf);
1696 strbuf_release(&in_buffer);
1697 curl_slist_free_all(dav_headers);
1699 return lock_flags;
1702 static struct object_list **add_one_object(struct object *obj, struct object_list **p)
1704 struct object_list *entry = xmalloc(sizeof(struct object_list));
1705 entry->item = obj;
1706 entry->next = *p;
1707 *p = entry;
1708 return &entry->next;
1711 static struct object_list **process_blob(struct blob *blob,
1712 struct object_list **p,
1713 struct name_path *path,
1714 const char *name)
1716 struct object *obj = &blob->object;
1718 obj->flags |= LOCAL;
1720 if (obj->flags & (UNINTERESTING | SEEN))
1721 return p;
1723 obj->flags |= SEEN;
1724 return add_one_object(obj, p);
1727 static struct object_list **process_tree(struct tree *tree,
1728 struct object_list **p,
1729 struct name_path *path,
1730 const char *name)
1732 struct object *obj = &tree->object;
1733 struct tree_desc desc;
1734 struct name_entry entry;
1735 struct name_path me;
1737 obj->flags |= LOCAL;
1739 if (obj->flags & (UNINTERESTING | SEEN))
1740 return p;
1741 if (parse_tree(tree) < 0)
1742 die("bad tree object %s", sha1_to_hex(obj->sha1));
1744 obj->flags |= SEEN;
1745 name = xstrdup(name);
1746 p = add_one_object(obj, p);
1747 me.up = path;
1748 me.elem = name;
1749 me.elem_len = strlen(name);
1751 init_tree_desc(&desc, tree->buffer, tree->size);
1753 while (tree_entry(&desc, &entry))
1754 switch (object_type(entry.mode)) {
1755 case OBJ_TREE:
1756 p = process_tree(lookup_tree(entry.sha1), p, &me, name);
1757 break;
1758 case OBJ_BLOB:
1759 p = process_blob(lookup_blob(entry.sha1), p, &me, name);
1760 break;
1761 default:
1762 /* Subproject commit - not in this repository */
1763 break;
1766 free(tree->buffer);
1767 tree->buffer = NULL;
1768 return p;
1771 static int get_delta(struct rev_info *revs, struct remote_lock *lock)
1773 int i;
1774 struct commit *commit;
1775 struct object_list **p = &objects;
1776 int count = 0;
1778 while ((commit = get_revision(revs)) != NULL) {
1779 p = process_tree(commit->tree, p, NULL, "");
1780 commit->object.flags |= LOCAL;
1781 if (!(commit->object.flags & UNINTERESTING))
1782 count += add_send_request(&commit->object, lock);
1785 for (i = 0; i < revs->pending.nr; i++) {
1786 struct object_array_entry *entry = revs->pending.objects + i;
1787 struct object *obj = entry->item;
1788 const char *name = entry->name;
1790 if (obj->flags & (UNINTERESTING | SEEN))
1791 continue;
1792 if (obj->type == OBJ_TAG) {
1793 obj->flags |= SEEN;
1794 p = add_one_object(obj, p);
1795 continue;
1797 if (obj->type == OBJ_TREE) {
1798 p = process_tree((struct tree *)obj, p, NULL, name);
1799 continue;
1801 if (obj->type == OBJ_BLOB) {
1802 p = process_blob((struct blob *)obj, p, NULL, name);
1803 continue;
1805 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
1808 while (objects) {
1809 if (!(objects->item->flags & UNINTERESTING))
1810 count += add_send_request(objects->item, lock);
1811 objects = objects->next;
1814 return count;
1817 static int update_remote(unsigned char *sha1, struct remote_lock *lock)
1819 struct active_request_slot *slot;
1820 struct slot_results results;
1821 struct buffer out_buffer = { STRBUF_INIT, 0 };
1822 struct curl_slist *dav_headers;
1824 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1826 strbuf_addf(&out_buffer.buf, "%s\n", sha1_to_hex(sha1));
1828 slot = get_active_slot();
1829 slot->results = &results;
1830 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1831 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1832 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1833 #ifndef NO_CURL_IOCTL
1834 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1835 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer);
1836 #endif
1837 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1838 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1839 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1840 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1841 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1842 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1844 if (start_active_slot(slot)) {
1845 run_active_slot(slot);
1846 strbuf_release(&out_buffer.buf);
1847 if (results.curl_result != CURLE_OK) {
1848 fprintf(stderr,
1849 "PUT error: curl result=%d, HTTP code=%ld\n",
1850 results.curl_result, results.http_code);
1851 /* We should attempt recovery? */
1852 return 0;
1854 } else {
1855 strbuf_release(&out_buffer.buf);
1856 fprintf(stderr, "Unable to start PUT request\n");
1857 return 0;
1860 return 1;
1863 static struct ref *remote_refs, **remote_tail;
1865 static void one_remote_ref(char *refname)
1867 struct ref *ref;
1868 struct object *obj;
1870 ref = alloc_ref(refname);
1872 if (http_fetch_ref(repo->url, ref) != 0) {
1873 fprintf(stderr,
1874 "Unable to fetch ref %s from %s\n",
1875 refname, repo->url);
1876 free(ref);
1877 return;
1881 * Fetch a copy of the object if it doesn't exist locally - it
1882 * may be required for updating server info later.
1884 if (repo->can_update_info_refs && !has_sha1_file(ref->old_sha1)) {
1885 obj = lookup_unknown_object(ref->old_sha1);
1886 if (obj) {
1887 fprintf(stderr, " fetch %s for %s\n",
1888 sha1_to_hex(ref->old_sha1), refname);
1889 add_fetch_request(obj);
1893 *remote_tail = ref;
1894 remote_tail = &ref->next;
1897 static void get_dav_remote_heads(void)
1899 remote_tail = &remote_refs;
1900 remote_ls("refs/", (PROCESS_FILES | PROCESS_DIRS | RECURSIVE), process_ls_ref, NULL);
1903 static int is_zero_sha1(const unsigned char *sha1)
1905 int i;
1907 for (i = 0; i < 20; i++) {
1908 if (*sha1++)
1909 return 0;
1911 return 1;
1914 static void add_remote_info_ref(struct remote_ls_ctx *ls)
1916 struct strbuf *buf = (struct strbuf *)ls->userData;
1917 struct object *o;
1918 int len;
1919 char *ref_info;
1920 struct ref *ref;
1922 ref = alloc_ref(ls->dentry_name);
1924 if (http_fetch_ref(repo->url, ref) != 0) {
1925 fprintf(stderr,
1926 "Unable to fetch ref %s from %s\n",
1927 ls->dentry_name, repo->url);
1928 aborted = 1;
1929 free(ref);
1930 return;
1933 o = parse_object(ref->old_sha1);
1934 if (!o) {
1935 fprintf(stderr,
1936 "Unable to parse object %s for remote ref %s\n",
1937 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1938 aborted = 1;
1939 free(ref);
1940 return;
1943 len = strlen(ls->dentry_name) + 42;
1944 ref_info = xcalloc(len + 1, 1);
1945 sprintf(ref_info, "%s %s\n",
1946 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1947 fwrite_buffer(ref_info, 1, len, buf);
1948 free(ref_info);
1950 if (o->type == OBJ_TAG) {
1951 o = deref_tag(o, ls->dentry_name, 0);
1952 if (o) {
1953 len = strlen(ls->dentry_name) + 45;
1954 ref_info = xcalloc(len + 1, 1);
1955 sprintf(ref_info, "%s %s^{}\n",
1956 sha1_to_hex(o->sha1), ls->dentry_name);
1957 fwrite_buffer(ref_info, 1, len, buf);
1958 free(ref_info);
1961 free(ref);
1964 static void update_remote_info_refs(struct remote_lock *lock)
1966 struct buffer buffer = { STRBUF_INIT, 0 };
1967 struct active_request_slot *slot;
1968 struct slot_results results;
1969 struct curl_slist *dav_headers;
1971 remote_ls("refs/", (PROCESS_FILES | RECURSIVE),
1972 add_remote_info_ref, &buffer.buf);
1973 if (!aborted) {
1974 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1976 slot = get_active_slot();
1977 slot->results = &results;
1978 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &buffer);
1979 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, buffer.buf.len);
1980 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1981 #ifndef NO_CURL_IOCTL
1982 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
1983 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &buffer);
1984 #endif
1985 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1986 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1987 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1988 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1989 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1990 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1992 if (start_active_slot(slot)) {
1993 run_active_slot(slot);
1994 if (results.curl_result != CURLE_OK) {
1995 fprintf(stderr,
1996 "PUT error: curl result=%d, HTTP code=%ld\n",
1997 results.curl_result, results.http_code);
2001 strbuf_release(&buffer.buf);
2004 static int remote_exists(const char *path)
2006 char *url = xmalloc(strlen(repo->url) + strlen(path) + 1);
2007 struct active_request_slot *slot;
2008 struct slot_results results;
2009 int ret = -1;
2011 sprintf(url, "%s%s", repo->url, path);
2013 slot = get_active_slot();
2014 slot->results = &results;
2015 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2016 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
2018 if (start_active_slot(slot)) {
2019 run_active_slot(slot);
2020 if (results.http_code == 404)
2021 ret = 0;
2022 else if (results.curl_result == CURLE_OK)
2023 ret = 1;
2024 else
2025 fprintf(stderr, "HEAD HTTP error %ld\n", results.http_code);
2026 } else {
2027 fprintf(stderr, "Unable to start HEAD request\n");
2030 free(url);
2031 return ret;
2034 static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
2036 char *url;
2037 struct strbuf buffer = STRBUF_INIT;
2038 struct active_request_slot *slot;
2039 struct slot_results results;
2041 url = xmalloc(strlen(repo->url) + strlen(path) + 1);
2042 sprintf(url, "%s%s", repo->url, path);
2044 slot = get_active_slot();
2045 slot->results = &results;
2046 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
2047 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
2048 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
2049 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2050 if (start_active_slot(slot)) {
2051 run_active_slot(slot);
2052 if (results.curl_result != CURLE_OK) {
2053 die("Couldn't get %s for remote symref\n%s",
2054 url, curl_errorstr);
2056 } else {
2057 die("Unable to start remote symref request");
2059 free(url);
2061 free(*symref);
2062 *symref = NULL;
2063 hashclr(sha1);
2065 if (buffer.len == 0)
2066 return;
2068 /* If it's a symref, set the refname; otherwise try for a sha1 */
2069 if (!prefixcmp((char *)buffer.buf, "ref: ")) {
2070 *symref = xmemdupz((char *)buffer.buf + 5, buffer.len - 6);
2071 } else {
2072 get_sha1_hex(buffer.buf, sha1);
2075 strbuf_release(&buffer);
2078 static int verify_merge_base(unsigned char *head_sha1, unsigned char *branch_sha1)
2080 struct commit *head = lookup_commit(head_sha1);
2081 struct commit *branch = lookup_commit(branch_sha1);
2082 struct commit_list *merge_bases = get_merge_bases(head, branch, 1);
2084 return (merge_bases && !merge_bases->next && merge_bases->item == branch);
2087 static int delete_remote_branch(char *pattern, int force)
2089 struct ref *refs = remote_refs;
2090 struct ref *remote_ref = NULL;
2091 unsigned char head_sha1[20];
2092 char *symref = NULL;
2093 int match;
2094 int patlen = strlen(pattern);
2095 int i;
2096 struct active_request_slot *slot;
2097 struct slot_results results;
2098 char *url;
2100 /* Find the remote branch(es) matching the specified branch name */
2101 for (match = 0; refs; refs = refs->next) {
2102 char *name = refs->name;
2103 int namelen = strlen(name);
2104 if (namelen < patlen ||
2105 memcmp(name + namelen - patlen, pattern, patlen))
2106 continue;
2107 if (namelen != patlen && name[namelen - patlen - 1] != '/')
2108 continue;
2109 match++;
2110 remote_ref = refs;
2112 if (match == 0)
2113 return error("No remote branch matches %s", pattern);
2114 if (match != 1)
2115 return error("More than one remote branch matches %s",
2116 pattern);
2119 * Remote HEAD must be a symref (not exactly foolproof; a remote
2120 * symlink to a symref will look like a symref)
2122 fetch_symref("HEAD", &symref, head_sha1);
2123 if (!symref)
2124 return error("Remote HEAD is not a symref");
2126 /* Remote branch must not be the remote HEAD */
2127 for (i=0; symref && i<MAXDEPTH; i++) {
2128 if (!strcmp(remote_ref->name, symref))
2129 return error("Remote branch %s is the current HEAD",
2130 remote_ref->name);
2131 fetch_symref(symref, &symref, head_sha1);
2134 /* Run extra sanity checks if delete is not forced */
2135 if (!force) {
2136 /* Remote HEAD must resolve to a known object */
2137 if (symref)
2138 return error("Remote HEAD symrefs too deep");
2139 if (is_zero_sha1(head_sha1))
2140 return error("Unable to resolve remote HEAD");
2141 if (!has_sha1_file(head_sha1))
2142 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1));
2144 /* Remote branch must resolve to a known object */
2145 if (is_zero_sha1(remote_ref->old_sha1))
2146 return error("Unable to resolve remote branch %s",
2147 remote_ref->name);
2148 if (!has_sha1_file(remote_ref->old_sha1))
2149 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));
2151 /* Remote branch must be an ancestor of remote HEAD */
2152 if (!verify_merge_base(head_sha1, remote_ref->old_sha1)) {
2153 return error("The branch '%s' is not an ancestor "
2154 "of your current HEAD.\n"
2155 "If you are sure you want to delete it,"
2156 " run:\n\t'git http-push -D %s %s'",
2157 remote_ref->name, repo->url, pattern);
2161 /* Send delete request */
2162 fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name);
2163 if (dry_run)
2164 return 0;
2165 url = xmalloc(strlen(repo->url) + strlen(remote_ref->name) + 1);
2166 sprintf(url, "%s%s", repo->url, remote_ref->name);
2167 slot = get_active_slot();
2168 slot->results = &results;
2169 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
2170 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
2171 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2172 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_DELETE);
2173 if (start_active_slot(slot)) {
2174 run_active_slot(slot);
2175 free(url);
2176 if (results.curl_result != CURLE_OK)
2177 return error("DELETE request failed (%d/%ld)\n",
2178 results.curl_result, results.http_code);
2179 } else {
2180 free(url);
2181 return error("Unable to start DELETE request");
2184 return 0;
2187 void run_request_queue(void)
2189 #ifdef USE_CURL_MULTI
2190 is_running_queue = 1;
2191 fill_active_slots();
2192 add_fill_function(NULL, fill_active_slot);
2193 #endif
2194 do {
2195 finish_all_active_slots();
2196 #ifdef USE_CURL_MULTI
2197 fill_active_slots();
2198 #endif
2199 } while (request_queue_head && !aborted);
2201 #ifdef USE_CURL_MULTI
2202 is_running_queue = 0;
2203 #endif
2206 int main(int argc, char **argv)
2208 struct transfer_request *request;
2209 struct transfer_request *next_request;
2210 int nr_refspec = 0;
2211 char **refspec = NULL;
2212 struct remote_lock *ref_lock = NULL;
2213 struct remote_lock *info_ref_lock = NULL;
2214 struct rev_info revs;
2215 int delete_branch = 0;
2216 int force_delete = 0;
2217 int objects_to_send;
2218 int rc = 0;
2219 int i;
2220 int new_refs;
2221 struct ref *ref, *local_refs;
2222 struct remote *remote;
2223 char *rewritten_url = NULL;
2225 git_extract_argv0_path(argv[0]);
2227 setup_git_directory();
2229 repo = xcalloc(sizeof(*repo), 1);
2231 argv++;
2232 for (i = 1; i < argc; i++, argv++) {
2233 char *arg = *argv;
2235 if (*arg == '-') {
2236 if (!strcmp(arg, "--all")) {
2237 push_all = MATCH_REFS_ALL;
2238 continue;
2240 if (!strcmp(arg, "--force")) {
2241 force_all = 1;
2242 continue;
2244 if (!strcmp(arg, "--dry-run")) {
2245 dry_run = 1;
2246 continue;
2248 if (!strcmp(arg, "--verbose")) {
2249 push_verbosely = 1;
2250 http_is_verbose = 1;
2251 continue;
2253 if (!strcmp(arg, "-d")) {
2254 delete_branch = 1;
2255 continue;
2257 if (!strcmp(arg, "-D")) {
2258 delete_branch = 1;
2259 force_delete = 1;
2260 continue;
2263 if (!repo->url) {
2264 char *path = strstr(arg, "//");
2265 repo->url = arg;
2266 repo->path_len = strlen(arg);
2267 if (path) {
2268 repo->path = strchr(path+2, '/');
2269 if (repo->path)
2270 repo->path_len = strlen(repo->path);
2272 continue;
2274 refspec = argv;
2275 nr_refspec = argc - i;
2276 break;
2279 #ifndef USE_CURL_MULTI
2280 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
2281 #endif
2283 if (!repo->url)
2284 usage(http_push_usage);
2286 if (delete_branch && nr_refspec != 1)
2287 die("You must specify only one branch name when deleting a remote branch");
2289 memset(remote_dir_exists, -1, 256);
2292 * Create a minimum remote by hand to give to http_init(),
2293 * primarily to allow it to look at the URL.
2295 remote = xcalloc(sizeof(*remote), 1);
2296 ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
2297 remote->url[remote->url_nr++] = repo->url;
2298 http_init(remote);
2300 if (repo->url && repo->url[strlen(repo->url)-1] != '/') {
2301 rewritten_url = xmalloc(strlen(repo->url)+2);
2302 strcpy(rewritten_url, repo->url);
2303 strcat(rewritten_url, "/");
2304 repo->path = rewritten_url + (repo->path - repo->url);
2305 repo->path_len++;
2306 repo->url = rewritten_url;
2309 #ifdef USE_CURL_MULTI
2310 is_running_queue = 0;
2311 #endif
2313 /* Verify DAV compliance/lock support */
2314 if (!locking_available()) {
2315 rc = 1;
2316 goto cleanup;
2319 sigchain_push_common(remove_locks_on_signal);
2321 /* Check whether the remote has server info files */
2322 repo->can_update_info_refs = 0;
2323 repo->has_info_refs = remote_exists("info/refs");
2324 repo->has_info_packs = remote_exists("objects/info/packs");
2325 if (repo->has_info_refs) {
2326 info_ref_lock = lock_remote("info/refs", LOCK_TIME);
2327 if (info_ref_lock)
2328 repo->can_update_info_refs = 1;
2329 else {
2330 error("cannot lock existing info/refs");
2331 rc = 1;
2332 goto cleanup;
2335 if (repo->has_info_packs)
2336 fetch_indices();
2338 /* Get a list of all local and remote heads to validate refspecs */
2339 local_refs = get_local_heads();
2340 fprintf(stderr, "Fetching remote heads...\n");
2341 get_dav_remote_heads();
2342 run_request_queue();
2344 /* Remove a remote branch if -d or -D was specified */
2345 if (delete_branch) {
2346 if (delete_remote_branch(refspec[0], force_delete) == -1)
2347 fprintf(stderr, "Unable to delete remote branch %s\n",
2348 refspec[0]);
2349 goto cleanup;
2352 /* match them up */
2353 if (!remote_tail)
2354 remote_tail = &remote_refs;
2355 if (match_refs(local_refs, remote_refs, &remote_tail,
2356 nr_refspec, (const char **) refspec, push_all)) {
2357 rc = -1;
2358 goto cleanup;
2360 if (!remote_refs) {
2361 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
2362 rc = 0;
2363 goto cleanup;
2366 new_refs = 0;
2367 for (ref = remote_refs; ref; ref = ref->next) {
2368 char old_hex[60], *new_hex;
2369 const char *commit_argv[4];
2370 int commit_argc;
2371 char *new_sha1_hex, *old_sha1_hex;
2373 if (!ref->peer_ref)
2374 continue;
2376 if (is_zero_sha1(ref->peer_ref->new_sha1)) {
2377 if (delete_remote_branch(ref->name, 1) == -1) {
2378 error("Could not remove %s", ref->name);
2379 rc = -4;
2381 new_refs++;
2382 continue;
2385 if (!hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
2386 if (push_verbosely || 1)
2387 fprintf(stderr, "'%s': up-to-date\n", ref->name);
2388 continue;
2391 if (!force_all &&
2392 !is_zero_sha1(ref->old_sha1) &&
2393 !ref->force) {
2394 if (!has_sha1_file(ref->old_sha1) ||
2395 !ref_newer(ref->peer_ref->new_sha1,
2396 ref->old_sha1)) {
2398 * We do not have the remote ref, or
2399 * we know that the remote ref is not
2400 * an ancestor of what we are trying to
2401 * push. Either way this can be losing
2402 * commits at the remote end and likely
2403 * we were not up to date to begin with.
2405 error("remote '%s' is not an ancestor of\n"
2406 "local '%s'.\n"
2407 "Maybe you are not up-to-date and "
2408 "need to pull first?",
2409 ref->name,
2410 ref->peer_ref->name);
2411 rc = -2;
2412 continue;
2415 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
2416 new_refs++;
2417 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
2418 new_hex = sha1_to_hex(ref->new_sha1);
2420 fprintf(stderr, "updating '%s'", ref->name);
2421 if (strcmp(ref->name, ref->peer_ref->name))
2422 fprintf(stderr, " using '%s'", ref->peer_ref->name);
2423 fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex);
2424 if (dry_run)
2425 continue;
2427 /* Lock remote branch ref */
2428 ref_lock = lock_remote(ref->name, LOCK_TIME);
2429 if (ref_lock == NULL) {
2430 fprintf(stderr, "Unable to lock remote branch %s\n",
2431 ref->name);
2432 rc = 1;
2433 continue;
2436 /* Set up revision info for this refspec */
2437 commit_argc = 3;
2438 new_sha1_hex = xstrdup(sha1_to_hex(ref->new_sha1));
2439 old_sha1_hex = NULL;
2440 commit_argv[1] = "--objects";
2441 commit_argv[2] = new_sha1_hex;
2442 if (!push_all && !is_zero_sha1(ref->old_sha1)) {
2443 old_sha1_hex = xmalloc(42);
2444 sprintf(old_sha1_hex, "^%s",
2445 sha1_to_hex(ref->old_sha1));
2446 commit_argv[3] = old_sha1_hex;
2447 commit_argc++;
2449 init_revisions(&revs, setup_git_directory());
2450 setup_revisions(commit_argc, commit_argv, &revs, NULL);
2451 revs.edge_hint = 0; /* just in case */
2452 free(new_sha1_hex);
2453 if (old_sha1_hex) {
2454 free(old_sha1_hex);
2455 commit_argv[1] = NULL;
2458 /* Generate a list of objects that need to be pushed */
2459 pushing = 0;
2460 if (prepare_revision_walk(&revs))
2461 die("revision walk setup failed");
2462 mark_edges_uninteresting(revs.commits, &revs, NULL);
2463 objects_to_send = get_delta(&revs, ref_lock);
2464 finish_all_active_slots();
2466 /* Push missing objects to remote, this would be a
2467 convenient time to pack them first if appropriate. */
2468 pushing = 1;
2469 if (objects_to_send)
2470 fprintf(stderr, " sending %d objects\n",
2471 objects_to_send);
2473 run_request_queue();
2475 /* Update the remote branch if all went well */
2476 if (aborted || !update_remote(ref->new_sha1, ref_lock))
2477 rc = 1;
2479 if (!rc)
2480 fprintf(stderr, " done\n");
2481 unlock_remote(ref_lock);
2482 check_locks();
2485 /* Update remote server info if appropriate */
2486 if (repo->has_info_refs && new_refs) {
2487 if (info_ref_lock && repo->can_update_info_refs) {
2488 fprintf(stderr, "Updating remote server info\n");
2489 if (!dry_run)
2490 update_remote_info_refs(info_ref_lock);
2491 } else {
2492 fprintf(stderr, "Unable to update server info\n");
2496 cleanup:
2497 free(rewritten_url);
2498 if (info_ref_lock)
2499 unlock_remote(info_ref_lock);
2500 free(repo);
2502 http_cleanup();
2504 request = request_queue_head;
2505 while (request != NULL) {
2506 next_request = request->next;
2507 release_request(request);
2508 request = next_request;
2511 return rc;