Skip tests that fail due to incomplete implementations, missing tools...
[git/mingw/j6t.git] / send-pack.c
blob684a2b00f3cad246e12a37f9cd428a76046b597a
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"
8 #include "connect.h"
9 #include "send-pack.h"
10 #include "quote.h"
11 #include "transport.h"
12 #include "version.h"
13 #include "sha1-array.h"
14 #include "gpg-interface.h"
15 #include "cache.h"
17 int option_parse_push_signed(const struct option *opt,
18 const char *arg, int unset)
20 if (unset) {
21 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_NEVER;
22 return 0;
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;
32 if (!strcasecmp("if-asked", arg)) {
33 *(int *)(opt->value) = SEND_PACK_PUSH_CERT_IF_ASKED;
34 return 0;
36 die("bad %s argument: %s", opt->long_name, arg);
39 static int feed_object(const unsigned char *sha1, int fd, int negative)
41 char buf[42];
43 if (negative && !has_sha1_file(sha1))
44 return 1;
46 memcpy(buf + negative, sha1_to_hex(sha1), 40);
47 if (negative)
48 buf[0] = '^';
49 buf[40 + negative] = '\n';
50 return write_or_whine(fd, buf, 41 + negative, "send-pack: send refs");
54 * Make a pack stream and spit it out into file descriptor fd
56 static int pack_objects(int fd, struct ref *refs, struct sha1_array *extra, struct send_pack_args *args)
59 * The child becomes pack-objects --revs; we feed
60 * the revision parameters to it via its stdin and
61 * let its stdout go back to the other end.
63 const char *argv[] = {
64 "pack-objects",
65 "--all-progress-implied",
66 "--revs",
67 "--stdout",
68 NULL,
69 NULL,
70 NULL,
71 NULL,
72 NULL,
73 NULL,
75 struct child_process po = CHILD_PROCESS_INIT;
76 int i;
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";
87 if (is_repository_shallow())
88 argv[i++] = "--shallow";
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");
97 * We feed the pack-objects we just spawned with revision
98 * parameters by writing to the pipe.
100 for (i = 0; i < extra->nr; i++)
101 if (!feed_object(extra->sha1[i], po.in, 1))
102 break;
104 while (refs) {
105 if (!is_null_sha1(refs->old_sha1) &&
106 !feed_object(refs->old_sha1, po.in, 1))
107 break;
108 if (!is_null_sha1(refs->new_sha1) &&
109 !feed_object(refs->new_sha1, po.in, 0))
110 break;
111 refs = refs->next;
114 close(po.in);
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);
124 free(buf);
125 close(po.out);
126 po.out = -1;
129 if (finish_command(&po))
130 return -1;
131 return 0;
134 static int receive_status(int in, struct ref *refs)
136 struct ref *hint;
137 int ret = 0;
138 char *line = packet_read_line(in, NULL);
139 if (!starts_with(line, "unpack "))
140 return error("did not receive remote status");
141 if (strcmp(line, "unpack ok")) {
142 error("unpack failed: %s", line + 7);
143 ret = -1;
145 hint = NULL;
146 while (1) {
147 char *refname;
148 char *msg;
149 line = packet_read_line(in, NULL);
150 if (!line)
151 break;
152 if (!starts_with(line, "ok ") && !starts_with(line, "ng ")) {
153 error("invalid ref status from remote: %s", line);
154 ret = -1;
155 break;
158 refname = line + 3;
159 msg = strchr(refname, ' ');
160 if (msg)
161 *msg++ = '\0';
163 /* first try searching at our hint, falling back to all refs */
164 if (hint)
165 hint = find_ref_by_name(hint, refname);
166 if (!hint)
167 hint = find_ref_by_name(refs, refname);
168 if (!hint) {
169 warning("remote reported status on unknown ref: %s",
170 refname);
171 continue;
173 if (hint->status != REF_STATUS_EXPECTING_REPORT) {
174 warning("remote reported status on unexpected ref: %s",
175 refname);
176 continue;
179 if (line[0] == 'o' && line[1] == 'k')
180 hint->status = REF_STATUS_OK;
181 else {
182 hint->status = REF_STATUS_REMOTE_REJECT;
183 ret = -1;
185 if (msg)
186 hint->remote_status = xstrdup(msg);
187 /* start our next search from the next ref */
188 hint = hint->next;
190 return ret;
193 static int sideband_demux(int in, int out, void *data)
195 int *fd = data, ret;
196 #ifdef NO_PTHREADS
197 close(fd[1]);
198 #endif
199 ret = recv_sideband("send-pack", fd[0], out);
200 close(out);
201 return ret;
204 static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
206 struct strbuf *sb = cb;
207 if (graft->nr_parent == -1)
208 packet_buf_write(sb, "shallow %s\n", oid_to_hex(&graft->oid));
209 return 0;
212 static void advertise_shallow_grafts_buf(struct strbuf *sb)
214 if (!is_repository_shallow())
215 return;
216 for_each_commit_graft(advertise_shallow_grafts_cb, sb);
219 #define CHECK_REF_NO_PUSH -1
220 #define CHECK_REF_STATUS_REJECTED -2
221 #define CHECK_REF_UPTODATE -3
222 static int check_to_send_update(const struct ref *ref, const struct send_pack_args *args)
224 if (!ref->peer_ref && !args->send_mirror)
225 return CHECK_REF_NO_PUSH;
227 /* Check for statuses set by set_ref_status_for_push() */
228 switch (ref->status) {
229 case REF_STATUS_REJECT_NONFASTFORWARD:
230 case REF_STATUS_REJECT_ALREADY_EXISTS:
231 case REF_STATUS_REJECT_FETCH_FIRST:
232 case REF_STATUS_REJECT_NEEDS_FORCE:
233 case REF_STATUS_REJECT_STALE:
234 case REF_STATUS_REJECT_NODELETE:
235 return CHECK_REF_STATUS_REJECTED;
236 case REF_STATUS_UPTODATE:
237 return CHECK_REF_UPTODATE;
238 default:
239 return 0;
244 * the beginning of the next line, or the end of buffer.
246 * NEEDSWORK: perhaps move this to git-compat-util.h or somewhere and
247 * convert many similar uses found by "git grep -A4 memchr".
249 static const char *next_line(const char *line, size_t len)
251 const char *nl = memchr(line, '\n', len);
252 if (!nl)
253 return line + len; /* incomplete line */
254 return nl + 1;
257 static int generate_push_cert(struct strbuf *req_buf,
258 const struct ref *remote_refs,
259 struct send_pack_args *args,
260 const char *cap_string,
261 const char *push_cert_nonce)
263 const struct ref *ref;
264 char *signing_key = xstrdup(get_signing_key());
265 const char *cp, *np;
266 struct strbuf cert = STRBUF_INIT;
267 int update_seen = 0;
269 strbuf_addf(&cert, "certificate version 0.1\n");
270 strbuf_addf(&cert, "pusher %s ", signing_key);
271 datestamp(&cert);
272 strbuf_addch(&cert, '\n');
273 if (args->url && *args->url) {
274 char *anon_url = transport_anonymize_url(args->url);
275 strbuf_addf(&cert, "pushee %s\n", anon_url);
276 free(anon_url);
278 if (push_cert_nonce[0])
279 strbuf_addf(&cert, "nonce %s\n", push_cert_nonce);
280 strbuf_addstr(&cert, "\n");
282 for (ref = remote_refs; ref; ref = ref->next) {
283 if (check_to_send_update(ref, args) < 0)
284 continue;
285 update_seen = 1;
286 strbuf_addf(&cert, "%s %s %s\n",
287 sha1_to_hex(ref->old_sha1),
288 sha1_to_hex(ref->new_sha1),
289 ref->name);
291 if (!update_seen)
292 goto free_return;
294 if (sign_buffer(&cert, &cert, signing_key))
295 die(_("failed to sign the push certificate"));
297 packet_buf_write(req_buf, "push-cert%c%s", 0, cap_string);
298 for (cp = cert.buf; cp < cert.buf + cert.len; cp = np) {
299 np = next_line(cp, cert.buf + cert.len - cp);
300 packet_buf_write(req_buf,
301 "%.*s", (int)(np - cp), cp);
303 packet_buf_write(req_buf, "push-cert-end\n");
305 free_return:
306 free(signing_key);
307 strbuf_release(&cert);
308 return update_seen;
312 static int atomic_push_failure(struct send_pack_args *args,
313 struct ref *remote_refs,
314 struct ref *failing_ref)
316 struct ref *ref;
317 /* Mark other refs as failed */
318 for (ref = remote_refs; ref; ref = ref->next) {
319 if (!ref->peer_ref && !args->send_mirror)
320 continue;
322 switch (ref->status) {
323 case REF_STATUS_EXPECTING_REPORT:
324 ref->status = REF_STATUS_ATOMIC_PUSH_FAILED;
325 continue;
326 default:
327 break; /* do nothing */
330 return error("atomic push failed for ref %s. status: %d\n",
331 failing_ref->name, failing_ref->status);
334 #define NONCE_LEN_LIMIT 256
336 static void reject_invalid_nonce(const char *nonce, int len)
338 int i = 0;
340 if (NONCE_LEN_LIMIT <= len)
341 die("the receiving end asked to sign an invalid nonce <%.*s>",
342 len, nonce);
344 for (i = 0; i < len; i++) {
345 int ch = nonce[i] & 0xFF;
346 if (isalnum(ch) ||
347 ch == '-' || ch == '.' ||
348 ch == '/' || ch == '+' ||
349 ch == '=' || ch == '_')
350 continue;
351 die("the receiving end asked to sign an invalid nonce <%.*s>",
352 len, nonce);
356 int send_pack(struct send_pack_args *args,
357 int fd[], struct child_process *conn,
358 struct ref *remote_refs,
359 struct sha1_array *extra_have)
361 int in = fd[0];
362 int out = fd[1];
363 struct strbuf req_buf = STRBUF_INIT;
364 struct strbuf cap_buf = STRBUF_INIT;
365 struct ref *ref;
366 int need_pack_data = 0;
367 int allow_deleting_refs = 0;
368 int status_report = 0;
369 int use_sideband = 0;
370 int quiet_supported = 0;
371 int agent_supported = 0;
372 int use_atomic = 0;
373 int atomic_supported = 0;
374 unsigned cmds_sent = 0;
375 int ret;
376 struct async demux;
377 const char *push_cert_nonce = NULL;
379 /* Does the other end support the reporting? */
380 if (server_supports("report-status"))
381 status_report = 1;
382 if (server_supports("delete-refs"))
383 allow_deleting_refs = 1;
384 if (server_supports("ofs-delta"))
385 args->use_ofs_delta = 1;
386 if (!broken_threaded_networked_spawn() &&
387 server_supports("side-band-64k"))
388 use_sideband = 1;
389 if (server_supports("quiet"))
390 quiet_supported = 1;
391 if (server_supports("agent"))
392 agent_supported = 1;
393 if (server_supports("no-thin"))
394 args->use_thin_pack = 0;
395 if (server_supports("atomic"))
396 atomic_supported = 1;
398 if (args->push_cert != SEND_PACK_PUSH_CERT_NEVER) {
399 int len;
400 push_cert_nonce = server_feature_value("push-cert", &len);
401 if (push_cert_nonce) {
402 reject_invalid_nonce(push_cert_nonce, len);
403 push_cert_nonce = xmemdupz(push_cert_nonce, len);
404 } else if (args->push_cert == SEND_PACK_PUSH_CERT_ALWAYS) {
405 die(_("the receiving end does not support --signed push"));
406 } else if (args->push_cert == SEND_PACK_PUSH_CERT_IF_ASKED) {
407 warning(_("not sending a push certificate since the"
408 " receiving end does not support --signed"
409 " push"));
413 if (!remote_refs) {
414 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
415 "Perhaps you should specify a branch such as 'master'.\n");
416 return 0;
418 if (args->atomic && !atomic_supported)
419 die(_("the receiving end does not support --atomic push"));
421 use_atomic = atomic_supported && args->atomic;
423 if (status_report)
424 strbuf_addstr(&cap_buf, " report-status");
425 if (use_sideband)
426 strbuf_addstr(&cap_buf, " side-band-64k");
427 if (quiet_supported && (args->quiet || !args->progress))
428 strbuf_addstr(&cap_buf, " quiet");
429 if (use_atomic)
430 strbuf_addstr(&cap_buf, " atomic");
431 if (agent_supported)
432 strbuf_addf(&cap_buf, " agent=%s", git_user_agent_sanitized());
435 * NEEDSWORK: why does delete-refs have to be so specific to
436 * send-pack machinery that set_ref_status_for_push() cannot
437 * set this bit for us???
439 for (ref = remote_refs; ref; ref = ref->next)
440 if (ref->deletion && !allow_deleting_refs)
441 ref->status = REF_STATUS_REJECT_NODELETE;
443 if (!args->dry_run)
444 advertise_shallow_grafts_buf(&req_buf);
446 if (!args->dry_run && push_cert_nonce)
447 cmds_sent = generate_push_cert(&req_buf, remote_refs, args,
448 cap_buf.buf, push_cert_nonce);
451 * Clear the status for each ref and see if we need to send
452 * the pack data.
454 for (ref = remote_refs; ref; ref = ref->next) {
455 switch (check_to_send_update(ref, args)) {
456 case 0: /* no error */
457 break;
458 case CHECK_REF_STATUS_REJECTED:
460 * When we know the server would reject a ref update if
461 * we were to send it and we're trying to send the refs
462 * atomically, abort the whole operation.
464 if (use_atomic)
465 return atomic_push_failure(args, remote_refs, ref);
466 /* Fallthrough for non atomic case. */
467 default:
468 continue;
470 if (!ref->deletion)
471 need_pack_data = 1;
473 if (args->dry_run || !status_report)
474 ref->status = REF_STATUS_OK;
475 else
476 ref->status = REF_STATUS_EXPECTING_REPORT;
480 * Finally, tell the other end!
482 for (ref = remote_refs; ref; ref = ref->next) {
483 char *old_hex, *new_hex;
485 if (args->dry_run || push_cert_nonce)
486 continue;
488 if (check_to_send_update(ref, args) < 0)
489 continue;
491 old_hex = sha1_to_hex(ref->old_sha1);
492 new_hex = sha1_to_hex(ref->new_sha1);
493 if (!cmds_sent) {
494 packet_buf_write(&req_buf,
495 "%s %s %s%c%s",
496 old_hex, new_hex, ref->name, 0,
497 cap_buf.buf);
498 cmds_sent = 1;
499 } else {
500 packet_buf_write(&req_buf, "%s %s %s",
501 old_hex, new_hex, ref->name);
505 if (args->stateless_rpc) {
506 if (!args->dry_run && (cmds_sent || is_repository_shallow())) {
507 packet_buf_flush(&req_buf);
508 send_sideband(out, -1, req_buf.buf, req_buf.len, LARGE_PACKET_MAX);
510 } else {
511 write_or_die(out, req_buf.buf, req_buf.len);
512 packet_flush(out);
514 strbuf_release(&req_buf);
515 strbuf_release(&cap_buf);
517 if (use_sideband && cmds_sent) {
518 memset(&demux, 0, sizeof(demux));
519 demux.proc = sideband_demux;
520 demux.data = fd;
521 demux.out = -1;
522 if (start_async(&demux))
523 die("send-pack: unable to fork off sideband demultiplexer");
524 in = demux.out;
527 if (need_pack_data && cmds_sent) {
528 if (pack_objects(out, remote_refs, extra_have, args) < 0) {
529 for (ref = remote_refs; ref; ref = ref->next)
530 ref->status = REF_STATUS_NONE;
531 if (args->stateless_rpc)
532 close(out);
533 if (git_connection_is_socket(conn))
534 shutdown(fd[0], SHUT_WR);
535 if (use_sideband)
536 finish_async(&demux);
537 fd[1] = -1;
538 return -1;
540 if (!args->stateless_rpc)
541 /* Closed by pack_objects() via start_command() */
542 fd[1] = -1;
544 if (args->stateless_rpc && cmds_sent)
545 packet_flush(out);
547 if (status_report && cmds_sent)
548 ret = receive_status(in, remote_refs);
549 else
550 ret = 0;
551 if (args->stateless_rpc)
552 packet_flush(out);
554 if (use_sideband && cmds_sent) {
555 if (finish_async(&demux)) {
556 error("error in sideband demultiplexer");
557 ret = -1;
559 close(demux.out);
562 if (ret < 0)
563 return ret;
565 if (args->porcelain)
566 return 0;
568 for (ref = remote_refs; ref; ref = ref->next) {
569 switch (ref->status) {
570 case REF_STATUS_NONE:
571 case REF_STATUS_UPTODATE:
572 case REF_STATUS_OK:
573 break;
574 default:
575 return -1;
578 return 0;