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