11 #include "list-objects.h"
 
  16 static const char http_push_usage[] =
 
  17 "git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
 
  24 #define XML_STATUS_OK    1
 
  25 #define XML_STATUS_ERROR 0
 
  28 #define PREV_BUF_SIZE 4096
 
  31 #define DAV_LOCK "LOCK"
 
  32 #define DAV_MKCOL "MKCOL"
 
  33 #define DAV_MOVE "MOVE"
 
  34 #define DAV_PROPFIND "PROPFIND"
 
  36 #define DAV_UNLOCK "UNLOCK"
 
  37 #define DAV_DELETE "DELETE"
 
  40 #define DAV_PROP_LOCKWR (1u << 0)
 
  41 #define DAV_PROP_LOCKEX (1u << 1)
 
  42 #define DAV_LOCK_OK (1u << 2)
 
  44 /* DAV XML properties */
 
  45 #define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
 
  46 #define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
 
  47 #define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
 
  48 #define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
 
  49 #define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
 
  50 #define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
 
  51 #define DAV_PROPFIND_RESP ".multistatus.response"
 
  52 #define DAV_PROPFIND_NAME ".multistatus.response.href"
 
  53 #define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
 
  55 /* DAV request body templates */
 
  56 #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>"
 
  57 #define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
 
  58 #define LOCK_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:lockinfo xmlns:D=\"DAV:\">\n<D:lockscope><D:exclusive/></D:lockscope>\n<D:locktype><D:write/></D:locktype>\n<D:owner>\n<D:href>mailto:%s</D:href>\n</D:owner>\n</D:lockinfo>"
 
  61 #define LOCK_REFRESH 30
 
  63 /* bits #0-15 in revision.h */
 
  65 #define LOCAL    (1u<<16)
 
  66 #define REMOTE   (1u<<17)
 
  67 #define FETCHING (1u<<18)
 
  68 #define PUSHING  (1u<<19)
 
  70 /* We allow "recursive" symbolic refs. Only within reason, though */
 
  75 static signed char remote_dir_exists[256];
 
  77 static int push_verbosely;
 
  78 static int push_all = MATCH_REFS_NONE;
 
  81 static int helper_status;
 
  83 static struct object_list *objects;
 
  90         int can_update_info_refs;
 
  92         struct packed_git *packs;
 
  93         struct remote_lock *locks;
 
  96 static struct repo *repo;
 
 110 struct transfer_request {
 
 114         struct remote_lock *lock;
 
 115         struct curl_slist *headers;
 
 116         struct buffer buffer;
 
 117         enum transfer_state state;
 
 118         CURLcode curl_result;
 
 119         char errorstr[CURL_ERROR_SIZE];
 
 122         struct active_request_slot *slot;
 
 123         struct transfer_request *next;
 
 126 static struct transfer_request *request_queue_head;
 
 132         void (*userFunc)(struct xml_ctx *ctx, int tag_closed);
 
 140         char tmpfile_suffix[41];
 
 144         struct remote_lock *next;
 
 147 /* Flags that control remote_ls processing */
 
 148 #define PROCESS_FILES (1u << 0)
 
 149 #define PROCESS_DIRS  (1u << 1)
 
 150 #define RECURSIVE     (1u << 2)
 
 152 /* Flags that remote_ls passes to callback functions */
 
 153 #define IS_DIR (1u << 0)
 
 155 struct remote_ls_ctx {
 
 157         void (*userFunc)(struct remote_ls_ctx *ls);
 
 162         struct remote_ls_ctx *parent;
 
 165 /* get_dav_token_headers options */
 
 166 enum dav_header_flag {
 
 167         DAV_HEADER_IF = (1u << 0),
 
 168         DAV_HEADER_LOCK = (1u << 1),
 
 169         DAV_HEADER_TIMEOUT = (1u << 2)
 
 172 static char *xml_entities(const char *s)
 
 174         struct strbuf buf = STRBUF_INIT;
 
 176                 size_t len = strcspn(s, "\"<>&");
 
 177                 strbuf_add(&buf, s, len);
 
 181                         strbuf_addstr(&buf, """);
 
 184                         strbuf_addstr(&buf, "<");
 
 187                         strbuf_addstr(&buf, ">");
 
 190                         strbuf_addstr(&buf, "&");
 
 193                         return strbuf_detach(&buf, NULL);
 
 197         return strbuf_detach(&buf, NULL);
 
 200 static void curl_setup_http_get(CURL *curl, const char *url,
 
 201                 const char *custom_req)
 
 203         curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
 
 204         curl_easy_setopt(curl, CURLOPT_URL, url);
 
 205         curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, custom_req);
 
 206         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_null);
 
 209 static void curl_setup_http(CURL *curl, const char *url,
 
 210                 const char *custom_req, struct buffer *buffer,
 
 211                 curl_write_callback write_fn)
 
 213         curl_easy_setopt(curl, CURLOPT_PUT, 1);
 
 214         curl_easy_setopt(curl, CURLOPT_URL, url);
 
 215         curl_easy_setopt(curl, CURLOPT_INFILE, buffer);
 
 216         curl_easy_setopt(curl, CURLOPT_INFILESIZE, buffer->buf.len);
 
 217         curl_easy_setopt(curl, CURLOPT_READFUNCTION, fread_buffer);
 
 218 #ifndef NO_CURL_IOCTL
 
 219         curl_easy_setopt(curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
 
 220         curl_easy_setopt(curl, CURLOPT_IOCTLDATA, &buffer);
 
 222         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_fn);
 
 223         curl_easy_setopt(curl, CURLOPT_NOBODY, 0);
 
 224         curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, custom_req);
 
 225         curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
 
 228 static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum dav_header_flag options)
 
 230         struct strbuf buf = STRBUF_INIT;
 
 231         struct curl_slist *dav_headers = NULL;
 
 233         if (options & DAV_HEADER_IF) {
 
 234                 strbuf_addf(&buf, "If: (<%s>)", lock->token);
 
 235                 dav_headers = curl_slist_append(dav_headers, buf.buf);
 
 238         if (options & DAV_HEADER_LOCK) {
 
 239                 strbuf_addf(&buf, "Lock-Token: <%s>", lock->token);
 
 240                 dav_headers = curl_slist_append(dav_headers, buf.buf);
 
 243         if (options & DAV_HEADER_TIMEOUT) {
 
 244                 strbuf_addf(&buf, "Timeout: Second-%ld", lock->timeout);
 
 245                 dav_headers = curl_slist_append(dav_headers, buf.buf);
 
 248         strbuf_release(&buf);
 
 253 static void finish_request(struct transfer_request *request);
 
 254 static void release_request(struct transfer_request *request);
 
 256 static void process_response(void *callback_data)
 
 258         struct transfer_request *request =
 
 259                 (struct transfer_request *)callback_data;
 
 261         finish_request(request);
 
 264 #ifdef USE_CURL_MULTI
 
 266 static void start_fetch_loose(struct transfer_request *request)
 
 268         struct active_request_slot *slot;
 
 269         struct http_object_request *obj_req;
 
 271         obj_req = new_http_object_request(repo->url, request->obj->sha1);
 
 272         if (obj_req == NULL) {
 
 273                 request->state = ABORTED;
 
 277         slot = obj_req->slot;
 
 278         slot->callback_func = process_response;
 
 279         slot->callback_data = request;
 
 280         request->slot = slot;
 
 281         request->userData = obj_req;
 
 283         /* Try to get the request started, abort the request on error */
 
 284         request->state = RUN_FETCH_LOOSE;
 
 285         if (!start_active_slot(slot)) {
 
 286                 fprintf(stderr, "Unable to start GET request\n");
 
 287                 repo->can_update_info_refs = 0;
 
 288                 release_http_object_request(obj_req);
 
 289                 release_request(request);
 
 293 static void start_mkcol(struct transfer_request *request)
 
 295         char *hex = sha1_to_hex(request->obj->sha1);
 
 296         struct active_request_slot *slot;
 
 298         request->url = get_remote_object_url(repo->url, hex, 1);
 
 300         slot = get_active_slot();
 
 301         slot->callback_func = process_response;
 
 302         slot->callback_data = request;
 
 303         curl_setup_http_get(slot->curl, request->url, DAV_MKCOL);
 
 304         curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
 
 306         if (start_active_slot(slot)) {
 
 307                 request->slot = slot;
 
 308                 request->state = RUN_MKCOL;
 
 310                 request->state = ABORTED;
 
 317 static void start_fetch_packed(struct transfer_request *request)
 
 319         struct packed_git *target;
 
 321         struct transfer_request *check_request = request_queue_head;
 
 322         struct http_pack_request *preq;
 
 324         target = find_sha1_pack(request->obj->sha1, repo->packs);
 
 326                 fprintf(stderr, "Unable to fetch %s, will not be able to update server info refs\n", sha1_to_hex(request->obj->sha1));
 
 327                 repo->can_update_info_refs = 0;
 
 328                 release_request(request);
 
 332         fprintf(stderr, "Fetching pack %s\n", sha1_to_hex(target->sha1));
 
 333         fprintf(stderr, " which contains %s\n", sha1_to_hex(request->obj->sha1));
 
 335         preq = new_http_pack_request(target, repo->url);
 
 337                 release_http_pack_request(preq);
 
 338                 repo->can_update_info_refs = 0;
 
 341         preq->lst = &repo->packs;
 
 343         /* Make sure there isn't another open request for this pack */
 
 344         while (check_request) {
 
 345                 if (check_request->state == RUN_FETCH_PACKED &&
 
 346                     !strcmp(check_request->url, preq->url)) {
 
 347                         release_http_pack_request(preq);
 
 348                         release_request(request);
 
 351                 check_request = check_request->next;
 
 354         preq->slot->callback_func = process_response;
 
 355         preq->slot->callback_data = request;
 
 356         request->slot = preq->slot;
 
 357         request->userData = preq;
 
 359         /* Try to get the request started, abort the request on error */
 
 360         request->state = RUN_FETCH_PACKED;
 
 361         if (!start_active_slot(preq->slot)) {
 
 362                 fprintf(stderr, "Unable to start GET request\n");
 
 363                 release_http_pack_request(preq);
 
 364                 repo->can_update_info_refs = 0;
 
 365                 release_request(request);
 
 369 static void start_put(struct transfer_request *request)
 
 371         char *hex = sha1_to_hex(request->obj->sha1);
 
 372         struct active_request_slot *slot;
 
 373         struct strbuf buf = STRBUF_INIT;
 
 374         enum object_type type;
 
 382         unpacked = read_sha1_file(request->obj->sha1, &type, &len);
 
 383         hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
 
 386         memset(&stream, 0, sizeof(stream));
 
 387         git_deflate_init(&stream, zlib_compression_level);
 
 388         size = git_deflate_bound(&stream, len + hdrlen);
 
 389         strbuf_init(&request->buffer.buf, size);
 
 390         request->buffer.posn = 0;
 
 393         stream.next_out = (unsigned char *)request->buffer.buf.buf;
 
 394         stream.avail_out = size;
 
 397         stream.next_in = (void *)hdr;
 
 398         stream.avail_in = hdrlen;
 
 399         while (git_deflate(&stream, 0) == Z_OK)
 
 402         /* Then the data itself.. */
 
 403         stream.next_in = unpacked;
 
 404         stream.avail_in = len;
 
 405         while (git_deflate(&stream, Z_FINISH) == Z_OK)
 
 407         git_deflate_end(&stream);
 
 410         request->buffer.buf.len = stream.total_out;
 
 412         strbuf_addstr(&buf, "Destination: ");
 
 413         append_remote_object_url(&buf, repo->url, hex, 0);
 
 414         request->dest = strbuf_detach(&buf, NULL);
 
 416         append_remote_object_url(&buf, repo->url, hex, 0);
 
 417         strbuf_add(&buf, request->lock->tmpfile_suffix, 41);
 
 418         request->url = strbuf_detach(&buf, NULL);
 
 420         slot = get_active_slot();
 
 421         slot->callback_func = process_response;
 
 422         slot->callback_data = request;
 
 423         curl_setup_http(slot->curl, request->url, DAV_PUT,
 
 424                         &request->buffer, fwrite_null);
 
 426         if (start_active_slot(slot)) {
 
 427                 request->slot = slot;
 
 428                 request->state = RUN_PUT;
 
 430                 request->state = ABORTED;
 
 436 static void start_move(struct transfer_request *request)
 
 438         struct active_request_slot *slot;
 
 439         struct curl_slist *dav_headers = NULL;
 
 441         slot = get_active_slot();
 
 442         slot->callback_func = process_response;
 
 443         slot->callback_data = request;
 
 444         curl_setup_http_get(slot->curl, request->url, DAV_MOVE);
 
 445         dav_headers = curl_slist_append(dav_headers, request->dest);
 
 446         dav_headers = curl_slist_append(dav_headers, "Overwrite: T");
 
 447         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
 
 449         if (start_active_slot(slot)) {
 
 450                 request->slot = slot;
 
 451                 request->state = RUN_MOVE;
 
 453                 request->state = ABORTED;
 
 459 static int refresh_lock(struct remote_lock *lock)
 
 461         struct active_request_slot *slot;
 
 462         struct slot_results results;
 
 463         struct curl_slist *dav_headers;
 
 466         lock->refreshing = 1;
 
 468         dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF | DAV_HEADER_TIMEOUT);
 
 470         slot = get_active_slot();
 
 471         slot->results = &results;
 
 472         curl_setup_http_get(slot->curl, lock->url, DAV_LOCK);
 
 473         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
 
 475         if (start_active_slot(slot)) {
 
 476                 run_active_slot(slot);
 
 477                 if (results.curl_result != CURLE_OK) {
 
 478                         fprintf(stderr, "LOCK HTTP error %ld\n",
 
 481                         lock->start_time = time(NULL);
 
 486         lock->refreshing = 0;
 
 487         curl_slist_free_all(dav_headers);
 
 492 static void check_locks(void)
 
 494         struct remote_lock *lock = repo->locks;
 
 495         time_t current_time = time(NULL);
 
 499                 time_remaining = lock->start_time + lock->timeout -
 
 501                 if (!lock->refreshing && time_remaining < LOCK_REFRESH) {
 
 502                         if (!refresh_lock(lock)) {
 
 504                                         "Unable to refresh lock for %s\n",
 
 514 static void release_request(struct transfer_request *request)
 
 516         struct transfer_request *entry = request_queue_head;
 
 518         if (request == request_queue_head) {
 
 519                 request_queue_head = request->next;
 
 521                 while (entry->next != NULL && entry->next != request)
 
 523                 if (entry->next == request)
 
 524                         entry->next = entry->next->next;
 
 531 static void finish_request(struct transfer_request *request)
 
 533         struct http_pack_request *preq;
 
 534         struct http_object_request *obj_req;
 
 536         request->curl_result = request->slot->curl_result;
 
 537         request->http_code = request->slot->http_code;
 
 538         request->slot = NULL;
 
 540         /* Keep locks active */
 
 543         if (request->headers != NULL)
 
 544                 curl_slist_free_all(request->headers);
 
 546         /* URL is reused for MOVE after PUT */
 
 547         if (request->state != RUN_PUT) {
 
 552         if (request->state == RUN_MKCOL) {
 
 553                 if (request->curl_result == CURLE_OK ||
 
 554                     request->http_code == 405) {
 
 555                         remote_dir_exists[request->obj->sha1[0]] = 1;
 
 558                         fprintf(stderr, "MKCOL %s failed, aborting (%d/%ld)\n",
 
 559                                 sha1_to_hex(request->obj->sha1),
 
 560                                 request->curl_result, request->http_code);
 
 561                         request->state = ABORTED;
 
 564         } else if (request->state == RUN_PUT) {
 
 565                 if (request->curl_result == CURLE_OK) {
 
 568                         fprintf(stderr, "PUT %s failed, aborting (%d/%ld)\n",
 
 569                                 sha1_to_hex(request->obj->sha1),
 
 570                                 request->curl_result, request->http_code);
 
 571                         request->state = ABORTED;
 
 574         } else if (request->state == RUN_MOVE) {
 
 575                 if (request->curl_result == CURLE_OK) {
 
 577                                 fprintf(stderr, "    sent %s\n",
 
 578                                         sha1_to_hex(request->obj->sha1));
 
 579                         request->obj->flags |= REMOTE;
 
 580                         release_request(request);
 
 582                         fprintf(stderr, "MOVE %s failed, aborting (%d/%ld)\n",
 
 583                                 sha1_to_hex(request->obj->sha1),
 
 584                                 request->curl_result, request->http_code);
 
 585                         request->state = ABORTED;
 
 588         } else if (request->state == RUN_FETCH_LOOSE) {
 
 589                 obj_req = (struct http_object_request *)request->userData;
 
 591                 if (finish_http_object_request(obj_req) == 0)
 
 592                         if (obj_req->rename == 0)
 
 593                                 request->obj->flags |= (LOCAL | REMOTE);
 
 595                 /* Try fetching packed if necessary */
 
 596                 if (request->obj->flags & LOCAL) {
 
 597                         release_http_object_request(obj_req);
 
 598                         release_request(request);
 
 600                         start_fetch_packed(request);
 
 602         } else if (request->state == RUN_FETCH_PACKED) {
 
 604                 if (request->curl_result != CURLE_OK) {
 
 605                         fprintf(stderr, "Unable to get pack file %s\n%s",
 
 606                                 request->url, curl_errorstr);
 
 608                         preq = (struct http_pack_request *)request->userData;
 
 611                                 if (finish_http_pack_request(preq) == 0)
 
 613                                 release_http_pack_request(preq);
 
 617                         repo->can_update_info_refs = 0;
 
 618                 release_request(request);
 
 622 #ifdef USE_CURL_MULTI
 
 623 static int is_running_queue;
 
 624 static int fill_active_slot(void *unused)
 
 626         struct transfer_request *request;
 
 628         if (aborted || !is_running_queue)
 
 631         for (request = request_queue_head; request; request = request->next) {
 
 632                 if (request->state == NEED_FETCH) {
 
 633                         start_fetch_loose(request);
 
 635                 } else if (pushing && request->state == NEED_PUSH) {
 
 636                         if (remote_dir_exists[request->obj->sha1[0]] == 1) {
 
 639                                 start_mkcol(request);
 
 648 static void get_remote_object_list(unsigned char parent);
 
 650 static void add_fetch_request(struct object *obj)
 
 652         struct transfer_request *request;
 
 657          * Don't fetch the object if it's known to exist locally
 
 658          * or is already in the request queue
 
 660         if (remote_dir_exists[obj->sha1[0]] == -1)
 
 661                 get_remote_object_list(obj->sha1[0]);
 
 662         if (obj->flags & (LOCAL | FETCHING))
 
 665         obj->flags |= FETCHING;
 
 666         request = xmalloc(sizeof(*request));
 
 669         request->lock = NULL;
 
 670         request->headers = NULL;
 
 671         request->state = NEED_FETCH;
 
 672         request->next = request_queue_head;
 
 673         request_queue_head = request;
 
 675 #ifdef USE_CURL_MULTI
 
 681 static int add_send_request(struct object *obj, struct remote_lock *lock)
 
 683         struct transfer_request *request = request_queue_head;
 
 684         struct packed_git *target;
 
 686         /* Keep locks active */
 
 690          * Don't push the object if it's known to exist on the remote
 
 691          * or is already in the request queue
 
 693         if (remote_dir_exists[obj->sha1[0]] == -1)
 
 694                 get_remote_object_list(obj->sha1[0]);
 
 695         if (obj->flags & (REMOTE | PUSHING))
 
 697         target = find_sha1_pack(obj->sha1, repo->packs);
 
 699                 obj->flags |= REMOTE;
 
 703         obj->flags |= PUSHING;
 
 704         request = xmalloc(sizeof(*request));
 
 707         request->lock = lock;
 
 708         request->headers = NULL;
 
 709         request->state = NEED_PUSH;
 
 710         request->next = request_queue_head;
 
 711         request_queue_head = request;
 
 713 #ifdef USE_CURL_MULTI
 
 721 static int fetch_indices(void)
 
 726                 fprintf(stderr, "Getting pack list\n");
 
 728         switch (http_get_info_packs(repo->url, &repo->packs)) {
 
 730         case HTTP_MISSING_TARGET:
 
 740 static void one_remote_object(const char *hex)
 
 742         unsigned char sha1[20];
 
 745         if (get_sha1_hex(hex, sha1) != 0)
 
 748         obj = lookup_object(sha1);
 
 750                 obj = parse_object(sha1);
 
 752         /* Ignore remote objects that don't exist locally */
 
 756         obj->flags |= REMOTE;
 
 757         if (!object_list_contains(objects, obj))
 
 758                 object_list_insert(obj, &objects);
 
 761 static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed)
 
 763         int *lock_flags = (int *)ctx->userData;
 
 766                 if (!strcmp(ctx->name, DAV_CTX_LOCKENTRY)) {
 
 767                         if ((*lock_flags & DAV_PROP_LOCKEX) &&
 
 768                             (*lock_flags & DAV_PROP_LOCKWR)) {
 
 769                                 *lock_flags |= DAV_LOCK_OK;
 
 771                         *lock_flags &= DAV_LOCK_OK;
 
 772                 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_WRITE)) {
 
 773                         *lock_flags |= DAV_PROP_LOCKWR;
 
 774                 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_EXCLUSIVE)) {
 
 775                         *lock_flags |= DAV_PROP_LOCKEX;
 
 780 static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
 
 782         struct remote_lock *lock = (struct remote_lock *)ctx->userData;
 
 784         unsigned char lock_token_sha1[20];
 
 786         if (tag_closed && ctx->cdata) {
 
 787                 if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) {
 
 788                         lock->owner = xmalloc(strlen(ctx->cdata) + 1);
 
 789                         strcpy(lock->owner, ctx->cdata);
 
 790                 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
 
 791                         if (!prefixcmp(ctx->cdata, "Second-"))
 
 793                                         strtol(ctx->cdata + 7, NULL, 10);
 
 794                 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
 
 795                         lock->token = xmalloc(strlen(ctx->cdata) + 1);
 
 796                         strcpy(lock->token, ctx->cdata);
 
 798                         git_SHA1_Init(&sha_ctx);
 
 799                         git_SHA1_Update(&sha_ctx, lock->token, strlen(lock->token));
 
 800                         git_SHA1_Final(lock_token_sha1, &sha_ctx);
 
 802                         lock->tmpfile_suffix[0] = '_';
 
 803                         memcpy(lock->tmpfile_suffix + 1, sha1_to_hex(lock_token_sha1), 40);
 
 808 static void one_remote_ref(const char *refname);
 
 811 xml_start_tag(void *userData, const char *name, const char **atts)
 
 813         struct xml_ctx *ctx = (struct xml_ctx *)userData;
 
 814         const char *c = strchr(name, ':');
 
 822         new_len = strlen(ctx->name) + strlen(c) + 2;
 
 824         if (new_len > ctx->len) {
 
 825                 ctx->name = xrealloc(ctx->name, new_len);
 
 828         strcat(ctx->name, ".");
 
 829         strcat(ctx->name, c);
 
 834         ctx->userFunc(ctx, 0);
 
 838 xml_end_tag(void *userData, const char *name)
 
 840         struct xml_ctx *ctx = (struct xml_ctx *)userData;
 
 841         const char *c = strchr(name, ':');
 
 844         ctx->userFunc(ctx, 1);
 
 851         ep = ctx->name + strlen(ctx->name) - strlen(c) - 1;
 
 856 xml_cdata(void *userData, const XML_Char *s, int len)
 
 858         struct xml_ctx *ctx = (struct xml_ctx *)userData;
 
 860         ctx->cdata = xmemdupz(s, len);
 
 863 static struct remote_lock *lock_remote(const char *path, long timeout)
 
 865         struct active_request_slot *slot;
 
 866         struct slot_results results;
 
 867         struct buffer out_buffer = { STRBUF_INIT, 0 };
 
 868         struct strbuf in_buffer = STRBUF_INIT;
 
 871         char timeout_header[25];
 
 872         struct remote_lock *lock = NULL;
 
 873         struct curl_slist *dav_headers = NULL;
 
 877         url = xmalloc(strlen(repo->url) + strlen(path) + 1);
 
 878         sprintf(url, "%s%s", repo->url, path);
 
 880         /* Make sure leading directories exist for the remote ref */
 
 881         ep = strchr(url + strlen(repo->url) + 1, '/');
 
 883                 char saved_character = ep[1];
 
 885                 slot = get_active_slot();
 
 886                 slot->results = &results;
 
 887                 curl_setup_http_get(slot->curl, url, DAV_MKCOL);
 
 888                 if (start_active_slot(slot)) {
 
 889                         run_active_slot(slot);
 
 890                         if (results.curl_result != CURLE_OK &&
 
 891                             results.http_code != 405) {
 
 893                                         "Unable to create branch path %s\n",
 
 899                         fprintf(stderr, "Unable to start MKCOL request\n");
 
 903                 ep[1] = saved_character;
 
 904                 ep = strchr(ep + 1, '/');
 
 907         escaped = xml_entities(git_default_email);
 
 908         strbuf_addf(&out_buffer.buf, LOCK_REQUEST, escaped);
 
 911         sprintf(timeout_header, "Timeout: Second-%ld", timeout);
 
 912         dav_headers = curl_slist_append(dav_headers, timeout_header);
 
 913         dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
 
 915         slot = get_active_slot();
 
 916         slot->results = &results;
 
 917         curl_setup_http(slot->curl, url, DAV_LOCK, &out_buffer, fwrite_buffer);
 
 918         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
 
 919         curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
 
 921         lock = xcalloc(1, sizeof(*lock));
 
 924         if (start_active_slot(slot)) {
 
 925                 run_active_slot(slot);
 
 926                 if (results.curl_result == CURLE_OK) {
 
 927                         XML_Parser parser = XML_ParserCreate(NULL);
 
 928                         enum XML_Status result;
 
 929                         ctx.name = xcalloc(10, 1);
 
 932                         ctx.userFunc = handle_new_lock_ctx;
 
 934                         XML_SetUserData(parser, &ctx);
 
 935                         XML_SetElementHandler(parser, xml_start_tag,
 
 937                         XML_SetCharacterDataHandler(parser, xml_cdata);
 
 938                         result = XML_Parse(parser, in_buffer.buf,
 
 941                         if (result != XML_STATUS_OK) {
 
 942                                 fprintf(stderr, "XML error: %s\n",
 
 944                                                 XML_GetErrorCode(parser)));
 
 947                         XML_ParserFree(parser);
 
 950                 fprintf(stderr, "Unable to start LOCK request\n");
 
 953         curl_slist_free_all(dav_headers);
 
 954         strbuf_release(&out_buffer.buf);
 
 955         strbuf_release(&in_buffer);
 
 957         if (lock->token == NULL || lock->timeout <= 0) {
 
 965                 lock->start_time = time(NULL);
 
 966                 lock->next = repo->locks;
 
 973 static int unlock_remote(struct remote_lock *lock)
 
 975         struct active_request_slot *slot;
 
 976         struct slot_results results;
 
 977         struct remote_lock *prev = repo->locks;
 
 978         struct curl_slist *dav_headers;
 
 981         dav_headers = get_dav_token_headers(lock, DAV_HEADER_LOCK);
 
 983         slot = get_active_slot();
 
 984         slot->results = &results;
 
 985         curl_setup_http_get(slot->curl, lock->url, DAV_UNLOCK);
 
 986         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
 
 988         if (start_active_slot(slot)) {
 
 989                 run_active_slot(slot);
 
 990                 if (results.curl_result == CURLE_OK)
 
 993                         fprintf(stderr, "UNLOCK HTTP error %ld\n",
 
 996                 fprintf(stderr, "Unable to start UNLOCK request\n");
 
 999         curl_slist_free_all(dav_headers);
 
1001         if (repo->locks == lock) {
 
1002                 repo->locks = lock->next;
 
1004                 while (prev && prev->next != lock)
 
1007                         prev->next = prev->next->next;
 
1018 static void remove_locks(void)
 
1020         struct remote_lock *lock = repo->locks;
 
1022         fprintf(stderr, "Removing remote locks...\n");
 
1024                 struct remote_lock *next = lock->next;
 
1025                 unlock_remote(lock);
 
1030 static void remove_locks_on_signal(int signo)
 
1033         sigchain_pop(signo);
 
1037 static void remote_ls(const char *path, int flags,
 
1038                       void (*userFunc)(struct remote_ls_ctx *ls),
 
1041 static void process_ls_object(struct remote_ls_ctx *ls)
 
1043         unsigned int *parent = (unsigned int *)ls->userData;
 
1044         char *path = ls->dentry_name;
 
1047         if (!strcmp(ls->path, ls->dentry_name) && (ls->flags & IS_DIR)) {
 
1048                 remote_dir_exists[*parent] = 1;
 
1052         if (strlen(path) != 49)
 
1055         obj_hex = xmalloc(strlen(path));
 
1056         /* NB: path is not null-terminated, can not use strlcpy here */
 
1057         memcpy(obj_hex, path, 2);
 
1058         strcpy(obj_hex + 2, path + 3);
 
1059         one_remote_object(obj_hex);
 
1063 static void process_ls_ref(struct remote_ls_ctx *ls)
 
1065         if (!strcmp(ls->path, ls->dentry_name) && (ls->dentry_flags & IS_DIR)) {
 
1066                 fprintf(stderr, "  %s\n", ls->dentry_name);
 
1070         if (!(ls->dentry_flags & IS_DIR))
 
1071                 one_remote_ref(ls->dentry_name);
 
1074 static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
 
1076         struct remote_ls_ctx *ls = (struct remote_ls_ctx *)ctx->userData;
 
1079                 if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) {
 
1080                         if (ls->dentry_flags & IS_DIR) {
 
1082                                 /* ensure collection names end with slash */
 
1083                                 str_end_url_with_slash(ls->dentry_name, &ls->dentry_name);
 
1085                                 if (ls->flags & PROCESS_DIRS) {
 
1088                                 if (strcmp(ls->dentry_name, ls->path) &&
 
1089                                     ls->flags & RECURSIVE) {
 
1090                                         remote_ls(ls->dentry_name,
 
1095                         } else if (ls->flags & PROCESS_FILES) {
 
1098                 } else if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
 
1099                         char *path = ctx->cdata;
 
1100                         if (*ctx->cdata == 'h') {
 
1101                                 path = strstr(path, "//");
 
1103                                         path = strchr(path+2, '/');
 
1107                                 const char *url = repo->url;
 
1110                                 if (strncmp(path, url, repo->path_len))
 
1111                                         error("Parsed path '%s' does not match url: '%s'\n",
 
1114                                         path += repo->path_len;
 
1115                                         ls->dentry_name = xstrdup(path);
 
1118                 } else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
 
1119                         ls->dentry_flags |= IS_DIR;
 
1121         } else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
 
1122                 free(ls->dentry_name);
 
1123                 ls->dentry_name = NULL;
 
1124                 ls->dentry_flags = 0;
 
1129  * NEEDSWORK: remote_ls() ignores info/refs on the remote side.  But it
 
1130  * should _only_ heed the information from that file, instead of trying to
 
1131  * determine the refs from the remote file system (badly: it does not even
 
1132  * know about packed-refs).
 
1134 static void remote_ls(const char *path, int flags,
 
1135                       void (*userFunc)(struct remote_ls_ctx *ls),
 
1138         char *url = xmalloc(strlen(repo->url) + strlen(path) + 1);
 
1139         struct active_request_slot *slot;
 
1140         struct slot_results results;
 
1141         struct strbuf in_buffer = STRBUF_INIT;
 
1142         struct buffer out_buffer = { STRBUF_INIT, 0 };
 
1143         struct curl_slist *dav_headers = NULL;
 
1145         struct remote_ls_ctx ls;
 
1148         ls.path = xstrdup(path);
 
1149         ls.dentry_name = NULL;
 
1150         ls.dentry_flags = 0;
 
1151         ls.userData = userData;
 
1152         ls.userFunc = userFunc;
 
1154         sprintf(url, "%s%s", repo->url, path);
 
1156         strbuf_addf(&out_buffer.buf, PROPFIND_ALL_REQUEST);
 
1158         dav_headers = curl_slist_append(dav_headers, "Depth: 1");
 
1159         dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
 
1161         slot = get_active_slot();
 
1162         slot->results = &results;
 
1163         curl_setup_http(slot->curl, url, DAV_PROPFIND,
 
1164                         &out_buffer, fwrite_buffer);
 
1165         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
 
1166         curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
 
1168         if (start_active_slot(slot)) {
 
1169                 run_active_slot(slot);
 
1170                 if (results.curl_result == CURLE_OK) {
 
1171                         XML_Parser parser = XML_ParserCreate(NULL);
 
1172                         enum XML_Status result;
 
1173                         ctx.name = xcalloc(10, 1);
 
1176                         ctx.userFunc = handle_remote_ls_ctx;
 
1178                         XML_SetUserData(parser, &ctx);
 
1179                         XML_SetElementHandler(parser, xml_start_tag,
 
1181                         XML_SetCharacterDataHandler(parser, xml_cdata);
 
1182                         result = XML_Parse(parser, in_buffer.buf,
 
1186                         if (result != XML_STATUS_OK) {
 
1187                                 fprintf(stderr, "XML error: %s\n",
 
1189                                                 XML_GetErrorCode(parser)));
 
1191                         XML_ParserFree(parser);
 
1194                 fprintf(stderr, "Unable to start PROPFIND request\n");
 
1199         strbuf_release(&out_buffer.buf);
 
1200         strbuf_release(&in_buffer);
 
1201         curl_slist_free_all(dav_headers);
 
1204 static void get_remote_object_list(unsigned char parent)
 
1206         char path[] = "objects/XX/";
 
1207         static const char hex[] = "0123456789abcdef";
 
1208         unsigned int val = parent;
 
1210         path[8] = hex[val >> 4];
 
1211         path[9] = hex[val & 0xf];
 
1212         remote_dir_exists[val] = 0;
 
1213         remote_ls(path, (PROCESS_FILES | PROCESS_DIRS),
 
1214                   process_ls_object, &val);
 
1217 static int locking_available(void)
 
1219         struct active_request_slot *slot;
 
1220         struct slot_results results;
 
1221         struct strbuf in_buffer = STRBUF_INIT;
 
1222         struct buffer out_buffer = { STRBUF_INIT, 0 };
 
1223         struct curl_slist *dav_headers = NULL;
 
1228         escaped = xml_entities(repo->url);
 
1229         strbuf_addf(&out_buffer.buf, PROPFIND_SUPPORTEDLOCK_REQUEST, escaped);
 
1232         dav_headers = curl_slist_append(dav_headers, "Depth: 0");
 
1233         dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
 
1235         slot = get_active_slot();
 
1236         slot->results = &results;
 
1237         curl_setup_http(slot->curl, repo->url, DAV_PROPFIND,
 
1238                         &out_buffer, fwrite_buffer);
 
1239         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
 
1240         curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
 
1242         if (start_active_slot(slot)) {
 
1243                 run_active_slot(slot);
 
1244                 if (results.curl_result == CURLE_OK) {
 
1245                         XML_Parser parser = XML_ParserCreate(NULL);
 
1246                         enum XML_Status result;
 
1247                         ctx.name = xcalloc(10, 1);
 
1250                         ctx.userFunc = handle_lockprop_ctx;
 
1251                         ctx.userData = &lock_flags;
 
1252                         XML_SetUserData(parser, &ctx);
 
1253                         XML_SetElementHandler(parser, xml_start_tag,
 
1255                         result = XML_Parse(parser, in_buffer.buf,
 
1259                         if (result != XML_STATUS_OK) {
 
1260                                 fprintf(stderr, "XML error: %s\n",
 
1262                                                 XML_GetErrorCode(parser)));
 
1265                         XML_ParserFree(parser);
 
1267                                 error("no DAV locking support on %s",
 
1271                         error("Cannot access URL %s, return code %d",
 
1272                               repo->url, results.curl_result);
 
1276                 error("Unable to start PROPFIND request on %s", repo->url);
 
1279         strbuf_release(&out_buffer.buf);
 
1280         strbuf_release(&in_buffer);
 
1281         curl_slist_free_all(dav_headers);
 
1286 static struct object_list **add_one_object(struct object *obj, struct object_list **p)
 
1288         struct object_list *entry = xmalloc(sizeof(struct object_list));
 
1292         return &entry->next;
 
1295 static struct object_list **process_blob(struct blob *blob,
 
1296                                          struct object_list **p,
 
1297                                          struct name_path *path,
 
1300         struct object *obj = &blob->object;
 
1302         obj->flags |= LOCAL;
 
1304         if (obj->flags & (UNINTERESTING | SEEN))
 
1308         return add_one_object(obj, p);
 
1311 static struct object_list **process_tree(struct tree *tree,
 
1312                                          struct object_list **p,
 
1313                                          struct name_path *path,
 
1316         struct object *obj = &tree->object;
 
1317         struct tree_desc desc;
 
1318         struct name_entry entry;
 
1319         struct name_path me;
 
1321         obj->flags |= LOCAL;
 
1323         if (obj->flags & (UNINTERESTING | SEEN))
 
1325         if (parse_tree(tree) < 0)
 
1326                 die("bad tree object %s", sha1_to_hex(obj->sha1));
 
1329         name = xstrdup(name);
 
1330         p = add_one_object(obj, p);
 
1333         me.elem_len = strlen(name);
 
1335         init_tree_desc(&desc, tree->buffer, tree->size);
 
1337         while (tree_entry(&desc, &entry))
 
1338                 switch (object_type(entry.mode)) {
 
1340                         p = process_tree(lookup_tree(entry.sha1), p, &me, name);
 
1343                         p = process_blob(lookup_blob(entry.sha1), p, &me, name);
 
1346                         /* Subproject commit - not in this repository */
 
1351         tree->buffer = NULL;
 
1355 static int get_delta(struct rev_info *revs, struct remote_lock *lock)
 
1358         struct commit *commit;
 
1359         struct object_list **p = &objects;
 
1362         while ((commit = get_revision(revs)) != NULL) {
 
1363                 p = process_tree(commit->tree, p, NULL, "");
 
1364                 commit->object.flags |= LOCAL;
 
1365                 if (!(commit->object.flags & UNINTERESTING))
 
1366                         count += add_send_request(&commit->object, lock);
 
1369         for (i = 0; i < revs->pending.nr; i++) {
 
1370                 struct object_array_entry *entry = revs->pending.objects + i;
 
1371                 struct object *obj = entry->item;
 
1372                 const char *name = entry->name;
 
1374                 if (obj->flags & (UNINTERESTING | SEEN))
 
1376                 if (obj->type == OBJ_TAG) {
 
1378                         p = add_one_object(obj, p);
 
1381                 if (obj->type == OBJ_TREE) {
 
1382                         p = process_tree((struct tree *)obj, p, NULL, name);
 
1385                 if (obj->type == OBJ_BLOB) {
 
1386                         p = process_blob((struct blob *)obj, p, NULL, name);
 
1389                 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
 
1393                 if (!(objects->item->flags & UNINTERESTING))
 
1394                         count += add_send_request(objects->item, lock);
 
1395                 objects = objects->next;
 
1401 static int update_remote(unsigned char *sha1, struct remote_lock *lock)
 
1403         struct active_request_slot *slot;
 
1404         struct slot_results results;
 
1405         struct buffer out_buffer = { STRBUF_INIT, 0 };
 
1406         struct curl_slist *dav_headers;
 
1408         dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
 
1410         strbuf_addf(&out_buffer.buf, "%s\n", sha1_to_hex(sha1));
 
1412         slot = get_active_slot();
 
1413         slot->results = &results;
 
1414         curl_setup_http(slot->curl, lock->url, DAV_PUT,
 
1415                         &out_buffer, fwrite_null);
 
1416         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
 
1418         if (start_active_slot(slot)) {
 
1419                 run_active_slot(slot);
 
1420                 strbuf_release(&out_buffer.buf);
 
1421                 if (results.curl_result != CURLE_OK) {
 
1423                                 "PUT error: curl result=%d, HTTP code=%ld\n",
 
1424                                 results.curl_result, results.http_code);
 
1425                         /* We should attempt recovery? */
 
1429                 strbuf_release(&out_buffer.buf);
 
1430                 fprintf(stderr, "Unable to start PUT request\n");
 
1437 static struct ref *remote_refs;
 
1439 static void one_remote_ref(const char *refname)
 
1444         ref = alloc_ref(refname);
 
1446         if (http_fetch_ref(repo->url, ref) != 0) {
 
1448                         "Unable to fetch ref %s from %s\n",
 
1449                         refname, repo->url);
 
1455          * Fetch a copy of the object if it doesn't exist locally - it
 
1456          * may be required for updating server info later.
 
1458         if (repo->can_update_info_refs && !has_sha1_file(ref->old_sha1)) {
 
1459                 obj = lookup_unknown_object(ref->old_sha1);
 
1461                         fprintf(stderr, "  fetch %s for %s\n",
 
1462                                 sha1_to_hex(ref->old_sha1), refname);
 
1463                         add_fetch_request(obj);
 
1467         ref->next = remote_refs;
 
1471 static void get_dav_remote_heads(void)
 
1473         remote_ls("refs/", (PROCESS_FILES | PROCESS_DIRS | RECURSIVE), process_ls_ref, NULL);
 
1476 static void add_remote_info_ref(struct remote_ls_ctx *ls)
 
1478         struct strbuf *buf = (struct strbuf *)ls->userData;
 
1484         ref = alloc_ref(ls->dentry_name);
 
1486         if (http_fetch_ref(repo->url, ref) != 0) {
 
1488                         "Unable to fetch ref %s from %s\n",
 
1489                         ls->dentry_name, repo->url);
 
1495         o = parse_object(ref->old_sha1);
 
1498                         "Unable to parse object %s for remote ref %s\n",
 
1499                         sha1_to_hex(ref->old_sha1), ls->dentry_name);
 
1505         len = strlen(ls->dentry_name) + 42;
 
1506         ref_info = xcalloc(len + 1, 1);
 
1507         sprintf(ref_info, "%s   %s\n",
 
1508                 sha1_to_hex(ref->old_sha1), ls->dentry_name);
 
1509         fwrite_buffer(ref_info, 1, len, buf);
 
1512         if (o->type == OBJ_TAG) {
 
1513                 o = deref_tag(o, ls->dentry_name, 0);
 
1515                         len = strlen(ls->dentry_name) + 45;
 
1516                         ref_info = xcalloc(len + 1, 1);
 
1517                         sprintf(ref_info, "%s   %s^{}\n",
 
1518                                 sha1_to_hex(o->sha1), ls->dentry_name);
 
1519                         fwrite_buffer(ref_info, 1, len, buf);
 
1526 static void update_remote_info_refs(struct remote_lock *lock)
 
1528         struct buffer buffer = { STRBUF_INIT, 0 };
 
1529         struct active_request_slot *slot;
 
1530         struct slot_results results;
 
1531         struct curl_slist *dav_headers;
 
1533         remote_ls("refs/", (PROCESS_FILES | RECURSIVE),
 
1534                   add_remote_info_ref, &buffer.buf);
 
1536                 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
 
1538                 slot = get_active_slot();
 
1539                 slot->results = &results;
 
1540                 curl_setup_http(slot->curl, lock->url, DAV_PUT,
 
1541                                 &buffer, fwrite_null);
 
1542                 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
 
1544                 if (start_active_slot(slot)) {
 
1545                         run_active_slot(slot);
 
1546                         if (results.curl_result != CURLE_OK) {
 
1548                                         "PUT error: curl result=%d, HTTP code=%ld\n",
 
1549                                         results.curl_result, results.http_code);
 
1553         strbuf_release(&buffer.buf);
 
1556 static int remote_exists(const char *path)
 
1558         char *url = xmalloc(strlen(repo->url) + strlen(path) + 1);
 
1561         sprintf(url, "%s%s", repo->url, path);
 
1563         switch (http_get_strbuf(url, NULL, 0)) {
 
1567         case HTTP_MISSING_TARGET:
 
1571                 http_error(url, HTTP_ERROR);
 
1579 static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
 
1582         struct strbuf buffer = STRBUF_INIT;
 
1584         url = xmalloc(strlen(repo->url) + strlen(path) + 1);
 
1585         sprintf(url, "%s%s", repo->url, path);
 
1587         if (http_get_strbuf(url, &buffer, 0) != HTTP_OK)
 
1588                 die("Couldn't get %s for remote symref\n%s", url,
 
1596         if (buffer.len == 0)
 
1599         /* If it's a symref, set the refname; otherwise try for a sha1 */
 
1600         if (!prefixcmp((char *)buffer.buf, "ref: ")) {
 
1601                 *symref = xmemdupz((char *)buffer.buf + 5, buffer.len - 6);
 
1603                 get_sha1_hex(buffer.buf, sha1);
 
1606         strbuf_release(&buffer);
 
1609 static int verify_merge_base(unsigned char *head_sha1, struct ref *remote)
 
1611         struct commit *head = lookup_commit_or_die(head_sha1, "HEAD");
 
1612         struct commit *branch = lookup_commit_or_die(remote->old_sha1, remote->name);
 
1613         struct commit_list *merge_bases = get_merge_bases(head, branch, 1);
 
1615         return (merge_bases && !merge_bases->next && merge_bases->item == branch);
 
1618 static int delete_remote_branch(const char *pattern, int force)
 
1620         struct ref *refs = remote_refs;
 
1621         struct ref *remote_ref = NULL;
 
1622         unsigned char head_sha1[20];
 
1623         char *symref = NULL;
 
1625         int patlen = strlen(pattern);
 
1627         struct active_request_slot *slot;
 
1628         struct slot_results results;
 
1631         /* Find the remote branch(es) matching the specified branch name */
 
1632         for (match = 0; refs; refs = refs->next) {
 
1633                 char *name = refs->name;
 
1634                 int namelen = strlen(name);
 
1635                 if (namelen < patlen ||
 
1636                     memcmp(name + namelen - patlen, pattern, patlen))
 
1638                 if (namelen != patlen && name[namelen - patlen - 1] != '/')
 
1644                 return error("No remote branch matches %s", pattern);
 
1646                 return error("More than one remote branch matches %s",
 
1650          * Remote HEAD must be a symref (not exactly foolproof; a remote
 
1651          * symlink to a symref will look like a symref)
 
1653         fetch_symref("HEAD", &symref, head_sha1);
 
1655                 return error("Remote HEAD is not a symref");
 
1657         /* Remote branch must not be the remote HEAD */
 
1658         for (i = 0; symref && i < MAXDEPTH; i++) {
 
1659                 if (!strcmp(remote_ref->name, symref))
 
1660                         return error("Remote branch %s is the current HEAD",
 
1662                 fetch_symref(symref, &symref, head_sha1);
 
1665         /* Run extra sanity checks if delete is not forced */
 
1667                 /* Remote HEAD must resolve to a known object */
 
1669                         return error("Remote HEAD symrefs too deep");
 
1670                 if (is_null_sha1(head_sha1))
 
1671                         return error("Unable to resolve remote HEAD");
 
1672                 if (!has_sha1_file(head_sha1))
 
1673                         return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1));
 
1675                 /* Remote branch must resolve to a known object */
 
1676                 if (is_null_sha1(remote_ref->old_sha1))
 
1677                         return error("Unable to resolve remote branch %s",
 
1679                 if (!has_sha1_file(remote_ref->old_sha1))
 
1680                         return error("Remote branch %s resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", remote_ref->name, sha1_to_hex(remote_ref->old_sha1));
 
1682                 /* Remote branch must be an ancestor of remote HEAD */
 
1683                 if (!verify_merge_base(head_sha1, remote_ref)) {
 
1684                         return error("The branch '%s' is not an ancestor "
 
1685                                      "of your current HEAD.\n"
 
1686                                      "If you are sure you want to delete it,"
 
1687                                      " run:\n\t'git http-push -D %s %s'",
 
1688                                      remote_ref->name, repo->url, pattern);
 
1692         /* Send delete request */
 
1693         fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name);
 
1696         url = xmalloc(strlen(repo->url) + strlen(remote_ref->name) + 1);
 
1697         sprintf(url, "%s%s", repo->url, remote_ref->name);
 
1698         slot = get_active_slot();
 
1699         slot->results = &results;
 
1700         curl_setup_http_get(slot->curl, url, DAV_DELETE);
 
1701         if (start_active_slot(slot)) {
 
1702                 run_active_slot(slot);
 
1704                 if (results.curl_result != CURLE_OK)
 
1705                         return error("DELETE request failed (%d/%ld)\n",
 
1706                                      results.curl_result, results.http_code);
 
1709                 return error("Unable to start DELETE request");
 
1715 static void run_request_queue(void)
 
1717 #ifdef USE_CURL_MULTI
 
1718         is_running_queue = 1;
 
1719         fill_active_slots();
 
1720         add_fill_function(NULL, fill_active_slot);
 
1723                 finish_all_active_slots();
 
1724 #ifdef USE_CURL_MULTI
 
1725                 fill_active_slots();
 
1727         } while (request_queue_head && !aborted);
 
1729 #ifdef USE_CURL_MULTI
 
1730         is_running_queue = 0;
 
1734 int main(int argc, char **argv)
 
1736         struct transfer_request *request;
 
1737         struct transfer_request *next_request;
 
1739         char **refspec = NULL;
 
1740         struct remote_lock *ref_lock = NULL;
 
1741         struct remote_lock *info_ref_lock = NULL;
 
1742         struct rev_info revs;
 
1743         int delete_branch = 0;
 
1744         int force_delete = 0;
 
1745         int objects_to_send;
 
1749         struct ref *ref, *local_refs;
 
1751         git_setup_gettext();
 
1753         git_extract_argv0_path(argv[0]);
 
1755         repo = xcalloc(sizeof(*repo), 1);
 
1758         for (i = 1; i < argc; i++, argv++) {
 
1762                         if (!strcmp(arg, "--all")) {
 
1763                                 push_all = MATCH_REFS_ALL;
 
1766                         if (!strcmp(arg, "--force")) {
 
1770                         if (!strcmp(arg, "--dry-run")) {
 
1774                         if (!strcmp(arg, "--helper-status")) {
 
1778                         if (!strcmp(arg, "--verbose")) {
 
1780                                 http_is_verbose = 1;
 
1783                         if (!strcmp(arg, "-d")) {
 
1787                         if (!strcmp(arg, "-D")) {
 
1792                         if (!strcmp(arg, "-h"))
 
1793                                 usage(http_push_usage);
 
1796                         char *path = strstr(arg, "//");
 
1797                         str_end_url_with_slash(arg, &repo->url);
 
1798                         repo->path_len = strlen(repo->url);
 
1800                                 repo->path = strchr(path+2, '/');
 
1802                                         repo->path_len = strlen(repo->path);
 
1807                 nr_refspec = argc - i;
 
1811 #ifndef USE_CURL_MULTI
 
1812         die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
 
1816                 usage(http_push_usage);
 
1818         if (delete_branch && nr_refspec != 1)
 
1819                 die("You must specify only one branch name when deleting a remote branch");
 
1821         setup_git_directory();
 
1823         memset(remote_dir_exists, -1, 256);
 
1825         http_init(NULL, repo->url, 1);
 
1827 #ifdef USE_CURL_MULTI
 
1828         is_running_queue = 0;
 
1831         /* Verify DAV compliance/lock support */
 
1832         if (!locking_available()) {
 
1837         sigchain_push_common(remove_locks_on_signal);
 
1839         /* Check whether the remote has server info files */
 
1840         repo->can_update_info_refs = 0;
 
1841         repo->has_info_refs = remote_exists("info/refs");
 
1842         repo->has_info_packs = remote_exists("objects/info/packs");
 
1843         if (repo->has_info_refs) {
 
1844                 info_ref_lock = lock_remote("info/refs", LOCK_TIME);
 
1846                         repo->can_update_info_refs = 1;
 
1848                         error("cannot lock existing info/refs");
 
1853         if (repo->has_info_packs)
 
1856         /* Get a list of all local and remote heads to validate refspecs */
 
1857         local_refs = get_local_heads();
 
1858         fprintf(stderr, "Fetching remote heads...\n");
 
1859         get_dav_remote_heads();
 
1860         run_request_queue();
 
1862         /* Remove a remote branch if -d or -D was specified */
 
1863         if (delete_branch) {
 
1864                 if (delete_remote_branch(refspec[0], force_delete) == -1) {
 
1865                         fprintf(stderr, "Unable to delete remote branch %s\n",
 
1868                                 printf("error %s cannot remove\n", refspec[0]);
 
1874         if (match_push_refs(local_refs, &remote_refs,
 
1875                             nr_refspec, (const char **) refspec, push_all)) {
 
1880                 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
 
1882                         printf("error null no match\n");
 
1888         for (ref = remote_refs; ref; ref = ref->next) {
 
1889                 char old_hex[60], *new_hex;
 
1890                 const char *commit_argv[5];
 
1892                 char *new_sha1_hex, *old_sha1_hex;
 
1897                 if (is_null_sha1(ref->peer_ref->new_sha1)) {
 
1898                         if (delete_remote_branch(ref->name, 1) == -1) {
 
1899                                 error("Could not remove %s", ref->name);
 
1901                                         printf("error %s cannot remove\n", ref->name);
 
1904                         else if (helper_status)
 
1905                                 printf("ok %s\n", ref->name);
 
1910                 if (!hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
 
1912                                 fprintf(stderr, "'%s': up-to-date\n", ref->name);
 
1914                                 printf("ok %s up to date\n", ref->name);
 
1919                     !is_null_sha1(ref->old_sha1) &&
 
1921                         if (!has_sha1_file(ref->old_sha1) ||
 
1922                             !ref_newer(ref->peer_ref->new_sha1,
 
1925                                  * We do not have the remote ref, or
 
1926                                  * we know that the remote ref is not
 
1927                                  * an ancestor of what we are trying to
 
1928                                  * push.  Either way this can be losing
 
1929                                  * commits at the remote end and likely
 
1930                                  * we were not up to date to begin with.
 
1932                                 error("remote '%s' is not an ancestor of\n"
 
1934                                       "Maybe you are not up-to-date and "
 
1935                                       "need to pull first?",
 
1937                                       ref->peer_ref->name);
 
1939                                         printf("error %s non-fast forward\n", ref->name);
 
1944                 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
 
1946                 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
 
1947                 new_hex = sha1_to_hex(ref->new_sha1);
 
1949                 fprintf(stderr, "updating '%s'", ref->name);
 
1950                 if (strcmp(ref->name, ref->peer_ref->name))
 
1951                         fprintf(stderr, " using '%s'", ref->peer_ref->name);
 
1952                 fprintf(stderr, "\n  from %s\n  to   %s\n", old_hex, new_hex);
 
1955                                 printf("ok %s\n", ref->name);
 
1959                 /* Lock remote branch ref */
 
1960                 ref_lock = lock_remote(ref->name, LOCK_TIME);
 
1961                 if (ref_lock == NULL) {
 
1962                         fprintf(stderr, "Unable to lock remote branch %s\n",
 
1965                                 printf("error %s lock error\n", ref->name);
 
1970                 /* Set up revision info for this refspec */
 
1972                 new_sha1_hex = xstrdup(sha1_to_hex(ref->new_sha1));
 
1973                 old_sha1_hex = NULL;
 
1974                 commit_argv[1] = "--objects";
 
1975                 commit_argv[2] = new_sha1_hex;
 
1976                 if (!push_all && !is_null_sha1(ref->old_sha1)) {
 
1977                         old_sha1_hex = xmalloc(42);
 
1978                         sprintf(old_sha1_hex, "^%s",
 
1979                                 sha1_to_hex(ref->old_sha1));
 
1980                         commit_argv[3] = old_sha1_hex;
 
1983                 commit_argv[commit_argc] = NULL;
 
1984                 init_revisions(&revs, setup_git_directory());
 
1985                 setup_revisions(commit_argc, commit_argv, &revs, NULL);
 
1986                 revs.edge_hint = 0; /* just in case */
 
1990                         commit_argv[1] = NULL;
 
1993                 /* Generate a list of objects that need to be pushed */
 
1995                 if (prepare_revision_walk(&revs))
 
1996                         die("revision walk setup failed");
 
1997                 mark_edges_uninteresting(revs.commits, &revs, NULL);
 
1998                 objects_to_send = get_delta(&revs, ref_lock);
 
1999                 finish_all_active_slots();
 
2001                 /* Push missing objects to remote, this would be a
 
2002                    convenient time to pack them first if appropriate. */
 
2004                 if (objects_to_send)
 
2005                         fprintf(stderr, "    sending %d objects\n",
 
2008                 run_request_queue();
 
2010                 /* Update the remote branch if all went well */
 
2011                 if (aborted || !update_remote(ref->new_sha1, ref_lock))
 
2015                         fprintf(stderr, "    done\n");
 
2017                         printf("%s %s\n", !rc ? "ok" : "error", ref->name);
 
2018                 unlock_remote(ref_lock);
 
2022         /* Update remote server info if appropriate */
 
2023         if (repo->has_info_refs && new_refs) {
 
2024                 if (info_ref_lock && repo->can_update_info_refs) {
 
2025                         fprintf(stderr, "Updating remote server info\n");
 
2027                                 update_remote_info_refs(info_ref_lock);
 
2029                         fprintf(stderr, "Unable to update server info\n");
 
2035                 unlock_remote(info_ref_lock);
 
2040         request = request_queue_head;
 
2041         while (request != NULL) {
 
2042                 next_request = request->next;
 
2043                 release_request(request);
 
2044                 request = next_request;