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