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