Git.pm: assorted build related fixes.
[git/dkf.git] / connect.c
blob9a87bd999a1d779c212a417367758cad0bb4e23a
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 <sys/wait.h>
7 #include <sys/socket.h>
8 #include <netinet/in.h>
9 #include <arpa/inet.h>
10 #include <netdb.h>
11 #include <signal.h>
13 static char *server_capabilities = NULL;
16 * Read all the refs from the other end
18 struct ref **get_remote_heads(int in, struct ref **list,
19 int nr_match, char **match, int ignore_funny)
21 *list = NULL;
22 for (;;) {
23 struct ref *ref;
24 unsigned char old_sha1[20];
25 static char buffer[1000];
26 char *name;
27 int len, name_len;
29 len = packet_read_line(in, buffer, sizeof(buffer));
30 if (!len)
31 break;
32 if (buffer[len-1] == '\n')
33 buffer[--len] = 0;
35 if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
36 die("protocol error: expected sha/ref, got '%s'", buffer);
37 name = buffer + 41;
39 name_len = strlen(name);
40 if (len != name_len + 41) {
41 if (server_capabilities)
42 free(server_capabilities);
43 server_capabilities = strdup(name + name_len + 1);
46 if (ignore_funny && 45 < len && !memcmp(name, "refs/", 5) &&
47 check_ref_format(name + 5))
48 continue;
50 if (nr_match && !path_match(name, nr_match, match))
51 continue;
52 ref = xcalloc(1, sizeof(*ref) + len - 40);
53 memcpy(ref->old_sha1, old_sha1, 20);
54 memcpy(ref->name, buffer + 41, len - 40);
55 *list = ref;
56 list = &ref->next;
58 return list;
61 int server_supports(const char *feature)
63 return server_capabilities &&
64 strstr(server_capabilities, feature) != NULL;
67 int get_ack(int fd, unsigned char *result_sha1)
69 static char line[1000];
70 int len = packet_read_line(fd, line, sizeof(line));
72 if (!len)
73 die("git-fetch-pack: expected ACK/NAK, got EOF");
74 if (line[len-1] == '\n')
75 line[--len] = 0;
76 if (!strcmp(line, "NAK"))
77 return 0;
78 if (!strncmp(line, "ACK ", 4)) {
79 if (!get_sha1_hex(line+4, result_sha1)) {
80 if (strstr(line+45, "continue"))
81 return 2;
82 return 1;
85 die("git-fetch_pack: expected ACK/NAK, got '%s'", line);
88 int path_match(const char *path, int nr, char **match)
90 int i;
91 int pathlen = strlen(path);
93 for (i = 0; i < nr; i++) {
94 char *s = match[i];
95 int len = strlen(s);
97 if (!len || len > pathlen)
98 continue;
99 if (memcmp(path + pathlen - len, s, len))
100 continue;
101 if (pathlen > len && path[pathlen - len - 1] != '/')
102 continue;
103 *s = 0;
104 return (i + 1);
106 return 0;
109 struct refspec {
110 char *src;
111 char *dst;
112 char force;
116 * A:B means fast forward remote B with local A.
117 * +A:B means overwrite remote B with local A.
118 * +A is a shorthand for +A:A.
119 * A is a shorthand for A:A.
121 static struct refspec *parse_ref_spec(int nr_refspec, char **refspec)
123 int i;
124 struct refspec *rs = xcalloc(sizeof(*rs), (nr_refspec + 1));
125 for (i = 0; i < nr_refspec; i++) {
126 char *sp, *dp, *ep;
127 sp = refspec[i];
128 if (*sp == '+') {
129 rs[i].force = 1;
130 sp++;
132 ep = strchr(sp, ':');
133 if (ep) {
134 dp = ep + 1;
135 *ep = 0;
137 else
138 dp = sp;
139 rs[i].src = sp;
140 rs[i].dst = dp;
142 rs[nr_refspec].src = rs[nr_refspec].dst = NULL;
143 return rs;
146 static int count_refspec_match(const char *pattern,
147 struct ref *refs,
148 struct ref **matched_ref)
150 int match;
151 int patlen = strlen(pattern);
153 for (match = 0; refs; refs = refs->next) {
154 char *name = refs->name;
155 int namelen = strlen(name);
156 if (namelen < patlen ||
157 memcmp(name + namelen - patlen, pattern, patlen))
158 continue;
159 if (namelen != patlen && name[namelen - patlen - 1] != '/')
160 continue;
161 match++;
162 *matched_ref = refs;
164 return match;
167 static void link_dst_tail(struct ref *ref, struct ref ***tail)
169 **tail = ref;
170 *tail = &ref->next;
171 **tail = NULL;
174 static struct ref *try_explicit_object_name(const char *name)
176 unsigned char sha1[20];
177 struct ref *ref;
178 int len;
179 if (get_sha1(name, sha1))
180 return NULL;
181 len = strlen(name) + 1;
182 ref = xcalloc(1, sizeof(*ref) + len);
183 memcpy(ref->name, name, len);
184 memcpy(ref->new_sha1, sha1, 20);
185 return ref;
188 static int match_explicit_refs(struct ref *src, struct ref *dst,
189 struct ref ***dst_tail, struct refspec *rs)
191 int i, errs;
192 for (i = errs = 0; rs[i].src; i++) {
193 struct ref *matched_src, *matched_dst;
195 matched_src = matched_dst = NULL;
196 switch (count_refspec_match(rs[i].src, src, &matched_src)) {
197 case 1:
198 break;
199 case 0:
200 /* The source could be in the get_sha1() format
201 * not a reference name.
203 matched_src = try_explicit_object_name(rs[i].src);
204 if (matched_src)
205 break;
206 errs = 1;
207 error("src refspec %s does not match any.",
208 rs[i].src);
209 break;
210 default:
211 errs = 1;
212 error("src refspec %s matches more than one.",
213 rs[i].src);
214 break;
216 switch (count_refspec_match(rs[i].dst, dst, &matched_dst)) {
217 case 1:
218 break;
219 case 0:
220 if (!memcmp(rs[i].dst, "refs/", 5)) {
221 int len = strlen(rs[i].dst) + 1;
222 matched_dst = xcalloc(1, sizeof(*dst) + len);
223 memcpy(matched_dst->name, rs[i].dst, len);
224 link_dst_tail(matched_dst, dst_tail);
226 else if (!strcmp(rs[i].src, rs[i].dst) &&
227 matched_src) {
228 /* pushing "master:master" when
229 * remote does not have master yet.
231 int len = strlen(matched_src->name) + 1;
232 matched_dst = xcalloc(1, sizeof(*dst) + len);
233 memcpy(matched_dst->name, matched_src->name,
234 len);
235 link_dst_tail(matched_dst, dst_tail);
237 else {
238 errs = 1;
239 error("dst refspec %s does not match any "
240 "existing ref on the remote and does "
241 "not start with refs/.", rs[i].dst);
243 break;
244 default:
245 errs = 1;
246 error("dst refspec %s matches more than one.",
247 rs[i].dst);
248 break;
250 if (errs)
251 continue;
252 if (matched_dst->peer_ref) {
253 errs = 1;
254 error("dst ref %s receives from more than one src.",
255 matched_dst->name);
257 else {
258 matched_dst->peer_ref = matched_src;
259 matched_dst->force = rs[i].force;
262 return -errs;
265 static struct ref *find_ref_by_name(struct ref *list, const char *name)
267 for ( ; list; list = list->next)
268 if (!strcmp(list->name, name))
269 return list;
270 return NULL;
273 int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
274 int nr_refspec, char **refspec, int all)
276 struct refspec *rs = parse_ref_spec(nr_refspec, refspec);
278 if (nr_refspec)
279 return match_explicit_refs(src, dst, dst_tail, rs);
281 /* pick the remainder */
282 for ( ; src; src = src->next) {
283 struct ref *dst_peer;
284 if (src->peer_ref)
285 continue;
286 dst_peer = find_ref_by_name(dst, src->name);
287 if ((dst_peer && dst_peer->peer_ref) || (!dst_peer && !all))
288 continue;
289 if (!dst_peer) {
290 /* Create a new one and link it */
291 int len = strlen(src->name) + 1;
292 dst_peer = xcalloc(1, sizeof(*dst_peer) + len);
293 memcpy(dst_peer->name, src->name, len);
294 memcpy(dst_peer->new_sha1, src->new_sha1, 20);
295 link_dst_tail(dst_peer, dst_tail);
297 dst_peer->peer_ref = src;
299 return 0;
302 enum protocol {
303 PROTO_LOCAL = 1,
304 PROTO_SSH,
305 PROTO_GIT,
308 static enum protocol get_protocol(const char *name)
310 if (!strcmp(name, "ssh"))
311 return PROTO_SSH;
312 if (!strcmp(name, "git"))
313 return PROTO_GIT;
314 if (!strcmp(name, "git+ssh"))
315 return PROTO_SSH;
316 if (!strcmp(name, "ssh+git"))
317 return PROTO_SSH;
318 die("I don't handle protocol '%s'", name);
321 #define STR_(s) # s
322 #define STR(s) STR_(s)
324 #ifndef NO_IPV6
327 * Returns a connected socket() fd, or else die()s.
329 static int git_tcp_connect_sock(char *host)
331 int sockfd = -1, saved_errno = 0;
332 char *colon, *end;
333 const char *port = STR(DEFAULT_GIT_PORT);
334 struct addrinfo hints, *ai0, *ai;
335 int gai;
337 if (host[0] == '[') {
338 end = strchr(host + 1, ']');
339 if (end) {
340 *end = 0;
341 end++;
342 host++;
343 } else
344 end = host;
345 } else
346 end = host;
347 colon = strchr(end, ':');
349 if (colon) {
350 *colon = 0;
351 port = colon + 1;
354 memset(&hints, 0, sizeof(hints));
355 hints.ai_socktype = SOCK_STREAM;
356 hints.ai_protocol = IPPROTO_TCP;
358 gai = getaddrinfo(host, port, &hints, &ai);
359 if (gai)
360 die("Unable to look up %s (%s)", host, gai_strerror(gai));
362 for (ai0 = ai; ai; ai = ai->ai_next) {
363 sockfd = socket(ai->ai_family,
364 ai->ai_socktype, ai->ai_protocol);
365 if (sockfd < 0) {
366 saved_errno = errno;
367 continue;
369 if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
370 saved_errno = errno;
371 close(sockfd);
372 sockfd = -1;
373 continue;
375 break;
378 freeaddrinfo(ai0);
380 if (sockfd < 0)
381 die("unable to connect a socket (%s)", strerror(saved_errno));
383 return sockfd;
386 #else /* NO_IPV6 */
389 * Returns a connected socket() fd, or else die()s.
391 static int git_tcp_connect_sock(char *host)
393 int sockfd = -1, saved_errno = 0;
394 char *colon, *end;
395 char *port = STR(DEFAULT_GIT_PORT), *ep;
396 struct hostent *he;
397 struct sockaddr_in sa;
398 char **ap;
399 unsigned int nport;
401 if (host[0] == '[') {
402 end = strchr(host + 1, ']');
403 if (end) {
404 *end = 0;
405 end++;
406 host++;
407 } else
408 end = host;
409 } else
410 end = host;
411 colon = strchr(end, ':');
413 if (colon) {
414 *colon = 0;
415 port = colon + 1;
418 he = gethostbyname(host);
419 if (!he)
420 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
421 nport = strtoul(port, &ep, 10);
422 if ( ep == port || *ep ) {
423 /* Not numeric */
424 struct servent *se = getservbyname(port,"tcp");
425 if ( !se )
426 die("Unknown port %s\n", port);
427 nport = se->s_port;
430 for (ap = he->h_addr_list; *ap; ap++) {
431 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
432 if (sockfd < 0) {
433 saved_errno = errno;
434 continue;
437 memset(&sa, 0, sizeof sa);
438 sa.sin_family = he->h_addrtype;
439 sa.sin_port = htons(nport);
440 memcpy(&sa.sin_addr, *ap, he->h_length);
442 if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
443 saved_errno = errno;
444 close(sockfd);
445 sockfd = -1;
446 continue;
448 break;
451 if (sockfd < 0)
452 die("unable to connect a socket (%s)", strerror(saved_errno));
454 return sockfd;
457 #endif /* NO_IPV6 */
460 static void git_tcp_connect(int fd[2], char *host)
462 int sockfd = git_tcp_connect_sock(host);
464 fd[0] = sockfd;
465 fd[1] = sockfd;
469 static char *git_proxy_command = NULL;
470 static const char *rhost_name = NULL;
471 static int rhost_len;
473 static int git_proxy_command_options(const char *var, const char *value)
475 if (!strcmp(var, "core.gitproxy")) {
476 const char *for_pos;
477 int matchlen = -1;
478 int hostlen;
480 if (git_proxy_command)
481 return 0;
482 /* [core]
483 * ;# matches www.kernel.org as well
484 * gitproxy = netcatter-1 for kernel.org
485 * gitproxy = netcatter-2 for sample.xz
486 * gitproxy = netcatter-default
488 for_pos = strstr(value, " for ");
489 if (!for_pos)
490 /* matches everybody */
491 matchlen = strlen(value);
492 else {
493 hostlen = strlen(for_pos + 5);
494 if (rhost_len < hostlen)
495 matchlen = -1;
496 else if (!strncmp(for_pos + 5,
497 rhost_name + rhost_len - hostlen,
498 hostlen) &&
499 ((rhost_len == hostlen) ||
500 rhost_name[rhost_len - hostlen -1] == '.'))
501 matchlen = for_pos - value;
502 else
503 matchlen = -1;
505 if (0 <= matchlen) {
506 /* core.gitproxy = none for kernel.org */
507 if (matchlen == 4 &&
508 !memcmp(value, "none", 4))
509 matchlen = 0;
510 git_proxy_command = xmalloc(matchlen + 1);
511 memcpy(git_proxy_command, value, matchlen);
512 git_proxy_command[matchlen] = 0;
514 return 0;
517 return git_default_config(var, value);
520 static int git_use_proxy(const char *host)
522 rhost_name = host;
523 rhost_len = strlen(host);
524 git_proxy_command = getenv("GIT_PROXY_COMMAND");
525 git_config(git_proxy_command_options);
526 rhost_name = NULL;
527 return (git_proxy_command && *git_proxy_command);
530 static void git_proxy_connect(int fd[2], char *host)
532 const char *port = STR(DEFAULT_GIT_PORT);
533 char *colon, *end;
534 int pipefd[2][2];
535 pid_t pid;
537 if (host[0] == '[') {
538 end = strchr(host + 1, ']');
539 if (end) {
540 *end = 0;
541 end++;
542 host++;
543 } else
544 end = host;
545 } else
546 end = host;
547 colon = strchr(end, ':');
549 if (colon) {
550 *colon = 0;
551 port = colon + 1;
554 if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
555 die("unable to create pipe pair for communication");
556 pid = fork();
557 if (!pid) {
558 dup2(pipefd[1][0], 0);
559 dup2(pipefd[0][1], 1);
560 close(pipefd[0][0]);
561 close(pipefd[0][1]);
562 close(pipefd[1][0]);
563 close(pipefd[1][1]);
564 execlp(git_proxy_command, git_proxy_command, host, port, NULL);
565 die("exec failed");
567 if (pid < 0)
568 die("fork failed");
569 fd[0] = pipefd[0][0];
570 fd[1] = pipefd[1][1];
571 close(pipefd[0][1]);
572 close(pipefd[1][0]);
576 * Yeah, yeah, fixme. Need to pass in the heads etc.
578 int git_connect(int fd[2], char *url, const char *prog)
580 char command[1024];
581 char *host, *path = url;
582 char *end;
583 int c;
584 int pipefd[2][2];
585 pid_t pid;
586 enum protocol protocol = PROTO_LOCAL;
587 int free_path = 0;
589 /* Without this we cannot rely on waitpid() to tell
590 * what happened to our children.
592 signal(SIGCHLD, SIG_DFL);
594 host = strstr(url, "://");
595 if(host) {
596 *host = '\0';
597 protocol = get_protocol(url);
598 host += 3;
599 c = '/';
600 } else {
601 host = url;
602 c = ':';
605 if (host[0] == '[') {
606 end = strchr(host + 1, ']');
607 if (end) {
608 *end = 0;
609 end++;
610 host++;
611 } else
612 end = host;
613 } else
614 end = host;
616 path = strchr(end, c);
617 if (c == ':') {
618 if (path) {
619 protocol = PROTO_SSH;
620 *path++ = '\0';
621 } else
622 path = host;
625 if (!path || !*path)
626 die("No path specified. See 'man git-pull' for valid url syntax");
629 * null-terminate hostname and point path to ~ for URL's like this:
630 * ssh://host.xz/~user/repo
632 if (protocol != PROTO_LOCAL && host != url) {
633 char *ptr = path;
634 if (path[1] == '~')
635 path++;
636 else {
637 path = strdup(ptr);
638 free_path = 1;
641 *ptr = '\0';
644 if (protocol == PROTO_GIT) {
645 /* These underlying connection commands die() if they
646 * cannot connect.
648 char *target_host = strdup(host);
649 if (git_use_proxy(host))
650 git_proxy_connect(fd, host);
651 else
652 git_tcp_connect(fd, host);
654 * Separate original protocol components prog and path
655 * from extended components with a NUL byte.
657 packet_write(fd[1],
658 "%s %s%chost=%s%c",
659 prog, path, 0,
660 target_host, 0);
661 free(target_host);
662 if (free_path)
663 free(path);
664 return 0;
667 if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
668 die("unable to create pipe pair for communication");
669 pid = fork();
670 if (pid < 0)
671 die("unable to fork");
672 if (!pid) {
673 snprintf(command, sizeof(command), "%s %s", prog,
674 sq_quote(path));
675 dup2(pipefd[1][0], 0);
676 dup2(pipefd[0][1], 1);
677 close(pipefd[0][0]);
678 close(pipefd[0][1]);
679 close(pipefd[1][0]);
680 close(pipefd[1][1]);
681 if (protocol == PROTO_SSH) {
682 const char *ssh, *ssh_basename;
683 ssh = getenv("GIT_SSH");
684 if (!ssh) ssh = "ssh";
685 ssh_basename = strrchr(ssh, '/');
686 if (!ssh_basename)
687 ssh_basename = ssh;
688 else
689 ssh_basename++;
690 execlp(ssh, ssh_basename, host, command, NULL);
692 else {
693 unsetenv(ALTERNATE_DB_ENVIRONMENT);
694 unsetenv(DB_ENVIRONMENT);
695 unsetenv(GIT_DIR_ENVIRONMENT);
696 unsetenv(GRAFT_ENVIRONMENT);
697 unsetenv(INDEX_ENVIRONMENT);
698 execlp("sh", "sh", "-c", command, NULL);
700 die("exec failed");
702 fd[0] = pipefd[0][0];
703 fd[1] = pipefd[1][1];
704 close(pipefd[0][1]);
705 close(pipefd[1][0]);
706 if (free_path)
707 free(path);
708 return pid;
711 int finish_connect(pid_t pid)
713 int ret;
715 for (;;) {
716 ret = waitpid(pid, NULL, 0);
717 if (!ret)
718 break;
719 if (errno != EINTR)
720 break;
722 return ret;