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