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