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