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