]>
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" |
f7192598 | 8 | |
96f1e58f | 9 | static char *server_capabilities; |
211b5f9e | 10 | |
2718ff09 LT |
11 | static int check_ref(const char *name, int len, unsigned int flags) |
12 | { | |
13 | if (!flags) | |
14 | return 1; | |
15 | ||
c41e20b3 | 16 | if (len < 5 || memcmp(name, "refs/", 5)) |
2718ff09 LT |
17 | return 0; |
18 | ||
19 | /* Skip the "refs/" part */ | |
20 | name += 5; | |
21 | len -= 5; | |
22 | ||
23 | /* REF_NORMAL means that we don't want the magic fake tag refs */ | |
24 | if ((flags & REF_NORMAL) && check_ref_format(name) < 0) | |
25 | return 0; | |
26 | ||
27 | /* REF_HEADS means that we want regular branch heads */ | |
28 | if ((flags & REF_HEADS) && !memcmp(name, "heads/", 6)) | |
29 | return 1; | |
30 | ||
31 | /* REF_TAGS means that we want tags */ | |
32 | if ((flags & REF_TAGS) && !memcmp(name, "tags/", 5)) | |
33 | return 1; | |
34 | ||
35 | /* All type bits clear means that we are ok with anything */ | |
36 | return !(flags & ~REF_NORMAL); | |
37 | } | |
38 | ||
d1c133f5 LT |
39 | /* |
40 | * Read all the refs from the other end | |
41 | */ | |
1a7141ff | 42 | struct ref **get_remote_heads(int in, struct ref **list, |
2718ff09 LT |
43 | int nr_match, char **match, |
44 | unsigned int flags) | |
d1c133f5 LT |
45 | { |
46 | *list = NULL; | |
47 | for (;;) { | |
48 | struct ref *ref; | |
49 | unsigned char old_sha1[20]; | |
50 | static char buffer[1000]; | |
51 | char *name; | |
211b5f9e | 52 | int len, name_len; |
d1c133f5 LT |
53 | |
54 | len = packet_read_line(in, buffer, sizeof(buffer)); | |
55 | if (!len) | |
56 | break; | |
57 | if (buffer[len-1] == '\n') | |
58 | buffer[--len] = 0; | |
59 | ||
60 | if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ') | |
61 | die("protocol error: expected sha/ref, got '%s'", buffer); | |
62 | name = buffer + 41; | |
1a7141ff | 63 | |
211b5f9e JS |
64 | name_len = strlen(name); |
65 | if (len != name_len + 41) { | |
66 | if (server_capabilities) | |
67 | free(server_capabilities); | |
9befac47 | 68 | server_capabilities = xstrdup(name + name_len + 1); |
211b5f9e JS |
69 | } |
70 | ||
2718ff09 | 71 | if (!check_ref(name, name_len, flags)) |
cfee10a7 | 72 | continue; |
d1c133f5 LT |
73 | if (nr_match && !path_match(name, nr_match, match)) |
74 | continue; | |
f88395ac | 75 | ref = xcalloc(1, sizeof(*ref) + len - 40); |
e702496e | 76 | hashcpy(ref->old_sha1, old_sha1); |
d1c133f5 | 77 | memcpy(ref->name, buffer + 41, len - 40); |
d1c133f5 LT |
78 | *list = ref; |
79 | list = &ref->next; | |
80 | } | |
81 | return list; | |
82 | } | |
83 | ||
211b5f9e JS |
84 | int server_supports(const char *feature) |
85 | { | |
1f5881bb JS |
86 | return server_capabilities && |
87 | strstr(server_capabilities, feature) != NULL; | |
211b5f9e JS |
88 | } |
89 | ||
41cb7488 LT |
90 | int get_ack(int fd, unsigned char *result_sha1) |
91 | { | |
92 | static char line[1000]; | |
93 | int len = packet_read_line(fd, line, sizeof(line)); | |
94 | ||
95 | if (!len) | |
96 | die("git-fetch-pack: expected ACK/NAK, got EOF"); | |
97 | if (line[len-1] == '\n') | |
98 | line[--len] = 0; | |
99 | if (!strcmp(line, "NAK")) | |
100 | return 0; | |
cc44c765 | 101 | if (!prefixcmp(line, "ACK ")) { |
c4c86f07 JS |
102 | if (!get_sha1_hex(line+4, result_sha1)) { |
103 | if (strstr(line+45, "continue")) | |
104 | return 2; | |
41cb7488 | 105 | return 1; |
c4c86f07 | 106 | } |
41cb7488 LT |
107 | } |
108 | die("git-fetch_pack: expected ACK/NAK, got '%s'", line); | |
109 | } | |
110 | ||
013e7c7f LT |
111 | int path_match(const char *path, int nr, char **match) |
112 | { | |
113 | int i; | |
114 | int pathlen = strlen(path); | |
115 | ||
116 | for (i = 0; i < nr; i++) { | |
117 | char *s = match[i]; | |
118 | int len = strlen(s); | |
119 | ||
120 | if (!len || len > pathlen) | |
121 | continue; | |
122 | if (memcmp(path + pathlen - len, s, len)) | |
123 | continue; | |
124 | if (pathlen > len && path[pathlen - len - 1] != '/') | |
125 | continue; | |
126 | *s = 0; | |
9546010b | 127 | return (i + 1); |
013e7c7f LT |
128 | } |
129 | return 0; | |
130 | } | |
131 | ||
2386d658 LT |
132 | enum protocol { |
133 | PROTO_LOCAL = 1, | |
134 | PROTO_SSH, | |
135 | PROTO_GIT, | |
136 | }; | |
137 | ||
138 | static enum protocol get_protocol(const char *name) | |
139 | { | |
140 | if (!strcmp(name, "ssh")) | |
141 | return PROTO_SSH; | |
142 | if (!strcmp(name, "git")) | |
143 | return PROTO_GIT; | |
c05186cc LT |
144 | if (!strcmp(name, "git+ssh")) |
145 | return PROTO_SSH; | |
146 | if (!strcmp(name, "ssh+git")) | |
147 | return PROTO_SSH; | |
2386d658 LT |
148 | die("I don't handle protocol '%s'", name); |
149 | } | |
150 | ||
5ba88448 YH |
151 | #define STR_(s) # s |
152 | #define STR(s) STR_(s) | |
2386d658 | 153 | |
49744d63 | 154 | #ifndef NO_IPV6 |
4c505f71 | 155 | |
ba505322 AR |
156 | static const char *ai_name(const struct addrinfo *ai) |
157 | { | |
158 | static char addr[INET_ADDRSTRLEN]; | |
159 | if ( AF_INET == ai->ai_family ) { | |
160 | struct sockaddr_in *in; | |
161 | in = (struct sockaddr_in *)ai->ai_addr; | |
162 | inet_ntop(ai->ai_family, &in->sin_addr, addr, sizeof(addr)); | |
163 | } else if ( AF_INET6 == ai->ai_family ) { | |
164 | struct sockaddr_in6 *in; | |
165 | in = (struct sockaddr_in6 *)ai->ai_addr; | |
166 | inet_ntop(ai->ai_family, &in->sin6_addr, addr, sizeof(addr)); | |
167 | } else { | |
168 | strcpy(addr, "(unknown)"); | |
169 | } | |
170 | return addr; | |
171 | } | |
172 | ||
5ad312be JL |
173 | /* |
174 | * Returns a connected socket() fd, or else die()s. | |
175 | */ | |
7841ce79 | 176 | static int git_tcp_connect_sock(char *host, int flags) |
2386d658 | 177 | { |
ac3bc6c1 | 178 | int sockfd = -1, saved_errno = 0; |
5ba88448 | 179 | char *colon, *end; |
554fe20d | 180 | const char *port = STR(DEFAULT_GIT_PORT); |
5ba88448 YH |
181 | struct addrinfo hints, *ai0, *ai; |
182 | int gai; | |
ba505322 | 183 | int cnt = 0; |
5ba88448 YH |
184 | |
185 | if (host[0] == '[') { | |
186 | end = strchr(host + 1, ']'); | |
187 | if (end) { | |
188 | *end = 0; | |
189 | end++; | |
190 | host++; | |
191 | } else | |
192 | end = host; | |
193 | } else | |
194 | end = host; | |
195 | colon = strchr(end, ':'); | |
196 | ||
ce6f8e7e LT |
197 | if (colon) { |
198 | *colon = 0; | |
5ba88448 | 199 | port = colon + 1; |
608d48b2 LT |
200 | if (!*port) |
201 | port = "<none>"; | |
ce6f8e7e | 202 | } |
5ba88448 YH |
203 | |
204 | memset(&hints, 0, sizeof(hints)); | |
205 | hints.ai_socktype = SOCK_STREAM; | |
206 | hints.ai_protocol = IPPROTO_TCP; | |
207 | ||
7841ce79 MT |
208 | if (flags & CONNECT_VERBOSE) |
209 | fprintf(stderr, "Looking up %s ... ", host); | |
210 | ||
5ba88448 YH |
211 | gai = getaddrinfo(host, port, &hints, &ai); |
212 | if (gai) | |
608d48b2 | 213 | die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai)); |
5ba88448 | 214 | |
7841ce79 MT |
215 | if (flags & CONNECT_VERBOSE) |
216 | fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port); | |
217 | ||
5ba88448 | 218 | for (ai0 = ai; ai; ai = ai->ai_next) { |
5ad312be JL |
219 | sockfd = socket(ai->ai_family, |
220 | ai->ai_socktype, ai->ai_protocol); | |
ac3bc6c1 PB |
221 | if (sockfd < 0) { |
222 | saved_errno = errno; | |
5ba88448 | 223 | continue; |
ac3bc6c1 | 224 | } |
5ba88448 | 225 | if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) { |
ac3bc6c1 | 226 | saved_errno = errno; |
7cbf2f24 | 227 | fprintf(stderr, "%s[%d: %s]: errno=%s\n", |
ba505322 AR |
228 | host, |
229 | cnt, | |
230 | ai_name(ai), | |
ba505322 | 231 | strerror(saved_errno)); |
5ba88448 YH |
232 | close(sockfd); |
233 | sockfd = -1; | |
234 | continue; | |
2386d658 | 235 | } |
ba505322 AR |
236 | if (flags & CONNECT_VERBOSE) |
237 | fprintf(stderr, "%s ", ai_name(ai)); | |
5ba88448 | 238 | break; |
2386d658 LT |
239 | } |
240 | ||
5ba88448 | 241 | freeaddrinfo(ai0); |
2386d658 | 242 | |
2386d658 | 243 | if (sockfd < 0) |
ac3bc6c1 | 244 | die("unable to connect a socket (%s)", strerror(saved_errno)); |
5ba88448 | 245 | |
7841ce79 MT |
246 | if (flags & CONNECT_VERBOSE) |
247 | fprintf(stderr, "done.\n"); | |
248 | ||
5ad312be | 249 | return sockfd; |
2386d658 LT |
250 | } |
251 | ||
49744d63 | 252 | #else /* NO_IPV6 */ |
4c505f71 | 253 | |
5ad312be JL |
254 | /* |
255 | * Returns a connected socket() fd, or else die()s. | |
256 | */ | |
7841ce79 | 257 | static int git_tcp_connect_sock(char *host, int flags) |
4c505f71 | 258 | { |
ac3bc6c1 | 259 | int sockfd = -1, saved_errno = 0; |
4c505f71 PA |
260 | char *colon, *end; |
261 | char *port = STR(DEFAULT_GIT_PORT), *ep; | |
262 | struct hostent *he; | |
263 | struct sockaddr_in sa; | |
264 | char **ap; | |
265 | unsigned int nport; | |
ba505322 | 266 | int cnt; |
4c505f71 PA |
267 | |
268 | if (host[0] == '[') { | |
269 | end = strchr(host + 1, ']'); | |
270 | if (end) { | |
271 | *end = 0; | |
272 | end++; | |
273 | host++; | |
274 | } else | |
275 | end = host; | |
276 | } else | |
277 | end = host; | |
278 | colon = strchr(end, ':'); | |
279 | ||
280 | if (colon) { | |
281 | *colon = 0; | |
282 | port = colon + 1; | |
283 | } | |
284 | ||
7841ce79 MT |
285 | if (flags & CONNECT_VERBOSE) |
286 | fprintf(stderr, "Looking up %s ... ", host); | |
287 | ||
4c505f71 PA |
288 | he = gethostbyname(host); |
289 | if (!he) | |
290 | die("Unable to look up %s (%s)", host, hstrerror(h_errno)); | |
291 | nport = strtoul(port, &ep, 10); | |
292 | if ( ep == port || *ep ) { | |
293 | /* Not numeric */ | |
294 | struct servent *se = getservbyname(port,"tcp"); | |
295 | if ( !se ) | |
296 | die("Unknown port %s\n", port); | |
297 | nport = se->s_port; | |
298 | } | |
299 | ||
7841ce79 MT |
300 | if (flags & CONNECT_VERBOSE) |
301 | fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port); | |
302 | ||
ba505322 | 303 | for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) { |
4c505f71 | 304 | sockfd = socket(he->h_addrtype, SOCK_STREAM, 0); |
ac3bc6c1 PB |
305 | if (sockfd < 0) { |
306 | saved_errno = errno; | |
4c505f71 | 307 | continue; |
ac3bc6c1 | 308 | } |
4c505f71 PA |
309 | |
310 | memset(&sa, 0, sizeof sa); | |
311 | sa.sin_family = he->h_addrtype; | |
6573faff | 312 | sa.sin_port = htons(nport); |
c6164218 | 313 | memcpy(&sa.sin_addr, *ap, he->h_length); |
4c505f71 PA |
314 | |
315 | if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) { | |
ac3bc6c1 | 316 | saved_errno = errno; |
7cbf2f24 | 317 | fprintf(stderr, "%s[%d: %s]: errno=%s\n", |
ba505322 AR |
318 | host, |
319 | cnt, | |
320 | inet_ntoa(*(struct in_addr *)&sa.sin_addr), | |
ba505322 | 321 | strerror(saved_errno)); |
4c505f71 PA |
322 | close(sockfd); |
323 | sockfd = -1; | |
324 | continue; | |
325 | } | |
ba505322 AR |
326 | if (flags & CONNECT_VERBOSE) |
327 | fprintf(stderr, "%s ", | |
328 | inet_ntoa(*(struct in_addr *)&sa.sin_addr)); | |
4c505f71 PA |
329 | break; |
330 | } | |
331 | ||
332 | if (sockfd < 0) | |
ac3bc6c1 | 333 | die("unable to connect a socket (%s)", strerror(saved_errno)); |
4c505f71 | 334 | |
7841ce79 MT |
335 | if (flags & CONNECT_VERBOSE) |
336 | fprintf(stderr, "done.\n"); | |
337 | ||
5ad312be JL |
338 | return sockfd; |
339 | } | |
340 | ||
341 | #endif /* NO_IPV6 */ | |
342 | ||
343 | ||
7841ce79 | 344 | static void git_tcp_connect(int fd[2], char *host, int flags) |
5ad312be | 345 | { |
7841ce79 | 346 | int sockfd = git_tcp_connect_sock(host, flags); |
5ad312be | 347 | |
4c505f71 | 348 | fd[0] = sockfd; |
ec587fde | 349 | fd[1] = dup(sockfd); |
4c505f71 PA |
350 | } |
351 | ||
4c505f71 | 352 | |
96f1e58f DR |
353 | static char *git_proxy_command; |
354 | static const char *rhost_name; | |
e814bc4d | 355 | static int rhost_len; |
f8014776 PC |
356 | |
357 | static int git_proxy_command_options(const char *var, const char *value) | |
358 | { | |
e814bc4d | 359 | if (!strcmp(var, "core.gitproxy")) { |
c3df8568 YH |
360 | const char *for_pos; |
361 | int matchlen = -1; | |
362 | int hostlen; | |
363 | ||
e814bc4d | 364 | if (git_proxy_command) |
f8014776 | 365 | return 0; |
e814bc4d JH |
366 | /* [core] |
367 | * ;# matches www.kernel.org as well | |
368 | * gitproxy = netcatter-1 for kernel.org | |
369 | * gitproxy = netcatter-2 for sample.xz | |
370 | * gitproxy = netcatter-default | |
371 | */ | |
c3df8568 | 372 | for_pos = strstr(value, " for "); |
e814bc4d JH |
373 | if (!for_pos) |
374 | /* matches everybody */ | |
375 | matchlen = strlen(value); | |
376 | else { | |
377 | hostlen = strlen(for_pos + 5); | |
378 | if (rhost_len < hostlen) | |
379 | matchlen = -1; | |
380 | else if (!strncmp(for_pos + 5, | |
381 | rhost_name + rhost_len - hostlen, | |
382 | hostlen) && | |
383 | ((rhost_len == hostlen) || | |
384 | rhost_name[rhost_len - hostlen -1] == '.')) | |
385 | matchlen = for_pos - value; | |
386 | else | |
387 | matchlen = -1; | |
388 | } | |
389 | if (0 <= matchlen) { | |
390 | /* core.gitproxy = none for kernel.org */ | |
a6080a0a | 391 | if (matchlen == 4 && |
e814bc4d JH |
392 | !memcmp(value, "none", 4)) |
393 | matchlen = 0; | |
394 | git_proxy_command = xmalloc(matchlen + 1); | |
395 | memcpy(git_proxy_command, value, matchlen); | |
396 | git_proxy_command[matchlen] = 0; | |
f8014776 | 397 | } |
e814bc4d | 398 | return 0; |
f8014776 PC |
399 | } |
400 | ||
401 | return git_default_config(var, value); | |
402 | } | |
403 | ||
e814bc4d | 404 | static int git_use_proxy(const char *host) |
f8014776 | 405 | { |
e814bc4d JH |
406 | rhost_name = host; |
407 | rhost_len = strlen(host); | |
f8014776 PC |
408 | git_proxy_command = getenv("GIT_PROXY_COMMAND"); |
409 | git_config(git_proxy_command_options); | |
e814bc4d JH |
410 | rhost_name = NULL; |
411 | return (git_proxy_command && *git_proxy_command); | |
f8014776 PC |
412 | } |
413 | ||
c78963d2 | 414 | static void git_proxy_connect(int fd[2], char *host) |
f8014776 | 415 | { |
554fe20d | 416 | const char *port = STR(DEFAULT_GIT_PORT); |
f8014776 | 417 | char *colon, *end; |
15a1c012 SP |
418 | const char *argv[4]; |
419 | struct child_process proxy; | |
f8014776 PC |
420 | |
421 | if (host[0] == '[') { | |
422 | end = strchr(host + 1, ']'); | |
423 | if (end) { | |
424 | *end = 0; | |
425 | end++; | |
426 | host++; | |
427 | } else | |
428 | end = host; | |
429 | } else | |
430 | end = host; | |
431 | colon = strchr(end, ':'); | |
432 | ||
433 | if (colon) { | |
434 | *colon = 0; | |
435 | port = colon + 1; | |
436 | } | |
437 | ||
15a1c012 SP |
438 | argv[0] = git_proxy_command; |
439 | argv[1] = host; | |
440 | argv[2] = port; | |
441 | argv[3] = NULL; | |
442 | memset(&proxy, 0, sizeof(proxy)); | |
443 | proxy.argv = argv; | |
444 | proxy.in = -1; | |
445 | proxy.out = -1; | |
446 | if (start_command(&proxy)) | |
447 | die("cannot start proxy %s", argv[0]); | |
448 | fd[0] = proxy.out; /* read from proxy stdout */ | |
449 | fd[1] = proxy.in; /* write to proxy stdin */ | |
f8014776 PC |
450 | } |
451 | ||
0f503d77 CC |
452 | #define MAX_CMD_LEN 1024 |
453 | ||
f7192598 | 454 | /* |
f42a5c4e FBH |
455 | * This returns 0 if the transport protocol does not need fork(2), |
456 | * or a process id if it does. Once done, finish the connection | |
457 | * with finish_connect() with the value returned from this function | |
458 | * (it is safe to call finish_connect() with 0 to support the former | |
459 | * case). | |
460 | * | |
461 | * Does not return a negative value on error; it just dies. | |
f7192598 | 462 | */ |
7841ce79 | 463 | pid_t git_connect(int fd[2], char *url, const char *prog, int flags) |
f7192598 | 464 | { |
faea9ccb | 465 | char *host, *path = url; |
356bece0 YH |
466 | char *end; |
467 | int c; | |
f7192598 LT |
468 | int pipefd[2][2]; |
469 | pid_t pid; | |
faea9ccb | 470 | enum protocol protocol = PROTO_LOCAL; |
da2a95b2 | 471 | int free_path = 0; |
faea9ccb | 472 | |
f0b7367c JH |
473 | /* Without this we cannot rely on waitpid() to tell |
474 | * what happened to our children. | |
475 | */ | |
476 | signal(SIGCHLD, SIG_DFL); | |
477 | ||
faea9ccb AE |
478 | host = strstr(url, "://"); |
479 | if(host) { | |
480 | *host = '\0'; | |
481 | protocol = get_protocol(url); | |
482 | host += 3; | |
356bece0 YH |
483 | c = '/'; |
484 | } else { | |
f7192598 | 485 | host = url; |
356bece0 YH |
486 | c = ':'; |
487 | } | |
488 | ||
489 | if (host[0] == '[') { | |
490 | end = strchr(host + 1, ']'); | |
491 | if (end) { | |
492 | *end = 0; | |
493 | end++; | |
494 | host++; | |
495 | } else | |
496 | end = host; | |
497 | } else | |
498 | end = host; | |
499 | ||
500 | path = strchr(end, c); | |
501 | if (c == ':') { | |
502 | if (path) { | |
faea9ccb | 503 | protocol = PROTO_SSH; |
356bece0 YH |
504 | *path++ = '\0'; |
505 | } else | |
506 | path = host; | |
f7192598 | 507 | } |
2386d658 | 508 | |
faea9ccb AE |
509 | if (!path || !*path) |
510 | die("No path specified. See 'man git-pull' for valid url syntax"); | |
511 | ||
512 | /* | |
513 | * null-terminate hostname and point path to ~ for URL's like this: | |
514 | * ssh://host.xz/~user/repo | |
515 | */ | |
516 | if (protocol != PROTO_LOCAL && host != url) { | |
517 | char *ptr = path; | |
518 | if (path[1] == '~') | |
519 | path++; | |
da2a95b2 | 520 | else { |
9befac47 | 521 | path = xstrdup(ptr); |
da2a95b2 SH |
522 | free_path = 1; |
523 | } | |
faea9ccb AE |
524 | |
525 | *ptr = '\0'; | |
526 | } | |
527 | ||
f8014776 | 528 | if (protocol == PROTO_GIT) { |
5ad312be JL |
529 | /* These underlying connection commands die() if they |
530 | * cannot connect. | |
531 | */ | |
9befac47 | 532 | char *target_host = xstrdup(host); |
e814bc4d | 533 | if (git_use_proxy(host)) |
c78963d2 | 534 | git_proxy_connect(fd, host); |
da2a95b2 | 535 | else |
7841ce79 | 536 | git_tcp_connect(fd, host, flags); |
5ad312be JL |
537 | /* |
538 | * Separate original protocol components prog and path | |
539 | * from extended components with a NUL byte. | |
540 | */ | |
541 | packet_write(fd[1], | |
542 | "%s %s%chost=%s%c", | |
543 | prog, path, 0, | |
544 | target_host, 0); | |
545 | free(target_host); | |
da2a95b2 SH |
546 | if (free_path) |
547 | free(path); | |
5ad312be | 548 | return 0; |
f8014776 | 549 | } |
2386d658 | 550 | |
f7192598 LT |
551 | if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0) |
552 | die("unable to create pipe pair for communication"); | |
553 | pid = fork(); | |
c9bc159d PD |
554 | if (pid < 0) |
555 | die("unable to fork"); | |
f7192598 | 556 | if (!pid) { |
0f503d77 CC |
557 | char command[MAX_CMD_LEN]; |
558 | char *posn = command; | |
559 | int size = MAX_CMD_LEN; | |
560 | int of = 0; | |
561 | ||
562 | of |= add_to_string(&posn, &size, prog, 0); | |
563 | of |= add_to_string(&posn, &size, " ", 0); | |
564 | of |= add_to_string(&posn, &size, path, 1); | |
565 | ||
566 | if (of) | |
567 | die("command line too long"); | |
568 | ||
f7192598 LT |
569 | dup2(pipefd[1][0], 0); |
570 | dup2(pipefd[0][1], 1); | |
571 | close(pipefd[0][0]); | |
572 | close(pipefd[0][1]); | |
573 | close(pipefd[1][0]); | |
574 | close(pipefd[1][1]); | |
4852f723 | 575 | if (protocol == PROTO_SSH) { |
c7c81b3a JR |
576 | const char *ssh, *ssh_basename; |
577 | ssh = getenv("GIT_SSH"); | |
578 | if (!ssh) ssh = "ssh"; | |
579 | ssh_basename = strrchr(ssh, '/'); | |
4852f723 MS |
580 | if (!ssh_basename) |
581 | ssh_basename = ssh; | |
582 | else | |
583 | ssh_basename++; | |
584 | execlp(ssh, ssh_basename, host, command, NULL); | |
585 | } | |
016fb48b MD |
586 | else { |
587 | unsetenv(ALTERNATE_DB_ENVIRONMENT); | |
588 | unsetenv(DB_ENVIRONMENT); | |
589 | unsetenv(GIT_DIR_ENVIRONMENT); | |
892c41b9 | 590 | unsetenv(GIT_WORK_TREE_ENVIRONMENT); |
016fb48b MD |
591 | unsetenv(GRAFT_ENVIRONMENT); |
592 | unsetenv(INDEX_ENVIRONMENT); | |
f7192598 | 593 | execlp("sh", "sh", "-c", command, NULL); |
016fb48b | 594 | } |
f7192598 | 595 | die("exec failed"); |
016fb48b | 596 | } |
f7192598 LT |
597 | fd[0] = pipefd[0][0]; |
598 | fd[1] = pipefd[1][1]; | |
599 | close(pipefd[0][1]); | |
600 | close(pipefd[1][0]); | |
da2a95b2 SH |
601 | if (free_path) |
602 | free(path); | |
f7192598 LT |
603 | return pid; |
604 | } | |
605 | ||
606 | int finish_connect(pid_t pid) | |
607 | { | |
f42a5c4e FBH |
608 | if (pid == 0) |
609 | return 0; | |
610 | ||
53e1a761 | 611 | while (waitpid(pid, NULL, 0) < 0) { |
f7192598 | 612 | if (errno != EINTR) |
53e1a761 | 613 | return -1; |
f7192598 | 614 | } |
53e1a761 | 615 | return 0; |
f7192598 | 616 | } |