]> git.proxmox.com Git - libgit2.git/blame - src/transports/smart_protocol.c
Merge pull request #2445 from ethomson/checkout_conflict_test
[libgit2.git] / src / transports / smart_protocol.c
CommitLineData
41fb1ca0 1/*
359fc2d2 2 * Copyright (C) the libgit2 contributors. All rights reserved.
41fb1ca0
PK
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
613d5eb9 7#include "git2.h"
83cc70d9 8#include "git2/odb_backend.h"
613d5eb9 9
41fb1ca0
PK
10#include "smart.h"
11#include "refs.h"
09cc0b92 12#include "repository.h"
613d5eb9
PK
13#include "push.h"
14#include "pack-objects.h"
15#include "remote.h"
b176eded 16#include "util.h"
41fb1ca0
PK
17
18#define NETWORK_XFER_THRESHOLD (100*1024)
b176eded
JM
19/* The minimal interval between progress updates (in seconds). */
20#define MIN_PROGRESS_UPDATE_INTERVAL 0.5
41fb1ca0
PK
21
22int git_smart__store_refs(transport_smart *t, int flushes)
23{
24 gitno_buffer *buf = &t->buffer;
25 git_vector *refs = &t->refs;
26 int error, flush = 0, recvd;
e583334c
L
27 const char *line_end = NULL;
28 git_pkt *pkt = NULL;
d8041638 29 size_t i;
41fb1ca0 30
613d5eb9
PK
31 /* Clear existing refs in case git_remote_connect() is called again
32 * after git_remote_disconnect().
33 */
4c4408c3
CMN
34 git_vector_foreach(refs, i, pkt) {
35 git_pkt_free(pkt);
d8041638 36 }
613d5eb9 37 git_vector_clear(refs);
4c4408c3 38 pkt = NULL;
613d5eb9 39
41fb1ca0
PK
40 do {
41 if (buf->offset > 0)
42 error = git_pkt_parse_line(&pkt, buf->data, &line_end, buf->offset);
43 else
44 error = GIT_EBUFS;
45
46 if (error < 0 && error != GIT_EBUFS)
25e0b157 47 return error;
41fb1ca0
PK
48
49 if (error == GIT_EBUFS) {
50 if ((recvd = gitno_recv(buf)) < 0)
80fc7d6b 51 return recvd;
41fb1ca0
PK
52
53 if (recvd == 0 && !flush) {
54 giterr_set(GITERR_NET, "Early EOF");
55 return -1;
56 }
57
58 continue;
59 }
60
61 gitno_consume(buf, line_end);
62 if (pkt->type == GIT_PKT_ERR) {
63 giterr_set(GITERR_NET, "Remote error: %s", ((git_pkt_err *)pkt)->error);
64 git__free(pkt);
65 return -1;
66 }
67
68 if (pkt->type != GIT_PKT_FLUSH && git_vector_insert(refs, pkt) < 0)
69 return -1;
70
71 if (pkt->type == GIT_PKT_FLUSH) {
72 flush++;
73 git_pkt_free(pkt);
74 }
75 } while (flush < flushes);
76
77 return flush;
78}
79
8156835d
CMN
80static int append_symref(const char **out, git_vector *symrefs, const char *ptr)
81{
82 int error;
83 const char *end;
84 git_buf buf = GIT_BUF_INIT;
85 git_refspec *mapping;
86
87 ptr += strlen(GIT_CAP_SYMREF);
88 if (*ptr != '=')
89 goto on_invalid;
90
91 ptr++;
92 if (!(end = strchr(ptr, ' ')) &&
93 !(end = strchr(ptr, '\0')))
94 goto on_invalid;
95
96 if ((error = git_buf_put(&buf, ptr, end - ptr)) < 0)
97 return error;
98
99 /* symref mapping has refspec format */
100 mapping = git__malloc(sizeof(git_refspec));
101 GITERR_CHECK_ALLOC(mapping);
102
103 error = git_refspec__parse(mapping, git_buf_cstr(&buf), true);
104 git_buf_free(&buf);
105
106 /* if the error isn't OOM, then it's a parse error; let's use a nicer message */
107 if (error < 0) {
108 if (giterr_last()->klass != GITERR_NOMEMORY)
109 goto on_invalid;
110
111 return error;
112 }
113
114 if ((error = git_vector_insert(symrefs, mapping)) < 0)
115 return error;
116
117 *out = end;
118 return 0;
119
120on_invalid:
121 giterr_set(GITERR_NET, "remote sent invalid symref");
122 return -1;
123}
124
125int git_smart__detect_caps(git_pkt_ref *pkt, transport_smart_caps *caps, git_vector *symrefs)
41fb1ca0
PK
126{
127 const char *ptr;
128
129 /* No refs or capabilites, odd but not a problem */
130 if (pkt == NULL || pkt->capabilities == NULL)
131 return 0;
132
133 ptr = pkt->capabilities;
134 while (ptr != NULL && *ptr != '\0') {
135 if (*ptr == ' ')
136 ptr++;
137
613d5eb9 138 if (!git__prefixcmp(ptr, GIT_CAP_OFS_DELTA)) {
41fb1ca0
PK
139 caps->common = caps->ofs_delta = 1;
140 ptr += strlen(GIT_CAP_OFS_DELTA);
141 continue;
142 }
143
2f8c481c
CMN
144 /* Keep multi_ack_detailed before multi_ack */
145 if (!git__prefixcmp(ptr, GIT_CAP_MULTI_ACK_DETAILED)) {
146 caps->common = caps->multi_ack_detailed = 1;
147 ptr += strlen(GIT_CAP_MULTI_ACK_DETAILED);
148 continue;
149 }
150
613d5eb9 151 if (!git__prefixcmp(ptr, GIT_CAP_MULTI_ACK)) {
41fb1ca0
PK
152 caps->common = caps->multi_ack = 1;
153 ptr += strlen(GIT_CAP_MULTI_ACK);
154 continue;
155 }
156
613d5eb9 157 if (!git__prefixcmp(ptr, GIT_CAP_INCLUDE_TAG)) {
41fb1ca0
PK
158 caps->common = caps->include_tag = 1;
159 ptr += strlen(GIT_CAP_INCLUDE_TAG);
160 continue;
161 }
162
163 /* Keep side-band check after side-band-64k */
613d5eb9 164 if (!git__prefixcmp(ptr, GIT_CAP_SIDE_BAND_64K)) {
41fb1ca0
PK
165 caps->common = caps->side_band_64k = 1;
166 ptr += strlen(GIT_CAP_SIDE_BAND_64K);
167 continue;
168 }
169
613d5eb9 170 if (!git__prefixcmp(ptr, GIT_CAP_SIDE_BAND)) {
41fb1ca0
PK
171 caps->common = caps->side_band = 1;
172 ptr += strlen(GIT_CAP_SIDE_BAND);
173 continue;
174 }
175
613d5eb9
PK
176 if (!git__prefixcmp(ptr, GIT_CAP_DELETE_REFS)) {
177 caps->common = caps->delete_refs = 1;
178 ptr += strlen(GIT_CAP_DELETE_REFS);
179 continue;
180 }
181
b4342b11
CMN
182 if (!git__prefixcmp(ptr, GIT_CAP_THIN_PACK)) {
183 caps->common = caps->thin_pack = 1;
184 ptr += strlen(GIT_CAP_THIN_PACK);
185 continue;
186 }
187
8156835d
CMN
188 if (!git__prefixcmp(ptr, GIT_CAP_SYMREF)) {
189 int error;
190
191 if ((error = append_symref(&ptr, symrefs, ptr)) < 0)
192 return error;
193
194 continue;
195 }
196
41fb1ca0
PK
197 /* We don't know this capability, so skip it */
198 ptr = strchr(ptr, ' ');
199 }
200
201 return 0;
202}
203
204static int recv_pkt(git_pkt **out, gitno_buffer *buf)
205{
206 const char *ptr = buf->data, *line_end = ptr;
e583334c 207 git_pkt *pkt = NULL;
41fb1ca0
PK
208 int pkt_type, error = 0, ret;
209
210 do {
211 if (buf->offset > 0)
212 error = git_pkt_parse_line(&pkt, ptr, &line_end, buf->offset);
213 else
214 error = GIT_EBUFS;
215
216 if (error == 0)
217 break; /* return the pkt */
218
219 if (error < 0 && error != GIT_EBUFS)
80fc7d6b 220 return error;
41fb1ca0
PK
221
222 if ((ret = gitno_recv(buf)) < 0)
80fc7d6b 223 return ret;
41fb1ca0
PK
224 } while (error);
225
226 gitno_consume(buf, line_end);
227 pkt_type = pkt->type;
228 if (out != NULL)
229 *out = pkt;
230 else
231 git__free(pkt);
232
233 return pkt_type;
234}
235
236static int store_common(transport_smart *t)
237{
238 git_pkt *pkt = NULL;
239 gitno_buffer *buf = &t->buffer;
80fc7d6b 240 int error;
41fb1ca0
PK
241
242 do {
80fc7d6b
ET
243 if ((error = recv_pkt(&pkt, buf)) < 0)
244 return error;
41fb1ca0
PK
245
246 if (pkt->type == GIT_PKT_ACK) {
247 if (git_vector_insert(&t->common, pkt) < 0)
248 return -1;
249 } else {
250 git__free(pkt);
251 return 0;
252 }
253
254 } while (1);
255
256 return 0;
257}
258
259static int fetch_setup_walk(git_revwalk **out, git_repository *repo)
260{
261 git_revwalk *walk;
262 git_strarray refs;
263 unsigned int i;
264 git_reference *ref;
25e0b157 265 int error;
41fb1ca0 266
25e0b157
RB
267 if ((error = git_reference_list(&refs, repo)) < 0)
268 return error;
41fb1ca0 269
25e0b157
RB
270 if ((error = git_revwalk_new(&walk, repo)) < 0)
271 return error;
41fb1ca0
PK
272
273 git_revwalk_sorting(walk, GIT_SORT_TIME);
274
275 for (i = 0; i < refs.count; ++i) {
276 /* No tags */
277 if (!git__prefixcmp(refs.strings[i], GIT_REFS_TAGS_DIR))
278 continue;
279
25e0b157 280 if ((error = git_reference_lookup(&ref, repo, refs.strings[i])) < 0)
41fb1ca0
PK
281 goto on_error;
282
283 if (git_reference_type(ref) == GIT_REF_SYMBOLIC)
284 continue;
80fc7d6b 285
25e0b157 286 if ((error = git_revwalk_push(walk, git_reference_target(ref))) < 0)
41fb1ca0
PK
287 goto on_error;
288
289 git_reference_free(ref);
290 }
291
292 git_strarray_free(&refs);
293 *out = walk;
294 return 0;
295
296on_error:
297 git_reference_free(ref);
298 git_strarray_free(&refs);
25e0b157 299 return error;
41fb1ca0
PK
300}
301
2f8c481c
CMN
302static int wait_while_ack(gitno_buffer *buf)
303{
304 int error;
305 git_pkt_ack *pkt = NULL;
306
307 while (1) {
308 git__free(pkt);
309
310 if ((error = recv_pkt((git_pkt **)&pkt, buf)) < 0)
311 return error;
312
313 if (pkt->type == GIT_PKT_NAK)
314 break;
315
316 if (pkt->type == GIT_PKT_ACK &&
317 (pkt->status != GIT_ACK_CONTINUE ||
318 pkt->status != GIT_ACK_COMMON)) {
319 git__free(pkt);
7616b8d3 320 return 0;
2f8c481c
CMN
321 }
322 }
323
324 git__free(pkt);
325 return 0;
326}
327
af613ecd 328int git_smart__negotiate_fetch(git_transport *transport, git_repository *repo, const git_remote_head * const *wants, size_t count)
41fb1ca0
PK
329{
330 transport_smart *t = (transport_smart *)transport;
331 gitno_buffer *buf = &t->buffer;
332 git_buf data = GIT_BUF_INIT;
333 git_revwalk *walk = NULL;
334 int error = -1, pkt_type;
335 unsigned int i;
336 git_oid oid;
337
af613ecd 338 if ((error = git_pkt_buffer_wants(wants, count, &t->caps, &data)) < 0)
77844988 339 return error;
41fb1ca0 340
77844988 341 if ((error = fetch_setup_walk(&walk, repo)) < 0)
41fb1ca0 342 goto on_error;
a7382aa2 343
41fb1ca0 344 /*
a7382aa2
CMN
345 * Our support for ACK extensions is simply to parse them. On
346 * the first ACK we will accept that as enough common
347 * objects. We give up if we haven't found an answer in the
348 * first 256 we send.
41fb1ca0
PK
349 */
350 i = 0;
a7382aa2 351 while (i < 256) {
77844988
PK
352 error = git_revwalk_next(&oid, walk);
353
354 if (error < 0) {
355 if (GIT_ITEROVER == error)
356 break;
357
358 goto on_error;
359 }
360
41fb1ca0
PK
361 git_pkt_buffer_have(&oid, &data);
362 i++;
363 if (i % 20 == 0) {
364 if (t->cancelled.val) {
365 giterr_set(GITERR_NET, "The fetch was cancelled by the user");
366 error = GIT_EUSER;
367 goto on_error;
368 }
369
370 git_pkt_buffer_flush(&data);
77844988
PK
371 if (git_buf_oom(&data)) {
372 error = -1;
41fb1ca0 373 goto on_error;
77844988 374 }
41fb1ca0 375
77844988 376 if ((error = git_smart__negotiation_step(&t->parent, data.ptr, data.size)) < 0)
41fb1ca0
PK
377 goto on_error;
378
379 git_buf_clear(&data);
2f8c481c 380 if (t->caps.multi_ack || t->caps.multi_ack_detailed) {
77844988 381 if ((error = store_common(t)) < 0)
41fb1ca0
PK
382 goto on_error;
383 } else {
384 pkt_type = recv_pkt(NULL, buf);
385
386 if (pkt_type == GIT_PKT_ACK) {
387 break;
388 } else if (pkt_type == GIT_PKT_NAK) {
389 continue;
77844988
PK
390 } else if (pkt_type < 0) {
391 /* recv_pkt returned an error */
392 error = pkt_type;
393 goto on_error;
41fb1ca0
PK
394 } else {
395 giterr_set(GITERR_NET, "Unexpected pkt type");
77844988 396 error = -1;
41fb1ca0
PK
397 goto on_error;
398 }
399 }
400 }
401
402 if (t->common.length > 0)
403 break;
404
405 if (i % 20 == 0 && t->rpc) {
406 git_pkt_ack *pkt;
407 unsigned int i;
408
af613ecd 409 if ((error = git_pkt_buffer_wants(wants, count, &t->caps, &data)) < 0)
41fb1ca0
PK
410 goto on_error;
411
412 git_vector_foreach(&t->common, i, pkt) {
77844988
PK
413 if ((error = git_pkt_buffer_have(&pkt->oid, &data)) < 0)
414 goto on_error;
41fb1ca0
PK
415 }
416
77844988
PK
417 if (git_buf_oom(&data)) {
418 error = -1;
41fb1ca0 419 goto on_error;
77844988 420 }
41fb1ca0
PK
421 }
422 }
423
41fb1ca0
PK
424 /* Tell the other end that we're done negotiating */
425 if (t->rpc && t->common.length > 0) {
426 git_pkt_ack *pkt;
427 unsigned int i;
428
af613ecd 429 if ((error = git_pkt_buffer_wants(wants, count, &t->caps, &data)) < 0)
41fb1ca0
PK
430 goto on_error;
431
432 git_vector_foreach(&t->common, i, pkt) {
77844988
PK
433 if ((error = git_pkt_buffer_have(&pkt->oid, &data)) < 0)
434 goto on_error;
41fb1ca0
PK
435 }
436
77844988
PK
437 if (git_buf_oom(&data)) {
438 error = -1;
41fb1ca0 439 goto on_error;
77844988 440 }
41fb1ca0
PK
441 }
442
77844988
PK
443 if ((error = git_pkt_buffer_done(&data)) < 0)
444 goto on_error;
445
41fb1ca0
PK
446 if (t->cancelled.val) {
447 giterr_set(GITERR_NET, "The fetch was cancelled by the user");
448 error = GIT_EUSER;
449 goto on_error;
450 }
77844988 451 if ((error = git_smart__negotiation_step(&t->parent, data.ptr, data.size)) < 0)
41fb1ca0
PK
452 goto on_error;
453
454 git_buf_free(&data);
455 git_revwalk_free(walk);
456
457 /* Now let's eat up whatever the server gives us */
2f8c481c 458 if (!t->caps.multi_ack && !t->caps.multi_ack_detailed) {
41fb1ca0 459 pkt_type = recv_pkt(NULL, buf);
77844988
PK
460
461 if (pkt_type < 0) {
462 return pkt_type;
463 } else if (pkt_type != GIT_PKT_ACK && pkt_type != GIT_PKT_NAK) {
41fb1ca0
PK
464 giterr_set(GITERR_NET, "Unexpected pkt type");
465 return -1;
466 }
467 } else {
2f8c481c 468 error = wait_while_ack(buf);
41fb1ca0
PK
469 }
470
2f8c481c 471 return error;
41fb1ca0
PK
472
473on_error:
474 git_revwalk_free(walk);
475 git_buf_free(&data);
476 return error;
477}
478
09cc0b92 479static int no_sideband(transport_smart *t, struct git_odb_writepack *writepack, gitno_buffer *buf, git_transfer_progress *stats)
41fb1ca0
PK
480{
481 int recvd;
482
483 do {
484 if (t->cancelled.val) {
485 giterr_set(GITERR_NET, "The fetch was cancelled by the user");
486 return GIT_EUSER;
487 }
488
a6154f21 489 if (writepack->append(writepack, buf->data, buf->offset, stats) < 0)
41fb1ca0
PK
490 return -1;
491
492 gitno_consume_n(buf, buf->offset);
493
494 if ((recvd = gitno_recv(buf)) < 0)
80fc7d6b 495 return recvd;
41fb1ca0
PK
496 } while(recvd > 0);
497
80fc7d6b 498 if (writepack->commit(writepack, stats) < 0)
41fb1ca0
PK
499 return -1;
500
501 return 0;
502}
503
a8122b5d 504struct network_packetsize_payload
41fb1ca0 505{
48e60ae7 506 git_transfer_progress_cb callback;
41fb1ca0
PK
507 void *payload;
508 git_transfer_progress *stats;
438906e1 509 size_t last_fired_bytes;
41fb1ca0
PK
510};
511
5b188225 512static int network_packetsize(size_t received, void *payload)
41fb1ca0
PK
513{
514 struct network_packetsize_payload *npp = (struct network_packetsize_payload*)payload;
515
516 /* Accumulate bytes */
517 npp->stats->received_bytes += received;
518
519 /* Fire notification if the threshold is reached */
520 if ((npp->stats->received_bytes - npp->last_fired_bytes) > NETWORK_XFER_THRESHOLD) {
a8122b5d 521 npp->last_fired_bytes = npp->stats->received_bytes;
5b188225 522
7baa7631 523 if (npp->callback(npp->stats, npp->payload))
5b188225 524 return GIT_EUSER;
41fb1ca0 525 }
5b188225
JM
526
527 return 0;
41fb1ca0
PK
528}
529
530int git_smart__download_pack(
531 git_transport *transport,
532 git_repository *repo,
533 git_transfer_progress *stats,
48e60ae7 534 git_transfer_progress_cb transfer_progress_cb,
41fb1ca0
PK
535 void *progress_payload)
536{
537 transport_smart *t = (transport_smart *)transport;
41fb1ca0 538 gitno_buffer *buf = &t->buffer;
09cc0b92
ET
539 git_odb *odb;
540 struct git_odb_writepack *writepack = NULL;
5b188225 541 int error = 0;
41fb1ca0
PK
542 struct network_packetsize_payload npp = {0};
543
438906e1
PK
544 memset(stats, 0, sizeof(git_transfer_progress));
545
9effa2fb
JG
546 if (transfer_progress_cb) {
547 npp.callback = transfer_progress_cb;
41fb1ca0
PK
548 npp.payload = progress_payload;
549 npp.stats = stats;
550 t->packetsize_cb = &network_packetsize;
551 t->packetsize_payload = &npp;
438906e1
PK
552
553 /* We might have something in the buffer already from negotiate_fetch */
5b188225 554 if (t->buffer.offset > 0 && !t->cancelled.val)
7baa7631 555 if (t->packetsize_cb(t->buffer.offset, t->packetsize_payload))
5b188225 556 git_atomic_set(&t->cancelled, 1);
41fb1ca0
PK
557 }
558
09cc0b92 559 if ((error = git_repository_odb__weakptr(&odb, repo)) < 0 ||
9effa2fb 560 ((error = git_odb_write_pack(&writepack, odb, transfer_progress_cb, progress_payload)) != 0))
5b188225 561 goto done;
41fb1ca0 562
41fb1ca0
PK
563 /*
564 * If the remote doesn't support the side-band, we can feed
09cc0b92 565 * the data directly to the pack writer. Otherwise, we need to
41fb1ca0
PK
566 * check which one belongs there.
567 */
568 if (!t->caps.side_band && !t->caps.side_band_64k) {
5b188225
JM
569 error = no_sideband(t, writepack, buf, stats);
570 goto done;
41fb1ca0
PK
571 }
572
573 do {
9effa2fb 574 git_pkt *pkt = NULL;
41fb1ca0 575
5b188225 576 /* Check cancellation before network call */
41fb1ca0 577 if (t->cancelled.val) {
db4cbfe5 578 giterr_clear();
41fb1ca0 579 error = GIT_EUSER;
5b188225 580 goto done;
41fb1ca0
PK
581 }
582
9effa2fb
JG
583 if ((error = recv_pkt(&pkt, buf)) >= 0) {
584 /* Check cancellation after network call */
585 if (t->cancelled.val) {
586 giterr_clear();
587 error = GIT_EUSER;
588 } else if (pkt->type == GIT_PKT_PROGRESS) {
589 if (t->progress_cb) {
590 git_pkt_progress *p = (git_pkt_progress *) pkt;
591 error = t->progress_cb(p->data, p->len, t->message_cb_payload);
592 }
593 } else if (pkt->type == GIT_PKT_DATA) {
594 git_pkt_data *p = (git_pkt_data *) pkt;
595 error = writepack->append(writepack, p->data, p->len, stats);
596 } else if (pkt->type == GIT_PKT_FLUSH) {
597 /* A flush indicates the end of the packfile */
598 git__free(pkt);
599 break;
600 }
5b188225 601 }
41fb1ca0 602
9effa2fb
JG
603 git__free(pkt);
604 if (error < 0)
605 goto done;
41fb1ca0 606
41fb1ca0
PK
607 } while (1);
608
db4cbfe5 609 /*
9effa2fb 610 * Trailing execution of transfer_progress_cb, if necessary...
db4cbfe5
JM
611 * Only the callback through the npp datastructure currently
612 * updates the last_fired_bytes value. It is possible that
25e0b157 613 * progress has already been reported with the correct
db4cbfe5
JM
614 * "received_bytes" value, but until (if?) this is unified
615 * then we will report progress again to be sure that the
616 * correct last received_bytes value is reported.
617 */
618 if (npp.callback && npp.stats->received_bytes > npp.last_fired_bytes) {
25e0b157
RB
619 error = npp.callback(npp.stats, npp.payload);
620 if (error != 0)
db4cbfe5 621 goto done;
db4cbfe5
JM
622 }
623
7baa7631 624 error = writepack->commit(writepack, stats);
41fb1ca0 625
5b188225 626done:
613d5eb9
PK
627 if (writepack)
628 writepack->free(writepack);
9effa2fb 629 if (transfer_progress_cb) {
300f4412
M
630 t->packetsize_cb = NULL;
631 t->packetsize_payload = NULL;
632 }
438906e1 633
41fb1ca0
PK
634 return error;
635}
613d5eb9
PK
636
637static int gen_pktline(git_buf *buf, git_push *push)
638{
613d5eb9 639 push_spec *spec;
4128f5aa
CW
640 size_t i, len;
641 char old_id[41], new_id[41];
642
643 old_id[40] = '\0'; new_id[40] = '\0';
613d5eb9
PK
644
645 git_vector_foreach(&push->specs, i, spec) {
4128f5aa 646 len = 2*GIT_OID_HEXSZ + 7 + strlen(spec->rref);
613d5eb9
PK
647
648 if (i == 0) {
4128f5aa 649 ++len; /* '\0' */
613d5eb9 650 if (push->report_status)
b8c32580
PK
651 len += strlen(GIT_CAP_REPORT_STATUS) + 1;
652 len += strlen(GIT_CAP_SIDE_BAND_64K) + 1;
613d5eb9
PK
653 }
654
4128f5aa
CW
655 git_oid_fmt(old_id, &spec->roid);
656 git_oid_fmt(new_id, &spec->loid);
613d5eb9 657
47fc2642 658 git_buf_printf(buf, "%04"PRIxZ"%s %s %s", len, old_id, new_id, spec->rref);
613d5eb9
PK
659
660 if (i == 0) {
661 git_buf_putc(buf, '\0');
b8c32580
PK
662 /* Core git always starts their capabilities string with a space */
663 if (push->report_status) {
664 git_buf_putc(buf, ' ');
613d5eb9 665 git_buf_printf(buf, GIT_CAP_REPORT_STATUS);
b8c32580
PK
666 }
667 git_buf_putc(buf, ' ');
668 git_buf_printf(buf, GIT_CAP_SIDE_BAND_64K);
613d5eb9
PK
669 }
670
671 git_buf_putc(buf, '\n');
672 }
4128f5aa 673
613d5eb9
PK
674 git_buf_puts(buf, "0000");
675 return git_buf_oom(buf) ? -1 : 0;
676}
677
b8c32580
PK
678static int add_push_report_pkt(git_push *push, git_pkt *pkt)
679{
680 push_status *status;
681
682 switch (pkt->type) {
683 case GIT_PKT_OK:
8b686b31 684 status = git__calloc(sizeof(push_status), 1);
b8c32580
PK
685 GITERR_CHECK_ALLOC(status);
686 status->msg = NULL;
687 status->ref = git__strdup(((git_pkt_ok *)pkt)->ref);
688 if (!status->ref ||
689 git_vector_insert(&push->status, status) < 0) {
690 git_push_status_free(status);
691 return -1;
692 }
693 break;
694 case GIT_PKT_NG:
695 status = git__calloc(sizeof(push_status), 1);
696 GITERR_CHECK_ALLOC(status);
697 status->ref = git__strdup(((git_pkt_ng *)pkt)->ref);
698 status->msg = git__strdup(((git_pkt_ng *)pkt)->msg);
699 if (!status->ref || !status->msg ||
700 git_vector_insert(&push->status, status) < 0) {
701 git_push_status_free(status);
702 return -1;
703 }
704 break;
705 case GIT_PKT_UNPACK:
706 push->unpack_ok = ((git_pkt_unpack *)pkt)->unpack_ok;
707 break;
708 case GIT_PKT_FLUSH:
709 return GIT_ITEROVER;
710 default:
711 giterr_set(GITERR_NET, "report-status: protocol error");
712 return -1;
713 }
714
715 return 0;
716}
717
718static int add_push_report_sideband_pkt(git_push *push, git_pkt_data *data_pkt)
719{
720 git_pkt *pkt;
721 const char *line = data_pkt->data, *line_end;
722 size_t line_len = data_pkt->len;
723 int error;
724
725 while (line_len > 0) {
726 error = git_pkt_parse_line(&pkt, line, &line_end, line_len);
727
728 if (error < 0)
729 return error;
730
731 /* Advance in the buffer */
732 line_len -= (line_end - line);
733 line = line_end;
734
735 error = add_push_report_pkt(push, pkt);
736
737 git_pkt_free(pkt);
738
f5898324 739 if (error < 0 && error != GIT_ITEROVER)
b8c32580
PK
740 return error;
741 }
742
743 return 0;
744}
745
9effa2fb 746static int parse_report(transport_smart *transport, git_push *push)
613d5eb9 747{
e583334c
L
748 git_pkt *pkt = NULL;
749 const char *line_end = NULL;
9effa2fb 750 gitno_buffer *buf = &transport->buffer;
613d5eb9
PK
751 int error, recvd;
752
753 for (;;) {
754 if (buf->offset > 0)
755 error = git_pkt_parse_line(&pkt, buf->data,
756 &line_end, buf->offset);
757 else
758 error = GIT_EBUFS;
759
760 if (error < 0 && error != GIT_EBUFS)
761 return -1;
762
763 if (error == GIT_EBUFS) {
764 if ((recvd = gitno_recv(buf)) < 0)
80fc7d6b 765 return recvd;
613d5eb9
PK
766
767 if (recvd == 0) {
768 giterr_set(GITERR_NET, "Early EOF");
769 return -1;
770 }
771 continue;
772 }
773
774 gitno_consume(buf, line_end);
775
b8c32580 776 error = 0;
613d5eb9 777
b8c32580
PK
778 switch (pkt->type) {
779 case GIT_PKT_DATA:
780 /* This is a sideband packet which contains other packets */
781 error = add_push_report_sideband_pkt(push, (git_pkt_data *)pkt);
782 break;
783 case GIT_PKT_ERR:
784 giterr_set(GITERR_NET, "report-status: Error reported: %s",
785 ((git_pkt_err *)pkt)->error);
786 error = -1;
787 break;
788 case GIT_PKT_PROGRESS:
9effa2fb
JG
789 if (transport->progress_cb) {
790 git_pkt_progress *p = (git_pkt_progress *) pkt;
791 error = transport->progress_cb(p->data, p->len, transport->message_cb_payload);
792 }
b8c32580
PK
793 break;
794 default:
795 error = add_push_report_pkt(push, pkt);
796 break;
613d5eb9
PK
797 }
798
b8c32580 799 git_pkt_free(pkt);
613d5eb9 800
b8c32580 801 /* add_push_report_pkt returns GIT_ITEROVER when it receives a flush */
f5898324 802 if (error == GIT_ITEROVER)
613d5eb9 803 return 0;
613d5eb9 804
b8c32580
PK
805 if (error < 0)
806 return error;
613d5eb9
PK
807 }
808}
809
df93a681
PK
810static int add_ref_from_push_spec(git_vector *refs, push_spec *push_spec)
811{
812 git_pkt_ref *added = git__calloc(1, sizeof(git_pkt_ref));
813 GITERR_CHECK_ALLOC(added);
814
815 added->type = GIT_PKT_REF;
816 git_oid_cpy(&added->head.oid, &push_spec->loid);
817 added->head.name = git__strdup(push_spec->rref);
818
819 if (!added->head.name ||
820 git_vector_insert(refs, added) < 0) {
821 git_pkt_free((git_pkt *)added);
822 return -1;
823 }
824
825 return 0;
826}
827
828static int update_refs_from_report(
829 git_vector *refs,
830 git_vector *push_specs,
831 git_vector *push_report)
832{
833 git_pkt_ref *ref;
834 push_spec *push_spec;
a150cc87 835 push_status *push_status;
df93a681
PK
836 size_t i, j, refs_len;
837 int cmp;
838
839 /* For each push spec we sent to the server, we should have
840 * gotten back a status packet in the push report */
841 if (push_specs->length != push_report->length) {
842 giterr_set(GITERR_NET, "report-status: protocol error");
843 return -1;
844 }
845
846 /* We require that push_specs be sorted with push_spec_rref_cmp,
847 * and that push_report be sorted with push_status_ref_cmp */
848 git_vector_sort(push_specs);
849 git_vector_sort(push_report);
850
851 git_vector_foreach(push_specs, i, push_spec) {
852 push_status = git_vector_get(push_report, i);
853
854 /* For each push spec we sent to the server, we should have
855 * gotten back a status packet in the push report which matches */
856 if (strcmp(push_spec->rref, push_status->ref)) {
857 giterr_set(GITERR_NET, "report-status: protocol error");
858 return -1;
859 }
860 }
861
862 /* We require that refs be sorted with ref_name_cmp */
863 git_vector_sort(refs);
864 i = j = 0;
865 refs_len = refs->length;
866
867 /* Merge join push_specs with refs */
868 while (i < push_specs->length && j < refs_len) {
869 push_spec = git_vector_get(push_specs, i);
a150cc87 870 push_status = git_vector_get(push_report, i);
df93a681
PK
871 ref = git_vector_get(refs, j);
872
873 cmp = strcmp(push_spec->rref, ref->head.name);
874
875 /* Iterate appropriately */
876 if (cmp <= 0) i++;
877 if (cmp >= 0) j++;
878
879 /* Add case */
880 if (cmp < 0 &&
881 !push_status->msg &&
882 add_ref_from_push_spec(refs, push_spec) < 0)
883 return -1;
884
885 /* Update case, delete case */
886 if (cmp == 0 &&
887 !push_status->msg)
888 git_oid_cpy(&ref->head.oid, &push_spec->loid);
889 }
890
891 for (; i < push_specs->length; i++) {
892 push_spec = git_vector_get(push_specs, i);
a150cc87 893 push_status = git_vector_get(push_report, i);
df93a681
PK
894
895 /* Add case */
896 if (!push_status->msg &&
897 add_ref_from_push_spec(refs, push_spec) < 0)
898 return -1;
899 }
900
901 /* Remove any refs which we updated to have a zero OID. */
902 git_vector_rforeach(refs, i, ref) {
903 if (git_oid_iszero(&ref->head.oid)) {
904 git_vector_remove(refs, i);
905 git_pkt_free((git_pkt *)ref);
906 }
907 }
908
909 git_vector_sort(refs);
910
911 return 0;
912}
913
b176eded
JM
914struct push_packbuilder_payload
915{
916 git_smart_subtransport_stream *stream;
917 git_packbuilder *pb;
918 git_push_transfer_progress cb;
919 void *cb_payload;
920 size_t last_bytes;
921 double last_progress_report_time;
922};
923
613d5eb9
PK
924static int stream_thunk(void *buf, size_t size, void *data)
925{
b176eded
JM
926 int error = 0;
927 struct push_packbuilder_payload *payload = data;
928
929 if ((error = payload->stream->write(payload->stream, (const char *)buf, size)) < 0)
930 return error;
931
932 if (payload->cb) {
933 double current_time = git__timer();
934 payload->last_bytes += size;
613d5eb9 935
b176eded
JM
936 if ((current_time - payload->last_progress_report_time) >= MIN_PROGRESS_UPDATE_INTERVAL) {
937 payload->last_progress_report_time = current_time;
4f62163e 938 error = payload->cb(payload->pb->nr_written, payload->pb->nr_objects, payload->last_bytes, payload->cb_payload);
b176eded
JM
939 }
940 }
941
942 return error;
613d5eb9
PK
943}
944
945int git_smart__push(git_transport *transport, git_push *push)
946{
947 transport_smart *t = (transport_smart *)transport;
b176eded 948 struct push_packbuilder_payload packbuilder_payload = {0};
613d5eb9 949 git_buf pktline = GIT_BUF_INIT;
5b188225 950 int error = 0, need_pack = 0;
51e4da6d
CMN
951 push_spec *spec;
952 unsigned int i;
613d5eb9 953
b176eded
JM
954 packbuilder_payload.pb = push->pb;
955
956 if (push->transfer_progress_cb) {
957 packbuilder_payload.cb = push->transfer_progress_cb;
958 packbuilder_payload.cb_payload = push->transfer_progress_cb_payload;
959 }
960
613d5eb9
PK
961#ifdef PUSH_DEBUG
962{
963 git_remote_head *head;
613d5eb9
PK
964 char hex[41]; hex[40] = '\0';
965
966 git_vector_foreach(&push->remote->refs, i, head) {
967 git_oid_fmt(hex, &head->oid);
968 fprintf(stderr, "%s (%s)\n", hex, head->name);
969 }
970
971 git_vector_foreach(&push->specs, i, spec) {
972 git_oid_fmt(hex, &spec->roid);
973 fprintf(stderr, "%s (%s) -> ", hex, spec->lref);
974 git_oid_fmt(hex, &spec->loid);
975 fprintf(stderr, "%s (%s)\n", hex, spec->rref ?
976 spec->rref : spec->lref);
977 }
978}
979#endif
980
51e4da6d
CMN
981 /*
982 * Figure out if we need to send a packfile; which is in all
983 * cases except when we only send delete commands
984 */
985 git_vector_foreach(&push->specs, i, spec) {
986 if (spec->lref) {
987 need_pack = 1;
988 break;
989 }
990 }
991
5b188225
JM
992 if ((error = git_smart__get_push_stream(t, &packbuilder_payload.stream)) < 0 ||
993 (error = gen_pktline(&pktline, push)) < 0 ||
994 (error = packbuilder_payload.stream->write(packbuilder_payload.stream, git_buf_cstr(&pktline), git_buf_len(&pktline))) < 0)
995 goto done;
51e4da6d 996
5b188225
JM
997 if (need_pack &&
998 (error = git_packbuilder_foreach(push->pb, &stream_thunk, &packbuilder_payload)) < 0)
999 goto done;
613d5eb9
PK
1000
1001 /* If we sent nothing or the server doesn't support report-status, then
1002 * we consider the pack to have been unpacked successfully */
1003 if (!push->specs.length || !push->report_status)
1004 push->unpack_ok = 1;
9effa2fb 1005 else if ((error = parse_report(t, push)) < 0)
5b188225 1006 goto done;
613d5eb9 1007
b176eded
JM
1008 /* If progress is being reported write the final report */
1009 if (push->transfer_progress_cb) {
4f62163e
JG
1010 error = push->transfer_progress_cb(
1011 push->pb->nr_written,
1012 push->pb->nr_objects,
1013 packbuilder_payload.last_bytes,
1014 push->transfer_progress_cb_payload);
1015
1016 if (error < 0)
1017 goto done;
b176eded
JM
1018 }
1019
a6192d7c 1020 if (push->status.length) {
7baa7631 1021 error = update_refs_from_report(&t->refs, &push->specs, &push->status);
a6192d7c
CMN
1022 if (error < 0)
1023 goto done;
1024
8156835d 1025 error = git_smart__update_heads(t, NULL);
a6192d7c 1026 }
613d5eb9 1027
5b188225 1028done:
613d5eb9 1029 git_buf_free(&pktline);
613d5eb9
PK
1030 return error;
1031}