]> git.ipfire.org Git - thirdparty/git.git/blame - send-pack.c
curl_trace(): eliminate switch fallthrough
[thirdparty/git.git] / send-pack.c
CommitLineData
f5d942e1 1#include "builtin.h"
b2141fc1 2#include "config.h"
f5d942e1
NTND
3#include "commit.h"
4#include "refs.h"
5#include "pkt-line.h"
6#include "sideband.h"
7#include "run-command.h"
8#include "remote.h"
47a59185 9#include "connect.h"
f5d942e1
NTND
10#include "send-pack.h"
11#include "quote.h"
12#include "transport.h"
13#include "version.h"
13eb4626 14#include "sha1-array.h"
a85b377d 15#include "gpg-interface.h"
30261094
DB
16#include "cache.h"
17
18int option_parse_push_signed(const struct option *opt,
19 const char *arg, int unset)
20{
21 if (unset) {
22 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_NEVER;
23 return 0;
24 }
25 switch (git_parse_maybe_bool(arg)) {
26 case 1:
27 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_ALWAYS;
28 return 0;
29 case 0:
30 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_NEVER;
31 return 0;
32 }
33 if (!strcasecmp("if-asked", arg)) {
34 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_IF_ASKED;
35 return 0;
36 }
37 die("bad %s argument: %s", opt->long_name, arg);
38}
f5d942e1 39
f0bca72d 40static void feed_object(const unsigned char *sha1, FILE *fh, int negative)
f5d942e1 41{
f5d942e1 42 if (negative && !has_sha1_file(sha1))
f0bca72d 43 return;
f5d942e1 44
f5d942e1 45 if (negative)
f0bca72d
JK
46 putc('^', fh);
47 fputs(sha1_to_hex(sha1), fh);
48 putc('\n', fh);
f5d942e1
NTND
49}
50
51/*
52 * Make a pack stream and spit it out into file descriptor fd
53 */
910650d2 54static int pack_objects(int fd, struct ref *refs, struct oid_array *extra, struct send_pack_args *args)
f5d942e1
NTND
55{
56 /*
57 * The child becomes pack-objects --revs; we feed
58 * the revision parameters to it via its stdin and
59 * let its stdout go back to the other end.
60 */
61 const char *argv[] = {
62 "pack-objects",
63 "--all-progress-implied",
64 "--revs",
65 "--stdout",
66 NULL,
67 NULL,
68 NULL,
69 NULL,
70 NULL,
2dacf26d 71 NULL,
f5d942e1 72 };
d3180279 73 struct child_process po = CHILD_PROCESS_INIT;
f0bca72d 74 FILE *po_in;
f5d942e1 75 int i;
d1a13d3f 76 int rc;
f5d942e1
NTND
77
78 i = 4;
79 if (args->use_thin_pack)
80 argv[i++] = "--thin";
81 if (args->use_ofs_delta)
82 argv[i++] = "--delta-base-offset";
83 if (args->quiet || !args->progress)
84 argv[i++] = "-q";
85 if (args->progress)
86 argv[i++] = "--progress";
2dacf26d 87 if (is_repository_shallow())
88 argv[i++] = "--shallow";
f5d942e1
NTND
89 po.argv = argv;
90 po.in = -1;
91 po.out = args->stateless_rpc ? -1 : fd;
92 po.git_cmd = 1;
93 if (start_command(&po))
94 die_errno("git pack-objects failed");
95
96 /*
97 * We feed the pack-objects we just spawned with revision
98 * parameters by writing to the pipe.
99 */
f0bca72d 100 po_in = xfdopen(po.in, "w");
f5d942e1 101 for (i = 0; i < extra->nr; i++)
ee3051bd 102 feed_object(extra->oid[i].hash, po_in, 1);
f5d942e1
NTND
103
104 while (refs) {
f0bca72d
JK
105 if (!is_null_oid(&refs->old_oid))
106 feed_object(refs->old_oid.hash, po_in, 1);
107 if (!is_null_oid(&refs->new_oid))
108 feed_object(refs->new_oid.hash, po_in, 0);
f5d942e1
NTND
109 refs = refs->next;
110 }
111
f0bca72d
JK
112 fflush(po_in);
113 if (ferror(po_in))
114 die_errno("error writing to pack-objects");
115 fclose(po_in);
f5d942e1
NTND
116
117 if (args->stateless_rpc) {
118 char *buf = xmalloc(LARGE_PACKET_MAX);
119 while (1) {
120 ssize_t n = xread(po.out, buf, LARGE_PACKET_MAX);
121 if (n <= 0)
122 break;
123 send_sideband(fd, -1, buf, n, LARGE_PACKET_MAX);
124 }
125 free(buf);
126 close(po.out);
127 po.out = -1;
128 }
129
d1a13d3f
JK
130 rc = finish_command(&po);
131 if (rc) {
132 /*
133 * For a normal non-zero exit, we assume pack-objects wrote
134 * something useful to stderr. For death by signal, though,
135 * we should mention it to the user. The exception is SIGPIPE
64127575 136 * (141), because that's a normal occurrence if the remote end
d1a13d3f
JK
137 * hangs up (and we'll report that by trying to read the unpack
138 * status).
139 */
140 if (rc > 128 && rc != 141)
141 error("pack-objects died of signal %d", rc - 128);
f5d942e1 142 return -1;
d1a13d3f 143 }
f5d942e1
NTND
144 return 0;
145}
146
7c39df29 147static int receive_unpack_status(int in)
f5d942e1 148{
7c39df29 149 const char *line = packet_read_line(in, NULL);
f7cd74d1 150 if (!skip_prefix(line, "unpack ", &line))
40d05d04 151 return error(_("unable to parse remote unpack status: %s"), line);
f7cd74d1 152 if (strcmp(line, "ok"))
40d05d04 153 return error(_("remote unpack failed: %s"), line);
7c39df29
JK
154 return 0;
155}
156
157static int receive_status(int in, struct ref *refs)
158{
159 struct ref *hint;
160 int ret;
161
f5d942e1 162 hint = NULL;
7c39df29 163 ret = receive_unpack_status(in);
f5d942e1
NTND
164 while (1) {
165 char *refname;
166 char *msg;
7c39df29 167 char *line = packet_read_line(in, NULL);
74543a04 168 if (!line)
f5d942e1 169 break;
59556548 170 if (!starts_with(line, "ok ") && !starts_with(line, "ng ")) {
8f9e3e49 171 error("invalid ref status from remote: %s", line);
f5d942e1
NTND
172 ret = -1;
173 break;
174 }
175
f5d942e1
NTND
176 refname = line + 3;
177 msg = strchr(refname, ' ');
178 if (msg)
179 *msg++ = '\0';
180
181 /* first try searching at our hint, falling back to all refs */
182 if (hint)
183 hint = find_ref_by_name(hint, refname);
184 if (!hint)
185 hint = find_ref_by_name(refs, refname);
186 if (!hint) {
187 warning("remote reported status on unknown ref: %s",
188 refname);
189 continue;
190 }
191 if (hint->status != REF_STATUS_EXPECTING_REPORT) {
192 warning("remote reported status on unexpected ref: %s",
193 refname);
194 continue;
195 }
196
197 if (line[0] == 'o' && line[1] == 'k')
198 hint->status = REF_STATUS_OK;
199 else {
200 hint->status = REF_STATUS_REMOTE_REJECT;
201 ret = -1;
202 }
13092a91 203 hint->remote_status = xstrdup_or_null(msg);
f5d942e1
NTND
204 /* start our next search from the next ref */
205 hint = hint->next;
206 }
207 return ret;
208}
209
210static int sideband_demux(int in, int out, void *data)
211{
212 int *fd = data, ret;
213#ifdef NO_PTHREADS
214 close(fd[1]);
215#endif
216 ret = recv_sideband("send-pack", fd[0], out);
217 close(out);
218 return ret;
219}
220
f2c681cf
NTND
221static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
222{
223 struct strbuf *sb = cb;
224 if (graft->nr_parent == -1)
7683e2e6 225 packet_buf_write(sb, "shallow %s\n", oid_to_hex(&graft->oid));
f2c681cf
NTND
226 return 0;
227}
228
16a2743c 229static void advertise_shallow_grafts_buf(struct strbuf *sb)
f2c681cf
NTND
230{
231 if (!is_repository_shallow())
232 return;
233 for_each_commit_graft(advertise_shallow_grafts_cb, sb);
234}
235
7582e939
SB
236#define CHECK_REF_NO_PUSH -1
237#define CHECK_REF_STATUS_REJECTED -2
238#define CHECK_REF_UPTODATE -3
239static int check_to_send_update(const struct ref *ref, const struct send_pack_args *args)
e40671a3
JH
240{
241 if (!ref->peer_ref && !args->send_mirror)
7582e939 242 return CHECK_REF_NO_PUSH;
e40671a3
JH
243
244 /* Check for statuses set by set_ref_status_for_push() */
245 switch (ref->status) {
246 case REF_STATUS_REJECT_NONFASTFORWARD:
247 case REF_STATUS_REJECT_ALREADY_EXISTS:
248 case REF_STATUS_REJECT_FETCH_FIRST:
249 case REF_STATUS_REJECT_NEEDS_FORCE:
250 case REF_STATUS_REJECT_STALE:
251 case REF_STATUS_REJECT_NODELETE:
7582e939 252 return CHECK_REF_STATUS_REJECTED;
e40671a3 253 case REF_STATUS_UPTODATE:
7582e939 254 return CHECK_REF_UPTODATE;
e40671a3 255 default:
7582e939 256 return 0;
e40671a3
JH
257 }
258}
259
a85b377d
JH
260/*
261 * the beginning of the next line, or the end of buffer.
262 *
263 * NEEDSWORK: perhaps move this to git-compat-util.h or somewhere and
264 * convert many similar uses found by "git grep -A4 memchr".
265 */
266static const char *next_line(const char *line, size_t len)
267{
268 const char *nl = memchr(line, '\n', len);
269 if (!nl)
270 return line + len; /* incomplete line */
271 return nl + 1;
272}
273
20a7558f
JH
274static int generate_push_cert(struct strbuf *req_buf,
275 const struct ref *remote_refs,
276 struct send_pack_args *args,
b89363e4
JH
277 const char *cap_string,
278 const char *push_cert_nonce)
a85b377d
JH
279{
280 const struct ref *ref;
f6a4e61f 281 struct string_list_item *item;
a85b377d
JH
282 char *signing_key = xstrdup(get_signing_key());
283 const char *cp, *np;
284 struct strbuf cert = STRBUF_INIT;
285 int update_seen = 0;
286
02962d36 287 strbuf_addstr(&cert, "certificate version 0.1\n");
fb06b528
JH
288 strbuf_addf(&cert, "pusher %s ", signing_key);
289 datestamp(&cert);
290 strbuf_addch(&cert, '\n');
9be89160
JH
291 if (args->url && *args->url) {
292 char *anon_url = transport_anonymize_url(args->url);
293 strbuf_addf(&cert, "pushee %s\n", anon_url);
294 free(anon_url);
295 }
b89363e4
JH
296 if (push_cert_nonce[0])
297 strbuf_addf(&cert, "nonce %s\n", push_cert_nonce);
f6a4e61f
SB
298 if (args->push_options)
299 for_each_string_list_item(item, args->push_options)
300 strbuf_addf(&cert, "push-option %s\n", item->string);
a85b377d
JH
301 strbuf_addstr(&cert, "\n");
302
303 for (ref = remote_refs; ref; ref = ref->next) {
7582e939 304 if (check_to_send_update(ref, args) < 0)
a85b377d
JH
305 continue;
306 update_seen = 1;
307 strbuf_addf(&cert, "%s %s %s\n",
f4e54d02 308 oid_to_hex(&ref->old_oid),
309 oid_to_hex(&ref->new_oid),
a85b377d
JH
310 ref->name);
311 }
312 if (!update_seen)
313 goto free_return;
314
315 if (sign_buffer(&cert, &cert, signing_key))
316 die(_("failed to sign the push certificate"));
317
20a7558f 318 packet_buf_write(req_buf, "push-cert%c%s", 0, cap_string);
a85b377d
JH
319 for (cp = cert.buf; cp < cert.buf + cert.len; cp = np) {
320 np = next_line(cp, cert.buf + cert.len - cp);
321 packet_buf_write(req_buf,
322 "%.*s", (int)(np - cp), cp);
323 }
324 packet_buf_write(req_buf, "push-cert-end\n");
325
326free_return:
327 free(signing_key);
328 strbuf_release(&cert);
20a7558f 329 return update_seen;
a85b377d
JH
330}
331
4ff17f10
RS
332
333static int atomic_push_failure(struct send_pack_args *args,
334 struct ref *remote_refs,
335 struct ref *failing_ref)
336{
337 struct ref *ref;
338 /* Mark other refs as failed */
339 for (ref = remote_refs; ref; ref = ref->next) {
340 if (!ref->peer_ref && !args->send_mirror)
341 continue;
342
343 switch (ref->status) {
344 case REF_STATUS_EXPECTING_REPORT:
345 ref->status = REF_STATUS_ATOMIC_PUSH_FAILED;
346 continue;
347 default:
348 break; /* do nothing */
349 }
350 }
351 return error("atomic push failed for ref %s. status: %d\n",
352 failing_ref->name, failing_ref->status);
353}
354
afcb6ee3
JH
355#define NONCE_LEN_LIMIT 256
356
357static void reject_invalid_nonce(const char *nonce, int len)
358{
359 int i = 0;
360
361 if (NONCE_LEN_LIMIT <= len)
362 die("the receiving end asked to sign an invalid nonce <%.*s>",
363 len, nonce);
364
365 for (i = 0; i < len; i++) {
366 int ch = nonce[i] & 0xFF;
367 if (isalnum(ch) ||
368 ch == '-' || ch == '.' ||
369 ch == '/' || ch == '+' ||
370 ch == '=' || ch == '_')
371 continue;
372 die("the receiving end asked to sign an invalid nonce <%.*s>",
373 len, nonce);
374 }
375}
376
f5d942e1
NTND
377int send_pack(struct send_pack_args *args,
378 int fd[], struct child_process *conn,
379 struct ref *remote_refs,
910650d2 380 struct oid_array *extra_have)
f5d942e1
NTND
381{
382 int in = fd[0];
383 int out = fd[1];
384 struct strbuf req_buf = STRBUF_INIT;
887f3533 385 struct strbuf cap_buf = STRBUF_INIT;
f5d942e1 386 struct ref *ref;
ab2b0c90 387 int need_pack_data = 0;
f5d942e1
NTND
388 int allow_deleting_refs = 0;
389 int status_report = 0;
390 int use_sideband = 0;
391 int quiet_supported = 0;
392 int agent_supported = 0;
4ff17f10
RS
393 int use_atomic = 0;
394 int atomic_supported = 0;
f6a4e61f
SB
395 int use_push_options = 0;
396 int push_options_supported = 0;
f5d942e1
NTND
397 unsigned cmds_sent = 0;
398 int ret;
399 struct async demux;
b89363e4 400 const char *push_cert_nonce = NULL;
f5d942e1
NTND
401
402 /* Does the other end support the reporting? */
403 if (server_supports("report-status"))
404 status_report = 1;
405 if (server_supports("delete-refs"))
406 allow_deleting_refs = 1;
407 if (server_supports("ofs-delta"))
408 args->use_ofs_delta = 1;
409 if (server_supports("side-band-64k"))
410 use_sideband = 1;
411 if (server_supports("quiet"))
412 quiet_supported = 1;
413 if (server_supports("agent"))
414 agent_supported = 1;
1ba98a79
CMN
415 if (server_supports("no-thin"))
416 args->use_thin_pack = 0;
4ff17f10
RS
417 if (server_supports("atomic"))
418 atomic_supported = 1;
f6a4e61f
SB
419 if (server_supports("push-options"))
420 push_options_supported = 1;
b89363e4 421
30261094
DB
422 if (args->push_cert != SEND_PACK_PUSH_CERT_NEVER) {
423 int len;
b89363e4 424 push_cert_nonce = server_feature_value("push-cert", &len);
30261094
DB
425 if (push_cert_nonce) {
426 reject_invalid_nonce(push_cert_nonce, len);
427 push_cert_nonce = xmemdupz(push_cert_nonce, len);
428 } else if (args->push_cert == SEND_PACK_PUSH_CERT_ALWAYS) {
b89363e4 429 die(_("the receiving end does not support --signed push"));
30261094
DB
430 } else if (args->push_cert == SEND_PACK_PUSH_CERT_IF_ASKED) {
431 warning(_("not sending a push certificate since the"
432 " receiving end does not support --signed"
433 " push"));
434 }
b89363e4 435 }
f5d942e1
NTND
436
437 if (!remote_refs) {
438 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
439 "Perhaps you should specify a branch such as 'master'.\n");
440 return 0;
441 }
4ff17f10 442 if (args->atomic && !atomic_supported)
c8b8f22a 443 die(_("the receiving end does not support --atomic push"));
4ff17f10
RS
444
445 use_atomic = atomic_supported && args->atomic;
f5d942e1 446
f6a4e61f
SB
447 if (args->push_options && !push_options_supported)
448 die(_("the receiving end does not support push options"));
449
450 use_push_options = push_options_supported && args->push_options;
451
887f3533
JH
452 if (status_report)
453 strbuf_addstr(&cap_buf, " report-status");
454 if (use_sideband)
455 strbuf_addstr(&cap_buf, " side-band-64k");
456 if (quiet_supported && (args->quiet || !args->progress))
457 strbuf_addstr(&cap_buf, " quiet");
4ff17f10
RS
458 if (use_atomic)
459 strbuf_addstr(&cap_buf, " atomic");
f6a4e61f
SB
460 if (use_push_options)
461 strbuf_addstr(&cap_buf, " push-options");
887f3533
JH
462 if (agent_supported)
463 strbuf_addf(&cap_buf, " agent=%s", git_user_agent_sanitized());
464
621b0599
JH
465 /*
466 * NEEDSWORK: why does delete-refs have to be so specific to
467 * send-pack machinery that set_ref_status_for_push() cannot
468 * set this bit for us???
469 */
470 for (ref = remote_refs; ref; ref = ref->next)
471 if (ref->deletion && !allow_deleting_refs)
472 ref->status = REF_STATUS_REJECT_NODELETE;
473
5dbd7676 474 if (!args->dry_run)
f2c681cf 475 advertise_shallow_grafts_buf(&req_buf);
5dbd7676 476
30261094 477 if (!args->dry_run && push_cert_nonce)
20a7558f 478 cmds_sent = generate_push_cert(&req_buf, remote_refs, args,
b89363e4 479 cap_buf.buf, push_cert_nonce);
a85b377d 480
f5d942e1 481 /*
b783aa71
JH
482 * Clear the status for each ref and see if we need to send
483 * the pack data.
f5d942e1 484 */
f5d942e1 485 for (ref = remote_refs; ref; ref = ref->next) {
4ff17f10
RS
486 switch (check_to_send_update(ref, args)) {
487 case 0: /* no error */
488 break;
489 case CHECK_REF_STATUS_REJECTED:
490 /*
491 * When we know the server would reject a ref update if
492 * we were to send it and we're trying to send the refs
493 * atomically, abort the whole operation.
494 */
872d651f
RS
495 if (use_atomic) {
496 strbuf_release(&req_buf);
497 strbuf_release(&cap_buf);
4ff17f10 498 return atomic_push_failure(args, remote_refs, ref);
872d651f 499 }
4ff17f10
RS
500 /* Fallthrough for non atomic case. */
501 default:
f5d942e1 502 continue;
4ff17f10 503 }
f5d942e1 504 if (!ref->deletion)
ab2b0c90 505 need_pack_data = 1;
f5d942e1 506
b783aa71 507 if (args->dry_run || !status_report)
f5d942e1 508 ref->status = REF_STATUS_OK;
b783aa71
JH
509 else
510 ref->status = REF_STATUS_EXPECTING_REPORT;
511 }
512
513 /*
514 * Finally, tell the other end!
515 */
516 for (ref = remote_refs; ref; ref = ref->next) {
517 char *old_hex, *new_hex;
518
30261094 519 if (args->dry_run || push_cert_nonce)
f5d942e1 520 continue;
f5d942e1 521
7582e939 522 if (check_to_send_update(ref, args) < 0)
b783aa71 523 continue;
f5d942e1 524
f4e54d02 525 old_hex = oid_to_hex(&ref->old_oid);
526 new_hex = oid_to_hex(&ref->new_oid);
c67072b9 527 if (!cmds_sent) {
b783aa71
JH
528 packet_buf_write(&req_buf,
529 "%s %s %s%c%s",
530 old_hex, new_hex, ref->name, 0,
531 cap_buf.buf);
c67072b9 532 cmds_sent = 1;
f5d942e1 533 } else {
b783aa71
JH
534 packet_buf_write(&req_buf, "%s %s %s",
535 old_hex, new_hex, ref->name);
f5d942e1
NTND
536 }
537 }
538
eb7b9749
BW
539 if (use_push_options) {
540 struct string_list_item *item;
541
542 packet_buf_flush(&req_buf);
543 for_each_string_list_item(item, args->push_options)
544 packet_buf_write(&req_buf, "%s", item->string);
545 }
546
f5d942e1 547 if (args->stateless_rpc) {
f2c681cf 548 if (!args->dry_run && (cmds_sent || is_repository_shallow())) {
f5d942e1
NTND
549 packet_buf_flush(&req_buf);
550 send_sideband(out, -1, req_buf.buf, req_buf.len, LARGE_PACKET_MAX);
551 }
552 } else {
cdf4fb8e 553 write_or_die(out, req_buf.buf, req_buf.len);
f5d942e1
NTND
554 packet_flush(out);
555 }
556 strbuf_release(&req_buf);
887f3533 557 strbuf_release(&cap_buf);
f5d942e1
NTND
558
559 if (use_sideband && cmds_sent) {
560 memset(&demux, 0, sizeof(demux));
561 demux.proc = sideband_demux;
562 demux.data = fd;
563 demux.out = -1;
3e8b06d0 564 demux.isolate_sigpipe = 1;
f5d942e1
NTND
565 if (start_async(&demux))
566 die("send-pack: unable to fork off sideband demultiplexer");
567 in = demux.out;
568 }
569
ab2b0c90 570 if (need_pack_data && cmds_sent) {
f5d942e1
NTND
571 if (pack_objects(out, remote_refs, extra_have, args) < 0) {
572 for (ref = remote_refs; ref; ref = ref->next)
573 ref->status = REF_STATUS_NONE;
574 if (args->stateless_rpc)
575 close(out);
576 if (git_connection_is_socket(conn))
577 shutdown(fd[0], SHUT_WR);
ba69f92d
JK
578
579 /*
580 * Do not even bother with the return value; we know we
581 * are failing, and just want the error() side effects.
582 */
583 if (status_report)
584 receive_unpack_status(in);
585
739cf491
JK
586 if (use_sideband) {
587 close(demux.out);
f5d942e1 588 finish_async(&demux);
739cf491 589 }
37cb1dd6 590 fd[1] = -1;
f5d942e1
NTND
591 return -1;
592 }
37cb1dd6
JL
593 if (!args->stateless_rpc)
594 /* Closed by pack_objects() via start_command() */
595 fd[1] = -1;
f5d942e1
NTND
596 }
597 if (args->stateless_rpc && cmds_sent)
598 packet_flush(out);
599
600 if (status_report && cmds_sent)
601 ret = receive_status(in, remote_refs);
602 else
603 ret = 0;
604 if (args->stateless_rpc)
605 packet_flush(out);
606
607 if (use_sideband && cmds_sent) {
739cf491 608 close(demux.out);
f5d942e1
NTND
609 if (finish_async(&demux)) {
610 error("error in sideband demultiplexer");
611 ret = -1;
612 }
f5d942e1
NTND
613 }
614
615 if (ret < 0)
616 return ret;
617
618 if (args->porcelain)
619 return 0;
620
621 for (ref = remote_refs; ref; ref = ref->next) {
622 switch (ref->status) {
623 case REF_STATUS_NONE:
624 case REF_STATUS_UPTODATE:
625 case REF_STATUS_OK:
626 break;
627 default:
628 return -1;
629 }
630 }
631 return 0;
632}