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