builtin-pack-objects: don't fail, if delta is not possible
[alt-git.git] / connect.c
blob8cbda88dda02e7de74149af1b387e537f77bc497
1 #include "git-compat-util.h"
2 #include "cache.h"
3 #include "pkt-line.h"
4 #include "quote.h"
5 #include "refs.h"
6 #include "run-command.h"
7 #include "remote.h"
9 static char *server_capabilities;
11 static int check_ref(const char *name, int len, unsigned int flags)
13 if (!flags)
14 return 1;
16 if (len < 5 || memcmp(name, "refs/", 5))
17 return 0;
19 /* Skip the "refs/" part */
20 name += 5;
21 len -= 5;
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;
27 /* REF_HEADS means that we want regular branch heads */
28 if ((flags & REF_HEADS) && !memcmp(name, "heads/", 6))
29 return 1;
31 /* REF_TAGS means that we want tags */
32 if ((flags & REF_TAGS) && !memcmp(name, "tags/", 5))
33 return 1;
35 /* All type bits clear means that we are ok with anything */
36 return !(flags & ~REF_NORMAL);
40 * Read all the refs from the other end
42 struct ref **get_remote_heads(int in, struct ref **list,
43 int nr_match, char **match,
44 unsigned int flags)
46 *list = NULL;
47 for (;;) {
48 struct ref *ref;
49 unsigned char old_sha1[20];
50 static char buffer[1000];
51 char *name;
52 int len, name_len;
54 len = packet_read_line(in, buffer, sizeof(buffer));
55 if (!len)
56 break;
57 if (buffer[len-1] == '\n')
58 buffer[--len] = 0;
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;
64 name_len = strlen(name);
65 if (len != name_len + 41) {
66 if (server_capabilities)
67 free(server_capabilities);
68 server_capabilities = xstrdup(name + name_len + 1);
71 if (!check_ref(name, name_len, flags))
72 continue;
73 if (nr_match && !path_match(name, nr_match, match))
74 continue;
75 ref = xcalloc(1, sizeof(*ref) + len - 40);
76 hashcpy(ref->old_sha1, old_sha1);
77 memcpy(ref->name, buffer + 41, len - 40);
78 *list = ref;
79 list = &ref->next;
81 return list;
84 int server_supports(const char *feature)
86 return server_capabilities &&
87 strstr(server_capabilities, feature) != NULL;
90 int get_ack(int fd, unsigned char *result_sha1)
92 static char line[1000];
93 int len = packet_read_line(fd, line, sizeof(line));
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;
101 if (!prefixcmp(line, "ACK ")) {
102 if (!get_sha1_hex(line+4, result_sha1)) {
103 if (strstr(line+45, "continue"))
104 return 2;
105 return 1;
108 die("git-fetch_pack: expected ACK/NAK, got '%s'", line);
111 int path_match(const char *path, int nr, char **match)
113 int i;
114 int pathlen = strlen(path);
116 for (i = 0; i < nr; i++) {
117 char *s = match[i];
118 int len = strlen(s);
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;
127 return (i + 1);
129 return 0;
132 enum protocol {
133 PROTO_LOCAL = 1,
134 PROTO_SSH,
135 PROTO_GIT,
138 static enum protocol get_protocol(const char *name)
140 if (!strcmp(name, "ssh"))
141 return PROTO_SSH;
142 if (!strcmp(name, "git"))
143 return PROTO_GIT;
144 if (!strcmp(name, "git+ssh"))
145 return PROTO_SSH;
146 if (!strcmp(name, "ssh+git"))
147 return PROTO_SSH;
148 die("I don't handle protocol '%s'", name);
151 #define STR_(s) # s
152 #define STR(s) STR_(s)
154 #ifndef NO_IPV6
156 static const char *ai_name(const struct addrinfo *ai)
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)");
170 return addr;
174 * Returns a connected socket() fd, or else die()s.
176 static int git_tcp_connect_sock(char *host, int flags)
178 int sockfd = -1, saved_errno = 0;
179 char *colon, *end;
180 const char *port = STR(DEFAULT_GIT_PORT);
181 struct addrinfo hints, *ai0, *ai;
182 int gai;
183 int cnt = 0;
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, ':');
197 if (colon) {
198 *colon = 0;
199 port = colon + 1;
200 if (!*port)
201 port = "<none>";
204 memset(&hints, 0, sizeof(hints));
205 hints.ai_socktype = SOCK_STREAM;
206 hints.ai_protocol = IPPROTO_TCP;
208 if (flags & CONNECT_VERBOSE)
209 fprintf(stderr, "Looking up %s ... ", host);
211 gai = getaddrinfo(host, port, &hints, &ai);
212 if (gai)
213 die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai));
215 if (flags & CONNECT_VERBOSE)
216 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
218 for (ai0 = ai; ai; ai = ai->ai_next) {
219 sockfd = socket(ai->ai_family,
220 ai->ai_socktype, ai->ai_protocol);
221 if (sockfd < 0) {
222 saved_errno = errno;
223 continue;
225 if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
226 saved_errno = errno;
227 fprintf(stderr, "%s[%d: %s]: net=%s, errno=%s\n",
228 host,
229 cnt,
230 ai_name(ai),
231 hstrerror(h_errno),
232 strerror(saved_errno));
233 close(sockfd);
234 sockfd = -1;
235 continue;
237 if (flags & CONNECT_VERBOSE)
238 fprintf(stderr, "%s ", ai_name(ai));
239 break;
242 freeaddrinfo(ai0);
244 if (sockfd < 0)
245 die("unable to connect a socket (%s)", strerror(saved_errno));
247 if (flags & CONNECT_VERBOSE)
248 fprintf(stderr, "done.\n");
250 return sockfd;
253 #else /* NO_IPV6 */
256 * Returns a connected socket() fd, or else die()s.
258 static int git_tcp_connect_sock(char *host, int flags)
260 int sockfd = -1, saved_errno = 0;
261 char *colon, *end;
262 char *port = STR(DEFAULT_GIT_PORT), *ep;
263 struct hostent *he;
264 struct sockaddr_in sa;
265 char **ap;
266 unsigned int nport;
267 int cnt;
269 if (host[0] == '[') {
270 end = strchr(host + 1, ']');
271 if (end) {
272 *end = 0;
273 end++;
274 host++;
275 } else
276 end = host;
277 } else
278 end = host;
279 colon = strchr(end, ':');
281 if (colon) {
282 *colon = 0;
283 port = colon + 1;
286 if (flags & CONNECT_VERBOSE)
287 fprintf(stderr, "Looking up %s ... ", host);
289 he = gethostbyname(host);
290 if (!he)
291 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
292 nport = strtoul(port, &ep, 10);
293 if ( ep == port || *ep ) {
294 /* Not numeric */
295 struct servent *se = getservbyname(port,"tcp");
296 if ( !se )
297 die("Unknown port %s\n", port);
298 nport = se->s_port;
301 if (flags & CONNECT_VERBOSE)
302 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
304 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
305 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
306 if (sockfd < 0) {
307 saved_errno = errno;
308 continue;
311 memset(&sa, 0, sizeof sa);
312 sa.sin_family = he->h_addrtype;
313 sa.sin_port = htons(nport);
314 memcpy(&sa.sin_addr, *ap, he->h_length);
316 if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
317 saved_errno = errno;
318 fprintf(stderr, "%s[%d: %s]: net=%s, errno=%s\n",
319 host,
320 cnt,
321 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
322 hstrerror(h_errno),
323 strerror(saved_errno));
324 close(sockfd);
325 sockfd = -1;
326 continue;
328 if (flags & CONNECT_VERBOSE)
329 fprintf(stderr, "%s ",
330 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
331 break;
334 if (sockfd < 0)
335 die("unable to connect a socket (%s)", strerror(saved_errno));
337 if (flags & CONNECT_VERBOSE)
338 fprintf(stderr, "done.\n");
340 return sockfd;
343 #endif /* NO_IPV6 */
346 static void git_tcp_connect(int fd[2], char *host, int flags)
348 int sockfd = git_tcp_connect_sock(host, flags);
350 fd[0] = sockfd;
351 fd[1] = dup(sockfd);
355 static char *git_proxy_command;
356 static const char *rhost_name;
357 static int rhost_len;
359 static int git_proxy_command_options(const char *var, const char *value)
361 if (!strcmp(var, "core.gitproxy")) {
362 const char *for_pos;
363 int matchlen = -1;
364 int hostlen;
366 if (git_proxy_command)
367 return 0;
368 /* [core]
369 * ;# matches www.kernel.org as well
370 * gitproxy = netcatter-1 for kernel.org
371 * gitproxy = netcatter-2 for sample.xz
372 * gitproxy = netcatter-default
374 for_pos = strstr(value, " for ");
375 if (!for_pos)
376 /* matches everybody */
377 matchlen = strlen(value);
378 else {
379 hostlen = strlen(for_pos + 5);
380 if (rhost_len < hostlen)
381 matchlen = -1;
382 else if (!strncmp(for_pos + 5,
383 rhost_name + rhost_len - hostlen,
384 hostlen) &&
385 ((rhost_len == hostlen) ||
386 rhost_name[rhost_len - hostlen -1] == '.'))
387 matchlen = for_pos - value;
388 else
389 matchlen = -1;
391 if (0 <= matchlen) {
392 /* core.gitproxy = none for kernel.org */
393 if (matchlen == 4 &&
394 !memcmp(value, "none", 4))
395 matchlen = 0;
396 git_proxy_command = xmalloc(matchlen + 1);
397 memcpy(git_proxy_command, value, matchlen);
398 git_proxy_command[matchlen] = 0;
400 return 0;
403 return git_default_config(var, value);
406 static int git_use_proxy(const char *host)
408 rhost_name = host;
409 rhost_len = strlen(host);
410 git_proxy_command = getenv("GIT_PROXY_COMMAND");
411 git_config(git_proxy_command_options);
412 rhost_name = NULL;
413 return (git_proxy_command && *git_proxy_command);
416 static void git_proxy_connect(int fd[2], char *host)
418 const char *port = STR(DEFAULT_GIT_PORT);
419 char *colon, *end;
420 const char *argv[4];
421 struct child_process proxy;
423 if (host[0] == '[') {
424 end = strchr(host + 1, ']');
425 if (end) {
426 *end = 0;
427 end++;
428 host++;
429 } else
430 end = host;
431 } else
432 end = host;
433 colon = strchr(end, ':');
435 if (colon) {
436 *colon = 0;
437 port = colon + 1;
440 argv[0] = git_proxy_command;
441 argv[1] = host;
442 argv[2] = port;
443 argv[3] = NULL;
444 memset(&proxy, 0, sizeof(proxy));
445 proxy.argv = argv;
446 proxy.in = -1;
447 proxy.out = -1;
448 if (start_command(&proxy))
449 die("cannot start proxy %s", argv[0]);
450 fd[0] = proxy.out; /* read from proxy stdout */
451 fd[1] = proxy.in; /* write to proxy stdin */
454 #define MAX_CMD_LEN 1024
457 * This returns 0 if the transport protocol does not need fork(2),
458 * or a process id if it does. Once done, finish the connection
459 * with finish_connect() with the value returned from this function
460 * (it is safe to call finish_connect() with 0 to support the former
461 * case).
463 * Does not return a negative value on error; it just dies.
465 pid_t git_connect(int fd[2], char *url, const char *prog, int flags)
467 char *host, *path = url;
468 char *end;
469 int c;
470 int pipefd[2][2];
471 pid_t pid;
472 enum protocol protocol = PROTO_LOCAL;
473 int free_path = 0;
475 /* Without this we cannot rely on waitpid() to tell
476 * what happened to our children.
478 signal(SIGCHLD, SIG_DFL);
480 host = strstr(url, "://");
481 if(host) {
482 *host = '\0';
483 protocol = get_protocol(url);
484 host += 3;
485 c = '/';
486 } else {
487 host = url;
488 c = ':';
491 if (host[0] == '[') {
492 end = strchr(host + 1, ']');
493 if (end) {
494 *end = 0;
495 end++;
496 host++;
497 } else
498 end = host;
499 } else
500 end = host;
502 path = strchr(end, c);
503 if (c == ':') {
504 if (path) {
505 protocol = PROTO_SSH;
506 *path++ = '\0';
507 } else
508 path = host;
511 if (!path || !*path)
512 die("No path specified. See 'man git-pull' for valid url syntax");
515 * null-terminate hostname and point path to ~ for URL's like this:
516 * ssh://host.xz/~user/repo
518 if (protocol != PROTO_LOCAL && host != url) {
519 char *ptr = path;
520 if (path[1] == '~')
521 path++;
522 else {
523 path = xstrdup(ptr);
524 free_path = 1;
527 *ptr = '\0';
530 if (protocol == PROTO_GIT) {
531 /* These underlying connection commands die() if they
532 * cannot connect.
534 char *target_host = xstrdup(host);
535 if (git_use_proxy(host))
536 git_proxy_connect(fd, host);
537 else
538 git_tcp_connect(fd, host, flags);
540 * Separate original protocol components prog and path
541 * from extended components with a NUL byte.
543 packet_write(fd[1],
544 "%s %s%chost=%s%c",
545 prog, path, 0,
546 target_host, 0);
547 free(target_host);
548 if (free_path)
549 free(path);
550 return 0;
553 if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
554 die("unable to create pipe pair for communication");
555 pid = fork();
556 if (pid < 0)
557 die("unable to fork");
558 if (!pid) {
559 char command[MAX_CMD_LEN];
560 char *posn = command;
561 int size = MAX_CMD_LEN;
562 int of = 0;
564 of |= add_to_string(&posn, &size, prog, 0);
565 of |= add_to_string(&posn, &size, " ", 0);
566 of |= add_to_string(&posn, &size, path, 1);
568 if (of)
569 die("command line too long");
571 dup2(pipefd[1][0], 0);
572 dup2(pipefd[0][1], 1);
573 close(pipefd[0][0]);
574 close(pipefd[0][1]);
575 close(pipefd[1][0]);
576 close(pipefd[1][1]);
577 if (protocol == PROTO_SSH) {
578 const char *ssh, *ssh_basename;
579 ssh = getenv("GIT_SSH");
580 if (!ssh) ssh = "ssh";
581 ssh_basename = strrchr(ssh, '/');
582 if (!ssh_basename)
583 ssh_basename = ssh;
584 else
585 ssh_basename++;
586 execlp(ssh, ssh_basename, host, command, NULL);
588 else {
589 unsetenv(ALTERNATE_DB_ENVIRONMENT);
590 unsetenv(DB_ENVIRONMENT);
591 unsetenv(GIT_DIR_ENVIRONMENT);
592 unsetenv(GRAFT_ENVIRONMENT);
593 unsetenv(INDEX_ENVIRONMENT);
594 execlp("sh", "sh", "-c", command, NULL);
596 die("exec failed");
598 fd[0] = pipefd[0][0];
599 fd[1] = pipefd[1][1];
600 close(pipefd[0][1]);
601 close(pipefd[1][0]);
602 if (free_path)
603 free(path);
604 return pid;
607 int finish_connect(pid_t pid)
609 if (pid == 0)
610 return 0;
612 while (waitpid(pid, NULL, 0) < 0) {
613 if (errno != EINTR)
614 return -1;
616 return 0;