use a hash of the lock token as the suffix for PUT/MOVE
[alt-git.git] / http-push.c
blob55ad808023f00acb76f2bfd198b0ec204c7d3592
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"
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
29 #define RANGE_HEADER_SIZE 30
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 struct curl_slist *no_pragma_header;
80 static int push_verbosely;
81 static int push_all = MATCH_REFS_NONE;
82 static int force_all;
83 static int dry_run;
85 static struct object_list *objects;
87 struct repo
89 char *url;
90 char *path;
91 int path_len;
92 int has_info_refs;
93 int can_update_info_refs;
94 int has_info_packs;
95 struct packed_git *packs;
96 struct remote_lock *locks;
99 static struct repo *remote;
101 enum transfer_state {
102 NEED_FETCH,
103 RUN_FETCH_LOOSE,
104 RUN_FETCH_PACKED,
105 NEED_PUSH,
106 RUN_MKCOL,
107 RUN_PUT,
108 RUN_MOVE,
109 ABORTED,
110 COMPLETE,
113 struct transfer_request
115 struct object *obj;
116 char *url;
117 char *dest;
118 struct remote_lock *lock;
119 struct curl_slist *headers;
120 struct buffer buffer;
121 char filename[PATH_MAX];
122 char tmpfile[PATH_MAX];
123 int local_fileno;
124 FILE *local_stream;
125 enum transfer_state state;
126 CURLcode curl_result;
127 char errorstr[CURL_ERROR_SIZE];
128 long http_code;
129 unsigned char real_sha1[20];
130 git_SHA_CTX c;
131 z_stream stream;
132 int zret;
133 int rename;
134 void *userData;
135 struct active_request_slot *slot;
136 struct transfer_request *next;
139 static struct transfer_request *request_queue_head;
141 struct xml_ctx
143 char *name;
144 int len;
145 char *cdata;
146 void (*userFunc)(struct xml_ctx *ctx, int tag_closed);
147 void *userData;
150 struct remote_lock
152 char *url;
153 char *owner;
154 char *token;
155 char tmpfile_suffix[41];
156 time_t start_time;
157 long timeout;
158 int refreshing;
159 struct remote_lock *next;
162 /* Flags that control remote_ls processing */
163 #define PROCESS_FILES (1u << 0)
164 #define PROCESS_DIRS (1u << 1)
165 #define RECURSIVE (1u << 2)
167 /* Flags that remote_ls passes to callback functions */
168 #define IS_DIR (1u << 0)
170 struct remote_ls_ctx
172 char *path;
173 void (*userFunc)(struct remote_ls_ctx *ls);
174 void *userData;
175 int flags;
176 char *dentry_name;
177 int dentry_flags;
178 struct remote_ls_ctx *parent;
181 /* get_dav_token_headers options */
182 enum dav_header_flag {
183 DAV_HEADER_IF = (1u << 0),
184 DAV_HEADER_LOCK = (1u << 1),
185 DAV_HEADER_TIMEOUT = (1u << 2)
188 static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum dav_header_flag options)
190 struct strbuf buf = STRBUF_INIT;
191 struct curl_slist *dav_headers = NULL;
193 if (options & DAV_HEADER_IF) {
194 strbuf_addf(&buf, "If: (<%s>)", lock->token);
195 dav_headers = curl_slist_append(dav_headers, buf.buf);
196 strbuf_reset(&buf);
198 if (options & DAV_HEADER_LOCK) {
199 strbuf_addf(&buf, "Lock-Token: <%s>", lock->token);
200 dav_headers = curl_slist_append(dav_headers, buf.buf);
201 strbuf_reset(&buf);
203 if (options & DAV_HEADER_TIMEOUT) {
204 strbuf_addf(&buf, "Timeout: Second-%ld", lock->timeout);
205 dav_headers = curl_slist_append(dav_headers, buf.buf);
206 strbuf_reset(&buf);
208 strbuf_release(&buf);
210 return dav_headers;
213 static void append_remote_object_url(struct strbuf *buf, const char *url,
214 const char *hex,
215 int only_two_digit_prefix)
217 strbuf_addf(buf, "%sobjects/%.*s/", url, 2, hex);
218 if (!only_two_digit_prefix)
219 strbuf_addf(buf, "%s", hex+2);
222 static void finish_request(struct transfer_request *request);
223 static void release_request(struct transfer_request *request);
225 static void process_response(void *callback_data)
227 struct transfer_request *request =
228 (struct transfer_request *)callback_data;
230 finish_request(request);
233 #ifdef USE_CURL_MULTI
235 static char *get_remote_object_url(const char *url, const char *hex,
236 int only_two_digit_prefix)
238 struct strbuf buf = STRBUF_INIT;
239 append_remote_object_url(&buf, url, hex, only_two_digit_prefix);
240 return strbuf_detach(&buf, NULL);
243 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
244 void *data)
246 unsigned char expn[4096];
247 size_t size = eltsize * nmemb;
248 int posn = 0;
249 struct transfer_request *request = (struct transfer_request *)data;
250 do {
251 ssize_t retval = xwrite(request->local_fileno,
252 (char *) ptr + posn, size - posn);
253 if (retval < 0)
254 return posn;
255 posn += retval;
256 } while (posn < size);
258 request->stream.avail_in = size;
259 request->stream.next_in = ptr;
260 do {
261 request->stream.next_out = expn;
262 request->stream.avail_out = sizeof(expn);
263 request->zret = git_inflate(&request->stream, Z_SYNC_FLUSH);
264 git_SHA1_Update(&request->c, expn,
265 sizeof(expn) - request->stream.avail_out);
266 } while (request->stream.avail_in && request->zret == Z_OK);
267 data_received++;
268 return size;
271 static void start_fetch_loose(struct transfer_request *request)
273 char *hex = sha1_to_hex(request->obj->sha1);
274 char *filename;
275 char prevfile[PATH_MAX];
276 char *url;
277 int prevlocal;
278 unsigned char prev_buf[PREV_BUF_SIZE];
279 ssize_t prev_read = 0;
280 long prev_posn = 0;
281 char range[RANGE_HEADER_SIZE];
282 struct curl_slist *range_header = NULL;
283 struct active_request_slot *slot;
285 filename = sha1_file_name(request->obj->sha1);
286 snprintf(request->filename, sizeof(request->filename), "%s", filename);
287 snprintf(request->tmpfile, sizeof(request->tmpfile),
288 "%s.temp", filename);
290 snprintf(prevfile, sizeof(prevfile), "%s.prev", request->filename);
291 unlink(prevfile);
292 rename(request->tmpfile, prevfile);
293 unlink(request->tmpfile);
295 if (request->local_fileno != -1)
296 error("fd leakage in start: %d", request->local_fileno);
297 request->local_fileno = open(request->tmpfile,
298 O_WRONLY | O_CREAT | O_EXCL, 0666);
299 /* This could have failed due to the "lazy directory creation";
300 * try to mkdir the last path component.
302 if (request->local_fileno < 0 && errno == ENOENT) {
303 char *dir = strrchr(request->tmpfile, '/');
304 if (dir) {
305 *dir = 0;
306 mkdir(request->tmpfile, 0777);
307 *dir = '/';
309 request->local_fileno = open(request->tmpfile,
310 O_WRONLY | O_CREAT | O_EXCL, 0666);
313 if (request->local_fileno < 0) {
314 request->state = ABORTED;
315 error("Couldn't create temporary file %s for %s: %s",
316 request->tmpfile, request->filename, strerror(errno));
317 return;
320 memset(&request->stream, 0, sizeof(request->stream));
322 git_inflate_init(&request->stream);
324 git_SHA1_Init(&request->c);
326 url = get_remote_object_url(remote->url, hex, 0);
327 request->url = xstrdup(url);
329 /* If a previous temp file is present, process what was already
330 fetched. */
331 prevlocal = open(prevfile, O_RDONLY);
332 if (prevlocal != -1) {
333 do {
334 prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE);
335 if (prev_read>0) {
336 if (fwrite_sha1_file(prev_buf,
338 prev_read,
339 request) == prev_read) {
340 prev_posn += prev_read;
341 } else {
342 prev_read = -1;
345 } while (prev_read > 0);
346 close(prevlocal);
348 unlink(prevfile);
350 /* Reset inflate/SHA1 if there was an error reading the previous temp
351 file; also rewind to the beginning of the local file. */
352 if (prev_read == -1) {
353 memset(&request->stream, 0, sizeof(request->stream));
354 git_inflate_init(&request->stream);
355 git_SHA1_Init(&request->c);
356 if (prev_posn>0) {
357 prev_posn = 0;
358 lseek(request->local_fileno, 0, SEEK_SET);
359 ftruncate(request->local_fileno, 0);
363 slot = get_active_slot();
364 slot->callback_func = process_response;
365 slot->callback_data = request;
366 request->slot = slot;
368 curl_easy_setopt(slot->curl, CURLOPT_FILE, request);
369 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
370 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
371 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
372 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
374 /* If we have successfully processed data from a previous fetch
375 attempt, only fetch the data we don't already have. */
376 if (prev_posn>0) {
377 if (push_verbosely)
378 fprintf(stderr,
379 "Resuming fetch of object %s at byte %ld\n",
380 hex, prev_posn);
381 sprintf(range, "Range: bytes=%ld-", prev_posn);
382 range_header = curl_slist_append(range_header, range);
383 curl_easy_setopt(slot->curl,
384 CURLOPT_HTTPHEADER, range_header);
387 /* Try to get the request started, abort the request on error */
388 request->state = RUN_FETCH_LOOSE;
389 if (!start_active_slot(slot)) {
390 fprintf(stderr, "Unable to start GET request\n");
391 remote->can_update_info_refs = 0;
392 release_request(request);
396 static void start_mkcol(struct transfer_request *request)
398 char *hex = sha1_to_hex(request->obj->sha1);
399 struct active_request_slot *slot;
401 request->url = get_remote_object_url(remote->url, hex, 1);
403 slot = get_active_slot();
404 slot->callback_func = process_response;
405 slot->callback_data = request;
406 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
407 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
408 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
409 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
410 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
412 if (start_active_slot(slot)) {
413 request->slot = slot;
414 request->state = RUN_MKCOL;
415 } else {
416 request->state = ABORTED;
417 free(request->url);
418 request->url = NULL;
421 #endif
423 static void start_fetch_packed(struct transfer_request *request)
425 char *url;
426 struct packed_git *target;
427 FILE *packfile;
428 char *filename;
429 long prev_posn = 0;
430 char range[RANGE_HEADER_SIZE];
431 struct curl_slist *range_header = NULL;
433 struct transfer_request *check_request = request_queue_head;
434 struct active_request_slot *slot;
436 target = find_sha1_pack(request->obj->sha1, remote->packs);
437 if (!target) {
438 fprintf(stderr, "Unable to fetch %s, will not be able to update server info refs\n", sha1_to_hex(request->obj->sha1));
439 remote->can_update_info_refs = 0;
440 release_request(request);
441 return;
444 fprintf(stderr, "Fetching pack %s\n", sha1_to_hex(target->sha1));
445 fprintf(stderr, " which contains %s\n", sha1_to_hex(request->obj->sha1));
447 filename = sha1_pack_name(target->sha1);
448 snprintf(request->filename, sizeof(request->filename), "%s", filename);
449 snprintf(request->tmpfile, sizeof(request->tmpfile),
450 "%s.temp", filename);
452 url = xmalloc(strlen(remote->url) + 64);
453 sprintf(url, "%sobjects/pack/pack-%s.pack",
454 remote->url, sha1_to_hex(target->sha1));
456 /* Make sure there isn't another open request for this pack */
457 while (check_request) {
458 if (check_request->state == RUN_FETCH_PACKED &&
459 !strcmp(check_request->url, url)) {
460 free(url);
461 release_request(request);
462 return;
464 check_request = check_request->next;
467 packfile = fopen(request->tmpfile, "a");
468 if (!packfile) {
469 fprintf(stderr, "Unable to open local file %s for pack",
470 request->tmpfile);
471 remote->can_update_info_refs = 0;
472 free(url);
473 return;
476 slot = get_active_slot();
477 slot->callback_func = process_response;
478 slot->callback_data = request;
479 request->slot = slot;
480 request->local_stream = packfile;
481 request->userData = target;
483 request->url = url;
484 curl_easy_setopt(slot->curl, CURLOPT_FILE, packfile);
485 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
486 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
487 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
488 slot->local = packfile;
490 /* If there is data present from a previous transfer attempt,
491 resume where it left off */
492 prev_posn = ftell(packfile);
493 if (prev_posn>0) {
494 if (push_verbosely)
495 fprintf(stderr,
496 "Resuming fetch of pack %s at byte %ld\n",
497 sha1_to_hex(target->sha1), prev_posn);
498 sprintf(range, "Range: bytes=%ld-", prev_posn);
499 range_header = curl_slist_append(range_header, range);
500 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
503 /* Try to get the request started, abort the request on error */
504 request->state = RUN_FETCH_PACKED;
505 if (!start_active_slot(slot)) {
506 fprintf(stderr, "Unable to start GET request\n");
507 remote->can_update_info_refs = 0;
508 release_request(request);
512 static void start_put(struct transfer_request *request)
514 char *hex = sha1_to_hex(request->obj->sha1);
515 struct active_request_slot *slot;
516 struct strbuf buf = STRBUF_INIT;
517 enum object_type type;
518 char hdr[50];
519 void *unpacked;
520 unsigned long len;
521 int hdrlen;
522 ssize_t size;
523 z_stream stream;
525 unpacked = read_sha1_file(request->obj->sha1, &type, &len);
526 hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
528 /* Set it up */
529 memset(&stream, 0, sizeof(stream));
530 deflateInit(&stream, zlib_compression_level);
531 size = deflateBound(&stream, len + hdrlen);
532 strbuf_init(&request->buffer.buf, size);
533 request->buffer.posn = 0;
535 /* Compress it */
536 stream.next_out = (unsigned char *)request->buffer.buf.buf;
537 stream.avail_out = size;
539 /* First header.. */
540 stream.next_in = (void *)hdr;
541 stream.avail_in = hdrlen;
542 while (deflate(&stream, 0) == Z_OK)
543 /* nothing */;
545 /* Then the data itself.. */
546 stream.next_in = unpacked;
547 stream.avail_in = len;
548 while (deflate(&stream, Z_FINISH) == Z_OK)
549 /* nothing */;
550 deflateEnd(&stream);
551 free(unpacked);
553 request->buffer.buf.len = stream.total_out;
555 strbuf_addstr(&buf, "Destination: ");
556 append_remote_object_url(&buf, remote->url, hex, 0);
557 request->dest = strbuf_detach(&buf, NULL);
559 append_remote_object_url(&buf, remote->url, hex, 0);
560 strbuf_add(&buf, request->lock->tmpfile_suffix, 41);
561 request->url = strbuf_detach(&buf, NULL);
563 slot = get_active_slot();
564 slot->callback_func = process_response;
565 slot->callback_data = request;
566 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &request->buffer);
567 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, request->buffer.buf.len);
568 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
569 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
570 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
571 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
572 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
573 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
574 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
576 if (start_active_slot(slot)) {
577 request->slot = slot;
578 request->state = RUN_PUT;
579 } else {
580 request->state = ABORTED;
581 free(request->url);
582 request->url = NULL;
586 static void start_move(struct transfer_request *request)
588 struct active_request_slot *slot;
589 struct curl_slist *dav_headers = NULL;
591 slot = get_active_slot();
592 slot->callback_func = process_response;
593 slot->callback_data = request;
594 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
595 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MOVE);
596 dav_headers = curl_slist_append(dav_headers, request->dest);
597 dav_headers = curl_slist_append(dav_headers, "Overwrite: T");
598 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
599 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
600 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
602 if (start_active_slot(slot)) {
603 request->slot = slot;
604 request->state = RUN_MOVE;
605 } else {
606 request->state = ABORTED;
607 free(request->url);
608 request->url = NULL;
612 static int refresh_lock(struct remote_lock *lock)
614 struct active_request_slot *slot;
615 struct slot_results results;
616 struct curl_slist *dav_headers;
617 int rc = 0;
619 lock->refreshing = 1;
621 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF | DAV_HEADER_TIMEOUT);
623 slot = get_active_slot();
624 slot->results = &results;
625 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
626 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
627 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
628 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
629 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
631 if (start_active_slot(slot)) {
632 run_active_slot(slot);
633 if (results.curl_result != CURLE_OK) {
634 fprintf(stderr, "LOCK HTTP error %ld\n",
635 results.http_code);
636 } else {
637 lock->start_time = time(NULL);
638 rc = 1;
642 lock->refreshing = 0;
643 curl_slist_free_all(dav_headers);
645 return rc;
648 static void check_locks(void)
650 struct remote_lock *lock = remote->locks;
651 time_t current_time = time(NULL);
652 int time_remaining;
654 while (lock) {
655 time_remaining = lock->start_time + lock->timeout -
656 current_time;
657 if (!lock->refreshing && time_remaining < LOCK_REFRESH) {
658 if (!refresh_lock(lock)) {
659 fprintf(stderr,
660 "Unable to refresh lock for %s\n",
661 lock->url);
662 aborted = 1;
663 return;
666 lock = lock->next;
670 static void release_request(struct transfer_request *request)
672 struct transfer_request *entry = request_queue_head;
674 if (request == request_queue_head) {
675 request_queue_head = request->next;
676 } else {
677 while (entry->next != NULL && entry->next != request)
678 entry = entry->next;
679 if (entry->next == request)
680 entry->next = entry->next->next;
683 if (request->local_fileno != -1)
684 close(request->local_fileno);
685 if (request->local_stream)
686 fclose(request->local_stream);
687 free(request->url);
688 free(request);
691 static void finish_request(struct transfer_request *request)
693 struct stat st;
694 struct packed_git *target;
695 struct packed_git **lst;
697 request->curl_result = request->slot->curl_result;
698 request->http_code = request->slot->http_code;
699 request->slot = NULL;
701 /* Keep locks active */
702 check_locks();
704 if (request->headers != NULL)
705 curl_slist_free_all(request->headers);
707 /* URL is reused for MOVE after PUT */
708 if (request->state != RUN_PUT) {
709 free(request->url);
710 request->url = NULL;
713 if (request->state == RUN_MKCOL) {
714 if (request->curl_result == CURLE_OK ||
715 request->http_code == 405) {
716 remote_dir_exists[request->obj->sha1[0]] = 1;
717 start_put(request);
718 } else {
719 fprintf(stderr, "MKCOL %s failed, aborting (%d/%ld)\n",
720 sha1_to_hex(request->obj->sha1),
721 request->curl_result, request->http_code);
722 request->state = ABORTED;
723 aborted = 1;
725 } else if (request->state == RUN_PUT) {
726 if (request->curl_result == CURLE_OK) {
727 start_move(request);
728 } else {
729 fprintf(stderr, "PUT %s failed, aborting (%d/%ld)\n",
730 sha1_to_hex(request->obj->sha1),
731 request->curl_result, request->http_code);
732 request->state = ABORTED;
733 aborted = 1;
735 } else if (request->state == RUN_MOVE) {
736 if (request->curl_result == CURLE_OK) {
737 if (push_verbosely)
738 fprintf(stderr, " sent %s\n",
739 sha1_to_hex(request->obj->sha1));
740 request->obj->flags |= REMOTE;
741 release_request(request);
742 } else {
743 fprintf(stderr, "MOVE %s failed, aborting (%d/%ld)\n",
744 sha1_to_hex(request->obj->sha1),
745 request->curl_result, request->http_code);
746 request->state = ABORTED;
747 aborted = 1;
749 } else if (request->state == RUN_FETCH_LOOSE) {
750 fchmod(request->local_fileno, 0444);
751 close(request->local_fileno); request->local_fileno = -1;
753 if (request->curl_result != CURLE_OK &&
754 request->http_code != 416) {
755 if (stat(request->tmpfile, &st) == 0) {
756 if (st.st_size == 0)
757 unlink(request->tmpfile);
759 } else {
760 if (request->http_code == 416)
761 fprintf(stderr, "Warning: requested range invalid; we may already have all the data.\n");
763 git_inflate_end(&request->stream);
764 git_SHA1_Final(request->real_sha1, &request->c);
765 if (request->zret != Z_STREAM_END) {
766 unlink(request->tmpfile);
767 } else if (hashcmp(request->obj->sha1, request->real_sha1)) {
768 unlink(request->tmpfile);
769 } else {
770 request->rename =
771 move_temp_to_file(
772 request->tmpfile,
773 request->filename);
774 if (request->rename == 0) {
775 request->obj->flags |= (LOCAL | REMOTE);
780 /* Try fetching packed if necessary */
781 if (request->obj->flags & LOCAL)
782 release_request(request);
783 else
784 start_fetch_packed(request);
786 } else if (request->state == RUN_FETCH_PACKED) {
787 if (request->curl_result != CURLE_OK) {
788 fprintf(stderr, "Unable to get pack file %s\n%s",
789 request->url, curl_errorstr);
790 remote->can_update_info_refs = 0;
791 } else {
792 off_t pack_size = ftell(request->local_stream);
794 fclose(request->local_stream);
795 request->local_stream = NULL;
796 if (!move_temp_to_file(request->tmpfile,
797 request->filename)) {
798 target = (struct packed_git *)request->userData;
799 target->pack_size = pack_size;
800 lst = &remote->packs;
801 while (*lst != target)
802 lst = &((*lst)->next);
803 *lst = (*lst)->next;
805 if (!verify_pack(target))
806 install_packed_git(target);
807 else
808 remote->can_update_info_refs = 0;
811 release_request(request);
815 #ifdef USE_CURL_MULTI
816 static int fill_active_slot(void *unused)
818 struct transfer_request *request = request_queue_head;
820 if (aborted)
821 return 0;
823 for (request = request_queue_head; request; request = request->next) {
824 if (request->state == NEED_FETCH) {
825 start_fetch_loose(request);
826 return 1;
827 } else if (pushing && request->state == NEED_PUSH) {
828 if (remote_dir_exists[request->obj->sha1[0]] == 1) {
829 start_put(request);
830 } else {
831 start_mkcol(request);
833 return 1;
836 return 0;
838 #endif
840 static void get_remote_object_list(unsigned char parent);
842 static void add_fetch_request(struct object *obj)
844 struct transfer_request *request;
846 check_locks();
849 * Don't fetch the object if it's known to exist locally
850 * or is already in the request queue
852 if (remote_dir_exists[obj->sha1[0]] == -1)
853 get_remote_object_list(obj->sha1[0]);
854 if (obj->flags & (LOCAL | FETCHING))
855 return;
857 obj->flags |= FETCHING;
858 request = xmalloc(sizeof(*request));
859 request->obj = obj;
860 request->url = NULL;
861 request->lock = NULL;
862 request->headers = NULL;
863 request->local_fileno = -1;
864 request->local_stream = NULL;
865 request->state = NEED_FETCH;
866 request->next = request_queue_head;
867 request_queue_head = request;
869 #ifdef USE_CURL_MULTI
870 fill_active_slots();
871 step_active_slots();
872 #endif
875 static int add_send_request(struct object *obj, struct remote_lock *lock)
877 struct transfer_request *request = request_queue_head;
878 struct packed_git *target;
880 /* Keep locks active */
881 check_locks();
884 * Don't push the object if it's known to exist on the remote
885 * or is already in the request queue
887 if (remote_dir_exists[obj->sha1[0]] == -1)
888 get_remote_object_list(obj->sha1[0]);
889 if (obj->flags & (REMOTE | PUSHING))
890 return 0;
891 target = find_sha1_pack(obj->sha1, remote->packs);
892 if (target) {
893 obj->flags |= REMOTE;
894 return 0;
897 obj->flags |= PUSHING;
898 request = xmalloc(sizeof(*request));
899 request->obj = obj;
900 request->url = NULL;
901 request->lock = lock;
902 request->headers = NULL;
903 request->local_fileno = -1;
904 request->local_stream = NULL;
905 request->state = NEED_PUSH;
906 request->next = request_queue_head;
907 request_queue_head = request;
909 #ifdef USE_CURL_MULTI
910 fill_active_slots();
911 step_active_slots();
912 #endif
914 return 1;
917 static int fetch_index(unsigned char *sha1)
919 char *hex = sha1_to_hex(sha1);
920 char *filename;
921 char *url;
922 char tmpfile[PATH_MAX];
923 long prev_posn = 0;
924 char range[RANGE_HEADER_SIZE];
925 struct curl_slist *range_header = NULL;
927 FILE *indexfile;
928 struct active_request_slot *slot;
929 struct slot_results results;
931 /* Don't use the index if the pack isn't there */
932 url = xmalloc(strlen(remote->url) + 64);
933 sprintf(url, "%sobjects/pack/pack-%s.pack", remote->url, hex);
934 slot = get_active_slot();
935 slot->results = &results;
936 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
937 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
938 if (start_active_slot(slot)) {
939 run_active_slot(slot);
940 if (results.curl_result != CURLE_OK) {
941 free(url);
942 return error("Unable to verify pack %s is available",
943 hex);
945 } else {
946 free(url);
947 return error("Unable to start request");
950 if (has_pack_index(sha1)) {
951 free(url);
952 return 0;
955 if (push_verbosely)
956 fprintf(stderr, "Getting index for pack %s\n", hex);
958 sprintf(url, "%sobjects/pack/pack-%s.idx", remote->url, hex);
960 filename = sha1_pack_index_name(sha1);
961 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
962 indexfile = fopen(tmpfile, "a");
963 if (!indexfile) {
964 free(url);
965 return error("Unable to open local file %s for pack index",
966 tmpfile);
969 slot = get_active_slot();
970 slot->results = &results;
971 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
972 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
973 curl_easy_setopt(slot->curl, CURLOPT_FILE, indexfile);
974 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
975 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
976 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
977 slot->local = indexfile;
979 /* If there is data present from a previous transfer attempt,
980 resume where it left off */
981 prev_posn = ftell(indexfile);
982 if (prev_posn>0) {
983 if (push_verbosely)
984 fprintf(stderr,
985 "Resuming fetch of index for pack %s at byte %ld\n",
986 hex, prev_posn);
987 sprintf(range, "Range: bytes=%ld-", prev_posn);
988 range_header = curl_slist_append(range_header, range);
989 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
992 if (start_active_slot(slot)) {
993 run_active_slot(slot);
994 if (results.curl_result != CURLE_OK) {
995 free(url);
996 fclose(indexfile);
997 return error("Unable to get pack index %s\n%s", url,
998 curl_errorstr);
1000 } else {
1001 free(url);
1002 fclose(indexfile);
1003 return error("Unable to start request");
1006 free(url);
1007 fclose(indexfile);
1009 return move_temp_to_file(tmpfile, filename);
1012 static int setup_index(unsigned char *sha1)
1014 struct packed_git *new_pack;
1016 if (fetch_index(sha1))
1017 return -1;
1019 new_pack = parse_pack_index(sha1);
1020 new_pack->next = remote->packs;
1021 remote->packs = new_pack;
1022 return 0;
1025 static int fetch_indices(void)
1027 unsigned char sha1[20];
1028 char *url;
1029 struct strbuf buffer = STRBUF_INIT;
1030 char *data;
1031 int i = 0;
1033 struct active_request_slot *slot;
1034 struct slot_results results;
1036 if (push_verbosely)
1037 fprintf(stderr, "Getting pack list\n");
1039 url = xmalloc(strlen(remote->url) + 20);
1040 sprintf(url, "%sobjects/info/packs", remote->url);
1042 slot = get_active_slot();
1043 slot->results = &results;
1044 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
1045 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1046 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1047 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
1048 if (start_active_slot(slot)) {
1049 run_active_slot(slot);
1050 if (results.curl_result != CURLE_OK) {
1051 strbuf_release(&buffer);
1052 free(url);
1053 if (results.http_code == 404)
1054 return 0;
1055 else
1056 return error("%s", curl_errorstr);
1058 } else {
1059 strbuf_release(&buffer);
1060 free(url);
1061 return error("Unable to start request");
1063 free(url);
1065 data = buffer.buf;
1066 while (i < buffer.len) {
1067 switch (data[i]) {
1068 case 'P':
1069 i++;
1070 if (i + 52 < buffer.len &&
1071 !prefixcmp(data + i, " pack-") &&
1072 !prefixcmp(data + i + 46, ".pack\n")) {
1073 get_sha1_hex(data + i + 6, sha1);
1074 setup_index(sha1);
1075 i += 51;
1076 break;
1078 default:
1079 while (data[i] != '\n')
1080 i++;
1082 i++;
1085 strbuf_release(&buffer);
1086 return 0;
1089 static void one_remote_object(const char *hex)
1091 unsigned char sha1[20];
1092 struct object *obj;
1094 if (get_sha1_hex(hex, sha1) != 0)
1095 return;
1097 obj = lookup_object(sha1);
1098 if (!obj)
1099 obj = parse_object(sha1);
1101 /* Ignore remote objects that don't exist locally */
1102 if (!obj)
1103 return;
1105 obj->flags |= REMOTE;
1106 if (!object_list_contains(objects, obj))
1107 object_list_insert(obj, &objects);
1110 static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed)
1112 int *lock_flags = (int *)ctx->userData;
1114 if (tag_closed) {
1115 if (!strcmp(ctx->name, DAV_CTX_LOCKENTRY)) {
1116 if ((*lock_flags & DAV_PROP_LOCKEX) &&
1117 (*lock_flags & DAV_PROP_LOCKWR)) {
1118 *lock_flags |= DAV_LOCK_OK;
1120 *lock_flags &= DAV_LOCK_OK;
1121 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_WRITE)) {
1122 *lock_flags |= DAV_PROP_LOCKWR;
1123 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_EXCLUSIVE)) {
1124 *lock_flags |= DAV_PROP_LOCKEX;
1129 static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
1131 struct remote_lock *lock = (struct remote_lock *)ctx->userData;
1132 git_SHA_CTX sha_ctx;
1133 unsigned char lock_token_sha1[20];
1135 if (tag_closed && ctx->cdata) {
1136 if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) {
1137 lock->owner = xmalloc(strlen(ctx->cdata) + 1);
1138 strcpy(lock->owner, ctx->cdata);
1139 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
1140 if (!prefixcmp(ctx->cdata, "Second-"))
1141 lock->timeout =
1142 strtol(ctx->cdata + 7, NULL, 10);
1143 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
1144 lock->token = xmalloc(strlen(ctx->cdata) + 1);
1145 strcpy(lock->token, ctx->cdata);
1147 git_SHA1_Init(&sha_ctx);
1148 git_SHA1_Update(&sha_ctx, lock->token, strlen(lock->token));
1149 git_SHA1_Final(lock_token_sha1, &sha_ctx);
1151 lock->tmpfile_suffix[0] = '_';
1152 memcpy(lock->tmpfile_suffix + 1, sha1_to_hex(lock_token_sha1), 40);
1157 static void one_remote_ref(char *refname);
1159 static void
1160 xml_start_tag(void *userData, const char *name, const char **atts)
1162 struct xml_ctx *ctx = (struct xml_ctx *)userData;
1163 const char *c = strchr(name, ':');
1164 int new_len;
1166 if (c == NULL)
1167 c = name;
1168 else
1169 c++;
1171 new_len = strlen(ctx->name) + strlen(c) + 2;
1173 if (new_len > ctx->len) {
1174 ctx->name = xrealloc(ctx->name, new_len);
1175 ctx->len = new_len;
1177 strcat(ctx->name, ".");
1178 strcat(ctx->name, c);
1180 free(ctx->cdata);
1181 ctx->cdata = NULL;
1183 ctx->userFunc(ctx, 0);
1186 static void
1187 xml_end_tag(void *userData, const char *name)
1189 struct xml_ctx *ctx = (struct xml_ctx *)userData;
1190 const char *c = strchr(name, ':');
1191 char *ep;
1193 ctx->userFunc(ctx, 1);
1195 if (c == NULL)
1196 c = name;
1197 else
1198 c++;
1200 ep = ctx->name + strlen(ctx->name) - strlen(c) - 1;
1201 *ep = 0;
1204 static void
1205 xml_cdata(void *userData, const XML_Char *s, int len)
1207 struct xml_ctx *ctx = (struct xml_ctx *)userData;
1208 free(ctx->cdata);
1209 ctx->cdata = xmemdupz(s, len);
1212 static struct remote_lock *lock_remote(const char *path, long timeout)
1214 struct active_request_slot *slot;
1215 struct slot_results results;
1216 struct buffer out_buffer = { STRBUF_INIT, 0 };
1217 struct strbuf in_buffer = STRBUF_INIT;
1218 char *url;
1219 char *ep;
1220 char timeout_header[25];
1221 struct remote_lock *lock = NULL;
1222 struct curl_slist *dav_headers = NULL;
1223 struct xml_ctx ctx;
1225 url = xmalloc(strlen(remote->url) + strlen(path) + 1);
1226 sprintf(url, "%s%s", remote->url, path);
1228 /* Make sure leading directories exist for the remote ref */
1229 ep = strchr(url + strlen(remote->url) + 1, '/');
1230 while (ep) {
1231 char saved_character = ep[1];
1232 ep[1] = '\0';
1233 slot = get_active_slot();
1234 slot->results = &results;
1235 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
1236 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1237 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
1238 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1239 if (start_active_slot(slot)) {
1240 run_active_slot(slot);
1241 if (results.curl_result != CURLE_OK &&
1242 results.http_code != 405) {
1243 fprintf(stderr,
1244 "Unable to create branch path %s\n",
1245 url);
1246 free(url);
1247 return NULL;
1249 } else {
1250 fprintf(stderr, "Unable to start MKCOL request\n");
1251 free(url);
1252 return NULL;
1254 ep[1] = saved_character;
1255 ep = strchr(ep + 1, '/');
1258 strbuf_addf(&out_buffer.buf, LOCK_REQUEST, git_default_email);
1260 sprintf(timeout_header, "Timeout: Second-%ld", timeout);
1261 dav_headers = curl_slist_append(dav_headers, timeout_header);
1262 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1264 slot = get_active_slot();
1265 slot->results = &results;
1266 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1267 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1268 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1269 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1270 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1271 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1272 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1273 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
1274 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1276 lock = xcalloc(1, sizeof(*lock));
1277 lock->timeout = -1;
1279 if (start_active_slot(slot)) {
1280 run_active_slot(slot);
1281 if (results.curl_result == CURLE_OK) {
1282 XML_Parser parser = XML_ParserCreate(NULL);
1283 enum XML_Status result;
1284 ctx.name = xcalloc(10, 1);
1285 ctx.len = 0;
1286 ctx.cdata = NULL;
1287 ctx.userFunc = handle_new_lock_ctx;
1288 ctx.userData = lock;
1289 XML_SetUserData(parser, &ctx);
1290 XML_SetElementHandler(parser, xml_start_tag,
1291 xml_end_tag);
1292 XML_SetCharacterDataHandler(parser, xml_cdata);
1293 result = XML_Parse(parser, in_buffer.buf,
1294 in_buffer.len, 1);
1295 free(ctx.name);
1296 if (result != XML_STATUS_OK) {
1297 fprintf(stderr, "XML error: %s\n",
1298 XML_ErrorString(
1299 XML_GetErrorCode(parser)));
1300 lock->timeout = -1;
1302 XML_ParserFree(parser);
1304 } else {
1305 fprintf(stderr, "Unable to start LOCK request\n");
1308 curl_slist_free_all(dav_headers);
1309 strbuf_release(&out_buffer.buf);
1310 strbuf_release(&in_buffer);
1312 if (lock->token == NULL || lock->timeout <= 0) {
1313 free(lock->token);
1314 free(lock->owner);
1315 free(url);
1316 free(lock);
1317 lock = NULL;
1318 } else {
1319 lock->url = url;
1320 lock->start_time = time(NULL);
1321 lock->next = remote->locks;
1322 remote->locks = lock;
1325 return lock;
1328 static int unlock_remote(struct remote_lock *lock)
1330 struct active_request_slot *slot;
1331 struct slot_results results;
1332 struct remote_lock *prev = remote->locks;
1333 struct curl_slist *dav_headers;
1334 int rc = 0;
1336 dav_headers = get_dav_token_headers(lock, DAV_HEADER_LOCK);
1338 slot = get_active_slot();
1339 slot->results = &results;
1340 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1341 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1342 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_UNLOCK);
1343 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1345 if (start_active_slot(slot)) {
1346 run_active_slot(slot);
1347 if (results.curl_result == CURLE_OK)
1348 rc = 1;
1349 else
1350 fprintf(stderr, "UNLOCK HTTP error %ld\n",
1351 results.http_code);
1352 } else {
1353 fprintf(stderr, "Unable to start UNLOCK request\n");
1356 curl_slist_free_all(dav_headers);
1358 if (remote->locks == lock) {
1359 remote->locks = lock->next;
1360 } else {
1361 while (prev && prev->next != lock)
1362 prev = prev->next;
1363 if (prev)
1364 prev->next = prev->next->next;
1367 free(lock->owner);
1368 free(lock->url);
1369 free(lock->token);
1370 free(lock);
1372 return rc;
1375 static void remove_locks(void)
1377 struct remote_lock *lock = remote->locks;
1379 fprintf(stderr, "Removing remote locks...\n");
1380 while (lock) {
1381 unlock_remote(lock);
1382 lock = lock->next;
1386 static void remove_locks_on_signal(int signo)
1388 remove_locks();
1389 signal(signo, SIG_DFL);
1390 raise(signo);
1393 static void remote_ls(const char *path, int flags,
1394 void (*userFunc)(struct remote_ls_ctx *ls),
1395 void *userData);
1397 static void process_ls_object(struct remote_ls_ctx *ls)
1399 unsigned int *parent = (unsigned int *)ls->userData;
1400 char *path = ls->dentry_name;
1401 char *obj_hex;
1403 if (!strcmp(ls->path, ls->dentry_name) && (ls->flags & IS_DIR)) {
1404 remote_dir_exists[*parent] = 1;
1405 return;
1408 if (strlen(path) != 49)
1409 return;
1410 path += 8;
1411 obj_hex = xmalloc(strlen(path));
1412 /* NB: path is not null-terminated, can not use strlcpy here */
1413 memcpy(obj_hex, path, 2);
1414 strcpy(obj_hex + 2, path + 3);
1415 one_remote_object(obj_hex);
1416 free(obj_hex);
1419 static void process_ls_ref(struct remote_ls_ctx *ls)
1421 if (!strcmp(ls->path, ls->dentry_name) && (ls->dentry_flags & IS_DIR)) {
1422 fprintf(stderr, " %s\n", ls->dentry_name);
1423 return;
1426 if (!(ls->dentry_flags & IS_DIR))
1427 one_remote_ref(ls->dentry_name);
1430 static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
1432 struct remote_ls_ctx *ls = (struct remote_ls_ctx *)ctx->userData;
1434 if (tag_closed) {
1435 if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) {
1436 if (ls->dentry_flags & IS_DIR) {
1437 if (ls->flags & PROCESS_DIRS) {
1438 ls->userFunc(ls);
1440 if (strcmp(ls->dentry_name, ls->path) &&
1441 ls->flags & RECURSIVE) {
1442 remote_ls(ls->dentry_name,
1443 ls->flags,
1444 ls->userFunc,
1445 ls->userData);
1447 } else if (ls->flags & PROCESS_FILES) {
1448 ls->userFunc(ls);
1450 } else if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
1451 char *path = ctx->cdata;
1452 if (*ctx->cdata == 'h') {
1453 path = strstr(path, "//");
1454 if (path) {
1455 path = strchr(path+2, '/');
1458 if (path) {
1459 path += remote->path_len;
1460 ls->dentry_name = xstrdup(path);
1462 } else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
1463 ls->dentry_flags |= IS_DIR;
1465 } else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
1466 free(ls->dentry_name);
1467 ls->dentry_name = NULL;
1468 ls->dentry_flags = 0;
1473 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1474 * should _only_ heed the information from that file, instead of trying to
1475 * determine the refs from the remote file system (badly: it does not even
1476 * know about packed-refs).
1478 static void remote_ls(const char *path, int flags,
1479 void (*userFunc)(struct remote_ls_ctx *ls),
1480 void *userData)
1482 char *url = xmalloc(strlen(remote->url) + strlen(path) + 1);
1483 struct active_request_slot *slot;
1484 struct slot_results results;
1485 struct strbuf in_buffer = STRBUF_INIT;
1486 struct buffer out_buffer = { STRBUF_INIT, 0 };
1487 struct curl_slist *dav_headers = NULL;
1488 struct xml_ctx ctx;
1489 struct remote_ls_ctx ls;
1491 ls.flags = flags;
1492 ls.path = xstrdup(path);
1493 ls.dentry_name = NULL;
1494 ls.dentry_flags = 0;
1495 ls.userData = userData;
1496 ls.userFunc = userFunc;
1498 sprintf(url, "%s%s", remote->url, path);
1500 strbuf_addf(&out_buffer.buf, PROPFIND_ALL_REQUEST);
1502 dav_headers = curl_slist_append(dav_headers, "Depth: 1");
1503 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1505 slot = get_active_slot();
1506 slot->results = &results;
1507 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1508 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1509 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1510 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1511 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1512 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1513 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1514 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1515 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1517 if (start_active_slot(slot)) {
1518 run_active_slot(slot);
1519 if (results.curl_result == CURLE_OK) {
1520 XML_Parser parser = XML_ParserCreate(NULL);
1521 enum XML_Status result;
1522 ctx.name = xcalloc(10, 1);
1523 ctx.len = 0;
1524 ctx.cdata = NULL;
1525 ctx.userFunc = handle_remote_ls_ctx;
1526 ctx.userData = &ls;
1527 XML_SetUserData(parser, &ctx);
1528 XML_SetElementHandler(parser, xml_start_tag,
1529 xml_end_tag);
1530 XML_SetCharacterDataHandler(parser, xml_cdata);
1531 result = XML_Parse(parser, in_buffer.buf,
1532 in_buffer.len, 1);
1533 free(ctx.name);
1535 if (result != XML_STATUS_OK) {
1536 fprintf(stderr, "XML error: %s\n",
1537 XML_ErrorString(
1538 XML_GetErrorCode(parser)));
1540 XML_ParserFree(parser);
1542 } else {
1543 fprintf(stderr, "Unable to start PROPFIND request\n");
1546 free(ls.path);
1547 free(url);
1548 strbuf_release(&out_buffer.buf);
1549 strbuf_release(&in_buffer);
1550 curl_slist_free_all(dav_headers);
1553 static void get_remote_object_list(unsigned char parent)
1555 char path[] = "objects/XX/";
1556 static const char hex[] = "0123456789abcdef";
1557 unsigned int val = parent;
1559 path[8] = hex[val >> 4];
1560 path[9] = hex[val & 0xf];
1561 remote_dir_exists[val] = 0;
1562 remote_ls(path, (PROCESS_FILES | PROCESS_DIRS),
1563 process_ls_object, &val);
1566 static int locking_available(void)
1568 struct active_request_slot *slot;
1569 struct slot_results results;
1570 struct strbuf in_buffer = STRBUF_INIT;
1571 struct buffer out_buffer = { STRBUF_INIT, 0 };
1572 struct curl_slist *dav_headers = NULL;
1573 struct xml_ctx ctx;
1574 int lock_flags = 0;
1576 strbuf_addf(&out_buffer.buf, PROPFIND_SUPPORTEDLOCK_REQUEST, remote->url);
1578 dav_headers = curl_slist_append(dav_headers, "Depth: 0");
1579 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1581 slot = get_active_slot();
1582 slot->results = &results;
1583 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1584 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1585 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1586 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1587 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1588 curl_easy_setopt(slot->curl, CURLOPT_URL, remote->url);
1589 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1590 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1591 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1593 if (start_active_slot(slot)) {
1594 run_active_slot(slot);
1595 if (results.curl_result == CURLE_OK) {
1596 XML_Parser parser = XML_ParserCreate(NULL);
1597 enum XML_Status result;
1598 ctx.name = xcalloc(10, 1);
1599 ctx.len = 0;
1600 ctx.cdata = NULL;
1601 ctx.userFunc = handle_lockprop_ctx;
1602 ctx.userData = &lock_flags;
1603 XML_SetUserData(parser, &ctx);
1604 XML_SetElementHandler(parser, xml_start_tag,
1605 xml_end_tag);
1606 result = XML_Parse(parser, in_buffer.buf,
1607 in_buffer.len, 1);
1608 free(ctx.name);
1610 if (result != XML_STATUS_OK) {
1611 fprintf(stderr, "XML error: %s\n",
1612 XML_ErrorString(
1613 XML_GetErrorCode(parser)));
1614 lock_flags = 0;
1616 XML_ParserFree(parser);
1617 if (!lock_flags)
1618 error("Error: no DAV locking support on %s",
1619 remote->url);
1621 } else {
1622 error("Cannot access URL %s, return code %d",
1623 remote->url, results.curl_result);
1624 lock_flags = 0;
1626 } else {
1627 error("Unable to start PROPFIND request on %s", remote->url);
1630 strbuf_release(&out_buffer.buf);
1631 strbuf_release(&in_buffer);
1632 curl_slist_free_all(dav_headers);
1634 return lock_flags;
1637 static struct object_list **add_one_object(struct object *obj, struct object_list **p)
1639 struct object_list *entry = xmalloc(sizeof(struct object_list));
1640 entry->item = obj;
1641 entry->next = *p;
1642 *p = entry;
1643 return &entry->next;
1646 static struct object_list **process_blob(struct blob *blob,
1647 struct object_list **p,
1648 struct name_path *path,
1649 const char *name)
1651 struct object *obj = &blob->object;
1653 obj->flags |= LOCAL;
1655 if (obj->flags & (UNINTERESTING | SEEN))
1656 return p;
1658 obj->flags |= SEEN;
1659 return add_one_object(obj, p);
1662 static struct object_list **process_tree(struct tree *tree,
1663 struct object_list **p,
1664 struct name_path *path,
1665 const char *name)
1667 struct object *obj = &tree->object;
1668 struct tree_desc desc;
1669 struct name_entry entry;
1670 struct name_path me;
1672 obj->flags |= LOCAL;
1674 if (obj->flags & (UNINTERESTING | SEEN))
1675 return p;
1676 if (parse_tree(tree) < 0)
1677 die("bad tree object %s", sha1_to_hex(obj->sha1));
1679 obj->flags |= SEEN;
1680 name = xstrdup(name);
1681 p = add_one_object(obj, p);
1682 me.up = path;
1683 me.elem = name;
1684 me.elem_len = strlen(name);
1686 init_tree_desc(&desc, tree->buffer, tree->size);
1688 while (tree_entry(&desc, &entry))
1689 switch (object_type(entry.mode)) {
1690 case OBJ_TREE:
1691 p = process_tree(lookup_tree(entry.sha1), p, &me, name);
1692 break;
1693 case OBJ_BLOB:
1694 p = process_blob(lookup_blob(entry.sha1), p, &me, name);
1695 break;
1696 default:
1697 /* Subproject commit - not in this repository */
1698 break;
1701 free(tree->buffer);
1702 tree->buffer = NULL;
1703 return p;
1706 static int get_delta(struct rev_info *revs, struct remote_lock *lock)
1708 int i;
1709 struct commit *commit;
1710 struct object_list **p = &objects;
1711 int count = 0;
1713 while ((commit = get_revision(revs)) != NULL) {
1714 p = process_tree(commit->tree, p, NULL, "");
1715 commit->object.flags |= LOCAL;
1716 if (!(commit->object.flags & UNINTERESTING))
1717 count += add_send_request(&commit->object, lock);
1720 for (i = 0; i < revs->pending.nr; i++) {
1721 struct object_array_entry *entry = revs->pending.objects + i;
1722 struct object *obj = entry->item;
1723 const char *name = entry->name;
1725 if (obj->flags & (UNINTERESTING | SEEN))
1726 continue;
1727 if (obj->type == OBJ_TAG) {
1728 obj->flags |= SEEN;
1729 p = add_one_object(obj, p);
1730 continue;
1732 if (obj->type == OBJ_TREE) {
1733 p = process_tree((struct tree *)obj, p, NULL, name);
1734 continue;
1736 if (obj->type == OBJ_BLOB) {
1737 p = process_blob((struct blob *)obj, p, NULL, name);
1738 continue;
1740 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
1743 while (objects) {
1744 if (!(objects->item->flags & UNINTERESTING))
1745 count += add_send_request(objects->item, lock);
1746 objects = objects->next;
1749 return count;
1752 static int update_remote(unsigned char *sha1, struct remote_lock *lock)
1754 struct active_request_slot *slot;
1755 struct slot_results results;
1756 struct buffer out_buffer = { STRBUF_INIT, 0 };
1757 struct curl_slist *dav_headers;
1759 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1761 strbuf_addf(&out_buffer.buf, "%s\n", sha1_to_hex(sha1));
1763 slot = get_active_slot();
1764 slot->results = &results;
1765 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1766 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1767 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1768 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1769 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1770 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1771 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1772 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1773 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1775 if (start_active_slot(slot)) {
1776 run_active_slot(slot);
1777 strbuf_release(&out_buffer.buf);
1778 if (results.curl_result != CURLE_OK) {
1779 fprintf(stderr,
1780 "PUT error: curl result=%d, HTTP code=%ld\n",
1781 results.curl_result, results.http_code);
1782 /* We should attempt recovery? */
1783 return 0;
1785 } else {
1786 strbuf_release(&out_buffer.buf);
1787 fprintf(stderr, "Unable to start PUT request\n");
1788 return 0;
1791 return 1;
1794 static struct ref *local_refs, **local_tail;
1795 static struct ref *remote_refs, **remote_tail;
1797 static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
1799 struct ref *ref;
1800 int len = strlen(refname) + 1;
1801 ref = xcalloc(1, sizeof(*ref) + len);
1802 hashcpy(ref->new_sha1, sha1);
1803 memcpy(ref->name, refname, len);
1804 *local_tail = ref;
1805 local_tail = &ref->next;
1806 return 0;
1809 static void one_remote_ref(char *refname)
1811 struct ref *ref;
1812 struct object *obj;
1814 ref = alloc_ref(refname);
1816 if (http_fetch_ref(remote->url, ref) != 0) {
1817 fprintf(stderr,
1818 "Unable to fetch ref %s from %s\n",
1819 refname, remote->url);
1820 free(ref);
1821 return;
1825 * Fetch a copy of the object if it doesn't exist locally - it
1826 * may be required for updating server info later.
1828 if (remote->can_update_info_refs && !has_sha1_file(ref->old_sha1)) {
1829 obj = lookup_unknown_object(ref->old_sha1);
1830 if (obj) {
1831 fprintf(stderr, " fetch %s for %s\n",
1832 sha1_to_hex(ref->old_sha1), refname);
1833 add_fetch_request(obj);
1837 *remote_tail = ref;
1838 remote_tail = &ref->next;
1841 static void get_local_heads(void)
1843 local_tail = &local_refs;
1844 for_each_ref(one_local_ref, NULL);
1847 static void get_dav_remote_heads(void)
1849 remote_tail = &remote_refs;
1850 remote_ls("refs/", (PROCESS_FILES | PROCESS_DIRS | RECURSIVE), process_ls_ref, NULL);
1853 static int is_zero_sha1(const unsigned char *sha1)
1855 int i;
1857 for (i = 0; i < 20; i++) {
1858 if (*sha1++)
1859 return 0;
1861 return 1;
1864 static void unmark_and_free(struct commit_list *list, unsigned int mark)
1866 while (list) {
1867 struct commit_list *temp = list;
1868 temp->item->object.flags &= ~mark;
1869 list = temp->next;
1870 free(temp);
1874 static int ref_newer(const unsigned char *new_sha1,
1875 const unsigned char *old_sha1)
1877 struct object *o;
1878 struct commit *old, *new;
1879 struct commit_list *list, *used;
1880 int found = 0;
1882 /* Both new and old must be commit-ish and new is descendant of
1883 * old. Otherwise we require --force.
1885 o = deref_tag(parse_object(old_sha1), NULL, 0);
1886 if (!o || o->type != OBJ_COMMIT)
1887 return 0;
1888 old = (struct commit *) o;
1890 o = deref_tag(parse_object(new_sha1), NULL, 0);
1891 if (!o || o->type != OBJ_COMMIT)
1892 return 0;
1893 new = (struct commit *) o;
1895 if (parse_commit(new) < 0)
1896 return 0;
1898 used = list = NULL;
1899 commit_list_insert(new, &list);
1900 while (list) {
1901 new = pop_most_recent_commit(&list, TMP_MARK);
1902 commit_list_insert(new, &used);
1903 if (new == old) {
1904 found = 1;
1905 break;
1908 unmark_and_free(list, TMP_MARK);
1909 unmark_and_free(used, TMP_MARK);
1910 return found;
1913 static void add_remote_info_ref(struct remote_ls_ctx *ls)
1915 struct strbuf *buf = (struct strbuf *)ls->userData;
1916 struct object *o;
1917 int len;
1918 char *ref_info;
1919 struct ref *ref;
1921 ref = alloc_ref(ls->dentry_name);
1923 if (http_fetch_ref(remote->url, ref) != 0) {
1924 fprintf(stderr,
1925 "Unable to fetch ref %s from %s\n",
1926 ls->dentry_name, remote->url);
1927 aborted = 1;
1928 free(ref);
1929 return;
1932 o = parse_object(ref->old_sha1);
1933 if (!o) {
1934 fprintf(stderr,
1935 "Unable to parse object %s for remote ref %s\n",
1936 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1937 aborted = 1;
1938 free(ref);
1939 return;
1942 len = strlen(ls->dentry_name) + 42;
1943 ref_info = xcalloc(len + 1, 1);
1944 sprintf(ref_info, "%s %s\n",
1945 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1946 fwrite_buffer(ref_info, 1, len, buf);
1947 free(ref_info);
1949 if (o->type == OBJ_TAG) {
1950 o = deref_tag(o, ls->dentry_name, 0);
1951 if (o) {
1952 len = strlen(ls->dentry_name) + 45;
1953 ref_info = xcalloc(len + 1, 1);
1954 sprintf(ref_info, "%s %s^{}\n",
1955 sha1_to_hex(o->sha1), ls->dentry_name);
1956 fwrite_buffer(ref_info, 1, len, buf);
1957 free(ref_info);
1960 free(ref);
1963 static void update_remote_info_refs(struct remote_lock *lock)
1965 struct buffer buffer = { STRBUF_INIT, 0 };
1966 struct active_request_slot *slot;
1967 struct slot_results results;
1968 struct curl_slist *dav_headers;
1970 remote_ls("refs/", (PROCESS_FILES | RECURSIVE),
1971 add_remote_info_ref, &buffer.buf);
1972 if (!aborted) {
1973 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1975 slot = get_active_slot();
1976 slot->results = &results;
1977 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &buffer);
1978 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, buffer.buf.len);
1979 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1980 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1981 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1982 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1983 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1984 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1985 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1987 if (start_active_slot(slot)) {
1988 run_active_slot(slot);
1989 if (results.curl_result != CURLE_OK) {
1990 fprintf(stderr,
1991 "PUT error: curl result=%d, HTTP code=%ld\n",
1992 results.curl_result, results.http_code);
1996 strbuf_release(&buffer.buf);
1999 static int remote_exists(const char *path)
2001 char *url = xmalloc(strlen(remote->url) + strlen(path) + 1);
2002 struct active_request_slot *slot;
2003 struct slot_results results;
2004 int ret = -1;
2006 sprintf(url, "%s%s", remote->url, path);
2008 slot = get_active_slot();
2009 slot->results = &results;
2010 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2011 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
2013 if (start_active_slot(slot)) {
2014 run_active_slot(slot);
2015 if (results.http_code == 404)
2016 ret = 0;
2017 else if (results.curl_result == CURLE_OK)
2018 ret = 1;
2019 else
2020 fprintf(stderr, "HEAD HTTP error %ld\n", results.http_code);
2021 } else {
2022 fprintf(stderr, "Unable to start HEAD request\n");
2025 free(url);
2026 return ret;
2029 static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
2031 char *url;
2032 struct strbuf buffer = STRBUF_INIT;
2033 struct active_request_slot *slot;
2034 struct slot_results results;
2036 url = xmalloc(strlen(remote->url) + strlen(path) + 1);
2037 sprintf(url, "%s%s", remote->url, path);
2039 slot = get_active_slot();
2040 slot->results = &results;
2041 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
2042 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
2043 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
2044 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2045 if (start_active_slot(slot)) {
2046 run_active_slot(slot);
2047 if (results.curl_result != CURLE_OK) {
2048 die("Couldn't get %s for remote symref\n%s",
2049 url, curl_errorstr);
2051 } else {
2052 die("Unable to start remote symref request");
2054 free(url);
2056 free(*symref);
2057 *symref = NULL;
2058 hashclr(sha1);
2060 if (buffer.len == 0)
2061 return;
2063 /* If it's a symref, set the refname; otherwise try for a sha1 */
2064 if (!prefixcmp((char *)buffer.buf, "ref: ")) {
2065 *symref = xmemdupz((char *)buffer.buf + 5, buffer.len - 6);
2066 } else {
2067 get_sha1_hex(buffer.buf, sha1);
2070 strbuf_release(&buffer);
2073 static int verify_merge_base(unsigned char *head_sha1, unsigned char *branch_sha1)
2075 struct commit *head = lookup_commit(head_sha1);
2076 struct commit *branch = lookup_commit(branch_sha1);
2077 struct commit_list *merge_bases = get_merge_bases(head, branch, 1);
2079 return (merge_bases && !merge_bases->next && merge_bases->item == branch);
2082 static int delete_remote_branch(char *pattern, int force)
2084 struct ref *refs = remote_refs;
2085 struct ref *remote_ref = NULL;
2086 unsigned char head_sha1[20];
2087 char *symref = NULL;
2088 int match;
2089 int patlen = strlen(pattern);
2090 int i;
2091 struct active_request_slot *slot;
2092 struct slot_results results;
2093 char *url;
2095 /* Find the remote branch(es) matching the specified branch name */
2096 for (match = 0; refs; refs = refs->next) {
2097 char *name = refs->name;
2098 int namelen = strlen(name);
2099 if (namelen < patlen ||
2100 memcmp(name + namelen - patlen, pattern, patlen))
2101 continue;
2102 if (namelen != patlen && name[namelen - patlen - 1] != '/')
2103 continue;
2104 match++;
2105 remote_ref = refs;
2107 if (match == 0)
2108 return error("No remote branch matches %s", pattern);
2109 if (match != 1)
2110 return error("More than one remote branch matches %s",
2111 pattern);
2114 * Remote HEAD must be a symref (not exactly foolproof; a remote
2115 * symlink to a symref will look like a symref)
2117 fetch_symref("HEAD", &symref, head_sha1);
2118 if (!symref)
2119 return error("Remote HEAD is not a symref");
2121 /* Remote branch must not be the remote HEAD */
2122 for (i=0; symref && i<MAXDEPTH; i++) {
2123 if (!strcmp(remote_ref->name, symref))
2124 return error("Remote branch %s is the current HEAD",
2125 remote_ref->name);
2126 fetch_symref(symref, &symref, head_sha1);
2129 /* Run extra sanity checks if delete is not forced */
2130 if (!force) {
2131 /* Remote HEAD must resolve to a known object */
2132 if (symref)
2133 return error("Remote HEAD symrefs too deep");
2134 if (is_zero_sha1(head_sha1))
2135 return error("Unable to resolve remote HEAD");
2136 if (!has_sha1_file(head_sha1))
2137 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1));
2139 /* Remote branch must resolve to a known object */
2140 if (is_zero_sha1(remote_ref->old_sha1))
2141 return error("Unable to resolve remote branch %s",
2142 remote_ref->name);
2143 if (!has_sha1_file(remote_ref->old_sha1))
2144 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));
2146 /* Remote branch must be an ancestor of remote HEAD */
2147 if (!verify_merge_base(head_sha1, remote_ref->old_sha1)) {
2148 return error("The branch '%s' is not an ancestor "
2149 "of your current HEAD.\n"
2150 "If you are sure you want to delete it,"
2151 " run:\n\t'git http-push -D %s %s'",
2152 remote_ref->name, remote->url, pattern);
2156 /* Send delete request */
2157 fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name);
2158 if (dry_run)
2159 return 0;
2160 url = xmalloc(strlen(remote->url) + strlen(remote_ref->name) + 1);
2161 sprintf(url, "%s%s", remote->url, remote_ref->name);
2162 slot = get_active_slot();
2163 slot->results = &results;
2164 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
2165 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
2166 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2167 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_DELETE);
2168 if (start_active_slot(slot)) {
2169 run_active_slot(slot);
2170 free(url);
2171 if (results.curl_result != CURLE_OK)
2172 return error("DELETE request failed (%d/%ld)\n",
2173 results.curl_result, results.http_code);
2174 } else {
2175 free(url);
2176 return error("Unable to start DELETE request");
2179 return 0;
2182 int main(int argc, char **argv)
2184 struct transfer_request *request;
2185 struct transfer_request *next_request;
2186 int nr_refspec = 0;
2187 char **refspec = NULL;
2188 struct remote_lock *ref_lock = NULL;
2189 struct remote_lock *info_ref_lock = NULL;
2190 struct rev_info revs;
2191 int delete_branch = 0;
2192 int force_delete = 0;
2193 int objects_to_send;
2194 int rc = 0;
2195 int i;
2196 int new_refs;
2197 struct ref *ref;
2198 char *rewritten_url = NULL;
2200 setup_git_directory();
2202 remote = xcalloc(sizeof(*remote), 1);
2204 argv++;
2205 for (i = 1; i < argc; i++, argv++) {
2206 char *arg = *argv;
2208 if (*arg == '-') {
2209 if (!strcmp(arg, "--all")) {
2210 push_all = MATCH_REFS_ALL;
2211 continue;
2213 if (!strcmp(arg, "--force")) {
2214 force_all = 1;
2215 continue;
2217 if (!strcmp(arg, "--dry-run")) {
2218 dry_run = 1;
2219 continue;
2221 if (!strcmp(arg, "--verbose")) {
2222 push_verbosely = 1;
2223 continue;
2225 if (!strcmp(arg, "-d")) {
2226 delete_branch = 1;
2227 continue;
2229 if (!strcmp(arg, "-D")) {
2230 delete_branch = 1;
2231 force_delete = 1;
2232 continue;
2235 if (!remote->url) {
2236 char *path = strstr(arg, "//");
2237 remote->url = arg;
2238 remote->path_len = strlen(arg);
2239 if (path) {
2240 remote->path = strchr(path+2, '/');
2241 if (remote->path)
2242 remote->path_len = strlen(remote->path);
2244 continue;
2246 refspec = argv;
2247 nr_refspec = argc - i;
2248 break;
2251 #ifndef USE_CURL_MULTI
2252 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
2253 #endif
2255 if (!remote->url)
2256 usage(http_push_usage);
2258 if (delete_branch && nr_refspec != 1)
2259 die("You must specify only one branch name when deleting a remote branch");
2261 memset(remote_dir_exists, -1, 256);
2263 http_init(NULL);
2265 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
2267 if (remote->url && remote->url[strlen(remote->url)-1] != '/') {
2268 rewritten_url = xmalloc(strlen(remote->url)+2);
2269 strcpy(rewritten_url, remote->url);
2270 strcat(rewritten_url, "/");
2271 remote->path = rewritten_url + (remote->path - remote->url);
2272 remote->path_len++;
2273 remote->url = rewritten_url;
2276 /* Verify DAV compliance/lock support */
2277 if (!locking_available()) {
2278 rc = 1;
2279 goto cleanup;
2282 signal(SIGINT, remove_locks_on_signal);
2283 signal(SIGHUP, remove_locks_on_signal);
2284 signal(SIGQUIT, remove_locks_on_signal);
2285 signal(SIGTERM, remove_locks_on_signal);
2287 /* Check whether the remote has server info files */
2288 remote->can_update_info_refs = 0;
2289 remote->has_info_refs = remote_exists("info/refs");
2290 remote->has_info_packs = remote_exists("objects/info/packs");
2291 if (remote->has_info_refs) {
2292 info_ref_lock = lock_remote("info/refs", LOCK_TIME);
2293 if (info_ref_lock)
2294 remote->can_update_info_refs = 1;
2295 else {
2296 fprintf(stderr, "Error: cannot lock existing info/refs\n");
2297 rc = 1;
2298 goto cleanup;
2301 if (remote->has_info_packs)
2302 fetch_indices();
2304 /* Get a list of all local and remote heads to validate refspecs */
2305 get_local_heads();
2306 fprintf(stderr, "Fetching remote heads...\n");
2307 get_dav_remote_heads();
2309 /* Remove a remote branch if -d or -D was specified */
2310 if (delete_branch) {
2311 if (delete_remote_branch(refspec[0], force_delete) == -1)
2312 fprintf(stderr, "Unable to delete remote branch %s\n",
2313 refspec[0]);
2314 goto cleanup;
2317 /* match them up */
2318 if (!remote_tail)
2319 remote_tail = &remote_refs;
2320 if (match_refs(local_refs, remote_refs, &remote_tail,
2321 nr_refspec, (const char **) refspec, push_all)) {
2322 rc = -1;
2323 goto cleanup;
2325 if (!remote_refs) {
2326 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
2327 rc = 0;
2328 goto cleanup;
2331 new_refs = 0;
2332 for (ref = remote_refs; ref; ref = ref->next) {
2333 char old_hex[60], *new_hex;
2334 const char *commit_argv[4];
2335 int commit_argc;
2336 char *new_sha1_hex, *old_sha1_hex;
2338 if (!ref->peer_ref)
2339 continue;
2341 if (is_zero_sha1(ref->peer_ref->new_sha1)) {
2342 if (delete_remote_branch(ref->name, 1) == -1) {
2343 error("Could not remove %s", ref->name);
2344 rc = -4;
2346 new_refs++;
2347 continue;
2350 if (!hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
2351 if (push_verbosely || 1)
2352 fprintf(stderr, "'%s': up-to-date\n", ref->name);
2353 continue;
2356 if (!force_all &&
2357 !is_zero_sha1(ref->old_sha1) &&
2358 !ref->force) {
2359 if (!has_sha1_file(ref->old_sha1) ||
2360 !ref_newer(ref->peer_ref->new_sha1,
2361 ref->old_sha1)) {
2363 * We do not have the remote ref, or
2364 * we know that the remote ref is not
2365 * an ancestor of what we are trying to
2366 * push. Either way this can be losing
2367 * commits at the remote end and likely
2368 * we were not up to date to begin with.
2370 error("remote '%s' is not an ancestor of\n"
2371 "local '%s'.\n"
2372 "Maybe you are not up-to-date and "
2373 "need to pull first?",
2374 ref->name,
2375 ref->peer_ref->name);
2376 rc = -2;
2377 continue;
2380 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
2381 new_refs++;
2382 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
2383 new_hex = sha1_to_hex(ref->new_sha1);
2385 fprintf(stderr, "updating '%s'", ref->name);
2386 if (strcmp(ref->name, ref->peer_ref->name))
2387 fprintf(stderr, " using '%s'", ref->peer_ref->name);
2388 fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex);
2389 if (dry_run)
2390 continue;
2392 /* Lock remote branch ref */
2393 ref_lock = lock_remote(ref->name, LOCK_TIME);
2394 if (ref_lock == NULL) {
2395 fprintf(stderr, "Unable to lock remote branch %s\n",
2396 ref->name);
2397 rc = 1;
2398 continue;
2401 /* Set up revision info for this refspec */
2402 commit_argc = 3;
2403 new_sha1_hex = xstrdup(sha1_to_hex(ref->new_sha1));
2404 old_sha1_hex = NULL;
2405 commit_argv[1] = "--objects";
2406 commit_argv[2] = new_sha1_hex;
2407 if (!push_all && !is_zero_sha1(ref->old_sha1)) {
2408 old_sha1_hex = xmalloc(42);
2409 sprintf(old_sha1_hex, "^%s",
2410 sha1_to_hex(ref->old_sha1));
2411 commit_argv[3] = old_sha1_hex;
2412 commit_argc++;
2414 init_revisions(&revs, setup_git_directory());
2415 setup_revisions(commit_argc, commit_argv, &revs, NULL);
2416 revs.edge_hint = 0; /* just in case */
2417 free(new_sha1_hex);
2418 if (old_sha1_hex) {
2419 free(old_sha1_hex);
2420 commit_argv[1] = NULL;
2423 /* Generate a list of objects that need to be pushed */
2424 pushing = 0;
2425 if (prepare_revision_walk(&revs))
2426 die("revision walk setup failed");
2427 mark_edges_uninteresting(revs.commits, &revs, NULL);
2428 objects_to_send = get_delta(&revs, ref_lock);
2429 finish_all_active_slots();
2431 /* Push missing objects to remote, this would be a
2432 convenient time to pack them first if appropriate. */
2433 pushing = 1;
2434 if (objects_to_send)
2435 fprintf(stderr, " sending %d objects\n",
2436 objects_to_send);
2437 #ifdef USE_CURL_MULTI
2438 fill_active_slots();
2439 add_fill_function(NULL, fill_active_slot);
2440 #endif
2441 do {
2442 finish_all_active_slots();
2443 #ifdef USE_CURL_MULTI
2444 fill_active_slots();
2445 #endif
2446 } while (request_queue_head && !aborted);
2448 /* Update the remote branch if all went well */
2449 if (aborted || !update_remote(ref->new_sha1, ref_lock))
2450 rc = 1;
2452 if (!rc)
2453 fprintf(stderr, " done\n");
2454 unlock_remote(ref_lock);
2455 check_locks();
2458 /* Update remote server info if appropriate */
2459 if (remote->has_info_refs && new_refs) {
2460 if (info_ref_lock && remote->can_update_info_refs) {
2461 fprintf(stderr, "Updating remote server info\n");
2462 if (!dry_run)
2463 update_remote_info_refs(info_ref_lock);
2464 } else {
2465 fprintf(stderr, "Unable to update server info\n");
2469 cleanup:
2470 free(rewritten_url);
2471 if (info_ref_lock)
2472 unlock_remote(info_ref_lock);
2473 free(remote);
2475 curl_slist_free_all(no_pragma_header);
2477 http_cleanup();
2479 request = request_queue_head;
2480 while (request != NULL) {
2481 next_request = request->next;
2482 release_request(request);
2483 request = next_request;
2486 return rc;