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