Support receiving server capabilities
[git/git.git] / connect.c
CommitLineData
f7192598 1#include "cache.h"
41cb7488 2#include "pkt-line.h"
b10d0ec7 3#include "quote.h"
6abf5c0c 4#include "refs.h"
f7192598 5#include <sys/wait.h>
2386d658
LT
6#include <sys/socket.h>
7#include <netinet/in.h>
8#include <arpa/inet.h>
9#include <netdb.h>
f7192598 10
211b5f9e
JS
11static char *server_capabilities = "";
12
d1c133f5
LT
13/*
14 * Read all the refs from the other end
15 */
1a7141ff
JH
16struct ref **get_remote_heads(int in, struct ref **list,
17 int nr_match, char **match, int ignore_funny)
d1c133f5
LT
18{
19 *list = NULL;
20 for (;;) {
21 struct ref *ref;
22 unsigned char old_sha1[20];
23 static char buffer[1000];
24 char *name;
211b5f9e 25 int len, name_len;
d1c133f5
LT
26
27 len = packet_read_line(in, buffer, sizeof(buffer));
28 if (!len)
29 break;
30 if (buffer[len-1] == '\n')
31 buffer[--len] = 0;
32
33 if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
34 die("protocol error: expected sha/ref, got '%s'", buffer);
35 name = buffer + 41;
1a7141ff
JH
36
37 if (ignore_funny && 45 < len && !memcmp(name, "refs/", 5) &&
38 check_ref_format(name + 5))
39 continue;
40
211b5f9e
JS
41 name_len = strlen(name);
42 if (len != name_len + 41) {
43 if (server_capabilities)
44 free(server_capabilities);
45 server_capabilities = strdup(name + name_len + 1);
46 }
47
d1c133f5
LT
48 if (nr_match && !path_match(name, nr_match, match))
49 continue;
f88395ac 50 ref = xcalloc(1, sizeof(*ref) + len - 40);
d1c133f5 51 memcpy(ref->old_sha1, old_sha1, 20);
d1c133f5 52 memcpy(ref->name, buffer + 41, len - 40);
d1c133f5
LT
53 *list = ref;
54 list = &ref->next;
55 }
56 return list;
57}
58
211b5f9e
JS
59int server_supports(const char *feature)
60{
61 return strstr(feature, server_capabilities) != NULL;
62}
63
41cb7488
LT
64int get_ack(int fd, unsigned char *result_sha1)
65{
66 static char line[1000];
67 int len = packet_read_line(fd, line, sizeof(line));
68
69 if (!len)
70 die("git-fetch-pack: expected ACK/NAK, got EOF");
71 if (line[len-1] == '\n')
72 line[--len] = 0;
73 if (!strcmp(line, "NAK"))
74 return 0;
75 if (!strncmp(line, "ACK ", 3)) {
af2d3aa4 76 if (!get_sha1_hex(line+4, result_sha1))
41cb7488
LT
77 return 1;
78 }
79 die("git-fetch_pack: expected ACK/NAK, got '%s'", line);
80}
81
013e7c7f
LT
82int path_match(const char *path, int nr, char **match)
83{
84 int i;
85 int pathlen = strlen(path);
86
87 for (i = 0; i < nr; i++) {
88 char *s = match[i];
89 int len = strlen(s);
90
91 if (!len || len > pathlen)
92 continue;
93 if (memcmp(path + pathlen - len, s, len))
94 continue;
95 if (pathlen > len && path[pathlen - len - 1] != '/')
96 continue;
97 *s = 0;
98 return 1;
99 }
100 return 0;
101}
102
f88395ac
JH
103struct refspec {
104 char *src;
105 char *dst;
ff27adf3 106 char force;
f88395ac
JH
107};
108
ff27adf3
JH
109/*
110 * A:B means fast forward remote B with local A.
111 * +A:B means overwrite remote B with local A.
112 * +A is a shorthand for +A:A.
113 * A is a shorthand for A:A.
114 */
f88395ac
JH
115static struct refspec *parse_ref_spec(int nr_refspec, char **refspec)
116{
117 int i;
ff27adf3 118 struct refspec *rs = xcalloc(sizeof(*rs), (nr_refspec + 1));
f88395ac
JH
119 for (i = 0; i < nr_refspec; i++) {
120 char *sp, *dp, *ep;
121 sp = refspec[i];
ff27adf3
JH
122 if (*sp == '+') {
123 rs[i].force = 1;
124 sp++;
125 }
f88395ac
JH
126 ep = strchr(sp, ':');
127 if (ep) {
128 dp = ep + 1;
129 *ep = 0;
130 }
131 else
132 dp = sp;
133 rs[i].src = sp;
134 rs[i].dst = dp;
135 }
136 rs[nr_refspec].src = rs[nr_refspec].dst = NULL;
137 return rs;
138}
139
140static int count_refspec_match(const char *pattern,
141 struct ref *refs,
142 struct ref **matched_ref)
143{
144 int match;
145 int patlen = strlen(pattern);
146
147 for (match = 0; refs; refs = refs->next) {
148 char *name = refs->name;
149 int namelen = strlen(name);
150 if (namelen < patlen ||
151 memcmp(name + namelen - patlen, pattern, patlen))
152 continue;
153 if (namelen != patlen && name[namelen - patlen - 1] != '/')
154 continue;
155 match++;
156 *matched_ref = refs;
157 }
158 return match;
159}
160
161static void link_dst_tail(struct ref *ref, struct ref ***tail)
162{
163 **tail = ref;
164 *tail = &ref->next;
165 **tail = NULL;
166}
167
15e02b37
JH
168static struct ref *try_explicit_object_name(const char *name)
169{
170 unsigned char sha1[20];
171 struct ref *ref;
172 int len;
173 if (get_sha1(name, sha1))
174 return NULL;
175 len = strlen(name) + 1;
176 ref = xcalloc(1, sizeof(*ref) + len);
177 memcpy(ref->name, name, len);
178 memcpy(ref->new_sha1, sha1, 20);
179 return ref;
180}
181
f88395ac
JH
182static int match_explicit_refs(struct ref *src, struct ref *dst,
183 struct ref ***dst_tail, struct refspec *rs)
184{
185 int i, errs;
186 for (i = errs = 0; rs[i].src; i++) {
187 struct ref *matched_src, *matched_dst;
188
189 matched_src = matched_dst = NULL;
190 switch (count_refspec_match(rs[i].src, src, &matched_src)) {
191 case 1:
192 break;
193 case 0:
15e02b37
JH
194 /* The source could be in the get_sha1() format
195 * not a reference name.
196 */
197 matched_src = try_explicit_object_name(rs[i].src);
198 if (matched_src)
199 break;
f88395ac 200 errs = 1;
4ec99bf0
TS
201 error("src refspec %s does not match any.",
202 rs[i].src);
f88395ac
JH
203 break;
204 default:
205 errs = 1;
206 error("src refspec %s matches more than one.",
207 rs[i].src);
208 break;
209 }
210 switch (count_refspec_match(rs[i].dst, dst, &matched_dst)) {
211 case 1:
212 break;
213 case 0:
214 if (!memcmp(rs[i].dst, "refs/", 5)) {
215 int len = strlen(rs[i].dst) + 1;
216 matched_dst = xcalloc(1, sizeof(*dst) + len);
217 memcpy(matched_dst->name, rs[i].dst, len);
218 link_dst_tail(matched_dst, dst_tail);
219 }
220 else if (!strcmp(rs[i].src, rs[i].dst) &&
221 matched_src) {
222 /* pushing "master:master" when
223 * remote does not have master yet.
224 */
4fa1604f 225 int len = strlen(matched_src->name) + 1;
f88395ac
JH
226 matched_dst = xcalloc(1, sizeof(*dst) + len);
227 memcpy(matched_dst->name, matched_src->name,
228 len);
229 link_dst_tail(matched_dst, dst_tail);
230 }
231 else {
232 errs = 1;
233 error("dst refspec %s does not match any "
234 "existing ref on the remote and does "
235 "not start with refs/.", rs[i].dst);
236 }
237 break;
238 default:
239 errs = 1;
240 error("dst refspec %s matches more than one.",
241 rs[i].dst);
242 break;
243 }
244 if (errs)
245 continue;
f88395ac
JH
246 if (matched_dst->peer_ref) {
247 errs = 1;
248 error("dst ref %s receives from more than one src.",
249 matched_dst->name);
250 }
ff27adf3 251 else {
f88395ac 252 matched_dst->peer_ref = matched_src;
ff27adf3
JH
253 matched_dst->force = rs[i].force;
254 }
f88395ac
JH
255 }
256 return -errs;
257}
258
259static struct ref *find_ref_by_name(struct ref *list, const char *name)
260{
261 for ( ; list; list = list->next)
262 if (!strcmp(list->name, name))
263 return list;
264 return NULL;
265}
266
267int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
268 int nr_refspec, char **refspec, int all)
269{
270 struct refspec *rs = parse_ref_spec(nr_refspec, refspec);
271
272 if (nr_refspec)
273 return match_explicit_refs(src, dst, dst_tail, rs);
274
275 /* pick the remainder */
276 for ( ; src; src = src->next) {
277 struct ref *dst_peer;
278 if (src->peer_ref)
279 continue;
280 dst_peer = find_ref_by_name(dst, src->name);
635d37af 281 if ((dst_peer && dst_peer->peer_ref) || (!dst_peer && !all))
f88395ac
JH
282 continue;
283 if (!dst_peer) {
f88395ac
JH
284 /* Create a new one and link it */
285 int len = strlen(src->name) + 1;
286 dst_peer = xcalloc(1, sizeof(*dst_peer) + len);
287 memcpy(dst_peer->name, src->name, len);
288 memcpy(dst_peer->new_sha1, src->new_sha1, 20);
289 link_dst_tail(dst_peer, dst_tail);
290 }
291 dst_peer->peer_ref = src;
292 }
293 return 0;
294}
295
2386d658
LT
296enum protocol {
297 PROTO_LOCAL = 1,
298 PROTO_SSH,
299 PROTO_GIT,
300};
301
302static enum protocol get_protocol(const char *name)
303{
304 if (!strcmp(name, "ssh"))
305 return PROTO_SSH;
306 if (!strcmp(name, "git"))
307 return PROTO_GIT;
c05186cc
LT
308 if (!strcmp(name, "git+ssh"))
309 return PROTO_SSH;
310 if (!strcmp(name, "ssh+git"))
311 return PROTO_SSH;
2386d658
LT
312 die("I don't handle protocol '%s'", name);
313}
314
5ba88448
YH
315#define STR_(s) # s
316#define STR(s) STR_(s)
2386d658 317
49744d63 318#ifndef NO_IPV6
4c505f71 319
2386d658
LT
320static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
321{
5ba88448
YH
322 int sockfd = -1;
323 char *colon, *end;
324 char *port = STR(DEFAULT_GIT_PORT);
325 struct addrinfo hints, *ai0, *ai;
326 int gai;
327
328 if (host[0] == '[') {
329 end = strchr(host + 1, ']');
330 if (end) {
331 *end = 0;
332 end++;
333 host++;
334 } else
335 end = host;
336 } else
337 end = host;
338 colon = strchr(end, ':');
339
ce6f8e7e
LT
340 if (colon) {
341 *colon = 0;
5ba88448 342 port = colon + 1;
ce6f8e7e 343 }
5ba88448
YH
344
345 memset(&hints, 0, sizeof(hints));
346 hints.ai_socktype = SOCK_STREAM;
347 hints.ai_protocol = IPPROTO_TCP;
348
349 gai = getaddrinfo(host, port, &hints, &ai);
350 if (gai)
351 die("Unable to look up %s (%s)", host, gai_strerror(gai));
352
353 for (ai0 = ai; ai; ai = ai->ai_next) {
354 sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
355 if (sockfd < 0)
356 continue;
357 if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
358 close(sockfd);
359 sockfd = -1;
360 continue;
2386d658 361 }
5ba88448 362 break;
2386d658
LT
363 }
364
5ba88448 365 freeaddrinfo(ai0);
2386d658 366
2386d658 367 if (sockfd < 0)
5ba88448
YH
368 die("unable to connect a socket (%s)", strerror(errno));
369
2386d658
LT
370 fd[0] = sockfd;
371 fd[1] = sockfd;
372 packet_write(sockfd, "%s %s\n", prog, path);
373 return 0;
374}
375
49744d63 376#else /* NO_IPV6 */
4c505f71
PA
377
378static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
379{
380 int sockfd = -1;
381 char *colon, *end;
382 char *port = STR(DEFAULT_GIT_PORT), *ep;
383 struct hostent *he;
384 struct sockaddr_in sa;
385 char **ap;
386 unsigned int nport;
387
388 if (host[0] == '[') {
389 end = strchr(host + 1, ']');
390 if (end) {
391 *end = 0;
392 end++;
393 host++;
394 } else
395 end = host;
396 } else
397 end = host;
398 colon = strchr(end, ':');
399
400 if (colon) {
401 *colon = 0;
402 port = colon + 1;
403 }
404
405
406 he = gethostbyname(host);
407 if (!he)
408 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
409 nport = strtoul(port, &ep, 10);
410 if ( ep == port || *ep ) {
411 /* Not numeric */
412 struct servent *se = getservbyname(port,"tcp");
413 if ( !se )
414 die("Unknown port %s\n", port);
415 nport = se->s_port;
416 }
417
418 for (ap = he->h_addr_list; *ap; ap++) {
419 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
420 if (sockfd < 0)
421 continue;
422
423 memset(&sa, 0, sizeof sa);
424 sa.sin_family = he->h_addrtype;
6573faff 425 sa.sin_port = htons(nport);
4c505f71
PA
426 memcpy(&sa.sin_addr, ap, he->h_length);
427
428 if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
429 close(sockfd);
430 sockfd = -1;
431 continue;
432 }
433 break;
434 }
435
436 if (sockfd < 0)
437 die("unable to connect a socket (%s)", strerror(errno));
438
439 fd[0] = sockfd;
440 fd[1] = sockfd;
441 packet_write(sockfd, "%s %s\n", prog, path);
442 return 0;
443}
444
49744d63 445#endif /* NO_IPV6 */
4c505f71 446
f7192598
LT
447/*
448 * Yeah, yeah, fixme. Need to pass in the heads etc.
449 */
450int git_connect(int fd[2], char *url, const char *prog)
451{
452 char command[1024];
2386d658 453 char *host, *path;
f7192598
LT
454 char *colon;
455 int pipefd[2][2];
456 pid_t pid;
2386d658 457 enum protocol protocol;
f7192598 458
f7192598
LT
459 host = NULL;
460 path = url;
461 colon = strchr(url, ':');
2386d658 462 protocol = PROTO_LOCAL;
f7192598
LT
463 if (colon) {
464 *colon = 0;
465 host = url;
466 path = colon+1;
2386d658
LT
467 protocol = PROTO_SSH;
468 if (!memcmp(path, "//", 2)) {
469 char *slash = strchr(path + 2, '/');
470 if (slash) {
471 int nr = slash - path - 2;
472 memmove(path, path+2, nr);
473 path[nr] = 0;
474 protocol = get_protocol(url);
475 host = path;
476 path = slash;
477 }
478 }
f7192598 479 }
2386d658
LT
480
481 if (protocol == PROTO_GIT)
482 return git_tcp_connect(fd, prog, host, path);
483
f7192598
LT
484 if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
485 die("unable to create pipe pair for communication");
486 pid = fork();
487 if (!pid) {
b10d0ec7
JH
488 snprintf(command, sizeof(command), "%s %s", prog,
489 sq_quote(path));
f7192598
LT
490 dup2(pipefd[1][0], 0);
491 dup2(pipefd[0][1], 1);
492 close(pipefd[0][0]);
493 close(pipefd[0][1]);
494 close(pipefd[1][0]);
495 close(pipefd[1][1]);
4852f723 496 if (protocol == PROTO_SSH) {
c7c81b3a
JR
497 const char *ssh, *ssh_basename;
498 ssh = getenv("GIT_SSH");
499 if (!ssh) ssh = "ssh";
500 ssh_basename = strrchr(ssh, '/');
4852f723
MS
501 if (!ssh_basename)
502 ssh_basename = ssh;
503 else
504 ssh_basename++;
505 execlp(ssh, ssh_basename, host, command, NULL);
506 }
f7192598
LT
507 else
508 execlp("sh", "sh", "-c", command, NULL);
509 die("exec failed");
510 }
511 fd[0] = pipefd[0][0];
512 fd[1] = pipefd[1][1];
513 close(pipefd[0][1]);
514 close(pipefd[1][0]);
515 return pid;
516}
517
518int finish_connect(pid_t pid)
519{
520 int ret;
521
522 for (;;) {
523 ret = waitpid(pid, NULL, 0);
524 if (!ret)
525 break;
526 if (errno != EINTR)
527 break;
528 }
529 return ret;
530}