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