Use sigaction and SA_RESTART in read-tree.c; add option in Makefile.
[alt-git.git] / http-push.c
blobfe925609b4024119c6171dd32350a6570bea8516
1 #include "cache.h"
2 #include "commit.h"
3 #include "pack.h"
4 #include "fetch.h"
5 #include "tag.h"
6 #include "blob.h"
7 #include "http.h"
9 #include <expat.h>
11 static const char http_push_usage[] =
12 "git-http-push [--complete] [--force] [--verbose] <url> <ref> [<ref>...]\n";
14 #ifndef XML_STATUS_OK
15 enum XML_Status {
16 XML_STATUS_OK = 1,
17 XML_STATUS_ERROR = 0
19 #define XML_STATUS_OK 1
20 #define XML_STATUS_ERROR 0
21 #endif
23 #define RANGE_HEADER_SIZE 30
25 /* DAV methods */
26 #define DAV_LOCK "LOCK"
27 #define DAV_MKCOL "MKCOL"
28 #define DAV_MOVE "MOVE"
29 #define DAV_PROPFIND "PROPFIND"
30 #define DAV_PUT "PUT"
31 #define DAV_UNLOCK "UNLOCK"
33 /* DAV lock flags */
34 #define DAV_PROP_LOCKWR (1u << 0)
35 #define DAV_PROP_LOCKEX (1u << 1)
36 #define DAV_LOCK_OK (1u << 2)
38 /* DAV XML properties */
39 #define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
40 #define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
41 #define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
42 #define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
43 #define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
44 #define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
46 /* DAV request body templates */
47 #define PROPFIND_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>"
48 #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>"
50 #define LOCK_TIME 600
51 #define LOCK_REFRESH 30
53 static int pushing = 0;
54 static int aborted = 0;
55 static char remote_dir_exists[256];
57 static struct curl_slist *no_pragma_header;
58 static struct curl_slist *default_headers;
60 static int push_verbosely = 0;
61 static int push_all = 0;
62 static int force_all = 0;
64 struct repo
66 char *url;
67 struct packed_git *packs;
70 static struct repo *remote = NULL;
72 enum transfer_state {
73 NEED_CHECK,
74 RUN_HEAD,
75 NEED_PUSH,
76 RUN_MKCOL,
77 RUN_PUT,
78 RUN_MOVE,
79 ABORTED,
80 COMPLETE,
83 struct transfer_request
85 unsigned char sha1[20];
86 char *url;
87 char *dest;
88 struct active_lock *lock;
89 struct curl_slist *headers;
90 struct buffer buffer;
91 char filename[PATH_MAX];
92 char tmpfile[PATH_MAX];
93 enum transfer_state state;
94 CURLcode curl_result;
95 char errorstr[CURL_ERROR_SIZE];
96 long http_code;
97 unsigned char real_sha1[20];
98 SHA_CTX c;
99 z_stream stream;
100 int zret;
101 int rename;
102 struct active_request_slot *slot;
103 struct transfer_request *next;
106 static struct transfer_request *request_queue_head = NULL;
108 struct xml_ctx
110 char *name;
111 int len;
112 char *cdata;
113 void (*userFunc)(struct xml_ctx *ctx, int tag_closed);
114 void *userData;
117 struct active_lock
119 char *url;
120 char *owner;
121 char *token;
122 time_t start_time;
123 long timeout;
124 int refreshing;
127 static void finish_request(struct transfer_request *request);
129 static void process_response(void *callback_data)
131 struct transfer_request *request =
132 (struct transfer_request *)callback_data;
134 finish_request(request);
137 static void start_check(struct transfer_request *request)
139 char *hex = sha1_to_hex(request->sha1);
140 struct active_request_slot *slot;
141 char *posn;
143 request->url = xmalloc(strlen(remote->url) + 55);
144 strcpy(request->url, remote->url);
145 posn = request->url + strlen(remote->url);
146 strcpy(posn, "objects/");
147 posn += 8;
148 memcpy(posn, hex, 2);
149 posn += 2;
150 *(posn++) = '/';
151 strcpy(posn, hex + 2);
153 slot = get_active_slot();
154 slot->callback_func = process_response;
155 slot->callback_data = request;
156 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
157 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
158 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
160 if (start_active_slot(slot)) {
161 request->slot = slot;
162 request->state = RUN_HEAD;
163 } else {
164 request->state = ABORTED;
165 free(request->url);
166 request->url = NULL;
170 static void start_mkcol(struct transfer_request *request)
172 char *hex = sha1_to_hex(request->sha1);
173 struct active_request_slot *slot;
174 char *posn;
176 request->url = xmalloc(strlen(remote->url) + 13);
177 strcpy(request->url, remote->url);
178 posn = request->url + strlen(remote->url);
179 strcpy(posn, "objects/");
180 posn += 8;
181 memcpy(posn, hex, 2);
182 posn += 2;
183 strcpy(posn, "/");
185 slot = get_active_slot();
186 slot->callback_func = process_response;
187 slot->callback_data = request;
188 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
189 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
190 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
191 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
192 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
194 if (start_active_slot(slot)) {
195 request->slot = slot;
196 request->state = RUN_MKCOL;
197 } else {
198 request->state = ABORTED;
199 free(request->url);
200 request->url = NULL;
204 static void start_put(struct transfer_request *request)
206 char *hex = sha1_to_hex(request->sha1);
207 struct active_request_slot *slot;
208 char *posn;
209 char type[20];
210 char hdr[50];
211 void *unpacked;
212 unsigned long len;
213 int hdrlen;
214 ssize_t size;
215 z_stream stream;
217 unpacked = read_sha1_file(request->sha1, type, &len);
218 hdrlen = sprintf(hdr, "%s %lu", type, len) + 1;
220 /* Set it up */
221 memset(&stream, 0, sizeof(stream));
222 deflateInit(&stream, Z_BEST_COMPRESSION);
223 size = deflateBound(&stream, len + hdrlen);
224 request->buffer.buffer = xmalloc(size);
226 /* Compress it */
227 stream.next_out = request->buffer.buffer;
228 stream.avail_out = size;
230 /* First header.. */
231 stream.next_in = (void *)hdr;
232 stream.avail_in = hdrlen;
233 while (deflate(&stream, 0) == Z_OK)
234 /* nothing */;
236 /* Then the data itself.. */
237 stream.next_in = unpacked;
238 stream.avail_in = len;
239 while (deflate(&stream, Z_FINISH) == Z_OK)
240 /* nothing */;
241 deflateEnd(&stream);
242 free(unpacked);
244 request->buffer.size = stream.total_out;
245 request->buffer.posn = 0;
247 request->url = xmalloc(strlen(remote->url) +
248 strlen(request->lock->token) + 51);
249 strcpy(request->url, remote->url);
250 posn = request->url + strlen(remote->url);
251 strcpy(posn, "objects/");
252 posn += 8;
253 memcpy(posn, hex, 2);
254 posn += 2;
255 *(posn++) = '/';
256 strcpy(posn, hex + 2);
257 request->dest = xmalloc(strlen(request->url) + 14);
258 sprintf(request->dest, "Destination: %s", request->url);
259 posn += 38;
260 *(posn++) = '.';
261 strcpy(posn, request->lock->token);
263 slot = get_active_slot();
264 slot->callback_func = process_response;
265 slot->callback_data = request;
266 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &request->buffer);
267 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, request->buffer.size);
268 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
269 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
270 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
271 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
272 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
273 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
274 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
276 if (start_active_slot(slot)) {
277 request->slot = slot;
278 request->state = RUN_PUT;
279 } else {
280 request->state = ABORTED;
281 free(request->url);
282 request->url = NULL;
286 static void start_move(struct transfer_request *request)
288 struct active_request_slot *slot;
289 struct curl_slist *dav_headers = NULL;
291 slot = get_active_slot();
292 slot->callback_func = process_response;
293 slot->callback_data = request;
294 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
295 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MOVE);
296 dav_headers = curl_slist_append(dav_headers, request->dest);
297 dav_headers = curl_slist_append(dav_headers, "Overwrite: T");
298 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
299 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
300 curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
302 if (start_active_slot(slot)) {
303 request->slot = slot;
304 request->state = RUN_MOVE;
305 } else {
306 request->state = ABORTED;
307 free(request->url);
308 request->url = NULL;
312 static int refresh_lock(struct active_lock *lock)
314 struct active_request_slot *slot;
315 char *if_header;
316 char timeout_header[25];
317 struct curl_slist *dav_headers = NULL;
318 int rc = 0;
320 lock->refreshing = 1;
322 if_header = xmalloc(strlen(lock->token) + 25);
323 sprintf(if_header, "If: (<opaquelocktoken:%s>)", lock->token);
324 sprintf(timeout_header, "Timeout: Second-%ld", lock->timeout);
325 dav_headers = curl_slist_append(dav_headers, if_header);
326 dav_headers = curl_slist_append(dav_headers, timeout_header);
328 slot = get_active_slot();
329 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
330 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
331 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
332 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
333 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
335 if (start_active_slot(slot)) {
336 run_active_slot(slot);
337 if (slot->curl_result != CURLE_OK) {
338 fprintf(stderr, "Got HTTP error %ld\n", slot->http_code);
339 } else {
340 lock->start_time = time(NULL);
341 rc = 1;
345 lock->refreshing = 0;
346 curl_slist_free_all(dav_headers);
347 free(if_header);
349 return rc;
352 static void finish_request(struct transfer_request *request)
354 time_t current_time = time(NULL);
355 int time_remaining;
357 request->curl_result = request->slot->curl_result;
358 request->http_code = request->slot->http_code;
359 request->slot = NULL;
361 /* Refresh the lock if it is close to timing out */
362 time_remaining = request->lock->start_time + request->lock->timeout
363 - current_time;
364 if (time_remaining < LOCK_REFRESH && !request->lock->refreshing) {
365 if (!refresh_lock(request->lock)) {
366 fprintf(stderr, "Unable to refresh remote lock\n");
367 aborted = 1;
371 if (request->headers != NULL)
372 curl_slist_free_all(request->headers);
374 /* URL is reused for MOVE after PUT */
375 if (request->state != RUN_PUT) {
376 free(request->url);
377 request->url = NULL;
380 if (request->state == RUN_HEAD) {
381 if (request->http_code == 404) {
382 request->state = NEED_PUSH;
383 } else if (request->curl_result == CURLE_OK) {
384 remote_dir_exists[request->sha1[0]] = 1;
385 request->state = COMPLETE;
386 } else {
387 fprintf(stderr, "HEAD %s failed, aborting (%d/%ld)\n",
388 sha1_to_hex(request->sha1),
389 request->curl_result, request->http_code);
390 request->state = ABORTED;
391 aborted = 1;
393 } else if (request->state == RUN_MKCOL) {
394 if (request->curl_result == CURLE_OK ||
395 request->http_code == 405) {
396 remote_dir_exists[request->sha1[0]] = 1;
397 start_put(request);
398 } else {
399 fprintf(stderr, "MKCOL %s failed, aborting (%d/%ld)\n",
400 sha1_to_hex(request->sha1),
401 request->curl_result, request->http_code);
402 request->state = ABORTED;
403 aborted = 1;
405 } else if (request->state == RUN_PUT) {
406 if (request->curl_result == CURLE_OK) {
407 start_move(request);
408 } else {
409 fprintf(stderr, "PUT %s failed, aborting (%d/%ld)\n",
410 sha1_to_hex(request->sha1),
411 request->curl_result, request->http_code);
412 request->state = ABORTED;
413 aborted = 1;
415 } else if (request->state == RUN_MOVE) {
416 if (request->curl_result == CURLE_OK) {
417 if (push_verbosely)
418 fprintf(stderr,
419 "sent %s\n",
420 sha1_to_hex(request->sha1));
421 request->state = COMPLETE;
422 } else {
423 fprintf(stderr, "MOVE %s failed, aborting (%d/%ld)\n",
424 sha1_to_hex(request->sha1),
425 request->curl_result, request->http_code);
426 request->state = ABORTED;
427 aborted = 1;
432 static void release_request(struct transfer_request *request)
434 struct transfer_request *entry = request_queue_head;
436 if (request == request_queue_head) {
437 request_queue_head = request->next;
438 } else {
439 while (entry->next != NULL && entry->next != request)
440 entry = entry->next;
441 if (entry->next == request)
442 entry->next = entry->next->next;
445 if (request->url != NULL)
446 free(request->url);
447 free(request);
450 void fill_active_slots(void)
452 struct transfer_request *request = request_queue_head;
453 struct active_request_slot *slot = active_queue_head;
454 int num_transfers;
456 if (aborted)
457 return;
459 while (active_requests < max_requests && request != NULL) {
460 if (!pushing && request->state == NEED_CHECK) {
461 start_check(request);
462 curl_multi_perform(curlm, &num_transfers);
463 } else if (pushing && request->state == NEED_PUSH) {
464 if (remote_dir_exists[request->sha1[0]])
465 start_put(request);
466 else
467 start_mkcol(request);
468 curl_multi_perform(curlm, &num_transfers);
470 request = request->next;
473 while (slot != NULL) {
474 if (!slot->in_use && slot->curl != NULL) {
475 curl_easy_cleanup(slot->curl);
476 slot->curl = NULL;
478 slot = slot->next;
482 static void add_request(unsigned char *sha1, struct active_lock *lock)
484 struct transfer_request *request = request_queue_head;
485 struct packed_git *target;
487 while (request != NULL && memcmp(request->sha1, sha1, 20))
488 request = request->next;
489 if (request != NULL)
490 return;
492 target = find_sha1_pack(sha1, remote->packs);
493 if (target)
494 return;
496 request = xmalloc(sizeof(*request));
497 memcpy(request->sha1, sha1, 20);
498 request->url = NULL;
499 request->lock = lock;
500 request->headers = NULL;
501 request->state = NEED_CHECK;
502 request->next = request_queue_head;
503 request_queue_head = request;
505 fill_active_slots();
506 step_active_slots();
509 static int fetch_index(unsigned char *sha1)
511 char *hex = sha1_to_hex(sha1);
512 char *filename;
513 char *url;
514 char tmpfile[PATH_MAX];
515 long prev_posn = 0;
516 char range[RANGE_HEADER_SIZE];
517 struct curl_slist *range_header = NULL;
519 FILE *indexfile;
520 struct active_request_slot *slot;
522 /* Don't use the index if the pack isn't there */
523 url = xmalloc(strlen(remote->url) + 65);
524 sprintf(url, "%s/objects/pack/pack-%s.pack", remote->url, hex);
525 slot = get_active_slot();
526 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
527 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
528 if (start_active_slot(slot)) {
529 run_active_slot(slot);
530 if (slot->curl_result != CURLE_OK) {
531 free(url);
532 return error("Unable to verify pack %s is available",
533 hex);
535 } else {
536 return error("Unable to start request");
539 if (has_pack_index(sha1))
540 return 0;
542 if (push_verbosely)
543 fprintf(stderr, "Getting index for pack %s\n", hex);
545 sprintf(url, "%s/objects/pack/pack-%s.idx", remote->url, hex);
547 filename = sha1_pack_index_name(sha1);
548 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
549 indexfile = fopen(tmpfile, "a");
550 if (!indexfile)
551 return error("Unable to open local file %s for pack index",
552 filename);
554 slot = get_active_slot();
555 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
556 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
557 curl_easy_setopt(slot->curl, CURLOPT_FILE, indexfile);
558 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
559 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
560 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
561 slot->local = indexfile;
563 /* If there is data present from a previous transfer attempt,
564 resume where it left off */
565 prev_posn = ftell(indexfile);
566 if (prev_posn>0) {
567 if (push_verbosely)
568 fprintf(stderr,
569 "Resuming fetch of index for pack %s at byte %ld\n",
570 hex, prev_posn);
571 sprintf(range, "Range: bytes=%ld-", prev_posn);
572 range_header = curl_slist_append(range_header, range);
573 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
576 if (start_active_slot(slot)) {
577 run_active_slot(slot);
578 if (slot->curl_result != CURLE_OK) {
579 free(url);
580 fclose(indexfile);
581 return error("Unable to get pack index %s\n%s", url,
582 curl_errorstr);
584 } else {
585 free(url);
586 fclose(indexfile);
587 return error("Unable to start request");
590 free(url);
591 fclose(indexfile);
593 return move_temp_to_file(tmpfile, filename);
596 static int setup_index(unsigned char *sha1)
598 struct packed_git *new_pack;
600 if (fetch_index(sha1))
601 return -1;
603 new_pack = parse_pack_index(sha1);
604 new_pack->next = remote->packs;
605 remote->packs = new_pack;
606 return 0;
609 static int fetch_indices(void)
611 unsigned char sha1[20];
612 char *url;
613 struct buffer buffer;
614 char *data;
615 int i = 0;
617 struct active_request_slot *slot;
619 data = xmalloc(4096);
620 memset(data, 0, 4096);
621 buffer.size = 4096;
622 buffer.posn = 0;
623 buffer.buffer = data;
625 if (push_verbosely)
626 fprintf(stderr, "Getting pack list\n");
628 url = xmalloc(strlen(remote->url) + 21);
629 sprintf(url, "%s/objects/info/packs", remote->url);
631 slot = get_active_slot();
632 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
633 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
634 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
635 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
636 if (start_active_slot(slot)) {
637 run_active_slot(slot);
638 if (slot->curl_result != CURLE_OK) {
639 free(buffer.buffer);
640 free(url);
641 if (slot->http_code == 404)
642 return 0;
643 else
644 return error("%s", curl_errorstr);
646 } else {
647 free(buffer.buffer);
648 free(url);
649 return error("Unable to start request");
651 free(url);
653 data = buffer.buffer;
654 while (i < buffer.posn) {
655 switch (data[i]) {
656 case 'P':
657 i++;
658 if (i + 52 < buffer.posn &&
659 !strncmp(data + i, " pack-", 6) &&
660 !strncmp(data + i + 46, ".pack\n", 6)) {
661 get_sha1_hex(data + i + 6, sha1);
662 setup_index(sha1);
663 i += 51;
664 break;
666 default:
667 while (data[i] != '\n')
668 i++;
670 i++;
673 free(buffer.buffer);
674 return 0;
677 static inline int needs_quote(int ch)
679 switch (ch) {
680 case '/': case '-': case '.':
681 case 'A'...'Z': case 'a'...'z': case '0'...'9':
682 return 0;
683 default:
684 return 1;
688 static inline int hex(int v)
690 if (v < 10) return '0' + v;
691 else return 'A' + v - 10;
694 static char *quote_ref_url(const char *base, const char *ref)
696 const char *cp;
697 char *dp, *qref;
698 int len, baselen, ch;
700 baselen = strlen(base);
701 len = baselen + 12; /* "refs/heads/" + NUL */
702 for (cp = ref; (ch = *cp) != 0; cp++, len++)
703 if (needs_quote(ch))
704 len += 2; /* extra two hex plus replacement % */
705 qref = xmalloc(len);
706 memcpy(qref, base, baselen);
707 memcpy(qref + baselen, "refs/heads/", 11);
708 for (cp = ref, dp = qref + baselen + 11; (ch = *cp) != 0; cp++) {
709 if (needs_quote(ch)) {
710 *dp++ = '%';
711 *dp++ = hex((ch >> 4) & 0xF);
712 *dp++ = hex(ch & 0xF);
714 else
715 *dp++ = ch;
717 *dp = 0;
719 return qref;
722 int fetch_ref(char *ref, unsigned char *sha1)
724 char *url;
725 char hex[42];
726 struct buffer buffer;
727 char *base = remote->url;
728 struct active_request_slot *slot;
729 buffer.size = 41;
730 buffer.posn = 0;
731 buffer.buffer = hex;
732 hex[41] = '\0';
734 url = quote_ref_url(base, ref);
735 slot = get_active_slot();
736 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
737 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
738 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
739 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
740 if (start_active_slot(slot)) {
741 run_active_slot(slot);
742 if (slot->curl_result != CURLE_OK)
743 return error("Couldn't get %s for %s\n%s",
744 url, ref, curl_errorstr);
745 } else {
746 return error("Unable to start request");
749 hex[40] = '\0';
750 get_sha1_hex(hex, sha1);
751 return 0;
754 static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed)
756 int *lock_flags = (int *)ctx->userData;
758 if (tag_closed) {
759 if (!strcmp(ctx->name, DAV_CTX_LOCKENTRY)) {
760 if ((*lock_flags & DAV_PROP_LOCKEX) &&
761 (*lock_flags & DAV_PROP_LOCKWR)) {
762 *lock_flags |= DAV_LOCK_OK;
764 *lock_flags &= DAV_LOCK_OK;
765 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_WRITE)) {
766 *lock_flags |= DAV_PROP_LOCKWR;
767 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_EXCLUSIVE)) {
768 *lock_flags |= DAV_PROP_LOCKEX;
773 static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
775 struct active_lock *lock = (struct active_lock *)ctx->userData;
777 if (tag_closed && ctx->cdata) {
778 if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) {
779 lock->owner = xmalloc(strlen(ctx->cdata) + 1);
780 strcpy(lock->owner, ctx->cdata);
781 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
782 if (!strncmp(ctx->cdata, "Second-", 7))
783 lock->timeout =
784 strtol(ctx->cdata + 7, NULL, 10);
785 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
786 if (!strncmp(ctx->cdata, "opaquelocktoken:", 16)) {
787 lock->token = xmalloc(strlen(ctx->cdata) - 15);
788 strcpy(lock->token, ctx->cdata + 16);
794 static void
795 xml_start_tag(void *userData, const char *name, const char **atts)
797 struct xml_ctx *ctx = (struct xml_ctx *)userData;
798 const char *c = index(name, ':');
799 int new_len;
801 if (c == NULL)
802 c = name;
803 else
804 c++;
806 new_len = strlen(ctx->name) + strlen(c) + 2;
808 if (new_len > ctx->len) {
809 ctx->name = xrealloc(ctx->name, new_len);
810 ctx->len = new_len;
812 strcat(ctx->name, ".");
813 strcat(ctx->name, c);
815 if (ctx->cdata) {
816 free(ctx->cdata);
817 ctx->cdata = NULL;
820 ctx->userFunc(ctx, 0);
823 static void
824 xml_end_tag(void *userData, const char *name)
826 struct xml_ctx *ctx = (struct xml_ctx *)userData;
827 const char *c = index(name, ':');
828 char *ep;
830 ctx->userFunc(ctx, 1);
832 if (c == NULL)
833 c = name;
834 else
835 c++;
837 ep = ctx->name + strlen(ctx->name) - strlen(c) - 1;
838 *ep = 0;
841 static void
842 xml_cdata(void *userData, const XML_Char *s, int len)
844 struct xml_ctx *ctx = (struct xml_ctx *)userData;
845 if (ctx->cdata)
846 free(ctx->cdata);
847 ctx->cdata = xcalloc(len+1, 1);
848 strncpy(ctx->cdata, s, len);
851 static struct active_lock *lock_remote(char *file, long timeout)
853 struct active_request_slot *slot;
854 struct buffer out_buffer;
855 struct buffer in_buffer;
856 char *out_data;
857 char *in_data;
858 char *url;
859 char *ep;
860 char timeout_header[25];
861 struct active_lock *new_lock = NULL;
862 XML_Parser parser = XML_ParserCreate(NULL);
863 enum XML_Status result;
864 struct curl_slist *dav_headers = NULL;
865 struct xml_ctx ctx;
867 url = xmalloc(strlen(remote->url) + strlen(file) + 1);
868 sprintf(url, "%s%s", remote->url, file);
870 /* Make sure leading directories exist for the remote ref */
871 ep = strchr(url + strlen(remote->url) + 11, '/');
872 while (ep) {
873 *ep = 0;
874 slot = get_active_slot();
875 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
876 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
877 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
878 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
879 if (start_active_slot(slot)) {
880 run_active_slot(slot);
881 if (slot->curl_result != CURLE_OK &&
882 slot->http_code != 405) {
883 fprintf(stderr,
884 "Unable to create branch path %s\n",
885 url);
886 free(url);
887 return NULL;
889 } else {
890 fprintf(stderr, "Unable to start request\n");
891 free(url);
892 return NULL;
894 *ep = '/';
895 ep = strchr(ep + 1, '/');
898 out_buffer.size = strlen(LOCK_REQUEST) + strlen(git_default_email) - 2;
899 out_data = xmalloc(out_buffer.size + 1);
900 snprintf(out_data, out_buffer.size + 1, LOCK_REQUEST, git_default_email);
901 out_buffer.posn = 0;
902 out_buffer.buffer = out_data;
904 in_buffer.size = 4096;
905 in_data = xmalloc(in_buffer.size);
906 in_buffer.posn = 0;
907 in_buffer.buffer = in_data;
909 sprintf(timeout_header, "Timeout: Second-%ld", timeout);
910 dav_headers = curl_slist_append(dav_headers, timeout_header);
911 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
913 slot = get_active_slot();
914 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
915 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size);
916 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
917 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
918 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
919 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
920 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
921 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
922 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
924 new_lock = xcalloc(1, sizeof(*new_lock));
925 new_lock->owner = NULL;
926 new_lock->token = NULL;
927 new_lock->timeout = -1;
928 new_lock->refreshing = 0;
930 if (start_active_slot(slot)) {
931 run_active_slot(slot);
932 if (slot->curl_result == CURLE_OK) {
933 ctx.name = xcalloc(10, 1);
934 ctx.len = 0;
935 ctx.cdata = NULL;
936 ctx.userFunc = handle_new_lock_ctx;
937 ctx.userData = new_lock;
938 XML_SetUserData(parser, &ctx);
939 XML_SetElementHandler(parser, xml_start_tag,
940 xml_end_tag);
941 XML_SetCharacterDataHandler(parser, xml_cdata);
942 result = XML_Parse(parser, in_buffer.buffer,
943 in_buffer.posn, 1);
944 free(ctx.name);
945 if (result != XML_STATUS_OK) {
946 fprintf(stderr, "XML error: %s\n",
947 XML_ErrorString(
948 XML_GetErrorCode(parser)));
949 new_lock->timeout = -1;
952 } else {
953 fprintf(stderr, "Unable to start request\n");
956 curl_slist_free_all(dav_headers);
957 free(out_data);
958 free(in_data);
960 if (new_lock->token == NULL || new_lock->timeout <= 0) {
961 if (new_lock->token != NULL)
962 free(new_lock->token);
963 if (new_lock->owner != NULL)
964 free(new_lock->owner);
965 free(url);
966 free(new_lock);
967 new_lock = NULL;
968 } else {
969 new_lock->url = url;
970 new_lock->start_time = time(NULL);
973 return new_lock;
976 static int unlock_remote(struct active_lock *lock)
978 struct active_request_slot *slot;
979 char *lock_token_header;
980 struct curl_slist *dav_headers = NULL;
981 int rc = 0;
983 lock_token_header = xmalloc(strlen(lock->token) + 31);
984 sprintf(lock_token_header, "Lock-Token: <opaquelocktoken:%s>",
985 lock->token);
986 dav_headers = curl_slist_append(dav_headers, lock_token_header);
988 slot = get_active_slot();
989 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
990 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
991 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_UNLOCK);
992 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
994 if (start_active_slot(slot)) {
995 run_active_slot(slot);
996 if (slot->curl_result == CURLE_OK)
997 rc = 1;
998 else
999 fprintf(stderr, "Got HTTP error %ld\n",
1000 slot->http_code);
1001 } else {
1002 fprintf(stderr, "Unable to start request\n");
1005 curl_slist_free_all(dav_headers);
1006 free(lock_token_header);
1008 if (lock->owner != NULL)
1009 free(lock->owner);
1010 free(lock->url);
1011 free(lock->token);
1012 free(lock);
1014 return rc;
1017 static int locking_available(void)
1019 struct active_request_slot *slot;
1020 struct buffer in_buffer;
1021 struct buffer out_buffer;
1022 char *in_data;
1023 char *out_data;
1024 XML_Parser parser = XML_ParserCreate(NULL);
1025 enum XML_Status result;
1026 struct curl_slist *dav_headers = NULL;
1027 struct xml_ctx ctx;
1028 int lock_flags = 0;
1030 out_buffer.size = strlen(PROPFIND_REQUEST) + strlen(remote->url) - 2;
1031 out_data = xmalloc(out_buffer.size + 1);
1032 snprintf(out_data, out_buffer.size + 1, PROPFIND_REQUEST, remote->url);
1033 out_buffer.posn = 0;
1034 out_buffer.buffer = out_data;
1036 in_buffer.size = 4096;
1037 in_data = xmalloc(in_buffer.size);
1038 in_buffer.posn = 0;
1039 in_buffer.buffer = in_data;
1041 dav_headers = curl_slist_append(dav_headers, "Depth: 0");
1042 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1044 slot = get_active_slot();
1045 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1046 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size);
1047 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1048 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1049 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1050 curl_easy_setopt(slot->curl, CURLOPT_URL, remote->url);
1051 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1052 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1053 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1055 if (start_active_slot(slot)) {
1056 run_active_slot(slot);
1057 if (slot->curl_result == CURLE_OK) {
1058 ctx.name = xcalloc(10, 1);
1059 ctx.len = 0;
1060 ctx.cdata = NULL;
1061 ctx.userFunc = handle_lockprop_ctx;
1062 ctx.userData = &lock_flags;
1063 XML_SetUserData(parser, &ctx);
1064 XML_SetElementHandler(parser, xml_start_tag,
1065 xml_end_tag);
1066 result = XML_Parse(parser, in_buffer.buffer,
1067 in_buffer.posn, 1);
1068 free(ctx.name);
1070 if (result != XML_STATUS_OK) {
1071 fprintf(stderr, "XML error: %s\n",
1072 XML_ErrorString(
1073 XML_GetErrorCode(parser)));
1074 lock_flags = 0;
1077 } else {
1078 fprintf(stderr, "Unable to start request\n");
1081 free(out_data);
1082 free(in_buffer.buffer);
1083 curl_slist_free_all(dav_headers);
1085 return lock_flags;
1088 static int is_ancestor(unsigned char *sha1, struct commit *commit)
1090 struct commit_list *parents;
1092 if (parse_commit(commit))
1093 return 0;
1094 parents = commit->parents;
1095 for (; parents; parents = parents->next) {
1096 if (!memcmp(sha1, parents->item->object.sha1, 20)) {
1097 return 1;
1098 } else if (parents->item->object.type == commit_type) {
1099 if (is_ancestor(
1100 sha1,
1101 (struct commit *)&parents->item->object
1103 return 1;
1106 return 0;
1109 static void get_delta(unsigned char *sha1, struct object *obj,
1110 struct active_lock *lock)
1112 struct commit *commit;
1113 struct commit_list *parents;
1114 struct tree *tree;
1115 struct tree_entry_list *entry;
1117 if (sha1 && !memcmp(sha1, obj->sha1, 20))
1118 return;
1120 if (aborted)
1121 return;
1123 if (obj->type == commit_type) {
1124 if (push_verbosely)
1125 fprintf(stderr, "walk %s\n", sha1_to_hex(obj->sha1));
1126 add_request(obj->sha1, lock);
1127 commit = (struct commit *)obj;
1128 if (parse_commit(commit)) {
1129 fprintf(stderr, "Error parsing commit %s\n",
1130 sha1_to_hex(obj->sha1));
1131 aborted = 1;
1132 return;
1134 parents = commit->parents;
1135 for (; parents; parents = parents->next)
1136 if (sha1 == NULL ||
1137 memcmp(sha1, parents->item->object.sha1, 20))
1138 get_delta(sha1, &parents->item->object,
1139 lock);
1140 get_delta(sha1, &commit->tree->object, lock);
1141 } else if (obj->type == tree_type) {
1142 if (push_verbosely)
1143 fprintf(stderr, "walk %s\n", sha1_to_hex(obj->sha1));
1144 add_request(obj->sha1, lock);
1145 tree = (struct tree *)obj;
1146 if (parse_tree(tree)) {
1147 fprintf(stderr, "Error parsing tree %s\n",
1148 sha1_to_hex(obj->sha1));
1149 aborted = 1;
1150 return;
1152 entry = tree->entries;
1153 tree->entries = NULL;
1154 while (entry) {
1155 struct tree_entry_list *next = entry->next;
1156 get_delta(sha1, entry->item.any, lock);
1157 free(entry->name);
1158 free(entry);
1159 entry = next;
1161 } else if (obj->type == blob_type || obj->type == tag_type) {
1162 add_request(obj->sha1, lock);
1166 static int update_remote(unsigned char *sha1, struct active_lock *lock)
1168 struct active_request_slot *slot;
1169 char *out_data;
1170 char *if_header;
1171 struct buffer out_buffer;
1172 struct curl_slist *dav_headers = NULL;
1173 int i;
1175 if_header = xmalloc(strlen(lock->token) + 25);
1176 sprintf(if_header, "If: (<opaquelocktoken:%s>)", lock->token);
1177 dav_headers = curl_slist_append(dav_headers, if_header);
1179 out_buffer.size = 41;
1180 out_data = xmalloc(out_buffer.size + 1);
1181 i = snprintf(out_data, out_buffer.size + 1, "%s\n", sha1_to_hex(sha1));
1182 if (i != out_buffer.size) {
1183 fprintf(stderr, "Unable to initialize PUT request body\n");
1184 return 0;
1186 out_buffer.posn = 0;
1187 out_buffer.buffer = out_data;
1189 slot = get_active_slot();
1190 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1191 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size);
1192 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1193 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1194 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1195 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1196 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1197 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1198 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1200 if (start_active_slot(slot)) {
1201 run_active_slot(slot);
1202 free(out_data);
1203 free(if_header);
1204 if (slot->curl_result != CURLE_OK) {
1205 fprintf(stderr,
1206 "PUT error: curl result=%d, HTTP code=%ld\n",
1207 slot->curl_result, slot->http_code);
1208 /* We should attempt recovery? */
1209 return 0;
1211 } else {
1212 free(out_data);
1213 free(if_header);
1214 fprintf(stderr, "Unable to start PUT request\n");
1215 return 0;
1218 return 1;
1221 int main(int argc, char **argv)
1223 struct transfer_request *request;
1224 struct transfer_request *next_request;
1225 int nr_refspec = 0;
1226 char **refspec = NULL;
1227 int do_remote_update;
1228 int new_branch;
1229 int force_this;
1230 char *local_ref;
1231 unsigned char local_sha1[20];
1232 struct object *local_object = NULL;
1233 char *remote_ref = NULL;
1234 unsigned char remote_sha1[20];
1235 struct active_lock *remote_lock;
1236 char *remote_path = NULL;
1237 int rc = 0;
1238 int i;
1240 setup_git_directory();
1241 setup_ident();
1243 remote = xmalloc(sizeof(*remote));
1244 remote->url = NULL;
1245 remote->packs = NULL;
1247 argv++;
1248 for (i = 1; i < argc; i++, argv++) {
1249 char *arg = *argv;
1251 if (*arg == '-') {
1252 if (!strcmp(arg, "--complete")) {
1253 push_all = 1;
1254 continue;
1256 if (!strcmp(arg, "--force")) {
1257 force_all = 1;
1258 continue;
1260 if (!strcmp(arg, "--verbose")) {
1261 push_verbosely = 1;
1262 continue;
1264 usage(http_push_usage);
1266 if (!remote->url) {
1267 remote->url = arg;
1268 continue;
1270 refspec = argv;
1271 nr_refspec = argc - i;
1272 break;
1275 if (!remote->url)
1276 usage(http_push_usage);
1278 memset(remote_dir_exists, 0, 256);
1280 http_init();
1282 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
1283 default_headers = curl_slist_append(default_headers, "Range:");
1284 default_headers = curl_slist_append(default_headers, "Destination:");
1285 default_headers = curl_slist_append(default_headers, "If:");
1286 default_headers = curl_slist_append(default_headers,
1287 "Pragma: no-cache");
1289 /* Verify DAV compliance/lock support */
1290 if (!locking_available()) {
1291 fprintf(stderr, "Error: no DAV locking support on remote repo %s\n", remote->url);
1292 rc = 1;
1293 goto cleanup;
1296 /* Process each refspec */
1297 for (i = 0; i < nr_refspec; i++) {
1298 char *ep;
1299 force_this = 0;
1300 do_remote_update = 0;
1301 new_branch = 0;
1302 local_ref = refspec[i];
1303 if (*local_ref == '+') {
1304 force_this = 1;
1305 local_ref++;
1307 ep = strchr(local_ref, ':');
1308 if (ep) {
1309 remote_ref = ep + 1;
1310 *ep = 0;
1312 else
1313 remote_ref = local_ref;
1315 /* Lock remote branch ref */
1316 if (remote_path)
1317 free(remote_path);
1318 remote_path = xmalloc(strlen(remote_ref) + 12);
1319 sprintf(remote_path, "refs/heads/%s", remote_ref);
1320 remote_lock = lock_remote(remote_path, LOCK_TIME);
1321 if (remote_lock == NULL) {
1322 fprintf(stderr, "Unable to lock remote branch %s\n",
1323 remote_ref);
1324 rc = 1;
1325 continue;
1328 /* Resolve local and remote refs */
1329 if (fetch_ref(remote_ref, remote_sha1) != 0) {
1330 fprintf(stderr,
1331 "Remote branch %s does not exist on %s\n",
1332 remote_ref, remote->url);
1333 new_branch = 1;
1335 if (get_sha1(local_ref, local_sha1) != 0) {
1336 fprintf(stderr, "Error resolving local branch %s\n",
1337 local_ref);
1338 rc = 1;
1339 goto unlock;
1342 /* Find relationship between local and remote */
1343 local_object = parse_object(local_sha1);
1344 if (!local_object) {
1345 fprintf(stderr, "Unable to parse local object %s\n",
1346 sha1_to_hex(local_sha1));
1347 rc = 1;
1348 goto unlock;
1349 } else if (new_branch) {
1350 do_remote_update = 1;
1351 } else {
1352 if (!memcmp(local_sha1, remote_sha1, 20)) {
1353 fprintf(stderr,
1354 "* %s: same as branch '%s' of %s\n",
1355 local_ref, remote_ref, remote->url);
1356 } else if (is_ancestor(remote_sha1,
1357 (struct commit *)local_object)) {
1358 fprintf(stderr,
1359 "Remote %s will fast-forward to local %s\n",
1360 remote_ref, local_ref);
1361 do_remote_update = 1;
1362 } else if (force_all || force_this) {
1363 fprintf(stderr,
1364 "* %s on %s does not fast forward to local branch '%s', overwriting\n",
1365 remote_ref, remote->url, local_ref);
1366 do_remote_update = 1;
1367 } else {
1368 fprintf(stderr,
1369 "* %s on %s does not fast forward to local branch '%s'\n",
1370 remote_ref, remote->url, local_ref);
1371 rc = 1;
1372 goto unlock;
1376 /* Generate and check list of required objects */
1377 pushing = 0;
1378 if (do_remote_update || push_all)
1379 fetch_indices();
1380 get_delta(push_all ? NULL : remote_sha1,
1381 local_object, remote_lock);
1382 finish_all_active_slots();
1384 /* Push missing objects to remote, this would be a
1385 convenient time to pack them first if appropriate. */
1386 pushing = 1;
1387 fill_active_slots();
1388 finish_all_active_slots();
1390 /* Update the remote branch if all went well */
1391 if (do_remote_update) {
1392 if (!aborted && update_remote(local_sha1,
1393 remote_lock)) {
1394 fprintf(stderr, "%s remote branch %s\n",
1395 new_branch ? "Created" : "Updated",
1396 remote_ref);
1397 } else {
1398 fprintf(stderr,
1399 "Unable to %s remote branch %s\n",
1400 new_branch ? "create" : "update",
1401 remote_ref);
1402 rc = 1;
1403 goto unlock;
1407 unlock:
1408 unlock_remote(remote_lock);
1409 free(remote_path);
1412 cleanup:
1413 free(remote);
1415 curl_slist_free_all(no_pragma_header);
1416 curl_slist_free_all(default_headers);
1418 http_cleanup();
1420 request = request_queue_head;
1421 while (request != NULL) {
1422 next_request = request->next;
1423 release_request(request);
1424 request = next_request;
1427 return rc;