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