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