6 #include <sys/socket.h>
7 #include <netinet/in.h>
11 static char *server_capabilities = "";
14 * Read all the refs from the other end
16 struct ref **get_remote_heads(int in, struct ref **list,
17 int nr_match, char **match, int ignore_funny)
22 unsigned char old_sha1[20];
23 static char buffer[1000];
27 len = packet_read_line(in, buffer, sizeof(buffer));
30 if (buffer[len-1] == '\n')
33 if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
34 die("protocol error: expected sha/ref, got '%s'", buffer);
37 if (ignore_funny && 45 < len && !memcmp(name, "refs/", 5) &&
38 check_ref_format(name + 5))
41 name_len = strlen(name);
42 if (len != name_len + 41) {
43 if (server_capabilities)
44 free(server_capabilities);
45 server_capabilities = strdup(name + name_len + 1);
48 if (nr_match && !path_match(name, nr_match, match))
50 ref = xcalloc(1, sizeof(*ref) + len - 40);
51 memcpy(ref->old_sha1, old_sha1, 20);
52 memcpy(ref->name, buffer + 41, len - 40);
59 int server_supports(const char *feature)
61 return strstr(feature, server_capabilities) != NULL;
64 int get_ack(int fd, unsigned char *result_sha1)
66 static char line[1000];
67 int len = packet_read_line(fd, line, sizeof(line));
70 die("git-fetch-pack: expected ACK/NAK, got EOF");
71 if (line[len-1] == '\n')
73 if (!strcmp(line, "NAK"))
75 if (!strncmp(line, "ACK ", 3)) {
76 if (!get_sha1_hex(line+4, result_sha1)) {
77 if (strstr(line+45, "continue"))
82 die("git-fetch_pack: expected ACK/NAK, got '%s'", line);
85 int path_match(const char *path, int nr, char **match)
88 int pathlen = strlen(path);
90 for (i = 0; i < nr; i++) {
94 if (!len || len > pathlen)
96 if (memcmp(path + pathlen - len, s, len))
98 if (pathlen > len && path[pathlen - len - 1] != '/')
113 * A:B means fast forward remote B with local A.
114 * +A:B means overwrite remote B with local A.
115 * +A is a shorthand for +A:A.
116 * A is a shorthand for A:A.
118 static struct refspec *parse_ref_spec(int nr_refspec, char **refspec)
121 struct refspec *rs = xcalloc(sizeof(*rs), (nr_refspec + 1));
122 for (i = 0; i < nr_refspec; i++) {
129 ep = strchr(sp, ':');
139 rs[nr_refspec].src = rs[nr_refspec].dst = NULL;
143 static int count_refspec_match(const char *pattern,
145 struct ref **matched_ref)
148 int patlen = strlen(pattern);
150 for (match = 0; refs; refs = refs->next) {
151 char *name = refs->name;
152 int namelen = strlen(name);
153 if (namelen < patlen ||
154 memcmp(name + namelen - patlen, pattern, patlen))
156 if (namelen != patlen && name[namelen - patlen - 1] != '/')
164 static void link_dst_tail(struct ref *ref, struct ref ***tail)
171 static struct ref *try_explicit_object_name(const char *name)
173 unsigned char sha1[20];
176 if (get_sha1(name, sha1))
178 len = strlen(name) + 1;
179 ref = xcalloc(1, sizeof(*ref) + len);
180 memcpy(ref->name, name, len);
181 memcpy(ref->new_sha1, sha1, 20);
185 static int match_explicit_refs(struct ref *src, struct ref *dst,
186 struct ref ***dst_tail, struct refspec *rs)
189 for (i = errs = 0; rs[i].src; i++) {
190 struct ref *matched_src, *matched_dst;
192 matched_src = matched_dst = NULL;
193 switch (count_refspec_match(rs[i].src, src, &matched_src)) {
197 /* The source could be in the get_sha1() format
198 * not a reference name.
200 matched_src = try_explicit_object_name(rs[i].src);
204 error("src refspec %s does not match any.",
209 error("src refspec %s matches more than one.",
213 switch (count_refspec_match(rs[i].dst, dst, &matched_dst)) {
217 if (!memcmp(rs[i].dst, "refs/", 5)) {
218 int len = strlen(rs[i].dst) + 1;
219 matched_dst = xcalloc(1, sizeof(*dst) + len);
220 memcpy(matched_dst->name, rs[i].dst, len);
221 link_dst_tail(matched_dst, dst_tail);
223 else if (!strcmp(rs[i].src, rs[i].dst) &&
225 /* pushing "master:master" when
226 * remote does not have master yet.
228 int len = strlen(matched_src->name) + 1;
229 matched_dst = xcalloc(1, sizeof(*dst) + len);
230 memcpy(matched_dst->name, matched_src->name,
232 link_dst_tail(matched_dst, dst_tail);
236 error("dst refspec %s does not match any "
237 "existing ref on the remote and does "
238 "not start with refs/.", rs[i].dst);
243 error("dst refspec %s matches more than one.",
249 if (matched_dst->peer_ref) {
251 error("dst ref %s receives from more than one src.",
255 matched_dst->peer_ref = matched_src;
256 matched_dst->force = rs[i].force;
262 static struct ref *find_ref_by_name(struct ref *list, const char *name)
264 for ( ; list; list = list->next)
265 if (!strcmp(list->name, name))
270 int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
271 int nr_refspec, char **refspec, int all)
273 struct refspec *rs = parse_ref_spec(nr_refspec, refspec);
276 return match_explicit_refs(src, dst, dst_tail, rs);
278 /* pick the remainder */
279 for ( ; src; src = src->next) {
280 struct ref *dst_peer;
283 dst_peer = find_ref_by_name(dst, src->name);
284 if ((dst_peer && dst_peer->peer_ref) || (!dst_peer && !all))
287 /* Create a new one and link it */
288 int len = strlen(src->name) + 1;
289 dst_peer = xcalloc(1, sizeof(*dst_peer) + len);
290 memcpy(dst_peer->name, src->name, len);
291 memcpy(dst_peer->new_sha1, src->new_sha1, 20);
292 link_dst_tail(dst_peer, dst_tail);
294 dst_peer->peer_ref = src;
305 static enum protocol get_protocol(const char *name)
307 if (!strcmp(name, "ssh"))
309 if (!strcmp(name, "git"))
311 if (!strcmp(name, "git+ssh"))
313 if (!strcmp(name, "ssh+git"))
315 die("I don't handle protocol '%s'", name);
319 #define STR(s) STR_(s)
323 static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
327 char *port = STR(DEFAULT_GIT_PORT);
328 struct addrinfo hints, *ai0, *ai;
331 if (host[0] == '[') {
332 end = strchr(host + 1, ']');
341 colon = strchr(end, ':');
348 memset(&hints, 0, sizeof(hints));
349 hints.ai_socktype = SOCK_STREAM;
350 hints.ai_protocol = IPPROTO_TCP;
352 gai = getaddrinfo(host, port, &hints, &ai);
354 die("Unable to look up %s (%s)", host, gai_strerror(gai));
356 for (ai0 = ai; ai; ai = ai->ai_next) {
357 sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
360 if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
371 die("unable to connect a socket (%s)", strerror(errno));
375 packet_write(sockfd, "%s %s\n", prog, path);
381 static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
385 char *port = STR(DEFAULT_GIT_PORT), *ep;
387 struct sockaddr_in sa;
391 if (host[0] == '[') {
392 end = strchr(host + 1, ']');
401 colon = strchr(end, ':');
409 he = gethostbyname(host);
411 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
412 nport = strtoul(port, &ep, 10);
413 if ( ep == port || *ep ) {
415 struct servent *se = getservbyname(port,"tcp");
417 die("Unknown port %s\n", port);
421 for (ap = he->h_addr_list; *ap; ap++) {
422 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
426 memset(&sa, 0, sizeof sa);
427 sa.sin_family = he->h_addrtype;
428 sa.sin_port = htons(nport);
429 memcpy(&sa.sin_addr, ap, he->h_length);
431 if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
440 die("unable to connect a socket (%s)", strerror(errno));
444 packet_write(sockfd, "%s %s\n", prog, path);
451 * Yeah, yeah, fixme. Need to pass in the heads etc.
453 int git_connect(int fd[2], char *url, const char *prog)
460 enum protocol protocol;
464 colon = strchr(url, ':');
465 protocol = PROTO_LOCAL;
470 protocol = PROTO_SSH;
471 if (!memcmp(path, "//", 2)) {
472 char *slash = strchr(path + 2, '/');
474 int nr = slash - path - 2;
475 memmove(path, path+2, nr);
477 protocol = get_protocol(url);
484 if (protocol == PROTO_GIT)
485 return git_tcp_connect(fd, prog, host, path);
487 if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
488 die("unable to create pipe pair for communication");
491 snprintf(command, sizeof(command), "%s %s", prog,
493 dup2(pipefd[1][0], 0);
494 dup2(pipefd[0][1], 1);
499 if (protocol == PROTO_SSH) {
500 const char *ssh, *ssh_basename;
501 ssh = getenv("GIT_SSH");
502 if (!ssh) ssh = "ssh";
503 ssh_basename = strrchr(ssh, '/');
508 execlp(ssh, ssh_basename, host, command, NULL);
511 execlp("sh", "sh", "-c", command, NULL);
514 fd[0] = pipefd[0][0];
515 fd[1] = pipefd[1][1];
521 int finish_connect(pid_t pid)
526 ret = waitpid(pid, NULL, 0);