interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / send-pack.c

782 lines · 21165 bytes Raw Blame History
1#include "git-compat-util.h"
2#include "config.h"
3#include "commit.h"
4#include "date.h"
5#include "gettext.h"
6#include "hex.h"
7#include "odb.h"
8#include "pkt-line.h"
9#include "sideband.h"
10#include "run-command.h"
11#include "remote.h"
12#include "connect.h"
13#include "send-pack.h"
14#include "transport.h"
15#include "version.h"
16#include "oid-array.h"
17#include "gpg-interface.h"
18#include "shallow.h"
19#include "parse-options.h"
20#include "trace2.h"
21#include "write-or-die.h"
22
23int option_parse_push_signed(const struct option *opt,
24 const char *arg, int unset)
25{
26 if (unset) {
27 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_NEVER;
28 return 0;
29 }
30 switch (git_parse_maybe_bool(arg)) {
31 case 1:
32 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_ALWAYS;
33 return 0;
34 case 0:
35 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_NEVER;
36 return 0;
37 }
38 if (!strcasecmp("if-asked", arg)) {
39 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_IF_ASKED;
40 return 0;
41 }
42 die("bad %s argument: %s", opt->long_name, arg);
43}
44
45static void append_negative_object(struct repository *r,
46 struct oid_array *haves,
47 const struct object_id *oid)
48{
49 /*
50 * The remote end may have advertised objects that we do not have in
51 * our object database. Skip those, as we cannot use them as boundary.
52 */
53 if (!odb_has_object(r->objects, oid, 0))
54 return;
55 oid_array_append(haves, oid);
56}
57
58/*
59 * Make a pack stream and spit it out into file descriptor fd
60 */
61static int pack_objects(struct repository *r,
62 int fd, struct ref *refs, struct oid_array *advertised,
63 struct oid_array *negotiated,
64 struct send_pack_args *args)
65{
66 struct odb_generate_pack_options opts = ODB_GENERATE_PACK_OPTIONS_INIT;
67 struct odb_pack_generator *generator;
68 int rc;
69
70 trace2_region_enter("send_pack", "pack_objects", r);
71
72 opts.thin = args->use_thin_pack;
73 opts.ofs_delta = args->use_ofs_delta;
74 if (args->progress)
75 opts.progress = ODB_GENERATE_PACK_PROGRESS_VERBOSE;
76 opts.shallow = is_repository_shallow(r);
77 opts.disable_bitmaps = args->disable_bitmaps;
78
79 /*
80 * The pack is either written directly to the remote's descriptor, or,
81 * in the case of a stateless RPC, read back from a pipe so that we
82 * can wrap the pack data into pkt-lines.
83 */
84 opts.pack_fd = args->stateless_rpc ? -1 : fd;
85
86 for (size_t i = 0; i < advertised->nr; i++)
87 append_negative_object(r, &opts.haves, &advertised->oid[i]);
88 for (size_t i = 0; i < negotiated->nr; i++)
89 append_negative_object(r, &opts.haves, &negotiated->oid[i]);
90
91 while (refs) {
92 if (!is_null_oid(&refs->old_oid))
93 append_negative_object(r, &opts.haves, &refs->old_oid);
94 if (!is_null_oid(&refs->new_oid))
95 oid_array_append(&opts.wants, &refs->new_oid);
96 refs = refs->next;
97 }
98
99 if (odb_generate_pack(r->objects, &generator, &opts))
100 die("git pack-objects failed");
101 odb_generate_pack_options_release(&opts);
102
103 if (args->stateless_rpc) {
104 char *buf = xmalloc(LARGE_PACKET_MAX);
105 while (1) {
106 ssize_t n = xread(generator->out, buf, LARGE_PACKET_MAX);
107 if (n <= 0)
108 break;
109 send_sideband(fd, -1, buf, n, LARGE_PACKET_MAX);
110 }
111 free(buf);
112 close(generator->out);
113 }
114
115 rc = odb_pack_generator_finish(generator);
116 trace2_region_leave("send_pack", "pack_objects", r);
117 return rc;
118}
119
120static int receive_unpack_status(struct packet_reader *reader)
121{
122 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
123 return error(_("unexpected flush packet while reading remote unpack status"));
124 if (!skip_prefix(reader->line, "unpack ", &reader->line))
125 return error(_("unable to parse remote unpack status: %s"), reader->line);
126 if (strcmp(reader->line, "ok"))
127 return error(_("remote unpack failed: %s"), reader->line);
128 return 0;
129}
130
131static int receive_status(struct repository *r,
132 struct packet_reader *reader, struct ref *refs)
133{
134 struct ref *hint;
135 int ret;
136 struct ref_push_report *report = NULL;
137 int new_report = 0;
138 int once = 0;
139
140 trace2_region_enter("send_pack", "receive_status", r);
141 hint = NULL;
142 ret = receive_unpack_status(reader);
143 while (1) {
144 struct object_id old_oid, new_oid;
145 char *head;
146 char *refname;
147 char *p;
148 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
149 break;
150 head = reader->line;
151 p = strchr(head, ' ');
152 if (!p) {
153 error("invalid status line from remote: %s", reader->line);
154 ret = -1;
155 break;
156 }
157 *p++ = '\0';
158
159 if (!strcmp(head, "option")) {
160 char *key;
161 const char *val;
162
163 if (!hint || !(report || new_report)) {
164 if (!once++)
165 error("'option' without a matching 'ok/ng' directive");
166 ret = -1;
167 continue;
168 }
169 if (new_report) {
170 if (!hint->report) {
171 CALLOC_ARRAY(hint->report, 1);
172 report = hint->report;
173 } else {
174 report = hint->report;
175 while (report->next)
176 report = report->next;
177 CALLOC_ARRAY(report->next, 1);
178 report = report->next;
179 }
180 new_report = 0;
181 }
182 key = p;
183 p = strchr(key, ' ');
184 if (p)
185 *p++ = '\0';
186 val = p;
187 if (!strcmp(key, "refname"))
188 report->ref_name = xstrdup_or_null(val);
189 else if (!strcmp(key, "old-oid") && val &&
190 !parse_oid_hex_algop(val, &old_oid, &val, r->hash_algo))
191 report->old_oid = oiddup(&old_oid);
192 else if (!strcmp(key, "new-oid") && val &&
193 !parse_oid_hex_algop(val, &new_oid, &val, r->hash_algo))
194 report->new_oid = oiddup(&new_oid);
195 else if (!strcmp(key, "forced-update"))
196 report->forced_update = 1;
197 continue;
198 }
199
200 report = NULL;
201 new_report = 0;
202 if (strcmp(head, "ok") && strcmp(head, "ng")) {
203 error("invalid ref status from remote: %s", head);
204 ret = -1;
205 break;
206 }
207 refname = p;
208 p = strchr(refname, ' ');
209 if (p)
210 *p++ = '\0';
211 /* first try searching at our hint, falling back to all refs */
212 if (hint)
213 hint = find_ref_by_name(hint, refname);
214 if (!hint)
215 hint = find_ref_by_name(refs, refname);
216 if (!hint) {
217 warning("remote reported status on unknown ref: %s",
218 refname);
219 continue;
220 }
221 if (hint->status != REF_STATUS_EXPECTING_REPORT &&
222 hint->status != REF_STATUS_OK &&
223 hint->status != REF_STATUS_REMOTE_REJECT) {
224 warning("remote reported status on unexpected ref: %s",
225 refname);
226 continue;
227 }
228
229 /*
230 * Clients sending duplicate refs can cause the same value
231 * to be overridden, causing a memory leak.
232 */
233 free(hint->remote_status);
234
235 if (!strcmp(head, "ng")) {
236 hint->status = REF_STATUS_REMOTE_REJECT;
237 if (p)
238 hint->remote_status = xstrdup(p);
239 else
240 hint->remote_status = xstrdup("failed");
241 } else {
242 hint->status = REF_STATUS_OK;
243 hint->remote_status = xstrdup_or_null(p);
244 new_report = 1;
245 }
246 }
247 trace2_region_leave("send_pack", "receive_status", r);
248 return ret;
249}
250
251static int sideband_demux(int in UNUSED, int out, void *data)
252{
253 int *fd = data, ret;
254 if (async_with_fork())
255 close(fd[1]);
256 ret = recv_sideband("send-pack", fd[0], out);
257 close(out);
258 return ret;
259}
260
261static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
262{
263 struct strbuf *sb = cb;
264 if (graft->nr_parent == -1)
265 packet_buf_write(sb, "shallow %s\n", oid_to_hex(&graft->oid));
266 return 0;
267}
268
269static void advertise_shallow_grafts_buf(struct repository *r, struct strbuf *sb)
270{
271 if (!is_repository_shallow(r))
272 return;
273 for_each_commit_graft(advertise_shallow_grafts_cb, sb);
274}
275
276#define CHECK_REF_NO_PUSH -1
277#define CHECK_REF_STATUS_REJECTED -2
278#define CHECK_REF_UPTODATE -3
279static int check_to_send_update(const struct ref *ref, const struct send_pack_args *args)
280{
281 if (!ref->peer_ref && !args->send_mirror)
282 return CHECK_REF_NO_PUSH;
283
284 /* Check for statuses set by set_ref_status_for_push() */
285 switch (ref->status) {
286 case REF_STATUS_REJECT_NONFASTFORWARD:
287 case REF_STATUS_REJECT_ALREADY_EXISTS:
288 case REF_STATUS_REJECT_FETCH_FIRST:
289 case REF_STATUS_REJECT_NEEDS_FORCE:
290 case REF_STATUS_REJECT_STALE:
291 case REF_STATUS_REJECT_REMOTE_UPDATED:
292 case REF_STATUS_REJECT_NODELETE:
293 return CHECK_REF_STATUS_REJECTED;
294 case REF_STATUS_UPTODATE:
295 return CHECK_REF_UPTODATE;
296
297 default:
298 case REF_STATUS_EXPECTING_REPORT:
299 /* already passed checks on the local side */
300 case REF_STATUS_OK:
301 /* of course this is OK */
302 return 0;
303 }
304}
305
306/*
307 * the beginning of the next line, or the end of buffer.
308 *
309 * NEEDSWORK: perhaps move this to git-compat-util.h or somewhere and
310 * convert many similar uses found by "git grep -A4 memchr".
311 */
312static const char *next_line(const char *line, size_t len)
313{
314 const char *nl = memchr(line, '\n', len);
315 if (!nl)
316 return line + len; /* incomplete line */
317 return nl + 1;
318}
319
320static int generate_push_cert(struct strbuf *req_buf,
321 const struct ref *remote_refs,
322 struct send_pack_args *args,
323 const char *cap_string,
324 const char *push_cert_nonce)
325{
326 const struct ref *ref;
327 struct string_list_item *item;
328 char *signing_key_id = get_signing_key_id();
329 char *signing_key = get_signing_key();
330 const char *cp, *np;
331 struct strbuf cert = STRBUF_INIT;
332 int update_seen = 0;
333
334 strbuf_addstr(&cert, "certificate version 0.1\n");
335 strbuf_addf(&cert, "pusher %s ", signing_key_id);
336 datestamp(&cert);
337 strbuf_addch(&cert, '\n');
338 if (args->url && *args->url) {
339 char *anon_url = transport_anonymize_url(args->url);
340 strbuf_addf(&cert, "pushee %s\n", anon_url);
341 free(anon_url);
342 }
343 if (push_cert_nonce[0])
344 strbuf_addf(&cert, "nonce %s\n", push_cert_nonce);
345 if (args->push_options)
346 for_each_string_list_item(item, args->push_options)
347 strbuf_addf(&cert, "push-option %s\n", item->string);
348 strbuf_addstr(&cert, "\n");
349
350 for (ref = remote_refs; ref; ref = ref->next) {
351 if (check_to_send_update(ref, args) < 0)
352 continue;
353 update_seen = 1;
354 strbuf_addf(&cert, "%s %s %s\n",
355 oid_to_hex(&ref->old_oid),
356 oid_to_hex(&ref->new_oid),
357 ref->name);
358 }
359 if (!update_seen)
360 goto free_return;
361
362 if (sign_buffer(&cert, &cert, signing_key, 0))
363 die(_("failed to sign the push certificate"));
364
365 packet_buf_write(req_buf, "push-cert%c%s", 0, cap_string);
366 for (cp = cert.buf; cp < cert.buf + cert.len; cp = np) {
367 np = next_line(cp, cert.buf + cert.len - cp);
368 packet_buf_write(req_buf,
369 "%.*s", (int)(np - cp), cp);
370 }
371 packet_buf_write(req_buf, "push-cert-end\n");
372
373free_return:
374 free(signing_key_id);
375 free(signing_key);
376 strbuf_release(&cert);
377 return update_seen;
378}
379
380#define NONCE_LEN_LIMIT 256
381
382static void reject_invalid_nonce(const char *nonce, int len)
383{
384 int i = 0;
385
386 if (NONCE_LEN_LIMIT <= len)
387 die("the receiving end asked to sign an invalid nonce <%.*s>",
388 len, nonce);
389
390 for (i = 0; i < len; i++) {
391 int ch = nonce[i] & 0xFF;
392 if (isalnum(ch) ||
393 ch == '-' || ch == '.' ||
394 ch == '/' || ch == '+' ||
395 ch == '=' || ch == '_')
396 continue;
397 die("the receiving end asked to sign an invalid nonce <%.*s>",
398 len, nonce);
399 }
400}
401
402static void get_commons_through_negotiation(struct repository *r,
403 const char *url,
404 const struct string_list *negotiation_include,
405 const struct string_list *negotiation_restrict,
406 const struct ref *remote_refs,
407 struct oid_array *commons)
408{
409 struct child_process child = CHILD_PROCESS_INIT;
410 const struct ref *ref;
411 int len = r->hash_algo->hexsz + 1; /* hash + NL */
412 int nr_negotiation = 0;
413
414 child.git_cmd = 1;
415 child.no_stdin = 1;
416 child.out = -1;
417 strvec_pushl(&child.args, "fetch", "--negotiate-only", NULL);
418
419 if (negotiation_restrict && negotiation_restrict->nr) {
420 struct string_list_item *item;
421 for_each_string_list_item(item, negotiation_restrict)
422 strvec_pushf(&child.args, "--negotiation-restrict=%s",
423 item->string);
424 nr_negotiation = negotiation_restrict->nr;
425 } else {
426 for (ref = remote_refs; ref; ref = ref->next) {
427 if (!is_null_oid(&ref->new_oid)) {
428 strvec_pushf(&child.args, "--negotiation-restrict=%s",
429 oid_to_hex(&ref->new_oid));
430 nr_negotiation++;
431 }
432 }
433 }
434
435 if (negotiation_include && negotiation_include->nr) {
436 struct string_list_item *item;
437 for_each_string_list_item(item, negotiation_include)
438 strvec_pushf(&child.args, "--negotiation-include=%s",
439 item->string);
440 nr_negotiation += negotiation_include->nr;
441 }
442
443 strvec_push(&child.args, url);
444
445 if (!nr_negotiation) {
446 child_process_clear(&child);
447 return;
448 }
449
450 if (start_command(&child))
451 die(_("send-pack: unable to fork off fetch subprocess"));
452
453 do {
454 char hex_hash[GIT_MAX_HEXSZ + 1];
455 int read_len = read_in_full(child.out, hex_hash, len);
456 struct object_id oid;
457 const char *end;
458
459 if (!read_len)
460 break;
461 if (read_len != len)
462 die("invalid length read %d", read_len);
463 if (parse_oid_hex_algop(hex_hash, &oid, &end, r->hash_algo) || *end != '\n')
464 die("invalid hash");
465 oid_array_append(commons, &oid);
466 } while (1);
467
468 if (finish_command(&child)) {
469 /*
470 * The information that push negotiation provides is useful but
471 * not mandatory.
472 */
473 warning(_("push negotiation failed; proceeding anyway with push"));
474 }
475}
476
477int send_pack(struct repository *r,
478 struct send_pack_args *args,
479 int fd[], struct child_process *conn,
480 struct ref *remote_refs,
481 struct oid_array *extra_have)
482{
483 struct oid_array commons = OID_ARRAY_INIT;
484 int in = fd[0];
485 int out = fd[1];
486 struct strbuf req_buf = STRBUF_INIT;
487 struct strbuf cap_buf = STRBUF_INIT;
488 struct ref *ref;
489 int need_pack_data = 0;
490 int allow_deleting_refs = 0;
491 int status_report = 0;
492 int use_sideband = 0;
493 int quiet_supported = 0;
494 int agent_supported = 0;
495 int advertise_sid = 0;
496 int push_negotiate = 0;
497 int use_atomic = 0;
498 int atomic_supported = 0;
499 int use_push_options = 0;
500 int push_options_supported = 0;
501 int object_format_supported = 0;
502 unsigned cmds_sent = 0;
503 int ret;
504 struct async demux;
505 char *push_cert_nonce = NULL;
506 struct packet_reader reader;
507 int use_bitmaps;
508
509 if (!remote_refs) {
510 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
511 "Perhaps you should specify a branch.\n");
512 ret = 0;
513 goto out;
514 }
515
516 repo_config_get_bool(r, "push.negotiate", &push_negotiate);
517 if (push_negotiate) {
518 trace2_region_enter("send_pack", "push_negotiate", r);
519 get_commons_through_negotiation(r, args->url,
520 args->negotiation_include,
521 args->negotiation_restrict,
522 remote_refs, &commons);
523 trace2_region_leave("send_pack", "push_negotiate", r);
524 }
525
526 if (!repo_config_get_bool(r, "push.usebitmaps", &use_bitmaps))
527 args->disable_bitmaps = !use_bitmaps;
528
529 repo_config_get_bool(r, "transfer.advertisesid", &advertise_sid);
530
531 /* Does the other end support the reporting? */
532 if (server_supports("report-status-v2"))
533 status_report = 2;
534 else if (server_supports("report-status"))
535 status_report = 1;
536 if (server_supports("delete-refs"))
537 allow_deleting_refs = 1;
538 if (server_supports("ofs-delta"))
539 args->use_ofs_delta = 1;
540 if (server_supports("side-band-64k"))
541 use_sideband = 1;
542 if (server_supports("quiet"))
543 quiet_supported = 1;
544 if (server_supports("agent"))
545 agent_supported = 1;
546 if (!server_supports("session-id"))
547 advertise_sid = 0;
548 if (server_supports("no-thin"))
549 args->use_thin_pack = 0;
550 if (server_supports("atomic"))
551 atomic_supported = 1;
552 if (server_supports("push-options"))
553 push_options_supported = 1;
554
555 if (!server_supports_hash(r->hash_algo->name, &object_format_supported))
556 die(_("the receiving end does not support this repository's hash algorithm"));
557
558 if (args->push_cert != SEND_PACK_PUSH_CERT_NEVER) {
559 size_t len;
560 const char *nonce = server_feature_value("push-cert", &len);
561
562 if (nonce) {
563 reject_invalid_nonce(nonce, len);
564 push_cert_nonce = xmemdupz(nonce, len);
565 } else if (args->push_cert == SEND_PACK_PUSH_CERT_ALWAYS) {
566 die(_("the receiving end does not support --signed push"));
567 } else if (args->push_cert == SEND_PACK_PUSH_CERT_IF_ASKED) {
568 warning(_("not sending a push certificate since the"
569 " receiving end does not support --signed"
570 " push"));
571 }
572 }
573
574 if (args->atomic && !atomic_supported)
575 die(_("the receiving end does not support --atomic push"));
576
577 use_atomic = atomic_supported && args->atomic;
578
579 if (args->push_options && !push_options_supported)
580 die(_("the receiving end does not support push options"));
581
582 use_push_options = push_options_supported && args->push_options;
583
584 if (status_report == 1)
585 strbuf_addstr(&cap_buf, " report-status");
586 else if (status_report == 2)
587 strbuf_addstr(&cap_buf, " report-status-v2");
588 if (use_sideband)
589 strbuf_addstr(&cap_buf, " side-band-64k");
590 if (quiet_supported && (args->quiet || !args->progress))
591 strbuf_addstr(&cap_buf, " quiet");
592 if (use_atomic)
593 strbuf_addstr(&cap_buf, " atomic");
594 if (use_push_options)
595 strbuf_addstr(&cap_buf, " push-options");
596 if (object_format_supported)
597 strbuf_addf(&cap_buf, " object-format=%s", r->hash_algo->name);
598 if (agent_supported)
599 strbuf_addf(&cap_buf, " agent=%s", git_user_agent_sanitized());
600 if (advertise_sid)
601 strbuf_addf(&cap_buf, " session-id=%s", trace2_session_id());
602
603 /*
604 * NEEDSWORK: why does delete-refs have to be so specific to
605 * send-pack machinery that set_ref_status_for_push() cannot
606 * set this bit for us???
607 */
608 for (ref = remote_refs; ref; ref = ref->next)
609 if (ref->deletion && !allow_deleting_refs)
610 ref->status = REF_STATUS_REJECT_NODELETE;
611
612 /*
613 * Clear the status for each ref and see if we need to send
614 * the pack data.
615 */
616 for (ref = remote_refs; ref; ref = ref->next) {
617 switch (check_to_send_update(ref, args)) {
618 case 0: /* no error */
619 break;
620 case CHECK_REF_STATUS_REJECTED:
621 /*
622 * When we know the server would reject a ref update if
623 * we were to send it and we're trying to send the refs
624 * atomically, abort the whole operation.
625 */
626 if (use_atomic) {
627 reject_atomic_push(remote_refs, args->send_mirror);
628 error("atomic push failed for ref %s. status: %d",
629 ref->name, ref->status);
630 ret = ERROR_SEND_PACK_BAD_REF_STATUS;
631 packet_flush(out);
632 goto out;
633 }
634 /* else fallthrough */
635 default:
636 continue;
637 }
638 if (!ref->deletion)
639 need_pack_data = 1;
640
641 if (args->dry_run || !status_report)
642 ref->status = REF_STATUS_OK;
643 else
644 ref->status = REF_STATUS_EXPECTING_REPORT;
645 }
646
647 if (!args->dry_run)
648 advertise_shallow_grafts_buf(r, &req_buf);
649
650 /*
651 * Finally, tell the other end!
652 */
653 if (!args->dry_run && push_cert_nonce) {
654 cmds_sent = generate_push_cert(&req_buf, remote_refs, args,
655 cap_buf.buf, push_cert_nonce);
656 trace2_printf("Generated push certificate");
657 } else if (!args->dry_run) {
658 for (ref = remote_refs; ref; ref = ref->next) {
659 char *old_hex, *new_hex;
660
661 if (check_to_send_update(ref, args) < 0)
662 continue;
663
664 old_hex = oid_to_hex(&ref->old_oid);
665 new_hex = oid_to_hex(&ref->new_oid);
666 if (!cmds_sent) {
667 packet_buf_write(&req_buf,
668 "%s %s %s%c%s",
669 old_hex, new_hex, ref->name, 0,
670 cap_buf.buf);
671 cmds_sent = 1;
672 } else {
673 packet_buf_write(&req_buf, "%s %s %s",
674 old_hex, new_hex, ref->name);
675 }
676 }
677 }
678
679 if (use_push_options) {
680 struct string_list_item *item;
681
682 packet_buf_flush(&req_buf);
683 for_each_string_list_item(item, args->push_options)
684 packet_buf_write(&req_buf, "%s", item->string);
685 }
686
687 if (args->stateless_rpc) {
688 if (!args->dry_run && (cmds_sent || is_repository_shallow(r))) {
689 packet_buf_flush(&req_buf);
690 send_sideband(out, -1, req_buf.buf, req_buf.len, LARGE_PACKET_MAX);
691 }
692 } else {
693 write_or_die(out, req_buf.buf, req_buf.len);
694 packet_flush(out);
695 }
696
697 if (use_sideband && cmds_sent) {
698 memset(&demux, 0, sizeof(demux));
699 demux.proc = sideband_demux;
700 demux.data = fd;
701 demux.out = -1;
702 demux.isolate_sigpipe = 1;
703 if (start_async(&demux))
704 die("send-pack: unable to fork off sideband demultiplexer");
705 in = demux.out;
706 }
707
708 packet_reader_init(&reader, in, NULL, 0,
709 PACKET_READ_CHOMP_NEWLINE |
710 PACKET_READ_DIE_ON_ERR_PACKET);
711
712 if (need_pack_data && cmds_sent) {
713 if (pack_objects(r, out, remote_refs, extra_have, &commons, args) < 0) {
714 if (args->stateless_rpc)
715 close(out);
716 if (git_connection_is_socket(conn))
717 shutdown(fd[0], SHUT_WR);
718
719 /*
720 * Do not even bother with the return value; we know we
721 * are failing, and just want the error() side effects,
722 * as well as marking refs with their remote status (if
723 * we get one).
724 */
725 if (status_report)
726 receive_status(r, &reader, remote_refs);
727
728 if (use_sideband) {
729 close(demux.out);
730 finish_async(&demux);
731 }
732 fd[1] = -1;
733
734 ret = -1;
735 goto out;
736 }
737 if (!args->stateless_rpc)
738 /* Consumed by the pack generator in pack_objects() */
739 fd[1] = -1;
740 }
741 if (args->stateless_rpc && cmds_sent)
742 packet_flush(out);
743
744 if (status_report && cmds_sent)
745 ret = receive_status(r, &reader, remote_refs);
746 else
747 ret = 0;
748 if (args->stateless_rpc)
749 packet_flush(out);
750
751 if (use_sideband && cmds_sent) {
752 close(demux.out);
753 if (finish_async(&demux)) {
754 error("error in sideband demultiplexer");
755 ret = -1;
756 }
757 }
758
759 if (ret < 0)
760 goto out;
761
762 for (ref = remote_refs; ref; ref = ref->next) {
763 switch (ref->status) {
764 case REF_STATUS_NONE:
765 case REF_STATUS_UPTODATE:
766 case REF_STATUS_OK:
767 break;
768 default:
769 ret = ERROR_SEND_PACK_BAD_REF_STATUS;
770 goto out;
771 }
772 }
773
774 ret = 0;
775
776out:
777 oid_array_clear(&commons);
778 strbuf_release(&req_buf);
779 strbuf_release(&cap_buf);
780 free(push_cert_nonce);
781 return ret;
782}