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