11 static const char http_push_usage[] =
12 "git-http-push [--complete] [--force] [--verbose] <url> <ref> [<ref>...]\n";
19 #define XML_STATUS_OK 1
20 #define XML_STATUS_ERROR 0
23 #define RANGE_HEADER_SIZE 30
26 #define DAV_LOCK "LOCK"
27 #define DAV_MKCOL "MKCOL"
28 #define DAV_MOVE "MOVE"
29 #define DAV_PROPFIND "PROPFIND"
31 #define DAV_UNLOCK "UNLOCK"
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>"
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;
67 struct packed_git *packs;
70 static struct repo *remote = NULL;
83 struct transfer_request
85 unsigned char sha1[20];
88 struct active_lock *lock;
89 struct curl_slist *headers;
91 char filename[PATH_MAX];
92 char tmpfile[PATH_MAX];
93 enum transfer_state state;
95 char errorstr[CURL_ERROR_SIZE];
97 unsigned char real_sha1[20];
102 struct active_request_slot *slot;
103 struct transfer_request *next;
106 static struct transfer_request *request_queue_head = NULL;
113 void (*userFunc)(struct xml_ctx *ctx, int tag_closed);
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;
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/");
148 memcpy(posn, hex, 2);
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;
164 request->state = ABORTED;
170 static void start_mkcol(struct transfer_request *request)
172 char *hex = sha1_to_hex(request->sha1);
173 struct active_request_slot *slot;
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/");
181 memcpy(posn, hex, 2);
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;
198 request->state = ABORTED;
204 static void start_put(struct transfer_request *request)
206 char *hex = sha1_to_hex(request->sha1);
207 struct active_request_slot *slot;
217 unpacked = read_sha1_file(request->sha1, type, &len);
218 hdrlen = sprintf(hdr, "%s %lu", type, len) + 1;
221 memset(&stream, 0, sizeof(stream));
222 deflateInit(&stream, Z_BEST_COMPRESSION);
223 size = deflateBound(&stream, len + hdrlen);
224 request->buffer.buffer = xmalloc(size);
227 stream.next_out = request->buffer.buffer;
228 stream.avail_out = size;
231 stream.next_in = (void *)hdr;
232 stream.avail_in = hdrlen;
233 while (deflate(&stream, 0) == Z_OK)
236 /* Then the data itself.. */
237 stream.next_in = unpacked;
238 stream.avail_in = len;
239 while (deflate(&stream, Z_FINISH) == Z_OK)
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/");
253 memcpy(posn, hex, 2);
256 strcpy(posn, hex + 2);
257 request->dest = xmalloc(strlen(request->url) + 14);
258 sprintf(request->dest, "Destination: %s", request->url);
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;
280 request->state = ABORTED;
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;
306 request->state = ABORTED;
312 static int refresh_lock(struct active_lock *lock)
314 struct active_request_slot *slot;
316 char timeout_header[25];
317 struct curl_slist *dav_headers = NULL;
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);
340 lock->start_time = time(NULL);
345 lock->refreshing = 0;
346 curl_slist_free_all(dav_headers);
352 static void finish_request(struct transfer_request *request)
354 time_t current_time = time(NULL);
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
364 if (time_remaining < LOCK_REFRESH && !request->lock->refreshing) {
365 if (!refresh_lock(request->lock)) {
366 fprintf(stderr, "Unable to refresh remote lock\n");
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) {
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;
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;
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;
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;
405 } else if (request->state == RUN_PUT) {
406 if (request->curl_result == CURLE_OK) {
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;
415 } else if (request->state == RUN_MOVE) {
416 if (request->curl_result == CURLE_OK) {
420 sha1_to_hex(request->sha1));
421 request->state = COMPLETE;
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;
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;
439 while (entry->next != NULL && entry->next != request)
441 if (entry->next == request)
442 entry->next = entry->next->next;
445 if (request->url != NULL)
450 void fill_active_slots(void)
452 struct transfer_request *request = request_queue_head;
453 struct active_request_slot *slot = active_queue_head;
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]])
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);
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;
492 target = find_sha1_pack(sha1, remote->packs);
496 request = xmalloc(sizeof(*request));
497 memcpy(request->sha1, sha1, 20);
499 request->lock = lock;
500 request->headers = NULL;
501 request->state = NEED_CHECK;
502 request->next = request_queue_head;
503 request_queue_head = request;
509 static int fetch_index(unsigned char *sha1)
511 char *hex = sha1_to_hex(sha1);
514 char tmpfile[PATH_MAX];
516 char range[RANGE_HEADER_SIZE];
517 struct curl_slist *range_header = NULL;
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) {
532 return error("Unable to verify pack %s is available",
536 return error("Unable to start request");
539 if (has_pack_index(sha1))
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");
551 return error("Unable to open local file %s for pack index",
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);
569 "Resuming fetch of index for pack %s at byte %ld\n",
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) {
581 return error("Unable to get pack index %s\n%s", url,
587 return error("Unable to start request");
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))
603 new_pack = parse_pack_index(sha1);
604 new_pack->next = remote->packs;
605 remote->packs = new_pack;
609 static int fetch_indices(void)
611 unsigned char sha1[20];
613 struct buffer buffer;
617 struct active_request_slot *slot;
619 data = xmalloc(4096);
620 memset(data, 0, 4096);
623 buffer.buffer = data;
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) {
641 if (slot->http_code == 404)
644 return error("%s", curl_errorstr);
649 return error("Unable to start request");
653 data = buffer.buffer;
654 while (i < buffer.posn) {
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);
667 while (data[i] != '\n')
677 static inline int needs_quote(int ch)
680 case '/': case '-': case '.':
681 case 'A'...'Z': case 'a'...'z': case '0'...'9':
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)
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++)
704 len += 2; /* extra two hex plus replacement % */
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)) {
711 *dp++ = hex((ch >> 4) & 0xF);
712 *dp++ = hex(ch & 0xF);
722 int fetch_ref(char *ref, unsigned char *sha1)
726 struct buffer buffer;
727 char *base = remote->url;
728 struct active_request_slot *slot;
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);
746 return error("Unable to start request");
750 get_sha1_hex(hex, sha1);
754 static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed)
756 int *lock_flags = (int *)ctx->userData;
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))
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);
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, ':');
806 new_len = strlen(ctx->name) + strlen(c) + 2;
808 if (new_len > ctx->len) {
809 ctx->name = xrealloc(ctx->name, new_len);
812 strcat(ctx->name, ".");
813 strcat(ctx->name, c);
820 ctx->userFunc(ctx, 0);
824 xml_end_tag(void *userData, const char *name)
826 struct xml_ctx *ctx = (struct xml_ctx *)userData;
827 const char *c = index(name, ':');
830 ctx->userFunc(ctx, 1);
837 ep = ctx->name + strlen(ctx->name) - strlen(c) - 1;
842 xml_cdata(void *userData, const XML_Char *s, int len)
844 struct xml_ctx *ctx = (struct xml_ctx *)userData;
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;
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;
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, '/');
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) {
884 "Unable to create branch path %s\n",
890 fprintf(stderr, "Unable to start request\n");
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);
902 out_buffer.buffer = out_data;
904 in_buffer.size = 4096;
905 in_data = xmalloc(in_buffer.size);
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);
936 ctx.userFunc = handle_new_lock_ctx;
937 ctx.userData = new_lock;
938 XML_SetUserData(parser, &ctx);
939 XML_SetElementHandler(parser, xml_start_tag,
941 XML_SetCharacterDataHandler(parser, xml_cdata);
942 result = XML_Parse(parser, in_buffer.buffer,
945 if (result != XML_STATUS_OK) {
946 fprintf(stderr, "XML error: %s\n",
948 XML_GetErrorCode(parser)));
949 new_lock->timeout = -1;
953 fprintf(stderr, "Unable to start request\n");
956 curl_slist_free_all(dav_headers);
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);
970 new_lock->start_time = time(NULL);
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;
983 lock_token_header = xmalloc(strlen(lock->token) + 31);
984 sprintf(lock_token_header, "Lock-Token: <opaquelocktoken:%s>",
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)
999 fprintf(stderr, "Got HTTP error %ld\n",
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)
1011 /* Freeing the token causes a segfault...
1019 static int locking_available(void)
1021 struct active_request_slot *slot;
1022 struct buffer in_buffer;
1023 struct buffer out_buffer;
1026 XML_Parser parser = XML_ParserCreate(NULL);
1027 enum XML_Status result;
1028 struct curl_slist *dav_headers = NULL;
1032 out_buffer.size = strlen(PROPFIND_REQUEST) + strlen(remote->url) - 2;
1033 out_data = xmalloc(out_buffer.size + 1);
1034 snprintf(out_data, out_buffer.size + 1, PROPFIND_REQUEST, remote->url);
1035 out_buffer.posn = 0;
1036 out_buffer.buffer = out_data;
1038 in_buffer.size = 4096;
1039 in_data = xmalloc(in_buffer.size);
1041 in_buffer.buffer = in_data;
1043 dav_headers = curl_slist_append(dav_headers, "Depth: 0");
1044 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1046 slot = get_active_slot();
1047 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1048 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size);
1049 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1050 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1051 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1052 curl_easy_setopt(slot->curl, CURLOPT_URL, remote->url);
1053 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1054 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1055 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1057 if (start_active_slot(slot)) {
1058 run_active_slot(slot);
1059 if (slot->curl_result == CURLE_OK) {
1060 ctx.name = xcalloc(10, 1);
1063 ctx.userFunc = handle_lockprop_ctx;
1064 ctx.userData = &lock_flags;
1065 XML_SetUserData(parser, &ctx);
1066 XML_SetElementHandler(parser, xml_start_tag,
1068 result = XML_Parse(parser, in_buffer.buffer,
1072 if (result != XML_STATUS_OK) {
1073 fprintf(stderr, "XML error: %s\n",
1075 XML_GetErrorCode(parser)));
1080 fprintf(stderr, "Unable to start request\n");
1084 free(in_buffer.buffer);
1085 curl_slist_free_all(dav_headers);
1090 static int is_ancestor(unsigned char *sha1, struct commit *commit)
1092 struct commit_list *parents;
1094 if (parse_commit(commit))
1096 parents = commit->parents;
1097 for (; parents; parents = parents->next) {
1098 if (!memcmp(sha1, parents->item->object.sha1, 20)) {
1100 } else if (parents->item->object.type == commit_type) {
1103 (struct commit *)&parents->item->object
1111 static void get_delta(unsigned char *sha1, struct object *obj,
1112 struct active_lock *lock)
1114 struct commit *commit;
1115 struct commit_list *parents;
1117 struct tree_entry_list *entry;
1119 if (sha1 && !memcmp(sha1, obj->sha1, 20))
1125 if (obj->type == commit_type) {
1127 fprintf(stderr, "walk %s\n", sha1_to_hex(obj->sha1));
1128 add_request(obj->sha1, lock);
1129 commit = (struct commit *)obj;
1130 if (parse_commit(commit)) {
1131 fprintf(stderr, "Error parsing commit %s\n",
1132 sha1_to_hex(obj->sha1));
1136 parents = commit->parents;
1137 for (; parents; parents = parents->next)
1139 memcmp(sha1, parents->item->object.sha1, 20))
1140 get_delta(sha1, &parents->item->object,
1142 get_delta(sha1, &commit->tree->object, lock);
1143 } else if (obj->type == tree_type) {
1145 fprintf(stderr, "walk %s\n", sha1_to_hex(obj->sha1));
1146 add_request(obj->sha1, lock);
1147 tree = (struct tree *)obj;
1148 if (parse_tree(tree)) {
1149 fprintf(stderr, "Error parsing tree %s\n",
1150 sha1_to_hex(obj->sha1));
1154 entry = tree->entries;
1155 tree->entries = NULL;
1157 struct tree_entry_list *next = entry->next;
1158 get_delta(sha1, entry->item.any, lock);
1163 } else if (obj->type == blob_type || obj->type == tag_type) {
1164 add_request(obj->sha1, lock);
1168 static int update_remote(unsigned char *sha1, struct active_lock *lock)
1170 struct active_request_slot *slot;
1173 struct buffer out_buffer;
1174 struct curl_slist *dav_headers = NULL;
1177 if_header = xmalloc(strlen(lock->token) + 25);
1178 sprintf(if_header, "If: (<opaquelocktoken:%s>)", lock->token);
1179 dav_headers = curl_slist_append(dav_headers, if_header);
1181 out_buffer.size = 41;
1182 out_data = xmalloc(out_buffer.size + 1);
1183 i = snprintf(out_data, out_buffer.size + 1, "%s\n", sha1_to_hex(sha1));
1184 if (i != out_buffer.size) {
1185 fprintf(stderr, "Unable to initialize PUT request body\n");
1188 out_buffer.posn = 0;
1189 out_buffer.buffer = out_data;
1191 slot = get_active_slot();
1192 curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1193 curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size);
1194 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1195 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1196 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1197 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1198 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1199 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1200 curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1202 if (start_active_slot(slot)) {
1203 run_active_slot(slot);
1206 if (slot->curl_result != CURLE_OK) {
1208 "PUT error: curl result=%d, HTTP code=%ld\n",
1209 slot->curl_result, slot->http_code);
1210 /* We should attempt recovery? */
1216 fprintf(stderr, "Unable to start PUT request\n");
1223 int main(int argc, char **argv)
1225 struct transfer_request *request;
1226 struct transfer_request *next_request;
1228 char **refspec = NULL;
1229 int do_remote_update;
1233 unsigned char local_sha1[20];
1234 struct object *local_object = NULL;
1235 char *remote_ref = NULL;
1236 unsigned char remote_sha1[20];
1237 struct active_lock *remote_lock;
1238 char *remote_path = NULL;
1244 remote = xmalloc(sizeof(*remote));
1246 remote->packs = NULL;
1249 for (i = 1; i < argc; i++, argv++) {
1253 if (!strcmp(arg, "--complete")) {
1257 if (!strcmp(arg, "--force")) {
1261 if (!strcmp(arg, "--verbose")) {
1265 usage(http_push_usage);
1272 nr_refspec = argc - i;
1276 memset(remote_dir_exists, 0, 256);
1280 no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
1281 default_headers = curl_slist_append(default_headers, "Range:");
1282 default_headers = curl_slist_append(default_headers, "Destination:");
1283 default_headers = curl_slist_append(default_headers, "If:");
1284 default_headers = curl_slist_append(default_headers,
1285 "Pragma: no-cache");
1287 /* Verify DAV compliance/lock support */
1288 if (!locking_available()) {
1289 fprintf(stderr, "Error: no DAV locking support on remote repo %s\n", remote->url);
1294 /* Process each refspec */
1295 for (i = 0; i < nr_refspec; i++) {
1298 do_remote_update = 0;
1300 local_ref = refspec[i];
1301 if (*local_ref == '+') {
1305 ep = strchr(local_ref, ':');
1307 remote_ref = ep + 1;
1311 remote_ref = local_ref;
1313 /* Lock remote branch ref */
1316 remote_path = xmalloc(strlen(remote_ref) + 12);
1317 sprintf(remote_path, "refs/heads/%s", remote_ref);
1318 remote_lock = lock_remote(remote_path, LOCK_TIME);
1319 if (remote_lock == NULL) {
1320 fprintf(stderr, "Unable to lock remote branch %s\n",
1326 /* Resolve local and remote refs */
1327 if (fetch_ref(remote_ref, remote_sha1) != 0) {
1329 "Remote branch %s does not exist on %s\n",
1330 remote_ref, remote->url);
1333 if (get_sha1(local_ref, local_sha1) != 0) {
1334 fprintf(stderr, "Error resolving local branch %s\n",
1340 /* Find relationship between local and remote */
1341 local_object = parse_object(local_sha1);
1342 if (!local_object) {
1343 fprintf(stderr, "Unable to parse local object %s\n",
1344 sha1_to_hex(local_sha1));
1347 } else if (new_branch) {
1348 do_remote_update = 1;
1350 if (!memcmp(local_sha1, remote_sha1, 20)) {
1352 "* %s: same as branch '%s' of %s\n",
1353 local_ref, remote_ref, remote->url);
1354 } else if (is_ancestor(remote_sha1,
1355 (struct commit *)local_object)) {
1357 "Remote %s will fast-forward to local %s\n",
1358 remote_ref, local_ref);
1359 do_remote_update = 1;
1360 } else if (force_all || force_this) {
1362 "* %s on %s does not fast forward to local branch '%s', overwriting\n",
1363 remote_ref, remote->url, local_ref);
1364 do_remote_update = 1;
1367 "* %s on %s does not fast forward to local branch '%s'\n",
1368 remote_ref, remote->url, local_ref);
1374 /* Generate and check list of required objects */
1376 if (do_remote_update || push_all)
1378 get_delta(push_all ? NULL : remote_sha1,
1379 local_object, remote_lock);
1380 finish_all_active_slots();
1382 /* Push missing objects to remote, this would be a
1383 convenient time to pack them first if appropriate. */
1385 fill_active_slots();
1386 finish_all_active_slots();
1388 /* Update the remote branch if all went well */
1389 if (do_remote_update) {
1390 if (!aborted && update_remote(local_sha1,
1392 fprintf(stderr, "%s remote branch %s\n",
1393 new_branch ? "Created" : "Updated",
1397 "Unable to %s remote branch %s\n",
1398 new_branch ? "create" : "update",
1406 unlock_remote(remote_lock);
1413 curl_slist_free_all(no_pragma_header);
1414 curl_slist_free_all(default_headers);
1418 request = request_queue_head;
1419 while (request != NULL) {
1420 next_request = request->next;
1421 release_request(request);
1422 request = next_request;