GIT 0.99.4 (release candidate)
[git/gitweb.git] / connect.c
blob20b80a1b54b965d4af272aa7cb7c0009c737e4dd
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 struct ref *try_explicit_object_name(const char *name)
138 unsigned char sha1[20];
139 struct ref *ref;
140 int len;
141 if (get_sha1(name, sha1))
142 return NULL;
143 len = strlen(name) + 1;
144 ref = xcalloc(1, sizeof(*ref) + len);
145 memcpy(ref->name, name, len);
146 memcpy(ref->new_sha1, sha1, 20);
147 return ref;
150 static int match_explicit_refs(struct ref *src, struct ref *dst,
151 struct ref ***dst_tail, struct refspec *rs)
153 int i, errs;
154 for (i = errs = 0; rs[i].src; i++) {
155 struct ref *matched_src, *matched_dst;
157 matched_src = matched_dst = NULL;
158 switch (count_refspec_match(rs[i].src, src, &matched_src)) {
159 case 1:
160 break;
161 case 0:
162 /* The source could be in the get_sha1() format
163 * not a reference name.
165 matched_src = try_explicit_object_name(rs[i].src);
166 if (matched_src)
167 break;
168 errs = 1;
169 error("src refspec %s does not match any.");
170 break;
171 default:
172 errs = 1;
173 error("src refspec %s matches more than one.",
174 rs[i].src);
175 break;
177 switch (count_refspec_match(rs[i].dst, dst, &matched_dst)) {
178 case 1:
179 break;
180 case 0:
181 if (!memcmp(rs[i].dst, "refs/", 5)) {
182 int len = strlen(rs[i].dst) + 1;
183 matched_dst = xcalloc(1, sizeof(*dst) + len);
184 memcpy(matched_dst->name, rs[i].dst, len);
185 link_dst_tail(matched_dst, dst_tail);
187 else if (!strcmp(rs[i].src, rs[i].dst) &&
188 matched_src) {
189 /* pushing "master:master" when
190 * remote does not have master yet.
192 int len = strlen(matched_src->name) + 1;
193 matched_dst = xcalloc(1, sizeof(*dst) + len);
194 memcpy(matched_dst->name, matched_src->name,
195 len);
196 link_dst_tail(matched_dst, dst_tail);
198 else {
199 errs = 1;
200 error("dst refspec %s does not match any "
201 "existing ref on the remote and does "
202 "not start with refs/.", rs[i].dst);
204 break;
205 default:
206 errs = 1;
207 error("dst refspec %s matches more than one.",
208 rs[i].dst);
209 break;
211 if (errs)
212 continue;
213 if (matched_dst->peer_ref) {
214 errs = 1;
215 error("dst ref %s receives from more than one src.",
216 matched_dst->name);
218 else
219 matched_dst->peer_ref = matched_src;
221 return -errs;
224 static struct ref *find_ref_by_name(struct ref *list, const char *name)
226 for ( ; list; list = list->next)
227 if (!strcmp(list->name, name))
228 return list;
229 return NULL;
232 int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
233 int nr_refspec, char **refspec, int all)
235 struct refspec *rs = parse_ref_spec(nr_refspec, refspec);
237 if (nr_refspec)
238 return match_explicit_refs(src, dst, dst_tail, rs);
240 /* pick the remainder */
241 for ( ; src; src = src->next) {
242 struct ref *dst_peer;
243 if (src->peer_ref)
244 continue;
245 dst_peer = find_ref_by_name(dst, src->name);
246 if ((dst_peer && dst_peer->peer_ref) || (!dst_peer && !all))
247 continue;
248 if (!dst_peer) {
249 /* Create a new one and link it */
250 int len = strlen(src->name) + 1;
251 dst_peer = xcalloc(1, sizeof(*dst_peer) + len);
252 memcpy(dst_peer->name, src->name, len);
253 memcpy(dst_peer->new_sha1, src->new_sha1, 20);
254 link_dst_tail(dst_peer, dst_tail);
256 dst_peer->peer_ref = src;
258 return 0;
261 enum protocol {
262 PROTO_LOCAL = 1,
263 PROTO_SSH,
264 PROTO_GIT,
267 static enum protocol get_protocol(const char *name)
269 if (!strcmp(name, "ssh"))
270 return PROTO_SSH;
271 if (!strcmp(name, "git"))
272 return PROTO_GIT;
273 die("I don't handle protocol '%s'", name);
276 #define STR_(s) # s
277 #define STR(s) STR_(s)
279 static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
281 int sockfd = -1;
282 char *colon, *end;
283 char *port = STR(DEFAULT_GIT_PORT);
284 struct addrinfo hints, *ai0, *ai;
285 int gai;
287 if (host[0] == '[') {
288 end = strchr(host + 1, ']');
289 if (end) {
290 *end = 0;
291 end++;
292 host++;
293 } else
294 end = host;
295 } else
296 end = host;
297 colon = strchr(end, ':');
299 if (colon) {
300 *colon = 0;
301 port = colon + 1;
304 memset(&hints, 0, sizeof(hints));
305 hints.ai_socktype = SOCK_STREAM;
306 hints.ai_protocol = IPPROTO_TCP;
308 gai = getaddrinfo(host, port, &hints, &ai);
309 if (gai)
310 die("Unable to look up %s (%s)", host, gai_strerror(gai));
312 for (ai0 = ai; ai; ai = ai->ai_next) {
313 sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
314 if (sockfd < 0)
315 continue;
316 if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
317 close(sockfd);
318 sockfd = -1;
319 continue;
321 break;
324 freeaddrinfo(ai0);
326 if (sockfd < 0)
327 die("unable to connect a socket (%s)", strerror(errno));
329 fd[0] = sockfd;
330 fd[1] = sockfd;
331 packet_write(sockfd, "%s %s\n", prog, path);
332 return 0;
336 * Yeah, yeah, fixme. Need to pass in the heads etc.
338 int git_connect(int fd[2], char *url, const char *prog)
340 char command[1024];
341 char *host, *path;
342 char *colon;
343 int pipefd[2][2];
344 pid_t pid;
345 enum protocol protocol;
347 host = NULL;
348 path = url;
349 colon = strchr(url, ':');
350 protocol = PROTO_LOCAL;
351 if (colon) {
352 *colon = 0;
353 host = url;
354 path = colon+1;
355 protocol = PROTO_SSH;
356 if (!memcmp(path, "//", 2)) {
357 char *slash = strchr(path + 2, '/');
358 if (slash) {
359 int nr = slash - path - 2;
360 memmove(path, path+2, nr);
361 path[nr] = 0;
362 protocol = get_protocol(url);
363 host = path;
364 path = slash;
369 if (protocol == PROTO_GIT)
370 return git_tcp_connect(fd, prog, host, path);
372 if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
373 die("unable to create pipe pair for communication");
374 pid = fork();
375 if (!pid) {
376 snprintf(command, sizeof(command), "%s %s", prog,
377 sq_quote(path));
378 dup2(pipefd[1][0], 0);
379 dup2(pipefd[0][1], 1);
380 close(pipefd[0][0]);
381 close(pipefd[0][1]);
382 close(pipefd[1][0]);
383 close(pipefd[1][1]);
384 if (protocol == PROTO_SSH)
385 execlp("ssh", "ssh", host, command, NULL);
386 else
387 execlp("sh", "sh", "-c", command, NULL);
388 die("exec failed");
390 fd[0] = pipefd[0][0];
391 fd[1] = pipefd[1][1];
392 close(pipefd[0][1]);
393 close(pipefd[1][0]);
394 return pid;
397 int finish_connect(pid_t pid)
399 int ret;
401 for (;;) {
402 ret = waitpid(pid, NULL, 0);
403 if (!ret)
404 break;
405 if (errno != EINTR)
406 break;
408 return ret;