1 #include "git-compat-util.h"
6 #include "run-command.h"
9 static char *server_capabilities;
11 static int check_ref(const char *name, int len, unsigned int flags)
16 if (len < 5 || memcmp(name, "refs/", 5))
19 /* Skip the "refs/" part */
23 /* REF_NORMAL means that we don't want the magic fake tag refs */
24 if ((flags & REF_NORMAL) && check_ref_format(name) < 0)
27 /* REF_HEADS means that we want regular branch heads */
28 if ((flags & REF_HEADS) && !memcmp(name, "heads/", 6))
31 /* REF_TAGS means that we want tags */
32 if ((flags & REF_TAGS) && !memcmp(name, "tags/", 5))
35 /* All type bits clear means that we are ok with anything */
36 return !(flags & ~REF_NORMAL);
39 int check_ref_type(const struct ref *ref, int flags)
41 return check_ref(ref->name, strlen(ref->name), flags);
44 static void add_extra_have(struct extra_have_objects *extra, unsigned char *sha1)
46 ALLOC_GROW(extra->array, extra->nr + 1, extra->alloc);
47 hashcpy(&(extra->array[extra->nr][0]), sha1);
52 * Read all the refs from the other end
54 struct ref **get_remote_heads(int in, struct ref **list,
55 int nr_match, char **match,
57 struct extra_have_objects *extra_have)
62 unsigned char old_sha1[20];
63 static char buffer[1000];
67 len = packet_read_line(in, buffer, sizeof(buffer));
70 if (buffer[len-1] == '\n')
73 if (len > 4 && !prefixcmp(buffer, "ERR "))
74 die("remote error: %s", buffer + 4);
76 if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
77 die("protocol error: expected sha/ref, got '%s'", buffer);
80 name_len = strlen(name);
81 if (len != name_len + 41) {
82 free(server_capabilities);
83 server_capabilities = xstrdup(name + name_len + 1);
87 name_len == 5 && !memcmp(".have", name, 5)) {
88 add_extra_have(extra_have, old_sha1);
92 if (!check_ref(name, name_len, flags))
94 if (nr_match && !path_match(name, nr_match, match))
96 ref = alloc_ref(buffer + 41);
97 hashcpy(ref->old_sha1, old_sha1);
104 int server_supports(const char *feature)
106 return server_capabilities &&
107 strstr(server_capabilities, feature) != NULL;
110 int path_match(const char *path, int nr, char **match)
113 int pathlen = strlen(path);
115 for (i = 0; i < nr; i++) {
119 if (!len || len > pathlen)
121 if (memcmp(path + pathlen - len, s, len))
123 if (pathlen > len && path[pathlen - len - 1] != '/')
137 static enum protocol get_protocol(const char *name)
139 if (!strcmp(name, "ssh"))
141 if (!strcmp(name, "git"))
143 if (!strcmp(name, "git+ssh"))
145 if (!strcmp(name, "ssh+git"))
147 if (!strcmp(name, "file"))
149 die("I don't handle protocol '%s'", name);
153 #define STR(s) STR_(s)
157 static const char *ai_name(const struct addrinfo *ai)
159 static char addr[NI_MAXHOST];
160 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0,
161 NI_NUMERICHOST) != 0)
162 strcpy(addr, "(unknown)");
168 * Returns a connected socket() fd, or else die()s.
170 static int git_tcp_connect_sock(char *host, int flags)
172 int sockfd = -1, saved_errno = 0;
174 const char *port = STR(DEFAULT_GIT_PORT);
175 struct addrinfo hints, *ai0, *ai;
179 if (host[0] == '[') {
180 end = strchr(host + 1, ']');
189 colon = strchr(end, ':');
198 memset(&hints, 0, sizeof(hints));
199 hints.ai_socktype = SOCK_STREAM;
200 hints.ai_protocol = IPPROTO_TCP;
202 if (flags & CONNECT_VERBOSE)
203 fprintf(stderr, "Looking up %s ... ", host);
205 gai = getaddrinfo(host, port, &hints, &ai);
207 die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai));
209 if (flags & CONNECT_VERBOSE)
210 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
212 for (ai0 = ai; ai; ai = ai->ai_next) {
213 sockfd = socket(ai->ai_family,
214 ai->ai_socktype, ai->ai_protocol);
219 if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
221 fprintf(stderr, "%s[%d: %s]: errno=%s\n",
225 strerror(saved_errno));
230 if (flags & CONNECT_VERBOSE)
231 fprintf(stderr, "%s ", ai_name(ai));
238 die("unable to connect a socket (%s)", strerror(saved_errno));
240 if (flags & CONNECT_VERBOSE)
241 fprintf(stderr, "done.\n");
249 * Returns a connected socket() fd, or else die()s.
251 static int git_tcp_connect_sock(char *host, int flags)
253 int sockfd = -1, saved_errno = 0;
255 char *port = STR(DEFAULT_GIT_PORT), *ep;
257 struct sockaddr_in sa;
262 if (host[0] == '[') {
263 end = strchr(host + 1, ']');
272 colon = strchr(end, ':');
279 if (flags & CONNECT_VERBOSE)
280 fprintf(stderr, "Looking up %s ... ", host);
282 he = gethostbyname(host);
284 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
285 nport = strtoul(port, &ep, 10);
286 if ( ep == port || *ep ) {
288 struct servent *se = getservbyname(port,"tcp");
290 die("Unknown port %s", port);
294 if (flags & CONNECT_VERBOSE)
295 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
297 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
298 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
304 memset(&sa, 0, sizeof sa);
305 sa.sin_family = he->h_addrtype;
306 sa.sin_port = htons(nport);
307 memcpy(&sa.sin_addr, *ap, he->h_length);
309 if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
311 fprintf(stderr, "%s[%d: %s]: errno=%s\n",
314 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
315 strerror(saved_errno));
320 if (flags & CONNECT_VERBOSE)
321 fprintf(stderr, "%s ",
322 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
327 die("unable to connect a socket (%s)", strerror(saved_errno));
329 if (flags & CONNECT_VERBOSE)
330 fprintf(stderr, "done.\n");
338 static void git_tcp_connect(int fd[2], char *host, int flags)
340 int sockfd = git_tcp_connect_sock(host, flags);
347 static char *git_proxy_command;
349 static int git_proxy_command_options(const char *var, const char *value,
352 if (!strcmp(var, "core.gitproxy")) {
356 const char *rhost_name = cb;
357 int rhost_len = strlen(rhost_name);
359 if (git_proxy_command)
362 return config_error_nonbool(var);
364 * ;# matches www.kernel.org as well
365 * gitproxy = netcatter-1 for kernel.org
366 * gitproxy = netcatter-2 for sample.xz
367 * gitproxy = netcatter-default
369 for_pos = strstr(value, " for ");
371 /* matches everybody */
372 matchlen = strlen(value);
374 hostlen = strlen(for_pos + 5);
375 if (rhost_len < hostlen)
377 else if (!strncmp(for_pos + 5,
378 rhost_name + rhost_len - hostlen,
380 ((rhost_len == hostlen) ||
381 rhost_name[rhost_len - hostlen -1] == '.'))
382 matchlen = for_pos - value;
387 /* core.gitproxy = none for kernel.org */
389 !memcmp(value, "none", 4))
391 git_proxy_command = xmemdupz(value, matchlen);
396 return git_default_config(var, value, cb);
399 static int git_use_proxy(const char *host)
401 git_proxy_command = getenv("GIT_PROXY_COMMAND");
402 git_config(git_proxy_command_options, (void*)host);
403 return (git_proxy_command && *git_proxy_command);
406 static void git_proxy_connect(int fd[2], char *host)
408 const char *port = STR(DEFAULT_GIT_PORT);
411 struct child_process proxy;
413 if (host[0] == '[') {
414 end = strchr(host + 1, ']');
423 colon = strchr(end, ':');
430 argv[0] = git_proxy_command;
434 memset(&proxy, 0, sizeof(proxy));
438 if (start_command(&proxy))
439 die("cannot start proxy %s", argv[0]);
440 fd[0] = proxy.out; /* read from proxy stdout */
441 fd[1] = proxy.in; /* write to proxy stdin */
444 #define MAX_CMD_LEN 1024
446 static char *get_port(char *host)
449 char *p = strchr(host, ':');
452 long port = strtol(p + 1, &end, 10);
453 if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
462 static struct child_process no_fork;
465 * This returns a dummy child_process if the transport protocol does not
466 * need fork(2), or a struct child_process object if it does. Once done,
467 * finish the connection with finish_connect() with the value returned from
468 * this function (it is safe to call finish_connect() with NULL to support
471 * If it returns, the connect is successful; it just dies on errors (this
472 * will hopefully be changed in a libification effort, to return NULL when
473 * the connection failed).
475 struct child_process *git_connect(int fd[2], const char *url_orig,
476 const char *prog, int flags)
478 char *url = xstrdup(url_orig);
482 struct child_process *conn;
483 enum protocol protocol = PROTO_LOCAL;
489 /* Without this we cannot rely on waitpid() to tell
490 * what happened to our children.
492 signal(SIGCHLD, SIG_DFL);
494 host = strstr(url, "://");
497 protocol = get_protocol(url);
505 if (host[0] == '[') {
506 end = strchr(host + 1, ']');
516 path = strchr(end, c);
517 if (path && !has_dos_drive_prefix(end)) {
519 protocol = PROTO_SSH;
526 die("No path specified. See 'man git-pull' for valid url syntax");
529 * null-terminate hostname and point path to ~ for URL's like this:
530 * ssh://host.xz/~user/repo
532 if (protocol != PROTO_LOCAL && host != url) {
545 * Add support for ssh port: ssh://host.xy:<port>/...
547 if (protocol == PROTO_SSH && host != url)
548 port = get_port(host);
550 if (protocol == PROTO_GIT) {
551 /* These underlying connection commands die() if they
554 char *target_host = xstrdup(host);
555 if (git_use_proxy(host))
556 git_proxy_connect(fd, host);
558 git_tcp_connect(fd, host, flags);
560 * Separate original protocol components prog and path
561 * from extended host header with a NUL byte.
563 * Note: Do not add any other headers here! Doing so
564 * will cause older git-daemon servers to crash.
577 conn = xcalloc(1, sizeof(*conn));
579 strbuf_init(&cmd, MAX_CMD_LEN);
580 strbuf_addstr(&cmd, prog);
581 strbuf_addch(&cmd, ' ');
582 sq_quote_buf(&cmd, path);
583 if (cmd.len >= MAX_CMD_LEN)
584 die("command line too long");
586 conn->in = conn->out = -1;
587 conn->argv = arg = xcalloc(7, sizeof(*arg));
588 if (protocol == PROTO_SSH) {
589 const char *ssh = getenv("GIT_SSH");
590 int putty = ssh && strcasestr(ssh, "plink");
591 if (!ssh) ssh = "ssh";
594 if (putty && !strcasestr(ssh, "tortoiseplink"))
597 /* P is for PuTTY, p is for OpenSSH */
598 *arg++ = putty ? "-P" : "-p";
604 /* remove these from the environment */
605 const char *env[] = {
606 ALTERNATE_DB_ENVIRONMENT,
609 GIT_WORK_TREE_ENVIRONMENT,
612 NO_REPLACE_OBJECTS_ENVIRONMENT,
621 if (start_command(conn))
622 die("unable to fork");
624 fd[0] = conn->out; /* read from child's stdout */
625 fd[1] = conn->in; /* write to child's stdin */
626 strbuf_release(&cmd);
633 int finish_connect(struct child_process *conn)
636 if (!conn || conn == &no_fork)
639 code = finish_command(conn);