blob: 5ac357278464324d82ac6912c1b7ad84d42d0743 [file] [log] [blame]
Jason Riedy731043f2006-01-25 12:38:36 -08001#include "git-compat-util.h"
Linus Torvaldsf7192592005-07-04 11:57:58 -07002#include "cache.h"
Linus Torvalds41cb7482005-07-05 15:44:09 -07003#include "pkt-line.h"
Junio C Hamanob10d0ec2005-07-08 00:02:52 -07004#include "quote.h"
Junio C Hamano6abf5c02005-10-16 00:25:26 -07005#include "refs.h"
Shawn O. Pearce15a1c0122007-03-12 19:00:19 -04006#include "run-command.h"
Daniel Barkalow6b628162007-05-12 11:45:59 -04007#include "remote.h"
Linus Torvaldsf7192592005-07-04 11:57:58 -07008
David Rientjes96f1e582006-08-15 10:23:48 -07009static char *server_capabilities;
Johannes Schindelin211b5f92005-10-28 04:48:54 +020010
Linus Torvalds2718ff02006-07-04 12:29:10 -070011static int check_ref(const char *name, int len, unsigned int flags)
12{
13 if (!flags)
14 return 1;
15
Andy Whitcroftc41e20b2006-09-05 20:00:17 +010016 if (len < 5 || memcmp(name, "refs/", 5))
Linus Torvalds2718ff02006-07-04 12:29:10 -070017 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
Daniel Barkalow45773702007-10-29 21:05:40 -040039int check_ref_type(const struct ref *ref, int flags)
40{
41 return check_ref(ref->name, strlen(ref->name), flags);
42}
43
Linus Torvaldsd1c133f2005-07-16 13:55:50 -070044/*
45 * Read all the refs from the other end
46 */
Junio C Hamano1a7141f2005-10-13 18:57:40 -070047struct ref **get_remote_heads(int in, struct ref **list,
Linus Torvalds2718ff02006-07-04 12:29:10 -070048 int nr_match, char **match,
49 unsigned int flags)
Linus Torvaldsd1c133f2005-07-16 13:55:50 -070050{
51 *list = NULL;
52 for (;;) {
53 struct ref *ref;
54 unsigned char old_sha1[20];
55 static char buffer[1000];
56 char *name;
Johannes Schindelin211b5f92005-10-28 04:48:54 +020057 int len, name_len;
Linus Torvaldsd1c133f2005-07-16 13:55:50 -070058
59 len = packet_read_line(in, buffer, sizeof(buffer));
60 if (!len)
61 break;
62 if (buffer[len-1] == '\n')
63 buffer[--len] = 0;
64
65 if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
66 die("protocol error: expected sha/ref, got '%s'", buffer);
67 name = buffer + 41;
Junio C Hamano1a7141f2005-10-13 18:57:40 -070068
Johannes Schindelin211b5f92005-10-28 04:48:54 +020069 name_len = strlen(name);
70 if (len != name_len + 41) {
71 if (server_capabilities)
72 free(server_capabilities);
Shawn Pearce9befac42006-09-02 00:16:31 -040073 server_capabilities = xstrdup(name + name_len + 1);
Johannes Schindelin211b5f92005-10-28 04:48:54 +020074 }
75
Linus Torvalds2718ff02006-07-04 12:29:10 -070076 if (!check_ref(name, name_len, flags))
Junio C Hamanocfee10a2005-12-25 23:18:37 -080077 continue;
Linus Torvaldsd1c133f2005-07-16 13:55:50 -070078 if (nr_match && !path_match(name, nr_match, match))
79 continue;
Junio C Hamano90446a02007-09-28 01:46:13 -070080 ref = alloc_ref(name_len + 1);
Shawn Pearcee7024962006-08-23 02:49:00 -040081 hashcpy(ref->old_sha1, old_sha1);
Junio C Hamano90446a02007-09-28 01:46:13 -070082 memcpy(ref->name, buffer + 41, name_len + 1);
Linus Torvaldsd1c133f2005-07-16 13:55:50 -070083 *list = ref;
84 list = &ref->next;
85 }
86 return list;
87}
88
Johannes Schindelin211b5f92005-10-28 04:48:54 +020089int server_supports(const char *feature)
90{
Johannes Schindelin1f5881b2005-10-28 05:56:41 +020091 return server_capabilities &&
92 strstr(server_capabilities, feature) != NULL;
Johannes Schindelin211b5f92005-10-28 04:48:54 +020093}
94
Linus Torvalds41cb7482005-07-05 15:44:09 -070095int get_ack(int fd, unsigned char *result_sha1)
96{
97 static char line[1000];
98 int len = packet_read_line(fd, line, sizeof(line));
99
100 if (!len)
101 die("git-fetch-pack: expected ACK/NAK, got EOF");
102 if (line[len-1] == '\n')
103 line[--len] = 0;
104 if (!strcmp(line, "NAK"))
105 return 0;
Junio C Hamanocc44c762007-02-20 01:53:29 -0800106 if (!prefixcmp(line, "ACK ")) {
Johannes Schindelinc4c86f02005-10-28 04:50:26 +0200107 if (!get_sha1_hex(line+4, result_sha1)) {
108 if (strstr(line+45, "continue"))
109 return 2;
Linus Torvalds41cb7482005-07-05 15:44:09 -0700110 return 1;
Johannes Schindelinc4c86f02005-10-28 04:50:26 +0200111 }
Linus Torvalds41cb7482005-07-05 15:44:09 -0700112 }
113 die("git-fetch_pack: expected ACK/NAK, got '%s'", line);
114}
115
Linus Torvalds013e7c72005-07-04 13:24:30 -0700116int path_match(const char *path, int nr, char **match)
117{
118 int i;
119 int pathlen = strlen(path);
120
121 for (i = 0; i < nr; i++) {
122 char *s = match[i];
123 int len = strlen(s);
124
125 if (!len || len > pathlen)
126 continue;
127 if (memcmp(path + pathlen - len, s, len))
128 continue;
129 if (pathlen > len && path[pathlen - len - 1] != '/')
130 continue;
131 *s = 0;
Junio C Hamano95460102006-05-11 15:28:44 -0700132 return (i + 1);
Linus Torvalds013e7c72005-07-04 13:24:30 -0700133 }
134 return 0;
135}
136
Linus Torvalds2386d652005-07-13 18:46:20 -0700137enum protocol {
138 PROTO_LOCAL = 1,
139 PROTO_SSH,
140 PROTO_GIT,
141};
142
143static enum protocol get_protocol(const char *name)
144{
145 if (!strcmp(name, "ssh"))
146 return PROTO_SSH;
147 if (!strcmp(name, "git"))
148 return PROTO_GIT;
Linus Torvaldsc05186c2005-10-14 17:14:56 -0700149 if (!strcmp(name, "git+ssh"))
150 return PROTO_SSH;
151 if (!strcmp(name, "ssh+git"))
152 return PROTO_SSH;
Linus Torvalds72a4f4b2007-08-01 10:03:37 -0700153 if (!strcmp(name, "file"))
154 return PROTO_LOCAL;
Linus Torvalds2386d652005-07-13 18:46:20 -0700155 die("I don't handle protocol '%s'", name);
156}
157
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400158#define STR_(s) # s
159#define STR(s) STR_(s)
Linus Torvalds2386d652005-07-13 18:46:20 -0700160
hpa49744d62005-09-28 16:52:21 -0700161#ifndef NO_IPV6
hpa4c505f72005-09-28 16:37:58 -0700162
Alex Riesenba505322007-05-23 23:34:27 +0200163static const char *ai_name(const struct addrinfo *ai)
164{
165 static char addr[INET_ADDRSTRLEN];
166 if ( AF_INET == ai->ai_family ) {
167 struct sockaddr_in *in;
168 in = (struct sockaddr_in *)ai->ai_addr;
169 inet_ntop(ai->ai_family, &in->sin_addr, addr, sizeof(addr));
170 } else if ( AF_INET6 == ai->ai_family ) {
171 struct sockaddr_in6 *in;
172 in = (struct sockaddr_in6 *)ai->ai_addr;
173 inet_ntop(ai->ai_family, &in->sin6_addr, addr, sizeof(addr));
174 } else {
175 strcpy(addr, "(unknown)");
176 }
177 return addr;
178}
179
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500180/*
181 * Returns a connected socket() fd, or else die()s.
182 */
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300183static int git_tcp_connect_sock(char *host, int flags)
Linus Torvalds2386d652005-07-13 18:46:20 -0700184{
Petr Baudisac3bc6c2006-07-01 23:56:26 +0200185 int sockfd = -1, saved_errno = 0;
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400186 char *colon, *end;
Timo Hirvonen554fe202006-06-28 12:04:39 +0300187 const char *port = STR(DEFAULT_GIT_PORT);
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400188 struct addrinfo hints, *ai0, *ai;
189 int gai;
Alex Riesenba505322007-05-23 23:34:27 +0200190 int cnt = 0;
Linus Torvalds2386d652005-07-13 18:46:20 -0700191
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400192 if (host[0] == '[') {
193 end = strchr(host + 1, ']');
194 if (end) {
195 *end = 0;
196 end++;
197 host++;
198 } else
199 end = host;
200 } else
201 end = host;
202 colon = strchr(end, ':');
203
Linus Torvaldsce6f8e72005-07-23 11:10:21 -0700204 if (colon) {
205 *colon = 0;
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400206 port = colon + 1;
Linus Torvalds608d48b2007-03-27 09:50:20 -0700207 if (!*port)
208 port = "<none>";
Linus Torvaldsce6f8e72005-07-23 11:10:21 -0700209 }
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400210
211 memset(&hints, 0, sizeof(hints));
212 hints.ai_socktype = SOCK_STREAM;
213 hints.ai_protocol = IPPROTO_TCP;
214
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300215 if (flags & CONNECT_VERBOSE)
216 fprintf(stderr, "Looking up %s ... ", host);
217
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400218 gai = getaddrinfo(host, port, &hints, &ai);
219 if (gai)
Linus Torvalds608d48b2007-03-27 09:50:20 -0700220 die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai));
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400221
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300222 if (flags & CONNECT_VERBOSE)
223 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
224
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400225 for (ai0 = ai; ai; ai = ai->ai_next) {
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500226 sockfd = socket(ai->ai_family,
227 ai->ai_socktype, ai->ai_protocol);
Petr Baudisac3bc6c2006-07-01 23:56:26 +0200228 if (sockfd < 0) {
229 saved_errno = errno;
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400230 continue;
Petr Baudisac3bc6c2006-07-01 23:56:26 +0200231 }
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400232 if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
Petr Baudisac3bc6c2006-07-01 23:56:26 +0200233 saved_errno = errno;
Alex Riesen7cbf2f22007-06-12 22:52:10 +0200234 fprintf(stderr, "%s[%d: %s]: errno=%s\n",
Alex Riesenba505322007-05-23 23:34:27 +0200235 host,
236 cnt,
237 ai_name(ai),
Alex Riesenba505322007-05-23 23:34:27 +0200238 strerror(saved_errno));
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400239 close(sockfd);
240 sockfd = -1;
241 continue;
Linus Torvalds2386d652005-07-13 18:46:20 -0700242 }
Alex Riesenba505322007-05-23 23:34:27 +0200243 if (flags & CONNECT_VERBOSE)
244 fprintf(stderr, "%s ", ai_name(ai));
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400245 break;
Linus Torvalds2386d652005-07-13 18:46:20 -0700246 }
247
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400248 freeaddrinfo(ai0);
Linus Torvalds2386d652005-07-13 18:46:20 -0700249
Linus Torvalds2386d652005-07-13 18:46:20 -0700250 if (sockfd < 0)
Petr Baudisac3bc6c2006-07-01 23:56:26 +0200251 die("unable to connect a socket (%s)", strerror(saved_errno));
YOSHIFUJI Hideaki5ba88442005-07-21 09:10:36 -0400252
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300253 if (flags & CONNECT_VERBOSE)
254 fprintf(stderr, "done.\n");
255
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500256 return sockfd;
Linus Torvalds2386d652005-07-13 18:46:20 -0700257}
258
hpa49744d62005-09-28 16:52:21 -0700259#else /* NO_IPV6 */
hpa4c505f72005-09-28 16:37:58 -0700260
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500261/*
262 * Returns a connected socket() fd, or else die()s.
263 */
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300264static int git_tcp_connect_sock(char *host, int flags)
hpa4c505f72005-09-28 16:37:58 -0700265{
Petr Baudisac3bc6c2006-07-01 23:56:26 +0200266 int sockfd = -1, saved_errno = 0;
hpa4c505f72005-09-28 16:37:58 -0700267 char *colon, *end;
268 char *port = STR(DEFAULT_GIT_PORT), *ep;
269 struct hostent *he;
270 struct sockaddr_in sa;
271 char **ap;
272 unsigned int nport;
Alex Riesenba505322007-05-23 23:34:27 +0200273 int cnt;
hpa4c505f72005-09-28 16:37:58 -0700274
275 if (host[0] == '[') {
276 end = strchr(host + 1, ']');
277 if (end) {
278 *end = 0;
279 end++;
280 host++;
281 } else
282 end = host;
283 } else
284 end = host;
285 colon = strchr(end, ':');
286
287 if (colon) {
288 *colon = 0;
289 port = colon + 1;
290 }
291
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300292 if (flags & CONNECT_VERBOSE)
293 fprintf(stderr, "Looking up %s ... ", host);
294
hpa4c505f72005-09-28 16:37:58 -0700295 he = gethostbyname(host);
296 if (!he)
297 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
298 nport = strtoul(port, &ep, 10);
299 if ( ep == port || *ep ) {
300 /* Not numeric */
301 struct servent *se = getservbyname(port,"tcp");
302 if ( !se )
303 die("Unknown port %s\n", port);
304 nport = se->s_port;
305 }
306
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300307 if (flags & CONNECT_VERBOSE)
308 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
309
Alex Riesenba505322007-05-23 23:34:27 +0200310 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
hpa4c505f72005-09-28 16:37:58 -0700311 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
Petr Baudisac3bc6c2006-07-01 23:56:26 +0200312 if (sockfd < 0) {
313 saved_errno = errno;
hpa4c505f72005-09-28 16:37:58 -0700314 continue;
Petr Baudisac3bc6c2006-07-01 23:56:26 +0200315 }
hpa4c505f72005-09-28 16:37:58 -0700316
317 memset(&sa, 0, sizeof sa);
318 sa.sin_family = he->h_addrtype;
Peter Anvin6573faf2005-09-28 17:26:44 -0700319 sa.sin_port = htons(nport);
Paul Sericec6164212005-11-22 07:54:23 -0600320 memcpy(&sa.sin_addr, *ap, he->h_length);
hpa4c505f72005-09-28 16:37:58 -0700321
322 if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
Petr Baudisac3bc6c2006-07-01 23:56:26 +0200323 saved_errno = errno;
Alex Riesen7cbf2f22007-06-12 22:52:10 +0200324 fprintf(stderr, "%s[%d: %s]: errno=%s\n",
Alex Riesenba505322007-05-23 23:34:27 +0200325 host,
326 cnt,
327 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
Alex Riesenba505322007-05-23 23:34:27 +0200328 strerror(saved_errno));
hpa4c505f72005-09-28 16:37:58 -0700329 close(sockfd);
330 sockfd = -1;
331 continue;
332 }
Alex Riesenba505322007-05-23 23:34:27 +0200333 if (flags & CONNECT_VERBOSE)
334 fprintf(stderr, "%s ",
335 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
hpa4c505f72005-09-28 16:37:58 -0700336 break;
337 }
338
339 if (sockfd < 0)
Petr Baudisac3bc6c2006-07-01 23:56:26 +0200340 die("unable to connect a socket (%s)", strerror(saved_errno));
hpa4c505f72005-09-28 16:37:58 -0700341
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300342 if (flags & CONNECT_VERBOSE)
343 fprintf(stderr, "done.\n");
344
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500345 return sockfd;
hpa4c505f72005-09-28 16:37:58 -0700346}
347
hpa49744d62005-09-28 16:52:21 -0700348#endif /* NO_IPV6 */
hpa4c505f72005-09-28 16:37:58 -0700349
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500350
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300351static void git_tcp_connect(int fd[2], char *host, int flags)
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500352{
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300353 int sockfd = git_tcp_connect_sock(host, flags);
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500354
355 fd[0] = sockfd;
Junio C Hamanoec587fd2007-01-21 17:10:51 -0800356 fd[1] = dup(sockfd);
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500357}
358
359
David Rientjes96f1e582006-08-15 10:23:48 -0700360static char *git_proxy_command;
361static const char *rhost_name;
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800362static int rhost_len;
Paul Collinsf8014772005-11-04 14:57:16 +0000363
364static int git_proxy_command_options(const char *var, const char *value)
365{
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800366 if (!strcmp(var, "core.gitproxy")) {
YOSHIFUJI Hideaki / 吉藤英明c3df8562005-11-22 12:18:23 +0900367 const char *for_pos;
368 int matchlen = -1;
369 int hostlen;
370
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800371 if (git_proxy_command)
Paul Collinsf8014772005-11-04 14:57:16 +0000372 return 0;
Junio C Hamanoc64b9ad2008-02-11 10:52:15 -0800373 if (!value)
374 return config_error_nonbool(var);
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800375 /* [core]
376 * ;# matches www.kernel.org as well
377 * gitproxy = netcatter-1 for kernel.org
378 * gitproxy = netcatter-2 for sample.xz
379 * gitproxy = netcatter-default
380 */
YOSHIFUJI Hideaki / 吉藤英明c3df8562005-11-22 12:18:23 +0900381 for_pos = strstr(value, " for ");
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800382 if (!for_pos)
383 /* matches everybody */
384 matchlen = strlen(value);
385 else {
386 hostlen = strlen(for_pos + 5);
387 if (rhost_len < hostlen)
388 matchlen = -1;
389 else if (!strncmp(for_pos + 5,
390 rhost_name + rhost_len - hostlen,
391 hostlen) &&
392 ((rhost_len == hostlen) ||
393 rhost_name[rhost_len - hostlen -1] == '.'))
394 matchlen = for_pos - value;
395 else
396 matchlen = -1;
Paul Collinsf8014772005-11-04 14:57:16 +0000397 }
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800398 if (0 <= matchlen) {
399 /* core.gitproxy = none for kernel.org */
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700400 if (matchlen == 4 &&
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800401 !memcmp(value, "none", 4))
402 matchlen = 0;
Pierre Habouzit182af832007-09-16 00:32:36 +0200403 git_proxy_command = xmemdupz(value, matchlen);
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800404 }
405 return 0;
Paul Collinsf8014772005-11-04 14:57:16 +0000406 }
407
408 return git_default_config(var, value);
409}
410
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800411static int git_use_proxy(const char *host)
Paul Collinsf8014772005-11-04 14:57:16 +0000412{
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800413 rhost_name = host;
414 rhost_len = strlen(host);
Paul Collinsf8014772005-11-04 14:57:16 +0000415 git_proxy_command = getenv("GIT_PROXY_COMMAND");
416 git_config(git_proxy_command_options);
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800417 rhost_name = NULL;
418 return (git_proxy_command && *git_proxy_command);
Paul Collinsf8014772005-11-04 14:57:16 +0000419}
420
Junio C Hamanoc78963d2006-06-28 03:50:33 -0700421static void git_proxy_connect(int fd[2], char *host)
Paul Collinsf8014772005-11-04 14:57:16 +0000422{
Timo Hirvonen554fe202006-06-28 12:04:39 +0300423 const char *port = STR(DEFAULT_GIT_PORT);
Paul Collinsf8014772005-11-04 14:57:16 +0000424 char *colon, *end;
Shawn O. Pearce15a1c0122007-03-12 19:00:19 -0400425 const char *argv[4];
426 struct child_process proxy;
Paul Collinsf8014772005-11-04 14:57:16 +0000427
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
Shawn O. Pearce15a1c0122007-03-12 19:00:19 -0400445 argv[0] = git_proxy_command;
446 argv[1] = host;
447 argv[2] = port;
448 argv[3] = NULL;
449 memset(&proxy, 0, sizeof(proxy));
450 proxy.argv = argv;
451 proxy.in = -1;
452 proxy.out = -1;
453 if (start_command(&proxy))
454 die("cannot start proxy %s", argv[0]);
455 fd[0] = proxy.out; /* read from proxy stdout */
456 fd[1] = proxy.in; /* write to proxy stdin */
Paul Collinsf8014772005-11-04 14:57:16 +0000457}
458
Christian Couder0f503d72006-09-11 07:04:50 +0200459#define MAX_CMD_LEN 1024
460
Luben Tuikov2e776662007-09-01 02:36:31 -0700461char *get_port(char *host)
462{
463 char *end;
464 char *p = strchr(host, ':');
465
466 if (p) {
467 strtol(p+1, &end, 10);
468 if (*end == '\0') {
469 *p = '\0';
470 return p+1;
471 }
472 }
473
474 return NULL;
475}
476
Johannes Schindelinfb32c912008-02-10 03:06:57 +0000477static struct child_process no_fork;
478
Linus Torvaldsf7192592005-07-04 11:57:58 -0700479/*
Johannes Schindelinfb32c912008-02-10 03:06:57 +0000480 * This returns a dummy child_process if the transport protocol does not
481 * need fork(2), or a struct child_process object if it does. Once done,
482 * finish the connection with finish_connect() with the value returned from
483 * this function (it is safe to call finish_connect() with NULL to support
484 * the former case).
Franck Bui-Huuf42a5c42006-09-12 11:00:13 +0200485 *
Johannes Schindelinfb32c912008-02-10 03:06:57 +0000486 * If it returns, the connect is successful; it just dies on errors (this
487 * will hopefully be changed in a libification effort, to return NULL when
488 * the connection failed).
Linus Torvaldsf7192592005-07-04 11:57:58 -0700489 */
Daniel Barkalow45773702007-10-29 21:05:40 -0400490struct child_process *git_connect(int fd[2], const char *url_orig,
Johannes Sixt98158e92007-10-19 21:47:53 +0200491 const char *prog, int flags)
Linus Torvaldsf7192592005-07-04 11:57:58 -0700492{
Daniel Barkalow45773702007-10-29 21:05:40 -0400493 char *url = xstrdup(url_orig);
Andreas Ericssonfaea9cc2005-11-17 20:37:14 +0100494 char *host, *path = url;
YOSHIFUJI Hideaki / 吉藤英明356bece2005-12-21 19:23:42 +0900495 char *end;
496 int c;
Johannes Sixt98158e92007-10-19 21:47:53 +0200497 struct child_process *conn;
Andreas Ericssonfaea9cc2005-11-17 20:37:14 +0100498 enum protocol protocol = PROTO_LOCAL;
Serge E. Hallynda2a95b2006-04-17 10:14:47 -0500499 int free_path = 0;
Luben Tuikov2e776662007-09-01 02:36:31 -0700500 char *port = NULL;
Johannes Sixtf364cb82007-10-19 21:47:54 +0200501 const char **arg;
502 struct strbuf cmd;
Linus Torvaldsf7192592005-07-04 11:57:58 -0700503
Junio C Hamanof0b73672006-06-19 18:25:21 -0700504 /* Without this we cannot rely on waitpid() to tell
505 * what happened to our children.
506 */
507 signal(SIGCHLD, SIG_DFL);
508
Andreas Ericssonfaea9cc2005-11-17 20:37:14 +0100509 host = strstr(url, "://");
510 if(host) {
511 *host = '\0';
512 protocol = get_protocol(url);
513 host += 3;
YOSHIFUJI Hideaki / 吉藤英明356bece2005-12-21 19:23:42 +0900514 c = '/';
515 } else {
Linus Torvaldsf7192592005-07-04 11:57:58 -0700516 host = url;
YOSHIFUJI Hideaki / 吉藤英明356bece2005-12-21 19:23:42 +0900517 c = ':';
518 }
519
520 if (host[0] == '[') {
521 end = strchr(host + 1, ']');
522 if (end) {
523 *end = 0;
524 end++;
525 host++;
526 } else
527 end = host;
528 } else
529 end = host;
530
531 path = strchr(end, c);
Linus Torvalds72a4f4b2007-08-01 10:03:37 -0700532 if (path) {
533 if (c == ':') {
Andreas Ericssonfaea9cc2005-11-17 20:37:14 +0100534 protocol = PROTO_SSH;
YOSHIFUJI Hideaki / 吉藤英明356bece2005-12-21 19:23:42 +0900535 *path++ = '\0';
Linus Torvalds72a4f4b2007-08-01 10:03:37 -0700536 }
537 } else
538 path = end;
Linus Torvalds2386d652005-07-13 18:46:20 -0700539
Andreas Ericssonfaea9cc2005-11-17 20:37:14 +0100540 if (!path || !*path)
541 die("No path specified. See 'man git-pull' for valid url syntax");
542
543 /*
544 * null-terminate hostname and point path to ~ for URL's like this:
545 * ssh://host.xz/~user/repo
546 */
547 if (protocol != PROTO_LOCAL && host != url) {
548 char *ptr = path;
549 if (path[1] == '~')
550 path++;
Serge E. Hallynda2a95b2006-04-17 10:14:47 -0500551 else {
Shawn Pearce9befac42006-09-02 00:16:31 -0400552 path = xstrdup(ptr);
Serge E. Hallynda2a95b2006-04-17 10:14:47 -0500553 free_path = 1;
554 }
Andreas Ericssonfaea9cc2005-11-17 20:37:14 +0100555
556 *ptr = '\0';
557 }
558
Luben Tuikov2e776662007-09-01 02:36:31 -0700559 /*
560 * Add support for ssh port: ssh://host.xy:<port>/...
561 */
562 if (protocol == PROTO_SSH && host != url)
563 port = get_port(host);
564
Paul Collinsf8014772005-11-04 14:57:16 +0000565 if (protocol == PROTO_GIT) {
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500566 /* These underlying connection commands die() if they
567 * cannot connect.
568 */
Shawn Pearce9befac42006-09-02 00:16:31 -0400569 char *target_host = xstrdup(host);
Junio C Hamanoe814bc42005-11-19 03:48:56 -0800570 if (git_use_proxy(host))
Junio C Hamanoc78963d2006-06-28 03:50:33 -0700571 git_proxy_connect(fd, host);
Serge E. Hallynda2a95b2006-04-17 10:14:47 -0500572 else
Michael S. Tsirkin7841ce72007-05-16 20:09:41 +0300573 git_tcp_connect(fd, host, flags);
Jon Loeliger5ad312b2006-06-06 22:58:41 -0500574 /*
575 * Separate original protocol components prog and path
576 * from extended components with a NUL byte.
577 */
578 packet_write(fd[1],
579 "%s %s%chost=%s%c",
580 prog, path, 0,
581 target_host, 0);
582 free(target_host);
Daniel Barkalow45773702007-10-29 21:05:40 -0400583 free(url);
Serge E. Hallynda2a95b2006-04-17 10:14:47 -0500584 if (free_path)
585 free(path);
Johannes Schindelinfb32c912008-02-10 03:06:57 +0000586 return &no_fork;
Paul Collinsf8014772005-11-04 14:57:16 +0000587 }
Linus Torvalds2386d652005-07-13 18:46:20 -0700588
Johannes Sixt98158e92007-10-19 21:47:53 +0200589 conn = xcalloc(1, sizeof(*conn));
Christian Couder0f503d72006-09-11 07:04:50 +0200590
Johannes Sixtf364cb82007-10-19 21:47:54 +0200591 strbuf_init(&cmd, MAX_CMD_LEN);
592 strbuf_addstr(&cmd, prog);
593 strbuf_addch(&cmd, ' ');
594 sq_quote_buf(&cmd, path);
595 if (cmd.len >= MAX_CMD_LEN)
596 die("command line too long");
Christian Couder0f503d72006-09-11 07:04:50 +0200597
Johannes Sixtf364cb82007-10-19 21:47:54 +0200598 conn->in = conn->out = -1;
599 conn->argv = arg = xcalloc(6, sizeof(*arg));
600 if (protocol == PROTO_SSH) {
601 const char *ssh = getenv("GIT_SSH");
602 if (!ssh) ssh = "ssh";
Luben Tuikov2e776662007-09-01 02:36:31 -0700603
Johannes Sixtf364cb82007-10-19 21:47:54 +0200604 *arg++ = ssh;
605 if (port) {
606 *arg++ = "-p";
607 *arg++ = port;
Martin Sivak4852f722005-08-03 17:15:42 +0200608 }
Johannes Sixtf364cb82007-10-19 21:47:54 +0200609 *arg++ = host;
Matt Draisey016fb482006-01-19 15:58:03 -0500610 }
Johannes Sixtf364cb82007-10-19 21:47:54 +0200611 else {
612 /* remove these from the environment */
613 const char *env[] = {
614 ALTERNATE_DB_ENVIRONMENT,
615 DB_ENVIRONMENT,
616 GIT_DIR_ENVIRONMENT,
617 GIT_WORK_TREE_ENVIRONMENT,
618 GRAFT_ENVIRONMENT,
619 INDEX_ENVIRONMENT,
620 NULL
621 };
622 conn->env = env;
623 *arg++ = "sh";
624 *arg++ = "-c";
625 }
626 *arg++ = cmd.buf;
627 *arg = NULL;
628
629 if (start_command(conn))
630 die("unable to fork");
631
632 fd[0] = conn->out; /* read from child's stdout */
633 fd[1] = conn->in; /* write to child's stdin */
634 strbuf_release(&cmd);
Daniel Barkalow45773702007-10-29 21:05:40 -0400635 free(url);
Serge E. Hallynda2a95b2006-04-17 10:14:47 -0500636 if (free_path)
637 free(path);
Johannes Sixt98158e92007-10-19 21:47:53 +0200638 return conn;
Linus Torvaldsf7192592005-07-04 11:57:58 -0700639}
640
Johannes Sixt98158e92007-10-19 21:47:53 +0200641int finish_connect(struct child_process *conn)
Linus Torvaldsf7192592005-07-04 11:57:58 -0700642{
Johannes Sixtf364cb82007-10-19 21:47:54 +0200643 int code;
Johannes Schindelinfb32c912008-02-10 03:06:57 +0000644 if (!conn || conn == &no_fork)
Franck Bui-Huuf42a5c42006-09-12 11:00:13 +0200645 return 0;
646
Johannes Sixtf364cb82007-10-19 21:47:54 +0200647 code = finish_command(conn);
648 free(conn->argv);
Johannes Sixt98158e92007-10-19 21:47:53 +0200649 free(conn);
Johannes Sixtf364cb82007-10-19 21:47:54 +0200650 return code;
Linus Torvaldsf7192592005-07-04 11:57:58 -0700651}