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