send-pack: allow the same source to be pushed more than once.
[git/dscho.git] / connect.c
blobc872bfd45b815d12ba87d215cc157495b6a1e4aa
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;
87 static struct refspec *parse_ref_spec(int nr_refspec, char **refspec)
89 int i;
90 struct refspec *rs = xmalloc(sizeof(*rs) * (nr_refspec + 1));
91 for (i = 0; i < nr_refspec; i++) {
92 char *sp, *dp, *ep;
93 sp = refspec[i];
94 ep = strchr(sp, ':');
95 if (ep) {
96 dp = ep + 1;
97 *ep = 0;
99 else
100 dp = sp;
101 rs[i].src = sp;
102 rs[i].dst = dp;
104 rs[nr_refspec].src = rs[nr_refspec].dst = NULL;
105 return rs;
108 static int count_refspec_match(const char *pattern,
109 struct ref *refs,
110 struct ref **matched_ref)
112 int match;
113 int patlen = strlen(pattern);
115 for (match = 0; refs; refs = refs->next) {
116 char *name = refs->name;
117 int namelen = strlen(name);
118 if (namelen < patlen ||
119 memcmp(name + namelen - patlen, pattern, patlen))
120 continue;
121 if (namelen != patlen && name[namelen - patlen - 1] != '/')
122 continue;
123 match++;
124 *matched_ref = refs;
126 return match;
129 static void link_dst_tail(struct ref *ref, struct ref ***tail)
131 **tail = ref;
132 *tail = &ref->next;
133 **tail = NULL;
136 static int match_explicit_refs(struct ref *src, struct ref *dst,
137 struct ref ***dst_tail, struct refspec *rs)
139 int i, errs;
140 for (i = errs = 0; rs[i].src; i++) {
141 struct ref *matched_src, *matched_dst;
143 matched_src = matched_dst = NULL;
144 switch (count_refspec_match(rs[i].src, src, &matched_src)) {
145 case 1:
146 break;
147 case 0:
148 errs = 1;
149 error("src refspec %s does not match any.");
150 break;
151 default:
152 errs = 1;
153 error("src refspec %s matches more than one.",
154 rs[i].src);
155 break;
157 switch (count_refspec_match(rs[i].dst, dst, &matched_dst)) {
158 case 1:
159 break;
160 case 0:
161 if (!memcmp(rs[i].dst, "refs/", 5)) {
162 int len = strlen(rs[i].dst) + 1;
163 matched_dst = xcalloc(1, sizeof(*dst) + len);
164 memcpy(matched_dst->name, rs[i].dst, len);
165 link_dst_tail(matched_dst, dst_tail);
167 else if (!strcmp(rs[i].src, rs[i].dst) &&
168 matched_src) {
169 /* pushing "master:master" when
170 * remote does not have master yet.
172 int len = strlen(matched_src->name) + 1;
173 matched_dst = xcalloc(1, sizeof(*dst) + len);
174 memcpy(matched_dst->name, matched_src->name,
175 len);
176 link_dst_tail(matched_dst, dst_tail);
178 else {
179 errs = 1;
180 error("dst refspec %s does not match any "
181 "existing ref on the remote and does "
182 "not start with refs/.", rs[i].dst);
184 break;
185 default:
186 errs = 1;
187 error("dst refspec %s matches more than one.",
188 rs[i].dst);
189 break;
191 if (errs)
192 continue;
193 if (matched_dst->peer_ref) {
194 errs = 1;
195 error("dst ref %s receives from more than one src.",
196 matched_dst->name);
198 else
199 matched_dst->peer_ref = matched_src;
201 return -errs;
204 static struct ref *find_ref_by_name(struct ref *list, const char *name)
206 for ( ; list; list = list->next)
207 if (!strcmp(list->name, name))
208 return list;
209 return NULL;
212 int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
213 int nr_refspec, char **refspec, int all)
215 struct refspec *rs = parse_ref_spec(nr_refspec, refspec);
217 if (nr_refspec)
218 return match_explicit_refs(src, dst, dst_tail, rs);
220 /* pick the remainder */
221 for ( ; src; src = src->next) {
222 struct ref *dst_peer;
223 if (src->peer_ref)
224 continue;
225 dst_peer = find_ref_by_name(dst, src->name);
226 if ((dst_peer && dst_peer->peer_ref) || (!dst_peer && !all))
227 continue;
228 if (!dst_peer) {
229 /* Create a new one and link it */
230 int len = strlen(src->name) + 1;
231 dst_peer = xcalloc(1, sizeof(*dst_peer) + len);
232 memcpy(dst_peer->name, src->name, len);
233 memcpy(dst_peer->new_sha1, src->new_sha1, 20);
234 link_dst_tail(dst_peer, dst_tail);
236 dst_peer->peer_ref = src;
238 return 0;
241 enum protocol {
242 PROTO_LOCAL = 1,
243 PROTO_SSH,
244 PROTO_GIT,
247 static enum protocol get_protocol(const char *name)
249 if (!strcmp(name, "ssh"))
250 return PROTO_SSH;
251 if (!strcmp(name, "git"))
252 return PROTO_GIT;
253 die("I don't handle protocol '%s'", name);
256 #define STR_(s) # s
257 #define STR(s) STR_(s)
259 static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
261 int sockfd = -1;
262 char *colon, *end;
263 char *port = STR(DEFAULT_GIT_PORT);
264 struct addrinfo hints, *ai0, *ai;
265 int gai;
267 if (host[0] == '[') {
268 end = strchr(host + 1, ']');
269 if (end) {
270 *end = 0;
271 end++;
272 host++;
273 } else
274 end = host;
275 } else
276 end = host;
277 colon = strchr(end, ':');
279 if (colon) {
280 *colon = 0;
281 port = colon + 1;
284 memset(&hints, 0, sizeof(hints));
285 hints.ai_socktype = SOCK_STREAM;
286 hints.ai_protocol = IPPROTO_TCP;
288 gai = getaddrinfo(host, port, &hints, &ai);
289 if (gai)
290 die("Unable to look up %s (%s)", host, gai_strerror(gai));
292 for (ai0 = ai; ai; ai = ai->ai_next) {
293 sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
294 if (sockfd < 0)
295 continue;
296 if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
297 close(sockfd);
298 sockfd = -1;
299 continue;
301 break;
304 freeaddrinfo(ai0);
306 if (sockfd < 0)
307 die("unable to connect a socket (%s)", strerror(errno));
309 fd[0] = sockfd;
310 fd[1] = sockfd;
311 packet_write(sockfd, "%s %s\n", prog, path);
312 return 0;
316 * Yeah, yeah, fixme. Need to pass in the heads etc.
318 int git_connect(int fd[2], char *url, const char *prog)
320 char command[1024];
321 char *host, *path;
322 char *colon;
323 int pipefd[2][2];
324 pid_t pid;
325 enum protocol protocol;
327 host = NULL;
328 path = url;
329 colon = strchr(url, ':');
330 protocol = PROTO_LOCAL;
331 if (colon) {
332 *colon = 0;
333 host = url;
334 path = colon+1;
335 protocol = PROTO_SSH;
336 if (!memcmp(path, "//", 2)) {
337 char *slash = strchr(path + 2, '/');
338 if (slash) {
339 int nr = slash - path - 2;
340 memmove(path, path+2, nr);
341 path[nr] = 0;
342 protocol = get_protocol(url);
343 host = path;
344 path = slash;
349 if (protocol == PROTO_GIT)
350 return git_tcp_connect(fd, prog, host, path);
352 if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
353 die("unable to create pipe pair for communication");
354 pid = fork();
355 if (!pid) {
356 snprintf(command, sizeof(command), "%s %s", prog,
357 sq_quote(path));
358 dup2(pipefd[1][0], 0);
359 dup2(pipefd[0][1], 1);
360 close(pipefd[0][0]);
361 close(pipefd[0][1]);
362 close(pipefd[1][0]);
363 close(pipefd[1][1]);
364 if (protocol == PROTO_SSH)
365 execlp("ssh", "ssh", host, command, NULL);
366 else
367 execlp("sh", "sh", "-c", command, NULL);
368 die("exec failed");
370 fd[0] = pipefd[0][0];
371 fd[1] = pipefd[1][1];
372 close(pipefd[0][1]);
373 close(pipefd[1][0]);
374 return pid;
377 int finish_connect(pid_t pid)
379 int ret;
381 for (;;) {
382 ret = waitpid(pid, NULL, 0);
383 if (!ret)
384 break;
385 if (errno != EINTR)
386 break;
388 return ret;