]> git.ipfire.org Git - thirdparty/git.git/blame - connect.c
xdiff: minor changes to match libxdiff-0.21
[thirdparty/git.git] / connect.c
CommitLineData
731043fd 1#include "git-compat-util.h"
f7192598 2#include "cache.h"
41cb7488 3#include "pkt-line.h"
b10d0ec7 4#include "quote.h"
6abf5c0c 5#include "refs.h"
f7192598 6#include <sys/wait.h>
2386d658
LT
7#include <sys/socket.h>
8#include <netinet/in.h>
9#include <arpa/inet.h>
10#include <netdb.h>
f7192598 11
1f5881bb 12static char *server_capabilities = NULL;
211b5f9e 13
d1c133f5
LT
14/*
15 * Read all the refs from the other end
16 */
1a7141ff
JH
17struct ref **get_remote_heads(int in, struct ref **list,
18 int nr_match, char **match, int ignore_funny)
d1c133f5
LT
19{
20 *list = NULL;
21 for (;;) {
22 struct ref *ref;
23 unsigned char old_sha1[20];
24 static char buffer[1000];
25 char *name;
211b5f9e 26 int len, name_len;
d1c133f5
LT
27
28 len = packet_read_line(in, buffer, sizeof(buffer));
29 if (!len)
30 break;
31 if (buffer[len-1] == '\n')
32 buffer[--len] = 0;
33
34 if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
35 die("protocol error: expected sha/ref, got '%s'", buffer);
36 name = buffer + 41;
1a7141ff 37
211b5f9e
JS
38 name_len = strlen(name);
39 if (len != name_len + 41) {
40 if (server_capabilities)
41 free(server_capabilities);
42 server_capabilities = strdup(name + name_len + 1);
43 }
44
cfee10a7
JH
45 if (ignore_funny && 45 < len && !memcmp(name, "refs/", 5) &&
46 check_ref_format(name + 5))
47 continue;
48
d1c133f5
LT
49 if (nr_match && !path_match(name, nr_match, match))
50 continue;
f88395ac 51 ref = xcalloc(1, sizeof(*ref) + len - 40);
d1c133f5 52 memcpy(ref->old_sha1, old_sha1, 20);
d1c133f5 53 memcpy(ref->name, buffer + 41, len - 40);
d1c133f5
LT
54 *list = ref;
55 list = &ref->next;
56 }
57 return list;
58}
59
211b5f9e
JS
60int server_supports(const char *feature)
61{
1f5881bb
JS
62 return server_capabilities &&
63 strstr(server_capabilities, feature) != NULL;
211b5f9e
JS
64}
65
41cb7488
LT
66int get_ack(int fd, unsigned char *result_sha1)
67{
68 static char line[1000];
69 int len = packet_read_line(fd, line, sizeof(line));
70
71 if (!len)
72 die("git-fetch-pack: expected ACK/NAK, got EOF");
73 if (line[len-1] == '\n')
74 line[--len] = 0;
75 if (!strcmp(line, "NAK"))
76 return 0;
da2a95b2 77 if (!strncmp(line, "ACK ", 4)) {
c4c86f07
JS
78 if (!get_sha1_hex(line+4, result_sha1)) {
79 if (strstr(line+45, "continue"))
80 return 2;
41cb7488 81 return 1;
c4c86f07 82 }
41cb7488
LT
83 }
84 die("git-fetch_pack: expected ACK/NAK, got '%s'", line);
85}
86
013e7c7f
LT
87int path_match(const char *path, int nr, char **match)
88{
89 int i;
90 int pathlen = strlen(path);
91
92 for (i = 0; i < nr; i++) {
93 char *s = match[i];
94 int len = strlen(s);
95
96 if (!len || len > pathlen)
97 continue;
98 if (memcmp(path + pathlen - len, s, len))
99 continue;
100 if (pathlen > len && path[pathlen - len - 1] != '/')
101 continue;
102 *s = 0;
9546010b 103 return (i + 1);
013e7c7f
LT
104 }
105 return 0;
106}
107
f88395ac
JH
108struct refspec {
109 char *src;
110 char *dst;
ff27adf3 111 char force;
f88395ac
JH
112};
113
ff27adf3
JH
114/*
115 * A:B means fast forward remote B with local A.
116 * +A:B means overwrite remote B with local A.
117 * +A is a shorthand for +A:A.
118 * A is a shorthand for A:A.
119 */
f88395ac
JH
120static struct refspec *parse_ref_spec(int nr_refspec, char **refspec)
121{
122 int i;
ff27adf3 123 struct refspec *rs = xcalloc(sizeof(*rs), (nr_refspec + 1));
f88395ac
JH
124 for (i = 0; i < nr_refspec; i++) {
125 char *sp, *dp, *ep;
126 sp = refspec[i];
ff27adf3
JH
127 if (*sp == '+') {
128 rs[i].force = 1;
129 sp++;
130 }
f88395ac
JH
131 ep = strchr(sp, ':');
132 if (ep) {
133 dp = ep + 1;
134 *ep = 0;
135 }
136 else
137 dp = sp;
138 rs[i].src = sp;
139 rs[i].dst = dp;
140 }
141 rs[nr_refspec].src = rs[nr_refspec].dst = NULL;
142 return rs;
143}
144
145static int count_refspec_match(const char *pattern,
146 struct ref *refs,
147 struct ref **matched_ref)
148{
149 int match;
150 int patlen = strlen(pattern);
151
152 for (match = 0; refs; refs = refs->next) {
153 char *name = refs->name;
154 int namelen = strlen(name);
155 if (namelen < patlen ||
156 memcmp(name + namelen - patlen, pattern, patlen))
157 continue;
158 if (namelen != patlen && name[namelen - patlen - 1] != '/')
159 continue;
160 match++;
161 *matched_ref = refs;
162 }
163 return match;
164}
165
166static void link_dst_tail(struct ref *ref, struct ref ***tail)
167{
168 **tail = ref;
169 *tail = &ref->next;
170 **tail = NULL;
171}
172
15e02b37
JH
173static struct ref *try_explicit_object_name(const char *name)
174{
175 unsigned char sha1[20];
176 struct ref *ref;
177 int len;
178 if (get_sha1(name, sha1))
179 return NULL;
180 len = strlen(name) + 1;
181 ref = xcalloc(1, sizeof(*ref) + len);
182 memcpy(ref->name, name, len);
183 memcpy(ref->new_sha1, sha1, 20);
184 return ref;
185}
186
f88395ac
JH
187static int match_explicit_refs(struct ref *src, struct ref *dst,
188 struct ref ***dst_tail, struct refspec *rs)
189{
190 int i, errs;
191 for (i = errs = 0; rs[i].src; i++) {
192 struct ref *matched_src, *matched_dst;
193
194 matched_src = matched_dst = NULL;
195 switch (count_refspec_match(rs[i].src, src, &matched_src)) {
196 case 1:
197 break;
198 case 0:
15e02b37
JH
199 /* The source could be in the get_sha1() format
200 * not a reference name.
201 */
202 matched_src = try_explicit_object_name(rs[i].src);
203 if (matched_src)
204 break;
f88395ac 205 errs = 1;
4ec99bf0
TS
206 error("src refspec %s does not match any.",
207 rs[i].src);
f88395ac
JH
208 break;
209 default:
210 errs = 1;
211 error("src refspec %s matches more than one.",
212 rs[i].src);
213 break;
214 }
215 switch (count_refspec_match(rs[i].dst, dst, &matched_dst)) {
216 case 1:
217 break;
218 case 0:
219 if (!memcmp(rs[i].dst, "refs/", 5)) {
220 int len = strlen(rs[i].dst) + 1;
221 matched_dst = xcalloc(1, sizeof(*dst) + len);
222 memcpy(matched_dst->name, rs[i].dst, len);
223 link_dst_tail(matched_dst, dst_tail);
224 }
225 else if (!strcmp(rs[i].src, rs[i].dst) &&
226 matched_src) {
227 /* pushing "master:master" when
228 * remote does not have master yet.
229 */
4fa1604f 230 int len = strlen(matched_src->name) + 1;
f88395ac
JH
231 matched_dst = xcalloc(1, sizeof(*dst) + len);
232 memcpy(matched_dst->name, matched_src->name,
233 len);
234 link_dst_tail(matched_dst, dst_tail);
235 }
236 else {
237 errs = 1;
238 error("dst refspec %s does not match any "
239 "existing ref on the remote and does "
240 "not start with refs/.", rs[i].dst);
241 }
242 break;
243 default:
244 errs = 1;
245 error("dst refspec %s matches more than one.",
246 rs[i].dst);
247 break;
248 }
249 if (errs)
250 continue;
f88395ac
JH
251 if (matched_dst->peer_ref) {
252 errs = 1;
253 error("dst ref %s receives from more than one src.",
254 matched_dst->name);
255 }
ff27adf3 256 else {
f88395ac 257 matched_dst->peer_ref = matched_src;
ff27adf3
JH
258 matched_dst->force = rs[i].force;
259 }
f88395ac
JH
260 }
261 return -errs;
262}
263
264static struct ref *find_ref_by_name(struct ref *list, const char *name)
265{
266 for ( ; list; list = list->next)
267 if (!strcmp(list->name, name))
268 return list;
269 return NULL;
270}
271
272int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
273 int nr_refspec, char **refspec, int all)
274{
275 struct refspec *rs = parse_ref_spec(nr_refspec, refspec);
276
277 if (nr_refspec)
278 return match_explicit_refs(src, dst, dst_tail, rs);
279
280 /* pick the remainder */
281 for ( ; src; src = src->next) {
282 struct ref *dst_peer;
283 if (src->peer_ref)
284 continue;
285 dst_peer = find_ref_by_name(dst, src->name);
635d37af 286 if ((dst_peer && dst_peer->peer_ref) || (!dst_peer && !all))
f88395ac
JH
287 continue;
288 if (!dst_peer) {
f88395ac
JH
289 /* Create a new one and link it */
290 int len = strlen(src->name) + 1;
291 dst_peer = xcalloc(1, sizeof(*dst_peer) + len);
292 memcpy(dst_peer->name, src->name, len);
293 memcpy(dst_peer->new_sha1, src->new_sha1, 20);
294 link_dst_tail(dst_peer, dst_tail);
295 }
296 dst_peer->peer_ref = src;
297 }
298 return 0;
299}
300
2386d658
LT
301enum protocol {
302 PROTO_LOCAL = 1,
303 PROTO_SSH,
304 PROTO_GIT,
305};
306
307static enum protocol get_protocol(const char *name)
308{
309 if (!strcmp(name, "ssh"))
310 return PROTO_SSH;
311 if (!strcmp(name, "git"))
312 return PROTO_GIT;
c05186cc
LT
313 if (!strcmp(name, "git+ssh"))
314 return PROTO_SSH;
315 if (!strcmp(name, "ssh+git"))
316 return PROTO_SSH;
2386d658
LT
317 die("I don't handle protocol '%s'", name);
318}
319
5ba88448
YH
320#define STR_(s) # s
321#define STR(s) STR_(s)
2386d658 322
49744d63 323#ifndef NO_IPV6
4c505f71 324
5ad312be
JL
325/*
326 * Returns a connected socket() fd, or else die()s.
327 */
328static int git_tcp_connect_sock(char *host)
2386d658 329{
5ba88448
YH
330 int sockfd = -1;
331 char *colon, *end;
332 char *port = STR(DEFAULT_GIT_PORT);
333 struct addrinfo hints, *ai0, *ai;
334 int gai;
335
336 if (host[0] == '[') {
337 end = strchr(host + 1, ']');
338 if (end) {
339 *end = 0;
340 end++;
341 host++;
342 } else
343 end = host;
344 } else
345 end = host;
346 colon = strchr(end, ':');
347
ce6f8e7e
LT
348 if (colon) {
349 *colon = 0;
5ba88448 350 port = colon + 1;
ce6f8e7e 351 }
5ba88448
YH
352
353 memset(&hints, 0, sizeof(hints));
354 hints.ai_socktype = SOCK_STREAM;
355 hints.ai_protocol = IPPROTO_TCP;
356
357 gai = getaddrinfo(host, port, &hints, &ai);
358 if (gai)
359 die("Unable to look up %s (%s)", host, gai_strerror(gai));
360
361 for (ai0 = ai; ai; ai = ai->ai_next) {
5ad312be
JL
362 sockfd = socket(ai->ai_family,
363 ai->ai_socktype, ai->ai_protocol);
5ba88448
YH
364 if (sockfd < 0)
365 continue;
366 if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
367 close(sockfd);
368 sockfd = -1;
369 continue;
2386d658 370 }
5ba88448 371 break;
2386d658
LT
372 }
373
5ba88448 374 freeaddrinfo(ai0);
2386d658 375
2386d658 376 if (sockfd < 0)
5ba88448
YH
377 die("unable to connect a socket (%s)", strerror(errno));
378
5ad312be 379 return sockfd;
2386d658
LT
380}
381
49744d63 382#else /* NO_IPV6 */
4c505f71 383
5ad312be
JL
384/*
385 * Returns a connected socket() fd, or else die()s.
386 */
387static int git_tcp_connect_sock(char *host)
4c505f71
PA
388{
389 int sockfd = -1;
390 char *colon, *end;
391 char *port = STR(DEFAULT_GIT_PORT), *ep;
392 struct hostent *he;
393 struct sockaddr_in sa;
394 char **ap;
395 unsigned int nport;
396
397 if (host[0] == '[') {
398 end = strchr(host + 1, ']');
399 if (end) {
400 *end = 0;
401 end++;
402 host++;
403 } else
404 end = host;
405 } else
406 end = host;
407 colon = strchr(end, ':');
408
409 if (colon) {
410 *colon = 0;
411 port = colon + 1;
412 }
413
4c505f71
PA
414 he = gethostbyname(host);
415 if (!he)
416 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
417 nport = strtoul(port, &ep, 10);
418 if ( ep == port || *ep ) {
419 /* Not numeric */
420 struct servent *se = getservbyname(port,"tcp");
421 if ( !se )
422 die("Unknown port %s\n", port);
423 nport = se->s_port;
424 }
425
426 for (ap = he->h_addr_list; *ap; ap++) {
427 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
428 if (sockfd < 0)
429 continue;
430
431 memset(&sa, 0, sizeof sa);
432 sa.sin_family = he->h_addrtype;
6573faff 433 sa.sin_port = htons(nport);
c6164218 434 memcpy(&sa.sin_addr, *ap, he->h_length);
4c505f71
PA
435
436 if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
437 close(sockfd);
438 sockfd = -1;
439 continue;
440 }
441 break;
442 }
443
444 if (sockfd < 0)
445 die("unable to connect a socket (%s)", strerror(errno));
446
5ad312be
JL
447 return sockfd;
448}
449
450#endif /* NO_IPV6 */
451
452
453static void git_tcp_connect(int fd[2],
454 const char *prog, char *host, char *path)
455{
456 int sockfd = git_tcp_connect_sock(host);
457
4c505f71
PA
458 fd[0] = sockfd;
459 fd[1] = sockfd;
4c505f71
PA
460}
461
4c505f71 462
f8014776 463static char *git_proxy_command = NULL;
e814bc4d
JH
464static const char *rhost_name = NULL;
465static int rhost_len;
f8014776
PC
466
467static int git_proxy_command_options(const char *var, const char *value)
468{
e814bc4d 469 if (!strcmp(var, "core.gitproxy")) {
c3df8568
YH
470 const char *for_pos;
471 int matchlen = -1;
472 int hostlen;
473
e814bc4d 474 if (git_proxy_command)
f8014776 475 return 0;
e814bc4d
JH
476 /* [core]
477 * ;# matches www.kernel.org as well
478 * gitproxy = netcatter-1 for kernel.org
479 * gitproxy = netcatter-2 for sample.xz
480 * gitproxy = netcatter-default
481 */
c3df8568 482 for_pos = strstr(value, " for ");
e814bc4d
JH
483 if (!for_pos)
484 /* matches everybody */
485 matchlen = strlen(value);
486 else {
487 hostlen = strlen(for_pos + 5);
488 if (rhost_len < hostlen)
489 matchlen = -1;
490 else if (!strncmp(for_pos + 5,
491 rhost_name + rhost_len - hostlen,
492 hostlen) &&
493 ((rhost_len == hostlen) ||
494 rhost_name[rhost_len - hostlen -1] == '.'))
495 matchlen = for_pos - value;
496 else
497 matchlen = -1;
498 }
499 if (0 <= matchlen) {
500 /* core.gitproxy = none for kernel.org */
501 if (matchlen == 4 &&
502 !memcmp(value, "none", 4))
503 matchlen = 0;
504 git_proxy_command = xmalloc(matchlen + 1);
505 memcpy(git_proxy_command, value, matchlen);
506 git_proxy_command[matchlen] = 0;
f8014776 507 }
e814bc4d 508 return 0;
f8014776
PC
509 }
510
511 return git_default_config(var, value);
512}
513
e814bc4d 514static int git_use_proxy(const char *host)
f8014776 515{
e814bc4d
JH
516 rhost_name = host;
517 rhost_len = strlen(host);
f8014776
PC
518 git_proxy_command = getenv("GIT_PROXY_COMMAND");
519 git_config(git_proxy_command_options);
e814bc4d
JH
520 rhost_name = NULL;
521 return (git_proxy_command && *git_proxy_command);
f8014776
PC
522}
523
5ad312be
JL
524static void git_proxy_connect(int fd[2],
525 const char *prog, char *host, char *path)
f8014776
PC
526{
527 char *port = STR(DEFAULT_GIT_PORT);
528 char *colon, *end;
529 int pipefd[2][2];
530 pid_t pid;
531
532 if (host[0] == '[') {
533 end = strchr(host + 1, ']');
534 if (end) {
535 *end = 0;
536 end++;
537 host++;
538 } else
539 end = host;
540 } else
541 end = host;
542 colon = strchr(end, ':');
543
544 if (colon) {
545 *colon = 0;
546 port = colon + 1;
547 }
548
549 if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
550 die("unable to create pipe pair for communication");
551 pid = fork();
552 if (!pid) {
553 dup2(pipefd[1][0], 0);
554 dup2(pipefd[0][1], 1);
555 close(pipefd[0][0]);
556 close(pipefd[0][1]);
557 close(pipefd[1][0]);
558 close(pipefd[1][1]);
559 execlp(git_proxy_command, git_proxy_command, host, port, NULL);
560 die("exec failed");
561 }
5ad312be
JL
562 if (pid < 0)
563 die("fork failed");
f8014776
PC
564 fd[0] = pipefd[0][0];
565 fd[1] = pipefd[1][1];
566 close(pipefd[0][1]);
567 close(pipefd[1][0]);
f8014776
PC
568}
569
f7192598
LT
570/*
571 * Yeah, yeah, fixme. Need to pass in the heads etc.
572 */
573int git_connect(int fd[2], char *url, const char *prog)
574{
575 char command[1024];
faea9ccb 576 char *host, *path = url;
356bece0
YH
577 char *end;
578 int c;
f7192598
LT
579 int pipefd[2][2];
580 pid_t pid;
faea9ccb 581 enum protocol protocol = PROTO_LOCAL;
da2a95b2 582 int free_path = 0;
faea9ccb
AE
583
584 host = strstr(url, "://");
585 if(host) {
586 *host = '\0';
587 protocol = get_protocol(url);
588 host += 3;
356bece0
YH
589 c = '/';
590 } else {
f7192598 591 host = url;
356bece0
YH
592 c = ':';
593 }
594
595 if (host[0] == '[') {
596 end = strchr(host + 1, ']');
597 if (end) {
598 *end = 0;
599 end++;
600 host++;
601 } else
602 end = host;
603 } else
604 end = host;
605
606 path = strchr(end, c);
607 if (c == ':') {
608 if (path) {
faea9ccb 609 protocol = PROTO_SSH;
356bece0
YH
610 *path++ = '\0';
611 } else
612 path = host;
f7192598 613 }
2386d658 614
faea9ccb
AE
615 if (!path || !*path)
616 die("No path specified. See 'man git-pull' for valid url syntax");
617
618 /*
619 * null-terminate hostname and point path to ~ for URL's like this:
620 * ssh://host.xz/~user/repo
621 */
622 if (protocol != PROTO_LOCAL && host != url) {
623 char *ptr = path;
624 if (path[1] == '~')
625 path++;
da2a95b2 626 else {
faea9ccb 627 path = strdup(ptr);
da2a95b2
SH
628 free_path = 1;
629 }
faea9ccb
AE
630
631 *ptr = '\0';
632 }
633
f8014776 634 if (protocol == PROTO_GIT) {
5ad312be
JL
635 /* These underlying connection commands die() if they
636 * cannot connect.
637 */
638 char *target_host = strdup(host);
e814bc4d 639 if (git_use_proxy(host))
5ad312be 640 git_proxy_connect(fd, prog, host, path);
da2a95b2 641 else
5ad312be
JL
642 git_tcp_connect(fd, prog, host, path);
643 /*
644 * Separate original protocol components prog and path
645 * from extended components with a NUL byte.
646 */
647 packet_write(fd[1],
648 "%s %s%chost=%s%c",
649 prog, path, 0,
650 target_host, 0);
651 free(target_host);
da2a95b2
SH
652 if (free_path)
653 free(path);
5ad312be 654 return 0;
f8014776 655 }
2386d658 656
f7192598
LT
657 if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
658 die("unable to create pipe pair for communication");
659 pid = fork();
c9bc159d
PD
660 if (pid < 0)
661 die("unable to fork");
f7192598 662 if (!pid) {
b10d0ec7
JH
663 snprintf(command, sizeof(command), "%s %s", prog,
664 sq_quote(path));
f7192598
LT
665 dup2(pipefd[1][0], 0);
666 dup2(pipefd[0][1], 1);
667 close(pipefd[0][0]);
668 close(pipefd[0][1]);
669 close(pipefd[1][0]);
670 close(pipefd[1][1]);
4852f723 671 if (protocol == PROTO_SSH) {
c7c81b3a
JR
672 const char *ssh, *ssh_basename;
673 ssh = getenv("GIT_SSH");
674 if (!ssh) ssh = "ssh";
675 ssh_basename = strrchr(ssh, '/');
4852f723
MS
676 if (!ssh_basename)
677 ssh_basename = ssh;
678 else
679 ssh_basename++;
680 execlp(ssh, ssh_basename, host, command, NULL);
681 }
016fb48b
MD
682 else {
683 unsetenv(ALTERNATE_DB_ENVIRONMENT);
684 unsetenv(DB_ENVIRONMENT);
685 unsetenv(GIT_DIR_ENVIRONMENT);
686 unsetenv(GRAFT_ENVIRONMENT);
687 unsetenv(INDEX_ENVIRONMENT);
f7192598 688 execlp("sh", "sh", "-c", command, NULL);
016fb48b 689 }
f7192598 690 die("exec failed");
016fb48b 691 }
f7192598
LT
692 fd[0] = pipefd[0][0];
693 fd[1] = pipefd[1][1];
694 close(pipefd[0][1]);
695 close(pipefd[1][0]);
da2a95b2
SH
696 if (free_path)
697 free(path);
f7192598
LT
698 return pid;
699}
700
701int finish_connect(pid_t pid)
702{
703 int ret;
704
705 for (;;) {
706 ret = waitpid(pid, NULL, 0);
707 if (!ret)
708 break;
709 if (errno != EINTR)
710 break;
711 }
712 return ret;
713}