http-push.c: get_remote_object_url() is only used under USE_CURL_MULTI
[git.git] / http-push.c
blob203c0750fe2f26da35782455c82dda7498a72666
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 time_t start_time;
156 long timeout;
157 int refreshing;
158 struct remote_lock *next;
161 /* Flags that control remote_ls processing */
162 #define PROCESS_FILES (1u << 0)
163 #define PROCESS_DIRS (1u << 1)
164 #define RECURSIVE (1u << 2)
166 /* Flags that remote_ls passes to callback functions */
167 #define IS_DIR (1u << 0)
169 struct remote_ls_ctx
171 char *path;
172 void (*userFunc)(struct remote_ls_ctx *ls);
173 void *userData;
174 int flags;
175 char *dentry_name;
176 int dentry_flags;
177 struct remote_ls_ctx *parent;
180 /* get_dav_token_headers options */
181 enum dav_header_flag {
182 DAV_HEADER_IF = (1u << 0),
183 DAV_HEADER_LOCK = (1u << 1),
184 DAV_HEADER_TIMEOUT = (1u << 2)
187 static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum dav_header_flag options)
189 struct strbuf buf = STRBUF_INIT;
190 struct curl_slist *dav_headers = NULL;
192 if (options & DAV_HEADER_IF) {
193 strbuf_addf(&buf, "If: (<%s>)", lock->token);
194 dav_headers = curl_slist_append(dav_headers, buf.buf);
195 strbuf_reset(&buf);
197 if (options & DAV_HEADER_LOCK) {
198 strbuf_addf(&buf, "Lock-Token: <%s>", lock->token);
199 dav_headers = curl_slist_append(dav_headers, buf.buf);
200 strbuf_reset(&buf);
202 if (options & DAV_HEADER_TIMEOUT) {
203 strbuf_addf(&buf, "Timeout: Second-%ld", lock->timeout);
204 dav_headers = curl_slist_append(dav_headers, buf.buf);
205 strbuf_reset(&buf);
207 strbuf_release(&buf);
209 return dav_headers;
212 static void append_remote_object_url(struct strbuf *buf, const char *url,
213 const char *hex,
214 int only_two_digit_prefix)
216 strbuf_addf(buf, "%sobjects/%.*s/", url, 2, hex);
217 if (!only_two_digit_prefix)
218 strbuf_addf(buf, "%s", hex+2);
221 static void finish_request(struct transfer_request *request);
222 static void release_request(struct transfer_request *request);
224 static void process_response(void *callback_data)
226 struct transfer_request *request =
227 (struct transfer_request *)callback_data;
229 finish_request(request);
232 #ifdef USE_CURL_MULTI
234 static char *get_remote_object_url(const char *url, const char *hex, int only_two_digit_prefix)
236 struct strbuf buf = STRBUF_INIT;
237 append_remote_object_url(&buf, url, hex, only_two_digit_prefix);
238 return strbuf_detach(&buf, NULL);
241 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
242 void *data)
244 unsigned char expn[4096];
245 size_t size = eltsize * nmemb;
246 int posn = 0;
247 struct transfer_request *request = (struct transfer_request *)data;
248 do {
249 ssize_t retval = xwrite(request->local_fileno,
250 (char *) ptr + posn, size - posn);
251 if (retval < 0)
252 return posn;
253 posn += retval;
254 } while (posn < size);
256 request->stream.avail_in = size;
257 request->stream.next_in = ptr;
258 do {
259 request->stream.next_out = expn;
260 request->stream.avail_out = sizeof(expn);
261 request->zret = git_inflate(&request->stream, Z_SYNC_FLUSH);
262 git_SHA1_Update(&request->c, expn,
263 sizeof(expn) - request->stream.avail_out);
264 } while (request->stream.avail_in && request->zret == Z_OK);
265 data_received++;
266 return size;
269 static void start_fetch_loose(struct transfer_request *request)
271 char *hex = sha1_to_hex(request->obj->sha1);
272 char *filename;
273 char prevfile[PATH_MAX];
274 char *url;
275 int prevlocal;
276 unsigned char prev_buf[PREV_BUF_SIZE];
277 ssize_t prev_read = 0;
278 long prev_posn = 0;
279 char range[RANGE_HEADER_SIZE];
280 struct curl_slist *range_header = NULL;
281 struct active_request_slot *slot;
283 filename = sha1_file_name(request->obj->sha1);
284 snprintf(request->filename, sizeof(request->filename), "%s", filename);
285 snprintf(request->tmpfile, sizeof(request->tmpfile),
286 "%s.temp", filename);
288 snprintf(prevfile, sizeof(prevfile), "%s.prev", request->filename);
289 unlink(prevfile);
290 rename(request->tmpfile, prevfile);
291 unlink(request->tmpfile);
293 if (request->local_fileno != -1)
294 error("fd leakage in start: %d", request->local_fileno);
295 request->local_fileno = open(request->tmpfile,
296 O_WRONLY | O_CREAT | O_EXCL, 0666);
297 /* This could have failed due to the "lazy directory creation";
298 * try to mkdir the last path component.
300 if (request->local_fileno < 0 && errno == ENOENT) {
301 char *dir = strrchr(request->tmpfile, '/');
302 if (dir) {
303 *dir = 0;
304 mkdir(request->tmpfile, 0777);
305 *dir = '/';
307 request->local_fileno = open(request->tmpfile,
308 O_WRONLY | O_CREAT | O_EXCL, 0666);
311 if (request->local_fileno < 0) {
312 request->state = ABORTED;
313 error("Couldn't create temporary file %s for %s: %s",
314 request->tmpfile, request->filename, strerror(errno));
315 return;
318 memset(&request->stream, 0, sizeof(request->stream));
320 git_inflate_init(&request->stream);
322 git_SHA1_Init(&request->c);
324 url = get_remote_object_url(remote->url, hex, 0);
325 request->url = xstrdup(url);
327 /* If a previous temp file is present, process what was already
328 fetched. */
329 prevlocal = open(prevfile, O_RDONLY);
330 if (prevlocal != -1) {
331 do {
332 prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE);
333 if (prev_read>0) {
334 if (fwrite_sha1_file(prev_buf,
336 prev_read,
337 request) == prev_read) {
338 prev_posn += prev_read;
339 } else {
340 prev_read = -1;
343 } while (prev_read > 0);
344 close(prevlocal);
346 unlink(prevfile);
348 /* Reset inflate/SHA1 if there was an error reading the previous temp
349 file; also rewind to the beginning of the local file. */
350 if (prev_read == -1) {
351 memset(&request->stream, 0, sizeof(request->stream));
352 git_inflate_init(&request->stream);
353 git_SHA1_Init(&request->c);
354 if (prev_posn>0) {
355 prev_posn = 0;
356 lseek(request->local_fileno, 0, SEEK_SET);
357 ftruncate(request->local_fileno, 0);
361 slot = get_active_slot();
362 slot->callback_func = process_response;
363 slot->callback_data = request;
364 request->slot = slot;
366 curl_easy_setopt(slot->curl, CURLOPT_FILE, request);
367 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
368 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
369 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
370 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
372 /* If we have successfully processed data from a previous fetch
373 attempt, only fetch the data we don't already have. */
374 if (prev_posn>0) {
375 if (push_verbosely)
376 fprintf(stderr,
377 "Resuming fetch of object %s at byte %ld\n",
378 hex, prev_posn);
379 sprintf(range, "Range: bytes=%ld-", prev_posn);
380 range_header = curl_slist_append(range_header, range);
381 curl_easy_setopt(slot->curl,
382 CURLOPT_HTTPHEADER, range_header);
385 /* Try to get the request started, abort the request on error */
386 request->state = RUN_FETCH_LOOSE;
387 if (!start_active_slot(slot)) {
388 fprintf(stderr, "Unable to start GET request\n");
389 remote->can_update_info_refs = 0;
390 release_request(request);
394 static void start_mkcol(struct transfer_request *request)
396 char *hex = sha1_to_hex(request->obj->sha1);
397 struct active_request_slot *slot;
399 request->url = get_remote_object_url(remote->url, hex, 1);
401 slot = get_active_slot();
402 slot->callback_func = process_response;
403 slot->callback_data = request;
404 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
405 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
406 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
407 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
408 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
410 if (start_active_slot(slot)) {
411 request->slot = slot;
412 request->state = RUN_MKCOL;
413 } else {
414 request->state = ABORTED;
415 free(request->url);
416 request->url = NULL;
419 #endif
421 static void start_fetch_packed(struct transfer_request *request)
423 char *url;
424 struct packed_git *target;
425 FILE *packfile;
426 char *filename;
427 long prev_posn = 0;
428 char range[RANGE_HEADER_SIZE];
429 struct curl_slist *range_header = NULL;
431 struct transfer_request *check_request = request_queue_head;
432 struct active_request_slot *slot;
434 target = find_sha1_pack(request->obj->sha1, remote->packs);
435 if (!target) {
436 fprintf(stderr, "Unable to fetch %s, will not be able to update server info refs\n", sha1_to_hex(request->obj->sha1));
437 remote->can_update_info_refs = 0;
438 release_request(request);
439 return;
442 fprintf(stderr, "Fetching pack %s\n", sha1_to_hex(target->sha1));
443 fprintf(stderr, " which contains %s\n", sha1_to_hex(request->obj->sha1));
445 filename = sha1_pack_name(target->sha1);
446 snprintf(request->filename, sizeof(request->filename), "%s", filename);
447 snprintf(request->tmpfile, sizeof(request->tmpfile),
448 "%s.temp", filename);
450 url = xmalloc(strlen(remote->url) + 64);
451 sprintf(url, "%sobjects/pack/pack-%s.pack",
452 remote->url, sha1_to_hex(target->sha1));
454 /* Make sure there isn't another open request for this pack */
455 while (check_request) {
456 if (check_request->state == RUN_FETCH_PACKED &&
457 !strcmp(check_request->url, url)) {
458 free(url);
459 release_request(request);
460 return;
462 check_request = check_request->next;
465 packfile = fopen(request->tmpfile, "a");
466 if (!packfile) {
467 fprintf(stderr, "Unable to open local file %s for pack",
468 request->tmpfile);
469 remote->can_update_info_refs = 0;
470 free(url);
471 return;
474 slot = get_active_slot();
475 slot->callback_func = process_response;
476 slot->callback_data = request;
477 request->slot = slot;
478 request->local_stream = packfile;
479 request->userData = target;
481 request->url = url;
482 curl_easy_setopt(slot->curl, CURLOPT_FILE, packfile);
483 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
484 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
485 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
486 slot->local = packfile;
488 /* If there is data present from a previous transfer attempt,
489 resume where it left off */
490 prev_posn = ftell(packfile);
491 if (prev_posn>0) {
492 if (push_verbosely)
493 fprintf(stderr,
494 "Resuming fetch of pack %s at byte %ld\n",
495 sha1_to_hex(target->sha1), prev_posn);
496 sprintf(range, "Range: bytes=%ld-", prev_posn);
497 range_header = curl_slist_append(range_header, range);
498 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
501 /* Try to get the request started, abort the request on error */
502 request->state = RUN_FETCH_PACKED;
503 if (!start_active_slot(slot)) {
504 fprintf(stderr, "Unable to start GET request\n");
505 remote->can_update_info_refs = 0;
506 release_request(request);
510 static void start_put(struct transfer_request *request)
512 char *hex = sha1_to_hex(request->obj->sha1);
513 struct active_request_slot *slot;
514 struct strbuf buf = STRBUF_INIT;
515 enum object_type type;
516 char hdr[50];
517 void *unpacked;
518 unsigned long len;
519 int hdrlen;
520 ssize_t size;
521 z_stream stream;
523 unpacked = read_sha1_file(request->obj->sha1, &type, &len);
524 hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
526 /* Set it up */
527 memset(&stream, 0, sizeof(stream));
528 deflateInit(&stream, zlib_compression_level);
529 size = deflateBound(&stream, len + hdrlen);
530 strbuf_init(&request->buffer.buf, size);
531 request->buffer.posn = 0;
533 /* Compress it */
534 stream.next_out = (unsigned char *)request->buffer.buf.buf;
535 stream.avail_out = size;
537 /* First header.. */
538 stream.next_in = (void *)hdr;
539 stream.avail_in = hdrlen;
540 while (deflate(&stream, 0) == Z_OK)
541 /* nothing */;
543 /* Then the data itself.. */
544 stream.next_in = unpacked;
545 stream.avail_in = len;
546 while (deflate(&stream, Z_FINISH) == Z_OK)
547 /* nothing */;
548 deflateEnd(&stream);
549 free(unpacked);
551 request->buffer.buf.len = stream.total_out;
553 strbuf_addstr(&buf, "Destination: ");
554 append_remote_object_url(&buf, remote->url, hex, 0);
555 request->dest = strbuf_detach(&buf, NULL);
557 append_remote_object_url(&buf, remote->url, hex, 0);
558 strbuf_addstr(&buf, request->lock->token);
559 request->url = strbuf_detach(&buf, NULL);
561 slot = get_active_slot();
562 slot->callback_func = process_response;
563 slot->callback_data = request;
564 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &request->buffer);
565 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, request->buffer.buf.len);
566 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
567 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
568 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
569 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
570 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
571 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
572 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
574 if (start_active_slot(slot)) {
575 request->slot = slot;
576 request->state = RUN_PUT;
577 } else {
578 request->state = ABORTED;
579 free(request->url);
580 request->url = NULL;
584 static void start_move(struct transfer_request *request)
586 struct active_request_slot *slot;
587 struct curl_slist *dav_headers = NULL;
589 slot = get_active_slot();
590 slot->callback_func = process_response;
591 slot->callback_data = request;
592 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
593 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MOVE);
594 dav_headers = curl_slist_append(dav_headers, request->dest);
595 dav_headers = curl_slist_append(dav_headers, "Overwrite: T");
596 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
597 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
598 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
600 if (start_active_slot(slot)) {
601 request->slot = slot;
602 request->state = RUN_MOVE;
603 } else {
604 request->state = ABORTED;
605 free(request->url);
606 request->url = NULL;
610 static int refresh_lock(struct remote_lock *lock)
612 struct active_request_slot *slot;
613 struct slot_results results;
614 struct curl_slist *dav_headers;
615 int rc = 0;
617 lock->refreshing = 1;
619 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF | DAV_HEADER_TIMEOUT);
621 slot = get_active_slot();
622 slot->results = &results;
623 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
624 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
625 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
626 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
627 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
629 if (start_active_slot(slot)) {
630 run_active_slot(slot);
631 if (results.curl_result != CURLE_OK) {
632 fprintf(stderr, "LOCK HTTP error %ld\n",
633 results.http_code);
634 } else {
635 lock->start_time = time(NULL);
636 rc = 1;
640 lock->refreshing = 0;
641 curl_slist_free_all(dav_headers);
643 return rc;
646 static void check_locks(void)
648 struct remote_lock *lock = remote->locks;
649 time_t current_time = time(NULL);
650 int time_remaining;
652 while (lock) {
653 time_remaining = lock->start_time + lock->timeout -
654 current_time;
655 if (!lock->refreshing && time_remaining < LOCK_REFRESH) {
656 if (!refresh_lock(lock)) {
657 fprintf(stderr,
658 "Unable to refresh lock for %s\n",
659 lock->url);
660 aborted = 1;
661 return;
664 lock = lock->next;
668 static void release_request(struct transfer_request *request)
670 struct transfer_request *entry = request_queue_head;
672 if (request == request_queue_head) {
673 request_queue_head = request->next;
674 } else {
675 while (entry->next != NULL && entry->next != request)
676 entry = entry->next;
677 if (entry->next == request)
678 entry->next = entry->next->next;
681 if (request->local_fileno != -1)
682 close(request->local_fileno);
683 if (request->local_stream)
684 fclose(request->local_stream);
685 free(request->url);
686 free(request);
689 static void finish_request(struct transfer_request *request)
691 struct stat st;
692 struct packed_git *target;
693 struct packed_git **lst;
695 request->curl_result = request->slot->curl_result;
696 request->http_code = request->slot->http_code;
697 request->slot = NULL;
699 /* Keep locks active */
700 check_locks();
702 if (request->headers != NULL)
703 curl_slist_free_all(request->headers);
705 /* URL is reused for MOVE after PUT */
706 if (request->state != RUN_PUT) {
707 free(request->url);
708 request->url = NULL;
711 if (request->state == RUN_MKCOL) {
712 if (request->curl_result == CURLE_OK ||
713 request->http_code == 405) {
714 remote_dir_exists[request->obj->sha1[0]] = 1;
715 start_put(request);
716 } else {
717 fprintf(stderr, "MKCOL %s failed, aborting (%d/%ld)\n",
718 sha1_to_hex(request->obj->sha1),
719 request->curl_result, request->http_code);
720 request->state = ABORTED;
721 aborted = 1;
723 } else if (request->state == RUN_PUT) {
724 if (request->curl_result == CURLE_OK) {
725 start_move(request);
726 } else {
727 fprintf(stderr, "PUT %s failed, aborting (%d/%ld)\n",
728 sha1_to_hex(request->obj->sha1),
729 request->curl_result, request->http_code);
730 request->state = ABORTED;
731 aborted = 1;
733 } else if (request->state == RUN_MOVE) {
734 if (request->curl_result == CURLE_OK) {
735 if (push_verbosely)
736 fprintf(stderr, " sent %s\n",
737 sha1_to_hex(request->obj->sha1));
738 request->obj->flags |= REMOTE;
739 release_request(request);
740 } else {
741 fprintf(stderr, "MOVE %s failed, aborting (%d/%ld)\n",
742 sha1_to_hex(request->obj->sha1),
743 request->curl_result, request->http_code);
744 request->state = ABORTED;
745 aborted = 1;
747 } else if (request->state == RUN_FETCH_LOOSE) {
748 fchmod(request->local_fileno, 0444);
749 close(request->local_fileno); request->local_fileno = -1;
751 if (request->curl_result != CURLE_OK &&
752 request->http_code != 416) {
753 if (stat(request->tmpfile, &st) == 0) {
754 if (st.st_size == 0)
755 unlink(request->tmpfile);
757 } else {
758 if (request->http_code == 416)
759 fprintf(stderr, "Warning: requested range invalid; we may already have all the data.\n");
761 git_inflate_end(&request->stream);
762 git_SHA1_Final(request->real_sha1, &request->c);
763 if (request->zret != Z_STREAM_END) {
764 unlink(request->tmpfile);
765 } else if (hashcmp(request->obj->sha1, request->real_sha1)) {
766 unlink(request->tmpfile);
767 } else {
768 request->rename =
769 move_temp_to_file(
770 request->tmpfile,
771 request->filename);
772 if (request->rename == 0) {
773 request->obj->flags |= (LOCAL | REMOTE);
778 /* Try fetching packed if necessary */
779 if (request->obj->flags & LOCAL)
780 release_request(request);
781 else
782 start_fetch_packed(request);
784 } else if (request->state == RUN_FETCH_PACKED) {
785 if (request->curl_result != CURLE_OK) {
786 fprintf(stderr, "Unable to get pack file %s\n%s",
787 request->url, curl_errorstr);
788 remote->can_update_info_refs = 0;
789 } else {
790 off_t pack_size = ftell(request->local_stream);
792 fclose(request->local_stream);
793 request->local_stream = NULL;
794 if (!move_temp_to_file(request->tmpfile,
795 request->filename)) {
796 target = (struct packed_git *)request->userData;
797 target->pack_size = pack_size;
798 lst = &remote->packs;
799 while (*lst != target)
800 lst = &((*lst)->next);
801 *lst = (*lst)->next;
803 if (!verify_pack(target))
804 install_packed_git(target);
805 else
806 remote->can_update_info_refs = 0;
809 release_request(request);
813 #ifdef USE_CURL_MULTI
814 static int fill_active_slot(void *unused)
816 struct transfer_request *request = request_queue_head;
818 if (aborted)
819 return 0;
821 for (request = request_queue_head; request; request = request->next) {
822 if (request->state == NEED_FETCH) {
823 start_fetch_loose(request);
824 return 1;
825 } else if (pushing && request->state == NEED_PUSH) {
826 if (remote_dir_exists[request->obj->sha1[0]] == 1) {
827 start_put(request);
828 } else {
829 start_mkcol(request);
831 return 1;
834 return 0;
836 #endif
838 static void get_remote_object_list(unsigned char parent);
840 static void add_fetch_request(struct object *obj)
842 struct transfer_request *request;
844 check_locks();
847 * Don't fetch the object if it's known to exist locally
848 * or is already in the request queue
850 if (remote_dir_exists[obj->sha1[0]] == -1)
851 get_remote_object_list(obj->sha1[0]);
852 if (obj->flags & (LOCAL | FETCHING))
853 return;
855 obj->flags |= FETCHING;
856 request = xmalloc(sizeof(*request));
857 request->obj = obj;
858 request->url = NULL;
859 request->lock = NULL;
860 request->headers = NULL;
861 request->local_fileno = -1;
862 request->local_stream = NULL;
863 request->state = NEED_FETCH;
864 request->next = request_queue_head;
865 request_queue_head = request;
867 #ifdef USE_CURL_MULTI
868 fill_active_slots();
869 step_active_slots();
870 #endif
873 static int add_send_request(struct object *obj, struct remote_lock *lock)
875 struct transfer_request *request = request_queue_head;
876 struct packed_git *target;
878 /* Keep locks active */
879 check_locks();
882 * Don't push the object if it's known to exist on the remote
883 * or is already in the request queue
885 if (remote_dir_exists[obj->sha1[0]] == -1)
886 get_remote_object_list(obj->sha1[0]);
887 if (obj->flags & (REMOTE | PUSHING))
888 return 0;
889 target = find_sha1_pack(obj->sha1, remote->packs);
890 if (target) {
891 obj->flags |= REMOTE;
892 return 0;
895 obj->flags |= PUSHING;
896 request = xmalloc(sizeof(*request));
897 request->obj = obj;
898 request->url = NULL;
899 request->lock = lock;
900 request->headers = NULL;
901 request->local_fileno = -1;
902 request->local_stream = NULL;
903 request->state = NEED_PUSH;
904 request->next = request_queue_head;
905 request_queue_head = request;
907 #ifdef USE_CURL_MULTI
908 fill_active_slots();
909 step_active_slots();
910 #endif
912 return 1;
915 static int fetch_index(unsigned char *sha1)
917 char *hex = sha1_to_hex(sha1);
918 char *filename;
919 char *url;
920 char tmpfile[PATH_MAX];
921 long prev_posn = 0;
922 char range[RANGE_HEADER_SIZE];
923 struct curl_slist *range_header = NULL;
925 FILE *indexfile;
926 struct active_request_slot *slot;
927 struct slot_results results;
929 /* Don't use the index if the pack isn't there */
930 url = xmalloc(strlen(remote->url) + 64);
931 sprintf(url, "%sobjects/pack/pack-%s.pack", remote->url, hex);
932 slot = get_active_slot();
933 slot->results = &results;
934 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
935 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
936 if (start_active_slot(slot)) {
937 run_active_slot(slot);
938 if (results.curl_result != CURLE_OK) {
939 free(url);
940 return error("Unable to verify pack %s is available",
941 hex);
943 } else {
944 free(url);
945 return error("Unable to start request");
948 if (has_pack_index(sha1)) {
949 free(url);
950 return 0;
953 if (push_verbosely)
954 fprintf(stderr, "Getting index for pack %s\n", hex);
956 sprintf(url, "%sobjects/pack/pack-%s.idx", remote->url, hex);
958 filename = sha1_pack_index_name(sha1);
959 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
960 indexfile = fopen(tmpfile, "a");
961 if (!indexfile) {
962 free(url);
963 return error("Unable to open local file %s for pack index",
964 tmpfile);
967 slot = get_active_slot();
968 slot->results = &results;
969 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
970 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
971 curl_easy_setopt(slot->curl, CURLOPT_FILE, indexfile);
972 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
973 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
974 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
975 slot->local = indexfile;
977 /* If there is data present from a previous transfer attempt,
978 resume where it left off */
979 prev_posn = ftell(indexfile);
980 if (prev_posn>0) {
981 if (push_verbosely)
982 fprintf(stderr,
983 "Resuming fetch of index for pack %s at byte %ld\n",
984 hex, prev_posn);
985 sprintf(range, "Range: bytes=%ld-", prev_posn);
986 range_header = curl_slist_append(range_header, range);
987 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
990 if (start_active_slot(slot)) {
991 run_active_slot(slot);
992 if (results.curl_result != CURLE_OK) {
993 free(url);
994 fclose(indexfile);
995 return error("Unable to get pack index %s\n%s", url,
996 curl_errorstr);
998 } else {
999 free(url);
1000 fclose(indexfile);
1001 return error("Unable to start request");
1004 free(url);
1005 fclose(indexfile);
1007 return move_temp_to_file(tmpfile, filename);
1010 static int setup_index(unsigned char *sha1)
1012 struct packed_git *new_pack;
1014 if (fetch_index(sha1))
1015 return -1;
1017 new_pack = parse_pack_index(sha1);
1018 new_pack->next = remote->packs;
1019 remote->packs = new_pack;
1020 return 0;
1023 static int fetch_indices(void)
1025 unsigned char sha1[20];
1026 char *url;
1027 struct strbuf buffer = STRBUF_INIT;
1028 char *data;
1029 int i = 0;
1031 struct active_request_slot *slot;
1032 struct slot_results results;
1034 if (push_verbosely)
1035 fprintf(stderr, "Getting pack list\n");
1037 url = xmalloc(strlen(remote->url) + 20);
1038 sprintf(url, "%sobjects/info/packs", remote->url);
1040 slot = get_active_slot();
1041 slot->results = &results;
1042 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
1043 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1044 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1045 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
1046 if (start_active_slot(slot)) {
1047 run_active_slot(slot);
1048 if (results.curl_result != CURLE_OK) {
1049 strbuf_release(&buffer);
1050 free(url);
1051 if (results.http_code == 404)
1052 return 0;
1053 else
1054 return error("%s", curl_errorstr);
1056 } else {
1057 strbuf_release(&buffer);
1058 free(url);
1059 return error("Unable to start request");
1061 free(url);
1063 data = buffer.buf;
1064 while (i < buffer.len) {
1065 switch (data[i]) {
1066 case 'P':
1067 i++;
1068 if (i + 52 < buffer.len &&
1069 !prefixcmp(data + i, " pack-") &&
1070 !prefixcmp(data + i + 46, ".pack\n")) {
1071 get_sha1_hex(data + i + 6, sha1);
1072 setup_index(sha1);
1073 i += 51;
1074 break;
1076 default:
1077 while (data[i] != '\n')
1078 i++;
1080 i++;
1083 strbuf_release(&buffer);
1084 return 0;
1087 static void one_remote_object(const char *hex)
1089 unsigned char sha1[20];
1090 struct object *obj;
1092 if (get_sha1_hex(hex, sha1) != 0)
1093 return;
1095 obj = lookup_object(sha1);
1096 if (!obj)
1097 obj = parse_object(sha1);
1099 /* Ignore remote objects that don't exist locally */
1100 if (!obj)
1101 return;
1103 obj->flags |= REMOTE;
1104 if (!object_list_contains(objects, obj))
1105 object_list_insert(obj, &objects);
1108 static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed)
1110 int *lock_flags = (int *)ctx->userData;
1112 if (tag_closed) {
1113 if (!strcmp(ctx->name, DAV_CTX_LOCKENTRY)) {
1114 if ((*lock_flags & DAV_PROP_LOCKEX) &&
1115 (*lock_flags & DAV_PROP_LOCKWR)) {
1116 *lock_flags |= DAV_LOCK_OK;
1118 *lock_flags &= DAV_LOCK_OK;
1119 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_WRITE)) {
1120 *lock_flags |= DAV_PROP_LOCKWR;
1121 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_EXCLUSIVE)) {
1122 *lock_flags |= DAV_PROP_LOCKEX;
1127 static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
1129 struct remote_lock *lock = (struct remote_lock *)ctx->userData;
1131 if (tag_closed && ctx->cdata) {
1132 if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) {
1133 lock->owner = xmalloc(strlen(ctx->cdata) + 1);
1134 strcpy(lock->owner, ctx->cdata);
1135 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
1136 if (!prefixcmp(ctx->cdata, "Second-"))
1137 lock->timeout =
1138 strtol(ctx->cdata + 7, NULL, 10);
1139 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
1140 lock->token = xmalloc(strlen(ctx->cdata) + 1);
1141 strcpy(lock->token, ctx->cdata);
1146 static void one_remote_ref(char *refname);
1148 static void
1149 xml_start_tag(void *userData, const char *name, const char **atts)
1151 struct xml_ctx *ctx = (struct xml_ctx *)userData;
1152 const char *c = strchr(name, ':');
1153 int new_len;
1155 if (c == NULL)
1156 c = name;
1157 else
1158 c++;
1160 new_len = strlen(ctx->name) + strlen(c) + 2;
1162 if (new_len > ctx->len) {
1163 ctx->name = xrealloc(ctx->name, new_len);
1164 ctx->len = new_len;
1166 strcat(ctx->name, ".");
1167 strcat(ctx->name, c);
1169 free(ctx->cdata);
1170 ctx->cdata = NULL;
1172 ctx->userFunc(ctx, 0);
1175 static void
1176 xml_end_tag(void *userData, const char *name)
1178 struct xml_ctx *ctx = (struct xml_ctx *)userData;
1179 const char *c = strchr(name, ':');
1180 char *ep;
1182 ctx->userFunc(ctx, 1);
1184 if (c == NULL)
1185 c = name;
1186 else
1187 c++;
1189 ep = ctx->name + strlen(ctx->name) - strlen(c) - 1;
1190 *ep = 0;
1193 static void
1194 xml_cdata(void *userData, const XML_Char *s, int len)
1196 struct xml_ctx *ctx = (struct xml_ctx *)userData;
1197 free(ctx->cdata);
1198 ctx->cdata = xmemdupz(s, len);
1201 static struct remote_lock *lock_remote(const char *path, long timeout)
1203 struct active_request_slot *slot;
1204 struct slot_results results;
1205 struct buffer out_buffer = { STRBUF_INIT, 0 };
1206 struct strbuf in_buffer = STRBUF_INIT;
1207 char *url;
1208 char *ep;
1209 char timeout_header[25];
1210 struct remote_lock *lock = NULL;
1211 struct curl_slist *dav_headers = NULL;
1212 struct xml_ctx ctx;
1214 url = xmalloc(strlen(remote->url) + strlen(path) + 1);
1215 sprintf(url, "%s%s", remote->url, path);
1217 /* Make sure leading directories exist for the remote ref */
1218 ep = strchr(url + strlen(remote->url) + 1, '/');
1219 while (ep) {
1220 char saved_character = ep[1];
1221 ep[1] = '\0';
1222 slot = get_active_slot();
1223 slot->results = &results;
1224 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
1225 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1226 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
1227 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1228 if (start_active_slot(slot)) {
1229 run_active_slot(slot);
1230 if (results.curl_result != CURLE_OK &&
1231 results.http_code != 405) {
1232 fprintf(stderr,
1233 "Unable to create branch path %s\n",
1234 url);
1235 free(url);
1236 return NULL;
1238 } else {
1239 fprintf(stderr, "Unable to start MKCOL request\n");
1240 free(url);
1241 return NULL;
1243 ep[1] = saved_character;
1244 ep = strchr(ep + 1, '/');
1247 strbuf_addf(&out_buffer.buf, LOCK_REQUEST, git_default_email);
1249 sprintf(timeout_header, "Timeout: Second-%ld", timeout);
1250 dav_headers = curl_slist_append(dav_headers, timeout_header);
1251 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1253 slot = get_active_slot();
1254 slot->results = &results;
1255 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1256 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1257 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1258 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1259 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1260 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1261 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1262 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
1263 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1265 lock = xcalloc(1, sizeof(*lock));
1266 lock->timeout = -1;
1268 if (start_active_slot(slot)) {
1269 run_active_slot(slot);
1270 if (results.curl_result == CURLE_OK) {
1271 XML_Parser parser = XML_ParserCreate(NULL);
1272 enum XML_Status result;
1273 ctx.name = xcalloc(10, 1);
1274 ctx.len = 0;
1275 ctx.cdata = NULL;
1276 ctx.userFunc = handle_new_lock_ctx;
1277 ctx.userData = lock;
1278 XML_SetUserData(parser, &ctx);
1279 XML_SetElementHandler(parser, xml_start_tag,
1280 xml_end_tag);
1281 XML_SetCharacterDataHandler(parser, xml_cdata);
1282 result = XML_Parse(parser, in_buffer.buf,
1283 in_buffer.len, 1);
1284 free(ctx.name);
1285 if (result != XML_STATUS_OK) {
1286 fprintf(stderr, "XML error: %s\n",
1287 XML_ErrorString(
1288 XML_GetErrorCode(parser)));
1289 lock->timeout = -1;
1291 XML_ParserFree(parser);
1293 } else {
1294 fprintf(stderr, "Unable to start LOCK request\n");
1297 curl_slist_free_all(dav_headers);
1298 strbuf_release(&out_buffer.buf);
1299 strbuf_release(&in_buffer);
1301 if (lock->token == NULL || lock->timeout <= 0) {
1302 free(lock->token);
1303 free(lock->owner);
1304 free(url);
1305 free(lock);
1306 lock = NULL;
1307 } else {
1308 lock->url = url;
1309 lock->start_time = time(NULL);
1310 lock->next = remote->locks;
1311 remote->locks = lock;
1314 return lock;
1317 static int unlock_remote(struct remote_lock *lock)
1319 struct active_request_slot *slot;
1320 struct slot_results results;
1321 struct remote_lock *prev = remote->locks;
1322 struct curl_slist *dav_headers;
1323 int rc = 0;
1325 dav_headers = get_dav_token_headers(lock, DAV_HEADER_LOCK);
1327 slot = get_active_slot();
1328 slot->results = &results;
1329 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1330 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1331 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_UNLOCK);
1332 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1334 if (start_active_slot(slot)) {
1335 run_active_slot(slot);
1336 if (results.curl_result == CURLE_OK)
1337 rc = 1;
1338 else
1339 fprintf(stderr, "UNLOCK HTTP error %ld\n",
1340 results.http_code);
1341 } else {
1342 fprintf(stderr, "Unable to start UNLOCK request\n");
1345 curl_slist_free_all(dav_headers);
1347 if (remote->locks == lock) {
1348 remote->locks = lock->next;
1349 } else {
1350 while (prev && prev->next != lock)
1351 prev = prev->next;
1352 if (prev)
1353 prev->next = prev->next->next;
1356 free(lock->owner);
1357 free(lock->url);
1358 free(lock->token);
1359 free(lock);
1361 return rc;
1364 static void remove_locks(void)
1366 struct remote_lock *lock = remote->locks;
1368 fprintf(stderr, "Removing remote locks...\n");
1369 while (lock) {
1370 unlock_remote(lock);
1371 lock = lock->next;
1375 static void remove_locks_on_signal(int signo)
1377 remove_locks();
1378 signal(signo, SIG_DFL);
1379 raise(signo);
1382 static void remote_ls(const char *path, int flags,
1383 void (*userFunc)(struct remote_ls_ctx *ls),
1384 void *userData);
1386 static void process_ls_object(struct remote_ls_ctx *ls)
1388 unsigned int *parent = (unsigned int *)ls->userData;
1389 char *path = ls->dentry_name;
1390 char *obj_hex;
1392 if (!strcmp(ls->path, ls->dentry_name) && (ls->flags & IS_DIR)) {
1393 remote_dir_exists[*parent] = 1;
1394 return;
1397 if (strlen(path) != 49)
1398 return;
1399 path += 8;
1400 obj_hex = xmalloc(strlen(path));
1401 /* NB: path is not null-terminated, can not use strlcpy here */
1402 memcpy(obj_hex, path, 2);
1403 strcpy(obj_hex + 2, path + 3);
1404 one_remote_object(obj_hex);
1405 free(obj_hex);
1408 static void process_ls_ref(struct remote_ls_ctx *ls)
1410 if (!strcmp(ls->path, ls->dentry_name) && (ls->dentry_flags & IS_DIR)) {
1411 fprintf(stderr, " %s\n", ls->dentry_name);
1412 return;
1415 if (!(ls->dentry_flags & IS_DIR))
1416 one_remote_ref(ls->dentry_name);
1419 static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
1421 struct remote_ls_ctx *ls = (struct remote_ls_ctx *)ctx->userData;
1423 if (tag_closed) {
1424 if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) {
1425 if (ls->dentry_flags & IS_DIR) {
1426 if (ls->flags & PROCESS_DIRS) {
1427 ls->userFunc(ls);
1429 if (strcmp(ls->dentry_name, ls->path) &&
1430 ls->flags & RECURSIVE) {
1431 remote_ls(ls->dentry_name,
1432 ls->flags,
1433 ls->userFunc,
1434 ls->userData);
1436 } else if (ls->flags & PROCESS_FILES) {
1437 ls->userFunc(ls);
1439 } else if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
1440 char *path = ctx->cdata;
1441 if (*ctx->cdata == 'h') {
1442 path = strstr(path, "//");
1443 if (path) {
1444 path = strchr(path+2, '/');
1447 if (path) {
1448 path += remote->path_len;
1449 ls->dentry_name = xstrdup(path);
1451 } else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
1452 ls->dentry_flags |= IS_DIR;
1454 } else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
1455 free(ls->dentry_name);
1456 ls->dentry_name = NULL;
1457 ls->dentry_flags = 0;
1462 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1463 * should _only_ heed the information from that file, instead of trying to
1464 * determine the refs from the remote file system (badly: it does not even
1465 * know about packed-refs).
1467 static void remote_ls(const char *path, int flags,
1468 void (*userFunc)(struct remote_ls_ctx *ls),
1469 void *userData)
1471 char *url = xmalloc(strlen(remote->url) + strlen(path) + 1);
1472 struct active_request_slot *slot;
1473 struct slot_results results;
1474 struct strbuf in_buffer = STRBUF_INIT;
1475 struct buffer out_buffer = { STRBUF_INIT, 0 };
1476 struct curl_slist *dav_headers = NULL;
1477 struct xml_ctx ctx;
1478 struct remote_ls_ctx ls;
1480 ls.flags = flags;
1481 ls.path = xstrdup(path);
1482 ls.dentry_name = NULL;
1483 ls.dentry_flags = 0;
1484 ls.userData = userData;
1485 ls.userFunc = userFunc;
1487 sprintf(url, "%s%s", remote->url, path);
1489 strbuf_addf(&out_buffer.buf, PROPFIND_ALL_REQUEST);
1491 dav_headers = curl_slist_append(dav_headers, "Depth: 1");
1492 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1494 slot = get_active_slot();
1495 slot->results = &results;
1496 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1497 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1498 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1499 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1500 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1501 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1502 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1503 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1504 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1506 if (start_active_slot(slot)) {
1507 run_active_slot(slot);
1508 if (results.curl_result == CURLE_OK) {
1509 XML_Parser parser = XML_ParserCreate(NULL);
1510 enum XML_Status result;
1511 ctx.name = xcalloc(10, 1);
1512 ctx.len = 0;
1513 ctx.cdata = NULL;
1514 ctx.userFunc = handle_remote_ls_ctx;
1515 ctx.userData = &ls;
1516 XML_SetUserData(parser, &ctx);
1517 XML_SetElementHandler(parser, xml_start_tag,
1518 xml_end_tag);
1519 XML_SetCharacterDataHandler(parser, xml_cdata);
1520 result = XML_Parse(parser, in_buffer.buf,
1521 in_buffer.len, 1);
1522 free(ctx.name);
1524 if (result != XML_STATUS_OK) {
1525 fprintf(stderr, "XML error: %s\n",
1526 XML_ErrorString(
1527 XML_GetErrorCode(parser)));
1529 XML_ParserFree(parser);
1531 } else {
1532 fprintf(stderr, "Unable to start PROPFIND request\n");
1535 free(ls.path);
1536 free(url);
1537 strbuf_release(&out_buffer.buf);
1538 strbuf_release(&in_buffer);
1539 curl_slist_free_all(dav_headers);
1542 static void get_remote_object_list(unsigned char parent)
1544 char path[] = "objects/XX/";
1545 static const char hex[] = "0123456789abcdef";
1546 unsigned int val = parent;
1548 path[8] = hex[val >> 4];
1549 path[9] = hex[val & 0xf];
1550 remote_dir_exists[val] = 0;
1551 remote_ls(path, (PROCESS_FILES | PROCESS_DIRS),
1552 process_ls_object, &val);
1555 static int locking_available(void)
1557 struct active_request_slot *slot;
1558 struct slot_results results;
1559 struct strbuf in_buffer = STRBUF_INIT;
1560 struct buffer out_buffer = { STRBUF_INIT, 0 };
1561 struct curl_slist *dav_headers = NULL;
1562 struct xml_ctx ctx;
1563 int lock_flags = 0;
1565 strbuf_addf(&out_buffer.buf, PROPFIND_SUPPORTEDLOCK_REQUEST, remote->url);
1567 dav_headers = curl_slist_append(dav_headers, "Depth: 0");
1568 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1570 slot = get_active_slot();
1571 slot->results = &results;
1572 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1573 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1574 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1575 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1576 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1577 curl_easy_setopt(slot->curl, CURLOPT_URL, remote->url);
1578 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1579 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1580 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1582 if (start_active_slot(slot)) {
1583 run_active_slot(slot);
1584 if (results.curl_result == CURLE_OK) {
1585 XML_Parser parser = XML_ParserCreate(NULL);
1586 enum XML_Status result;
1587 ctx.name = xcalloc(10, 1);
1588 ctx.len = 0;
1589 ctx.cdata = NULL;
1590 ctx.userFunc = handle_lockprop_ctx;
1591 ctx.userData = &lock_flags;
1592 XML_SetUserData(parser, &ctx);
1593 XML_SetElementHandler(parser, xml_start_tag,
1594 xml_end_tag);
1595 result = XML_Parse(parser, in_buffer.buf,
1596 in_buffer.len, 1);
1597 free(ctx.name);
1599 if (result != XML_STATUS_OK) {
1600 fprintf(stderr, "XML error: %s\n",
1601 XML_ErrorString(
1602 XML_GetErrorCode(parser)));
1603 lock_flags = 0;
1605 XML_ParserFree(parser);
1606 if (!lock_flags)
1607 error("Error: no DAV locking support on %s",
1608 remote->url);
1610 } else {
1611 error("Cannot access URL %s, return code %d",
1612 remote->url, results.curl_result);
1613 lock_flags = 0;
1615 } else {
1616 error("Unable to start PROPFIND request on %s", remote->url);
1619 strbuf_release(&out_buffer.buf);
1620 strbuf_release(&in_buffer);
1621 curl_slist_free_all(dav_headers);
1623 return lock_flags;
1626 static struct object_list **add_one_object(struct object *obj, struct object_list **p)
1628 struct object_list *entry = xmalloc(sizeof(struct object_list));
1629 entry->item = obj;
1630 entry->next = *p;
1631 *p = entry;
1632 return &entry->next;
1635 static struct object_list **process_blob(struct blob *blob,
1636 struct object_list **p,
1637 struct name_path *path,
1638 const char *name)
1640 struct object *obj = &blob->object;
1642 obj->flags |= LOCAL;
1644 if (obj->flags & (UNINTERESTING | SEEN))
1645 return p;
1647 obj->flags |= SEEN;
1648 return add_one_object(obj, p);
1651 static struct object_list **process_tree(struct tree *tree,
1652 struct object_list **p,
1653 struct name_path *path,
1654 const char *name)
1656 struct object *obj = &tree->object;
1657 struct tree_desc desc;
1658 struct name_entry entry;
1659 struct name_path me;
1661 obj->flags |= LOCAL;
1663 if (obj->flags & (UNINTERESTING | SEEN))
1664 return p;
1665 if (parse_tree(tree) < 0)
1666 die("bad tree object %s", sha1_to_hex(obj->sha1));
1668 obj->flags |= SEEN;
1669 name = xstrdup(name);
1670 p = add_one_object(obj, p);
1671 me.up = path;
1672 me.elem = name;
1673 me.elem_len = strlen(name);
1675 init_tree_desc(&desc, tree->buffer, tree->size);
1677 while (tree_entry(&desc, &entry))
1678 switch (object_type(entry.mode)) {
1679 case OBJ_TREE:
1680 p = process_tree(lookup_tree(entry.sha1), p, &me, name);
1681 break;
1682 case OBJ_BLOB:
1683 p = process_blob(lookup_blob(entry.sha1), p, &me, name);
1684 break;
1685 default:
1686 /* Subproject commit - not in this repository */
1687 break;
1690 free(tree->buffer);
1691 tree->buffer = NULL;
1692 return p;
1695 static int get_delta(struct rev_info *revs, struct remote_lock *lock)
1697 int i;
1698 struct commit *commit;
1699 struct object_list **p = &objects;
1700 int count = 0;
1702 while ((commit = get_revision(revs)) != NULL) {
1703 p = process_tree(commit->tree, p, NULL, "");
1704 commit->object.flags |= LOCAL;
1705 if (!(commit->object.flags & UNINTERESTING))
1706 count += add_send_request(&commit->object, lock);
1709 for (i = 0; i < revs->pending.nr; i++) {
1710 struct object_array_entry *entry = revs->pending.objects + i;
1711 struct object *obj = entry->item;
1712 const char *name = entry->name;
1714 if (obj->flags & (UNINTERESTING | SEEN))
1715 continue;
1716 if (obj->type == OBJ_TAG) {
1717 obj->flags |= SEEN;
1718 p = add_one_object(obj, p);
1719 continue;
1721 if (obj->type == OBJ_TREE) {
1722 p = process_tree((struct tree *)obj, p, NULL, name);
1723 continue;
1725 if (obj->type == OBJ_BLOB) {
1726 p = process_blob((struct blob *)obj, p, NULL, name);
1727 continue;
1729 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
1732 while (objects) {
1733 if (!(objects->item->flags & UNINTERESTING))
1734 count += add_send_request(objects->item, lock);
1735 objects = objects->next;
1738 return count;
1741 static int update_remote(unsigned char *sha1, struct remote_lock *lock)
1743 struct active_request_slot *slot;
1744 struct slot_results results;
1745 struct buffer out_buffer = { STRBUF_INIT, 0 };
1746 struct curl_slist *dav_headers;
1748 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1750 strbuf_addf(&out_buffer.buf, "%s\n", sha1_to_hex(sha1));
1752 slot = get_active_slot();
1753 slot->results = &results;
1754 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1755 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len);
1756 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1757 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1758 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1759 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1760 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1761 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1762 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1764 if (start_active_slot(slot)) {
1765 run_active_slot(slot);
1766 strbuf_release(&out_buffer.buf);
1767 if (results.curl_result != CURLE_OK) {
1768 fprintf(stderr,
1769 "PUT error: curl result=%d, HTTP code=%ld\n",
1770 results.curl_result, results.http_code);
1771 /* We should attempt recovery? */
1772 return 0;
1774 } else {
1775 strbuf_release(&out_buffer.buf);
1776 fprintf(stderr, "Unable to start PUT request\n");
1777 return 0;
1780 return 1;
1783 static struct ref *local_refs, **local_tail;
1784 static struct ref *remote_refs, **remote_tail;
1786 static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
1788 struct ref *ref;
1789 int len = strlen(refname) + 1;
1790 ref = xcalloc(1, sizeof(*ref) + len);
1791 hashcpy(ref->new_sha1, sha1);
1792 memcpy(ref->name, refname, len);
1793 *local_tail = ref;
1794 local_tail = &ref->next;
1795 return 0;
1798 static void one_remote_ref(char *refname)
1800 struct ref *ref;
1801 struct object *obj;
1803 ref = alloc_ref(refname);
1805 if (http_fetch_ref(remote->url, ref) != 0) {
1806 fprintf(stderr,
1807 "Unable to fetch ref %s from %s\n",
1808 refname, remote->url);
1809 free(ref);
1810 return;
1814 * Fetch a copy of the object if it doesn't exist locally - it
1815 * may be required for updating server info later.
1817 if (remote->can_update_info_refs && !has_sha1_file(ref->old_sha1)) {
1818 obj = lookup_unknown_object(ref->old_sha1);
1819 if (obj) {
1820 fprintf(stderr, " fetch %s for %s\n",
1821 sha1_to_hex(ref->old_sha1), refname);
1822 add_fetch_request(obj);
1826 *remote_tail = ref;
1827 remote_tail = &ref->next;
1830 static void get_local_heads(void)
1832 local_tail = &local_refs;
1833 for_each_ref(one_local_ref, NULL);
1836 static void get_dav_remote_heads(void)
1838 remote_tail = &remote_refs;
1839 remote_ls("refs/", (PROCESS_FILES | PROCESS_DIRS | RECURSIVE), process_ls_ref, NULL);
1842 static int is_zero_sha1(const unsigned char *sha1)
1844 int i;
1846 for (i = 0; i < 20; i++) {
1847 if (*sha1++)
1848 return 0;
1850 return 1;
1853 static void unmark_and_free(struct commit_list *list, unsigned int mark)
1855 while (list) {
1856 struct commit_list *temp = list;
1857 temp->item->object.flags &= ~mark;
1858 list = temp->next;
1859 free(temp);
1863 static int ref_newer(const unsigned char *new_sha1,
1864 const unsigned char *old_sha1)
1866 struct object *o;
1867 struct commit *old, *new;
1868 struct commit_list *list, *used;
1869 int found = 0;
1871 /* Both new and old must be commit-ish and new is descendant of
1872 * old. Otherwise we require --force.
1874 o = deref_tag(parse_object(old_sha1), NULL, 0);
1875 if (!o || o->type != OBJ_COMMIT)
1876 return 0;
1877 old = (struct commit *) o;
1879 o = deref_tag(parse_object(new_sha1), NULL, 0);
1880 if (!o || o->type != OBJ_COMMIT)
1881 return 0;
1882 new = (struct commit *) o;
1884 if (parse_commit(new) < 0)
1885 return 0;
1887 used = list = NULL;
1888 commit_list_insert(new, &list);
1889 while (list) {
1890 new = pop_most_recent_commit(&list, TMP_MARK);
1891 commit_list_insert(new, &used);
1892 if (new == old) {
1893 found = 1;
1894 break;
1897 unmark_and_free(list, TMP_MARK);
1898 unmark_and_free(used, TMP_MARK);
1899 return found;
1902 static void add_remote_info_ref(struct remote_ls_ctx *ls)
1904 struct strbuf *buf = (struct strbuf *)ls->userData;
1905 struct object *o;
1906 int len;
1907 char *ref_info;
1908 struct ref *ref;
1910 ref = alloc_ref(ls->dentry_name);
1912 if (http_fetch_ref(remote->url, ref) != 0) {
1913 fprintf(stderr,
1914 "Unable to fetch ref %s from %s\n",
1915 ls->dentry_name, remote->url);
1916 aborted = 1;
1917 free(ref);
1918 return;
1921 o = parse_object(ref->old_sha1);
1922 if (!o) {
1923 fprintf(stderr,
1924 "Unable to parse object %s for remote ref %s\n",
1925 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1926 aborted = 1;
1927 free(ref);
1928 return;
1931 len = strlen(ls->dentry_name) + 42;
1932 ref_info = xcalloc(len + 1, 1);
1933 sprintf(ref_info, "%s %s\n",
1934 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1935 fwrite_buffer(ref_info, 1, len, buf);
1936 free(ref_info);
1938 if (o->type == OBJ_TAG) {
1939 o = deref_tag(o, ls->dentry_name, 0);
1940 if (o) {
1941 len = strlen(ls->dentry_name) + 45;
1942 ref_info = xcalloc(len + 1, 1);
1943 sprintf(ref_info, "%s %s^{}\n",
1944 sha1_to_hex(o->sha1), ls->dentry_name);
1945 fwrite_buffer(ref_info, 1, len, buf);
1946 free(ref_info);
1949 free(ref);
1952 static void update_remote_info_refs(struct remote_lock *lock)
1954 struct buffer buffer = { STRBUF_INIT, 0 };
1955 struct active_request_slot *slot;
1956 struct slot_results results;
1957 struct curl_slist *dav_headers;
1959 remote_ls("refs/", (PROCESS_FILES | RECURSIVE),
1960 add_remote_info_ref, &buffer.buf);
1961 if (!aborted) {
1962 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1964 slot = get_active_slot();
1965 slot->results = &results;
1966 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &buffer);
1967 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, buffer.buf.len);
1968 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1969 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1970 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1971 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1972 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1973 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1974 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1976 if (start_active_slot(slot)) {
1977 run_active_slot(slot);
1978 if (results.curl_result != CURLE_OK) {
1979 fprintf(stderr,
1980 "PUT error: curl result=%d, HTTP code=%ld\n",
1981 results.curl_result, results.http_code);
1985 strbuf_release(&buffer.buf);
1988 static int remote_exists(const char *path)
1990 char *url = xmalloc(strlen(remote->url) + strlen(path) + 1);
1991 struct active_request_slot *slot;
1992 struct slot_results results;
1993 int ret = -1;
1995 sprintf(url, "%s%s", remote->url, path);
1997 slot = get_active_slot();
1998 slot->results = &results;
1999 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2000 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
2002 if (start_active_slot(slot)) {
2003 run_active_slot(slot);
2004 if (results.http_code == 404)
2005 ret = 0;
2006 else if (results.curl_result == CURLE_OK)
2007 ret = 1;
2008 else
2009 fprintf(stderr, "HEAD HTTP error %ld\n", results.http_code);
2010 } else {
2011 fprintf(stderr, "Unable to start HEAD request\n");
2014 free(url);
2015 return ret;
2018 static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
2020 char *url;
2021 struct strbuf buffer = STRBUF_INIT;
2022 struct active_request_slot *slot;
2023 struct slot_results results;
2025 url = xmalloc(strlen(remote->url) + strlen(path) + 1);
2026 sprintf(url, "%s%s", remote->url, path);
2028 slot = get_active_slot();
2029 slot->results = &results;
2030 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
2031 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
2032 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
2033 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2034 if (start_active_slot(slot)) {
2035 run_active_slot(slot);
2036 if (results.curl_result != CURLE_OK) {
2037 die("Couldn't get %s for remote symref\n%s",
2038 url, curl_errorstr);
2040 } else {
2041 die("Unable to start remote symref request");
2043 free(url);
2045 free(*symref);
2046 *symref = NULL;
2047 hashclr(sha1);
2049 if (buffer.len == 0)
2050 return;
2052 /* If it's a symref, set the refname; otherwise try for a sha1 */
2053 if (!prefixcmp((char *)buffer.buf, "ref: ")) {
2054 *symref = xmemdupz((char *)buffer.buf + 5, buffer.len - 6);
2055 } else {
2056 get_sha1_hex(buffer.buf, sha1);
2059 strbuf_release(&buffer);
2062 static int verify_merge_base(unsigned char *head_sha1, unsigned char *branch_sha1)
2064 struct commit *head = lookup_commit(head_sha1);
2065 struct commit *branch = lookup_commit(branch_sha1);
2066 struct commit_list *merge_bases = get_merge_bases(head, branch, 1);
2068 return (merge_bases && !merge_bases->next && merge_bases->item == branch);
2071 static int delete_remote_branch(char *pattern, int force)
2073 struct ref *refs = remote_refs;
2074 struct ref *remote_ref = NULL;
2075 unsigned char head_sha1[20];
2076 char *symref = NULL;
2077 int match;
2078 int patlen = strlen(pattern);
2079 int i;
2080 struct active_request_slot *slot;
2081 struct slot_results results;
2082 char *url;
2084 /* Find the remote branch(es) matching the specified branch name */
2085 for (match = 0; refs; refs = refs->next) {
2086 char *name = refs->name;
2087 int namelen = strlen(name);
2088 if (namelen < patlen ||
2089 memcmp(name + namelen - patlen, pattern, patlen))
2090 continue;
2091 if (namelen != patlen && name[namelen - patlen - 1] != '/')
2092 continue;
2093 match++;
2094 remote_ref = refs;
2096 if (match == 0)
2097 return error("No remote branch matches %s", pattern);
2098 if (match != 1)
2099 return error("More than one remote branch matches %s",
2100 pattern);
2103 * Remote HEAD must be a symref (not exactly foolproof; a remote
2104 * symlink to a symref will look like a symref)
2106 fetch_symref("HEAD", &symref, head_sha1);
2107 if (!symref)
2108 return error("Remote HEAD is not a symref");
2110 /* Remote branch must not be the remote HEAD */
2111 for (i=0; symref && i<MAXDEPTH; i++) {
2112 if (!strcmp(remote_ref->name, symref))
2113 return error("Remote branch %s is the current HEAD",
2114 remote_ref->name);
2115 fetch_symref(symref, &symref, head_sha1);
2118 /* Run extra sanity checks if delete is not forced */
2119 if (!force) {
2120 /* Remote HEAD must resolve to a known object */
2121 if (symref)
2122 return error("Remote HEAD symrefs too deep");
2123 if (is_zero_sha1(head_sha1))
2124 return error("Unable to resolve remote HEAD");
2125 if (!has_sha1_file(head_sha1))
2126 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1));
2128 /* Remote branch must resolve to a known object */
2129 if (is_zero_sha1(remote_ref->old_sha1))
2130 return error("Unable to resolve remote branch %s",
2131 remote_ref->name);
2132 if (!has_sha1_file(remote_ref->old_sha1))
2133 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));
2135 /* Remote branch must be an ancestor of remote HEAD */
2136 if (!verify_merge_base(head_sha1, remote_ref->old_sha1)) {
2137 return error("The branch '%s' is not an ancestor "
2138 "of your current HEAD.\n"
2139 "If you are sure you want to delete it,"
2140 " run:\n\t'git http-push -D %s %s'",
2141 remote_ref->name, remote->url, pattern);
2145 /* Send delete request */
2146 fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name);
2147 if (dry_run)
2148 return 0;
2149 url = xmalloc(strlen(remote->url) + strlen(remote_ref->name) + 1);
2150 sprintf(url, "%s%s", remote->url, remote_ref->name);
2151 slot = get_active_slot();
2152 slot->results = &results;
2153 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
2154 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
2155 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2156 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_DELETE);
2157 if (start_active_slot(slot)) {
2158 run_active_slot(slot);
2159 free(url);
2160 if (results.curl_result != CURLE_OK)
2161 return error("DELETE request failed (%d/%ld)\n",
2162 results.curl_result, results.http_code);
2163 } else {
2164 free(url);
2165 return error("Unable to start DELETE request");
2168 return 0;
2171 int main(int argc, char **argv)
2173 struct transfer_request *request;
2174 struct transfer_request *next_request;
2175 int nr_refspec = 0;
2176 char **refspec = NULL;
2177 struct remote_lock *ref_lock = NULL;
2178 struct remote_lock *info_ref_lock = NULL;
2179 struct rev_info revs;
2180 int delete_branch = 0;
2181 int force_delete = 0;
2182 int objects_to_send;
2183 int rc = 0;
2184 int i;
2185 int new_refs;
2186 struct ref *ref;
2187 char *rewritten_url = NULL;
2189 setup_git_directory();
2191 remote = xcalloc(sizeof(*remote), 1);
2193 argv++;
2194 for (i = 1; i < argc; i++, argv++) {
2195 char *arg = *argv;
2197 if (*arg == '-') {
2198 if (!strcmp(arg, "--all")) {
2199 push_all = MATCH_REFS_ALL;
2200 continue;
2202 if (!strcmp(arg, "--force")) {
2203 force_all = 1;
2204 continue;
2206 if (!strcmp(arg, "--dry-run")) {
2207 dry_run = 1;
2208 continue;
2210 if (!strcmp(arg, "--verbose")) {
2211 push_verbosely = 1;
2212 continue;
2214 if (!strcmp(arg, "-d")) {
2215 delete_branch = 1;
2216 continue;
2218 if (!strcmp(arg, "-D")) {
2219 delete_branch = 1;
2220 force_delete = 1;
2221 continue;
2224 if (!remote->url) {
2225 char *path = strstr(arg, "//");
2226 remote->url = arg;
2227 remote->path_len = strlen(arg);
2228 if (path) {
2229 remote->path = strchr(path+2, '/');
2230 if (remote->path)
2231 remote->path_len = strlen(remote->path);
2233 continue;
2235 refspec = argv;
2236 nr_refspec = argc - i;
2237 break;
2240 #ifndef USE_CURL_MULTI
2241 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
2242 #endif
2244 if (!remote->url)
2245 usage(http_push_usage);
2247 if (delete_branch && nr_refspec != 1)
2248 die("You must specify only one branch name when deleting a remote branch");
2250 memset(remote_dir_exists, -1, 256);
2252 http_init(NULL);
2254 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
2256 if (remote->url && remote->url[strlen(remote->url)-1] != '/') {
2257 rewritten_url = xmalloc(strlen(remote->url)+2);
2258 strcpy(rewritten_url, remote->url);
2259 strcat(rewritten_url, "/");
2260 remote->path = rewritten_url + (remote->path - remote->url);
2261 remote->path_len++;
2262 remote->url = rewritten_url;
2265 /* Verify DAV compliance/lock support */
2266 if (!locking_available()) {
2267 rc = 1;
2268 goto cleanup;
2271 signal(SIGINT, remove_locks_on_signal);
2272 signal(SIGHUP, remove_locks_on_signal);
2273 signal(SIGQUIT, remove_locks_on_signal);
2274 signal(SIGTERM, remove_locks_on_signal);
2276 /* Check whether the remote has server info files */
2277 remote->can_update_info_refs = 0;
2278 remote->has_info_refs = remote_exists("info/refs");
2279 remote->has_info_packs = remote_exists("objects/info/packs");
2280 if (remote->has_info_refs) {
2281 info_ref_lock = lock_remote("info/refs", LOCK_TIME);
2282 if (info_ref_lock)
2283 remote->can_update_info_refs = 1;
2284 else {
2285 fprintf(stderr, "Error: cannot lock existing info/refs\n");
2286 rc = 1;
2287 goto cleanup;
2290 if (remote->has_info_packs)
2291 fetch_indices();
2293 /* Get a list of all local and remote heads to validate refspecs */
2294 get_local_heads();
2295 fprintf(stderr, "Fetching remote heads...\n");
2296 get_dav_remote_heads();
2298 /* Remove a remote branch if -d or -D was specified */
2299 if (delete_branch) {
2300 if (delete_remote_branch(refspec[0], force_delete) == -1)
2301 fprintf(stderr, "Unable to delete remote branch %s\n",
2302 refspec[0]);
2303 goto cleanup;
2306 /* match them up */
2307 if (!remote_tail)
2308 remote_tail = &remote_refs;
2309 if (match_refs(local_refs, remote_refs, &remote_tail,
2310 nr_refspec, (const char **) refspec, push_all)) {
2311 rc = -1;
2312 goto cleanup;
2314 if (!remote_refs) {
2315 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
2316 rc = 0;
2317 goto cleanup;
2320 new_refs = 0;
2321 for (ref = remote_refs; ref; ref = ref->next) {
2322 char old_hex[60], *new_hex;
2323 const char *commit_argv[4];
2324 int commit_argc;
2325 char *new_sha1_hex, *old_sha1_hex;
2327 if (!ref->peer_ref)
2328 continue;
2330 if (is_zero_sha1(ref->peer_ref->new_sha1)) {
2331 if (delete_remote_branch(ref->name, 1) == -1) {
2332 error("Could not remove %s", ref->name);
2333 rc = -4;
2335 new_refs++;
2336 continue;
2339 if (!hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
2340 if (push_verbosely || 1)
2341 fprintf(stderr, "'%s': up-to-date\n", ref->name);
2342 continue;
2345 if (!force_all &&
2346 !is_zero_sha1(ref->old_sha1) &&
2347 !ref->force) {
2348 if (!has_sha1_file(ref->old_sha1) ||
2349 !ref_newer(ref->peer_ref->new_sha1,
2350 ref->old_sha1)) {
2352 * We do not have the remote ref, or
2353 * we know that the remote ref is not
2354 * an ancestor of what we are trying to
2355 * push. Either way this can be losing
2356 * commits at the remote end and likely
2357 * we were not up to date to begin with.
2359 error("remote '%s' is not an ancestor of\n"
2360 "local '%s'.\n"
2361 "Maybe you are not up-to-date and "
2362 "need to pull first?",
2363 ref->name,
2364 ref->peer_ref->name);
2365 rc = -2;
2366 continue;
2369 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
2370 new_refs++;
2371 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
2372 new_hex = sha1_to_hex(ref->new_sha1);
2374 fprintf(stderr, "updating '%s'", ref->name);
2375 if (strcmp(ref->name, ref->peer_ref->name))
2376 fprintf(stderr, " using '%s'", ref->peer_ref->name);
2377 fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex);
2378 if (dry_run)
2379 continue;
2381 /* Lock remote branch ref */
2382 ref_lock = lock_remote(ref->name, LOCK_TIME);
2383 if (ref_lock == NULL) {
2384 fprintf(stderr, "Unable to lock remote branch %s\n",
2385 ref->name);
2386 rc = 1;
2387 continue;
2390 /* Set up revision info for this refspec */
2391 commit_argc = 3;
2392 new_sha1_hex = xstrdup(sha1_to_hex(ref->new_sha1));
2393 old_sha1_hex = NULL;
2394 commit_argv[1] = "--objects";
2395 commit_argv[2] = new_sha1_hex;
2396 if (!push_all && !is_zero_sha1(ref->old_sha1)) {
2397 old_sha1_hex = xmalloc(42);
2398 sprintf(old_sha1_hex, "^%s",
2399 sha1_to_hex(ref->old_sha1));
2400 commit_argv[3] = old_sha1_hex;
2401 commit_argc++;
2403 init_revisions(&revs, setup_git_directory());
2404 setup_revisions(commit_argc, commit_argv, &revs, NULL);
2405 revs.edge_hint = 0; /* just in case */
2406 free(new_sha1_hex);
2407 if (old_sha1_hex) {
2408 free(old_sha1_hex);
2409 commit_argv[1] = NULL;
2412 /* Generate a list of objects that need to be pushed */
2413 pushing = 0;
2414 if (prepare_revision_walk(&revs))
2415 die("revision walk setup failed");
2416 mark_edges_uninteresting(revs.commits, &revs, NULL);
2417 objects_to_send = get_delta(&revs, ref_lock);
2418 finish_all_active_slots();
2420 /* Push missing objects to remote, this would be a
2421 convenient time to pack them first if appropriate. */
2422 pushing = 1;
2423 if (objects_to_send)
2424 fprintf(stderr, " sending %d objects\n",
2425 objects_to_send);
2426 #ifdef USE_CURL_MULTI
2427 fill_active_slots();
2428 add_fill_function(NULL, fill_active_slot);
2429 #endif
2430 do {
2431 finish_all_active_slots();
2432 #ifdef USE_CURL_MULTI
2433 fill_active_slots();
2434 #endif
2435 } while (request_queue_head && !aborted);
2437 /* Update the remote branch if all went well */
2438 if (aborted || !update_remote(ref->new_sha1, ref_lock))
2439 rc = 1;
2441 if (!rc)
2442 fprintf(stderr, " done\n");
2443 unlock_remote(ref_lock);
2444 check_locks();
2447 /* Update remote server info if appropriate */
2448 if (remote->has_info_refs && new_refs) {
2449 if (info_ref_lock && remote->can_update_info_refs) {
2450 fprintf(stderr, "Updating remote server info\n");
2451 if (!dry_run)
2452 update_remote_info_refs(info_ref_lock);
2453 } else {
2454 fprintf(stderr, "Unable to update server info\n");
2458 cleanup:
2459 free(rewritten_url);
2460 if (info_ref_lock)
2461 unlock_remote(info_ref_lock);
2462 free(remote);
2464 curl_slist_free_all(no_pragma_header);
2466 http_cleanup();
2468 request = request_queue_head;
2469 while (request != NULL) {
2470 next_request = request->next;
2471 release_request(request);
2472 request = next_request;
2475 return rc;