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