]>
Commit | Line | Data |
---|---|---|
731043fd | 1 | #include "git-compat-util.h" |
f7192598 | 2 | #include "cache.h" |
41cb7488 | 3 | #include "pkt-line.h" |
b10d0ec7 | 4 | #include "quote.h" |
6abf5c0c | 5 | #include "refs.h" |
15a1c012 | 6 | #include "run-command.h" |
6b62816c | 7 | #include "remote.h" |
47a59185 | 8 | #include "connect.h" |
9d2e9420 | 9 | #include "url.h" |
f7192598 | 10 | |
96f1e58f | 11 | static char *server_capabilities; |
211b5f9e | 12 | |
2718ff09 LT |
13 | static int check_ref(const char *name, int len, unsigned int flags) |
14 | { | |
15 | if (!flags) | |
16 | return 1; | |
17 | ||
c41e20b3 | 18 | if (len < 5 || memcmp(name, "refs/", 5)) |
2718ff09 LT |
19 | return 0; |
20 | ||
21 | /* Skip the "refs/" part */ | |
22 | name += 5; | |
23 | len -= 5; | |
24 | ||
25 | /* REF_NORMAL means that we don't want the magic fake tag refs */ | |
8d9c5010 | 26 | if ((flags & REF_NORMAL) && check_refname_format(name, 0)) |
2718ff09 LT |
27 | return 0; |
28 | ||
29 | /* REF_HEADS means that we want regular branch heads */ | |
30 | if ((flags & REF_HEADS) && !memcmp(name, "heads/", 6)) | |
31 | return 1; | |
32 | ||
33 | /* REF_TAGS means that we want tags */ | |
34 | if ((flags & REF_TAGS) && !memcmp(name, "tags/", 5)) | |
35 | return 1; | |
36 | ||
37 | /* All type bits clear means that we are ok with anything */ | |
38 | return !(flags & ~REF_NORMAL); | |
39 | } | |
40 | ||
4577370e DB |
41 | int check_ref_type(const struct ref *ref, int flags) |
42 | { | |
43 | return check_ref(ref->name, strlen(ref->name), flags); | |
44 | } | |
45 | ||
40c155ff JH |
46 | static void add_extra_have(struct extra_have_objects *extra, unsigned char *sha1) |
47 | { | |
48 | ALLOC_GROW(extra->array, extra->nr + 1, extra->alloc); | |
49 | hashcpy(&(extra->array[extra->nr][0]), sha1); | |
50 | extra->nr++; | |
51 | } | |
52 | ||
46284dd1 HV |
53 | static void die_initial_contact(int got_at_least_one_head) |
54 | { | |
55 | if (got_at_least_one_head) | |
56 | die("The remote end hung up upon initial contact"); | |
57 | else | |
58 | die("Could not read from remote repository.\n\n" | |
59 | "Please make sure you have the correct access rights\n" | |
60 | "and the repository exists."); | |
61 | } | |
62 | ||
d1c133f5 LT |
63 | /* |
64 | * Read all the refs from the other end | |
65 | */ | |
85edf4f5 JK |
66 | struct ref **get_remote_heads(int in, char *src_buf, size_t src_len, |
67 | struct ref **list, unsigned int flags, | |
40c155ff | 68 | struct extra_have_objects *extra_have) |
d1c133f5 | 69 | { |
46284dd1 HV |
70 | int got_at_least_one_head = 0; |
71 | ||
d1c133f5 LT |
72 | *list = NULL; |
73 | for (;;) { | |
74 | struct ref *ref; | |
75 | unsigned char old_sha1[20]; | |
d1c133f5 | 76 | char *name; |
211b5f9e | 77 | int len, name_len; |
74543a04 | 78 | char *buffer = packet_buffer; |
d1c133f5 | 79 | |
85edf4f5 | 80 | len = packet_read(in, &src_buf, &src_len, |
4981fe75 | 81 | packet_buffer, sizeof(packet_buffer), |
819b929d JK |
82 | PACKET_READ_GENTLE_ON_EOF | |
83 | PACKET_READ_CHOMP_NEWLINE); | |
46284dd1 HV |
84 | if (len < 0) |
85 | die_initial_contact(got_at_least_one_head); | |
86 | ||
d1c133f5 LT |
87 | if (!len) |
88 | break; | |
d1c133f5 | 89 | |
a8073289 TPW |
90 | if (len > 4 && !prefixcmp(buffer, "ERR ")) |
91 | die("remote error: %s", buffer + 4); | |
92 | ||
d1c133f5 LT |
93 | if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ') |
94 | die("protocol error: expected sha/ref, got '%s'", buffer); | |
95 | name = buffer + 41; | |
1a7141ff | 96 | |
211b5f9e JS |
97 | name_len = strlen(name); |
98 | if (len != name_len + 41) { | |
8e0f7003 | 99 | free(server_capabilities); |
9befac47 | 100 | server_capabilities = xstrdup(name + name_len + 1); |
211b5f9e JS |
101 | } |
102 | ||
40c155ff JH |
103 | if (extra_have && |
104 | name_len == 5 && !memcmp(".have", name, 5)) { | |
105 | add_extra_have(extra_have, old_sha1); | |
106 | continue; | |
107 | } | |
108 | ||
2718ff09 | 109 | if (!check_ref(name, name_len, flags)) |
cfee10a7 | 110 | continue; |
59c69c0c | 111 | ref = alloc_ref(buffer + 41); |
e702496e | 112 | hashcpy(ref->old_sha1, old_sha1); |
d1c133f5 LT |
113 | *list = ref; |
114 | list = &ref->next; | |
46284dd1 | 115 | got_at_least_one_head = 1; |
d1c133f5 LT |
116 | } |
117 | return list; | |
118 | } | |
119 | ||
94427108 | 120 | const char *parse_feature_value(const char *feature_list, const char *feature, int *lenp) |
f47182c8 JH |
121 | { |
122 | int len; | |
123 | ||
124 | if (!feature_list) | |
125 | return NULL; | |
126 | ||
127 | len = strlen(feature); | |
128 | while (*feature_list) { | |
129 | const char *found = strstr(feature_list, feature); | |
130 | if (!found) | |
131 | return NULL; | |
94427108 JK |
132 | if (feature_list == found || isspace(found[-1])) { |
133 | const char *value = found + len; | |
134 | /* feature with no value (e.g., "thin-pack") */ | |
135 | if (!*value || isspace(*value)) { | |
136 | if (lenp) | |
137 | *lenp = 0; | |
138 | return value; | |
139 | } | |
140 | /* feature with a value (e.g., "agent=git/1.2.3") */ | |
141 | else if (*value == '=') { | |
142 | value++; | |
143 | if (lenp) | |
144 | *lenp = strcspn(value, " \t\n"); | |
145 | return value; | |
146 | } | |
147 | /* | |
148 | * otherwise we matched a substring of another feature; | |
149 | * keep looking | |
150 | */ | |
151 | } | |
f47182c8 JH |
152 | feature_list = found + 1; |
153 | } | |
154 | return NULL; | |
211b5f9e JS |
155 | } |
156 | ||
94427108 JK |
157 | int parse_feature_request(const char *feature_list, const char *feature) |
158 | { | |
159 | return !!parse_feature_value(feature_list, feature, NULL); | |
160 | } | |
161 | ||
162 | const char *server_feature_value(const char *feature, int *len) | |
163 | { | |
164 | return parse_feature_value(server_capabilities, feature, len); | |
165 | } | |
166 | ||
167 | int server_supports(const char *feature) | |
168 | { | |
169 | return !!server_feature_value(feature, NULL); | |
170 | } | |
171 | ||
2386d658 LT |
172 | enum protocol { |
173 | PROTO_LOCAL = 1, | |
174 | PROTO_SSH, | |
4b05548f | 175 | PROTO_GIT |
2386d658 LT |
176 | }; |
177 | ||
178 | static enum protocol get_protocol(const char *name) | |
179 | { | |
180 | if (!strcmp(name, "ssh")) | |
181 | return PROTO_SSH; | |
182 | if (!strcmp(name, "git")) | |
183 | return PROTO_GIT; | |
c05186cc LT |
184 | if (!strcmp(name, "git+ssh")) |
185 | return PROTO_SSH; | |
186 | if (!strcmp(name, "ssh+git")) | |
187 | return PROTO_SSH; | |
72a4f4b6 LT |
188 | if (!strcmp(name, "file")) |
189 | return PROTO_LOCAL; | |
2386d658 LT |
190 | die("I don't handle protocol '%s'", name); |
191 | } | |
192 | ||
5ba88448 YH |
193 | #define STR_(s) # s |
194 | #define STR(s) STR_(s) | |
2386d658 | 195 | |
72a534da ML |
196 | static void get_host_and_port(char **host, const char **port) |
197 | { | |
198 | char *colon, *end; | |
199 | ||
200 | if (*host[0] == '[') { | |
201 | end = strchr(*host + 1, ']'); | |
202 | if (end) { | |
203 | *end = 0; | |
204 | end++; | |
205 | (*host)++; | |
206 | } else | |
207 | end = *host; | |
208 | } else | |
209 | end = *host; | |
210 | colon = strchr(end, ':'); | |
211 | ||
212 | if (colon) { | |
213 | *colon = 0; | |
214 | *port = colon + 1; | |
215 | } | |
216 | } | |
217 | ||
e47a8583 EW |
218 | static void enable_keepalive(int sockfd) |
219 | { | |
220 | int ka = 1; | |
221 | ||
222 | if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0) | |
223 | fprintf(stderr, "unable to set SO_KEEPALIVE on socket: %s\n", | |
224 | strerror(errno)); | |
225 | } | |
226 | ||
49744d63 | 227 | #ifndef NO_IPV6 |
4c505f71 | 228 | |
ba505322 AR |
229 | static const char *ai_name(const struct addrinfo *ai) |
230 | { | |
785a9857 BK |
231 | static char addr[NI_MAXHOST]; |
232 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0, | |
233 | NI_NUMERICHOST) != 0) | |
ba505322 | 234 | strcpy(addr, "(unknown)"); |
785a9857 | 235 | |
ba505322 AR |
236 | return addr; |
237 | } | |
238 | ||
5ad312be JL |
239 | /* |
240 | * Returns a connected socket() fd, or else die()s. | |
241 | */ | |
7841ce79 | 242 | static int git_tcp_connect_sock(char *host, int flags) |
2386d658 | 243 | { |
63a995b6 DZ |
244 | struct strbuf error_message = STRBUF_INIT; |
245 | int sockfd = -1; | |
554fe20d | 246 | const char *port = STR(DEFAULT_GIT_PORT); |
5ba88448 YH |
247 | struct addrinfo hints, *ai0, *ai; |
248 | int gai; | |
ba505322 | 249 | int cnt = 0; |
5ba88448 | 250 | |
72a534da ML |
251 | get_host_and_port(&host, &port); |
252 | if (!*port) | |
253 | port = "<none>"; | |
5ba88448 YH |
254 | |
255 | memset(&hints, 0, sizeof(hints)); | |
256 | hints.ai_socktype = SOCK_STREAM; | |
257 | hints.ai_protocol = IPPROTO_TCP; | |
258 | ||
7841ce79 MT |
259 | if (flags & CONNECT_VERBOSE) |
260 | fprintf(stderr, "Looking up %s ... ", host); | |
261 | ||
5ba88448 YH |
262 | gai = getaddrinfo(host, port, &hints, &ai); |
263 | if (gai) | |
608d48b2 | 264 | die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai)); |
5ba88448 | 265 | |
7841ce79 MT |
266 | if (flags & CONNECT_VERBOSE) |
267 | fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port); | |
268 | ||
e08afecd | 269 | for (ai0 = ai; ai; ai = ai->ai_next, cnt++) { |
5ad312be JL |
270 | sockfd = socket(ai->ai_family, |
271 | ai->ai_socktype, ai->ai_protocol); | |
63a995b6 DZ |
272 | if ((sockfd < 0) || |
273 | (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) { | |
274 | strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n", | |
275 | host, cnt, ai_name(ai), strerror(errno)); | |
276 | if (0 <= sockfd) | |
277 | close(sockfd); | |
5ba88448 YH |
278 | sockfd = -1; |
279 | continue; | |
2386d658 | 280 | } |
ba505322 AR |
281 | if (flags & CONNECT_VERBOSE) |
282 | fprintf(stderr, "%s ", ai_name(ai)); | |
5ba88448 | 283 | break; |
2386d658 LT |
284 | } |
285 | ||
5ba88448 | 286 | freeaddrinfo(ai0); |
2386d658 | 287 | |
2386d658 | 288 | if (sockfd < 0) |
63a995b6 | 289 | die("unable to connect to %s:\n%s", host, error_message.buf); |
5ba88448 | 290 | |
e47a8583 EW |
291 | enable_keepalive(sockfd); |
292 | ||
7841ce79 MT |
293 | if (flags & CONNECT_VERBOSE) |
294 | fprintf(stderr, "done.\n"); | |
295 | ||
63a995b6 DZ |
296 | strbuf_release(&error_message); |
297 | ||
5ad312be | 298 | return sockfd; |
2386d658 LT |
299 | } |
300 | ||
49744d63 | 301 | #else /* NO_IPV6 */ |
4c505f71 | 302 | |
5ad312be JL |
303 | /* |
304 | * Returns a connected socket() fd, or else die()s. | |
305 | */ | |
7841ce79 | 306 | static int git_tcp_connect_sock(char *host, int flags) |
4c505f71 | 307 | { |
7203a2d1 EFL |
308 | struct strbuf error_message = STRBUF_INIT; |
309 | int sockfd = -1; | |
72a534da ML |
310 | const char *port = STR(DEFAULT_GIT_PORT); |
311 | char *ep; | |
4c505f71 PA |
312 | struct hostent *he; |
313 | struct sockaddr_in sa; | |
314 | char **ap; | |
315 | unsigned int nport; | |
ba505322 | 316 | int cnt; |
4c505f71 | 317 | |
72a534da | 318 | get_host_and_port(&host, &port); |
4c505f71 | 319 | |
7841ce79 MT |
320 | if (flags & CONNECT_VERBOSE) |
321 | fprintf(stderr, "Looking up %s ... ", host); | |
322 | ||
4c505f71 PA |
323 | he = gethostbyname(host); |
324 | if (!he) | |
325 | die("Unable to look up %s (%s)", host, hstrerror(h_errno)); | |
326 | nport = strtoul(port, &ep, 10); | |
327 | if ( ep == port || *ep ) { | |
328 | /* Not numeric */ | |
329 | struct servent *se = getservbyname(port,"tcp"); | |
330 | if ( !se ) | |
d7530708 | 331 | die("Unknown port %s", port); |
4c505f71 PA |
332 | nport = se->s_port; |
333 | } | |
334 | ||
7841ce79 MT |
335 | if (flags & CONNECT_VERBOSE) |
336 | fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port); | |
337 | ||
ba505322 | 338 | for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) { |
4c505f71 PA |
339 | memset(&sa, 0, sizeof sa); |
340 | sa.sin_family = he->h_addrtype; | |
6573faff | 341 | sa.sin_port = htons(nport); |
c6164218 | 342 | memcpy(&sa.sin_addr, *ap, he->h_length); |
4c505f71 | 343 | |
7203a2d1 EFL |
344 | sockfd = socket(he->h_addrtype, SOCK_STREAM, 0); |
345 | if ((sockfd < 0) || | |
346 | connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) { | |
347 | strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n", | |
ba505322 AR |
348 | host, |
349 | cnt, | |
350 | inet_ntoa(*(struct in_addr *)&sa.sin_addr), | |
7203a2d1 EFL |
351 | strerror(errno)); |
352 | if (0 <= sockfd) | |
353 | close(sockfd); | |
4c505f71 PA |
354 | sockfd = -1; |
355 | continue; | |
356 | } | |
ba505322 AR |
357 | if (flags & CONNECT_VERBOSE) |
358 | fprintf(stderr, "%s ", | |
359 | inet_ntoa(*(struct in_addr *)&sa.sin_addr)); | |
4c505f71 PA |
360 | break; |
361 | } | |
362 | ||
363 | if (sockfd < 0) | |
7203a2d1 | 364 | die("unable to connect to %s:\n%s", host, error_message.buf); |
4c505f71 | 365 | |
e47a8583 EW |
366 | enable_keepalive(sockfd); |
367 | ||
7841ce79 MT |
368 | if (flags & CONNECT_VERBOSE) |
369 | fprintf(stderr, "done.\n"); | |
370 | ||
5ad312be JL |
371 | return sockfd; |
372 | } | |
373 | ||
374 | #endif /* NO_IPV6 */ | |
375 | ||
376 | ||
7841ce79 | 377 | static void git_tcp_connect(int fd[2], char *host, int flags) |
5ad312be | 378 | { |
7841ce79 | 379 | int sockfd = git_tcp_connect_sock(host, flags); |
5ad312be | 380 | |
4c505f71 | 381 | fd[0] = sockfd; |
ec587fde | 382 | fd[1] = dup(sockfd); |
4c505f71 PA |
383 | } |
384 | ||
4c505f71 | 385 | |
96f1e58f | 386 | static char *git_proxy_command; |
f8014776 | 387 | |
ef90d6d4 JS |
388 | static int git_proxy_command_options(const char *var, const char *value, |
389 | void *cb) | |
f8014776 | 390 | { |
e814bc4d | 391 | if (!strcmp(var, "core.gitproxy")) { |
c3df8568 YH |
392 | const char *for_pos; |
393 | int matchlen = -1; | |
394 | int hostlen; | |
15112c95 EFL |
395 | const char *rhost_name = cb; |
396 | int rhost_len = strlen(rhost_name); | |
c3df8568 | 397 | |
e814bc4d | 398 | if (git_proxy_command) |
f8014776 | 399 | return 0; |
c64b9ad0 JH |
400 | if (!value) |
401 | return config_error_nonbool(var); | |
e814bc4d JH |
402 | /* [core] |
403 | * ;# matches www.kernel.org as well | |
404 | * gitproxy = netcatter-1 for kernel.org | |
405 | * gitproxy = netcatter-2 for sample.xz | |
406 | * gitproxy = netcatter-default | |
407 | */ | |
c3df8568 | 408 | for_pos = strstr(value, " for "); |
e814bc4d JH |
409 | if (!for_pos) |
410 | /* matches everybody */ | |
411 | matchlen = strlen(value); | |
412 | else { | |
413 | hostlen = strlen(for_pos + 5); | |
414 | if (rhost_len < hostlen) | |
415 | matchlen = -1; | |
416 | else if (!strncmp(for_pos + 5, | |
417 | rhost_name + rhost_len - hostlen, | |
418 | hostlen) && | |
419 | ((rhost_len == hostlen) || | |
420 | rhost_name[rhost_len - hostlen -1] == '.')) | |
421 | matchlen = for_pos - value; | |
422 | else | |
423 | matchlen = -1; | |
424 | } | |
425 | if (0 <= matchlen) { | |
426 | /* core.gitproxy = none for kernel.org */ | |
a6080a0a | 427 | if (matchlen == 4 && |
e814bc4d JH |
428 | !memcmp(value, "none", 4)) |
429 | matchlen = 0; | |
182af834 | 430 | git_proxy_command = xmemdupz(value, matchlen); |
f8014776 | 431 | } |
e814bc4d | 432 | return 0; |
f8014776 PC |
433 | } |
434 | ||
ef90d6d4 | 435 | return git_default_config(var, value, cb); |
f8014776 PC |
436 | } |
437 | ||
e814bc4d | 438 | static int git_use_proxy(const char *host) |
f8014776 PC |
439 | { |
440 | git_proxy_command = getenv("GIT_PROXY_COMMAND"); | |
15112c95 | 441 | git_config(git_proxy_command_options, (void*)host); |
e814bc4d | 442 | return (git_proxy_command && *git_proxy_command); |
f8014776 PC |
443 | } |
444 | ||
5cbf8246 | 445 | static struct child_process *git_proxy_connect(int fd[2], char *host) |
f8014776 | 446 | { |
554fe20d | 447 | const char *port = STR(DEFAULT_GIT_PORT); |
5cbf8246 JK |
448 | const char **argv; |
449 | struct child_process *proxy; | |
f8014776 | 450 | |
72a534da | 451 | get_host_and_port(&host, &port); |
f8014776 | 452 | |
5cbf8246 | 453 | argv = xmalloc(sizeof(*argv) * 4); |
15a1c012 SP |
454 | argv[0] = git_proxy_command; |
455 | argv[1] = host; | |
456 | argv[2] = port; | |
457 | argv[3] = NULL; | |
5cbf8246 JK |
458 | proxy = xcalloc(1, sizeof(*proxy)); |
459 | proxy->argv = argv; | |
460 | proxy->in = -1; | |
461 | proxy->out = -1; | |
462 | if (start_command(proxy)) | |
15a1c012 | 463 | die("cannot start proxy %s", argv[0]); |
5cbf8246 JK |
464 | fd[0] = proxy->out; /* read from proxy stdout */ |
465 | fd[1] = proxy->in; /* write to proxy stdin */ | |
466 | return proxy; | |
f8014776 PC |
467 | } |
468 | ||
0f503d77 CC |
469 | #define MAX_CMD_LEN 1024 |
470 | ||
2af202be | 471 | static char *get_port(char *host) |
2e776665 LT |
472 | { |
473 | char *end; | |
474 | char *p = strchr(host, ':'); | |
475 | ||
476 | if (p) { | |
8f148253 RS |
477 | long port = strtol(p + 1, &end, 10); |
478 | if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) { | |
2e776665 LT |
479 | *p = '\0'; |
480 | return p+1; | |
481 | } | |
482 | } | |
483 | ||
484 | return NULL; | |
485 | } | |
486 | ||
fb32c917 JS |
487 | static struct child_process no_fork; |
488 | ||
f7192598 | 489 | /* |
fb32c917 JS |
490 | * This returns a dummy child_process if the transport protocol does not |
491 | * need fork(2), or a struct child_process object if it does. Once done, | |
492 | * finish the connection with finish_connect() with the value returned from | |
493 | * this function (it is safe to call finish_connect() with NULL to support | |
494 | * the former case). | |
f42a5c4e | 495 | * |
fb32c917 JS |
496 | * If it returns, the connect is successful; it just dies on errors (this |
497 | * will hopefully be changed in a libification effort, to return NULL when | |
498 | * the connection failed). | |
f7192598 | 499 | */ |
4577370e | 500 | struct child_process *git_connect(int fd[2], const char *url_orig, |
98158e9c | 501 | const char *prog, int flags) |
f7192598 | 502 | { |
9d2e9420 | 503 | char *url; |
8e76bf3f | 504 | char *host, *path; |
356bece0 YH |
505 | char *end; |
506 | int c; | |
5cbf8246 | 507 | struct child_process *conn = &no_fork; |
faea9ccb | 508 | enum protocol protocol = PROTO_LOCAL; |
da2a95b2 | 509 | int free_path = 0; |
2e776665 | 510 | char *port = NULL; |
f364cb88 JS |
511 | const char **arg; |
512 | struct strbuf cmd; | |
faea9ccb | 513 | |
f0b7367c JH |
514 | /* Without this we cannot rely on waitpid() to tell |
515 | * what happened to our children. | |
516 | */ | |
517 | signal(SIGCHLD, SIG_DFL); | |
518 | ||
9d2e9420 JK |
519 | if (is_url(url_orig)) |
520 | url = url_decode(url_orig); | |
521 | else | |
522 | url = xstrdup(url_orig); | |
523 | ||
faea9ccb | 524 | host = strstr(url, "://"); |
eeefa7c9 | 525 | if (host) { |
faea9ccb AE |
526 | *host = '\0'; |
527 | protocol = get_protocol(url); | |
528 | host += 3; | |
356bece0 YH |
529 | c = '/'; |
530 | } else { | |
f7192598 | 531 | host = url; |
356bece0 YH |
532 | c = ':'; |
533 | } | |
534 | ||
9aa5053d IL |
535 | /* |
536 | * Don't do destructive transforms with git:// as that | |
9517e6b8 | 537 | * protocol code does '[]' unwrapping of its own. |
9aa5053d | 538 | */ |
356bece0 YH |
539 | if (host[0] == '[') { |
540 | end = strchr(host + 1, ']'); | |
541 | if (end) { | |
9aa5053d IL |
542 | if (protocol != PROTO_GIT) { |
543 | *end = 0; | |
544 | host++; | |
545 | } | |
356bece0 | 546 | end++; |
356bece0 YH |
547 | } else |
548 | end = host; | |
549 | } else | |
550 | end = host; | |
551 | ||
552 | path = strchr(end, c); | |
be501813 | 553 | if (path && !has_dos_drive_prefix(end)) { |
72a4f4b6 | 554 | if (c == ':') { |
8d3d28f5 | 555 | if (host != url || path < strchrnul(host, '/')) { |
60003340 NTND |
556 | protocol = PROTO_SSH; |
557 | *path++ = '\0'; | |
558 | } else /* '/' in the host part, assume local path */ | |
559 | path = end; | |
72a4f4b6 LT |
560 | } |
561 | } else | |
562 | path = end; | |
2386d658 | 563 | |
faea9ccb AE |
564 | if (!path || !*path) |
565 | die("No path specified. See 'man git-pull' for valid url syntax"); | |
566 | ||
567 | /* | |
568 | * null-terminate hostname and point path to ~ for URL's like this: | |
569 | * ssh://host.xz/~user/repo | |
570 | */ | |
571 | if (protocol != PROTO_LOCAL && host != url) { | |
572 | char *ptr = path; | |
573 | if (path[1] == '~') | |
574 | path++; | |
da2a95b2 | 575 | else { |
9befac47 | 576 | path = xstrdup(ptr); |
da2a95b2 SH |
577 | free_path = 1; |
578 | } | |
faea9ccb AE |
579 | |
580 | *ptr = '\0'; | |
581 | } | |
582 | ||
2e776665 LT |
583 | /* |
584 | * Add support for ssh port: ssh://host.xy:<port>/... | |
585 | */ | |
586 | if (protocol == PROTO_SSH && host != url) | |
7acf4382 | 587 | port = get_port(end); |
2e776665 | 588 | |
f8014776 | 589 | if (protocol == PROTO_GIT) { |
5ad312be JL |
590 | /* These underlying connection commands die() if they |
591 | * cannot connect. | |
592 | */ | |
9befac47 | 593 | char *target_host = xstrdup(host); |
e814bc4d | 594 | if (git_use_proxy(host)) |
5cbf8246 | 595 | conn = git_proxy_connect(fd, host); |
da2a95b2 | 596 | else |
7841ce79 | 597 | git_tcp_connect(fd, host, flags); |
5ad312be JL |
598 | /* |
599 | * Separate original protocol components prog and path | |
73bb33a9 SP |
600 | * from extended host header with a NUL byte. |
601 | * | |
602 | * Note: Do not add any other headers here! Doing so | |
603 | * will cause older git-daemon servers to crash. | |
5ad312be JL |
604 | */ |
605 | packet_write(fd[1], | |
606 | "%s %s%chost=%s%c", | |
607 | prog, path, 0, | |
608 | target_host, 0); | |
609 | free(target_host); | |
4577370e | 610 | free(url); |
da2a95b2 SH |
611 | if (free_path) |
612 | free(path); | |
5cbf8246 | 613 | return conn; |
f8014776 | 614 | } |
2386d658 | 615 | |
98158e9c | 616 | conn = xcalloc(1, sizeof(*conn)); |
2e776665 | 617 | |
f364cb88 JS |
618 | strbuf_init(&cmd, MAX_CMD_LEN); |
619 | strbuf_addstr(&cmd, prog); | |
620 | strbuf_addch(&cmd, ' '); | |
621 | sq_quote_buf(&cmd, path); | |
622 | if (cmd.len >= MAX_CMD_LEN) | |
623 | die("command line too long"); | |
624 | ||
625 | conn->in = conn->out = -1; | |
36ad53ff | 626 | conn->argv = arg = xcalloc(7, sizeof(*arg)); |
f364cb88 JS |
627 | if (protocol == PROTO_SSH) { |
628 | const char *ssh = getenv("GIT_SSH"); | |
36ad53ff | 629 | int putty = ssh && strcasestr(ssh, "plink"); |
f364cb88 JS |
630 | if (!ssh) ssh = "ssh"; |
631 | ||
632 | *arg++ = ssh; | |
36ad53ff EY |
633 | if (putty && !strcasestr(ssh, "tortoiseplink")) |
634 | *arg++ = "-batch"; | |
f364cb88 | 635 | if (port) { |
36ad53ff EY |
636 | /* P is for PuTTY, p is for OpenSSH */ |
637 | *arg++ = putty ? "-P" : "-p"; | |
f364cb88 | 638 | *arg++ = port; |
4852f723 | 639 | } |
f364cb88 JS |
640 | *arg++ = host; |
641 | } | |
642 | else { | |
48a7c1c4 GB |
643 | /* remove repo-local variables from the environment */ |
644 | conn->env = local_repo_env; | |
4cfb2a44 | 645 | conn->use_shell = 1; |
016fb48b | 646 | } |
f364cb88 JS |
647 | *arg++ = cmd.buf; |
648 | *arg = NULL; | |
649 | ||
650 | if (start_command(conn)) | |
651 | die("unable to fork"); | |
652 | ||
653 | fd[0] = conn->out; /* read from child's stdout */ | |
654 | fd[1] = conn->in; /* write to child's stdin */ | |
655 | strbuf_release(&cmd); | |
4577370e | 656 | free(url); |
da2a95b2 SH |
657 | if (free_path) |
658 | free(path); | |
98158e9c | 659 | return conn; |
f7192598 LT |
660 | } |
661 | ||
7ffe853b JK |
662 | int git_connection_is_socket(struct child_process *conn) |
663 | { | |
664 | return conn == &no_fork; | |
665 | } | |
666 | ||
98158e9c | 667 | int finish_connect(struct child_process *conn) |
f7192598 | 668 | { |
f364cb88 | 669 | int code; |
7ffe853b | 670 | if (!conn || git_connection_is_socket(conn)) |
f42a5c4e FBH |
671 | return 0; |
672 | ||
f364cb88 JS |
673 | code = finish_command(conn); |
674 | free(conn->argv); | |
98158e9c | 675 | free(conn); |
f364cb88 | 676 | return code; |
f7192598 | 677 | } |