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