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