]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/send-pack.c
connect: discover protocol version outside of get_remote_heads
[thirdparty/git.git] / builtin / send-pack.c
CommitLineData
c2e86add 1#include "builtin.h"
b2141fc1 2#include "config.h"
2a9c3fe8 3#include "commit.h"
584c6cc9 4#include "refs.h"
f3a3214e 5#include "pkt-line.h"
de1a2fdd 6#include "sideband.h"
38b1c662 7#include "run-command.h"
6b62816c 8#include "remote.h"
47a59185 9#include "connect.h"
96249c04 10#include "send-pack.h"
de1a2fdd 11#include "quote.h"
f1863d0d 12#include "transport.h"
ff5effdf 13#include "version.h"
13eb4626 14#include "sha1-array.h"
d830d395 15#include "gpg-interface.h"
068c77a5 16#include "gettext.h"
ad6ac124 17#include "protocol.h"
61221472 18
068c77a5
DB
19static const char * const send_pack_usage[] = {
20 N_("git send-pack [--all | --mirror] [--dry-run] [--force] "
21 "[--receive-pack=<git-receive-pack>] [--verbose] [--thin] [--atomic] "
22 "[<host>:]<directory> [<ref>...]\n"
23 " --all and explicit <ref> specification are mutually exclusive."),
24 NULL,
25};
96249c04 26
6828f72f 27static struct send_pack_args args;
61221472 28
de1a2fdd
SP
29static void print_helper_status(struct ref *ref)
30{
31 struct strbuf buf = STRBUF_INIT;
32
33 for (; ref; ref = ref->next) {
34 const char *msg = NULL;
35 const char *res;
36
37 switch(ref->status) {
38 case REF_STATUS_NONE:
39 res = "error";
40 msg = "no match";
41 break;
42
43 case REF_STATUS_OK:
44 res = "ok";
45 break;
46
47 case REF_STATUS_UPTODATE:
48 res = "ok";
49 msg = "up to date";
50 break;
51
52 case REF_STATUS_REJECT_NONFASTFORWARD:
53 res = "error";
54 msg = "non-fast forward";
dbfeddb1
CR
55 break;
56
75e5c0dc
JH
57 case REF_STATUS_REJECT_FETCH_FIRST:
58 res = "error";
59 msg = "fetch first";
60 break;
61
62 case REF_STATUS_REJECT_NEEDS_FORCE:
63 res = "error";
64 msg = "needs force";
65 break;
66
631b5ef2
JH
67 case REF_STATUS_REJECT_STALE:
68 res = "error";
69 msg = "stale info";
70 break;
71
dbfeddb1
CR
72 case REF_STATUS_REJECT_ALREADY_EXISTS:
73 res = "error";
74 msg = "already exists";
de1a2fdd
SP
75 break;
76
77 case REF_STATUS_REJECT_NODELETE:
78 case REF_STATUS_REMOTE_REJECT:
79 res = "error";
80 break;
81
82 case REF_STATUS_EXPECTING_REPORT:
83 default:
84 continue;
85 }
86
87 strbuf_reset(&buf);
88 strbuf_addf(&buf, "%s %s", res, ref->name);
89 if (ref->remote_status)
90 msg = ref->remote_status;
91 if (msg) {
92 strbuf_addch(&buf, ' ');
93 quote_two_c_style(&buf, "", msg, 0);
94 }
95 strbuf_addch(&buf, '\n');
96
cdf4fb8e 97 write_or_die(1, buf.buf, buf.len);
de1a2fdd
SP
98 }
99 strbuf_release(&buf);
100}
101
68c757f2
DB
102static int send_pack_config(const char *k, const char *v, void *cb)
103{
104 git_gpg_config(k, v, NULL);
105
106 if (!strcmp(k, "push.gpgsign")) {
107 const char *value;
108 if (!git_config_get_value("push.gpgsign", &value)) {
89576613 109 switch (git_parse_maybe_bool(value)) {
68c757f2
DB
110 case 0:
111 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
112 break;
113 case 1:
114 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
115 break;
116 default:
117 if (value && !strcasecmp(value, "if-asked"))
118 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
119 else
120 return error("Invalid value for '%s'", k);
121 }
122 }
123 }
124 return 0;
125}
126
96249c04 127int cmd_send_pack(int argc, const char **argv, const char *prefix)
61221472 128{
64fcef2d
DB
129 int i, nr_refspecs = 0;
130 const char **refspecs = NULL;
96249c04 131 const char *remote_name = NULL;
b516968f 132 struct remote *remote = NULL;
96249c04 133 const char *dest = NULL;
64fcef2d
DB
134 int fd[2];
135 struct child_process *conn;
910650d2 136 struct oid_array extra_have = OID_ARRAY_INIT;
137 struct oid_array shallow = OID_ARRAY_INIT;
6d2bf96e 138 struct ref *remote_refs, *local_refs;
64fcef2d 139 int ret;
de1a2fdd 140 int helper_status = 0;
64fcef2d 141 int send_all = 0;
068c77a5 142 int verbose = 0;
64fcef2d 143 const char *receivepack = "git-receive-pack";
068c77a5
DB
144 unsigned dry_run = 0;
145 unsigned send_mirror = 0;
146 unsigned force_update = 0;
147 unsigned quiet = 0;
30261094 148 int push_cert = 0;
511155db 149 struct string_list push_options = STRING_LIST_INIT_NODUP;
068c77a5
DB
150 unsigned use_thin_pack = 0;
151 unsigned atomic = 0;
152 unsigned stateless_rpc = 0;
64fcef2d 153 int flags;
10643d4e 154 unsigned int reject_reasons;
391b1f20 155 int progress = -1;
26be19ba 156 int from_stdin = 0;
28f5d176 157 struct push_cas_option cas = {0};
ad6ac124 158 struct packet_reader reader;
84a9b58c 159
068c77a5
DB
160 struct option options[] = {
161 OPT__VERBOSITY(&verbose),
162 OPT_STRING(0, "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
163 OPT_STRING(0, "exec", &receivepack, "receive-pack", N_("receive pack program")),
164 OPT_STRING(0, "remote", &remote_name, "remote", N_("remote name")),
165 OPT_BOOL(0, "all", &send_all, N_("push all refs")),
166 OPT_BOOL('n' , "dry-run", &dry_run, N_("dry run")),
167 OPT_BOOL(0, "mirror", &send_mirror, N_("mirror all refs")),
168 OPT_BOOL('f', "force", &force_update, N_("force updates")),
30261094
DB
169 { OPTION_CALLBACK,
170 0, "signed", &push_cert, "yes|no|if-asked", N_("GPG sign the push"),
171 PARSE_OPT_OPTARG, option_parse_push_signed },
511155db
BW
172 OPT_STRING_LIST(0, "push-option", &push_options,
173 N_("server-specific"),
174 N_("option to transmit")),
068c77a5
DB
175 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
176 OPT_BOOL(0, "thin", &use_thin_pack, N_("use thin pack")),
177 OPT_BOOL(0, "atomic", &atomic, N_("request atomic transaction on remote side")),
178 OPT_BOOL(0, "stateless-rpc", &stateless_rpc, N_("use stateless RPC protocol")),
179 OPT_BOOL(0, "stdin", &from_stdin, N_("read refs from stdin")),
180 OPT_BOOL(0, "helper-status", &helper_status, N_("print status from remote helper")),
181 { OPTION_CALLBACK,
182 0, CAS_OPT_NAME, &cas, N_("refname>:<expect"),
183 N_("require old value of ref to be at this value"),
184 PARSE_OPT_OPTARG, parseopt_push_cas_option },
185 OPT_END()
186 };
61221472 187
68c757f2 188 git_config(send_pack_config, NULL);
068c77a5
DB
189 argc = parse_options(argc, argv, prefix, options, send_pack_usage, 0);
190 if (argc > 0) {
191 dest = argv[0];
192 refspecs = (const char **)(argv + 1);
193 nr_refspecs = argc - 1;
61221472 194 }
068c77a5 195
61221472 196 if (!dest)
068c77a5
DB
197 usage_with_options(send_pack_usage, options);
198
199 args.verbose = verbose;
200 args.dry_run = dry_run;
201 args.send_mirror = send_mirror;
202 args.force_update = force_update;
203 args.quiet = quiet;
204 args.push_cert = push_cert;
205 args.progress = progress;
206 args.use_thin_pack = use_thin_pack;
207 args.atomic = atomic;
208 args.stateless_rpc = stateless_rpc;
511155db 209 args.push_options = push_options.nr ? &push_options : NULL;
26be19ba
JK
210
211 if (from_stdin) {
212 struct argv_array all_refspecs = ARGV_ARRAY_INIT;
213
214 for (i = 0; i < nr_refspecs; i++)
215 argv_array_push(&all_refspecs, refspecs[i]);
216
217 if (args.stateless_rpc) {
218 const char *buf;
219 while ((buf = packet_read_line(0, NULL)))
220 argv_array_push(&all_refspecs, buf);
221 } else {
222 struct strbuf line = STRBUF_INIT;
933bea92 223 while (strbuf_getline(&line, stdin) != EOF)
26be19ba
JK
224 argv_array_push(&all_refspecs, line.buf);
225 strbuf_release(&line);
226 }
227
228 refspecs = all_refspecs.argv;
229 nr_refspecs = all_refspecs.argc;
230 }
231
28b9d6e5
AW
232 /*
233 * --all and --mirror are incompatible; neither makes sense
234 * with any refspecs.
235 */
c6777563 236 if ((nr_refspecs > 0 && (send_all || args.send_mirror)) ||
64fcef2d 237 (send_all && args.send_mirror))
068c77a5 238 usage_with_options(send_pack_usage, options);
37adac76 239
b516968f
DB
240 if (remote_name) {
241 remote = remote_get(remote_name);
28b91f8a 242 if (!remote_has_url(remote, dest)) {
b516968f
DB
243 die("Destination %s is not a uri for %s",
244 dest, remote_name);
245 }
246 }
247
391b1f20
JK
248 if (progress == -1)
249 progress = !args.quiet && isatty(2);
250 args.progress = progress;
8d32e60d 251
de1a2fdd
SP
252 if (args.stateless_rpc) {
253 conn = NULL;
254 fd[0] = 0;
255 fd[1] = 1;
256 } else {
5a277f3f 257 conn = git_connect(fd, dest, receivepack,
de1a2fdd
SP
258 args.verbose ? CONNECT_VERBOSE : 0);
259 }
96249c04 260
ad6ac124
BW
261 packet_reader_init(&reader, fd[0], NULL, 0,
262 PACKET_READ_CHOMP_NEWLINE |
263 PACKET_READ_GENTLE_ON_EOF);
264
265 switch (discover_version(&reader)) {
266 case protocol_v1:
267 case protocol_v0:
268 get_remote_heads(&reader, &remote_refs, REF_NORMAL,
269 &extra_have, &shallow);
270 break;
271 case protocol_unknown_version:
272 BUG("unknown protocol version");
273 }
64fcef2d 274
f1863d0d 275 transport_verify_remote_names(nr_refspecs, refspecs);
64fcef2d
DB
276
277 local_refs = get_local_heads();
96249c04 278
64fcef2d
DB
279 flags = MATCH_REFS_NONE;
280
281 if (send_all)
282 flags |= MATCH_REFS_ALL;
283 if (args.send_mirror)
284 flags |= MATCH_REFS_MIRROR;
285
286 /* match them up */
29753cdd 287 if (match_push_refs(local_refs, &remote_refs, nr_refspecs, refspecs, flags))
64fcef2d 288 return -1;
96249c04 289
91048a95
JH
290 if (!is_empty_cas(&cas))
291 apply_push_cas(&cas, remote, remote_refs);
292
20e8b465
TRC
293 set_ref_status_for_push(remote_refs, args.send_mirror,
294 args.force_update);
295
64fcef2d 296 ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
96249c04 297
de1a2fdd
SP
298 if (helper_status)
299 print_helper_status(remote_refs);
300
64fcef2d 301 close(fd[1]);
7f8e9828 302 close(fd[0]);
64fcef2d 303
98158e9c 304 ret |= finish_connect(conn);
64fcef2d 305
de1a2fdd 306 if (!helper_status)
10643d4e 307 transport_print_push_status(dest, remote_refs, args.verbose, 0, &reject_reasons);
64fcef2d
DB
308
309 if (!args.dry_run && remote) {
310 struct ref *ref;
311 for (ref = remote_refs; ref; ref = ref->next)
f1863d0d 312 transport_update_tracking_ref(remote, ref, args.verbose);
64fcef2d
DB
313 }
314
f1863d0d 315 if (!ret && !transport_refs_pushed(remote_refs))
64fcef2d
DB
316 fprintf(stderr, "Everything up-to-date\n");
317
318 return ret;
61221472 319}