debian: new upstream release
[git/debian.git] / http-push.c
bloba704f490fdb2c2144b47a5629ed9ee0643ab0d83
1 #include "git-compat-util.h"
2 #include "environment.h"
3 #include "hex.h"
4 #include "repository.h"
5 #include "commit.h"
6 #include "tag.h"
7 #include "blob.h"
8 #include "http.h"
9 #include "refs.h"
10 #include "diff.h"
11 #include "revision.h"
12 #include "exec-cmd.h"
13 #include "remote.h"
14 #include "list-objects.h"
15 #include "setup.h"
16 #include "sigchain.h"
17 #include "strvec.h"
18 #include "tree.h"
19 #include "tree-walk.h"
20 #include "packfile.h"
21 #include "object-store-ll.h"
22 #include "commit-reach.h"
24 #ifdef EXPAT_NEEDS_XMLPARSE_H
25 #include <xmlparse.h>
26 #else
27 #include <expat.h>
28 #endif
30 static const char http_push_usage[] =
31 "git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
33 #ifndef XML_STATUS_OK
34 enum XML_Status {
35 XML_STATUS_OK = 1,
36 XML_STATUS_ERROR = 0
38 #define XML_STATUS_OK 1
39 #define XML_STATUS_ERROR 0
40 #endif
42 #define PREV_BUF_SIZE 4096
44 /* DAV methods */
45 #define DAV_LOCK "LOCK"
46 #define DAV_MKCOL "MKCOL"
47 #define DAV_MOVE "MOVE"
48 #define DAV_PROPFIND "PROPFIND"
49 #define DAV_PUT "PUT"
50 #define DAV_UNLOCK "UNLOCK"
51 #define DAV_DELETE "DELETE"
53 /* DAV lock flags */
54 #define DAV_PROP_LOCKWR (1u << 0)
55 #define DAV_PROP_LOCKEX (1u << 1)
56 #define DAV_LOCK_OK (1u << 2)
58 /* DAV XML properties */
59 #define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
60 #define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
61 #define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
62 #define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
63 #define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
64 #define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
65 #define DAV_PROPFIND_RESP ".multistatus.response"
66 #define DAV_PROPFIND_NAME ".multistatus.response.href"
67 #define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
69 /* DAV request body templates */
70 #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>"
71 #define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
72 #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>"
74 #define LOCK_TIME 600
75 #define LOCK_REFRESH 30
77 /* Remember to update object flag allocation in object.h */
78 #define LOCAL (1u<<11)
79 #define REMOTE (1u<<12)
80 #define FETCHING (1u<<13)
81 #define PUSHING (1u<<14)
83 /* We allow "recursive" symbolic refs. Only within reason, though */
84 #define MAXDEPTH 5
86 static int pushing;
87 static int aborted;
88 static signed char remote_dir_exists[256];
90 static int push_verbosely;
91 static int push_all = MATCH_REFS_NONE;
92 static int force_all;
93 static int dry_run;
94 static int helper_status;
96 static struct object_list *objects;
98 struct repo {
99 char *url;
100 char *path;
101 int path_len;
102 int has_info_refs;
103 int can_update_info_refs;
104 int has_info_packs;
105 struct packed_git *packs;
106 struct remote_lock *locks;
109 static struct repo *repo;
111 enum transfer_state {
112 NEED_FETCH,
113 RUN_FETCH_LOOSE,
114 RUN_FETCH_PACKED,
115 NEED_PUSH,
116 RUN_MKCOL,
117 RUN_PUT,
118 RUN_MOVE,
119 ABORTED,
120 COMPLETE
123 struct transfer_request {
124 struct object *obj;
125 struct packed_git *target;
126 char *url;
127 char *dest;
128 struct remote_lock *lock;
129 struct curl_slist *headers;
130 struct buffer buffer;
131 enum transfer_state state;
132 CURLcode curl_result;
133 char errorstr[CURL_ERROR_SIZE];
134 long http_code;
135 void *userData;
136 struct active_request_slot *slot;
137 struct transfer_request *next;
140 static struct transfer_request *request_queue_head;
142 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 {
151 char *url;
152 char *owner;
153 char *token;
154 char tmpfile_suffix[GIT_MAX_HEXSZ + 1];
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 {
170 char *path;
171 void (*userFunc)(struct remote_ls_ctx *ls);
172 void *userData;
173 int flags;
174 char *dentry_name;
175 int dentry_flags;
176 struct remote_ls_ctx *parent;
179 /* get_dav_token_headers options */
180 enum dav_header_flag {
181 DAV_HEADER_IF = (1u << 0),
182 DAV_HEADER_LOCK = (1u << 1),
183 DAV_HEADER_TIMEOUT = (1u << 2)
186 static char *xml_entities(const char *s)
188 struct strbuf buf = STRBUF_INIT;
189 strbuf_addstr_xml_quoted(&buf, s);
190 return strbuf_detach(&buf, NULL);
193 static void curl_setup_http_get(CURL *curl, const char *url,
194 const char *custom_req)
196 curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
197 curl_easy_setopt(curl, CURLOPT_URL, url);
198 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, custom_req);
199 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_null);
202 static void curl_setup_http(CURL *curl, const char *url,
203 const char *custom_req, struct buffer *buffer,
204 curl_write_callback write_fn)
206 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
207 curl_easy_setopt(curl, CURLOPT_URL, url);
208 curl_easy_setopt(curl, CURLOPT_INFILE, buffer);
209 curl_easy_setopt(curl, CURLOPT_INFILESIZE, buffer->buf.len);
210 curl_easy_setopt(curl, CURLOPT_READFUNCTION, fread_buffer);
211 curl_easy_setopt(curl, CURLOPT_SEEKFUNCTION, seek_buffer);
212 curl_easy_setopt(curl, CURLOPT_SEEKDATA, buffer);
213 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_fn);
214 curl_easy_setopt(curl, CURLOPT_NOBODY, 0);
215 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, custom_req);
216 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
219 static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum dav_header_flag options)
221 struct strbuf buf = STRBUF_INIT;
222 struct curl_slist *dav_headers = http_copy_default_headers();
224 if (options & DAV_HEADER_IF) {
225 strbuf_addf(&buf, "If: (<%s>)", lock->token);
226 dav_headers = curl_slist_append(dav_headers, buf.buf);
227 strbuf_reset(&buf);
229 if (options & DAV_HEADER_LOCK) {
230 strbuf_addf(&buf, "Lock-Token: <%s>", lock->token);
231 dav_headers = curl_slist_append(dav_headers, buf.buf);
232 strbuf_reset(&buf);
234 if (options & DAV_HEADER_TIMEOUT) {
235 strbuf_addf(&buf, "Timeout: Second-%ld", lock->timeout);
236 dav_headers = curl_slist_append(dav_headers, buf.buf);
237 strbuf_reset(&buf);
239 strbuf_release(&buf);
241 return dav_headers;
244 static void finish_request(struct transfer_request *request);
245 static void release_request(struct transfer_request *request);
247 static void process_response(void *callback_data)
249 struct transfer_request *request =
250 (struct transfer_request *)callback_data;
252 finish_request(request);
255 static void start_fetch_loose(struct transfer_request *request)
257 struct active_request_slot *slot;
258 struct http_object_request *obj_req;
260 obj_req = new_http_object_request(repo->url, &request->obj->oid);
261 if (!obj_req) {
262 request->state = ABORTED;
263 return;
266 slot = obj_req->slot;
267 slot->callback_func = process_response;
268 slot->callback_data = request;
269 request->slot = slot;
270 request->userData = obj_req;
272 /* Try to get the request started, abort the request on error */
273 request->state = RUN_FETCH_LOOSE;
274 if (!start_active_slot(slot)) {
275 fprintf(stderr, "Unable to start GET request\n");
276 repo->can_update_info_refs = 0;
277 release_http_object_request(obj_req);
278 release_request(request);
282 static void start_mkcol(struct transfer_request *request)
284 char *hex = oid_to_hex(&request->obj->oid);
285 struct active_request_slot *slot;
287 request->url = get_remote_object_url(repo->url, hex, 1);
289 slot = get_active_slot();
290 slot->callback_func = process_response;
291 slot->callback_data = request;
292 curl_setup_http_get(slot->curl, request->url, DAV_MKCOL);
293 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
295 if (start_active_slot(slot)) {
296 request->slot = slot;
297 request->state = RUN_MKCOL;
298 } else {
299 request->state = ABORTED;
300 FREE_AND_NULL(request->url);
304 static void start_fetch_packed(struct transfer_request *request)
306 struct packed_git *target;
308 struct transfer_request *check_request = request_queue_head;
309 struct http_pack_request *preq;
311 target = find_sha1_pack(request->obj->oid.hash, repo->packs);
312 if (!target) {
313 fprintf(stderr, "Unable to fetch %s, will not be able to update server info refs\n", oid_to_hex(&request->obj->oid));
314 repo->can_update_info_refs = 0;
315 release_request(request);
316 return;
318 close_pack_index(target);
319 request->target = target;
321 fprintf(stderr, "Fetching pack %s\n",
322 hash_to_hex(target->hash));
323 fprintf(stderr, " which contains %s\n", oid_to_hex(&request->obj->oid));
325 preq = new_http_pack_request(target->hash, repo->url);
326 if (!preq) {
327 repo->can_update_info_refs = 0;
328 return;
331 /* Make sure there isn't another open request for this pack */
332 while (check_request) {
333 if (check_request->state == RUN_FETCH_PACKED &&
334 !strcmp(check_request->url, preq->url)) {
335 release_http_pack_request(preq);
336 release_request(request);
337 return;
339 check_request = check_request->next;
342 preq->slot->callback_func = process_response;
343 preq->slot->callback_data = request;
344 request->slot = preq->slot;
345 request->userData = preq;
347 /* Try to get the request started, abort the request on error */
348 request->state = RUN_FETCH_PACKED;
349 if (!start_active_slot(preq->slot)) {
350 fprintf(stderr, "Unable to start GET request\n");
351 release_http_pack_request(preq);
352 repo->can_update_info_refs = 0;
353 release_request(request);
357 static void start_put(struct transfer_request *request)
359 char *hex = oid_to_hex(&request->obj->oid);
360 struct active_request_slot *slot;
361 struct strbuf buf = STRBUF_INIT;
362 enum object_type type;
363 char hdr[50];
364 void *unpacked;
365 unsigned long len;
366 int hdrlen;
367 ssize_t size;
368 git_zstream stream;
370 unpacked = repo_read_object_file(the_repository, &request->obj->oid,
371 &type, &len);
372 hdrlen = format_object_header(hdr, sizeof(hdr), type, len);
374 /* Set it up */
375 git_deflate_init(&stream, zlib_compression_level);
376 size = git_deflate_bound(&stream, len + hdrlen);
377 strbuf_init(&request->buffer.buf, size);
378 request->buffer.posn = 0;
380 /* Compress it */
381 stream.next_out = (unsigned char *)request->buffer.buf.buf;
382 stream.avail_out = size;
384 /* First header.. */
385 stream.next_in = (void *)hdr;
386 stream.avail_in = hdrlen;
387 while (git_deflate(&stream, 0) == Z_OK)
388 ; /* nothing */
390 /* Then the data itself.. */
391 stream.next_in = unpacked;
392 stream.avail_in = len;
393 while (git_deflate(&stream, Z_FINISH) == Z_OK)
394 ; /* nothing */
395 git_deflate_end(&stream);
396 free(unpacked);
398 request->buffer.buf.len = stream.total_out;
400 strbuf_addstr(&buf, "Destination: ");
401 append_remote_object_url(&buf, repo->url, hex, 0);
402 request->dest = strbuf_detach(&buf, NULL);
404 append_remote_object_url(&buf, repo->url, hex, 0);
405 strbuf_add(&buf, request->lock->tmpfile_suffix, the_hash_algo->hexsz + 1);
406 request->url = strbuf_detach(&buf, NULL);
408 slot = get_active_slot();
409 slot->callback_func = process_response;
410 slot->callback_data = request;
411 curl_setup_http(slot->curl, request->url, DAV_PUT,
412 &request->buffer, fwrite_null);
414 if (start_active_slot(slot)) {
415 request->slot = slot;
416 request->state = RUN_PUT;
417 } else {
418 request->state = ABORTED;
419 FREE_AND_NULL(request->url);
423 static void start_move(struct transfer_request *request)
425 struct active_request_slot *slot;
426 struct curl_slist *dav_headers = http_copy_default_headers();
428 slot = get_active_slot();
429 slot->callback_func = process_response;
430 slot->callback_data = request;
431 curl_setup_http_get(slot->curl, request->url, DAV_MOVE);
432 dav_headers = curl_slist_append(dav_headers, request->dest);
433 dav_headers = curl_slist_append(dav_headers, "Overwrite: T");
434 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
436 if (start_active_slot(slot)) {
437 request->slot = slot;
438 request->state = RUN_MOVE;
439 } else {
440 request->state = ABORTED;
441 FREE_AND_NULL(request->url);
445 static int refresh_lock(struct remote_lock *lock)
447 struct active_request_slot *slot;
448 struct slot_results results;
449 struct curl_slist *dav_headers;
450 int rc = 0;
452 lock->refreshing = 1;
454 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF | DAV_HEADER_TIMEOUT);
456 slot = get_active_slot();
457 slot->results = &results;
458 curl_setup_http_get(slot->curl, lock->url, DAV_LOCK);
459 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
461 if (start_active_slot(slot)) {
462 run_active_slot(slot);
463 if (results.curl_result != CURLE_OK) {
464 fprintf(stderr, "LOCK HTTP error %ld\n",
465 results.http_code);
466 } else {
467 lock->start_time = time(NULL);
468 rc = 1;
472 lock->refreshing = 0;
473 curl_slist_free_all(dav_headers);
475 return rc;
478 static void check_locks(void)
480 struct remote_lock *lock = repo->locks;
481 time_t current_time = time(NULL);
482 int time_remaining;
484 while (lock) {
485 time_remaining = lock->start_time + lock->timeout -
486 current_time;
487 if (!lock->refreshing && time_remaining < LOCK_REFRESH) {
488 if (!refresh_lock(lock)) {
489 fprintf(stderr,
490 "Unable to refresh lock for %s\n",
491 lock->url);
492 aborted = 1;
493 return;
496 lock = lock->next;
500 static void release_request(struct transfer_request *request)
502 struct transfer_request *entry = request_queue_head;
504 if (request == request_queue_head) {
505 request_queue_head = request->next;
506 } else {
507 while (entry && entry->next != request)
508 entry = entry->next;
509 if (entry)
510 entry->next = request->next;
513 free(request->url);
514 free(request);
517 static void finish_request(struct transfer_request *request)
519 struct http_pack_request *preq;
520 struct http_object_request *obj_req;
522 request->curl_result = request->slot->curl_result;
523 request->http_code = request->slot->http_code;
524 request->slot = NULL;
526 /* Keep locks active */
527 check_locks();
529 if (request->headers)
530 curl_slist_free_all(request->headers);
532 /* URL is reused for MOVE after PUT and used during FETCH */
533 if (request->state != RUN_PUT && request->state != RUN_FETCH_PACKED) {
534 FREE_AND_NULL(request->url);
537 if (request->state == RUN_MKCOL) {
538 if (request->curl_result == CURLE_OK ||
539 request->http_code == 405) {
540 remote_dir_exists[request->obj->oid.hash[0]] = 1;
541 start_put(request);
542 } else {
543 fprintf(stderr, "MKCOL %s failed, aborting (%d/%ld)\n",
544 oid_to_hex(&request->obj->oid),
545 request->curl_result, request->http_code);
546 request->state = ABORTED;
547 aborted = 1;
549 } else if (request->state == RUN_PUT) {
550 if (request->curl_result == CURLE_OK) {
551 start_move(request);
552 } else {
553 fprintf(stderr, "PUT %s failed, aborting (%d/%ld)\n",
554 oid_to_hex(&request->obj->oid),
555 request->curl_result, request->http_code);
556 request->state = ABORTED;
557 aborted = 1;
559 } else if (request->state == RUN_MOVE) {
560 if (request->curl_result == CURLE_OK) {
561 if (push_verbosely)
562 fprintf(stderr, " sent %s\n",
563 oid_to_hex(&request->obj->oid));
564 request->obj->flags |= REMOTE;
565 release_request(request);
566 } else {
567 fprintf(stderr, "MOVE %s failed, aborting (%d/%ld)\n",
568 oid_to_hex(&request->obj->oid),
569 request->curl_result, request->http_code);
570 request->state = ABORTED;
571 aborted = 1;
573 } else if (request->state == RUN_FETCH_LOOSE) {
574 obj_req = (struct http_object_request *)request->userData;
576 if (finish_http_object_request(obj_req) == 0)
577 if (obj_req->rename == 0)
578 request->obj->flags |= (LOCAL | REMOTE);
580 /* Try fetching packed if necessary */
581 if (request->obj->flags & LOCAL) {
582 release_http_object_request(obj_req);
583 release_request(request);
584 } else
585 start_fetch_packed(request);
587 } else if (request->state == RUN_FETCH_PACKED) {
588 int fail = 1;
589 if (request->curl_result != CURLE_OK) {
590 fprintf(stderr, "Unable to get pack file %s\n%s",
591 request->url, curl_errorstr);
592 } else {
593 preq = (struct http_pack_request *)request->userData;
595 if (preq) {
596 if (finish_http_pack_request(preq) == 0)
597 fail = 0;
598 release_http_pack_request(preq);
601 if (fail)
602 repo->can_update_info_refs = 0;
603 else
604 http_install_packfile(request->target, &repo->packs);
605 release_request(request);
609 static int is_running_queue;
610 static int fill_active_slot(void *data UNUSED)
612 struct transfer_request *request;
614 if (aborted || !is_running_queue)
615 return 0;
617 for (request = request_queue_head; request; request = request->next) {
618 if (request->state == NEED_FETCH) {
619 start_fetch_loose(request);
620 return 1;
621 } else if (pushing && request->state == NEED_PUSH) {
622 if (remote_dir_exists[request->obj->oid.hash[0]] == 1) {
623 start_put(request);
624 } else {
625 start_mkcol(request);
627 return 1;
630 return 0;
633 static void get_remote_object_list(unsigned char parent);
635 static void add_fetch_request(struct object *obj)
637 struct transfer_request *request;
639 check_locks();
642 * Don't fetch the object if it's known to exist locally
643 * or is already in the request queue
645 if (remote_dir_exists[obj->oid.hash[0]] == -1)
646 get_remote_object_list(obj->oid.hash[0]);
647 if (obj->flags & (LOCAL | FETCHING))
648 return;
650 obj->flags |= FETCHING;
651 request = xmalloc(sizeof(*request));
652 request->obj = obj;
653 request->url = NULL;
654 request->lock = NULL;
655 request->headers = NULL;
656 request->state = NEED_FETCH;
657 request->next = request_queue_head;
658 request_queue_head = request;
660 fill_active_slots();
661 step_active_slots();
664 static int add_send_request(struct object *obj, struct remote_lock *lock)
666 struct transfer_request *request;
667 struct packed_git *target;
669 /* Keep locks active */
670 check_locks();
673 * Don't push the object if it's known to exist on the remote
674 * or is already in the request queue
676 if (remote_dir_exists[obj->oid.hash[0]] == -1)
677 get_remote_object_list(obj->oid.hash[0]);
678 if (obj->flags & (REMOTE | PUSHING))
679 return 0;
680 target = find_sha1_pack(obj->oid.hash, repo->packs);
681 if (target) {
682 obj->flags |= REMOTE;
683 return 0;
686 obj->flags |= PUSHING;
687 request = xmalloc(sizeof(*request));
688 request->obj = obj;
689 request->url = NULL;
690 request->lock = lock;
691 request->headers = NULL;
692 request->state = NEED_PUSH;
693 request->next = request_queue_head;
694 request_queue_head = request;
696 fill_active_slots();
697 step_active_slots();
699 return 1;
702 static int fetch_indices(void)
704 int ret;
706 if (push_verbosely)
707 fprintf(stderr, "Getting pack list\n");
709 switch (http_get_info_packs(repo->url, &repo->packs)) {
710 case HTTP_OK:
711 case HTTP_MISSING_TARGET:
712 ret = 0;
713 break;
714 default:
715 ret = -1;
718 return ret;
721 static void one_remote_object(const struct object_id *oid)
723 struct object *obj;
725 obj = lookup_object(the_repository, oid);
726 if (!obj)
727 obj = parse_object(the_repository, oid);
729 /* Ignore remote objects that don't exist locally */
730 if (!obj)
731 return;
733 obj->flags |= REMOTE;
734 if (!object_list_contains(objects, obj))
735 object_list_insert(obj, &objects);
738 static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed)
740 int *lock_flags = (int *)ctx->userData;
742 if (tag_closed) {
743 if (!strcmp(ctx->name, DAV_CTX_LOCKENTRY)) {
744 if ((*lock_flags & DAV_PROP_LOCKEX) &&
745 (*lock_flags & DAV_PROP_LOCKWR)) {
746 *lock_flags |= DAV_LOCK_OK;
748 *lock_flags &= DAV_LOCK_OK;
749 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_WRITE)) {
750 *lock_flags |= DAV_PROP_LOCKWR;
751 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_EXCLUSIVE)) {
752 *lock_flags |= DAV_PROP_LOCKEX;
757 static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
759 struct remote_lock *lock = (struct remote_lock *)ctx->userData;
760 git_hash_ctx hash_ctx;
761 unsigned char lock_token_hash[GIT_MAX_RAWSZ];
763 if (tag_closed && ctx->cdata) {
764 if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) {
765 lock->owner = xstrdup(ctx->cdata);
766 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
767 const char *arg;
768 if (skip_prefix(ctx->cdata, "Second-", &arg))
769 lock->timeout = strtol(arg, NULL, 10);
770 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
771 lock->token = xstrdup(ctx->cdata);
773 the_hash_algo->init_fn(&hash_ctx);
774 the_hash_algo->update_fn(&hash_ctx, lock->token, strlen(lock->token));
775 the_hash_algo->final_fn(lock_token_hash, &hash_ctx);
777 lock->tmpfile_suffix[0] = '_';
778 memcpy(lock->tmpfile_suffix + 1, hash_to_hex(lock_token_hash), the_hash_algo->hexsz);
783 static void one_remote_ref(const char *refname);
785 static void
786 xml_start_tag(void *userData, const char *name, const char **atts UNUSED)
788 struct xml_ctx *ctx = (struct xml_ctx *)userData;
789 const char *c = strchr(name, ':');
790 int old_namelen, new_len;
792 if (!c)
793 c = name;
794 else
795 c++;
797 old_namelen = strlen(ctx->name);
798 new_len = old_namelen + strlen(c) + 2;
800 if (new_len > ctx->len) {
801 ctx->name = xrealloc(ctx->name, new_len);
802 ctx->len = new_len;
804 xsnprintf(ctx->name + old_namelen, ctx->len - old_namelen, ".%s", c);
806 FREE_AND_NULL(ctx->cdata);
808 ctx->userFunc(ctx, 0);
811 static void
812 xml_end_tag(void *userData, const char *name)
814 struct xml_ctx *ctx = (struct xml_ctx *)userData;
815 const char *c = strchr(name, ':');
816 char *ep;
818 ctx->userFunc(ctx, 1);
820 if (!c)
821 c = name;
822 else
823 c++;
825 ep = ctx->name + strlen(ctx->name) - strlen(c) - 1;
826 *ep = 0;
829 static void
830 xml_cdata(void *userData, const XML_Char *s, int len)
832 struct xml_ctx *ctx = (struct xml_ctx *)userData;
833 free(ctx->cdata);
834 ctx->cdata = xmemdupz(s, len);
837 static struct remote_lock *lock_remote(const char *path, long timeout)
839 struct active_request_slot *slot;
840 struct slot_results results;
841 struct buffer out_buffer = { STRBUF_INIT, 0 };
842 struct strbuf in_buffer = STRBUF_INIT;
843 char *url;
844 char *ep;
845 char timeout_header[25];
846 struct remote_lock *lock = NULL;
847 struct curl_slist *dav_headers = http_copy_default_headers();
848 struct xml_ctx ctx;
849 char *escaped;
851 url = xstrfmt("%s%s", repo->url, path);
853 /* Make sure leading directories exist for the remote ref */
854 ep = strchr(url + strlen(repo->url) + 1, '/');
855 while (ep) {
856 char saved_character = ep[1];
857 ep[1] = '\0';
858 slot = get_active_slot();
859 slot->results = &results;
860 curl_setup_http_get(slot->curl, url, DAV_MKCOL);
861 if (start_active_slot(slot)) {
862 run_active_slot(slot);
863 if (results.curl_result != CURLE_OK &&
864 results.http_code != 405) {
865 fprintf(stderr,
866 "Unable to create branch path %s\n",
867 url);
868 free(url);
869 return NULL;
871 } else {
872 fprintf(stderr, "Unable to start MKCOL request\n");
873 free(url);
874 return NULL;
876 ep[1] = saved_character;
877 ep = strchr(ep + 1, '/');
880 escaped = xml_entities(ident_default_email());
881 strbuf_addf(&out_buffer.buf, LOCK_REQUEST, escaped);
882 free(escaped);
884 xsnprintf(timeout_header, sizeof(timeout_header), "Timeout: Second-%ld", timeout);
885 dav_headers = curl_slist_append(dav_headers, timeout_header);
886 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
888 slot = get_active_slot();
889 slot->results = &results;
890 curl_setup_http(slot->curl, url, DAV_LOCK, &out_buffer, fwrite_buffer);
891 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
892 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, &in_buffer);
894 CALLOC_ARRAY(lock, 1);
895 lock->timeout = -1;
897 if (start_active_slot(slot)) {
898 run_active_slot(slot);
899 if (results.curl_result == CURLE_OK) {
900 XML_Parser parser = XML_ParserCreate(NULL);
901 enum XML_Status result;
902 ctx.name = xcalloc(10, 1);
903 ctx.len = 0;
904 ctx.cdata = NULL;
905 ctx.userFunc = handle_new_lock_ctx;
906 ctx.userData = lock;
907 XML_SetUserData(parser, &ctx);
908 XML_SetElementHandler(parser, xml_start_tag,
909 xml_end_tag);
910 XML_SetCharacterDataHandler(parser, xml_cdata);
911 result = XML_Parse(parser, in_buffer.buf,
912 in_buffer.len, 1);
913 free(ctx.name);
914 if (result != XML_STATUS_OK) {
915 fprintf(stderr, "XML error: %s\n",
916 XML_ErrorString(
917 XML_GetErrorCode(parser)));
918 lock->timeout = -1;
920 XML_ParserFree(parser);
921 } else {
922 fprintf(stderr,
923 "error: curl result=%d, HTTP code=%ld\n",
924 results.curl_result, results.http_code);
926 } else {
927 fprintf(stderr, "Unable to start LOCK request\n");
930 curl_slist_free_all(dav_headers);
931 strbuf_release(&out_buffer.buf);
932 strbuf_release(&in_buffer);
934 if (lock->token == NULL || lock->timeout <= 0) {
935 free(lock->token);
936 free(lock->owner);
937 free(url);
938 FREE_AND_NULL(lock);
939 } else {
940 lock->url = url;
941 lock->start_time = time(NULL);
942 lock->next = repo->locks;
943 repo->locks = lock;
946 return lock;
949 static int unlock_remote(struct remote_lock *lock)
951 struct active_request_slot *slot;
952 struct slot_results results;
953 struct remote_lock *prev = repo->locks;
954 struct curl_slist *dav_headers;
955 int rc = 0;
957 dav_headers = get_dav_token_headers(lock, DAV_HEADER_LOCK);
959 slot = get_active_slot();
960 slot->results = &results;
961 curl_setup_http_get(slot->curl, lock->url, DAV_UNLOCK);
962 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
964 if (start_active_slot(slot)) {
965 run_active_slot(slot);
966 if (results.curl_result == CURLE_OK)
967 rc = 1;
968 else
969 fprintf(stderr, "UNLOCK HTTP error %ld\n",
970 results.http_code);
971 } else {
972 fprintf(stderr, "Unable to start UNLOCK request\n");
975 curl_slist_free_all(dav_headers);
977 if (repo->locks == lock) {
978 repo->locks = lock->next;
979 } else {
980 while (prev && prev->next != lock)
981 prev = prev->next;
982 if (prev)
983 prev->next = lock->next;
986 free(lock->owner);
987 free(lock->url);
988 free(lock->token);
989 free(lock);
991 return rc;
994 static void remove_locks(void)
996 struct remote_lock *lock = repo->locks;
998 fprintf(stderr, "Removing remote locks...\n");
999 while (lock) {
1000 struct remote_lock *next = lock->next;
1001 unlock_remote(lock);
1002 lock = next;
1006 static void remove_locks_on_signal(int signo)
1008 remove_locks();
1009 sigchain_pop(signo);
1010 raise(signo);
1013 static void remote_ls(const char *path, int flags,
1014 void (*userFunc)(struct remote_ls_ctx *ls),
1015 void *userData);
1017 /* extract hex from sharded "xx/x{38}" filename */
1018 static int get_oid_hex_from_objpath(const char *path, struct object_id *oid)
1020 oid->algo = hash_algo_by_ptr(the_hash_algo);
1022 if (strlen(path) != the_hash_algo->hexsz + 1)
1023 return -1;
1025 if (hex_to_bytes(oid->hash, path, 1))
1026 return -1;
1027 path += 2;
1028 path++; /* skip '/' */
1030 return hex_to_bytes(oid->hash + 1, path, the_hash_algo->rawsz - 1);
1033 static void process_ls_object(struct remote_ls_ctx *ls)
1035 unsigned int *parent = (unsigned int *)ls->userData;
1036 const char *path = ls->dentry_name;
1037 struct object_id oid;
1039 if (!strcmp(ls->path, ls->dentry_name) && (ls->flags & IS_DIR)) {
1040 remote_dir_exists[*parent] = 1;
1041 return;
1044 if (!skip_prefix(path, "objects/", &path) ||
1045 get_oid_hex_from_objpath(path, &oid))
1046 return;
1048 one_remote_object(&oid);
1051 static void process_ls_ref(struct remote_ls_ctx *ls)
1053 if (!strcmp(ls->path, ls->dentry_name) && (ls->dentry_flags & IS_DIR)) {
1054 fprintf(stderr, " %s\n", ls->dentry_name);
1055 return;
1058 if (!(ls->dentry_flags & IS_DIR))
1059 one_remote_ref(ls->dentry_name);
1062 static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
1064 struct remote_ls_ctx *ls = (struct remote_ls_ctx *)ctx->userData;
1066 if (tag_closed) {
1067 if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) {
1068 if (ls->dentry_flags & IS_DIR) {
1070 /* ensure collection names end with slash */
1071 str_end_url_with_slash(ls->dentry_name, &ls->dentry_name);
1073 if (ls->flags & PROCESS_DIRS) {
1074 ls->userFunc(ls);
1076 if (strcmp(ls->dentry_name, ls->path) &&
1077 ls->flags & RECURSIVE) {
1078 remote_ls(ls->dentry_name,
1079 ls->flags,
1080 ls->userFunc,
1081 ls->userData);
1083 } else if (ls->flags & PROCESS_FILES) {
1084 ls->userFunc(ls);
1086 } else if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
1087 char *path = ctx->cdata;
1088 if (*ctx->cdata == 'h') {
1089 path = strstr(path, "//");
1090 if (path) {
1091 path = strchr(path+2, '/');
1094 if (path) {
1095 const char *url = repo->url;
1096 if (repo->path)
1097 url = repo->path;
1098 if (strncmp(path, url, repo->path_len))
1099 error("Parsed path '%s' does not match url: '%s'",
1100 path, url);
1101 else {
1102 path += repo->path_len;
1103 ls->dentry_name = xstrdup(path);
1106 } else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
1107 ls->dentry_flags |= IS_DIR;
1109 } else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
1110 FREE_AND_NULL(ls->dentry_name);
1111 ls->dentry_flags = 0;
1116 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1117 * should _only_ heed the information from that file, instead of trying to
1118 * determine the refs from the remote file system (badly: it does not even
1119 * know about packed-refs).
1121 static void remote_ls(const char *path, int flags,
1122 void (*userFunc)(struct remote_ls_ctx *ls),
1123 void *userData)
1125 char *url = xstrfmt("%s%s", repo->url, path);
1126 struct active_request_slot *slot;
1127 struct slot_results results;
1128 struct strbuf in_buffer = STRBUF_INIT;
1129 struct buffer out_buffer = { STRBUF_INIT, 0 };
1130 struct curl_slist *dav_headers = http_copy_default_headers();
1131 struct xml_ctx ctx;
1132 struct remote_ls_ctx ls;
1134 ls.flags = flags;
1135 ls.path = xstrdup(path);
1136 ls.dentry_name = NULL;
1137 ls.dentry_flags = 0;
1138 ls.userData = userData;
1139 ls.userFunc = userFunc;
1141 strbuf_addstr(&out_buffer.buf, PROPFIND_ALL_REQUEST);
1143 dav_headers = curl_slist_append(dav_headers, "Depth: 1");
1144 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1146 slot = get_active_slot();
1147 slot->results = &results;
1148 curl_setup_http(slot->curl, url, DAV_PROPFIND,
1149 &out_buffer, fwrite_buffer);
1150 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1151 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, &in_buffer);
1153 if (start_active_slot(slot)) {
1154 run_active_slot(slot);
1155 if (results.curl_result == CURLE_OK) {
1156 XML_Parser parser = XML_ParserCreate(NULL);
1157 enum XML_Status result;
1158 ctx.name = xcalloc(10, 1);
1159 ctx.len = 0;
1160 ctx.cdata = NULL;
1161 ctx.userFunc = handle_remote_ls_ctx;
1162 ctx.userData = &ls;
1163 XML_SetUserData(parser, &ctx);
1164 XML_SetElementHandler(parser, xml_start_tag,
1165 xml_end_tag);
1166 XML_SetCharacterDataHandler(parser, xml_cdata);
1167 result = XML_Parse(parser, in_buffer.buf,
1168 in_buffer.len, 1);
1169 free(ctx.name);
1171 if (result != XML_STATUS_OK) {
1172 fprintf(stderr, "XML error: %s\n",
1173 XML_ErrorString(
1174 XML_GetErrorCode(parser)));
1176 XML_ParserFree(parser);
1178 } else {
1179 fprintf(stderr, "Unable to start PROPFIND request\n");
1182 free(ls.path);
1183 free(url);
1184 strbuf_release(&out_buffer.buf);
1185 strbuf_release(&in_buffer);
1186 curl_slist_free_all(dav_headers);
1189 static void get_remote_object_list(unsigned char parent)
1191 char path[] = "objects/XX/";
1192 static const char hex[] = "0123456789abcdef";
1193 unsigned int val = parent;
1195 path[8] = hex[val >> 4];
1196 path[9] = hex[val & 0xf];
1197 remote_dir_exists[val] = 0;
1198 remote_ls(path, (PROCESS_FILES | PROCESS_DIRS),
1199 process_ls_object, &val);
1202 static int locking_available(void)
1204 struct active_request_slot *slot;
1205 struct slot_results results;
1206 struct strbuf in_buffer = STRBUF_INIT;
1207 struct buffer out_buffer = { STRBUF_INIT, 0 };
1208 struct curl_slist *dav_headers = http_copy_default_headers();
1209 struct xml_ctx ctx;
1210 int lock_flags = 0;
1211 char *escaped;
1213 escaped = xml_entities(repo->url);
1214 strbuf_addf(&out_buffer.buf, PROPFIND_SUPPORTEDLOCK_REQUEST, escaped);
1215 free(escaped);
1217 dav_headers = curl_slist_append(dav_headers, "Depth: 0");
1218 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1220 slot = get_active_slot();
1221 slot->results = &results;
1222 curl_setup_http(slot->curl, repo->url, DAV_PROPFIND,
1223 &out_buffer, fwrite_buffer);
1224 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1225 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, &in_buffer);
1227 if (start_active_slot(slot)) {
1228 run_active_slot(slot);
1229 if (results.curl_result == CURLE_OK) {
1230 XML_Parser parser = XML_ParserCreate(NULL);
1231 enum XML_Status result;
1232 ctx.name = xcalloc(10, 1);
1233 ctx.len = 0;
1234 ctx.cdata = NULL;
1235 ctx.userFunc = handle_lockprop_ctx;
1236 ctx.userData = &lock_flags;
1237 XML_SetUserData(parser, &ctx);
1238 XML_SetElementHandler(parser, xml_start_tag,
1239 xml_end_tag);
1240 result = XML_Parse(parser, in_buffer.buf,
1241 in_buffer.len, 1);
1242 free(ctx.name);
1244 if (result != XML_STATUS_OK) {
1245 fprintf(stderr, "XML error: %s\n",
1246 XML_ErrorString(
1247 XML_GetErrorCode(parser)));
1248 lock_flags = 0;
1250 XML_ParserFree(parser);
1251 if (!lock_flags)
1252 error("no DAV locking support on %s",
1253 repo->url);
1255 } else {
1256 error("Cannot access URL %s, return code %d",
1257 repo->url, results.curl_result);
1258 lock_flags = 0;
1260 } else {
1261 error("Unable to start PROPFIND request on %s", repo->url);
1264 strbuf_release(&out_buffer.buf);
1265 strbuf_release(&in_buffer);
1266 curl_slist_free_all(dav_headers);
1268 return lock_flags;
1271 static struct object_list **add_one_object(struct object *obj, struct object_list **p)
1273 struct object_list *entry = xmalloc(sizeof(struct object_list));
1274 entry->item = obj;
1275 entry->next = *p;
1276 *p = entry;
1277 return &entry->next;
1280 static struct object_list **process_blob(struct blob *blob,
1281 struct object_list **p)
1283 struct object *obj = &blob->object;
1285 obj->flags |= LOCAL;
1287 if (obj->flags & (UNINTERESTING | SEEN))
1288 return p;
1290 obj->flags |= SEEN;
1291 return add_one_object(obj, p);
1294 static struct object_list **process_tree(struct tree *tree,
1295 struct object_list **p)
1297 struct object *obj = &tree->object;
1298 struct tree_desc desc;
1299 struct name_entry entry;
1301 obj->flags |= LOCAL;
1303 if (obj->flags & (UNINTERESTING | SEEN))
1304 return p;
1305 if (parse_tree(tree) < 0)
1306 die("bad tree object %s", oid_to_hex(&obj->oid));
1308 obj->flags |= SEEN;
1309 p = add_one_object(obj, p);
1311 init_tree_desc(&desc, tree->buffer, tree->size);
1313 while (tree_entry(&desc, &entry))
1314 switch (object_type(entry.mode)) {
1315 case OBJ_TREE:
1316 p = process_tree(lookup_tree(the_repository, &entry.oid),
1318 break;
1319 case OBJ_BLOB:
1320 p = process_blob(lookup_blob(the_repository, &entry.oid),
1322 break;
1323 default:
1324 /* Subproject commit - not in this repository */
1325 break;
1328 free_tree_buffer(tree);
1329 return p;
1332 static int get_delta(struct rev_info *revs, struct remote_lock *lock)
1334 int i;
1335 struct commit *commit;
1336 struct object_list **p = &objects;
1337 int count = 0;
1339 while ((commit = get_revision(revs)) != NULL) {
1340 p = process_tree(repo_get_commit_tree(the_repository, commit),
1342 commit->object.flags |= LOCAL;
1343 if (!(commit->object.flags & UNINTERESTING))
1344 count += add_send_request(&commit->object, lock);
1347 for (i = 0; i < revs->pending.nr; i++) {
1348 struct object_array_entry *entry = revs->pending.objects + i;
1349 struct object *obj = entry->item;
1350 const char *name = entry->name;
1352 if (obj->flags & (UNINTERESTING | SEEN))
1353 continue;
1354 if (obj->type == OBJ_TAG) {
1355 obj->flags |= SEEN;
1356 p = add_one_object(obj, p);
1357 continue;
1359 if (obj->type == OBJ_TREE) {
1360 p = process_tree((struct tree *)obj, p);
1361 continue;
1363 if (obj->type == OBJ_BLOB) {
1364 p = process_blob((struct blob *)obj, p);
1365 continue;
1367 die("unknown pending object %s (%s)", oid_to_hex(&obj->oid), name);
1370 while (objects) {
1371 if (!(objects->item->flags & UNINTERESTING))
1372 count += add_send_request(objects->item, lock);
1373 objects = objects->next;
1376 return count;
1379 static int update_remote(const struct object_id *oid, struct remote_lock *lock)
1381 struct active_request_slot *slot;
1382 struct slot_results results;
1383 struct buffer out_buffer = { STRBUF_INIT, 0 };
1384 struct curl_slist *dav_headers;
1386 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1388 strbuf_addf(&out_buffer.buf, "%s\n", oid_to_hex(oid));
1390 slot = get_active_slot();
1391 slot->results = &results;
1392 curl_setup_http(slot->curl, lock->url, DAV_PUT,
1393 &out_buffer, fwrite_null);
1394 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1396 if (start_active_slot(slot)) {
1397 run_active_slot(slot);
1398 strbuf_release(&out_buffer.buf);
1399 if (results.curl_result != CURLE_OK) {
1400 fprintf(stderr,
1401 "PUT error: curl result=%d, HTTP code=%ld\n",
1402 results.curl_result, results.http_code);
1403 /* We should attempt recovery? */
1404 return 0;
1406 } else {
1407 strbuf_release(&out_buffer.buf);
1408 fprintf(stderr, "Unable to start PUT request\n");
1409 return 0;
1412 return 1;
1415 static struct ref *remote_refs;
1417 static void one_remote_ref(const char *refname)
1419 struct ref *ref;
1420 struct object *obj;
1422 ref = alloc_ref(refname);
1424 if (http_fetch_ref(repo->url, ref) != 0) {
1425 fprintf(stderr,
1426 "Unable to fetch ref %s from %s\n",
1427 refname, repo->url);
1428 free(ref);
1429 return;
1433 * Fetch a copy of the object if it doesn't exist locally - it
1434 * may be required for updating server info later.
1436 if (repo->can_update_info_refs && !repo_has_object_file(the_repository, &ref->old_oid)) {
1437 obj = lookup_unknown_object(the_repository, &ref->old_oid);
1438 fprintf(stderr, " fetch %s for %s\n",
1439 oid_to_hex(&ref->old_oid), refname);
1440 add_fetch_request(obj);
1443 ref->next = remote_refs;
1444 remote_refs = ref;
1447 static void get_dav_remote_heads(void)
1449 remote_ls("refs/", (PROCESS_FILES | PROCESS_DIRS | RECURSIVE), process_ls_ref, NULL);
1452 static void add_remote_info_ref(struct remote_ls_ctx *ls)
1454 struct strbuf *buf = (struct strbuf *)ls->userData;
1455 struct object *o;
1456 struct ref *ref;
1458 ref = alloc_ref(ls->dentry_name);
1460 if (http_fetch_ref(repo->url, ref) != 0) {
1461 fprintf(stderr,
1462 "Unable to fetch ref %s from %s\n",
1463 ls->dentry_name, repo->url);
1464 aborted = 1;
1465 free(ref);
1466 return;
1469 o = parse_object(the_repository, &ref->old_oid);
1470 if (!o) {
1471 fprintf(stderr,
1472 "Unable to parse object %s for remote ref %s\n",
1473 oid_to_hex(&ref->old_oid), ls->dentry_name);
1474 aborted = 1;
1475 free(ref);
1476 return;
1479 strbuf_addf(buf, "%s\t%s\n",
1480 oid_to_hex(&ref->old_oid), ls->dentry_name);
1482 if (o->type == OBJ_TAG) {
1483 o = deref_tag(the_repository, o, ls->dentry_name, 0);
1484 if (o)
1485 strbuf_addf(buf, "%s\t%s^{}\n",
1486 oid_to_hex(&o->oid), ls->dentry_name);
1488 free(ref);
1491 static void update_remote_info_refs(struct remote_lock *lock)
1493 struct buffer buffer = { STRBUF_INIT, 0 };
1494 struct active_request_slot *slot;
1495 struct slot_results results;
1496 struct curl_slist *dav_headers;
1498 remote_ls("refs/", (PROCESS_FILES | RECURSIVE),
1499 add_remote_info_ref, &buffer.buf);
1500 if (!aborted) {
1501 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1503 slot = get_active_slot();
1504 slot->results = &results;
1505 curl_setup_http(slot->curl, lock->url, DAV_PUT,
1506 &buffer, fwrite_null);
1507 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1509 if (start_active_slot(slot)) {
1510 run_active_slot(slot);
1511 if (results.curl_result != CURLE_OK) {
1512 fprintf(stderr,
1513 "PUT error: curl result=%d, HTTP code=%ld\n",
1514 results.curl_result, results.http_code);
1518 strbuf_release(&buffer.buf);
1521 static int remote_exists(const char *path)
1523 char *url = xstrfmt("%s%s", repo->url, path);
1524 int ret;
1527 switch (http_get_strbuf(url, NULL, NULL)) {
1528 case HTTP_OK:
1529 ret = 1;
1530 break;
1531 case HTTP_MISSING_TARGET:
1532 ret = 0;
1533 break;
1534 case HTTP_ERROR:
1535 error("unable to access '%s': %s", url, curl_errorstr);
1536 /* fallthrough */
1537 default:
1538 ret = -1;
1540 free(url);
1541 return ret;
1544 static void fetch_symref(const char *path, char **symref, struct object_id *oid)
1546 char *url = xstrfmt("%s%s", repo->url, path);
1547 struct strbuf buffer = STRBUF_INIT;
1548 const char *name;
1550 if (http_get_strbuf(url, &buffer, NULL) != HTTP_OK)
1551 die("Couldn't get %s for remote symref\n%s", url,
1552 curl_errorstr);
1553 free(url);
1555 FREE_AND_NULL(*symref);
1556 oidclr(oid);
1558 if (buffer.len == 0)
1559 return;
1561 /* Cut off trailing newline. */
1562 strbuf_rtrim(&buffer);
1564 /* If it's a symref, set the refname; otherwise try for a sha1 */
1565 if (skip_prefix(buffer.buf, "ref: ", &name)) {
1566 *symref = xmemdupz(name, buffer.len - (name - buffer.buf));
1567 } else {
1568 get_oid_hex(buffer.buf, oid);
1571 strbuf_release(&buffer);
1574 static int verify_merge_base(struct object_id *head_oid, struct ref *remote)
1576 struct commit *head = lookup_commit_or_die(head_oid, "HEAD");
1577 struct commit *branch = lookup_commit_or_die(&remote->old_oid,
1578 remote->name);
1580 return repo_in_merge_bases(the_repository, branch, head);
1583 static int delete_remote_branch(const char *pattern, int force)
1585 struct ref *refs = remote_refs;
1586 struct ref *remote_ref = NULL;
1587 struct object_id head_oid;
1588 char *symref = NULL;
1589 int match;
1590 int patlen = strlen(pattern);
1591 int i;
1592 struct active_request_slot *slot;
1593 struct slot_results results;
1594 char *url;
1596 /* Find the remote branch(es) matching the specified branch name */
1597 for (match = 0; refs; refs = refs->next) {
1598 char *name = refs->name;
1599 int namelen = strlen(name);
1600 if (namelen < patlen ||
1601 memcmp(name + namelen - patlen, pattern, patlen))
1602 continue;
1603 if (namelen != patlen && name[namelen - patlen - 1] != '/')
1604 continue;
1605 match++;
1606 remote_ref = refs;
1608 if (match == 0)
1609 return error("No remote branch matches %s", pattern);
1610 if (match != 1)
1611 return error("More than one remote branch matches %s",
1612 pattern);
1615 * Remote HEAD must be a symref (not exactly foolproof; a remote
1616 * symlink to a symref will look like a symref)
1618 fetch_symref("HEAD", &symref, &head_oid);
1619 if (!symref)
1620 return error("Remote HEAD is not a symref");
1622 /* Remote branch must not be the remote HEAD */
1623 for (i = 0; symref && i < MAXDEPTH; i++) {
1624 if (!strcmp(remote_ref->name, symref))
1625 return error("Remote branch %s is the current HEAD",
1626 remote_ref->name);
1627 fetch_symref(symref, &symref, &head_oid);
1630 /* Run extra sanity checks if delete is not forced */
1631 if (!force) {
1632 /* Remote HEAD must resolve to a known object */
1633 if (symref)
1634 return error("Remote HEAD symrefs too deep");
1635 if (is_null_oid(&head_oid))
1636 return error("Unable to resolve remote HEAD");
1637 if (!repo_has_object_file(the_repository, &head_oid))
1638 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", oid_to_hex(&head_oid));
1640 /* Remote branch must resolve to a known object */
1641 if (is_null_oid(&remote_ref->old_oid))
1642 return error("Unable to resolve remote branch %s",
1643 remote_ref->name);
1644 if (!repo_has_object_file(the_repository, &remote_ref->old_oid))
1645 return error("Remote branch %s resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", remote_ref->name, oid_to_hex(&remote_ref->old_oid));
1647 /* Remote branch must be an ancestor of remote HEAD */
1648 if (!verify_merge_base(&head_oid, remote_ref)) {
1649 return error("The branch '%s' is not an ancestor "
1650 "of your current HEAD.\n"
1651 "If you are sure you want to delete it,"
1652 " run:\n\t'git http-push -D %s %s'",
1653 remote_ref->name, repo->url, pattern);
1657 /* Send delete request */
1658 fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name);
1659 if (dry_run)
1660 return 0;
1661 url = xstrfmt("%s%s", repo->url, remote_ref->name);
1662 slot = get_active_slot();
1663 slot->results = &results;
1664 curl_setup_http_get(slot->curl, url, DAV_DELETE);
1665 if (start_active_slot(slot)) {
1666 run_active_slot(slot);
1667 free(url);
1668 if (results.curl_result != CURLE_OK)
1669 return error("DELETE request failed (%d/%ld)",
1670 results.curl_result, results.http_code);
1671 } else {
1672 free(url);
1673 return error("Unable to start DELETE request");
1676 return 0;
1679 static void run_request_queue(void)
1681 is_running_queue = 1;
1682 fill_active_slots();
1683 add_fill_function(NULL, fill_active_slot);
1684 do {
1685 finish_all_active_slots();
1686 fill_active_slots();
1687 } while (request_queue_head && !aborted);
1689 is_running_queue = 0;
1692 int cmd_main(int argc, const char **argv)
1694 struct transfer_request *request;
1695 struct transfer_request *next_request;
1696 struct refspec rs = REFSPEC_INIT_PUSH;
1697 struct remote_lock *ref_lock = NULL;
1698 struct remote_lock *info_ref_lock = NULL;
1699 int delete_branch = 0;
1700 int force_delete = 0;
1701 int objects_to_send;
1702 int rc = 0;
1703 int i;
1704 int new_refs;
1705 struct ref *ref, *local_refs;
1707 CALLOC_ARRAY(repo, 1);
1709 argv++;
1710 for (i = 1; i < argc; i++, argv++) {
1711 const char *arg = *argv;
1713 if (*arg == '-') {
1714 if (!strcmp(arg, "--all")) {
1715 push_all = MATCH_REFS_ALL;
1716 continue;
1718 if (!strcmp(arg, "--force")) {
1719 force_all = 1;
1720 continue;
1722 if (!strcmp(arg, "--dry-run")) {
1723 dry_run = 1;
1724 continue;
1726 if (!strcmp(arg, "--helper-status")) {
1727 helper_status = 1;
1728 continue;
1730 if (!strcmp(arg, "--verbose")) {
1731 push_verbosely = 1;
1732 http_is_verbose = 1;
1733 continue;
1735 if (!strcmp(arg, "-d")) {
1736 delete_branch = 1;
1737 continue;
1739 if (!strcmp(arg, "-D")) {
1740 delete_branch = 1;
1741 force_delete = 1;
1742 continue;
1744 if (!strcmp(arg, "-h"))
1745 usage(http_push_usage);
1747 if (!repo->url) {
1748 char *path = strstr(arg, "//");
1749 str_end_url_with_slash(arg, &repo->url);
1750 repo->path_len = strlen(repo->url);
1751 if (path) {
1752 repo->path = strchr(path+2, '/');
1753 if (repo->path)
1754 repo->path_len = strlen(repo->path);
1756 continue;
1758 refspec_appendn(&rs, argv, argc - i);
1759 break;
1762 if (!repo->url)
1763 usage(http_push_usage);
1765 if (delete_branch && rs.nr != 1)
1766 die("You must specify only one branch name when deleting a remote branch");
1768 setup_git_directory();
1770 memset(remote_dir_exists, -1, 256);
1772 http_init(NULL, repo->url, 1);
1774 is_running_queue = 0;
1776 /* Verify DAV compliance/lock support */
1777 if (!locking_available()) {
1778 rc = 1;
1779 goto cleanup;
1782 sigchain_push_common(remove_locks_on_signal);
1784 /* Check whether the remote has server info files */
1785 repo->can_update_info_refs = 0;
1786 repo->has_info_refs = remote_exists("info/refs");
1787 repo->has_info_packs = remote_exists("objects/info/packs");
1788 if (repo->has_info_refs) {
1789 info_ref_lock = lock_remote("info/refs", LOCK_TIME);
1790 if (info_ref_lock)
1791 repo->can_update_info_refs = 1;
1792 else {
1793 error("cannot lock existing info/refs");
1794 rc = 1;
1795 goto cleanup;
1798 if (repo->has_info_packs)
1799 fetch_indices();
1801 /* Get a list of all local and remote heads to validate refspecs */
1802 local_refs = get_local_heads();
1803 fprintf(stderr, "Fetching remote heads...\n");
1804 get_dav_remote_heads();
1805 run_request_queue();
1807 /* Remove a remote branch if -d or -D was specified */
1808 if (delete_branch) {
1809 const char *branch = rs.items[i].src;
1810 if (delete_remote_branch(branch, force_delete) == -1) {
1811 fprintf(stderr, "Unable to delete remote branch %s\n",
1812 branch);
1813 if (helper_status)
1814 printf("error %s cannot remove\n", branch);
1816 goto cleanup;
1819 /* match them up */
1820 if (match_push_refs(local_refs, &remote_refs, &rs, push_all)) {
1821 rc = -1;
1822 goto cleanup;
1824 if (!remote_refs) {
1825 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
1826 if (helper_status)
1827 printf("error null no match\n");
1828 rc = 0;
1829 goto cleanup;
1832 new_refs = 0;
1833 for (ref = remote_refs; ref; ref = ref->next) {
1834 struct rev_info revs;
1835 struct strvec commit_argv = STRVEC_INIT;
1837 if (!ref->peer_ref)
1838 continue;
1840 if (is_null_oid(&ref->peer_ref->new_oid)) {
1841 if (delete_remote_branch(ref->name, 1) == -1) {
1842 error("Could not remove %s", ref->name);
1843 if (helper_status)
1844 printf("error %s cannot remove\n", ref->name);
1845 rc = -4;
1847 else if (helper_status)
1848 printf("ok %s\n", ref->name);
1849 new_refs++;
1850 continue;
1853 if (oideq(&ref->old_oid, &ref->peer_ref->new_oid)) {
1854 if (push_verbosely)
1855 fprintf(stderr, "'%s': up-to-date\n", ref->name);
1856 if (helper_status)
1857 printf("ok %s up to date\n", ref->name);
1858 continue;
1861 if (!force_all &&
1862 !is_null_oid(&ref->old_oid) &&
1863 !ref->force) {
1864 if (!repo_has_object_file(the_repository, &ref->old_oid) ||
1865 !ref_newer(&ref->peer_ref->new_oid,
1866 &ref->old_oid)) {
1868 * We do not have the remote ref, or
1869 * we know that the remote ref is not
1870 * an ancestor of what we are trying to
1871 * push. Either way this can be losing
1872 * commits at the remote end and likely
1873 * we were not up to date to begin with.
1875 error("remote '%s' is not an ancestor of\n"
1876 "local '%s'.\n"
1877 "Maybe you are not up-to-date and "
1878 "need to pull first?",
1879 ref->name,
1880 ref->peer_ref->name);
1881 if (helper_status)
1882 printf("error %s non-fast forward\n", ref->name);
1883 rc = -2;
1884 continue;
1887 oidcpy(&ref->new_oid, &ref->peer_ref->new_oid);
1888 new_refs++;
1890 fprintf(stderr, "updating '%s'", ref->name);
1891 if (strcmp(ref->name, ref->peer_ref->name))
1892 fprintf(stderr, " using '%s'", ref->peer_ref->name);
1893 fprintf(stderr, "\n from %s\n to %s\n",
1894 oid_to_hex(&ref->old_oid), oid_to_hex(&ref->new_oid));
1895 if (dry_run) {
1896 if (helper_status)
1897 printf("ok %s\n", ref->name);
1898 continue;
1901 /* Lock remote branch ref */
1902 ref_lock = lock_remote(ref->name, LOCK_TIME);
1903 if (!ref_lock) {
1904 fprintf(stderr, "Unable to lock remote branch %s\n",
1905 ref->name);
1906 if (helper_status)
1907 printf("error %s lock error\n", ref->name);
1908 rc = 1;
1909 continue;
1912 /* Set up revision info for this refspec */
1913 strvec_push(&commit_argv, ""); /* ignored */
1914 strvec_push(&commit_argv, "--objects");
1915 strvec_push(&commit_argv, oid_to_hex(&ref->new_oid));
1916 if (!push_all && !is_null_oid(&ref->old_oid))
1917 strvec_pushf(&commit_argv, "^%s",
1918 oid_to_hex(&ref->old_oid));
1919 repo_init_revisions(the_repository, &revs, setup_git_directory());
1920 setup_revisions(commit_argv.nr, commit_argv.v, &revs, NULL);
1921 revs.edge_hint = 0; /* just in case */
1923 /* Generate a list of objects that need to be pushed */
1924 pushing = 0;
1925 if (prepare_revision_walk(&revs))
1926 die("revision walk setup failed");
1927 mark_edges_uninteresting(&revs, NULL, 0);
1928 objects_to_send = get_delta(&revs, ref_lock);
1929 finish_all_active_slots();
1931 /* Push missing objects to remote, this would be a
1932 convenient time to pack them first if appropriate. */
1933 pushing = 1;
1934 if (objects_to_send)
1935 fprintf(stderr, " sending %d objects\n",
1936 objects_to_send);
1938 run_request_queue();
1940 /* Update the remote branch if all went well */
1941 if (aborted || !update_remote(&ref->new_oid, ref_lock))
1942 rc = 1;
1944 if (!rc)
1945 fprintf(stderr, " done\n");
1946 if (helper_status)
1947 printf("%s %s\n", !rc ? "ok" : "error", ref->name);
1948 unlock_remote(ref_lock);
1949 check_locks();
1950 strvec_clear(&commit_argv);
1951 release_revisions(&revs);
1954 /* Update remote server info if appropriate */
1955 if (repo->has_info_refs && new_refs) {
1956 if (info_ref_lock && repo->can_update_info_refs) {
1957 fprintf(stderr, "Updating remote server info\n");
1958 if (!dry_run)
1959 update_remote_info_refs(info_ref_lock);
1960 } else {
1961 fprintf(stderr, "Unable to update server info\n");
1965 cleanup:
1966 if (info_ref_lock)
1967 unlock_remote(info_ref_lock);
1968 free(repo);
1970 http_cleanup();
1972 request = request_queue_head;
1973 while (request != NULL) {
1974 next_request = request->next;
1975 release_request(request);
1976 request = next_request;
1979 return rc;