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