blob: 8bba57270b00b72c7e70fe749880a29c1ddfea1f [file] [log] [blame]
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04003#include "remote.h"
Brandon Williamsad6ac122018-03-14 11:31:45 -07004#include "connect.h"
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04005#include "strbuf.h"
6#include "walker.h"
7#include "http.h"
Stefan Bellerd807c4a2018-04-10 14:26:18 -07008#include "exec-cmd.h"
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07009#include "run-command.h"
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -070010#include "pkt-line.h"
Junio C Hamano05c1eb12013-08-02 15:14:50 -070011#include "string-list.h"
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -070012#include "sideband.h"
Junio C Hamano222b1212013-07-08 22:16:31 -070013#include "argv-array.h"
Jeff King2501aff2013-09-28 04:31:45 -040014#include "credential.h"
Nguyễn Thái Ngọc Duy16094882013-12-05 20:02:50 +070015#include "sha1-array.h"
Dave Borowitz30261092015-08-19 11:26:46 -040016#include "send-pack.h"
Brandon Williamsad6ac122018-03-14 11:31:45 -070017#include "protocol.h"
Jeff King90dce212018-02-19 14:50:14 -050018#include "quote.h"
Johannes Schindelinc1284b22019-03-04 07:33:46 -080019#include "transport.h"
Daniel Barkalowa2d725b2009-08-05 01:01:56 -040020
Shawn O. Pearce37a87682009-10-30 17:47:26 -070021static struct remote *remote;
Jeff Kingb227bbc2013-09-28 04:35:25 -040022/* always ends with a trailing slash */
23static struct strbuf url = STRBUF_INIT;
Shawn O. Pearce37a87682009-10-30 17:47:26 -070024
Shawn O. Pearceef08ef92009-10-30 17:47:29 -070025struct options {
26 int verbosity;
27 unsigned long depth;
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +070028 char *deepen_since;
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +070029 struct string_list deepen_not;
Brandon Williams511155d2017-03-22 15:22:00 -070030 struct string_list push_options;
Jeff Hostetleracb0c572017-12-08 15:58:44 +000031 char *filter;
Shawn O. Pearceef08ef92009-10-30 17:47:29 -070032 unsigned progress : 1,
Nguyễn Thái Ngọc Duy9ba38042013-07-21 15:18:05 +070033 check_self_contained_and_connected : 1,
Nguyễn Thái Ngọc Duy16094882013-12-05 20:02:50 +070034 cloning : 1,
35 update_shallow : 1,
Shawn O. Pearceae4efe12009-10-30 17:47:30 -070036 followtags : 1,
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -070037 dry_run : 1,
Junio C Hamano0ea47f92014-09-15 14:59:00 -070038 thin : 1,
Dave Borowitz30261092015-08-19 11:26:46 -040039 /* One of the SEND_PACK_PUSH_CERT_* constants. */
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +070040 push_cert : 2,
Jonathan Tan88e2f9e2017-12-05 16:58:49 +000041 deepen_relative : 1,
42 from_promisor : 1,
43 no_dependents : 1;
Shawn O. Pearceef08ef92009-10-30 17:47:29 -070044};
45static struct options options;
Junio C Hamano05c1eb12013-08-02 15:14:50 -070046static struct string_list cas_options = STRING_LIST_INIT_DUP;
Shawn O. Pearceef08ef92009-10-30 17:47:29 -070047
Shawn O. Pearceef08ef92009-10-30 17:47:29 -070048static int set_option(const char *name, const char *value)
49{
50 if (!strcmp(name, "verbosity")) {
51 char *end;
52 int v = strtol(value, &end, 10);
53 if (value == end || *end)
54 return -1;
55 options.verbosity = v;
56 return 0;
57 }
58 else if (!strcmp(name, "progress")) {
59 if (!strcmp(value, "true"))
60 options.progress = 1;
61 else if (!strcmp(value, "false"))
62 options.progress = 0;
63 else
64 return -1;
Shawn O. Pearce249b2002009-10-30 17:47:42 -070065 return 0;
Shawn O. Pearceef08ef92009-10-30 17:47:29 -070066 }
67 else if (!strcmp(name, "depth")) {
68 char *end;
69 unsigned long v = strtoul(value, &end, 10);
70 if (value == end || *end)
71 return -1;
72 options.depth = v;
Shawn O. Pearce249b2002009-10-30 17:47:42 -070073 return 0;
Shawn O. Pearceef08ef92009-10-30 17:47:29 -070074 }
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +070075 else if (!strcmp(name, "deepen-since")) {
76 options.deepen_since = xstrdup(value);
77 return 0;
78 }
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +070079 else if (!strcmp(name, "deepen-not")) {
80 string_list_append(&options.deepen_not, value);
81 return 0;
82 }
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +070083 else if (!strcmp(name, "deepen-relative")) {
84 if (!strcmp(value, "true"))
85 options.deepen_relative = 1;
86 else if (!strcmp(value, "false"))
87 options.deepen_relative = 0;
88 else
89 return -1;
90 return 0;
91 }
Shawn O. Pearceef08ef92009-10-30 17:47:29 -070092 else if (!strcmp(name, "followtags")) {
93 if (!strcmp(value, "true"))
94 options.followtags = 1;
95 else if (!strcmp(value, "false"))
96 options.followtags = 0;
97 else
98 return -1;
Shawn O. Pearce249b2002009-10-30 17:47:42 -070099 return 0;
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700100 }
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700101 else if (!strcmp(name, "dry-run")) {
102 if (!strcmp(value, "true"))
103 options.dry_run = 1;
104 else if (!strcmp(value, "false"))
105 options.dry_run = 0;
106 else
107 return -1;
108 return 0;
109 }
Nguyễn Thái Ngọc Duy9ba38042013-07-21 15:18:05 +0700110 else if (!strcmp(name, "check-connectivity")) {
111 if (!strcmp(value, "true"))
112 options.check_self_contained_and_connected = 1;
113 else if (!strcmp(value, "false"))
114 options.check_self_contained_and_connected = 0;
115 else
116 return -1;
117 return 0;
118 }
Junio C Hamano05c1eb12013-08-02 15:14:50 -0700119 else if (!strcmp(name, "cas")) {
120 struct strbuf val = STRBUF_INIT;
121 strbuf_addf(&val, "--" CAS_OPT_NAME "=%s", value);
122 string_list_append(&cas_options, val.buf);
123 strbuf_release(&val);
124 return 0;
Nguyễn Thái Ngọc Duy16094882013-12-05 20:02:50 +0700125 } else if (!strcmp(name, "cloning")) {
126 if (!strcmp(value, "true"))
127 options.cloning = 1;
128 else if (!strcmp(value, "false"))
129 options.cloning = 0;
130 else
131 return -1;
132 return 0;
133 } else if (!strcmp(name, "update-shallow")) {
134 if (!strcmp(value, "true"))
135 options.update_shallow = 1;
136 else if (!strcmp(value, "false"))
137 options.update_shallow = 0;
138 else
139 return -1;
140 return 0;
Junio C Hamano0ea47f92014-09-15 14:59:00 -0700141 } else if (!strcmp(name, "pushcert")) {
142 if (!strcmp(value, "true"))
Dave Borowitz30261092015-08-19 11:26:46 -0400143 options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
Junio C Hamano0ea47f92014-09-15 14:59:00 -0700144 else if (!strcmp(value, "false"))
Dave Borowitz30261092015-08-19 11:26:46 -0400145 options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
146 else if (!strcmp(value, "if-asked"))
147 options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
Junio C Hamano0ea47f92014-09-15 14:59:00 -0700148 else
149 return -1;
150 return 0;
Brandon Williams511155d2017-03-22 15:22:00 -0700151 } else if (!strcmp(name, "push-option")) {
Jeff King90dce212018-02-19 14:50:14 -0500152 if (*value != '"')
153 string_list_append(&options.push_options, value);
154 else {
155 struct strbuf unquoted = STRBUF_INIT;
156 if (unquote_c_style(&unquoted, value, NULL) < 0)
Johannes Schindelined8b4132019-03-05 15:20:40 -0800157 die(_("invalid quoting in push-option value: '%s'"), value);
Jeff King90dce212018-02-19 14:50:14 -0500158 string_list_append_nodup(&options.push_options,
159 strbuf_detach(&unquoted, NULL));
160 }
Brandon Williams511155d2017-03-22 15:22:00 -0700161 return 0;
Eric Wongc915f112016-02-03 04:09:14 +0000162
163#if LIBCURL_VERSION_NUM >= 0x070a08
164 } else if (!strcmp(name, "family")) {
165 if (!strcmp(value, "ipv4"))
166 git_curl_ipresolve = CURL_IPRESOLVE_V4;
167 else if (!strcmp(value, "ipv6"))
168 git_curl_ipresolve = CURL_IPRESOLVE_V6;
169 else if (!strcmp(value, "all"))
170 git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
171 else
172 return -1;
173 return 0;
174#endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
Jonathan Tan88e2f9e2017-12-05 16:58:49 +0000175 } else if (!strcmp(name, "from-promisor")) {
176 options.from_promisor = 1;
177 return 0;
178 } else if (!strcmp(name, "no-dependents")) {
179 options.no_dependents = 1;
180 return 0;
Jeff Hostetleracb0c572017-12-08 15:58:44 +0000181 } else if (!strcmp(name, "filter")) {
Elijah Newrenc3b9bc92018-09-05 10:03:07 -0700182 options.filter = xstrdup(value);
Jeff Hostetleracb0c572017-12-08 15:58:44 +0000183 return 0;
Nguyễn Thái Ngọc Duy16094882013-12-05 20:02:50 +0700184 } else {
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700185 return 1 /* unsupported */;
186 }
187}
188
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700189struct discovery {
Brandon Williamsf08a5d42018-03-15 10:31:36 -0700190 char *service;
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700191 char *buf_alloc;
192 char *buf;
193 size_t len;
Jeff King2a455202013-02-20 15:07:19 -0500194 struct ref *refs;
brian m. carlson910650d2017-03-31 01:40:00 +0000195 struct oid_array shallow;
Brandon Williams49e85e92018-03-15 10:31:37 -0700196 enum protocol_version version;
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700197 unsigned proto_git : 1;
198};
199static struct discovery *last_discovery;
200
Jeff Kingb8054bb2013-02-20 15:07:11 -0500201static struct ref *parse_git_refs(struct discovery *heads, int for_push)
202{
203 struct ref *list = NULL;
Brandon Williamsad6ac122018-03-14 11:31:45 -0700204 struct packet_reader reader;
205
206 packet_reader_init(&reader, -1, heads->buf, heads->len,
207 PACKET_READ_CHOMP_NEWLINE |
Masaya Suzuki2d103c32018-12-29 13:19:15 -0800208 PACKET_READ_GENTLE_ON_EOF |
209 PACKET_READ_DIE_ON_ERR_PACKET);
Brandon Williamsad6ac122018-03-14 11:31:45 -0700210
Brandon Williams49e85e92018-03-15 10:31:37 -0700211 heads->version = discover_version(&reader);
212 switch (heads->version) {
Brandon Williams8f6982b2018-03-14 11:31:47 -0700213 case protocol_v2:
Brandon Williams0f1dc532018-03-15 10:31:41 -0700214 /*
215 * Do nothing. This isn't a list of refs but rather a
216 * capability advertisement. Client would have run
217 * 'stateless-connect' so we'll dump this capability listing
218 * and let them request the refs themselves.
219 */
Brandon Williams8f6982b2018-03-14 11:31:47 -0700220 break;
Brandon Williamsad6ac122018-03-14 11:31:45 -0700221 case protocol_v1:
222 case protocol_v0:
223 get_remote_heads(&reader, &list, for_push ? REF_NORMAL : 0,
224 NULL, &heads->shallow);
225 break;
226 case protocol_unknown_version:
227 BUG("unknown protocol version");
228 }
229
Jeff Kingb8054bb2013-02-20 15:07:11 -0500230 return list;
231}
232
233static struct ref *parse_info_refs(struct discovery *heads)
234{
235 char *data, *start, *mid;
236 char *ref_name;
237 int i = 0;
238
239 struct ref *refs = NULL;
240 struct ref *ref = NULL;
241 struct ref *last_ref = NULL;
242
243 data = heads->buf;
244 start = NULL;
245 mid = data;
246 while (i < heads->len) {
247 if (!start) {
248 start = &data[i];
249 }
250 if (data[i] == '\t')
251 mid = &data[i];
252 if (data[i] == '\n') {
253 if (mid - start != 40)
Johannes Schindelined8b4132019-03-05 15:20:40 -0800254 die(_("%sinfo/refs not valid: is this a git repository?"),
Johannes Schindelinc1284b22019-03-04 07:33:46 -0800255 transport_anonymize_url(url.buf));
Jeff Kingb8054bb2013-02-20 15:07:11 -0500256 data[i] = 0;
257 ref_name = mid + 1;
Jeff King6f687c22015-09-24 17:08:09 -0400258 ref = alloc_ref(ref_name);
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000259 get_oid_hex(start, &ref->old_oid);
Jeff Kingb8054bb2013-02-20 15:07:11 -0500260 if (!refs)
261 refs = ref;
262 if (last_ref)
263 last_ref->next = ref;
264 last_ref = ref;
265 start = NULL;
266 }
267 i++;
268 }
269
270 ref = alloc_ref("HEAD");
Jeff Kingb227bbc2013-09-28 04:35:25 -0400271 if (!http_fetch_ref(url.buf, ref) &&
Jeff Kingb8054bb2013-02-20 15:07:11 -0500272 !resolve_remote_symref(ref, refs)) {
273 ref->next = refs;
274 refs = ref;
275 } else {
276 free(ref);
277 }
278
279 return refs;
280}
281
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700282static void free_discovery(struct discovery *d)
283{
284 if (d) {
285 if (d == last_discovery)
286 last_discovery = NULL;
brian m. carlsonee3051b2017-03-26 16:01:37 +0000287 free(d->shallow.oid);
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700288 free(d->buf_alloc);
Jeff King2a455202013-02-20 15:07:19 -0500289 free_refs(d->refs);
Brandon Williamsf08a5d42018-03-15 10:31:36 -0700290 free(d->service);
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700291 free(d);
292 }
293}
294
Jeff Kingfc1b7742014-05-22 05:30:29 -0400295static int show_http_message(struct strbuf *type, struct strbuf *charset,
296 struct strbuf *msg)
Jeff King426e70d2013-04-05 18:17:23 -0400297{
298 const char *p, *eol;
299
300 /*
301 * We only show text/plain parts, as other types are likely
302 * to be ugly to look at on the user's terminal.
Jeff King426e70d2013-04-05 18:17:23 -0400303 */
Jeff Kingbf197fd2014-05-22 05:29:47 -0400304 if (strcmp(type->buf, "text/plain"))
Jeff King426e70d2013-04-05 18:17:23 -0400305 return -1;
Jeff Kingfc1b7742014-05-22 05:30:29 -0400306 if (charset->len)
307 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
Jeff King426e70d2013-04-05 18:17:23 -0400308
309 strbuf_trim(msg);
310 if (!msg->len)
311 return -1;
312
313 p = msg->buf;
314 do {
315 eol = strchrnul(p, '\n');
316 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
317 p = eol + 1;
318 } while(*eol);
319 return 0;
320}
321
Brandon Williams884e5862018-03-15 10:31:39 -0700322static int get_protocol_http_header(enum protocol_version version,
323 struct strbuf *header)
324{
325 if (version > 0) {
326 strbuf_addf(header, GIT_PROTOCOL_HEADER ": version=%d",
327 version);
328
329 return 1;
330 }
331
332 return 0;
333}
334
Jeff King8ee3e122019-02-06 14:18:48 -0500335static void check_smart_http(struct discovery *d, const char *service,
336 struct strbuf *type)
337{
338 const char *p;
339 struct packet_reader reader;
340
341 /*
342 * If we don't see x-$service-advertisement, then it's not smart-http.
343 * But once we do, we commit to it and assume any other protocol
344 * violations are hard errors.
345 */
346 if (!skip_prefix(type->buf, "application/x-", &p) ||
347 !skip_prefix(p, service, &p) ||
348 strcmp(p, "-advertisement"))
349 return;
350
351 packet_reader_init(&reader, -1, d->buf, d->len,
352 PACKET_READ_CHOMP_NEWLINE |
353 PACKET_READ_DIE_ON_ERR_PACKET);
354 if (packet_reader_read(&reader) != PACKET_READ_NORMAL)
Junio C Hamanoaa1edf12019-04-16 19:28:05 +0900355 die(_("invalid server response; expected service, got flush packet"));
Jeff King8ee3e122019-02-06 14:18:48 -0500356
357 if (skip_prefix(reader.line, "# service=", &p) && !strcmp(p, service)) {
358 /*
359 * The header can include additional metadata lines, up
360 * until a packet flush marker. Ignore these now, but
361 * in the future we might start to scan them.
362 */
363 for (;;) {
364 packet_reader_read(&reader);
365 if (reader.pktlen <= 0) {
366 break;
367 }
368 }
369
370 /*
371 * v0 smart http; callers expect us to soak up the
372 * service and header packets
373 */
374 d->buf = reader.src_buffer;
375 d->len = reader.src_len;
376 d->proto_git = 1;
377
Jeff Kingcbdb8d12019-02-06 14:18:58 -0500378 } else if (!strcmp(reader.line, "version 2")) {
Jeff King8ee3e122019-02-06 14:18:48 -0500379 /*
380 * v2 smart http; do not consume version packet, which will
381 * be handled elsewhere.
382 */
383 d->proto_git = 1;
384
385 } else {
Junio C Hamanoaa1edf12019-04-16 19:28:05 +0900386 die(_("invalid server response; got '%s'"), reader.line);
Jeff King8ee3e122019-02-06 14:18:48 -0500387 }
388}
389
David Aguilar24d36f12014-08-31 13:11:31 -0700390static struct discovery *discover_refs(const char *service, int for_push)
Daniel Barkalowa2d725b2009-08-05 01:01:56 -0400391{
Shawn Pearce4656bf42013-01-31 13:02:07 -0800392 struct strbuf type = STRBUF_INIT;
Jeff Kingfc1b7742014-05-22 05:30:29 -0400393 struct strbuf charset = STRBUF_INIT;
Daniel Barkalowa2d725b2009-08-05 01:01:56 -0400394 struct strbuf buffer = STRBUF_INIT;
Jeff Kingc65d5692013-09-28 04:35:10 -0400395 struct strbuf refs_url = STRBUF_INIT;
Jeff King050ef362013-09-28 04:35:35 -0400396 struct strbuf effective_url = STRBUF_INIT;
Brandon Williams884e5862018-03-15 10:31:39 -0700397 struct strbuf protocol_header = STRBUF_INIT;
398 struct string_list extra_headers = STRING_LIST_INIT_DUP;
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700399 struct discovery *last = last_discovery;
Jeff King243c3292012-09-20 13:00:22 -0400400 int http_ret, maybe_smart = 0;
Jeff Kingfcaa6e62016-12-06 13:24:38 -0500401 struct http_get_options http_options;
Brandon Williamsa4d78ce2018-03-15 10:31:42 -0700402 enum protocol_version version = get_protocol_version_config();
Daniel Barkalowa2d725b2009-08-05 01:01:56 -0400403
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700404 if (last && !strcmp(service, last->service))
405 return last;
406 free_discovery(last);
Daniel Barkalowa2d725b2009-08-05 01:01:56 -0400407
Jeff Kingb227bbc2013-09-28 04:35:25 -0400408 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
Christian Couder59556542013-11-30 21:55:40 +0100409 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
Jeff King02572c22012-09-20 17:30:58 -0400410 git_env_bool("GIT_SMART_HTTP", 1)) {
Jeff King243c3292012-09-20 13:00:22 -0400411 maybe_smart = 1;
Jeff Kingb227bbc2013-09-28 04:35:25 -0400412 if (!strchr(url.buf, '?'))
Jeff Kingc65d5692013-09-28 04:35:10 -0400413 strbuf_addch(&refs_url, '?');
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700414 else
Jeff Kingc65d5692013-09-28 04:35:10 -0400415 strbuf_addch(&refs_url, '&');
416 strbuf_addf(&refs_url, "service=%s", service);
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700417 }
Daniel Barkalowa2d725b2009-08-05 01:01:56 -0400418
Brandon Williamsa4d78ce2018-03-15 10:31:42 -0700419 /*
420 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
421 * to perform a push, then fallback to v0 since the client doesn't know
422 * how to push yet using v2.
423 */
424 if (version == protocol_v2 && !strcmp("git-receive-pack", service))
425 version = protocol_v0;
426
Brandon Williams884e5862018-03-15 10:31:39 -0700427 /* Add the extra Git-Protocol header */
Brandon Williamsa4d78ce2018-03-15 10:31:42 -0700428 if (get_protocol_http_header(version, &protocol_header))
Brandon Williams884e5862018-03-15 10:31:39 -0700429 string_list_append(&extra_headers, protocol_header.buf);
430
Jeff Kingfcaa6e62016-12-06 13:24:38 -0500431 memset(&http_options, 0, sizeof(http_options));
432 http_options.content_type = &type;
433 http_options.charset = &charset;
434 http_options.effective_url = &effective_url;
435 http_options.base_url = &url;
Brandon Williams884e5862018-03-15 10:31:39 -0700436 http_options.extra_headers = &extra_headers;
Jeff King50d34132016-12-06 13:24:41 -0500437 http_options.initial_request = 1;
Jeff Kingfcaa6e62016-12-06 13:24:38 -0500438 http_options.no_cache = 1;
Jeff King1bbcc222013-09-28 04:31:23 -0400439
Jeff Kingfcaa6e62016-12-06 13:24:38 -0500440 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
Daniel Barkalowa2d725b2009-08-05 01:01:56 -0400441 switch (http_ret) {
442 case HTTP_OK:
443 break;
444 case HTTP_MISSING_TARGET:
Jeff Kingfc1b7742014-05-22 05:30:29 -0400445 show_http_message(&type, &charset, &buffer);
Johannes Schindelined8b4132019-03-05 15:20:40 -0800446 die(_("repository '%s' not found"),
Johannes Schindelinc1284b22019-03-04 07:33:46 -0800447 transport_anonymize_url(url.buf));
Scott Chacon42653c02010-04-01 15:14:35 -0700448 case HTTP_NOAUTH:
Jeff Kingfc1b7742014-05-22 05:30:29 -0400449 show_http_message(&type, &charset, &buffer);
Johannes Schindelined8b4132019-03-05 15:20:40 -0800450 die(_("Authentication failed for '%s'"),
Johannes Schindelinc1284b22019-03-04 07:33:46 -0800451 transport_anonymize_url(url.buf));
Daniel Barkalowa2d725b2009-08-05 01:01:56 -0400452 default:
Jeff Kingfc1b7742014-05-22 05:30:29 -0400453 show_http_message(&type, &charset, &buffer);
Johannes Schindelined8b4132019-03-05 15:20:40 -0800454 die(_("unable to access '%s': %s"),
Johannes Schindelinc1284b22019-03-04 07:33:46 -0800455 transport_anonymize_url(url.buf), curl_errorstr);
Daniel Barkalowa2d725b2009-08-05 01:01:56 -0400456 }
457
Johannes Schindelinc1284b22019-03-04 07:33:46 -0800458 if (options.verbosity && !starts_with(refs_url.buf, url.buf)) {
459 char *u = transport_anonymize_url(url.buf);
460 warning(_("redirecting to %s"), u);
461 free(u);
462 }
Jeff King50d34132016-12-06 13:24:41 -0500463
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700464 last= xcalloc(1, sizeof(*last_discovery));
Brandon Williamsf08a5d42018-03-15 10:31:36 -0700465 last->service = xstrdup(service);
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700466 last->buf_alloc = strbuf_detach(&buffer, &last->len);
467 last->buf = last->buf_alloc;
468
Jeff King8ee3e122019-02-06 14:18:48 -0500469 if (maybe_smart)
470 check_smart_http(last, service, &type);
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700471
Jeff King2a455202013-02-20 15:07:19 -0500472 if (last->proto_git)
473 last->refs = parse_git_refs(last, for_push);
474 else
475 last->refs = parse_info_refs(last);
476
Jeff Kingc65d5692013-09-28 04:35:10 -0400477 strbuf_release(&refs_url);
Shawn Pearce4656bf42013-01-31 13:02:07 -0800478 strbuf_release(&type);
Jeff Kingfc1b7742014-05-22 05:30:29 -0400479 strbuf_release(&charset);
Jeff King050ef362013-09-28 04:35:35 -0400480 strbuf_release(&effective_url);
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700481 strbuf_release(&buffer);
Brandon Williams884e5862018-03-15 10:31:39 -0700482 strbuf_release(&protocol_header);
483 string_list_clear(&extra_headers, 0);
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700484 last_discovery = last;
485 return last;
486}
487
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700488static struct ref *get_refs(int for_push)
489{
490 struct discovery *heads;
491
492 if (for_push)
Jeff King2a455202013-02-20 15:07:19 -0500493 heads = discover_refs("git-receive-pack", for_push);
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700494 else
Jeff King2a455202013-02-20 15:07:19 -0500495 heads = discover_refs("git-upload-pack", for_push);
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700496
Jeff King2a455202013-02-20 15:07:19 -0500497 return heads->refs;
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700498}
499
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700500static void output_refs(struct ref *refs)
501{
502 struct ref *posn;
503 for (posn = refs; posn; posn = posn->next) {
504 if (posn->symref)
505 printf("@%s %s\n", posn->symref, posn->name);
506 else
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000507 printf("%s %s\n", oid_to_hex(&posn->old_oid), posn->name);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700508 }
509 printf("\n");
510 fflush(stdout);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700511}
512
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700513struct rpc_state {
514 const char *service_name;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700515 char *service_url;
516 char *hdr_content_type;
517 char *hdr_accept;
Brandon Williams884e5862018-03-15 10:31:39 -0700518 char *protocol_header;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700519 char *buf;
520 size_t alloc;
521 size_t len;
522 size_t pos;
523 int in;
524 int out;
David Turner296b8472016-11-18 15:30:49 -0500525 int any_written;
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700526 unsigned gzip_request : 1;
Martin Storsjö6c81a992009-12-01 12:33:39 +0200527 unsigned initial_buffer : 1;
Jonathan Tana97d0072019-02-21 12:24:41 -0800528
529 /*
530 * Whenever a pkt-line is read into buf, append the 4 characters
531 * denoting its length before appending the payload.
532 */
533 unsigned write_line_lengths : 1;
534
535 /*
536 * Used by rpc_out; initialize to 0. This is true if a flush has been
537 * read, but the corresponding line length (if write_line_lengths is
538 * true) and EOF have not been sent to libcurl. Since each flush marks
539 * the end of a request, each flush must be completely sent before any
540 * further reading occurs.
541 */
542 unsigned flush_read_but_not_sent : 1;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700543};
544
Jonathan Tan78ad9172019-02-21 12:24:40 -0800545/*
546 * Appends the result of reading from rpc->out to the string represented by
547 * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
548 * enough space, 0 otherwise.
549 *
Jonathan Tana97d0072019-02-21 12:24:41 -0800550 * If rpc->write_line_lengths is true, appends the line length as a 4-byte
551 * hexadecimal string before appending the result described above.
552 *
553 * Writes the total number of bytes appended into appended.
Jonathan Tan78ad9172019-02-21 12:24:40 -0800554 */
Jonathan Tana97d0072019-02-21 12:24:41 -0800555static int rpc_read_from_out(struct rpc_state *rpc, int options,
556 size_t *appended,
557 enum packet_read_status *status) {
558 size_t left;
559 char *buf;
560 int pktlen_raw;
561
562 if (rpc->write_line_lengths) {
563 left = rpc->alloc - rpc->len - 4;
564 buf = rpc->buf + rpc->len + 4;
565 } else {
566 left = rpc->alloc - rpc->len;
567 buf = rpc->buf + rpc->len;
568 }
Jonathan Tan78ad9172019-02-21 12:24:40 -0800569
570 if (left < LARGE_PACKET_MAX)
571 return 0;
572
Jonathan Tana97d0072019-02-21 12:24:41 -0800573 *status = packet_read_with_status(rpc->out, NULL, NULL, buf,
574 left, &pktlen_raw, options);
575 if (*status != PACKET_READ_EOF) {
576 *appended = pktlen_raw + (rpc->write_line_lengths ? 4 : 0);
577 rpc->len += *appended;
578 }
579
580 if (rpc->write_line_lengths) {
581 switch (*status) {
582 case PACKET_READ_EOF:
583 if (!(options & PACKET_READ_GENTLE_ON_EOF))
Junio C Hamanoaa1edf12019-04-16 19:28:05 +0900584 die(_("shouldn't have EOF when not gentle on EOF"));
Jonathan Tana97d0072019-02-21 12:24:41 -0800585 break;
586 case PACKET_READ_NORMAL:
587 set_packet_header(buf - 4, *appended);
588 break;
589 case PACKET_READ_DELIM:
590 memcpy(buf - 4, "0001", 4);
591 break;
592 case PACKET_READ_FLUSH:
593 memcpy(buf - 4, "0000", 4);
594 break;
595 }
596 }
597
Jonathan Tan78ad9172019-02-21 12:24:40 -0800598 return 1;
599}
600
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700601static size_t rpc_out(void *ptr, size_t eltsize,
602 size_t nmemb, void *buffer_)
603{
604 size_t max = eltsize * nmemb;
605 struct rpc_state *rpc = buffer_;
606 size_t avail = rpc->len - rpc->pos;
Jonathan Tana97d0072019-02-21 12:24:41 -0800607 enum packet_read_status status;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700608
609 if (!avail) {
Martin Storsjö6c81a992009-12-01 12:33:39 +0200610 rpc->initial_buffer = 0;
Jonathan Tan78ad9172019-02-21 12:24:40 -0800611 rpc->len = 0;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700612 rpc->pos = 0;
Jonathan Tana97d0072019-02-21 12:24:41 -0800613 if (!rpc->flush_read_but_not_sent) {
614 if (!rpc_read_from_out(rpc, 0, &avail, &status))
615 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
616 if (status == PACKET_READ_FLUSH)
617 rpc->flush_read_but_not_sent = 1;
618 }
619 /*
620 * If flush_read_but_not_sent is true, we have already read one
621 * full request but have not fully sent it + EOF, which is why
622 * we need to refrain from reading.
623 */
624 }
625 if (rpc->flush_read_but_not_sent) {
626 if (!avail) {
627 /*
628 * The line length either does not need to be sent at
629 * all or has already been completely sent. Now we can
630 * return 0, indicating EOF, meaning that the flush has
631 * been fully sent.
632 */
633 rpc->flush_read_but_not_sent = 0;
634 return 0;
635 }
636 /*
637 * If avail is non-zerp, the line length for the flush still
638 * hasn't been fully sent. Proceed with sending the line
639 * length.
640 */
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700641 }
642
Tay Ray Chuan48310602009-11-24 10:31:30 +0800643 if (max < avail)
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700644 avail = max;
645 memcpy(ptr, rpc->buf + rpc->pos, avail);
646 rpc->pos += avail;
647 return avail;
648}
649
Martin Storsjö6c81a992009-12-01 12:33:39 +0200650#ifndef NO_CURL_IOCTL
Junio C Hamano5092d3e2010-01-11 22:30:36 -0800651static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
Martin Storsjö6c81a992009-12-01 12:33:39 +0200652{
653 struct rpc_state *rpc = clientp;
654
655 switch (cmd) {
656 case CURLIOCMD_NOP:
657 return CURLIOE_OK;
658
659 case CURLIOCMD_RESTARTREAD:
660 if (rpc->initial_buffer) {
661 rpc->pos = 0;
662 return CURLIOE_OK;
663 }
Johannes Schindelined8b4132019-03-05 15:20:40 -0800664 error(_("unable to rewind rpc post data - try increasing http.postBuffer"));
Martin Storsjö6c81a992009-12-01 12:33:39 +0200665 return CURLIOE_FAILRESTART;
666
667 default:
668 return CURLIOE_UNKNOWNCMD;
669 }
670}
671#endif
672
Masaya Suzukicf2fb922019-01-10 11:33:48 -0800673struct rpc_in_data {
674 struct rpc_state *rpc;
Masaya Suzukib79bdd82019-01-10 11:33:49 -0800675 struct active_request_slot *slot;
Masaya Suzukicf2fb922019-01-10 11:33:48 -0800676};
677
678/*
679 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
680 * from ptr.
681 */
Dan McGeea04ff3e2011-05-03 23:47:27 +0800682static size_t rpc_in(char *ptr, size_t eltsize,
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700683 size_t nmemb, void *buffer_)
684{
685 size_t size = eltsize * nmemb;
Masaya Suzukicf2fb922019-01-10 11:33:48 -0800686 struct rpc_in_data *data = buffer_;
Masaya Suzukib79bdd82019-01-10 11:33:49 -0800687 long response_code;
688
689 if (curl_easy_getinfo(data->slot->curl, CURLINFO_RESPONSE_CODE,
690 &response_code) != CURLE_OK)
691 return size;
692 if (response_code >= 300)
693 return size;
David Turner296b8472016-11-18 15:30:49 -0500694 if (size)
Masaya Suzukicf2fb922019-01-10 11:33:48 -0800695 data->rpc->any_written = 1;
696 write_or_die(data->rpc->in, ptr, size);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700697 return size;
698}
699
Jeff King3a347ed2013-10-31 02:36:26 -0400700static int run_slot(struct active_request_slot *slot,
701 struct slot_results *results)
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800702{
Jeff Kingb81401c2012-08-27 09:27:15 -0400703 int err;
Jeff King3a347ed2013-10-31 02:36:26 -0400704 struct slot_results results_buf;
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800705
Jeff King3a347ed2013-10-31 02:36:26 -0400706 if (!results)
707 results = &results_buf;
708
Jeff Kingbeed3362014-02-18 05:34:20 -0500709 err = run_one_slot(slot, results);
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800710
Jeff Kingb81401c2012-08-27 09:27:15 -0400711 if (err != HTTP_OK && err != HTTP_REAUTH) {
Shawn Pearce00540452016-02-13 17:39:34 -0800712 struct strbuf msg = STRBUF_INIT;
713 if (results->http_code && results->http_code != 200)
714 strbuf_addf(&msg, "HTTP %ld", results->http_code);
715 if (results->curl_result != CURLE_OK) {
716 if (msg.len)
717 strbuf_addch(&msg, ' ');
718 strbuf_addf(&msg, "curl %d", results->curl_result);
719 if (curl_errorstr[0]) {
720 strbuf_addch(&msg, ' ');
721 strbuf_addstr(&msg, curl_errorstr);
722 }
723 }
Johannes Schindelined8b4132019-03-05 15:20:40 -0800724 error(_("RPC failed; %s"), msg.buf);
Shawn Pearce00540452016-02-13 17:39:34 -0800725 strbuf_release(&msg);
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800726 }
727
728 return err;
729}
730
Jeff King3a347ed2013-10-31 02:36:26 -0400731static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800732{
733 struct active_request_slot *slot;
Johannes Schindelin8cb01e22016-04-27 14:20:37 +0200734 struct curl_slist *headers = http_copy_default_headers();
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800735 struct strbuf buf = STRBUF_INIT;
736 int err;
737
738 slot = get_active_slot();
739
740 headers = curl_slist_append(headers, rpc->hdr_content_type);
741 headers = curl_slist_append(headers, rpc->hdr_accept);
742
743 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
744 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
745 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
Shawn O. Pearceaa90b962012-09-19 16:12:02 -0700746 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800747 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
748 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
749 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
750 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
751 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
752
Jeff King3a347ed2013-10-31 02:36:26 -0400753 err = run_slot(slot, results);
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800754
755 curl_slist_free_all(headers);
756 strbuf_release(&buf);
757 return err;
758}
759
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +0100760static curl_off_t xcurl_off_t(size_t len)
761{
Torsten Bögershausencb8010b2018-11-09 18:41:10 +0100762 uintmax_t size = len;
763 if (size > maximum_signed_value_of_type(curl_off_t))
Johannes Schindelined8b4132019-03-05 15:20:40 -0800764 die(_("cannot handle pushes this big"));
Torsten Bögershausencb8010b2018-11-09 18:41:10 +0100765 return (curl_off_t)size;
David Turner37ee6802017-04-11 14:13:57 -0400766}
767
Jonathan Tana97d0072019-02-21 12:24:41 -0800768/*
769 * If flush_received is true, do not attempt to read any more; just use what's
770 * in rpc->buf.
771 */
772static int post_rpc(struct rpc_state *rpc, int flush_received)
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700773{
774 struct active_request_slot *slot;
Johannes Schindelin8cb01e22016-04-27 14:20:37 +0200775 struct curl_slist *headers = http_copy_default_headers();
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700776 int use_gzip = rpc->gzip_request;
777 char *gzip_body = NULL;
Ramsay Jones37711542012-11-21 19:08:51 +0000778 size_t gzip_size = 0;
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800779 int err, large_request = 0;
Brian M. Carlsonc80d96c2013-10-31 02:36:51 -0400780 int needs_100_continue = 0;
Masaya Suzukicf2fb922019-01-10 11:33:48 -0800781 struct rpc_in_data rpc_in_data;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700782
783 /* Try to load the entire request, if we can fit it into the
784 * allocated buffer space we can use HTTP/1.0 and avoid the
785 * chunked encoding mess.
786 */
Jonathan Tana97d0072019-02-21 12:24:41 -0800787 if (!flush_received) {
788 while (1) {
789 size_t n;
790 enum packet_read_status status;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700791
Jonathan Tana97d0072019-02-21 12:24:41 -0800792 if (!rpc_read_from_out(rpc, 0, &n, &status)) {
793 large_request = 1;
794 use_gzip = 0;
795 break;
796 }
797 if (status == PACKET_READ_FLUSH)
798 break;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700799 }
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700800 }
801
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800802 if (large_request) {
Brian M. Carlsonc80d96c2013-10-31 02:36:51 -0400803 struct slot_results results;
804
Jeff Kingb81401c2012-08-27 09:27:15 -0400805 do {
Brian M. Carlsonc80d96c2013-10-31 02:36:51 -0400806 err = probe_rpc(rpc, &results);
Jeff King2501aff2013-09-28 04:31:45 -0400807 if (err == HTTP_REAUTH)
808 credential_fill(&http_auth);
Jeff Kingb81401c2012-08-27 09:27:15 -0400809 } while (err == HTTP_REAUTH);
810 if (err != HTTP_OK)
811 return -1;
Brian M. Carlsonc80d96c2013-10-31 02:36:51 -0400812
813 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
814 needs_100_continue = 1;
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800815 }
816
Jeff Kingabf8df82012-10-12 03:35:33 -0400817 headers = curl_slist_append(headers, rpc->hdr_content_type);
818 headers = curl_slist_append(headers, rpc->hdr_accept);
Brian M. Carlsonc80d96c2013-10-31 02:36:51 -0400819 headers = curl_slist_append(headers, needs_100_continue ?
820 "Expect: 100-continue" : "Expect:");
Jeff Kingabf8df82012-10-12 03:35:33 -0400821
Brandon Williams884e5862018-03-15 10:31:39 -0700822 /* Add the extra Git-Protocol header */
823 if (rpc->protocol_header)
824 headers = curl_slist_append(headers, rpc->protocol_header);
825
Jeff Kingabf8df82012-10-12 03:35:33 -0400826retry:
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700827 slot = get_active_slot();
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700828
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700829 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
Martin Storsjöd21f9792009-11-23 11:03:28 +0800830 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700831 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
Brandon Williams1a53e692018-05-22 11:42:03 -0700832 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700833
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700834 if (large_request) {
835 /* The request body is large and the size cannot be predicted.
836 * We must use chunked encoding to send it.
837 */
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700838 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
Martin Storsjö6c81a992009-12-01 12:33:39 +0200839 rpc->initial_buffer = 1;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700840 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
841 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
Martin Storsjö6c81a992009-12-01 12:33:39 +0200842#ifndef NO_CURL_IOCTL
843 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
844 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
845#endif
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700846 if (options.verbosity > 1) {
847 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
848 fflush(stderr);
849 }
850
Jeff King2e736fd2012-10-31 07:29:16 -0400851 } else if (gzip_body) {
852 /*
853 * If we are looping to retry authentication, then the previous
854 * run will have set up the headers and gzip buffer already,
855 * and we just need to send it.
856 */
857 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
David Turner37ee6802017-04-11 14:13:57 -0400858 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
Jeff King2e736fd2012-10-31 07:29:16 -0400859
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700860 } else if (use_gzip && 1024 < rpc->len) {
861 /* The client backend isn't giving us compressed data so
Johannes Schindelina8132412018-08-08 04:50:00 -0700862 * we can try to deflate it ourselves, this may save on
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700863 * the transfer time.
864 */
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700865 git_zstream stream;
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700866 int ret;
867
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700868 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
Jeff Kingdf126e12012-10-31 07:20:15 -0400869 gzip_size = git_deflate_bound(&stream, rpc->len);
870 gzip_body = xmalloc(gzip_size);
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700871
872 stream.next_in = (unsigned char *)rpc->buf;
873 stream.avail_in = rpc->len;
874 stream.next_out = (unsigned char *)gzip_body;
Jeff Kingdf126e12012-10-31 07:20:15 -0400875 stream.avail_out = gzip_size;
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700876
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700877 ret = git_deflate(&stream, Z_FINISH);
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700878 if (ret != Z_STREAM_END)
Johannes Schindelined8b4132019-03-05 15:20:40 -0800879 die(_("cannot deflate request; zlib deflate error %d"), ret);
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700880
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700881 ret = git_deflate_end_gently(&stream);
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700882 if (ret != Z_OK)
Johannes Schindelined8b4132019-03-05 15:20:40 -0800883 die(_("cannot deflate request; zlib end error %d"), ret);
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700884
Jeff Kingdf126e12012-10-31 07:20:15 -0400885 gzip_size = stream.total_out;
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700886
887 headers = curl_slist_append(headers, "Content-Encoding: gzip");
888 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
David Turner37ee6802017-04-11 14:13:57 -0400889 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700890
891 if (options.verbosity > 1) {
892 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
893 rpc->service_name,
Jeff Kingdf126e12012-10-31 07:20:15 -0400894 (unsigned long)rpc->len, (unsigned long)gzip_size);
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700895 fflush(stderr);
896 }
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700897 } else {
898 /* We know the complete request size in advance, use the
899 * more normal Content-Length approach.
900 */
901 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
David Turner37ee6802017-04-11 14:13:57 -0400902 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700903 if (options.verbosity > 1) {
904 fprintf(stderr, "POST %s (%lu bytes)\n",
905 rpc->service_name, (unsigned long)rpc->len);
906 fflush(stderr);
907 }
908 }
909
910 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
911 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
Masaya Suzukicf2fb922019-01-10 11:33:48 -0800912 rpc_in_data.rpc = rpc;
Masaya Suzukib79bdd82019-01-10 11:33:49 -0800913 rpc_in_data.slot = slot;
Masaya Suzukicf2fb922019-01-10 11:33:48 -0800914 curl_easy_setopt(slot->curl, CURLOPT_FILE, &rpc_in_data);
Masaya Suzukib79bdd82019-01-10 11:33:49 -0800915 curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700916
David Turner296b8472016-11-18 15:30:49 -0500917
918 rpc->any_written = 0;
Jeff King3a347ed2013-10-31 02:36:26 -0400919 err = run_slot(slot, NULL);
Jeff King2501aff2013-09-28 04:31:45 -0400920 if (err == HTTP_REAUTH && !large_request) {
921 credential_fill(&http_auth);
Jeff Kingabf8df82012-10-12 03:35:33 -0400922 goto retry;
Jeff King2501aff2013-09-28 04:31:45 -0400923 }
Jeff Kingb81401c2012-08-27 09:27:15 -0400924 if (err != HTTP_OK)
925 err = -1;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700926
David Turner296b8472016-11-18 15:30:49 -0500927 if (!rpc->any_written)
928 err = -1;
929
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700930 curl_slist_free_all(headers);
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700931 free(gzip_body);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700932 return err;
933}
934
Jonathan Tan7d50d342019-02-14 11:06:35 -0800935static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
Jonathan Tanb3590302019-02-14 11:06:37 -0800936 const char **client_argv, const struct strbuf *preamble,
937 struct strbuf *rpc_result)
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700938{
939 const char *svc = rpc->service_name;
940 struct strbuf buf = STRBUF_INIT;
René Scharfed3180272014-08-19 21:09:35 +0200941 struct child_process client = CHILD_PROCESS_INIT;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700942 int err = 0;
943
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700944 client.in = -1;
945 client.out = -1;
946 client.git_cmd = 1;
Jonathan Tan7d50d342019-02-14 11:06:35 -0800947 client.argv = client_argv;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700948 if (start_command(&client))
949 exit(1);
Jonathan Tan5d916692019-02-14 11:06:36 -0800950 write_or_die(client.in, preamble->buf, preamble->len);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700951 if (heads)
952 write_or_die(client.in, heads->buf, heads->len);
953
954 rpc->alloc = http_post_buffer;
955 rpc->buf = xmalloc(rpc->alloc);
956 rpc->in = client.in;
957 rpc->out = client.out;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700958
Jeff Kingb227bbc2013-09-28 04:35:25 -0400959 strbuf_addf(&buf, "%s%s", url.buf, svc);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700960 rpc->service_url = strbuf_detach(&buf, NULL);
961
962 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
963 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
964
Shawn O. Pearce8efa5f62010-01-12 09:54:04 -0800965 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700966 rpc->hdr_accept = strbuf_detach(&buf, NULL);
967
Brandon Williams884e5862018-03-15 10:31:39 -0700968 if (get_protocol_http_header(heads->version, &buf))
969 rpc->protocol_header = strbuf_detach(&buf, NULL);
970 else
971 rpc->protocol_header = NULL;
972
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700973 while (!err) {
Jeff King4981fe72013-02-23 17:31:34 -0500974 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700975 if (!n)
976 break;
977 rpc->pos = 0;
978 rpc->len = n;
Jonathan Tana97d0072019-02-21 12:24:41 -0800979 err |= post_rpc(rpc, 0);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700980 }
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700981
982 close(client.in);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700983 client.in = -1;
Shawn O. Pearce6cdf0222011-10-04 16:20:19 -0700984 if (!err) {
Jonathan Tanb3590302019-02-14 11:06:37 -0800985 strbuf_read(rpc_result, client.out, 0);
Shawn O. Pearce6cdf0222011-10-04 16:20:19 -0700986 } else {
987 char buf[4096];
988 for (;;)
989 if (xread(client.out, buf, sizeof(buf)) <= 0)
990 break;
991 }
Shawn O. Pearceb4ee10f2010-08-06 14:19:44 -0700992
993 close(client.out);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700994 client.out = -1;
995
996 err |= finish_command(&client);
997 free(rpc->service_url);
998 free(rpc->hdr_content_type);
999 free(rpc->hdr_accept);
Brandon Williams884e5862018-03-15 10:31:39 -07001000 free(rpc->protocol_header);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001001 free(rpc->buf);
1002 strbuf_release(&buf);
1003 return err;
1004}
1005
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001006static int fetch_dumb(int nr_heads, struct ref **to_fetch)
1007{
Tay Ray Chuan26e1e0b2010-03-02 18:49:31 +08001008 struct walker *walker;
Jeff Kingb32fa952016-02-22 17:44:25 -05001009 char **targets;
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001010 int ret, i;
1011
Jeff Kingb32fa952016-02-22 17:44:25 -05001012 ALLOC_ARRAY(targets, nr_heads);
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001013 if (options.depth || options.deepen_since)
Johannes Schindelined8b4132019-03-05 15:20:40 -08001014 die(_("dumb http transport does not support shallow capabilities"));
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001015 for (i = 0; i < nr_heads; i++)
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001016 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001017
Jeff Kingb227bbc2013-09-28 04:35:25 -04001018 walker = get_http_walker(url.buf);
Shawn O. Pearceef08ef92009-10-30 17:47:29 -07001019 walker->get_verbosely = options.verbosity >= 3;
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001020 walker->get_recover = 0;
1021 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
Tay Ray Chuan26e1e0b2010-03-02 18:49:31 +08001022 walker_free(walker);
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001023
1024 for (i = 0; i < nr_heads; i++)
1025 free(targets[i]);
1026 free(targets);
1027
Johannes Schindelined8b4132019-03-05 15:20:40 -08001028 return ret ? error(_("fetch failed.")) : 0;
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001029}
1030
Shawn O. Pearce249b2002009-10-30 17:47:42 -07001031static int fetch_git(struct discovery *heads,
1032 int nr_heads, struct ref **to_fetch)
1033{
1034 struct rpc_state rpc;
Ivan Todoroski81507492012-04-02 17:14:44 +02001035 struct strbuf preamble = STRBUF_INIT;
Nguyễn Thái Ngọc Duyb5f62eb2016-06-12 17:53:43 +07001036 int i, err;
1037 struct argv_array args = ARGV_ARRAY_INIT;
Jonathan Tanb3590302019-02-14 11:06:37 -08001038 struct strbuf rpc_result = STRBUF_INIT;
Shawn O. Pearce249b2002009-10-30 17:47:42 -07001039
Nguyễn Thái Ngọc Duyb5f62eb2016-06-12 17:53:43 +07001040 argv_array_pushl(&args, "fetch-pack", "--stateless-rpc",
1041 "--stdin", "--lock-pack", NULL);
Shawn O. Pearce249b2002009-10-30 17:47:42 -07001042 if (options.followtags)
Nguyễn Thái Ngọc Duyb5f62eb2016-06-12 17:53:43 +07001043 argv_array_push(&args, "--include-tag");
Shawn O. Pearce249b2002009-10-30 17:47:42 -07001044 if (options.thin)
Nguyễn Thái Ngọc Duyb5f62eb2016-06-12 17:53:43 +07001045 argv_array_push(&args, "--thin");
1046 if (options.verbosity >= 3)
1047 argv_array_pushl(&args, "-v", "-v", NULL);
Nguyễn Thái Ngọc Duy9ba38042013-07-21 15:18:05 +07001048 if (options.check_self_contained_and_connected)
Nguyễn Thái Ngọc Duyb5f62eb2016-06-12 17:53:43 +07001049 argv_array_push(&args, "--check-self-contained-and-connected");
Nguyễn Thái Ngọc Duy16094882013-12-05 20:02:50 +07001050 if (options.cloning)
Nguyễn Thái Ngọc Duyb5f62eb2016-06-12 17:53:43 +07001051 argv_array_push(&args, "--cloning");
Nguyễn Thái Ngọc Duy16094882013-12-05 20:02:50 +07001052 if (options.update_shallow)
Nguyễn Thái Ngọc Duyb5f62eb2016-06-12 17:53:43 +07001053 argv_array_push(&args, "--update-shallow");
Shawn O. Pearce249b2002009-10-30 17:47:42 -07001054 if (!options.progress)
Nguyễn Thái Ngọc Duyb5f62eb2016-06-12 17:53:43 +07001055 argv_array_push(&args, "--no-progress");
1056 if (options.depth)
1057 argv_array_pushf(&args, "--depth=%lu", options.depth);
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001058 if (options.deepen_since)
1059 argv_array_pushf(&args, "--shallow-since=%s", options.deepen_since);
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +07001060 for (i = 0; i < options.deepen_not.nr; i++)
1061 argv_array_pushf(&args, "--shallow-exclude=%s",
1062 options.deepen_not.items[i].string);
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +07001063 if (options.deepen_relative && options.depth)
1064 argv_array_push(&args, "--deepen-relative");
Jonathan Tan88e2f9e2017-12-05 16:58:49 +00001065 if (options.from_promisor)
1066 argv_array_push(&args, "--from-promisor");
1067 if (options.no_dependents)
1068 argv_array_push(&args, "--no-dependents");
Jeff Hostetleracb0c572017-12-08 15:58:44 +00001069 if (options.filter)
1070 argv_array_pushf(&args, "--filter=%s", options.filter);
Nguyễn Thái Ngọc Duyb5f62eb2016-06-12 17:53:43 +07001071 argv_array_push(&args, url.buf);
Ivan Todoroski81507492012-04-02 17:14:44 +02001072
Shawn O. Pearce249b2002009-10-30 17:47:42 -07001073 for (i = 0; i < nr_heads; i++) {
1074 struct ref *ref = to_fetch[i];
Jeff King94ee8e22015-01-28 12:58:50 -05001075 if (!*ref->name)
Johannes Schindelined8b4132019-03-05 15:20:40 -08001076 die(_("cannot fetch by sha1 over smart http"));
Nguyễn Thái Ngọc Duy58f2ed02013-12-05 20:02:49 +07001077 packet_buf_write(&preamble, "%s %s\n",
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001078 oid_to_hex(&ref->old_oid), ref->name);
Shawn O. Pearce249b2002009-10-30 17:47:42 -07001079 }
Ivan Todoroski81507492012-04-02 17:14:44 +02001080 packet_buf_flush(&preamble);
Shawn O. Pearce249b2002009-10-30 17:47:42 -07001081
1082 memset(&rpc, 0, sizeof(rpc));
1083 rpc.service_name = "git-upload-pack",
Shawn O. Pearceb8538602009-10-30 17:47:43 -07001084 rpc.gzip_request = 1;
Shawn O. Pearce249b2002009-10-30 17:47:42 -07001085
Jonathan Tanb3590302019-02-14 11:06:37 -08001086 err = rpc_service(&rpc, heads, args.argv, &preamble, &rpc_result);
1087 if (rpc_result.len)
1088 write_or_die(1, rpc_result.buf, rpc_result.len);
1089 strbuf_release(&rpc_result);
Ivan Todoroski81507492012-04-02 17:14:44 +02001090 strbuf_release(&preamble);
Nguyễn Thái Ngọc Duyb5f62eb2016-06-12 17:53:43 +07001091 argv_array_clear(&args);
Shawn O. Pearce249b2002009-10-30 17:47:42 -07001092 return err;
1093}
1094
1095static int fetch(int nr_heads, struct ref **to_fetch)
1096{
Jeff King2a455202013-02-20 15:07:19 -05001097 struct discovery *d = discover_refs("git-upload-pack", 0);
Shawn O. Pearce249b2002009-10-30 17:47:42 -07001098 if (d->proto_git)
1099 return fetch_git(d, nr_heads, to_fetch);
1100 else
1101 return fetch_dumb(nr_heads, to_fetch);
1102}
1103
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001104static void parse_fetch(struct strbuf *buf)
1105{
1106 struct ref **to_fetch = NULL;
1107 struct ref *list_head = NULL;
1108 struct ref **list = &list_head;
1109 int alloc_heads = 0, nr_heads = 0;
1110
1111 do {
Jeff King95b567c2014-06-18 15:48:29 -04001112 const char *p;
1113 if (skip_prefix(buf->buf, "fetch ", &p)) {
1114 const char *name;
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001115 struct ref *ref;
brian m. carlson8338c912015-11-10 02:22:22 +00001116 struct object_id old_oid;
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001117
brian m. carlson8338c912015-11-10 02:22:22 +00001118 if (get_oid_hex(p, &old_oid))
Johannes Schindelined8b4132019-03-05 15:20:40 -08001119 die(_("protocol error: expected sha/ref, got %s'"), p);
brian m. carlson8338c912015-11-10 02:22:22 +00001120 if (p[GIT_SHA1_HEXSZ] == ' ')
1121 name = p + GIT_SHA1_HEXSZ + 1;
1122 else if (!p[GIT_SHA1_HEXSZ])
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001123 name = "";
1124 else
Johannes Schindelined8b4132019-03-05 15:20:40 -08001125 die(_("protocol error: expected sha/ref, got %s'"), p);
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001126
1127 ref = alloc_ref(name);
brian m. carlson8338c912015-11-10 02:22:22 +00001128 oidcpy(&ref->old_oid, &old_oid);
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001129
1130 *list = ref;
1131 list = &ref->next;
1132
1133 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
1134 to_fetch[nr_heads++] = ref;
1135 }
1136 else
Johannes Schindelined8b4132019-03-05 15:20:40 -08001137 die(_("http transport does not support %s"), buf->buf);
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001138
1139 strbuf_reset(buf);
Junio C Hamano8f309ae2016-01-13 15:31:17 -08001140 if (strbuf_getline_lf(buf, stdin) == EOF)
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001141 return;
1142 if (!*buf->buf)
1143 break;
1144 } while (1);
1145
Shawn O. Pearce249b2002009-10-30 17:47:42 -07001146 if (fetch(nr_heads, to_fetch))
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001147 exit(128); /* error already reported */
1148 free_refs(list_head);
1149 free(to_fetch);
1150
1151 printf("\n");
1152 fflush(stdout);
1153 strbuf_reset(buf);
1154}
1155
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001156static int push_dav(int nr_spec, char **specs)
1157{
Jeff King850d2fe2016-02-22 17:44:21 -05001158 struct child_process child = CHILD_PROCESS_INIT;
1159 size_t i;
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001160
Jeff King850d2fe2016-02-22 17:44:21 -05001161 child.git_cmd = 1;
1162 argv_array_push(&child.args, "http-push");
1163 argv_array_push(&child.args, "--helper-status");
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001164 if (options.dry_run)
Jeff King850d2fe2016-02-22 17:44:21 -05001165 argv_array_push(&child.args, "--dry-run");
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001166 if (options.verbosity > 1)
Jeff King850d2fe2016-02-22 17:44:21 -05001167 argv_array_push(&child.args, "--verbose");
1168 argv_array_push(&child.args, url.buf);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001169 for (i = 0; i < nr_spec; i++)
Jeff King850d2fe2016-02-22 17:44:21 -05001170 argv_array_push(&child.args, specs[i]);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001171
Jeff King850d2fe2016-02-22 17:44:21 -05001172 if (run_command(&child))
Johannes Schindelined8b4132019-03-05 15:20:40 -08001173 die(_("git-http-push failed"));
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001174 return 0;
1175}
1176
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001177static int push_git(struct discovery *heads, int nr_spec, char **specs)
1178{
1179 struct rpc_state rpc;
Junio C Hamano222b1212013-07-08 22:16:31 -07001180 int i, err;
1181 struct argv_array args;
Junio C Hamano05c1eb12013-08-02 15:14:50 -07001182 struct string_list_item *cas_option;
Jeff King26be19b2014-08-21 08:21:20 -04001183 struct strbuf preamble = STRBUF_INIT;
Jonathan Tanb3590302019-02-14 11:06:37 -08001184 struct strbuf rpc_result = STRBUF_INIT;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001185
Junio C Hamano222b1212013-07-08 22:16:31 -07001186 argv_array_init(&args);
1187 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
1188 NULL);
1189
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001190 if (options.thin)
Junio C Hamano222b1212013-07-08 22:16:31 -07001191 argv_array_push(&args, "--thin");
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001192 if (options.dry_run)
Junio C Hamano222b1212013-07-08 22:16:31 -07001193 argv_array_push(&args, "--dry-run");
Dave Borowitz30261092015-08-19 11:26:46 -04001194 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
1195 argv_array_push(&args, "--signed=yes");
1196 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
1197 argv_array_push(&args, "--signed=if-asked");
Clemens Buchacherc207e342012-01-08 22:06:20 +01001198 if (options.verbosity == 0)
Junio C Hamano222b1212013-07-08 22:16:31 -07001199 argv_array_push(&args, "--quiet");
Clemens Buchacherc207e342012-01-08 22:06:20 +01001200 else if (options.verbosity > 1)
Junio C Hamano222b1212013-07-08 22:16:31 -07001201 argv_array_push(&args, "--verbose");
Brandon Williams511155d2017-03-22 15:22:00 -07001202 for (i = 0; i < options.push_options.nr; i++)
1203 argv_array_pushf(&args, "--push-option=%s",
1204 options.push_options.items[i].string);
Junio C Hamano222b1212013-07-08 22:16:31 -07001205 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
Junio C Hamano05c1eb12013-08-02 15:14:50 -07001206 for_each_string_list_item(cas_option, &cas_options)
Junio C Hamano2233ad42013-09-09 14:30:29 -07001207 argv_array_push(&args, cas_option->string);
Jeff Kingb227bbc2013-09-28 04:35:25 -04001208 argv_array_push(&args, url.buf);
Jeff King26be19b2014-08-21 08:21:20 -04001209
1210 argv_array_push(&args, "--stdin");
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001211 for (i = 0; i < nr_spec; i++)
Jeff King26be19b2014-08-21 08:21:20 -04001212 packet_buf_write(&preamble, "%s\n", specs[i]);
1213 packet_buf_flush(&preamble);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001214
1215 memset(&rpc, 0, sizeof(rpc));
1216 rpc.service_name = "git-receive-pack",
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001217
Jonathan Tanb3590302019-02-14 11:06:37 -08001218 err = rpc_service(&rpc, heads, args.argv, &preamble, &rpc_result);
1219 if (rpc_result.len)
1220 write_or_die(1, rpc_result.buf, rpc_result.len);
1221 strbuf_release(&rpc_result);
Jeff King26be19b2014-08-21 08:21:20 -04001222 strbuf_release(&preamble);
Junio C Hamano222b1212013-07-08 22:16:31 -07001223 argv_array_clear(&args);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001224 return err;
1225}
1226
1227static int push(int nr_spec, char **specs)
1228{
Jeff King2a455202013-02-20 15:07:19 -05001229 struct discovery *heads = discover_refs("git-receive-pack", 1);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001230 int ret;
1231
1232 if (heads->proto_git)
1233 ret = push_git(heads, nr_spec, specs);
1234 else
1235 ret = push_dav(nr_spec, specs);
1236 free_discovery(heads);
1237 return ret;
1238}
1239
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001240static void parse_push(struct strbuf *buf)
1241{
1242 char **specs = NULL;
Shawn O. Pearce5238cbf2012-01-19 19:12:09 -08001243 int alloc_spec = 0, nr_spec = 0, i, ret;
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001244
1245 do {
Christian Couder59556542013-11-30 21:55:40 +01001246 if (starts_with(buf->buf, "push ")) {
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001247 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
1248 specs[nr_spec++] = xstrdup(buf->buf + 5);
1249 }
1250 else
Johannes Schindelined8b4132019-03-05 15:20:40 -08001251 die(_("http transport does not support %s"), buf->buf);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001252
1253 strbuf_reset(buf);
Junio C Hamano8f309ae2016-01-13 15:31:17 -08001254 if (strbuf_getline_lf(buf, stdin) == EOF)
Jim Meyeringdc4cd762011-06-20 09:40:06 +02001255 goto free_specs;
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001256 if (!*buf->buf)
1257 break;
1258 } while (1);
1259
Shawn O. Pearce5238cbf2012-01-19 19:12:09 -08001260 ret = push(nr_spec, specs);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001261 printf("\n");
1262 fflush(stdout);
Jim Meyeringdc4cd762011-06-20 09:40:06 +02001263
Shawn O. Pearce5238cbf2012-01-19 19:12:09 -08001264 if (ret)
1265 exit(128); /* error already reported */
1266
Jim Meyeringdc4cd762011-06-20 09:40:06 +02001267 free_specs:
1268 for (i = 0; i < nr_spec; i++)
1269 free(specs[i]);
1270 free(specs);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001271}
1272
Brandon Williams0f1dc532018-03-15 10:31:41 -07001273static int stateless_connect(const char *service_name)
1274{
1275 struct discovery *discover;
Jonathan Tana97d0072019-02-21 12:24:41 -08001276 struct rpc_state rpc;
1277 struct strbuf buf = STRBUF_INIT;
Brandon Williams0f1dc532018-03-15 10:31:41 -07001278
1279 /*
1280 * Run the info/refs request and see if the server supports protocol
1281 * v2. If and only if the server supports v2 can we successfully
1282 * establish a stateless connection, otherwise we need to tell the
1283 * client to fallback to using other transport helper functions to
1284 * complete their request.
1285 */
1286 discover = discover_refs(service_name, 0);
1287 if (discover->version != protocol_v2) {
1288 printf("fallback\n");
1289 fflush(stdout);
1290 return -1;
1291 } else {
1292 /* Stateless Connection established */
1293 printf("\n");
1294 fflush(stdout);
1295 }
1296
Jonathan Tana97d0072019-02-21 12:24:41 -08001297 rpc.service_name = service_name;
1298 rpc.service_url = xstrfmt("%s%s", url.buf, rpc.service_name);
1299 rpc.hdr_content_type = xstrfmt("Content-Type: application/x-%s-request", rpc.service_name);
1300 rpc.hdr_accept = xstrfmt("Accept: application/x-%s-result", rpc.service_name);
1301 if (get_protocol_http_header(discover->version, &buf)) {
1302 rpc.protocol_header = strbuf_detach(&buf, NULL);
1303 } else {
1304 rpc.protocol_header = NULL;
1305 strbuf_release(&buf);
1306 }
1307 rpc.buf = xmalloc(http_post_buffer);
1308 rpc.alloc = http_post_buffer;
1309 rpc.len = 0;
1310 rpc.pos = 0;
1311 rpc.in = 1;
1312 rpc.out = 0;
1313 rpc.any_written = 0;
1314 rpc.gzip_request = 1;
1315 rpc.initial_buffer = 0;
1316 rpc.write_line_lengths = 1;
1317 rpc.flush_read_but_not_sent = 0;
Brandon Williams0f1dc532018-03-15 10:31:41 -07001318
1319 /*
1320 * Dump the capability listing that we got from the server earlier
1321 * during the info/refs request.
1322 */
Jonathan Tana97d0072019-02-21 12:24:41 -08001323 write_or_die(rpc.in, discover->buf, discover->len);
Brandon Williams0f1dc532018-03-15 10:31:41 -07001324
Jonathan Tana97d0072019-02-21 12:24:41 -08001325 /* Until we see EOF keep sending POSTs */
1326 while (1) {
1327 size_t avail;
1328 enum packet_read_status status;
1329
1330 if (!rpc_read_from_out(&rpc, PACKET_READ_GENTLE_ON_EOF, &avail,
1331 &status))
1332 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
1333 if (status == PACKET_READ_EOF)
1334 break;
1335 if (post_rpc(&rpc, status == PACKET_READ_FLUSH))
Brandon Williams0f1dc532018-03-15 10:31:41 -07001336 /* We would have an err here */
1337 break;
Jonathan Tana97d0072019-02-21 12:24:41 -08001338 /* Reset the buffer for next request */
1339 rpc.len = 0;
Brandon Williams0f1dc532018-03-15 10:31:41 -07001340 }
1341
Jonathan Tana97d0072019-02-21 12:24:41 -08001342 free(rpc.service_url);
1343 free(rpc.hdr_content_type);
1344 free(rpc.hdr_accept);
1345 free(rpc.protocol_header);
1346 free(rpc.buf);
1347 strbuf_release(&buf);
1348
Brandon Williams0f1dc532018-03-15 10:31:41 -07001349 return 0;
1350}
1351
Jeff King3f2e2292016-07-01 01:58:58 -04001352int cmd_main(int argc, const char **argv)
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001353{
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001354 struct strbuf buf = STRBUF_INIT;
Daniel Barkalowa45d3d72009-11-03 21:52:35 -05001355 int nongit;
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001356
Daniel Barkalowa45d3d72009-11-03 21:52:35 -05001357 setup_git_directory_gently(&nongit);
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001358 if (argc < 2) {
Johannes Schindelined8b4132019-03-05 15:20:40 -08001359 error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001360 return 1;
1361 }
1362
Shawn O. Pearceef08ef92009-10-30 17:47:29 -07001363 options.verbosity = 1;
1364 options.progress = !!isatty(2);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001365 options.thin = 1;
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +07001366 string_list_init(&options.deepen_not, 1);
Brandon Williams511155d2017-03-22 15:22:00 -07001367 string_list_init(&options.push_options, 1);
Shawn O. Pearceef08ef92009-10-30 17:47:29 -07001368
Jeff Hostetleree4512e2019-02-22 14:25:01 -08001369 /*
1370 * Just report "remote-curl" here (folding all the various aliases
1371 * ("git-remote-http", "git-remote-https", and etc.) here since they
1372 * are all just copies of the same actual executable.
1373 */
1374 trace2_cmd_name("remote-curl");
1375
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001376 remote = remote_get(argv[1]);
1377
1378 if (argc > 2) {
Jeff Kingb227bbc2013-09-28 04:35:25 -04001379 end_url_with_slash(&url, argv[2]);
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001380 } else {
Jeff Kingb227bbc2013-09-28 04:35:25 -04001381 end_url_with_slash(&url, remote->url[0]);
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001382 }
1383
Jeff Kingb227bbc2013-09-28 04:35:25 -04001384 http_init(remote, url.buf, 0);
Tay Ray Chuan888692b2010-03-02 18:49:29 +08001385
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001386 do {
Jeff King95b567c2014-06-18 15:48:29 -04001387 const char *arg;
1388
Junio C Hamano8f309ae2016-01-13 15:31:17 -08001389 if (strbuf_getline_lf(&buf, stdin) == EOF) {
Sverre Rabbelier1843f0c2011-07-16 15:03:29 +02001390 if (ferror(stdin))
Johannes Schindelined8b4132019-03-05 15:20:40 -08001391 error(_("remote-curl: error reading command stream from git"));
Sverre Rabbelier1843f0c2011-07-16 15:03:29 +02001392 return 1;
1393 }
1394 if (buf.len == 0)
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001395 break;
Christian Couder59556542013-11-30 21:55:40 +01001396 if (starts_with(buf.buf, "fetch ")) {
Daniel Barkalowa45d3d72009-11-03 21:52:35 -05001397 if (nongit)
Johannes Schindelined8b4132019-03-05 15:20:40 -08001398 die(_("remote-curl: fetch attempted without a local repo"));
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001399 parse_fetch(&buf);
1400
Christian Couder59556542013-11-30 21:55:40 +01001401 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -07001402 int for_push = !!strstr(buf.buf + 4, "for-push");
1403 output_refs(get_refs(for_push));
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001404
Christian Couder59556542013-11-30 21:55:40 +01001405 } else if (starts_with(buf.buf, "push ")) {
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001406 parse_push(&buf);
1407
Jeff King95b567c2014-06-18 15:48:29 -04001408 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1409 char *value = strchr(arg, ' ');
Shawn O. Pearceef08ef92009-10-30 17:47:29 -07001410 int result;
1411
1412 if (value)
1413 *value++ = '\0';
1414 else
1415 value = "true";
1416
Jeff King95b567c2014-06-18 15:48:29 -04001417 result = set_option(arg, value);
Shawn O. Pearceef08ef92009-10-30 17:47:29 -07001418 if (!result)
1419 printf("ok\n");
1420 else if (result < 0)
1421 printf("error invalid value\n");
1422 else
1423 printf("unsupported\n");
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001424 fflush(stdout);
Shawn O. Pearceef08ef92009-10-30 17:47:29 -07001425
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001426 } else if (!strcmp(buf.buf, "capabilities")) {
Brandon Williams0f1dc532018-03-15 10:31:41 -07001427 printf("stateless-connect\n");
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001428 printf("fetch\n");
Shawn O. Pearceef08ef92009-10-30 17:47:29 -07001429 printf("option\n");
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001430 printf("push\n");
Nguyễn Thái Ngọc Duy9ba38042013-07-21 15:18:05 +07001431 printf("check-connectivity\n");
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001432 printf("\n");
1433 fflush(stdout);
Brandon Williams0f1dc532018-03-15 10:31:41 -07001434 } else if (skip_prefix(buf.buf, "stateless-connect ", &arg)) {
1435 if (!stateless_connect(arg))
1436 break;
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001437 } else {
Johannes Schindelined8b4132019-03-05 15:20:40 -08001438 error(_("remote-curl: unknown command '%s' from git"), buf.buf);
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001439 return 1;
1440 }
1441 strbuf_reset(&buf);
1442 } while (1);
Tay Ray Chuan888692b2010-03-02 18:49:29 +08001443
1444 http_cleanup();
1445
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001446 return 0;
1447}