f24cfd2b46a65bde53610eecb6998fa615b62363
   1#include "cache.h"
   2#include "commit.h"
   3#include "pack.h"
   4#include "fetch.h"
   5#include "tag.h"
   6#include "blob.h"
   7#include "http.h"
   8#include "refs.h"
   9#include "revision.h"
  10
  11#include <expat.h>
  12
  13static const char http_push_usage[] =
  14"git-http-push [--complete] [--force] [--verbose] <url> <ref> [<ref>...]\n";
  15
  16#ifndef XML_STATUS_OK
  17enum XML_Status {
  18  XML_STATUS_OK = 1,
  19  XML_STATUS_ERROR = 0
  20};
  21#define XML_STATUS_OK    1
  22#define XML_STATUS_ERROR 0
  23#endif
  24
  25#define RANGE_HEADER_SIZE 30
  26
  27/* DAV methods */
  28#define DAV_LOCK "LOCK"
  29#define DAV_MKCOL "MKCOL"
  30#define DAV_MOVE "MOVE"
  31#define DAV_PROPFIND "PROPFIND"
  32#define DAV_PUT "PUT"
  33#define DAV_UNLOCK "UNLOCK"
  34
  35/* DAV lock flags */
  36#define DAV_PROP_LOCKWR (1u << 0)
  37#define DAV_PROP_LOCKEX (1u << 1)
  38#define DAV_LOCK_OK (1u << 2)
  39
  40/* DAV XML properties */
  41#define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
  42#define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
  43#define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
  44#define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
  45#define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
  46#define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
  47#define DAV_PROPFIND_RESP ".multistatus.response"
  48#define DAV_PROPFIND_NAME ".multistatus.response.href"
  49#define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
  50
  51/* DAV request body templates */
  52#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>"
  53#define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
  54#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>"
  55
  56#define LOCK_TIME 600
  57#define LOCK_REFRESH 30
  58
  59/* bits #0-4 in revision.h */
  60
  61#define LOCAL   (1u << 5)
  62#define REMOTE  (1u << 6)
  63#define PUSHING (1u << 7)
  64
  65static int pushing = 0;
  66static int aborted = 0;
  67static char remote_dir_exists[256];
  68
  69static struct curl_slist *no_pragma_header;
  70static struct curl_slist *default_headers;
  71
  72static int push_verbosely = 0;
  73static int push_all = 0;
  74static int force_all = 0;
  75
  76static struct object_list *objects = NULL;
  77
  78struct repo
  79{
  80        char *url;
  81        int path_len;
  82        struct packed_git *packs;
  83};
  84
  85static struct repo *remote = NULL;
  86static struct remote_lock *remote_locks = NULL;
  87
  88enum transfer_state {
  89        NEED_PUSH,
  90        RUN_MKCOL,
  91        RUN_PUT,
  92        RUN_MOVE,
  93        ABORTED,
  94        COMPLETE,
  95};
  96
  97struct transfer_request
  98{
  99        struct object *obj;
 100        char *url;
 101        char *dest;
 102        struct remote_lock *lock;
 103        struct curl_slist *headers;
 104        struct buffer buffer;
 105        char filename[PATH_MAX];
 106        char tmpfile[PATH_MAX];
 107        enum transfer_state state;
 108        CURLcode curl_result;
 109        char errorstr[CURL_ERROR_SIZE];
 110        long http_code;
 111        unsigned char real_sha1[20];
 112        SHA_CTX c;
 113        z_stream stream;
 114        int zret;
 115        int rename;
 116        struct active_request_slot *slot;
 117        struct transfer_request *next;
 118};
 119
 120static struct transfer_request *request_queue_head = NULL;
 121
 122struct xml_ctx
 123{
 124        char *name;
 125        int len;
 126        char *cdata;
 127        void (*userFunc)(struct xml_ctx *ctx, int tag_closed);
 128        void *userData;
 129};
 130
 131struct remote_lock
 132{
 133        char *url;
 134        char *owner;
 135        char *token;
 136        time_t start_time;
 137        long timeout;
 138        int active;
 139        int refreshing;
 140        struct remote_lock *next;
 141};
 142
 143/* Flags that control remote_ls processing */
 144#define PROCESS_FILES (1u << 0)
 145#define PROCESS_DIRS  (1u << 1)
 146#define RECURSIVE     (1u << 2)
 147
 148/* Flags that remote_ls passes to callback functions */
 149#define IS_DIR (1u << 0)
 150
 151struct remote_ls_ctx
 152{
 153        char *path;
 154        void (*userFunc)(struct remote_ls_ctx *ls);
 155        void *userData;
 156        int flags;
 157        char *dentry_name;
 158        int dentry_flags;
 159        struct remote_ls_ctx *parent;
 160};
 161
 162static void finish_request(struct transfer_request *request);
 163
 164static void process_response(void *callback_data)
 165{
 166        struct transfer_request *request =
 167                (struct transfer_request *)callback_data;
 168
 169        finish_request(request);
 170}
 171
 172static void start_mkcol(struct transfer_request *request)
 173{
 174        char *hex = sha1_to_hex(request->obj->sha1);
 175        struct active_request_slot *slot;
 176        char *posn;
 177
 178        request->url = xmalloc(strlen(remote->url) + 13);
 179        strcpy(request->url, remote->url);
 180        posn = request->url + strlen(remote->url);
 181        strcpy(posn, "objects/");
 182        posn += 8;
 183        memcpy(posn, hex, 2);
 184        posn += 2;
 185        strcpy(posn, "/");
 186
 187        slot = get_active_slot();
 188        slot->callback_func = process_response;
 189        slot->callback_data = request;
 190        curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
 191        curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
 192        curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
 193        curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
 194        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
 195
 196        if (start_active_slot(slot)) {
 197                request->slot = slot;
 198                request->state = RUN_MKCOL;
 199        } else {
 200                request->state = ABORTED;
 201                free(request->url);
 202                request->url = NULL;
 203        }
 204}
 205
 206static void start_put(struct transfer_request *request)
 207{
 208        char *hex = sha1_to_hex(request->obj->sha1);
 209        struct active_request_slot *slot;
 210        char *posn;
 211        char type[20];
 212        char hdr[50];
 213        void *unpacked;
 214        unsigned long len;
 215        int hdrlen;
 216        ssize_t size;
 217        z_stream stream;
 218
 219        unpacked = read_sha1_file(request->obj->sha1, type, &len);
 220        hdrlen = sprintf(hdr, "%s %lu", type, len) + 1;
 221
 222        /* Set it up */
 223        memset(&stream, 0, sizeof(stream));
 224        deflateInit(&stream, Z_BEST_COMPRESSION);
 225        size = deflateBound(&stream, len + hdrlen);
 226        request->buffer.buffer = xmalloc(size);
 227
 228        /* Compress it */
 229        stream.next_out = request->buffer.buffer;
 230        stream.avail_out = size;
 231
 232        /* First header.. */
 233        stream.next_in = (void *)hdr;
 234        stream.avail_in = hdrlen;
 235        while (deflate(&stream, 0) == Z_OK)
 236                /* nothing */;
 237
 238        /* Then the data itself.. */
 239        stream.next_in = unpacked;
 240        stream.avail_in = len;
 241        while (deflate(&stream, Z_FINISH) == Z_OK)
 242                /* nothing */;
 243        deflateEnd(&stream);
 244        free(unpacked);
 245
 246        request->buffer.size = stream.total_out;
 247        request->buffer.posn = 0;
 248
 249        request->url = xmalloc(strlen(remote->url) + 
 250                               strlen(request->lock->token) + 51);
 251        strcpy(request->url, remote->url);
 252        posn = request->url + strlen(remote->url);
 253        strcpy(posn, "objects/");
 254        posn += 8;
 255        memcpy(posn, hex, 2);
 256        posn += 2;
 257        *(posn++) = '/';
 258        strcpy(posn, hex + 2);
 259        request->dest = xmalloc(strlen(request->url) + 14);
 260        sprintf(request->dest, "Destination: %s", request->url);
 261        posn += 38;
 262        *(posn++) = '.';
 263        strcpy(posn, request->lock->token);
 264
 265        slot = get_active_slot();
 266        slot->callback_func = process_response;
 267        slot->callback_data = request;
 268        curl_easy_setopt(slot->curl, CURLOPT_INFILE, &request->buffer);
 269        curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, request->buffer.size);
 270        curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
 271        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
 272        curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
 273        curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
 274        curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
 275        curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
 276        curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
 277
 278        if (start_active_slot(slot)) {
 279                request->slot = slot;
 280                request->state = RUN_PUT;
 281        } else {
 282                request->state = ABORTED;
 283                free(request->url);
 284                request->url = NULL;
 285        }
 286}
 287
 288static void start_move(struct transfer_request *request)
 289{
 290        struct active_request_slot *slot;
 291        struct curl_slist *dav_headers = NULL;
 292
 293        slot = get_active_slot();
 294        slot->callback_func = process_response;
 295        slot->callback_data = request;
 296        curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
 297        curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MOVE);
 298        dav_headers = curl_slist_append(dav_headers, request->dest);
 299        dav_headers = curl_slist_append(dav_headers, "Overwrite: T");
 300        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
 301        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
 302        curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
 303
 304        if (start_active_slot(slot)) {
 305                request->slot = slot;
 306                request->state = RUN_MOVE;
 307        } else {
 308                request->state = ABORTED;
 309                free(request->url);
 310                request->url = NULL;
 311        }
 312}
 313
 314static int refresh_lock(struct remote_lock *check_lock)
 315{
 316        struct active_request_slot *slot;
 317        struct slot_results results;
 318        char *if_header;
 319        char timeout_header[25];
 320        struct curl_slist *dav_headers = NULL;
 321        struct remote_lock *lock;
 322        int time_remaining;
 323        time_t current_time;
 324
 325        /* Refresh all active locks if they're close to expiring */
 326        for (lock = remote_locks; lock; lock = lock->next) {
 327                if (!lock->active)
 328                        continue;
 329
 330                current_time = time(NULL);
 331                time_remaining = lock->start_time + lock->timeout
 332                        - current_time;
 333                if (time_remaining > LOCK_REFRESH)
 334                        continue;
 335
 336                lock->refreshing = 1;
 337
 338                if_header = xmalloc(strlen(lock->token) + 25);
 339                sprintf(if_header, "If: (<opaquelocktoken:%s>)", lock->token);
 340                sprintf(timeout_header, "Timeout: Second-%ld", lock->timeout);
 341                dav_headers = curl_slist_append(dav_headers, if_header);
 342                dav_headers = curl_slist_append(dav_headers, timeout_header);
 343
 344                slot = get_active_slot();
 345                slot->results = &results;
 346                curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
 347                curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
 348                curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
 349                curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
 350                curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
 351
 352                if (start_active_slot(slot)) {
 353                        run_active_slot(slot);
 354                        if (results.curl_result != CURLE_OK) {
 355                                fprintf(stderr, "Got HTTP error %ld\n", results.http_code);
 356                                lock->active = 0;
 357                        } else {
 358                                lock->active = 1;
 359                                lock->start_time = time(NULL);
 360                        }
 361                }
 362
 363                lock->refreshing = 0;
 364                curl_slist_free_all(dav_headers);
 365                free(if_header);
 366        }
 367
 368        if (check_lock)
 369                return check_lock->active;
 370        else
 371                return 0;
 372}
 373
 374static void release_request(struct transfer_request *request)
 375{
 376        struct transfer_request *entry = request_queue_head;
 377
 378        if (request == request_queue_head) {
 379                request_queue_head = request->next;
 380        } else {
 381                while (entry->next != NULL && entry->next != request)
 382                        entry = entry->next;
 383                if (entry->next == request)
 384                        entry->next = entry->next->next;
 385        }
 386
 387        if (request->url != NULL)
 388                free(request->url);
 389        free(request);
 390}
 391
 392static void finish_request(struct transfer_request *request)
 393{
 394        request->curl_result =  request->slot->curl_result;
 395        request->http_code = request->slot->http_code;
 396        request->slot = NULL;
 397
 398        /* Keep locks active */
 399        refresh_lock(request->lock);
 400
 401        if (request->headers != NULL)
 402                curl_slist_free_all(request->headers);
 403
 404        /* URL is reused for MOVE after PUT */
 405        if (request->state != RUN_PUT) {
 406                free(request->url);
 407                request->url = NULL;
 408        }
 409
 410        if (request->state == RUN_MKCOL) {
 411                if (request->curl_result == CURLE_OK ||
 412                    request->http_code == 405) {
 413                        remote_dir_exists[request->obj->sha1[0]] = 1;
 414                        start_put(request);
 415                } else {
 416                        fprintf(stderr, "MKCOL %s failed, aborting (%d/%ld)\n",
 417                                sha1_to_hex(request->obj->sha1),
 418                                request->curl_result, request->http_code);
 419                        request->state = ABORTED;
 420                        aborted = 1;
 421                }
 422        } else if (request->state == RUN_PUT) {
 423                if (request->curl_result == CURLE_OK) {
 424                        start_move(request);
 425                } else {
 426                        fprintf(stderr, "PUT %s failed, aborting (%d/%ld)\n",
 427                                sha1_to_hex(request->obj->sha1),
 428                                request->curl_result, request->http_code);
 429                        request->state = ABORTED;
 430                        aborted = 1;
 431                }
 432        } else if (request->state == RUN_MOVE) {
 433                if (request->curl_result == CURLE_OK) {
 434                        fprintf(stderr, "    sent %s\n",
 435                                sha1_to_hex(request->obj->sha1));
 436                        request->state = COMPLETE;
 437                        request->obj->flags |= REMOTE;
 438                        release_request(request);
 439                } else {
 440                        fprintf(stderr, "MOVE %s failed, aborting (%d/%ld)\n",
 441                                sha1_to_hex(request->obj->sha1),
 442                                request->curl_result, request->http_code);
 443                        request->state = ABORTED;
 444                        aborted = 1;
 445                }
 446        }
 447}
 448
 449void fill_active_slots(void)
 450{
 451        struct transfer_request *request = request_queue_head;
 452        struct active_request_slot *slot = active_queue_head;
 453        int num_transfers;
 454
 455        if (aborted)
 456                return;
 457
 458        while (active_requests < max_requests && request != NULL) {
 459                if (pushing && request->state == NEED_PUSH) {
 460                        if (remote_dir_exists[request->obj->sha1[0]] == 1) {
 461                                start_put(request);
 462                        } else {
 463                                start_mkcol(request);
 464                        }
 465                        curl_multi_perform(curlm, &num_transfers);
 466                }
 467                request = request->next;
 468        }
 469
 470        while (slot != NULL) {
 471                if (!slot->in_use && slot->curl != NULL) {
 472                        curl_easy_cleanup(slot->curl);
 473                        slot->curl = NULL;
 474                }
 475                slot = slot->next;
 476        }
 477}
 478
 479static void get_remote_object_list(unsigned char parent);
 480
 481static void add_request(struct object *obj, struct remote_lock *lock)
 482{
 483        struct transfer_request *request = request_queue_head;
 484        struct packed_git *target;
 485
 486        /*
 487         * Don't push the object if it's known to exist on the remote
 488         * or is already in the request queue
 489         */
 490        if (remote_dir_exists[obj->sha1[0]] == -1)
 491                get_remote_object_list(obj->sha1[0]);
 492        if (obj->flags & (REMOTE | PUSHING))
 493                return;
 494        target = find_sha1_pack(obj->sha1, remote->packs);
 495        if (target) {
 496                obj->flags |= REMOTE;
 497                return;
 498        }
 499
 500        obj->flags |= PUSHING;
 501        request = xmalloc(sizeof(*request));
 502        request->obj = obj;
 503        request->url = NULL;
 504        request->lock = lock;
 505        request->headers = NULL;
 506        request->state = NEED_PUSH;
 507        request->next = request_queue_head;
 508        request_queue_head = request;
 509
 510        fill_active_slots();
 511        step_active_slots();
 512}
 513
 514static int fetch_index(unsigned char *sha1)
 515{
 516        char *hex = sha1_to_hex(sha1);
 517        char *filename;
 518        char *url;
 519        char tmpfile[PATH_MAX];
 520        long prev_posn = 0;
 521        char range[RANGE_HEADER_SIZE];
 522        struct curl_slist *range_header = NULL;
 523
 524        FILE *indexfile;
 525        struct active_request_slot *slot;
 526        struct slot_results results;
 527
 528        /* Don't use the index if the pack isn't there */
 529        url = xmalloc(strlen(remote->url) + 65);
 530        sprintf(url, "%s/objects/pack/pack-%s.pack", remote->url, hex);
 531        slot = get_active_slot();
 532        slot->results = &results;
 533        curl_easy_setopt(slot->curl, CURLOPT_URL, url);
 534        curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
 535        if (start_active_slot(slot)) {
 536                run_active_slot(slot);
 537                if (results.curl_result != CURLE_OK) {
 538                        free(url);
 539                        return error("Unable to verify pack %s is available",
 540                                     hex);
 541                }
 542        } else {
 543                return error("Unable to start request");
 544        }
 545
 546        if (has_pack_index(sha1))
 547                return 0;
 548
 549        if (push_verbosely)
 550                fprintf(stderr, "Getting index for pack %s\n", hex);
 551        
 552        sprintf(url, "%s/objects/pack/pack-%s.idx", remote->url, hex);
 553        
 554        filename = sha1_pack_index_name(sha1);
 555        snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
 556        indexfile = fopen(tmpfile, "a");
 557        if (!indexfile)
 558                return error("Unable to open local file %s for pack index",
 559                             filename);
 560
 561        slot = get_active_slot();
 562        slot->results = &results;
 563        curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
 564        curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
 565        curl_easy_setopt(slot->curl, CURLOPT_FILE, indexfile);
 566        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
 567        curl_easy_setopt(slot->curl, CURLOPT_URL, url);
 568        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
 569        slot->local = indexfile;
 570
 571        /* If there is data present from a previous transfer attempt,
 572           resume where it left off */
 573        prev_posn = ftell(indexfile);
 574        if (prev_posn>0) {
 575                if (push_verbosely)
 576                        fprintf(stderr,
 577                                "Resuming fetch of index for pack %s at byte %ld\n",
 578                                hex, prev_posn);
 579                sprintf(range, "Range: bytes=%ld-", prev_posn);
 580                range_header = curl_slist_append(range_header, range);
 581                curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, range_header);
 582        }
 583
 584        if (start_active_slot(slot)) {
 585                run_active_slot(slot);
 586                if (results.curl_result != CURLE_OK) {
 587                        free(url);
 588                        fclose(indexfile);
 589                        return error("Unable to get pack index %s\n%s", url,
 590                                     curl_errorstr);
 591                }
 592        } else {
 593                free(url);
 594                fclose(indexfile);
 595                return error("Unable to start request");
 596        }
 597
 598        free(url);
 599        fclose(indexfile);
 600
 601        return move_temp_to_file(tmpfile, filename);
 602}
 603
 604static int setup_index(unsigned char *sha1)
 605{
 606        struct packed_git *new_pack;
 607
 608        if (fetch_index(sha1))
 609                return -1;
 610
 611        new_pack = parse_pack_index(sha1);
 612        new_pack->next = remote->packs;
 613        remote->packs = new_pack;
 614        return 0;
 615}
 616
 617static int fetch_indices(void)
 618{
 619        unsigned char sha1[20];
 620        char *url;
 621        struct buffer buffer;
 622        char *data;
 623        int i = 0;
 624
 625        struct active_request_slot *slot;
 626        struct slot_results results;
 627
 628        data = xmalloc(4096);
 629        memset(data, 0, 4096);
 630        buffer.size = 4096;
 631        buffer.posn = 0;
 632        buffer.buffer = data;
 633
 634        if (push_verbosely)
 635                fprintf(stderr, "Getting pack list\n");
 636        
 637        url = xmalloc(strlen(remote->url) + 21);
 638        sprintf(url, "%s/objects/info/packs", remote->url);
 639
 640        slot = get_active_slot();
 641        slot->results = &results;
 642        curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
 643        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
 644        curl_easy_setopt(slot->curl, CURLOPT_URL, url);
 645        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
 646        if (start_active_slot(slot)) {
 647                run_active_slot(slot);
 648                if (results.curl_result != CURLE_OK) {
 649                        free(buffer.buffer);
 650                        free(url);
 651                        if (results.http_code == 404)
 652                                return 0;
 653                        else
 654                                return error("%s", curl_errorstr);
 655                }
 656        } else {
 657                free(buffer.buffer);
 658                free(url);
 659                return error("Unable to start request");
 660        }
 661        free(url);
 662
 663        data = buffer.buffer;
 664        while (i < buffer.posn) {
 665                switch (data[i]) {
 666                case 'P':
 667                        i++;
 668                        if (i + 52 < buffer.posn &&
 669                            !strncmp(data + i, " pack-", 6) &&
 670                            !strncmp(data + i + 46, ".pack\n", 6)) {
 671                                get_sha1_hex(data + i + 6, sha1);
 672                                setup_index(sha1);
 673                                i += 51;
 674                                break;
 675                        }
 676                default:
 677                        while (data[i] != '\n')
 678                                i++;
 679                }
 680                i++;
 681        }
 682
 683        free(buffer.buffer);
 684        return 0;
 685}
 686
 687static inline int needs_quote(int ch)
 688{
 689        switch (ch) {
 690        case '/': case '-': case '.':
 691        case 'A'...'Z': case 'a'...'z': case '0'...'9':
 692                return 0;
 693        default:
 694                return 1;
 695        }
 696}
 697
 698static inline int hex(int v)
 699{
 700        if (v < 10) return '0' + v;
 701        else return 'A' + v - 10;
 702}
 703
 704static char *quote_ref_url(const char *base, const char *ref)
 705{
 706        const char *cp;
 707        char *dp, *qref;
 708        int len, baselen, ch;
 709
 710        baselen = strlen(base);
 711        len = baselen + 1;
 712        for (cp = ref; (ch = *cp) != 0; cp++, len++)
 713                if (needs_quote(ch))
 714                        len += 2; /* extra two hex plus replacement % */
 715        qref = xmalloc(len);
 716        memcpy(qref, base, baselen);
 717        for (cp = ref, dp = qref + baselen; (ch = *cp) != 0; cp++) {
 718                if (needs_quote(ch)) {
 719                        *dp++ = '%';
 720                        *dp++ = hex((ch >> 4) & 0xF);
 721                        *dp++ = hex(ch & 0xF);
 722                }
 723                else
 724                        *dp++ = ch;
 725        }
 726        *dp = 0;
 727
 728        return qref;
 729}
 730
 731int fetch_ref(char *ref, unsigned char *sha1)
 732{
 733        char *url;
 734        char hex[42];
 735        struct buffer buffer;
 736        char *base = remote->url;
 737        struct active_request_slot *slot;
 738        struct slot_results results;
 739        buffer.size = 41;
 740        buffer.posn = 0;
 741        buffer.buffer = hex;
 742        hex[41] = '\0';
 743
 744        url = quote_ref_url(base, ref);
 745        slot = get_active_slot();
 746        slot->results = &results;
 747        curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
 748        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
 749        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
 750        curl_easy_setopt(slot->curl, CURLOPT_URL, url);
 751        if (start_active_slot(slot)) {
 752                run_active_slot(slot);
 753                if (results.curl_result != CURLE_OK)
 754                        return error("Couldn't get %s for %s\n%s",
 755                                     url, ref, curl_errorstr);
 756        } else {
 757                return error("Unable to start request");
 758        }
 759
 760        hex[40] = '\0';
 761        get_sha1_hex(hex, sha1);
 762        return 0;
 763}
 764
 765static void one_remote_object(const char *hex)
 766{
 767        unsigned char sha1[20];
 768        struct object *obj;
 769
 770        if (get_sha1_hex(hex, sha1) != 0)
 771                return;
 772
 773        obj = lookup_object(sha1);
 774        if (!obj)
 775                obj = parse_object(sha1);
 776
 777        /* Ignore remote objects that don't exist locally */
 778        if (!obj)
 779                return;
 780
 781        obj->flags |= REMOTE;
 782        if (!object_list_contains(objects, obj))
 783                add_object(obj, &objects, NULL, "");
 784}
 785
 786static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed)
 787{
 788        int *lock_flags = (int *)ctx->userData;
 789
 790        if (tag_closed) {
 791                if (!strcmp(ctx->name, DAV_CTX_LOCKENTRY)) {
 792                        if ((*lock_flags & DAV_PROP_LOCKEX) &&
 793                            (*lock_flags & DAV_PROP_LOCKWR)) {
 794                                *lock_flags |= DAV_LOCK_OK;
 795                        }
 796                        *lock_flags &= DAV_LOCK_OK;
 797                } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_WRITE)) {
 798                        *lock_flags |= DAV_PROP_LOCKWR;
 799                } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_EXCLUSIVE)) {
 800                        *lock_flags |= DAV_PROP_LOCKEX;
 801                }
 802        }
 803}
 804
 805static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
 806{
 807        struct remote_lock *lock = (struct remote_lock *)ctx->userData;
 808
 809        if (tag_closed && ctx->cdata) {
 810                if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) {
 811                        lock->owner = xmalloc(strlen(ctx->cdata) + 1);
 812                        strcpy(lock->owner, ctx->cdata);
 813                } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
 814                        if (!strncmp(ctx->cdata, "Second-", 7))
 815                                lock->timeout =
 816                                        strtol(ctx->cdata + 7, NULL, 10);
 817                } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
 818                        if (!strncmp(ctx->cdata, "opaquelocktoken:", 16)) {
 819                                lock->token = xmalloc(strlen(ctx->cdata) - 15);
 820                                strcpy(lock->token, ctx->cdata + 16);
 821                        }
 822                }
 823        }
 824}
 825
 826static void one_remote_ref(char *refname);
 827
 828static void
 829xml_start_tag(void *userData, const char *name, const char **atts)
 830{
 831        struct xml_ctx *ctx = (struct xml_ctx *)userData;
 832        const char *c = index(name, ':');
 833        int new_len;
 834
 835        if (c == NULL)
 836                c = name;
 837        else
 838                c++;
 839
 840        new_len = strlen(ctx->name) + strlen(c) + 2;
 841
 842        if (new_len > ctx->len) {
 843                ctx->name = xrealloc(ctx->name, new_len);
 844                ctx->len = new_len;
 845        }
 846        strcat(ctx->name, ".");
 847        strcat(ctx->name, c);
 848
 849        if (ctx->cdata) {
 850                free(ctx->cdata);
 851                ctx->cdata = NULL;
 852        }
 853
 854        ctx->userFunc(ctx, 0);
 855}
 856
 857static void
 858xml_end_tag(void *userData, const char *name)
 859{
 860        struct xml_ctx *ctx = (struct xml_ctx *)userData;
 861        const char *c = index(name, ':');
 862        char *ep;
 863
 864        ctx->userFunc(ctx, 1);
 865
 866        if (c == NULL)
 867                c = name;
 868        else
 869                c++;
 870
 871        ep = ctx->name + strlen(ctx->name) - strlen(c) - 1;
 872        *ep = 0;
 873}
 874
 875static void
 876xml_cdata(void *userData, const XML_Char *s, int len)
 877{
 878        struct xml_ctx *ctx = (struct xml_ctx *)userData;
 879        if (ctx->cdata)
 880                free(ctx->cdata);
 881        ctx->cdata = xcalloc(len+1, 1);
 882        strncpy(ctx->cdata, s, len);
 883}
 884
 885static struct remote_lock *lock_remote(char *path, long timeout)
 886{
 887        struct active_request_slot *slot;
 888        struct slot_results results;
 889        struct buffer out_buffer;
 890        struct buffer in_buffer;
 891        char *out_data;
 892        char *in_data;
 893        char *url;
 894        char *ep;
 895        char timeout_header[25];
 896        struct remote_lock *lock = remote_locks;
 897        XML_Parser parser = XML_ParserCreate(NULL);
 898        enum XML_Status result;
 899        struct curl_slist *dav_headers = NULL;
 900        struct xml_ctx ctx;
 901
 902        url = xmalloc(strlen(remote->url) + strlen(path) + 1);
 903        sprintf(url, "%s%s", remote->url, path);
 904
 905        /* Make sure the url is not already locked */
 906        while (lock && strcmp(lock->url, url)) {
 907                lock = lock->next;
 908        }
 909        if (lock) {
 910                free(url);
 911                if (refresh_lock(lock))
 912                        return lock;
 913                else
 914                        return NULL;
 915        }
 916
 917        /* Make sure leading directories exist for the remote ref */
 918        ep = strchr(url + strlen(remote->url) + 11, '/');
 919        while (ep) {
 920                *ep = 0;
 921                slot = get_active_slot();
 922                slot->results = &results;
 923                curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
 924                curl_easy_setopt(slot->curl, CURLOPT_URL, url);
 925                curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
 926                curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
 927                if (start_active_slot(slot)) {
 928                        run_active_slot(slot);
 929                        if (results.curl_result != CURLE_OK &&
 930                            results.http_code != 405) {
 931                                fprintf(stderr,
 932                                        "Unable to create branch path %s\n",
 933                                        url);
 934                                free(url);
 935                                return NULL;
 936                        }
 937                } else {
 938                        fprintf(stderr, "Unable to start request\n");
 939                        free(url);
 940                        return NULL;
 941                }
 942                *ep = '/';
 943                ep = strchr(ep + 1, '/');
 944        }
 945
 946        out_buffer.size = strlen(LOCK_REQUEST) + strlen(git_default_email) - 2;
 947        out_data = xmalloc(out_buffer.size + 1);
 948        snprintf(out_data, out_buffer.size + 1, LOCK_REQUEST, git_default_email);
 949        out_buffer.posn = 0;
 950        out_buffer.buffer = out_data;
 951
 952        in_buffer.size = 4096;
 953        in_data = xmalloc(in_buffer.size);
 954        in_buffer.posn = 0;
 955        in_buffer.buffer = in_data;
 956
 957        sprintf(timeout_header, "Timeout: Second-%ld", timeout);
 958        dav_headers = curl_slist_append(dav_headers, timeout_header);
 959        dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
 960
 961        slot = get_active_slot();
 962        slot->results = &results;
 963        curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
 964        curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size);
 965        curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
 966        curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
 967        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
 968        curl_easy_setopt(slot->curl, CURLOPT_URL, url);
 969        curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
 970        curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
 971        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
 972
 973        lock = xcalloc(1, sizeof(*lock));
 974        lock->owner = NULL;
 975        lock->token = NULL;
 976        lock->timeout = -1;
 977        lock->refreshing = 0;
 978
 979        if (start_active_slot(slot)) {
 980                run_active_slot(slot);
 981                if (results.curl_result == CURLE_OK) {
 982                        ctx.name = xcalloc(10, 1);
 983                        ctx.len = 0;
 984                        ctx.cdata = NULL;
 985                        ctx.userFunc = handle_new_lock_ctx;
 986                        ctx.userData = lock;
 987                        XML_SetUserData(parser, &ctx);
 988                        XML_SetElementHandler(parser, xml_start_tag,
 989                                              xml_end_tag);
 990                        XML_SetCharacterDataHandler(parser, xml_cdata);
 991                        result = XML_Parse(parser, in_buffer.buffer,
 992                                           in_buffer.posn, 1);
 993                        free(ctx.name);
 994                        if (result != XML_STATUS_OK) {
 995                                fprintf(stderr, "XML error: %s\n",
 996                                        XML_ErrorString(
 997                                                XML_GetErrorCode(parser)));
 998                                lock->timeout = -1;
 999                        }
1000                }
1001        } else {
1002                fprintf(stderr, "Unable to start request\n");
1003        }
1004
1005        curl_slist_free_all(dav_headers);
1006        free(out_data);
1007        free(in_data);
1008
1009        if (lock->token == NULL || lock->timeout <= 0) {
1010                if (lock->token != NULL)
1011                        free(lock->token);
1012                if (lock->owner != NULL)
1013                        free(lock->owner);
1014                free(url);
1015                free(lock);
1016                lock = NULL;
1017        } else {
1018                lock->url = url;
1019                lock->active = 1;
1020                lock->start_time = time(NULL);
1021                lock->next = remote_locks;
1022                remote_locks = lock;
1023        }
1024
1025        return lock;
1026}
1027
1028static int unlock_remote(struct remote_lock *lock)
1029{
1030        struct active_request_slot *slot;
1031        struct slot_results results;
1032        char *lock_token_header;
1033        struct curl_slist *dav_headers = NULL;
1034        int rc = 0;
1035
1036        lock_token_header = xmalloc(strlen(lock->token) + 31);
1037        sprintf(lock_token_header, "Lock-Token: <opaquelocktoken:%s>",
1038                lock->token);
1039        dav_headers = curl_slist_append(dav_headers, lock_token_header);
1040
1041        slot = get_active_slot();
1042        slot->results = &results;
1043        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1044        curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1045        curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_UNLOCK);
1046        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1047
1048        if (start_active_slot(slot)) {
1049                run_active_slot(slot);
1050                if (results.curl_result == CURLE_OK)
1051                        rc = 1;
1052                else
1053                        fprintf(stderr, "Got HTTP error %ld\n",
1054                                results.http_code);
1055        } else {
1056                fprintf(stderr, "Unable to start request\n");
1057        }
1058
1059        curl_slist_free_all(dav_headers);
1060        free(lock_token_header);
1061
1062        lock->active = 0;
1063
1064        return rc;
1065}
1066
1067static void remote_ls(const char *path, int flags,
1068                      void (*userFunc)(struct remote_ls_ctx *ls),
1069                      void *userData);
1070
1071static void process_ls_object(struct remote_ls_ctx *ls)
1072{
1073        unsigned int *parent = (unsigned int *)ls->userData;
1074        char *path = ls->dentry_name;
1075        char *obj_hex;
1076
1077        if (!strcmp(ls->path, ls->dentry_name) && (ls->flags & IS_DIR)) {
1078                remote_dir_exists[*parent] = 1;
1079                return;
1080        }
1081
1082        if (strlen(path) != 49)
1083                return;
1084        path += 8;
1085        obj_hex = xmalloc(strlen(path));
1086        strncpy(obj_hex, path, 2);
1087        strcpy(obj_hex + 2, path + 3);
1088        one_remote_object(obj_hex);
1089        free(obj_hex);
1090}
1091
1092static void process_ls_ref(struct remote_ls_ctx *ls)
1093{
1094        if (!strcmp(ls->path, ls->dentry_name) && (ls->dentry_flags & IS_DIR)) {
1095                fprintf(stderr, "  %s\n", ls->dentry_name);
1096                return;
1097        }
1098
1099        if (!(ls->dentry_flags & IS_DIR))
1100                one_remote_ref(ls->dentry_name);
1101}
1102
1103static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
1104{
1105        struct remote_ls_ctx *ls = (struct remote_ls_ctx *)ctx->userData;
1106
1107        if (tag_closed) {
1108                if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) {
1109                        if (ls->dentry_flags & IS_DIR) {
1110                                if (ls->flags & PROCESS_DIRS) {
1111                                        ls->userFunc(ls);
1112                                }
1113                                if (strcmp(ls->dentry_name, ls->path) &&
1114                                    ls->flags & RECURSIVE) {
1115                                        remote_ls(ls->dentry_name,
1116                                                  ls->flags,
1117                                                  ls->userFunc,
1118                                                  ls->userData);
1119                                }
1120                        } else if (ls->flags & PROCESS_FILES) {
1121                                ls->userFunc(ls);
1122                        }
1123                } else if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
1124                        ls->dentry_name = xmalloc(strlen(ctx->cdata) -
1125                                                  remote->path_len + 1);
1126                        strcpy(ls->dentry_name, ctx->cdata + remote->path_len);
1127                } else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
1128                        ls->dentry_flags |= IS_DIR;
1129                }
1130        } else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
1131                if (ls->dentry_name) {
1132                        free(ls->dentry_name);
1133                }
1134                ls->dentry_name = NULL;
1135                ls->dentry_flags = 0;
1136        }
1137}
1138
1139static void remote_ls(const char *path, int flags,
1140                      void (*userFunc)(struct remote_ls_ctx *ls),
1141                      void *userData)
1142{
1143        char *url = xmalloc(strlen(remote->url) + strlen(path) + 1);
1144        struct active_request_slot *slot;
1145        struct slot_results results;
1146        struct buffer in_buffer;
1147        struct buffer out_buffer;
1148        char *in_data;
1149        char *out_data;
1150        XML_Parser parser = XML_ParserCreate(NULL);
1151        enum XML_Status result;
1152        struct curl_slist *dav_headers = NULL;
1153        struct xml_ctx ctx;
1154        struct remote_ls_ctx ls;
1155
1156        ls.flags = flags;
1157        ls.path = strdup(path);
1158        ls.dentry_name = NULL;
1159        ls.dentry_flags = 0;
1160        ls.userData = userData;
1161        ls.userFunc = userFunc;
1162
1163        sprintf(url, "%s%s", remote->url, path);
1164
1165        out_buffer.size = strlen(PROPFIND_ALL_REQUEST);
1166        out_data = xmalloc(out_buffer.size + 1);
1167        snprintf(out_data, out_buffer.size + 1, PROPFIND_ALL_REQUEST);
1168        out_buffer.posn = 0;
1169        out_buffer.buffer = out_data;
1170
1171        in_buffer.size = 4096;
1172        in_data = xmalloc(in_buffer.size);
1173        in_buffer.posn = 0;
1174        in_buffer.buffer = in_data;
1175
1176        dav_headers = curl_slist_append(dav_headers, "Depth: 1");
1177        dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1178
1179        slot = get_active_slot();
1180        slot->results = &results;
1181        curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1182        curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size);
1183        curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1184        curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1185        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1186        curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1187        curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1188        curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1189        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1190
1191        if (start_active_slot(slot)) {
1192                run_active_slot(slot);
1193                if (results.curl_result == CURLE_OK) {
1194                        ctx.name = xcalloc(10, 1);
1195                        ctx.len = 0;
1196                        ctx.cdata = NULL;
1197                        ctx.userFunc = handle_remote_ls_ctx;
1198                        ctx.userData = &ls;
1199                        XML_SetUserData(parser, &ctx);
1200                        XML_SetElementHandler(parser, xml_start_tag,
1201                                              xml_end_tag);
1202                        XML_SetCharacterDataHandler(parser, xml_cdata);
1203                        result = XML_Parse(parser, in_buffer.buffer,
1204                                           in_buffer.posn, 1);
1205                        free(ctx.name);
1206
1207                        if (result != XML_STATUS_OK) {
1208                                fprintf(stderr, "XML error: %s\n",
1209                                        XML_ErrorString(
1210                                                XML_GetErrorCode(parser)));
1211                        }
1212                }
1213        } else {
1214                fprintf(stderr, "Unable to start PROPFIND request\n");
1215        }
1216
1217        free(ls.path);
1218        free(url);
1219        free(out_data);
1220        free(in_buffer.buffer);
1221        curl_slist_free_all(dav_headers);
1222}
1223
1224static void get_remote_object_list(unsigned char parent)
1225{
1226        char path[] = "objects/XX/";
1227        static const char hex[] = "0123456789abcdef";
1228        unsigned int val = parent;
1229
1230        path[8] = hex[val >> 4];
1231        path[9] = hex[val & 0xf];
1232        remote_dir_exists[val] = 0;
1233        remote_ls(path, (PROCESS_FILES | PROCESS_DIRS),
1234                  process_ls_object, &val);
1235}
1236
1237static int locking_available(void)
1238{
1239        struct active_request_slot *slot;
1240        struct slot_results results;
1241        struct buffer in_buffer;
1242        struct buffer out_buffer;
1243        char *in_data;
1244        char *out_data;
1245        XML_Parser parser = XML_ParserCreate(NULL);
1246        enum XML_Status result;
1247        struct curl_slist *dav_headers = NULL;
1248        struct xml_ctx ctx;
1249        int lock_flags = 0;
1250
1251        out_buffer.size =
1252                strlen(PROPFIND_SUPPORTEDLOCK_REQUEST) +
1253                strlen(remote->url) - 2;
1254        out_data = xmalloc(out_buffer.size + 1);
1255        snprintf(out_data, out_buffer.size + 1,
1256                 PROPFIND_SUPPORTEDLOCK_REQUEST, remote->url);
1257        out_buffer.posn = 0;
1258        out_buffer.buffer = out_data;
1259
1260        in_buffer.size = 4096;
1261        in_data = xmalloc(in_buffer.size);
1262        in_buffer.posn = 0;
1263        in_buffer.buffer = in_data;
1264
1265        dav_headers = curl_slist_append(dav_headers, "Depth: 0");
1266        dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1267
1268        slot = get_active_slot();
1269        slot->results = &results;
1270        curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1271        curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size);
1272        curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1273        curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1274        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
1275        curl_easy_setopt(slot->curl, CURLOPT_URL, remote->url);
1276        curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1277        curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND);
1278        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1279
1280        if (start_active_slot(slot)) {
1281                run_active_slot(slot);
1282                if (results.curl_result == CURLE_OK) {
1283                        ctx.name = xcalloc(10, 1);
1284                        ctx.len = 0;
1285                        ctx.cdata = NULL;
1286                        ctx.userFunc = handle_lockprop_ctx;
1287                        ctx.userData = &lock_flags;
1288                        XML_SetUserData(parser, &ctx);
1289                        XML_SetElementHandler(parser, xml_start_tag,
1290                                              xml_end_tag);
1291                        result = XML_Parse(parser, in_buffer.buffer,
1292                                           in_buffer.posn, 1);
1293                        free(ctx.name);
1294
1295                        if (result != XML_STATUS_OK) {
1296                                fprintf(stderr, "XML error: %s\n",
1297                                        XML_ErrorString(
1298                                                XML_GetErrorCode(parser)));
1299                                lock_flags = 0;
1300                        }
1301                }
1302        } else {
1303                fprintf(stderr, "Unable to start request\n");
1304        }
1305
1306        free(out_data);
1307        free(in_buffer.buffer);
1308        curl_slist_free_all(dav_headers);
1309
1310        return lock_flags;
1311}
1312
1313static struct object_list **process_blob(struct blob *blob,
1314                                         struct object_list **p,
1315                                         struct name_path *path,
1316                                         const char *name)
1317{
1318        struct object *obj = &blob->object;
1319
1320        obj->flags |= LOCAL;
1321
1322        if (obj->flags & (UNINTERESTING | SEEN))
1323                return p;
1324
1325        obj->flags |= SEEN;
1326        return add_object(obj, p, path, name);
1327}
1328
1329static struct object_list **process_tree(struct tree *tree,
1330                                         struct object_list **p,
1331                                         struct name_path *path,
1332                                         const char *name)
1333{
1334        struct object *obj = &tree->object;
1335        struct tree_entry_list *entry;
1336        struct name_path me;
1337
1338        obj->flags |= LOCAL;
1339
1340        if (obj->flags & (UNINTERESTING | SEEN))
1341                return p;
1342        if (parse_tree(tree) < 0)
1343                die("bad tree object %s", sha1_to_hex(obj->sha1));
1344
1345        obj->flags |= SEEN;
1346        p = add_object(obj, p, NULL, name);
1347        me.up = path;
1348        me.elem = name;
1349        me.elem_len = strlen(name);
1350        entry = tree->entries;
1351        tree->entries = NULL;
1352        while (entry) {
1353                struct tree_entry_list *next = entry->next;
1354                if (entry->directory)
1355                        p = process_tree(entry->item.tree, p, &me, entry->name);
1356                else
1357                        p = process_blob(entry->item.blob, p, &me, entry->name);
1358                free(entry);
1359                entry = next;
1360        }
1361        return p;
1362}
1363
1364static void get_delta(struct rev_info *revs, struct remote_lock *lock)
1365{
1366        struct commit *commit;
1367        struct object_list **p = &objects, *pending;
1368
1369        while ((commit = get_revision(revs)) != NULL) {
1370                p = process_tree(commit->tree, p, NULL, "");
1371                commit->object.flags |= LOCAL;
1372                if (!(commit->object.flags & UNINTERESTING))
1373                        add_request(&commit->object, lock);
1374        }
1375
1376        for (pending = revs->pending_objects; pending; pending = pending->next) {
1377                struct object *obj = pending->item;
1378                const char *name = pending->name;
1379
1380                if (obj->flags & (UNINTERESTING | SEEN))
1381                        continue;
1382                if (obj->type == tag_type) {
1383                        obj->flags |= SEEN;
1384                        p = add_object(obj, p, NULL, name);
1385                        continue;
1386                }
1387                if (obj->type == tree_type) {
1388                        p = process_tree((struct tree *)obj, p, NULL, name);
1389                        continue;
1390                }
1391                if (obj->type == blob_type) {
1392                        p = process_blob((struct blob *)obj, p, NULL, name);
1393                        continue;
1394                }
1395                die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
1396        }
1397
1398        while (objects) {
1399                if (!(objects->item->flags & UNINTERESTING))
1400                        add_request(objects->item, lock);
1401                objects = objects->next;
1402        }
1403}
1404
1405static int update_remote(unsigned char *sha1, struct remote_lock *lock)
1406{
1407        struct active_request_slot *slot;
1408        struct slot_results results;
1409        char *out_data;
1410        char *if_header;
1411        struct buffer out_buffer;
1412        struct curl_slist *dav_headers = NULL;
1413        int i;
1414
1415        if_header = xmalloc(strlen(lock->token) + 25);
1416        sprintf(if_header, "If: (<opaquelocktoken:%s>)", lock->token);
1417        dav_headers = curl_slist_append(dav_headers, if_header);
1418
1419        out_buffer.size = 41;
1420        out_data = xmalloc(out_buffer.size + 1);
1421        i = snprintf(out_data, out_buffer.size + 1, "%s\n", sha1_to_hex(sha1));
1422        if (i != out_buffer.size) {
1423                fprintf(stderr, "Unable to initialize PUT request body\n");
1424                return 0;
1425        }
1426        out_buffer.posn = 0;
1427        out_buffer.buffer = out_data;
1428
1429        slot = get_active_slot();
1430        slot->results = &results;
1431        curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer);
1432        curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.size);
1433        curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer);
1434        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1435        curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT);
1436        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1437        curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1438        curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1439        curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1440
1441        if (start_active_slot(slot)) {
1442                run_active_slot(slot);
1443                free(out_data);
1444                free(if_header);
1445                if (results.curl_result != CURLE_OK) {
1446                        fprintf(stderr,
1447                                "PUT error: curl result=%d, HTTP code=%ld\n",
1448                                results.curl_result, results.http_code);
1449                        /* We should attempt recovery? */
1450                        return 0;
1451                }
1452        } else {
1453                free(out_data);
1454                free(if_header);
1455                fprintf(stderr, "Unable to start PUT request\n");
1456                return 0;
1457        }
1458
1459        return 1;
1460}
1461
1462static struct ref *local_refs, **local_tail;
1463static struct ref *remote_refs, **remote_tail;
1464
1465static int one_local_ref(const char *refname, const unsigned char *sha1)
1466{
1467        struct ref *ref;
1468        int len = strlen(refname) + 1;
1469        ref = xcalloc(1, sizeof(*ref) + len);
1470        memcpy(ref->new_sha1, sha1, 20);
1471        memcpy(ref->name, refname, len);
1472        *local_tail = ref;
1473        local_tail = &ref->next;
1474        return 0;
1475}
1476
1477static void one_remote_ref(char *refname)
1478{
1479        struct ref *ref;
1480        unsigned char remote_sha1[20];
1481
1482        if (fetch_ref(refname, remote_sha1) != 0) {
1483                fprintf(stderr,
1484                        "Unable to fetch ref %s from %s\n",
1485                        refname, remote->url);
1486                return;
1487        }
1488
1489        int len = strlen(refname) + 1;
1490        ref = xcalloc(1, sizeof(*ref) + len);
1491        memcpy(ref->old_sha1, remote_sha1, 20);
1492        memcpy(ref->name, refname, len);
1493        *remote_tail = ref;
1494        remote_tail = &ref->next;
1495}
1496
1497static void get_local_heads(void)
1498{
1499        local_tail = &local_refs;
1500        for_each_ref(one_local_ref);
1501}
1502
1503static void get_dav_remote_heads(void)
1504{
1505        remote_tail = &remote_refs;
1506        remote_ls("refs/", (PROCESS_FILES | PROCESS_DIRS | RECURSIVE), process_ls_ref, NULL);
1507}
1508
1509static int is_zero_sha1(const unsigned char *sha1)
1510{
1511        int i;
1512
1513        for (i = 0; i < 20; i++) {
1514                if (*sha1++)
1515                        return 0;
1516        }
1517        return 1;
1518}
1519
1520static void unmark_and_free(struct commit_list *list, unsigned int mark)
1521{
1522        while (list) {
1523                struct commit_list *temp = list;
1524                temp->item->object.flags &= ~mark;
1525                list = temp->next;
1526                free(temp);
1527        }
1528}
1529
1530static int ref_newer(const unsigned char *new_sha1,
1531                     const unsigned char *old_sha1)
1532{
1533        struct object *o;
1534        struct commit *old, *new;
1535        struct commit_list *list, *used;
1536        int found = 0;
1537
1538        /* Both new and old must be commit-ish and new is descendant of
1539         * old.  Otherwise we require --force.
1540         */
1541        o = deref_tag(parse_object(old_sha1), NULL, 0);
1542        if (!o || o->type != commit_type)
1543                return 0;
1544        old = (struct commit *) o;
1545
1546        o = deref_tag(parse_object(new_sha1), NULL, 0);
1547        if (!o || o->type != commit_type)
1548                return 0;
1549        new = (struct commit *) o;
1550
1551        if (parse_commit(new) < 0)
1552                return 0;
1553
1554        used = list = NULL;
1555        commit_list_insert(new, &list);
1556        while (list) {
1557                new = pop_most_recent_commit(&list, TMP_MARK);
1558                commit_list_insert(new, &used);
1559                if (new == old) {
1560                        found = 1;
1561                        break;
1562                }
1563        }
1564        unmark_and_free(list, TMP_MARK);
1565        unmark_and_free(used, TMP_MARK);
1566        return found;
1567}
1568
1569static void mark_edge_parents_uninteresting(struct commit *commit)
1570{
1571        struct commit_list *parents;
1572
1573        for (parents = commit->parents; parents; parents = parents->next) {
1574                struct commit *parent = parents->item;
1575                if (!(parent->object.flags & UNINTERESTING))
1576                        continue;
1577                mark_tree_uninteresting(parent->tree);
1578        }
1579}
1580
1581static void mark_edges_uninteresting(struct commit_list *list)
1582{
1583        for ( ; list; list = list->next) {
1584                struct commit *commit = list->item;
1585
1586                if (commit->object.flags & UNINTERESTING) {
1587                        mark_tree_uninteresting(commit->tree);
1588                        continue;
1589                }
1590                mark_edge_parents_uninteresting(commit);
1591        }
1592}
1593
1594int main(int argc, char **argv)
1595{
1596        struct transfer_request *request;
1597        struct transfer_request *next_request;
1598        int nr_refspec = 0;
1599        char **refspec = NULL;
1600        struct remote_lock *ref_lock;
1601        struct rev_info revs;
1602        int rc = 0;
1603        int i;
1604
1605        setup_git_directory();
1606        setup_ident();
1607
1608        remote = xmalloc(sizeof(*remote));
1609        remote->url = NULL;
1610        remote->path_len = 0;
1611        remote->packs = NULL;
1612
1613        argv++;
1614        for (i = 1; i < argc; i++, argv++) {
1615                char *arg = *argv;
1616
1617                if (*arg == '-') {
1618                        if (!strcmp(arg, "--all")) {
1619                                push_all = 1;
1620                                continue;
1621                        }
1622                        if (!strcmp(arg, "--force")) {
1623                                force_all = 1;
1624                                continue;
1625                        }
1626                        if (!strcmp(arg, "--verbose")) {
1627                                push_verbosely = 1;
1628                                continue;
1629                        }
1630                        usage(http_push_usage);
1631                }
1632                if (!remote->url) {
1633                        remote->url = arg;
1634                        char *path = strstr(arg, "//");
1635                        if (path) {
1636                                path = index(path+2, '/');
1637                                if (path)
1638                                        remote->path_len = strlen(path);
1639                        }
1640                        continue;
1641                }
1642                refspec = argv;
1643                nr_refspec = argc - i;
1644                break;
1645        }
1646
1647        if (!remote->url)
1648                usage(http_push_usage);
1649
1650        memset(remote_dir_exists, -1, 256);
1651
1652        http_init();
1653
1654        no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
1655        default_headers = curl_slist_append(default_headers, "Range:");
1656        default_headers = curl_slist_append(default_headers, "Destination:");
1657        default_headers = curl_slist_append(default_headers, "If:");
1658        default_headers = curl_slist_append(default_headers,
1659                                            "Pragma: no-cache");
1660
1661        /* Verify DAV compliance/lock support */
1662        if (!locking_available()) {
1663                fprintf(stderr, "Error: no DAV locking support on remote repo %s\n", remote->url);
1664                rc = 1;
1665                goto cleanup;
1666        }
1667
1668        /* Get a list of all local and remote heads to validate refspecs */
1669        get_local_heads();
1670        fprintf(stderr, "Fetching remote heads...\n");
1671        get_dav_remote_heads();
1672
1673        /* match them up */
1674        if (!remote_tail)
1675                remote_tail = &remote_refs;
1676        if (match_refs(local_refs, remote_refs, &remote_tail,
1677                       nr_refspec, refspec, push_all))
1678                return -1;
1679        if (!remote_refs) {
1680                fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
1681                return 0;
1682        }
1683
1684        int ret = 0;
1685        int new_refs = 0;
1686        struct ref *ref;
1687        for (ref = remote_refs; ref; ref = ref->next) {
1688                char old_hex[60], *new_hex;
1689                if (!ref->peer_ref)
1690                        continue;
1691                if (!memcmp(ref->old_sha1, ref->peer_ref->new_sha1, 20)) {
1692                        if (push_verbosely || 1)
1693                                fprintf(stderr, "'%s': up-to-date\n", ref->name);
1694                        continue;
1695                }
1696
1697                if (!force_all &&
1698                    !is_zero_sha1(ref->old_sha1) &&
1699                    !ref->force) {
1700                        if (!has_sha1_file(ref->old_sha1) ||
1701                            !ref_newer(ref->peer_ref->new_sha1,
1702                                       ref->old_sha1)) {
1703                                /* We do not have the remote ref, or
1704                                 * we know that the remote ref is not
1705                                 * an ancestor of what we are trying to
1706                                 * push.  Either way this can be losing
1707                                 * commits at the remote end and likely
1708                                 * we were not up to date to begin with.
1709                                 */
1710                                error("remote '%s' is not a strict "
1711                                      "subset of local ref '%s'. "
1712                                      "maybe you are not up-to-date and "
1713                                      "need to pull first?",
1714                                      ref->name,
1715                                      ref->peer_ref->name);
1716                                ret = -2;
1717                                continue;
1718                        }
1719                }
1720                memcpy(ref->new_sha1, ref->peer_ref->new_sha1, 20);
1721                if (is_zero_sha1(ref->new_sha1)) {
1722                        error("cannot happen anymore");
1723                        ret = -3;
1724                        continue;
1725                }
1726                new_refs++;
1727                strcpy(old_hex, sha1_to_hex(ref->old_sha1));
1728                new_hex = sha1_to_hex(ref->new_sha1);
1729
1730                fprintf(stderr, "updating '%s'", ref->name);
1731                if (strcmp(ref->name, ref->peer_ref->name))
1732                        fprintf(stderr, " using '%s'", ref->peer_ref->name);
1733                fprintf(stderr, "\n  from %s\n  to   %s\n", old_hex, new_hex);
1734
1735
1736                /* Lock remote branch ref */
1737                ref_lock = lock_remote(ref->name, LOCK_TIME);
1738                if (ref_lock == NULL) {
1739                        fprintf(stderr, "Unable to lock remote branch %s\n",
1740                                ref->name);
1741                        rc = 1;
1742                        continue;
1743                }
1744
1745                /* Set up revision info for this refspec */
1746                const char *commit_argv[4];
1747                int commit_argc = 3;
1748                char *new_sha1_hex = strdup(sha1_to_hex(ref->new_sha1));
1749                char *old_sha1_hex = NULL;
1750                commit_argv[1] = "--objects";
1751                commit_argv[2] = new_sha1_hex;
1752                if (!push_all && !is_zero_sha1(ref->old_sha1)) {
1753                        old_sha1_hex = xmalloc(42);
1754                        sprintf(old_sha1_hex, "^%s",
1755                                sha1_to_hex(ref->old_sha1));
1756                        commit_argv[3] = old_sha1_hex;
1757                        commit_argc++;
1758                }
1759                setup_revisions(commit_argc, commit_argv, &revs, NULL);
1760                free(new_sha1_hex);
1761                if (old_sha1_hex) {
1762                        free(old_sha1_hex);
1763                        commit_argv[1] = NULL;
1764                }
1765
1766                /* Generate a list of objects that need to be pushed */
1767                pushing = 0;
1768                prepare_revision_walk(&revs);
1769                mark_edges_uninteresting(revs.commits);
1770                fetch_indices();
1771                get_delta(&revs, ref_lock);
1772                finish_all_active_slots();
1773
1774                /* Push missing objects to remote, this would be a
1775                   convenient time to pack them first if appropriate. */
1776                pushing = 1;
1777                fill_active_slots();
1778                finish_all_active_slots();
1779
1780                /* Update the remote branch if all went well */
1781                if (aborted || !update_remote(ref->new_sha1, ref_lock)) {
1782                        rc = 1;
1783                        goto unlock;
1784                }
1785
1786        unlock:
1787                if (!rc)
1788                        fprintf(stderr, "    done\n");
1789                unlock_remote(ref_lock);
1790        }
1791
1792 cleanup:
1793        free(remote);
1794
1795        curl_slist_free_all(no_pragma_header);
1796        curl_slist_free_all(default_headers);
1797
1798        http_cleanup();
1799
1800        request = request_queue_head;
1801        while (request != NULL) {
1802                next_request = request->next;
1803                release_request(request);
1804                request = next_request;
1805        }
1806
1807        return rc;
1808}