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