]>
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" |
f7192598 | 6 | #include <sys/wait.h> |
2386d658 LT |
7 | #include <sys/socket.h> |
8 | #include <netinet/in.h> | |
9 | #include <arpa/inet.h> | |
10 | #include <netdb.h> | |
e898081d | 11 | #include <signal.h> |
f7192598 | 12 | |
1f5881bb | 13 | static char *server_capabilities = NULL; |
211b5f9e | 14 | |
d1c133f5 LT |
15 | /* |
16 | * Read all the refs from the other end | |
17 | */ | |
1a7141ff JH |
18 | struct ref **get_remote_heads(int in, struct ref **list, |
19 | int nr_match, char **match, int ignore_funny) | |
d1c133f5 LT |
20 | { |
21 | *list = NULL; | |
22 | for (;;) { | |
23 | struct ref *ref; | |
24 | unsigned char old_sha1[20]; | |
25 | static char buffer[1000]; | |
26 | char *name; | |
211b5f9e | 27 | int len, name_len; |
d1c133f5 LT |
28 | |
29 | len = packet_read_line(in, buffer, sizeof(buffer)); | |
30 | if (!len) | |
31 | break; | |
32 | if (buffer[len-1] == '\n') | |
33 | buffer[--len] = 0; | |
34 | ||
35 | if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ') | |
36 | die("protocol error: expected sha/ref, got '%s'", buffer); | |
37 | name = buffer + 41; | |
1a7141ff | 38 | |
211b5f9e JS |
39 | name_len = strlen(name); |
40 | if (len != name_len + 41) { | |
41 | if (server_capabilities) | |
42 | free(server_capabilities); | |
43 | server_capabilities = strdup(name + name_len + 1); | |
44 | } | |
45 | ||
cfee10a7 JH |
46 | if (ignore_funny && 45 < len && !memcmp(name, "refs/", 5) && |
47 | check_ref_format(name + 5)) | |
48 | continue; | |
49 | ||
d1c133f5 LT |
50 | if (nr_match && !path_match(name, nr_match, match)) |
51 | continue; | |
f88395ac | 52 | ref = xcalloc(1, sizeof(*ref) + len - 40); |
d1c133f5 | 53 | memcpy(ref->old_sha1, old_sha1, 20); |
d1c133f5 | 54 | memcpy(ref->name, buffer + 41, len - 40); |
d1c133f5 LT |
55 | *list = ref; |
56 | list = &ref->next; | |
57 | } | |
58 | return list; | |
59 | } | |
60 | ||
211b5f9e JS |
61 | int server_supports(const char *feature) |
62 | { | |
1f5881bb JS |
63 | return server_capabilities && |
64 | strstr(server_capabilities, feature) != NULL; | |
211b5f9e JS |
65 | } |
66 | ||
41cb7488 LT |
67 | int get_ack(int fd, unsigned char *result_sha1) |
68 | { | |
69 | static char line[1000]; | |
70 | int len = packet_read_line(fd, line, sizeof(line)); | |
71 | ||
72 | if (!len) | |
73 | die("git-fetch-pack: expected ACK/NAK, got EOF"); | |
74 | if (line[len-1] == '\n') | |
75 | line[--len] = 0; | |
76 | if (!strcmp(line, "NAK")) | |
77 | return 0; | |
da2a95b2 | 78 | if (!strncmp(line, "ACK ", 4)) { |
c4c86f07 JS |
79 | if (!get_sha1_hex(line+4, result_sha1)) { |
80 | if (strstr(line+45, "continue")) | |
81 | return 2; | |
41cb7488 | 82 | return 1; |
c4c86f07 | 83 | } |
41cb7488 LT |
84 | } |
85 | die("git-fetch_pack: expected ACK/NAK, got '%s'", line); | |
86 | } | |
87 | ||
013e7c7f LT |
88 | int path_match(const char *path, int nr, char **match) |
89 | { | |
90 | int i; | |
91 | int pathlen = strlen(path); | |
92 | ||
93 | for (i = 0; i < nr; i++) { | |
94 | char *s = match[i]; | |
95 | int len = strlen(s); | |
96 | ||
97 | if (!len || len > pathlen) | |
98 | continue; | |
99 | if (memcmp(path + pathlen - len, s, len)) | |
100 | continue; | |
101 | if (pathlen > len && path[pathlen - len - 1] != '/') | |
102 | continue; | |
103 | *s = 0; | |
9546010b | 104 | return (i + 1); |
013e7c7f LT |
105 | } |
106 | return 0; | |
107 | } | |
108 | ||
f88395ac JH |
109 | struct refspec { |
110 | char *src; | |
111 | char *dst; | |
ff27adf3 | 112 | char force; |
f88395ac JH |
113 | }; |
114 | ||
ff27adf3 JH |
115 | /* |
116 | * A:B means fast forward remote B with local A. | |
117 | * +A:B means overwrite remote B with local A. | |
118 | * +A is a shorthand for +A:A. | |
119 | * A is a shorthand for A:A. | |
120 | */ | |
f88395ac JH |
121 | static struct refspec *parse_ref_spec(int nr_refspec, char **refspec) |
122 | { | |
123 | int i; | |
ff27adf3 | 124 | struct refspec *rs = xcalloc(sizeof(*rs), (nr_refspec + 1)); |
f88395ac JH |
125 | for (i = 0; i < nr_refspec; i++) { |
126 | char *sp, *dp, *ep; | |
127 | sp = refspec[i]; | |
ff27adf3 JH |
128 | if (*sp == '+') { |
129 | rs[i].force = 1; | |
130 | sp++; | |
131 | } | |
f88395ac JH |
132 | ep = strchr(sp, ':'); |
133 | if (ep) { | |
134 | dp = ep + 1; | |
135 | *ep = 0; | |
136 | } | |
137 | else | |
138 | dp = sp; | |
139 | rs[i].src = sp; | |
140 | rs[i].dst = dp; | |
141 | } | |
142 | rs[nr_refspec].src = rs[nr_refspec].dst = NULL; | |
143 | return rs; | |
144 | } | |
145 | ||
146 | static int count_refspec_match(const char *pattern, | |
147 | struct ref *refs, | |
148 | struct ref **matched_ref) | |
149 | { | |
150 | int match; | |
151 | int patlen = strlen(pattern); | |
152 | ||
153 | for (match = 0; refs; refs = refs->next) { | |
154 | char *name = refs->name; | |
155 | int namelen = strlen(name); | |
156 | if (namelen < patlen || | |
157 | memcmp(name + namelen - patlen, pattern, patlen)) | |
158 | continue; | |
159 | if (namelen != patlen && name[namelen - patlen - 1] != '/') | |
160 | continue; | |
161 | match++; | |
162 | *matched_ref = refs; | |
163 | } | |
164 | return match; | |
165 | } | |
166 | ||
167 | static void link_dst_tail(struct ref *ref, struct ref ***tail) | |
168 | { | |
169 | **tail = ref; | |
170 | *tail = &ref->next; | |
171 | **tail = NULL; | |
172 | } | |
173 | ||
15e02b37 JH |
174 | static struct ref *try_explicit_object_name(const char *name) |
175 | { | |
176 | unsigned char sha1[20]; | |
177 | struct ref *ref; | |
178 | int len; | |
179 | if (get_sha1(name, sha1)) | |
180 | return NULL; | |
181 | len = strlen(name) + 1; | |
182 | ref = xcalloc(1, sizeof(*ref) + len); | |
183 | memcpy(ref->name, name, len); | |
184 | memcpy(ref->new_sha1, sha1, 20); | |
185 | return ref; | |
186 | } | |
187 | ||
f88395ac JH |
188 | static int match_explicit_refs(struct ref *src, struct ref *dst, |
189 | struct ref ***dst_tail, struct refspec *rs) | |
190 | { | |
191 | int i, errs; | |
192 | for (i = errs = 0; rs[i].src; i++) { | |
193 | struct ref *matched_src, *matched_dst; | |
194 | ||
195 | matched_src = matched_dst = NULL; | |
196 | switch (count_refspec_match(rs[i].src, src, &matched_src)) { | |
197 | case 1: | |
198 | break; | |
199 | case 0: | |
15e02b37 JH |
200 | /* The source could be in the get_sha1() format |
201 | * not a reference name. | |
202 | */ | |
203 | matched_src = try_explicit_object_name(rs[i].src); | |
204 | if (matched_src) | |
205 | break; | |
f88395ac | 206 | errs = 1; |
4ec99bf0 TS |
207 | error("src refspec %s does not match any.", |
208 | rs[i].src); | |
f88395ac JH |
209 | break; |
210 | default: | |
211 | errs = 1; | |
212 | error("src refspec %s matches more than one.", | |
213 | rs[i].src); | |
214 | break; | |
215 | } | |
216 | switch (count_refspec_match(rs[i].dst, dst, &matched_dst)) { | |
217 | case 1: | |
218 | break; | |
219 | case 0: | |
220 | if (!memcmp(rs[i].dst, "refs/", 5)) { | |
221 | int len = strlen(rs[i].dst) + 1; | |
222 | matched_dst = xcalloc(1, sizeof(*dst) + len); | |
223 | memcpy(matched_dst->name, rs[i].dst, len); | |
224 | link_dst_tail(matched_dst, dst_tail); | |
225 | } | |
226 | else if (!strcmp(rs[i].src, rs[i].dst) && | |
227 | matched_src) { | |
228 | /* pushing "master:master" when | |
229 | * remote does not have master yet. | |
230 | */ | |
4fa1604f | 231 | int len = strlen(matched_src->name) + 1; |
f88395ac JH |
232 | matched_dst = xcalloc(1, sizeof(*dst) + len); |
233 | memcpy(matched_dst->name, matched_src->name, | |
234 | len); | |
235 | link_dst_tail(matched_dst, dst_tail); | |
236 | } | |
237 | else { | |
238 | errs = 1; | |
239 | error("dst refspec %s does not match any " | |
240 | "existing ref on the remote and does " | |
241 | "not start with refs/.", rs[i].dst); | |
242 | } | |
243 | break; | |
244 | default: | |
245 | errs = 1; | |
246 | error("dst refspec %s matches more than one.", | |
247 | rs[i].dst); | |
248 | break; | |
249 | } | |
250 | if (errs) | |
251 | continue; | |
f88395ac JH |
252 | if (matched_dst->peer_ref) { |
253 | errs = 1; | |
254 | error("dst ref %s receives from more than one src.", | |
255 | matched_dst->name); | |
256 | } | |
ff27adf3 | 257 | else { |
f88395ac | 258 | matched_dst->peer_ref = matched_src; |
ff27adf3 JH |
259 | matched_dst->force = rs[i].force; |
260 | } | |
f88395ac JH |
261 | } |
262 | return -errs; | |
263 | } | |
264 | ||
265 | static struct ref *find_ref_by_name(struct ref *list, const char *name) | |
266 | { | |
267 | for ( ; list; list = list->next) | |
268 | if (!strcmp(list->name, name)) | |
269 | return list; | |
270 | return NULL; | |
271 | } | |
272 | ||
273 | int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail, | |
274 | int nr_refspec, char **refspec, int all) | |
275 | { | |
276 | struct refspec *rs = parse_ref_spec(nr_refspec, refspec); | |
277 | ||
278 | if (nr_refspec) | |
279 | return match_explicit_refs(src, dst, dst_tail, rs); | |
280 | ||
281 | /* pick the remainder */ | |
282 | for ( ; src; src = src->next) { | |
283 | struct ref *dst_peer; | |
284 | if (src->peer_ref) | |
285 | continue; | |
286 | dst_peer = find_ref_by_name(dst, src->name); | |
635d37af | 287 | if ((dst_peer && dst_peer->peer_ref) || (!dst_peer && !all)) |
f88395ac JH |
288 | continue; |
289 | if (!dst_peer) { | |
f88395ac JH |
290 | /* Create a new one and link it */ |
291 | int len = strlen(src->name) + 1; | |
292 | dst_peer = xcalloc(1, sizeof(*dst_peer) + len); | |
293 | memcpy(dst_peer->name, src->name, len); | |
294 | memcpy(dst_peer->new_sha1, src->new_sha1, 20); | |
295 | link_dst_tail(dst_peer, dst_tail); | |
296 | } | |
297 | dst_peer->peer_ref = src; | |
298 | } | |
299 | return 0; | |
300 | } | |
301 | ||
2386d658 LT |
302 | enum protocol { |
303 | PROTO_LOCAL = 1, | |
304 | PROTO_SSH, | |
305 | PROTO_GIT, | |
306 | }; | |
307 | ||
308 | static enum protocol get_protocol(const char *name) | |
309 | { | |
310 | if (!strcmp(name, "ssh")) | |
311 | return PROTO_SSH; | |
312 | if (!strcmp(name, "git")) | |
313 | return PROTO_GIT; | |
c05186cc LT |
314 | if (!strcmp(name, "git+ssh")) |
315 | return PROTO_SSH; | |
316 | if (!strcmp(name, "ssh+git")) | |
317 | return PROTO_SSH; | |
2386d658 LT |
318 | die("I don't handle protocol '%s'", name); |
319 | } | |
320 | ||
5ba88448 YH |
321 | #define STR_(s) # s |
322 | #define STR(s) STR_(s) | |
2386d658 | 323 | |
49744d63 | 324 | #ifndef NO_IPV6 |
4c505f71 | 325 | |
5ad312be JL |
326 | /* |
327 | * Returns a connected socket() fd, or else die()s. | |
328 | */ | |
329 | static int git_tcp_connect_sock(char *host) | |
2386d658 | 330 | { |
5ba88448 YH |
331 | int sockfd = -1; |
332 | char *colon, *end; | |
333 | char *port = STR(DEFAULT_GIT_PORT); | |
334 | struct addrinfo hints, *ai0, *ai; | |
335 | int gai; | |
336 | ||
337 | if (host[0] == '[') { | |
338 | end = strchr(host + 1, ']'); | |
339 | if (end) { | |
340 | *end = 0; | |
341 | end++; | |
342 | host++; | |
343 | } else | |
344 | end = host; | |
345 | } else | |
346 | end = host; | |
347 | colon = strchr(end, ':'); | |
348 | ||
ce6f8e7e LT |
349 | if (colon) { |
350 | *colon = 0; | |
5ba88448 | 351 | port = colon + 1; |
ce6f8e7e | 352 | } |
5ba88448 YH |
353 | |
354 | memset(&hints, 0, sizeof(hints)); | |
355 | hints.ai_socktype = SOCK_STREAM; | |
356 | hints.ai_protocol = IPPROTO_TCP; | |
357 | ||
358 | gai = getaddrinfo(host, port, &hints, &ai); | |
359 | if (gai) | |
360 | die("Unable to look up %s (%s)", host, gai_strerror(gai)); | |
361 | ||
362 | for (ai0 = ai; ai; ai = ai->ai_next) { | |
5ad312be JL |
363 | sockfd = socket(ai->ai_family, |
364 | ai->ai_socktype, ai->ai_protocol); | |
5ba88448 YH |
365 | if (sockfd < 0) |
366 | continue; | |
367 | if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) { | |
368 | close(sockfd); | |
369 | sockfd = -1; | |
370 | continue; | |
2386d658 | 371 | } |
5ba88448 | 372 | break; |
2386d658 LT |
373 | } |
374 | ||
5ba88448 | 375 | freeaddrinfo(ai0); |
2386d658 | 376 | |
2386d658 | 377 | if (sockfd < 0) |
5ba88448 YH |
378 | die("unable to connect a socket (%s)", strerror(errno)); |
379 | ||
5ad312be | 380 | return sockfd; |
2386d658 LT |
381 | } |
382 | ||
49744d63 | 383 | #else /* NO_IPV6 */ |
4c505f71 | 384 | |
5ad312be JL |
385 | /* |
386 | * Returns a connected socket() fd, or else die()s. | |
387 | */ | |
388 | static int git_tcp_connect_sock(char *host) | |
4c505f71 PA |
389 | { |
390 | int sockfd = -1; | |
391 | char *colon, *end; | |
392 | char *port = STR(DEFAULT_GIT_PORT), *ep; | |
393 | struct hostent *he; | |
394 | struct sockaddr_in sa; | |
395 | char **ap; | |
396 | unsigned int nport; | |
397 | ||
398 | if (host[0] == '[') { | |
399 | end = strchr(host + 1, ']'); | |
400 | if (end) { | |
401 | *end = 0; | |
402 | end++; | |
403 | host++; | |
404 | } else | |
405 | end = host; | |
406 | } else | |
407 | end = host; | |
408 | colon = strchr(end, ':'); | |
409 | ||
410 | if (colon) { | |
411 | *colon = 0; | |
412 | port = colon + 1; | |
413 | } | |
414 | ||
4c505f71 PA |
415 | he = gethostbyname(host); |
416 | if (!he) | |
417 | die("Unable to look up %s (%s)", host, hstrerror(h_errno)); | |
418 | nport = strtoul(port, &ep, 10); | |
419 | if ( ep == port || *ep ) { | |
420 | /* Not numeric */ | |
421 | struct servent *se = getservbyname(port,"tcp"); | |
422 | if ( !se ) | |
423 | die("Unknown port %s\n", port); | |
424 | nport = se->s_port; | |
425 | } | |
426 | ||
427 | for (ap = he->h_addr_list; *ap; ap++) { | |
428 | sockfd = socket(he->h_addrtype, SOCK_STREAM, 0); | |
429 | if (sockfd < 0) | |
430 | continue; | |
431 | ||
432 | memset(&sa, 0, sizeof sa); | |
433 | sa.sin_family = he->h_addrtype; | |
6573faff | 434 | sa.sin_port = htons(nport); |
c6164218 | 435 | memcpy(&sa.sin_addr, *ap, he->h_length); |
4c505f71 PA |
436 | |
437 | if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) { | |
438 | close(sockfd); | |
439 | sockfd = -1; | |
440 | continue; | |
441 | } | |
442 | break; | |
443 | } | |
444 | ||
445 | if (sockfd < 0) | |
446 | die("unable to connect a socket (%s)", strerror(errno)); | |
447 | ||
5ad312be JL |
448 | return sockfd; |
449 | } | |
450 | ||
451 | #endif /* NO_IPV6 */ | |
452 | ||
453 | ||
454 | static void git_tcp_connect(int fd[2], | |
455 | const char *prog, char *host, char *path) | |
456 | { | |
457 | int sockfd = git_tcp_connect_sock(host); | |
458 | ||
4c505f71 PA |
459 | fd[0] = sockfd; |
460 | fd[1] = sockfd; | |
4c505f71 PA |
461 | } |
462 | ||
4c505f71 | 463 | |
f8014776 | 464 | static char *git_proxy_command = NULL; |
e814bc4d JH |
465 | static const char *rhost_name = NULL; |
466 | static int rhost_len; | |
f8014776 PC |
467 | |
468 | static int git_proxy_command_options(const char *var, const char *value) | |
469 | { | |
e814bc4d | 470 | if (!strcmp(var, "core.gitproxy")) { |
c3df8568 YH |
471 | const char *for_pos; |
472 | int matchlen = -1; | |
473 | int hostlen; | |
474 | ||
e814bc4d | 475 | if (git_proxy_command) |
f8014776 | 476 | return 0; |
e814bc4d JH |
477 | /* [core] |
478 | * ;# matches www.kernel.org as well | |
479 | * gitproxy = netcatter-1 for kernel.org | |
480 | * gitproxy = netcatter-2 for sample.xz | |
481 | * gitproxy = netcatter-default | |
482 | */ | |
c3df8568 | 483 | for_pos = strstr(value, " for "); |
e814bc4d JH |
484 | if (!for_pos) |
485 | /* matches everybody */ | |
486 | matchlen = strlen(value); | |
487 | else { | |
488 | hostlen = strlen(for_pos + 5); | |
489 | if (rhost_len < hostlen) | |
490 | matchlen = -1; | |
491 | else if (!strncmp(for_pos + 5, | |
492 | rhost_name + rhost_len - hostlen, | |
493 | hostlen) && | |
494 | ((rhost_len == hostlen) || | |
495 | rhost_name[rhost_len - hostlen -1] == '.')) | |
496 | matchlen = for_pos - value; | |
497 | else | |
498 | matchlen = -1; | |
499 | } | |
500 | if (0 <= matchlen) { | |
501 | /* core.gitproxy = none for kernel.org */ | |
502 | if (matchlen == 4 && | |
503 | !memcmp(value, "none", 4)) | |
504 | matchlen = 0; | |
505 | git_proxy_command = xmalloc(matchlen + 1); | |
506 | memcpy(git_proxy_command, value, matchlen); | |
507 | git_proxy_command[matchlen] = 0; | |
f8014776 | 508 | } |
e814bc4d | 509 | return 0; |
f8014776 PC |
510 | } |
511 | ||
512 | return git_default_config(var, value); | |
513 | } | |
514 | ||
e814bc4d | 515 | static int git_use_proxy(const char *host) |
f8014776 | 516 | { |
e814bc4d JH |
517 | rhost_name = host; |
518 | rhost_len = strlen(host); | |
f8014776 PC |
519 | git_proxy_command = getenv("GIT_PROXY_COMMAND"); |
520 | git_config(git_proxy_command_options); | |
e814bc4d JH |
521 | rhost_name = NULL; |
522 | return (git_proxy_command && *git_proxy_command); | |
f8014776 PC |
523 | } |
524 | ||
5ad312be JL |
525 | static void git_proxy_connect(int fd[2], |
526 | const char *prog, char *host, char *path) | |
f8014776 PC |
527 | { |
528 | char *port = STR(DEFAULT_GIT_PORT); | |
529 | char *colon, *end; | |
530 | int pipefd[2][2]; | |
531 | pid_t pid; | |
532 | ||
533 | if (host[0] == '[') { | |
534 | end = strchr(host + 1, ']'); | |
535 | if (end) { | |
536 | *end = 0; | |
537 | end++; | |
538 | host++; | |
539 | } else | |
540 | end = host; | |
541 | } else | |
542 | end = host; | |
543 | colon = strchr(end, ':'); | |
544 | ||
545 | if (colon) { | |
546 | *colon = 0; | |
547 | port = colon + 1; | |
548 | } | |
549 | ||
550 | if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0) | |
551 | die("unable to create pipe pair for communication"); | |
552 | pid = fork(); | |
553 | if (!pid) { | |
554 | dup2(pipefd[1][0], 0); | |
555 | dup2(pipefd[0][1], 1); | |
556 | close(pipefd[0][0]); | |
557 | close(pipefd[0][1]); | |
558 | close(pipefd[1][0]); | |
559 | close(pipefd[1][1]); | |
560 | execlp(git_proxy_command, git_proxy_command, host, port, NULL); | |
561 | die("exec failed"); | |
562 | } | |
5ad312be JL |
563 | if (pid < 0) |
564 | die("fork failed"); | |
f8014776 PC |
565 | fd[0] = pipefd[0][0]; |
566 | fd[1] = pipefd[1][1]; | |
567 | close(pipefd[0][1]); | |
568 | close(pipefd[1][0]); | |
f8014776 PC |
569 | } |
570 | ||
f7192598 LT |
571 | /* |
572 | * Yeah, yeah, fixme. Need to pass in the heads etc. | |
573 | */ | |
574 | int git_connect(int fd[2], char *url, const char *prog) | |
575 | { | |
576 | char command[1024]; | |
faea9ccb | 577 | char *host, *path = url; |
356bece0 YH |
578 | char *end; |
579 | int c; | |
f7192598 LT |
580 | int pipefd[2][2]; |
581 | pid_t pid; | |
faea9ccb | 582 | enum protocol protocol = PROTO_LOCAL; |
da2a95b2 | 583 | int free_path = 0; |
faea9ccb | 584 | |
f0b7367c JH |
585 | /* Without this we cannot rely on waitpid() to tell |
586 | * what happened to our children. | |
587 | */ | |
588 | signal(SIGCHLD, SIG_DFL); | |
589 | ||
faea9ccb AE |
590 | host = strstr(url, "://"); |
591 | if(host) { | |
592 | *host = '\0'; | |
593 | protocol = get_protocol(url); | |
594 | host += 3; | |
356bece0 YH |
595 | c = '/'; |
596 | } else { | |
f7192598 | 597 | host = url; |
356bece0 YH |
598 | c = ':'; |
599 | } | |
600 | ||
601 | if (host[0] == '[') { | |
602 | end = strchr(host + 1, ']'); | |
603 | if (end) { | |
604 | *end = 0; | |
605 | end++; | |
606 | host++; | |
607 | } else | |
608 | end = host; | |
609 | } else | |
610 | end = host; | |
611 | ||
612 | path = strchr(end, c); | |
613 | if (c == ':') { | |
614 | if (path) { | |
faea9ccb | 615 | protocol = PROTO_SSH; |
356bece0 YH |
616 | *path++ = '\0'; |
617 | } else | |
618 | path = host; | |
f7192598 | 619 | } |
2386d658 | 620 | |
faea9ccb AE |
621 | if (!path || !*path) |
622 | die("No path specified. See 'man git-pull' for valid url syntax"); | |
623 | ||
624 | /* | |
625 | * null-terminate hostname and point path to ~ for URL's like this: | |
626 | * ssh://host.xz/~user/repo | |
627 | */ | |
628 | if (protocol != PROTO_LOCAL && host != url) { | |
629 | char *ptr = path; | |
630 | if (path[1] == '~') | |
631 | path++; | |
da2a95b2 | 632 | else { |
faea9ccb | 633 | path = strdup(ptr); |
da2a95b2 SH |
634 | free_path = 1; |
635 | } | |
faea9ccb AE |
636 | |
637 | *ptr = '\0'; | |
638 | } | |
639 | ||
f8014776 | 640 | if (protocol == PROTO_GIT) { |
5ad312be JL |
641 | /* These underlying connection commands die() if they |
642 | * cannot connect. | |
643 | */ | |
644 | char *target_host = strdup(host); | |
e814bc4d | 645 | if (git_use_proxy(host)) |
5ad312be | 646 | git_proxy_connect(fd, prog, host, path); |
da2a95b2 | 647 | else |
5ad312be JL |
648 | git_tcp_connect(fd, prog, host, path); |
649 | /* | |
650 | * Separate original protocol components prog and path | |
651 | * from extended components with a NUL byte. | |
652 | */ | |
653 | packet_write(fd[1], | |
654 | "%s %s%chost=%s%c", | |
655 | prog, path, 0, | |
656 | target_host, 0); | |
657 | free(target_host); | |
da2a95b2 SH |
658 | if (free_path) |
659 | free(path); | |
5ad312be | 660 | return 0; |
f8014776 | 661 | } |
2386d658 | 662 | |
f7192598 LT |
663 | if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0) |
664 | die("unable to create pipe pair for communication"); | |
665 | pid = fork(); | |
c9bc159d PD |
666 | if (pid < 0) |
667 | die("unable to fork"); | |
f7192598 | 668 | if (!pid) { |
b10d0ec7 JH |
669 | snprintf(command, sizeof(command), "%s %s", prog, |
670 | sq_quote(path)); | |
f7192598 LT |
671 | dup2(pipefd[1][0], 0); |
672 | dup2(pipefd[0][1], 1); | |
673 | close(pipefd[0][0]); | |
674 | close(pipefd[0][1]); | |
675 | close(pipefd[1][0]); | |
676 | close(pipefd[1][1]); | |
4852f723 | 677 | if (protocol == PROTO_SSH) { |
c7c81b3a JR |
678 | const char *ssh, *ssh_basename; |
679 | ssh = getenv("GIT_SSH"); | |
680 | if (!ssh) ssh = "ssh"; | |
681 | ssh_basename = strrchr(ssh, '/'); | |
4852f723 MS |
682 | if (!ssh_basename) |
683 | ssh_basename = ssh; | |
684 | else | |
685 | ssh_basename++; | |
686 | execlp(ssh, ssh_basename, host, command, NULL); | |
687 | } | |
016fb48b MD |
688 | else { |
689 | unsetenv(ALTERNATE_DB_ENVIRONMENT); | |
690 | unsetenv(DB_ENVIRONMENT); | |
691 | unsetenv(GIT_DIR_ENVIRONMENT); | |
692 | unsetenv(GRAFT_ENVIRONMENT); | |
693 | unsetenv(INDEX_ENVIRONMENT); | |
f7192598 | 694 | execlp("sh", "sh", "-c", command, NULL); |
016fb48b | 695 | } |
f7192598 | 696 | die("exec failed"); |
016fb48b | 697 | } |
f7192598 LT |
698 | fd[0] = pipefd[0][0]; |
699 | fd[1] = pipefd[1][1]; | |
700 | close(pipefd[0][1]); | |
701 | close(pipefd[1][0]); | |
da2a95b2 SH |
702 | if (free_path) |
703 | free(path); | |
f7192598 LT |
704 | return pid; |
705 | } | |
706 | ||
707 | int finish_connect(pid_t pid) | |
708 | { | |
709 | int ret; | |
710 | ||
711 | for (;;) { | |
712 | ret = waitpid(pid, NULL, 0); | |
713 | if (!ret) | |
714 | break; | |
715 | if (errno != EINTR) | |
716 | break; | |
717 | } | |
718 | return ret; | |
719 | } |