Replace zero-length array decls with [].
[git/git.git] / connect.c
CommitLineData
f7192598 1#include "cache.h"
41cb7488 2#include "pkt-line.h"
b10d0ec7 3#include "quote.h"
f7192598 4#include <sys/wait.h>
2386d658
LT
5#include <sys/socket.h>
6#include <netinet/in.h>
7#include <arpa/inet.h>
8#include <netdb.h>
f7192598 9
d1c133f5
LT
10/*
11 * Read all the refs from the other end
12 */
13struct ref **get_remote_heads(int in, struct ref **list, int nr_match, char **match)
14{
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;
22
23 len = packet_read_line(in, buffer, sizeof(buffer));
24 if (!len)
25 break;
26 if (buffer[len-1] == '\n')
27 buffer[--len] = 0;
28
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;
f88395ac 34 ref = xcalloc(1, sizeof(*ref) + len - 40);
d1c133f5 35 memcpy(ref->old_sha1, old_sha1, 20);
d1c133f5 36 memcpy(ref->name, buffer + 41, len - 40);
d1c133f5
LT
37 *list = ref;
38 list = &ref->next;
39 }
40 return list;
41}
42
41cb7488
LT
43int get_ack(int fd, unsigned char *result_sha1)
44{
45 static char line[1000];
46 int len = packet_read_line(fd, line, sizeof(line));
47
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;
57 }
58 die("git-fetch_pack: expected ACK/NAK, got '%s'", line);
59}
60
013e7c7f
LT
61int path_match(const char *path, int nr, char **match)
62{
63 int i;
64 int pathlen = strlen(path);
65
66 for (i = 0; i < nr; i++) {
67 char *s = match[i];
68 int len = strlen(s);
69
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;
78 }
79 return 0;
80}
81
f88395ac
JH
82struct refspec {
83 char *src;
84 char *dst;
85};
86
87static struct refspec *parse_ref_spec(int nr_refspec, char **refspec)
88{
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;
98 }
99 else
100 dp = sp;
101 rs[i].src = sp;
102 rs[i].dst = dp;
103 }
104 rs[nr_refspec].src = rs[nr_refspec].dst = NULL;
105 return rs;
106}
107
108static int count_refspec_match(const char *pattern,
109 struct ref *refs,
110 struct ref **matched_ref)
111{
112 int match;
113 int patlen = strlen(pattern);
114
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;
125 }
126 return match;
127}
128
129static void link_dst_tail(struct ref *ref, struct ref ***tail)
130{
131 **tail = ref;
132 *tail = &ref->next;
133 **tail = NULL;
134}
135
15e02b37
JH
136static struct ref *try_explicit_object_name(const char *name)
137{
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;
148}
149
f88395ac
JH
150static int match_explicit_refs(struct ref *src, struct ref *dst,
151 struct ref ***dst_tail, struct refspec *rs)
152{
153 int i, errs;
154 for (i = errs = 0; rs[i].src; i++) {
155 struct ref *matched_src, *matched_dst;
156
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:
15e02b37
JH
162 /* The source could be in the get_sha1() format
163 * not a reference name.
164 */
165 matched_src = try_explicit_object_name(rs[i].src);
166 if (matched_src)
167 break;
f88395ac 168 errs = 1;
4ec99bf0
TS
169 error("src refspec %s does not match any.",
170 rs[i].src);
f88395ac
JH
171 break;
172 default:
173 errs = 1;
174 error("src refspec %s matches more than one.",
175 rs[i].src);
176 break;
177 }
178 switch (count_refspec_match(rs[i].dst, dst, &matched_dst)) {
179 case 1:
180 break;
181 case 0:
182 if (!memcmp(rs[i].dst, "refs/", 5)) {
183 int len = strlen(rs[i].dst) + 1;
184 matched_dst = xcalloc(1, sizeof(*dst) + len);
185 memcpy(matched_dst->name, rs[i].dst, len);
186 link_dst_tail(matched_dst, dst_tail);
187 }
188 else if (!strcmp(rs[i].src, rs[i].dst) &&
189 matched_src) {
190 /* pushing "master:master" when
191 * remote does not have master yet.
192 */
4fa1604f 193 int len = strlen(matched_src->name) + 1;
f88395ac
JH
194 matched_dst = xcalloc(1, sizeof(*dst) + len);
195 memcpy(matched_dst->name, matched_src->name,
196 len);
197 link_dst_tail(matched_dst, dst_tail);
198 }
199 else {
200 errs = 1;
201 error("dst refspec %s does not match any "
202 "existing ref on the remote and does "
203 "not start with refs/.", rs[i].dst);
204 }
205 break;
206 default:
207 errs = 1;
208 error("dst refspec %s matches more than one.",
209 rs[i].dst);
210 break;
211 }
212 if (errs)
213 continue;
f88395ac
JH
214 if (matched_dst->peer_ref) {
215 errs = 1;
216 error("dst ref %s receives from more than one src.",
217 matched_dst->name);
218 }
219 else
220 matched_dst->peer_ref = matched_src;
221 }
222 return -errs;
223}
224
225static struct ref *find_ref_by_name(struct ref *list, const char *name)
226{
227 for ( ; list; list = list->next)
228 if (!strcmp(list->name, name))
229 return list;
230 return NULL;
231}
232
233int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
234 int nr_refspec, char **refspec, int all)
235{
236 struct refspec *rs = parse_ref_spec(nr_refspec, refspec);
237
238 if (nr_refspec)
239 return match_explicit_refs(src, dst, dst_tail, rs);
240
241 /* pick the remainder */
242 for ( ; src; src = src->next) {
243 struct ref *dst_peer;
244 if (src->peer_ref)
245 continue;
246 dst_peer = find_ref_by_name(dst, src->name);
635d37af 247 if ((dst_peer && dst_peer->peer_ref) || (!dst_peer && !all))
f88395ac
JH
248 continue;
249 if (!dst_peer) {
f88395ac
JH
250 /* Create a new one and link it */
251 int len = strlen(src->name) + 1;
252 dst_peer = xcalloc(1, sizeof(*dst_peer) + len);
253 memcpy(dst_peer->name, src->name, len);
254 memcpy(dst_peer->new_sha1, src->new_sha1, 20);
255 link_dst_tail(dst_peer, dst_tail);
256 }
257 dst_peer->peer_ref = src;
258 }
259 return 0;
260}
261
2386d658
LT
262enum protocol {
263 PROTO_LOCAL = 1,
264 PROTO_SSH,
265 PROTO_GIT,
266};
267
268static enum protocol get_protocol(const char *name)
269{
270 if (!strcmp(name, "ssh"))
271 return PROTO_SSH;
272 if (!strcmp(name, "git"))
273 return PROTO_GIT;
274 die("I don't handle protocol '%s'", name);
275}
276
5ba88448
YH
277#define STR_(s) # s
278#define STR(s) STR_(s)
2386d658
LT
279
280static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
281{
5ba88448
YH
282 int sockfd = -1;
283 char *colon, *end;
284 char *port = STR(DEFAULT_GIT_PORT);
285 struct addrinfo hints, *ai0, *ai;
286 int gai;
287
288 if (host[0] == '[') {
289 end = strchr(host + 1, ']');
290 if (end) {
291 *end = 0;
292 end++;
293 host++;
294 } else
295 end = host;
296 } else
297 end = host;
298 colon = strchr(end, ':');
299
ce6f8e7e
LT
300 if (colon) {
301 *colon = 0;
5ba88448 302 port = colon + 1;
ce6f8e7e 303 }
5ba88448
YH
304
305 memset(&hints, 0, sizeof(hints));
306 hints.ai_socktype = SOCK_STREAM;
307 hints.ai_protocol = IPPROTO_TCP;
308
309 gai = getaddrinfo(host, port, &hints, &ai);
310 if (gai)
311 die("Unable to look up %s (%s)", host, gai_strerror(gai));
312
313 for (ai0 = ai; ai; ai = ai->ai_next) {
314 sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
315 if (sockfd < 0)
316 continue;
317 if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
318 close(sockfd);
319 sockfd = -1;
320 continue;
2386d658 321 }
5ba88448 322 break;
2386d658
LT
323 }
324
5ba88448 325 freeaddrinfo(ai0);
2386d658 326
2386d658 327 if (sockfd < 0)
5ba88448
YH
328 die("unable to connect a socket (%s)", strerror(errno));
329
2386d658
LT
330 fd[0] = sockfd;
331 fd[1] = sockfd;
332 packet_write(sockfd, "%s %s\n", prog, path);
333 return 0;
334}
335
f7192598
LT
336/*
337 * Yeah, yeah, fixme. Need to pass in the heads etc.
338 */
339int git_connect(int fd[2], char *url, const char *prog)
340{
341 char command[1024];
2386d658 342 char *host, *path;
f7192598
LT
343 char *colon;
344 int pipefd[2][2];
345 pid_t pid;
2386d658 346 enum protocol protocol;
f7192598 347
f7192598
LT
348 host = NULL;
349 path = url;
350 colon = strchr(url, ':');
2386d658 351 protocol = PROTO_LOCAL;
f7192598
LT
352 if (colon) {
353 *colon = 0;
354 host = url;
355 path = colon+1;
2386d658
LT
356 protocol = PROTO_SSH;
357 if (!memcmp(path, "//", 2)) {
358 char *slash = strchr(path + 2, '/');
359 if (slash) {
360 int nr = slash - path - 2;
361 memmove(path, path+2, nr);
362 path[nr] = 0;
363 protocol = get_protocol(url);
364 host = path;
365 path = slash;
366 }
367 }
f7192598 368 }
2386d658
LT
369
370 if (protocol == PROTO_GIT)
371 return git_tcp_connect(fd, prog, host, path);
372
f7192598
LT
373 if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
374 die("unable to create pipe pair for communication");
375 pid = fork();
376 if (!pid) {
b10d0ec7
JH
377 snprintf(command, sizeof(command), "%s %s", prog,
378 sq_quote(path));
f7192598
LT
379 dup2(pipefd[1][0], 0);
380 dup2(pipefd[0][1], 1);
381 close(pipefd[0][0]);
382 close(pipefd[0][1]);
383 close(pipefd[1][0]);
384 close(pipefd[1][1]);
4852f723
MS
385 if (protocol == PROTO_SSH) {
386 const char *ssh = getenv("GIT_SSH") ? : "ssh";
387 const char *ssh_basename = strrchr(ssh, '/');
388 if (!ssh_basename)
389 ssh_basename = ssh;
390 else
391 ssh_basename++;
392 execlp(ssh, ssh_basename, host, command, NULL);
393 }
f7192598
LT
394 else
395 execlp("sh", "sh", "-c", command, NULL);
396 die("exec failed");
397 }
398 fd[0] = pipefd[0][0];
399 fd[1] = pipefd[1][1];
400 close(pipefd[0][1]);
401 close(pipefd[1][0]);
402 return pid;
403}
404
405int finish_connect(pid_t pid)
406{
407 int ret;
408
409 for (;;) {
410 ret = waitpid(pid, NULL, 0);
411 if (!ret)
412 break;
413 if (errno != EINTR)
414 break;
415 }
416 return ret;
417}