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