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