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