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