1 #include "git-compat-util.h"
6 #include "run-command.h"
10 static char *server_capabilities;
12 static int check_ref(const char *name, int len, unsigned int flags)
17 if (len < 5 || memcmp(name, "refs/", 5))
20 /* Skip the "refs/" part */
24 /* REF_NORMAL means that we don't want the magic fake tag refs */
25 if ((flags & REF_NORMAL) && check_refname_format(name, 0))
28 /* REF_HEADS means that we want regular branch heads */
29 if ((flags & REF_HEADS) && !memcmp(name, "heads/", 6))
32 /* REF_TAGS means that we want tags */
33 if ((flags & REF_TAGS) && !memcmp(name, "tags/", 5))
36 /* All type bits clear means that we are ok with anything */
37 return !(flags & ~REF_NORMAL);
40 int check_ref_type(const struct ref *ref, int flags)
42 return check_ref(ref->name, strlen(ref->name), flags);
45 static void add_extra_have(struct extra_have_objects *extra, unsigned char *sha1)
47 ALLOC_GROW(extra->array, extra->nr + 1, extra->alloc);
48 hashcpy(&(extra->array[extra->nr][0]), sha1);
53 * Read all the refs from the other end
55 struct ref **get_remote_heads(int in, struct ref **list,
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 ref = alloc_ref(buffer + 41);
95 hashcpy(ref->old_sha1, old_sha1);
102 int server_supports(const char *feature)
104 return server_capabilities &&
105 strstr(server_capabilities, feature) != NULL;
108 int path_match(const char *path, int nr, char **match)
111 int pathlen = strlen(path);
113 for (i = 0; i < nr; i++) {
117 if (!len || len > pathlen)
119 if (memcmp(path + pathlen - len, s, len))
121 if (pathlen > len && path[pathlen - len - 1] != '/')
135 static enum protocol get_protocol(const char *name)
137 if (!strcmp(name, "ssh"))
139 if (!strcmp(name, "git"))
141 if (!strcmp(name, "git+ssh"))
143 if (!strcmp(name, "ssh+git"))
145 if (!strcmp(name, "file"))
147 die("I don't handle protocol '%s'", name);
151 #define STR(s) STR_(s)
153 static void get_host_and_port(char **host, const char **port)
157 if (*host[0] == '[') {
158 end = strchr(*host + 1, ']');
167 colon = strchr(end, ':');
177 static const char *ai_name(const struct addrinfo *ai)
179 static char addr[NI_MAXHOST];
180 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0,
181 NI_NUMERICHOST) != 0)
182 strcpy(addr, "(unknown)");
188 * Returns a connected socket() fd, or else die()s.
190 static int git_tcp_connect_sock(char *host, int flags)
192 struct strbuf error_message = STRBUF_INIT;
194 const char *port = STR(DEFAULT_GIT_PORT);
195 struct addrinfo hints, *ai0, *ai;
199 get_host_and_port(&host, &port);
203 memset(&hints, 0, sizeof(hints));
204 hints.ai_socktype = SOCK_STREAM;
205 hints.ai_protocol = IPPROTO_TCP;
207 if (flags & CONNECT_VERBOSE)
208 fprintf(stderr, "Looking up %s ... ", host);
210 gai = getaddrinfo(host, port, &hints, &ai);
212 die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai));
214 if (flags & CONNECT_VERBOSE)
215 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
217 for (ai0 = ai; ai; ai = ai->ai_next, cnt++) {
218 sockfd = socket(ai->ai_family,
219 ai->ai_socktype, ai->ai_protocol);
221 (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) {
222 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
223 host, cnt, ai_name(ai), strerror(errno));
229 if (flags & CONNECT_VERBOSE)
230 fprintf(stderr, "%s ", ai_name(ai));
237 die("unable to connect to %s:\n%s", host, error_message.buf);
239 if (flags & CONNECT_VERBOSE)
240 fprintf(stderr, "done.\n");
242 strbuf_release(&error_message);
250 * Returns a connected socket() fd, or else die()s.
252 static int git_tcp_connect_sock(char *host, int flags)
254 struct strbuf error_message = STRBUF_INIT;
256 const char *port = STR(DEFAULT_GIT_PORT);
259 struct sockaddr_in sa;
264 get_host_and_port(&host, &port);
266 if (flags & CONNECT_VERBOSE)
267 fprintf(stderr, "Looking up %s ... ", host);
269 he = gethostbyname(host);
271 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
272 nport = strtoul(port, &ep, 10);
273 if ( ep == port || *ep ) {
275 struct servent *se = getservbyname(port,"tcp");
277 die("Unknown port %s", port);
281 if (flags & CONNECT_VERBOSE)
282 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
284 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
285 memset(&sa, 0, sizeof sa);
286 sa.sin_family = he->h_addrtype;
287 sa.sin_port = htons(nport);
288 memcpy(&sa.sin_addr, *ap, he->h_length);
290 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
292 connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
293 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
296 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
303 if (flags & CONNECT_VERBOSE)
304 fprintf(stderr, "%s ",
305 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
310 die("unable to connect to %s:\n%s", host, error_message.buf);
312 if (flags & CONNECT_VERBOSE)
313 fprintf(stderr, "done.\n");
321 static void git_tcp_connect(int fd[2], char *host, int flags)
323 int sockfd = git_tcp_connect_sock(host, flags);
330 static char *git_proxy_command;
332 static int git_proxy_command_options(const char *var, const char *value,
335 if (!strcmp(var, "core.gitproxy")) {
339 const char *rhost_name = cb;
340 int rhost_len = strlen(rhost_name);
342 if (git_proxy_command)
345 return config_error_nonbool(var);
347 * ;# matches www.kernel.org as well
348 * gitproxy = netcatter-1 for kernel.org
349 * gitproxy = netcatter-2 for sample.xz
350 * gitproxy = netcatter-default
352 for_pos = strstr(value, " for ");
354 /* matches everybody */
355 matchlen = strlen(value);
357 hostlen = strlen(for_pos + 5);
358 if (rhost_len < hostlen)
360 else if (!strncmp(for_pos + 5,
361 rhost_name + rhost_len - hostlen,
363 ((rhost_len == hostlen) ||
364 rhost_name[rhost_len - hostlen -1] == '.'))
365 matchlen = for_pos - value;
370 /* core.gitproxy = none for kernel.org */
372 !memcmp(value, "none", 4))
374 git_proxy_command = xmemdupz(value, matchlen);
379 return git_default_config(var, value, cb);
382 static int git_use_proxy(const char *host)
384 git_proxy_command = getenv("GIT_PROXY_COMMAND");
385 git_config(git_proxy_command_options, (void*)host);
386 return (git_proxy_command && *git_proxy_command);
389 static struct child_process *git_proxy_connect(int fd[2], char *host)
391 const char *port = STR(DEFAULT_GIT_PORT);
393 struct child_process *proxy;
395 get_host_and_port(&host, &port);
397 argv = xmalloc(sizeof(*argv) * 4);
398 argv[0] = git_proxy_command;
402 proxy = xcalloc(1, sizeof(*proxy));
406 if (start_command(proxy))
407 die("cannot start proxy %s", argv[0]);
408 fd[0] = proxy->out; /* read from proxy stdout */
409 fd[1] = proxy->in; /* write to proxy stdin */
413 #define MAX_CMD_LEN 1024
415 static char *get_port(char *host)
418 char *p = strchr(host, ':');
421 long port = strtol(p + 1, &end, 10);
422 if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
431 static struct child_process no_fork;
434 * This returns a dummy child_process if the transport protocol does not
435 * need fork(2), or a struct child_process object if it does. Once done,
436 * finish the connection with finish_connect() with the value returned from
437 * this function (it is safe to call finish_connect() with NULL to support
440 * If it returns, the connect is successful; it just dies on errors (this
441 * will hopefully be changed in a libification effort, to return NULL when
442 * the connection failed).
444 struct child_process *git_connect(int fd[2], const char *url_orig,
445 const char *prog, int flags)
451 struct child_process *conn = &no_fork;
452 enum protocol protocol = PROTO_LOCAL;
458 /* Without this we cannot rely on waitpid() to tell
459 * what happened to our children.
461 signal(SIGCHLD, SIG_DFL);
463 if (is_url(url_orig))
464 url = url_decode(url_orig);
466 url = xstrdup(url_orig);
468 host = strstr(url, "://");
471 protocol = get_protocol(url);
480 * Don't do destructive transforms with git:// as that
481 * protocol code does '[]' unwrapping of its own.
483 if (host[0] == '[') {
484 end = strchr(host + 1, ']');
486 if (protocol != PROTO_GIT) {
496 path = strchr(end, c);
497 if (path && !has_dos_drive_prefix(end)) {
499 protocol = PROTO_SSH;
506 die("No path specified. See 'man git-pull' for valid url syntax");
509 * null-terminate hostname and point path to ~ for URL's like this:
510 * ssh://host.xz/~user/repo
512 if (protocol != PROTO_LOCAL && host != url) {
525 * Add support for ssh port: ssh://host.xy:<port>/...
527 if (protocol == PROTO_SSH && host != url)
528 port = get_port(host);
530 if (protocol == PROTO_GIT) {
531 /* These underlying connection commands die() if they
534 char *target_host = xstrdup(host);
535 if (git_use_proxy(host))
536 conn = git_proxy_connect(fd, host);
538 git_tcp_connect(fd, host, flags);
540 * Separate original protocol components prog and path
541 * from extended host header with a NUL byte.
543 * Note: Do not add any other headers here! Doing so
544 * will cause older git-daemon servers to crash.
557 conn = xcalloc(1, sizeof(*conn));
559 strbuf_init(&cmd, MAX_CMD_LEN);
560 strbuf_addstr(&cmd, prog);
561 strbuf_addch(&cmd, ' ');
562 sq_quote_buf(&cmd, path);
563 if (cmd.len >= MAX_CMD_LEN)
564 die("command line too long");
566 conn->in = conn->out = -1;
567 conn->argv = arg = xcalloc(7, sizeof(*arg));
568 if (protocol == PROTO_SSH) {
569 const char *ssh = getenv("GIT_SSH");
570 int putty = ssh && strcasestr(ssh, "plink");
571 if (!ssh) ssh = "ssh";
574 if (putty && !strcasestr(ssh, "tortoiseplink"))
577 /* P is for PuTTY, p is for OpenSSH */
578 *arg++ = putty ? "-P" : "-p";
584 /* remove repo-local variables from the environment */
585 conn->env = local_repo_env;
591 if (start_command(conn))
592 die("unable to fork");
594 fd[0] = conn->out; /* read from child's stdout */
595 fd[1] = conn->in; /* write to child's stdin */
596 strbuf_release(&cmd);
603 int git_connection_is_socket(struct child_process *conn)
605 return conn == &no_fork;
608 int finish_connect(struct child_process *conn)
611 if (!conn || git_connection_is_socket(conn))
614 code = finish_command(conn);
620 char *git_getpass(const char *prompt)
623 struct child_process pass;
625 static struct strbuf buffer = STRBUF_INIT;
627 askpass = getenv("GIT_ASKPASS");
629 askpass = askpass_program;
631 askpass = getenv("SSH_ASKPASS");
632 if (!askpass || !(*askpass)) {
633 char *result = getpass(prompt);
635 die_errno("Could not read password");
643 memset(&pass, 0, sizeof(pass));
647 if (start_command(&pass))
650 strbuf_reset(&buffer);
651 if (strbuf_read(&buffer, pass.out, 20) < 0)
652 die("failed to read password from %s\n", askpass);
656 if (finish_command(&pass))
659 strbuf_setlen(&buffer, strcspn(buffer.buf, "\r\n"));