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