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