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