THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1445 lines
37KB

  1. #include "cache.h"
  2. #include "config.h"
  3. #include "remote.h"
  4. #include "connect.h"
  5. #include "strbuf.h"
  6. #include "walker.h"
  7. #include "http.h"
  8. #include "exec-cmd.h"
  9. #include "run-command.h"
  10. #include "pkt-line.h"
  11. #include "string-list.h"
  12. #include "sideband.h"
  13. #include "argv-array.h"
  14. #include "credential.h"
  15. #include "sha1-array.h"
  16. #include "send-pack.h"
  17. #include "protocol.h"
  18. #include "quote.h"
  19. #include "transport.h"
  20. static struct remote *remote;
  21. /* always ends with a trailing slash */
  22. static struct strbuf url = STRBUF_INIT;
  23. struct options {
  24. int verbosity;
  25. unsigned long depth;
  26. char *deepen_since;
  27. struct string_list deepen_not;
  28. struct string_list push_options;
  29. char *filter;
  30. unsigned progress : 1,
  31. check_self_contained_and_connected : 1,
  32. cloning : 1,
  33. update_shallow : 1,
  34. followtags : 1,
  35. dry_run : 1,
  36. thin : 1,
  37. /* One of the SEND_PACK_PUSH_CERT_* constants. */
  38. push_cert : 2,
  39. deepen_relative : 1,
  40. from_promisor : 1,
  41. no_dependents : 1;
  42. };
  43. static struct options options;
  44. static struct string_list cas_options = STRING_LIST_INIT_DUP;
  45. static int set_option(const char *name, const char *value)
  46. {
  47. if (!strcmp(name, "verbosity")) {
  48. char *end;
  49. int v = strtol(value, &end, 10);
  50. if (value == end || *end)
  51. return -1;
  52. options.verbosity = v;
  53. return 0;
  54. }
  55. else if (!strcmp(name, "progress")) {
  56. if (!strcmp(value, "true"))
  57. options.progress = 1;
  58. else if (!strcmp(value, "false"))
  59. options.progress = 0;
  60. else
  61. return -1;
  62. return 0;
  63. }
  64. else if (!strcmp(name, "depth")) {
  65. char *end;
  66. unsigned long v = strtoul(value, &end, 10);
  67. if (value == end || *end)
  68. return -1;
  69. options.depth = v;
  70. return 0;
  71. }
  72. else if (!strcmp(name, "deepen-since")) {
  73. options.deepen_since = xstrdup(value);
  74. return 0;
  75. }
  76. else if (!strcmp(name, "deepen-not")) {
  77. string_list_append(&options.deepen_not, value);
  78. return 0;
  79. }
  80. else if (!strcmp(name, "deepen-relative")) {
  81. if (!strcmp(value, "true"))
  82. options.deepen_relative = 1;
  83. else if (!strcmp(value, "false"))
  84. options.deepen_relative = 0;
  85. else
  86. return -1;
  87. return 0;
  88. }
  89. else if (!strcmp(name, "followtags")) {
  90. if (!strcmp(value, "true"))
  91. options.followtags = 1;
  92. else if (!strcmp(value, "false"))
  93. options.followtags = 0;
  94. else
  95. return -1;
  96. return 0;
  97. }
  98. else if (!strcmp(name, "dry-run")) {
  99. if (!strcmp(value, "true"))
  100. options.dry_run = 1;
  101. else if (!strcmp(value, "false"))
  102. options.dry_run = 0;
  103. else
  104. return -1;
  105. return 0;
  106. }
  107. else if (!strcmp(name, "check-connectivity")) {
  108. if (!strcmp(value, "true"))
  109. options.check_self_contained_and_connected = 1;
  110. else if (!strcmp(value, "false"))
  111. options.check_self_contained_and_connected = 0;
  112. else
  113. return -1;
  114. return 0;
  115. }
  116. else if (!strcmp(name, "cas")) {
  117. struct strbuf val = STRBUF_INIT;
  118. strbuf_addf(&val, "--" CAS_OPT_NAME "=%s", value);
  119. string_list_append(&cas_options, val.buf);
  120. strbuf_release(&val);
  121. return 0;
  122. } else if (!strcmp(name, "cloning")) {
  123. if (!strcmp(value, "true"))
  124. options.cloning = 1;
  125. else if (!strcmp(value, "false"))
  126. options.cloning = 0;
  127. else
  128. return -1;
  129. return 0;
  130. } else if (!strcmp(name, "update-shallow")) {
  131. if (!strcmp(value, "true"))
  132. options.update_shallow = 1;
  133. else if (!strcmp(value, "false"))
  134. options.update_shallow = 0;
  135. else
  136. return -1;
  137. return 0;
  138. } else if (!strcmp(name, "pushcert")) {
  139. if (!strcmp(value, "true"))
  140. options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
  141. else if (!strcmp(value, "false"))
  142. options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
  143. else if (!strcmp(value, "if-asked"))
  144. options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
  145. else
  146. return -1;
  147. return 0;
  148. } else if (!strcmp(name, "push-option")) {
  149. if (*value != '"')
  150. string_list_append(&options.push_options, value);
  151. else {
  152. struct strbuf unquoted = STRBUF_INIT;
  153. if (unquote_c_style(&unquoted, value, NULL) < 0)
  154. die(_("invalid quoting in push-option value: '%s'"), value);
  155. string_list_append_nodup(&options.push_options,
  156. strbuf_detach(&unquoted, NULL));
  157. }
  158. return 0;
  159. #if LIBCURL_VERSION_NUM >= 0x070a08
  160. } else if (!strcmp(name, "family")) {
  161. if (!strcmp(value, "ipv4"))
  162. git_curl_ipresolve = CURL_IPRESOLVE_V4;
  163. else if (!strcmp(value, "ipv6"))
  164. git_curl_ipresolve = CURL_IPRESOLVE_V6;
  165. else if (!strcmp(value, "all"))
  166. git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
  167. else
  168. return -1;
  169. return 0;
  170. #endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
  171. } else if (!strcmp(name, "from-promisor")) {
  172. options.from_promisor = 1;
  173. return 0;
  174. } else if (!strcmp(name, "no-dependents")) {
  175. options.no_dependents = 1;
  176. return 0;
  177. } else if (!strcmp(name, "filter")) {
  178. options.filter = xstrdup(value);
  179. return 0;
  180. } else {
  181. return 1 /* unsupported */;
  182. }
  183. }
  184. struct discovery {
  185. char *service;
  186. char *buf_alloc;
  187. char *buf;
  188. size_t len;
  189. struct ref *refs;
  190. struct oid_array shallow;
  191. enum protocol_version version;
  192. unsigned proto_git : 1;
  193. };
  194. static struct discovery *last_discovery;
  195. static struct ref *parse_git_refs(struct discovery *heads, int for_push)
  196. {
  197. struct ref *list = NULL;
  198. struct packet_reader reader;
  199. packet_reader_init(&reader, -1, heads->buf, heads->len,
  200. PACKET_READ_CHOMP_NEWLINE |
  201. PACKET_READ_GENTLE_ON_EOF |
  202. PACKET_READ_DIE_ON_ERR_PACKET);
  203. heads->version = discover_version(&reader);
  204. switch (heads->version) {
  205. case protocol_v2:
  206. /*
  207. * Do nothing. This isn't a list of refs but rather a
  208. * capability advertisement. Client would have run
  209. * 'stateless-connect' so we'll dump this capability listing
  210. * and let them request the refs themselves.
  211. */
  212. break;
  213. case protocol_v1:
  214. case protocol_v0:
  215. get_remote_heads(&reader, &list, for_push ? REF_NORMAL : 0,
  216. NULL, &heads->shallow);
  217. break;
  218. case protocol_unknown_version:
  219. BUG("unknown protocol version");
  220. }
  221. return list;
  222. }
  223. static struct ref *parse_info_refs(struct discovery *heads)
  224. {
  225. char *data, *start, *mid;
  226. char *ref_name;
  227. int i = 0;
  228. struct ref *refs = NULL;
  229. struct ref *ref = NULL;
  230. struct ref *last_ref = NULL;
  231. data = heads->buf;
  232. start = NULL;
  233. mid = data;
  234. while (i < heads->len) {
  235. if (!start) {
  236. start = &data[i];
  237. }
  238. if (data[i] == '\t')
  239. mid = &data[i];
  240. if (data[i] == '\n') {
  241. if (mid - start != the_hash_algo->hexsz)
  242. die(_("%sinfo/refs not valid: is this a git repository?"),
  243. transport_anonymize_url(url.buf));
  244. data[i] = 0;
  245. ref_name = mid + 1;
  246. ref = alloc_ref(ref_name);
  247. get_oid_hex(start, &ref->old_oid);
  248. if (!refs)
  249. refs = ref;
  250. if (last_ref)
  251. last_ref->next = ref;
  252. last_ref = ref;
  253. start = NULL;
  254. }
  255. i++;
  256. }
  257. ref = alloc_ref("HEAD");
  258. if (!http_fetch_ref(url.buf, ref) &&
  259. !resolve_remote_symref(ref, refs)) {
  260. ref->next = refs;
  261. refs = ref;
  262. } else {
  263. free(ref);
  264. }
  265. return refs;
  266. }
  267. static void free_discovery(struct discovery *d)
  268. {
  269. if (d) {
  270. if (d == last_discovery)
  271. last_discovery = NULL;
  272. free(d->shallow.oid);
  273. free(d->buf_alloc);
  274. free_refs(d->refs);
  275. free(d->service);
  276. free(d);
  277. }
  278. }
  279. static int show_http_message(struct strbuf *type, struct strbuf *charset,
  280. struct strbuf *msg)
  281. {
  282. const char *p, *eol;
  283. /*
  284. * We only show text/plain parts, as other types are likely
  285. * to be ugly to look at on the user's terminal.
  286. */
  287. if (strcmp(type->buf, "text/plain"))
  288. return -1;
  289. if (charset->len)
  290. strbuf_reencode(msg, charset->buf, get_log_output_encoding());
  291. strbuf_trim(msg);
  292. if (!msg->len)
  293. return -1;
  294. p = msg->buf;
  295. do {
  296. eol = strchrnul(p, '\n');
  297. fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
  298. p = eol + 1;
  299. } while(*eol);
  300. return 0;
  301. }
  302. static int get_protocol_http_header(enum protocol_version version,
  303. struct strbuf *header)
  304. {
  305. if (version > 0) {
  306. strbuf_addf(header, GIT_PROTOCOL_HEADER ": version=%d",
  307. version);
  308. return 1;
  309. }
  310. return 0;
  311. }
  312. static void check_smart_http(struct discovery *d, const char *service,
  313. struct strbuf *type)
  314. {
  315. const char *p;
  316. struct packet_reader reader;
  317. /*
  318. * If we don't see x-$service-advertisement, then it's not smart-http.
  319. * But once we do, we commit to it and assume any other protocol
  320. * violations are hard errors.
  321. */
  322. if (!skip_prefix(type->buf, "application/x-", &p) ||
  323. !skip_prefix(p, service, &p) ||
  324. strcmp(p, "-advertisement"))
  325. return;
  326. packet_reader_init(&reader, -1, d->buf, d->len,
  327. PACKET_READ_CHOMP_NEWLINE |
  328. PACKET_READ_DIE_ON_ERR_PACKET);
  329. if (packet_reader_read(&reader) != PACKET_READ_NORMAL)
  330. die(_("invalid server response; expected service, got flush packet"));
  331. if (skip_prefix(reader.line, "# service=", &p) && !strcmp(p, service)) {
  332. /*
  333. * The header can include additional metadata lines, up
  334. * until a packet flush marker. Ignore these now, but
  335. * in the future we might start to scan them.
  336. */
  337. for (;;) {
  338. packet_reader_read(&reader);
  339. if (reader.pktlen <= 0) {
  340. break;
  341. }
  342. }
  343. /*
  344. * v0 smart http; callers expect us to soak up the
  345. * service and header packets
  346. */
  347. d->buf = reader.src_buffer;
  348. d->len = reader.src_len;
  349. d->proto_git = 1;
  350. } else if (!strcmp(reader.line, "version 2")) {
  351. /*
  352. * v2 smart http; do not consume version packet, which will
  353. * be handled elsewhere.
  354. */
  355. d->proto_git = 1;
  356. } else {
  357. die(_("invalid server response; got '%s'"), reader.line);
  358. }
  359. }
  360. static struct discovery *discover_refs(const char *service, int for_push)
  361. {
  362. struct strbuf type = STRBUF_INIT;
  363. struct strbuf charset = STRBUF_INIT;
  364. struct strbuf buffer = STRBUF_INIT;
  365. struct strbuf refs_url = STRBUF_INIT;
  366. struct strbuf effective_url = STRBUF_INIT;
  367. struct strbuf protocol_header = STRBUF_INIT;
  368. struct string_list extra_headers = STRING_LIST_INIT_DUP;
  369. struct discovery *last = last_discovery;
  370. int http_ret, maybe_smart = 0;
  371. struct http_get_options http_options;
  372. enum protocol_version version = get_protocol_version_config();
  373. if (last && !strcmp(service, last->service))
  374. return last;
  375. free_discovery(last);
  376. strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
  377. if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
  378. git_env_bool("GIT_SMART_HTTP", 1)) {
  379. maybe_smart = 1;
  380. if (!strchr(url.buf, '?'))
  381. strbuf_addch(&refs_url, '?');
  382. else
  383. strbuf_addch(&refs_url, '&');
  384. strbuf_addf(&refs_url, "service=%s", service);
  385. }
  386. /*
  387. * NEEDSWORK: If we are trying to use protocol v2 and we are planning
  388. * to perform a push, then fallback to v0 since the client doesn't know
  389. * how to push yet using v2.
  390. */
  391. if (version == protocol_v2 && !strcmp("git-receive-pack", service))
  392. version = protocol_v0;
  393. /* Add the extra Git-Protocol header */
  394. if (get_protocol_http_header(version, &protocol_header))
  395. string_list_append(&extra_headers, protocol_header.buf);
  396. memset(&http_options, 0, sizeof(http_options));
  397. http_options.content_type = &type;
  398. http_options.charset = &charset;
  399. http_options.effective_url = &effective_url;
  400. http_options.base_url = &url;
  401. http_options.extra_headers = &extra_headers;
  402. http_options.initial_request = 1;
  403. http_options.no_cache = 1;
  404. http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
  405. switch (http_ret) {
  406. case HTTP_OK:
  407. break;
  408. case HTTP_MISSING_TARGET:
  409. show_http_message(&type, &charset, &buffer);
  410. die(_("repository '%s' not found"),
  411. transport_anonymize_url(url.buf));
  412. case HTTP_NOAUTH:
  413. show_http_message(&type, &charset, &buffer);
  414. die(_("Authentication failed for '%s'"),
  415. transport_anonymize_url(url.buf));
  416. default:
  417. show_http_message(&type, &charset, &buffer);
  418. die(_("unable to access '%s': %s"),
  419. transport_anonymize_url(url.buf), curl_errorstr);
  420. }
  421. if (options.verbosity && !starts_with(refs_url.buf, url.buf)) {
  422. char *u = transport_anonymize_url(url.buf);
  423. warning(_("redirecting to %s"), u);
  424. free(u);
  425. }
  426. last= xcalloc(1, sizeof(*last_discovery));
  427. last->service = xstrdup(service);
  428. last->buf_alloc = strbuf_detach(&buffer, &last->len);
  429. last->buf = last->buf_alloc;
  430. if (maybe_smart)
  431. check_smart_http(last, service, &type);
  432. if (last->proto_git)
  433. last->refs = parse_git_refs(last, for_push);
  434. else
  435. last->refs = parse_info_refs(last);
  436. strbuf_release(&refs_url);
  437. strbuf_release(&type);
  438. strbuf_release(&charset);
  439. strbuf_release(&effective_url);
  440. strbuf_release(&buffer);
  441. strbuf_release(&protocol_header);
  442. string_list_clear(&extra_headers, 0);
  443. last_discovery = last;
  444. return last;
  445. }
  446. static struct ref *get_refs(int for_push)
  447. {
  448. struct discovery *heads;
  449. if (for_push)
  450. heads = discover_refs("git-receive-pack", for_push);
  451. else
  452. heads = discover_refs("git-upload-pack", for_push);
  453. return heads->refs;
  454. }
  455. static void output_refs(struct ref *refs)
  456. {
  457. struct ref *posn;
  458. for (posn = refs; posn; posn = posn->next) {
  459. if (posn->symref)
  460. printf("@%s %s\n", posn->symref, posn->name);
  461. else
  462. printf("%s %s\n", oid_to_hex(&posn->old_oid), posn->name);
  463. }
  464. printf("\n");
  465. fflush(stdout);
  466. }
  467. struct rpc_state {
  468. const char *service_name;
  469. char *service_url;
  470. char *hdr_content_type;
  471. char *hdr_accept;
  472. char *protocol_header;
  473. char *buf;
  474. size_t alloc;
  475. size_t len;
  476. size_t pos;
  477. int in;
  478. int out;
  479. int any_written;
  480. unsigned gzip_request : 1;
  481. unsigned initial_buffer : 1;
  482. /*
  483. * Whenever a pkt-line is read into buf, append the 4 characters
  484. * denoting its length before appending the payload.
  485. */
  486. unsigned write_line_lengths : 1;
  487. /*
  488. * Used by rpc_out; initialize to 0. This is true if a flush has been
  489. * read, but the corresponding line length (if write_line_lengths is
  490. * true) and EOF have not been sent to libcurl. Since each flush marks
  491. * the end of a request, each flush must be completely sent before any
  492. * further reading occurs.
  493. */
  494. unsigned flush_read_but_not_sent : 1;
  495. };
  496. /*
  497. * Appends the result of reading from rpc->out to the string represented by
  498. * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
  499. * enough space, 0 otherwise.
  500. *
  501. * If rpc->write_line_lengths is true, appends the line length as a 4-byte
  502. * hexadecimal string before appending the result described above.
  503. *
  504. * Writes the total number of bytes appended into appended.
  505. */
  506. static int rpc_read_from_out(struct rpc_state *rpc, int options,
  507. size_t *appended,
  508. enum packet_read_status *status) {
  509. size_t left;
  510. char *buf;
  511. int pktlen_raw;
  512. if (rpc->write_line_lengths) {
  513. left = rpc->alloc - rpc->len - 4;
  514. buf = rpc->buf + rpc->len + 4;
  515. } else {
  516. left = rpc->alloc - rpc->len;
  517. buf = rpc->buf + rpc->len;
  518. }
  519. if (left < LARGE_PACKET_MAX)
  520. return 0;
  521. *status = packet_read_with_status(rpc->out, NULL, NULL, buf,
  522. left, &pktlen_raw, options);
  523. if (*status != PACKET_READ_EOF) {
  524. *appended = pktlen_raw + (rpc->write_line_lengths ? 4 : 0);
  525. rpc->len += *appended;
  526. }
  527. if (rpc->write_line_lengths) {
  528. switch (*status) {
  529. case PACKET_READ_EOF:
  530. if (!(options & PACKET_READ_GENTLE_ON_EOF))
  531. die(_("shouldn't have EOF when not gentle on EOF"));
  532. break;
  533. case PACKET_READ_NORMAL:
  534. set_packet_header(buf - 4, *appended);
  535. break;
  536. case PACKET_READ_DELIM:
  537. memcpy(buf - 4, "0001", 4);
  538. break;
  539. case PACKET_READ_FLUSH:
  540. memcpy(buf - 4, "0000", 4);
  541. break;
  542. }
  543. }
  544. return 1;
  545. }
  546. static size_t rpc_out(void *ptr, size_t eltsize,
  547. size_t nmemb, void *buffer_)
  548. {
  549. size_t max = eltsize * nmemb;
  550. struct rpc_state *rpc = buffer_;
  551. size_t avail = rpc->len - rpc->pos;
  552. enum packet_read_status status;
  553. if (!avail) {
  554. rpc->initial_buffer = 0;
  555. rpc->len = 0;
  556. rpc->pos = 0;
  557. if (!rpc->flush_read_but_not_sent) {
  558. if (!rpc_read_from_out(rpc, 0, &avail, &status))
  559. BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
  560. if (status == PACKET_READ_FLUSH)
  561. rpc->flush_read_but_not_sent = 1;
  562. }
  563. /*
  564. * If flush_read_but_not_sent is true, we have already read one
  565. * full request but have not fully sent it + EOF, which is why
  566. * we need to refrain from reading.
  567. */
  568. }
  569. if (rpc->flush_read_but_not_sent) {
  570. if (!avail) {
  571. /*
  572. * The line length either does not need to be sent at
  573. * all or has already been completely sent. Now we can
  574. * return 0, indicating EOF, meaning that the flush has
  575. * been fully sent.
  576. */
  577. rpc->flush_read_but_not_sent = 0;
  578. return 0;
  579. }
  580. /*
  581. * If avail is non-zerp, the line length for the flush still
  582. * hasn't been fully sent. Proceed with sending the line
  583. * length.
  584. */
  585. }
  586. if (max < avail)
  587. avail = max;
  588. memcpy(ptr, rpc->buf + rpc->pos, avail);
  589. rpc->pos += avail;
  590. return avail;
  591. }
  592. #ifndef NO_CURL_IOCTL
  593. static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
  594. {
  595. struct rpc_state *rpc = clientp;
  596. switch (cmd) {
  597. case CURLIOCMD_NOP:
  598. return CURLIOE_OK;
  599. case CURLIOCMD_RESTARTREAD:
  600. if (rpc->initial_buffer) {
  601. rpc->pos = 0;
  602. return CURLIOE_OK;
  603. }
  604. error(_("unable to rewind rpc post data - try increasing http.postBuffer"));
  605. return CURLIOE_FAILRESTART;
  606. default:
  607. return CURLIOE_UNKNOWNCMD;
  608. }
  609. }
  610. #endif
  611. struct rpc_in_data {
  612. struct rpc_state *rpc;
  613. struct active_request_slot *slot;
  614. };
  615. /*
  616. * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
  617. * from ptr.
  618. */
  619. static size_t rpc_in(char *ptr, size_t eltsize,
  620. size_t nmemb, void *buffer_)
  621. {
  622. size_t size = eltsize * nmemb;
  623. struct rpc_in_data *data = buffer_;
  624. long response_code;
  625. if (curl_easy_getinfo(data->slot->curl, CURLINFO_RESPONSE_CODE,
  626. &response_code) != CURLE_OK)
  627. return size;
  628. if (response_code >= 300)
  629. return size;
  630. if (size)
  631. data->rpc->any_written = 1;
  632. write_or_die(data->rpc->in, ptr, size);
  633. return size;
  634. }
  635. static int run_slot(struct active_request_slot *slot,
  636. struct slot_results *results)
  637. {
  638. int err;
  639. struct slot_results results_buf;
  640. if (!results)
  641. results = &results_buf;
  642. err = run_one_slot(slot, results);
  643. if (err != HTTP_OK && err != HTTP_REAUTH) {
  644. struct strbuf msg = STRBUF_INIT;
  645. if (results->http_code && results->http_code != 200)
  646. strbuf_addf(&msg, "HTTP %ld", results->http_code);
  647. if (results->curl_result != CURLE_OK) {
  648. if (msg.len)
  649. strbuf_addch(&msg, ' ');
  650. strbuf_addf(&msg, "curl %d", results->curl_result);
  651. if (curl_errorstr[0]) {
  652. strbuf_addch(&msg, ' ');
  653. strbuf_addstr(&msg, curl_errorstr);
  654. }
  655. }
  656. error(_("RPC failed; %s"), msg.buf);
  657. strbuf_release(&msg);
  658. }
  659. return err;
  660. }
  661. static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
  662. {
  663. struct active_request_slot *slot;
  664. struct curl_slist *headers = http_copy_default_headers();
  665. struct strbuf buf = STRBUF_INIT;
  666. int err;
  667. slot = get_active_slot();
  668. headers = curl_slist_append(headers, rpc->hdr_content_type);
  669. headers = curl_slist_append(headers, rpc->hdr_accept);
  670. curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
  671. curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
  672. curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
  673. curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
  674. curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
  675. curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
  676. curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
  677. curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
  678. curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
  679. err = run_slot(slot, results);
  680. curl_slist_free_all(headers);
  681. strbuf_release(&buf);
  682. return err;
  683. }
  684. static curl_off_t xcurl_off_t(size_t len)
  685. {
  686. uintmax_t size = len;
  687. if (size > maximum_signed_value_of_type(curl_off_t))
  688. die(_("cannot handle pushes this big"));
  689. return (curl_off_t)size;
  690. }
  691. /*
  692. * If flush_received is true, do not attempt to read any more; just use what's
  693. * in rpc->buf.
  694. */
  695. static int post_rpc(struct rpc_state *rpc, int flush_received)
  696. {
  697. struct active_request_slot *slot;
  698. struct curl_slist *headers = http_copy_default_headers();
  699. int use_gzip = rpc->gzip_request;
  700. char *gzip_body = NULL;
  701. size_t gzip_size = 0;
  702. int err, large_request = 0;
  703. int needs_100_continue = 0;
  704. struct rpc_in_data rpc_in_data;
  705. /* Try to load the entire request, if we can fit it into the
  706. * allocated buffer space we can use HTTP/1.0 and avoid the
  707. * chunked encoding mess.
  708. */
  709. if (!flush_received) {
  710. while (1) {
  711. size_t n;
  712. enum packet_read_status status;
  713. if (!rpc_read_from_out(rpc, 0, &n, &status)) {
  714. large_request = 1;
  715. use_gzip = 0;
  716. break;
  717. }
  718. if (status == PACKET_READ_FLUSH)
  719. break;
  720. }
  721. }
  722. if (large_request) {
  723. struct slot_results results;
  724. do {
  725. err = probe_rpc(rpc, &results);
  726. if (err == HTTP_REAUTH)
  727. credential_fill(&http_auth);
  728. } while (err == HTTP_REAUTH);
  729. if (err != HTTP_OK)
  730. return -1;
  731. if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
  732. needs_100_continue = 1;
  733. }
  734. headers = curl_slist_append(headers, rpc->hdr_content_type);
  735. headers = curl_slist_append(headers, rpc->hdr_accept);
  736. headers = curl_slist_append(headers, needs_100_continue ?
  737. "Expect: 100-continue" : "Expect:");
  738. /* Add the extra Git-Protocol header */
  739. if (rpc->protocol_header)
  740. headers = curl_slist_append(headers, rpc->protocol_header);
  741. retry:
  742. slot = get_active_slot();
  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);
  746. curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
  747. if (large_request) {
  748. /* The request body is large and the size cannot be predicted.
  749. * We must use chunked encoding to send it.
  750. */
  751. headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
  752. rpc->initial_buffer = 1;
  753. curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
  754. curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
  755. #ifndef NO_CURL_IOCTL
  756. curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
  757. curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
  758. #endif
  759. if (options.verbosity > 1) {
  760. fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
  761. fflush(stderr);
  762. }
  763. } else if (gzip_body) {
  764. /*
  765. * If we are looping to retry authentication, then the previous
  766. * run will have set up the headers and gzip buffer already,
  767. * and we just need to send it.
  768. */
  769. curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
  770. curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
  771. } else if (use_gzip && 1024 < rpc->len) {
  772. /* The client backend isn't giving us compressed data so
  773. * we can try to deflate it ourselves, this may save on
  774. * the transfer time.
  775. */
  776. git_zstream stream;
  777. int ret;
  778. git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
  779. gzip_size = git_deflate_bound(&stream, rpc->len);
  780. gzip_body = xmalloc(gzip_size);
  781. stream.next_in = (unsigned char *)rpc->buf;
  782. stream.avail_in = rpc->len;
  783. stream.next_out = (unsigned char *)gzip_body;
  784. stream.avail_out = gzip_size;
  785. ret = git_deflate(&stream, Z_FINISH);
  786. if (ret != Z_STREAM_END)
  787. die(_("cannot deflate request; zlib deflate error %d"), ret);
  788. ret = git_deflate_end_gently(&stream);
  789. if (ret != Z_OK)
  790. die(_("cannot deflate request; zlib end error %d"), ret);
  791. gzip_size = stream.total_out;
  792. headers = curl_slist_append(headers, "Content-Encoding: gzip");
  793. curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
  794. curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
  795. if (options.verbosity > 1) {
  796. fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
  797. rpc->service_name,
  798. (unsigned long)rpc->len, (unsigned long)gzip_size);
  799. fflush(stderr);
  800. }
  801. } else {
  802. /* We know the complete request size in advance, use the
  803. * more normal Content-Length approach.
  804. */
  805. curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
  806. curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
  807. if (options.verbosity > 1) {
  808. fprintf(stderr, "POST %s (%lu bytes)\n",
  809. rpc->service_name, (unsigned long)rpc->len);
  810. fflush(stderr);
  811. }
  812. }
  813. curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
  814. curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
  815. rpc_in_data.rpc = rpc;
  816. rpc_in_data.slot = slot;
  817. curl_easy_setopt(slot->curl, CURLOPT_FILE, &rpc_in_data);
  818. curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
  819. rpc->any_written = 0;
  820. err = run_slot(slot, NULL);
  821. if (err == HTTP_REAUTH && !large_request) {
  822. credential_fill(&http_auth);
  823. goto retry;
  824. }
  825. if (err != HTTP_OK)
  826. err = -1;
  827. if (!rpc->any_written)
  828. err = -1;
  829. curl_slist_free_all(headers);
  830. free(gzip_body);
  831. return err;
  832. }
  833. static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
  834. const char **client_argv, const struct strbuf *preamble,
  835. struct strbuf *rpc_result)
  836. {
  837. const char *svc = rpc->service_name;
  838. struct strbuf buf = STRBUF_INIT;
  839. struct child_process client = CHILD_PROCESS_INIT;
  840. int err = 0;
  841. client.in = -1;
  842. client.out = -1;
  843. client.git_cmd = 1;
  844. client.argv = client_argv;
  845. if (start_command(&client))
  846. exit(1);
  847. write_or_die(client.in, preamble->buf, preamble->len);
  848. if (heads)
  849. write_or_die(client.in, heads->buf, heads->len);
  850. rpc->alloc = http_post_buffer;
  851. rpc->buf = xmalloc(rpc->alloc);
  852. rpc->in = client.in;
  853. rpc->out = client.out;
  854. strbuf_addf(&buf, "%s%s", url.buf, svc);
  855. rpc->service_url = strbuf_detach(&buf, NULL);
  856. strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
  857. rpc->hdr_content_type = strbuf_detach(&buf, NULL);
  858. strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
  859. rpc->hdr_accept = strbuf_detach(&buf, NULL);
  860. if (get_protocol_http_header(heads->version, &buf))
  861. rpc->protocol_header = strbuf_detach(&buf, NULL);
  862. else
  863. rpc->protocol_header = NULL;
  864. while (!err) {
  865. int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
  866. if (!n)
  867. break;
  868. rpc->pos = 0;
  869. rpc->len = n;
  870. err |= post_rpc(rpc, 0);
  871. }
  872. close(client.in);
  873. client.in = -1;
  874. if (!err) {
  875. strbuf_read(rpc_result, client.out, 0);
  876. } else {
  877. char buf[4096];
  878. for (;;)
  879. if (xread(client.out, buf, sizeof(buf)) <= 0)
  880. break;
  881. }
  882. close(client.out);
  883. client.out = -1;
  884. err |= finish_command(&client);
  885. free(rpc->service_url);
  886. free(rpc->hdr_content_type);
  887. free(rpc->hdr_accept);
  888. free(rpc->protocol_header);
  889. free(rpc->buf);
  890. strbuf_release(&buf);
  891. return err;
  892. }
  893. static int fetch_dumb(int nr_heads, struct ref **to_fetch)
  894. {
  895. struct walker *walker;
  896. char **targets;
  897. int ret, i;
  898. ALLOC_ARRAY(targets, nr_heads);
  899. if (options.depth || options.deepen_since)
  900. die(_("dumb http transport does not support shallow capabilities"));
  901. for (i = 0; i < nr_heads; i++)
  902. targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
  903. walker = get_http_walker(url.buf);
  904. walker->get_verbosely = options.verbosity >= 3;
  905. walker->get_recover = 0;
  906. ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
  907. walker_free(walker);
  908. for (i = 0; i < nr_heads; i++)
  909. free(targets[i]);
  910. free(targets);
  911. return ret ? error(_("fetch failed.")) : 0;
  912. }
  913. static int fetch_git(struct discovery *heads,
  914. int nr_heads, struct ref **to_fetch)
  915. {
  916. struct rpc_state rpc;
  917. struct strbuf preamble = STRBUF_INIT;
  918. int i, err;
  919. struct argv_array args = ARGV_ARRAY_INIT;
  920. struct strbuf rpc_result = STRBUF_INIT;
  921. argv_array_pushl(&args, "fetch-pack", "--stateless-rpc",
  922. "--stdin", "--lock-pack", NULL);
  923. if (options.followtags)
  924. argv_array_push(&args, "--include-tag");
  925. if (options.thin)
  926. argv_array_push(&args, "--thin");
  927. if (options.verbosity >= 3)
  928. argv_array_pushl(&args, "-v", "-v", NULL);
  929. if (options.check_self_contained_and_connected)
  930. argv_array_push(&args, "--check-self-contained-and-connected");
  931. if (options.cloning)
  932. argv_array_push(&args, "--cloning");
  933. if (options.update_shallow)
  934. argv_array_push(&args, "--update-shallow");
  935. if (!options.progress)
  936. argv_array_push(&args, "--no-progress");
  937. if (options.depth)
  938. argv_array_pushf(&args, "--depth=%lu", options.depth);
  939. if (options.deepen_since)
  940. argv_array_pushf(&args, "--shallow-since=%s", options.deepen_since);
  941. for (i = 0; i < options.deepen_not.nr; i++)
  942. argv_array_pushf(&args, "--shallow-exclude=%s",
  943. options.deepen_not.items[i].string);
  944. if (options.deepen_relative && options.depth)
  945. argv_array_push(&args, "--deepen-relative");
  946. if (options.from_promisor)
  947. argv_array_push(&args, "--from-promisor");
  948. if (options.no_dependents)
  949. argv_array_push(&args, "--no-dependents");
  950. if (options.filter)
  951. argv_array_pushf(&args, "--filter=%s", options.filter);
  952. argv_array_push(&args, url.buf);
  953. for (i = 0; i < nr_heads; i++) {
  954. struct ref *ref = to_fetch[i];
  955. if (!*ref->name)
  956. die(_("cannot fetch by sha1 over smart http"));
  957. packet_buf_write(&preamble, "%s %s\n",
  958. oid_to_hex(&ref->old_oid), ref->name);
  959. }
  960. packet_buf_flush(&preamble);
  961. memset(&rpc, 0, sizeof(rpc));
  962. rpc.service_name = "git-upload-pack",
  963. rpc.gzip_request = 1;
  964. err = rpc_service(&rpc, heads, args.argv, &preamble, &rpc_result);
  965. if (rpc_result.len)
  966. write_or_die(1, rpc_result.buf, rpc_result.len);
  967. strbuf_release(&rpc_result);
  968. strbuf_release(&preamble);
  969. argv_array_clear(&args);
  970. return err;
  971. }
  972. static int fetch(int nr_heads, struct ref **to_fetch)
  973. {
  974. struct discovery *d = discover_refs("git-upload-pack", 0);
  975. if (d->proto_git)
  976. return fetch_git(d, nr_heads, to_fetch);
  977. else
  978. return fetch_dumb(nr_heads, to_fetch);
  979. }
  980. static void parse_fetch(struct strbuf *buf)
  981. {
  982. struct ref **to_fetch = NULL;
  983. struct ref *list_head = NULL;
  984. struct ref **list = &list_head;
  985. int alloc_heads = 0, nr_heads = 0;
  986. do {
  987. const char *p;
  988. if (skip_prefix(buf->buf, "fetch ", &p)) {
  989. const char *name;
  990. struct ref *ref;
  991. struct object_id old_oid;
  992. const char *q;
  993. if (parse_oid_hex(p, &old_oid, &q))
  994. die(_("protocol error: expected sha/ref, got '%s'"), p);
  995. if (*q == ' ')
  996. name = q + 1;
  997. else if (!*q)
  998. name = "";
  999. else
  1000. die(_("protocol error: expected sha/ref, got '%s'"), p);
  1001. ref = alloc_ref(name);
  1002. oidcpy(&ref->old_oid, &old_oid);
  1003. *list = ref;
  1004. list = &ref->next;
  1005. ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
  1006. to_fetch[nr_heads++] = ref;
  1007. }
  1008. else
  1009. die(_("http transport does not support %s"), buf->buf);
  1010. strbuf_reset(buf);
  1011. if (strbuf_getline_lf(buf, stdin) == EOF)
  1012. return;
  1013. if (!*buf->buf)
  1014. break;
  1015. } while (1);
  1016. if (fetch(nr_heads, to_fetch))
  1017. exit(128); /* error already reported */
  1018. free_refs(list_head);
  1019. free(to_fetch);
  1020. printf("\n");
  1021. fflush(stdout);
  1022. strbuf_reset(buf);
  1023. }
  1024. static int push_dav(int nr_spec, const char **specs)
  1025. {
  1026. struct child_process child = CHILD_PROCESS_INIT;
  1027. size_t i;
  1028. child.git_cmd = 1;
  1029. argv_array_push(&child.args, "http-push");
  1030. argv_array_push(&child.args, "--helper-status");
  1031. if (options.dry_run)
  1032. argv_array_push(&child.args, "--dry-run");
  1033. if (options.verbosity > 1)
  1034. argv_array_push(&child.args, "--verbose");
  1035. argv_array_push(&child.args, url.buf);
  1036. for (i = 0; i < nr_spec; i++)
  1037. argv_array_push(&child.args, specs[i]);
  1038. if (run_command(&child))
  1039. die(_("git-http-push failed"));
  1040. return 0;
  1041. }
  1042. static int push_git(struct discovery *heads, int nr_spec, const char **specs)
  1043. {
  1044. struct rpc_state rpc;
  1045. int i, err;
  1046. struct argv_array args;
  1047. struct string_list_item *cas_option;
  1048. struct strbuf preamble = STRBUF_INIT;
  1049. struct strbuf rpc_result = STRBUF_INIT;
  1050. argv_array_init(&args);
  1051. argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
  1052. NULL);
  1053. if (options.thin)
  1054. argv_array_push(&args, "--thin");
  1055. if (options.dry_run)
  1056. argv_array_push(&args, "--dry-run");
  1057. if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
  1058. argv_array_push(&args, "--signed=yes");
  1059. else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
  1060. argv_array_push(&args, "--signed=if-asked");
  1061. if (options.verbosity == 0)
  1062. argv_array_push(&args, "--quiet");
  1063. else if (options.verbosity > 1)
  1064. argv_array_push(&args, "--verbose");
  1065. for (i = 0; i < options.push_options.nr; i++)
  1066. argv_array_pushf(&args, "--push-option=%s",
  1067. options.push_options.items[i].string);
  1068. argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
  1069. for_each_string_list_item(cas_option, &cas_options)
  1070. argv_array_push(&args, cas_option->string);
  1071. argv_array_push(&args, url.buf);
  1072. argv_array_push(&args, "--stdin");
  1073. for (i = 0; i < nr_spec; i++)
  1074. packet_buf_write(&preamble, "%s\n", specs[i]);
  1075. packet_buf_flush(&preamble);
  1076. memset(&rpc, 0, sizeof(rpc));
  1077. rpc.service_name = "git-receive-pack",
  1078. err = rpc_service(&rpc, heads, args.argv, &preamble, &rpc_result);
  1079. if (rpc_result.len)
  1080. write_or_die(1, rpc_result.buf, rpc_result.len);
  1081. strbuf_release(&rpc_result);
  1082. strbuf_release(&preamble);
  1083. argv_array_clear(&args);
  1084. return err;
  1085. }
  1086. static int push(int nr_spec, const char **specs)
  1087. {
  1088. struct discovery *heads = discover_refs("git-receive-pack", 1);
  1089. int ret;
  1090. if (heads->proto_git)
  1091. ret = push_git(heads, nr_spec, specs);
  1092. else
  1093. ret = push_dav(nr_spec, specs);
  1094. free_discovery(heads);
  1095. return ret;
  1096. }
  1097. static void parse_push(struct strbuf *buf)
  1098. {
  1099. struct argv_array specs = ARGV_ARRAY_INIT;
  1100. int ret;
  1101. do {
  1102. if (starts_with(buf->buf, "push "))
  1103. argv_array_push(&specs, buf->buf + 5);
  1104. else
  1105. die(_("http transport does not support %s"), buf->buf);
  1106. strbuf_reset(buf);
  1107. if (strbuf_getline_lf(buf, stdin) == EOF)
  1108. goto free_specs;
  1109. if (!*buf->buf)
  1110. break;
  1111. } while (1);
  1112. ret = push(specs.argc, specs.argv);
  1113. printf("\n");
  1114. fflush(stdout);
  1115. if (ret)
  1116. exit(128); /* error already reported */
  1117. free_specs:
  1118. argv_array_clear(&specs);
  1119. }
  1120. static int stateless_connect(const char *service_name)
  1121. {
  1122. struct discovery *discover;
  1123. struct rpc_state rpc;
  1124. struct strbuf buf = STRBUF_INIT;
  1125. /*
  1126. * Run the info/refs request and see if the server supports protocol
  1127. * v2. If and only if the server supports v2 can we successfully
  1128. * establish a stateless connection, otherwise we need to tell the
  1129. * client to fallback to using other transport helper functions to
  1130. * complete their request.
  1131. */
  1132. discover = discover_refs(service_name, 0);
  1133. if (discover->version != protocol_v2) {
  1134. printf("fallback\n");
  1135. fflush(stdout);
  1136. return -1;
  1137. } else {
  1138. /* Stateless Connection established */
  1139. printf("\n");
  1140. fflush(stdout);
  1141. }
  1142. rpc.service_name = service_name;
  1143. rpc.service_url = xstrfmt("%s%s", url.buf, rpc.service_name);
  1144. rpc.hdr_content_type = xstrfmt("Content-Type: application/x-%s-request", rpc.service_name);
  1145. rpc.hdr_accept = xstrfmt("Accept: application/x-%s-result", rpc.service_name);
  1146. if (get_protocol_http_header(discover->version, &buf)) {
  1147. rpc.protocol_header = strbuf_detach(&buf, NULL);
  1148. } else {
  1149. rpc.protocol_header = NULL;
  1150. strbuf_release(&buf);
  1151. }
  1152. rpc.buf = xmalloc(http_post_buffer);
  1153. rpc.alloc = http_post_buffer;
  1154. rpc.len = 0;
  1155. rpc.pos = 0;
  1156. rpc.in = 1;
  1157. rpc.out = 0;
  1158. rpc.any_written = 0;
  1159. rpc.gzip_request = 1;
  1160. rpc.initial_buffer = 0;
  1161. rpc.write_line_lengths = 1;
  1162. rpc.flush_read_but_not_sent = 0;
  1163. /*
  1164. * Dump the capability listing that we got from the server earlier
  1165. * during the info/refs request.
  1166. */
  1167. write_or_die(rpc.in, discover->buf, discover->len);
  1168. /* Until we see EOF keep sending POSTs */
  1169. while (1) {
  1170. size_t avail;
  1171. enum packet_read_status status;
  1172. if (!rpc_read_from_out(&rpc, PACKET_READ_GENTLE_ON_EOF, &avail,
  1173. &status))
  1174. BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
  1175. if (status == PACKET_READ_EOF)
  1176. break;
  1177. if (post_rpc(&rpc, status == PACKET_READ_FLUSH))
  1178. /* We would have an err here */
  1179. break;
  1180. /* Reset the buffer for next request */
  1181. rpc.len = 0;
  1182. }
  1183. free(rpc.service_url);
  1184. free(rpc.hdr_content_type);
  1185. free(rpc.hdr_accept);
  1186. free(rpc.protocol_header);
  1187. free(rpc.buf);
  1188. strbuf_release(&buf);
  1189. return 0;
  1190. }
  1191. int cmd_main(int argc, const char **argv)
  1192. {
  1193. struct strbuf buf = STRBUF_INIT;
  1194. int nongit;
  1195. setup_git_directory_gently(&nongit);
  1196. if (argc < 2) {
  1197. error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
  1198. return 1;
  1199. }
  1200. options.verbosity = 1;
  1201. options.progress = !!isatty(2);
  1202. options.thin = 1;
  1203. string_list_init(&options.deepen_not, 1);
  1204. string_list_init(&options.push_options, 1);
  1205. /*
  1206. * Just report "remote-curl" here (folding all the various aliases
  1207. * ("git-remote-http", "git-remote-https", and etc.) here since they
  1208. * are all just copies of the same actual executable.
  1209. */
  1210. trace2_cmd_name("remote-curl");
  1211. remote = remote_get(argv[1]);
  1212. if (argc > 2) {
  1213. end_url_with_slash(&url, argv[2]);
  1214. } else {
  1215. end_url_with_slash(&url, remote->url[0]);
  1216. }
  1217. http_init(remote, url.buf, 0);
  1218. do {
  1219. const char *arg;
  1220. if (strbuf_getline_lf(&buf, stdin) == EOF) {
  1221. if (ferror(stdin))
  1222. error(_("remote-curl: error reading command stream from git"));
  1223. return 1;
  1224. }
  1225. if (buf.len == 0)
  1226. break;
  1227. if (starts_with(buf.buf, "fetch ")) {
  1228. if (nongit)
  1229. die(_("remote-curl: fetch attempted without a local repo"));
  1230. parse_fetch(&buf);
  1231. } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
  1232. int for_push = !!strstr(buf.buf + 4, "for-push");
  1233. output_refs(get_refs(for_push));
  1234. } else if (starts_with(buf.buf, "push ")) {
  1235. parse_push(&buf);
  1236. } else if (skip_prefix(buf.buf, "option ", &arg)) {
  1237. char *value = strchr(arg, ' ');
  1238. int result;
  1239. if (value)
  1240. *value++ = '\0';
  1241. else
  1242. value = "true";
  1243. result = set_option(arg, value);
  1244. if (!result)
  1245. printf("ok\n");
  1246. else if (result < 0)
  1247. printf("error invalid value\n");
  1248. else
  1249. printf("unsupported\n");
  1250. fflush(stdout);
  1251. } else if (!strcmp(buf.buf, "capabilities")) {
  1252. printf("stateless-connect\n");
  1253. printf("fetch\n");
  1254. printf("option\n");
  1255. printf("push\n");
  1256. printf("check-connectivity\n");
  1257. printf("\n");
  1258. fflush(stdout);
  1259. } else if (skip_prefix(buf.buf, "stateless-connect ", &arg)) {
  1260. if (!stateless_connect(arg))
  1261. break;
  1262. } else {
  1263. error(_("remote-curl: unknown command '%s' from git"), buf.buf);
  1264. return 1;
  1265. }
  1266. strbuf_reset(&buf);
  1267. } while (1);
  1268. http_cleanup();
  1269. return 0;
  1270. }