Merge branch 'rj/test-regex'
[git] / http-push.c
1 #include "cache.h"
2 #include "commit.h"
3 #include "tag.h"
4 #include "blob.h"
5 #include "http.h"
6 #include "refs.h"
7 #include "diff.h"
8 #include "revision.h"
9 #include "exec_cmd.h"
10 #include "remote.h"
11 #include "list-objects.h"
12 #include "sigchain.h"
13
14 #include <expat.h>
15
16 static const char http_push_usage[] =
17 "git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
18
19 #ifndef XML_STATUS_OK
20 enum XML_Status {
21   XML_STATUS_OK = 1,
22   XML_STATUS_ERROR = 0
23 };
24 #define XML_STATUS_OK    1
25 #define XML_STATUS_ERROR 0
26 #endif
27
28 #define PREV_BUF_SIZE 4096
29
30 /* DAV methods */
31 #define DAV_LOCK "LOCK"
32 #define DAV_MKCOL "MKCOL"
33 #define DAV_MOVE "MOVE"
34 #define DAV_PROPFIND "PROPFIND"
35 #define DAV_PUT "PUT"
36 #define DAV_UNLOCK "UNLOCK"
37 #define DAV_DELETE "DELETE"
38
39 /* DAV lock flags */
40 #define DAV_PROP_LOCKWR (1u << 0)
41 #define DAV_PROP_LOCKEX (1u << 1)
42 #define DAV_LOCK_OK (1u << 2)
43
44 /* DAV XML properties */
45 #define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
46 #define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
47 #define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
48 #define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
49 #define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
50 #define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
51 #define DAV_PROPFIND_RESP ".multistatus.response"
52 #define DAV_PROPFIND_NAME ".multistatus.response.href"
53 #define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
54
55 /* DAV request body templates */
56 #define PROPFIND_SUPPORTEDLOCK_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:prop xmlns:R=\"%s\">\n<D:supportedlock/>\n</D:prop>\n</D:propfind>"
57 #define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
58 #define LOCK_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:lockinfo xmlns:D=\"DAV:\">\n<D:lockscope><D:exclusive/></D:lockscope>\n<D:locktype><D:write/></D:locktype>\n<D:owner>\n<D:href>mailto:%s</D:href>\n</D:owner>\n</D:lockinfo>"
59
60 #define LOCK_TIME 600
61 #define LOCK_REFRESH 30
62
63 /* bits #0-15 in revision.h */
64
65 #define LOCAL    (1u<<16)
66 #define REMOTE   (1u<<17)
67 #define FETCHING (1u<<18)
68 #define PUSHING  (1u<<19)
69
70 /* We allow "recursive" symbolic refs. Only within reason, though */
71 #define MAXDEPTH 5
72
73 static int pushing;
74 static int aborted;
75 static signed char remote_dir_exists[256];
76
77 static int push_verbosely;
78 static int push_all = MATCH_REFS_NONE;
79 static int force_all;
80 static int dry_run;
81 static int helper_status;
82
83 static struct object_list *objects;
84
85 struct repo {
86         char *url;
87         char *path;
88         int path_len;
89         int has_info_refs;
90         int can_update_info_refs;
91         int has_info_packs;
92         struct packed_git *packs;
93         struct remote_lock *locks;
94 };
95
96 static struct repo *repo;
97
98 enum transfer_state {
99         NEED_FETCH,
100         RUN_FETCH_LOOSE,
101         RUN_FETCH_PACKED,
102         NEED_PUSH,
103         RUN_MKCOL,
104         RUN_PUT,
105         RUN_MOVE,
106         ABORTED,
107         COMPLETE
108 };
109
110 struct transfer_request {
111         struct object *obj;
112         char *url;
113         char *dest;
114         struct remote_lock *lock;
115         struct curl_slist *headers;
116         struct buffer buffer;
117         enum transfer_state state;
118         CURLcode curl_result;
119         char errorstr[CURL_ERROR_SIZE];
120         long http_code;
121         void *userData;
122         struct active_request_slot *slot;
123         struct transfer_request *next;
124 };
125
126 static struct transfer_request *request_queue_head;
127
128 struct xml_ctx {
129         char *name;
130         int len;
131         char *cdata;
132         void (*userFunc)(struct xml_ctx *ctx, int tag_closed);
133         void *userData;
134 };
135
136 struct remote_lock {
137         char *url;
138         char *owner;
139         char *token;
140         char tmpfile_suffix[41];
141         time_t start_time;
142         long timeout;
143         int refreshing;
144         struct remote_lock *next;
145 };
146
147 /* Flags that control remote_ls processing */
148 #define PROCESS_FILES (1u << 0)
149 #define PROCESS_DIRS  (1u << 1)
150 #define RECURSIVE     (1u << 2)
151
152 /* Flags that remote_ls passes to callback functions */
153 #define IS_DIR (1u << 0)
154
155 struct remote_ls_ctx {
156         char *path;
157         void (*userFunc)(struct remote_ls_ctx *ls);
158         void *userData;
159         int flags;
160         char *dentry_name;
161         int dentry_flags;
162         struct remote_ls_ctx *parent;
163 };
164
165 /* get_dav_token_headers options */
166 enum dav_header_flag {
167         DAV_HEADER_IF = (1u << 0),
168         DAV_HEADER_LOCK = (1u << 1),
169         DAV_HEADER_TIMEOUT = (1u << 2)
170 };
171
172 static char *xml_entities(const char *s)
173 {
174         struct strbuf buf = STRBUF_INIT;
175         while (*s) {
176                 size_t len = strcspn(s, "\"<>&");
177                 strbuf_add(&buf, s, len);
178                 s += len;
179                 switch (*s) {
180                 case '"':
181                         strbuf_addstr(&buf, "&quot;");
182                         break;
183                 case '<':
184                         strbuf_addstr(&buf, "&lt;");
185                         break;
186                 case '>':
187                         strbuf_addstr(&buf, "&gt;");
188                         break;
189                 case '&':
190                         strbuf_addstr(&buf, "&amp;");
191                         break;
192                 case 0:
193                         return strbuf_detach(&buf, NULL);
194                 }
195                 s++;
196         }
197         return strbuf_detach(&buf, NULL);
198 }
199
200 static void curl_setup_http_get(CURL *curl, const char *url,
201                 const char *custom_req)
202 {
203         curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
204         curl_easy_setopt(curl, CURLOPT_URL, url);
205         curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, custom_req);
206         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_null);
207 }
208
209 static void curl_setup_http(CURL *curl, const char *url,
210                 const char *custom_req, struct buffer *buffer,
211                 curl_write_callback write_fn)
212 {
213         curl_easy_setopt(curl, CURLOPT_PUT, 1);
214         curl_easy_setopt(curl, CURLOPT_URL, url);
215         curl_easy_setopt(curl, CURLOPT_INFILE, buffer);
216         curl_easy_setopt(curl, CURLOPT_INFILESIZE, buffer->buf.len);
217         curl_easy_setopt(curl, CURLOPT_READFUNCTION, fread_buffer);
218 #ifndef NO_CURL_IOCTL
219         curl_easy_setopt(curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
220         curl_easy_setopt(curl, CURLOPT_IOCTLDATA, &buffer);
221 #endif
222         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_fn);
223         curl_easy_setopt(curl, CURLOPT_NOBODY, 0);
224         curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, custom_req);
225         curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
226 }
227
228 static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum dav_header_flag options)
229 {
230         struct strbuf buf = STRBUF_INIT;
231         struct curl_slist *dav_headers = NULL;
232
233         if (options & DAV_HEADER_IF) {
234                 strbuf_addf(&buf, "If: (<%s>)", lock->token);
235                 dav_headers = curl_slist_append(dav_headers, buf.buf);
236                 strbuf_reset(&buf);
237         }
238         if (options & DAV_HEADER_LOCK) {
239                 strbuf_addf(&buf, "Lock-Token: <%s>", lock->token);
240                 dav_headers = curl_slist_append(dav_headers, buf.buf);
241                 strbuf_reset(&buf);
242         }
243         if (options & DAV_HEADER_TIMEOUT) {
244                 strbuf_addf(&buf, "Timeout: Second-%ld", lock->timeout);
245                 dav_headers = curl_slist_append(dav_headers, buf.buf);
246                 strbuf_reset(&buf);
247         }
248         strbuf_release(&buf);
249
250         return dav_headers;
251 }
252
253 static void finish_request(struct transfer_request *request);
254 static void release_request(struct transfer_request *request);
255
256 static void process_response(void *callback_data)
257 {
258         struct transfer_request *request =
259                 (struct transfer_request *)callback_data;
260
261         finish_request(request);
262 }
263
264 #ifdef USE_CURL_MULTI
265
266 static void start_fetch_loose(struct transfer_request *request)
267 {
268         struct active_request_slot *slot;
269         struct http_object_request *obj_req;
270
271         obj_req = new_http_object_request(repo->url, request->obj->sha1);
272         if (obj_req == NULL) {
273                 request->state = ABORTED;
274                 return;
275         }
276
277         slot = obj_req->slot;
278         slot->callback_func = process_response;
279         slot->callback_data = request;
280         request->slot = slot;
281         request->userData = obj_req;
282
283         /* Try to get the request started, abort the request on error */
284         request->state = RUN_FETCH_LOOSE;
285         if (!start_active_slot(slot)) {
286                 fprintf(stderr, "Unable to start GET request\n");
287                 repo->can_update_info_refs = 0;
288                 release_http_object_request(obj_req);
289                 release_request(request);
290         }
291 }
292
293 static void start_mkcol(struct transfer_request *request)
294 {
295         char *hex = sha1_to_hex(request->obj->sha1);
296         struct active_request_slot *slot;
297
298         request->url = get_remote_object_url(repo->url, hex, 1);
299
300         slot = get_active_slot();
301         slot->callback_func = process_response;
302         slot->callback_data = request;
303         curl_setup_http_get(slot->curl, request->url, DAV_MKCOL);
304         curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
305
306         if (start_active_slot(slot)) {
307                 request->slot = slot;
308                 request->state = RUN_MKCOL;
309         } else {
310                 request->state = ABORTED;
311                 free(request->url);
312                 request->url = NULL;
313         }
314 }
315 #endif
316
317 static void start_fetch_packed(struct transfer_request *request)
318 {
319         struct packed_git *target;
320
321         struct transfer_request *check_request = request_queue_head;
322         struct http_pack_request *preq;
323
324         target = find_sha1_pack(request->obj->sha1, repo->packs);
325         if (!target) {
326                 fprintf(stderr, "Unable to fetch %s, will not be able to update server info refs\n", sha1_to_hex(request->obj->sha1));
327                 repo->can_update_info_refs = 0;
328                 release_request(request);
329                 return;
330         }
331
332         fprintf(stderr, "Fetching pack %s\n", sha1_to_hex(target->sha1));
333         fprintf(stderr, " which contains %s\n", sha1_to_hex(request->obj->sha1));
334
335         preq = new_http_pack_request(target, repo->url);
336         if (preq == NULL) {
337                 release_http_pack_request(preq);
338                 repo->can_update_info_refs = 0;
339                 return;
340         }
341         preq->lst = &repo->packs;
342
343         /* Make sure there isn't another open request for this pack */
344         while (check_request) {
345                 if (check_request->state == RUN_FETCH_PACKED &&
346                     !strcmp(check_request->url, preq->url)) {
347                         release_http_pack_request(preq);
348                         release_request(request);
349                         return;
350                 }
351                 check_request = check_request->next;
352         }
353
354         preq->slot->callback_func = process_response;
355         preq->slot->callback_data = request;
356         request->slot = preq->slot;
357         request->userData = preq;
358
359         /* Try to get the request started, abort the request on error */
360         request->state = RUN_FETCH_PACKED;
361         if (!start_active_slot(preq->slot)) {
362                 fprintf(stderr, "Unable to start GET request\n");
363                 release_http_pack_request(preq);
364                 repo->can_update_info_refs = 0;
365                 release_request(request);
366         }
367 }
368
369 static void start_put(struct transfer_request *request)
370 {
371         char *hex = sha1_to_hex(request->obj->sha1);
372         struct active_request_slot *slot;
373         struct strbuf buf = STRBUF_INIT;
374         enum object_type type;
375         char hdr[50];
376         void *unpacked;
377         unsigned long len;
378         int hdrlen;
379         ssize_t size;
380         git_zstream stream;
381
382         unpacked = read_sha1_file(request->obj->sha1, &type, &len);
383         hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
384
385         /* Set it up */
386         memset(&stream, 0, sizeof(stream));
387         git_deflate_init(&stream, zlib_compression_level);
388         size = git_deflate_bound(&stream, len + hdrlen);
389         strbuf_init(&request->buffer.buf, size);
390         request->buffer.posn = 0;
391
392         /* Compress it */
393         stream.next_out = (unsigned char *)request->buffer.buf.buf;
394         stream.avail_out = size;
395
396         /* First header.. */
397         stream.next_in = (void *)hdr;
398         stream.avail_in = hdrlen;
399         while (git_deflate(&stream, 0) == Z_OK)
400                 ; /* nothing */
401
402         /* Then the data itself.. */
403         stream.next_in = unpacked;
404         stream.avail_in = len;
405         while (git_deflate(&stream, Z_FINISH) == Z_OK)
406                 ; /* nothing */
407         git_deflate_end(&stream);
408         free(unpacked);
409
410         request->buffer.buf.len = stream.total_out;
411
412         strbuf_addstr(&buf, "Destination: ");
413         append_remote_object_url(&buf, repo->url, hex, 0);
414         request->dest = strbuf_detach(&buf, NULL);
415
416         append_remote_object_url(&buf, repo->url, hex, 0);
417         strbuf_add(&buf, request->lock->tmpfile_suffix, 41);
418         request->url = strbuf_detach(&buf, NULL);
419
420         slot = get_active_slot();
421         slot->callback_func = process_response;
422         slot->callback_data = request;
423         curl_setup_http(slot->curl, request->url, DAV_PUT,
424                         &request->buffer, fwrite_null);
425
426         if (start_active_slot(slot)) {
427                 request->slot = slot;
428                 request->state = RUN_PUT;
429         } else {
430                 request->state = ABORTED;
431                 free(request->url);
432                 request->url = NULL;
433         }
434 }
435
436 static void start_move(struct transfer_request *request)
437 {
438         struct active_request_slot *slot;
439         struct curl_slist *dav_headers = NULL;
440
441         slot = get_active_slot();
442         slot->callback_func = process_response;
443         slot->callback_data = request;
444         curl_setup_http_get(slot->curl, request->url, DAV_MOVE);
445         dav_headers = curl_slist_append(dav_headers, request->dest);
446         dav_headers = curl_slist_append(dav_headers, "Overwrite: T");
447         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
448
449         if (start_active_slot(slot)) {
450                 request->slot = slot;
451                 request->state = RUN_MOVE;
452         } else {
453                 request->state = ABORTED;
454                 free(request->url);
455                 request->url = NULL;
456         }
457 }
458
459 static int refresh_lock(struct remote_lock *lock)
460 {
461         struct active_request_slot *slot;
462         struct slot_results results;
463         struct curl_slist *dav_headers;
464         int rc = 0;
465
466         lock->refreshing = 1;
467
468         dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF | DAV_HEADER_TIMEOUT);
469
470         slot = get_active_slot();
471         slot->results = &results;
472         curl_setup_http_get(slot->curl, lock->url, DAV_LOCK);
473         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
474
475         if (start_active_slot(slot)) {
476                 run_active_slot(slot);
477                 if (results.curl_result != CURLE_OK) {
478                         fprintf(stderr, "LOCK HTTP error %ld\n",
479                                 results.http_code);
480                 } else {
481                         lock->start_time = time(NULL);
482                         rc = 1;
483                 }
484         }
485
486         lock->refreshing = 0;
487         curl_slist_free_all(dav_headers);
488
489         return rc;
490 }
491
492 static void check_locks(void)
493 {
494         struct remote_lock *lock = repo->locks;
495         time_t current_time = time(NULL);
496         int time_remaining;
497
498         while (lock) {
499                 time_remaining = lock->start_time + lock->timeout -
500                         current_time;
501                 if (!lock->refreshing && time_remaining < LOCK_REFRESH) {
502                         if (!refresh_lock(lock)) {
503                                 fprintf(stderr,
504                                         "Unable to refresh lock for %s\n",
505                                         lock->url);
506                                 aborted = 1;
507                                 return;
508                         }
509                 }
510                 lock = lock->next;
511         }
512 }
513
514 static void release_request(struct transfer_request *request)
515 {
516         struct transfer_request *entry = request_queue_head;
517
518         if (request == request_queue_head) {
519                 request_queue_head = request->next;
520         } else {
521                 while (entry->next != NULL && entry->next != request)
522                         entry = entry->next;
523                 if (entry->next == request)
524                         entry->next = entry->next->next;
525         }
526
527         free(request->url);
528         free(request);
529 }
530
531 static void finish_request(struct transfer_request *request)
532 {
533         struct http_pack_request *preq;
534         struct http_object_request *obj_req;
535
536         request->curl_result = request->slot->curl_result;
537         request->http_code = request->slot->http_code;
538         request->slot = NULL;
539
540         /* Keep locks active */
541         check_locks();
542
543         if (request->headers != NULL)
544                 curl_slist_free_all(request->headers);
545
546         /* URL is reused for MOVE after PUT */
547         if (request->state != RUN_PUT) {
548                 free(request->url);
549                 request->url = NULL;
550         }
551
552         if (request->state == RUN_MKCOL) {
553                 if (request->curl_result == CURLE_OK ||
554                     request->http_code == 405) {
555                         remote_dir_exists[request->obj->sha1[0]] = 1;
556                         start_put(request);
557                 } else {
558                         fprintf(stderr, "MKCOL %s failed, aborting (%d/%ld)\n",
559                                 sha1_to_hex(request->obj->sha1),
560                                 request->curl_result, request->http_code);
561                         request->state = ABORTED;
562                         aborted = 1;
563                 }
564         } else if (request->state == RUN_PUT) {
565                 if (request->curl_result == CURLE_OK) {
566                         start_move(request);
567                 } else {
568                         fprintf(stderr, "PUT %s failed, aborting (%d/%ld)\n",
569                                 sha1_to_hex(request->obj->sha1),
570                                 request->curl_result, request->http_code);
571                         request->state = ABORTED;
572                         aborted = 1;
573                 }
574         } else if (request->state == RUN_MOVE) {
575                 if (request->curl_result == CURLE_OK) {
576                         if (push_verbosely)
577                                 fprintf(stderr, "    sent %s\n",
578                                         sha1_to_hex(request->obj->sha1));
579                         request->obj->flags |= REMOTE;
580                         release_request(request);
581                 } else {
582                         fprintf(stderr, "MOVE %s failed, aborting (%d/%ld)\n",
583                                 sha1_to_hex(request->obj->sha1),
584                                 request->curl_result, request->http_code);
585                         request->state = ABORTED;
586                         aborted = 1;
587                 }
588         } else if (request->state == RUN_FETCH_LOOSE) {
589                 obj_req = (struct http_object_request *)request->userData;
590
591                 if (finish_http_object_request(obj_req) == 0)
592                         if (obj_req->rename == 0)
593                                 request->obj->flags |= (LOCAL | REMOTE);
594
595                 /* Try fetching packed if necessary */
596                 if (request->obj->flags & LOCAL) {
597                         release_http_object_request(obj_req);
598                         release_request(request);
599                 } else
600                         start_fetch_packed(request);
601
602         } else if (request->state == RUN_FETCH_PACKED) {
603                 int fail = 1;
604                 if (request->curl_result != CURLE_OK) {
605                         fprintf(stderr, "Unable to get pack file %s\n%s",
606                                 request->url, curl_errorstr);
607                 } else {
608                         preq = (struct http_pack_request *)request->userData;
609
610                         if (preq) {
611                                 if (finish_http_pack_request(preq) == 0)
612                                         fail = 0;
613                                 release_http_pack_request(preq);
614                         }
615                 }
616                 if (fail)
617                         repo->can_update_info_refs = 0;
618                 release_request(request);
619         }
620 }
621
622 #ifdef USE_CURL_MULTI
623 static int is_running_queue;
624 static int fill_active_slot(void *unused)
625 {
626         struct transfer_request *request;
627
628         if (aborted || !is_running_queue)
629                 return 0;
630
631         for (request = request_queue_head; request; request = request->next) {
632                 if (request->state == NEED_FETCH) {
633                         start_fetch_loose(request);
634                         return 1;
635                 } else if (pushing && request->state == NEED_PUSH) {
636                         if (remote_dir_exists[request->obj->sha1[0]] == 1) {
637                                 start_put(request);
638                         } else {
639                                 start_mkcol(request);
640                         }
641                         return 1;
642                 }
643         }
644         return 0;
645 }
646 #endif
647
648 static void get_remote_object_list(unsigned char parent);
649
650 static void add_fetch_request(struct object *obj)
651 {
652         struct transfer_request *request;
653
654         check_locks();
655
656         /*
657          * Don't fetch the object if it's known to exist locally
658          * or is already in the request queue
659          */
660         if (remote_dir_exists[obj->sha1[0]] == -1)
661                 get_remote_object_list(obj->sha1[0]);
662         if (obj->flags & (LOCAL | FETCHING))
663                 return;
664
665         obj->flags |= FETCHING;
666         request = xmalloc(sizeof(*request));
667         request->obj = obj;
668         request->url = NULL;
669         request->lock = NULL;
670         request->headers = NULL;
671         request->state = NEED_FETCH;
672         request->next = request_queue_head;
673         request_queue_head = request;
674
675 #ifdef USE_CURL_MULTI
676         fill_active_slots();
677         step_active_slots();
678 #endif
679 }
680
681 static int add_send_request(struct object *obj, struct remote_lock *lock)
682 {
683         struct transfer_request *request = request_queue_head;
684         struct packed_git *target;
685
686         /* Keep locks active */
687         check_locks();
688
689         /*
690          * Don't push the object if it's known to exist on the remote
691          * or is already in the request queue
692          */
693         if (remote_dir_exists[obj->sha1[0]] == -1)
694                 get_remote_object_list(obj->sha1[0]);
695         if (obj->flags & (REMOTE | PUSHING))
696                 return 0;
697         target = find_sha1_pack(obj->sha1, repo->packs);
698         if (target) {
699                 obj->flags |= REMOTE;
700                 return 0;
701         }
702
703         obj->flags |= PUSHING;
704         request = xmalloc(sizeof(*request));
705         request->obj = obj;
706         request->url = NULL;
707         request->lock = lock;
708         request->headers = NULL;
709         request->state = NEED_PUSH;
710         request->next = request_queue_head;
711         request_queue_head = request;
712
713 #ifdef USE_CURL_MULTI
714         fill_active_slots();
715         step_active_slots();
716 #endif
717
718         return 1;
719 }
720
721 static int fetch_indices(void)
722 {
723         int ret;
724
725         if (push_verbosely)
726                 fprintf(stderr, "Getting pack list\n");
727
728         switch (http_get_info_packs(repo->url, &repo->packs)) {
729         case HTTP_OK:
730         case HTTP_MISSING_TARGET:
731                 ret = 0;
732                 break;
733         default:
734                 ret = -1;
735         }
736
737         return ret;
738 }
739
740 static void one_remote_object(const char *hex)
741 {
742         unsigned char sha1[20];
743         struct object *obj;
744
745         if (get_sha1_hex(hex, sha1) != 0)
746                 return;
747
748         obj = lookup_object(sha1);
749         if (!obj)
750                 obj = parse_object(sha1);
751
752         /* Ignore remote objects that don't exist locally */
753         if (!obj)
754                 return;
755
756         obj->flags |= REMOTE;
757         if (!object_list_contains(objects, obj))
758                 object_list_insert(obj, &objects);
759 }
760
761 static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed)
762 {
763         int *lock_flags = (int *)ctx->userData;
764
765         if (tag_closed) {
766                 if (!strcmp(ctx->name, DAV_CTX_LOCKENTRY)) {
767                         if ((*lock_flags & DAV_PROP_LOCKEX) &&
768                             (*lock_flags & DAV_PROP_LOCKWR)) {
769                                 *lock_flags |= DAV_LOCK_OK;
770                         }
771                         *lock_flags &= DAV_LOCK_OK;
772                 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_WRITE)) {
773                         *lock_flags |= DAV_PROP_LOCKWR;
774                 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_EXCLUSIVE)) {
775                         *lock_flags |= DAV_PROP_LOCKEX;
776                 }
777         }
778 }
779
780 static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
781 {
782         struct remote_lock *lock = (struct remote_lock *)ctx->userData;
783         git_SHA_CTX sha_ctx;
784         unsigned char lock_token_sha1[20];
785
786         if (tag_closed && ctx->cdata) {
787                 if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) {
788                         lock->owner = xmalloc(strlen(ctx->cdata) + 1);
789                         strcpy(lock->owner, ctx->cdata);
790                 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
791                         if (!prefixcmp(ctx->cdata, "Second-"))
792                                 lock->timeout =
793                                         strtol(ctx->cdata + 7, NULL, 10);
794                 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
795                         lock->token = xmalloc(strlen(ctx->cdata) + 1);
796                         strcpy(lock->token, ctx->cdata);
797
798                         git_SHA1_Init(&sha_ctx);
799                         git_SHA1_Update(&sha_ctx, lock->token, strlen(lock->token));
800                         git_SHA1_Final(lock_token_sha1, &sha_ctx);
801
802                         lock->tmpfile_suffix[0] = '_';
803                         memcpy(lock->tmpfile_suffix + 1, sha1_to_hex(lock_token_sha1), 40);
804                 }
805         }
806 }
807
808 static void one_remote_ref(const char *refname);
809
810 static void
811 xml_start_tag(void *userData, const char *name, const char **atts)
812 {
813         struct xml_ctx *ctx = (struct xml_ctx *)userData;
814         const char *c = strchr(name, ':');
815         int new_len;
816
817         if (c == NULL)
818                 c = name;
819         else
820                 c++;
821
822         new_len = strlen(ctx->name) + strlen(c) + 2;
823
824         if (new_len > ctx->len) {
825                 ctx->name = xrealloc(ctx->name, new_len);
826                 ctx->len = new_len;
827         }
828         strcat(ctx->name, ".");
829         strcat(ctx->name, c);
830
831         free(ctx->cdata);
832         ctx->cdata = NULL;
833
834         ctx->userFunc(ctx, 0);
835 }
836
837 static void
838 xml_end_tag(void *userData, const char *name)
839 {
840         struct xml_ctx *ctx = (struct xml_ctx *)userData;
841         const char *c = strchr(name, ':');
842         char *ep;
843
844         ctx->userFunc(ctx, 1);
845
846         if (c == NULL)
847                 c = name;
848         else
849                 c++;
850
851         ep = ctx->name + strlen(ctx->name) - strlen(c) - 1;
852         *ep = 0;
853 }
854
855 static void
856 xml_cdata(void *userData, const XML_Char *s, int len)
857 {
858         struct xml_ctx *ctx = (struct xml_ctx *)userData;
859         free(ctx->cdata);
860         ctx->cdata = xmemdupz(s, len);
861 }
862
863 static struct remote_lock *lock_remote(const char *path, long timeout)
864 {
865         struct active_request_slot *slot;
866         struct slot_results results;
867         struct buffer out_buffer = { STRBUF_INIT, 0 };
868         struct strbuf in_buffer = STRBUF_INIT;
869         char *url;
870         char *ep;
871         char timeout_header[25];
872         struct remote_lock *lock = NULL;
873         struct curl_slist *dav_headers = NULL;
874         struct xml_ctx ctx;
875         char *escaped;
876
877         url = xmalloc(strlen(repo->url) + strlen(path) + 1);
878         sprintf(url, "%s%s", repo->url, path);
879
880         /* Make sure leading directories exist for the remote ref */
881         ep = strchr(url + strlen(repo->url) + 1, '/');
882         while (ep) {
883                 char saved_character = ep[1];
884                 ep[1] = '\0';
885                 slot = get_active_slot();
886                 slot->results = &results;
887                 curl_setup_http_get(slot->curl, url, DAV_MKCOL);
888                 if (start_active_slot(slot)) {
889                         run_active_slot(slot);
890                         if (results.curl_result != CURLE_OK &&
891                             results.http_code != 405) {
892                                 fprintf(stderr,
893                                         "Unable to create branch path %s\n",
894                                         url);
895                                 free(url);
896                                 return NULL;
897                         }
898                 } else {
899                         fprintf(stderr, "Unable to start MKCOL request\n");
900                         free(url);
901                         return NULL;
902                 }
903                 ep[1] = saved_character;
904                 ep = strchr(ep + 1, '/');
905         }
906
907         escaped = xml_entities(ident_default_email());
908         strbuf_addf(&out_buffer.buf, LOCK_REQUEST, escaped);
909         free(escaped);
910
911         sprintf(timeout_header, "Timeout: Second-%ld", timeout);
912         dav_headers = curl_slist_append(dav_headers, timeout_header);
913         dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
914
915         slot = get_active_slot();
916         slot->results = &results;
917         curl_setup_http(slot->curl, url, DAV_LOCK, &out_buffer, fwrite_buffer);
918         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
919         curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
920
921         lock = xcalloc(1, sizeof(*lock));
922         lock->timeout = -1;
923
924         if (start_active_slot(slot)) {
925                 run_active_slot(slot);
926                 if (results.curl_result == CURLE_OK) {
927                         XML_Parser parser = XML_ParserCreate(NULL);
928                         enum XML_Status result;
929                         ctx.name = xcalloc(10, 1);
930                         ctx.len = 0;
931                         ctx.cdata = NULL;
932                         ctx.userFunc = handle_new_lock_ctx;
933                         ctx.userData = lock;
934                         XML_SetUserData(parser, &ctx);
935                         XML_SetElementHandler(parser, xml_start_tag,
936                                               xml_end_tag);
937                         XML_SetCharacterDataHandler(parser, xml_cdata);
938                         result = XML_Parse(parser, in_buffer.buf,
939                                            in_buffer.len, 1);
940                         free(ctx.name);
941                         if (result != XML_STATUS_OK) {
942                                 fprintf(stderr, "XML error: %s\n",
943                                         XML_ErrorString(
944                                                 XML_GetErrorCode(parser)));
945                                 lock->timeout = -1;
946                         }
947                         XML_ParserFree(parser);
948                 }
949         } else {
950                 fprintf(stderr, "Unable to start LOCK request\n");
951         }
952
953         curl_slist_free_all(dav_headers);
954         strbuf_release(&out_buffer.buf);
955         strbuf_release(&in_buffer);
956
957         if (lock->token == NULL || lock->timeout <= 0) {
958                 free(lock->token);
959                 free(lock->owner);
960                 free(url);
961                 free(lock);
962                 lock = NULL;
963         } else {
964                 lock->url = url;
965                 lock->start_time = time(NULL);
966                 lock->next = repo->locks;
967                 repo->locks = lock;
968         }
969
970         return lock;
971 }
972
973 static int unlock_remote(struct remote_lock *lock)
974 {
975         struct active_request_slot *slot;
976         struct slot_results results;
977         struct remote_lock *prev = repo->locks;
978         struct curl_slist *dav_headers;
979         int rc = 0;
980
981         dav_headers = get_dav_token_headers(lock, DAV_HEADER_LOCK);
982
983         slot = get_active_slot();
984         slot->results = &results;
985         curl_setup_http_get(slot->curl, lock->url, DAV_UNLOCK);
986         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
987
988         if (start_active_slot(slot)) {
989                 run_active_slot(slot);
990                 if (results.curl_result == CURLE_OK)
991                         rc = 1;
992                 else
993                         fprintf(stderr, "UNLOCK HTTP error %ld\n",
994                                 results.http_code);
995         } else {
996                 fprintf(stderr, "Unable to start UNLOCK request\n");
997         }
998
999         curl_slist_free_all(dav_headers);
1000
1001         if (repo->locks == lock) {
1002                 repo->locks = lock->next;
1003         } else {
1004                 while (prev && prev->next != lock)
1005                         prev = prev->next;
1006                 if (prev)
1007                         prev->next = prev->next->next;
1008         }
1009
1010         free(lock->owner);
1011         free(lock->url);
1012         free(lock->token);
1013         free(lock);
1014
1015         return rc;
1016 }
1017
1018 static void remove_locks(void)
1019 {
1020         struct remote_lock *lock = repo->locks;
1021
1022         fprintf(stderr, "Removing remote locks...\n");
1023         while (lock) {
1024                 struct remote_lock *next = lock->next;
1025                 unlock_remote(lock);
1026                 lock = next;
1027         }
1028 }
1029
1030 static void remove_locks_on_signal(int signo)
1031 {
1032         remove_locks();
1033         sigchain_pop(signo);
1034         raise(signo);
1035 }
1036
1037 static void remote_ls(const char *path, int flags,
1038                       void (*userFunc)(struct remote_ls_ctx *ls),
1039                       void *userData);
1040
1041 static void process_ls_object(struct remote_ls_ctx *ls)
1042 {
1043         unsigned int *parent = (unsigned int *)ls->userData;
1044         char *path = ls->dentry_name;
1045         char *obj_hex;
1046
1047         if (!strcmp(ls->path, ls->dentry_name) && (ls->flags & IS_DIR)) {
1048                 remote_dir_exists[*parent] = 1;
1049                 return;
1050         }
1051
1052         if (strlen(path) != 49)
1053                 return;
1054         path += 8;
1055         obj_hex = xmalloc(strlen(path));
1056         /* NB: path is not null-terminated, can not use strlcpy here */
1057         memcpy(obj_hex, path, 2);
1058         strcpy(obj_hex + 2, path + 3);
1059         one_remote_object(obj_hex);
1060         free(obj_hex);
1061 }
1062
1063 static void process_ls_ref(struct remote_ls_ctx *ls)
1064 {
1065         if (!strcmp(ls->path, ls->dentry_name) && (ls->dentry_flags & IS_DIR)) {
1066                 fprintf(stderr, "  %s\n", ls->dentry_name);
1067                 return;
1068         }
1069
1070         if (!(ls->dentry_flags & IS_DIR))
1071                 one_remote_ref(ls->dentry_name);
1072 }
1073
1074 static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
1075 {
1076         struct remote_ls_ctx *ls = (struct remote_ls_ctx *)ctx->userData;
1077
1078         if (tag_closed) {
1079                 if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) {
1080                         if (ls->dentry_flags & IS_DIR) {
1081
1082                                 /* ensure collection names end with slash */
1083                                 str_end_url_with_slash(ls->dentry_name, &ls->dentry_name);
1084
1085                                 if (ls->flags & PROCESS_DIRS) {
1086                                         ls->userFunc(ls);
1087                                 }
1088                                 if (strcmp(ls->dentry_name, ls->path) &&
1089                                     ls->flags & RECURSIVE) {
1090                                         remote_ls(ls->dentry_name,
1091                                                   ls->flags,
1092                                                   ls->userFunc,
1093                                                   ls->userData);
1094                                 }
1095                         } else if (ls->flags & PROCESS_FILES) {
1096                                 ls->userFunc(ls);
1097                         }
1098                 } else if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
1099                         char *path = ctx->cdata;
1100                         if (*ctx->cdata == 'h') {
1101                                 path = strstr(path, "//");
1102                                 if (path) {
1103                                         path = strchr(path+2, '/');
1104                                 }
1105                         }
1106                         if (path) {
1107                                 const char *url = repo->url;
1108                                 if (repo->path)
1109                                         url = repo->path;
1110                                 if (strncmp(path, url, repo->path_len))
1111                                         error("Parsed path '%s' does not match url: '%s'",
1112                                               path, url);
1113                                 else {
1114                                         path += repo->path_len;
1115                                         ls->dentry_name = xstrdup(path);
1116                                 }
1117                         }
1118                 } else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
1119                         ls->dentry_flags |= IS_DIR;
1120                 }
1121         } else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
1122                 free(ls->dentry_name);
1123                 ls->dentry_name = NULL;
1124                 ls->dentry_flags = 0;
1125         }
1126 }
1127
1128 /*
1129  * NEEDSWORK: remote_ls() ignores info/refs on the remote side.  But it
1130  * should _only_ heed the information from that file, instead of trying to
1131  * determine the refs from the remote file system (badly: it does not even
1132  * know about packed-refs).
1133  */
1134 static void remote_ls(const char *path, int flags,
1135                       void (*userFunc)(struct remote_ls_ctx *ls),
1136                       void *userData)
1137 {
1138         char *url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1139         struct active_request_slot *slot;
1140         struct slot_results results;
1141         struct strbuf in_buffer = STRBUF_INIT;
1142         struct buffer out_buffer = { STRBUF_INIT, 0 };
1143         struct curl_slist *dav_headers = NULL;
1144         struct xml_ctx ctx;
1145         struct remote_ls_ctx ls;
1146
1147         ls.flags = flags;
1148         ls.path = xstrdup(path);
1149         ls.dentry_name = NULL;
1150         ls.dentry_flags = 0;
1151         ls.userData = userData;
1152         ls.userFunc = userFunc;
1153
1154         sprintf(url, "%s%s", repo->url, path);
1155
1156         strbuf_addf(&out_buffer.buf, PROPFIND_ALL_REQUEST);
1157
1158         dav_headers = curl_slist_append(dav_headers, "Depth: 1");
1159         dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1160
1161         slot = get_active_slot();
1162         slot->results = &results;
1163         curl_setup_http(slot->curl, url, DAV_PROPFIND,
1164                         &out_buffer, fwrite_buffer);
1165         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1166         curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1167
1168         if (start_active_slot(slot)) {
1169                 run_active_slot(slot);
1170                 if (results.curl_result == CURLE_OK) {
1171                         XML_Parser parser = XML_ParserCreate(NULL);
1172                         enum XML_Status result;
1173                         ctx.name = xcalloc(10, 1);
1174                         ctx.len = 0;
1175                         ctx.cdata = NULL;
1176                         ctx.userFunc = handle_remote_ls_ctx;
1177                         ctx.userData = &ls;
1178                         XML_SetUserData(parser, &ctx);
1179                         XML_SetElementHandler(parser, xml_start_tag,
1180                                               xml_end_tag);
1181                         XML_SetCharacterDataHandler(parser, xml_cdata);
1182                         result = XML_Parse(parser, in_buffer.buf,
1183                                            in_buffer.len, 1);
1184                         free(ctx.name);
1185
1186                         if (result != XML_STATUS_OK) {
1187                                 fprintf(stderr, "XML error: %s\n",
1188                                         XML_ErrorString(
1189                                                 XML_GetErrorCode(parser)));
1190                         }
1191                         XML_ParserFree(parser);
1192                 }
1193         } else {
1194                 fprintf(stderr, "Unable to start PROPFIND request\n");
1195         }
1196
1197         free(ls.path);
1198         free(url);
1199         strbuf_release(&out_buffer.buf);
1200         strbuf_release(&in_buffer);
1201         curl_slist_free_all(dav_headers);
1202 }
1203
1204 static void get_remote_object_list(unsigned char parent)
1205 {
1206         char path[] = "objects/XX/";
1207         static const char hex[] = "0123456789abcdef";
1208         unsigned int val = parent;
1209
1210         path[8] = hex[val >> 4];
1211         path[9] = hex[val & 0xf];
1212         remote_dir_exists[val] = 0;
1213         remote_ls(path, (PROCESS_FILES | PROCESS_DIRS),
1214                   process_ls_object, &val);
1215 }
1216
1217 static int locking_available(void)
1218 {
1219         struct active_request_slot *slot;
1220         struct slot_results results;
1221         struct strbuf in_buffer = STRBUF_INIT;
1222         struct buffer out_buffer = { STRBUF_INIT, 0 };
1223         struct curl_slist *dav_headers = NULL;
1224         struct xml_ctx ctx;
1225         int lock_flags = 0;
1226         char *escaped;
1227
1228         escaped = xml_entities(repo->url);
1229         strbuf_addf(&out_buffer.buf, PROPFIND_SUPPORTEDLOCK_REQUEST, escaped);
1230         free(escaped);
1231
1232         dav_headers = curl_slist_append(dav_headers, "Depth: 0");
1233         dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1234
1235         slot = get_active_slot();
1236         slot->results = &results;
1237         curl_setup_http(slot->curl, repo->url, DAV_PROPFIND,
1238                         &out_buffer, fwrite_buffer);
1239         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1240         curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
1241
1242         if (start_active_slot(slot)) {
1243                 run_active_slot(slot);
1244                 if (results.curl_result == CURLE_OK) {
1245                         XML_Parser parser = XML_ParserCreate(NULL);
1246                         enum XML_Status result;
1247                         ctx.name = xcalloc(10, 1);
1248                         ctx.len = 0;
1249                         ctx.cdata = NULL;
1250                         ctx.userFunc = handle_lockprop_ctx;
1251                         ctx.userData = &lock_flags;
1252                         XML_SetUserData(parser, &ctx);
1253                         XML_SetElementHandler(parser, xml_start_tag,
1254                                               xml_end_tag);
1255                         result = XML_Parse(parser, in_buffer.buf,
1256                                            in_buffer.len, 1);
1257                         free(ctx.name);
1258
1259                         if (result != XML_STATUS_OK) {
1260                                 fprintf(stderr, "XML error: %s\n",
1261                                         XML_ErrorString(
1262                                                 XML_GetErrorCode(parser)));
1263                                 lock_flags = 0;
1264                         }
1265                         XML_ParserFree(parser);
1266                         if (!lock_flags)
1267                                 error("no DAV locking support on %s",
1268                                       repo->url);
1269
1270                 } else {
1271                         error("Cannot access URL %s, return code %d",
1272                               repo->url, results.curl_result);
1273                         lock_flags = 0;
1274                 }
1275         } else {
1276                 error("Unable to start PROPFIND request on %s", repo->url);
1277         }
1278
1279         strbuf_release(&out_buffer.buf);
1280         strbuf_release(&in_buffer);
1281         curl_slist_free_all(dav_headers);
1282
1283         return lock_flags;
1284 }
1285
1286 static struct object_list **add_one_object(struct object *obj, struct object_list **p)
1287 {
1288         struct object_list *entry = xmalloc(sizeof(struct object_list));
1289         entry->item = obj;
1290         entry->next = *p;
1291         *p = entry;
1292         return &entry->next;
1293 }
1294
1295 static struct object_list **process_blob(struct blob *blob,
1296                                          struct object_list **p,
1297                                          struct name_path *path,
1298                                          const char *name)
1299 {
1300         struct object *obj = &blob->object;
1301
1302         obj->flags |= LOCAL;
1303
1304         if (obj->flags & (UNINTERESTING | SEEN))
1305                 return p;
1306
1307         obj->flags |= SEEN;
1308         return add_one_object(obj, p);
1309 }
1310
1311 static struct object_list **process_tree(struct tree *tree,
1312                                          struct object_list **p,
1313                                          struct name_path *path,
1314                                          const char *name)
1315 {
1316         struct object *obj = &tree->object;
1317         struct tree_desc desc;
1318         struct name_entry entry;
1319         struct name_path me;
1320
1321         obj->flags |= LOCAL;
1322
1323         if (obj->flags & (UNINTERESTING | SEEN))
1324                 return p;
1325         if (parse_tree(tree) < 0)
1326                 die("bad tree object %s", sha1_to_hex(obj->sha1));
1327
1328         obj->flags |= SEEN;
1329         name = xstrdup(name);
1330         p = add_one_object(obj, p);
1331         me.up = path;
1332         me.elem = name;
1333         me.elem_len = strlen(name);
1334
1335         init_tree_desc(&desc, tree->buffer, tree->size);
1336
1337         while (tree_entry(&desc, &entry))
1338                 switch (object_type(entry.mode)) {
1339                 case OBJ_TREE:
1340                         p = process_tree(lookup_tree(entry.sha1), p, &me, name);
1341                         break;
1342                 case OBJ_BLOB:
1343                         p = process_blob(lookup_blob(entry.sha1), p, &me, name);
1344                         break;
1345                 default:
1346                         /* Subproject commit - not in this repository */
1347                         break;
1348                 }
1349
1350         free(tree->buffer);
1351         tree->buffer = NULL;
1352         return p;
1353 }
1354
1355 static int get_delta(struct rev_info *revs, struct remote_lock *lock)
1356 {
1357         int i;
1358         struct commit *commit;
1359         struct object_list **p = &objects;
1360         int count = 0;
1361
1362         while ((commit = get_revision(revs)) != NULL) {
1363                 p = process_tree(commit->tree, p, NULL, "");
1364                 commit->object.flags |= LOCAL;
1365                 if (!(commit->object.flags & UNINTERESTING))
1366                         count += add_send_request(&commit->object, lock);
1367         }
1368
1369         for (i = 0; i < revs->pending.nr; i++) {
1370                 struct object_array_entry *entry = revs->pending.objects + i;
1371                 struct object *obj = entry->item;
1372                 const char *name = entry->name;
1373
1374                 if (obj->flags & (UNINTERESTING | SEEN))
1375                         continue;
1376                 if (obj->type == OBJ_TAG) {
1377                         obj->flags |= SEEN;
1378                         p = add_one_object(obj, p);
1379                         continue;
1380                 }
1381                 if (obj->type == OBJ_TREE) {
1382                         p = process_tree((struct tree *)obj, p, NULL, name);
1383                         continue;
1384                 }
1385                 if (obj->type == OBJ_BLOB) {
1386                         p = process_blob((struct blob *)obj, p, NULL, name);
1387                         continue;
1388                 }
1389                 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
1390         }
1391
1392         while (objects) {
1393                 if (!(objects->item->flags & UNINTERESTING))
1394                         count += add_send_request(objects->item, lock);
1395                 objects = objects->next;
1396         }
1397
1398         return count;
1399 }
1400
1401 static int update_remote(unsigned char *sha1, struct remote_lock *lock)
1402 {
1403         struct active_request_slot *slot;
1404         struct slot_results results;
1405         struct buffer out_buffer = { STRBUF_INIT, 0 };
1406         struct curl_slist *dav_headers;
1407
1408         dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1409
1410         strbuf_addf(&out_buffer.buf, "%s\n", sha1_to_hex(sha1));
1411
1412         slot = get_active_slot();
1413         slot->results = &results;
1414         curl_setup_http(slot->curl, lock->url, DAV_PUT,
1415                         &out_buffer, fwrite_null);
1416         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1417
1418         if (start_active_slot(slot)) {
1419                 run_active_slot(slot);
1420                 strbuf_release(&out_buffer.buf);
1421                 if (results.curl_result != CURLE_OK) {
1422                         fprintf(stderr,
1423                                 "PUT error: curl result=%d, HTTP code=%ld\n",
1424                                 results.curl_result, results.http_code);
1425                         /* We should attempt recovery? */
1426                         return 0;
1427                 }
1428         } else {
1429                 strbuf_release(&out_buffer.buf);
1430                 fprintf(stderr, "Unable to start PUT request\n");
1431                 return 0;
1432         }
1433
1434         return 1;
1435 }
1436
1437 static struct ref *remote_refs;
1438
1439 static void one_remote_ref(const char *refname)
1440 {
1441         struct ref *ref;
1442         struct object *obj;
1443
1444         ref = alloc_ref(refname);
1445
1446         if (http_fetch_ref(repo->url, ref) != 0) {
1447                 fprintf(stderr,
1448                         "Unable to fetch ref %s from %s\n",
1449                         refname, repo->url);
1450                 free(ref);
1451                 return;
1452         }
1453
1454         /*
1455          * Fetch a copy of the object if it doesn't exist locally - it
1456          * may be required for updating server info later.
1457          */
1458         if (repo->can_update_info_refs && !has_sha1_file(ref->old_sha1)) {
1459                 obj = lookup_unknown_object(ref->old_sha1);
1460                 if (obj) {
1461                         fprintf(stderr, "  fetch %s for %s\n",
1462                                 sha1_to_hex(ref->old_sha1), refname);
1463                         add_fetch_request(obj);
1464                 }
1465         }
1466
1467         ref->next = remote_refs;
1468         remote_refs = ref;
1469 }
1470
1471 static void get_dav_remote_heads(void)
1472 {
1473         remote_ls("refs/", (PROCESS_FILES | PROCESS_DIRS | RECURSIVE), process_ls_ref, NULL);
1474 }
1475
1476 static void add_remote_info_ref(struct remote_ls_ctx *ls)
1477 {
1478         struct strbuf *buf = (struct strbuf *)ls->userData;
1479         struct object *o;
1480         int len;
1481         char *ref_info;
1482         struct ref *ref;
1483
1484         ref = alloc_ref(ls->dentry_name);
1485
1486         if (http_fetch_ref(repo->url, ref) != 0) {
1487                 fprintf(stderr,
1488                         "Unable to fetch ref %s from %s\n",
1489                         ls->dentry_name, repo->url);
1490                 aborted = 1;
1491                 free(ref);
1492                 return;
1493         }
1494
1495         o = parse_object(ref->old_sha1);
1496         if (!o) {
1497                 fprintf(stderr,
1498                         "Unable to parse object %s for remote ref %s\n",
1499                         sha1_to_hex(ref->old_sha1), ls->dentry_name);
1500                 aborted = 1;
1501                 free(ref);
1502                 return;
1503         }
1504
1505         len = strlen(ls->dentry_name) + 42;
1506         ref_info = xcalloc(len + 1, 1);
1507         sprintf(ref_info, "%s   %s\n",
1508                 sha1_to_hex(ref->old_sha1), ls->dentry_name);
1509         fwrite_buffer(ref_info, 1, len, buf);
1510         free(ref_info);
1511
1512         if (o->type == OBJ_TAG) {
1513                 o = deref_tag(o, ls->dentry_name, 0);
1514                 if (o) {
1515                         len = strlen(ls->dentry_name) + 45;
1516                         ref_info = xcalloc(len + 1, 1);
1517                         sprintf(ref_info, "%s   %s^{}\n",
1518                                 sha1_to_hex(o->sha1), ls->dentry_name);
1519                         fwrite_buffer(ref_info, 1, len, buf);
1520                         free(ref_info);
1521                 }
1522         }
1523         free(ref);
1524 }
1525
1526 static void update_remote_info_refs(struct remote_lock *lock)
1527 {
1528         struct buffer buffer = { STRBUF_INIT, 0 };
1529         struct active_request_slot *slot;
1530         struct slot_results results;
1531         struct curl_slist *dav_headers;
1532
1533         remote_ls("refs/", (PROCESS_FILES | RECURSIVE),
1534                   add_remote_info_ref, &buffer.buf);
1535         if (!aborted) {
1536                 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
1537
1538                 slot = get_active_slot();
1539                 slot->results = &results;
1540                 curl_setup_http(slot->curl, lock->url, DAV_PUT,
1541                                 &buffer, fwrite_null);
1542                 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1543
1544                 if (start_active_slot(slot)) {
1545                         run_active_slot(slot);
1546                         if (results.curl_result != CURLE_OK) {
1547                                 fprintf(stderr,
1548                                         "PUT error: curl result=%d, HTTP code=%ld\n",
1549                                         results.curl_result, results.http_code);
1550                         }
1551                 }
1552         }
1553         strbuf_release(&buffer.buf);
1554 }
1555
1556 static int remote_exists(const char *path)
1557 {
1558         char *url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1559         int ret;
1560
1561         sprintf(url, "%s%s", repo->url, path);
1562
1563         switch (http_get_strbuf(url, NULL, 0)) {
1564         case HTTP_OK:
1565                 ret = 1;
1566                 break;
1567         case HTTP_MISSING_TARGET:
1568                 ret = 0;
1569                 break;
1570         case HTTP_ERROR:
1571                 http_error(url, HTTP_ERROR);
1572         default:
1573                 ret = -1;
1574         }
1575         free(url);
1576         return ret;
1577 }
1578
1579 static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
1580 {
1581         char *url;
1582         struct strbuf buffer = STRBUF_INIT;
1583
1584         url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1585         sprintf(url, "%s%s", repo->url, path);
1586
1587         if (http_get_strbuf(url, &buffer, 0) != HTTP_OK)
1588                 die("Couldn't get %s for remote symref\n%s", url,
1589                     curl_errorstr);
1590         free(url);
1591
1592         free(*symref);
1593         *symref = NULL;
1594         hashclr(sha1);
1595
1596         if (buffer.len == 0)
1597                 return;
1598
1599         /* If it's a symref, set the refname; otherwise try for a sha1 */
1600         if (!prefixcmp((char *)buffer.buf, "ref: ")) {
1601                 *symref = xmemdupz((char *)buffer.buf + 5, buffer.len - 6);
1602         } else {
1603                 get_sha1_hex(buffer.buf, sha1);
1604         }
1605
1606         strbuf_release(&buffer);
1607 }
1608
1609 static int verify_merge_base(unsigned char *head_sha1, struct ref *remote)
1610 {
1611         struct commit *head = lookup_commit_or_die(head_sha1, "HEAD");
1612         struct commit *branch = lookup_commit_or_die(remote->old_sha1, remote->name);
1613
1614         return in_merge_bases(branch, head);
1615 }
1616
1617 static int delete_remote_branch(const char *pattern, int force)
1618 {
1619         struct ref *refs = remote_refs;
1620         struct ref *remote_ref = NULL;
1621         unsigned char head_sha1[20];
1622         char *symref = NULL;
1623         int match;
1624         int patlen = strlen(pattern);
1625         int i;
1626         struct active_request_slot *slot;
1627         struct slot_results results;
1628         char *url;
1629
1630         /* Find the remote branch(es) matching the specified branch name */
1631         for (match = 0; refs; refs = refs->next) {
1632                 char *name = refs->name;
1633                 int namelen = strlen(name);
1634                 if (namelen < patlen ||
1635                     memcmp(name + namelen - patlen, pattern, patlen))
1636                         continue;
1637                 if (namelen != patlen && name[namelen - patlen - 1] != '/')
1638                         continue;
1639                 match++;
1640                 remote_ref = refs;
1641         }
1642         if (match == 0)
1643                 return error("No remote branch matches %s", pattern);
1644         if (match != 1)
1645                 return error("More than one remote branch matches %s",
1646                              pattern);
1647
1648         /*
1649          * Remote HEAD must be a symref (not exactly foolproof; a remote
1650          * symlink to a symref will look like a symref)
1651          */
1652         fetch_symref("HEAD", &symref, head_sha1);
1653         if (!symref)
1654                 return error("Remote HEAD is not a symref");
1655
1656         /* Remote branch must not be the remote HEAD */
1657         for (i = 0; symref && i < MAXDEPTH; i++) {
1658                 if (!strcmp(remote_ref->name, symref))
1659                         return error("Remote branch %s is the current HEAD",
1660                                      remote_ref->name);
1661                 fetch_symref(symref, &symref, head_sha1);
1662         }
1663
1664         /* Run extra sanity checks if delete is not forced */
1665         if (!force) {
1666                 /* Remote HEAD must resolve to a known object */
1667                 if (symref)
1668                         return error("Remote HEAD symrefs too deep");
1669                 if (is_null_sha1(head_sha1))
1670                         return error("Unable to resolve remote HEAD");
1671                 if (!has_sha1_file(head_sha1))
1672                         return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1));
1673
1674                 /* Remote branch must resolve to a known object */
1675                 if (is_null_sha1(remote_ref->old_sha1))
1676                         return error("Unable to resolve remote branch %s",
1677                                      remote_ref->name);
1678                 if (!has_sha1_file(remote_ref->old_sha1))
1679                         return error("Remote branch %s resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", remote_ref->name, sha1_to_hex(remote_ref->old_sha1));
1680
1681                 /* Remote branch must be an ancestor of remote HEAD */
1682                 if (!verify_merge_base(head_sha1, remote_ref)) {
1683                         return error("The branch '%s' is not an ancestor "
1684                                      "of your current HEAD.\n"
1685                                      "If you are sure you want to delete it,"
1686                                      " run:\n\t'git http-push -D %s %s'",
1687                                      remote_ref->name, repo->url, pattern);
1688                 }
1689         }
1690
1691         /* Send delete request */
1692         fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name);
1693         if (dry_run)
1694                 return 0;
1695         url = xmalloc(strlen(repo->url) + strlen(remote_ref->name) + 1);
1696         sprintf(url, "%s%s", repo->url, remote_ref->name);
1697         slot = get_active_slot();
1698         slot->results = &results;
1699         curl_setup_http_get(slot->curl, url, DAV_DELETE);
1700         if (start_active_slot(slot)) {
1701                 run_active_slot(slot);
1702                 free(url);
1703                 if (results.curl_result != CURLE_OK)
1704                         return error("DELETE request failed (%d/%ld)",
1705                                      results.curl_result, results.http_code);
1706         } else {
1707                 free(url);
1708                 return error("Unable to start DELETE request");
1709         }
1710
1711         return 0;
1712 }
1713
1714 static void run_request_queue(void)
1715 {
1716 #ifdef USE_CURL_MULTI
1717         is_running_queue = 1;
1718         fill_active_slots();
1719         add_fill_function(NULL, fill_active_slot);
1720 #endif
1721         do {
1722                 finish_all_active_slots();
1723 #ifdef USE_CURL_MULTI
1724                 fill_active_slots();
1725 #endif
1726         } while (request_queue_head && !aborted);
1727
1728 #ifdef USE_CURL_MULTI
1729         is_running_queue = 0;
1730 #endif
1731 }
1732
1733 int main(int argc, char **argv)
1734 {
1735         struct transfer_request *request;
1736         struct transfer_request *next_request;
1737         int nr_refspec = 0;
1738         char **refspec = NULL;
1739         struct remote_lock *ref_lock = NULL;
1740         struct remote_lock *info_ref_lock = NULL;
1741         struct rev_info revs;
1742         int delete_branch = 0;
1743         int force_delete = 0;
1744         int objects_to_send;
1745         int rc = 0;
1746         int i;
1747         int new_refs;
1748         struct ref *ref, *local_refs;
1749
1750         git_setup_gettext();
1751
1752         git_extract_argv0_path(argv[0]);
1753
1754         repo = xcalloc(sizeof(*repo), 1);
1755
1756         argv++;
1757         for (i = 1; i < argc; i++, argv++) {
1758                 char *arg = *argv;
1759
1760                 if (*arg == '-') {
1761                         if (!strcmp(arg, "--all")) {
1762                                 push_all = MATCH_REFS_ALL;
1763                                 continue;
1764                         }
1765                         if (!strcmp(arg, "--force")) {
1766                                 force_all = 1;
1767                                 continue;
1768                         }
1769                         if (!strcmp(arg, "--dry-run")) {
1770                                 dry_run = 1;
1771                                 continue;
1772                         }
1773                         if (!strcmp(arg, "--helper-status")) {
1774                                 helper_status = 1;
1775                                 continue;
1776                         }
1777                         if (!strcmp(arg, "--verbose")) {
1778                                 push_verbosely = 1;
1779                                 http_is_verbose = 1;
1780                                 continue;
1781                         }
1782                         if (!strcmp(arg, "-d")) {
1783                                 delete_branch = 1;
1784                                 continue;
1785                         }
1786                         if (!strcmp(arg, "-D")) {
1787                                 delete_branch = 1;
1788                                 force_delete = 1;
1789                                 continue;
1790                         }
1791                         if (!strcmp(arg, "-h"))
1792                                 usage(http_push_usage);
1793                 }
1794                 if (!repo->url) {
1795                         char *path = strstr(arg, "//");
1796                         str_end_url_with_slash(arg, &repo->url);
1797                         repo->path_len = strlen(repo->url);
1798                         if (path) {
1799                                 repo->path = strchr(path+2, '/');
1800                                 if (repo->path)
1801                                         repo->path_len = strlen(repo->path);
1802                         }
1803                         continue;
1804                 }
1805                 refspec = argv;
1806                 nr_refspec = argc - i;
1807                 break;
1808         }
1809
1810 #ifndef USE_CURL_MULTI
1811         die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
1812 #endif
1813
1814         if (!repo->url)
1815                 usage(http_push_usage);
1816
1817         if (delete_branch && nr_refspec != 1)
1818                 die("You must specify only one branch name when deleting a remote branch");
1819
1820         setup_git_directory();
1821
1822         memset(remote_dir_exists, -1, 256);
1823
1824         http_init(NULL, repo->url, 1);
1825
1826 #ifdef USE_CURL_MULTI
1827         is_running_queue = 0;
1828 #endif
1829
1830         /* Verify DAV compliance/lock support */
1831         if (!locking_available()) {
1832                 rc = 1;
1833                 goto cleanup;
1834         }
1835
1836         sigchain_push_common(remove_locks_on_signal);
1837
1838         /* Check whether the remote has server info files */
1839         repo->can_update_info_refs = 0;
1840         repo->has_info_refs = remote_exists("info/refs");
1841         repo->has_info_packs = remote_exists("objects/info/packs");
1842         if (repo->has_info_refs) {
1843                 info_ref_lock = lock_remote("info/refs", LOCK_TIME);
1844                 if (info_ref_lock)
1845                         repo->can_update_info_refs = 1;
1846                 else {
1847                         error("cannot lock existing info/refs");
1848                         rc = 1;
1849                         goto cleanup;
1850                 }
1851         }
1852         if (repo->has_info_packs)
1853                 fetch_indices();
1854
1855         /* Get a list of all local and remote heads to validate refspecs */
1856         local_refs = get_local_heads();
1857         fprintf(stderr, "Fetching remote heads...\n");
1858         get_dav_remote_heads();
1859         run_request_queue();
1860
1861         /* Remove a remote branch if -d or -D was specified */
1862         if (delete_branch) {
1863                 if (delete_remote_branch(refspec[0], force_delete) == -1) {
1864                         fprintf(stderr, "Unable to delete remote branch %s\n",
1865                                 refspec[0]);
1866                         if (helper_status)
1867                                 printf("error %s cannot remove\n", refspec[0]);
1868                 }
1869                 goto cleanup;
1870         }
1871
1872         /* match them up */
1873         if (match_push_refs(local_refs, &remote_refs,
1874                             nr_refspec, (const char **) refspec, push_all)) {
1875                 rc = -1;
1876                 goto cleanup;
1877         }
1878         if (!remote_refs) {
1879                 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
1880                 if (helper_status)
1881                         printf("error null no match\n");
1882                 rc = 0;
1883                 goto cleanup;
1884         }
1885
1886         new_refs = 0;
1887         for (ref = remote_refs; ref; ref = ref->next) {
1888                 char old_hex[60], *new_hex;
1889                 const char *commit_argv[5];
1890                 int commit_argc;
1891                 char *new_sha1_hex, *old_sha1_hex;
1892
1893                 if (!ref->peer_ref)
1894                         continue;
1895
1896                 if (is_null_sha1(ref->peer_ref->new_sha1)) {
1897                         if (delete_remote_branch(ref->name, 1) == -1) {
1898                                 error("Could not remove %s", ref->name);
1899                                 if (helper_status)
1900                                         printf("error %s cannot remove\n", ref->name);
1901                                 rc = -4;
1902                         }
1903                         else if (helper_status)
1904                                 printf("ok %s\n", ref->name);
1905                         new_refs++;
1906                         continue;
1907                 }
1908
1909                 if (!hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
1910                         if (push_verbosely)
1911                                 fprintf(stderr, "'%s': up-to-date\n", ref->name);
1912                         if (helper_status)
1913                                 printf("ok %s up to date\n", ref->name);
1914                         continue;
1915                 }
1916
1917                 if (!force_all &&
1918                     !is_null_sha1(ref->old_sha1) &&
1919                     !ref->force) {
1920                         if (!has_sha1_file(ref->old_sha1) ||
1921                             !ref_newer(ref->peer_ref->new_sha1,
1922                                        ref->old_sha1)) {
1923                                 /*
1924                                  * We do not have the remote ref, or
1925                                  * we know that the remote ref is not
1926                                  * an ancestor of what we are trying to
1927                                  * push.  Either way this can be losing
1928                                  * commits at the remote end and likely
1929                                  * we were not up to date to begin with.
1930                                  */
1931                                 error("remote '%s' is not an ancestor of\n"
1932                                       "local '%s'.\n"
1933                                       "Maybe you are not up-to-date and "
1934                                       "need to pull first?",
1935                                       ref->name,
1936                                       ref->peer_ref->name);
1937                                 if (helper_status)
1938                                         printf("error %s non-fast forward\n", ref->name);
1939                                 rc = -2;
1940                                 continue;
1941                         }
1942                 }
1943                 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
1944                 new_refs++;
1945                 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
1946                 new_hex = sha1_to_hex(ref->new_sha1);
1947
1948                 fprintf(stderr, "updating '%s'", ref->name);
1949                 if (strcmp(ref->name, ref->peer_ref->name))
1950                         fprintf(stderr, " using '%s'", ref->peer_ref->name);
1951                 fprintf(stderr, "\n  from %s\n  to   %s\n", old_hex, new_hex);
1952                 if (dry_run) {
1953                         if (helper_status)
1954                                 printf("ok %s\n", ref->name);
1955                         continue;
1956                 }
1957
1958                 /* Lock remote branch ref */
1959                 ref_lock = lock_remote(ref->name, LOCK_TIME);
1960                 if (ref_lock == NULL) {
1961                         fprintf(stderr, "Unable to lock remote branch %s\n",
1962                                 ref->name);
1963                         if (helper_status)
1964                                 printf("error %s lock error\n", ref->name);
1965                         rc = 1;
1966                         continue;
1967                 }
1968
1969                 /* Set up revision info for this refspec */
1970                 commit_argc = 3;
1971                 new_sha1_hex = xstrdup(sha1_to_hex(ref->new_sha1));
1972                 old_sha1_hex = NULL;
1973                 commit_argv[1] = "--objects";
1974                 commit_argv[2] = new_sha1_hex;
1975                 if (!push_all && !is_null_sha1(ref->old_sha1)) {
1976                         old_sha1_hex = xmalloc(42);
1977                         sprintf(old_sha1_hex, "^%s",
1978                                 sha1_to_hex(ref->old_sha1));
1979                         commit_argv[3] = old_sha1_hex;
1980                         commit_argc++;
1981                 }
1982                 commit_argv[commit_argc] = NULL;
1983                 init_revisions(&revs, setup_git_directory());
1984                 setup_revisions(commit_argc, commit_argv, &revs, NULL);
1985                 revs.edge_hint = 0; /* just in case */
1986                 free(new_sha1_hex);
1987                 if (old_sha1_hex) {
1988                         free(old_sha1_hex);
1989                         commit_argv[1] = NULL;
1990                 }
1991
1992                 /* Generate a list of objects that need to be pushed */
1993                 pushing = 0;
1994                 if (prepare_revision_walk(&revs))
1995                         die("revision walk setup failed");
1996                 mark_edges_uninteresting(revs.commits, &revs, NULL);
1997                 objects_to_send = get_delta(&revs, ref_lock);
1998                 finish_all_active_slots();
1999
2000                 /* Push missing objects to remote, this would be a
2001                    convenient time to pack them first if appropriate. */
2002                 pushing = 1;
2003                 if (objects_to_send)
2004                         fprintf(stderr, "    sending %d objects\n",
2005                                 objects_to_send);
2006
2007                 run_request_queue();
2008
2009                 /* Update the remote branch if all went well */
2010                 if (aborted || !update_remote(ref->new_sha1, ref_lock))
2011                         rc = 1;
2012
2013                 if (!rc)
2014                         fprintf(stderr, "    done\n");
2015                 if (helper_status)
2016                         printf("%s %s\n", !rc ? "ok" : "error", ref->name);
2017                 unlock_remote(ref_lock);
2018                 check_locks();
2019         }
2020
2021         /* Update remote server info if appropriate */
2022         if (repo->has_info_refs && new_refs) {
2023                 if (info_ref_lock && repo->can_update_info_refs) {
2024                         fprintf(stderr, "Updating remote server info\n");
2025                         if (!dry_run)
2026                                 update_remote_info_refs(info_ref_lock);
2027                 } else {
2028                         fprintf(stderr, "Unable to update server info\n");
2029                 }
2030         }
2031
2032  cleanup:
2033         if (info_ref_lock)
2034                 unlock_remote(info_ref_lock);
2035         free(repo);
2036
2037         http_cleanup();
2038
2039         request = request_queue_head;
2040         while (request != NULL) {
2041                 next_request = request->next;
2042                 release_request(request);
2043                 request = next_request;
2044         }
2045
2046         return rc;
2047 }