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