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