Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "remote.h" |
| 3 | #include "strbuf.h" |
| 4 | #include "walker.h" |
| 5 | #include "http.h" |
Björn Steinbrink | d01a8e3 | 2009-10-14 00:11:09 +0200 | [diff] [blame] | 6 | #include "exec_cmd.h" |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 7 | #include "run-command.h" |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 8 | #include "pkt-line.h" |
Junio C Hamano | 05c1eb1 | 2013-08-02 15:14:50 -0700 | [diff] [blame] | 9 | #include "string-list.h" |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 10 | #include "sideband.h" |
Junio C Hamano | 222b121 | 2013-07-08 22:16:31 -0700 | [diff] [blame] | 11 | #include "argv-array.h" |
Jeff King | 2501aff | 2013-09-28 04:31:45 -0400 | [diff] [blame] | 12 | #include "credential.h" |
Nguyễn Thái Ngọc Duy | 1609488 | 2013-12-05 20:02:50 +0700 | [diff] [blame] | 13 | #include "sha1-array.h" |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 14 | |
Shawn O. Pearce | 37a8768 | 2009-10-30 17:47:26 -0700 | [diff] [blame] | 15 | static struct remote *remote; |
Jeff King | b227bbc | 2013-09-28 04:35:25 -0400 | [diff] [blame] | 16 | /* always ends with a trailing slash */ |
| 17 | static struct strbuf url = STRBUF_INIT; |
Shawn O. Pearce | 37a8768 | 2009-10-30 17:47:26 -0700 | [diff] [blame] | 18 | |
Shawn O. Pearce | ef08ef9 | 2009-10-30 17:47:29 -0700 | [diff] [blame] | 19 | struct options { |
| 20 | int verbosity; |
| 21 | unsigned long depth; |
| 22 | unsigned progress : 1, |
Nguyễn Thái Ngọc Duy | 9ba3804 | 2013-07-21 15:18:05 +0700 | [diff] [blame] | 23 | check_self_contained_and_connected : 1, |
Nguyễn Thái Ngọc Duy | 1609488 | 2013-12-05 20:02:50 +0700 | [diff] [blame] | 24 | cloning : 1, |
| 25 | update_shallow : 1, |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 26 | followtags : 1, |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 27 | dry_run : 1, |
| 28 | thin : 1; |
Shawn O. Pearce | ef08ef9 | 2009-10-30 17:47:29 -0700 | [diff] [blame] | 29 | }; |
| 30 | static struct options options; |
Junio C Hamano | 05c1eb1 | 2013-08-02 15:14:50 -0700 | [diff] [blame] | 31 | static struct string_list cas_options = STRING_LIST_INIT_DUP; |
Shawn O. Pearce | ef08ef9 | 2009-10-30 17:47:29 -0700 | [diff] [blame] | 32 | |
Shawn O. Pearce | ef08ef9 | 2009-10-30 17:47:29 -0700 | [diff] [blame] | 33 | static int set_option(const char *name, const char *value) |
| 34 | { |
| 35 | if (!strcmp(name, "verbosity")) { |
| 36 | char *end; |
| 37 | int v = strtol(value, &end, 10); |
| 38 | if (value == end || *end) |
| 39 | return -1; |
| 40 | options.verbosity = v; |
| 41 | return 0; |
| 42 | } |
| 43 | else if (!strcmp(name, "progress")) { |
| 44 | if (!strcmp(value, "true")) |
| 45 | options.progress = 1; |
| 46 | else if (!strcmp(value, "false")) |
| 47 | options.progress = 0; |
| 48 | else |
| 49 | return -1; |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 50 | return 0; |
Shawn O. Pearce | ef08ef9 | 2009-10-30 17:47:29 -0700 | [diff] [blame] | 51 | } |
| 52 | else if (!strcmp(name, "depth")) { |
| 53 | char *end; |
| 54 | unsigned long v = strtoul(value, &end, 10); |
| 55 | if (value == end || *end) |
| 56 | return -1; |
| 57 | options.depth = v; |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 58 | return 0; |
Shawn O. Pearce | ef08ef9 | 2009-10-30 17:47:29 -0700 | [diff] [blame] | 59 | } |
| 60 | else if (!strcmp(name, "followtags")) { |
| 61 | if (!strcmp(value, "true")) |
| 62 | options.followtags = 1; |
| 63 | else if (!strcmp(value, "false")) |
| 64 | options.followtags = 0; |
| 65 | else |
| 66 | return -1; |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 67 | return 0; |
Shawn O. Pearce | ef08ef9 | 2009-10-30 17:47:29 -0700 | [diff] [blame] | 68 | } |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 69 | else if (!strcmp(name, "dry-run")) { |
| 70 | if (!strcmp(value, "true")) |
| 71 | options.dry_run = 1; |
| 72 | else if (!strcmp(value, "false")) |
| 73 | options.dry_run = 0; |
| 74 | else |
| 75 | return -1; |
| 76 | return 0; |
| 77 | } |
Nguyễn Thái Ngọc Duy | 9ba3804 | 2013-07-21 15:18:05 +0700 | [diff] [blame] | 78 | else if (!strcmp(name, "check-connectivity")) { |
| 79 | if (!strcmp(value, "true")) |
| 80 | options.check_self_contained_and_connected = 1; |
| 81 | else if (!strcmp(value, "false")) |
| 82 | options.check_self_contained_and_connected = 0; |
| 83 | else |
| 84 | return -1; |
| 85 | return 0; |
| 86 | } |
Junio C Hamano | 05c1eb1 | 2013-08-02 15:14:50 -0700 | [diff] [blame] | 87 | else if (!strcmp(name, "cas")) { |
| 88 | struct strbuf val = STRBUF_INIT; |
| 89 | strbuf_addf(&val, "--" CAS_OPT_NAME "=%s", value); |
| 90 | string_list_append(&cas_options, val.buf); |
| 91 | strbuf_release(&val); |
| 92 | return 0; |
Nguyễn Thái Ngọc Duy | 1609488 | 2013-12-05 20:02:50 +0700 | [diff] [blame] | 93 | } else if (!strcmp(name, "cloning")) { |
| 94 | if (!strcmp(value, "true")) |
| 95 | options.cloning = 1; |
| 96 | else if (!strcmp(value, "false")) |
| 97 | options.cloning = 0; |
| 98 | else |
| 99 | return -1; |
| 100 | return 0; |
| 101 | } else if (!strcmp(name, "update-shallow")) { |
| 102 | if (!strcmp(value, "true")) |
| 103 | options.update_shallow = 1; |
| 104 | else if (!strcmp(value, "false")) |
| 105 | options.update_shallow = 0; |
| 106 | else |
| 107 | return -1; |
| 108 | return 0; |
| 109 | } else { |
Shawn O. Pearce | ef08ef9 | 2009-10-30 17:47:29 -0700 | [diff] [blame] | 110 | return 1 /* unsupported */; |
| 111 | } |
| 112 | } |
| 113 | |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 114 | struct discovery { |
| 115 | const char *service; |
| 116 | char *buf_alloc; |
| 117 | char *buf; |
| 118 | size_t len; |
Jeff King | 2a45520 | 2013-02-20 15:07:19 -0500 | [diff] [blame] | 119 | struct ref *refs; |
Nguyễn Thái Ngọc Duy | 1609488 | 2013-12-05 20:02:50 +0700 | [diff] [blame] | 120 | struct sha1_array shallow; |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 121 | unsigned proto_git : 1; |
| 122 | }; |
| 123 | static struct discovery *last_discovery; |
| 124 | |
Jeff King | b8054bb | 2013-02-20 15:07:11 -0500 | [diff] [blame] | 125 | static struct ref *parse_git_refs(struct discovery *heads, int for_push) |
| 126 | { |
| 127 | struct ref *list = NULL; |
| 128 | get_remote_heads(-1, heads->buf, heads->len, &list, |
Nguyễn Thái Ngọc Duy | 1609488 | 2013-12-05 20:02:50 +0700 | [diff] [blame] | 129 | for_push ? REF_NORMAL : 0, NULL, &heads->shallow); |
Jeff King | b8054bb | 2013-02-20 15:07:11 -0500 | [diff] [blame] | 130 | return list; |
| 131 | } |
| 132 | |
| 133 | static struct ref *parse_info_refs(struct discovery *heads) |
| 134 | { |
| 135 | char *data, *start, *mid; |
| 136 | char *ref_name; |
| 137 | int i = 0; |
| 138 | |
| 139 | struct ref *refs = NULL; |
| 140 | struct ref *ref = NULL; |
| 141 | struct ref *last_ref = NULL; |
| 142 | |
| 143 | data = heads->buf; |
| 144 | start = NULL; |
| 145 | mid = data; |
| 146 | while (i < heads->len) { |
| 147 | if (!start) { |
| 148 | start = &data[i]; |
| 149 | } |
| 150 | if (data[i] == '\t') |
| 151 | mid = &data[i]; |
| 152 | if (data[i] == '\n') { |
| 153 | if (mid - start != 40) |
Jeff King | b227bbc | 2013-09-28 04:35:25 -0400 | [diff] [blame] | 154 | die("%sinfo/refs not valid: is this a git repository?", |
| 155 | url.buf); |
Jeff King | b8054bb | 2013-02-20 15:07:11 -0500 | [diff] [blame] | 156 | data[i] = 0; |
| 157 | ref_name = mid + 1; |
| 158 | ref = xmalloc(sizeof(struct ref) + |
| 159 | strlen(ref_name) + 1); |
| 160 | memset(ref, 0, sizeof(struct ref)); |
| 161 | strcpy(ref->name, ref_name); |
| 162 | get_sha1_hex(start, ref->old_sha1); |
| 163 | if (!refs) |
| 164 | refs = ref; |
| 165 | if (last_ref) |
| 166 | last_ref->next = ref; |
| 167 | last_ref = ref; |
| 168 | start = NULL; |
| 169 | } |
| 170 | i++; |
| 171 | } |
| 172 | |
| 173 | ref = alloc_ref("HEAD"); |
Jeff King | b227bbc | 2013-09-28 04:35:25 -0400 | [diff] [blame] | 174 | if (!http_fetch_ref(url.buf, ref) && |
Jeff King | b8054bb | 2013-02-20 15:07:11 -0500 | [diff] [blame] | 175 | !resolve_remote_symref(ref, refs)) { |
| 176 | ref->next = refs; |
| 177 | refs = ref; |
| 178 | } else { |
| 179 | free(ref); |
| 180 | } |
| 181 | |
| 182 | return refs; |
| 183 | } |
| 184 | |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 185 | static void free_discovery(struct discovery *d) |
| 186 | { |
| 187 | if (d) { |
| 188 | if (d == last_discovery) |
| 189 | last_discovery = NULL; |
Nguyễn Thái Ngọc Duy | 1609488 | 2013-12-05 20:02:50 +0700 | [diff] [blame] | 190 | free(d->shallow.sha1); |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 191 | free(d->buf_alloc); |
Jeff King | 2a45520 | 2013-02-20 15:07:19 -0500 | [diff] [blame] | 192 | free_refs(d->refs); |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 193 | free(d); |
| 194 | } |
| 195 | } |
| 196 | |
Jeff King | 426e70d | 2013-04-05 18:17:23 -0400 | [diff] [blame] | 197 | static int show_http_message(struct strbuf *type, struct strbuf *msg) |
| 198 | { |
| 199 | const char *p, *eol; |
| 200 | |
| 201 | /* |
| 202 | * We only show text/plain parts, as other types are likely |
| 203 | * to be ugly to look at on the user's terminal. |
| 204 | * |
| 205 | * TODO should handle "; charset=XXX", and re-encode into |
| 206 | * logoutputencoding |
| 207 | */ |
| 208 | if (strcasecmp(type->buf, "text/plain")) |
| 209 | return -1; |
| 210 | |
| 211 | strbuf_trim(msg); |
| 212 | if (!msg->len) |
| 213 | return -1; |
| 214 | |
| 215 | p = msg->buf; |
| 216 | do { |
| 217 | eol = strchrnul(p, '\n'); |
| 218 | fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p); |
| 219 | p = eol + 1; |
| 220 | } while(*eol); |
| 221 | return 0; |
| 222 | } |
| 223 | |
Jeff King | 2a45520 | 2013-02-20 15:07:19 -0500 | [diff] [blame] | 224 | static struct discovery* discover_refs(const char *service, int for_push) |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 225 | { |
Shawn Pearce | 4656bf4 | 2013-01-31 13:02:07 -0800 | [diff] [blame] | 226 | struct strbuf exp = STRBUF_INIT; |
| 227 | struct strbuf type = STRBUF_INIT; |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 228 | struct strbuf buffer = STRBUF_INIT; |
Jeff King | c65d569 | 2013-09-28 04:35:10 -0400 | [diff] [blame] | 229 | struct strbuf refs_url = STRBUF_INIT; |
Jeff King | 050ef36 | 2013-09-28 04:35:35 -0400 | [diff] [blame] | 230 | struct strbuf effective_url = STRBUF_INIT; |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 231 | struct discovery *last = last_discovery; |
Jeff King | 243c329 | 2012-09-20 13:00:22 -0400 | [diff] [blame] | 232 | int http_ret, maybe_smart = 0; |
Jeff King | 1bbcc22 | 2013-09-28 04:31:23 -0400 | [diff] [blame] | 233 | struct http_get_options options; |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 234 | |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 235 | if (last && !strcmp(service, last->service)) |
| 236 | return last; |
| 237 | free_discovery(last); |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 238 | |
Jeff King | b227bbc | 2013-09-28 04:35:25 -0400 | [diff] [blame] | 239 | strbuf_addf(&refs_url, "%sinfo/refs", url.buf); |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 240 | if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) && |
Jeff King | 02572c2 | 2012-09-20 17:30:58 -0400 | [diff] [blame] | 241 | git_env_bool("GIT_SMART_HTTP", 1)) { |
Jeff King | 243c329 | 2012-09-20 13:00:22 -0400 | [diff] [blame] | 242 | maybe_smart = 1; |
Jeff King | b227bbc | 2013-09-28 04:35:25 -0400 | [diff] [blame] | 243 | if (!strchr(url.buf, '?')) |
Jeff King | c65d569 | 2013-09-28 04:35:10 -0400 | [diff] [blame] | 244 | strbuf_addch(&refs_url, '?'); |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 245 | else |
Jeff King | c65d569 | 2013-09-28 04:35:10 -0400 | [diff] [blame] | 246 | strbuf_addch(&refs_url, '&'); |
| 247 | strbuf_addf(&refs_url, "service=%s", service); |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 248 | } |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 249 | |
Jeff King | 1bbcc22 | 2013-09-28 04:31:23 -0400 | [diff] [blame] | 250 | memset(&options, 0, sizeof(options)); |
| 251 | options.content_type = &type; |
Jeff King | 050ef36 | 2013-09-28 04:35:35 -0400 | [diff] [blame] | 252 | options.effective_url = &effective_url; |
| 253 | options.base_url = &url; |
Jeff King | 1bbcc22 | 2013-09-28 04:31:23 -0400 | [diff] [blame] | 254 | options.no_cache = 1; |
| 255 | options.keep_error = 1; |
| 256 | |
Jeff King | c65d569 | 2013-09-28 04:35:10 -0400 | [diff] [blame] | 257 | http_ret = http_get_strbuf(refs_url.buf, &buffer, &options); |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 258 | switch (http_ret) { |
| 259 | case HTTP_OK: |
| 260 | break; |
| 261 | case HTTP_MISSING_TARGET: |
Jeff King | cfa0f40 | 2013-04-05 18:20:43 -0400 | [diff] [blame] | 262 | show_http_message(&type, &buffer); |
Jeff King | b227bbc | 2013-09-28 04:35:25 -0400 | [diff] [blame] | 263 | die("repository '%s' not found", url.buf); |
Scott Chacon | 42653c0 | 2010-04-01 15:14:35 -0700 | [diff] [blame] | 264 | case HTTP_NOAUTH: |
Jeff King | 426e70d | 2013-04-05 18:17:23 -0400 | [diff] [blame] | 265 | show_http_message(&type, &buffer); |
Jeff King | b227bbc | 2013-09-28 04:35:25 -0400 | [diff] [blame] | 266 | die("Authentication failed for '%s'", url.buf); |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 267 | default: |
Jeff King | 426e70d | 2013-04-05 18:17:23 -0400 | [diff] [blame] | 268 | show_http_message(&type, &buffer); |
Jeff King | b227bbc | 2013-09-28 04:35:25 -0400 | [diff] [blame] | 269 | die("unable to access '%s': %s", url.buf, curl_errorstr); |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 270 | } |
| 271 | |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 272 | last= xcalloc(1, sizeof(*last_discovery)); |
| 273 | last->service = service; |
| 274 | last->buf_alloc = strbuf_detach(&buffer, &last->len); |
| 275 | last->buf = last->buf_alloc; |
| 276 | |
Shawn Pearce | 4656bf4 | 2013-01-31 13:02:07 -0800 | [diff] [blame] | 277 | strbuf_addf(&exp, "application/x-%s-advertisement", service); |
| 278 | if (maybe_smart && |
| 279 | (5 <= last->len && last->buf[4] == '#') && |
| 280 | !strbuf_cmp(&exp, &type)) { |
Jeff King | 4981fe7 | 2013-02-23 17:31:34 -0500 | [diff] [blame] | 281 | char *line; |
| 282 | |
Shawn Pearce | 4656bf4 | 2013-01-31 13:02:07 -0800 | [diff] [blame] | 283 | /* |
| 284 | * smart HTTP response; validate that the service |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 285 | * pkt-line matches our request. |
| 286 | */ |
Jeff King | 4981fe7 | 2013-02-23 17:31:34 -0500 | [diff] [blame] | 287 | line = packet_read_line_buf(&last->buf, &last->len, NULL); |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 288 | |
Shawn Pearce | 4656bf4 | 2013-01-31 13:02:07 -0800 | [diff] [blame] | 289 | strbuf_reset(&exp); |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 290 | strbuf_addf(&exp, "# service=%s", service); |
Jeff King | 4981fe7 | 2013-02-23 17:31:34 -0500 | [diff] [blame] | 291 | if (strcmp(line, exp.buf)) |
| 292 | die("invalid server response; got '%s'", line); |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 293 | strbuf_release(&exp); |
| 294 | |
| 295 | /* The header can include additional metadata lines, up |
| 296 | * until a packet flush marker. Ignore these now, but |
| 297 | * in the future we might start to scan them. |
| 298 | */ |
Jeff King | 4981fe7 | 2013-02-23 17:31:34 -0500 | [diff] [blame] | 299 | while (packet_read_line_buf(&last->buf, &last->len, NULL)) |
| 300 | ; |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 301 | |
| 302 | last->proto_git = 1; |
| 303 | } |
| 304 | |
Jeff King | 2a45520 | 2013-02-20 15:07:19 -0500 | [diff] [blame] | 305 | if (last->proto_git) |
| 306 | last->refs = parse_git_refs(last, for_push); |
| 307 | else |
| 308 | last->refs = parse_info_refs(last); |
| 309 | |
Jeff King | c65d569 | 2013-09-28 04:35:10 -0400 | [diff] [blame] | 310 | strbuf_release(&refs_url); |
Shawn Pearce | 4656bf4 | 2013-01-31 13:02:07 -0800 | [diff] [blame] | 311 | strbuf_release(&exp); |
| 312 | strbuf_release(&type); |
Jeff King | 050ef36 | 2013-09-28 04:35:35 -0400 | [diff] [blame] | 313 | strbuf_release(&effective_url); |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 314 | strbuf_release(&buffer); |
| 315 | last_discovery = last; |
| 316 | return last; |
| 317 | } |
| 318 | |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 319 | static struct ref *get_refs(int for_push) |
| 320 | { |
| 321 | struct discovery *heads; |
| 322 | |
| 323 | if (for_push) |
Jeff King | 2a45520 | 2013-02-20 15:07:19 -0500 | [diff] [blame] | 324 | heads = discover_refs("git-receive-pack", for_push); |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 325 | else |
Jeff King | 2a45520 | 2013-02-20 15:07:19 -0500 | [diff] [blame] | 326 | heads = discover_refs("git-upload-pack", for_push); |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 327 | |
Jeff King | 2a45520 | 2013-02-20 15:07:19 -0500 | [diff] [blame] | 328 | return heads->refs; |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 331 | static void output_refs(struct ref *refs) |
| 332 | { |
| 333 | struct ref *posn; |
| 334 | for (posn = refs; posn; posn = posn->next) { |
| 335 | if (posn->symref) |
| 336 | printf("@%s %s\n", posn->symref, posn->name); |
| 337 | else |
| 338 | printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name); |
| 339 | } |
| 340 | printf("\n"); |
| 341 | fflush(stdout); |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 344 | struct rpc_state { |
| 345 | const char *service_name; |
| 346 | const char **argv; |
Ivan Todoroski | 8150749 | 2012-04-02 17:14:44 +0200 | [diff] [blame] | 347 | struct strbuf *stdin_preamble; |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 348 | char *service_url; |
| 349 | char *hdr_content_type; |
| 350 | char *hdr_accept; |
| 351 | char *buf; |
| 352 | size_t alloc; |
| 353 | size_t len; |
| 354 | size_t pos; |
| 355 | int in; |
| 356 | int out; |
| 357 | struct strbuf result; |
Shawn O. Pearce | b853860 | 2009-10-30 17:47:43 -0700 | [diff] [blame] | 358 | unsigned gzip_request : 1; |
Martin Storsjö | 6c81a99 | 2009-12-01 12:33:39 +0200 | [diff] [blame] | 359 | unsigned initial_buffer : 1; |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 360 | }; |
| 361 | |
| 362 | static size_t rpc_out(void *ptr, size_t eltsize, |
| 363 | size_t nmemb, void *buffer_) |
| 364 | { |
| 365 | size_t max = eltsize * nmemb; |
| 366 | struct rpc_state *rpc = buffer_; |
| 367 | size_t avail = rpc->len - rpc->pos; |
| 368 | |
| 369 | if (!avail) { |
Martin Storsjö | 6c81a99 | 2009-12-01 12:33:39 +0200 | [diff] [blame] | 370 | rpc->initial_buffer = 0; |
Jeff King | 4981fe7 | 2013-02-23 17:31:34 -0500 | [diff] [blame] | 371 | avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 372 | if (!avail) |
| 373 | return 0; |
| 374 | rpc->pos = 0; |
| 375 | rpc->len = avail; |
| 376 | } |
| 377 | |
Tay Ray Chuan | 4831060 | 2009-11-24 10:31:30 +0800 | [diff] [blame] | 378 | if (max < avail) |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 379 | avail = max; |
| 380 | memcpy(ptr, rpc->buf + rpc->pos, avail); |
| 381 | rpc->pos += avail; |
| 382 | return avail; |
| 383 | } |
| 384 | |
Martin Storsjö | 6c81a99 | 2009-12-01 12:33:39 +0200 | [diff] [blame] | 385 | #ifndef NO_CURL_IOCTL |
Junio C Hamano | 5092d3e | 2010-01-11 22:30:36 -0800 | [diff] [blame] | 386 | static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp) |
Martin Storsjö | 6c81a99 | 2009-12-01 12:33:39 +0200 | [diff] [blame] | 387 | { |
| 388 | struct rpc_state *rpc = clientp; |
| 389 | |
| 390 | switch (cmd) { |
| 391 | case CURLIOCMD_NOP: |
| 392 | return CURLIOE_OK; |
| 393 | |
| 394 | case CURLIOCMD_RESTARTREAD: |
| 395 | if (rpc->initial_buffer) { |
| 396 | rpc->pos = 0; |
| 397 | return CURLIOE_OK; |
| 398 | } |
| 399 | fprintf(stderr, "Unable to rewind rpc post data - try increasing http.postBuffer\n"); |
| 400 | return CURLIOE_FAILRESTART; |
| 401 | |
| 402 | default: |
| 403 | return CURLIOE_UNKNOWNCMD; |
| 404 | } |
| 405 | } |
| 406 | #endif |
| 407 | |
Dan McGee | a04ff3e | 2011-05-03 23:47:27 +0800 | [diff] [blame] | 408 | static size_t rpc_in(char *ptr, size_t eltsize, |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 409 | size_t nmemb, void *buffer_) |
| 410 | { |
| 411 | size_t size = eltsize * nmemb; |
| 412 | struct rpc_state *rpc = buffer_; |
| 413 | write_or_die(rpc->in, ptr, size); |
| 414 | return size; |
| 415 | } |
| 416 | |
Jeff King | 3a347ed | 2013-10-31 02:36:26 -0400 | [diff] [blame] | 417 | static int run_slot(struct active_request_slot *slot, |
| 418 | struct slot_results *results) |
Shawn O. Pearce | 206b099 | 2011-02-15 08:57:24 -0800 | [diff] [blame] | 419 | { |
Jeff King | b81401c | 2012-08-27 09:27:15 -0400 | [diff] [blame] | 420 | int err; |
Jeff King | 3a347ed | 2013-10-31 02:36:26 -0400 | [diff] [blame] | 421 | struct slot_results results_buf; |
Shawn O. Pearce | 206b099 | 2011-02-15 08:57:24 -0800 | [diff] [blame] | 422 | |
Jeff King | 3a347ed | 2013-10-31 02:36:26 -0400 | [diff] [blame] | 423 | if (!results) |
| 424 | results = &results_buf; |
| 425 | |
Jeff King | beed336 | 2014-02-18 05:34:20 -0500 | [diff] [blame] | 426 | err = run_one_slot(slot, results); |
Shawn O. Pearce | 206b099 | 2011-02-15 08:57:24 -0800 | [diff] [blame] | 427 | |
Jeff King | b81401c | 2012-08-27 09:27:15 -0400 | [diff] [blame] | 428 | if (err != HTTP_OK && err != HTTP_REAUTH) { |
| 429 | error("RPC failed; result=%d, HTTP code = %ld", |
Jeff King | 3a347ed | 2013-10-31 02:36:26 -0400 | [diff] [blame] | 430 | results->curl_result, results->http_code); |
Shawn O. Pearce | 206b099 | 2011-02-15 08:57:24 -0800 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | return err; |
| 434 | } |
| 435 | |
Jeff King | 3a347ed | 2013-10-31 02:36:26 -0400 | [diff] [blame] | 436 | 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] | 437 | { |
| 438 | struct active_request_slot *slot; |
| 439 | struct curl_slist *headers = NULL; |
| 440 | struct strbuf buf = STRBUF_INIT; |
| 441 | int err; |
| 442 | |
| 443 | slot = get_active_slot(); |
| 444 | |
| 445 | headers = curl_slist_append(headers, rpc->hdr_content_type); |
| 446 | headers = curl_slist_append(headers, rpc->hdr_accept); |
| 447 | |
| 448 | curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0); |
| 449 | curl_easy_setopt(slot->curl, CURLOPT_POST, 1); |
| 450 | curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url); |
Shawn O. Pearce | aa90b96 | 2012-09-19 16:12:02 -0700 | [diff] [blame] | 451 | curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL); |
Shawn O. Pearce | 206b099 | 2011-02-15 08:57:24 -0800 | [diff] [blame] | 452 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000"); |
| 453 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4); |
| 454 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers); |
| 455 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer); |
| 456 | curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf); |
| 457 | |
Jeff King | 3a347ed | 2013-10-31 02:36:26 -0400 | [diff] [blame] | 458 | err = run_slot(slot, results); |
Shawn O. Pearce | 206b099 | 2011-02-15 08:57:24 -0800 | [diff] [blame] | 459 | |
| 460 | curl_slist_free_all(headers); |
| 461 | strbuf_release(&buf); |
| 462 | return err; |
| 463 | } |
| 464 | |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 465 | static int post_rpc(struct rpc_state *rpc) |
| 466 | { |
| 467 | struct active_request_slot *slot; |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 468 | struct curl_slist *headers = NULL; |
Shawn O. Pearce | b853860 | 2009-10-30 17:47:43 -0700 | [diff] [blame] | 469 | int use_gzip = rpc->gzip_request; |
| 470 | char *gzip_body = NULL; |
Ramsay Jones | 3771154 | 2012-11-21 19:08:51 +0000 | [diff] [blame] | 471 | size_t gzip_size = 0; |
Shawn O. Pearce | 206b099 | 2011-02-15 08:57:24 -0800 | [diff] [blame] | 472 | int err, large_request = 0; |
Brian M. Carlson | c80d96c | 2013-10-31 02:36:51 -0400 | [diff] [blame] | 473 | int needs_100_continue = 0; |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 474 | |
| 475 | /* Try to load the entire request, if we can fit it into the |
| 476 | * allocated buffer space we can use HTTP/1.0 and avoid the |
| 477 | * chunked encoding mess. |
| 478 | */ |
| 479 | while (1) { |
| 480 | size_t left = rpc->alloc - rpc->len; |
| 481 | char *buf = rpc->buf + rpc->len; |
| 482 | int n; |
| 483 | |
| 484 | if (left < LARGE_PACKET_MAX) { |
| 485 | large_request = 1; |
Shawn O. Pearce | b853860 | 2009-10-30 17:47:43 -0700 | [diff] [blame] | 486 | use_gzip = 0; |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 487 | break; |
| 488 | } |
| 489 | |
Jeff King | 4981fe7 | 2013-02-23 17:31:34 -0500 | [diff] [blame] | 490 | n = packet_read(rpc->out, NULL, NULL, buf, left, 0); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 491 | if (!n) |
| 492 | break; |
| 493 | rpc->len += n; |
| 494 | } |
| 495 | |
Shawn O. Pearce | 206b099 | 2011-02-15 08:57:24 -0800 | [diff] [blame] | 496 | if (large_request) { |
Brian M. Carlson | c80d96c | 2013-10-31 02:36:51 -0400 | [diff] [blame] | 497 | struct slot_results results; |
| 498 | |
Jeff King | b81401c | 2012-08-27 09:27:15 -0400 | [diff] [blame] | 499 | do { |
Brian M. Carlson | c80d96c | 2013-10-31 02:36:51 -0400 | [diff] [blame] | 500 | err = probe_rpc(rpc, &results); |
Jeff King | 2501aff | 2013-09-28 04:31:45 -0400 | [diff] [blame] | 501 | if (err == HTTP_REAUTH) |
| 502 | credential_fill(&http_auth); |
Jeff King | b81401c | 2012-08-27 09:27:15 -0400 | [diff] [blame] | 503 | } while (err == HTTP_REAUTH); |
| 504 | if (err != HTTP_OK) |
| 505 | return -1; |
Brian M. Carlson | c80d96c | 2013-10-31 02:36:51 -0400 | [diff] [blame] | 506 | |
| 507 | if (results.auth_avail & CURLAUTH_GSSNEGOTIATE) |
| 508 | needs_100_continue = 1; |
Shawn O. Pearce | 206b099 | 2011-02-15 08:57:24 -0800 | [diff] [blame] | 509 | } |
| 510 | |
Jeff King | abf8df8 | 2012-10-12 03:35:33 -0400 | [diff] [blame] | 511 | headers = curl_slist_append(headers, rpc->hdr_content_type); |
| 512 | headers = curl_slist_append(headers, rpc->hdr_accept); |
Brian M. Carlson | c80d96c | 2013-10-31 02:36:51 -0400 | [diff] [blame] | 513 | headers = curl_slist_append(headers, needs_100_continue ? |
| 514 | "Expect: 100-continue" : "Expect:"); |
Jeff King | abf8df8 | 2012-10-12 03:35:33 -0400 | [diff] [blame] | 515 | |
| 516 | retry: |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 517 | slot = get_active_slot(); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 518 | |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 519 | curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0); |
Martin Storsjö | d21f979 | 2009-11-23 11:03:28 +0800 | [diff] [blame] | 520 | curl_easy_setopt(slot->curl, CURLOPT_POST, 1); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 521 | curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url); |
Shawn O. Pearce | aa90b96 | 2012-09-19 16:12:02 -0700 | [diff] [blame] | 522 | curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip"); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 523 | |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 524 | if (large_request) { |
| 525 | /* The request body is large and the size cannot be predicted. |
| 526 | * We must use chunked encoding to send it. |
| 527 | */ |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 528 | headers = curl_slist_append(headers, "Transfer-Encoding: chunked"); |
Martin Storsjö | 6c81a99 | 2009-12-01 12:33:39 +0200 | [diff] [blame] | 529 | rpc->initial_buffer = 1; |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 530 | curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out); |
| 531 | curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc); |
Martin Storsjö | 6c81a99 | 2009-12-01 12:33:39 +0200 | [diff] [blame] | 532 | #ifndef NO_CURL_IOCTL |
| 533 | curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl); |
| 534 | curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc); |
| 535 | #endif |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 536 | if (options.verbosity > 1) { |
| 537 | fprintf(stderr, "POST %s (chunked)\n", rpc->service_name); |
| 538 | fflush(stderr); |
| 539 | } |
| 540 | |
Jeff King | 2e736fd | 2012-10-31 07:29:16 -0400 | [diff] [blame] | 541 | } else if (gzip_body) { |
| 542 | /* |
| 543 | * If we are looping to retry authentication, then the previous |
| 544 | * run will have set up the headers and gzip buffer already, |
| 545 | * and we just need to send it. |
| 546 | */ |
| 547 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body); |
| 548 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size); |
| 549 | |
Shawn O. Pearce | b853860 | 2009-10-30 17:47:43 -0700 | [diff] [blame] | 550 | } else if (use_gzip && 1024 < rpc->len) { |
| 551 | /* The client backend isn't giving us compressed data so |
| 552 | * we can try to deflate it ourselves, this may save on. |
| 553 | * the transfer time. |
| 554 | */ |
Junio C Hamano | ef49a7a | 2011-06-10 11:52:15 -0700 | [diff] [blame] | 555 | git_zstream stream; |
Shawn O. Pearce | b853860 | 2009-10-30 17:47:43 -0700 | [diff] [blame] | 556 | int ret; |
| 557 | |
| 558 | memset(&stream, 0, sizeof(stream)); |
Junio C Hamano | 55bb5c9 | 2011-06-10 10:55:10 -0700 | [diff] [blame] | 559 | git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION); |
Jeff King | df126e1 | 2012-10-31 07:20:15 -0400 | [diff] [blame] | 560 | gzip_size = git_deflate_bound(&stream, rpc->len); |
| 561 | gzip_body = xmalloc(gzip_size); |
Shawn O. Pearce | b853860 | 2009-10-30 17:47:43 -0700 | [diff] [blame] | 562 | |
| 563 | stream.next_in = (unsigned char *)rpc->buf; |
| 564 | stream.avail_in = rpc->len; |
| 565 | stream.next_out = (unsigned char *)gzip_body; |
Jeff King | df126e1 | 2012-10-31 07:20:15 -0400 | [diff] [blame] | 566 | stream.avail_out = gzip_size; |
Shawn O. Pearce | b853860 | 2009-10-30 17:47:43 -0700 | [diff] [blame] | 567 | |
Junio C Hamano | 55bb5c9 | 2011-06-10 10:55:10 -0700 | [diff] [blame] | 568 | ret = git_deflate(&stream, Z_FINISH); |
Shawn O. Pearce | b853860 | 2009-10-30 17:47:43 -0700 | [diff] [blame] | 569 | if (ret != Z_STREAM_END) |
| 570 | die("cannot deflate request; zlib deflate error %d", ret); |
| 571 | |
Junio C Hamano | 55bb5c9 | 2011-06-10 10:55:10 -0700 | [diff] [blame] | 572 | ret = git_deflate_end_gently(&stream); |
Shawn O. Pearce | b853860 | 2009-10-30 17:47:43 -0700 | [diff] [blame] | 573 | if (ret != Z_OK) |
| 574 | die("cannot deflate request; zlib end error %d", ret); |
| 575 | |
Jeff King | df126e1 | 2012-10-31 07:20:15 -0400 | [diff] [blame] | 576 | gzip_size = stream.total_out; |
Shawn O. Pearce | b853860 | 2009-10-30 17:47:43 -0700 | [diff] [blame] | 577 | |
| 578 | headers = curl_slist_append(headers, "Content-Encoding: gzip"); |
| 579 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body); |
Jeff King | df126e1 | 2012-10-31 07:20:15 -0400 | [diff] [blame] | 580 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size); |
Shawn O. Pearce | b853860 | 2009-10-30 17:47:43 -0700 | [diff] [blame] | 581 | |
| 582 | if (options.verbosity > 1) { |
| 583 | fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n", |
| 584 | rpc->service_name, |
Jeff King | df126e1 | 2012-10-31 07:20:15 -0400 | [diff] [blame] | 585 | (unsigned long)rpc->len, (unsigned long)gzip_size); |
Shawn O. Pearce | b853860 | 2009-10-30 17:47:43 -0700 | [diff] [blame] | 586 | fflush(stderr); |
| 587 | } |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 588 | } else { |
| 589 | /* We know the complete request size in advance, use the |
| 590 | * more normal Content-Length approach. |
| 591 | */ |
| 592 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf); |
| 593 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len); |
| 594 | if (options.verbosity > 1) { |
| 595 | fprintf(stderr, "POST %s (%lu bytes)\n", |
| 596 | rpc->service_name, (unsigned long)rpc->len); |
| 597 | fflush(stderr); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers); |
| 602 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in); |
| 603 | curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc); |
| 604 | |
Jeff King | 3a347ed | 2013-10-31 02:36:26 -0400 | [diff] [blame] | 605 | err = run_slot(slot, NULL); |
Jeff King | 2501aff | 2013-09-28 04:31:45 -0400 | [diff] [blame] | 606 | if (err == HTTP_REAUTH && !large_request) { |
| 607 | credential_fill(&http_auth); |
Jeff King | abf8df8 | 2012-10-12 03:35:33 -0400 | [diff] [blame] | 608 | goto retry; |
Jeff King | 2501aff | 2013-09-28 04:31:45 -0400 | [diff] [blame] | 609 | } |
Jeff King | b81401c | 2012-08-27 09:27:15 -0400 | [diff] [blame] | 610 | if (err != HTTP_OK) |
| 611 | err = -1; |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 612 | |
| 613 | curl_slist_free_all(headers); |
Shawn O. Pearce | b853860 | 2009-10-30 17:47:43 -0700 | [diff] [blame] | 614 | free(gzip_body); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 615 | return err; |
| 616 | } |
| 617 | |
| 618 | static int rpc_service(struct rpc_state *rpc, struct discovery *heads) |
| 619 | { |
| 620 | const char *svc = rpc->service_name; |
| 621 | struct strbuf buf = STRBUF_INIT; |
Ivan Todoroski | 8150749 | 2012-04-02 17:14:44 +0200 | [diff] [blame] | 622 | struct strbuf *preamble = rpc->stdin_preamble; |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 623 | struct child_process client; |
| 624 | int err = 0; |
| 625 | |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 626 | memset(&client, 0, sizeof(client)); |
| 627 | client.in = -1; |
| 628 | client.out = -1; |
| 629 | client.git_cmd = 1; |
| 630 | client.argv = rpc->argv; |
| 631 | if (start_command(&client)) |
| 632 | exit(1); |
Ivan Todoroski | 8150749 | 2012-04-02 17:14:44 +0200 | [diff] [blame] | 633 | if (preamble) |
| 634 | write_or_die(client.in, preamble->buf, preamble->len); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 635 | if (heads) |
| 636 | write_or_die(client.in, heads->buf, heads->len); |
| 637 | |
| 638 | rpc->alloc = http_post_buffer; |
| 639 | rpc->buf = xmalloc(rpc->alloc); |
| 640 | rpc->in = client.in; |
| 641 | rpc->out = client.out; |
| 642 | strbuf_init(&rpc->result, 0); |
| 643 | |
Jeff King | b227bbc | 2013-09-28 04:35:25 -0400 | [diff] [blame] | 644 | strbuf_addf(&buf, "%s%s", url.buf, svc); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 645 | rpc->service_url = strbuf_detach(&buf, NULL); |
| 646 | |
| 647 | strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc); |
| 648 | rpc->hdr_content_type = strbuf_detach(&buf, NULL); |
| 649 | |
Shawn O. Pearce | 8efa5f6 | 2010-01-12 09:54:04 -0800 | [diff] [blame] | 650 | strbuf_addf(&buf, "Accept: application/x-%s-result", svc); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 651 | rpc->hdr_accept = strbuf_detach(&buf, NULL); |
| 652 | |
| 653 | while (!err) { |
Jeff King | 4981fe7 | 2013-02-23 17:31:34 -0500 | [diff] [blame] | 654 | 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] | 655 | if (!n) |
| 656 | break; |
| 657 | rpc->pos = 0; |
| 658 | rpc->len = n; |
| 659 | err |= post_rpc(rpc); |
| 660 | } |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 661 | |
| 662 | close(client.in); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 663 | client.in = -1; |
Shawn O. Pearce | 6cdf022 | 2011-10-04 16:20:19 -0700 | [diff] [blame] | 664 | if (!err) { |
| 665 | strbuf_read(&rpc->result, client.out, 0); |
| 666 | } else { |
| 667 | char buf[4096]; |
| 668 | for (;;) |
| 669 | if (xread(client.out, buf, sizeof(buf)) <= 0) |
| 670 | break; |
| 671 | } |
Shawn O. Pearce | b4ee10f | 2010-08-06 14:19:44 -0700 | [diff] [blame] | 672 | |
| 673 | close(client.out); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 674 | client.out = -1; |
| 675 | |
| 676 | err |= finish_command(&client); |
| 677 | free(rpc->service_url); |
| 678 | free(rpc->hdr_content_type); |
| 679 | free(rpc->hdr_accept); |
| 680 | free(rpc->buf); |
| 681 | strbuf_release(&buf); |
| 682 | return err; |
| 683 | } |
| 684 | |
Shawn O. Pearce | 292ce46 | 2009-10-30 17:47:28 -0700 | [diff] [blame] | 685 | static int fetch_dumb(int nr_heads, struct ref **to_fetch) |
| 686 | { |
Tay Ray Chuan | 26e1e0b | 2010-03-02 18:49:31 +0800 | [diff] [blame] | 687 | struct walker *walker; |
Shawn O. Pearce | 292ce46 | 2009-10-30 17:47:28 -0700 | [diff] [blame] | 688 | char **targets = xmalloc(nr_heads * sizeof(char*)); |
| 689 | int ret, i; |
| 690 | |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 691 | if (options.depth) |
| 692 | die("dumb http transport does not support --depth"); |
Shawn O. Pearce | 292ce46 | 2009-10-30 17:47:28 -0700 | [diff] [blame] | 693 | for (i = 0; i < nr_heads; i++) |
| 694 | targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1)); |
| 695 | |
Jeff King | b227bbc | 2013-09-28 04:35:25 -0400 | [diff] [blame] | 696 | walker = get_http_walker(url.buf); |
Shawn O. Pearce | 292ce46 | 2009-10-30 17:47:28 -0700 | [diff] [blame] | 697 | walker->get_all = 1; |
| 698 | walker->get_tree = 1; |
| 699 | walker->get_history = 1; |
Shawn O. Pearce | ef08ef9 | 2009-10-30 17:47:29 -0700 | [diff] [blame] | 700 | walker->get_verbosely = options.verbosity >= 3; |
Shawn O. Pearce | 292ce46 | 2009-10-30 17:47:28 -0700 | [diff] [blame] | 701 | walker->get_recover = 0; |
| 702 | ret = walker_fetch(walker, nr_heads, targets, NULL, NULL); |
Tay Ray Chuan | 26e1e0b | 2010-03-02 18:49:31 +0800 | [diff] [blame] | 703 | walker_free(walker); |
Shawn O. Pearce | 292ce46 | 2009-10-30 17:47:28 -0700 | [diff] [blame] | 704 | |
| 705 | for (i = 0; i < nr_heads; i++) |
| 706 | free(targets[i]); |
| 707 | free(targets); |
| 708 | |
| 709 | return ret ? error("Fetch failed.") : 0; |
| 710 | } |
| 711 | |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 712 | static int fetch_git(struct discovery *heads, |
| 713 | int nr_heads, struct ref **to_fetch) |
| 714 | { |
| 715 | struct rpc_state rpc; |
Ivan Todoroski | 8150749 | 2012-04-02 17:14:44 +0200 | [diff] [blame] | 716 | struct strbuf preamble = STRBUF_INIT; |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 717 | char *depth_arg = NULL; |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 718 | int argc = 0, i, err; |
Nguyễn Thái Ngọc Duy | 1609488 | 2013-12-05 20:02:50 +0700 | [diff] [blame] | 719 | const char *argv[17]; |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 720 | |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 721 | argv[argc++] = "fetch-pack"; |
| 722 | argv[argc++] = "--stateless-rpc"; |
Ivan Todoroski | 8150749 | 2012-04-02 17:14:44 +0200 | [diff] [blame] | 723 | argv[argc++] = "--stdin"; |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 724 | argv[argc++] = "--lock-pack"; |
| 725 | if (options.followtags) |
| 726 | argv[argc++] = "--include-tag"; |
| 727 | if (options.thin) |
| 728 | argv[argc++] = "--thin"; |
| 729 | if (options.verbosity >= 3) { |
| 730 | argv[argc++] = "-v"; |
| 731 | argv[argc++] = "-v"; |
| 732 | } |
Nguyễn Thái Ngọc Duy | 9ba3804 | 2013-07-21 15:18:05 +0700 | [diff] [blame] | 733 | if (options.check_self_contained_and_connected) |
| 734 | argv[argc++] = "--check-self-contained-and-connected"; |
Nguyễn Thái Ngọc Duy | 1609488 | 2013-12-05 20:02:50 +0700 | [diff] [blame] | 735 | if (options.cloning) |
| 736 | argv[argc++] = "--cloning"; |
| 737 | if (options.update_shallow) |
| 738 | argv[argc++] = "--update-shallow"; |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 739 | if (!options.progress) |
| 740 | argv[argc++] = "--no-progress"; |
| 741 | if (options.depth) { |
| 742 | struct strbuf buf = STRBUF_INIT; |
| 743 | strbuf_addf(&buf, "--depth=%lu", options.depth); |
| 744 | depth_arg = strbuf_detach(&buf, NULL); |
| 745 | argv[argc++] = depth_arg; |
| 746 | } |
Jeff King | b227bbc | 2013-09-28 04:35:25 -0400 | [diff] [blame] | 747 | argv[argc++] = url.buf; |
Ivan Todoroski | 8150749 | 2012-04-02 17:14:44 +0200 | [diff] [blame] | 748 | argv[argc++] = NULL; |
| 749 | |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 750 | for (i = 0; i < nr_heads; i++) { |
| 751 | struct ref *ref = to_fetch[i]; |
| 752 | if (!ref->name || !*ref->name) |
| 753 | 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] | 754 | packet_buf_write(&preamble, "%s %s\n", |
| 755 | sha1_to_hex(ref->old_sha1), ref->name); |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 756 | } |
Ivan Todoroski | 8150749 | 2012-04-02 17:14:44 +0200 | [diff] [blame] | 757 | packet_buf_flush(&preamble); |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 758 | |
| 759 | memset(&rpc, 0, sizeof(rpc)); |
| 760 | rpc.service_name = "git-upload-pack", |
| 761 | rpc.argv = argv; |
Ivan Todoroski | 8150749 | 2012-04-02 17:14:44 +0200 | [diff] [blame] | 762 | rpc.stdin_preamble = &preamble; |
Shawn O. Pearce | b853860 | 2009-10-30 17:47:43 -0700 | [diff] [blame] | 763 | rpc.gzip_request = 1; |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 764 | |
| 765 | err = rpc_service(&rpc, heads); |
| 766 | if (rpc.result.len) |
Jeff King | cdf4fb8 | 2013-02-20 15:01:56 -0500 | [diff] [blame] | 767 | write_or_die(1, rpc.result.buf, rpc.result.len); |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 768 | strbuf_release(&rpc.result); |
Ivan Todoroski | 8150749 | 2012-04-02 17:14:44 +0200 | [diff] [blame] | 769 | strbuf_release(&preamble); |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 770 | free(depth_arg); |
| 771 | return err; |
| 772 | } |
| 773 | |
| 774 | static int fetch(int nr_heads, struct ref **to_fetch) |
| 775 | { |
Jeff King | 2a45520 | 2013-02-20 15:07:19 -0500 | [diff] [blame] | 776 | struct discovery *d = discover_refs("git-upload-pack", 0); |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 777 | if (d->proto_git) |
| 778 | return fetch_git(d, nr_heads, to_fetch); |
| 779 | else |
| 780 | return fetch_dumb(nr_heads, to_fetch); |
| 781 | } |
| 782 | |
Shawn O. Pearce | 292ce46 | 2009-10-30 17:47:28 -0700 | [diff] [blame] | 783 | static void parse_fetch(struct strbuf *buf) |
| 784 | { |
| 785 | struct ref **to_fetch = NULL; |
| 786 | struct ref *list_head = NULL; |
| 787 | struct ref **list = &list_head; |
| 788 | int alloc_heads = 0, nr_heads = 0; |
| 789 | |
| 790 | do { |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 791 | if (starts_with(buf->buf, "fetch ")) { |
Shawn O. Pearce | 292ce46 | 2009-10-30 17:47:28 -0700 | [diff] [blame] | 792 | char *p = buf->buf + strlen("fetch "); |
| 793 | char *name; |
| 794 | struct ref *ref; |
| 795 | unsigned char old_sha1[20]; |
| 796 | |
| 797 | if (strlen(p) < 40 || get_sha1_hex(p, old_sha1)) |
| 798 | die("protocol error: expected sha/ref, got %s'", p); |
| 799 | if (p[40] == ' ') |
| 800 | name = p + 41; |
| 801 | else if (!p[40]) |
| 802 | name = ""; |
| 803 | else |
| 804 | die("protocol error: expected sha/ref, got %s'", p); |
| 805 | |
| 806 | ref = alloc_ref(name); |
| 807 | hashcpy(ref->old_sha1, old_sha1); |
| 808 | |
| 809 | *list = ref; |
| 810 | list = &ref->next; |
| 811 | |
| 812 | ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads); |
| 813 | to_fetch[nr_heads++] = ref; |
| 814 | } |
| 815 | else |
| 816 | die("http transport does not support %s", buf->buf); |
| 817 | |
| 818 | strbuf_reset(buf); |
| 819 | if (strbuf_getline(buf, stdin, '\n') == EOF) |
| 820 | return; |
| 821 | if (!*buf->buf) |
| 822 | break; |
| 823 | } while (1); |
| 824 | |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 825 | if (fetch(nr_heads, to_fetch)) |
Shawn O. Pearce | 292ce46 | 2009-10-30 17:47:28 -0700 | [diff] [blame] | 826 | exit(128); /* error already reported */ |
| 827 | free_refs(list_head); |
| 828 | free(to_fetch); |
| 829 | |
| 830 | printf("\n"); |
| 831 | fflush(stdout); |
| 832 | strbuf_reset(buf); |
| 833 | } |
| 834 | |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 835 | static int push_dav(int nr_spec, char **specs) |
| 836 | { |
| 837 | const char **argv = xmalloc((10 + nr_spec) * sizeof(char*)); |
| 838 | int argc = 0, i; |
| 839 | |
| 840 | argv[argc++] = "http-push"; |
| 841 | argv[argc++] = "--helper-status"; |
| 842 | if (options.dry_run) |
| 843 | argv[argc++] = "--dry-run"; |
| 844 | if (options.verbosity > 1) |
| 845 | argv[argc++] = "--verbose"; |
Jeff King | b227bbc | 2013-09-28 04:35:25 -0400 | [diff] [blame] | 846 | argv[argc++] = url.buf; |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 847 | for (i = 0; i < nr_spec; i++) |
| 848 | argv[argc++] = specs[i]; |
| 849 | argv[argc++] = NULL; |
| 850 | |
| 851 | if (run_command_v_opt(argv, RUN_GIT_CMD)) |
| 852 | die("git-%s failed", argv[0]); |
| 853 | free(argv); |
| 854 | return 0; |
| 855 | } |
| 856 | |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 857 | static int push_git(struct discovery *heads, int nr_spec, char **specs) |
| 858 | { |
| 859 | struct rpc_state rpc; |
Junio C Hamano | 222b121 | 2013-07-08 22:16:31 -0700 | [diff] [blame] | 860 | int i, err; |
| 861 | struct argv_array args; |
Junio C Hamano | 05c1eb1 | 2013-08-02 15:14:50 -0700 | [diff] [blame] | 862 | struct string_list_item *cas_option; |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 863 | |
Junio C Hamano | 222b121 | 2013-07-08 22:16:31 -0700 | [diff] [blame] | 864 | argv_array_init(&args); |
| 865 | argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status", |
| 866 | NULL); |
| 867 | |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 868 | if (options.thin) |
Junio C Hamano | 222b121 | 2013-07-08 22:16:31 -0700 | [diff] [blame] | 869 | argv_array_push(&args, "--thin"); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 870 | if (options.dry_run) |
Junio C Hamano | 222b121 | 2013-07-08 22:16:31 -0700 | [diff] [blame] | 871 | argv_array_push(&args, "--dry-run"); |
Clemens Buchacher | c207e34 | 2012-01-08 22:06:20 +0100 | [diff] [blame] | 872 | if (options.verbosity == 0) |
Junio C Hamano | 222b121 | 2013-07-08 22:16:31 -0700 | [diff] [blame] | 873 | argv_array_push(&args, "--quiet"); |
Clemens Buchacher | c207e34 | 2012-01-08 22:06:20 +0100 | [diff] [blame] | 874 | else if (options.verbosity > 1) |
Junio C Hamano | 222b121 | 2013-07-08 22:16:31 -0700 | [diff] [blame] | 875 | argv_array_push(&args, "--verbose"); |
| 876 | argv_array_push(&args, options.progress ? "--progress" : "--no-progress"); |
Junio C Hamano | 05c1eb1 | 2013-08-02 15:14:50 -0700 | [diff] [blame] | 877 | for_each_string_list_item(cas_option, &cas_options) |
Junio C Hamano | 2233ad4 | 2013-09-09 14:30:29 -0700 | [diff] [blame] | 878 | argv_array_push(&args, cas_option->string); |
Jeff King | b227bbc | 2013-09-28 04:35:25 -0400 | [diff] [blame] | 879 | argv_array_push(&args, url.buf); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 880 | for (i = 0; i < nr_spec; i++) |
Junio C Hamano | 222b121 | 2013-07-08 22:16:31 -0700 | [diff] [blame] | 881 | argv_array_push(&args, specs[i]); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 882 | |
| 883 | memset(&rpc, 0, sizeof(rpc)); |
| 884 | rpc.service_name = "git-receive-pack", |
Junio C Hamano | 222b121 | 2013-07-08 22:16:31 -0700 | [diff] [blame] | 885 | rpc.argv = args.argv; |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 886 | |
| 887 | err = rpc_service(&rpc, heads); |
| 888 | if (rpc.result.len) |
Jeff King | cdf4fb8 | 2013-02-20 15:01:56 -0500 | [diff] [blame] | 889 | write_or_die(1, rpc.result.buf, rpc.result.len); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 890 | strbuf_release(&rpc.result); |
Junio C Hamano | 222b121 | 2013-07-08 22:16:31 -0700 | [diff] [blame] | 891 | argv_array_clear(&args); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 892 | return err; |
| 893 | } |
| 894 | |
| 895 | static int push(int nr_spec, char **specs) |
| 896 | { |
Jeff King | 2a45520 | 2013-02-20 15:07:19 -0500 | [diff] [blame] | 897 | struct discovery *heads = discover_refs("git-receive-pack", 1); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 898 | int ret; |
| 899 | |
| 900 | if (heads->proto_git) |
| 901 | ret = push_git(heads, nr_spec, specs); |
| 902 | else |
| 903 | ret = push_dav(nr_spec, specs); |
| 904 | free_discovery(heads); |
| 905 | return ret; |
| 906 | } |
| 907 | |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 908 | static void parse_push(struct strbuf *buf) |
| 909 | { |
| 910 | char **specs = NULL; |
Shawn O. Pearce | 5238cbf | 2012-01-19 19:12:09 -0800 | [diff] [blame] | 911 | int alloc_spec = 0, nr_spec = 0, i, ret; |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 912 | |
| 913 | do { |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 914 | if (starts_with(buf->buf, "push ")) { |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 915 | ALLOC_GROW(specs, nr_spec + 1, alloc_spec); |
| 916 | specs[nr_spec++] = xstrdup(buf->buf + 5); |
| 917 | } |
| 918 | else |
| 919 | die("http transport does not support %s", buf->buf); |
| 920 | |
| 921 | strbuf_reset(buf); |
| 922 | if (strbuf_getline(buf, stdin, '\n') == EOF) |
Jim Meyering | dc4cd76 | 2011-06-20 09:40:06 +0200 | [diff] [blame] | 923 | goto free_specs; |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 924 | if (!*buf->buf) |
| 925 | break; |
| 926 | } while (1); |
| 927 | |
Shawn O. Pearce | 5238cbf | 2012-01-19 19:12:09 -0800 | [diff] [blame] | 928 | ret = push(nr_spec, specs); |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 929 | printf("\n"); |
| 930 | fflush(stdout); |
Jim Meyering | dc4cd76 | 2011-06-20 09:40:06 +0200 | [diff] [blame] | 931 | |
Shawn O. Pearce | 5238cbf | 2012-01-19 19:12:09 -0800 | [diff] [blame] | 932 | if (ret) |
| 933 | exit(128); /* error already reported */ |
| 934 | |
Jim Meyering | dc4cd76 | 2011-06-20 09:40:06 +0200 | [diff] [blame] | 935 | free_specs: |
| 936 | for (i = 0; i < nr_spec; i++) |
| 937 | free(specs[i]); |
| 938 | free(specs); |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 939 | } |
| 940 | |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 941 | int main(int argc, const char **argv) |
| 942 | { |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 943 | struct strbuf buf = STRBUF_INIT; |
Daniel Barkalow | a45d3d7 | 2009-11-03 21:52:35 -0500 | [diff] [blame] | 944 | int nongit; |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 945 | |
Johannes Sixt | c6dfb39 | 2009-10-13 12:53:28 +0200 | [diff] [blame] | 946 | git_extract_argv0_path(argv[0]); |
Daniel Barkalow | a45d3d7 | 2009-11-03 21:52:35 -0500 | [diff] [blame] | 947 | setup_git_directory_gently(&nongit); |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 948 | if (argc < 2) { |
| 949 | fprintf(stderr, "Remote needed\n"); |
| 950 | return 1; |
| 951 | } |
| 952 | |
Shawn O. Pearce | ef08ef9 | 2009-10-30 17:47:29 -0700 | [diff] [blame] | 953 | options.verbosity = 1; |
| 954 | options.progress = !!isatty(2); |
Shawn O. Pearce | de1a2fd | 2009-10-30 17:47:41 -0700 | [diff] [blame] | 955 | options.thin = 1; |
Shawn O. Pearce | ef08ef9 | 2009-10-30 17:47:29 -0700 | [diff] [blame] | 956 | |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 957 | remote = remote_get(argv[1]); |
| 958 | |
| 959 | if (argc > 2) { |
Jeff King | b227bbc | 2013-09-28 04:35:25 -0400 | [diff] [blame] | 960 | end_url_with_slash(&url, argv[2]); |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 961 | } else { |
Jeff King | b227bbc | 2013-09-28 04:35:25 -0400 | [diff] [blame] | 962 | end_url_with_slash(&url, remote->url[0]); |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 963 | } |
| 964 | |
Jeff King | b227bbc | 2013-09-28 04:35:25 -0400 | [diff] [blame] | 965 | http_init(remote, url.buf, 0); |
Tay Ray Chuan | 888692b | 2010-03-02 18:49:29 +0800 | [diff] [blame] | 966 | |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 967 | do { |
Sverre Rabbelier | 1843f0c | 2011-07-16 15:03:29 +0200 | [diff] [blame] | 968 | if (strbuf_getline(&buf, stdin, '\n') == EOF) { |
| 969 | if (ferror(stdin)) |
| 970 | fprintf(stderr, "Error reading command stream\n"); |
| 971 | else |
| 972 | fprintf(stderr, "Unexpected end of command stream\n"); |
| 973 | return 1; |
| 974 | } |
| 975 | if (buf.len == 0) |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 976 | break; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 977 | if (starts_with(buf.buf, "fetch ")) { |
Daniel Barkalow | a45d3d7 | 2009-11-03 21:52:35 -0500 | [diff] [blame] | 978 | if (nongit) |
| 979 | die("Fetch attempted without a local repo"); |
Shawn O. Pearce | 292ce46 | 2009-10-30 17:47:28 -0700 | [diff] [blame] | 980 | parse_fetch(&buf); |
| 981 | |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 982 | } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) { |
Shawn O. Pearce | 97cc7bc | 2009-10-30 17:47:40 -0700 | [diff] [blame] | 983 | int for_push = !!strstr(buf.buf + 4, "for-push"); |
| 984 | output_refs(get_refs(for_push)); |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 985 | |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 986 | } else if (starts_with(buf.buf, "push ")) { |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 987 | parse_push(&buf); |
| 988 | |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 989 | } else if (starts_with(buf.buf, "option ")) { |
Shawn O. Pearce | ef08ef9 | 2009-10-30 17:47:29 -0700 | [diff] [blame] | 990 | char *name = buf.buf + strlen("option "); |
| 991 | char *value = strchr(name, ' '); |
| 992 | int result; |
| 993 | |
| 994 | if (value) |
| 995 | *value++ = '\0'; |
| 996 | else |
| 997 | value = "true"; |
| 998 | |
| 999 | result = set_option(name, value); |
| 1000 | if (!result) |
| 1001 | printf("ok\n"); |
| 1002 | else if (result < 0) |
| 1003 | printf("error invalid value\n"); |
| 1004 | else |
| 1005 | printf("unsupported\n"); |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 1006 | fflush(stdout); |
Shawn O. Pearce | ef08ef9 | 2009-10-30 17:47:29 -0700 | [diff] [blame] | 1007 | |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 1008 | } else if (!strcmp(buf.buf, "capabilities")) { |
| 1009 | printf("fetch\n"); |
Shawn O. Pearce | ef08ef9 | 2009-10-30 17:47:29 -0700 | [diff] [blame] | 1010 | printf("option\n"); |
Shawn O. Pearce | ae4efe1 | 2009-10-30 17:47:30 -0700 | [diff] [blame] | 1011 | printf("push\n"); |
Nguyễn Thái Ngọc Duy | 9ba3804 | 2013-07-21 15:18:05 +0700 | [diff] [blame] | 1012 | printf("check-connectivity\n"); |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 1013 | printf("\n"); |
| 1014 | fflush(stdout); |
| 1015 | } else { |
Sverre Rabbelier | 1843f0c | 2011-07-16 15:03:29 +0200 | [diff] [blame] | 1016 | fprintf(stderr, "Unknown command '%s'\n", buf.buf); |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 1017 | return 1; |
| 1018 | } |
| 1019 | strbuf_reset(&buf); |
| 1020 | } while (1); |
Tay Ray Chuan | 888692b | 2010-03-02 18:49:29 +0800 | [diff] [blame] | 1021 | |
| 1022 | http_cleanup(); |
| 1023 | |
Daniel Barkalow | a2d725b | 2009-08-05 01:01:56 -0400 | [diff] [blame] | 1024 | return 0; |
| 1025 | } |