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