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