Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 1 | #include "cache.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 2 | #include "config.h" |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 3 | #include "remote.h" |
Brandon Williams | ad6ac12 | 2018-03-14 11:31:45 -0700 | [diff] [blame] | 4 | #include "connect.h" |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 5 | #include "strbuf.h" |
| 6 | #include "walker.h" |
| 7 | #include "http.h" |
Stefan Beller | d807c4a | 2018-04-10 14:26:18 -0700 | [diff] [blame] | 8 | #include "exec-cmd.h" |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 9 | #include "run-command.h" |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 10 | #include "pkt-line.h" |
Junio C Hamano | 05c1eb1 | 2013-08-02 15:14:50 -0700 | [diff] [blame] | 11 | #include "string-list.h" |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 12 | #include "sideband.h" |
Junio C Hamano | 222b121 | 2013-07-08 22:16:31 -0700 | [diff] [blame] | 13 | #include "argv-array.h" |
Jeff King | 2501aff | 2013-09-28 04:31:45 -0400 | [diff] [blame] | 14 | #include "credential.h" |
Nguyễn Thái Ngọc Duy | 1609488 | 2013-12-05 20:02:50 +0700 | [diff] [blame] | 15 | #include "sha1-array.h" |
Dave Borowitz | 3026109 | 2015-08-19 11:26:46 -0400 | [diff] [blame] | 16 | #include "send-pack.h" |
Brandon Williams | ad6ac12 | 2018-03-14 11:31:45 -0700 | [diff] [blame] | 17 | #include "protocol.h" |
Jeff King | 90dce21 | 2018-02-19 14:50:14 -0500 | [diff] [blame] | 18 | #include "quote.h" |
Johannes Schindelin | c1284b2 | 2019-03-04 07:33:46 -0800 | [diff] [blame] | 19 | #include "transport.h" |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 20 | |
Shawn O. Pearce | 37a8768 | 2009-10-30 17:47:26 -0700 | [diff] [blame] | 21 | static struct remote *remote; |
Jeff King | b227bbc | 2013-09-28 04:35:25 -0400 | [diff] [blame] | 22 | /* always ends with a trailing slash */ |
| 23 | static struct strbuf url = STRBUF_INIT; |
Shawn O. Pearce | 37a8768 | 2009-10-30 17:47:26 -0700 | [diff] [blame] | 24 | |
Shawn O. Pearce | ef08ef9 | 2009-10-30 17:47:29 -0700 | [diff] [blame] | 25 | struct options { |
| 26 | int verbosity; |
| 27 | unsigned long depth; |
Nguyễn Thái Ngọc Duy | 508ea88 | 2016-06-12 17:53:59 +0700 | [diff] [blame] | 28 | char *deepen_since; |
Nguyễn Thái Ngọc Duy | a45a260 | 2016-06-12 17:54:04 +0700 | [diff] [blame] | 29 | struct string_list deepen_not; |
Brandon Williams | 511155d | 2017-03-22 15:22:00 -0700 | [diff] [blame] | 30 | struct string_list push_options; |
Jeff Hostetler | acb0c57 | 2017-12-08 15:58:44 +0000 | [diff] [blame] | 31 | char *filter; |
Shawn O. Pearce | ef08ef9 | 2009-10-30 17:47:29 -0700 | [diff] [blame] | 32 | unsigned progress : 1, |
Nguyễn Thái Ngọc Duy | 9ba3804 | 2013-07-21 15:18:05 +0700 | [diff] [blame] | 33 | check_self_contained_and_connected : 1, |
Nguyễn Thái Ngọc Duy | 1609488 | 2013-12-05 20:02:50 +0700 | [diff] [blame] | 34 | cloning : 1, |
| 35 | update_shallow : 1, |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 36 | followtags : 1, |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 37 | dry_run : 1, |
Junio C Hamano | 0ea47f9 | 2014-09-15 14:59:00 -0700 | [diff] [blame] | 38 | thin : 1, |
Dave Borowitz | 3026109 | 2015-08-19 11:26:46 -0400 | [diff] [blame] | 39 | /* One of the SEND_PACK_PUSH_CERT_* constants. */ |
Nguyễn Thái Ngọc Duy | cccf74e | 2016-06-12 17:54:09 +0700 | [diff] [blame] | 40 | push_cert : 2, |
Jonathan Tan | 88e2f9e | 2017-12-05 16:58:49 +0000 | [diff] [blame] | 41 | deepen_relative : 1, |
| 42 | from_promisor : 1, |
| 43 | no_dependents : 1; |
Shawn O. Pearce | ef08ef9 | 2009-10-30 17:47:29 -0700 | [diff] [blame] | 44 | }; |
| 45 | static struct options options; |
Junio C Hamano | 05c1eb1 | 2013-08-02 15:14:50 -0700 | [diff] [blame] | 46 | static struct string_list cas_options = STRING_LIST_INIT_DUP; |
Shawn O. Pearce | ef08ef9 | 2009-10-30 17:47:29 -0700 | [diff] [blame] | 47 | |
Shawn O. Pearce | ef08ef9 | 2009-10-30 17:47:29 -0700 | [diff] [blame] | 48 | static 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. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 65 | return 0; |
Shawn O. Pearce | ef08ef9 | 2009-10-30 17:47:29 -0700 | [diff] [blame] | 66 | } |
| 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. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 73 | return 0; |
Shawn O. Pearce | ef08ef9 | 2009-10-30 17:47:29 -0700 | [diff] [blame] | 74 | } |
Nguyễn Thái Ngọc Duy | 508ea88 | 2016-06-12 17:53:59 +0700 | [diff] [blame] | 75 | else if (!strcmp(name, "deepen-since")) { |
| 76 | options.deepen_since = xstrdup(value); |
| 77 | return 0; |
| 78 | } |
Nguyễn Thái Ngọc Duy | a45a260 | 2016-06-12 17:54:04 +0700 | [diff] [blame] | 79 | else if (!strcmp(name, "deepen-not")) { |
| 80 | string_list_append(&options.deepen_not, value); |
| 81 | return 0; |
| 82 | } |
Nguyễn Thái Ngọc Duy | cccf74e | 2016-06-12 17:54:09 +0700 | [diff] [blame] | 83 | 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. Pearce | ef08ef9 | 2009-10-30 17:47:29 -0700 | [diff] [blame] | 92 | 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. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 99 | return 0; |
Shawn O. Pearce | ef08ef9 | 2009-10-30 17:47:29 -0700 | [diff] [blame] | 100 | } |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 101 | 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 Duy | 9ba3804 | 2013-07-21 15:18:05 +0700 | [diff] [blame] | 110 | 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 Hamano | 05c1eb1 | 2013-08-02 15:14:50 -0700 | [diff] [blame] | 119 | 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 Duy | 1609488 | 2013-12-05 20:02:50 +0700 | [diff] [blame] | 125 | } 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 Hamano | 0ea47f9 | 2014-09-15 14:59:00 -0700 | [diff] [blame] | 141 | } else if (!strcmp(name, "pushcert")) { |
| 142 | if (!strcmp(value, "true")) |
Dave Borowitz | 3026109 | 2015-08-19 11:26:46 -0400 | [diff] [blame] | 143 | options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS; |
Junio C Hamano | 0ea47f9 | 2014-09-15 14:59:00 -0700 | [diff] [blame] | 144 | else if (!strcmp(value, "false")) |
Dave Borowitz | 3026109 | 2015-08-19 11:26:46 -0400 | [diff] [blame] | 145 | 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 Hamano | 0ea47f9 | 2014-09-15 14:59:00 -0700 | [diff] [blame] | 148 | else |
| 149 | return -1; |
| 150 | return 0; |
Brandon Williams | 511155d | 2017-03-22 15:22:00 -0700 | [diff] [blame] | 151 | } else if (!strcmp(name, "push-option")) { |
Jeff King | 90dce21 | 2018-02-19 14:50:14 -0500 | [diff] [blame] | 152 | 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 Schindelin | ed8b413 | 2019-03-05 15:20:40 -0800 | [diff] [blame] | 157 | die(_("invalid quoting in push-option value: '%s'"), value); |
Jeff King | 90dce21 | 2018-02-19 14:50:14 -0500 | [diff] [blame] | 158 | string_list_append_nodup(&options.push_options, |
| 159 | strbuf_detach(&unquoted, NULL)); |
| 160 | } |
Brandon Williams | 511155d | 2017-03-22 15:22:00 -0700 | [diff] [blame] | 161 | return 0; |
Eric Wong | c915f11 | 2016-02-03 04:09:14 +0000 | [diff] [blame] | 162 | |
| 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 Tan | 88e2f9e | 2017-12-05 16:58:49 +0000 | [diff] [blame] | 175 | } 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 Hostetler | acb0c57 | 2017-12-08 15:58:44 +0000 | [diff] [blame] | 181 | } else if (!strcmp(name, "filter")) { |
Elijah Newren | c3b9bc9 | 2018-09-05 10:03:07 -0700 | [diff] [blame] | 182 | options.filter = xstrdup(value); |
Jeff Hostetler | acb0c57 | 2017-12-08 15:58:44 +0000 | [diff] [blame] | 183 | return 0; |
Nguyễn Thái Ngọc Duy | 1609488 | 2013-12-05 20:02:50 +0700 | [diff] [blame] | 184 | } else { |
Shawn O. Pearce | ef08ef9 | 2009-10-30 17:47:29 -0700 | [diff] [blame] | 185 | return 1 /* unsupported */; |
| 186 | } |
| 187 | } |
| 188 | |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 189 | struct discovery { |
Brandon Williams | f08a5d4 | 2018-03-15 10:31:36 -0700 | [diff] [blame] | 190 | char *service; |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 191 | char *buf_alloc; |
| 192 | char *buf; |
| 193 | size_t len; |
Jeff King | 2a45520 | 2013-02-20 15:07:19 -0500 | [diff] [blame] | 194 | struct ref *refs; |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 195 | struct oid_array shallow; |
Brandon Williams | 49e85e9 | 2018-03-15 10:31:37 -0700 | [diff] [blame] | 196 | enum protocol_version version; |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 197 | unsigned proto_git : 1; |
| 198 | }; |
| 199 | static struct discovery *last_discovery; |
| 200 | |
Jeff King | b8054bb | 2013-02-20 15:07:11 -0500 | [diff] [blame] | 201 | static struct ref *parse_git_refs(struct discovery *heads, int for_push) |
| 202 | { |
| 203 | struct ref *list = NULL; |
Brandon Williams | ad6ac12 | 2018-03-14 11:31:45 -0700 | [diff] [blame] | 204 | struct packet_reader reader; |
| 205 | |
| 206 | packet_reader_init(&reader, -1, heads->buf, heads->len, |
| 207 | PACKET_READ_CHOMP_NEWLINE | |
Masaya Suzuki | 2d103c3 | 2018-12-29 13:19:15 -0800 | [diff] [blame] | 208 | PACKET_READ_GENTLE_ON_EOF | |
| 209 | PACKET_READ_DIE_ON_ERR_PACKET); |
Brandon Williams | ad6ac12 | 2018-03-14 11:31:45 -0700 | [diff] [blame] | 210 | |
Brandon Williams | 49e85e9 | 2018-03-15 10:31:37 -0700 | [diff] [blame] | 211 | heads->version = discover_version(&reader); |
| 212 | switch (heads->version) { |
Brandon Williams | 8f6982b | 2018-03-14 11:31:47 -0700 | [diff] [blame] | 213 | case protocol_v2: |
Brandon Williams | 0f1dc53 | 2018-03-15 10:31:41 -0700 | [diff] [blame] | 214 | /* |
| 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 Williams | 8f6982b | 2018-03-14 11:31:47 -0700 | [diff] [blame] | 220 | break; |
Brandon Williams | ad6ac12 | 2018-03-14 11:31:45 -0700 | [diff] [blame] | 221 | 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 King | b8054bb | 2013-02-20 15:07:11 -0500 | [diff] [blame] | 230 | return list; |
| 231 | } |
| 232 | |
| 233 | static 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') { |
brian m. carlson | 9c9492e | 2019-02-19 00:05:16 +0000 | [diff] [blame] | 253 | if (mid - start != the_hash_algo->hexsz) |
Johannes Schindelin | ed8b413 | 2019-03-05 15:20:40 -0800 | [diff] [blame] | 254 | die(_("%sinfo/refs not valid: is this a git repository?"), |
Johannes Schindelin | c1284b2 | 2019-03-04 07:33:46 -0800 | [diff] [blame] | 255 | transport_anonymize_url(url.buf)); |
Jeff King | b8054bb | 2013-02-20 15:07:11 -0500 | [diff] [blame] | 256 | data[i] = 0; |
| 257 | ref_name = mid + 1; |
Jeff King | 6f687c2 | 2015-09-24 17:08:09 -0400 | [diff] [blame] | 258 | ref = alloc_ref(ref_name); |
brian m. carlson | f4e54d0 | 2015-11-10 02:22:20 +0000 | [diff] [blame] | 259 | get_oid_hex(start, &ref->old_oid); |
Jeff King | b8054bb | 2013-02-20 15:07:11 -0500 | [diff] [blame] | 260 | 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 King | b227bbc | 2013-09-28 04:35:25 -0400 | [diff] [blame] | 271 | if (!http_fetch_ref(url.buf, ref) && |
Jeff King | b8054bb | 2013-02-20 15:07:11 -0500 | [diff] [blame] | 272 | !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. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 282 | static void free_discovery(struct discovery *d) |
| 283 | { |
| 284 | if (d) { |
| 285 | if (d == last_discovery) |
| 286 | last_discovery = NULL; |
brian m. carlson | ee3051b | 2017-03-26 16:01:37 +0000 | [diff] [blame] | 287 | free(d->shallow.oid); |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 288 | free(d->buf_alloc); |
Jeff King | 2a45520 | 2013-02-20 15:07:19 -0500 | [diff] [blame] | 289 | free_refs(d->refs); |
Brandon Williams | f08a5d4 | 2018-03-15 10:31:36 -0700 | [diff] [blame] | 290 | free(d->service); |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 291 | free(d); |
| 292 | } |
| 293 | } |
| 294 | |
Jeff King | fc1b774 | 2014-05-22 05:30:29 -0400 | [diff] [blame] | 295 | static int show_http_message(struct strbuf *type, struct strbuf *charset, |
| 296 | struct strbuf *msg) |
Jeff King | 426e70d | 2013-04-05 18:17:23 -0400 | [diff] [blame] | 297 | { |
| 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 King | 426e70d | 2013-04-05 18:17:23 -0400 | [diff] [blame] | 303 | */ |
Jeff King | bf197fd | 2014-05-22 05:29:47 -0400 | [diff] [blame] | 304 | if (strcmp(type->buf, "text/plain")) |
Jeff King | 426e70d | 2013-04-05 18:17:23 -0400 | [diff] [blame] | 305 | return -1; |
Jeff King | fc1b774 | 2014-05-22 05:30:29 -0400 | [diff] [blame] | 306 | if (charset->len) |
| 307 | strbuf_reencode(msg, charset->buf, get_log_output_encoding()); |
Jeff King | 426e70d | 2013-04-05 18:17:23 -0400 | [diff] [blame] | 308 | |
| 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 Williams | 884e586 | 2018-03-15 10:31:39 -0700 | [diff] [blame] | 322 | static 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 King | 8ee3e12 | 2019-02-06 14:18:48 -0500 | [diff] [blame] | 335 | static 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 Hamano | aa1edf1 | 2019-04-16 19:28:05 +0900 | [diff] [blame] | 355 | die(_("invalid server response; expected service, got flush packet")); |
Jeff King | 8ee3e12 | 2019-02-06 14:18:48 -0500 | [diff] [blame] | 356 | |
| 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 King | cbdb8d1 | 2019-02-06 14:18:58 -0500 | [diff] [blame] | 378 | } else if (!strcmp(reader.line, "version 2")) { |
Jeff King | 8ee3e12 | 2019-02-06 14:18:48 -0500 | [diff] [blame] | 379 | /* |
| 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 Hamano | aa1edf1 | 2019-04-16 19:28:05 +0900 | [diff] [blame] | 386 | die(_("invalid server response; got '%s'"), reader.line); |
Jeff King | 8ee3e12 | 2019-02-06 14:18:48 -0500 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
David Aguilar | 24d36f1 | 2014-08-31 13:11:31 -0700 | [diff] [blame] | 390 | static struct discovery *discover_refs(const char *service, int for_push) |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 391 | { |
Shawn Pearce | 4656bf4 | 2013-01-31 13:02:07 -0800 | [diff] [blame] | 392 | struct strbuf type = STRBUF_INIT; |
Jeff King | fc1b774 | 2014-05-22 05:30:29 -0400 | [diff] [blame] | 393 | struct strbuf charset = STRBUF_INIT; |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 394 | struct strbuf buffer = STRBUF_INIT; |
Jeff King | c65d569 | 2013-09-28 04:35:10 -0400 | [diff] [blame] | 395 | struct strbuf refs_url = STRBUF_INIT; |
Jeff King | 050ef36 | 2013-09-28 04:35:35 -0400 | [diff] [blame] | 396 | struct strbuf effective_url = STRBUF_INIT; |
Brandon Williams | 884e586 | 2018-03-15 10:31:39 -0700 | [diff] [blame] | 397 | struct strbuf protocol_header = STRBUF_INIT; |
| 398 | struct string_list extra_headers = STRING_LIST_INIT_DUP; |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 399 | struct discovery *last = last_discovery; |
Jeff King | 243c329 | 2012-09-20 13:00:22 -0400 | [diff] [blame] | 400 | int http_ret, maybe_smart = 0; |
Jeff King | fcaa6e6 | 2016-12-06 13:24:38 -0500 | [diff] [blame] | 401 | struct http_get_options http_options; |
Brandon Williams | a4d78ce | 2018-03-15 10:31:42 -0700 | [diff] [blame] | 402 | enum protocol_version version = get_protocol_version_config(); |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 403 | |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 404 | if (last && !strcmp(service, last->service)) |
| 405 | return last; |
| 406 | free_discovery(last); |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 407 | |
Jeff King | b227bbc | 2013-09-28 04:35:25 -0400 | [diff] [blame] | 408 | strbuf_addf(&refs_url, "%sinfo/refs", url.buf); |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 409 | if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) && |
Jeff King | 02572c2 | 2012-09-20 17:30:58 -0400 | [diff] [blame] | 410 | git_env_bool("GIT_SMART_HTTP", 1)) { |
Jeff King | 243c329 | 2012-09-20 13:00:22 -0400 | [diff] [blame] | 411 | maybe_smart = 1; |
Jeff King | b227bbc | 2013-09-28 04:35:25 -0400 | [diff] [blame] | 412 | if (!strchr(url.buf, '?')) |
Jeff King | c65d569 | 2013-09-28 04:35:10 -0400 | [diff] [blame] | 413 | strbuf_addch(&refs_url, '?'); |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 414 | else |
Jeff King | c65d569 | 2013-09-28 04:35:10 -0400 | [diff] [blame] | 415 | strbuf_addch(&refs_url, '&'); |
| 416 | strbuf_addf(&refs_url, "service=%s", service); |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 417 | } |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 418 | |
Brandon Williams | a4d78ce | 2018-03-15 10:31:42 -0700 | [diff] [blame] | 419 | /* |
| 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 Williams | 884e586 | 2018-03-15 10:31:39 -0700 | [diff] [blame] | 427 | /* Add the extra Git-Protocol header */ |
Brandon Williams | a4d78ce | 2018-03-15 10:31:42 -0700 | [diff] [blame] | 428 | if (get_protocol_http_header(version, &protocol_header)) |
Brandon Williams | 884e586 | 2018-03-15 10:31:39 -0700 | [diff] [blame] | 429 | string_list_append(&extra_headers, protocol_header.buf); |
| 430 | |
Jeff King | fcaa6e6 | 2016-12-06 13:24:38 -0500 | [diff] [blame] | 431 | 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 Williams | 884e586 | 2018-03-15 10:31:39 -0700 | [diff] [blame] | 436 | http_options.extra_headers = &extra_headers; |
Jeff King | 50d3413 | 2016-12-06 13:24:41 -0500 | [diff] [blame] | 437 | http_options.initial_request = 1; |
Jeff King | fcaa6e6 | 2016-12-06 13:24:38 -0500 | [diff] [blame] | 438 | http_options.no_cache = 1; |
Jeff King | 1bbcc22 | 2013-09-28 04:31:23 -0400 | [diff] [blame] | 439 | |
Jeff King | fcaa6e6 | 2016-12-06 13:24:38 -0500 | [diff] [blame] | 440 | http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options); |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 441 | switch (http_ret) { |
| 442 | case HTTP_OK: |
| 443 | break; |
| 444 | case HTTP_MISSING_TARGET: |
Jeff King | fc1b774 | 2014-05-22 05:30:29 -0400 | [diff] [blame] | 445 | show_http_message(&type, &charset, &buffer); |
Johannes Schindelin | ed8b413 | 2019-03-05 15:20:40 -0800 | [diff] [blame] | 446 | die(_("repository '%s' not found"), |
Johannes Schindelin | c1284b2 | 2019-03-04 07:33:46 -0800 | [diff] [blame] | 447 | transport_anonymize_url(url.buf)); |
Scott Chacon | 42653c0 | 2010-04-01 15:14:35 -0700 | [diff] [blame] | 448 | case HTTP_NOAUTH: |
Jeff King | fc1b774 | 2014-05-22 05:30:29 -0400 | [diff] [blame] | 449 | show_http_message(&type, &charset, &buffer); |
Johannes Schindelin | ed8b413 | 2019-03-05 15:20:40 -0800 | [diff] [blame] | 450 | die(_("Authentication failed for '%s'"), |
Johannes Schindelin | c1284b2 | 2019-03-04 07:33:46 -0800 | [diff] [blame] | 451 | transport_anonymize_url(url.buf)); |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 452 | default: |
Jeff King | fc1b774 | 2014-05-22 05:30:29 -0400 | [diff] [blame] | 453 | show_http_message(&type, &charset, &buffer); |
Johannes Schindelin | ed8b413 | 2019-03-05 15:20:40 -0800 | [diff] [blame] | 454 | die(_("unable to access '%s': %s"), |
Johannes Schindelin | c1284b2 | 2019-03-04 07:33:46 -0800 | [diff] [blame] | 455 | transport_anonymize_url(url.buf), curl_errorstr); |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 456 | } |
| 457 | |
Johannes Schindelin | c1284b2 | 2019-03-04 07:33:46 -0800 | [diff] [blame] | 458 | 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 King | 50d3413 | 2016-12-06 13:24:41 -0500 | [diff] [blame] | 463 | |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 464 | last= xcalloc(1, sizeof(*last_discovery)); |
Brandon Williams | f08a5d4 | 2018-03-15 10:31:36 -0700 | [diff] [blame] | 465 | last->service = xstrdup(service); |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 466 | last->buf_alloc = strbuf_detach(&buffer, &last->len); |
| 467 | last->buf = last->buf_alloc; |
| 468 | |
Jeff King | 8ee3e12 | 2019-02-06 14:18:48 -0500 | [diff] [blame] | 469 | if (maybe_smart) |
| 470 | check_smart_http(last, service, &type); |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 471 | |
Jeff King | 2a45520 | 2013-02-20 15:07:19 -0500 | [diff] [blame] | 472 | if (last->proto_git) |
| 473 | last->refs = parse_git_refs(last, for_push); |
| 474 | else |
| 475 | last->refs = parse_info_refs(last); |
| 476 | |
Jeff King | c65d569 | 2013-09-28 04:35:10 -0400 | [diff] [blame] | 477 | strbuf_release(&refs_url); |
Shawn Pearce | 4656bf4 | 2013-01-31 13:02:07 -0800 | [diff] [blame] | 478 | strbuf_release(&type); |
Jeff King | fc1b774 | 2014-05-22 05:30:29 -0400 | [diff] [blame] | 479 | strbuf_release(&charset); |
Jeff King | 050ef36 | 2013-09-28 04:35:35 -0400 | [diff] [blame] | 480 | strbuf_release(&effective_url); |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 481 | strbuf_release(&buffer); |
Brandon Williams | 884e586 | 2018-03-15 10:31:39 -0700 | [diff] [blame] | 482 | strbuf_release(&protocol_header); |
| 483 | string_list_clear(&extra_headers, 0); |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 484 | last_discovery = last; |
| 485 | return last; |
| 486 | } |
| 487 | |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 488 | static struct ref *get_refs(int for_push) |
| 489 | { |
| 490 | struct discovery *heads; |
| 491 | |
| 492 | if (for_push) |
Jeff King | 2a45520 | 2013-02-20 15:07:19 -0500 | [diff] [blame] | 493 | heads = discover_refs("git-receive-pack", for_push); |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 494 | else |
Jeff King | 2a45520 | 2013-02-20 15:07:19 -0500 | [diff] [blame] | 495 | heads = discover_refs("git-upload-pack", for_push); |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 496 | |
Jeff King | 2a45520 | 2013-02-20 15:07:19 -0500 | [diff] [blame] | 497 | return heads->refs; |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 500 | static 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. carlson | f4e54d0 | 2015-11-10 02:22:20 +0000 | [diff] [blame] | 507 | printf("%s %s\n", oid_to_hex(&posn->old_oid), posn->name); |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 508 | } |
| 509 | printf("\n"); |
| 510 | fflush(stdout); |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 513 | struct rpc_state { |
| 514 | const char *service_name; |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 515 | char *service_url; |
| 516 | char *hdr_content_type; |
| 517 | char *hdr_accept; |
Brandon Williams | 884e586 | 2018-03-15 10:31:39 -0700 | [diff] [blame] | 518 | char *protocol_header; |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 519 | char *buf; |
| 520 | size_t alloc; |
| 521 | size_t len; |
| 522 | size_t pos; |
| 523 | int in; |
| 524 | int out; |
David Turner | 296b847 | 2016-11-18 15:30:49 -0500 | [diff] [blame] | 525 | int any_written; |
Shawn O. Pearce | b853860 | 2009-10-30 17:47:43 -0700 | [diff] [blame] | 526 | unsigned gzip_request : 1; |
Martin Storsjö | 6c81a99 | 2009-12-01 12:33:39 +0200 | [diff] [blame] | 527 | unsigned initial_buffer : 1; |
Jonathan Tan | a97d007 | 2019-02-21 12:24:41 -0800 | [diff] [blame] | 528 | |
| 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. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 543 | }; |
| 544 | |
Jonathan Tan | 78ad917 | 2019-02-21 12:24:40 -0800 | [diff] [blame] | 545 | /* |
| 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 Tan | a97d007 | 2019-02-21 12:24:41 -0800 | [diff] [blame] | 550 | * 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 Tan | 78ad917 | 2019-02-21 12:24:40 -0800 | [diff] [blame] | 554 | */ |
Jonathan Tan | a97d007 | 2019-02-21 12:24:41 -0800 | [diff] [blame] | 555 | static 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 Tan | 78ad917 | 2019-02-21 12:24:40 -0800 | [diff] [blame] | 569 | |
| 570 | if (left < LARGE_PACKET_MAX) |
| 571 | return 0; |
| 572 | |
Jonathan Tan | a97d007 | 2019-02-21 12:24:41 -0800 | [diff] [blame] | 573 | *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 Hamano | aa1edf1 | 2019-04-16 19:28:05 +0900 | [diff] [blame] | 584 | die(_("shouldn't have EOF when not gentle on EOF")); |
Jonathan Tan | a97d007 | 2019-02-21 12:24:41 -0800 | [diff] [blame] | 585 | 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 Tan | 78ad917 | 2019-02-21 12:24:40 -0800 | [diff] [blame] | 598 | return 1; |
| 599 | } |
| 600 | |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 601 | static 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 Tan | a97d007 | 2019-02-21 12:24:41 -0800 | [diff] [blame] | 607 | enum packet_read_status status; |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 608 | |
| 609 | if (!avail) { |
Martin Storsjö | 6c81a99 | 2009-12-01 12:33:39 +0200 | [diff] [blame] | 610 | rpc->initial_buffer = 0; |
Jonathan Tan | 78ad917 | 2019-02-21 12:24:40 -0800 | [diff] [blame] | 611 | rpc->len = 0; |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 612 | rpc->pos = 0; |
Jonathan Tan | a97d007 | 2019-02-21 12:24:41 -0800 | [diff] [blame] | 613 | 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. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 641 | } |
| 642 | |
Tay Ray Chuan | 4831060 | 2009-11-24 10:31:30 +0800 | [diff] [blame] | 643 | if (max < avail) |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 644 | avail = max; |
| 645 | memcpy(ptr, rpc->buf + rpc->pos, avail); |
| 646 | rpc->pos += avail; |
| 647 | return avail; |
| 648 | } |
| 649 | |
Martin Storsjö | 6c81a99 | 2009-12-01 12:33:39 +0200 | [diff] [blame] | 650 | #ifndef NO_CURL_IOCTL |
Junio C Hamano | 5092d3e | 2010-01-11 22:30:36 -0800 | [diff] [blame] | 651 | static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp) |
Martin Storsjö | 6c81a99 | 2009-12-01 12:33:39 +0200 | [diff] [blame] | 652 | { |
| 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 Schindelin | ed8b413 | 2019-03-05 15:20:40 -0800 | [diff] [blame] | 664 | error(_("unable to rewind rpc post data - try increasing http.postBuffer")); |
Martin Storsjö | 6c81a99 | 2009-12-01 12:33:39 +0200 | [diff] [blame] | 665 | return CURLIOE_FAILRESTART; |
| 666 | |
| 667 | default: |
| 668 | return CURLIOE_UNKNOWNCMD; |
| 669 | } |
| 670 | } |
| 671 | #endif |
| 672 | |
Masaya Suzuki | cf2fb92 | 2019-01-10 11:33:48 -0800 | [diff] [blame] | 673 | struct rpc_in_data { |
| 674 | struct rpc_state *rpc; |
Masaya Suzuki | b79bdd8 | 2019-01-10 11:33:49 -0800 | [diff] [blame] | 675 | struct active_request_slot *slot; |
Masaya Suzuki | cf2fb92 | 2019-01-10 11:33:48 -0800 | [diff] [blame] | 676 | }; |
| 677 | |
| 678 | /* |
| 679 | * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed |
| 680 | * from ptr. |
| 681 | */ |
Dan McGee | a04ff3e | 2011-05-03 23:47:27 +0800 | [diff] [blame] | 682 | static size_t rpc_in(char *ptr, size_t eltsize, |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 683 | size_t nmemb, void *buffer_) |
| 684 | { |
| 685 | size_t size = eltsize * nmemb; |
Masaya Suzuki | cf2fb92 | 2019-01-10 11:33:48 -0800 | [diff] [blame] | 686 | struct rpc_in_data *data = buffer_; |
Masaya Suzuki | b79bdd8 | 2019-01-10 11:33:49 -0800 | [diff] [blame] | 687 | 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 Turner | 296b847 | 2016-11-18 15:30:49 -0500 | [diff] [blame] | 694 | if (size) |
Masaya Suzuki | cf2fb92 | 2019-01-10 11:33:48 -0800 | [diff] [blame] | 695 | data->rpc->any_written = 1; |
| 696 | write_or_die(data->rpc->in, ptr, size); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 697 | return size; |
| 698 | } |
| 699 | |
Jeff King | 3a347ed | 2013-10-31 02:36:26 -0400 | [diff] [blame] | 700 | static int run_slot(struct active_request_slot *slot, |
| 701 | struct slot_results *results) |
Shawn O. Pearce | 206b099 | 2011-02-15 08:57:24 -0800 | [diff] [blame] | 702 | { |
Jeff King | b81401c | 2012-08-27 09:27:15 -0400 | [diff] [blame] | 703 | int err; |
Jeff King | 3a347ed | 2013-10-31 02:36:26 -0400 | [diff] [blame] | 704 | struct slot_results results_buf; |
Shawn O. Pearce | 206b099 | 2011-02-15 08:57:24 -0800 | [diff] [blame] | 705 | |
Jeff King | 3a347ed | 2013-10-31 02:36:26 -0400 | [diff] [blame] | 706 | if (!results) |
| 707 | results = &results_buf; |
| 708 | |
Jeff King | beed336 | 2014-02-18 05:34:20 -0500 | [diff] [blame] | 709 | err = run_one_slot(slot, results); |
Shawn O. Pearce | 206b099 | 2011-02-15 08:57:24 -0800 | [diff] [blame] | 710 | |
Jeff King | b81401c | 2012-08-27 09:27:15 -0400 | [diff] [blame] | 711 | if (err != HTTP_OK && err != HTTP_REAUTH) { |
Shawn Pearce | 0054045 | 2016-02-13 17:39:34 -0800 | [diff] [blame] | 712 | 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 Schindelin | ed8b413 | 2019-03-05 15:20:40 -0800 | [diff] [blame] | 724 | error(_("RPC failed; %s"), msg.buf); |
Shawn Pearce | 0054045 | 2016-02-13 17:39:34 -0800 | [diff] [blame] | 725 | strbuf_release(&msg); |
Shawn O. Pearce | 206b099 | 2011-02-15 08:57:24 -0800 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | return err; |
| 729 | } |
| 730 | |
Jeff King | 3a347ed | 2013-10-31 02:36:26 -0400 | [diff] [blame] | 731 | static int probe_rpc(struct rpc_state *rpc, struct slot_results *results) |
Shawn O. Pearce | 206b099 | 2011-02-15 08:57:24 -0800 | [diff] [blame] | 732 | { |
| 733 | struct active_request_slot *slot; |
Johannes Schindelin | 8cb01e2 | 2016-04-27 14:20:37 +0200 | [diff] [blame] | 734 | struct curl_slist *headers = http_copy_default_headers(); |
Shawn O. Pearce | 206b099 | 2011-02-15 08:57:24 -0800 | [diff] [blame] | 735 | 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. Pearce | aa90b96 | 2012-09-19 16:12:02 -0700 | [diff] [blame] | 746 | curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL); |
Shawn O. Pearce | 206b099 | 2011-02-15 08:57:24 -0800 | [diff] [blame] | 747 | 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 King | 3a347ed | 2013-10-31 02:36:26 -0400 | [diff] [blame] | 753 | err = run_slot(slot, results); |
Shawn O. Pearce | 206b099 | 2011-02-15 08:57:24 -0800 | [diff] [blame] | 754 | |
| 755 | curl_slist_free_all(headers); |
| 756 | strbuf_release(&buf); |
| 757 | return err; |
| 758 | } |
| 759 | |
Nguyễn Thái Ngọc Duy | 3b33576 | 2018-12-09 11:25:21 +0100 | [diff] [blame] | 760 | static curl_off_t xcurl_off_t(size_t len) |
| 761 | { |
Torsten Bögershausen | cb8010b | 2018-11-09 18:41:10 +0100 | [diff] [blame] | 762 | uintmax_t size = len; |
| 763 | if (size > maximum_signed_value_of_type(curl_off_t)) |
Johannes Schindelin | ed8b413 | 2019-03-05 15:20:40 -0800 | [diff] [blame] | 764 | die(_("cannot handle pushes this big")); |
Torsten Bögershausen | cb8010b | 2018-11-09 18:41:10 +0100 | [diff] [blame] | 765 | return (curl_off_t)size; |
David Turner | 37ee680 | 2017-04-11 14:13:57 -0400 | [diff] [blame] | 766 | } |
| 767 | |
Jonathan Tan | a97d007 | 2019-02-21 12:24:41 -0800 | [diff] [blame] | 768 | /* |
| 769 | * If flush_received is true, do not attempt to read any more; just use what's |
| 770 | * in rpc->buf. |
| 771 | */ |
| 772 | static int post_rpc(struct rpc_state *rpc, int flush_received) |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 773 | { |
| 774 | struct active_request_slot *slot; |
Johannes Schindelin | 8cb01e2 | 2016-04-27 14:20:37 +0200 | [diff] [blame] | 775 | struct curl_slist *headers = http_copy_default_headers(); |
Shawn O. Pearce | b853860 | 2009-10-30 17:47:43 -0700 | [diff] [blame] | 776 | int use_gzip = rpc->gzip_request; |
| 777 | char *gzip_body = NULL; |
Ramsay Jones | 3771154 | 2012-11-21 19:08:51 +0000 | [diff] [blame] | 778 | size_t gzip_size = 0; |
Shawn O. Pearce | 206b099 | 2011-02-15 08:57:24 -0800 | [diff] [blame] | 779 | int err, large_request = 0; |
Brian M. Carlson | c80d96c | 2013-10-31 02:36:51 -0400 | [diff] [blame] | 780 | int needs_100_continue = 0; |
Masaya Suzuki | cf2fb92 | 2019-01-10 11:33:48 -0800 | [diff] [blame] | 781 | struct rpc_in_data rpc_in_data; |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 782 | |
| 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 Tan | a97d007 | 2019-02-21 12:24:41 -0800 | [diff] [blame] | 787 | if (!flush_received) { |
| 788 | while (1) { |
| 789 | size_t n; |
| 790 | enum packet_read_status status; |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 791 | |
Jonathan Tan | a97d007 | 2019-02-21 12:24:41 -0800 | [diff] [blame] | 792 | 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. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 799 | } |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 800 | } |
| 801 | |
Shawn O. Pearce | 206b099 | 2011-02-15 08:57:24 -0800 | [diff] [blame] | 802 | if (large_request) { |
Brian M. Carlson | c80d96c | 2013-10-31 02:36:51 -0400 | [diff] [blame] | 803 | struct slot_results results; |
| 804 | |
Jeff King | b81401c | 2012-08-27 09:27:15 -0400 | [diff] [blame] | 805 | do { |
Brian M. Carlson | c80d96c | 2013-10-31 02:36:51 -0400 | [diff] [blame] | 806 | err = probe_rpc(rpc, &results); |
Jeff King | 2501aff | 2013-09-28 04:31:45 -0400 | [diff] [blame] | 807 | if (err == HTTP_REAUTH) |
| 808 | credential_fill(&http_auth); |
Jeff King | b81401c | 2012-08-27 09:27:15 -0400 | [diff] [blame] | 809 | } while (err == HTTP_REAUTH); |
| 810 | if (err != HTTP_OK) |
| 811 | return -1; |
Brian M. Carlson | c80d96c | 2013-10-31 02:36:51 -0400 | [diff] [blame] | 812 | |
| 813 | if (results.auth_avail & CURLAUTH_GSSNEGOTIATE) |
| 814 | needs_100_continue = 1; |
Shawn O. Pearce | 206b099 | 2011-02-15 08:57:24 -0800 | [diff] [blame] | 815 | } |
| 816 | |
Jeff King | abf8df8 | 2012-10-12 03:35:33 -0400 | [diff] [blame] | 817 | headers = curl_slist_append(headers, rpc->hdr_content_type); |
| 818 | headers = curl_slist_append(headers, rpc->hdr_accept); |
Brian M. Carlson | c80d96c | 2013-10-31 02:36:51 -0400 | [diff] [blame] | 819 | headers = curl_slist_append(headers, needs_100_continue ? |
| 820 | "Expect: 100-continue" : "Expect:"); |
Jeff King | abf8df8 | 2012-10-12 03:35:33 -0400 | [diff] [blame] | 821 | |
Brandon Williams | 884e586 | 2018-03-15 10:31:39 -0700 | [diff] [blame] | 822 | /* Add the extra Git-Protocol header */ |
| 823 | if (rpc->protocol_header) |
| 824 | headers = curl_slist_append(headers, rpc->protocol_header); |
| 825 | |
Jeff King | abf8df8 | 2012-10-12 03:35:33 -0400 | [diff] [blame] | 826 | retry: |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 827 | slot = get_active_slot(); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 828 | |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 829 | curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0); |
Martin Storsjö | d21f979 | 2009-11-23 11:03:28 +0800 | [diff] [blame] | 830 | curl_easy_setopt(slot->curl, CURLOPT_POST, 1); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 831 | curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url); |
Brandon Williams | 1a53e69 | 2018-05-22 11:42:03 -0700 | [diff] [blame] | 832 | curl_easy_setopt(slot->curl, CURLOPT_ENCODING, ""); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 833 | |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 834 | 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. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 838 | headers = curl_slist_append(headers, "Transfer-Encoding: chunked"); |
Martin Storsjö | 6c81a99 | 2009-12-01 12:33:39 +0200 | [diff] [blame] | 839 | rpc->initial_buffer = 1; |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 840 | curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out); |
| 841 | curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc); |
Martin Storsjö | 6c81a99 | 2009-12-01 12:33:39 +0200 | [diff] [blame] | 842 | #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. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 846 | if (options.verbosity > 1) { |
| 847 | fprintf(stderr, "POST %s (chunked)\n", rpc->service_name); |
| 848 | fflush(stderr); |
| 849 | } |
| 850 | |
Jeff King | 2e736fd | 2012-10-31 07:29:16 -0400 | [diff] [blame] | 851 | } 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 Turner | 37ee680 | 2017-04-11 14:13:57 -0400 | [diff] [blame] | 858 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size)); |
Jeff King | 2e736fd | 2012-10-31 07:29:16 -0400 | [diff] [blame] | 859 | |
Shawn O. Pearce | b853860 | 2009-10-30 17:47:43 -0700 | [diff] [blame] | 860 | } else if (use_gzip && 1024 < rpc->len) { |
| 861 | /* The client backend isn't giving us compressed data so |
Johannes Schindelin | a813241 | 2018-08-08 04:50:00 -0700 | [diff] [blame] | 862 | * we can try to deflate it ourselves, this may save on |
Shawn O. Pearce | b853860 | 2009-10-30 17:47:43 -0700 | [diff] [blame] | 863 | * the transfer time. |
| 864 | */ |
Junio C Hamano | ef49a7a | 2011-06-10 11:52:15 -0700 | [diff] [blame] | 865 | git_zstream stream; |
Shawn O. Pearce | b853860 | 2009-10-30 17:47:43 -0700 | [diff] [blame] | 866 | int ret; |
| 867 | |
Junio C Hamano | 55bb5c9 | 2011-06-10 10:55:10 -0700 | [diff] [blame] | 868 | git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION); |
Jeff King | df126e1 | 2012-10-31 07:20:15 -0400 | [diff] [blame] | 869 | gzip_size = git_deflate_bound(&stream, rpc->len); |
| 870 | gzip_body = xmalloc(gzip_size); |
Shawn O. Pearce | b853860 | 2009-10-30 17:47:43 -0700 | [diff] [blame] | 871 | |
| 872 | stream.next_in = (unsigned char *)rpc->buf; |
| 873 | stream.avail_in = rpc->len; |
| 874 | stream.next_out = (unsigned char *)gzip_body; |
Jeff King | df126e1 | 2012-10-31 07:20:15 -0400 | [diff] [blame] | 875 | stream.avail_out = gzip_size; |
Shawn O. Pearce | b853860 | 2009-10-30 17:47:43 -0700 | [diff] [blame] | 876 | |
Junio C Hamano | 55bb5c9 | 2011-06-10 10:55:10 -0700 | [diff] [blame] | 877 | ret = git_deflate(&stream, Z_FINISH); |
Shawn O. Pearce | b853860 | 2009-10-30 17:47:43 -0700 | [diff] [blame] | 878 | if (ret != Z_STREAM_END) |
Johannes Schindelin | ed8b413 | 2019-03-05 15:20:40 -0800 | [diff] [blame] | 879 | die(_("cannot deflate request; zlib deflate error %d"), ret); |
Shawn O. Pearce | b853860 | 2009-10-30 17:47:43 -0700 | [diff] [blame] | 880 | |
Junio C Hamano | 55bb5c9 | 2011-06-10 10:55:10 -0700 | [diff] [blame] | 881 | ret = git_deflate_end_gently(&stream); |
Shawn O. Pearce | b853860 | 2009-10-30 17:47:43 -0700 | [diff] [blame] | 882 | if (ret != Z_OK) |
Johannes Schindelin | ed8b413 | 2019-03-05 15:20:40 -0800 | [diff] [blame] | 883 | die(_("cannot deflate request; zlib end error %d"), ret); |
Shawn O. Pearce | b853860 | 2009-10-30 17:47:43 -0700 | [diff] [blame] | 884 | |
Jeff King | df126e1 | 2012-10-31 07:20:15 -0400 | [diff] [blame] | 885 | gzip_size = stream.total_out; |
Shawn O. Pearce | b853860 | 2009-10-30 17:47:43 -0700 | [diff] [blame] | 886 | |
| 887 | headers = curl_slist_append(headers, "Content-Encoding: gzip"); |
| 888 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body); |
David Turner | 37ee680 | 2017-04-11 14:13:57 -0400 | [diff] [blame] | 889 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size)); |
Shawn O. Pearce | b853860 | 2009-10-30 17:47:43 -0700 | [diff] [blame] | 890 | |
| 891 | if (options.verbosity > 1) { |
| 892 | fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n", |
| 893 | rpc->service_name, |
Jeff King | df126e1 | 2012-10-31 07:20:15 -0400 | [diff] [blame] | 894 | (unsigned long)rpc->len, (unsigned long)gzip_size); |
Shawn O. Pearce | b853860 | 2009-10-30 17:47:43 -0700 | [diff] [blame] | 895 | fflush(stderr); |
| 896 | } |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 897 | } 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 Turner | 37ee680 | 2017-04-11 14:13:57 -0400 | [diff] [blame] | 902 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len)); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 903 | 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 Suzuki | cf2fb92 | 2019-01-10 11:33:48 -0800 | [diff] [blame] | 912 | rpc_in_data.rpc = rpc; |
Masaya Suzuki | b79bdd8 | 2019-01-10 11:33:49 -0800 | [diff] [blame] | 913 | rpc_in_data.slot = slot; |
Masaya Suzuki | cf2fb92 | 2019-01-10 11:33:48 -0800 | [diff] [blame] | 914 | curl_easy_setopt(slot->curl, CURLOPT_FILE, &rpc_in_data); |
Masaya Suzuki | b79bdd8 | 2019-01-10 11:33:49 -0800 | [diff] [blame] | 915 | curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 916 | |
David Turner | 296b847 | 2016-11-18 15:30:49 -0500 | [diff] [blame] | 917 | |
| 918 | rpc->any_written = 0; |
Jeff King | 3a347ed | 2013-10-31 02:36:26 -0400 | [diff] [blame] | 919 | err = run_slot(slot, NULL); |
Jeff King | 2501aff | 2013-09-28 04:31:45 -0400 | [diff] [blame] | 920 | if (err == HTTP_REAUTH && !large_request) { |
| 921 | credential_fill(&http_auth); |
Jeff King | abf8df8 | 2012-10-12 03:35:33 -0400 | [diff] [blame] | 922 | goto retry; |
Jeff King | 2501aff | 2013-09-28 04:31:45 -0400 | [diff] [blame] | 923 | } |
Jeff King | b81401c | 2012-08-27 09:27:15 -0400 | [diff] [blame] | 924 | if (err != HTTP_OK) |
| 925 | err = -1; |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 926 | |
David Turner | 296b847 | 2016-11-18 15:30:49 -0500 | [diff] [blame] | 927 | if (!rpc->any_written) |
| 928 | err = -1; |
| 929 | |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 930 | curl_slist_free_all(headers); |
Shawn O. Pearce | b853860 | 2009-10-30 17:47:43 -0700 | [diff] [blame] | 931 | free(gzip_body); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 932 | return err; |
| 933 | } |
| 934 | |
Jonathan Tan | 7d50d34 | 2019-02-14 11:06:35 -0800 | [diff] [blame] | 935 | static int rpc_service(struct rpc_state *rpc, struct discovery *heads, |
Jonathan Tan | b359030 | 2019-02-14 11:06:37 -0800 | [diff] [blame] | 936 | const char **client_argv, const struct strbuf *preamble, |
| 937 | struct strbuf *rpc_result) |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 938 | { |
| 939 | const char *svc = rpc->service_name; |
| 940 | struct strbuf buf = STRBUF_INIT; |
René Scharfe | d318027 | 2014-08-19 21:09:35 +0200 | [diff] [blame] | 941 | struct child_process client = CHILD_PROCESS_INIT; |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 942 | int err = 0; |
| 943 | |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 944 | client.in = -1; |
| 945 | client.out = -1; |
| 946 | client.git_cmd = 1; |
Jonathan Tan | 7d50d34 | 2019-02-14 11:06:35 -0800 | [diff] [blame] | 947 | client.argv = client_argv; |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 948 | if (start_command(&client)) |
| 949 | exit(1); |
Jonathan Tan | 5d91669 | 2019-02-14 11:06:36 -0800 | [diff] [blame] | 950 | write_or_die(client.in, preamble->buf, preamble->len); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 951 | 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. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 958 | |
Jeff King | b227bbc | 2013-09-28 04:35:25 -0400 | [diff] [blame] | 959 | strbuf_addf(&buf, "%s%s", url.buf, svc); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 960 | 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. Pearce | 8efa5f6 | 2010-01-12 09:54:04 -0800 | [diff] [blame] | 965 | strbuf_addf(&buf, "Accept: application/x-%s-result", svc); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 966 | rpc->hdr_accept = strbuf_detach(&buf, NULL); |
| 967 | |
Brandon Williams | 884e586 | 2018-03-15 10:31:39 -0700 | [diff] [blame] | 968 | 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. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 973 | while (!err) { |
Jeff King | 4981fe7 | 2013-02-23 17:31:34 -0500 | [diff] [blame] | 974 | int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 975 | if (!n) |
| 976 | break; |
| 977 | rpc->pos = 0; |
| 978 | rpc->len = n; |
Jonathan Tan | a97d007 | 2019-02-21 12:24:41 -0800 | [diff] [blame] | 979 | err |= post_rpc(rpc, 0); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 980 | } |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 981 | |
| 982 | close(client.in); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 983 | client.in = -1; |
Shawn O. Pearce | 6cdf022 | 2011-10-04 16:20:19 -0700 | [diff] [blame] | 984 | if (!err) { |
Jonathan Tan | b359030 | 2019-02-14 11:06:37 -0800 | [diff] [blame] | 985 | strbuf_read(rpc_result, client.out, 0); |
Shawn O. Pearce | 6cdf022 | 2011-10-04 16:20:19 -0700 | [diff] [blame] | 986 | } else { |
| 987 | char buf[4096]; |
| 988 | for (;;) |
| 989 | if (xread(client.out, buf, sizeof(buf)) <= 0) |
| 990 | break; |
| 991 | } |
Shawn O. Pearce | b4ee10f | 2010-08-06 14:19:44 -0700 | [diff] [blame] | 992 | |
| 993 | close(client.out); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 994 | 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 Williams | 884e586 | 2018-03-15 10:31:39 -0700 | [diff] [blame] | 1000 | free(rpc->protocol_header); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 1001 | free(rpc->buf); |
| 1002 | strbuf_release(&buf); |
| 1003 | return err; |
| 1004 | } |
| 1005 | |
Shawn O. Pearce | 292ce46 | 2009-10-30 17:47:28 -0700 | [diff] [blame] | 1006 | static int fetch_dumb(int nr_heads, struct ref **to_fetch) |
| 1007 | { |
Tay Ray Chuan | 26e1e0b | 2010-03-02 18:49:31 +0800 | [diff] [blame] | 1008 | struct walker *walker; |
Jeff King | b32fa95 | 2016-02-22 17:44:25 -0500 | [diff] [blame] | 1009 | char **targets; |
Shawn O. Pearce | 292ce46 | 2009-10-30 17:47:28 -0700 | [diff] [blame] | 1010 | int ret, i; |
| 1011 | |
Jeff King | b32fa95 | 2016-02-22 17:44:25 -0500 | [diff] [blame] | 1012 | ALLOC_ARRAY(targets, nr_heads); |
Nguyễn Thái Ngọc Duy | 508ea88 | 2016-06-12 17:53:59 +0700 | [diff] [blame] | 1013 | if (options.depth || options.deepen_since) |
Johannes Schindelin | ed8b413 | 2019-03-05 15:20:40 -0800 | [diff] [blame] | 1014 | die(_("dumb http transport does not support shallow capabilities")); |
Shawn O. Pearce | 292ce46 | 2009-10-30 17:47:28 -0700 | [diff] [blame] | 1015 | for (i = 0; i < nr_heads; i++) |
brian m. carlson | f4e54d0 | 2015-11-10 02:22:20 +0000 | [diff] [blame] | 1016 | targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid)); |
Shawn O. Pearce | 292ce46 | 2009-10-30 17:47:28 -0700 | [diff] [blame] | 1017 | |
Jeff King | b227bbc | 2013-09-28 04:35:25 -0400 | [diff] [blame] | 1018 | walker = get_http_walker(url.buf); |
Shawn O. Pearce | ef08ef9 | 2009-10-30 17:47:29 -0700 | [diff] [blame] | 1019 | walker->get_verbosely = options.verbosity >= 3; |
Shawn O. Pearce | 292ce46 | 2009-10-30 17:47:28 -0700 | [diff] [blame] | 1020 | walker->get_recover = 0; |
| 1021 | ret = walker_fetch(walker, nr_heads, targets, NULL, NULL); |
Tay Ray Chuan | 26e1e0b | 2010-03-02 18:49:31 +0800 | [diff] [blame] | 1022 | walker_free(walker); |
Shawn O. Pearce | 292ce46 | 2009-10-30 17:47:28 -0700 | [diff] [blame] | 1023 | |
| 1024 | for (i = 0; i < nr_heads; i++) |
| 1025 | free(targets[i]); |
| 1026 | free(targets); |
| 1027 | |
Johannes Schindelin | ed8b413 | 2019-03-05 15:20:40 -0800 | [diff] [blame] | 1028 | return ret ? error(_("fetch failed.")) : 0; |
Shawn O. Pearce | 292ce46 | 2009-10-30 17:47:28 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 1031 | static int fetch_git(struct discovery *heads, |
| 1032 | int nr_heads, struct ref **to_fetch) |
| 1033 | { |
| 1034 | struct rpc_state rpc; |
Ivan Todoroski | 8150749 | 2012-04-02 17:14:44 +0200 | [diff] [blame] | 1035 | struct strbuf preamble = STRBUF_INIT; |
Nguyễn Thái Ngọc Duy | b5f62eb | 2016-06-12 17:53:43 +0700 | [diff] [blame] | 1036 | int i, err; |
| 1037 | struct argv_array args = ARGV_ARRAY_INIT; |
Jonathan Tan | b359030 | 2019-02-14 11:06:37 -0800 | [diff] [blame] | 1038 | struct strbuf rpc_result = STRBUF_INIT; |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 1039 | |
Nguyễn Thái Ngọc Duy | b5f62eb | 2016-06-12 17:53:43 +0700 | [diff] [blame] | 1040 | argv_array_pushl(&args, "fetch-pack", "--stateless-rpc", |
| 1041 | "--stdin", "--lock-pack", NULL); |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 1042 | if (options.followtags) |
Nguyễn Thái Ngọc Duy | b5f62eb | 2016-06-12 17:53:43 +0700 | [diff] [blame] | 1043 | argv_array_push(&args, "--include-tag"); |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 1044 | if (options.thin) |
Nguyễn Thái Ngọc Duy | b5f62eb | 2016-06-12 17:53:43 +0700 | [diff] [blame] | 1045 | argv_array_push(&args, "--thin"); |
| 1046 | if (options.verbosity >= 3) |
| 1047 | argv_array_pushl(&args, "-v", "-v", NULL); |
Nguyễn Thái Ngọc Duy | 9ba3804 | 2013-07-21 15:18:05 +0700 | [diff] [blame] | 1048 | if (options.check_self_contained_and_connected) |
Nguyễn Thái Ngọc Duy | b5f62eb | 2016-06-12 17:53:43 +0700 | [diff] [blame] | 1049 | argv_array_push(&args, "--check-self-contained-and-connected"); |
Nguyễn Thái Ngọc Duy | 1609488 | 2013-12-05 20:02:50 +0700 | [diff] [blame] | 1050 | if (options.cloning) |
Nguyễn Thái Ngọc Duy | b5f62eb | 2016-06-12 17:53:43 +0700 | [diff] [blame] | 1051 | argv_array_push(&args, "--cloning"); |
Nguyễn Thái Ngọc Duy | 1609488 | 2013-12-05 20:02:50 +0700 | [diff] [blame] | 1052 | if (options.update_shallow) |
Nguyễn Thái Ngọc Duy | b5f62eb | 2016-06-12 17:53:43 +0700 | [diff] [blame] | 1053 | argv_array_push(&args, "--update-shallow"); |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 1054 | if (!options.progress) |
Nguyễn Thái Ngọc Duy | b5f62eb | 2016-06-12 17:53:43 +0700 | [diff] [blame] | 1055 | 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 Duy | 508ea88 | 2016-06-12 17:53:59 +0700 | [diff] [blame] | 1058 | if (options.deepen_since) |
| 1059 | argv_array_pushf(&args, "--shallow-since=%s", options.deepen_since); |
Nguyễn Thái Ngọc Duy | a45a260 | 2016-06-12 17:54:04 +0700 | [diff] [blame] | 1060 | 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 Duy | cccf74e | 2016-06-12 17:54:09 +0700 | [diff] [blame] | 1063 | if (options.deepen_relative && options.depth) |
| 1064 | argv_array_push(&args, "--deepen-relative"); |
Jonathan Tan | 88e2f9e | 2017-12-05 16:58:49 +0000 | [diff] [blame] | 1065 | if (options.from_promisor) |
| 1066 | argv_array_push(&args, "--from-promisor"); |
| 1067 | if (options.no_dependents) |
| 1068 | argv_array_push(&args, "--no-dependents"); |
Jeff Hostetler | acb0c57 | 2017-12-08 15:58:44 +0000 | [diff] [blame] | 1069 | if (options.filter) |
| 1070 | argv_array_pushf(&args, "--filter=%s", options.filter); |
Nguyễn Thái Ngọc Duy | b5f62eb | 2016-06-12 17:53:43 +0700 | [diff] [blame] | 1071 | argv_array_push(&args, url.buf); |
Ivan Todoroski | 8150749 | 2012-04-02 17:14:44 +0200 | [diff] [blame] | 1072 | |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 1073 | for (i = 0; i < nr_heads; i++) { |
| 1074 | struct ref *ref = to_fetch[i]; |
Jeff King | 94ee8e2 | 2015-01-28 12:58:50 -0500 | [diff] [blame] | 1075 | if (!*ref->name) |
Johannes Schindelin | ed8b413 | 2019-03-05 15:20:40 -0800 | [diff] [blame] | 1076 | die(_("cannot fetch by sha1 over smart http")); |
Nguyễn Thái Ngọc Duy | 58f2ed0 | 2013-12-05 20:02:49 +0700 | [diff] [blame] | 1077 | packet_buf_write(&preamble, "%s %s\n", |
brian m. carlson | f4e54d0 | 2015-11-10 02:22:20 +0000 | [diff] [blame] | 1078 | oid_to_hex(&ref->old_oid), ref->name); |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 1079 | } |
Ivan Todoroski | 8150749 | 2012-04-02 17:14:44 +0200 | [diff] [blame] | 1080 | packet_buf_flush(&preamble); |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 1081 | |
| 1082 | memset(&rpc, 0, sizeof(rpc)); |
| 1083 | rpc.service_name = "git-upload-pack", |
Shawn O. Pearce | b853860 | 2009-10-30 17:47:43 -0700 | [diff] [blame] | 1084 | rpc.gzip_request = 1; |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 1085 | |
Jonathan Tan | b359030 | 2019-02-14 11:06:37 -0800 | [diff] [blame] | 1086 | 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 Todoroski | 8150749 | 2012-04-02 17:14:44 +0200 | [diff] [blame] | 1090 | strbuf_release(&preamble); |
Nguyễn Thái Ngọc Duy | b5f62eb | 2016-06-12 17:53:43 +0700 | [diff] [blame] | 1091 | argv_array_clear(&args); |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 1092 | return err; |
| 1093 | } |
| 1094 | |
| 1095 | static int fetch(int nr_heads, struct ref **to_fetch) |
| 1096 | { |
Jeff King | 2a45520 | 2013-02-20 15:07:19 -0500 | [diff] [blame] | 1097 | struct discovery *d = discover_refs("git-upload-pack", 0); |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 1098 | 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. Pearce | 292ce46 | 2009-10-30 17:47:28 -0700 | [diff] [blame] | 1104 | static 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 King | 95b567c | 2014-06-18 15:48:29 -0400 | [diff] [blame] | 1112 | const char *p; |
| 1113 | if (skip_prefix(buf->buf, "fetch ", &p)) { |
| 1114 | const char *name; |
Shawn O. Pearce | 292ce46 | 2009-10-30 17:47:28 -0700 | [diff] [blame] | 1115 | struct ref *ref; |
brian m. carlson | 8338c91 | 2015-11-10 02:22:22 +0000 | [diff] [blame] | 1116 | struct object_id old_oid; |
brian m. carlson | 9c9492e | 2019-02-19 00:05:16 +0000 | [diff] [blame] | 1117 | const char *q; |
Shawn O. Pearce | 292ce46 | 2009-10-30 17:47:28 -0700 | [diff] [blame] | 1118 | |
brian m. carlson | 9c9492e | 2019-02-19 00:05:16 +0000 | [diff] [blame] | 1119 | if (parse_oid_hex(p, &old_oid, &q)) |
Jiang Xin | 8a1569d | 2019-06-02 23:11:22 +0800 | [diff] [blame] | 1120 | die(_("protocol error: expected sha/ref, got '%s'"), p); |
brian m. carlson | 9c9492e | 2019-02-19 00:05:16 +0000 | [diff] [blame] | 1121 | if (*q == ' ') |
| 1122 | name = q + 1; |
| 1123 | else if (!*q) |
Shawn O. Pearce | 292ce46 | 2009-10-30 17:47:28 -0700 | [diff] [blame] | 1124 | name = ""; |
| 1125 | else |
Jiang Xin | 8a1569d | 2019-06-02 23:11:22 +0800 | [diff] [blame] | 1126 | die(_("protocol error: expected sha/ref, got '%s'"), p); |
Shawn O. Pearce | 292ce46 | 2009-10-30 17:47:28 -0700 | [diff] [blame] | 1127 | |
| 1128 | ref = alloc_ref(name); |
brian m. carlson | 8338c91 | 2015-11-10 02:22:22 +0000 | [diff] [blame] | 1129 | oidcpy(&ref->old_oid, &old_oid); |
Shawn O. Pearce | 292ce46 | 2009-10-30 17:47:28 -0700 | [diff] [blame] | 1130 | |
| 1131 | *list = ref; |
| 1132 | list = &ref->next; |
| 1133 | |
| 1134 | ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads); |
| 1135 | to_fetch[nr_heads++] = ref; |
| 1136 | } |
| 1137 | else |
Johannes Schindelin | ed8b413 | 2019-03-05 15:20:40 -0800 | [diff] [blame] | 1138 | die(_("http transport does not support %s"), buf->buf); |
Shawn O. Pearce | 292ce46 | 2009-10-30 17:47:28 -0700 | [diff] [blame] | 1139 | |
| 1140 | strbuf_reset(buf); |
Junio C Hamano | 8f309ae | 2016-01-13 15:31:17 -0800 | [diff] [blame] | 1141 | if (strbuf_getline_lf(buf, stdin) == EOF) |
Shawn O. Pearce | 292ce46 | 2009-10-30 17:47:28 -0700 | [diff] [blame] | 1142 | return; |
| 1143 | if (!*buf->buf) |
| 1144 | break; |
| 1145 | } while (1); |
| 1146 | |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 1147 | if (fetch(nr_heads, to_fetch)) |
Shawn O. Pearce | 292ce46 | 2009-10-30 17:47:28 -0700 | [diff] [blame] | 1148 | exit(128); /* error already reported */ |
| 1149 | free_refs(list_head); |
| 1150 | free(to_fetch); |
| 1151 | |
| 1152 | printf("\n"); |
| 1153 | fflush(stdout); |
| 1154 | strbuf_reset(buf); |
| 1155 | } |
| 1156 | |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 1157 | static int push_dav(int nr_spec, char **specs) |
| 1158 | { |
Jeff King | 850d2fe | 2016-02-22 17:44:21 -0500 | [diff] [blame] | 1159 | struct child_process child = CHILD_PROCESS_INIT; |
| 1160 | size_t i; |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 1161 | |
Jeff King | 850d2fe | 2016-02-22 17:44:21 -0500 | [diff] [blame] | 1162 | child.git_cmd = 1; |
| 1163 | argv_array_push(&child.args, "http-push"); |
| 1164 | argv_array_push(&child.args, "--helper-status"); |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 1165 | if (options.dry_run) |
Jeff King | 850d2fe | 2016-02-22 17:44:21 -0500 | [diff] [blame] | 1166 | argv_array_push(&child.args, "--dry-run"); |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 1167 | if (options.verbosity > 1) |
Jeff King | 850d2fe | 2016-02-22 17:44:21 -0500 | [diff] [blame] | 1168 | argv_array_push(&child.args, "--verbose"); |
| 1169 | argv_array_push(&child.args, url.buf); |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 1170 | for (i = 0; i < nr_spec; i++) |
Jeff King | 850d2fe | 2016-02-22 17:44:21 -0500 | [diff] [blame] | 1171 | argv_array_push(&child.args, specs[i]); |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 1172 | |
Jeff King | 850d2fe | 2016-02-22 17:44:21 -0500 | [diff] [blame] | 1173 | if (run_command(&child)) |
Johannes Schindelin | ed8b413 | 2019-03-05 15:20:40 -0800 | [diff] [blame] | 1174 | die(_("git-http-push failed")); |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 1175 | return 0; |
| 1176 | } |
| 1177 | |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 1178 | static int push_git(struct discovery *heads, int nr_spec, char **specs) |
| 1179 | { |
| 1180 | struct rpc_state rpc; |
Junio C Hamano | 222b121 | 2013-07-08 22:16:31 -0700 | [diff] [blame] | 1181 | int i, err; |
| 1182 | struct argv_array args; |
Junio C Hamano | 05c1eb1 | 2013-08-02 15:14:50 -0700 | [diff] [blame] | 1183 | struct string_list_item *cas_option; |
Jeff King | 26be19b | 2014-08-21 08:21:20 -0400 | [diff] [blame] | 1184 | struct strbuf preamble = STRBUF_INIT; |
Jonathan Tan | b359030 | 2019-02-14 11:06:37 -0800 | [diff] [blame] | 1185 | struct strbuf rpc_result = STRBUF_INIT; |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 1186 | |
Junio C Hamano | 222b121 | 2013-07-08 22:16:31 -0700 | [diff] [blame] | 1187 | argv_array_init(&args); |
| 1188 | argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status", |
| 1189 | NULL); |
| 1190 | |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 1191 | if (options.thin) |
Junio C Hamano | 222b121 | 2013-07-08 22:16:31 -0700 | [diff] [blame] | 1192 | argv_array_push(&args, "--thin"); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 1193 | if (options.dry_run) |
Junio C Hamano | 222b121 | 2013-07-08 22:16:31 -0700 | [diff] [blame] | 1194 | argv_array_push(&args, "--dry-run"); |
Dave Borowitz | 3026109 | 2015-08-19 11:26:46 -0400 | [diff] [blame] | 1195 | if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS) |
| 1196 | argv_array_push(&args, "--signed=yes"); |
| 1197 | else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED) |
| 1198 | argv_array_push(&args, "--signed=if-asked"); |
Clemens Buchacher | c207e34 | 2012-01-08 22:06:20 +0100 | [diff] [blame] | 1199 | if (options.verbosity == 0) |
Junio C Hamano | 222b121 | 2013-07-08 22:16:31 -0700 | [diff] [blame] | 1200 | argv_array_push(&args, "--quiet"); |
Clemens Buchacher | c207e34 | 2012-01-08 22:06:20 +0100 | [diff] [blame] | 1201 | else if (options.verbosity > 1) |
Junio C Hamano | 222b121 | 2013-07-08 22:16:31 -0700 | [diff] [blame] | 1202 | argv_array_push(&args, "--verbose"); |
Brandon Williams | 511155d | 2017-03-22 15:22:00 -0700 | [diff] [blame] | 1203 | for (i = 0; i < options.push_options.nr; i++) |
| 1204 | argv_array_pushf(&args, "--push-option=%s", |
| 1205 | options.push_options.items[i].string); |
Junio C Hamano | 222b121 | 2013-07-08 22:16:31 -0700 | [diff] [blame] | 1206 | argv_array_push(&args, options.progress ? "--progress" : "--no-progress"); |
Junio C Hamano | 05c1eb1 | 2013-08-02 15:14:50 -0700 | [diff] [blame] | 1207 | for_each_string_list_item(cas_option, &cas_options) |
Junio C Hamano | 2233ad4 | 2013-09-09 14:30:29 -0700 | [diff] [blame] | 1208 | argv_array_push(&args, cas_option->string); |
Jeff King | b227bbc | 2013-09-28 04:35:25 -0400 | [diff] [blame] | 1209 | argv_array_push(&args, url.buf); |
Jeff King | 26be19b | 2014-08-21 08:21:20 -0400 | [diff] [blame] | 1210 | |
| 1211 | argv_array_push(&args, "--stdin"); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 1212 | for (i = 0; i < nr_spec; i++) |
Jeff King | 26be19b | 2014-08-21 08:21:20 -0400 | [diff] [blame] | 1213 | packet_buf_write(&preamble, "%s\n", specs[i]); |
| 1214 | packet_buf_flush(&preamble); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 1215 | |
| 1216 | memset(&rpc, 0, sizeof(rpc)); |
| 1217 | rpc.service_name = "git-receive-pack", |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 1218 | |
Jonathan Tan | b359030 | 2019-02-14 11:06:37 -0800 | [diff] [blame] | 1219 | err = rpc_service(&rpc, heads, args.argv, &preamble, &rpc_result); |
| 1220 | if (rpc_result.len) |
| 1221 | write_or_die(1, rpc_result.buf, rpc_result.len); |
| 1222 | strbuf_release(&rpc_result); |
Jeff King | 26be19b | 2014-08-21 08:21:20 -0400 | [diff] [blame] | 1223 | strbuf_release(&preamble); |
Junio C Hamano | 222b121 | 2013-07-08 22:16:31 -0700 | [diff] [blame] | 1224 | argv_array_clear(&args); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 1225 | return err; |
| 1226 | } |
| 1227 | |
| 1228 | static int push(int nr_spec, char **specs) |
| 1229 | { |
Jeff King | 2a45520 | 2013-02-20 15:07:19 -0500 | [diff] [blame] | 1230 | struct discovery *heads = discover_refs("git-receive-pack", 1); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 1231 | int ret; |
| 1232 | |
| 1233 | if (heads->proto_git) |
| 1234 | ret = push_git(heads, nr_spec, specs); |
| 1235 | else |
| 1236 | ret = push_dav(nr_spec, specs); |
| 1237 | free_discovery(heads); |
| 1238 | return ret; |
| 1239 | } |
| 1240 | |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 1241 | static void parse_push(struct strbuf *buf) |
| 1242 | { |
| 1243 | char **specs = NULL; |
Shawn O. Pearce | 5238cbf | 2012-01-19 19:12:09 -0800 | [diff] [blame] | 1244 | int alloc_spec = 0, nr_spec = 0, i, ret; |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 1245 | |
| 1246 | do { |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1247 | if (starts_with(buf->buf, "push ")) { |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 1248 | ALLOC_GROW(specs, nr_spec + 1, alloc_spec); |
| 1249 | specs[nr_spec++] = xstrdup(buf->buf + 5); |
| 1250 | } |
| 1251 | else |
Johannes Schindelin | ed8b413 | 2019-03-05 15:20:40 -0800 | [diff] [blame] | 1252 | die(_("http transport does not support %s"), buf->buf); |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 1253 | |
| 1254 | strbuf_reset(buf); |
Junio C Hamano | 8f309ae | 2016-01-13 15:31:17 -0800 | [diff] [blame] | 1255 | if (strbuf_getline_lf(buf, stdin) == EOF) |
Jim Meyering | dc4cd76 | 2011-06-20 09:40:06 +0200 | [diff] [blame] | 1256 | goto free_specs; |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 1257 | if (!*buf->buf) |
| 1258 | break; |
| 1259 | } while (1); |
| 1260 | |
Shawn O. Pearce | 5238cbf | 2012-01-19 19:12:09 -0800 | [diff] [blame] | 1261 | ret = push(nr_spec, specs); |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 1262 | printf("\n"); |
| 1263 | fflush(stdout); |
Jim Meyering | dc4cd76 | 2011-06-20 09:40:06 +0200 | [diff] [blame] | 1264 | |
Shawn O. Pearce | 5238cbf | 2012-01-19 19:12:09 -0800 | [diff] [blame] | 1265 | if (ret) |
| 1266 | exit(128); /* error already reported */ |
| 1267 | |
Jim Meyering | dc4cd76 | 2011-06-20 09:40:06 +0200 | [diff] [blame] | 1268 | free_specs: |
| 1269 | for (i = 0; i < nr_spec; i++) |
| 1270 | free(specs[i]); |
| 1271 | free(specs); |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 1272 | } |
| 1273 | |
Brandon Williams | 0f1dc53 | 2018-03-15 10:31:41 -0700 | [diff] [blame] | 1274 | static int stateless_connect(const char *service_name) |
| 1275 | { |
| 1276 | struct discovery *discover; |
Jonathan Tan | a97d007 | 2019-02-21 12:24:41 -0800 | [diff] [blame] | 1277 | struct rpc_state rpc; |
| 1278 | struct strbuf buf = STRBUF_INIT; |
Brandon Williams | 0f1dc53 | 2018-03-15 10:31:41 -0700 | [diff] [blame] | 1279 | |
| 1280 | /* |
| 1281 | * Run the info/refs request and see if the server supports protocol |
| 1282 | * v2. If and only if the server supports v2 can we successfully |
| 1283 | * establish a stateless connection, otherwise we need to tell the |
| 1284 | * client to fallback to using other transport helper functions to |
| 1285 | * complete their request. |
| 1286 | */ |
| 1287 | discover = discover_refs(service_name, 0); |
| 1288 | if (discover->version != protocol_v2) { |
| 1289 | printf("fallback\n"); |
| 1290 | fflush(stdout); |
| 1291 | return -1; |
| 1292 | } else { |
| 1293 | /* Stateless Connection established */ |
| 1294 | printf("\n"); |
| 1295 | fflush(stdout); |
| 1296 | } |
| 1297 | |
Jonathan Tan | a97d007 | 2019-02-21 12:24:41 -0800 | [diff] [blame] | 1298 | rpc.service_name = service_name; |
| 1299 | rpc.service_url = xstrfmt("%s%s", url.buf, rpc.service_name); |
| 1300 | rpc.hdr_content_type = xstrfmt("Content-Type: application/x-%s-request", rpc.service_name); |
| 1301 | rpc.hdr_accept = xstrfmt("Accept: application/x-%s-result", rpc.service_name); |
| 1302 | if (get_protocol_http_header(discover->version, &buf)) { |
| 1303 | rpc.protocol_header = strbuf_detach(&buf, NULL); |
| 1304 | } else { |
| 1305 | rpc.protocol_header = NULL; |
| 1306 | strbuf_release(&buf); |
| 1307 | } |
| 1308 | rpc.buf = xmalloc(http_post_buffer); |
| 1309 | rpc.alloc = http_post_buffer; |
| 1310 | rpc.len = 0; |
| 1311 | rpc.pos = 0; |
| 1312 | rpc.in = 1; |
| 1313 | rpc.out = 0; |
| 1314 | rpc.any_written = 0; |
| 1315 | rpc.gzip_request = 1; |
| 1316 | rpc.initial_buffer = 0; |
| 1317 | rpc.write_line_lengths = 1; |
| 1318 | rpc.flush_read_but_not_sent = 0; |
Brandon Williams | 0f1dc53 | 2018-03-15 10:31:41 -0700 | [diff] [blame] | 1319 | |
| 1320 | /* |
| 1321 | * Dump the capability listing that we got from the server earlier |
| 1322 | * during the info/refs request. |
| 1323 | */ |
Jonathan Tan | a97d007 | 2019-02-21 12:24:41 -0800 | [diff] [blame] | 1324 | write_or_die(rpc.in, discover->buf, discover->len); |
Brandon Williams | 0f1dc53 | 2018-03-15 10:31:41 -0700 | [diff] [blame] | 1325 | |
Jonathan Tan | a97d007 | 2019-02-21 12:24:41 -0800 | [diff] [blame] | 1326 | /* Until we see EOF keep sending POSTs */ |
| 1327 | while (1) { |
| 1328 | size_t avail; |
| 1329 | enum packet_read_status status; |
| 1330 | |
| 1331 | if (!rpc_read_from_out(&rpc, PACKET_READ_GENTLE_ON_EOF, &avail, |
| 1332 | &status)) |
| 1333 | BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX"); |
| 1334 | if (status == PACKET_READ_EOF) |
| 1335 | break; |
| 1336 | if (post_rpc(&rpc, status == PACKET_READ_FLUSH)) |
Brandon Williams | 0f1dc53 | 2018-03-15 10:31:41 -0700 | [diff] [blame] | 1337 | /* We would have an err here */ |
| 1338 | break; |
Jonathan Tan | a97d007 | 2019-02-21 12:24:41 -0800 | [diff] [blame] | 1339 | /* Reset the buffer for next request */ |
| 1340 | rpc.len = 0; |
Brandon Williams | 0f1dc53 | 2018-03-15 10:31:41 -0700 | [diff] [blame] | 1341 | } |
| 1342 | |
Jonathan Tan | a97d007 | 2019-02-21 12:24:41 -0800 | [diff] [blame] | 1343 | free(rpc.service_url); |
| 1344 | free(rpc.hdr_content_type); |
| 1345 | free(rpc.hdr_accept); |
| 1346 | free(rpc.protocol_header); |
| 1347 | free(rpc.buf); |
| 1348 | strbuf_release(&buf); |
| 1349 | |
Brandon Williams | 0f1dc53 | 2018-03-15 10:31:41 -0700 | [diff] [blame] | 1350 | return 0; |
| 1351 | } |
| 1352 | |
Jeff King | 3f2e229 | 2016-07-01 01:58:58 -0400 | [diff] [blame] | 1353 | int cmd_main(int argc, const char **argv) |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 1354 | { |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 1355 | struct strbuf buf = STRBUF_INIT; |
Daniel Barkalow | a45d3d7 | 2009-11-03 21:52:35 -0500 | [diff] [blame] | 1356 | int nongit; |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 1357 | |
Daniel Barkalow | a45d3d7 | 2009-11-03 21:52:35 -0500 | [diff] [blame] | 1358 | setup_git_directory_gently(&nongit); |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 1359 | if (argc < 2) { |
Johannes Schindelin | ed8b413 | 2019-03-05 15:20:40 -0800 | [diff] [blame] | 1360 | error(_("remote-curl: usage: git remote-curl <remote> [<url>]")); |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 1361 | return 1; |
| 1362 | } |
| 1363 | |
Shawn O. Pearce | ef08ef9 | 2009-10-30 17:47:29 -0700 | [diff] [blame] | 1364 | options.verbosity = 1; |
| 1365 | options.progress = !!isatty(2); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 1366 | options.thin = 1; |
Nguyễn Thái Ngọc Duy | a45a260 | 2016-06-12 17:54:04 +0700 | [diff] [blame] | 1367 | string_list_init(&options.deepen_not, 1); |
Brandon Williams | 511155d | 2017-03-22 15:22:00 -0700 | [diff] [blame] | 1368 | string_list_init(&options.push_options, 1); |
Shawn O. Pearce | ef08ef9 | 2009-10-30 17:47:29 -0700 | [diff] [blame] | 1369 | |
Jeff Hostetler | ee4512e | 2019-02-22 14:25:01 -0800 | [diff] [blame] | 1370 | /* |
| 1371 | * Just report "remote-curl" here (folding all the various aliases |
| 1372 | * ("git-remote-http", "git-remote-https", and etc.) here since they |
| 1373 | * are all just copies of the same actual executable. |
| 1374 | */ |
| 1375 | trace2_cmd_name("remote-curl"); |
| 1376 | |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 1377 | remote = remote_get(argv[1]); |
| 1378 | |
| 1379 | if (argc > 2) { |
Jeff King | b227bbc | 2013-09-28 04:35:25 -0400 | [diff] [blame] | 1380 | end_url_with_slash(&url, argv[2]); |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 1381 | } else { |
Jeff King | b227bbc | 2013-09-28 04:35:25 -0400 | [diff] [blame] | 1382 | end_url_with_slash(&url, remote->url[0]); |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 1383 | } |
| 1384 | |
Jeff King | b227bbc | 2013-09-28 04:35:25 -0400 | [diff] [blame] | 1385 | http_init(remote, url.buf, 0); |
Tay Ray Chuan | 888692b | 2010-03-02 18:49:29 +0800 | [diff] [blame] | 1386 | |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 1387 | do { |
Jeff King | 95b567c | 2014-06-18 15:48:29 -0400 | [diff] [blame] | 1388 | const char *arg; |
| 1389 | |
Junio C Hamano | 8f309ae | 2016-01-13 15:31:17 -0800 | [diff] [blame] | 1390 | if (strbuf_getline_lf(&buf, stdin) == EOF) { |
Sverre Rabbelier | 1843f0c | 2011-07-16 15:03:29 +0200 | [diff] [blame] | 1391 | if (ferror(stdin)) |
Johannes Schindelin | ed8b413 | 2019-03-05 15:20:40 -0800 | [diff] [blame] | 1392 | error(_("remote-curl: error reading command stream from git")); |
Sverre Rabbelier | 1843f0c | 2011-07-16 15:03:29 +0200 | [diff] [blame] | 1393 | return 1; |
| 1394 | } |
| 1395 | if (buf.len == 0) |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 1396 | break; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1397 | if (starts_with(buf.buf, "fetch ")) { |
Daniel Barkalow | a45d3d7 | 2009-11-03 21:52:35 -0500 | [diff] [blame] | 1398 | if (nongit) |
Johannes Schindelin | ed8b413 | 2019-03-05 15:20:40 -0800 | [diff] [blame] | 1399 | die(_("remote-curl: fetch attempted without a local repo")); |
Shawn O. Pearce | 292ce46 | 2009-10-30 17:47:28 -0700 | [diff] [blame] | 1400 | parse_fetch(&buf); |
| 1401 | |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1402 | } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) { |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 1403 | int for_push = !!strstr(buf.buf + 4, "for-push"); |
| 1404 | output_refs(get_refs(for_push)); |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 1405 | |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1406 | } else if (starts_with(buf.buf, "push ")) { |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 1407 | parse_push(&buf); |
| 1408 | |
Jeff King | 95b567c | 2014-06-18 15:48:29 -0400 | [diff] [blame] | 1409 | } else if (skip_prefix(buf.buf, "option ", &arg)) { |
| 1410 | char *value = strchr(arg, ' '); |
Shawn O. Pearce | ef08ef9 | 2009-10-30 17:47:29 -0700 | [diff] [blame] | 1411 | int result; |
| 1412 | |
| 1413 | if (value) |
| 1414 | *value++ = '\0'; |
| 1415 | else |
| 1416 | value = "true"; |
| 1417 | |
Jeff King | 95b567c | 2014-06-18 15:48:29 -0400 | [diff] [blame] | 1418 | result = set_option(arg, value); |
Shawn O. Pearce | ef08ef9 | 2009-10-30 17:47:29 -0700 | [diff] [blame] | 1419 | if (!result) |
| 1420 | printf("ok\n"); |
| 1421 | else if (result < 0) |
| 1422 | printf("error invalid value\n"); |
| 1423 | else |
| 1424 | printf("unsupported\n"); |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 1425 | fflush(stdout); |
Shawn O. Pearce | ef08ef9 | 2009-10-30 17:47:29 -0700 | [diff] [blame] | 1426 | |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 1427 | } else if (!strcmp(buf.buf, "capabilities")) { |
Brandon Williams | 0f1dc53 | 2018-03-15 10:31:41 -0700 | [diff] [blame] | 1428 | printf("stateless-connect\n"); |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 1429 | printf("fetch\n"); |
Shawn O. Pearce | ef08ef9 | 2009-10-30 17:47:29 -0700 | [diff] [blame] | 1430 | printf("option\n"); |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 1431 | printf("push\n"); |
Nguyễn Thái Ngọc Duy | 9ba3804 | 2013-07-21 15:18:05 +0700 | [diff] [blame] | 1432 | printf("check-connectivity\n"); |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 1433 | printf("\n"); |
| 1434 | fflush(stdout); |
Brandon Williams | 0f1dc53 | 2018-03-15 10:31:41 -0700 | [diff] [blame] | 1435 | } else if (skip_prefix(buf.buf, "stateless-connect ", &arg)) { |
| 1436 | if (!stateless_connect(arg)) |
| 1437 | break; |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 1438 | } else { |
Johannes Schindelin | ed8b413 | 2019-03-05 15:20:40 -0800 | [diff] [blame] | 1439 | error(_("remote-curl: unknown command '%s' from git"), buf.buf); |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 1440 | return 1; |
| 1441 | } |
| 1442 | strbuf_reset(&buf); |
| 1443 | } while (1); |
Tay Ray Chuan | 888692b | 2010-03-02 18:49:29 +0800 | [diff] [blame] | 1444 | |
| 1445 | http_cleanup(); |
| 1446 | |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 1447 | return 0; |
| 1448 | } |